/src/curl/lib/socketpair.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 | | #include "curl_setup.h" |
25 | | |
26 | | #include "socketpair.h" |
27 | | #include "urldata.h" |
28 | | #include "rand.h" |
29 | | #include "curlx/nonblock.h" |
30 | | |
31 | | #ifndef CURL_DISABLE_SOCKETPAIR |
32 | | |
33 | | /* choose implementation */ |
34 | | #ifdef USE_EVENTFD |
35 | | |
36 | | #include <sys/eventfd.h> |
37 | | |
38 | | static int wakeup_eventfd(curl_socket_t socks[2], bool nonblocking) |
39 | 42.0k | { |
40 | 42.0k | int efd = eventfd(0, nonblocking ? EFD_CLOEXEC | EFD_NONBLOCK : EFD_CLOEXEC); |
41 | 42.0k | if(efd == -1) { |
42 | 0 | socks[0] = socks[1] = CURL_SOCKET_BAD; |
43 | 0 | return -1; |
44 | 0 | } |
45 | 42.0k | socks[0] = socks[1] = efd; |
46 | 42.0k | return 0; |
47 | 42.0k | } |
48 | | |
49 | | #elif defined(HAVE_PIPE) |
50 | | |
51 | | #ifdef HAVE_FCNTL |
52 | | #include <fcntl.h> |
53 | | #endif |
54 | | |
55 | | static int wakeup_pipe(curl_socket_t socks[2], bool nonblocking) |
56 | | { |
57 | | #ifdef HAVE_PIPE2 |
58 | | return pipe2(socks, (nonblocking ? O_NONBLOCK : 0) | O_CLOEXEC) ? -1 : 0; |
59 | | #else |
60 | | if(pipe(socks)) |
61 | | return -1; |
62 | | #ifdef HAVE_FCNTL |
63 | | if(fcntl(socks[0], F_SETFD, FD_CLOEXEC) || |
64 | | fcntl(socks[1], F_SETFD, FD_CLOEXEC)) { |
65 | | sclose(socks[0]); |
66 | | sclose(socks[1]); |
67 | | socks[0] = socks[1] = CURL_SOCKET_BAD; |
68 | | return -1; |
69 | | } |
70 | | #endif |
71 | | if(nonblocking) { |
72 | | if(curlx_nonblock(socks[0], TRUE) < 0 || |
73 | | curlx_nonblock(socks[1], TRUE) < 0) { |
74 | | sclose(socks[0]); |
75 | | sclose(socks[1]); |
76 | | socks[0] = socks[1] = CURL_SOCKET_BAD; |
77 | | return -1; |
78 | | } |
79 | | } |
80 | | return 0; |
81 | | #endif /* HAVE_PIPE2 */ |
82 | | } |
83 | | |
84 | | #elif defined(HAVE_SOCKETPAIR) /* !USE_EVENTFD && !HAVE_PIPE */ |
85 | | |
86 | | #ifndef USE_UNIX_SOCKETS |
87 | | #error "unsupported Unix domain and socketpair build combo" |
88 | | #endif |
89 | | |
90 | | static int wakeup_socketpair(curl_socket_t socks[2], bool nonblocking) |
91 | | { |
92 | | int type = SOCK_STREAM; |
93 | | #ifdef SOCK_CLOEXEC |
94 | | type |= SOCK_CLOEXEC; |
95 | | #endif |
96 | | #ifdef CURL_USE_SOCK_NONBLOCK |
97 | | if(nonblocking) |
98 | | type |= SOCK_NONBLOCK; |
99 | | #endif |
100 | | |
101 | | if(CURL_SOCKETPAIR(AF_UNIX, type, 0, socks)) |
102 | | return -1; |
103 | | #ifndef CURL_USE_SOCK_NONBLOCK |
104 | | if(nonblocking) { |
105 | | if(curlx_nonblock(socks[0], TRUE) < 0 || |
106 | | curlx_nonblock(socks[1], TRUE) < 0) { |
107 | | sclose(socks[0]); |
108 | | sclose(socks[1]); |
109 | | socks[0] = socks[1] = CURL_SOCKET_BAD; |
110 | | return -1; |
111 | | } |
112 | | } |
113 | | #endif |
114 | | #ifdef USE_SO_NOSIGPIPE |
115 | | if(Curl_sock_nosigpipe(socks[1]) < 0) { |
116 | | sclose(socks[0]); |
117 | | sclose(socks[1]); |
118 | | socks[0] = socks[1] = CURL_SOCKET_BAD; |
119 | | return -1; |
120 | | } |
121 | | #endif /* USE_SO_NOSIGPIPE */ |
122 | | |
123 | | return 0; |
124 | | } |
125 | | |
126 | | #else /* !USE_EVENTFD && !HAVE_PIPE && !HAVE_SOCKETPAIR */ |
127 | | |
128 | | #ifdef HAVE_NETDB_H |
129 | | #include <netdb.h> |
130 | | #endif |
131 | | #ifdef HAVE_NETINET_IN_H |
132 | | #include <netinet/in.h> /* for IPPROTO_TCP */ |
133 | | #endif |
134 | | #ifdef HAVE_ARPA_INET_H |
135 | | #include <arpa/inet.h> |
136 | | #endif |
137 | | |
138 | | #ifndef INADDR_LOOPBACK |
139 | | #define INADDR_LOOPBACK 0x7f000001 |
140 | | #endif |
141 | | |
142 | | #include "select.h" /* for Curl_poll */ |
143 | | |
144 | | static int wakeup_inet(curl_socket_t socks[2], bool nonblocking) |
145 | | { |
146 | | union { |
147 | | struct sockaddr_in inaddr; |
148 | | struct sockaddr addr; |
149 | | } a; |
150 | | curl_socket_t listener; |
151 | | curl_socklen_t addrlen = sizeof(a.inaddr); |
152 | | int reuse = 1; |
153 | | struct pollfd pfd[1]; |
154 | | |
155 | | listener = CURL_SOCKET(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
156 | | if(listener == CURL_SOCKET_BAD) |
157 | | return -1; |
158 | | |
159 | | memset(&a, 0, sizeof(a)); |
160 | | a.inaddr.sin_family = AF_INET; |
161 | | a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
162 | | a.inaddr.sin_port = 0; |
163 | | |
164 | | socks[0] = socks[1] = CURL_SOCKET_BAD; |
165 | | |
166 | | #if defined(_WIN32) || defined(__CYGWIN__) |
167 | | /* do not set SO_REUSEADDR on Windows */ |
168 | | (void)reuse; |
169 | | #ifdef SO_EXCLUSIVEADDRUSE |
170 | | { |
171 | | int exclusive = 1; |
172 | | if(setsockopt(listener, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, |
173 | | (char *)&exclusive, (curl_socklen_t)sizeof(exclusive)) == -1) |
174 | | goto error; |
175 | | } |
176 | | #endif |
177 | | #else |
178 | | if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, |
179 | | (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1) |
180 | | goto error; |
181 | | #endif |
182 | | if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1) |
183 | | goto error; |
184 | | if(getsockname(listener, &a.addr, &addrlen) == -1 || |
185 | | addrlen < (int)sizeof(a.inaddr)) |
186 | | goto error; |
187 | | if(listen(listener, 1) == -1) |
188 | | goto error; |
189 | | socks[0] = CURL_SOCKET(AF_INET, SOCK_STREAM, 0); |
190 | | if(socks[0] == CURL_SOCKET_BAD) |
191 | | goto error; |
192 | | if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1) |
193 | | goto error; |
194 | | |
195 | | /* use non-blocking accept to make sure we do not block forever */ |
196 | | if(curlx_nonblock(listener, TRUE) < 0) |
197 | | goto error; |
198 | | pfd[0].fd = listener; |
199 | | pfd[0].events = POLLIN; |
200 | | pfd[0].revents = 0; |
201 | | (void)Curl_poll(pfd, 1, 1000); /* one second */ |
202 | | socks[1] = CURL_ACCEPT(listener, NULL, NULL); |
203 | | if(socks[1] == CURL_SOCKET_BAD) |
204 | | goto error; |
205 | | else { |
206 | | struct curltime start = curlx_now(); |
207 | | char rnd[9]; |
208 | | char check[sizeof(rnd)]; |
209 | | char *p = &check[0]; |
210 | | size_t s = sizeof(check); |
211 | | |
212 | | if(Curl_rand(NULL, (unsigned char *)rnd, sizeof(rnd))) |
213 | | goto error; |
214 | | |
215 | | /* write data to the socket */ |
216 | | swrite(socks[0], rnd, sizeof(rnd)); |
217 | | /* verify that we read the correct data */ |
218 | | do { |
219 | | ssize_t nread; |
220 | | |
221 | | /* Do not block forever */ |
222 | | if(curlx_timediff_ms(curlx_now(), start) > (60 * 1000)) |
223 | | goto error; |
224 | | |
225 | | pfd[0].fd = socks[1]; |
226 | | pfd[0].events = POLLIN; |
227 | | pfd[0].revents = 0; |
228 | | (void)Curl_poll(pfd, 1, 1000); /* one second */ |
229 | | |
230 | | nread = sread(socks[1], p, s); |
231 | | if(!nread) { /* unexpected close before getting complete `check` */ |
232 | | goto error; |
233 | | } |
234 | | else if(nread == -1) { /* error or close */ |
235 | | int sockerr = SOCKERRNO; |
236 | | if(SOCK_EAGAIN(sockerr) |
237 | | #ifndef USE_WINSOCK |
238 | | || (sockerr == SOCKEINTR) || (sockerr == SOCKEINPROGRESS) |
239 | | #endif |
240 | | ) { |
241 | | continue; |
242 | | } |
243 | | goto error; |
244 | | } |
245 | | s -= nread; |
246 | | if(s) { |
247 | | p += nread; |
248 | | continue; |
249 | | } |
250 | | if(memcmp(rnd, check, sizeof(check))) |
251 | | goto error; |
252 | | break; |
253 | | } while(1); |
254 | | } |
255 | | |
256 | | if(nonblocking) |
257 | | if(curlx_nonblock(socks[0], TRUE) < 0 || |
258 | | curlx_nonblock(socks[1], TRUE) < 0) |
259 | | goto error; |
260 | | #ifdef USE_SO_NOSIGPIPE |
261 | | if(Curl_sock_nosigpipe(socks[1]) < 0) |
262 | | goto error; |
263 | | #endif |
264 | | sclose(listener); |
265 | | return 0; |
266 | | |
267 | | error: |
268 | | sclose(listener); |
269 | | sclose(socks[0]); |
270 | | sclose(socks[1]); |
271 | | socks[0] = socks[1] = CURL_SOCKET_BAD; |
272 | | return -1; |
273 | | } |
274 | | |
275 | | #endif /* choose implementation */ |
276 | | |
277 | | int Curl_wakeup_init(curl_socket_t socks[2], bool nonblocking) |
278 | 42.0k | { |
279 | 42.0k | #ifdef USE_EVENTFD |
280 | 42.0k | return wakeup_eventfd(socks, nonblocking); |
281 | | #elif defined(HAVE_PIPE) |
282 | | return wakeup_pipe(socks, nonblocking); |
283 | | #elif defined(HAVE_SOCKETPAIR) |
284 | | return wakeup_socketpair(socks, nonblocking); |
285 | | #else |
286 | | return wakeup_inet(socks, nonblocking); |
287 | | #endif |
288 | 42.0k | } |
289 | | |
290 | | #if defined(USE_EVENTFD) || defined(HAVE_PIPE) |
291 | | |
292 | 15 | #define wakeup_write write |
293 | 88.3k | #define wakeup_read read |
294 | 42.0k | #define wakeup_close close |
295 | | |
296 | | #else /* !USE_EVENTFD && !HAVE_PIPE */ |
297 | | |
298 | | #define wakeup_write swrite |
299 | | #define wakeup_read sread |
300 | | #define wakeup_close sclose |
301 | | |
302 | | #endif |
303 | | |
304 | | int Curl_wakeup_signal(curl_socket_t socks[2]) |
305 | 15 | { |
306 | 15 | int sockerr = 0; |
307 | 15 | #ifdef USE_EVENTFD |
308 | 15 | const uint64_t buf[1] = { 1 }; |
309 | | #else |
310 | | const char buf[1] = { 1 }; |
311 | | #endif |
312 | | |
313 | 15 | while(1) { |
314 | 15 | sockerr = 0; |
315 | 15 | if(wakeup_write(socks[1], buf, sizeof(buf)) < 0) { |
316 | 0 | sockerr = SOCKERRNO; |
317 | 0 | #ifndef USE_WINSOCK |
318 | 0 | if(sockerr == SOCKEINTR) |
319 | 0 | continue; |
320 | 0 | #endif |
321 | 0 | if(SOCK_EAGAIN(sockerr)) |
322 | 0 | sockerr = 0; /* wakeup is already ongoing */ |
323 | 0 | } |
324 | 15 | break; |
325 | 15 | } |
326 | 15 | return sockerr; |
327 | 15 | } |
328 | | |
329 | | CURLcode Curl_wakeup_consume(curl_socket_t socks[2], bool all) |
330 | 88.3k | { |
331 | 88.3k | char buf[64]; |
332 | 88.3k | ssize_t rc; |
333 | 88.3k | CURLcode result = CURLE_OK; |
334 | | |
335 | 88.3k | do { |
336 | 88.3k | rc = wakeup_read(socks[0], buf, sizeof(buf)); |
337 | 88.3k | if(!rc) |
338 | 0 | break; |
339 | 88.3k | else if(rc < 0) { |
340 | 88.3k | int sockerr = SOCKERRNO; |
341 | 88.3k | #ifndef USE_WINSOCK |
342 | 88.3k | if(sockerr == SOCKEINTR) |
343 | 0 | continue; |
344 | 88.3k | #endif |
345 | 88.3k | if(SOCK_EAGAIN(sockerr)) |
346 | 88.3k | break; |
347 | 0 | result = CURLE_READ_ERROR; |
348 | 0 | break; |
349 | 88.3k | } |
350 | 88.3k | } while(all); |
351 | 88.3k | return result; |
352 | 88.3k | } |
353 | | |
354 | | void Curl_wakeup_destroy(curl_socket_t socks[2]) |
355 | 42.1k | { |
356 | | #ifndef USE_EVENTFD |
357 | | if(socks[1] != CURL_SOCKET_BAD) |
358 | | wakeup_close(socks[1]); |
359 | | #endif |
360 | 42.1k | if(socks[0] != CURL_SOCKET_BAD) |
361 | 42.0k | wakeup_close(socks[0]); |
362 | 42.1k | socks[0] = socks[1] = CURL_SOCKET_BAD; |
363 | 42.1k | } |
364 | | |
365 | | #endif /* !CURL_DISABLE_SOCKETPAIR */ |