Coverage Report

Created: 2026-12-01 06:27

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
0
{
89
0
  size_t i;
90
0
  for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) {
91
0
    if((transport == transport_providers[i].transport) &&
92
0
       (tunnel == transport_providers[i].tunnel))
93
0
      return transport_providers[i].cf_create;
94
0
  }
95
0
  return NULL;
96
0
}
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
0
{
126
0
  iter->cf = cf;
127
0
  iter->peer = peer; /* not linked, ctx->ballers owns and has same lifetime */
128
0
  iter->ai_family = ai_family;
129
0
  iter->n = 0;
130
0
}
131
132
static const struct Curl_addrinfo *cf_ai_iter_next(struct cf_ai_iter *iter,
133
                                                   struct Curl_easy *data)
134
0
{
135
0
  const struct Curl_addrinfo *addr;
136
137
0
  if(!iter->cf)
138
0
    return NULL;
139
140
0
  addr = Curl_dns_cf_get_ai(data, iter->cf, iter->peer,
141
0
                            iter->ai_family, iter->n);
142
0
  if(addr)
143
0
    iter->n++;
144
0
  return addr;
145
0
}
146
147
static bool cf_ai_iter_has_more(struct cf_ai_iter *iter,
148
                                struct Curl_easy *data)
149
0
{
150
0
  return (iter->cf &&
151
0
          !!Curl_dns_cf_get_ai(data, iter->cf, iter->peer,
152
0
                                 iter->ai_family, iter->n));
153
0
}
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
0
{
177
0
  if(a) {
178
0
    Curl_cfchain_cleanup(&a->chain, data);
179
0
    Curl_peer_unlink(&a->origin);
180
0
    Curl_peer_unlink(&a->peer);
181
0
    Curl_peer_unlink(&a->tunnel_peer);
182
0
    curlx_free(a);
183
0
  }
184
0
}
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
0
{
198
0
  struct cf_ip_attempt *a;
199
0
  CURLcode result = CURLE_OK;
200
201
0
  *pa = NULL;
202
0
  a = curlx_calloc(1, sizeof(*a));
203
0
  if(!a)
204
0
    return CURLE_OUT_OF_MEMORY;
205
206
0
  Curl_peer_link(&a->origin, origin);
207
0
  Curl_peer_link(&a->peer, peer);
208
0
  a->transport_peer = transport_peer;
209
0
  Curl_peer_link(&a->tunnel_peer, tunnel_peer);
210
0
  a->tunnel_transport = tunnel_transport;
211
0
  a->addr = *addr;
212
0
  a->ai_family = ai_family;
213
0
  a->result = CURLE_OK;
214
0
  a->cf_create = cf_create;
215
0
  Curl_cfchain_init_child(&a->chain, cf->chain);
216
0
  *pa = a;
217
218
0
  result = a->cf_create(&a->chain, data, a->origin, a->peer,
219
0
                        a->transport_peer, &a->addr, a->tunnel_peer,
220
0
                        a->tunnel_transport);
221
0
  if(result) {
222
0
    cf_ip_attempt_free(a, data);
223
0
    *pa = NULL;
224
0
  }
225
0
  return result;
226
0
}
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
0
{
233
0
  (void)cf;
234
0
  *connected = (bool)a->connected;
235
0
  if(!a->result && !*connected) {
236
    /* evaluate again */
237
0
    VERBOSE(Curl_cfchain_trc(data, cf, &a->chain, "attempt"));
238
0
    a->result = Curl_conn_cf_connect(a->chain.cf, data, connected);
239
240
0
    if(!a->result) {
241
0
      if(*connected) {
242
0
        a->connected = TRUE;
243
0
      }
244
0
    }
245
0
    else {
246
0
      if(a->result == CURLE_WEIRD_SERVER_REPLY)
247
0
        a->inconclusive = TRUE;
248
0
      Curl_cfchain_reset(&a->chain, data);
249
0
    }
250
0
  }
251
0
  return a->result;
252
0
}
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
0
{
302
0
  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
0
  bs->running_tail = &bs->running;
308
0
  cf_ip_attempt_free(bs->winner, data);
309
0
  bs->winner = NULL;
310
0
  Curl_peer_unlink(&bs->origin);
311
0
  Curl_peer_unlink(&bs->peer);
312
0
  Curl_peer_unlink(&bs->tunnel_peer);
313
0
}
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
0
{
325
0
  memset(bs, 0, sizeof(*bs));
326
0
  bs->running_tail = &bs->running;
327
0
  bs->cf_create = get_cf_create(transport_peer, !!tunnel_peer);
328
0
  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
0
  Curl_peer_link(&bs->origin, origin);
334
0
  Curl_peer_link(&bs->peer, peer);
335
0
  bs->transport_peer = transport_peer;
336
0
  Curl_peer_link(&bs->tunnel_peer, tunnel_peer);
337
0
  bs->tunnel_transport = tunnel_transport;
338
0
  bs->attempt_delay_ms = attempt_delay_ms;
339
0
  bs->max_concurrent = max_concurrent;
340
0
  bs->last_attempt_ai_family = AF_INET; /* so AF_INET6 is next */
341
0
  return CURLE_OK;
342
0
}
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
0
{
349
0
  struct cf_ip_attempt *a = NULL, **panchor;
350
0
  uint32_t ongoing = 0;
351
352
0
  for(a = bs->running; a; a = a->next) {
353
0
    if(!a->result && !a->connected)
354
0
      ++ongoing;
355
0
  }
356
357
0
  panchor = &bs->running;
358
0
  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
0
}
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
0
{
381
0
  CURLcode result = CURLE_OK;
382
0
  struct cf_ip_attempt *a = NULL, **panchor;
383
0
  bool do_more;
384
0
  timediff_t next_expire_ms;
385
0
  uint32_t inconclusive, ongoing;
386
0
  const struct curltime *pnow = NULL;
387
0
  VERBOSE(int i);
388
389
0
  if(bs->winner)
390
0
    return CURLE_OK;
391
392
0
evaluate:
393
0
  ongoing = inconclusive = 0;
394
395
  /* check if a running baller connects now */
396
0
  VERBOSE(i = -1);
397
0
  for(panchor = &bs->running; *panchor;) {
398
0
    VERBOSE(++i);
399
0
    a = *panchor;
400
0
    a->result = cf_ip_attempt_connect(a, data, cf, connected);
401
0
    if(!a->result) {
402
0
      if(*connected) {
403
        /* connected, declare the winner, remove from running,
404
         * clear remaining running list. */
405
0
        CURL_TRC_CF(data, cf, "connect attempt #%d successful", i);
406
0
        bs->winner = a;
407
0
        *panchor = a->next;
408
0
        a->next = NULL;
409
0
        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
0
        bs->running_tail = &bs->running;
415
0
        return CURLE_OK;
416
0
      }
417
      /* still running */
418
0
      ++ongoing;
419
0
      panchor = &(*panchor)->next;
420
0
    }
421
0
    else if(a->inconclusive) { /* failed, but inconclusive */
422
0
      ++inconclusive;
423
0
      panchor = &(*panchor)->next;
424
0
    }
425
0
    else {
426
      /* hard failure, discard right away */
427
0
      *panchor = a->next;
428
0
      if(!a->next)
429
0
        bs->running_tail = panchor;
430
0
      a->next = NULL;
431
0
      bs->last_dead_result = a->result;
432
0
      bs->had_dead = TRUE;
433
0
      cf_ip_attempt_free(a, data);
434
0
    }
435
0
  }
436
0
  if(bs->running)
437
0
    CURL_TRC_CF(data, cf, "checked connect attempts: "
438
0
                "%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
0
  pnow = Curl_pgrs_now(data);
444
0
  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
0
  if(!ongoing) {
452
0
    do_more = TRUE;
453
0
  }
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
0
  if(do_more) {
469
    /* start the next attempt if there is another ip address to try.
470
     * Alternate between address families when possible. */
471
0
    const struct Curl_addrinfo *ai = NULL;
472
0
    int ai_family = 0;
473
0
    CURL_TRC_CF(data, cf, "want to do more");
474
0
#ifdef USE_IPV6
475
0
    if((bs->last_attempt_ai_family == AF_INET) ||
476
0
       !cf_ai_iter_has_more(&bs->addr_iter, data)) {
477
0
      ai = cf_ai_iter_next(&bs->ipv6_iter, data);
478
0
      ai_family = bs->ipv6_iter.ai_family;
479
0
      CURL_TRC_CF(data, cf, "check for next AAAA address: %s",
480
0
                  ai ? "found" : "none");
481
0
    }
482
0
#endif
483
0
    if(!ai) {
484
0
      ai = cf_ai_iter_next(&bs->addr_iter, data);
485
0
      ai_family = bs->addr_iter.ai_family;
486
0
      CURL_TRC_CF(data, cf, "check for next A address: %s",
487
0
                  ai ? "found" : "none");
488
0
    }
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
0
    Curl_reset_fail(data);
493
494
0
    if(ai) {  /* try another address */
495
0
      struct Curl_sockaddr_ex addr;
496
497
      /* Discard oldest to make room for new attempt */
498
0
      if(bs->max_concurrent)
499
0
        cf_ip_ballers_prune(bs, cf, data, bs->max_concurrent - 1);
500
501
0
      result = Curl_socket_addr_from_ai(&addr, ai, bs->transport_peer);
502
0
      if(result)
503
0
        goto out;
504
505
0
      result = cf_ip_attempt_new(&a, data, cf, bs->origin, bs->peer,
506
0
                                 bs->transport_peer, &addr, ai_family,
507
0
                                 bs->tunnel_peer, bs->tunnel_transport,
508
0
                                 bs->cf_create);
509
0
      CURL_TRC_CF(data, cf, "starting %s attempt for ipv%s -> %d",
510
0
                  bs->running ? "next" : "first",
511
0
                  (ai_family == AF_INET) ? "4" : "6", (int)result);
512
0
      if(result)
513
0
        goto out;
514
0
      DEBUGASSERT(a);
515
516
      /* append to running list */
517
0
      *bs->running_tail = a;
518
0
      a->next = NULL;
519
0
      bs->running_tail = &a->next;
520
0
      bs->last_attempt_started = *pnow;
521
0
      bs->last_attempt_ai_family = ai_family;
522
      /* and run everything again */
523
0
      goto evaluate;
524
0
    }
525
0
    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
0
    else if(!ongoing && dns_resolved) {
557
      /* no more addresses, no inconclusive attempts */
558
0
      CURL_TRC_CF(data, cf, "no more attempts to try");
559
0
      DEBUGASSERT(!bs->running);
560
0
      result = bs->had_dead ? bs->last_dead_result : CURLE_COULDNT_CONNECT;
561
0
    }
562
0
  }
563
564
0
out:
565
0
  if(!result) {
566
0
    bool more_possible;
567
568
    /* when do we need to be called again? */
569
0
    pnow = Curl_pgrs_now(data);
570
0
    next_expire_ms = Curl_timeleft_now_ms(data, pnow);
571
0
    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
0
    more_possible = cf_ai_iter_has_more(&bs->addr_iter, data);
578
0
#ifdef USE_IPV6
579
0
    if(!more_possible)
580
0
      more_possible = cf_ai_iter_has_more(&bs->ipv6_iter, data);
581
0
#endif
582
0
    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
0
  }
597
0
  return result;
598
0
}
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
0
{
626
0
  struct cf_ip_attempt *a;
627
0
  CURLcode result = CURLE_OK;
628
0
  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
0
  return result;
634
0
}
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
0
{
685
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
686
0
  struct connectdata *conn = CF_CONN(cf);
687
0
  CURLcode result;
688
689
0
  result = cf_ip_ballers_run(&ctx->ballers, cf, data,
690
0
                             (bool)ctx->dns_resolved, connected);
691
692
0
  if(!result)
693
0
    return CURLE_OK;
694
0
  else {
695
0
    struct Curl_peer *peer = NULL, *proxy_peer = NULL;
696
0
    char viamsg[160];
697
698
0
    peer = Curl_cf_get_first_peer(cf);
699
0
    if(!conn->origin || !peer)
700
0
      return CURLE_FAILED_INIT;
701
702
0
#ifndef CURL_DISABLE_PROXY
703
0
    if(conn->socks_proxy.peer)
704
0
      proxy_peer = conn->socks_proxy.peer;
705
0
    else if(conn->http_proxy.peer)
706
0
      proxy_peer = conn->http_proxy.peer;
707
0
#endif
708
709
0
    viamsg[0] = 0;
710
0
    if(!Curl_peer_equal(peer, conn->origin) &&
711
0
       !Curl_peer_equal(peer, proxy_peer)) {
712
0
#ifdef USE_UNIX_SOCKETS
713
0
      if(peer->unix_socket)
714
0
        curl_msnprintf(viamsg, sizeof(viamsg), " over unix://%s",
715
0
                       peer->hostname);
716
0
      else
717
0
#endif
718
0
      curl_msnprintf(viamsg, sizeof(viamsg), " via %s:%u",
719
0
                     peer->hostname, peer->port);
720
0
    }
721
722
0
    failf(data, "Failed to connect to %s:%u%s %s%s%safter "
723
0
          "%" FMT_TIMEDIFF_T " ms: %s",
724
0
          conn->origin->hostname, conn->origin->port, viamsg,
725
0
          proxy_peer ? "over proxy " : "",
726
0
          proxy_peer ? proxy_peer->hostname : "",
727
0
          proxy_peer ? " " : "",
728
0
          Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE),
729
0
          curl_easy_strerror(result));
730
731
0
#ifdef SOCKETIMEDOUT
732
0
    if(SOCKETIMEDOUT == data->state.os_errno)
733
0
      result = CURLE_OPERATION_TIMEDOUT;
734
0
#endif
735
736
0
    return result;
737
0
  }
738
0
}
739
740
0
#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
0
{
745
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
746
747
0
  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
0
  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
0
  else { /* TCP/UDP/QUIC */
761
0
#ifdef USE_IPV6
762
0
    cf_ai_iter_init(&ctx->ballers.ipv6_iter, cf, ctx->ballers.peer, AF_INET6);
763
0
#endif
764
0
    cf_ai_iter_init(&ctx->ballers.addr_iter, cf, ctx->ballers.peer, AF_INET);
765
0
  }
766
767
0
  CURL_TRC_CF(data, cf, "init ip ballers for transport %u",
768
0
              ctx->ballers.transport_peer);
769
0
  return CURLE_OK;
770
0
}
771
772
static void cf_ip_happy_ctx_clear(struct cf_ip_happy_ctx *ctx,
773
                                  struct Curl_easy *data)
