Coverage Report

Created: 2026-09-14 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/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
{
110
  size_t i;
111
  for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) {
112
    if(transport == transport_providers[i].transport) {
113
      transport_providers[i].cf_create = cf_create;
114
    }
115
  }
116
}
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
  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
0
{
182
0
  if(a) {
183
0
    if(a->cf)
184
0
      Curl_conn_cf_discard_chain(&a->cf, data);
185
0
    Curl_peer_unlink(&a->origin);
186
0
    Curl_peer_unlink(&a->peer);
187
0
    Curl_peer_unlink(&a->tunnel_peer);
188
0
    curlx_free(a);
189
0
  }
190
0
}
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
0
{
204
0
  struct Curl_cfilter *wcf;
205
0
  struct cf_ip_attempt *a;
206
0
  CURLcode result = CURLE_OK;
207
208
0
  *pa = NULL;
209
0
  a = curlx_calloc(1, sizeof(*a));
210
0
  if(!a)
211
0
    return CURLE_OUT_OF_MEMORY;
212
213
0
  Curl_peer_link(&a->origin, origin);
214
0
  Curl_peer_link(&a->peer, peer);
215
0
  a->transport_peer = transport_peer;
216
0
  Curl_peer_link(&a->tunnel_peer, tunnel_peer);
217
0
  a->tunnel_transport = tunnel_transport;
218
0
  a->addr = *addr;
219
0
  a->ai_family = ai_family;
220
0
  a->result = CURLE_OK;
221
0
  a->cf_create = cf_create;
222
0
  *pa = a;
223
224
0
  result = a->cf_create(&a->cf, data, a->origin, a->peer, a->transport_peer,
225
0
                        cf->conn, &a->addr, a->tunnel_peer,
226
0
                        a->tunnel_transport);
227
0
  if(result)
228
0
    goto out;
229
230
  /* the new filter might have sub-filters */
231
0
  for(wcf = a->cf; wcf; wcf = wcf->next) {
232
0
    wcf->conn = cf->conn;
233
0
    wcf->sockindex = cf->sockindex;
234
0
  }
235
236
0
out:
237
0
  if(result) {
238
0
    cf_ip_attempt_free(a, data);
239
0
    *pa = NULL;
240
0
  }
241
0
  return result;
242
0
}
243
244
static CURLcode cf_ip_attempt_connect(struct cf_ip_attempt *a,
245
                                      struct Curl_easy *data,
246
                                      bool *connected)
