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-https-connect.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
#ifndef CURL_DISABLE_HTTP
27
28
#include "urldata.h"
29
#include "curl_trc.h"
30
#include "cfilters.h"
31
#include "cf-setup.h"
32
#include "connect.h"
33
#include "multiif.h"
34
#include "cf-https-connect.h"
35
#include "http2.h"
36
#include "progress.h"
37
#include "select.h"
38
#include "vdns/cf-dns.h"
39
#include "vdns/hostip.h"
40
#include "vdns/httpsrr.h"
41
#include "vquic/vquic.h"
42
43
typedef enum {
44
  CF_HC_RESOLV,
45
  CF_HC_INIT,
46
  CF_HC_CONNECT,
47
  CF_HC_SUCCESS,
48
  CF_HC_FAILURE
49
} cf_hc_state;
50
51
struct cf_hc_baller {
52
  const char *name;
53
  struct Curl_cfilter *cf;
54
  CURLcode result;
55
  int reply_ms;
56
  uint8_t transport;
57
  enum alpnid alpn_id;
58
  BIT(shutdown);
59
};
60
61
static void cf_hc_baller_discard(struct cf_hc_baller *b,
62
                                 struct Curl_easy *data)
63
0
{
64
0
  if(b->cf) {
65
0
    Curl_conn_cf_discard_chain(&b->cf, data);
66
0
    b->cf = NULL;
67
0
  }
68
0
}
69
70
static bool cf_hc_baller_is_connecting(struct cf_hc_baller *b)
71
0
{
72
0
  return b->cf && !b->result;
73
0
}
74
75
static bool cf_hc_baller_has_started(struct cf_hc_baller *b)
76
0
{
77
0
  return !!b->cf;
78
0
}
79
80
static int cf_hc_baller_reply_ms(struct cf_hc_baller *b,
81
                                 struct Curl_easy *data)
82
0
{
83
0
  if(b->cf && (b->reply_ms < 0))
84
0
    b->cf->cft->query(b->cf, data, CF_QUERY_CONNECT_REPLY_MS,
85
0
                      &b->reply_ms, NULL);
86
0
  return b->reply_ms;
87
0
}
88
89
static bool cf_hc_baller_data_pending(struct cf_hc_baller *b,
90
                                      const struct Curl_easy *data)
91
0
{
92
0
  return b->cf && !b->result && b->cf->cft->has_data_pending(b->cf, data);
93
0
}
94
95
static bool cf_hc_baller_needs_flush(struct cf_hc_baller *b,
96
                                     struct Curl_easy *data)
97
0
{
98
0
  return b->cf && !b->result && Curl_conn_cf_needs_flush(b->cf, data);
99
0
}
100
101
static CURLcode cf_hc_baller_cntrl(struct cf_hc_baller *b,
102
                                   struct Curl_easy *data,
103
                                   int event, int arg1, void *arg2)
104
0
{
105
0
  if(b->cf && !b->result)
106
0
    return Curl_conn_cf_cntrl(b->cf, data, FALSE, event, arg1, arg2);
107
0
  return CURLE_OK;
108
0
}
109
110
struct cf_hc_ctx {
111
  cf_hc_state state;
112
  struct Curl_peer *destination; /* who we ultimately want to talk to */
113
  struct curltime started;  /* when connect started */
114
  CURLcode result;          /* overall result */
115
  CURLcode check_h3_result;
116
  struct cf_hc_baller ballers[2];
117
  size_t baller_count;
118
  timediff_t soft_eyeballs_timeout_ms;
119
  timediff_t hard_eyeballs_timeout_ms;
120
  uint8_t def_transport;
121
  BIT(httpsrr_resolved);
122
  BIT(checked_h3);
123
  BIT(ballers_complete);
124
};
125
126
static void cf_hc_ctx_destroy(struct Curl_easy *data,
127
                              struct cf_hc_ctx *ctx)
