Coverage Report

Created: 2026-08-13 07:18

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
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
            Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE));
572
0
      return CURLE_OPERATION_TIMEDOUT;
573
0
    }
574
575
0
    more_possible = cf_ai_iter_has_more(&bs->addr_iter, data);
576
0
#ifdef USE_IPV6
577
0
    if(!more_possible)
578
0
      more_possible = cf_ai_iter_has_more(&bs->ipv6_iter, data);
579
0
#endif
580
0
    if(more_possible) {
581
0
      timediff_t expire_ms, elapsed_ms;
582
0
      elapsed_ms =
583
0
        curlx_ptimediff_ms(Curl_pgrs_now(data), &bs->last_attempt_started);
584
0
      expire_ms = CURLMAX(bs->attempt_delay_ms - elapsed_ms, 0);
585
0
      next_expire_ms = CURLMIN(next_expire_ms, expire_ms);
586
0
      if(next_expire_ms <= 0) {
587
0
        CURL_TRC_CF(data, cf, "HAPPY_EYEBALLS timeout due, re-evaluate");
588
0
        goto evaluate;
589
0
      }
590
0
      CURL_TRC_CF(data, cf, "next HAPPY_EYEBALLS timeout in %" FMT_TIMEDIFF_T
591
0
                  "ms", next_expire_ms);
592
0
      Curl_expire(data, next_expire_ms, EXPIRE_HAPPY_EYEBALLS);
593
0
    }
594
0
  }
595
0
  return result;
596
0
}
597
598
static CURLcode cf_ip_ballers_shutdown(struct cf_ip_ballers *bs,
599
                                       struct Curl_easy *data,
600
                                       bool *done)
601
0
{
602
0
  struct cf_ip_attempt *a;
603
604
  /* shutdown all ballers that have not done so already. If one fails,
605
   * continue shutting down others until all are shutdown. */
606
0
  *done = TRUE;
607
0
  for(a = bs->running; a; a = a->next) {
608
0
    bool bdone = FALSE;
609
0
    if(a->shutdown || !a->cf)
610
0
      continue;
611
0
    a->result = a->cf->cft->do_shutdown(a->cf, data, &bdone);
612
0
    if(a->result || bdone)
613
0
      a->shutdown = TRUE; /* treat a failed shutdown as done */
614
0
    else
615
0
      *done = FALSE;
616
0
  }
617
0
  return CURLE_OK;
618
0
}
619
620
static CURLcode cf_ip_ballers_pollset(struct cf_ip_ballers *bs,
621
                                      struct Curl_easy *data,
622
                                      struct easy_pollset *ps)
623
0
{
624
0
  struct cf_ip_attempt *a;
625
0
  CURLcode result = CURLE_OK;
626
0
  for(a = bs->running; a && !result; a = a->next) {
627
0
    if(a->result)
628
0
      continue;
629
0
    result = Curl_conn_cf_adjust_pollset(a->cf, data, ps);
630
0
  }
631
0
  return result;
632
0
}
633
634
static bool cf_ip_ballers_pending(struct cf_ip_ballers *bs,
635
                                  const struct Curl_easy *data)
636
0
{
637
0
  struct cf_ip_attempt *a;
638
639
0
  for(a = bs->running; a; a = a->next) {
640
0
    if(a->result)
641
0
      continue;
642
0
    if(a->cf && a->cf->cft->has_data_pending(a->cf, data))
643
0
      return TRUE;
644
0
  }
645
0
  return FALSE;
646
0
}
647
648
static struct curltime cf_ip_ballers_max_time(struct cf_ip_ballers *bs,
649
                                              struct Curl_easy *data,
650
                                              int query)
651
0
{
652
0
  struct curltime t, tmax;
653
0
  struct cf_ip_attempt *a;
654
655
0
  memset(&tmax, 0, sizeof(tmax));
656
0
  for(a = bs->running; a; a = a->next) {
657
0
    memset(&t, 0, sizeof(t));
658
0
    if(a->cf && !a->cf->cft->query(a->cf, data, query, NULL, &t)) {
659
0
      if((t.tv_sec || t.tv_usec) && curlx_ptimediff_us(&t, &tmax) > 0)
660
0
        tmax = t;
661
0
    }
662
0
  }
663
0
  return tmax;
664
0
}
665
666
static int cf_ip_ballers_min_reply_ms(struct cf_ip_ballers *bs,
667
                                      struct Curl_easy *data)
