Coverage Report

Created: 2024-05-04 12:45

/proc/self/cwd/external/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) && !defined(USE_HYPER)
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 "multiif.h"
35
#include "cf-https-connect.h"
36
#include "http2.h"
37
#include "vquic/vquic.h"
38
39
/* The last 3 #include files should be in this order */
40
#include "curl_printf.h"
41
#include "curl_memory.h"
42
#include "memdebug.h"
43
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
  bool enabled;
59
};
60
61
static void cf_hc_baller_reset(struct cf_hc_baller *b,
62
                               struct Curl_easy *data)
63
0
{
64
0
  if(b->cf) {
65
0
    Curl_conn_cf_close(b->cf, data);
66
0
    Curl_conn_cf_discard_chain(&b->cf, data);
67
0
    b->cf = NULL;
68
0
  }
69
0
  b->result = CURLE_OK;
70
0
  b->reply_ms = -1;
71
0
}
72
73
static bool cf_hc_baller_is_active(struct cf_hc_baller *b)
74
0
{
75
0
  return b->enabled && b->cf && !b->result;
76
0
}
77
78
static bool cf_hc_baller_has_started(struct cf_hc_baller *b)
79
0
{
80
0
  return !!b->cf;
81
0
}
82
83
static int cf_hc_baller_reply_ms(struct cf_hc_baller *b,
84
                                 struct Curl_easy *data)
85
0
{
86
0
  if(b->reply_ms < 0)
87
0
    b->cf->cft->query(b->cf, data, CF_QUERY_CONNECT_REPLY_MS,
88
0
                      &b->reply_ms, NULL);
89
0
  return b->reply_ms;
90
0
}
91
92
static bool cf_hc_baller_data_pending(struct cf_hc_baller *b,
93
                                      const struct Curl_easy *data)
94
0
{
95
0
  return b->cf && !b->result && b->cf->cft->has_data_pending(b->cf, data);
96
0
}
97
98
struct cf_hc_ctx {
99
  cf_hc_state state;
100
  const struct Curl_dns_entry *remotehost;
101
  struct curltime started;  /* when connect started */
102
  CURLcode result;          /* overall result */
103
  struct cf_hc_baller h3_baller;
104
  struct cf_hc_baller h21_baller;
105
  int soft_eyeballs_timeout_ms;
106
  int hard_eyeballs_timeout_ms;
107
};
108
109
static void cf_hc_baller_init(struct cf_hc_baller *b,
110
                              struct Curl_cfilter *cf,
111
                              struct Curl_easy *data,
112
                              const char *name,
113
                              int transport)
114
0
{
115
0
  struct cf_hc_ctx *ctx = cf->ctx;
116
0
  struct Curl_cfilter *save = cf->next;
117
118
0
  b->name = name;
119
0
  cf->next = NULL;
120
0
  b->started = Curl_now();
121
0
  b->result = Curl_cf_setup_insert_after(cf, data, ctx->remotehost,
122
0
                                         transport, CURL_CF_SSL_ENABLE);
123
0
  b->cf = cf->next;
124
0
  cf->next = save;
125
0
}
126
127
static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b,
128
                                     struct Curl_cfilter *cf,
129
                                     struct Curl_easy *data,
130
                                     bool *done)
131
0
{
132
0
  struct Curl_cfilter *save = cf->next;
133
134
0
  cf->next = b->cf;
135
0
  b->result = Curl_conn_cf_connect(cf->next, data, FALSE, done);
136
0
  b->cf = cf->next; /* it might mutate */
137
0
  cf->next = save;
138
0
  return b->result;
139
0
}
140
141
static void cf_hc_reset(struct Curl_cfilter *cf, struct Curl_easy *data)
142
0
{
143
0
  struct cf_hc_ctx *ctx = cf->ctx;
144
145
0
  if(ctx) {
146
0
    cf_hc_baller_reset(&ctx->h3_baller, data);
147
0
    cf_hc_baller_reset(&ctx->h21_baller, data);
148
0
    ctx->state = CF_HC_INIT;
149
0
    ctx->result = CURLE_OK;
150
0
    ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
151
0
    ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 2;
152
0
  }
153
0
}
154
155
static CURLcode baller_connected(struct Curl_cfilter *cf,
156
                                 struct Curl_easy *data,
157
                                 struct cf_hc_baller *winner)