128
0
{
129
0
  if(ctx) {
130
0
    size_t i;
131
0
    for(i = 0; i < ctx->baller_count; ++i)
132
0
      cf_hc_baller_discard(&ctx->ballers[i], data);
133
0
    Curl_peer_unlink(&ctx->destination);
134
0
    curlx_free(ctx);
135
0
  }
136
0
}
137
138
static void cf_hc_baller_assign(struct cf_hc_baller *b,
139
                                enum alpnid alpn_id,
140
                                uint8_t def_transport)
141
0
{
142
0
  b->alpn_id = alpn_id;
143
0
  b->transport = def_transport;
144
0
  b->cf = NULL;
145
0
  b->result = CURLE_OK;
146
0
  b->reply_ms = -1;
147
0
  b->shutdown = FALSE;
148
0
  switch(b->alpn_id) {
149
0
  case ALPN_h3:
150
0
    b->name = "h3";
151
0
    b->transport = TRNSPRT_QUIC;
152
0
    break;
153
0
  case ALPN_h2:
154
0
    b->name = "h2";
155
0
    break;
156
0
  case ALPN_h1:
157
0
    b->name = "h1";
158
0
    break;
159
0
  case ALPN_none:
160
0
    b->name = "no-alpn";
161
0
    break;
162
0
  default:
163
0
    b->result = CURLE_FAILED_INIT;
164
0
    break;
165
0
  }
166
0
}
167
168
static void cf_hc_baller_init(struct cf_hc_baller *b,
169
                              struct Curl_cfilter *cf,
170
                              struct Curl_easy *data)
171
0
{
172
0
  struct Curl_cfilter *save = cf->next;
173
174
0
  cf->next = NULL;
175
0
  b->result = Curl_cf_setup_insert_after(cf, data, b->transport,
176
0
                                         CURL_CF_SSL_ENABLE);
177
0
  b->cf = cf->next;
178
0
  cf->next = save;
179
0
}
180
181
static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b,
182
                                     struct Curl_cfilter *cf,
183
                                     struct Curl_easy *data,
184
                                     bool *done)
185
0
{
186
0
  struct Curl_cfilter *save = cf->next;
187
188
0
  cf->next = b->cf;
189
0
  b->result = Curl_conn_cf_connect(cf->next, data, done);
190
0
  b->cf = cf->next; /* it might mutate */
191
0
  cf->next = save;
192
0
  return b->result;
193
0
}
194
195
static CURLcode baller_connected(struct Curl_cfilter *cf,
196
                                 struct Curl_easy *data,
197
                                 struct cf_hc_baller *winner)
198
0
{
199
0
  struct cf_hc_ctx *ctx = cf->ctx;
200
201
  /* Make the winner's connection filter out own sub-filter, check, move,
202
   * close all remaining. */
203
0
  if(cf->next) {
204
0
    DEBUGASSERT(0);
205
0
    return CURLE_FAILED_INIT;
206
0
  }
207
0
  if(!winner->cf) {
208
0
    DEBUGASSERT(0);
209
0
    return CURLE_FAILED_INIT;
210
0
  }
211
212
0
  cf->next = winner->cf;
213
0
  winner->cf = NULL;
214
0
  ctx->state = CF_HC_SUCCESS;
215
0
  cf->connected = TRUE;
216
217
  /* ballers may have failf()'d, the winner resets it, so our
218
   * errorbuf is clean again. */
219
0
  Curl_reset_fail(data);
220
221
0
#ifdef USE_NGHTTP2
222
0
  {
223
    /* For a negotiated HTTP/2 connection insert the h2 filter. */
224
0
    const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
225
0
    if(alpn && !strcmp("h2", alpn)) {
226
0
      CURLcode result = Curl_http2_switch_at(cf, data);
227
0
      if(result) {
228
0
        ctx->state = CF_HC_FAILURE;
229
0
        ctx->result = result;
230
0
        return result;
231
0
      }
232
0
    }
233
0
  }
234
0
#endif
235
0
  return CURLE_OK;
236
0
}
237
238
static bool time_to_start_baller2(struct Curl_cfilter *cf,
239
                                  struct Curl_easy *data,
240
                                  const struct curltime *pnow)
