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