668
0
{
669
0
  int reply_ms = -1, breply_ms;
670
0
  struct cf_ip_attempt *a;
671
672
0
  for(a = bs->running; a; a = a->next) {
673
0
    if(a->cf && !a->cf->cft->query(a->cf, data, CF_QUERY_CONNECT_REPLY_MS,
674
0
                                   &breply_ms, NULL)) {
675
0
      if(breply_ms >= 0 && (reply_ms < 0 || breply_ms < reply_ms))
676
0
        reply_ms = breply_ms;
677
0
    }
678
0
  }
679
0
  return reply_ms;
680
0
}
681
682
typedef enum {
683
  SCFST_INIT,
684
  SCFST_WAITING,
685
  SCFST_DONE
686
} cf_connect_state;
687
688
struct cf_ip_happy_ctx {
689
  cf_ip_connect_create *cf_create;
690
  cf_connect_state state;
691
  struct cf_ip_ballers ballers;
692
  struct curltime started;
693
  BIT(dns_resolved);
694
};
695
696
static CURLcode is_connected(struct Curl_cfilter *cf,
697
                             struct Curl_easy *data,
698
                             bool *connected)
699
0
{
700
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
701
0
  struct connectdata *conn = cf->conn;
702
0
  CURLcode result;
703
704
0
  result = cf_ip_ballers_run(&ctx->ballers, cf, data,
705
0
                             (bool)ctx->dns_resolved, connected);
706
707
0
  if(!result)
708
0
    return CURLE_OK;
709
0
  else {
710
0
    struct Curl_peer *peer = NULL, *proxy_peer = NULL;
711
0
    char viamsg[160];
712
713
0
    peer = Curl_conn_get_first_peer(conn, cf->sockindex);
714
0
    if(!conn->origin || !peer)
715
0
      return CURLE_FAILED_INIT;
716
717
0
#ifndef CURL_DISABLE_PROXY
718
0
    if(conn->socks_proxy.peer)
719
0
      proxy_peer = conn->socks_proxy.peer;
720
0
    else if(conn->http_proxy.peer)
721
0
      proxy_peer = conn->http_proxy.peer;
722
0
#endif
723
724
0
    viamsg[0] = 0;
725
0
    if(!Curl_peer_equal(peer, conn->origin) &&
726
0
       !Curl_peer_equal(peer, proxy_peer)) {
727
0
#ifdef USE_UNIX_SOCKETS
728
0
      if(peer->unix_socket)
729
0
        curl_msnprintf(viamsg, sizeof(viamsg), " over unix://%s",
730
0
                       peer->hostname);
731
0
      else
732
0
#endif
733
0
      curl_msnprintf(viamsg, sizeof(viamsg), " via %s:%u",
734
0
                     peer->hostname, peer->port);
735
0
    }
736
737
0
    failf(data, "Failed to connect to %s:%u%s %s%s%safter "
738
0
          "%" FMT_TIMEDIFF_T " ms: %s",
739
0
          conn->origin->hostname, conn->origin->port, viamsg,
740
0
          proxy_peer ? "over proxy " : "",
741
0
          proxy_peer ? proxy_peer->hostname : "",
742
0
          proxy_peer ? " " : "",
743
0
          Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE),
744
0
          curl_easy_strerror(result));
745
746
0
#ifdef SOCKETIMEDOUT
747
0
    if(SOCKETIMEDOUT == data->state.os_errno)
748
0
      result = CURLE_OPERATION_TIMEDOUT;
749
0
#endif
750
751
0
    return result;
752
0
  }
753
0
}
754
755
0
#define IP_HE_MAX_CONCURRENT_ATTEMPTS     6
756
757
static CURLcode cf_ip_happy_init(struct Curl_cfilter *cf,
758
                                 struct Curl_easy *data)