241
0
{
242
0
  struct cf_hc_ctx *ctx = cf->ctx;
243
0
  timediff_t elapsed_ms;
244
245
0
  if(ctx->baller_count < 2)
246
0
    return FALSE;
247
0
  else if(cf_hc_baller_has_started(&ctx->ballers[1]))
248
0
    return FALSE;
249
0
  else if(ctx->ballers[0].result) {
250
0
    CURL_TRC_CF(data, cf, "%s baller failed, starting %s",
251
0
                ctx->ballers[0].name, ctx->ballers[1].name);
252
0
    return TRUE;
253
0
  }
254
255
0
  elapsed_ms = curlx_ptimediff_ms(pnow, &ctx->started);
256
0
  if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
257
0
    CURL_TRC_CF(data, cf, "%s inconclusive after %" FMT_TIMEDIFF_T ", "
258
0
                "starting %s", ctx->ballers[0].name,
259
0
                ctx->hard_eyeballs_timeout_ms, ctx->ballers[1].name);
260
0
    return TRUE;
261
0
  }
262
0
  else if(elapsed_ms >= ctx->soft_eyeballs_timeout_ms) {
263
0
    if(cf_hc_baller_reply_ms(&ctx->ballers[0], data) < 0) {
264
0
      CURL_TRC_CF(data, cf, "%s has not seen any data after %"
265
0
                  FMT_TIMEDIFF_T "ms, starting %s",
266
0
                  ctx->ballers[0].name, ctx->soft_eyeballs_timeout_ms,
267
0
                  ctx->ballers[1].name);
268
0
      return TRUE;
269
0
    }
270
0
  }
271
0
  return FALSE;
272
0
}
273
274
static bool cf_hc_may_h3(struct Curl_cfilter *cf,
275
                         struct Curl_easy *data)
276
0
{
277
0
  struct cf_hc_ctx *ctx = cf->ctx;
278
0
  if(!ctx->checked_h3) {
279
0
    ctx->check_h3_result =
280
0
      Curl_conn_may_http3(data, cf->conn, ctx->def_transport);
281
0
    ctx->checked_h3 = TRUE;
282
0
  }
283
0
  return !ctx->check_h3_result;
284
0
}
285
286
static enum alpnid cf_hc_get_httpsrr_alpn(struct Curl_cfilter *cf,
287
                                          struct Curl_easy *data,
288
                                          enum alpnid not_this_one)
289
0
{
290
#ifdef USE_HTTPSRR
291
  struct cf_hc_ctx *ctx = cf->ctx;
292
  /* Is there an HTTPSRR use its ALPNs here.
293
   * We are here after having selected a connection to a host+port and
294
   * can no longer change that. Any HTTPSRR advice for other hosts and ports
295
   * we need to ignore. */
296
  const struct Curl_https_rrinfo *rr;
297
  size_t i;
298
299
  /* Do we have HTTPS-RR information? */
300
  rr = Curl_conn_dns_get_https(data, cf->sockindex, ctx->destination);
301
302
  CURL_TRC_CF(data, cf, "HTTPS-RR %savailable", rr ? "" : "not ");
303
  /* We do not support `rr->no_def_alpn`. */
304
  if(Curl_httpsrr_applicable(data, rr) && !rr->no_def_alpn) {
305
    for(i = 0; i < CURL_ARRAYSIZE(rr->alpns); ++i) {
306
      enum alpnid alpn_rr = (enum alpnid)rr->alpns[i];
307
      if(alpn_rr == not_this_one) /* do not want this one */
308
        continue;
309
      switch(alpn_rr) {
310
      case ALPN_h3:
311
        if((data->state.http_neg.allowed & CURL_HTTP_V3x) &&
312
           cf_hc_may_h3(cf, data)) {
313
          return alpn_rr;
314
        }
315
        break;
316
      case ALPN_h2:
317
        if(data->state.http_neg.allowed & CURL_HTTP_V2x) {
318
          return alpn_rr;
319
        }
320
        break;
321
      case ALPN_h1:
322
        if(data->state.http_neg.allowed & CURL_HTTP_V1x) {
323
          return alpn_rr;
324
        }
325
        break;
326
      default: /* ignore */
327
        break;
328
      }
329
    }
330
  }
331
#else
332
0
  (void)cf;
333
0
  (void)data;
334
0
  (void)not_this_one;
335
0
#endif
336
0
  return ALPN_none;
337
0
}
338
339
static enum alpnid cf_hc_get_pref_alpn(struct Curl_cfilter *cf,
340
                                       struct Curl_easy *data,
341
                                       enum alpnid not_this_one)
