Coverage Report

Created: 2026-12-01 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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_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
38
39
#ifdef __VMS
40
#include <in.h>
41
#include <inet.h>
42
#endif
43
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 */
56
57
58
struct transport_provider {
59
  cf_ip_connect_create *cf_create;
60
  uint8_t transport;
61
  bool tunnel;
62
};
63
64
static
65
#ifndef UNITTESTS
66
const
67
#endif
68
struct 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
};
85
86
static cf_ip_connect_create *get_cf_create(uint8_t transport,
87
                                           bool tunnel)
88
32.1k
{
89
32.1k
  size_t i;
90
32.1k
  for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) {
91
32.1k
    if((transport == transport_providers[i].transport) &&
92
32.1k
       (tunnel == transport_providers[i].tunnel))
93
32.1k
      return transport_providers[i].cf_create;
94
32.1k
  }
95
0
  return NULL;
96
32.1k
}
97
98
#ifdef UNITTESTS
99
/* @unittest 2600 */
100
UNITTEST void debug_set_transport_provider(
101
  uint8_t transport, cf_ip_connect_create *cf_create);
102
UNITTEST void debug_set_transport_provider(
103
  uint8_t transport, cf_ip_connect_create *cf_create)
104
0
{
105
0
  size_t i;
106
0
  for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) {
107
0
    if(transport == transport_providers[i].transport) {
108
0
      transport_providers[i].cf_create = cf_create;
109
0
    }
110
0
  }
111
0
}
112
#endif /* UNITTESTS */
113
114
struct cf_ai_iter {
115
  struct Curl_cfilter *cf;
116
  struct Curl_peer *peer;
117
  int ai_family;
118
  unsigned int n;
119
};
120
121
static 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
64.3k
{
126
64.3k
  iter->cf = cf;
127
64.3k
  iter->peer = peer; /* not linked, ctx->ballers owns and has same lifetime */
128
64.3k
  iter->ai_family = ai_family;
129
64.3k
  iter->n = 0;
130
64.3k
}
131
132
static const struct Curl_addrinfo *cf_ai_iter_next(struct cf_ai_iter *iter,
133
                                                   struct Curl_easy *data)
134
81.3k
{
135
81.3k
  const struct Curl_addrinfo *addr;
136
137
81.3k
  if(!iter->cf)
138
0
    return NULL;
139
140
81.3k
  addr = Curl_dns_cf_get_ai(data, iter->cf, iter->peer,
141
81.3k
                            iter->ai_family, iter->n);
142
81.3k
  if(addr)
143
32.0k
    iter->n++;
144
81.3k
  return addr;
145
81.3k
}
146
147
static bool cf_ai_iter_has_more(struct cf_ai_iter *iter,
148
                                struct Curl_easy *data)
149
13.2k
{
150
13.2k
  return (iter->cf &&
151
13.2k
          !!Curl_dns_cf_get_ai(data, iter->cf, iter->peer,
152
13.2k
                                 iter->ai_family, iter->n));
153
13.2k
}
154
155
struct 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_cfchain chain;     /* current sub-cfilter chain 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
};
173
174
static void cf_ip_attempt_free(struct cf_ip_attempt *a,
175
                               struct Curl_easy *data)
176
96.3k
{
177
96.3k
  if(a) {
178
32.0k
    Curl_cfchain_cleanup(&a->chain, data);
179
32.0k
    Curl_peer_unlink(&a->origin);
180
32.0k
    Curl_peer_unlink(&a->peer);
181
32.0k
    Curl_peer_unlink(&a->tunnel_peer);
182
32.0k
    curlx_free(a);
183
32.0k
  }
184
96.3k
}
185
186
static CURLcode cf_ip_attempt_new(struct cf_ip_attempt **pa,
187
                                  struct Curl_easy *data,
188
                                  struct Curl_cfilter *cf,
189
                                  struct Curl_peer *origin,
190
                                  struct Curl_peer *peer,
191
                                  uint8_t transport_peer,
192
                                  struct Curl_sockaddr_ex *addr,
193
                                  int ai_family,
194
                                  struct Curl_peer *tunnel_peer,
195
                                  uint8_t tunnel_transport,
196
                                  cf_ip_connect_create *cf_create)
197
32.0k
{
198
32.0k
  struct cf_ip_attempt *a;
199
32.0k
  CURLcode result = CURLE_OK;
200
201
32.0k
  *pa = NULL;
202
32.0k
  a = curlx_calloc(1, sizeof(*a));
203
32.0k
  if(!a)
204
6
    return CURLE_OUT_OF_MEMORY;
205
206
32.0k
  Curl_peer_link(&a->origin, origin);
207
32.0k
  Curl_peer_link(&a->peer, peer);
208
32.0k
  a->transport_peer = transport_peer;
209
32.0k
  Curl_peer_link(&a->tunnel_peer, tunnel_peer);
210
32.0k
  a->tunnel_transport = tunnel_transport;
211
32.0k
  a->addr = *addr;
212
32.0k
  a->ai_family = ai_family;
213
32.0k
  a->result = CURLE_OK;
214
32.0k
  a->cf_create = cf_create;
215
32.0k
  Curl_cfchain_init_child(&a->chain, cf->chain);
216
32.0k
  *pa = a;
217
218
32.0k
  result = a->cf_create(&a->chain, data, a->origin, a->peer,
219
32.0k
                        a->transport_peer, &a->addr, a->tunnel_peer,
220
32.0k
                        a->tunnel_transport);
221
32.0k
  if(result) {
222
9
    cf_ip_attempt_free(a, data);
223
9
    *pa = NULL;
224
9
  }
225
32.0k
  return result;
226
32.0k
}
227
228
static CURLcode cf_ip_attempt_connect(struct cf_ip_attempt *a,
229
                                      struct Curl_easy *data,
230
                                      struct Curl_cfilter *cf,
231
                                      bool *connected)
232
31.9k
{
233
31.9k
  (void)cf;
234
31.9k
  *connected = (bool)a->connected;
235
31.9k
  if(!a->result && !*connected) {
236
    /* evaluate again */
