Coverage Report

Created: 2026-07-16 06:10

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