247
0
{
248
0
  *connected = (bool)a->connected;
249
0
  if(!a->result && !*connected) {
250
    /* evaluate again */
251
0
    a->result = Curl_conn_cf_connect(a->cf, data, connected);
252
253
0
    if(!a->result) {
254
0
      if(*connected) {
255
0
        a->connected = TRUE;
256
0
      }
257
0
    }
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
0
  }
265
0
  return a->result;
266
0
}
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
0
{
320
0
  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
0
  cf_ip_attempt_free(bs->winner, data);
326
0
  bs->winner = NULL;
327
0
  Curl_peer_unlink(&bs->origin);
328
0
  Curl_peer_unlink(&bs->peer);
329
0
  Curl_peer_unlink(&bs->tunnel_peer);
330
0
}
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
0
{
342
0
  memset(bs, 0, sizeof(*bs));
343
0
  bs->cf_create = get_cf_create(transport_peer, !!tunnel_peer);
344
0
  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
0
  Curl_peer_link(&bs->origin, origin);
350
0
  Curl_peer_link(&bs->peer, peer);
351
0
  bs->transport_peer = transport_peer;
352
0
  Curl_peer_link(&bs->tunnel_peer, tunnel_peer);
353
0
  bs->tunnel_transport = tunnel_transport;
354
0
  bs->attempt_delay_ms = attempt_delay_ms;
355
0
  bs->max_concurrent = max_concurrent;
356
0
  bs->last_attempt_ai_family = AF_INET; /* so AF_INET6 is next */
357
0
  return CURLE_OK;
358
0
}
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
0
{
365
0
  struct cf_ip_attempt *a = NULL, **panchor;
366
0
  uint32_t ongoing = 0;
367
368
0
  for(a = bs->running; a; a = a->next) {
369
0
    if(!a->result && !a->connected)
370
0
      ++ongoing;
371
0
  }
372
373
0
  panchor = &bs->running;
374
0
  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
0
}
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
0
{
395
0
  CURLcode result = CURLE_OK;
396
0
  struct cf_ip_attempt *a = NULL, **panchor;
397
0
  bool do_more;
398
0
  timediff_t next_expire_ms;
399
0
  uint32_t inconclusive, ongoing;
400
0
  const struct curltime *pnow = NULL;
401
0
  VERBOSE(int i);
402
403
0
  if(bs->winner)
404
0
    return CURLE_OK;
405
406
0
evaluate:
407
0
  ongoing = inconclusive = 0;
408
409
  /* check if a running baller connects now */
410
0
  VERBOSE(i = -1);
411
0
  for(panchor = &bs->running; *panchor; panchor = &(*panchor)->next) {
412
0
    VERBOSE(++i);
413
0
    a = *panchor;
414
0
    a->result = cf_ip_attempt_connect(a, data, connected);
415
0
    if(!a->result) {
416
0
      if(*connected) {
417
        /* connected, declare the winner, remove from running,
418
         * clear remaining running list. */
419
0
        CURL_TRC_CF(data, cf, "connect attempt #%d successful", i);
420
0
        bs->winner = a;
421
0
        *panchor = a->next;
422
0
        a->next = NULL;
423
0
        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
0
        return CURLE_OK;
429
0
      }
430
      /* still running */
431
0
      ++ongoing;
432
0
    }
433
0
    else if(a->inconclusive) /* failed, but inconclusive */
434
0
      ++inconclusive;
435
0
  }
436
0
  if(bs->running)
437
0
    CURL_TRC_CF(data, cf, "checked connect attempts: "
438
0
                "%u ongoing, %u inconclusive", ongoing, inconclusive);
439
440
  /* no attempt connected yet, start another one? */
441
0
  pnow = Curl_pgrs_now(data);
442
0
  if(!ongoing) {
443
0
    do_more = TRUE;
444
0
  }
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
0
  if(do_more) {
460
    /* start the next attempt if there is another ip address to try.
461
     * Alternate between address families when possible. */
462
0
    const struct Curl_addrinfo *ai = NULL;
463
0
    int ai_family = 0;
464
0
    CURL_TRC_CF(data, cf, "want to do more");
465
0
#ifdef USE_IPV6
466
0
    if((bs->last_attempt_ai_family == AF_INET) ||
467
0
       !cf_ai_iter_has_more(&bs->addr_iter, data)) {
468
0
      ai = cf_ai_iter_next(&bs->ipv6_iter, data);
469
0
      ai_family = bs->ipv6_iter.ai_family;
470
0
      CURL_TRC_CF(data, cf, "check for next AAAA address: %s",
471
0
                  ai ? "found" : "none");
472
0
    }
473
0
#endif
474
0
    if(!ai) {
475
0
      ai = cf_ai_iter_next(&bs->addr_iter, data);
476
0
      ai_family = bs->addr_iter.ai_family;
477
0
      CURL_TRC_CF(data, cf, "check for next A address: %s",
478
0
                  ai ? "found" : "none");
479
0
    }
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
0
    Curl_reset_fail(data);
484
485
0
    if(ai) {  /* try another address */
486
0
      struct Curl_sockaddr_ex addr;
487
488
      /* Discard oldest to make room for new attempt */
489
0
      if(bs->max_concurrent)
490
0
        cf_ip_ballers_prune(bs, cf, data, bs->max_concurrent - 1);
491
492
0
      result = Curl_socket_addr_from_ai(&addr, ai, bs->transport_peer);
493
0
      if(result)
494
0
        goto out;
495
496
0
      result = cf_ip_attempt_new(&a, data, cf, bs->origin, bs->peer,
497
0
                                 bs->transport_peer, &addr, ai_family,
498
0
                                 bs->tunnel_peer, bs->tunnel_transport,
499
0
                                 bs->cf_create);
500
0
      CURL_TRC_CF(data, cf, "starting %s attempt for ipv%s -> %d",
501
0
                  bs->running ? "next" : "first",
502
0
                  (ai_family == AF_INET) ? "4" : "6", (int)result);
503
0
      if(result)
504
0
        goto out;
505
0
      DEBUGASSERT(a);
506
507
      /* append to running list */
508
0
      panchor = &bs->running;
509
0
      while(*panchor)
510
0
        panchor = &(*panchor)->next;
511
0
      *panchor = a;
512
0
      bs->last_attempt_started = *pnow;
513
0
      bs->last_attempt_ai_family = ai_family;
514
      /* and run everything again */
515
0
      goto evaluate;
516
0
    }
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
0
  }
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
      if(breply_ms >= 0 && (reply_ms < 0 || breply_ms < reply_ms))
657
0
        reply_ms = breply_ms;
658
0
    }