237
31.9k
    VERBOSE(Curl_cfchain_trc(data, cf, &a->chain, "attempt"));
238
31.9k
    a->result = Curl_conn_cf_connect(a->chain.cf, data, connected);
239
240
31.9k
    if(!a->result) {
241
29.7k
      if(*connected) {
242
29.7k
        a->connected = TRUE;
243
29.7k
      }
244
29.7k
    }
245
2.26k
    else {
246
2.26k
      if(a->result == CURLE_WEIRD_SERVER_REPLY)
247
0
        a->inconclusive = TRUE;
248
2.26k
      Curl_cfchain_reset(&a->chain, data);
249
2.26k
    }
250
31.9k
  }
251
31.9k
  return a->result;
252
31.9k
}
253
254
struct cf_ip_ballers {
255
  struct cf_ip_attempt *running;
256
  struct cf_ip_attempt **running_tail;  /* &running or &(last->next) */
257
  struct cf_ip_attempt *winner;
258
  struct cf_ai_iter addr_iter;
259
#ifdef USE_IPV6
260
  struct cf_ai_iter ipv6_iter;
261
#endif
262
  struct Curl_peer *origin;
263
  struct Curl_peer *peer;
264
  struct Curl_peer *tunnel_peer;
265
  cf_ip_connect_create *cf_create;   /* for creating cf */
266
  struct curltime last_attempt_started;
267
  timediff_t attempt_delay_ms;
268
  CURLcode last_dead_result;  /* result of last discarded hard failure */
269
  int last_attempt_ai_family;
270
  uint32_t max_concurrent;
271
  uint8_t transport_peer;
272
  uint8_t tunnel_transport;
273
  BIT(had_dead);              /* a hard failure was discarded */
274
};
275
276
static CURLcode cf_ip_attempt_restart(struct cf_ip_attempt *a,
277
                                      struct Curl_cfilter *cf,
278
                                      struct Curl_easy *data)
279
0
{
280
0
  CURLcode result;
281
282
0
  (void)cf;
283
0
  Curl_cfchain_reset(&a->chain, data);
284
0
  a->result = CURLE_OK;
285
0
  a->connected = FALSE;
286
0
  a->inconclusive = FALSE;
287
0
  a->chain.cf = NULL;
288
289
0
  result = a->cf_create(&a->chain, data, a->origin, a->peer,
290
0
                        a->transport_peer, &a->addr, a->tunnel_peer,
291
0
                        a->tunnel_transport);
292
0
  if(!result) {
293
0
    bool dummy;
294
0
    a->result = cf_ip_attempt_connect(a, data, cf, &dummy);
295
0
  }
296
0
  return result;
297
0
}
298
299
static void cf_ip_ballers_clear(struct Curl_easy *data,
300
                                struct cf_ip_ballers *bs)
301
94.0k
{
302
94.0k
  while(bs->running) {
303
0
    struct cf_ip_attempt *a = bs->running;
304
0
    bs->running = a->next;
305
0
    cf_ip_attempt_free(a, data);
306
0
  }
307
94.0k
  bs->running_tail = &bs->running;
308
94.0k
  cf_ip_attempt_free(bs->winner, data);
309
94.0k
  bs->winner = NULL;
310
94.0k
  Curl_peer_unlink(&bs->origin);
311
94.0k
  Curl_peer_unlink(&bs->peer);
312
94.0k
  Curl_peer_unlink(&bs->tunnel_peer);
313
94.0k
}
314
315
static CURLcode cf_ip_ballers_init(struct cf_ip_ballers *bs,
316
                                   struct Curl_easy *data,
317
                                   struct Curl_peer *origin,
318
                                   struct Curl_peer *peer,
319
                                   uint8_t transport_peer,
320
                                   struct Curl_peer *tunnel_peer,
321
                                   uint8_t tunnel_transport,
322
                                   timediff_t attempt_delay_ms,
323
                                   uint32_t max_concurrent)
324
32.1k
{
325
32.1k
  memset(bs, 0, sizeof(*bs));
326
32.1k
  bs->running_tail = &bs->running;
327
32.1k
  bs->cf_create = get_cf_create(transport_peer, !!tunnel_peer);
328
32.1k
  if(!bs->cf_create) {
329
0
    failf(data, "unsupported transport type %u%s",
330
0
          transport_peer, tunnel_peer ? " to proxy" : "");
331
0
    return CURLE_UNSUPPORTED_PROTOCOL;
332
0
  }
333
32.1k
  Curl_peer_link(&bs->origin, origin);
334
32.1k
  Curl_peer_link(&bs->peer, peer);
335
32.1k
  bs->transport_peer = transport_peer;
336
32.1k
  Curl_peer_link(&bs->tunnel_peer, tunnel_peer);
337
32.1k
  bs->tunnel_transport = tunnel_transport;
338
32.1k
  bs->attempt_delay_ms = attempt_delay_ms;
339
32.1k
  bs->max_concurrent = max_concurrent;
340
32.1k
  bs->last_attempt_ai_family = AF_INET; /* so AF_INET6 is next */
341
32.1k
  return CURLE_OK;
342
32.1k
}
343
344
static void cf_ip_ballers_prune(struct cf_ip_ballers *bs,
345
                                struct Curl_cfilter *cf,
346
                                struct Curl_easy *data,
347
                                uint32_t max_concurrent)
