Coverage Report

Created: 2025-08-26 07:17

/src/curl/lib/cf-https-connect.c
Line
Count
Source (jump to first uncovered line)
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
25
#include "curl_setup.h"
26
27
#ifndef CURL_DISABLE_HTTP
28
29
#include "urldata.h"
30
#include <curl/curl.h>
31
#include "curl_trc.h"
32
#include "cfilters.h"
33
#include "connect.h"
34
#include "hostip.h"
35
#include "multiif.h"
36
#include "cf-https-connect.h"
37
#include "http2.h"
38
#include "select.h"
39
#include "vquic/vquic.h"
40
41
/* The last 3 #include files should be in this order */
42
#include "curl_printf.h"
43
#include "curl_memory.h"
44
#include "memdebug.h"
45
46
typedef enum {
47
  CF_HC_INIT,
48
  CF_HC_CONNECT,
49
  CF_HC_SUCCESS,
50
  CF_HC_FAILURE
51
} cf_hc_state;
52
53
struct cf_hc_baller {
54
  const char *name;
55
  struct Curl_cfilter *cf;
56
  CURLcode result;
57
  struct curltime started;
58
  int reply_ms;
59
  unsigned char transport;
60
  enum alpnid alpn_id;
61
  BIT(shutdown);
62
};
63
64
static void cf_hc_baller_reset(struct cf_hc_baller *b,
65
                               struct Curl_easy *data)
66
18.0k
{
67
18.0k
  if(b->cf) {
68
6.01k
    Curl_conn_cf_close(b->cf, data);
69
6.01k
    Curl_conn_cf_discard_chain(&b->cf, data);
70
6.01k
    b->cf = NULL;
71
6.01k
  }
72
18.0k
  b->result = CURLE_OK;
73
18.0k
  b->reply_ms = -1;
74
18.0k
}
75
76
static bool cf_hc_baller_is_active(struct cf_hc_baller *b)
77
4.94M
{
78
4.94M
  return b->cf && !b->result;
79
4.94M
}
80
81
static bool cf_hc_baller_has_started(struct cf_hc_baller *b)
82
0
{
83
0
  return !!b->cf;
84
0
}
85
86
static int cf_hc_baller_reply_ms(struct cf_hc_baller *b,
87
                                 struct Curl_easy *data)
88
0
{
89
0
  if(b->cf && (b->reply_ms < 0))
90
0
    b->cf->cft->query(b->cf, data, CF_QUERY_CONNECT_REPLY_MS,
91
0
                      &b->reply_ms, NULL);
92
0
  return b->reply_ms;
93
0
}
94
95
static bool cf_hc_baller_data_pending(struct cf_hc_baller *b,
96
                                      const struct Curl_easy *data)
97
0
{
98
0
  return b->cf && !b->result && b->cf->cft->has_data_pending(b->cf, data);
99
0
}
100
101
static bool cf_hc_baller_needs_flush(struct cf_hc_baller *b,
102
                                     struct Curl_easy *data)
103
2.47M
{
104
2.47M
  return b->cf && !b->result && Curl_conn_cf_needs_flush(b->cf, data);
105
2.47M
}
106
107
static CURLcode cf_hc_baller_cntrl(struct cf_hc_baller *b,
108
                                   struct Curl_easy *data,
109
                                   int event, int arg1, void *arg2)
110
6.01k
{
111
6.01k
  if(b->cf && !b->result)
112
554
    return Curl_conn_cf_cntrl(b->cf, data, FALSE, event, arg1, arg2);
113
5.45k
  return CURLE_OK;
114
6.01k
}
115
116
struct cf_hc_ctx {
117
  cf_hc_state state;
118
  struct curltime started;  /* when connect started */
119
  CURLcode result;          /* overall result */
120
  struct cf_hc_baller ballers[2];
121
  size_t baller_count;
122
  timediff_t soft_eyeballs_timeout_ms;
123
  timediff_t hard_eyeballs_timeout_ms;
124
};
125
126
static void cf_hc_baller_assign(struct cf_hc_baller *b,
127
                                enum alpnid alpn_id,
128
                                unsigned char def_transport)
