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