348
32.0k
{
349
32.0k
  struct cf_ip_attempt *a = NULL, **panchor;
350
32.0k
  uint32_t ongoing = 0;
351
352
32.0k
  for(a = bs->running; a; a = a->next) {
353
0
    if(!a->result && !a->connected)
354
0
      ++ongoing;
355
0
  }
356
357
32.0k
  panchor = &bs->running;
358
32.0k
  while(*panchor && (ongoing > max_concurrent)) {
359
0
    a = *panchor;
360
0
    if(!a->result && !a->connected) {
361
0
      *panchor = a->next;
362
0
      if(!a->next)
363
0
        bs->running_tail = panchor;
364
0
      a->next = NULL;
365
0
      cf_ip_attempt_free(a, data);
366
0
      --ongoing;
367
0
      CURL_TRC_CF(data, cf, "discarding oldest attempt to keep limit");
368
0
    }
369
0
    else {
370
0
      panchor = &a->next;
371
0
    }
372
0
  }
373
32.0k
}
374
375
static CURLcode cf_ip_ballers_run(struct cf_ip_ballers *bs,
376
                                  struct Curl_cfilter *cf,
377
                                  struct Curl_easy *data,
378
                                  bool dns_resolved,
379
                                  bool *connected)
380
38.6k
{
381
38.6k
  CURLcode result = CURLE_OK;
382
38.6k
  struct cf_ip_attempt *a = NULL, **panchor;
383
38.6k
  bool do_more;
384
38.6k
  timediff_t next_expire_ms;
385
38.6k
  uint32_t inconclusive, ongoing;
386
38.6k
  const struct curltime *pnow = NULL;
387
38.6k
  VERBOSE(int i);
388
389
38.6k
  if(bs->winner)
390
0
    return CURLE_OK;
391
392
70.6k
evaluate:
393
70.6k
  ongoing = inconclusive = 0;
394
395
  /* check if a running baller connects now */
396
70.6k
  VERBOSE(i = -1);
397
72.8k
  for(panchor = &bs->running; *panchor;) {
398
31.9k
    VERBOSE(++i);
399
31.9k
    a = *panchor;
400
31.9k
    a->result = cf_ip_attempt_connect(a, data, cf, connected);
401
31.9k
    if(!a->result) {
402
29.7k
      if(*connected) {
403
        /* connected, declare the winner, remove from running,
404
         * clear remaining running list. */
405
29.7k
        CURL_TRC_CF(data, cf, "connect attempt #%d successful", i);
406
29.7k
        bs->winner = a;
407
29.7k
        *panchor = a->next;
408
29.7k
        a->next = NULL;
409
29.7k
        while(bs->running) {
410
0
          a = bs->running;
411
0
          bs->running = a->next;
412
0
          cf_ip_attempt_free(a, data);
413
0
        }
414
29.7k
        bs->running_tail = &bs->running;
415
29.7k
        return CURLE_OK;
416
29.7k
      }
417
      /* still running */
418
0
      ++ongoing;
419
0
      panchor = &(*panchor)->next;
420
0
    }
421
2.26k
    else if(a->inconclusive) { /* failed, but inconclusive */
422
0
      ++inconclusive;
423
0
      panchor = &(*panchor)->next;
424
0
    }
425
2.26k
    else {
426
      /* hard failure, discard right away */
427
2.26k
      *panchor = a->next;
428
2.26k
      if(!a->next)
429
2.26k
        bs->running_tail = panchor;
430
2.26k
      a->next = NULL;
431
2.26k
      bs->last_dead_result = a->result;
432
2.26k
      bs->had_dead = TRUE;
433
2.26k
      cf_ip_attempt_free(a, data);
434
2.26k
    }
435
31.9k
  }
436
40.8k
  if(bs->running)
437
0
    CURL_TRC_CF(data, cf, "checked connect attempts: "
438
40.8k
                "%u ongoing, %u inconclusive", ongoing, inconclusive);
439
440
  /* Nothing connected. Only now check the time: an attempt that completed
441
     since we were last called must be accepted even if the application
442
     was late in calling us. */
443
40.8k
  pnow = Curl_pgrs_now(data);
444
40.8k
  if(Curl_timeleft_now_ms(data, pnow) < 0) {
445
0
    failf(data, "Connection timeout after %" FMT_OFF_T " ms",
446
0
          Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE));
447
0
    return CURLE_OPERATION_TIMEDOUT;
448
0
  }
449
450
  /* no attempt connected yet, start another one? */
451
40.8k
  if(!ongoing) {
452
40.8k
    do_more = TRUE;
453
40.8k
  }