759
0
{
760
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
761
762
0
  if(Curl_timeleft_ms(data) < 0) {
763
    /* a precaution, no need to continue if time already is up */
764
0
    failf(data, "Connection time-out");
765
0
    return CURLE_OPERATION_TIMEDOUT;
766
0
  }
767
768
0
  if(ctx->ballers.transport_peer == TRNSPRT_UNIX) {
769
0
#ifdef USE_UNIX_SOCKETS
770
0
    cf_ai_iter_init(&ctx->ballers.addr_iter, cf, ctx->ballers.peer, AF_UNIX);
771
#else
772
    return CURLE_UNSUPPORTED_PROTOCOL;
773
#endif
774
0
  }
775
0
  else { /* TCP/UDP/QUIC */
776
0
#ifdef USE_IPV6
777
0
    cf_ai_iter_init(&ctx->ballers.ipv6_iter, cf, ctx->ballers.peer, AF_INET6);
778
0
#endif
779
0
    cf_ai_iter_init(&ctx->ballers.addr_iter, cf, ctx->ballers.peer, AF_INET);
780
0
  }
781
782
0
  CURL_TRC_CF(data, cf, "init ip ballers for transport %u",
783
0
              ctx->ballers.transport_peer);
784
0
  ctx->started = *Curl_pgrs_now(data);
785
0
  return CURLE_OK;
786
0
}
787
788
static void cf_ip_happy_ctx_clear(struct cf_ip_happy_ctx *ctx,
789
                                  struct Curl_easy *data)
790
0
{
791
0
  DEBUGASSERT(ctx);
792
0
  if(ctx)
793
0
    cf_ip_ballers_clear(data, &ctx->ballers);
794
0
}
795
796
static void cf_ip_happy_ctx_destroy(struct cf_ip_happy_ctx *ctx,
797
                                    struct Curl_easy *data)
798
0
{
799
0
  if(ctx) {
800
0
    cf_ip_happy_ctx_clear(ctx, data);
801
0
    curlx_free(ctx);
802
0
  }
803
0
}
804
805
static CURLcode cf_ip_happy_shutdown(struct Curl_cfilter *cf,
806
                                     struct Curl_easy *data,
807
                                     bool *done)
808
0
{
809
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
810
0
  CURLcode result = CURLE_OK;
811
812
0
  DEBUGASSERT(data);
813
0
  if(cf->connected) {
814
0
    *done = TRUE;
815
0
    return CURLE_OK;
816
0
  }
817
818
0
  result = cf_ip_ballers_shutdown(&ctx->ballers, data, done);
819
0
  CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", (int)result, *done);
820
0
  return result;
821
0
}
822
823
static CURLcode cf_ip_happy_adjust_pollset(struct Curl_cfilter *cf,
824
                                           struct Curl_easy *data,
825
                                           struct easy_pollset *ps)
826
0
{
827
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
828
0
  CURLcode result = CURLE_OK;
829
830
0
  if(!cf->connected) {
831
0
    result = cf_ip_ballers_pollset(&ctx->ballers, data, ps);
832
0
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %u socks", (int)result,
833
0
                ps->n);
834
0
  }
835
0
  return result;
836
0
}
837
838
static CURLcode cf_ip_happy_connect(struct Curl_cfilter *cf,
839
                                    struct Curl_easy *data,
840
                                    bool *done)
