]> code.delx.au - pulseaudio/blob - src/pulsecore/pipe.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[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 #include <pulsecore/core-util.h>
34
35 #include "pipe.h"
36
37 #ifndef HAVE_PIPE
38
39 static int set_block(int fd, int blocking) {
40 #ifdef O_NONBLOCK
41
42 int v;
43
44 assert(fd >= 0);
45
46 if ((v = fcntl(fd, F_GETFL)) < 0)
47 return -1;
48
49 if (blocking)
50 v &= ~O_NONBLOCK;
51 else
52 v |= O_NONBLOCK;
53
54 if (fcntl(fd, F_SETFL, v) < 0)
55 return -1;
56
57 return 0;
58
59 #elif defined(OS_IS_WIN32)
60
61 u_long arg;
62
63 arg = !blocking;
64
65 if (ioctlsocket(fd, FIONBIO, &arg) < 0)
66 return -1;
67
68 return 0;
69
70 #else
71
72 return -1;
73
74 #endif
75 }
76
77 int pipe(int filedes[2]) {
78 int listener;
79 struct sockaddr_in addr, peer;
80 socklen_t len;
81
82 listener = -1;
83 filedes[0] = -1;
84 filedes[1] = -1;
85
86 listener = socket(PF_INET, SOCK_STREAM, 0);
87 if (listener < 0)
88 goto error;
89
90 filedes[0] = socket(PF_INET, SOCK_STREAM, 0);
91 if (filedes[0] < 0)
92 goto error;
93
94 filedes[1] = socket(PF_INET, SOCK_STREAM, 0);
95 if (filedes[1] < 0)
96 goto error;
97
98 /* Make non-blocking so that connect() won't block */
99 if (set_block(filedes[0], 0) < 0)
100 goto error;
101
102 addr.sin_family = AF_INET;
103 addr.sin_port = 0;
104 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
105
106 if (bind(listener, (struct sockaddr*)&addr, sizeof(addr)) < 0)
107 goto error;
108
109 if (listen(listener, 1) < 0)
110 goto error;
111
112 len = sizeof(addr);
113 if (getsockname(listener, (struct sockaddr*)&addr, &len) < 0)
114 goto error;
115
116 if (connect(filedes[0], (struct sockaddr*)&addr, sizeof(addr)) < 0) {
117 #ifdef OS_IS_WIN32
118 if (WSAGetLastError() != EWOULDBLOCK)
119 #else
120 if (errno != EINPROGRESS)
121 #endif
122 goto error;
123 }
124
125 len = sizeof(peer);
126 filedes[1] = accept(listener, (struct sockaddr*)&peer, &len);
127 if (filedes[1] < 0)
128 goto error;
129
130 /* Restore blocking */
131 if (set_block(filedes[0], 1) < 0)
132 goto error;
133
134 len = sizeof(addr);
135 if (getsockname(filedes[0], (struct sockaddr*)&addr, &len) < 0)
136 goto error;
137
138 /* Check that someone else didn't steal the connection */
139 if ((addr.sin_port != peer.sin_port) || (addr.sin_addr.s_addr != peer.sin_addr.s_addr))
140 goto error;
141
142 pa_close(listener);
143
144 return 0;
145
146 error:
147 if (listener >= 0)
148 pa_close(listener);
149 if (filedes[0] >= 0)
150 pa_close(filedes[0]);
151 if (filedes[1] >= 0)
152 pa_close(filedes[0]);
153
154 return -1;
155 }
156
157 #endif /* HAVE_PIPE */