342
0
{
343
0
  if((data->state.http_neg.preferred & data->state.http_neg.allowed)) {
344
0
    switch(data->state.http_neg.preferred) {
345
0
    case CURL_HTTP_V3x:
346
0
      if(cf_hc_may_h3(cf, data) && (ALPN_h3 != not_this_one))
347
0
        return ALPN_h3;
348
0
      break;
349
0
    case CURL_HTTP_V2x:
350
0
      if(ALPN_h2 != not_this_one)
351
0
        return ALPN_h2;
352
0
      break;
353
0
    case CURL_HTTP_V1x:
354
      /* If we are trying h2 already, h1 is already used as fallback */
355
0
      if((ALPN_h1 != not_this_one) && (ALPN_h2 != not_this_one))
356
0
        return ALPN_h1;
357
0
      break;
358
0
    default:
359
0
      break;
360
0
    }
361
0
  }
362
0
  return ALPN_none;
363
0
}
364
365
static enum alpnid cf_hc_get_first_alpn(struct Curl_cfilter *cf,
366
                                        struct Curl_easy *data,
367
                                        http_majors choices,
368
                                        enum alpnid not_this_one)
369
0
{
370
  /* When told to not try h2, we also do not try h1 and vice versa */
371
0
  bool allow_h1_or_h2 = (not_this_one != ALPN_h1) &&
372
0
                        (not_this_one != ALPN_h2);
373
0
  if((ALPN_h3 != not_this_one) && (choices & CURL_HTTP_V3x) &&
374
0
     cf_hc_may_h3(cf, data)) {
375
0
    return ALPN_h3;
376
0
  }
377
0
  if(allow_h1_or_h2 && (choices & CURL_HTTP_V2x)) {
378
0
    return ALPN_h2;
379
0
  }
380
0
  if(allow_h1_or_h2 && (choices & CURL_HTTP_V1x)) {
381
0
    return ALPN_h1;
382
0
  }
383
0
  return ALPN_none;
384
0
}
385
386
static CURLcode cf_hc_set_baller1(struct Curl_cfilter *cf,
387
                                  struct Curl_easy *data)
388
0
{
389
0
  struct cf_hc_ctx *ctx = cf->ctx;
390
0
  enum alpnid alpn1 = ALPN_none;
391
0
  VERBOSE(const char *source = "HTTPS-RR");
392
393
0
  DEBUGASSERT(cf->conn->bits.tls_enable_alpn);
394
395
0
  alpn1 = cf_hc_get_httpsrr_alpn(cf, data, ALPN_none);
396
0
  if(alpn1 == ALPN_none) {
397
    /* preference is configured and allowed, can we use it? */
398
0
    VERBOSE(source = "preferred version");
399
0
    alpn1 = cf_hc_get_pref_alpn(cf, data, ALPN_none);
400
0
  }
401
0
  if(alpn1 == ALPN_none) {
402
0
    VERBOSE(source = "wanted versions");
403
0
    alpn1 = cf_hc_get_first_alpn(cf, data,
404
0
                                 data->state.http_neg.wanted,
405
0
                                 ALPN_none);
406
0
  }
407
0
  if(alpn1 == ALPN_none) {
408
0
    VERBOSE(source = "allowed versions");
409
0
    alpn1 = cf_hc_get_first_alpn(cf, data,
410
0
                                 data->state.http_neg.allowed,
411
0
                                 ALPN_none);
412
0
  }
413
414
0
  if(alpn1 == ALPN_none) {
415
    /* None of the wanted/allowed HTTP versions could be chosen */
416
0
    if(ctx->check_h3_result) {
417
0
      CURL_TRC_CF(data, cf, "unable to use HTTP/3");
418
0
      return ctx->check_h3_result;
419
0
    }
420
0
    CURL_TRC_CF(data, cf, "unable to select HTTP version");
421
0
    return CURLE_FAILED_INIT;
422
0
  }
423
424
0
  cf_hc_baller_assign(&ctx->ballers[0], alpn1, ctx->def_transport);
425
0
  ctx->baller_count = 1;
426
0
  CURL_TRC_CF(data, cf, "1st attempt uses %s from %s",
427
0
              ctx->ballers[0].name, source);
428
429
0
  switch(alpn1) {
430
0
  case ALPN_h1:
431
    /* We really want h1, switch off h2 to make it disappear in ALPN */
432
0
    data->state.http_neg.wanted &= (uint8_t)~CURL_HTTP_V2x;
433
0
    break;
434
0
  default:
435
0
    break;
436
0
  }
437
438
0
  return CURLE_OK;
439
0
}
440
441
static void cf_hc_set_baller2(struct Curl_cfilter *cf,
442
                              struct Curl_easy *data)
