Coverage Report

Created: 2026-04-29 07:01

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