659
0
  }
660
0
  return reply_ms;
661
0
}
662
663
typedef enum {
664
  SCFST_INIT,
665
  SCFST_WAITING,
666
  SCFST_DONE
667
} cf_connect_state;
668
669
struct cf_ip_happy_ctx {
670
  cf_ip_connect_create *cf_create;
671
  cf_connect_state state;
672
  struct cf_ip_ballers ballers;
673
  BIT(dns_resolved);
674
};
675
676
static CURLcode is_connected(struct Curl_cfilter *cf,
677
                             struct Curl_easy *data,
678
                             bool *connected)
679
0
{
680
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
681
0
  struct connectdata *conn = cf->conn;
682
0
  CURLcode result;
683
684
0
  result = cf_ip_ballers_run(&ctx->ballers, cf, data,
685
0
                             (bool)ctx->dns_resolved, connected);
686
687
0
  if(!result)
688
0
    return CURLE_OK;
689
0
  else {
690
0
    struct Curl_peer *peer = NULL, *proxy_peer = NULL;
691
0
    char viamsg[160];
692
693
0
    peer = Curl_conn_get_first_peer(conn, cf->sockindex);
694
0
    if(!conn->origin || !peer)
695
0
      return CURLE_FAILED_INIT;
696
697
0
#ifndef CURL_DISABLE_PROXY
698
0
    if(conn->socks_proxy.peer)
699
0
      proxy_peer = conn->socks_proxy.peer;
700
0
    else if(conn->http_proxy.peer)
701
0
      proxy_peer = conn->http_proxy.peer;
702
0
#endif
703
704
0
    viamsg[0] = 0;
705
0
    if(!Curl_peer_equal(peer, conn->origin) &&
706
0
       !Curl_peer_equal(peer, proxy_peer)) {
707
#ifdef USE_UNIX_SOCKETS
708
      if(peer->unix_socket)
709
        curl_msnprintf(viamsg, sizeof(viamsg), " over unix://%s",
710
                       peer->hostname);
711
      else
712
#endif
713
0
      curl_msnprintf(viamsg, sizeof(viamsg), " via %s:%u",
714
0
                     peer->hostname, peer->port);
715
0
    }
716
717
0
    failf(data, "Failed to connect to %s:%u%s %s%s%safter "
718
0
          "%" FMT_TIMEDIFF_T " ms: %s",
719
0
          conn->origin->hostname, conn->origin->port, viamsg,
720
0
          proxy_peer ? "over proxy " : "",
721
0
          proxy_peer ? proxy_peer->hostname : "",
722
0
          proxy_peer ? " " : "",
723
0
          Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE),
724
0
          curl_easy_strerror(result));
725
726
0
#ifdef SOCKETIMEDOUT
727
0
    if(SOCKETIMEDOUT == data->state.os_errno)
728
0
      result = CURLE_OPERATION_TIMEDOUT;
729
0
#endif
730
731
0
    return result;
732
0
  }
