1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 2019 - 2022, Daniel Stenberg, <[email protected]>, et al.
|
---|
9 | *
|
---|
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.
|
---|
13 | *
|
---|
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.
|
---|
17 | *
|
---|
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
19 | * KIND, either express or implied.
|
---|
20 | *
|
---|
21 | * SPDX-License-Identifier: curl
|
---|
22 | *
|
---|
23 | ***************************************************************************/
|
---|
24 |
|
---|
25 | #include "curl_setup.h"
|
---|
26 | #include "socketpair.h"
|
---|
27 |
|
---|
28 | #if !defined(HAVE_SOCKETPAIR) && !defined(CURL_DISABLE_SOCKETPAIR)
|
---|
29 | #ifdef WIN32
|
---|
30 | /*
|
---|
31 | * This is a socketpair() implementation for Windows.
|
---|
32 | */
|
---|
33 | #include <string.h>
|
---|
34 | #include <winsock2.h>
|
---|
35 | #include <ws2tcpip.h>
|
---|
36 | #include <windows.h>
|
---|
37 | #include <io.h>
|
---|
38 | #else
|
---|
39 | #ifdef HAVE_NETDB_H
|
---|
40 | #include <netdb.h>
|
---|
41 | #endif
|
---|
42 | #ifdef HAVE_NETINET_IN_H
|
---|
43 | #include <netinet/in.h> /* IPPROTO_TCP */
|
---|
44 | #endif
|
---|
45 | #ifdef HAVE_ARPA_INET_H
|
---|
46 | #include <arpa/inet.h>
|
---|
47 | #endif
|
---|
48 | #ifndef INADDR_LOOPBACK
|
---|
49 | #define INADDR_LOOPBACK 0x7f000001
|
---|
50 | #endif /* !INADDR_LOOPBACK */
|
---|
51 | #endif /* !WIN32 */
|
---|
52 |
|
---|
53 | #include "nonblock.h" /* for curlx_nonblock */
|
---|
54 | #include "timeval.h" /* needed before select.h */
|
---|
55 | #include "select.h" /* for Curl_poll */
|
---|
56 |
|
---|
57 | /* The last 3 #include files should be in this order */
|
---|
58 | #include "curl_printf.h"
|
---|
59 | #include "curl_memory.h"
|
---|
60 | #include "memdebug.h"
|
---|
61 |
|
---|
62 | int Curl_socketpair(int domain, int type, int protocol,
|
---|
63 | curl_socket_t socks[2])
|
---|
64 | {
|
---|
65 | union {
|
---|
66 | struct sockaddr_in inaddr;
|
---|
67 | struct sockaddr addr;
|
---|
68 | } a, a2;
|
---|
69 | curl_socket_t listener;
|
---|
70 | curl_socklen_t addrlen = sizeof(a.inaddr);
|
---|
71 | int reuse = 1;
|
---|
72 | struct pollfd pfd[1];
|
---|
73 | (void)domain;
|
---|
74 | (void)type;
|
---|
75 | (void)protocol;
|
---|
76 |
|
---|
77 | listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
78 | if(listener == CURL_SOCKET_BAD)
|
---|
79 | return -1;
|
---|
80 |
|
---|
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;
|
---|
85 |
|
---|
86 | socks[0] = socks[1] = CURL_SOCKET_BAD;
|
---|
87 |
|
---|
88 | if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
|
---|
89 | (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1)
|
---|
90 | goto error;
|
---|
91 | if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1)
|
---|
92 | goto error;
|
---|
93 | if(getsockname(listener, &a.addr, &addrlen) == -1 ||
|
---|
94 | addrlen < (int)sizeof(a.inaddr))
|
---|
95 | goto error;
|
---|
96 | if(listen(listener, 1) == -1)
|
---|
97 | goto error;
|
---|
98 | socks[0] = socket(AF_INET, SOCK_STREAM, 0);
|
---|
99 | if(socks[0] == CURL_SOCKET_BAD)
|
---|
100 | goto error;
|
---|
101 | if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1)
|
---|
102 | goto error;
|
---|
103 |
|
---|
104 | /* use non-blocking accept to make sure we don't block forever */
|
---|
105 | if(curlx_nonblock(listener, TRUE) < 0)
|
---|
106 | goto error;
|
---|
107 | pfd[0].fd = listener;
|
---|
108 | pfd[0].events = POLLIN;
|
---|
109 | pfd[0].revents = 0;
|
---|
110 | (void)Curl_poll(pfd, 1, 10*1000); /* 10 seconds */
|
---|
111 | socks[1] = accept(listener, NULL, NULL);
|
---|
112 | if(socks[1] == CURL_SOCKET_BAD)
|
---|
113 | goto error;
|
---|
114 |
|
---|
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))
|
---|
119 | goto error;
|
---|
120 | addrlen = sizeof(a2.inaddr);
|
---|
121 | if(getpeername(socks[1], &a2.addr, &addrlen) == -1 ||
|
---|
122 | addrlen < (int)sizeof(a2.inaddr))
|
---|
123 | goto error;
|
---|
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)
|
---|
127 | goto error;
|
---|
128 |
|
---|
129 | sclose(listener);
|
---|
130 | return 0;
|
---|
131 |
|
---|
132 | error:
|
---|
133 | sclose(listener);
|
---|
134 | sclose(socks[0]);
|
---|
135 | sclose(socks[1]);
|
---|
136 | return -1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | #endif /* ! HAVE_SOCKETPAIR */
|
---|