443
0
{
444
0
  struct cf_hc_ctx *ctx = cf->ctx;
445
0
  enum alpnid alpn2 = ALPN_none, alpn1 = ctx->ballers[0].alpn_id;
446
0
  VERBOSE(const char *source = "HTTPS-RR");
447
448
0
  if(ctx->ballers_complete)
449
0
    return; /* already done */
450
0
  if(!ctx->httpsrr_resolved)
451
0
    return; /* HTTPS-RR pending */
452
453
0
  alpn2 = cf_hc_get_httpsrr_alpn(cf, data, alpn1);
454
0
  if(alpn2 == ALPN_none) {
455
    /* preference is configured and allowed, can we use it? */
456
0
    VERBOSE(source = "preferred version");
457
0
    alpn2 = cf_hc_get_pref_alpn(cf, data, alpn1);
458
0
  }
459
0
  if(alpn2 == ALPN_none) {
460
0
    VERBOSE(source = "wanted versions");
461
0
    alpn2 = cf_hc_get_first_alpn(cf, data,
462
0
                                 data->state.http_neg.wanted,
463
0
                                 alpn1);
464
0
  }
465
466
0
  if(alpn2 != ALPN_none) {
467
0
    cf_hc_baller_assign(&ctx->ballers[1], alpn2, ctx->def_transport);
468
0
    ctx->baller_count = 2;
469
0
    CURL_TRC_CF(data, cf, "2nd attempt uses %s from %s",
470
0
                ctx->ballers[1].name, source);
471
0
  }
472
0
  ctx->ballers_complete = TRUE;
473
0
}
474
475
static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
476
                              struct Curl_easy *data,
477
                              bool *done)