158
0
{
159
0
  struct cf_hc_ctx *ctx = cf->ctx;
160
0
  CURLcode result = CURLE_OK;
161
162
0
  DEBUGASSERT(winner->cf);
163
0
  if(winner != &ctx->h3_baller)
164
0
    cf_hc_baller_reset(&ctx->h3_baller, data);
165
0
  if(winner != &ctx->h21_baller)
166
0
    cf_hc_baller_reset(&ctx->h21_baller, data);
167
168
0
  CURL_TRC_CF(data, cf, "connect+handshake %s: %dms, 1st data: %dms",
169
0
              winner->name, (int)Curl_timediff(Curl_now(), winner->started),
170
0
              cf_hc_baller_reply_ms(winner, data));
171
0
  cf->next = winner->cf;
172
0
  winner->cf = NULL;
173
174
0
  switch(cf->conn->alpn) {
175
0
  case CURL_HTTP_VERSION_3:
176
0
    infof(data, "using HTTP/3");
177
0
    break;
178
0
  case CURL_HTTP_VERSION_2:
179
#ifdef USE_NGHTTP2
180
    /* Using nghttp2, we add the filter "below" us, so when the conn
181
     * closes, we tear it down for a fresh reconnect */
182
    result = Curl_http2_switch_at(cf, data);
183
    if(result) {
184
      ctx->state = CF_HC_FAILURE;
185
      ctx->result = result;
186
      return result;
187
    }
188
#endif
189
0
    infof(data, "using HTTP/2");
190
0
    break;
191
0
  case CURL_HTTP_VERSION_1_1:
192
0
    infof(data, "using HTTP/1.1");
193
0
    break;
194
0
  default:
195
0
    infof(data, "using HTTP/1.x");
196
0
    break;
197
0
  }
198
0
  ctx->state = CF_HC_SUCCESS;
199
0
  cf->connected = TRUE;
200
0
  Curl_conn_cf_cntrl(cf->next, data, TRUE,
201
0
                     CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
202
0
  return result;
203
0
}
204
205
206
static bool time_to_start_h21(struct Curl_cfilter *cf,
207
                              struct Curl_easy *data,
208
                              struct curltime now)
209
0
{
210
0
  struct cf_hc_ctx *ctx = cf->ctx;
211
0
  timediff_t elapsed_ms;
212
213
0
  if(!ctx->h21_baller.enabled || cf_hc_baller_has_started(&ctx->h21_baller))
214
0
    return FALSE;
215
216
0
  if(!ctx->h3_baller.enabled || !cf_hc_baller_is_active(&ctx->h3_baller))
217
0
    return TRUE;
218
219
0
  elapsed_ms = Curl_timediff(now, ctx->started);
220
0
  if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
221
0
    CURL_TRC_CF(data, cf, "hard timeout of %dms reached, starting h21",
222
0
                ctx->hard_eyeballs_timeout_ms);
223
0
    return TRUE;
224
0
  }
225
226
0
  if(elapsed_ms >= ctx->soft_eyeballs_timeout_ms) {
227
0
    if(cf_hc_baller_reply_ms(&ctx->h3_baller, data) < 0) {
228
0
      CURL_TRC_CF(data, cf, "soft timeout of %dms reached, h3 has not "
229
0
                  "seen any data, starting h21",
230
0
                  ctx->soft_eyeballs_timeout_ms);
231
0
      return TRUE;
232
0
    }
233
    /* set the effective hard timeout again */
234
0
    Curl_expire(data, ctx->hard_eyeballs_timeout_ms - elapsed_ms,
235
0
                EXPIRE_ALPN_EYEBALLS);
236
0
  }
237
0
  return FALSE;
238
0
}
239
240
static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
241
                              struct Curl_easy *data,
242
                              bool blocking, bool *done)
