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