478
0
{
479
0
  struct cf_hc_ctx *ctx = cf->ctx;
480
0
  CURLcode result = CURLE_OK;
481
0
  const struct curltime *pnow = NULL;
482
483
0
  if(cf->connected) {
484
0
    *done = TRUE;
485
0
    return CURLE_OK;
486
0
  }
487
488
0
  *done = FALSE;
489
490
0
  if(!ctx->httpsrr_resolved) {
491
0
    ctx->httpsrr_resolved = Curl_conn_dns_resolved_https(
492
0
      data, cf->sockindex, ctx->destination);
493
#ifdef DEBUGBUILD
494
    if(!ctx->httpsrr_resolved && getenv("CURL_DBG_AWAIT_HTTPSRR")) {
495
      CURL_TRC_CF(data, cf, "awaiting HTTPS-RR");
496
      return CURLE_OK;
497
    }
498
#endif
499
0
  }
500
501
0
  switch(ctx->state) {
502
0
  case CF_HC_RESOLV:
503
0
    ctx->state = CF_HC_INIT;
504
0
    FALLTHROUGH();
505
506
0
  case CF_HC_INIT:
507
0
    DEBUGASSERT(!cf->next);
508
0
    CURL_TRC_CF(data, cf, "connect, init");
509
0
    result = cf_hc_set_baller1(cf, data);
510
0
    if(result) {
511
0
      ctx->result = result;
512
0
      ctx->state = CF_HC_FAILURE;
513
0
      goto out;
514
0
    }
515
0
    cf_hc_set_baller2(cf, data);
516
0
    pnow = Curl_pgrs_now(data);
517
0
    ctx->started = *pnow;
518
0
    cf_hc_baller_init(&ctx->ballers[0], cf, data);
519
0
    if((ctx->baller_count > 1) || !ctx->ballers_complete) {
520
0
      Curl_expire_set(data, EXPIRE_ALPN_EYEBALLS,
521
0
                      ctx->soft_eyeballs_timeout_ms, pnow);
522
0
    }
523
0
    ctx->state = CF_HC_CONNECT;
524
0
    FALLTHROUGH();
525
526
0
  case CF_HC_CONNECT:
527
0
    if(!ctx->ballers_complete)
528
0
      cf_hc_set_baller2(cf, data);
529
530
0
    if(cf_hc_baller_is_connecting(&ctx->ballers[0])) {
531
0
      result = cf_hc_baller_connect(&ctx->ballers[0], cf, data, done);
532
0
      if(!result && *done) {
533
0
        result = baller_connected(cf, data, &ctx->ballers[0]);
534
0
        goto out;
535
0
      }
536
0
    }
537
538
0
    pnow = Curl_pgrs_now(data);
539
0
    if(time_to_start_baller2(cf, data, pnow)) {
540
0
      cf_hc_baller_init(&ctx->ballers[1], cf, data);
541
0
    }
542
543
0
    if(cf_hc_baller_is_connecting(&ctx->ballers[1])) {
544
0
      result = cf_hc_baller_connect(&ctx->ballers[1], cf, data, done);
545
0
      if(!result && *done) {
546
0
        result = baller_connected(cf, data, &ctx->ballers[1]);
547
0
        goto out;
548
0
      }
549
0
    }
550
551
0
    if(ctx->ballers[0].result &&
552
0
       (ctx->ballers[1].result ||
553
0
        (ctx->ballers_complete && (ctx->baller_count < 2)))) {
554
      /* all have failed. we give up */
555
0
      CURL_TRC_CF(data, cf, "connect, all attempts failed");
556
0
      ctx->result = result = ctx->ballers[0].result;
557
0
      ctx->state = CF_HC_FAILURE;
558
0
      goto out;
559
0
    }
560
0
    result = CURLE_OK;
561
0
    *done = FALSE;
562
0
    break;
563
564
0
  case CF_HC_FAILURE:
565
0
    result = ctx->result;
566
0
    cf->connected = FALSE;
567
0
    *done = FALSE;
568
0
    break;
569
570
0
  case CF_HC_SUCCESS:
571
0
    result = CURLE_OK;
572
0
    cf->connected = TRUE;
573
0
    *done = TRUE;
574
0
    break;
575
0
  }
576
577
0
out:
578
0
  CURL_TRC_CF(data, cf, "connect -> %d, done=%d", (int)result, *done);
579
0
  return result;
580
0
}
581
582
static CURLcode cf_hc_shutdown(struct Curl_cfilter *cf,
583
                               struct Curl_easy *data, bool *done)
584
0
{
585
0
  struct cf_hc_ctx *ctx = cf->ctx;
586
0
  size_t i;
587
0
  CURLcode result = CURLE_OK;
588
589
0
  DEBUGASSERT(data);
590
0
  if(cf->connected) {
591
0
    *done = TRUE;
592
0
    return CURLE_OK;
593
0
  }
594
595
  /* shutdown all ballers that have not done so already. If one fails,
596
   * continue shutting down others until all are shutdown. */
597
0
  for(i = 0; i < ctx->baller_count; i++) {
598
0
    struct cf_hc_baller *b = &ctx->ballers[i];
599
0
    bool bdone = FALSE;
600
0
    if(!cf_hc_baller_is_connecting(b) || b->shutdown)
601
0
      continue;
602
0
    b->result = b->cf->cft->do_shutdown(b->cf, data, &bdone);
603
0
    if(b->result || bdone)
604
0
      b->shutdown = TRUE; /* treat a failed shutdown as done */
605
0
  }
606
607
0
  *done = TRUE;
608
0
  for(i = 0; i < ctx->baller_count; i++) {
609
0
    if(!ctx->ballers[i].shutdown)
610
0
      *done = FALSE;
611
0
  }
612
0
  if(*done) {
613
0
    for(i = 0; i < ctx->baller_count; i++) {
614
0
      if(ctx->ballers[i].result)
615
0
        result = ctx->ballers[i].result;
616
0
    }
617
0
  }
618
0
  CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", (int)result, *done);
619
0
  return result;
620
0
}
621
622
static CURLcode cf_hc_adjust_pollset(struct Curl_cfilter *cf,
623
                                     struct Curl_easy *data,
624
                                     struct easy_pollset *ps)