774
0
{
775
0
  DEBUGASSERT(ctx);
776
0
  if(ctx)
777
0
    cf_ip_ballers_clear(data, &ctx->ballers);
778
0
}
779
780
static void cf_ip_happy_ctx_destroy(struct cf_ip_happy_ctx *ctx,
781
                                    struct Curl_easy *data)
782
0
{
783
0
  if(ctx) {
784
0
    cf_ip_happy_ctx_clear(ctx, data);
785
0
    curlx_free(ctx);
786
0
  }
787
0
}
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
0
{
811
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
812
0
  CURLcode result = CURLE_OK;
813
814
0
  if(!cf->connected) {
815
0
    result = cf_ip_ballers_pollset(&ctx->ballers, data, ps);
816
0
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %u socks", (int)result,
817
0
                ps->n);
818
0
  }
819
0
  return result;
820
0
}
821
822
static CURLcode cf_ip_happy_connect(struct Curl_cfilter *cf,
823
                                    struct Curl_easy *data,
824
                                    bool *done)
825
0
{
826
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
827
0
  CURLcode result = CURLE_OK;
828
829
  /* -Werror=null-dereference finds false positives suddenly. */
830
0
  if(!data)
831
0
    return CURLE_FAILED_INIT;
832
833
0
  if(cf->connected) {
834
0
    *done = TRUE;
835
0
    return CURLE_OK;
836
0
  }
837
838
0
  DEBUGASSERT(ctx);
839
0
  *done = FALSE;
840
841
0
  if(!ctx->dns_resolved) {
842
0
    result = Curl_dns_cf_addr_result(cf, ctx->ballers.peer);
843
0
    if(!result)
844
0
      ctx->dns_resolved = TRUE;
845
0
    else if(result == CURLE_AGAIN) {
846
0
      result = CURLE_OK;
847
0
    }
848
0
    else /* real error */
849
0
      goto out;
850
0
  }
851
852
0
  switch(ctx->state) {
853
0
  case SCFST_INIT:
854
0
    DEBUGASSERT(CURL_SOCKET_BAD == cf->chain->sock);
855
0
    DEBUGASSERT(!cf->connected);
856
0
    result = cf_ip_happy_init(cf, data);
857
0
    if(result)
858
0
      goto out;
859
0
    ctx->state = SCFST_WAITING;
860
0
    FALLTHROUGH();
861
0
  case SCFST_WAITING:
862
0
    result = is_connected(cf, data, done);
863
0
    if(!result && *done) {
864
0
      DEBUGASSERT(ctx->ballers.winner);
865
0
      DEBUGASSERT(ctx->ballers.winner->chain.cf);
866
0
      DEBUGASSERT(ctx->ballers.winner->chain.cf->connected);
867
      /* we have a winner. Install and activate it.
868
       * close/free all others. */
869
0
      ctx->state = SCFST_DONE;
870
0
      cf->connected = TRUE;
871
0
      Curl_cfchain_merge(cf->chain, &ctx->ballers.winner->chain);
872
0
      VERBOSE(Curl_cfchain_trc(data, cf, cf->chain, "merged winner"));
873
874
0
      if(CF_CONN(cf)->scheme->protocol & PROTO_FAMILY_SSH)
875
0
        Curl_pgrsTime(data, TIMER_APPCONNECT); /* we are connected already */
876
0
#ifdef CURLVERBOSE
877
0
      if(Curl_trc_cf_is_verbose(cf, data)) {
878
0
        struct ip_quadruple ipquad;
879
0
        bool is_ipv6;
880
0
        if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) {
881
0
          CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u",
882
0
                      ctx->ballers.peer->hostname,
883
0
                      ipquad.remote_ip, ipquad.remote_port);
884
0
        }
885
0
      }
886
0
#endif
887
0
      cf_ip_happy_ctx_clear(ctx, data);
888
0
      Curl_expire_clear(data, EXPIRE_HAPPY_EYEBALLS);
889
      /* whatever errors were reported by ballers, clear our errorbuf */
890
0
      Curl_reset_fail(data);
891
0
      data->info.numconnects++; /* to track the # of connections made */
892
0
    }
