Coverage Report

Created: 2026-09-04 07:15

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