454
0
  else {
455
0
    bool more_possible = cf_ai_iter_has_more(&bs->addr_iter, data);
456
0
#ifdef USE_IPV6
457
0
    if(!more_possible)
458
0
      more_possible = cf_ai_iter_has_more(&bs->ipv6_iter, data);
459
0
#endif
460
0
    do_more = more_possible &&
461
0
      (curlx_ptimediff_ms(pnow, &bs->last_attempt_started) >=
462
0
       bs->attempt_delay_ms);
463
0
    if(do_more)
464
0
      CURL_TRC_CF(data, cf, "happy eyeballs timeout expired, "
465
0
                  "start next attempt");
466
0
  }
467
468
40.8k
  if(do_more) {
469
    /* start the next attempt if there is another ip address to try.
470
     * Alternate between address families when possible. */
471
40.8k
    const struct Curl_addrinfo *ai = NULL;
472
40.8k
    int ai_family = 0;
473
40.8k
    CURL_TRC_CF(data, cf, "want to do more");
474
40.8k
#ifdef USE_IPV6
475
40.8k
    if((bs->last_attempt_ai_family == AF_INET) ||
476
40.8k
       !cf_ai_iter_has_more(&bs->addr_iter, data)) {
477
40.8k
      ai = cf_ai_iter_next(&bs->ipv6_iter, data);
478
40.8k
      ai_family = bs->ipv6_iter.ai_family;
479
40.8k
      CURL_TRC_CF(data, cf, "check for next AAAA address: %s",
480
40.8k
                  ai ? "found" : "none");
481
40.8k
    }
482
40.8k
#endif
483
40.8k
    if(!ai) {
484
40.5k
      ai = cf_ai_iter_next(&bs->addr_iter, data);
485
40.5k
      ai_family = bs->addr_iter.ai_family;
486
40.5k
      CURL_TRC_CF(data, cf, "check for next A address: %s",
487
40.5k
                  ai ? "found" : "none");
488
40.5k
    }
489
    /* We are (re-)starting attempts. We are not interested in
490
     * keeping old failure information. The new attempt will either
491
     * succeed or persist new failure. */
492
40.8k
    Curl_reset_fail(data);
493
494
40.8k
    if(ai) {  /* try another address */
495
32.0k
      struct Curl_sockaddr_ex addr;
496
497
      /* Discard oldest to make room for new attempt */
498
32.0k
      if(bs->max_concurrent)
499
32.0k
        cf_ip_ballers_prune(bs, cf, data, bs->max_concurrent - 1);
500
501
32.0k
      result = Curl_socket_addr_from_ai(&addr, ai, bs->transport_peer);
502
32.0k
      if(result)
503
0
        goto out;
504
505
32.0k
      result = cf_ip_attempt_new(&a, data, cf, bs->origin, bs->peer,
506
32.0k
                                 bs->transport_peer, &addr, ai_family,
507
32.0k
                                 bs->tunnel_peer, bs->tunnel_transport,
508
32.0k
                                 bs->cf_create);
509
32.0k
      CURL_TRC_CF(data, cf, "starting %s attempt for ipv%s -> %d",
510
32.0k
                  bs->running ? "next" : "first",
511
32.0k
                  (ai_family == AF_INET) ? "4" : "6", (int)result);
512
32.0k
      if(result)
513
15
        goto out;
514
31.9k
      DEBUGASSERT(a);
515
516
      /* append to running list */
517
31.9k
      *bs->running_tail = a;
518
31.9k
      a->next = NULL;
519
31.9k
      bs->running_tail = &a->next;
520
31.9k
      bs->last_attempt_started = *pnow;
521
31.9k
      bs->last_attempt_ai_family = ai_family;
522
      /* and run everything again */
523
31.9k
      goto evaluate;
524
31.9k
    }
525
8.86k
    else if(inconclusive) {
526
      /* tried all addresses, no success but some where inconclusive.
527
       * Let's restart the inconclusive ones. */
528
0
      timediff_t since_ms =
529
0
        curlx_ptimediff_ms(pnow, &bs->last_attempt_started);
530
0
      timediff_t delay_ms = bs->attempt_delay_ms - since_ms;
531
0
      if(delay_ms <= 0) {
532
0
        CURL_TRC_CF(data, cf, "all attempts inconclusive, restarting one");
533
0
        VERBOSE(i = -1);
534
0
        for(a = bs->running; a; a = a->next) {
535
0
          VERBOSE(++i);
536
0
          if(!a->inconclusive)
537
0
            continue;
538
0
          result = cf_ip_attempt_restart(a, cf, data);
539
0
          CURL_TRC_CF(data, cf, "restarted baller %d -> %d", i, (int)result);
540
0
          if(result) /* serious failure */
541
0
            goto out;
542
0
          bs->last_attempt_started = *pnow;
543
0
          goto evaluate;
544
0
        }
545
0
        DEBUGASSERT(0); /* should not come here */
546
0
      }
547
0
      else {
548
        /* let's wait some more before restarting */
549
0
        infof(data, "connect attempts inconclusive, retrying "
550
0
                    "in %" FMT_TIMEDIFF_T "ms", delay_ms);
551
0
        Curl_expire_set(data, EXPIRE_HAPPY_EYEBALLS, delay_ms, pnow);
552
0
      }
553
      /* attempt timeout for restart has not expired yet */
554
0
      goto out;
555
0
    }
556
8.86k
    else if(!ongoing && dns_resolved) {
557
      /* no more addresses, no inconclusive attempts */
558
2.26k
      CURL_TRC_CF(data, cf, "no more attempts to try");
559
2.26k
      DEBUGASSERT(!bs->running);
560
2.26k
      result = bs->had_dead ? bs->last_dead_result : CURLE_COULDNT_CONNECT;
561
2.26k
    }
562
40.8k
  }