733
0
}
734
735
0
#define IP_HE_MAX_CONCURRENT_ATTEMPTS     6
736
737
static CURLcode cf_ip_happy_init(struct Curl_cfilter *cf,
738
                                 struct Curl_easy *data)
739
0
{
740
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
741
742
0
  if(Curl_timeleft_ms(data) < 0) {
743
    /* a precaution, no need to continue if time already is up */
744
0
    failf(data, "Connection time-out");
745
0
    return CURLE_OPERATION_TIMEDOUT;
746
0
  }
747
748
0
  if(ctx->ballers.transport_peer == TRNSPRT_UNIX) {
749
#ifdef USE_UNIX_SOCKETS
750
    cf_ai_iter_init(&ctx->ballers.addr_iter, cf, ctx->ballers.peer, AF_UNIX);
751
#else
752
0
    return CURLE_UNSUPPORTED_PROTOCOL;
753
0
#endif
754
0
  }
755
0
  else { /* TCP/UDP/QUIC */
756
0
#ifdef USE_IPV6
757
0
    cf_ai_iter_init(&ctx->ballers.ipv6_iter, cf, ctx->ballers.peer, AF_INET6);
758
0
#endif
759
0
    cf_ai_iter_init(&ctx->ballers.addr_iter, cf, ctx->ballers.peer, AF_INET);
760
0
  }
761
762
0
  CURL_TRC_CF(data, cf, "init ip ballers for transport %u",
763
0
              ctx->ballers.transport_peer);
764
0
  return CURLE_OK;
765
0
}
766
767
static void cf_ip_happy_ctx_clear(struct cf_ip_happy_ctx *ctx,
768
                                  struct Curl_easy *data)
769
0
{
770
0
  DEBUGASSERT(ctx);
771
0
  if(ctx)
772
0
    cf_ip_ballers_clear(data, &ctx->ballers);
773
0
}
774
775
static void cf_ip_happy_ctx_destroy(struct cf_ip_happy_ctx *ctx,
776
                                    struct Curl_easy *data)
777
0
{
778
0
  if(ctx) {
779
0
    cf_ip_happy_ctx_clear(ctx, data);
780
0
    curlx_free(ctx);
781
0
  }
782
0
}
783
784
static CURLcode cf_ip_happy_shutdown(struct Curl_cfilter *cf,
785
                                     struct Curl_easy *data,
786
                                     bool *done)
787
0
{
788
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
789
0
  CURLcode result = CURLE_OK;
790
791
0
  DEBUGASSERT(data);
792
0
  if(cf->connected) {
793
0
    *done = TRUE;
794
0
    return CURLE_OK;
795
0
  }
796
797
0
  result = cf_ip_ballers_shutdown(&ctx->ballers, data, done);
798
0
  CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", (int)result, *done);
799
0
  return result;
800
0
}
801
802
static CURLcode cf_ip_happy_adjust_pollset(struct Curl_cfilter *cf,
803
                                           struct Curl_easy *data,
804
                                           struct easy_pollset *ps)
805
0
{
806
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
807
0
  CURLcode result = CURLE_OK;
808
809
0
  if(!cf->connected) {
810
0
    result = cf_ip_ballers_pollset(&ctx->ballers, data, ps);
811
0
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %u socks", (int)result,
812
0
                ps->n);
813
0
  }
814
0
  return result;
815
0
}
816
817
static CURLcode cf_ip_happy_connect(struct Curl_cfilter *cf,
818
                                    struct Curl_easy *data,
819
                                    bool *done)