129
6.01k
{
130
6.01k
  b->alpn_id = alpn_id;
131
6.01k
  b->transport = def_transport;
132
6.01k
  switch(b->alpn_id) {
133
0
  case ALPN_h3:
134
0
    b->name = "h3";
135
0
    b->transport = TRNSPRT_QUIC;
136
0
    break;
137
5.96k
  case ALPN_h2:
138
5.96k
    b->name = "h2";
139
5.96k
    break;
140
43
  case ALPN_h1:
141
43
    b->name = "h1";
142
43
    break;
143
0
  default:
144
0
    b->result = CURLE_FAILED_INIT;
145
0
    break;
146
6.01k
  }
147
6.01k
}
148
149
static void cf_hc_baller_init(struct cf_hc_baller *b,
150
                              struct Curl_cfilter *cf,
151
                              struct Curl_easy *data,
152
                              int transport)
153
6.01k
{
154
6.01k
  struct Curl_cfilter *save = cf->next;
155
156
6.01k
  cf->next = NULL;
157
6.01k
  b->started = curlx_now();
158
6.01k
  switch(b->alpn_id) {
159
0
  case ALPN_h3:
160
0
    transport = TRNSPRT_QUIC;
161
0
    break;
162
6.01k
  default:
163
6.01k
    break;
164
6.01k
  }
165
166
6.01k
  if(!b->result)
167
6.01k
    b->result = Curl_cf_setup_insert_after(cf, data, transport,
168
6.01k
                                           CURL_CF_SSL_ENABLE);
169
6.01k
  b->cf = cf->next;
170
6.01k
  cf->next = save;
171
6.01k
}
172
173
static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b,
174
                                     struct Curl_cfilter *cf,
175
                                     struct Curl_easy *data,
176
                                     bool *done)
177
2.47M
{
178
2.47M
  struct Curl_cfilter *save = cf->next;
179
180
2.47M
  cf->next = b->cf;
181
2.47M
  b->result = Curl_conn_cf_connect(cf->next, data, done);
182
2.47M
  b->cf = cf->next; /* it might mutate */
183
2.47M
  cf->next = save;
184
2.47M
  return b->result;
185
2.47M
}
186
187
static void cf_hc_reset(struct Curl_cfilter *cf, struct Curl_easy *data)
188
18.0k
{
189
18.0k
  struct cf_hc_ctx *ctx = cf->ctx;
190
18.0k
  size_t i;
191
192
18.0k
  if(ctx) {
193
36.0k
    for(i = 0; i < ctx->baller_count; ++i)
194
18.0k
      cf_hc_baller_reset(&ctx->ballers[i], data);
195
18.0k
    ctx->state = CF_HC_INIT;
196
18.0k
    ctx->result = CURLE_OK;
197
18.0k
    ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
198
18.0k
    ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 4;
199
18.0k
  }
200
18.0k
}
201
202
static CURLcode baller_connected(struct Curl_cfilter *cf,
203
                                 struct Curl_easy *data,
204
                                 struct cf_hc_baller *winner)