243
0
{
244
0
  struct cf_hc_ctx *ctx = cf->ctx;
245
0
  struct curltime now;
246
0
  CURLcode result = CURLE_OK;
247
248
0
  (void)blocking;
249
0
  if(cf->connected) {
250
0
    *done = TRUE;
251
0
    return CURLE_OK;
252
0
  }
253
254
0
  *done = FALSE;
255
0
  now = Curl_now();
256
0
  switch(ctx->state) {
257
0
  case CF_HC_INIT:
258
0
    DEBUGASSERT(!ctx->h3_baller.cf);
259
0
    DEBUGASSERT(!ctx->h21_baller.cf);
260
0
    DEBUGASSERT(!cf->next);
261
0
    CURL_TRC_CF(data, cf, "connect, init");
262
0
    ctx->started = now;
263
0
    if(ctx->h3_baller.enabled) {
264
0
      cf_hc_baller_init(&ctx->h3_baller, cf, data, "h3", TRNSPRT_QUIC);
265
0
      if(ctx->h21_baller.enabled)
266
0
        Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
267
0
    }
268
0
    else if(ctx->h21_baller.enabled)
269
0
      cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
270
0
                       cf->conn->transport);
271
0
    ctx->state = CF_HC_CONNECT;
272
    /* FALLTHROUGH */
273
274
0
  case CF_HC_CONNECT:
275
0
    if(cf_hc_baller_is_active(&ctx->h3_baller)) {
276
0
      result = cf_hc_baller_connect(&ctx->h3_baller, cf, data, done);
277
0
      if(!result && *done) {
278
0
        result = baller_connected(cf, data, &ctx->h3_baller);
279
0
        goto out;
280
0
      }
281
0
    }
282
283
0
    if(time_to_start_h21(cf, data, now)) {
284
0
      cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
285
0
                        cf->conn->transport);
286
0
    }
287
288
0
    if(cf_hc_baller_is_active(&ctx->h21_baller)) {
289
0
      CURL_TRC_CF(data, cf, "connect, check h21");
290
0
      result = cf_hc_baller_connect(&ctx->h21_baller, cf, data, done);
291
0
      if(!result && *done) {
292
0
        result = baller_connected(cf, data, &ctx->h21_baller);
293
0
        goto out;
294
0
      }
295
0
    }
296
297
0
    if((!ctx->h3_baller.enabled || ctx->h3_baller.result) &&
298
0
       (!ctx->h21_baller.enabled || ctx->h21_baller.result)) {
299
      /* both failed or disabled. we give up */
300
0
      CURL_TRC_CF(data, cf, "connect, all failed");
301
0
      result = ctx->result = ctx->h3_baller.enabled?
302
0
                              ctx->h3_baller.result : ctx->h21_baller.result;
303
0
      ctx->state = CF_HC_FAILURE;
304
0
      goto out;
305
0
    }
306
0
    result = CURLE_OK;
307
0
    *done = FALSE;
308
0
    break;
309
310
0
  case CF_HC_FAILURE:
311
0
    result = ctx->result;
312
0
    cf->connected = FALSE;
313
0
    *done = FALSE;
314
0
    break;
315
316
0
  case CF_HC_SUCCESS:
317
0
    result = CURLE_OK;
318
0
    cf->connected = TRUE;
319
0
    *done = TRUE;
320
0
    break;
321
0
  }
322
323
0
out:
324
0
  CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
325
0
  return result;
326
0
}
327
328
static int cf_hc_get_select_socks(struct Curl_cfilter *cf,
329
                                  struct Curl_easy *data,
330
                                  curl_socket_t *socks)
