master
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"
26#ifdef HAVE_NETINET_IN_H
27#include <netinet/in.h> /* <netinet/tcp.h> may need it */
28#endif
29#ifdef HAVE_SYS_IOCTL_H
30#include <sys/ioctl.h>
31#endif
32#ifdef HAVE_NETDB_H
33#include <netdb.h>
34#endif
35#ifdef HAVE_ARPA_INET_H
36#include <arpa/inet.h>
37#endif
39#ifdef __VMS
40#include <in.h>
41#include <inet.h>
42#endif
44#include "urldata.h"
45#include "connect.h"
46#include "cfilters.h"
47#include "cf-ip-happy.h"
48#include "curl_addrinfo.h"
49#include "curl_trc.h"
50#include "multiif.h"
51#include "progress.h"
52#include "select.h"
53#include "sockaddr.h"
54#include "vdns/cf-dns.h"
55#include "vquic/vquic.h" /* for quic cfilters */
58struct transport_provider {
59 cf_ip_connect_create *cf_create;
60 uint8_t transport;
61 bool tunnel;
62};
64static
65#ifndef UNITTESTS
66const
67#endif
68struct transport_provider transport_providers[] = {
69 { Curl_cf_tcp_create, TRNSPRT_TCP, FALSE },
70 { Curl_cf_tcp_create, TRNSPRT_TCP, TRUE },
71#if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3)
72 { Curl_cf_quic_create, TRNSPRT_QUIC, FALSE },
73#endif
74#if !defined(CURL_DISABLE_HTTP) && defined(USE_PROXY_HTTP3)
75 { Curl_cf_h3_proxy_create, TRNSPRT_QUIC, TRUE },
76#endif
77#ifndef CURL_DISABLE_TFTP
78 { Curl_cf_udp_create, TRNSPRT_UDP, FALSE },
79#endif
80#ifdef USE_UNIX_SOCKETS
81 { Curl_cf_unix_create, TRNSPRT_UNIX, FALSE },
82 { Curl_cf_unix_create, TRNSPRT_UNIX, TRUE },
83#endif
84};
86static cf_ip_connect_create *get_cf_create(uint8_t transport,
87 bool tunnel)
88{
89 size_t i;
90 for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) {
91 if((transport == transport_providers[i].transport) &&
92 (tunnel == transport_providers[i].tunnel))
93 return transport_providers[i].cf_create;
94 }
95 return NULL;
96}
98#ifdef UNITTESTS
99/* @unittest 2600 */
100UNITTEST void debug_set_transport_provider(
101 uint8_t transport, cf_ip_connect_create *cf_create);
102UNITTEST void debug_set_transport_provider(
103 uint8_t transport, cf_ip_connect_create *cf_create)
104{
105 size_t i;
106 for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) {
107 if(transport == transport_providers[i].transport) {
108 transport_providers[i].cf_create = cf_create;
109 }
110 }
111}
112#endif /* UNITTESTS */
114struct cf_ai_iter {
115 struct Curl_cfilter *cf;
116 struct Curl_peer *peer;
117 int ai_family;
118 unsigned int n;
119};
121static void cf_ai_iter_init(struct cf_ai_iter *iter,
122 struct Curl_cfilter *cf,
123 struct Curl_peer *peer,
124 int ai_family)
125{
126 iter->cf = cf;
127 iter->peer = peer; /* not linked, ctx->ballers owns and has same lifetime */
128 iter->ai_family = ai_family;
129 iter->n = 0;
130}
132static const struct Curl_addrinfo *cf_ai_iter_next(struct cf_ai_iter *iter,
133 struct Curl_easy *data)
134{
135 const struct Curl_addrinfo *addr;
137 if(!iter->cf)
138 return NULL;
140 addr = Curl_conn_dns_get_ai(data, iter->peer, iter->cf->sockindex,
141 iter->ai_family, iter->n);
142 if(addr)
143 iter->n++;
144 return addr;
145}
147static bool cf_ai_iter_has_more(struct cf_ai_iter *iter,
148 struct Curl_easy *data)
149{
150 return (iter->cf &&
151 !!Curl_conn_dns_get_ai(data, iter->peer, iter->cf->sockindex,
152 iter->ai_family, iter->n));
153}
155struct cf_ip_attempt {
156 struct cf_ip_attempt *next;
157 struct Curl_peer *origin;
158 struct Curl_peer *peer;
159 struct Curl_peer *tunnel_peer;
160 struct Curl_sockaddr_ex addr;
161 struct Curl_cfilter *cf; /* current sub-cfilter connecting */
162 cf_ip_connect_create *cf_create;
163 CURLcode result;
164 int ai_family;
165 uint8_t transport_peer;
166 uint8_t tunnel_transport;
167 int error;
168 BIT(connected); /* cf has connected */
169 BIT(shutdown); /* cf has shutdown */
170 BIT(inconclusive); /* connect was not a hard failure, we
171 * might talk to a restarting server */
172};
174static void cf_ip_attempt_free(struct cf_ip_attempt *a,
175 struct Curl_easy *data)
176{
177 if(a) {
178 if(a->cf)
179 Curl_conn_cf_discard_chain(&a->cf, data);
180 Curl_peer_unlink(&a->origin);
181 Curl_peer_unlink(&a->peer);
182 Curl_peer_unlink(&a->tunnel_peer);
183 curlx_free(a);
184 }
185}
187static CURLcode cf_ip_attempt_new(struct cf_ip_attempt **pa,
188 struct Curl_easy *data,
189 struct Curl_cfilter *cf,
190 struct Curl_peer *origin,
191 struct Curl_peer *peer,
192 uint8_t transport_peer,
193 struct Curl_sockaddr_ex *addr,
194 int ai_family,
195 struct Curl_peer *tunnel_peer,
196 uint8_t tunnel_transport,
197 cf_ip_connect_create *cf_create)
198{
199 struct Curl_cfilter *wcf;
200 struct cf_ip_attempt *a;
201 CURLcode result = CURLE_OK;
203 *pa = NULL;
204 a = curlx_calloc(1, sizeof(*a));
205 if(!a)
206 return CURLE_OUT_OF_MEMORY;
208 Curl_peer_link(&a->origin, origin);
209 Curl_peer_link(&a->peer, peer);
210 a->transport_peer = transport_peer;
211 Curl_peer_link(&a->tunnel_peer, tunnel_peer);
212 a->tunnel_transport = tunnel_transport;
213 a->addr = *addr;
214 a->ai_family = ai_family;
215 a->result = CURLE_OK;
216 a->cf_create = cf_create;
217 *pa = a;
219 result = a->cf_create(&a->cf, data, a->origin, a->peer, a->transport_peer,
220 cf->conn, &a->addr, a->tunnel_peer,
221 a->tunnel_transport);
222 if(result)
223 goto out;
225 /* the new filter might have sub-filters */
226 for(wcf = a->cf; wcf; wcf = wcf->next) {
227 wcf->conn = cf->conn;
228 wcf->sockindex = cf->sockindex;
229 }
231out:
232 if(result) {
233 cf_ip_attempt_free(a, data);
234 *pa = NULL;
235 }
236 return result;
237}
239static CURLcode cf_ip_attempt_connect(struct cf_ip_attempt *a,
240 struct Curl_easy *data,
241 bool *connected)
242{
243 *connected = (bool)a->connected;
244 if(!a->result && !*connected) {
245 /* evaluate again */
246 a->result = Curl_conn_cf_connect(a->cf, data, connected);
248 if(!a->result) {
249 if(*connected) {
250 a->connected = TRUE;
251 }
252 }
253 else {
254 if(a->result == CURLE_WEIRD_SERVER_REPLY)
255 a->inconclusive = TRUE;
256 if(a->cf)
257 Curl_conn_cf_discard_chain(&a->cf, data);
258 }
259 }
260 return a->result;
261}
263struct cf_ip_ballers {
264 struct cf_ip_attempt *running;
265 struct cf_ip_attempt **running_tail; /* &running or &(last->next) */
266 struct cf_ip_attempt *winner;
267 struct cf_ai_iter addr_iter;
268#ifdef USE_IPV6
269 struct cf_ai_iter ipv6_iter;
270#endif
271 struct Curl_peer *origin;
272 struct Curl_peer *peer;
273 struct Curl_peer *tunnel_peer;
274 cf_ip_connect_create *cf_create; /* for creating cf */
275 struct curltime last_attempt_started;
276 timediff_t attempt_delay_ms;
277 CURLcode last_dead_result; /* result of last discarded hard failure */
278 int last_attempt_ai_family;
279 uint32_t max_concurrent;
280 uint8_t transport_peer;
281 uint8_t tunnel_transport;
282 BIT(had_dead); /* a hard failure was discarded */
283};
285static CURLcode cf_ip_attempt_restart(struct cf_ip_attempt *a,
286 struct Curl_cfilter *cf,
287 struct Curl_easy *data)
288{
289 struct Curl_cfilter *wcf;
290 CURLcode result;
292 if(a->cf)
293 Curl_conn_cf_discard_chain(&a->cf, data);
295 a->result = CURLE_OK;
296 a->connected = FALSE;
297 a->inconclusive = FALSE;
298 a->cf = NULL;
300 result = a->cf_create(&a->cf, data, a->origin, a->peer, a->transport_peer,
301 cf->conn, &a->addr,
302 a->tunnel_peer, a->tunnel_transport);
303 if(!result) {
304 bool dummy;
305 /* the new filter might have sub-filters */
306 for(wcf = a->cf; wcf; wcf = wcf->next) {
307 wcf->conn = cf->conn;
308 wcf->sockindex = cf->sockindex;
309 }
310 a->result = cf_ip_attempt_connect(a, data, &dummy);
311 }
312 return result;
313}
315static void cf_ip_ballers_clear(struct Curl_easy *data,
316 struct cf_ip_ballers *bs)
317{
318 while(bs->running) {
319 struct cf_ip_attempt *a = bs->running;
320 bs->running = a->next;
321 cf_ip_attempt_free(a, data);
322 }
323 bs->running_tail = &bs->running;
324 cf_ip_attempt_free(bs->winner, data);
325 bs->winner = NULL;
326 Curl_peer_unlink(&bs->origin);
327 Curl_peer_unlink(&bs->peer);
328 Curl_peer_unlink(&bs->tunnel_peer);
329}
331static CURLcode cf_ip_ballers_init(struct cf_ip_ballers *bs,
332 struct Curl_easy *data,
333 struct Curl_peer *origin,
334 struct Curl_peer *peer,
335 uint8_t transport_peer,
336 struct Curl_peer *tunnel_peer,
337 uint8_t tunnel_transport,
338 timediff_t attempt_delay_ms,
339 uint32_t max_concurrent)
340{
341 memset(bs, 0, sizeof(*bs));
342 bs->running_tail = &bs->running;
343 bs->cf_create = get_cf_create(transport_peer, !!tunnel_peer);
344 if(!bs->cf_create) {
345 failf(data, "unsupported transport type %u%s",
346 transport_peer, tunnel_peer ? " to proxy" : "");
347 return CURLE_UNSUPPORTED_PROTOCOL;
348 }
349 Curl_peer_link(&bs->origin, origin);
350 Curl_peer_link(&bs->peer, peer);
351 bs->transport_peer = transport_peer;
352 Curl_peer_link(&bs->tunnel_peer, tunnel_peer);
353 bs->tunnel_transport = tunnel_transport;
354 bs->attempt_delay_ms = attempt_delay_ms;
355 bs->max_concurrent = max_concurrent;
356 bs->last_attempt_ai_family = AF_INET; /* so AF_INET6 is next */
357 return CURLE_OK;
358}
360static void cf_ip_ballers_prune(struct cf_ip_ballers *bs,
361 struct Curl_cfilter *cf,
362 struct Curl_easy *data,
363 uint32_t max_concurrent)
364{
365 struct cf_ip_attempt *a = NULL, **panchor;
366 uint32_t ongoing = 0;
368 for(a = bs->running; a; a = a->next) {
369 if(!a->result && !a->connected)
370 ++ongoing;
371 }
373 panchor = &bs->running;
374 while(*panchor && (ongoing > max_concurrent)) {
375 a = *panchor;
376 if(!a->result && !a->connected) {
377 *panchor = a->next;
378 if(!a->next)
379 bs->running_tail = panchor;
380 a->next = NULL;
381 cf_ip_attempt_free(a, data);
382 --ongoing;
383 CURL_TRC_CF(data, cf, "discarding oldest attempt to keep limit");
384 }
385 else {
386 panchor = &a->next;
387 }
388 }
389}
391static CURLcode cf_ip_ballers_run(struct cf_ip_ballers *bs,
392 struct Curl_cfilter *cf,
393 struct Curl_easy *data,
394 bool dns_resolved,
395 bool *connected)
396{
397 CURLcode result = CURLE_OK;
398 struct cf_ip_attempt *a = NULL, **panchor;
399 bool do_more;
400 timediff_t next_expire_ms;
401 uint32_t inconclusive, ongoing;
402 const struct curltime *pnow = NULL;
403 VERBOSE(int i);
405 if(bs->winner)
406 return CURLE_OK;
408evaluate:
409 if(Curl_timeleft_now_ms(data, Curl_pgrs_now(data)) < 0) {
410 failf(data, "Connection timeout after %" FMT_OFF_T " ms",
411 Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE));
412 return CURLE_OPERATION_TIMEDOUT;
413 }
415 ongoing = inconclusive = 0;
417 /* check if a running baller connects now */
418 VERBOSE(i = -1);
419 for(panchor = &bs->running; *panchor;) {
420 VERBOSE(++i);
421 a = *panchor;
422 a->result = cf_ip_attempt_connect(a, data, connected);
423 if(!a->result) {
424 if(*connected) {
425 /* connected, declare the winner, remove from running,
426 * clear remaining running list. */
427 CURL_TRC_CF(data, cf, "connect attempt #%d successful", i);
428 bs->winner = a;
429 *panchor = a->next;
430 a->next = NULL;
431 while(bs->running) {
432 a = bs->running;
433 bs->running = a->next;
434 cf_ip_attempt_free(a, data);
435 }
436 bs->running_tail = &bs->running;
437 return CURLE_OK;
438 }
439 /* still running */
440 ++ongoing;
441 panchor = &(*panchor)->next;
442 }
443 else if(a->inconclusive) { /* failed, but inconclusive */
444 ++inconclusive;
445 panchor = &(*panchor)->next;
446 }
447 else {
448 /* hard failure, discard right away */
449 *panchor = a->next;
450 if(!a->next)
451 bs->running_tail = panchor;
452 a->next = NULL;
453 bs->last_dead_result = a->result;
454 bs->had_dead = TRUE;
455 cf_ip_attempt_free(a, data);
456 }
457 }
458 if(bs->running)
459 CURL_TRC_CF(data, cf, "checked connect attempts: "
460 "%u ongoing, %u inconclusive", ongoing, inconclusive);
462 /* no attempt connected yet, start another one? */
463 pnow = Curl_pgrs_now(data);
464 if(!ongoing) {
465 do_more = TRUE;
466 }
467 else {
468 bool more_possible = cf_ai_iter_has_more(&bs->addr_iter, data);
469#ifdef USE_IPV6
470 if(!more_possible)
471 more_possible = cf_ai_iter_has_more(&bs->ipv6_iter, data);
472#endif
473 do_more = more_possible &&
474 (curlx_ptimediff_ms(pnow, &bs->last_attempt_started) >=
475 bs->attempt_delay_ms);
476 if(do_more)
477 CURL_TRC_CF(data, cf, "happy eyeballs timeout expired, "
478 "start next attempt");
479 }
481 if(do_more) {
482 /* start the next attempt if there is another ip address to try.
483 * Alternate between address families when possible. */
484 const struct Curl_addrinfo *ai = NULL;
485 int ai_family = 0;
486 CURL_TRC_CF(data, cf, "want to do more");
487#ifdef USE_IPV6
488 if((bs->last_attempt_ai_family == AF_INET) ||
489 !cf_ai_iter_has_more(&bs->addr_iter, data)) {
490 ai = cf_ai_iter_next(&bs->ipv6_iter, data);
491 ai_family = bs->ipv6_iter.ai_family;
492 CURL_TRC_CF(data, cf, "check for next AAAA address: %s",
493 ai ? "found" : "none");
494 }
495#endif
496 if(!ai) {
497 ai = cf_ai_iter_next(&bs->addr_iter, data);
498 ai_family = bs->addr_iter.ai_family;
499 CURL_TRC_CF(data, cf, "check for next A address: %s",
500 ai ? "found" : "none");
501 }
502 /* We are (re-)starting attempts. We are not interested in
503 * keeping old failure information. The new attempt will either
504 * succeed or persist new failure. */
505 Curl_reset_fail(data);
507 if(ai) { /* try another address */
508 struct Curl_sockaddr_ex addr;
510 /* Discard oldest to make room for new attempt */
511 if(bs->max_concurrent)
512 cf_ip_ballers_prune(bs, cf, data, bs->max_concurrent - 1);
514 result = Curl_socket_addr_from_ai(&addr, ai, bs->transport_peer);
515 if(result)
516 goto out;
518 result = cf_ip_attempt_new(&a, data, cf, bs->origin, bs->peer,
519 bs->transport_peer, &addr, ai_family,
520 bs->tunnel_peer, bs->tunnel_transport,
521 bs->cf_create);
522 CURL_TRC_CF(data, cf, "starting %s attempt for ipv%s -> %d",
523 bs->running ? "next" : "first",
524 (ai_family == AF_INET) ? "4" : "6", (int)result);
525 if(result)
526 goto out;
527 DEBUGASSERT(a);
529 /* append to running list */
530 *bs->running_tail = a;
531 a->next = NULL;
532 bs->running_tail = &a->next;
533 bs->last_attempt_started = *pnow;
534 bs->last_attempt_ai_family = ai_family;
535 /* and run everything again */
536 goto evaluate;
537 }
538 else if(inconclusive) {
539 /* tried all addresses, no success but some where inconclusive.
540 * Let's restart the inconclusive ones. */
541 timediff_t since_ms =
542 curlx_ptimediff_ms(pnow, &bs->last_attempt_started);
543 timediff_t delay_ms = bs->attempt_delay_ms - since_ms;
544 if(delay_ms <= 0) {
545 CURL_TRC_CF(data, cf, "all attempts inconclusive, restarting one");
546 VERBOSE(i = -1);
547 for(a = bs->running; a; a = a->next) {
548 VERBOSE(++i);
549 if(!a->inconclusive)
550 continue;
551 result = cf_ip_attempt_restart(a, cf, data);
552 CURL_TRC_CF(data, cf, "restarted baller %d -> %d", i, (int)result);
553 if(result) /* serious failure */
554 goto out;
555 bs->last_attempt_started = *pnow;
556 goto evaluate;
557 }
558 DEBUGASSERT(0); /* should not come here */
559 }
560 else {
561 /* let's wait some more before restarting */
562 infof(data, "connect attempts inconclusive, retrying "
563 "in %" FMT_TIMEDIFF_T "ms", delay_ms);
564 Curl_expire_set(data, EXPIRE_HAPPY_EYEBALLS, delay_ms, pnow);
565 }
566 /* attempt timeout for restart has not expired yet */
567 goto out;
568 }
569 else if(!ongoing && dns_resolved) {
570 /* no more addresses, no inconclusive attempts */
571 CURL_TRC_CF(data, cf, "no more attempts to try");
572 DEBUGASSERT(!bs->running);
573 result = bs->had_dead ? bs->last_dead_result : CURLE_COULDNT_CONNECT;
574 }
575 }
577out:
578 if(!result) {
579 bool more_possible;
581 /* when do we need to be called again? */
582 pnow = Curl_pgrs_now(data);
583 next_expire_ms = Curl_timeleft_now_ms(data, pnow);
584 if(next_expire_ms < 0) {
585 failf(data, "Connection timeout after %" FMT_OFF_T " ms",
586 Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE));
587 return CURLE_OPERATION_TIMEDOUT;
588 }
590 more_possible = cf_ai_iter_has_more(&bs->addr_iter, data);
591#ifdef USE_IPV6
592 if(!more_possible)
593 more_possible = cf_ai_iter_has_more(&bs->ipv6_iter, data);
594#endif
595 if(more_possible) {
596 timediff_t expire_ms, elapsed_ms;
598 elapsed_ms = curlx_ptimediff_ms(pnow, &bs->last_attempt_started);
599 expire_ms = CURLMAX(bs->attempt_delay_ms - elapsed_ms, 0);
600 next_expire_ms = CURLMIN(next_expire_ms, expire_ms);
601 if(next_expire_ms <= 0) {
602 CURL_TRC_CF(data, cf, "HAPPY_EYEBALLS timeout due, re-evaluate");
603 goto evaluate;
604 }
605 CURL_TRC_CF(data, cf, "next HAPPY_EYEBALLS timeout in %" FMT_TIMEDIFF_T
606 "ms", next_expire_ms);
607 Curl_expire_set(data, EXPIRE_HAPPY_EYEBALLS, next_expire_ms, pnow);
608 }
609 }
610 return result;
611}
613static CURLcode cf_ip_ballers_shutdown(struct cf_ip_ballers *bs,
614 struct Curl_easy *data,
615 bool *done)
616{
617 struct cf_ip_attempt *a;
619 /* shutdown all ballers that have not done so already. If one fails,
620 * continue shutting down others until all are shutdown. */
621 *done = TRUE;
622 for(a = bs->running; a; a = a->next) {
623 bool bdone = FALSE;
624 if(a->shutdown || !a->cf)
625 continue;
626 a->result = a->cf->cft->do_shutdown(a->cf, data, &bdone);
627 if(a->result || bdone)
628 a->shutdown = TRUE; /* treat a failed shutdown as done */
629 else
630 *done = FALSE;
631 }
632 return CURLE_OK;
633}
635static CURLcode cf_ip_ballers_pollset(struct cf_ip_ballers *bs,
636 struct Curl_easy *data,
637 struct easy_pollset *ps)
638{
639 struct cf_ip_attempt *a;
640 CURLcode result = CURLE_OK;
641 for(a = bs->running; a && !result; a = a->next) {
642 if(a->result)
643 continue;
644 result = Curl_conn_cf_adjust_pollset(a->cf, data, ps);
645 }
646 return result;
647}
649static bool cf_ip_ballers_pending(struct cf_ip_ballers *bs,
650 const struct Curl_easy *data)
651{
652 struct cf_ip_attempt *a;
654 for(a = bs->running; a; a = a->next) {
655 if(a->result)
656 continue;
657 if(a->cf && a->cf->cft->has_data_pending(a->cf, data))
658 return TRUE;
659 }
660 return FALSE;
661}
663static int cf_ip_ballers_min_reply_ms(struct cf_ip_ballers *bs,
664 struct Curl_easy *data)
665{
666 int reply_ms = -1, breply_ms;
667 struct cf_ip_attempt *a;
669 for(a = bs->running; a; a = a->next) {
670 if(a->cf && !a->cf->cft->query(a->cf, data, CF_QUERY_CONNECT_REPLY_MS,
671 &breply_ms, NULL) &&
672 breply_ms >= 0 && (reply_ms < 0 || breply_ms < reply_ms))
673 reply_ms = breply_ms;
674 }
675 return reply_ms;
676}
678typedef enum {
679 SCFST_INIT,
680 SCFST_WAITING,
681 SCFST_DONE
682} cf_connect_state;
684struct cf_ip_happy_ctx {
685 cf_ip_connect_create *cf_create;
686 cf_connect_state state;
687 struct cf_ip_ballers ballers;
688 BIT(dns_resolved);
689};
691static CURLcode is_connected(struct Curl_cfilter *cf,
692 struct Curl_easy *data,
693 bool *connected)
694{
695 struct cf_ip_happy_ctx *ctx = cf->ctx;
696 struct connectdata *conn = cf->conn;
697 CURLcode result;
699 result = cf_ip_ballers_run(&ctx->ballers, cf, data,
700 (bool)ctx->dns_resolved, connected);
702 if(!result)
703 return CURLE_OK;
704 else {
705 struct Curl_peer *peer = NULL, *proxy_peer = NULL;
706 char viamsg[160];
708 peer = Curl_conn_get_first_peer(conn, cf->sockindex);
709 if(!conn->origin || !peer)
710 return CURLE_FAILED_INIT;
712#ifndef CURL_DISABLE_PROXY
713 if(conn->socks_proxy.peer)
714 proxy_peer = conn->socks_proxy.peer;
715 else if(conn->http_proxy.peer)
716 proxy_peer = conn->http_proxy.peer;
717#endif
719 viamsg[0] = 0;
720 if(!Curl_peer_equal(peer, conn->origin) &&
721 !Curl_peer_equal(peer, proxy_peer)) {
722#ifdef USE_UNIX_SOCKETS
723 if(peer->unix_socket)
724 curl_msnprintf(viamsg, sizeof(viamsg), " over unix://%s",
725 peer->hostname);
726 else
727#endif
728 curl_msnprintf(viamsg, sizeof(viamsg), " via %s:%u",
729 peer->hostname, peer->port);
730 }
732 failf(data, "Failed to connect to %s:%u%s %s%s%safter "
733 "%" FMT_TIMEDIFF_T " ms: %s",
734 conn->origin->hostname, conn->origin->port, viamsg,
735 proxy_peer ? "over proxy " : "",
736 proxy_peer ? proxy_peer->hostname : "",
737 proxy_peer ? " " : "",
738 Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE),
739 curl_easy_strerror(result));
741#ifdef SOCKETIMEDOUT
742 if(SOCKETIMEDOUT == data->state.os_errno)
743 result = CURLE_OPERATION_TIMEDOUT;
744#endif
746 return result;
747 }
748}
750#define IP_HE_MAX_CONCURRENT_ATTEMPTS 6
752static CURLcode cf_ip_happy_init(struct Curl_cfilter *cf,
753 struct Curl_easy *data)
754{
755 struct cf_ip_happy_ctx *ctx = cf->ctx;
757 if(Curl_timeleft_ms(data) < 0) {
758 /* a precaution, no need to continue if time already is up */
759 failf(data, "Connection time-out");
760 return CURLE_OPERATION_TIMEDOUT;
761 }
763 if(ctx->ballers.transport_peer == TRNSPRT_UNIX) {
764#ifdef USE_UNIX_SOCKETS
765 cf_ai_iter_init(&ctx->ballers.addr_iter, cf, ctx->ballers.peer, AF_UNIX);
766#else
767 return CURLE_UNSUPPORTED_PROTOCOL;
768#endif
769 }
770 else { /* TCP/UDP/QUIC */
771#ifdef USE_IPV6
772 cf_ai_iter_init(&ctx->ballers.ipv6_iter, cf, ctx->ballers.peer, AF_INET6);
773#endif
774 cf_ai_iter_init(&ctx->ballers.addr_iter, cf, ctx->ballers.peer, AF_INET);
775 }
777 CURL_TRC_CF(data, cf, "init ip ballers for transport %u",
778 ctx->ballers.transport_peer);
779 return CURLE_OK;
780}
782static void cf_ip_happy_ctx_clear(struct cf_ip_happy_ctx *ctx,
783 struct Curl_easy *data)
784{
785 DEBUGASSERT(ctx);
786 if(ctx)
787 cf_ip_ballers_clear(data, &ctx->ballers);
788}
790static void cf_ip_happy_ctx_destroy(struct cf_ip_happy_ctx *ctx,
791 struct Curl_easy *data)
792{
793 if(ctx) {
794 cf_ip_happy_ctx_clear(ctx, data);
795 curlx_free(ctx);
796 }
797}
799static CURLcode cf_ip_happy_shutdown(struct Curl_cfilter *cf,
800 struct Curl_easy *data,
801 bool *done)
802{
803 struct cf_ip_happy_ctx *ctx = cf->ctx;
804 CURLcode result = CURLE_OK;
806 DEBUGASSERT(data);
807 if(cf->connected) {
808 *done = TRUE;
809 return CURLE_OK;
810 }
812 result = cf_ip_ballers_shutdown(&ctx->ballers, data, done);
813 CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", (int)result, *done);
814 return result;
815}
817static CURLcode cf_ip_happy_adjust_pollset(struct Curl_cfilter *cf,
818 struct Curl_easy *data,
819 struct easy_pollset *ps)
820{
821 struct cf_ip_happy_ctx *ctx = cf->ctx;
822 CURLcode result = CURLE_OK;
824 if(!cf->connected) {
825 result = cf_ip_ballers_pollset(&ctx->ballers, data, ps);
826 CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %u socks", (int)result,
827 ps->n);
828 }
829 return result;
830}
832static CURLcode cf_ip_happy_connect(struct Curl_cfilter *cf,
833 struct Curl_easy *data,
834 bool *done)
835{
836 struct cf_ip_happy_ctx *ctx = cf->ctx;
837 CURLcode result = CURLE_OK;
839 /* -Werror=null-dereference finds false positives suddenly. */
840 if(!data)
841 return CURLE_FAILED_INIT;
843 if(cf->connected) {
844 *done = TRUE;
845 return CURLE_OK;
846 }
848 DEBUGASSERT(ctx);
849 *done = FALSE;
851 if(!ctx->dns_resolved) {
852 result = Curl_conn_dns_addr_result(cf->conn, cf->sockindex,
853 ctx->ballers.peer);
854 if(!result)
855 ctx->dns_resolved = TRUE;
856 else if(result == CURLE_AGAIN) {
857 result = CURLE_OK;
858 }
859 else /* real error */
860 goto out;
861 }
863 switch(ctx->state) {
864 case SCFST_INIT:
865 DEBUGASSERT(CURL_SOCKET_BAD == Curl_conn_cf_get_socket(cf, data));
866 DEBUGASSERT(!cf->connected);
867 result = cf_ip_happy_init(cf, data);
868 if(result)
869 goto out;
870 ctx->state = SCFST_WAITING;
871 FALLTHROUGH();
872 case SCFST_WAITING:
873 result = is_connected(cf, data, done);
874 if(!result && *done) {
875 DEBUGASSERT(ctx->ballers.winner);
876 DEBUGASSERT(ctx->ballers.winner->cf);
877 DEBUGASSERT(ctx->ballers.winner->cf->connected);
878 /* we have a winner. Install and activate it.
879 * close/free all others. */
880 ctx->state = SCFST_DONE;
881 cf->connected = TRUE;
882 cf->next = ctx->ballers.winner->cf;
883 ctx->ballers.winner->cf = NULL;
885 if(cf->conn->scheme->protocol & PROTO_FAMILY_SSH)
886 Curl_pgrsTime(data, TIMER_APPCONNECT); /* we are connected already */
887#ifdef CURLVERBOSE
888 if(Curl_trc_cf_is_verbose(cf, data)) {
889 struct ip_quadruple ipquad;
890 bool is_ipv6;
891 if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) {
892 CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u",
893 ctx->ballers.peer->hostname,
894 ipquad.remote_ip, ipquad.remote_port);
895 }
896 }
897#endif
898 cf_ip_happy_ctx_clear(ctx, data);
899 Curl_expire_clear(data, EXPIRE_HAPPY_EYEBALLS);
900 /* whatever errors were reported by ballers, clear our errorbuf */
901 Curl_reset_fail(data);
902 data->info.numconnects++; /* to track the # of connections made */
903 }
904 break;
905 case SCFST_DONE:
906 *done = TRUE;
907 break;
908 }
909out:
910 return result;
911}
913static bool cf_ip_happy_data_pending(struct Curl_cfilter *cf,
914 const struct Curl_easy *data)
915{
916 struct cf_ip_happy_ctx *ctx = cf->ctx;
918 if(!cf->connected) {
919 return cf_ip_ballers_pending(&ctx->ballers, data);
920 }
921 return cf->next->cft->has_data_pending(cf->next, data);
922}
924static CURLcode cf_ip_happy_query(struct Curl_cfilter *cf,
925 struct Curl_easy *data,
926 int query, int *pres1, void *pres2)
927{
928 struct cf_ip_happy_ctx *ctx = cf->ctx;
930 if(!cf->connected) {
931 switch(query) {
932 case CF_QUERY_CONNECT_REPLY_MS: {
933 *pres1 = cf_ip_ballers_min_reply_ms(&ctx->ballers, data);
934 CURL_TRC_CF(data, cf, "query connect reply: %dms", *pres1);
935 return CURLE_OK;
936 }
937 default:
938 break;
939 }
940 }
942 return cf->next ?
943 cf->next->cft->query(cf->next, data, query, pres1, pres2) :
944 CURLE_UNKNOWN_OPTION;
945}
947static void cf_ip_happy_destroy(struct Curl_cfilter *cf,
948 struct Curl_easy *data)
949{
950 struct cf_ip_happy_ctx *ctx = cf->ctx;
952 CURL_TRC_CF(data, cf, "destroy");
953 if(ctx) {
954 cf_ip_happy_ctx_clear(ctx, data);
955 cf_ip_happy_ctx_destroy(ctx, data);
956 }
957}
959struct Curl_cftype Curl_cft_ip_happy = {
960 "HAPPY-EYEBALLS",
961 CF_TYPE_SETUP,
962 CURL_LOG_LVL_NONE,
963 cf_ip_happy_destroy,
964 cf_ip_happy_connect,
965 cf_ip_happy_shutdown,
966 cf_ip_happy_adjust_pollset,
967 cf_ip_happy_data_pending,
968 Curl_cf_def_send,
969 Curl_cf_def_recv,
970 Curl_cf_def_cntrl,
971 Curl_cf_def_conn_is_alive,
972 Curl_cf_def_conn_keep_alive,
973 cf_ip_happy_query,
974};
976static CURLcode cf_ip_happy_create(struct Curl_cfilter **pcf,
977 struct Curl_easy *data,
978 struct Curl_peer *origin,
979 struct Curl_peer *peer,
980 uint8_t transport_peer,
981 struct connectdata *conn,
982 struct Curl_peer *tunnel_peer,
983 uint8_t tunnel_transport)
984{
985 struct cf_ip_happy_ctx *ctx = NULL;
986 CURLcode result;
988 (void)data;
989 (void)conn;
990 *pcf = NULL;
991 ctx = curlx_calloc(1, sizeof(*ctx));
992 if(!ctx) {
993 result = CURLE_OUT_OF_MEMORY;
994 goto out;
995 }
996 result = cf_ip_ballers_init(&ctx->ballers, data,
997 origin, peer, transport_peer,
998 tunnel_peer, tunnel_transport,
999 data->set.happy_eyeballs_timeout,
1000 IP_HE_MAX_CONCURRENT_ATTEMPTS);
1001 if(result)
1002 goto out;
1004 result = Curl_cf_create(pcf, &Curl_cft_ip_happy, ctx);
1006out:
1007 if(result) {
1008 curlx_safefree(*pcf);
1009 cf_ip_happy_ctx_destroy(ctx, data);
1010 }
1011 return result;
1012}
1014CURLcode Curl_cf_ip_happy_insert_after(struct Curl_cfilter *cf_at,
1015 struct Curl_easy *data,
1016 struct Curl_peer *origin,
1017 struct Curl_peer *peer,
1018 uint8_t transport_peer,
1019 struct Curl_peer *tunnel_peer,
1020 uint8_t tunnel_transport)
1021{
1022 struct Curl_cfilter *cf;
1023 CURLcode result;
1025 /* Need to be first */
1026 DEBUGASSERT(cf_at);
1027 result = cf_ip_happy_create(&cf, data, origin, peer, transport_peer,
1028 cf_at->conn, tunnel_peer, tunnel_transport);
1029 if(result)
1030 return result;
1032 Curl_conn_cf_insert_after(cf_at, cf);
1033 return CURLE_OK;
1034}
Diagnostics4 git calls · 35 ms
CallStatusSizeTime035 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=30 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2008.1 KiB · hit30 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=8319 git.response.cache=HIT git.response.duration_ms=30 git.fetch.wants=b5347b61060caf77c4bccdde7491580ce6bc7cea git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=8319 git.pkt.payload.max=8298 git.sideband.band1.bytes=8297 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e3200048e69de8
- Request
- 11f3125d…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format