205
0
{
206
0
  struct cf_hc_ctx *ctx = cf->ctx;
207
0
  CURLcode result = CURLE_OK;
208
0
  int reply_ms;
209
0
  size_t i;
210
211
0
  DEBUGASSERT(winner->cf);
212
0
  for(i = 0; i < ctx->baller_count; ++i)
213
0
    if(winner != &ctx->ballers[i])
214
0
      cf_hc_baller_reset(&ctx->ballers[i], data);
215
216
0
  reply_ms = cf_hc_baller_reply_ms(winner, data);
217
0
  if(reply_ms >= 0)
218
0
    CURL_TRC_CF(data, cf, "connect+handshake %s: %dms, 1st data: %dms",
219
0
                winner->name, (int)curlx_timediff(curlx_now(),
220
0
                                                  winner->started), reply_ms);
221
0
  else
222
0
    CURL_TRC_CF(data, cf, "deferred handshake %s: %dms",
223
0
                winner->name, (int)curlx_timediff(curlx_now(),
224
0
                                                  winner->started));
225
226
  /* install the winning filter below this one. */
227
0
  cf->next = winner->cf;
228
0
  winner->cf = NULL;
229
230
0
#ifdef USE_NGHTTP2
231
0
  {
232
    /* Using nghttp2, we add the filter "below" us, so when the conn
233
     * closes, we tear it down for a fresh reconnect */
234
0
    const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
235
0
    if(alpn && !strcmp("h2", alpn)) {
236
0
      result = Curl_http2_switch_at(cf, data);
237
0
      if(result) {
238
0
        ctx->state = CF_HC_FAILURE;
239
0
        ctx->result = result;
240
0
        return result;
241
0
      }
242
0
    }
243
0
  }
244
0
#endif
245
246
0
  ctx->state = CF_HC_SUCCESS;
247
0
  cf->connected = TRUE;
248
0
  return result;
249
0
}
250
251
252
static bool time_to_start_next(struct Curl_cfilter *cf,
253
                               struct Curl_easy *data,
254
                               size_t idx, struct curltime now)
255
2.47M
{
256
2.47M
  struct cf_hc_ctx *ctx = cf->ctx;
257
2.47M
  timediff_t elapsed_ms;
258
2.47M
  size_t i;
259
260
2.47M
  if(idx >= ctx->baller_count)
261
2.47M
    return FALSE;
262
0
  if(cf_hc_baller_has_started(&ctx->ballers[idx]))
263
0
    return FALSE;
264
0
  for(i = 0; i < idx; i++) {
265
0
    if(!ctx->ballers[i].result)
266
0
      break;
267
0
  }
268
0
  if(i == idx) {
269
0
    CURL_TRC_CF(data, cf, "all previous attempts failed, starting %s",
270
0
                ctx->ballers[idx].name);
271
0
    return TRUE;
272
0
  }
273
0
  elapsed_ms = curlx_timediff(now, ctx->started);
274
0
  if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
275
0
    CURL_TRC_CF(data, cf, "hard timeout of %" FMT_TIMEDIFF_T "ms reached, "
276
0
                "starting %s",
277
0
                ctx->hard_eyeballs_timeout_ms, ctx->ballers[idx].name);
278
0
    return TRUE;
279
0
  }
280
281
0
  if((idx > 0) && (elapsed_ms >= ctx->soft_eyeballs_timeout_ms)) {
282
0
    if(cf_hc_baller_reply_ms(&ctx->ballers[idx - 1], data) < 0) {
283
0
      CURL_TRC_CF(data, cf, "soft timeout of %" FMT_TIMEDIFF_T "ms reached, "
284
0
                  "%s has not seen any data, starting %s",
285
0
                  ctx->soft_eyeballs_timeout_ms,
286
0
                  ctx->ballers[idx - 1].name, ctx->ballers[idx].name);
287
0
      return TRUE;
288
0
    }
289
    /* set the effective hard timeout again */
290
0
    Curl_expire(data, ctx->hard_eyeballs_timeout_ms - elapsed_ms,
291
0
                EXPIRE_ALPN_EYEBALLS);
292
0
  }
293
0
  return FALSE;
294
0
}
295
296
static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
297
                              struct Curl_easy *data,
298
                              bool *done)