820
0
{
821
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
822
0
  CURLcode result = CURLE_OK;
823
824
  /* -Werror=null-dereference finds false positives suddenly. */
825
0
  if(!data)
826
0
    return CURLE_FAILED_INIT;
827
828
0
  if(cf->connected) {
829
0
    *done = TRUE;
830
0
    return CURLE_OK;
831
0
  }
832
833
0
  DEBUGASSERT(ctx);
834
0
  *done = FALSE;
835
836
0
  if(!ctx->dns_resolved) {
837
0
    result = Curl_conn_dns_addr_result(cf->conn, cf->sockindex,
838
0
                                       ctx->ballers.peer);
839
0
    if(!result)
840
0
      ctx->dns_resolved = TRUE;
841
0
    else if(result == CURLE_AGAIN) {
842
0
      result = CURLE_OK;
843
0
    }
844
0
    else /* real error */
845
0
      goto out;
846
0
  }
847
848
0
  switch(ctx->state) {
849
0
  case SCFST_INIT:
850
0
    DEBUGASSERT(CURL_SOCKET_BAD == Curl_conn_cf_get_socket(cf, data));
851
0
    DEBUGASSERT(!cf->connected);
852
0
    result = cf_ip_happy_init(cf, data);
853
0
    if(result)
854
0
      goto out;
855
0
    ctx->state = SCFST_WAITING;
856
0
    FALLTHROUGH();
857
0
  case SCFST_WAITING:
858
0
    result = is_connected(cf, data, done);
859
0
    if(!result && *done) {
860
0
      DEBUGASSERT(ctx->ballers.winner);
861
0
      DEBUGASSERT(ctx->ballers.winner->cf);
862
0
      DEBUGASSERT(ctx->ballers.winner->cf->connected);
863
      /* we have a winner. Install and activate it.
864
       * close/free all others. */
865
0
      ctx->state = SCFST_DONE;
866
0
      cf->connected = TRUE;
867
0
      cf->next = ctx->ballers.winner->cf;
868
0
      ctx->ballers.winner->cf = NULL;
869
870
0
      if(cf->conn->scheme->protocol & PROTO_FAMILY_SSH)
871
0
        Curl_pgrsTime(data, TIMER_APPCONNECT); /* we are connected already */
872
0
#ifdef CURLVERBOSE
873
0
      if(Curl_trc_cf_is_verbose(cf, data)) {
874
0
        struct ip_quadruple ipquad;
875
0
        bool is_ipv6;
876
0
        if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) {
877
0
          CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u",
878
0
                      ctx->ballers.peer->hostname,
879
0
                      ipquad.remote_ip, ipquad.remote_port);
880
0
        }
881
0
      }
882
0
#endif
883
0
      cf_ip_happy_ctx_clear(ctx, data);
884
0
      Curl_expire_clear(data, EXPIRE_HAPPY_EYEBALLS);
885
      /* whatever errors were reported by ballers, clear our errorbuf */
886
0
      Curl_reset_fail(data);
887
0
      data->info.numconnects++; /* to track the # of connections made */
888
0
    }
889
0
    break;
890
0
  case SCFST_DONE:
891
0
    *done = TRUE;
892
0
    break;
893
0
  }
894
0
out:
895
0
  return result;
896
0
}
897
898
static bool cf_ip_happy_data_pending(struct Curl_cfilter *cf,
899
                                     const struct Curl_easy *data)
900
0
{
901
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
902
903
0
  if(!cf->connected) {
904
0
    return cf_ip_ballers_pending(&ctx->ballers, data);
905
0
  }
906
0
  return cf->next->cft->has_data_pending(cf->next, data);
907
0
}
908
909
static CURLcode cf_ip_happy_query(struct Curl_cfilter *cf,
910
                                  struct Curl_easy *data,
911
                                  int query, int *pres1, void *pres2)
912
0
{
913
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
914
915
0
  if(!cf->connected) {
916
0
    switch(query) {
917
0
    case CF_QUERY_CONNECT_REPLY_MS: {
918
0
      *pres1 = cf_ip_ballers_min_reply_ms(&ctx->ballers, data);
919
0
      CURL_TRC_CF(data, cf, "query connect reply: %dms", *pres1);
920
0
      return CURLE_OK;
921
0
    }
922
0
    default:
923
0
      break;
924
0
    }
925
0
  }
926
927
0
  return cf->next ?
928
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
929
0
    CURLE_UNKNOWN_OPTION;
930
0
}
931
932
static void cf_ip_happy_destroy(struct Curl_cfilter *cf,
933
                                struct Curl_easy *data)