331
0
{
332
0
  struct cf_hc_ctx *ctx = cf->ctx;
333
0
  size_t i, j, s;
334
0
  int brc, rc = GETSOCK_BLANK;
335
0
  curl_socket_t bsocks[MAX_SOCKSPEREASYHANDLE];
336
0
  struct cf_hc_baller *ballers[2];
337
338
0
  if(cf->connected)
339
0
    return cf->next->cft->get_select_socks(cf->next, data, socks);
340
341
0
  ballers[0] = &ctx->h3_baller;
342
0
  ballers[1] = &ctx->h21_baller;
343
0
  for(i = s = 0; i < sizeof(ballers)/sizeof(ballers[0]); i++) {
344
0
    struct cf_hc_baller *b = ballers[i];
345
0
    if(!cf_hc_baller_is_active(b))
346
0
      continue;
347
0
    brc = Curl_conn_cf_get_select_socks(b->cf, data, bsocks);
348
0
    CURL_TRC_CF(data, cf, "get_selected_socks(%s) -> %x", b->name, brc);
349
0
    if(!brc)
350
0
      continue;
351
0
    for(j = 0; j < MAX_SOCKSPEREASYHANDLE && s < MAX_SOCKSPEREASYHANDLE; ++j) {
352
0
      if((brc & GETSOCK_WRITESOCK(j)) || (brc & GETSOCK_READSOCK(j))) {
353
0
        socks[s] = bsocks[j];
354
0
        if(brc & GETSOCK_WRITESOCK(j))
355
0
          rc |= GETSOCK_WRITESOCK(s);
356
0
        if(brc & GETSOCK_READSOCK(j))
357
0
          rc |= GETSOCK_READSOCK(s);
358
0
        s++;
359
0
      }
360
0
    }
361
0
  }
362
0
  CURL_TRC_CF(data, cf, "get_selected_socks -> %x", rc);
363
0
  return rc;
364
0
}
365
366
static bool cf_hc_data_pending(struct Curl_cfilter *cf,
367
                               const struct Curl_easy *data)
368
0
{
369
0
  struct cf_hc_ctx *ctx = cf->ctx;
370
371
0
  if(cf->connected)
372
0
    return cf->next->cft->has_data_pending(cf->next, data);
373
374
0
  CURL_TRC_CF((struct Curl_easy *)data, cf, "data_pending");
375
0
  return cf_hc_baller_data_pending(&ctx->h3_baller, data)
376
0
         || cf_hc_baller_data_pending(&ctx->h21_baller, data);
377
0
}
378
379
static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
380
                                              struct Curl_easy *data,
381
                                              int query)
382
0
{
383
0
  struct cf_hc_ctx *ctx = cf->ctx;
384
0
  struct Curl_cfilter *cfb;
385
0
  struct curltime t, tmax;
386
387
0
  memset(&tmax, 0, sizeof(tmax));
388
0
  memset(&t, 0, sizeof(t));
389
0
  cfb = ctx->h21_baller.enabled? ctx->h21_baller.cf : NULL;
390
0
  if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
391
0
    if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
392
0
      tmax = t;
393
0
  }
394
0
  memset(&t, 0, sizeof(t));
395
0
  cfb = ctx->h3_baller.enabled? ctx->h3_baller.cf : NULL;
396
0
  if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
397
0
    if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
398
0
      tmax = t;
399
0
  }
400
0
  return tmax;
401
0
}
402
403
static CURLcode cf_hc_query(struct Curl_cfilter *cf,
404
                            struct Curl_easy *data,
405
                            int query, int *pres1, void *pres2)
406
0
{
407
0
  if(!cf->connected) {
408
0
    switch(query) {
409
0
    case CF_QUERY_TIMER_CONNECT: {
410
0
      struct curltime *when = pres2;
411
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
412
0
      return CURLE_OK;
413
0
    }
414
0
    case CF_QUERY_TIMER_APPCONNECT: {
415
0
      struct curltime *when = pres2;
416
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
417
0
      return CURLE_OK;
418
0
    }
419
0
    default:
420
0
      break;
421
0
    }
422
0
  }
423
0
  return cf->next?
424
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
425
0
    CURLE_UNKNOWN_OPTION;
426
0
}
427
428
static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data)
429
0
{
430
0
  CURL_TRC_CF(data, cf, "close");
431
0
  cf_hc_reset(cf, data);
432
0
  cf->connected = FALSE;
433
434
0
  if(cf->next) {
435
0
    cf->next->cft->do_close(cf->next, data);
436
0
    Curl_conn_cf_discard_chain(&cf->next, data);
437
0
  }
438
0
}
439
440
static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
441
0
{
442
0
  struct cf_hc_ctx *ctx = cf->ctx;
443
444
0
  (void)data;
445
0
  CURL_TRC_CF(data, cf, "destroy");
446
0
  cf_hc_reset(cf, data);
447
0
  Curl_safefree(ctx);
448
0
}
449
450
struct Curl_cftype Curl_cft_http_connect = {
451
  "HTTPS-CONNECT",
452
  0,
453
  CURL_LOG_LVL_NONE,
454
  cf_hc_destroy,
455
  cf_hc_connect,
456
  cf_hc_close,
457
  Curl_cf_def_get_host,
458
  cf_hc_get_select_socks,
459
  cf_hc_data_pending,
460
  Curl_cf_def_send,
461
  Curl_cf_def_recv,
462
  Curl_cf_def_cntrl,
463
  Curl_cf_def_conn_is_alive,
464
  Curl_cf_def_conn_keep_alive,
465
  cf_hc_query,
466
};
467
468
static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
469
                             struct Curl_easy *data,