299
2.47M
{
300
2.47M
  struct cf_hc_ctx *ctx = cf->ctx;
301
2.47M
  struct curltime now;
302
2.47M
  CURLcode result = CURLE_OK;
303
2.47M
  size_t i, failed_ballers;
304
305
2.47M
  if(cf->connected) {
306
0
    *done = TRUE;
307
0
    return CURLE_OK;
308
0
  }
309
310
2.47M
  *done = FALSE;
311
2.47M
  now = curlx_now();
312
2.47M
  switch(ctx->state) {
313
6.01k
  case CF_HC_INIT:
314
6.01k
    DEBUGASSERT(!cf->next);
315
12.0k
    for(i = 0; i < ctx->baller_count; i++)
316
6.01k
      DEBUGASSERT(!ctx->ballers[i].cf);
317
6.01k
    CURL_TRC_CF(data, cf, "connect, init");
318
6.01k
    ctx->started = now;
319
6.01k
    cf_hc_baller_init(&ctx->ballers[0], cf, data, ctx->ballers[0].transport);
320
6.01k
    if(ctx->baller_count > 1) {
321
0
      Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
322
0
      CURL_TRC_CF(data, cf, "set next attempt to start in %" FMT_TIMEDIFF_T
323
0
                  "ms", ctx->soft_eyeballs_timeout_ms);
324
0
    }
325
6.01k
    ctx->state = CF_HC_CONNECT;
326
6.01k
    FALLTHROUGH();
327
328
2.47M
  case CF_HC_CONNECT:
329
2.47M
    if(cf_hc_baller_is_active(&ctx->ballers[0])) {
330
2.47M
      result = cf_hc_baller_connect(&ctx->ballers[0], cf, data, done);
331
2.47M
      if(!result && *done) {
332
0
        result = baller_connected(cf, data, &ctx->ballers[0]);
333
0
        goto out;
334
0
      }
335
2.47M
    }
336
337
2.47M
    if(time_to_start_next(cf, data, 1, now)) {
338
0
      cf_hc_baller_init(&ctx->ballers[1], cf, data, ctx->ballers[1].transport);
339
0
    }
340
341
2.47M
    if((ctx->baller_count > 1) && cf_hc_baller_is_active(&ctx->ballers[1])) {
342
0
      CURL_TRC_CF(data, cf, "connect, check %s", ctx->ballers[1].name);
343
0
      result = cf_hc_baller_connect(&ctx->ballers[1], cf, data, done);
344
0
      if(!result && *done) {
345
0
        result = baller_connected(cf, data, &ctx->ballers[1]);
346
0
        goto out;
347
0
      }
348
0
    }
349
350
2.47M
    failed_ballers = 0;
351
4.95M
    for(i = 0; i < ctx->baller_count; i++) {
352
2.47M
      if(ctx->ballers[i].result)
353
5.45k
        ++failed_ballers;
354
2.47M
    }
355
356
2.47M
    if(failed_ballers == ctx->baller_count) {
357
      /* all have failed. we give up */
358
5.45k
      CURL_TRC_CF(data, cf, "connect, all attempts failed");
359
5.45k
      for(i = 0; i < ctx->baller_count; i++) {
360
5.45k
        if(ctx->ballers[i].result) {
361
5.45k
          result = ctx->ballers[i].result;
362
5.45k
          break;
363
5.45k
        }
364
5.45k
      }
365
5.45k
      ctx->state = CF_HC_FAILURE;
366
5.45k
      goto out;
367
5.45k
    }
368
2.47M
    result = CURLE_OK;
369
2.47M
    *done = FALSE;
370
2.47M
    break;
371
372
0
  case CF_HC_FAILURE:
373
0
    result = ctx->result;
374
0
    cf->connected = FALSE;
375
0
    *done = FALSE;
376
0
    break;
377
378
0
  case CF_HC_SUCCESS:
379
0
    result = CURLE_OK;
380
0
    cf->connected = TRUE;
381
0
    *done = TRUE;
382
0
    break;
383
2.47M
  }
384
385
2.47M
out:
386
2.47M
  CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
387
2.47M
  return result;
388
2.47M
}
389
390
static CURLcode cf_hc_shutdown(struct Curl_cfilter *cf,
391
                               struct Curl_easy *data, bool *done)
