]> code.delx.au - pulseaudio/blob - src/pulsecore/pipe.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / pipe.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31
32 #include <sys/types.h>
33
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37
38 #include "winsock.h"
39
40 #include "pipe.h"
41
42 #ifndef HAVE_PIPE
43
44 static int set_block(int fd, int blocking) {
45 #ifdef O_NONBLOCK
46
47 int v;
48
49 assert(fd >= 0);
50
51 if ((v = fcntl(fd, F_GETFL)) < 0)
52 return -1;
53
54 if (blocking)
55 v &= ~O_NONBLOCK;
56 else
57 v |= O_NONBLOCK;
58
59 if (fcntl(fd, F_SETFL, v) < 0)
60 return -1;
61
62 return 0;
63
64 #elif defined(OS_IS_WIN32)
65
66 u_long arg;
67
68 arg = !blocking;
69
70 if (ioctlsocket(fd, FIONBIO, &arg) < 0)
71 return -1;
72
73 return 0;
74
75 #else
76
77 return -1;
78
79 #endif
80 }
81
82 int pipe(int filedes[2]) {
83 int listener;
84 struct sockaddr_in addr, peer;
85 socklen_t len;
86
87 listener = -1;
88 filedes[0] = -1;
89 filedes[1] = -1;
90
91 listener = socket(PF_INET, SOCK_STREAM, 0);
92 if (listener < 0)
93 goto error;
94
95 filedes[0] = socket(PF_INET, SOCK_STREAM, 0);
96 if (filedes[0] < 0)
97 goto error;
98
99 filedes[1] = socket(PF_INET, SOCK_STREAM, 0);
100 if (filedes[1] < 0)
101 goto error;
102
103 /* Make non-blocking so that connect() won't block */
104 if (set_block(filedes[0], 0) < 0)
105 goto error;
106
107 addr.sin_family = AF_INET;
108 addr.sin_port = 0;
109 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
110
111 if (bind(listener, (struct sockaddr*)&addr, sizeof(addr)) < 0)
112 goto error;
113
114 if (listen(listener, 1) < 0)
115 goto error;
116
117 len = sizeof(addr);
118 if (getsockname(listener, (struct sockaddr*)&addr, &len) < 0)
119 goto error;
120
121 if (connect(filedes[0], (struct sockaddr*)&addr, sizeof(addr)) < 0) {
122 #ifdef OS_IS_WIN32
123 if (WSAGetLastError() != EWOULDBLOCK)
124 #else
125 if (errno != EINPROGRESS)
126 #endif
127 goto error;
128 }
129
130 len = sizeof(peer);
131 filedes[1] = accept(listener, (struct sockaddr*)&peer, &len);
132 if (filedes[1] < 0)
133 goto error;
134
135 /* Restore blocking */
136 if (set_block(filedes[0], 1) < 0)
137 goto error;
138
139 len = sizeof(addr);
140 if (getsockname(filedes[0], (struct sockaddr*)&addr, &len) < 0)
141 goto error;
142
143 /* Check that someone else didn't steal the connection */
144 if ((addr.sin_port != peer.sin_port) || (addr.sin_addr.s_addr != peer.sin_addr.s_addr))
145 goto error;
146
147 pa_close(listener);
148
149 return 0;
150
151 error:
152 if (listener >= 0)
153 pa_close(listener);
154 if (filedes[0] >= 0)
155 pa_close(filedes[0]);
156 if (filedes[1] >= 0)
157 pa_close(filedes[0]);
158
159 return -1;
160 }
161
162 #endif /* HAVE_PIPE */