841
0
{
842
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
843
0
  CURLcode result = CURLE_OK;
844
845
  /* -Werror=null-dereference finds false positives suddenly. */
846
0
  if(!data)
847
0
    return CURLE_FAILED_INIT;
848
849
0
  if(cf->connected) {
850
0
    *done = TRUE;
851
0
    return CURLE_OK;
852
0
  }
853
854
0
  DEBUGASSERT(ctx);
855
0
  *done = FALSE;
856
857
0
  if(!ctx->dns_resolved) {
858
0
    result = Curl_conn_dns_addr_result(cf->conn, cf->sockindex,
859
0
                                       ctx->ballers.peer);
860
0
    if(!result)
861
0
      ctx->dns_resolved = TRUE;
862
0
    else if(result == CURLE_AGAIN) {
863
0
      result = CURLE_OK;
864
0
    }
865
0
    else /* real error */
866
0
      goto out;
867
0
  }
868
869
0
  switch(ctx->state) {
870
0
  case SCFST_INIT:
871
0
    DEBUGASSERT(CURL_SOCKET_BAD == Curl_conn_cf_get_socket(cf, data));
872
0
    DEBUGASSERT(!cf->connected);
873
0
    result = cf_ip_happy_init(cf, data);
874
0
    if(result)
875
0
      goto out;
876
0
    ctx->state = SCFST_WAITING;
877
0
    FALLTHROUGH();
878
0
  case SCFST_WAITING:
879
0
    result = is_connected(cf, data, done);
880
0
    if(!result && *done) {
881
0
      DEBUGASSERT(ctx->ballers.winner);
882
0
      DEBUGASSERT(ctx->ballers.winner->cf);
883
0
      DEBUGASSERT(ctx->ballers.winner->cf->connected);
884
      /* we have a winner. Install and activate it.
885
       * close/free all others. */
886
0
      ctx->state = SCFST_DONE;
887
0
      cf->connected = TRUE;
888
0
      cf->next = ctx->ballers.winner->cf;
889
0
      ctx->ballers.winner->cf = NULL;
890
891
0
      if(cf->conn->scheme->protocol & PROTO_FAMILY_SSH)
892
0
        Curl_pgrsTime(data, TIMER_APPCONNECT); /* we are connected already */
893
0
#ifdef CURLVERBOSE
894
0
      if(Curl_trc_cf_is_verbose(cf, data)) {
895
0
        struct ip_quadruple ipquad;
896
0
        bool is_ipv6;
897
0
        if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) {
898
0
          CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u",
899
0
                      ctx->ballers.peer->hostname,
900
0
                      ipquad.remote_ip, ipquad.remote_port);
901
0
        }
902
0
      }
903
0
#endif
904
0
      cf_ip_happy_ctx_clear(ctx, data);
905
0
      Curl_expire_done(data, EXPIRE_HAPPY_EYEBALLS);
906
      /* whatever errors were reported by ballers, clear our errorbuf */
907
0
      Curl_reset_fail(data);
908
0
      data->info.numconnects++; /* to track the # of connections made */
909
0
    }
910
0
    break;
911
0
  case SCFST_DONE:
912
0
    *done = TRUE;
913
0
    break;
914
0
  }
915
0
out:
916
0
  return result;
917
0
}
918
919
static bool cf_ip_happy_data_pending(struct Curl_cfilter *cf,
920
                                     const struct Curl_easy *data)
921
0
{
922
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
923
924
0
  if(!cf->connected) {
925
0
    return cf_ip_ballers_pending(&ctx->ballers, data);
926
0
  }
927
0
  return cf->next->cft->has_data_pending(cf->next, data);
928
0
}
929
930
static CURLcode cf_ip_happy_query(struct Curl_cfilter *cf,
931
                                  struct Curl_easy *data,
932
                                  int query, int *pres1, void *pres2)
933
0
{
934
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
935
936
0
  if(!cf->connected) {
937
0
    switch(query) {
938
0
    case CF_QUERY_CONNECT_REPLY_MS: {
939
0
      *pres1 = cf_ip_ballers_min_reply_ms(&ctx->ballers, data);
940
0
      CURL_TRC_CF(data, cf, "query connect reply: %dms", *pres1);
941
0
      return CURLE_OK;
942
0
    }
943
0
    case CF_QUERY_TIMER_CONNECT: {
944
0
      struct curltime *when = pres2;
945
0
      *when = cf_ip_ballers_max_time(&ctx->ballers, data,
946
0
                                     CF_QUERY_TIMER_CONNECT);
947
0
      return CURLE_OK;
948
0
    }
949
0
    case CF_QUERY_TIMER_APPCONNECT: {
950
0
      struct curltime *when = pres2;
951
0
      *when = cf_ip_ballers_max_time(&ctx->ballers, data,
952
0
                                     CF_QUERY_TIMER_APPCONNECT);
953
0
      return CURLE_OK;
954
0
    }
955
0
    default:
956
0
      break;
957
0
    }
958
0
  }
959
960
0
  return cf->next ?
961
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
962
0
    CURLE_UNKNOWN_OPTION;
963
0
}
964
965
static void cf_ip_happy_destroy(struct Curl_cfilter *cf,
966
                                struct Curl_easy *data)