392
0
{
393
0
  struct cf_hc_ctx *ctx = cf->ctx;
394
0
  size_t i;
395
0
  CURLcode result = CURLE_OK;
396
397
0
  DEBUGASSERT(data);
398
0
  if(cf->connected) {
399
0
    *done = TRUE;
400
0
    return CURLE_OK;
401
0
  }
402
403
  /* shutdown all ballers that have not done so already. If one fails,
404
   * continue shutting down others until all are shutdown. */
405
0
  for(i = 0; i < ctx->baller_count; i++) {
406
0
    struct cf_hc_baller *b = &ctx->ballers[i];
407
0
    bool bdone = FALSE;
408
0
    if(!cf_hc_baller_is_active(b) || b->shutdown)
409
0
      continue;
410
0
    b->result = b->cf->cft->do_shutdown(b->cf, data, &bdone);
411
0
    if(b->result || bdone)
412
0
      b->shutdown = TRUE; /* treat a failed shutdown as done */
413
0
  }
414
415
0
  *done = TRUE;
416
0
  for(i = 0; i < ctx->baller_count; i++) {
417
0
    if(!ctx->ballers[i].shutdown)
418
0
      *done = FALSE;
419
0
  }
420
0
  if(*done) {
421
0
    for(i = 0; i < ctx->baller_count; i++) {
422
0
      if(ctx->ballers[i].result)
423
0
        result = ctx->ballers[i].result;
424
0
    }
425
0
  }
426
0
  CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", result, *done);
427
0
  return result;
428
0
}
429
430
static CURLcode cf_hc_adjust_pollset(struct Curl_cfilter *cf,
431
                                     struct Curl_easy *data,
432
                                     struct easy_pollset *ps)
433
2.47M
{
434
2.47M
  CURLcode result = CURLE_OK;
435
2.47M
  if(!cf->connected) {
436
2.47M
    struct cf_hc_ctx *ctx = cf->ctx;
437
2.47M
    size_t i;
438
439
4.94M
    for(i = 0; (i < ctx->baller_count) && !result; i++) {
440
2.47M
      struct cf_hc_baller *b = &ctx->ballers[i];
441
2.47M
      if(!cf_hc_baller_is_active(b))
442
0
        continue;
443
2.47M
      result = Curl_conn_cf_adjust_pollset(b->cf, data, ps);
444
2.47M
    }
445
2.47M
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %d socks", result, ps->n);
446
2.47M
  }
447
2.47M
  return result;
448
2.47M
}
449
450
static bool cf_hc_data_pending(struct Curl_cfilter *cf,
451
                               const struct Curl_easy *data)
452
0
{
453
0
  struct cf_hc_ctx *ctx = cf->ctx;
454
0
  size_t i;
455
456
0
  if(cf->connected)
457
0
    return cf->next->cft->has_data_pending(cf->next, data);
458
459
0
  for(i = 0; i < ctx->baller_count; i++)
460
0
    if(cf_hc_baller_data_pending(&ctx->ballers[i], data))
461
0
      return TRUE;
462
0
  return FALSE;
463
0
}
464
465
static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
466
                                              struct Curl_easy *data,
467
                                              int query)
468
10.9k
{
469
10.9k
  struct cf_hc_ctx *ctx = cf->ctx;
470
10.9k
  struct curltime t, tmax;
471
10.9k
  size_t i;
472
473
10.9k
  memset(&tmax, 0, sizeof(tmax));
474
21.8k
  for(i = 0; i < ctx->baller_count; i++) {
475
10.9k
    struct Curl_cfilter *cfb = ctx->ballers[i].cf;
476
10.9k
    memset(&t, 0, sizeof(t));
477
10.9k
    if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
478
9.19k
      if((t.tv_sec || t.tv_usec) && curlx_timediff_us(t, tmax) > 0)
479
4.90k
        tmax = t;
480
9.19k
    }
481
10.9k
  }
482
10.9k
  return tmax;
483
10.9k
}
484
485
static CURLcode cf_hc_query(struct Curl_cfilter *cf,
486
                            struct Curl_easy *data,
487
                            int query, int *pres1, void *pres2)