563
564
8.87k
out:
565
8.87k
  if(!result) {
566
6.59k
    bool more_possible;
567
568
    /* when do we need to be called again? */
569
6.59k
    pnow = Curl_pgrs_now(data);
570
6.59k
    next_expire_ms = Curl_timeleft_now_ms(data, pnow);
571
6.59k
    if(next_expire_ms < 0) {
572
0
      failf(data, "Connection timeout after %" FMT_OFF_T " ms",
573
0
            Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE));
574
0
      return CURLE_OPERATION_TIMEDOUT;
575
0
    }
576
577
6.59k
    more_possible = cf_ai_iter_has_more(&bs->addr_iter, data);
578
6.59k
#ifdef USE_IPV6
579
6.59k
    if(!more_possible)
580
6.59k
      more_possible = cf_ai_iter_has_more(&bs->ipv6_iter, data);
581
6.59k
#endif
582
6.59k
    if(more_possible) {
583
0
      timediff_t expire_ms, elapsed_ms;
584
585
0
      elapsed_ms = curlx_ptimediff_ms(pnow, &bs->last_attempt_started);
586
0
      expire_ms = CURLMAX(bs->attempt_delay_ms - elapsed_ms, 0);
587
0
      next_expire_ms = CURLMIN(next_expire_ms, expire_ms);
588
0
      if(next_expire_ms <= 0) {
589
0
        CURL_TRC_CF(data, cf, "HAPPY_EYEBALLS timeout due, re-evaluate");
590
0
        goto evaluate;
591
0
      }
592
0
      CURL_TRC_CF(data, cf, "next HAPPY_EYEBALLS timeout in %" FMT_TIMEDIFF_T
593
0
                  "ms", next_expire_ms);
594
0
      Curl_expire_set(data, EXPIRE_HAPPY_EYEBALLS, next_expire_ms, pnow);
595
0
    }
596
6.59k
  }
597
8.87k
  return result;
598
8.87k
}
599
600
static CURLcode cf_ip_ballers_shutdown(struct cf_ip_ballers *bs,
601
                                       struct Curl_easy *data,
602
                                       bool *done)
603
0
{
604
0
  struct cf_ip_attempt *a;
605
606
  /* shutdown all ballers that have not done so already. If one fails,
607
   * continue shutting down others until all are shutdown. */
608
0
  *done = TRUE;
609
0
  for(a = bs->running; a; a = a->next) {
610
0
    bool bdone = FALSE;
611
0
    if(a->shutdown || !a->chain.cf)
612
0
      continue;
613
0
    a->result = a->chain.cf->cft->do_shutdown(a->chain.cf, data, &bdone);
614
0
    if(a->result || bdone)
615
0
      a->shutdown = TRUE; /* treat a failed shutdown as done */
616
0
    else
617
0
      *done = FALSE;
618
0
  }
619
0
  return CURLE_OK;
620
0
}
621
622
static CURLcode cf_ip_ballers_pollset(struct cf_ip_ballers *bs,
623
                                      struct Curl_easy *data,
624
                                      struct easy_pollset *ps)
625
6.53k
{
626
6.53k
  struct cf_ip_attempt *a;
627
6.53k
  CURLcode result = CURLE_OK;
628
6.53k
  for(a = bs->running; a && !result; a = a->next) {
629
0
    if(a->result)
630
0
      continue;
631
0
    result = Curl_conn_cf_adjust_pollset(a->chain.cf, data, ps);
632
0
  }
633
6.53k
  return result;
634
6.53k
}
635
636
static bool cf_ip_ballers_pending(struct cf_ip_ballers *bs,
637
                                  const struct Curl_easy *data)
638
0
{
639
0
  struct cf_ip_attempt *a;
640
641
0
  for(a = bs->running; a; a = a->next) {
642
0
    if(a->result)
643
0
      continue;
644
0
    if(a->chain.cf &&
645
0
       a->chain.cf->cft->has_data_pending(a->chain.cf, data))
646
0
      return TRUE;
647
0
  }
648
0
  return FALSE;
649
0
}
650
651
static int cf_ip_ballers_min_reply_ms(struct cf_ip_ballers *bs,
652
                                      struct Curl_easy *data)
653
0
{
654
0
  int reply_ms = -1, breply_ms;
655
0
  struct cf_ip_attempt *a;
656
657
0
  for(a = bs->running; a; a = a->next) {
658
0
    if(a->chain.cf &&
659
0
       !a->chain.cf->cft->query(a->chain.cf, data,
660
0
                                   CF_QUERY_CONNECT_REPLY_MS,
661
0
                                   &breply_ms, NULL) &&
662
0
       breply_ms >= 0 && (reply_ms < 0 || breply_ms < reply_ms))
663
0
      reply_ms = breply_ms;
664
0
  }
665
0
  return reply_ms;
666
0
}
667
668
typedef enum {
669
  SCFST_INIT,
670
  SCFST_WAITING,
671
  SCFST_DONE
672
} cf_connect_state;
673
674
struct cf_ip_happy_ctx {
675
  cf_ip_connect_create *cf_create;
676
  cf_connect_state state;
677
  struct cf_ip_ballers ballers;
678
  BIT(dns_resolved);
679
};
680
681
static CURLcode is_connected(struct Curl_cfilter *cf,
682
                             struct Curl_easy *data,
683
                             bool *connected)
