2 This file is part of PulseAudio.
4 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
42 static int set_block(int fd, int blocking) {
49 if ((v = fcntl(fd, F_GETFL)) < 0)
57 if (fcntl(fd, F_SETFL, v) < 0)
62 #elif defined(OS_IS_WIN32)
68 if (ioctlsocket(fd, FIONBIO, &arg) < 0)
80 int pipe(int filedes[2]) {
82 struct sockaddr_in addr, peer;
89 listener = socket(PF_INET, SOCK_STREAM, 0);
93 filedes[0] = socket(PF_INET, SOCK_STREAM, 0);
97 filedes[1] = socket(PF_INET, SOCK_STREAM, 0);
101 /* Make non-blocking so that connect() won't block */
102 if (set_block(filedes[0], 0) < 0)
105 addr.sin_family = AF_INET;
107 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
109 if (bind(listener, (struct sockaddr*)&addr, sizeof(addr)) < 0)
112 if (listen(listener, 1) < 0)
116 if (getsockname(listener, (struct sockaddr*)&addr, &len) < 0)
119 if (connect(filedes[0], (struct sockaddr*)&addr, sizeof(addr)) < 0) {
121 if (WSAGetLastError() != EWOULDBLOCK)
123 if (errno != EINPROGRESS)
129 filedes[1] = accept(listener, (struct sockaddr*)&peer, &len);
133 /* Restore blocking */
134 if (set_block(filedes[0], 1) < 0)
138 if (getsockname(filedes[0], (struct sockaddr*)&addr, &len) < 0)
141 /* Check that someone else didn't steal the connection */
142 if ((addr.sin_port != peer.sin_port) || (addr.sin_addr.s_addr != peer.sin_addr.s_addr))
153 pa_close(filedes[0]);
155 pa_close(filedes[0]);
160 #endif /* HAVE_PIPE */