Coverage Report

Created: 2026-02-14 09:00

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