/src/curl/lib/cf-ip-happy.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #ifdef HAVE_NETINET_IN_H |
27 | | #include <netinet/in.h> /* <netinet/tcp.h> may need it */ |
28 | | #endif |
29 | | #ifdef HAVE_LINUX_TCP_H |
30 | | #include <linux/tcp.h> |
31 | | #elif defined(HAVE_NETINET_TCP_H) |
32 | | #include <netinet/tcp.h> |
33 | | #endif |
34 | | #ifdef HAVE_SYS_IOCTL_H |
35 | | #include <sys/ioctl.h> |
36 | | #endif |
37 | | #ifdef HAVE_NETDB_H |
38 | | #include <netdb.h> |
39 | | #endif |
40 | | #ifdef HAVE_ARPA_INET_H |
41 | | #include <arpa/inet.h> |
42 | | #endif |
43 | | |
44 | | #ifdef __VMS |
45 | | #include <in.h> |
46 | | #include <inet.h> |
47 | | #endif |
48 | | |
49 | | #include "urldata.h" |
50 | | #include "connect.h" |
51 | | #include "cfilters.h" |
52 | | #include "cf-ip-happy.h" |
53 | | #include "curl_addrinfo.h" |
54 | | #include "curl_trc.h" |
55 | | #include "multiif.h" |
56 | | #include "progress.h" |
57 | | #include "select.h" |
58 | | #include "vquic/vquic.h" /* for quic cfilters */ |
59 | | |
60 | | |
61 | | struct transport_provider { |
62 | | uint8_t transport; |
63 | | cf_ip_connect_create *cf_create; |
64 | | }; |
65 | | |
66 | | static |
67 | | #ifndef UNITTESTS |
68 | | const |
69 | | #endif |
70 | | struct transport_provider transport_providers[] = { |
71 | | { TRNSPRT_TCP, Curl_cf_tcp_create }, |
72 | | #if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3) |
73 | | { TRNSPRT_QUIC, Curl_cf_quic_create }, |
74 | | #endif |
75 | | #ifndef CURL_DISABLE_TFTP |
76 | | { TRNSPRT_UDP, Curl_cf_udp_create }, |
77 | | #endif |
78 | | #ifdef USE_UNIX_SOCKETS |
79 | | { TRNSPRT_UNIX, Curl_cf_unix_create }, |
80 | | #endif |
81 | | }; |
82 | | |
83 | | static cf_ip_connect_create *get_cf_create(uint8_t transport) |
84 | 116k | { |
85 | 116k | size_t i; |
86 | 122k | for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) { |
87 | 122k | if(transport == transport_providers[i].transport) |
88 | 116k | return transport_providers[i].cf_create; |
89 | 122k | } |
90 | 0 | return NULL; |
91 | 116k | } |
92 | | |
93 | | #ifdef UNITTESTS |
94 | | /* used by unit2600.c */ |
95 | | void Curl_debug_set_transport_provider(uint8_t transport, |
96 | | cf_ip_connect_create *cf_create) |
97 | | { |
98 | | size_t i; |
99 | | for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) { |
100 | | if(transport == transport_providers[i].transport) { |
101 | | transport_providers[i].cf_create = cf_create; |
102 | | return; |
103 | | } |
104 | | } |
105 | | } |
106 | | #endif /* UNITTESTS */ |
107 | | |
108 | | struct cf_ai_iter { |
109 | | const struct Curl_addrinfo *head; |
110 | | const struct Curl_addrinfo *last; |
111 | | int ai_family; |
112 | | int n; |
113 | | }; |
114 | | |
115 | | static void cf_ai_iter_init(struct cf_ai_iter *iter, |
116 | | const struct Curl_addrinfo *list, |
117 | | int ai_family) |
118 | 231k | { |
119 | 231k | iter->head = list; |
120 | 231k | iter->ai_family = ai_family; |
121 | 231k | iter->last = NULL; |
122 | 231k | iter->n = -1; |
123 | 231k | } |
124 | | |
125 | | static const struct Curl_addrinfo *cf_ai_iter_next(struct cf_ai_iter *iter) |
126 | 236k | { |
127 | 236k | const struct Curl_addrinfo *addr; |
128 | 236k | if(iter->n < 0) { |
129 | 230k | iter->n++; |
130 | 345k | for(addr = iter->head; addr; addr = addr->ai_next) { |
131 | 231k | if(addr->ai_family == iter->ai_family) |
132 | 116k | break; |
133 | 231k | } |
134 | 230k | iter->last = addr; |
135 | 230k | } |
136 | 5.42k | else if(iter->last) { |
137 | 2.64k | iter->n++; |
138 | 3.05k | for(addr = iter->last->ai_next; addr; addr = addr->ai_next) { |
139 | 413 | if(addr->ai_family == iter->ai_family) |
140 | 0 | break; |
141 | 413 | } |
142 | 2.64k | iter->last = addr; |
143 | 2.64k | } |
144 | 236k | return iter->last; |
145 | 236k | } |
146 | | |
147 | | static bool cf_ai_iter_has_more(struct cf_ai_iter *iter) |
148 | 3.50M | { |
149 | 3.50M | const struct Curl_addrinfo *addr = iter->last ? iter->last->ai_next : |
150 | 3.50M | ((iter->n < 0) ? iter->head : NULL); |
151 | 3.50M | while(addr) { |
152 | 1.07k | if(addr->ai_family == iter->ai_family) |
153 | 404 | return TRUE; |
154 | 670 | addr = addr->ai_next; |
155 | 670 | } |
156 | 3.50M | return FALSE; |
157 | 3.50M | } |
158 | | |
159 | | struct cf_ip_attempt { |
160 | | struct cf_ip_attempt *next; |
161 | | const struct Curl_addrinfo *addr; /* List of addresses to try, not owned */ |
162 | | struct Curl_cfilter *cf; /* current sub-cfilter connecting */ |
163 | | cf_ip_connect_create *cf_create; |
164 | | struct curltime started; /* start of current attempt */ |
165 | | CURLcode result; |
166 | | int ai_family; |
167 | | uint8_t transport; |
168 | | int error; |
169 | | BIT(connected); /* cf has connected */ |
170 | | BIT(shutdown); /* cf has shutdown */ |
171 | | BIT(inconclusive); /* connect was not a hard failure, we |
172 | | * might talk to a restarting server */ |
173 | | }; |
174 | | |
175 | | static void cf_ip_attempt_free(struct cf_ip_attempt *a, |
176 | | struct Curl_easy *data) |
177 | 349k | { |
178 | 349k | if(a) { |
179 | 116k | if(a->cf) |
180 | 3.10k | Curl_conn_cf_discard_chain(&a->cf, data); |
181 | 116k | curlx_free(a); |
182 | 116k | } |
183 | 349k | } |
184 | | |
185 | | static CURLcode cf_ip_attempt_new(struct cf_ip_attempt **pa, |
186 | | struct Curl_cfilter *cf, |
187 | | struct Curl_easy *data, |
188 | | const struct Curl_addrinfo *addr, |
189 | | int ai_family, |
190 | | uint8_t transport, |
191 | | cf_ip_connect_create *cf_create) |
192 | 116k | { |
193 | 116k | struct Curl_cfilter *wcf; |
194 | 116k | struct cf_ip_attempt *a; |
195 | 116k | CURLcode result = CURLE_OK; |
196 | | |
197 | 116k | *pa = NULL; |
198 | 116k | a = curlx_calloc(1, sizeof(*a)); |
199 | 116k | if(!a) |
200 | 0 | return CURLE_OUT_OF_MEMORY; |
201 | | |
202 | 116k | a->addr = addr; |
203 | 116k | a->ai_family = ai_family; |
204 | 116k | a->transport = transport; |
205 | 116k | a->result = CURLE_OK; |
206 | 116k | a->cf_create = cf_create; |
207 | 116k | *pa = a; |
208 | | |
209 | 116k | result = a->cf_create(&a->cf, data, cf->conn, a->addr, transport); |
210 | 116k | if(result) |
211 | 0 | goto out; |
212 | | |
213 | | /* the new filter might have sub-filters */ |
214 | 233k | for(wcf = a->cf; wcf; wcf = wcf->next) { |
215 | 116k | wcf->conn = cf->conn; |
216 | 116k | wcf->sockindex = cf->sockindex; |
217 | 116k | } |
218 | | |
219 | 116k | out: |
220 | 116k | if(result) { |
221 | 0 | cf_ip_attempt_free(a, data); |
222 | 0 | *pa = NULL; |
223 | 0 | } |
224 | 116k | return result; |
225 | 116k | } |
226 | | |
227 | | static CURLcode cf_ip_attempt_connect(struct cf_ip_attempt *a, |
228 | | struct Curl_easy *data, |
229 | | bool *connected) |
230 | 993k | { |
231 | 993k | *connected = (bool)a->connected; |
232 | 993k | if(!a->result && !*connected) { |
233 | | /* evaluate again */ |
234 | 992k | a->result = Curl_conn_cf_connect(a->cf, data, connected); |
235 | | |
236 | 992k | if(!a->result) { |
237 | 990k | if(*connected) { |
238 | 113k | a->connected = TRUE; |
239 | 113k | } |
240 | 990k | } |
241 | 2.64k | else if(a->result == CURLE_WEIRD_SERVER_REPLY) |
242 | 0 | a->inconclusive = TRUE; |
243 | 992k | } |
244 | 993k | return a->result; |
245 | 993k | } |
246 | | |
247 | | struct cf_ip_ballers { |
248 | | struct cf_ip_attempt *running; |
249 | | struct cf_ip_attempt *winner; |
250 | | struct cf_ai_iter addr_iter; |
251 | | #ifdef USE_IPV6 |
252 | | struct cf_ai_iter ipv6_iter; |
253 | | #endif |
254 | | cf_ip_connect_create *cf_create; /* for creating cf */ |
255 | | struct curltime started; |
256 | | struct curltime last_attempt_started; |
257 | | timediff_t attempt_delay_ms; |
258 | | int last_attempt_ai_family; |
259 | | uint8_t transport; |
260 | | }; |
261 | | |
262 | | static CURLcode cf_ip_attempt_restart(struct cf_ip_attempt *a, |
263 | | struct Curl_cfilter *cf, |
264 | | struct Curl_easy *data) |
265 | 0 | { |
266 | 0 | struct Curl_cfilter *cf_prev = a->cf; |
267 | 0 | struct Curl_cfilter *wcf; |
268 | 0 | CURLcode result; |
269 | | |
270 | | /* When restarting, we tear down and existing filter *after* we |
271 | | * started up the new one. This gives us a new socket number and |
272 | | * probably a new local port. Which may prevent confusion. */ |
273 | 0 | a->result = CURLE_OK; |
274 | 0 | a->connected = FALSE; |
275 | 0 | a->inconclusive = FALSE; |
276 | 0 | a->cf = NULL; |
277 | |
|
278 | 0 | result = a->cf_create(&a->cf, data, cf->conn, a->addr, a->transport); |
279 | 0 | if(!result) { |
280 | 0 | bool dummy; |
281 | | /* the new filter might have sub-filters */ |
282 | 0 | for(wcf = a->cf; wcf; wcf = wcf->next) { |
283 | 0 | wcf->conn = cf->conn; |
284 | 0 | wcf->sockindex = cf->sockindex; |
285 | 0 | } |
286 | 0 | a->result = cf_ip_attempt_connect(a, data, &dummy); |
287 | 0 | } |
288 | 0 | if(cf_prev) |
289 | 0 | Curl_conn_cf_discard_chain(&cf_prev, data); |
290 | 0 | return result; |
291 | 0 | } |
292 | | |
293 | | static void cf_ip_ballers_clear(struct Curl_cfilter *cf, |
294 | | struct Curl_easy *data, |
295 | | struct cf_ip_ballers *bs) |
296 | 346k | { |
297 | 346k | (void)cf; |
298 | 349k | while(bs->running) { |
299 | 3.10k | struct cf_ip_attempt *a = bs->running; |
300 | 3.10k | bs->running = a->next; |
301 | 3.10k | cf_ip_attempt_free(a, data); |
302 | 3.10k | } |
303 | 346k | cf_ip_attempt_free(bs->winner, data); |
304 | 346k | bs->winner = NULL; |
305 | 346k | } |
306 | | |
307 | | static CURLcode cf_ip_ballers_init(struct cf_ip_ballers *bs, int ip_version, |
308 | | const struct Curl_addrinfo *addr_list, |
309 | | cf_ip_connect_create *cf_create, |
310 | | uint8_t transport, |
311 | | timediff_t attempt_delay_ms) |
312 | 116k | { |
313 | 116k | memset(bs, 0, sizeof(*bs)); |
314 | 116k | bs->cf_create = cf_create; |
315 | 116k | bs->transport = transport; |
316 | 116k | bs->attempt_delay_ms = attempt_delay_ms; |
317 | 116k | bs->last_attempt_ai_family = AF_INET; /* so AF_INET6 is next */ |
318 | | |
319 | 116k | if(transport == TRNSPRT_UNIX) { |
320 | 1.19k | #ifdef USE_UNIX_SOCKETS |
321 | 1.19k | cf_ai_iter_init(&bs->addr_iter, addr_list, AF_UNIX); |
322 | | #else |
323 | | return CURLE_UNSUPPORTED_PROTOCOL; |
324 | | #endif |
325 | 1.19k | } |
326 | 115k | else { /* TCP/UDP/QUIC */ |
327 | 115k | #ifdef USE_IPV6 |
328 | 115k | if(ip_version == CURL_IPRESOLVE_V6) |
329 | 25 | cf_ai_iter_init(&bs->addr_iter, NULL, AF_INET); |
330 | 115k | else |
331 | 115k | cf_ai_iter_init(&bs->addr_iter, addr_list, AF_INET); |
332 | | |
333 | 115k | if(ip_version == CURL_IPRESOLVE_V4) |
334 | 73 | cf_ai_iter_init(&bs->ipv6_iter, NULL, AF_INET6); |
335 | 115k | else |
336 | 115k | cf_ai_iter_init(&bs->ipv6_iter, addr_list, AF_INET6); |
337 | | #else |
338 | | (void)ip_version; |
339 | | cf_ai_iter_init(&bs->addr_iter, addr_list, AF_INET); |
340 | | #endif |
341 | 115k | } |
342 | 116k | return CURLE_OK; |
343 | 116k | } |
344 | | |
345 | | static CURLcode cf_ip_ballers_run(struct cf_ip_ballers *bs, |
346 | | struct Curl_cfilter *cf, |
347 | | struct Curl_easy *data, |
348 | | bool *connected) |
349 | 992k | { |
350 | 992k | CURLcode result = CURLE_OK; |
351 | 992k | struct cf_ip_attempt *a = NULL, **panchor; |
352 | 992k | bool do_more; |
353 | 992k | timediff_t next_expire_ms; |
354 | 992k | int inconclusive, ongoing; |
355 | 992k | VERBOSE(int i); |
356 | | |
357 | 992k | if(bs->winner) |
358 | 0 | return CURLE_OK; |
359 | | |
360 | 1.10M | evaluate: |
361 | 1.10M | ongoing = inconclusive = 0; |
362 | | |
363 | | /* check if a running baller connects now */ |
364 | 1.10M | VERBOSE(i = -1); |
365 | 1.98M | for(panchor = &bs->running; *panchor; panchor = &((*panchor)->next)) { |
366 | 993k | VERBOSE(++i); |
367 | 993k | a = *panchor; |
368 | 993k | a->result = cf_ip_attempt_connect(a, data, connected); |
369 | 993k | if(!a->result) { |
370 | 990k | if(*connected) { |
371 | | /* connected, declare the winner, remove from running, |
372 | | * clear remaining running list. */ |
373 | 113k | CURL_TRC_CF(data, cf, "connect attempt #%d successful", i); |
374 | 113k | bs->winner = a; |
375 | 113k | *panchor = a->next; |
376 | 113k | a->next = NULL; |
377 | 113k | while(bs->running) { |
378 | 0 | a = bs->running; |
379 | 0 | bs->running = a->next; |
380 | 0 | cf_ip_attempt_free(a, data); |
381 | 0 | } |
382 | 113k | return CURLE_OK; |
383 | 113k | } |
384 | | /* still running */ |
385 | 876k | ++ongoing; |
386 | 876k | } |
387 | 3.07k | else if(a->inconclusive) /* failed, but inconclusive */ |
388 | 0 | ++inconclusive; |
389 | 993k | } |
390 | 995k | if(bs->running) |
391 | 879k | CURL_TRC_CF(data, cf, "checked connect attempts: " |
392 | 995k | "%d ongoing, %d inconclusive", ongoing, inconclusive); |
393 | | |
394 | | /* no attempt connected yet, start another one? */ |
395 | 995k | if(!ongoing) { |
396 | 118k | if(!bs->started.tv_sec && !bs->started.tv_usec) |
397 | 116k | bs->started = *Curl_pgrs_now(data); |
398 | 118k | do_more = TRUE; |
399 | 118k | } |
400 | 876k | else { |
401 | 876k | bool more_possible = cf_ai_iter_has_more(&bs->addr_iter); |
402 | 876k | #ifdef USE_IPV6 |
403 | 876k | if(!more_possible) |
404 | 876k | more_possible = cf_ai_iter_has_more(&bs->ipv6_iter); |
405 | 876k | #endif |
406 | 876k | do_more = more_possible && |
407 | 0 | (curlx_ptimediff_ms(Curl_pgrs_now(data), &bs->last_attempt_started) >= |
408 | 0 | bs->attempt_delay_ms); |
409 | 876k | if(do_more) |
410 | 0 | CURL_TRC_CF(data, cf, "happy eyeballs timeout expired, " |
411 | 876k | "start next attempt"); |
412 | 876k | } |
413 | | |
414 | 995k | if(do_more) { |
415 | | /* start the next attempt if there is another ip address to try. |
416 | | * Alternate between address families when possible. */ |
417 | 118k | const struct Curl_addrinfo *addr = NULL; |
418 | 118k | int ai_family = 0; |
419 | 118k | #ifdef USE_IPV6 |
420 | 118k | if((bs->last_attempt_ai_family == AF_INET) || |
421 | 118k | !cf_ai_iter_has_more(&bs->addr_iter)) { |
422 | 118k | addr = cf_ai_iter_next(&bs->ipv6_iter); |
423 | 118k | ai_family = bs->ipv6_iter.ai_family; |
424 | 118k | } |
425 | 118k | #endif |
426 | 118k | if(!addr) { |
427 | 117k | addr = cf_ai_iter_next(&bs->addr_iter); |
428 | 117k | ai_family = bs->addr_iter.ai_family; |
429 | 117k | } |
430 | | |
431 | 118k | if(addr) { /* try another address */ |
432 | 116k | result = cf_ip_attempt_new(&a, cf, data, addr, ai_family, |
433 | 116k | bs->transport, bs->cf_create); |
434 | 116k | CURL_TRC_CF(data, cf, "starting %s attempt for ipv%s -> %d", |
435 | 116k | bs->running ? "next" : "first", |
436 | 116k | (ai_family == AF_INET) ? "4" : "6", result); |
437 | 116k | if(result) |
438 | 0 | goto out; |
439 | 116k | DEBUGASSERT(a); |
440 | | |
441 | | /* append to running list */ |
442 | 116k | panchor = &bs->running; |
443 | 117k | while(*panchor) |
444 | 404 | panchor = &((*panchor)->next); |
445 | 116k | *panchor = a; |
446 | 116k | bs->last_attempt_started = *Curl_pgrs_now(data); |
447 | 116k | bs->last_attempt_ai_family = ai_family; |
448 | | /* and run everything again */ |
449 | 116k | goto evaluate; |
450 | 116k | } |
451 | 2.34k | else if(inconclusive) { |
452 | | /* tried all addresses, no success but some where inconclusive. |
453 | | * Let's restart the inconclusive ones. */ |
454 | 0 | timediff_t since_ms = |
455 | 0 | curlx_ptimediff_ms(Curl_pgrs_now(data), &bs->last_attempt_started); |
456 | 0 | timediff_t delay_ms = bs->attempt_delay_ms - since_ms; |
457 | 0 | if(delay_ms <= 0) { |
458 | 0 | CURL_TRC_CF(data, cf, "all attempts inconclusive, restarting one"); |
459 | 0 | VERBOSE(i = -1); |
460 | 0 | for(a = bs->running; a; a = a->next) { |
461 | 0 | VERBOSE(++i); |
462 | 0 | if(!a->inconclusive) |
463 | 0 | continue; |
464 | 0 | result = cf_ip_attempt_restart(a, cf, data); |
465 | 0 | CURL_TRC_CF(data, cf, "restarted baller %d -> %d", i, result); |
466 | 0 | if(result) /* serious failure */ |
467 | 0 | goto out; |
468 | 0 | bs->last_attempt_started = *Curl_pgrs_now(data); |
469 | 0 | goto evaluate; |
470 | 0 | } |
471 | 0 | DEBUGASSERT(0); /* should not come here */ |
472 | 0 | } |
473 | 0 | else { |
474 | | /* let's wait some more before restarting */ |
475 | 0 | infof(data, "connect attempts inconclusive, retrying " |
476 | 0 | "in %" FMT_TIMEDIFF_T "ms", delay_ms); |
477 | 0 | Curl_expire(data, delay_ms, EXPIRE_HAPPY_EYEBALLS); |
478 | 0 | } |
479 | | /* attempt timeout for restart has not expired yet */ |
480 | 0 | goto out; |
481 | 0 | } |
482 | 2.34k | else if(!ongoing) { |
483 | | /* no more addresses, no inconclusive attempts */ |
484 | 2.34k | CURL_TRC_CF(data, cf, "no more attempts to try"); |
485 | 2.34k | result = CURLE_COULDNT_CONNECT; |
486 | 2.34k | VERBOSE(i = 0); |
487 | 4.98k | for(a = bs->running; a; a = a->next) { |
488 | 2.64k | CURL_TRC_CF(data, cf, "baller %d: result=%d", i, a->result); |
489 | 2.64k | if(a->result) |
490 | 2.64k | result = a->result; |
491 | 2.64k | } |
492 | 2.34k | } |
493 | 118k | } |
494 | | |
495 | 879k | out: |
496 | 879k | if(!result) { |
497 | 876k | bool more_possible; |
498 | | |
499 | | /* when do we need to be called again? */ |
500 | 876k | next_expire_ms = Curl_timeleft_ms(data); |
501 | 876k | if(next_expire_ms < 0) { |
502 | 0 | failf(data, "Connection timeout after %" FMT_OFF_T " ms", |
503 | 0 | curlx_ptimediff_ms(Curl_pgrs_now(data), |
504 | 0 | &data->progress.t_startsingle)); |
505 | 0 | return CURLE_OPERATION_TIMEDOUT; |
506 | 0 | } |
507 | | |
508 | 876k | more_possible = cf_ai_iter_has_more(&bs->addr_iter); |
509 | 876k | #ifdef USE_IPV6 |
510 | 876k | if(!more_possible) |
511 | 876k | more_possible = cf_ai_iter_has_more(&bs->ipv6_iter); |
512 | 876k | #endif |
513 | 876k | if(more_possible) { |
514 | 0 | timediff_t expire_ms, elapsed_ms; |
515 | 0 | elapsed_ms = |
516 | 0 | curlx_ptimediff_ms(Curl_pgrs_now(data), &bs->last_attempt_started); |
517 | 0 | expire_ms = CURLMAX(bs->attempt_delay_ms - elapsed_ms, 0); |
518 | 0 | next_expire_ms = CURLMIN(next_expire_ms, expire_ms); |
519 | 0 | if(next_expire_ms <= 0) { |
520 | 0 | CURL_TRC_CF(data, cf, "HAPPY_EYEBALLS timeout due, re-evaluate"); |
521 | 0 | goto evaluate; |
522 | 0 | } |
523 | 0 | CURL_TRC_CF(data, cf, "next HAPPY_EYEBALLS timeout in %" FMT_TIMEDIFF_T |
524 | 0 | "ms", next_expire_ms); |
525 | 0 | Curl_expire(data, next_expire_ms, EXPIRE_HAPPY_EYEBALLS); |
526 | 0 | } |
527 | 876k | } |
528 | 879k | return result; |
529 | 879k | } |
530 | | |
531 | | static CURLcode cf_ip_ballers_shutdown(struct cf_ip_ballers *bs, |
532 | | struct Curl_easy *data, |
533 | | bool *done) |
534 | 0 | { |
535 | 0 | struct cf_ip_attempt *a; |
536 | | |
537 | | /* shutdown all ballers that have not done so already. If one fails, |
538 | | * continue shutting down others until all are shutdown. */ |
539 | 0 | *done = TRUE; |
540 | 0 | for(a = bs->running; a; a = a->next) { |
541 | 0 | bool bdone = FALSE; |
542 | 0 | if(a->shutdown) |
543 | 0 | continue; |
544 | 0 | a->result = a->cf->cft->do_shutdown(a->cf, data, &bdone); |
545 | 0 | if(a->result || bdone) |
546 | 0 | a->shutdown = TRUE; /* treat a failed shutdown as done */ |
547 | 0 | else |
548 | 0 | *done = FALSE; |
549 | 0 | } |
550 | 0 | return CURLE_OK; |
551 | 0 | } |
552 | | |
553 | | static CURLcode cf_ip_ballers_pollset(struct cf_ip_ballers *bs, |
554 | | struct Curl_easy *data, |
555 | | struct easy_pollset *ps) |
556 | 876k | { |
557 | 876k | struct cf_ip_attempt *a; |
558 | 876k | CURLcode result = CURLE_OK; |
559 | 1.75M | for(a = bs->running; a && !result; a = a->next) { |
560 | 876k | if(a->result) |
561 | 30 | continue; |
562 | 876k | result = Curl_conn_cf_adjust_pollset(a->cf, data, ps); |
563 | 876k | } |
564 | 876k | return result; |
565 | 876k | } |
566 | | |
567 | | static bool cf_ip_ballers_pending(struct cf_ip_ballers *bs, |
568 | | const struct Curl_easy *data) |
569 | 0 | { |
570 | 0 | struct cf_ip_attempt *a; |
571 | |
|
572 | 0 | for(a = bs->running; a; a = a->next) { |
573 | 0 | if(a->result) |
574 | 0 | continue; |
575 | 0 | if(a->cf->cft->has_data_pending(a->cf, data)) |
576 | 0 | return TRUE; |
577 | 0 | } |
578 | 0 | return FALSE; |
579 | 0 | } |
580 | | |
581 | | static struct curltime cf_ip_ballers_max_time(struct cf_ip_ballers *bs, |
582 | | struct Curl_easy *data, |
583 | | int query) |
584 | 4.69k | { |
585 | 4.69k | struct curltime t, tmax; |
586 | 4.69k | struct cf_ip_attempt *a; |
587 | | |
588 | 4.69k | memset(&tmax, 0, sizeof(tmax)); |
589 | 9.97k | for(a = bs->running; a; a = a->next) { |
590 | 5.28k | memset(&t, 0, sizeof(t)); |
591 | 5.28k | if(!a->cf->cft->query(a->cf, data, query, NULL, &t)) { |
592 | 2.64k | if((t.tv_sec || t.tv_usec) && curlx_ptimediff_us(&t, &tmax) > 0) |
593 | 0 | tmax = t; |
594 | 2.64k | } |
595 | 5.28k | } |
596 | 4.69k | return tmax; |
597 | 4.69k | } |
598 | | |
599 | | static int cf_ip_ballers_min_reply_ms(struct cf_ip_ballers *bs, |
600 | | struct Curl_easy *data) |
601 | 0 | { |
602 | 0 | int reply_ms = -1, breply_ms; |
603 | 0 | struct cf_ip_attempt *a; |
604 | |
|
605 | 0 | for(a = bs->running; a; a = a->next) { |
606 | 0 | if(!a->cf->cft->query(a->cf, data, CF_QUERY_CONNECT_REPLY_MS, |
607 | 0 | &breply_ms, NULL)) { |
608 | 0 | if(breply_ms >= 0 && (reply_ms < 0 || breply_ms < reply_ms)) |
609 | 0 | reply_ms = breply_ms; |
610 | 0 | } |
611 | 0 | } |
612 | 0 | return reply_ms; |
613 | 0 | } |
614 | | |
615 | | typedef enum { |
616 | | SCFST_INIT, |
617 | | SCFST_WAITING, |
618 | | SCFST_DONE |
619 | | } cf_connect_state; |
620 | | |
621 | | struct cf_ip_happy_ctx { |
622 | | uint8_t transport; |
623 | | cf_ip_connect_create *cf_create; |
624 | | cf_connect_state state; |
625 | | struct cf_ip_ballers ballers; |
626 | | struct curltime started; |
627 | | }; |
628 | | |
629 | | static CURLcode is_connected(struct Curl_cfilter *cf, |
630 | | struct Curl_easy *data, |
631 | | bool *connected) |
632 | 992k | { |
633 | 992k | struct cf_ip_happy_ctx *ctx = cf->ctx; |
634 | 992k | struct connectdata *conn = cf->conn; |
635 | 992k | CURLcode result; |
636 | | |
637 | 992k | result = cf_ip_ballers_run(&ctx->ballers, cf, data, connected); |
638 | | |
639 | 992k | if(!result) |
640 | 990k | return CURLE_OK; |
641 | | |
642 | 2.34k | { |
643 | 2.34k | const char *hostname, *proxy_name = NULL; |
644 | 2.34k | char viamsg[160]; |
645 | 2.34k | #ifndef CURL_DISABLE_PROXY |
646 | 2.34k | if(conn->bits.socksproxy) |
647 | 22 | proxy_name = conn->socks_proxy.host.name; |
648 | 2.32k | else if(conn->bits.httpproxy) |
649 | 654 | proxy_name = conn->http_proxy.host.name; |
650 | 2.34k | #endif |
651 | 2.34k | hostname = conn->bits.conn_to_host ? conn->conn_to_host.name : |
652 | 2.34k | conn->host.name; |
653 | | |
654 | 2.34k | #ifdef USE_UNIX_SOCKETS |
655 | 2.34k | if(conn->unix_domain_socket) |
656 | 34 | curl_msnprintf(viamsg, sizeof(viamsg), "over %s", |
657 | 34 | conn->unix_domain_socket); |
658 | 2.31k | else |
659 | 2.31k | #endif |
660 | 2.31k | { |
661 | 2.31k | int port; |
662 | 2.31k | if(cf->sockindex == SECONDARYSOCKET) |
663 | 3 | port = conn->secondary_port; |
664 | 2.30k | else if(cf->conn->bits.conn_to_port) |
665 | 0 | port = conn->conn_to_port; |
666 | 2.30k | else |
667 | 2.30k | port = conn->remote_port; |
668 | 2.31k | curl_msnprintf(viamsg, sizeof(viamsg), "port %u", port); |
669 | 2.31k | } |
670 | | |
671 | 2.34k | failf(data, "Failed to connect to %s %s %s%s%safter " |
672 | 2.34k | "%" FMT_TIMEDIFF_T " ms: %s", |
673 | 2.34k | hostname, viamsg, |
674 | 2.34k | proxy_name ? "via " : "", |
675 | 2.34k | proxy_name ? proxy_name : "", |
676 | 2.34k | proxy_name ? " " : "", |
677 | 2.34k | curlx_ptimediff_ms(Curl_pgrs_now(data), |
678 | 2.34k | &data->progress.t_startsingle), |
679 | 2.34k | curl_easy_strerror(result)); |
680 | 2.34k | } |
681 | | |
682 | 2.34k | #ifdef SOCKETIMEDOUT |
683 | 2.34k | if(SOCKETIMEDOUT == data->state.os_errno) |
684 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
685 | 2.34k | #endif |
686 | | |
687 | 2.34k | return result; |
688 | 992k | } |
689 | | |
690 | | /* |
691 | | * Connect to the given host with timeout, proxy or remote does not matter. |
692 | | * There might be more than one IP address to try out. |
693 | | */ |
694 | | static CURLcode start_connect(struct Curl_cfilter *cf, |
695 | | struct Curl_easy *data) |
696 | 116k | { |
697 | 116k | struct cf_ip_happy_ctx *ctx = cf->ctx; |
698 | 116k | struct Curl_dns_entry *dns = data->state.dns[cf->sockindex]; |
699 | | |
700 | 116k | if(!dns) |
701 | 0 | return CURLE_FAILED_INIT; |
702 | | |
703 | 116k | if(Curl_timeleft_ms(data) < 0) { |
704 | | /* a precaution, no need to continue if time already is up */ |
705 | 2 | failf(data, "Connection time-out"); |
706 | 2 | return CURLE_OPERATION_TIMEDOUT; |
707 | 2 | } |
708 | | |
709 | 116k | CURL_TRC_CF(data, cf, "init ip ballers for transport %u", ctx->transport); |
710 | 116k | ctx->started = *Curl_pgrs_now(data); |
711 | 116k | return cf_ip_ballers_init(&ctx->ballers, cf->conn->ip_version, |
712 | 116k | dns->addr, ctx->cf_create, ctx->transport, |
713 | 116k | data->set.happy_eyeballs_timeout); |
714 | 116k | } |
715 | | |
716 | | static void cf_ip_happy_ctx_clear(struct Curl_cfilter *cf, |
717 | | struct Curl_easy *data) |
718 | 346k | { |
719 | 346k | struct cf_ip_happy_ctx *ctx = cf->ctx; |
720 | | |
721 | 346k | DEBUGASSERT(ctx); |
722 | 346k | DEBUGASSERT(data); |
723 | 346k | cf_ip_ballers_clear(cf, data, &ctx->ballers); |
724 | 346k | } |
725 | | |
726 | | static CURLcode cf_ip_happy_shutdown(struct Curl_cfilter *cf, |
727 | | struct Curl_easy *data, |
728 | | bool *done) |
729 | 24.0k | { |
730 | 24.0k | struct cf_ip_happy_ctx *ctx = cf->ctx; |
731 | 24.0k | CURLcode result = CURLE_OK; |
732 | | |
733 | 24.0k | DEBUGASSERT(data); |
734 | 24.0k | if(cf->connected) { |
735 | 24.0k | *done = TRUE; |
736 | 24.0k | return CURLE_OK; |
737 | 24.0k | } |
738 | | |
739 | 0 | result = cf_ip_ballers_shutdown(&ctx->ballers, data, done); |
740 | 0 | CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", result, *done); |
741 | 0 | return result; |
742 | 24.0k | } |
743 | | |
744 | | static CURLcode cf_ip_happy_adjust_pollset(struct Curl_cfilter *cf, |
745 | | struct Curl_easy *data, |
746 | | struct easy_pollset *ps) |
747 | 42.3M | { |
748 | 42.3M | struct cf_ip_happy_ctx *ctx = cf->ctx; |
749 | 42.3M | CURLcode result = CURLE_OK; |
750 | | |
751 | 42.3M | if(!cf->connected) { |
752 | 876k | result = cf_ip_ballers_pollset(&ctx->ballers, data, ps); |
753 | 876k | CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %d socks", result, ps->n); |
754 | 876k | } |
755 | 42.3M | return result; |
756 | 42.3M | } |
757 | | |
758 | | static CURLcode cf_ip_happy_connect(struct Curl_cfilter *cf, |
759 | | struct Curl_easy *data, |
760 | | bool *done) |
761 | 2.71M | { |
762 | 2.71M | struct cf_ip_happy_ctx *ctx = cf->ctx; |
763 | 2.71M | CURLcode result = CURLE_OK; |
764 | | |
765 | 2.71M | if(cf->connected) { |
766 | 1.72M | *done = TRUE; |
767 | 1.72M | return CURLE_OK; |
768 | 1.72M | } |
769 | | |
770 | 992k | DEBUGASSERT(ctx); |
771 | 992k | *done = FALSE; |
772 | | |
773 | 992k | switch(ctx->state) { |
774 | 116k | case SCFST_INIT: |
775 | 116k | DEBUGASSERT(CURL_SOCKET_BAD == Curl_conn_cf_get_socket(cf, data)); |
776 | 116k | DEBUGASSERT(!cf->connected); |
777 | 116k | result = start_connect(cf, data); |
778 | 116k | if(result) |
779 | 2 | return result; |
780 | 116k | ctx->state = SCFST_WAITING; |
781 | 116k | FALLTHROUGH(); |
782 | 992k | case SCFST_WAITING: |
783 | 992k | result = is_connected(cf, data, done); |
784 | 992k | if(!result && *done) { |
785 | 113k | DEBUGASSERT(ctx->ballers.winner); |
786 | 113k | DEBUGASSERT(ctx->ballers.winner->cf); |
787 | 113k | DEBUGASSERT(ctx->ballers.winner->cf->connected); |
788 | | /* we have a winner. Install and activate it. |
789 | | * close/free all others. */ |
790 | 113k | ctx->state = SCFST_DONE; |
791 | 113k | cf->connected = TRUE; |
792 | 113k | cf->next = ctx->ballers.winner->cf; |
793 | 113k | ctx->ballers.winner->cf = NULL; |
794 | 113k | cf_ip_happy_ctx_clear(cf, data); |
795 | 113k | Curl_expire_done(data, EXPIRE_HAPPY_EYEBALLS); |
796 | | |
797 | 113k | if(cf->conn->scheme->protocol & PROTO_FAMILY_SSH) |
798 | 0 | Curl_pgrsTime(data, TIMER_APPCONNECT); /* we are connected already */ |
799 | 113k | #ifdef CURLVERBOSE |
800 | 113k | if(Curl_trc_cf_is_verbose(cf, data)) { |
801 | 0 | struct ip_quadruple ipquad; |
802 | 0 | bool is_ipv6; |
803 | 0 | if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) { |
804 | 0 | const char *host; |
805 | 0 | int port; |
806 | 0 | Curl_conn_get_current_host(data, cf->sockindex, &host, &port); |
807 | 0 | CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u", |
808 | 0 | host, ipquad.remote_ip, ipquad.remote_port); |
809 | 0 | } |
810 | 0 | } |
811 | 113k | #endif |
812 | 113k | data->info.numconnects++; /* to track the # of connections made */ |
813 | 113k | } |
814 | 992k | break; |
815 | 992k | case SCFST_DONE: |
816 | 0 | *done = TRUE; |
817 | 0 | break; |
818 | 992k | } |
819 | 992k | return result; |
820 | 992k | } |
821 | | |
822 | | static void cf_ip_happy_close(struct Curl_cfilter *cf, |
823 | | struct Curl_easy *data) |
824 | 116k | { |
825 | 116k | struct cf_ip_happy_ctx *ctx = cf->ctx; |
826 | | |
827 | 116k | CURL_TRC_CF(data, cf, "close"); |
828 | 116k | cf_ip_happy_ctx_clear(cf, data); |
829 | 116k | cf->connected = FALSE; |
830 | 116k | ctx->state = SCFST_INIT; |
831 | | |
832 | 116k | if(cf->next) { |
833 | 113k | cf->next->cft->do_close(cf->next, data); |
834 | 113k | Curl_conn_cf_discard_chain(&cf->next, data); |
835 | 113k | } |
836 | 116k | } |
837 | | |
838 | | static bool cf_ip_happy_data_pending(struct Curl_cfilter *cf, |
839 | | const struct Curl_easy *data) |
840 | 58.6M | { |
841 | 58.6M | struct cf_ip_happy_ctx *ctx = cf->ctx; |
842 | | |
843 | 58.6M | if(!cf->connected) { |
844 | 0 | return cf_ip_ballers_pending(&ctx->ballers, data); |
845 | 0 | } |
846 | 58.6M | return cf->next->cft->has_data_pending(cf->next, data); |
847 | 58.6M | } |
848 | | |
849 | | static CURLcode cf_ip_happy_query(struct Curl_cfilter *cf, |
850 | | struct Curl_easy *data, |
851 | | int query, int *pres1, void *pres2) |
852 | 14.6M | { |
853 | 14.6M | struct cf_ip_happy_ctx *ctx = cf->ctx; |
854 | | |
855 | 14.6M | if(!cf->connected) { |
856 | 1.09M | switch(query) { |
857 | 0 | case CF_QUERY_CONNECT_REPLY_MS: { |
858 | 0 | *pres1 = cf_ip_ballers_min_reply_ms(&ctx->ballers, data); |
859 | 0 | CURL_TRC_CF(data, cf, "query connect reply: %dms", *pres1); |
860 | 0 | return CURLE_OK; |
861 | 0 | } |
862 | 2.34k | case CF_QUERY_TIMER_CONNECT: { |
863 | 2.34k | struct curltime *when = pres2; |
864 | 2.34k | *when = cf_ip_ballers_max_time(&ctx->ballers, data, |
865 | 2.34k | CF_QUERY_TIMER_CONNECT); |
866 | 2.34k | return CURLE_OK; |
867 | 0 | } |
868 | 2.34k | case CF_QUERY_TIMER_APPCONNECT: { |
869 | 2.34k | struct curltime *when = pres2; |
870 | 2.34k | *when = cf_ip_ballers_max_time(&ctx->ballers, data, |
871 | 2.34k | CF_QUERY_TIMER_APPCONNECT); |
872 | 2.34k | return CURLE_OK; |
873 | 0 | } |
874 | 1.09M | default: |
875 | 1.09M | break; |
876 | 1.09M | } |
877 | 1.09M | } |
878 | | |
879 | 14.6M | return cf->next ? |
880 | 13.5M | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
881 | 14.6M | CURLE_UNKNOWN_OPTION; |
882 | 14.6M | } |
883 | | |
884 | | static void cf_ip_happy_destroy(struct Curl_cfilter *cf, |
885 | | struct Curl_easy *data) |
886 | 116k | { |
887 | 116k | struct cf_ip_happy_ctx *ctx = cf->ctx; |
888 | | |
889 | 116k | CURL_TRC_CF(data, cf, "destroy"); |
890 | 116k | if(ctx) { |
891 | 116k | cf_ip_happy_ctx_clear(cf, data); |
892 | 116k | } |
893 | | /* release any resources held in state */ |
894 | 116k | Curl_safefree(ctx); |
895 | 116k | } |
896 | | |
897 | | struct Curl_cftype Curl_cft_ip_happy = { |
898 | | "HAPPY-EYEBALLS", |
899 | | 0, |
900 | | CURL_LOG_LVL_NONE, |
901 | | cf_ip_happy_destroy, |
902 | | cf_ip_happy_connect, |
903 | | cf_ip_happy_close, |
904 | | cf_ip_happy_shutdown, |
905 | | cf_ip_happy_adjust_pollset, |
906 | | cf_ip_happy_data_pending, |
907 | | Curl_cf_def_send, |
908 | | Curl_cf_def_recv, |
909 | | Curl_cf_def_cntrl, |
910 | | Curl_cf_def_conn_is_alive, |
911 | | Curl_cf_def_conn_keep_alive, |
912 | | cf_ip_happy_query, |
913 | | }; |
914 | | |
915 | | /** |
916 | | * Create an IP happy eyeball connection filter that uses the, once resolved, |
917 | | * address information to connect on ip families based on connection |
918 | | * configuration. |
919 | | * @param pcf output, the created cfilter |
920 | | * @param data easy handle used in creation |
921 | | * @param conn connection the filter is created for |
922 | | * @param cf_create method to create the sub-filters performing the |
923 | | * actual connects. |
924 | | */ |
925 | | static CURLcode cf_ip_happy_create(struct Curl_cfilter **pcf, |
926 | | struct Curl_easy *data, |
927 | | struct connectdata *conn, |
928 | | cf_ip_connect_create *cf_create, |
929 | | uint8_t transport) |
930 | 116k | { |
931 | 116k | struct cf_ip_happy_ctx *ctx = NULL; |
932 | 116k | CURLcode result; |
933 | | |
934 | 116k | (void)data; |
935 | 116k | (void)conn; |
936 | 116k | *pcf = NULL; |
937 | 116k | ctx = curlx_calloc(1, sizeof(*ctx)); |
938 | 116k | if(!ctx) { |
939 | 0 | result = CURLE_OUT_OF_MEMORY; |
940 | 0 | goto out; |
941 | 0 | } |
942 | 116k | ctx->transport = transport; |
943 | 116k | ctx->cf_create = cf_create; |
944 | | |
945 | 116k | result = Curl_cf_create(pcf, &Curl_cft_ip_happy, ctx); |
946 | | |
947 | 116k | out: |
948 | 116k | if(result) { |
949 | 0 | Curl_safefree(*pcf); |
950 | 0 | curlx_free(ctx); |
951 | 0 | } |
952 | 116k | return result; |
953 | 116k | } |
954 | | |
955 | | CURLcode cf_ip_happy_insert_after(struct Curl_cfilter *cf_at, |
956 | | struct Curl_easy *data, |
957 | | uint8_t transport) |
958 | 116k | { |
959 | 116k | cf_ip_connect_create *cf_create; |
960 | 116k | struct Curl_cfilter *cf; |
961 | 116k | CURLcode result; |
962 | | |
963 | | /* Need to be first */ |
964 | 116k | DEBUGASSERT(cf_at); |
965 | 116k | cf_create = get_cf_create(transport); |
966 | 116k | if(!cf_create) { |
967 | 0 | CURL_TRC_CF(data, cf_at, "unsupported transport type %u", transport); |
968 | 0 | return CURLE_UNSUPPORTED_PROTOCOL; |
969 | 0 | } |
970 | 116k | result = cf_ip_happy_create(&cf, data, cf_at->conn, cf_create, transport); |
971 | 116k | if(result) |
972 | 0 | return result; |
973 | | |
974 | 116k | Curl_conn_cf_insert_after(cf_at, cf); |
975 | 116k | return CURLE_OK; |
976 | 116k | } |