684
38.6k
{
685
38.6k
  struct cf_ip_happy_ctx *ctx = cf->ctx;
686
38.6k
  struct connectdata *conn = CF_CONN(cf);
687
38.6k
  CURLcode result;
688
689
38.6k
  result = cf_ip_ballers_run(&ctx->ballers, cf, data,
690
38.6k
                             (bool)ctx->dns_resolved, connected);
691
692
38.6k
  if(!result)
693
36.3k
    return CURLE_OK;
694
2.28k
  else {
695
2.28k
    struct Curl_peer *peer = NULL, *proxy_peer = NULL;
696
2.28k
    char viamsg[160];
697
698
2.28k
    peer = Curl_cf_get_first_peer(cf);
699
2.28k
    if(!conn->origin || !peer)
700
0
      return CURLE_FAILED_INIT;
701
702
2.28k
#ifndef CURL_DISABLE_PROXY
703
2.28k
    if(conn->socks_proxy.peer)
704
0
      proxy_peer = conn->socks_proxy.peer;
705
2.28k
    else if(conn->http_proxy.peer)
706
0
      proxy_peer = conn->http_proxy.peer;
707
2.28k
#endif
708
709
2.28k
    viamsg[0] = 0;
710
2.28k
    if(!Curl_peer_equal(peer, conn->origin) &&
711
2.19k
       !Curl_peer_equal(peer, proxy_peer)) {
712
2.19k
#ifdef USE_UNIX_SOCKETS
713
2.19k
      if(peer->unix_socket)
714
0
        curl_msnprintf(viamsg, sizeof(viamsg), " over unix://%s",
715
0
                       peer->hostname);
716
2.19k
      else
717
2.19k
#endif
718
2.19k
      curl_msnprintf(viamsg, sizeof(viamsg), " via %s:%u",
719
2.19k
                     peer->hostname, peer->port);
720
2.19k
    }
721
722
2.28k
    failf(data, "Failed to connect to %s:%u%s %s%s%safter "
723
2.28k
          "%" FMT_TIMEDIFF_T " ms: %s",
724
2.28k
          conn->origin->hostname, conn->origin->port, viamsg,
725
2.28k
          proxy_peer ? "over proxy " : "",
726
2.28k
          proxy_peer ? proxy_peer->hostname : "",
727
2.28k
          proxy_peer ? " " : "",
728
2.28k
          Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE),
729
2.28k
          curl_easy_strerror(result));
730
731
2.28k
#ifdef SOCKETIMEDOUT
732
2.28k
    if(SOCKETIMEDOUT == data->state.os_errno)
733
0
      result = CURLE_OPERATION_TIMEDOUT;
734
2.28k
#endif
735
736
2.28k
    return result;
737
2.28k
  }
738
38.6k
}
739
740
32.1k
#define IP_HE_MAX_CONCURRENT_ATTEMPTS     6
741
742
static CURLcode cf_ip_happy_init(struct Curl_cfilter *cf,
743
                                 struct Curl_easy *data)
744
32.1k
{
745
32.1k
  struct cf_ip_happy_ctx *ctx = cf->ctx;
746
747
32.1k
  if(Curl_timeleft_ms(data) < 0) {
748
    /* a precaution, no need to continue if time already is up */
749
0
    failf(data, "Connection time-out");
750
0
    return CURLE_OPERATION_TIMEDOUT;
751
0
  }
752
753
32.1k
  if(ctx->ballers.transport_peer == TRNSPRT_UNIX) {
754
0
#ifdef USE_UNIX_SOCKETS
755
0
    cf_ai_iter_init(&ctx->ballers.addr_iter, cf, ctx->ballers.peer, AF_UNIX);
756
#else
757
    return CURLE_UNSUPPORTED_PROTOCOL;
758
#endif
759
0
  }
760
32.1k
  else { /* TCP/UDP/QUIC */
761
32.1k
#ifdef USE_IPV6
762
32.1k
    cf_ai_iter_init(&ctx->ballers.ipv6_iter, cf, ctx->ballers.peer, AF_INET6);
763
32.1k
#endif
764
32.1k
    cf_ai_iter_init(&ctx->ballers.addr_iter, cf, ctx->ballers.peer, AF_INET);
765
32.1k
  }
766
767
32.1k
  CURL_TRC_CF(data, cf, "init ip ballers for transport %u",
768
32.1k
              ctx->ballers.transport_peer);
769
32.1k
  return CURLE_OK;
770
32.1k
}
771
772
static void cf_ip_happy_ctx_clear(struct cf_ip_happy_ctx *ctx,
773
                                  struct Curl_easy *data)
774
94.0k
{
775
94.0k
  DEBUGASSERT(ctx);
776
94.0k
  if(ctx)
777
94.0k
    cf_ip_ballers_clear(data, &ctx->ballers);
778
94.0k
}
779
780
static void cf_ip_happy_ctx_destroy(struct cf_ip_happy_ctx *ctx,
781
                                    struct Curl_easy *data)