488
4.95M
{
489
4.95M
  struct cf_hc_ctx *ctx = cf->ctx;
490
4.95M
  size_t i;
491
492
4.95M
  if(!cf->connected) {
493
4.95M
    switch(query) {
494
5.45k
    case CF_QUERY_TIMER_CONNECT: {
495
5.45k
      struct curltime *when = pres2;
496
5.45k
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
497
5.45k
      return CURLE_OK;
498
0
    }
499
5.45k
    case CF_QUERY_TIMER_APPCONNECT: {
500
5.45k
      struct curltime *when = pres2;
501
5.45k
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
502
5.45k
      return CURLE_OK;
503
0
    }
504
2.47M
    case CF_QUERY_NEED_FLUSH: {
505
4.95M
      for(i = 0; i < ctx->baller_count; i++)
506
2.47M
        if(cf_hc_baller_needs_flush(&ctx->ballers[i], data)) {
507
0
          *pres1 = TRUE;
508
0
          return CURLE_OK;
509
0
        }
510
2.47M
      break;
511
2.47M
    }
512
2.47M
    default:
513
2.47M
      break;
514
4.95M
    }
515
4.95M
  }
516
4.94M
  return cf->next ?
517
85
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
518
4.94M
    CURLE_UNKNOWN_OPTION;
519
4.95M
}
520
521
static CURLcode cf_hc_cntrl(struct Curl_cfilter *cf,
522
                            struct Curl_easy *data,
523
                            int event, int arg1, void *arg2)
524
6.01k
{
525
6.01k
  struct cf_hc_ctx *ctx = cf->ctx;
526
6.01k
  CURLcode result = CURLE_OK;
527
6.01k
  size_t i;
528
529
6.01k
  if(!cf->connected) {
530
12.0k
    for(i = 0; i < ctx->baller_count; i++) {
531
6.01k
      result = cf_hc_baller_cntrl(&ctx->ballers[i], data, event, arg1, arg2);
532
6.01k
      if(result && (result != CURLE_AGAIN))
533
0
        goto out;
534
6.01k
    }
535
6.01k
    result = CURLE_OK;
536
6.01k
  }
537
6.01k
out:
538
6.01k
  return result;
539
6.01k
}
540
541
static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data)
542
6.01k
{
543
6.01k
  CURL_TRC_CF(data, cf, "close");
544
6.01k
  cf_hc_reset(cf, data);
545
6.01k
  cf->connected = FALSE;
546
547
6.01k
  if(cf->next) {
548
0
    cf->next->cft->do_close(cf->next, data);
549
0
    Curl_conn_cf_discard_chain(&cf->next, data);
550
0
  }
551
6.01k
}
552
553
static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
554
6.01k
{
555
6.01k
  struct cf_hc_ctx *ctx = cf->ctx;
556
557
6.01k
  (void)data;
558
6.01k
  CURL_TRC_CF(data, cf, "destroy");
559
6.01k
  cf_hc_reset(cf, data);
560
6.01k
  Curl_safefree(ctx);
561
6.01k
}
562
563
struct Curl_cftype Curl_cft_http_connect = {
564
  "HTTPS-CONNECT",
565
  0,
566
  CURL_LOG_LVL_NONE,
567
  cf_hc_destroy,
568
  cf_hc_connect,
569
  cf_hc_close,
570
  cf_hc_shutdown,
571
  cf_hc_adjust_pollset,
572
  cf_hc_data_pending,
573
  Curl_cf_def_send,
574
  Curl_cf_def_recv,
575
  cf_hc_cntrl,
576
  Curl_cf_def_conn_is_alive,
577
  Curl_cf_def_conn_keep_alive,
578
  cf_hc_query,
579
};
580
581
static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
582
                             struct Curl_easy *data,
583
                             enum alpnid *alpnids, size_t alpn_count,
584
                             unsigned char def_transport)