934
0
{
935
0
  struct cf_ip_happy_ctx *ctx = cf->ctx;
936
937
0
  CURL_TRC_CF(data, cf, "destroy");
938
0
  if(ctx) {
939
0
    cf_ip_happy_ctx_clear(ctx, data);
940
0
    cf_ip_happy_ctx_destroy(ctx, data);
941
0
  }
942
0
}
943
944
struct Curl_cftype Curl_cft_ip_happy = {
945
  "HAPPY-EYEBALLS",
946
  CF_TYPE_SETUP,
947
  CURL_LOG_LVL_NONE,
948
  cf_ip_happy_destroy,
949
  cf_ip_happy_connect,
950
  cf_ip_happy_shutdown,
951
  cf_ip_happy_adjust_pollset,
952
  cf_ip_happy_data_pending,
953
  Curl_cf_def_send,
954
  Curl_cf_def_recv,
955
  Curl_cf_def_cntrl,
956
  Curl_cf_def_conn_is_alive,
957
  Curl_cf_def_conn_keep_alive,
958
  cf_ip_happy_query,
959
};
960
961
static CURLcode cf_ip_happy_create(struct Curl_cfilter **pcf,
962
                                   struct Curl_easy *data,
963
                                   struct Curl_peer *origin,
964
                                   struct Curl_peer *peer,
965
                                   uint8_t transport_peer,
966
                                   struct connectdata *conn,
967
                                   struct Curl_peer *tunnel_peer,
968
                                   uint8_t tunnel_transport)
969
0
{
970
0
  struct cf_ip_happy_ctx *ctx = NULL;
971
0
  CURLcode result;
972
973
0
  (void)data;
974
0
  (void)conn;
975
0
  *pcf = NULL;
976
0
  ctx = curlx_calloc(1, sizeof(*ctx));
977
0
  if(!ctx) {
978
0
    result = CURLE_OUT_OF_MEMORY;
979
0
    goto out;
980
0
  }
981
0
  result = cf_ip_ballers_init(&ctx->ballers, data,
982
0
                              origin, peer, transport_peer,
983
0
                              tunnel_peer, tunnel_transport,
984
0
                              data->set.happy_eyeballs_timeout,
985
0
                              IP_HE_MAX_CONCURRENT_ATTEMPTS);
986
0
  if(result)
987
0
    goto out;
988
989
0
  result = Curl_cf_create(pcf, &Curl_cft_ip_happy, ctx);
990
991
0
out:
992
0
  if(result) {
993
0
    curlx_safefree(*pcf);
994
0
    cf_ip_happy_ctx_destroy(ctx, data);
995
0
  }
996
0
  return result;
997
0
}
998
999
CURLcode Curl_cf_ip_happy_insert_after(struct Curl_cfilter *cf_at,
1000
                                       struct Curl_easy *data,
1001
                                       struct Curl_peer *origin,
1002
                                       struct Curl_peer *peer,
1003
                                       uint8_t transport_peer,
1004
                                       struct Curl_peer *tunnel_peer,
1005
                                       uint8_t tunnel_transport)
1006
0
{
1007
0
  struct Curl_cfilter *cf;
1008
0
  CURLcode result;
1009
1010
  /* Need to be first */
1011
0
  DEBUGASSERT(cf_at);
1012
0
  result = cf_ip_happy_create(&cf, data, origin, peer, transport_peer,
1013
0
                              cf_at->conn, tunnel_peer, tunnel_transport);
1014
0
  if(result)
1015
0
    return result;
1016
1017
0
  Curl_conn_cf_insert_after(cf_at, cf);
1018
0
  return CURLE_OK;
1019
0
}