625
0
{
626
0
  CURLcode result = CURLE_OK;
627
0
  if(!cf->connected) {
628
0
    struct cf_hc_ctx *ctx = cf->ctx;
629
0
    size_t i;
630
631
0
    for(i = 0; (i < ctx->baller_count) && !result; i++) {
632
0
      struct cf_hc_baller *b = &ctx->ballers[i];
633
0
      if(!cf_hc_baller_is_connecting(b))
634
0
        continue;
635
0
      result = Curl_conn_cf_adjust_pollset(b->cf, data, ps);
636
0
    }
637
0
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %u socks", (int)result,
638
0
                ps->n);
639
0
  }
640
0
  return result;
641
0
}
642
643
static bool cf_hc_data_pending(struct Curl_cfilter *cf,
644
                               const struct Curl_easy *data)
645
0
{
646
0
  struct cf_hc_ctx *ctx = cf->ctx;
647
0
  size_t i;
648
649
0
  if(cf->connected)
650
0
    return cf->next->cft->has_data_pending(cf->next, data);
651
652
0
  for(i = 0; i < ctx->baller_count; i++)
653
0
    if(cf_hc_baller_data_pending(&ctx->ballers[i], data))
654
0
      return TRUE;
655
0
  return FALSE;
656
0
}
657
658
static CURLcode cf_hc_query(struct Curl_cfilter *cf,
659
                            struct Curl_easy *data,
660
                            int query, int *pres1, void *pres2)
661
0
{
662
0
  struct cf_hc_ctx *ctx = cf->ctx;
663
0
  size_t i;
664
665
0
  if(!cf->connected) {
666
0
    switch(query) {
667
0
    case CF_QUERY_NEED_FLUSH: {
668
0
      for(i = 0; i < ctx->baller_count; i++)
669
0
        if(cf_hc_baller_needs_flush(&ctx->ballers[i], data)) {
670
0
          *pres1 = TRUE;
671
0
          return CURLE_OK;
672
0
        }
673
0
      break;
674
0
    }
675
0
    default:
676
0
      break;
677
0
    }
678
0
  }
679
0
  return cf->next ?
680
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
681
0
    CURLE_UNKNOWN_OPTION;
682
0
}
683
684
static CURLcode cf_hc_cntrl(struct Curl_cfilter *cf,
685
                            struct Curl_easy *data,
686
                            int event, int arg1, void *arg2)
