Coverage Report

Created: 2025-08-26 07:08

/src/PROJ/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
0
{
67
0
  if(b->cf) {
68
0
    Curl_conn_cf_close(b->cf, data);
69
0
    Curl_conn_cf_discard_chain(&b->cf, data);
70
0
    b->cf = NULL;
71
0
  }
72
0
  b->result = CURLE_OK;
73
0
  b->reply_ms = -1;
74
0
}
75
76
static bool cf_hc_baller_is_active(struct cf_hc_baller *b)
77
0
{
78
0
  return b->cf && !b->result;
79
0
}
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
0
{
104
0
  return b->cf && !b->result && Curl_conn_cf_needs_flush(b->cf, data);
105
0
}
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
0
{
111
0
  if(b->cf && !b->result)
112
0
    return Curl_conn_cf_cntrl(b->cf, data, FALSE, event, arg1, arg2);
113
0
  return CURLE_OK;
114
0
}
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
0
{
130
0
  b->alpn_id = alpn_id;
131
0
  b->transport = def_transport;
132
0
  switch(b->alpn_id) {
133
0
  case ALPN_h3:
134
0
    b->name = "h3";
135
0
    b->transport = TRNSPRT_QUIC;
136
0
    break;
137
0
  case ALPN_h2:
138
0
    b->name = "h2";
139
0
    break;
140
0
  case ALPN_h1:
141
0
    b->name = "h1";
142
0
    break;
143
0
  default:
144
0
    b->result = CURLE_FAILED_INIT;
145
0
    break;
146
0
  }
147
0
}
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
0
{
154
0
  struct Curl_cfilter *save = cf->next;
155
156
0
  cf->next = NULL;
157
0
  b->started = curlx_now();
158
0
  switch(b->alpn_id) {
159
0
  case ALPN_h3:
160
0
    transport = TRNSPRT_QUIC;
161
0
    break;
162
0
  default:
163
0
    break;
164
0
  }
165
166
0
  if(!b->result)
167
0
    b->result = Curl_cf_setup_insert_after(cf, data, transport,
168
0
                                           CURL_CF_SSL_ENABLE);
169
0
  b->cf = cf->next;
170
0
  cf->next = save;
171
0
}
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
0
{
178
0
  struct Curl_cfilter *save = cf->next;
179
180
0
  cf->next = b->cf;
181
0
  b->result = Curl_conn_cf_connect(cf->next, data, done);
182
0
  b->cf = cf->next; /* it might mutate */
183
0
  cf->next = save;
184
0
  return b->result;
185
0
}
186
187
static void cf_hc_reset(struct Curl_cfilter *cf, struct Curl_easy *data)
188
0
{
189
0
  struct cf_hc_ctx *ctx = cf->ctx;
190
0
  size_t i;
191
192
0
  if(ctx) {
193
0
    for(i = 0; i < ctx->baller_count; ++i)
194
0
      cf_hc_baller_reset(&ctx->ballers[i], data);
195
0
    ctx->state = CF_HC_INIT;
196
0
    ctx->result = CURLE_OK;
197
0
    ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
198
0
    ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 4;
199
0
  }
200
0
}
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
#ifdef USE_NGHTTP2
231
  {
232
    /* Using nghttp2, we add the filter "below" us, so when the conn
233
     * closes, we tear it down for a fresh reconnect */
234
    const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
235
    if(alpn && !strcmp("h2", alpn)) {
236
      result = Curl_http2_switch_at(cf, data);
237
      if(result) {
238
        ctx->state = CF_HC_FAILURE;
239
        ctx->result = result;
240
        return result;
241
      }
242
    }
243
  }