585
6.01k
{
586
6.01k
  struct Curl_cfilter *cf = NULL;
587
6.01k
  struct cf_hc_ctx *ctx;
588
6.01k
  CURLcode result = CURLE_OK;
589
6.01k
  size_t i;
590
591
6.01k
  DEBUGASSERT(alpnids);
592
6.01k
  DEBUGASSERT(alpn_count);
593
6.01k
  DEBUGASSERT(alpn_count <= CURL_ARRAYSIZE(ctx->ballers));
594
6.01k
  if(!alpn_count || (alpn_count > CURL_ARRAYSIZE(ctx->ballers))) {
595
0
    failf(data, "https-connect filter create with unsupported %zu ALPN ids",
596
0
          alpn_count);
597
0
    return CURLE_FAILED_INIT;
598
0
  }
599
600
6.01k
  ctx = calloc(1, sizeof(*ctx));
601
6.01k
  if(!ctx) {
602
0
    result = CURLE_OUT_OF_MEMORY;
603
0
    goto out;
604
0
  }
605
12.0k
  for(i = 0; i < alpn_count; ++i)
606
6.01k
    cf_hc_baller_assign(&ctx->ballers[i], alpnids[i], def_transport);
607
12.0k
  for(; i < CURL_ARRAYSIZE(ctx->ballers); ++i)
608
6.01k
    ctx->ballers[i].alpn_id = ALPN_none;
609
6.01k
  ctx->baller_count = alpn_count;
610
611
6.01k
  result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
612
6.01k
  if(result)
613
0
    goto out;
614
6.01k
  ctx = NULL;
615
6.01k
  cf_hc_reset(cf, data);
616
617
6.01k
out:
618
6.01k
  *pcf = result ? NULL : cf;
619
6.01k
  free(ctx);
620
6.01k
  return result;
621
6.01k
}
622
623
static CURLcode cf_http_connect_add(struct Curl_easy *data,
624
                                    struct connectdata *conn,
625
                                    int sockindex,
626
                                    enum alpnid *alpn_ids, size_t alpn_count,
627
                                    unsigned char def_transport)
628
6.01k
{
629
6.01k
  struct Curl_cfilter *cf;
630
6.01k
  CURLcode result = CURLE_OK;
631
632
6.01k
  DEBUGASSERT(data);
633
6.01k
  result = cf_hc_create(&cf, data, alpn_ids, alpn_count, def_transport);
634
6.01k
  if(result)
635
0
    goto out;
636
6.01k
  Curl_conn_cf_add(data, conn, sockindex, cf);
637
6.01k
out:
638
6.01k
  return result;
639
6.01k
}
640
641
static bool cf_https_alpns_contain(enum alpnid id,
642
                                   enum alpnid *list, size_t len)
643
6.01k
{
644
6.01k
  size_t i;
645
6.01k
  for(i = 0; i < len; ++i) {
646
0
    if(id == list[i])
647
0
      return TRUE;
648
0
  }
649
6.01k
  return FALSE;
650
6.01k
}
651
652
CURLcode Curl_cf_https_setup(struct Curl_easy *data,
653
                             struct connectdata *conn,
654
                             int sockindex)
