4 This file is part of PulseAudio.
6 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
32 #include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
44 static int set_block(int fd, int blocking) {
51 if ((v = fcntl(fd, F_GETFL)) < 0)
59 if (fcntl(fd, F_SETFL, v) < 0)
64 #elif defined(OS_IS_WIN32)
70 if (ioctlsocket(fd, FIONBIO, &arg) < 0)
82 int pipe(int filedes[2]) {
84 struct sockaddr_in addr, peer;
91 listener = socket(PF_INET, SOCK_STREAM, 0);
95 filedes[0] = socket(PF_INET, SOCK_STREAM, 0);
99 filedes[1] = socket(PF_INET, SOCK_STREAM, 0);
103 /* Make non-blocking so that connect() won't block */
104 if (set_block(filedes[0], 0) < 0)
107 addr.sin_family = AF_INET;
109 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
111 if (bind(listener, (struct sockaddr*)&addr, sizeof(addr)) < 0)
114 if (listen(listener, 1) < 0)
118 if (getsockname(listener, (struct sockaddr*)&addr, &len) < 0)
121 if (connect(filedes[0], (struct sockaddr*)&addr, sizeof(addr)) < 0) {
123 if (WSAGetLastError() != EWOULDBLOCK)
125 if (errno != EINPROGRESS)
131 filedes[1] = accept(listener, (struct sockaddr*)&peer, &len);
135 /* Restore blocking */
136 if (set_block(filedes[0], 1) < 0)
140 if (getsockname(filedes[0], (struct sockaddr*)&addr, &len) < 0)
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))
162 #endif /* HAVE_PIPE */