687
0
{
688
0
  struct cf_hc_ctx *ctx = cf->ctx;
689
0
  CURLcode result = CURLE_OK;
690
0
  size_t i;
691
692
0
  if(!cf->connected) {
693
0
    switch(event) {
694
0
    case CF_CTRL_REPORT_STATS:
695
0
      for(i = 0; i < ctx->baller_count; i++) {
696
        /* Make the first baller that connected at network level report */
697
0
        if(Curl_conn_cf_is_ip_connected(ctx->ballers[i].cf, data)) {
698
0
          Curl_conn_cf_cntrl(ctx->ballers[i].cf, data, TRUE,
699
0
                             event, arg1, arg2);
700
0
          break;
701
0
        }
702
0
      }
703
0
      break;
704
0
    default:
705
0
      for(i = 0; i < ctx->baller_count; i++) {
706
0
        result = cf_hc_baller_cntrl(&ctx->ballers[i], data, event, arg1, arg2);
707
0
        if(result && (result != CURLE_AGAIN))
708
0
          goto out;
709
0
      }
710
0
      result = CURLE_OK;
711
0
      break;
712
0
    }
713
0
  }
714
0
out:
715
0
  return result;
716
0
}
717
718
static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
719
0
{
720
0
  struct cf_hc_ctx *ctx = cf->ctx;
721
722
0
  CURL_TRC_CF(data, cf, "destroy");
723
0
  cf_hc_ctx_destroy(data, ctx);
724
0
}
725
726
struct Curl_cftype Curl_cft_http_connect = {
727
  "HTTPS-CONNECT",
728
  CF_TYPE_SETUP,
729
  CURL_LOG_LVL_NONE,
730
  cf_hc_destroy,
731
  cf_hc_connect,
732
  cf_hc_shutdown,
733
  cf_hc_adjust_pollset,
734
  cf_hc_data_pending,
735
  Curl_cf_def_send,
736
  Curl_cf_def_recv,
737
  cf_hc_cntrl,
738
  Curl_cf_def_conn_is_alive,
739
  Curl_cf_def_conn_keep_alive,
740
  cf_hc_query,
741
};
742
743
static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
744
                             struct Curl_easy *data,
745
                             struct Curl_peer *destination,
746
                             uint8_t def_transport)
747
0
{
748
0
  struct Curl_cfilter *cf = NULL;
749
0
  struct cf_hc_ctx *ctx;
750
0
  CURLcode result = CURLE_OK;
751
752
0
  ctx = curlx_calloc(1, sizeof(*ctx));
753
0
  if(!ctx) {
754
0
    result = CURLE_OUT_OF_MEMORY;
755
0
    goto out;
756
0
  }
757
0
  Curl_peer_link(&ctx->destination, destination);
758
0
  ctx->def_transport = def_transport;
759
0
  ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
760
0
  ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 2;
761
762
0
  result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
763
0
  if(result)
764
0
    goto out;
765
0
  ctx = NULL;
766
767
0
out:
768
0
  *pcf = result ? NULL : cf;
769
0
  cf_hc_ctx_destroy(data, ctx);
770
0
  return result;
771
0
}
772
773
static CURLcode cf_hc_add(struct Curl_easy *data,
774
                          struct Curl_peer *destination,
775
                          struct connectdata *conn,
776
                          int8_t sockindex,
777
                          uint8_t def_transport)
778
0
{
779
0
  struct Curl_cfilter *cf;
780
0
  CURLcode result = CURLE_OK;
781
782
0
  DEBUGASSERT(data);
783
0
  result = cf_hc_create(&cf, data, destination, def_transport);
784
0
  if(result)
785
0
    goto out;
786
0
  Curl_conn_cf_add(data, conn, sockindex, cf);
787
788
#ifdef USE_HTTPSRR
789
  result = Curl_conn_dns_add_https_resolve(data, cf->conn, cf->sockindex,
790
                                           destination);
791
#endif
792
0
out:
793
0
  return result;
794
0
}
795
796
CURLcode Curl_cf_https_setup(struct Curl_easy *data,
797
                             struct Curl_peer *destination,
798
                             struct connectdata *conn,
799
                             int8_t sockindex)
800
0
{
801
0
  CURLcode result = CURLE_OK;
802
803
0
  DEBUGASSERT(conn->scheme->protocol == CURLPROTO_HTTPS);
804
805
  /* This filter is intended for HTTPS using ALPN and does
806
   * not support HTTPS Eyeballing to a proxy. */
807
0
  if((conn->scheme->protocol != CURLPROTO_HTTPS) ||
808
0
#ifndef CURL_DISABLE_PROXY
809
0
     conn->bits.origin_is_proxy ||
810
0
#endif
811
0
     !conn->bits.tls_enable_alpn)
812
0
    goto out;
813
814
0
  result = cf_hc_add(data, destination, conn, sockindex,
815
0
                     conn->transport_wanted);
816
817
0
out:
818
0
  return result;
819
0
}
820
821
#endif /* !CURL_DISABLE_HTTP */