655
6.03k
{
656
6.03k
  enum alpnid alpn_ids[2];
657
6.03k
  size_t alpn_count = 0;
658
6.03k
  CURLcode result = CURLE_OK;
659
6.03k
  struct Curl_cfilter cf_fake, *cf = NULL;
660
661
6.03k
  (void)sockindex;
662
  /* we want to log for the filter before we create it, fake it. */
663
6.03k
  memset(&cf_fake, 0, sizeof(cf_fake));
664
6.03k
  cf_fake.cft = &Curl_cft_http_connect;
665
6.03k
  cf = &cf_fake;
666
667
6.03k
  if(conn->bits.tls_enable_alpn) {
668
#ifdef USE_HTTPSRR
669
    /* Is there an HTTPSRR use its ALPNs here.
670
     * We are here after having selected a connection to a host+port and
671
     * can no longer change that. Any HTTPSRR advice for other hosts and ports
672
     * we need to ignore. */
673
    struct Curl_dns_entry *dns = data->state.dns[sockindex];
674
    struct Curl_https_rrinfo *rr = dns ? dns->hinfo : NULL;
675
    if(rr && !rr->no_def_alpn &&  /* ALPNs are defaults */
676
       (!rr->target ||      /* for same host */
677
        !rr->target[0] ||
678
        (rr->target[0] == '.' &&
679
         !rr->target[1])) &&
680
       (rr->port < 0 ||    /* for same port */
681
        rr->port == conn->remote_port)) {
682
      size_t i;
683
      for(i = 0; i < CURL_ARRAYSIZE(rr->alpns) &&
684
                 alpn_count < CURL_ARRAYSIZE(alpn_ids); ++i) {
685
        enum alpnid alpn = rr->alpns[i];
686
        if(cf_https_alpns_contain(alpn, alpn_ids, alpn_count))
687
          continue;
688
        switch(alpn) {
689
        case ALPN_h3:
690
          if(Curl_conn_may_http3(data, conn, conn->transport_wanted))
691
            break;  /* not possible */
692
          if(data->state.http_neg.allowed & CURL_HTTP_V3x) {
693
            CURL_TRC_CF(data, cf, "adding h3 via HTTPS-RR");
694
            alpn_ids[alpn_count++] = alpn;
695
          }
696
          break;
697
        case ALPN_h2:
698
          if(data->state.http_neg.allowed & CURL_HTTP_V2x) {
699
            CURL_TRC_CF(data, cf, "adding h2 via HTTPS-RR");
700
            alpn_ids[alpn_count++] = alpn;
701
          }
702
          break;
703
        case ALPN_h1:
704
          if(data->state.http_neg.allowed & CURL_HTTP_V1x) {
705
            CURL_TRC_CF(data, cf, "adding h1 via HTTPS-RR");
706
            alpn_ids[alpn_count++] = alpn;
707
          }
708
          break;
709
        default: /* ignore */
710
          break;
711
        }
712
      }
713
    }
714
#endif
715
716
6.01k
    if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
717
6.01k
       (data->state.http_neg.wanted & CURL_HTTP_V3x) &&
718
6.01k
       !cf_https_alpns_contain(ALPN_h3, alpn_ids, alpn_count)) {
719
0
      result = Curl_conn_may_http3(data, conn, conn->transport_wanted);
720
0
      if(!result) {
721
0
        CURL_TRC_CF(data, cf, "adding wanted h3");
722
0
        alpn_ids[alpn_count++] = ALPN_h3;
723
0
      }
724
0
      else if(data->state.http_neg.wanted == CURL_HTTP_V3x)
725
0
        goto out; /* only h3 allowed, not possible, error out */
726
0
    }
727
6.01k
    if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
728
6.01k
       (data->state.http_neg.wanted & CURL_HTTP_V2x) &&
729
6.01k
       !cf_https_alpns_contain(ALPN_h2, alpn_ids, alpn_count)) {
730
5.96k
      CURL_TRC_CF(data, cf, "adding wanted h2");
731
5.96k
      alpn_ids[alpn_count++] = ALPN_h2;
732
5.96k
    }
733
43
    else if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
734
43
            (data->state.http_neg.wanted & CURL_HTTP_V1x) &&
735
43
            !cf_https_alpns_contain(ALPN_h1, alpn_ids, alpn_count)) {
736
43
      CURL_TRC_CF(data, cf, "adding wanted h1");
737
43
      alpn_ids[alpn_count++] = ALPN_h1;
738
43
    }
739
6.01k
  }
740
741
  /* If we identified ALPNs to use, install our filter. Otherwise,
742
   * install nothing, so our call will use a default connect setup. */
743
6.03k
  if(alpn_count) {
744
6.01k
    result = cf_http_connect_add(data, conn, sockindex,
745
6.01k
                                 alpn_ids, alpn_count,
746
6.01k
                                 conn->transport_wanted);
747
6.01k
  }
748
749
6.03k
out:
750
6.03k
  return result;
751
6.03k
}
752
753
#endif /* !CURL_DISABLE_HTTP */