782
32.1k
{
783
32.1k
  if(ctx) {
784
32.1k
    cf_ip_happy_ctx_clear(ctx, data);
785
32.1k
    curlx_free(ctx);
786
32.1k
  }
787
32.1k
}
788
789
static CURLcode cf_ip_happy_shutdown(struct Curl_cfilter *cf,
790
                                     struct Curl_easy *data,
791
                                     bool *done)
792
0
{
793
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
794
0
  CURLcode result = CURLE_OK;
795
796
0
  DEBUGASSERT(data);
797
0
  if(cf->connected) {
798
0
    *done = TRUE;
799
0
    return CURLE_OK;
800
0
  }
801
802
0
  result = cf_ip_ballers_shutdown(&ctx->ballers, data, done);
803
0
  CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", (int)result, *done);
804
0
  return result;
805
0
}
806
807
static CURLcode cf_ip_happy_adjust_pollset(struct Curl_cfilter *cf,
808
                                           struct Curl_easy *data,
809
                                           struct easy_pollset *ps)
810
6.79k
{
811
6.79k
  struct cf_ip_happy_ctx *ctx = cf->ctx;
812
6.79k
  CURLcode result = CURLE_OK;
813
814
6.79k
  if(!cf->connected) {
815
6.53k
    result = cf_ip_ballers_pollset(&ctx->ballers, data, ps);
816
6.53k
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %u socks", (int)result,
817
6.53k
                ps->n);
818
6.53k
  }
819
6.79k
  return result;
820
6.79k
}
821
822
static CURLcode cf_ip_happy_connect(struct Curl_cfilter *cf,
823
                                    struct Curl_easy *data,
824
                                    bool *done)
825
38.6k
{
826
38.6k
  struct cf_ip_happy_ctx *ctx = cf->ctx;
827
38.6k
  CURLcode result = CURLE_OK;
828
829
  /* -Werror=null-dereference finds false positives suddenly. */
830
38.6k
  if(!data)
831
0
    return CURLE_FAILED_INIT;
832
833
38.6k
  if(cf->connected) {
834
0
    *done = TRUE;
835
0
    return CURLE_OK;
836
0
  }
837
838
38.6k
  DEBUGASSERT(ctx);
839
38.6k
  *done = FALSE;
840
841
38.6k
  if(!ctx->dns_resolved) {
842
38.6k
    result = Curl_dns_cf_addr_result(cf, ctx->ballers.peer);
843
38.6k
    if(!result)
844
32.0k
      ctx->dns_resolved = TRUE;
845
6.59k
    else if(result == CURLE_AGAIN) {
846
6.59k
      result = CURLE_OK;
847
6.59k
    }
848
0
    else /* real error */
849
0
      goto out;
850
38.6k
  }
851
852
38.6k
  switch(ctx->state) {
853
32.1k
  case SCFST_INIT:
854
32.1k
    DEBUGASSERT(CURL_SOCKET_BAD == cf->chain->sock);
855
32.1k
    DEBUGASSERT(!cf->connected);
856
32.1k
    result = cf_ip_happy_init(cf, data);
857
32.1k
    if(result)
858
0
      goto out;
859
32.1k
    ctx->state = SCFST_WAITING;
860
32.1k
    FALLTHROUGH();
861
38.6k
  case SCFST_WAITING:
862
38.6k
    result = is_connected(cf, data, done);
863
38.6k
    if(!result && *done) {
864
29.7k
      DEBUGASSERT(ctx->ballers.winner);
865
29.7k
      DEBUGASSERT(ctx->ballers.winner->chain.cf);
866
29.7k
      DEBUGASSERT(ctx->ballers.winner->chain.cf->connected);
867
      /* we have a winner. Install and activate it.
868
       * close/free all others. */
869
29.7k
      ctx->state = SCFST_DONE;
870
29.7k
      cf->connected = TRUE;
871
29.7k
      Curl_cfchain_merge(cf->chain, &ctx->ballers.winner->chain);
872
29.7k
      VERBOSE(Curl_cfchain_trc(data, cf, cf->chain, "merged winner"));
873
874
29.7k
      if(CF_CONN(cf)->scheme->protocol & PROTO_FAMILY_SSH)
875
0
        Curl_pgrsTime(data, TIMER_APPCONNECT); /* we are connected already */
876
29.7k
#ifdef CURLVERBOSE
877
29.7k
      if(Curl_trc_cf_is_verbose(cf, data)) {
878
1.23k
        struct ip_quadruple ipquad;
879
1.23k
        bool is_ipv6;
880
1.23k
        if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) {
881
1.23k
          CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u",
882
1.23k
                      ctx->ballers.peer->hostname,
883
1.23k
                      ipquad.remote_ip, ipquad.remote_port);
884
1.23k
        }
885
1.23k
      }
886
29.7k
#endif
887
29.7k
      cf_ip_happy_ctx_clear(ctx, data);
888
29.7k
      Curl_expire_clear(data, EXPIRE_HAPPY_EYEBALLS);
889
      /* whatever errors were reported by ballers, clear our errorbuf */
890
29.7k
      Curl_reset_fail(data);
891
29.7k
      data->info.numconnects++; /* to track the # of connections made */
892
29.7k
    }
893
38.6k
    break;
894
38.6k
  case SCFST_DONE:
895
0
    *done = TRUE;
896
0
    break;
897
38.6k
  }
898
38.6k
out:
899
38.6k
  return result;
900
38.6k
}
901
902
static bool cf_ip_happy_data_pending(struct Curl_cfilter *cf,
903
                                     const struct Curl_easy *data)
