1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2019 - 2021, 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 ***************************************************************************/
23 #include "curl_setup.h"
24 #include "socketpair.h"
26 #if !defined(HAVE_SOCKETPAIR) && !defined(CURL_DISABLE_SOCKETPAIR)
29 * This is a socketpair() implementation for Windows.
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h> /* IPPROTO_TCP */
43 #ifdef HAVE_ARPA_INET_H
44 #include <arpa/inet.h>
46 #ifndef INADDR_LOOPBACK
47 #define INADDR_LOOPBACK 0x7f000001
48 #endif /* !INADDR_LOOPBACK */
51 #include "nonblock.h" /* for curlx_nonblock */
52 #include "timeval.h" /* needed before select.h */
53 #include "select.h" /* for Curl_poll */
55 /* The last 3 #include files should be in this order */
56 #include "curl_printf.h"
57 #include "curl_memory.h"
60 int Curl_socketpair(int domain, int type, int protocol,
61 curl_socket_t socks[2])
64 struct sockaddr_in inaddr;
67 curl_socket_t listener;
68 curl_socklen_t addrlen = sizeof(a.inaddr);
75 listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
76 if(listener == CURL_SOCKET_BAD)
79 memset(&a, 0, sizeof(a));
80 a.inaddr.sin_family = AF_INET;
81 a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
82 a.inaddr.sin_port = 0;
84 socks[0] = socks[1] = CURL_SOCKET_BAD;
86 if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
87 (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1)
89 if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1)
91 if(getsockname(listener, &a.addr, &addrlen) == -1 ||
92 addrlen < (int)sizeof(a.inaddr))
94 if(listen(listener, 1) == -1)
96 socks[0] = socket(AF_INET, SOCK_STREAM, 0);
97 if(socks[0] == CURL_SOCKET_BAD)
99 if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1)
102 /* use non-blocking accept to make sure we don't block forever */
103 if(curlx_nonblock(listener, TRUE) < 0)
105 pfd[0].fd = listener;
106 pfd[0].events = POLLIN;
108 (void)Curl_poll(pfd, 1, 10*1000); /* 10 seconds */
109 socks[1] = accept(listener, NULL, NULL);
110 if(socks[1] == CURL_SOCKET_BAD)
113 /* verify that nothing else connected */
114 addrlen = sizeof(a.inaddr);
115 if(getsockname(socks[0], &a.addr, &addrlen) == -1 ||
116 addrlen < (int)sizeof(a.inaddr))
118 addrlen = sizeof(a2.inaddr);
119 if(getpeername(socks[1], &a2.addr, &addrlen) == -1 ||
120 addrlen < (int)sizeof(a2.inaddr))
122 if(a.inaddr.sin_family != a2.inaddr.sin_family ||
123 a.inaddr.sin_addr.s_addr != a2.inaddr.sin_addr.s_addr ||
124 a.inaddr.sin_port != a2.inaddr.sin_port)
137 #endif /* ! HAVE_SOCKETPAIR */