Coverage Report

Created: 2025-07-11 06:33

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