967
0
{
968
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
969
970
0
  CURL_TRC_CF(data, cf, "destroy");
971
0
  if(ctx) {
972
0
    cf_ip_happy_ctx_clear(ctx, data);
973
0
    cf_ip_happy_ctx_destroy(ctx, data);
974
0
  }
975
0
}
976
977
struct Curl_cftype Curl_cft_ip_happy = {
978
  "HAPPY-EYEBALLS",
979
  CF_TYPE_SETUP,
980
  CURL_LOG_LVL_NONE,
981
  cf_ip_happy_destroy,
982
  cf_ip_happy_connect,
983
  cf_ip_happy_shutdown,
984
  cf_ip_happy_adjust_pollset,
985
  cf_ip_happy_data_pending,
986
  Curl_cf_def_send,
987
  Curl_cf_def_recv,
988
  Curl_cf_def_cntrl,
989
  Curl_cf_def_conn_is_alive,
990
  Curl_cf_def_conn_keep_alive,
991
  cf_ip_happy_query,
992
};
993
994
static CURLcode cf_ip_happy_create(struct Curl_cfilter **pcf,
995
                                   struct Curl_easy *data,
996
                                   struct Curl_peer *origin,
997
                                   struct Curl_peer *peer,
998
                                   uint8_t transport_peer,
999
                                   struct connectdata *conn,
1000
                                   struct Curl_peer *tunnel_peer,
1001
                                   uint8_t tunnel_transport)
1002
0
{
1003
0
  struct cf_ip_happy_ctx *ctx = NULL;
1004
0
  CURLcode result;
1005
1006
0
  (void)data;
1007
0
  (void)conn;
1008
0
  *pcf = NULL;
1009
0
  ctx = curlx_calloc(1, sizeof(*ctx));
1010
0
  if(!ctx) {
1011
0
    result = CURLE_OUT_OF_MEMORY;
1012
0
    goto out;
1013
0
  }
1014
0
  result = cf_ip_ballers_init(&ctx->ballers, data,
1015
0
                              origin, peer, transport_peer,
1016
0
                              tunnel_peer, tunnel_transport,
1017
0
                              data->set.happy_eyeballs_timeout,
1018
0
                              IP_HE_MAX_CONCURRENT_ATTEMPTS);
1019
0
  if(result)
1020
0
    goto out;
1021
1022
0
  result = Curl_cf_create(pcf, &Curl_cft_ip_happy, ctx);
1023
1024
0
out:
1025
0
  if(result) {
1026
0
    curlx_safefree(*pcf);
1027
0
    cf_ip_happy_ctx_destroy(ctx, data);
1028
0
  }
1029
0
  return result;
1030
0
}
1031
1032
CURLcode Curl_cf_ip_happy_insert_after(struct Curl_cfilter *cf_at,
1033
                                       struct Curl_easy *data,
1034
                                       struct Curl_peer *origin,
1035
                                       struct Curl_peer *peer,
1036
                                       uint8_t transport_peer,
1037
                                       struct Curl_peer *tunnel_peer,
1038
                                       uint8_t tunnel_transport)
1039
0
{
1040
0
  struct Curl_cfilter *cf;
1041
0
  CURLcode result;
1042
1043
  /* Need to be first */
1044
0
  DEBUGASSERT(cf_at);
1045
0
  result = cf_ip_happy_create(&cf, data, origin, peer, transport_peer,
1046
0
                              cf_at->conn, tunnel_peer, tunnel_transport);
1047
0
  if(result)
1048
0
    return result;
1049
1050
0
  Curl_conn_cf_insert_after(cf_at, cf);
1051
0
  return CURLE_OK;
1052
0
}