904
2
{
905
2
  struct cf_ip_happy_ctx *ctx = cf->ctx;
906
907
2
  if(!cf->connected) {
908
0
    return cf_ip_ballers_pending(&ctx->ballers, data);
909
0
  }
910
2
  return cf->next->cft->has_data_pending(cf->next, data);
911
2
}
912
913
static CURLcode cf_ip_happy_query(struct Curl_cfilter *cf,
914
                                  struct Curl_easy *data,
915
                                  int query, int *pres1, void *pres2)
916
46.2k
{
917
46.2k
  struct cf_ip_happy_ctx *ctx = cf->ctx;
918
919
46.2k
  if(!cf->connected) {
920
6.70k
    switch(query) {
921
0
    case CF_QUERY_CONNECT_REPLY_MS: {
922
0
      *pres1 = cf_ip_ballers_min_reply_ms(&ctx->ballers, data);
923
0
      CURL_TRC_CF(data, cf, "query connect reply: %dms", *pres1);
924
0
      return CURLE_OK;
925
0
    }
926
6.70k
    default:
927
6.70k
      break;
928
6.70k
    }
929
6.70k
  }
930
931
46.2k
  return cf->next ?
932
39.5k
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
933
46.2k
    CURLE_UNKNOWN_OPTION;
934
46.2k
}
935
936
static void cf_ip_happy_destroy(struct Curl_cfilter *cf,
937
                                struct Curl_easy *data)
938
32.1k
{
939
32.1k
  struct cf_ip_happy_ctx *ctx = cf->ctx;
940
941
32.1k
  CURL_TRC_CF(data, cf, "destroy");
942
32.1k
  if(ctx) {
943
32.1k
    cf_ip_happy_ctx_clear(ctx, data);
944
32.1k
    cf_ip_happy_ctx_destroy(ctx, data);
945
32.1k
  }
946
32.1k
}
947
948
struct Curl_cftype Curl_cft_ip_happy = {
949
  "HAPPY-EYEBALLS",
950
  CF_TYPE_SETUP,
951
  CURL_LOG_LVL_NONE,
952
  cf_ip_happy_destroy,
953
  cf_ip_happy_connect,
954
  cf_ip_happy_shutdown,
955
  cf_ip_happy_adjust_pollset,
956
  cf_ip_happy_data_pending,
957
  Curl_cf_def_send,
958
  Curl_cf_def_recv,
959
  Curl_cf_def_cntrl,
960
  Curl_cf_def_conn_is_alive,
961
  Curl_cf_def_conn_keep_alive,
962
  cf_ip_happy_query,
963
};
964
965
static CURLcode cf_ip_happy_create(struct Curl_cfilter **pcf,
966
                                   struct Curl_easy *data,
967
                                   struct Curl_peer *origin,
968
                                   struct Curl_peer *peer,
969
                                   uint8_t transport_peer,
970
                                   struct connectdata *conn,
971
                                   struct Curl_peer *tunnel_peer,
972
                                   uint8_t tunnel_transport)
973
32.1k
{
974
32.1k
  struct cf_ip_happy_ctx *ctx = NULL;
975
32.1k
  CURLcode result;
976
977
32.1k
  (void)data;
978
32.1k
  (void)conn;
979
32.1k
  *pcf = NULL;
980
32.1k
  ctx = curlx_calloc(1, sizeof(*ctx));
981
32.1k
  if(!ctx) {
982
2
    result = CURLE_OUT_OF_MEMORY;
983
2
    goto out;
984
2
  }
985
32.1k
  result = cf_ip_ballers_init(&ctx->ballers, data,
986
32.1k
                              origin, peer, transport_peer,
987
32.1k
                              tunnel_peer, tunnel_transport,
988
32.1k
                              data->set.happy_eyeballs_timeout,
989
32.1k
                              IP_HE_MAX_CONCURRENT_ATTEMPTS);
990
32.1k
  if(result)
991
0
    goto out;
992
993
32.1k
  result = Curl_cf_create(pcf, &Curl_cft_ip_happy, ctx);
994
995
32.1k
out:
996
32.1k
  if(result) {
997
9
    curlx_safefree(*pcf);
998
9
    cf_ip_happy_ctx_destroy(ctx, data);
999
9
  }
1000
32.1k
  return result;
1001
32.1k
}
1002
1003
CURLcode Curl_cf_ip_happy_insert_after(struct Curl_cfilter *cf_at,
1004
                                       struct Curl_easy *data,
1005
                                       struct Curl_peer *origin,
1006
                                       struct Curl_peer *peer,
1007
                                       uint8_t transport_peer,
1008
                                       struct Curl_peer *tunnel_peer,
1009
                                       uint8_t tunnel_transport)
1010
32.1k
{
1011
32.1k
  struct Curl_cfilter *cf;
1012
32.1k
  CURLcode result;
1013
1014
  /* Need to be first */
1015
32.1k
  DEBUGASSERT(cf_at);
1016
32.1k
  result = cf_ip_happy_create(&cf, data, origin, peer, transport_peer,
1017
32.1k
                              CF_CONN(cf_at), tunnel_peer, tunnel_transport);
1018
32.1k
  if(result)
1019
9
    return result;
1020
1021
32.1k
  Curl_conn_cf_insert_after(cf_at, cf);
1022
32.1k
  return CURLE_OK;
1023
32.1k
}