summaryrefslogtreecommitdiffstats
path: root/src/polypcore/pipe.c
blob: eef6d5335b055d4e0b77d1efb0187bf24042dac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* $Id$ */

/***
   This file is part of polypaudio.

   polypaudio is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as published
   by the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   polypaudio is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with polypaudio; If not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
   USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/types.h>

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif

#include "winsock.h"

#include "pipe.h"

#ifndef HAVE_PIPE

static int set_block(int fd, int blocking) {
#ifdef O_NONBLOCK

    int v;

    assert(fd >= 0);

    if ((v = fcntl(fd, F_GETFL)) < 0)
        return -1;

    if (blocking)
        v &= ~O_NONBLOCK;
    else
        v |= O_NONBLOCK;

    if (fcntl(fd, F_SETFL, v) < 0)
        return -1;

    return 0;

#elif defined(OS_IS_WIN32)

    u_long arg;

    arg = !blocking;

    if (ioctlsocket(fd, FIONBIO, &arg) < 0)
        return -1;

    return 0;

#else

    return -1;

#endif
}

int pipe(int filedes[2]) {
    int listener;
    struct sockaddr_in addr, peer;
    socklen_t len;

    listener = -1;
    filedes[0] = -1;
    filedes[1] = -1;

    listener = socket(PF_INET, SOCK_STREAM, 0);
    if (listener < 0)
        goto error;

    filedes[0] = socket(PF_INET, SOCK_STREAM, 0);
    if (filedes[0] < 0)
        goto error;

    filedes[1] = socket(PF_INET, SOCK_STREAM, 0);
    if (filedes[1] < 0)
        goto error;

    /* Make non-blocking so that connect() won't block */
    if (set_block(filedes[0], 0) < 0)
        goto error;

    addr.sin_family = AF_INET;
    addr.sin_port = 0;
    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

    if (bind(listener, (struct sockaddr*)&addr, sizeof(addr)) < 0)
        goto error;

    if (listen(listener, 1) < 0)
        goto error;

    len = sizeof(addr);
    if (getsockname(listener, (struct sockaddr*)&addr, &len) < 0)
        goto error;

    if (connect(filedes[0], (struct sockaddr*)&addr, sizeof(addr)) < 0) {
#ifdef OS_IS_WIN32
        if (WSAGetLastError() != EWOULDBLOCK)
#else
        if (errno != EINPROGRESS)
#endif
            goto error;
    }

    len = sizeof(peer);
    filedes[1] = accept(listener, (struct sockaddr*)&peer, &len);
    if (filedes[1] < 0)
        goto error;

    /* Restore blocking */
    if (set_block(filedes[0], 1) < 0)
        goto error;

    len = sizeof(addr);
    if (getsockname(filedes[0], (struct sockaddr*)&addr, &len) < 0)
        goto error;

    /* Check that someone else didn't steal the connection */
    if ((addr.sin_port != peer.sin_port) || (addr.sin_addr.s_addr != peer.sin_addr.s_addr))
        goto error;

    close(listener);

    return 0;

error:
	if (listener >= 0)
		close(listener);
	if (filedes[0] >= 0)
		close(filedes[0]);
	if (filedes[1] >= 0)
		close(filedes[0]);

	return -1;
}

#endif /* HAVE_PIPE */