244
#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
0
{
256
0
  struct cf_hc_ctx *ctx = cf->ctx;
257
0
  timediff_t elapsed_ms;
258
0
  size_t i;
259
260
0
  if(idx >= ctx->baller_count)
261
0
    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
0
{
300
0
  struct cf_hc_ctx *ctx = cf->ctx;
301
0
  struct curltime now;
302
0
  CURLcode result = CURLE_OK;
303
0
  size_t i, failed_ballers;
304
305
0
  if(cf->connected) {
306
0
    *done = TRUE;
307
0
    return CURLE_OK;
308
0
  }
309
310
0
  *done = FALSE;
311
0
  now = curlx_now();
312
0
  switch(ctx->state) {
313
0
  case CF_HC_INIT:
314
0
    DEBUGASSERT(!cf->next);
315
0
    for(i = 0; i < ctx->baller_count; i++)
316
0
      DEBUGASSERT(!ctx->ballers[i].cf);
317
0
    CURL_TRC_CF(data, cf, "connect, init");
318
0
    ctx->started = now;
319
0
    cf_hc_baller_init(&ctx->ballers[0], cf, data, ctx->ballers[0].transport);
320
0
    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
0
    ctx->state = CF_HC_CONNECT;
326
0
    FALLTHROUGH();
327
328
0
  case CF_HC_CONNECT:
329
0
    if(cf_hc_baller_is_active(&ctx->ballers[0])) {
330
0
      result = cf_hc_baller_connect(&ctx->ballers[0], cf, data, done);
331
0
      if(!result && *done) {
332
0
        result = baller_connected(cf, data, &ctx->ballers[0]);
333
0
        goto out;
334
0
      }
335
0
    }
336
337
0
    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
0
    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
0
    failed_ballers = 0;
351
0
    for(i = 0; i < ctx->baller_count; i++) {
352
0
      if(ctx->ballers[i].result)
353
0
        ++failed_ballers;
354
0
    }
355
356
0
    if(failed_ballers == ctx->baller_count) {
357
      /* all have failed. we give up */
358
0
      CURL_TRC_CF(data, cf, "connect, all attempts failed");
359
0
      for(i = 0; i < ctx->baller_count; i++) {
360
0
        if(ctx->ballers[i].result) {
361
0
          result = ctx->ballers[i].result;
362
0
          break;
363
0
        }
364
0
      }
365
0
      ctx->state = CF_HC_FAILURE;
366
0
      goto out;
367
0
    }
368
0
    result = CURLE_OK;
369
0
    *done = FALSE;
370
0
    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
0
  }
384
385
0
out:
386
0
  CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
387
0
  return result;
388
0
}
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
0
{
434
0
  CURLcode result = CURLE_OK;
435
0
  if(!cf->connected) {
436
0
    struct cf_hc_ctx *ctx = cf->ctx;
437
0
    size_t i;
438
439
0
    for(i = 0; (i < ctx->baller_count) && !result; i++) {
440
0
      struct cf_hc_baller *b = &ctx->ballers[i];
441
0
      if(!cf_hc_baller_is_active(b))
442
0
        continue;
443
0
      result = Curl_conn_cf_adjust_pollset(b->cf, data, ps);
444
0
    }
445
0
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %d socks", result, ps->n);
446
0
  }
447
0
  return result;
448
0
}
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
0
{
469
0
  struct cf_hc_ctx *ctx = cf->ctx;
470
0
  struct curltime t, tmax;
471
0
  size_t i;
472
473
0
  memset(&tmax, 0, sizeof(tmax));
474
0
  for(i = 0; i < ctx->baller_count; i++) {
475
0
    struct Curl_cfilter *cfb = ctx->ballers[i].cf;
476
0
    memset(&t, 0, sizeof(t));
477
0
    if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
478
0
      if((t.tv_sec || t.tv_usec) && curlx_timediff_us(t, tmax) > 0)
479
0
        tmax = t;
480
0
    }
481
0
  }
482
0
  return tmax;
483
0
}
484
485
static CURLcode cf_hc_query(struct Curl_cfilter *cf,
486
                            struct Curl_easy *data,
487
                            int query, int *pres1, void *pres2)
488
0
{
489
0
  struct cf_hc_ctx *ctx = cf->ctx;
490
0
  size_t i;
491
492
0
  if(!cf->connected) {
493
0
    switch(query) {
494
0
    case CF_QUERY_TIMER_CONNECT: {
495
0
      struct curltime *when = pres2;
496
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
497
0
      return CURLE_OK;
498
0
    }
499
0
    case CF_QUERY_TIMER_APPCONNECT: {
500
0
      struct curltime *when = pres2;
501
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
502
0
      return CURLE_OK;
503
0
    }
504
0
    case CF_QUERY_NEED_FLUSH: {
505
0
      for(i = 0; i < ctx->baller_count; i++)
506
0
        if(cf_hc_baller_needs_flush(&ctx->ballers[i], data)) {
507
0
          *pres1 = TRUE;
508
0
          return CURLE_OK;
509
0
        }
510
0
      break;
511
0
    }
512
0
    default:
513
0
      break;
514
0
    }
515
0
  }
516
0
  return cf->next ?
517
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
518
0
    CURLE_UNKNOWN_OPTION;
519
0
}
520
521
static CURLcode cf_hc_cntrl(struct Curl_cfilter *cf,
522
                            struct Curl_easy *data,
523
                            int event, int arg1, void *arg2)
