/src/CMake/Utilities/cmcurl/lib/cf-socket.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 | | #ifdef HAVE_NETINET_IN_H |
27 | | #include <netinet/in.h> /* <netinet/tcp.h> may need it */ |
28 | | #endif |
29 | | #ifdef HAVE_LINUX_TCP_H |
30 | | #include <linux/tcp.h> |
31 | | #elif defined(HAVE_NETINET_TCP_H) |
32 | | #include <netinet/tcp.h> |
33 | | #endif |
34 | | #ifdef HAVE_NETINET_UDP_H |
35 | | #include <netinet/udp.h> |
36 | | #endif |
37 | | #ifdef HAVE_SYS_IOCTL_H |
38 | | #include <sys/ioctl.h> |
39 | | #endif |
40 | | #ifdef HAVE_NETDB_H |
41 | | #include <netdb.h> |
42 | | #endif |
43 | | #ifdef HAVE_ARPA_INET_H |
44 | | #include <arpa/inet.h> |
45 | | #endif |
46 | | |
47 | | #ifdef __VMS |
48 | | #include <in.h> |
49 | | #include <inet.h> |
50 | | #endif |
51 | | |
52 | | #ifdef __DragonFly__ |
53 | | /* Required for __DragonFly_version */ |
54 | | #include <sys/param.h> |
55 | | #endif |
56 | | |
57 | | #include "urldata.h" |
58 | | #include "curl_trc.h" |
59 | | #include "if2ip.h" |
60 | | #include "cfilters.h" |
61 | | #include "cf-socket.h" |
62 | | #include "connect.h" |
63 | | #include "curl_addrinfo.h" |
64 | | #include "select.h" |
65 | | #include "multiif.h" |
66 | | #include "curlx/inet_pton.h" |
67 | | #include "progress.h" |
68 | | #include "conncache.h" |
69 | | #include "multihandle.h" |
70 | | #include "rand.h" |
71 | | #include "curlx/strdup.h" |
72 | | #include "system_win32.h" |
73 | | #include "curlx/nonblock.h" |
74 | | #include "curlx/version_win32.h" |
75 | | #include "curlx/strerr.h" |
76 | | #include "curlx/strparse.h" |
77 | | |
78 | | |
79 | | static void tcpnodelay(struct Curl_cfilter *cf, |
80 | | struct Curl_easy *data, |
81 | | curl_socket_t sockfd) |
82 | 0 | { |
83 | 0 | #if defined(TCP_NODELAY) && defined(CURL_TCP_NODELAY_SUPPORTED) |
84 | 0 | curl_socklen_t onoff = (curl_socklen_t)1; |
85 | 0 | int level = IPPROTO_TCP; |
86 | 0 | VERBOSE(char buffer[STRERROR_LEN]); |
87 | |
|
88 | 0 | if(setsockopt(sockfd, level, TCP_NODELAY, |
89 | 0 | (void *)&onoff, sizeof(onoff)) < 0) |
90 | 0 | CURL_TRC_CF(data, cf, "Could not set TCP_NODELAY: %s", |
91 | 0 | curlx_strerror(SOCKERRNO, buffer, sizeof(buffer))); |
92 | | #else |
93 | | (void)cf; |
94 | | (void)data; |
95 | | (void)sockfd; |
96 | | #endif |
97 | 0 | } |
98 | | |
99 | | #if defined(USE_WINSOCK) || defined(TCP_KEEPIDLE) || \ |
100 | | defined(TCP_KEEPALIVE) || defined(TCP_KEEPALIVE_THRESHOLD) || \ |
101 | | defined(TCP_KEEPINTVL) || defined(TCP_KEEPALIVE_ABORT_THRESHOLD) |
102 | | #if defined(USE_WINSOCK) || \ |
103 | | (defined(__sun) && !defined(TCP_KEEPIDLE)) || \ |
104 | | (defined(__DragonFly__) && __DragonFly_version < 500702) || \ |
105 | | (defined(_WIN32) && !defined(TCP_KEEPIDLE)) |
106 | | /* Solaris < 11.4, DragonFlyBSD < 500702 and Windows < 10.0.16299 |
107 | | * use millisecond units. */ |
108 | | #define KEEPALIVE_FACTOR(x) ((x) *= 1000) |
109 | | #else |
110 | | #define KEEPALIVE_FACTOR(x) |
111 | | #endif |
112 | | #endif |
113 | | |
114 | | static void tcpkeepalive(struct Curl_cfilter *cf, |
115 | | struct Curl_easy *data, |
116 | | curl_socket_t sockfd) |
117 | 0 | { |
118 | 0 | int optval = data->set.tcp_keepalive ? 1 : 0; |
119 | | |
120 | | /* only set IDLE and INTVL if setting KEEPALIVE is successful */ |
121 | 0 | if(setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, |
122 | 0 | (void *)&optval, sizeof(optval)) < 0) { |
123 | 0 | CURL_TRC_CF(data, cf, "Failed to set SO_KEEPALIVE on fd " |
124 | 0 | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
125 | 0 | } |
126 | 0 | else { |
127 | | #ifdef USE_WINSOCK |
128 | | /* Windows 10, version 1709 (10.0.16299) and later versions can use |
129 | | setsockopt() TCP_KEEP*. Older versions return with failure. */ |
130 | | if(curlx_verify_windows_version(10, 0, 16299, PLATFORM_WINNT, |
131 | | VERSION_GREATER_THAN_EQUAL)) { |
132 | | CURL_TRC_CF(data, cf, "Set TCP_KEEP* on fd=%" FMT_SOCKET_T, sockfd); |
133 | | optval = curlx_sltosi(data->set.tcp_keepidle); |
134 | | /* Offered by mingw-w64 v12+. MS SDK 6.0A+. */ |
135 | | #ifndef TCP_KEEPALIVE |
136 | | #define TCP_KEEPALIVE 3 |
137 | | #endif |
138 | | /* Offered by mingw-w64 v12+. MS SDK ~10+/~VS2017+. */ |
139 | | #ifndef TCP_KEEPCNT |
140 | | #define TCP_KEEPCNT 16 |
141 | | #endif |
142 | | #ifndef TCP_KEEPIDLE |
143 | | #define TCP_KEEPIDLE TCP_KEEPALIVE |
144 | | #endif |
145 | | #ifndef TCP_KEEPINTVL |
146 | | #define TCP_KEEPINTVL 17 |
147 | | #endif |
148 | | if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, |
149 | | (const char *)&optval, sizeof(optval)) < 0) { |
150 | | CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPIDLE on fd " |
151 | | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
152 | | } |
153 | | optval = curlx_sltosi(data->set.tcp_keepintvl); |
154 | | if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, |
155 | | (const char *)&optval, sizeof(optval)) < 0) { |
156 | | CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPINTVL on fd " |
157 | | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
158 | | } |
159 | | optval = curlx_sltosi(data->set.tcp_keepcnt); |
160 | | if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, |
161 | | (const char *)&optval, sizeof(optval)) < 0) { |
162 | | CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPCNT on fd " |
163 | | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
164 | | } |
165 | | } |
166 | | else { |
167 | | /* Offered by mingw-w64 and MS SDK. Latter only when targeting Win7+. */ |
168 | | #ifndef SIO_KEEPALIVE_VALS |
169 | | #define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR, 4) |
170 | | struct tcp_keepalive { |
171 | | u_long onoff; |
172 | | u_long keepalivetime; |
173 | | u_long keepaliveinterval; |
174 | | }; |
175 | | #endif |
176 | | struct tcp_keepalive vals; |
177 | | DWORD dummy; |
178 | | vals.onoff = 1; |
179 | | optval = curlx_sltosi(data->set.tcp_keepidle); |
180 | | KEEPALIVE_FACTOR(optval); |
181 | | vals.keepalivetime = (u_long)optval; |
182 | | optval = curlx_sltosi(data->set.tcp_keepintvl); |
183 | | KEEPALIVE_FACTOR(optval); |
184 | | vals.keepaliveinterval = (u_long)optval; |
185 | | if(WSAIoctl(sockfd, SIO_KEEPALIVE_VALS, (LPVOID)&vals, sizeof(vals), |
186 | | NULL, 0, &dummy, NULL, NULL) != 0) { |
187 | | CURL_TRC_CF(data, cf, "Failed to set SIO_KEEPALIVE_VALS on fd " |
188 | | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
189 | | } |
190 | | } |
191 | | #else /* !USE_WINSOCK */ |
192 | 0 | #ifdef TCP_KEEPIDLE |
193 | 0 | optval = curlx_sltosi(data->set.tcp_keepidle); |
194 | 0 | KEEPALIVE_FACTOR(optval); |
195 | 0 | if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, |
196 | 0 | (void *)&optval, sizeof(optval)) < 0) { |
197 | 0 | CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPIDLE on fd " |
198 | 0 | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
199 | 0 | } |
200 | | #elif defined(TCP_KEEPALIVE) |
201 | | /* macOS style */ |
202 | | optval = curlx_sltosi(data->set.tcp_keepidle); |
203 | | KEEPALIVE_FACTOR(optval); |
204 | | if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE, |
205 | | (void *)&optval, sizeof(optval)) < 0) { |
206 | | CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPALIVE on fd " |
207 | | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
208 | | } |
209 | | #elif defined(TCP_KEEPALIVE_THRESHOLD) |
210 | | /* Solaris <11.4 style */ |
211 | | optval = curlx_sltosi(data->set.tcp_keepidle); |
212 | | KEEPALIVE_FACTOR(optval); |
213 | | if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, |
214 | | (void *)&optval, sizeof(optval)) < 0) { |
215 | | CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPALIVE_THRESHOLD on fd " |
216 | | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
217 | | } |
218 | | #endif |
219 | 0 | #ifdef TCP_KEEPINTVL |
220 | 0 | optval = curlx_sltosi(data->set.tcp_keepintvl); |
221 | 0 | KEEPALIVE_FACTOR(optval); |
222 | 0 | if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, |
223 | 0 | (void *)&optval, sizeof(optval)) < 0) { |
224 | 0 | CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPINTVL on fd " |
225 | 0 | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
226 | 0 | } |
227 | | #elif defined(TCP_KEEPALIVE_ABORT_THRESHOLD) |
228 | | /* Solaris <11.4 style */ |
229 | | /* TCP_KEEPALIVE_ABORT_THRESHOLD should equal to |
230 | | * TCP_KEEPCNT * TCP_KEEPINTVL on other platforms. |
231 | | * The default value of TCP_KEEPCNT is 9 on Linux, |
232 | | * 8 on *BSD/macOS, 5 or 10 on Windows. We use the |
233 | | * default config for Solaris <11.4 because there is |
234 | | * no default value for TCP_KEEPCNT on Solaris 11.4. |
235 | | * |
236 | | * Note that the consequent probes will not be sent |
237 | | * at equal intervals on Solaris, but will be sent |
238 | | * using the exponential backoff algorithm. */ |
239 | | optval = curlx_sltosi(data->set.tcp_keepcnt) * |
240 | | curlx_sltosi(data->set.tcp_keepintvl); |
241 | | KEEPALIVE_FACTOR(optval); |
242 | | if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, |
243 | | (void *)&optval, sizeof(optval)) < 0) { |
244 | | CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPALIVE_ABORT_THRESHOLD" |
245 | | " on fd %" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
246 | | } |
247 | | #endif |
248 | 0 | #ifdef TCP_KEEPCNT |
249 | 0 | optval = curlx_sltosi(data->set.tcp_keepcnt); |
250 | 0 | if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, |
251 | 0 | (void *)&optval, sizeof(optval)) < 0) { |
252 | 0 | CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPCNT on fd " |
253 | 0 | "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO); |
254 | 0 | } |
255 | 0 | #endif |
256 | 0 | #endif /* USE_WINSOCK */ |
257 | 0 | } |
258 | 0 | } |
259 | | |
260 | | /** |
261 | | * Assign the address `ai` to the Curl_sockaddr_ex `dest` and |
262 | | * set the transport used. |
263 | | */ |
264 | | static CURLcode sock_assign_addr(struct Curl_sockaddr_ex *dest, |
265 | | const struct Curl_addrinfo *ai, |
266 | | uint8_t transport) |
267 | 0 | { |
268 | | /* |
269 | | * The Curl_sockaddr_ex structure is libcurl's external API curl_sockaddr |
270 | | * structure with enough space available to directly hold any |
271 | | * protocol-specific address structures. The variable declared here will be |
272 | | * used to pass / receive data to/from the fopensocket callback if this has |
273 | | * been set, before that, it is initialized from parameters. |
274 | | */ |
275 | 0 | dest->family = ai->ai_family; |
276 | 0 | switch(transport) { |
277 | 0 | case TRNSPRT_TCP: |
278 | 0 | dest->socktype = SOCK_STREAM; |
279 | 0 | dest->protocol = IPPROTO_TCP; |
280 | 0 | break; |
281 | 0 | case TRNSPRT_UNIX: |
282 | 0 | dest->socktype = SOCK_STREAM; |
283 | 0 | dest->protocol = IPPROTO_IP; |
284 | 0 | break; |
285 | 0 | default: /* UDP and QUIC */ |
286 | 0 | dest->socktype = SOCK_DGRAM; |
287 | 0 | dest->protocol = IPPROTO_UDP; |
288 | 0 | break; |
289 | 0 | } |
290 | 0 | dest->addrlen = (unsigned int)ai->ai_addrlen; |
291 | |
|
292 | 0 | DEBUGASSERT(dest->addrlen <= sizeof(dest->curl_sa_addrbuf)); |
293 | 0 | if(dest->addrlen > sizeof(dest->curl_sa_addrbuf)) |
294 | 0 | return CURLE_TOO_LARGE; |
295 | | |
296 | 0 | memcpy(&dest->curl_sa_addrbuf, ai->ai_addr, dest->addrlen); |
297 | 0 | return CURLE_OK; |
298 | 0 | } |
299 | | |
300 | | #ifdef USE_SO_NOSIGPIPE |
301 | | int Curl_sock_nosigpipe(curl_socket_t sockfd) |
302 | | { |
303 | | int onoff = 1; |
304 | | return setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, |
305 | | (void *)&onoff, sizeof(onoff)); |
306 | | } |
307 | | #endif /* USE_SO_NOSIGPIPE */ |
308 | | |
309 | | static CURLcode socket_open(struct Curl_easy *data, |
310 | | struct Curl_sockaddr_ex *addr, |
311 | | curl_socket_t *sockfd) |
312 | 0 | { |
313 | 0 | char errbuf[STRERROR_LEN]; |
314 | |
|
315 | 0 | #ifdef SOCK_CLOEXEC |
316 | 0 | addr->socktype |= SOCK_CLOEXEC; |
317 | 0 | #endif |
318 | |
|
319 | 0 | DEBUGASSERT(data); |
320 | 0 | DEBUGASSERT(data->conn); |
321 | 0 | if(data->set.fopensocket) { |
322 | | /* |
323 | | * If the opensocket callback is set, all the destination address |
324 | | * information is passed to the callback. Depending on this information the |
325 | | * callback may opt to abort the connection, this is indicated returning |
326 | | * CURL_SOCKET_BAD; otherwise it will return a not-connected socket. When |
327 | | * the callback returns a valid socket the destination address information |
328 | | * might have been changed and this 'new' address will actually be used |
329 | | * here to connect. |
330 | | */ |
331 | 0 | Curl_set_in_callback(data, TRUE); |
332 | 0 | *sockfd = data->set.fopensocket(data->set.opensocket_client, |
333 | 0 | CURLSOCKTYPE_IPCXN, |
334 | 0 | (struct curl_sockaddr *)addr); |
335 | 0 | Curl_set_in_callback(data, FALSE); |
336 | 0 | } |
337 | 0 | else { |
338 | | /* opensocket callback not set, so create the socket now */ |
339 | | #ifdef DEBUGBUILD |
340 | | if((addr->family == AF_INET6) && getenv("CURL_DBG_SOCK_FAIL_IPV6")) { |
341 | | failf(data, "CURL_DBG_SOCK_FAIL_IPV6: failed to open socket"); |
342 | | return CURLE_COULDNT_CONNECT; |
343 | | } |
344 | | #endif |
345 | 0 | *sockfd = CURL_SOCKET(addr->family, addr->socktype, addr->protocol); |
346 | 0 | if((*sockfd == CURL_SOCKET_BAD) && (SOCKERRNO == SOCKENOMEM)) |
347 | 0 | return CURLE_OUT_OF_MEMORY; |
348 | 0 | } |
349 | | |
350 | 0 | if(*sockfd == CURL_SOCKET_BAD) { |
351 | | /* no socket, no connection */ |
352 | 0 | failf(data, "failed to open socket: %s", |
353 | 0 | curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf))); |
354 | 0 | return CURLE_COULDNT_CONNECT; |
355 | 0 | } |
356 | | |
357 | | #ifdef USE_SO_NOSIGPIPE |
358 | | if(Curl_sock_nosigpipe(*sockfd) < 0) { |
359 | | failf(data, "setsockopt enable SO_NOSIGPIPE: %s", |
360 | | curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf))); |
361 | | sclose(*sockfd); |
362 | | *sockfd = CURL_SOCKET_BAD; |
363 | | return CURLE_COULDNT_CONNECT; |
364 | | } |
365 | | #endif /* USE_SO_NOSIGPIPE */ |
366 | | |
367 | | #if defined(HAVE_FCNTL) && !defined(SOCK_CLOEXEC) |
368 | | if(fcntl(*sockfd, F_SETFD, FD_CLOEXEC) < 0) { |
369 | | failf(data, "fcntl set CLOEXEC: %s", |
370 | | curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf))); |
371 | | sclose(*sockfd); |
372 | | *sockfd = CURL_SOCKET_BAD; |
373 | | return CURLE_COULDNT_CONNECT; |
374 | | } |
375 | | #endif |
376 | | |
377 | 0 | #if defined(USE_IPV6) && defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID) |
378 | 0 | if(data->conn->scope_id && (addr->family == AF_INET6)) { |
379 | 0 | struct sockaddr_in6 * const sa6 = (void *)&addr->curl_sa_addr; |
380 | 0 | sa6->sin6_scope_id = data->conn->scope_id; |
381 | 0 | } |
382 | 0 | #endif |
383 | 0 | return CURLE_OK; |
384 | 0 | } |
385 | | |
386 | | /* |
387 | | * Create a socket based on info from 'conn' and 'ai'. |
388 | | * |
389 | | * 'addr' should be a pointer to the correct struct to get data back, or NULL. |
390 | | * 'sockfd' must be a pointer to a socket descriptor. |
391 | | * |
392 | | * If the open socket callback is set, used that! |
393 | | * |
394 | | */ |
395 | | CURLcode Curl_socket_open(struct Curl_easy *data, |
396 | | const struct Curl_addrinfo *ai, |
397 | | struct Curl_sockaddr_ex *addr, |
398 | | uint8_t transport, |
399 | | curl_socket_t *sockfd) |
400 | 0 | { |
401 | 0 | struct Curl_sockaddr_ex dummy; |
402 | 0 | CURLcode result; |
403 | |
|
404 | 0 | if(!addr) |
405 | | /* if the caller does not want info back, use a local temp copy */ |
406 | 0 | addr = &dummy; |
407 | |
|
408 | 0 | result = sock_assign_addr(addr, ai, transport); |
409 | 0 | if(result) |
410 | 0 | return result; |
411 | | |
412 | 0 | return socket_open(data, addr, sockfd); |
413 | 0 | } |
414 | | |
415 | | static int socket_close(struct Curl_easy *data, struct connectdata *conn, |
416 | | int use_callback, curl_socket_t sock) |
417 | 0 | { |
418 | 0 | if(sock == CURL_SOCKET_BAD) |
419 | 0 | return 0; |
420 | | |
421 | 0 | if(use_callback && conn && conn->fclosesocket) { |
422 | 0 | int rc; |
423 | 0 | Curl_multi_will_close(data, sock); |
424 | 0 | Curl_set_in_callback(data, TRUE); |
425 | 0 | rc = conn->fclosesocket(conn->closesocket_client, sock); |
426 | 0 | Curl_set_in_callback(data, FALSE); |
427 | 0 | return rc; |
428 | 0 | } |
429 | | |
430 | 0 | if(conn) |
431 | | /* tell the multi-socket code about this */ |
432 | 0 | Curl_multi_will_close(data, sock); |
433 | |
|
434 | 0 | sclose(sock); |
435 | |
|
436 | 0 | return 0; |
437 | 0 | } |
438 | | |
439 | | /* |
440 | | * Close a socket. |
441 | | * |
442 | | * 'conn' can be NULL, beware! |
443 | | */ |
444 | | int Curl_socket_close(struct Curl_easy *data, struct connectdata *conn, |
445 | | curl_socket_t sock) |
446 | 0 | { |
447 | 0 | return socket_close(data, conn, FALSE, sock); |
448 | 0 | } |
449 | | |
450 | | /* |
451 | | * Curl_parse_interface() |
452 | | * |
453 | | * This is used to parse interface argument in the following formats. |
454 | | * In all the examples, `host` can be an IP address or a hostname. |
455 | | * |
456 | | * <iface_or_host> - can be either an interface name or a host. |
457 | | * if!<iface> - interface name. |
458 | | * host!<host> - hostname. |
459 | | * ifhost!<iface>!<host> - interface name and hostname. |
460 | | * |
461 | | * Parameters: |
462 | | * |
463 | | * input [in] - input string. |
464 | | * len [in] - length of the input string. |
465 | | * dev [in/out] - address where a pointer to newly allocated memory |
466 | | * holding the interface-or-host will be stored upon |
467 | | * completion. |
468 | | * iface [in/out] - address where a pointer to newly allocated memory |
469 | | * holding the interface will be stored upon completion. |
470 | | * host [in/out] - address where a pointer to newly allocated memory |
471 | | * holding the host will be stored upon completion. |
472 | | * |
473 | | * Returns CURLE_OK on success. |
474 | | */ |
475 | | CURLcode Curl_parse_interface(const char *input, |
476 | | char **dev, char **iface, char **host) |
477 | 0 | { |
478 | 0 | static const char if_prefix[] = "if!"; |
479 | 0 | static const char host_prefix[] = "host!"; |
480 | 0 | static const char if_host_prefix[] = "ifhost!"; |
481 | 0 | size_t len; |
482 | |
|
483 | 0 | DEBUGASSERT(dev); |
484 | 0 | DEBUGASSERT(iface); |
485 | 0 | DEBUGASSERT(host); |
486 | |
|
487 | 0 | len = strlen(input); |
488 | 0 | if(len > 512) |
489 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
490 | | |
491 | 0 | if(!strncmp(if_prefix, input, strlen(if_prefix))) { |
492 | 0 | input += strlen(if_prefix); |
493 | 0 | if(!*input) |
494 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
495 | 0 | *iface = curlx_memdup0(input, len - strlen(if_prefix)); |
496 | 0 | return *iface ? CURLE_OK : CURLE_OUT_OF_MEMORY; |
497 | 0 | } |
498 | 0 | else if(!strncmp(host_prefix, input, strlen(host_prefix))) { |
499 | 0 | input += strlen(host_prefix); |
500 | 0 | if(!*input) |
501 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
502 | 0 | *host = curlx_memdup0(input, len - strlen(host_prefix)); |
503 | 0 | return *host ? CURLE_OK : CURLE_OUT_OF_MEMORY; |
504 | 0 | } |
505 | 0 | else if(!strncmp(if_host_prefix, input, strlen(if_host_prefix))) { |
506 | 0 | const char *host_part; |
507 | 0 | input += strlen(if_host_prefix); |
508 | 0 | len -= strlen(if_host_prefix); |
509 | 0 | host_part = memchr(input, '!', len); |
510 | 0 | if(!host_part || !*(host_part + 1)) |
511 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
512 | 0 | *iface = curlx_memdup0(input, host_part - input); |
513 | 0 | if(!*iface) |
514 | 0 | return CURLE_OUT_OF_MEMORY; |
515 | 0 | ++host_part; |
516 | 0 | *host = curlx_memdup0(host_part, len - (host_part - input)); |
517 | 0 | if(!*host) { |
518 | 0 | curlx_free(*iface); |
519 | 0 | *iface = NULL; |
520 | 0 | return CURLE_OUT_OF_MEMORY; |
521 | 0 | } |
522 | 0 | return CURLE_OK; |
523 | 0 | } |
524 | | |
525 | 0 | if(!*input) |
526 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
527 | 0 | *dev = curlx_memdup0(input, len); |
528 | 0 | return *dev ? CURLE_OK : CURLE_OUT_OF_MEMORY; |
529 | 0 | } |
530 | | |
531 | | #ifndef CURL_DISABLE_BINDLOCAL |
532 | | static CURLcode bindlocal(struct Curl_easy *data, struct connectdata *conn, |
533 | | curl_socket_t sockfd, int af, unsigned int scope) |
534 | 0 | { |
535 | 0 | struct Curl_sockaddr_storage sa; |
536 | 0 | struct sockaddr *sock = (struct sockaddr *)&sa; /* bind to this address */ |
537 | 0 | curl_socklen_t sizeof_sa = 0; /* size of the data sock points to */ |
538 | 0 | struct sockaddr_in *si4 = (struct sockaddr_in *)&sa; |
539 | 0 | #ifdef USE_IPV6 |
540 | 0 | struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)&sa; |
541 | 0 | #endif |
542 | |
|
543 | 0 | struct Curl_dns_entry *h = NULL; |
544 | 0 | unsigned short port = data->set.localport; /* use this port number, 0 for |
545 | | "random" */ |
546 | | /* how many port numbers to try to bind to, increasing one at a time */ |
547 | 0 | int portnum = data->set.localportrange; |
548 | 0 | const char *dev = data->set.str[STRING_DEVICE]; |
549 | 0 | const char *iface_input = data->set.str[STRING_INTERFACE]; |
550 | 0 | const char *host_input = data->set.str[STRING_BINDHOST]; |
551 | 0 | const char *iface = iface_input ? iface_input : dev; |
552 | 0 | const char *host = host_input ? host_input : dev; |
553 | 0 | int error; |
554 | 0 | #ifdef IP_BIND_ADDRESS_NO_PORT |
555 | 0 | int on = 1; |
556 | 0 | #endif |
557 | | #ifndef USE_IPV6 |
558 | | (void)scope; |
559 | | #endif |
560 | | |
561 | | /************************************************************* |
562 | | * Select device to bind socket to |
563 | | *************************************************************/ |
564 | 0 | if(!iface && !host && !port) |
565 | | /* no local kind of binding was requested */ |
566 | 0 | return CURLE_OK; |
567 | 0 | else if(iface && (strlen(iface) >= 255)) |
568 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
569 | | |
570 | 0 | memset(&sa, 0, sizeof(struct Curl_sockaddr_storage)); |
571 | |
|
572 | 0 | if(iface || host) { |
573 | 0 | char myhost[256] = ""; |
574 | 0 | int done = 0; /* -1 for error, 1 for address found */ |
575 | 0 | if2ip_result_t if2ip_result = IF2IP_NOT_FOUND; |
576 | |
|
577 | 0 | #ifdef SO_BINDTODEVICE |
578 | 0 | if(iface) { |
579 | | /* |
580 | | * This binds the local socket to a particular interface. This will |
581 | | * force even requests to other local interfaces to go out the external |
582 | | * interface. Only bind to the interface when specified as interface, |
583 | | * not as a hostname or ip address. |
584 | | * |
585 | | * The interface might be a VRF, eg: vrf-blue, which means it cannot be |
586 | | * converted to an IP address and would fail Curl_if2ip. Try to |
587 | | * use it straight away. |
588 | | */ |
589 | 0 | if(setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, |
590 | 0 | iface, (curl_socklen_t)strlen(iface) + 1) == 0) { |
591 | | /* This is often "errno 1, error: Operation not permitted" if you are |
592 | | * not running as root or another suitable privileged user. If it |
593 | | * succeeds it means the parameter was a valid interface and not an IP |
594 | | * address. Return immediately. |
595 | | */ |
596 | 0 | if(!host_input) { |
597 | 0 | infof(data, "socket successfully bound to interface '%s'", iface); |
598 | 0 | return CURLE_OK; |
599 | 0 | } |
600 | 0 | } |
601 | 0 | } |
602 | 0 | #endif |
603 | 0 | if(!host_input) { |
604 | | /* Discover IP from input device, then bind to it */ |
605 | 0 | if2ip_result = Curl_if2ip(af, |
606 | 0 | #ifdef USE_IPV6 |
607 | 0 | scope, conn->scope_id, |
608 | 0 | #endif |
609 | 0 | iface, myhost, sizeof(myhost)); |
610 | 0 | } |
611 | 0 | switch(if2ip_result) { |
612 | 0 | case IF2IP_NOT_FOUND: |
613 | 0 | if(iface_input && !host_input) { |
614 | | /* Do not fall back to treating it as a hostname */ |
615 | 0 | char buffer[STRERROR_LEN]; |
616 | 0 | data->state.os_errno = error = SOCKERRNO; |
617 | 0 | failf(data, "Could not bind to interface '%s' with errno %d: %s", |
618 | 0 | iface, error, curlx_strerror(error, buffer, sizeof(buffer))); |
619 | 0 | return CURLE_INTERFACE_FAILED; |
620 | 0 | } |
621 | 0 | break; |
622 | 0 | case IF2IP_AF_NOT_SUPPORTED: |
623 | | /* Signal the caller to try another address family if available */ |
624 | 0 | return CURLE_UNSUPPORTED_PROTOCOL; |
625 | 0 | case IF2IP_FOUND: |
626 | | /* |
627 | | * We now have the numerical IP address in the 'myhost' buffer |
628 | | */ |
629 | 0 | host = myhost; |
630 | 0 | infof(data, "Local Interface %s is ip %s using address family %i", |
631 | 0 | iface, host, af); |
632 | 0 | done = 1; |
633 | 0 | break; |
634 | 0 | } |
635 | 0 | if(!iface_input || host_input) { |
636 | | /* |
637 | | * This was not an interface, resolve the name as a hostname |
638 | | * or IP number |
639 | | * |
640 | | * Temporarily force name resolution to use only the address type |
641 | | * of the connection. The resolve functions should really be changed |
642 | | * to take a type parameter instead. |
643 | | */ |
644 | 0 | int ip_version = (af == AF_INET) ? |
645 | 0 | CURL_IPRESOLVE_V4 : CURL_IPRESOLVE_WHATEVER; |
646 | 0 | #ifdef USE_IPV6 |
647 | 0 | if(af == AF_INET6) |
648 | 0 | ip_version = CURL_IPRESOLVE_V6; |
649 | 0 | #endif |
650 | |
|
651 | 0 | (void)Curl_resolv_blocking(data, host, 80, ip_version, &h); |
652 | 0 | if(h) { |
653 | 0 | int h_af = h->addr->ai_family; |
654 | | /* convert the resolved address, sizeof myhost >= INET_ADDRSTRLEN */ |
655 | 0 | Curl_printable_address(h->addr, myhost, sizeof(myhost)); |
656 | 0 | infof(data, "Name '%s' family %i resolved to '%s' family %i", |
657 | 0 | host, af, myhost, h_af); |
658 | 0 | Curl_resolv_unlink(data, &h); /* this will NULL, potential free h */ |
659 | 0 | if(af != h_af) { |
660 | | /* bad IP version combo, signal the caller to try another address |
661 | | family if available */ |
662 | 0 | return CURLE_UNSUPPORTED_PROTOCOL; |
663 | 0 | } |
664 | 0 | done = 1; |
665 | 0 | } |
666 | 0 | else { |
667 | | /* |
668 | | * provided dev was no interface (or interfaces are not supported |
669 | | * e.g. Solaris) no ip address and no domain we fail here |
670 | | */ |
671 | 0 | done = -1; |
672 | 0 | } |
673 | 0 | } |
674 | | |
675 | 0 | if(done > 0) { |
676 | 0 | #ifdef USE_IPV6 |
677 | | /* IPv6 address */ |
678 | 0 | if(af == AF_INET6) { |
679 | 0 | #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID |
680 | 0 | char *scope_ptr = strchr(myhost, '%'); |
681 | 0 | if(scope_ptr) |
682 | 0 | *(scope_ptr++) = '\0'; |
683 | 0 | #endif |
684 | 0 | if(curlx_inet_pton(AF_INET6, myhost, &si6->sin6_addr) > 0) { |
685 | 0 | si6->sin6_family = AF_INET6; |
686 | 0 | si6->sin6_port = htons(port); |
687 | 0 | #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID |
688 | 0 | if(scope_ptr) { |
689 | | /* The "myhost" string either comes from Curl_if2ip or from |
690 | | Curl_printable_address. The latter returns only numeric scope |
691 | | IDs and the former returns none at all. So the scope ID, if |
692 | | present, is known to be numeric */ |
693 | 0 | curl_off_t scope_id; |
694 | 0 | if(curlx_str_number((const char **)CURL_UNCONST(&scope_ptr), |
695 | 0 | &scope_id, UINT_MAX)) |
696 | 0 | return CURLE_UNSUPPORTED_PROTOCOL; |
697 | 0 | si6->sin6_scope_id = (unsigned int)scope_id; |
698 | 0 | } |
699 | 0 | #endif |
700 | 0 | } |
701 | 0 | sizeof_sa = sizeof(struct sockaddr_in6); |
702 | 0 | } |
703 | 0 | else |
704 | 0 | #endif |
705 | | /* IPv4 address */ |
706 | 0 | if((af == AF_INET) && |
707 | 0 | (curlx_inet_pton(AF_INET, myhost, &si4->sin_addr) > 0)) { |
708 | 0 | si4->sin_family = AF_INET; |
709 | 0 | si4->sin_port = htons(port); |
710 | 0 | sizeof_sa = sizeof(struct sockaddr_in); |
711 | 0 | } |
712 | 0 | } |
713 | | |
714 | 0 | if(done < 1) { |
715 | | /* errorbuf is set false so failf will overwrite any message already in |
716 | | the error buffer, so the user receives this error message instead of a |
717 | | generic resolve error. */ |
718 | 0 | char buffer[STRERROR_LEN]; |
719 | 0 | data->state.errorbuf = FALSE; |
720 | 0 | data->state.os_errno = error = SOCKERRNO; |
721 | 0 | failf(data, "Could not bind to '%s' with errno %d: %s", host, |
722 | 0 | error, curlx_strerror(error, buffer, sizeof(buffer))); |
723 | 0 | return CURLE_INTERFACE_FAILED; |
724 | 0 | } |
725 | 0 | } |
726 | 0 | else { |
727 | | /* no device was given, prepare sa to match af's needs */ |
728 | 0 | #ifdef USE_IPV6 |
729 | 0 | if(af == AF_INET6) { |
730 | 0 | si6->sin6_family = AF_INET6; |
731 | 0 | si6->sin6_port = htons(port); |
732 | 0 | sizeof_sa = sizeof(struct sockaddr_in6); |
733 | 0 | } |
734 | 0 | else |
735 | 0 | #endif |
736 | 0 | if(af == AF_INET) { |
737 | 0 | si4->sin_family = AF_INET; |
738 | 0 | si4->sin_port = htons(port); |
739 | 0 | sizeof_sa = sizeof(struct sockaddr_in); |
740 | 0 | } |
741 | 0 | } |
742 | 0 | #ifdef IP_BIND_ADDRESS_NO_PORT |
743 | 0 | (void)setsockopt(sockfd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &on, sizeof(on)); |
744 | 0 | #endif |
745 | 0 | for(;;) { |
746 | 0 | if(bind(sockfd, sock, sizeof_sa) >= 0) { |
747 | | /* we succeeded to bind */ |
748 | 0 | infof(data, "Local port: %hu", port); |
749 | 0 | conn->bits.bound = TRUE; |
750 | 0 | return CURLE_OK; |
751 | 0 | } |
752 | | |
753 | 0 | if(--portnum > 0) { |
754 | 0 | port++; /* try next port */ |
755 | 0 | if(port == 0) |
756 | 0 | break; |
757 | 0 | infof(data, "Bind to local port %d failed, trying next", port - 1); |
758 | | /* We reuse/clobber the port variable here below */ |
759 | 0 | if(sock->sa_family == AF_INET) |
760 | 0 | si4->sin_port = htons(port); |
761 | 0 | #ifdef USE_IPV6 |
762 | 0 | else |
763 | 0 | si6->sin6_port = htons(port); |
764 | 0 | #endif |
765 | 0 | } |
766 | 0 | else |
767 | 0 | break; |
768 | 0 | } |
769 | 0 | { |
770 | 0 | char buffer[STRERROR_LEN]; |
771 | 0 | data->state.os_errno = error = SOCKERRNO; |
772 | 0 | failf(data, "bind failed with errno %d: %s", |
773 | 0 | error, curlx_strerror(error, buffer, sizeof(buffer))); |
774 | 0 | } |
775 | |
|
776 | 0 | return CURLE_INTERFACE_FAILED; |
777 | 0 | } |
778 | | #endif |
779 | | |
780 | | /* |
781 | | * verifyconnect() returns TRUE if the connect really has happened. |
782 | | */ |
783 | | static bool verifyconnect(curl_socket_t sockfd, int *error) |
784 | 0 | { |
785 | 0 | bool rc = TRUE; |
786 | 0 | #ifdef SO_ERROR |
787 | 0 | int err = 0; |
788 | 0 | curl_socklen_t errSize = sizeof(err); |
789 | |
|
790 | | #ifdef _WIN32 |
791 | | /* |
792 | | * In October 2003 we effectively nullified this function on Windows due to |
793 | | * problems with it using all CPU in multi-threaded cases. |
794 | | * |
795 | | * In May 2004, we brought it back to offer more info back on connect |
796 | | * failures. We could reproduce the former problems with this function, but |
797 | | * could avoid them by adding this SleepEx() call below: |
798 | | * |
799 | | * "I do not have Rational Quantify, but the hint from his post was |
800 | | * ntdll::NtRemoveIoCompletion(). I would assume the SleepEx (or maybe |
801 | | * Sleep(0) would be enough?) would release whatever |
802 | | * mutex/critical-section the ntdll call is waiting on. |
803 | | * |
804 | | * Someone got to verify this on Win-NT 4.0, 2000." |
805 | | */ |
806 | | SleepEx(0, FALSE); |
807 | | #endif |
808 | |
|
809 | 0 | if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&err, &errSize)) |
810 | 0 | err = SOCKERRNO; |
811 | | #if defined(EBADIOCTL) && defined(__minix) |
812 | | /* Minix 3.1.x does not support getsockopt on UDP sockets */ |
813 | | if(EBADIOCTL == err) { |
814 | | SET_SOCKERRNO(0); |
815 | | err = 0; |
816 | | } |
817 | | #endif |
818 | 0 | if((err == 0) || (SOCKEISCONN == err)) |
819 | | /* we are connected, awesome! */ |
820 | 0 | rc = TRUE; |
821 | 0 | else |
822 | | /* This was not a successful connect */ |
823 | 0 | rc = FALSE; |
824 | 0 | if(error) |
825 | 0 | *error = err; |
826 | | #else |
827 | | (void)sockfd; |
828 | | if(error) |
829 | | *error = SOCKERRNO; |
830 | | #endif |
831 | 0 | return rc; |
832 | 0 | } |
833 | | |
834 | | /** |
835 | | * Determine the curl code for a socket connect() == -1 with errno. |
836 | | */ |
837 | | static CURLcode socket_connect_result(struct Curl_easy *data, |
838 | | const char *ipaddress, int error) |
839 | 0 | { |
840 | 0 | switch(error) { |
841 | 0 | case SOCKEINPROGRESS: |
842 | 0 | case SOCKEWOULDBLOCK: |
843 | 0 | #ifdef EAGAIN |
844 | | #if (EAGAIN) != (SOCKEWOULDBLOCK) |
845 | | /* On some platforms EAGAIN and EWOULDBLOCK are the |
846 | | * same value, and on others they are different, hence |
847 | | * the odd #if |
848 | | */ |
849 | | case EAGAIN: |
850 | | #endif |
851 | 0 | #endif |
852 | 0 | return CURLE_OK; |
853 | | |
854 | 0 | default: |
855 | | /* unknown error, fallthrough and try another address! */ |
856 | 0 | { |
857 | 0 | VERBOSE(char buffer[STRERROR_LEN]); |
858 | 0 | infof(data, "Immediate connect fail for %s: %s", ipaddress, |
859 | 0 | curlx_strerror(error, buffer, sizeof(buffer))); |
860 | 0 | NOVERBOSE((void)ipaddress); |
861 | 0 | } |
862 | 0 | data->state.os_errno = error; |
863 | | /* connect failed */ |
864 | 0 | return CURLE_COULDNT_CONNECT; |
865 | 0 | } |
866 | 0 | } |
867 | | |
868 | | struct cf_socket_ctx { |
869 | | uint8_t transport; |
870 | | struct Curl_sockaddr_ex addr; /* address to connect to */ |
871 | | curl_socket_t sock; /* current attempt socket */ |
872 | | struct ip_quadruple ip; /* The IP quadruple 2x(addr+port) */ |
873 | | struct curltime started_at; /* when socket was created */ |
874 | | struct curltime connected_at; /* when socket connected/got first byte */ |
875 | | struct curltime first_byte_at; /* when first byte was recvd */ |
876 | | #ifdef USE_WINSOCK |
877 | | struct curltime last_sndbuf_query_at; /* when SO_SNDBUF last queried */ |
878 | | ULONG sndbuf_size; /* the last set SO_SNDBUF size */ |
879 | | #endif |
880 | | int error; /* errno of last failure or 0 */ |
881 | | #ifdef DEBUGBUILD |
882 | | int wblock_percent; /* percent of writes doing EAGAIN */ |
883 | | int wpartial_percent; /* percent of bytes written in send */ |
884 | | int rblock_percent; /* percent of reads doing EAGAIN */ |
885 | | size_t recv_max; /* max enforced read size */ |
886 | | #endif |
887 | | BIT(got_first_byte); /* if first byte was received */ |
888 | | BIT(listening); /* socket is listening */ |
889 | | BIT(accepted); /* socket was accepted, not connected */ |
890 | | BIT(sock_connected); /* socket is "connected", e.g. in UDP */ |
891 | | BIT(active); |
892 | | }; |
893 | | |
894 | | static CURLcode cf_socket_ctx_init(struct cf_socket_ctx *ctx, |
895 | | const struct Curl_addrinfo *ai, |
896 | | uint8_t transport) |
897 | 0 | { |
898 | 0 | CURLcode result; |
899 | |
|
900 | 0 | memset(ctx, 0, sizeof(*ctx)); |
901 | 0 | ctx->sock = CURL_SOCKET_BAD; |
902 | 0 | ctx->transport = transport; |
903 | |
|
904 | 0 | result = sock_assign_addr(&ctx->addr, ai, transport); |
905 | 0 | if(result) |
906 | 0 | return result; |
907 | | |
908 | | #ifdef DEBUGBUILD |
909 | | { |
910 | | const char *p = getenv("CURL_DBG_SOCK_WBLOCK"); |
911 | | if(p) { |
912 | | curl_off_t l; |
913 | | if(!curlx_str_number(&p, &l, 100)) |
914 | | ctx->wblock_percent = (int)l; |
915 | | } |
916 | | p = getenv("CURL_DBG_SOCK_WPARTIAL"); |
917 | | if(p) { |
918 | | curl_off_t l; |
919 | | if(!curlx_str_number(&p, &l, 100)) |
920 | | ctx->wpartial_percent = (int)l; |
921 | | } |
922 | | p = getenv("CURL_DBG_SOCK_RBLOCK"); |
923 | | if(p) { |
924 | | curl_off_t l; |
925 | | if(!curlx_str_number(&p, &l, 100)) |
926 | | ctx->rblock_percent = (int)l; |
927 | | } |
928 | | p = getenv("CURL_DBG_SOCK_RMAX"); |
929 | | if(p) { |
930 | | curl_off_t l; |
931 | | if(!curlx_str_number(&p, &l, CURL_OFF_T_MAX)) |
932 | | ctx->recv_max = (size_t)l; |
933 | | } |
934 | | } |
935 | | #endif |
936 | | |
937 | 0 | return result; |
938 | 0 | } |
939 | | |
940 | | static void cf_socket_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
941 | 0 | { |
942 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
943 | |
|
944 | 0 | if(ctx && ctx->sock != CURL_SOCKET_BAD) { |
945 | 0 | CURL_TRC_CF(data, cf, "cf_socket_close, fd=%" FMT_SOCKET_T, ctx->sock); |
946 | 0 | if(ctx->sock == cf->conn->sock[cf->sockindex]) |
947 | 0 | cf->conn->sock[cf->sockindex] = CURL_SOCKET_BAD; |
948 | 0 | socket_close(data, cf->conn, !ctx->accepted, ctx->sock); |
949 | 0 | ctx->sock = CURL_SOCKET_BAD; |
950 | 0 | ctx->active = FALSE; |
951 | 0 | memset(&ctx->started_at, 0, sizeof(ctx->started_at)); |
952 | 0 | memset(&ctx->connected_at, 0, sizeof(ctx->connected_at)); |
953 | 0 | } |
954 | |
|
955 | 0 | cf->connected = FALSE; |
956 | 0 | } |
957 | | |
958 | | static CURLcode cf_socket_shutdown(struct Curl_cfilter *cf, |
959 | | struct Curl_easy *data, |
960 | | bool *done) |
961 | 0 | { |
962 | 0 | if(cf->connected) { |
963 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
964 | |
|
965 | 0 | CURL_TRC_CF(data, cf, "cf_socket_shutdown, fd=%" FMT_SOCKET_T, ctx->sock); |
966 | | /* On TCP, and when the socket looks well and non-blocking mode |
967 | | * can be enabled, receive dangling bytes before close to avoid |
968 | | * entering RST states unnecessarily. */ |
969 | 0 | if(ctx->sock != CURL_SOCKET_BAD && |
970 | 0 | ctx->transport == TRNSPRT_TCP && |
971 | 0 | (curlx_nonblock(ctx->sock, TRUE) >= 0)) { |
972 | 0 | unsigned char buf[1024]; |
973 | 0 | (void)sread(ctx->sock, buf, sizeof(buf)); |
974 | 0 | } |
975 | 0 | } |
976 | 0 | *done = TRUE; |
977 | 0 | return CURLE_OK; |
978 | 0 | } |
979 | | |
980 | | static void cf_socket_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
981 | 0 | { |
982 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
983 | |
|
984 | 0 | cf_socket_close(cf, data); |
985 | 0 | CURL_TRC_CF(data, cf, "destroy"); |
986 | 0 | curlx_free(ctx); |
987 | 0 | cf->ctx = NULL; |
988 | 0 | } |
989 | | |
990 | | static void set_local_ip(struct Curl_cfilter *cf, |
991 | | struct Curl_easy *data) |
992 | 0 | { |
993 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
994 | 0 | ctx->ip.local_ip[0] = 0; |
995 | 0 | ctx->ip.local_port = 0; |
996 | |
|
997 | 0 | #ifdef HAVE_GETSOCKNAME |
998 | 0 | if((ctx->sock != CURL_SOCKET_BAD) && |
999 | 0 | !(data->conn->scheme->protocol & CURLPROTO_TFTP)) { |
1000 | | /* TFTP does not connect, so it cannot get the IP like this */ |
1001 | 0 | struct Curl_sockaddr_storage ssloc; |
1002 | 0 | curl_socklen_t slen = sizeof(struct Curl_sockaddr_storage); |
1003 | 0 | VERBOSE(char buffer[STRERROR_LEN]); |
1004 | |
|
1005 | 0 | memset(&ssloc, 0, sizeof(ssloc)); |
1006 | 0 | if(getsockname(ctx->sock, (struct sockaddr *)&ssloc, &slen)) { |
1007 | 0 | VERBOSE(int error = SOCKERRNO); |
1008 | 0 | infof(data, "getsockname() failed with errno %d: %s", |
1009 | 0 | error, curlx_strerror(error, buffer, sizeof(buffer))); |
1010 | 0 | } |
1011 | 0 | else if(!Curl_addr2string((struct sockaddr *)&ssloc, slen, |
1012 | 0 | ctx->ip.local_ip, &ctx->ip.local_port)) { |
1013 | 0 | infof(data, "ssloc inet_ntop() failed with errno %d: %s", |
1014 | 0 | errno, curlx_strerror(errno, buffer, sizeof(buffer))); |
1015 | 0 | } |
1016 | 0 | } |
1017 | | #else |
1018 | | (void)data; |
1019 | | #endif |
1020 | 0 | } |
1021 | | |
1022 | | static CURLcode set_remote_ip(struct Curl_cfilter *cf, |
1023 | | struct Curl_easy *data) |
1024 | 0 | { |
1025 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1026 | | |
1027 | | /* store remote address and port used in this connection attempt */ |
1028 | 0 | ctx->ip.transport = ctx->transport; |
1029 | 0 | if(!Curl_addr2string(&ctx->addr.curl_sa_addr, |
1030 | 0 | (curl_socklen_t)ctx->addr.addrlen, |
1031 | 0 | ctx->ip.remote_ip, &ctx->ip.remote_port)) { |
1032 | 0 | char buffer[STRERROR_LEN]; |
1033 | |
|
1034 | 0 | ctx->error = errno; |
1035 | | /* malformed address or bug in inet_ntop, try next address */ |
1036 | 0 | failf(data, "curl_sa_addr inet_ntop() failed with errno %d: %s", |
1037 | 0 | errno, curlx_strerror(errno, buffer, sizeof(buffer))); |
1038 | 0 | return CURLE_FAILED_INIT; |
1039 | 0 | } |
1040 | 0 | return CURLE_OK; |
1041 | 0 | } |
1042 | | |
1043 | | /* to figure out the type of the socket safely, remove the possibly ORed |
1044 | | bits before comparing */ |
1045 | | static int cf_socktype(int x) |
1046 | 0 | { |
1047 | 0 | #ifdef SOCK_CLOEXEC |
1048 | 0 | x &= ~SOCK_CLOEXEC; |
1049 | 0 | #endif |
1050 | 0 | #ifdef SOCK_NONBLOCK |
1051 | 0 | x &= ~SOCK_NONBLOCK; |
1052 | 0 | #endif |
1053 | 0 | return x; |
1054 | 0 | } |
1055 | | |
1056 | | static CURLcode cf_socket_open(struct Curl_cfilter *cf, |
1057 | | struct Curl_easy *data) |
1058 | 0 | { |
1059 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1060 | 0 | int error = 0; |
1061 | 0 | bool isconnected = FALSE; |
1062 | 0 | CURLcode result = CURLE_COULDNT_CONNECT; |
1063 | 0 | bool is_tcp; |
1064 | |
|
1065 | 0 | DEBUGASSERT(ctx->sock == CURL_SOCKET_BAD); |
1066 | 0 | ctx->started_at = *Curl_pgrs_now(data); |
1067 | 0 | #ifdef SOCK_NONBLOCK |
1068 | | /* Do not tuck SOCK_NONBLOCK into socktype when opensocket callback is set |
1069 | | * because we would not know how socketype is about to be used in the |
1070 | | * callback, SOCK_NONBLOCK might get factored out before calling socket(). |
1071 | | */ |
1072 | 0 | if(!data->set.fopensocket) |
1073 | 0 | ctx->addr.socktype |= SOCK_NONBLOCK; |
1074 | 0 | #endif |
1075 | 0 | result = socket_open(data, &ctx->addr, &ctx->sock); |
1076 | 0 | #ifdef SOCK_NONBLOCK |
1077 | | /* Restore the socktype after the socket is created. */ |
1078 | 0 | if(!data->set.fopensocket) |
1079 | 0 | ctx->addr.socktype &= ~SOCK_NONBLOCK; |
1080 | 0 | #endif |
1081 | 0 | if(result) |
1082 | 0 | goto out; |
1083 | | |
1084 | 0 | result = set_remote_ip(cf, data); |
1085 | 0 | if(result) |
1086 | 0 | goto out; |
1087 | | |
1088 | 0 | #ifdef USE_IPV6 |
1089 | 0 | if(ctx->addr.family == AF_INET6) { |
1090 | | #ifdef USE_WINSOCK |
1091 | | /* Turn on support for IPv4-mapped IPv6 addresses. |
1092 | | * Linux kernel, NetBSD, FreeBSD, Darwin, lwIP: default is off; |
1093 | | * Windows Vista and later: default is on; |
1094 | | * DragonFly BSD: acts like off, and dummy setting; |
1095 | | * OpenBSD and earlier Windows: unsupported. |
1096 | | * Linux: controlled by /proc/sys/net/ipv6/bindv6only. |
1097 | | */ |
1098 | | int on = 0; |
1099 | | (void)setsockopt(ctx->sock, IPPROTO_IPV6, IPV6_V6ONLY, |
1100 | | (void *)&on, sizeof(on)); |
1101 | | #endif |
1102 | 0 | infof(data, " Trying [%s]:%d...", ctx->ip.remote_ip, ctx->ip.remote_port); |
1103 | 0 | } |
1104 | 0 | else |
1105 | 0 | #endif |
1106 | 0 | infof(data, " Trying %s:%d...", ctx->ip.remote_ip, ctx->ip.remote_port); |
1107 | |
|
1108 | 0 | #ifdef USE_IPV6 |
1109 | 0 | is_tcp = (ctx->addr.family == AF_INET || |
1110 | 0 | ctx->addr.family == AF_INET6) && |
1111 | 0 | cf_socktype(ctx->addr.socktype) == SOCK_STREAM; |
1112 | | #else |
1113 | | is_tcp = (ctx->addr.family == AF_INET) && |
1114 | | cf_socktype(ctx->addr.socktype) == SOCK_STREAM; |
1115 | | #endif |
1116 | 0 | if(is_tcp && data->set.tcp_nodelay) |
1117 | 0 | tcpnodelay(cf, data, ctx->sock); |
1118 | |
|
1119 | 0 | if(is_tcp && data->set.tcp_keepalive) |
1120 | 0 | tcpkeepalive(cf, data, ctx->sock); |
1121 | |
|
1122 | 0 | if(data->set.fsockopt) { |
1123 | | /* activate callback for setting socket options */ |
1124 | 0 | Curl_set_in_callback(data, TRUE); |
1125 | 0 | error = data->set.fsockopt(data->set.sockopt_client, |
1126 | 0 | ctx->sock, |
1127 | 0 | CURLSOCKTYPE_IPCXN); |
1128 | 0 | Curl_set_in_callback(data, FALSE); |
1129 | |
|
1130 | 0 | if(error == CURL_SOCKOPT_ALREADY_CONNECTED) |
1131 | 0 | isconnected = TRUE; |
1132 | 0 | else if(error) { |
1133 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
1134 | 0 | goto out; |
1135 | 0 | } |
1136 | 0 | } |
1137 | | |
1138 | 0 | #ifndef CURL_DISABLE_BINDLOCAL |
1139 | | /* possibly bind the local end to an IP, interface or port */ |
1140 | 0 | if(ctx->addr.family == AF_INET |
1141 | 0 | #ifdef USE_IPV6 |
1142 | 0 | || ctx->addr.family == AF_INET6 |
1143 | 0 | #endif |
1144 | 0 | ) { |
1145 | 0 | result = bindlocal(data, cf->conn, ctx->sock, ctx->addr.family, |
1146 | 0 | Curl_ipv6_scope(&ctx->addr.curl_sa_addr)); |
1147 | 0 | if(result) { |
1148 | 0 | if(result == CURLE_UNSUPPORTED_PROTOCOL) { |
1149 | | /* The address family is not supported on this interface. |
1150 | | We can continue trying addresses */ |
1151 | 0 | result = CURLE_COULDNT_CONNECT; |
1152 | 0 | } |
1153 | 0 | goto out; |
1154 | 0 | } |
1155 | 0 | } |
1156 | 0 | #endif |
1157 | | |
1158 | | #ifndef SOCK_NONBLOCK |
1159 | | /* Set socket non-blocking, must be a non-blocking socket for |
1160 | | * a non-blocking connect. */ |
1161 | | error = curlx_nonblock(ctx->sock, TRUE); |
1162 | | if(error < 0) { |
1163 | | result = CURLE_UNSUPPORTED_PROTOCOL; |
1164 | | ctx->error = SOCKERRNO; |
1165 | | goto out; |
1166 | | } |
1167 | | #else |
1168 | 0 | if(data->set.fopensocket) { |
1169 | | /* Set socket non-blocking, must be a non-blocking socket for |
1170 | | * a non-blocking connect. */ |
1171 | 0 | error = curlx_nonblock(ctx->sock, TRUE); |
1172 | 0 | if(error < 0) { |
1173 | 0 | result = CURLE_UNSUPPORTED_PROTOCOL; |
1174 | 0 | ctx->error = SOCKERRNO; |
1175 | 0 | goto out; |
1176 | 0 | } |
1177 | 0 | } |
1178 | 0 | #endif |
1179 | 0 | ctx->sock_connected = (cf_socktype(ctx->addr.socktype) != SOCK_DGRAM); |
1180 | 0 | out: |
1181 | 0 | if(result) { |
1182 | 0 | if(ctx->sock != CURL_SOCKET_BAD) { |
1183 | 0 | socket_close(data, cf->conn, TRUE, ctx->sock); |
1184 | 0 | ctx->sock = CURL_SOCKET_BAD; |
1185 | 0 | } |
1186 | 0 | } |
1187 | 0 | else if(isconnected) { |
1188 | 0 | set_local_ip(cf, data); |
1189 | 0 | ctx->connected_at = *Curl_pgrs_now(data); |
1190 | 0 | cf->connected = TRUE; |
1191 | 0 | } |
1192 | 0 | CURL_TRC_CF(data, cf, "cf_socket_open() -> %d, fd=%" FMT_SOCKET_T, |
1193 | 0 | result, ctx->sock); |
1194 | 0 | return result; |
1195 | 0 | } |
1196 | | |
1197 | | static int do_connect(struct Curl_cfilter *cf, struct Curl_easy *data, |
1198 | | bool is_tcp_fastopen) |
1199 | 0 | { |
1200 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1201 | 0 | #ifdef TCP_FASTOPEN_CONNECT |
1202 | 0 | int optval = 1; |
1203 | 0 | #endif |
1204 | 0 | int rc = -1; |
1205 | |
|
1206 | 0 | (void)data; |
1207 | 0 | if(is_tcp_fastopen) { |
1208 | | #ifdef CONNECT_DATA_IDEMPOTENT /* Darwin */ |
1209 | | # ifdef HAVE_BUILTIN_AVAILABLE |
1210 | | /* while connectx function is available since macOS 10.11 / iOS 9, |
1211 | | it did not have the interface declared correctly until |
1212 | | Xcode 9 / macOS SDK 10.13 */ |
1213 | | if(__builtin_available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *)) { |
1214 | | sa_endpoints_t endpoints; |
1215 | | endpoints.sae_srcif = 0; |
1216 | | endpoints.sae_srcaddr = NULL; |
1217 | | endpoints.sae_srcaddrlen = 0; |
1218 | | endpoints.sae_dstaddr = &ctx->addr.curl_sa_addr; |
1219 | | endpoints.sae_dstaddrlen = ctx->addr.addrlen; |
1220 | | |
1221 | | rc = connectx(ctx->sock, &endpoints, SAE_ASSOCID_ANY, |
1222 | | CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT, |
1223 | | NULL, 0, NULL, NULL); |
1224 | | } |
1225 | | else { |
1226 | | rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, ctx->addr.addrlen); |
1227 | | } |
1228 | | # else |
1229 | | rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, ctx->addr.addrlen); |
1230 | | # endif /* HAVE_BUILTIN_AVAILABLE */ |
1231 | | #elif defined(TCP_FASTOPEN_CONNECT) /* Linux >= 4.11 */ |
1232 | 0 | if(setsockopt(ctx->sock, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, |
1233 | 0 | (void *)&optval, sizeof(optval)) < 0) |
1234 | 0 | CURL_TRC_CF(data, cf, "Failed to enable TCP Fast Open on fd %" |
1235 | 0 | FMT_SOCKET_T, ctx->sock); |
1236 | |
|
1237 | 0 | rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, ctx->addr.addrlen); |
1238 | | #elif defined(MSG_FASTOPEN) /* old Linux */ |
1239 | | if(Curl_conn_is_ssl(cf->conn, cf->sockindex)) |
1240 | | rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, ctx->addr.addrlen); |
1241 | | else |
1242 | | rc = 0; /* Do nothing */ |
1243 | | #endif |
1244 | 0 | } |
1245 | 0 | else { |
1246 | 0 | rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, |
1247 | 0 | (curl_socklen_t)ctx->addr.addrlen); |
1248 | 0 | } |
1249 | 0 | return rc; |
1250 | 0 | } |
1251 | | |
1252 | | static CURLcode cf_tcp_connect(struct Curl_cfilter *cf, |
1253 | | struct Curl_easy *data, |
1254 | | bool *done) |
1255 | 0 | { |
1256 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1257 | 0 | CURLcode result = CURLE_COULDNT_CONNECT; |
1258 | 0 | int rc = 0; |
1259 | |
|
1260 | 0 | if(cf->connected) { |
1261 | 0 | *done = TRUE; |
1262 | 0 | return CURLE_OK; |
1263 | 0 | } |
1264 | | |
1265 | 0 | *done = FALSE; /* a negative world view is best */ |
1266 | 0 | if(ctx->sock == CURL_SOCKET_BAD) { |
1267 | 0 | int error; |
1268 | |
|
1269 | 0 | result = cf_socket_open(cf, data); |
1270 | 0 | if(result) |
1271 | 0 | goto out; |
1272 | | |
1273 | 0 | if(cf->connected) { |
1274 | 0 | *done = TRUE; |
1275 | 0 | return CURLE_OK; |
1276 | 0 | } |
1277 | | |
1278 | | /* Connect TCP socket */ |
1279 | 0 | rc = do_connect(cf, data, (bool)cf->conn->bits.tcp_fastopen); |
1280 | 0 | error = SOCKERRNO; |
1281 | 0 | set_local_ip(cf, data); |
1282 | 0 | CURL_TRC_CF(data, cf, "local address %s port %d...", |
1283 | 0 | ctx->ip.local_ip, ctx->ip.local_port); |
1284 | 0 | if(rc == -1) { |
1285 | 0 | result = socket_connect_result(data, ctx->ip.remote_ip, error); |
1286 | 0 | goto out; |
1287 | 0 | } |
1288 | 0 | } |
1289 | | |
1290 | | #ifdef mpeix |
1291 | | /* Call this function once now, and ignore the results. We do this to |
1292 | | "clear" the error state on the socket so that we can later read it |
1293 | | reliably. This is reported necessary on the MPE/iX operating |
1294 | | system. */ |
1295 | | (void)verifyconnect(ctx->sock, NULL); |
1296 | | #endif |
1297 | | /* check socket for connect */ |
1298 | 0 | rc = SOCKET_WRITABLE(ctx->sock, 0); |
1299 | |
|
1300 | 0 | if(rc == 0) { /* no connection yet */ |
1301 | 0 | CURL_TRC_CF(data, cf, "not connected yet on fd=%" FMT_SOCKET_T, ctx->sock); |
1302 | 0 | return CURLE_OK; |
1303 | 0 | } |
1304 | 0 | else if(rc == CURL_CSELECT_OUT || cf->conn->bits.tcp_fastopen) { |
1305 | 0 | if(verifyconnect(ctx->sock, &ctx->error)) { |
1306 | | /* we are connected with TCP, awesome! */ |
1307 | 0 | ctx->connected_at = *Curl_pgrs_now(data); |
1308 | 0 | set_local_ip(cf, data); |
1309 | 0 | *done = TRUE; |
1310 | 0 | cf->connected = TRUE; |
1311 | 0 | CURL_TRC_CF(data, cf, "connected on fd=%" FMT_SOCKET_T, ctx->sock); |
1312 | 0 | return CURLE_OK; |
1313 | 0 | } |
1314 | 0 | } |
1315 | 0 | else if(rc & CURL_CSELECT_ERR) { |
1316 | 0 | (void)verifyconnect(ctx->sock, &ctx->error); |
1317 | 0 | result = CURLE_COULDNT_CONNECT; |
1318 | 0 | } |
1319 | | |
1320 | 0 | out: |
1321 | 0 | if(result) { |
1322 | 0 | if(ctx->error) { |
1323 | 0 | VERBOSE(char buffer[STRERROR_LEN]); |
1324 | 0 | set_local_ip(cf, data); |
1325 | 0 | data->state.os_errno = ctx->error; |
1326 | 0 | SET_SOCKERRNO(ctx->error); |
1327 | 0 | infof(data, "connect to %s port %u from %s port %d failed: %s", |
1328 | 0 | ctx->ip.remote_ip, ctx->ip.remote_port, |
1329 | 0 | ctx->ip.local_ip, ctx->ip.local_port, |
1330 | 0 | curlx_strerror(ctx->error, buffer, sizeof(buffer))); |
1331 | 0 | } |
1332 | 0 | if(ctx->sock != CURL_SOCKET_BAD) { |
1333 | 0 | socket_close(data, cf->conn, TRUE, ctx->sock); |
1334 | 0 | ctx->sock = CURL_SOCKET_BAD; |
1335 | 0 | } |
1336 | 0 | *done = FALSE; |
1337 | 0 | } |
1338 | 0 | return result; |
1339 | 0 | } |
1340 | | |
1341 | | static CURLcode cf_socket_adjust_pollset(struct Curl_cfilter *cf, |
1342 | | struct Curl_easy *data, |
1343 | | struct easy_pollset *ps) |
1344 | 0 | { |
1345 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1346 | 0 | CURLcode result = CURLE_OK; |
1347 | |
|
1348 | 0 | if(ctx->sock != CURL_SOCKET_BAD) { |
1349 | | /* A listening socket filter needs to be connected before the accept |
1350 | | * for some weird FTP interaction. This should be rewritten, so that |
1351 | | * FTP no longer does the socket checks and accept calls and delegates |
1352 | | * all that to the filter. */ |
1353 | 0 | if(ctx->listening) { |
1354 | 0 | result = Curl_pollset_set_in_only(data, ps, ctx->sock); |
1355 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, listening, POLLIN fd=%" |
1356 | 0 | FMT_SOCKET_T, ctx->sock); |
1357 | 0 | } |
1358 | 0 | else if(!cf->connected) { |
1359 | 0 | result = Curl_pollset_set_out_only(data, ps, ctx->sock); |
1360 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, !connected, POLLOUT fd=%" |
1361 | 0 | FMT_SOCKET_T, ctx->sock); |
1362 | 0 | } |
1363 | 0 | else if(!ctx->active) { |
1364 | 0 | result = Curl_pollset_add_in(data, ps, ctx->sock); |
1365 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, !active, POLLIN fd=%" |
1366 | 0 | FMT_SOCKET_T, ctx->sock); |
1367 | 0 | } |
1368 | 0 | } |
1369 | 0 | return result; |
1370 | 0 | } |
1371 | | |
1372 | | #ifdef USE_WINSOCK |
1373 | | |
1374 | | /* Offered by mingw-w64 v13+. MS SDK 7.0A+. */ |
1375 | | #ifndef SIO_IDEAL_SEND_BACKLOG_QUERY |
1376 | | #define SIO_IDEAL_SEND_BACKLOG_QUERY 0x4004747B |
1377 | | #endif |
1378 | | |
1379 | | static void win_update_sndbuf_size(struct Curl_easy *data, |
1380 | | struct cf_socket_ctx *ctx) |
1381 | | { |
1382 | | ULONG ideal; |
1383 | | DWORD ideallen; |
1384 | | |
1385 | | if(curlx_ptimediff_ms(Curl_pgrs_now(data), |
1386 | | &ctx->last_sndbuf_query_at) > 1000) { |
1387 | | if(!WSAIoctl(ctx->sock, SIO_IDEAL_SEND_BACKLOG_QUERY, 0, 0, |
1388 | | &ideal, sizeof(ideal), &ideallen, 0, 0) && |
1389 | | ideal != ctx->sndbuf_size && |
1390 | | !setsockopt(ctx->sock, SOL_SOCKET, SO_SNDBUF, |
1391 | | (const char *)&ideal, sizeof(ideal))) { |
1392 | | ctx->sndbuf_size = ideal; |
1393 | | } |
1394 | | ctx->last_sndbuf_query_at = *Curl_pgrs_now(data); |
1395 | | } |
1396 | | } |
1397 | | |
1398 | | #endif /* USE_WINSOCK */ |
1399 | | |
1400 | | static CURLcode cf_socket_send(struct Curl_cfilter *cf, struct Curl_easy *data, |
1401 | | const uint8_t *buf, size_t len, bool eos, |
1402 | | size_t *pnwritten) |
1403 | 0 | { |
1404 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1405 | 0 | curl_socket_t fdsave; |
1406 | 0 | ssize_t rv; |
1407 | 0 | CURLcode result = CURLE_OK; |
1408 | 0 | VERBOSE(size_t orig_len = len); |
1409 | |
|
1410 | 0 | (void)eos; |
1411 | 0 | *pnwritten = 0; |
1412 | 0 | fdsave = cf->conn->sock[cf->sockindex]; |
1413 | 0 | cf->conn->sock[cf->sockindex] = ctx->sock; |
1414 | |
|
1415 | | #ifdef DEBUGBUILD |
1416 | | /* simulate network blocking/partial writes */ |
1417 | | if(ctx->wblock_percent > 0) { |
1418 | | unsigned char c = 0; |
1419 | | Curl_rand_bytes(data, FALSE, &c, 1); |
1420 | | if(c >= ((100 - ctx->wblock_percent) * 256 / 100)) { |
1421 | | CURL_TRC_CF(data, cf, "send(len=%zu) SIMULATE EWOULDBLOCK", orig_len); |
1422 | | cf->conn->sock[cf->sockindex] = fdsave; |
1423 | | return CURLE_AGAIN; |
1424 | | } |
1425 | | } |
1426 | | if(cf->cft != &Curl_cft_udp && ctx->wpartial_percent > 0 && len > 8) { |
1427 | | len = len * ctx->wpartial_percent / 100; |
1428 | | if(!len) |
1429 | | len = 1; |
1430 | | CURL_TRC_CF(data, cf, "send(len=%zu) SIMULATE partial write of %zu bytes", |
1431 | | orig_len, len); |
1432 | | } |
1433 | | #endif |
1434 | |
|
1435 | | #if defined(MSG_FASTOPEN) && !defined(TCP_FASTOPEN_CONNECT) /* Linux */ |
1436 | | if(cf->conn->bits.tcp_fastopen) { |
1437 | | rv = sendto(ctx->sock, buf, len, MSG_FASTOPEN, |
1438 | | &ctx->addr.curl_sa_addr, ctx->addr.addrlen); |
1439 | | cf->conn->bits.tcp_fastopen = FALSE; |
1440 | | } |
1441 | | else |
1442 | | #endif |
1443 | 0 | rv = swrite(ctx->sock, buf, len); |
1444 | |
|
1445 | 0 | if(!curlx_sztouz(rv, pnwritten)) { |
1446 | 0 | int sockerr = SOCKERRNO; |
1447 | |
|
1448 | 0 | if( |
1449 | | #ifdef USE_WINSOCK |
1450 | | /* This is how Windows does it */ |
1451 | | (SOCKEWOULDBLOCK == sockerr) |
1452 | | #else |
1453 | | /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned |
1454 | | due to its inability to send off data without blocking. We therefore |
1455 | | treat both error codes the same here */ |
1456 | 0 | (SOCKEWOULDBLOCK == sockerr) || |
1457 | 0 | (EAGAIN == sockerr) || (SOCKEINTR == sockerr) || |
1458 | 0 | (SOCKEINPROGRESS == sockerr) |
1459 | 0 | #endif |
1460 | 0 | ) { |
1461 | | /* EWOULDBLOCK */ |
1462 | 0 | result = CURLE_AGAIN; |
1463 | 0 | } |
1464 | 0 | else { |
1465 | 0 | char buffer[STRERROR_LEN]; |
1466 | 0 | failf(data, "Send failure: %s", |
1467 | 0 | curlx_strerror(sockerr, buffer, sizeof(buffer))); |
1468 | 0 | data->state.os_errno = sockerr; |
1469 | 0 | result = CURLE_SEND_ERROR; |
1470 | 0 | } |
1471 | 0 | } |
1472 | |
|
1473 | | #ifdef USE_WINSOCK |
1474 | | if(!result) |
1475 | | win_update_sndbuf_size(data, ctx); |
1476 | | #endif |
1477 | |
|
1478 | 0 | CURL_TRC_CF(data, cf, "send(len=%zu) -> %d, %zu", |
1479 | 0 | orig_len, result, *pnwritten); |
1480 | 0 | cf->conn->sock[cf->sockindex] = fdsave; |
1481 | 0 | return result; |
1482 | 0 | } |
1483 | | |
1484 | | static CURLcode cf_socket_recv(struct Curl_cfilter *cf, struct Curl_easy *data, |
1485 | | char *buf, size_t len, size_t *pnread) |
1486 | 0 | { |
1487 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1488 | 0 | CURLcode result = CURLE_OK; |
1489 | 0 | ssize_t rv; |
1490 | |
|
1491 | 0 | *pnread = 0; |
1492 | | #ifdef DEBUGBUILD |
1493 | | /* simulate network blocking/partial reads */ |
1494 | | if(cf->cft != &Curl_cft_udp && ctx->rblock_percent > 0) { |
1495 | | unsigned char c = 0; |
1496 | | Curl_rand(data, &c, 1); |
1497 | | if(c >= ((100 - ctx->rblock_percent) * 256 / 100)) { |
1498 | | CURL_TRC_CF(data, cf, "recv(len=%zu) SIMULATE EWOULDBLOCK", len); |
1499 | | return CURLE_AGAIN; |
1500 | | } |
1501 | | } |
1502 | | if(cf->cft != &Curl_cft_udp && ctx->recv_max && ctx->recv_max < len) { |
1503 | | CURL_TRC_CF(data, cf, "recv(len=%zu) SIMULATE max read of %zu bytes", |
1504 | | len, ctx->recv_max); |
1505 | | len = ctx->recv_max; |
1506 | | } |
1507 | | #endif |
1508 | |
|
1509 | 0 | rv = sread(ctx->sock, buf, len); |
1510 | |
|
1511 | 0 | if(!curlx_sztouz(rv, pnread)) { |
1512 | 0 | int sockerr = SOCKERRNO; |
1513 | |
|
1514 | 0 | if( |
1515 | | #ifdef USE_WINSOCK |
1516 | | /* This is how Windows does it */ |
1517 | | (SOCKEWOULDBLOCK == sockerr) |
1518 | | #else |
1519 | | /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned |
1520 | | due to its inability to send off data without blocking. We therefore |
1521 | | treat both error codes the same here */ |
1522 | 0 | (SOCKEWOULDBLOCK == sockerr) || |
1523 | 0 | (EAGAIN == sockerr) || (SOCKEINTR == sockerr) |
1524 | 0 | #endif |
1525 | 0 | ) { |
1526 | | /* EWOULDBLOCK */ |
1527 | 0 | result = CURLE_AGAIN; |
1528 | 0 | } |
1529 | 0 | else { |
1530 | 0 | char buffer[STRERROR_LEN]; |
1531 | 0 | failf(data, "Recv failure: %s", |
1532 | 0 | curlx_strerror(sockerr, buffer, sizeof(buffer))); |
1533 | 0 | data->state.os_errno = sockerr; |
1534 | 0 | result = CURLE_RECV_ERROR; |
1535 | 0 | } |
1536 | 0 | } |
1537 | |
|
1538 | 0 | CURL_TRC_CF(data, cf, "recv(len=%zu) -> %d, %zu", len, result, *pnread); |
1539 | 0 | if(!result && !ctx->got_first_byte) { |
1540 | 0 | ctx->first_byte_at = *Curl_pgrs_now(data); |
1541 | 0 | ctx->got_first_byte = TRUE; |
1542 | 0 | } |
1543 | 0 | return result; |
1544 | 0 | } |
1545 | | |
1546 | | static void cf_socket_update_data(struct Curl_cfilter *cf, |
1547 | | struct Curl_easy *data) |
1548 | 0 | { |
1549 | | /* Update the IP info held in the transfer, if we have that. */ |
1550 | 0 | if(cf->connected && (cf->sockindex == FIRSTSOCKET)) { |
1551 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1552 | 0 | data->info.primary = ctx->ip; |
1553 | | /* not sure if this is redundant... */ |
1554 | 0 | data->info.conn_remote_port = cf->conn->remote_port; |
1555 | 0 | } |
1556 | 0 | } |
1557 | | |
1558 | | static void cf_socket_active(struct Curl_cfilter *cf, struct Curl_easy *data) |
1559 | 0 | { |
1560 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1561 | | |
1562 | | /* use this socket from now on */ |
1563 | 0 | cf->conn->sock[cf->sockindex] = ctx->sock; |
1564 | 0 | set_local_ip(cf, data); |
1565 | 0 | #ifdef USE_IPV6 |
1566 | 0 | if(cf->sockindex == FIRSTSOCKET) |
1567 | 0 | cf->conn->bits.ipv6 = (ctx->addr.family == AF_INET6); |
1568 | 0 | #endif |
1569 | 0 | ctx->active = TRUE; |
1570 | 0 | } |
1571 | | |
1572 | | static CURLcode cf_socket_cntrl(struct Curl_cfilter *cf, |
1573 | | struct Curl_easy *data, |
1574 | | int event, int arg1, void *arg2) |
1575 | 0 | { |
1576 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1577 | |
|
1578 | 0 | (void)arg1; |
1579 | 0 | (void)arg2; |
1580 | 0 | switch(event) { |
1581 | 0 | case CF_CTRL_CONN_INFO_UPDATE: |
1582 | 0 | cf_socket_active(cf, data); |
1583 | 0 | cf_socket_update_data(cf, data); |
1584 | 0 | break; |
1585 | 0 | case CF_CTRL_DATA_SETUP: |
1586 | 0 | cf_socket_update_data(cf, data); |
1587 | 0 | break; |
1588 | 0 | case CF_CTRL_FORGET_SOCKET: |
1589 | 0 | ctx->sock = CURL_SOCKET_BAD; |
1590 | 0 | break; |
1591 | 0 | } |
1592 | 0 | return CURLE_OK; |
1593 | 0 | } |
1594 | | |
1595 | | static bool cf_socket_conn_is_alive(struct Curl_cfilter *cf, |
1596 | | struct Curl_easy *data, |
1597 | | bool *input_pending) |
1598 | 0 | { |
1599 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1600 | 0 | struct pollfd pfd[1]; |
1601 | 0 | int r; |
1602 | |
|
1603 | 0 | *input_pending = FALSE; |
1604 | |
|
1605 | 0 | if(!ctx || ctx->sock == CURL_SOCKET_BAD) |
1606 | 0 | return FALSE; |
1607 | | |
1608 | | /* Check with 0 timeout if there are any events pending on the socket */ |
1609 | 0 | pfd[0].fd = ctx->sock; |
1610 | 0 | pfd[0].events = POLLRDNORM | POLLIN | POLLRDBAND | POLLPRI; |
1611 | 0 | pfd[0].revents = 0; |
1612 | |
|
1613 | 0 | r = Curl_poll(pfd, 1, 0); |
1614 | 0 | if(r < 0) { |
1615 | 0 | CURL_TRC_CF(data, cf, "is_alive: poll error, assume dead"); |
1616 | 0 | return FALSE; |
1617 | 0 | } |
1618 | 0 | else if(r == 0) { |
1619 | 0 | CURL_TRC_CF(data, cf, "is_alive: poll timeout, assume alive"); |
1620 | 0 | return TRUE; |
1621 | 0 | } |
1622 | 0 | else if(pfd[0].revents & (POLLERR | POLLHUP | POLLPRI | POLLNVAL)) { |
1623 | 0 | CURL_TRC_CF(data, cf, "is_alive: err/hup/etc events, assume dead"); |
1624 | 0 | return FALSE; |
1625 | 0 | } |
1626 | | |
1627 | 0 | CURL_TRC_CF(data, cf, "is_alive: valid events, looks alive"); |
1628 | 0 | *input_pending = TRUE; |
1629 | 0 | return TRUE; |
1630 | 0 | } |
1631 | | |
1632 | | static CURLcode cf_socket_query(struct Curl_cfilter *cf, |
1633 | | struct Curl_easy *data, |
1634 | | int query, int *pres1, void *pres2) |
1635 | 0 | { |
1636 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1637 | |
|
1638 | 0 | switch(query) { |
1639 | 0 | case CF_QUERY_SOCKET: |
1640 | 0 | DEBUGASSERT(pres2); |
1641 | 0 | *((curl_socket_t *)pres2) = ctx->sock; |
1642 | 0 | return CURLE_OK; |
1643 | 0 | case CF_QUERY_TRANSPORT: |
1644 | 0 | DEBUGASSERT(pres1); |
1645 | 0 | *pres1 = ctx->transport; |
1646 | 0 | return CURLE_OK; |
1647 | 0 | case CF_QUERY_REMOTE_ADDR: |
1648 | 0 | DEBUGASSERT(pres2); |
1649 | 0 | *((const struct Curl_sockaddr_ex **)pres2) = cf->connected ? |
1650 | 0 | &ctx->addr : NULL; |
1651 | 0 | return CURLE_OK; |
1652 | 0 | case CF_QUERY_CONNECT_REPLY_MS: |
1653 | 0 | if(ctx->got_first_byte) { |
1654 | 0 | timediff_t ms = curlx_ptimediff_ms(&ctx->first_byte_at, |
1655 | 0 | &ctx->started_at); |
1656 | 0 | *pres1 = (ms < INT_MAX) ? (int)ms : INT_MAX; |
1657 | 0 | } |
1658 | 0 | else |
1659 | 0 | *pres1 = -1; |
1660 | 0 | return CURLE_OK; |
1661 | 0 | case CF_QUERY_TIMER_CONNECT: { |
1662 | 0 | struct curltime *when = pres2; |
1663 | 0 | switch(ctx->transport) { |
1664 | 0 | case TRNSPRT_UDP: |
1665 | 0 | case TRNSPRT_QUIC: |
1666 | | /* Since UDP connected sockets work different from TCP, we use the |
1667 | | * time of the first byte from the peer as the "connect" time. */ |
1668 | 0 | if(ctx->got_first_byte) { |
1669 | 0 | *when = ctx->first_byte_at; |
1670 | 0 | break; |
1671 | 0 | } |
1672 | 0 | FALLTHROUGH(); |
1673 | 0 | default: |
1674 | 0 | *when = ctx->connected_at; |
1675 | 0 | break; |
1676 | 0 | } |
1677 | 0 | return CURLE_OK; |
1678 | 0 | } |
1679 | 0 | case CF_QUERY_IP_INFO: |
1680 | 0 | #ifdef USE_IPV6 |
1681 | 0 | *pres1 = (ctx->addr.family == AF_INET6); |
1682 | | #else |
1683 | | *pres1 = FALSE; |
1684 | | #endif |
1685 | 0 | *(struct ip_quadruple *)pres2 = ctx->ip; |
1686 | 0 | return CURLE_OK; |
1687 | 0 | default: |
1688 | 0 | break; |
1689 | 0 | } |
1690 | 0 | return cf->next ? |
1691 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
1692 | 0 | CURLE_UNKNOWN_OPTION; |
1693 | 0 | } |
1694 | | |
1695 | | struct Curl_cftype Curl_cft_tcp = { |
1696 | | "TCP", |
1697 | | CF_TYPE_IP_CONNECT, |
1698 | | CURL_LOG_LVL_NONE, |
1699 | | cf_socket_destroy, |
1700 | | cf_tcp_connect, |
1701 | | cf_socket_close, |
1702 | | cf_socket_shutdown, |
1703 | | cf_socket_adjust_pollset, |
1704 | | Curl_cf_def_data_pending, |
1705 | | cf_socket_send, |
1706 | | cf_socket_recv, |
1707 | | cf_socket_cntrl, |
1708 | | cf_socket_conn_is_alive, |
1709 | | Curl_cf_def_conn_keep_alive, |
1710 | | cf_socket_query, |
1711 | | }; |
1712 | | |
1713 | | CURLcode Curl_cf_tcp_create(struct Curl_cfilter **pcf, |
1714 | | struct Curl_easy *data, |
1715 | | struct connectdata *conn, |
1716 | | const struct Curl_addrinfo *ai, |
1717 | | uint8_t transport) |
1718 | 0 | { |
1719 | 0 | struct cf_socket_ctx *ctx = NULL; |
1720 | 0 | struct Curl_cfilter *cf = NULL; |
1721 | 0 | CURLcode result; |
1722 | |
|
1723 | 0 | (void)data; |
1724 | 0 | (void)conn; |
1725 | 0 | DEBUGASSERT(transport == TRNSPRT_TCP); |
1726 | 0 | if(!ai) { |
1727 | 0 | result = CURLE_BAD_FUNCTION_ARGUMENT; |
1728 | 0 | goto out; |
1729 | 0 | } |
1730 | | |
1731 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
1732 | 0 | if(!ctx) { |
1733 | 0 | result = CURLE_OUT_OF_MEMORY; |
1734 | 0 | goto out; |
1735 | 0 | } |
1736 | | |
1737 | 0 | result = cf_socket_ctx_init(ctx, ai, transport); |
1738 | 0 | if(result) |
1739 | 0 | goto out; |
1740 | | |
1741 | 0 | result = Curl_cf_create(&cf, &Curl_cft_tcp, ctx); |
1742 | |
|
1743 | 0 | out: |
1744 | 0 | *pcf = (!result) ? cf : NULL; |
1745 | 0 | if(result) { |
1746 | 0 | Curl_safefree(cf); |
1747 | 0 | Curl_safefree(ctx); |
1748 | 0 | } |
1749 | |
|
1750 | 0 | return result; |
1751 | 0 | } |
1752 | | |
1753 | | #ifdef __linux__ |
1754 | | static void linux_quic_mtu(struct cf_socket_ctx *ctx) |
1755 | 0 | { |
1756 | 0 | int val; |
1757 | 0 | switch(ctx->addr.family) { |
1758 | 0 | #ifdef IP_MTU_DISCOVER |
1759 | 0 | case AF_INET: |
1760 | 0 | val = IP_PMTUDISC_DO; |
1761 | 0 | (void)setsockopt(ctx->sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, |
1762 | 0 | sizeof(val)); |
1763 | 0 | break; |
1764 | 0 | #endif |
1765 | 0 | #ifdef IPV6_MTU_DISCOVER |
1766 | 0 | case AF_INET6: |
1767 | 0 | val = IPV6_PMTUDISC_DO; |
1768 | 0 | (void)setsockopt(ctx->sock, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val, |
1769 | 0 | sizeof(val)); |
1770 | 0 | break; |
1771 | 0 | #endif |
1772 | 0 | } |
1773 | 0 | } |
1774 | | #else |
1775 | | #define linux_quic_mtu(x) |
1776 | | #endif |
1777 | | |
1778 | | #if defined(UDP_GRO) && \ |
1779 | | (defined(HAVE_SENDMMSG) || defined(HAVE_SENDMSG)) && \ |
1780 | | ((defined(USE_NGTCP2) && defined(USE_NGHTTP3)) || defined(USE_QUICHE)) |
1781 | | static void linux_quic_gro(struct cf_socket_ctx *ctx) |
1782 | | { |
1783 | | int one = 1; |
1784 | | (void)setsockopt(ctx->sock, IPPROTO_UDP, UDP_GRO, &one, |
1785 | | (socklen_t)sizeof(one)); |
1786 | | } |
1787 | | #else |
1788 | | #define linux_quic_gro(x) |
1789 | | #endif |
1790 | | |
1791 | | static CURLcode cf_udp_setup_quic(struct Curl_cfilter *cf, |
1792 | | struct Curl_easy *data) |
1793 | 0 | { |
1794 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1795 | 0 | int rc; |
1796 | | |
1797 | | /* QUIC needs a connected socket, nonblocking */ |
1798 | 0 | DEBUGASSERT(ctx->sock != CURL_SOCKET_BAD); |
1799 | | |
1800 | | /* error: The 1st argument to 'connect' is -1 but should be >= 0 |
1801 | | NOLINTNEXTLINE(clang-analyzer-unix.StdCLibraryFunctions) */ |
1802 | 0 | rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, |
1803 | 0 | (curl_socklen_t)ctx->addr.addrlen); |
1804 | 0 | if(rc == -1) { |
1805 | 0 | return socket_connect_result(data, ctx->ip.remote_ip, SOCKERRNO); |
1806 | 0 | } |
1807 | 0 | ctx->sock_connected = TRUE; |
1808 | 0 | set_local_ip(cf, data); |
1809 | 0 | CURL_TRC_CF(data, cf, "%s socket %" FMT_SOCKET_T |
1810 | 0 | " connected: [%s:%d] -> [%s:%d]", |
1811 | 0 | (ctx->transport == TRNSPRT_QUIC) ? "QUIC" : "UDP", |
1812 | 0 | ctx->sock, ctx->ip.local_ip, ctx->ip.local_port, |
1813 | 0 | ctx->ip.remote_ip, ctx->ip.remote_port); |
1814 | | |
1815 | | /* Currently, cf->ctx->sock is always non-blocking because the only |
1816 | | * caller to cf_udp_setup_quic() is cf_udp_connect() that passes the |
1817 | | * non-blocking socket created by cf_socket_open() to it. Thus, we |
1818 | | * do not need to call curlx_nonblock() in cf_udp_setup_quic() anymore. |
1819 | | */ |
1820 | 0 | linux_quic_mtu(ctx); |
1821 | 0 | linux_quic_gro(ctx); |
1822 | |
|
1823 | 0 | return CURLE_OK; |
1824 | 0 | } |
1825 | | |
1826 | | static CURLcode cf_udp_connect(struct Curl_cfilter *cf, |
1827 | | struct Curl_easy *data, |
1828 | | bool *done) |
1829 | 0 | { |
1830 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1831 | 0 | CURLcode result = CURLE_COULDNT_CONNECT; |
1832 | |
|
1833 | 0 | if(cf->connected) { |
1834 | 0 | *done = TRUE; |
1835 | 0 | return CURLE_OK; |
1836 | 0 | } |
1837 | | |
1838 | 0 | *done = FALSE; |
1839 | 0 | if(ctx->sock == CURL_SOCKET_BAD) { |
1840 | 0 | result = cf_socket_open(cf, data); |
1841 | 0 | if(result) { |
1842 | 0 | CURL_TRC_CF(data, cf, "cf_udp_connect(), open failed -> %d", result); |
1843 | 0 | goto out; |
1844 | 0 | } |
1845 | | |
1846 | 0 | if(ctx->transport == TRNSPRT_QUIC) { |
1847 | 0 | result = cf_udp_setup_quic(cf, data); |
1848 | 0 | if(result) |
1849 | 0 | goto out; |
1850 | 0 | CURL_TRC_CF(data, cf, "cf_udp_connect(), opened socket=%" |
1851 | 0 | FMT_SOCKET_T " (%s:%d)", |
1852 | 0 | ctx->sock, ctx->ip.local_ip, ctx->ip.local_port); |
1853 | 0 | } |
1854 | 0 | *done = TRUE; |
1855 | 0 | cf->connected = TRUE; |
1856 | 0 | } |
1857 | 0 | out: |
1858 | 0 | return result; |
1859 | 0 | } |
1860 | | |
1861 | | struct Curl_cftype Curl_cft_udp = { |
1862 | | "UDP", |
1863 | | CF_TYPE_IP_CONNECT, |
1864 | | CURL_LOG_LVL_NONE, |
1865 | | cf_socket_destroy, |
1866 | | cf_udp_connect, |
1867 | | cf_socket_close, |
1868 | | cf_socket_shutdown, |
1869 | | cf_socket_adjust_pollset, |
1870 | | Curl_cf_def_data_pending, |
1871 | | cf_socket_send, |
1872 | | cf_socket_recv, |
1873 | | cf_socket_cntrl, |
1874 | | cf_socket_conn_is_alive, |
1875 | | Curl_cf_def_conn_keep_alive, |
1876 | | cf_socket_query, |
1877 | | }; |
1878 | | |
1879 | | CURLcode Curl_cf_udp_create(struct Curl_cfilter **pcf, |
1880 | | struct Curl_easy *data, |
1881 | | struct connectdata *conn, |
1882 | | const struct Curl_addrinfo *ai, |
1883 | | uint8_t transport) |
1884 | 0 | { |
1885 | 0 | struct cf_socket_ctx *ctx = NULL; |
1886 | 0 | struct Curl_cfilter *cf = NULL; |
1887 | 0 | CURLcode result; |
1888 | |
|
1889 | 0 | (void)data; |
1890 | 0 | (void)conn; |
1891 | 0 | DEBUGASSERT(transport == TRNSPRT_UDP || transport == TRNSPRT_QUIC); |
1892 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
1893 | 0 | if(!ctx) { |
1894 | 0 | result = CURLE_OUT_OF_MEMORY; |
1895 | 0 | goto out; |
1896 | 0 | } |
1897 | | |
1898 | 0 | result = cf_socket_ctx_init(ctx, ai, transport); |
1899 | 0 | if(result) |
1900 | 0 | goto out; |
1901 | | |
1902 | 0 | result = Curl_cf_create(&cf, &Curl_cft_udp, ctx); |
1903 | |
|
1904 | 0 | out: |
1905 | 0 | *pcf = (!result) ? cf : NULL; |
1906 | 0 | if(result) { |
1907 | 0 | Curl_safefree(cf); |
1908 | 0 | Curl_safefree(ctx); |
1909 | 0 | } |
1910 | |
|
1911 | 0 | return result; |
1912 | 0 | } |
1913 | | |
1914 | | /* this is the TCP filter which can also handle this case */ |
1915 | | struct Curl_cftype Curl_cft_unix = { |
1916 | | "UNIX", |
1917 | | CF_TYPE_IP_CONNECT, |
1918 | | CURL_LOG_LVL_NONE, |
1919 | | cf_socket_destroy, |
1920 | | cf_tcp_connect, |
1921 | | cf_socket_close, |
1922 | | cf_socket_shutdown, |
1923 | | cf_socket_adjust_pollset, |
1924 | | Curl_cf_def_data_pending, |
1925 | | cf_socket_send, |
1926 | | cf_socket_recv, |
1927 | | cf_socket_cntrl, |
1928 | | cf_socket_conn_is_alive, |
1929 | | Curl_cf_def_conn_keep_alive, |
1930 | | cf_socket_query, |
1931 | | }; |
1932 | | |
1933 | | CURLcode Curl_cf_unix_create(struct Curl_cfilter **pcf, |
1934 | | struct Curl_easy *data, |
1935 | | struct connectdata *conn, |
1936 | | const struct Curl_addrinfo *ai, |
1937 | | uint8_t transport) |
1938 | 0 | { |
1939 | 0 | struct cf_socket_ctx *ctx = NULL; |
1940 | 0 | struct Curl_cfilter *cf = NULL; |
1941 | 0 | CURLcode result; |
1942 | |
|
1943 | 0 | (void)data; |
1944 | 0 | (void)conn; |
1945 | 0 | DEBUGASSERT(transport == TRNSPRT_UNIX); |
1946 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
1947 | 0 | if(!ctx) { |
1948 | 0 | result = CURLE_OUT_OF_MEMORY; |
1949 | 0 | goto out; |
1950 | 0 | } |
1951 | | |
1952 | 0 | result = cf_socket_ctx_init(ctx, ai, transport); |
1953 | 0 | if(result) |
1954 | 0 | goto out; |
1955 | | |
1956 | 0 | result = Curl_cf_create(&cf, &Curl_cft_unix, ctx); |
1957 | |
|
1958 | 0 | out: |
1959 | 0 | *pcf = (!result) ? cf : NULL; |
1960 | 0 | if(result) { |
1961 | 0 | Curl_safefree(cf); |
1962 | 0 | Curl_safefree(ctx); |
1963 | 0 | } |
1964 | |
|
1965 | 0 | return result; |
1966 | 0 | } |
1967 | | |
1968 | | static timediff_t cf_tcp_accept_timeleft(struct Curl_cfilter *cf, |
1969 | | struct Curl_easy *data) |
1970 | 0 | { |
1971 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
1972 | 0 | timediff_t timeout_ms = DEFAULT_ACCEPT_TIMEOUT; |
1973 | 0 | timediff_t other_ms; |
1974 | |
|
1975 | 0 | #ifndef CURL_DISABLE_FTP |
1976 | 0 | if(data->set.accepttimeout > 0) |
1977 | 0 | timeout_ms = data->set.accepttimeout; |
1978 | 0 | #endif |
1979 | | |
1980 | | /* check if the generic timeout possibly is set shorter */ |
1981 | 0 | other_ms = Curl_timeleft_ms(data); |
1982 | 0 | if(other_ms && (other_ms < timeout_ms)) |
1983 | | /* note that this also works fine for when other_ms happens to be negative |
1984 | | due to it already having elapsed */ |
1985 | 0 | timeout_ms = other_ms; |
1986 | 0 | else { |
1987 | | /* subtract elapsed time */ |
1988 | 0 | timeout_ms -= curlx_ptimediff_ms(Curl_pgrs_now(data), &ctx->started_at); |
1989 | 0 | if(!timeout_ms) |
1990 | | /* avoid returning 0 as that means no timeout! */ |
1991 | 0 | timeout_ms = -1; |
1992 | 0 | } |
1993 | 0 | return timeout_ms; |
1994 | 0 | } |
1995 | | |
1996 | | static void cf_tcp_set_accepted_remote_ip(struct Curl_cfilter *cf, |
1997 | | struct Curl_easy *data) |
1998 | 0 | { |
1999 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
2000 | 0 | #ifdef HAVE_GETPEERNAME |
2001 | 0 | char buffer[STRERROR_LEN]; |
2002 | 0 | struct Curl_sockaddr_storage ssrem; |
2003 | 0 | curl_socklen_t plen; |
2004 | |
|
2005 | 0 | ctx->ip.remote_ip[0] = 0; |
2006 | 0 | ctx->ip.remote_port = 0; |
2007 | 0 | plen = sizeof(ssrem); |
2008 | 0 | memset(&ssrem, 0, plen); |
2009 | 0 | if(getpeername(ctx->sock, (struct sockaddr *)&ssrem, &plen)) { |
2010 | 0 | int error = SOCKERRNO; |
2011 | 0 | failf(data, "getpeername() failed with errno %d: %s", |
2012 | 0 | error, curlx_strerror(error, buffer, sizeof(buffer))); |
2013 | 0 | return; |
2014 | 0 | } |
2015 | 0 | if(!Curl_addr2string((struct sockaddr *)&ssrem, plen, |
2016 | 0 | ctx->ip.remote_ip, &ctx->ip.remote_port)) { |
2017 | 0 | failf(data, "ssrem inet_ntop() failed with errno %d: %s", |
2018 | 0 | errno, curlx_strerror(errno, buffer, sizeof(buffer))); |
2019 | 0 | return; |
2020 | 0 | } |
2021 | | #else |
2022 | | ctx->ip.remote_ip[0] = 0; |
2023 | | ctx->ip.remote_port = 0; |
2024 | | (void)data; |
2025 | | #endif |
2026 | 0 | } |
2027 | | |
2028 | | static CURLcode cf_tcp_accept_connect(struct Curl_cfilter *cf, |
2029 | | struct Curl_easy *data, |
2030 | | bool *done) |
2031 | 0 | { |
2032 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
2033 | 0 | char errbuf[STRERROR_LEN]; |
2034 | 0 | #ifdef USE_IPV6 |
2035 | 0 | struct Curl_sockaddr_storage add; |
2036 | | #else |
2037 | | struct sockaddr_in add; |
2038 | | #endif |
2039 | 0 | curl_socklen_t size = (curl_socklen_t)sizeof(add); |
2040 | 0 | curl_socket_t s_accepted = CURL_SOCKET_BAD; |
2041 | 0 | timediff_t timeout_ms; |
2042 | 0 | int socketstate = 0; |
2043 | 0 | bool incoming = FALSE; |
2044 | | |
2045 | | /* we start accepted, if we ever close, we cannot go on */ |
2046 | 0 | (void)data; |
2047 | 0 | if(cf->connected) { |
2048 | 0 | *done = TRUE; |
2049 | 0 | return CURLE_OK; |
2050 | 0 | } |
2051 | | |
2052 | 0 | *done = FALSE; |
2053 | 0 | timeout_ms = cf_tcp_accept_timeleft(cf, data); |
2054 | 0 | if(timeout_ms < 0) { |
2055 | | /* if a timeout was already reached, bail out */ |
2056 | 0 | failf(data, "Accept timeout occurred while waiting server connect"); |
2057 | 0 | return CURLE_FTP_ACCEPT_TIMEOUT; |
2058 | 0 | } |
2059 | | |
2060 | 0 | CURL_TRC_CF(data, cf, "Checking for incoming on fd=%" FMT_SOCKET_T |
2061 | 0 | " ip=%s:%d", ctx->sock, ctx->ip.local_ip, ctx->ip.local_port); |
2062 | 0 | socketstate = SOCKET_READABLE(ctx->sock, 0); |
2063 | 0 | CURL_TRC_CF(data, cf, "socket_check -> %x", socketstate); |
2064 | 0 | switch(socketstate) { |
2065 | 0 | case -1: /* error */ |
2066 | | /* let's die here */ |
2067 | 0 | failf(data, "Error while waiting for server connect"); |
2068 | 0 | return CURLE_FTP_ACCEPT_FAILED; |
2069 | 0 | default: |
2070 | 0 | if(socketstate & CURL_CSELECT_IN) { |
2071 | 0 | infof(data, "Ready to accept data connection from server"); |
2072 | 0 | incoming = TRUE; |
2073 | 0 | } |
2074 | 0 | break; |
2075 | 0 | } |
2076 | | |
2077 | 0 | if(!incoming) { |
2078 | 0 | CURL_TRC_CF(data, cf, "nothing heard from the server yet"); |
2079 | 0 | return CURLE_OK; |
2080 | 0 | } |
2081 | | |
2082 | 0 | size = sizeof(add); |
2083 | 0 | #ifdef HAVE_ACCEPT4 |
2084 | 0 | s_accepted = CURL_ACCEPT4(ctx->sock, (struct sockaddr *)&add, &size, |
2085 | 0 | SOCK_NONBLOCK | SOCK_CLOEXEC); |
2086 | | #else |
2087 | | s_accepted = CURL_ACCEPT(ctx->sock, (struct sockaddr *)&add, &size); |
2088 | | #endif |
2089 | |
|
2090 | 0 | if(s_accepted == CURL_SOCKET_BAD) { |
2091 | 0 | failf(data, "Error accept()ing server connect: %s", |
2092 | 0 | curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf))); |
2093 | 0 | return CURLE_FTP_ACCEPT_FAILED; |
2094 | 0 | } |
2095 | | #ifndef HAVE_ACCEPT4 |
2096 | | #ifdef HAVE_FCNTL |
2097 | | if(fcntl(s_accepted, F_SETFD, FD_CLOEXEC) < 0) { |
2098 | | failf(data, "fcntl set CLOEXEC: %s", |
2099 | | curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf))); |
2100 | | Curl_socket_close(data, cf->conn, s_accepted); |
2101 | | return CURLE_FTP_ACCEPT_FAILED; |
2102 | | } |
2103 | | #endif /* HAVE_FCNTL */ |
2104 | | if(curlx_nonblock(s_accepted, TRUE) < 0) { |
2105 | | failf(data, "set socket NONBLOCK: %s", |
2106 | | curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf))); |
2107 | | Curl_socket_close(data, cf->conn, s_accepted); |
2108 | | return CURLE_FTP_ACCEPT_FAILED; |
2109 | | } |
2110 | | #endif /* !HAVE_ACCEPT4 */ |
2111 | 0 | infof(data, "Connection accepted from server"); |
2112 | | |
2113 | | /* Replace any filter on SECONDARY with one listening on this socket */ |
2114 | 0 | ctx->listening = FALSE; |
2115 | 0 | ctx->accepted = TRUE; |
2116 | 0 | socket_close(data, cf->conn, TRUE, ctx->sock); |
2117 | 0 | ctx->sock = s_accepted; |
2118 | |
|
2119 | 0 | cf->conn->sock[cf->sockindex] = ctx->sock; |
2120 | 0 | cf_tcp_set_accepted_remote_ip(cf, data); |
2121 | 0 | set_local_ip(cf, data); |
2122 | 0 | ctx->active = TRUE; |
2123 | 0 | ctx->connected_at = *Curl_pgrs_now(data); |
2124 | 0 | cf->connected = TRUE; |
2125 | 0 | CURL_TRC_CF(data, cf, "accepted_set(sock=%" FMT_SOCKET_T |
2126 | 0 | ", remote=%s port=%d)", |
2127 | 0 | ctx->sock, ctx->ip.remote_ip, ctx->ip.remote_port); |
2128 | |
|
2129 | 0 | if(data->set.fsockopt) { |
2130 | 0 | int error = 0; |
2131 | | |
2132 | | /* activate callback for setting socket options */ |
2133 | 0 | Curl_set_in_callback(data, true); |
2134 | 0 | error = data->set.fsockopt(data->set.sockopt_client, |
2135 | 0 | ctx->sock, CURLSOCKTYPE_ACCEPT); |
2136 | 0 | Curl_set_in_callback(data, false); |
2137 | |
|
2138 | 0 | if(error) |
2139 | 0 | return CURLE_ABORTED_BY_CALLBACK; |
2140 | 0 | } |
2141 | 0 | *done = TRUE; |
2142 | 0 | return CURLE_OK; |
2143 | 0 | } |
2144 | | |
2145 | | struct Curl_cftype Curl_cft_tcp_accept = { |
2146 | | "TCP-ACCEPT", |
2147 | | CF_TYPE_IP_CONNECT, |
2148 | | CURL_LOG_LVL_NONE, |
2149 | | cf_socket_destroy, |
2150 | | cf_tcp_accept_connect, |
2151 | | cf_socket_close, |
2152 | | cf_socket_shutdown, |
2153 | | cf_socket_adjust_pollset, |
2154 | | Curl_cf_def_data_pending, |
2155 | | cf_socket_send, |
2156 | | cf_socket_recv, |
2157 | | cf_socket_cntrl, |
2158 | | cf_socket_conn_is_alive, |
2159 | | Curl_cf_def_conn_keep_alive, |
2160 | | cf_socket_query, |
2161 | | }; |
2162 | | |
2163 | | CURLcode Curl_conn_tcp_listen_set(struct Curl_easy *data, |
2164 | | struct connectdata *conn, |
2165 | | int sockindex, curl_socket_t *s) |
2166 | 0 | { |
2167 | 0 | CURLcode result; |
2168 | 0 | struct Curl_cfilter *cf = NULL; |
2169 | 0 | struct cf_socket_ctx *ctx = NULL; |
2170 | | |
2171 | | /* replace any existing */ |
2172 | 0 | Curl_conn_cf_discard_all(data, conn, sockindex); |
2173 | 0 | DEBUGASSERT(conn->sock[sockindex] == CURL_SOCKET_BAD); |
2174 | |
|
2175 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
2176 | 0 | if(!ctx) { |
2177 | 0 | result = CURLE_OUT_OF_MEMORY; |
2178 | 0 | goto out; |
2179 | 0 | } |
2180 | 0 | ctx->transport = TRNSPRT_TCP; |
2181 | 0 | ctx->sock = *s; |
2182 | 0 | ctx->listening = TRUE; |
2183 | 0 | ctx->accepted = FALSE; |
2184 | 0 | result = Curl_cf_create(&cf, &Curl_cft_tcp_accept, ctx); |
2185 | 0 | if(result) |
2186 | 0 | goto out; |
2187 | 0 | Curl_conn_cf_add(data, conn, sockindex, cf); |
2188 | |
|
2189 | 0 | ctx->started_at = *Curl_pgrs_now(data); |
2190 | 0 | conn->sock[sockindex] = ctx->sock; |
2191 | 0 | set_local_ip(cf, data); |
2192 | 0 | CURL_TRC_CF(data, cf, "set filter for listen socket fd=%" FMT_SOCKET_T |
2193 | 0 | " ip=%s:%d", ctx->sock, |
2194 | 0 | ctx->ip.local_ip, ctx->ip.local_port); |
2195 | |
|
2196 | 0 | out: |
2197 | 0 | if(result) { |
2198 | 0 | Curl_safefree(cf); |
2199 | 0 | Curl_safefree(ctx); |
2200 | 0 | } |
2201 | 0 | return result; |
2202 | 0 | } |
2203 | | |
2204 | | bool Curl_conn_is_tcp_listen(struct Curl_easy *data, |
2205 | | int sockindex) |
2206 | 0 | { |
2207 | 0 | struct Curl_cfilter *cf = data->conn->cfilter[sockindex]; |
2208 | 0 | while(cf) { |
2209 | 0 | if(cf->cft == &Curl_cft_tcp_accept) |
2210 | 0 | return TRUE; |
2211 | 0 | cf = cf->next; |
2212 | 0 | } |
2213 | 0 | return FALSE; |
2214 | 0 | } |
2215 | | |
2216 | | /** |
2217 | | * Return TRUE iff `cf` is a socket filter. |
2218 | | */ |
2219 | | static bool cf_is_socket(struct Curl_cfilter *cf) |
2220 | 0 | { |
2221 | 0 | return cf && (cf->cft == &Curl_cft_tcp || |
2222 | 0 | cf->cft == &Curl_cft_udp || |
2223 | 0 | cf->cft == &Curl_cft_unix || |
2224 | 0 | cf->cft == &Curl_cft_tcp_accept); |
2225 | 0 | } |
2226 | | |
2227 | | CURLcode Curl_cf_socket_peek(struct Curl_cfilter *cf, |
2228 | | struct Curl_easy *data, |
2229 | | curl_socket_t *psock, |
2230 | | const struct Curl_sockaddr_ex **paddr, |
2231 | | struct ip_quadruple *pip) |
2232 | 0 | { |
2233 | 0 | (void)data; |
2234 | 0 | if(cf_is_socket(cf) && cf->ctx) { |
2235 | 0 | struct cf_socket_ctx *ctx = cf->ctx; |
2236 | |
|
2237 | 0 | if(psock) |
2238 | 0 | *psock = ctx->sock; |
2239 | 0 | if(paddr) |
2240 | 0 | *paddr = &ctx->addr; |
2241 | 0 | if(pip) |
2242 | 0 | *pip = ctx->ip; |
2243 | 0 | return CURLE_OK; |
2244 | 0 | } |
2245 | 0 | return CURLE_FAILED_INIT; |
2246 | 0 | } |