Coverage Report

Created: 2026-09-04 07:15

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