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