1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2019 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
26 #include "socketpair.h"
28 #if !defined(HAVE_SOCKETPAIR) && !defined(CURL_DISABLE_SOCKETPAIR)
31 * This is a socketpair() implementation for Windows.
42 #ifdef HAVE_NETINET_IN_H
43 #include <netinet/in.h> /* IPPROTO_TCP */
45 #ifdef HAVE_ARPA_INET_H
46 #include <arpa/inet.h>
48 #ifndef INADDR_LOOPBACK
49 #define INADDR_LOOPBACK 0x7f000001
50 #endif /* !INADDR_LOOPBACK */
53 #include "nonblock.h" /* for curlx_nonblock */
54 #include "timeval.h" /* needed before select.h */
55 #include "select.h" /* for Curl_poll */
57 /* The last 3 #include files should be in this order */
58 #include "curl_printf.h"
59 #include "curl_memory.h"
62 int Curl_socketpair(int domain, int type, int protocol,
63 curl_socket_t socks[2])
66 struct sockaddr_in inaddr;
69 curl_socket_t listener;
70 curl_socklen_t addrlen = sizeof(a.inaddr);
77 listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
78 if(listener == CURL_SOCKET_BAD)
81 memset(&a, 0, sizeof(a));
82 a.inaddr.sin_family = AF_INET;
83 a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
84 a.inaddr.sin_port = 0;
86 socks[0] = socks[1] = CURL_SOCKET_BAD;
88 if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
89 (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1)
91 if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1)
93 if(getsockname(listener, &a.addr, &addrlen) == -1 ||
94 addrlen < (int)sizeof(a.inaddr))
96 if(listen(listener, 1) == -1)
98 socks[0] = socket(AF_INET, SOCK_STREAM, 0);
99 if(socks[0] == CURL_SOCKET_BAD)
101 if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1)
104 /* use non-blocking accept to make sure we don't block forever */
105 if(curlx_nonblock(listener, TRUE) < 0)
107 pfd[0].fd = listener;
108 pfd[0].events = POLLIN;
110 (void)Curl_poll(pfd, 1, 10*1000); /* 10 seconds */
111 socks[1] = accept(listener, NULL, NULL);
112 if(socks[1] == CURL_SOCKET_BAD)
115 /* verify that nothing else connected */
116 addrlen = sizeof(a.inaddr);
117 if(getsockname(socks[0], &a.addr, &addrlen) == -1 ||
118 addrlen < (int)sizeof(a.inaddr))
120 addrlen = sizeof(a2.inaddr);
121 if(getpeername(socks[1], &a2.addr, &addrlen) == -1 ||
122 addrlen < (int)sizeof(a2.inaddr))
124 if(a.inaddr.sin_family != a2.inaddr.sin_family ||
125 a.inaddr.sin_addr.s_addr != a2.inaddr.sin_addr.s_addr ||
126 a.inaddr.sin_port != a2.inaddr.sin_port)
139 #endif /* ! HAVE_SOCKETPAIR */