470
                             const struct Curl_dns_entry *remotehost,
471
                             bool try_h3, bool try_h21)
472
0
{
473
0
  struct Curl_cfilter *cf = NULL;
474
0
  struct cf_hc_ctx *ctx;
475
0
  CURLcode result = CURLE_OK;
476
477
0
  (void)data;
478
0
  ctx = calloc(sizeof(*ctx), 1);
479
0
  if(!ctx) {
480
0
    result = CURLE_OUT_OF_MEMORY;
481
0
    goto out;
482
0
  }
483
0
  ctx->remotehost = remotehost;
484
0
  ctx->h3_baller.enabled = try_h3;
485
0
  ctx->h21_baller.enabled = try_h21;
486
487
0
  result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
488
0
  if(result)
489
0
    goto out;
490
0
  ctx = NULL;
491
0
  cf_hc_reset(cf, data);
492
493
0
out:
494
0
  *pcf = result? NULL : cf;
495
0
  free(ctx);
496
0
  return result;
497
0
}
498
499
static CURLcode cf_http_connect_add(struct Curl_easy *data,
500
                                    struct connectdata *conn,
501
                                    int sockindex,
502
                                    const struct Curl_dns_entry *remotehost,
503
                                    bool try_h3, bool try_h21)
504
0
{
505
0
  struct Curl_cfilter *cf;
506
0
  CURLcode result = CURLE_OK;
507
508
0
  DEBUGASSERT(data);
509
0
  result = cf_hc_create(&cf, data, remotehost, try_h3, try_h21);
510
0
  if(result)
511
0
    goto out;
512
0
  Curl_conn_cf_add(data, conn, sockindex, cf);
513
0
out:
514
0
  return result;
515
0
}
516
517
CURLcode Curl_cf_https_setup(struct Curl_easy *data,
518
                             struct connectdata *conn,
519
                             int sockindex,
520
                             const struct Curl_dns_entry *remotehost)
521
0
{
522
0
  bool try_h3 = FALSE, try_h21 = TRUE; /* defaults, for now */
523
0
  CURLcode result = CURLE_OK;
524
525
0
  (void)sockindex;
526
0
  (void)remotehost;
527
528
0
  if(!conn->bits.tls_enable_alpn)
529
0
    goto out;
530
531
0
  if(data->state.httpwant == CURL_HTTP_VERSION_3ONLY) {
532
0
    result = Curl_conn_may_http3(data, conn);
533
0
    if(result) /* can't do it */
534
0
      goto out;
535
0
    try_h3 = TRUE;
536
0
    try_h21 = FALSE;
537
0
  }
538
0
  else if(data->state.httpwant >= CURL_HTTP_VERSION_3) {
539
    /* We assume that silently not even trying H3 is ok here */
540
    /* TODO: should we fail instead? */
541
0
    try_h3 = (Curl_conn_may_http3(data, conn) == CURLE_OK);
542
0
    try_h21 = TRUE;
543
0
  }
544
545
0
  result = cf_http_connect_add(data, conn, sockindex, remotehost,
546
0
                               try_h3, try_h21);
547
0
out:
548
0
  return result;
549
0
}
550
551
#endif /* !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER) */