524
0
{
525
0
  struct cf_hc_ctx *ctx = cf->ctx;
526
0
  CURLcode result = CURLE_OK;
527
0
  size_t i;
528
529
0
  if(!cf->connected) {
530
0
    for(i = 0; i < ctx->baller_count; i++) {
531
0
      result = cf_hc_baller_cntrl(&ctx->ballers[i], data, event, arg1, arg2);
532
0
      if(result && (result != CURLE_AGAIN))
533
0
        goto out;
534
0
    }
535
0
    result = CURLE_OK;
536
0
  }
537
0
out:
538
0
  return result;
539
0
}
540
541
static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data)
542
0
{
543
0
  CURL_TRC_CF(data, cf, "close");
544
0
  cf_hc_reset(cf, data);
545
0
  cf->connected = FALSE;
546
547
0
  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
0
}
552
553
static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
554
0
{
555
0
  struct cf_hc_ctx *ctx = cf->ctx;
556
557
0
  (void)data;
558
0
  CURL_TRC_CF(data, cf, "destroy");
559
0
  cf_hc_reset(cf, data);
560
0
  Curl_safefree(ctx);
561
0
}
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
0
{
586
0
  struct Curl_cfilter *cf = NULL;
587
0
  struct cf_hc_ctx *ctx;
588
0
  CURLcode result = CURLE_OK;
589
0
  size_t i;
590
591
0
  DEBUGASSERT(alpnids);
592
0
  DEBUGASSERT(alpn_count);
593
0
  DEBUGASSERT(alpn_count <= CURL_ARRAYSIZE(ctx->ballers));
594
0
  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
0
  ctx = calloc(1, sizeof(*ctx));
601
0
  if(!ctx) {
602
0
    result = CURLE_OUT_OF_MEMORY;
603
0
    goto out;
604
0
  }
605
0
  for(i = 0; i < alpn_count; ++i)
606
0
    cf_hc_baller_assign(&ctx->ballers[i], alpnids[i], def_transport);
607
0
  for(; i < CURL_ARRAYSIZE(ctx->ballers); ++i)
608
0
    ctx->ballers[i].alpn_id = ALPN_none;
609
0
  ctx->baller_count = alpn_count;
610
611
0
  result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
612
0
  if(result)
613
0
    goto out;
614
0
  ctx = NULL;
615
0
  cf_hc_reset(cf, data);
616
617
0
out:
618
0
  *pcf = result ? NULL : cf;
619
0
  free(ctx);
620
0
  return result;
621
0
}
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
0
{
629
0
  struct Curl_cfilter *cf;
630
0
  CURLcode result = CURLE_OK;
631
632
0
  DEBUGASSERT(data);
633
0
  result = cf_hc_create(&cf, data, alpn_ids, alpn_count, def_transport);
634
0
  if(result)
635
0
    goto out;
636
0
  Curl_conn_cf_add(data, conn, sockindex, cf);
637
0
out:
638
0
  return result;
639
0
}
640
641
static bool cf_https_alpns_contain(enum alpnid id,
642
                                   enum alpnid *list, size_t len)
643
0
{
644
0
  size_t i;
645
0
  for(i = 0; i < len; ++i) {
646
0
    if(id == list[i])
647
0
      return TRUE;
648
0
  }
649
0
  return FALSE;
650
0
}
651
652
CURLcode Curl_cf_https_setup(struct Curl_easy *data,
653
                             struct connectdata *conn,
654
                             int sockindex)
655
0
{
656
0
  enum alpnid alpn_ids[2];
657
0
  size_t alpn_count = 0;
658
0
  CURLcode result = CURLE_OK;
659
0
  struct Curl_cfilter cf_fake, *cf = NULL;
660
661
0
  (void)sockindex;
662
  /* we want to log for the filter before we create it, fake it. */
663
0
  memset(&cf_fake, 0, sizeof(cf_fake));
664
0
  cf_fake.cft = &Curl_cft_http_connect;
665
0
  cf = &cf_fake;
666
667
0
  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
0
    if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
717
0
       (data->state.http_neg.wanted & CURL_HTTP_V3x) &&
718
0
       !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
0
    if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
728
0
       (data->state.http_neg.wanted & CURL_HTTP_V2x) &&
729
0
       !cf_https_alpns_contain(ALPN_h2, alpn_ids, alpn_count)) {
730
0
      CURL_TRC_CF(data, cf, "adding wanted h2");
731
0
      alpn_ids[alpn_count++] = ALPN_h2;
732
0
    }
733
0
    else if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
734
0
            (data->state.http_neg.wanted & CURL_HTTP_V1x) &&
735
0
            !cf_https_alpns_contain(ALPN_h1, alpn_ids, alpn_count)) {
736
0
      CURL_TRC_CF(data, cf, "adding wanted h1");
737
0
      alpn_ids[alpn_count++] = ALPN_h1;
738
0
    }
739
0
  }
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
0
  if(alpn_count) {
744
0
    result = cf_http_connect_add(data, conn, sockindex,
745
0
                                 alpn_ids, alpn_count,
746
0
                                 conn->transport_wanted);
747
0
  }
748
749
0
out:
750
0
  return result;
751
0
}
752
753
#endif /* !CURL_DISABLE_HTTP */