893
0
    break;
894
0
  case SCFST_DONE:
895
0
    *done = TRUE;
896
0
    break;
897
0
  }
898
0
out:
899
0
  return result;
900
0
}
901
902
static bool cf_ip_happy_data_pending(struct Curl_cfilter *cf,
903
                                     const struct Curl_easy *data)
904
0
{
905
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
906
907
0
  if(!cf->connected) {
908
0
    return cf_ip_ballers_pending(&ctx->ballers, data);
909
0
  }
910
0
  return cf->next->cft->has_data_pending(cf->next, data);
911
0
}
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
0
{
917
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
918
919
0
  if(!cf->connected) {
920
0
    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
0
    default:
927
0
      break;
928
0
    }
929
0
  }
930
931
0
  return cf->next ?
932
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
933
0
    CURLE_UNKNOWN_OPTION;
934
0
}
935
936
static void cf_ip_happy_destroy(struct Curl_cfilter *cf,
937
                                struct Curl_easy *data)
938
0
{
939
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
940
941
0
  CURL_TRC_CF(data, cf, "destroy");
942
0
  if(ctx) {
943
0
    cf_ip_happy_ctx_clear(ctx, data);
944
0
    cf_ip_happy_ctx_destroy(ctx, data);
945
0
  }
946
0
}
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
0
{
974
0
  struct cf_ip_happy_ctx *ctx = NULL;
975
0
  CURLcode result;
976
977
0
  (void)data;
978
0
  (void)conn;
979
0
  *pcf = NULL;
980
0
  ctx = curlx_calloc(1, sizeof(*ctx));
981
0
  if(!ctx) {
982
0
    result = CURLE_OUT_OF_MEMORY;
983
0
    goto out;
984
0
  }
985
0
  result = cf_ip_ballers_init(&ctx->ballers, data,
986
0
                              origin, peer, transport_peer,
987
0
                              tunnel_peer, tunnel_transport,
988
0
                              data->set.happy_eyeballs_timeout,
989
0
                              IP_HE_MAX_CONCURRENT_ATTEMPTS);
990
0
  if(result)
991
0
    goto out;
992
993
0
  result = Curl_cf_create(pcf, &Curl_cft_ip_happy, ctx);
994
995
0
out:
996
0
  if(result) {
997
0
    curlx_safefree(*pcf);
998
0
    cf_ip_happy_ctx_destroy(ctx, data);
999
0
  }
1000
0
  return result;
1001
0
}
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
0
{
1011
0
  struct Curl_cfilter *cf;
1012
0
  CURLcode result;
1013
1014
  /* Need to be first */
1015
0
  DEBUGASSERT(cf_at);
1016
0
  result = cf_ip_happy_create(&cf, data, origin, peer, transport_peer,
1017
0
                              CF_CONN(cf_at), tunnel_peer, tunnel_transport);
1018
0
  if(result)
1019
0
    return result;
1020
1021
0
  Curl_conn_cf_insert_after(cf_at, cf);
1022
0
  return CURLE_OK;
1023
0
}