Coverage Report

Created: 2024-02-25 06:14

/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) && !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
  default:
192
0
    infof(data, "using HTTP/1.x");
193
0
    break;
194
0
  }
195
0
  ctx->state = CF_HC_SUCCESS;
196
0
  cf->connected = TRUE;
197
0
  Curl_conn_cf_cntrl(cf->next, data, TRUE,
198
0
                     CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
199
0
  return result;
200
0
}
201
202
203
static bool time_to_start_h21(struct Curl_cfilter *cf,
204
                              struct Curl_easy *data,
205
                              struct curltime now)
206
0
{
207
0
  struct cf_hc_ctx *ctx = cf->ctx;
208
0
  timediff_t elapsed_ms;
209
210
0
  if(!ctx->h21_baller.enabled || cf_hc_baller_has_started(&ctx->h21_baller))
211
0
    return FALSE;
212
213
0
  if(!ctx->h3_baller.enabled || !cf_hc_baller_is_active(&ctx->h3_baller))
214
0
    return TRUE;
215
216
0
  elapsed_ms = Curl_timediff(now, ctx->started);
217
0
  if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
218
0
    CURL_TRC_CF(data, cf, "hard timeout of %dms reached, starting h21",
219
0
                ctx->hard_eyeballs_timeout_ms);
220
0
    return TRUE;
221
0
  }
222
223
0
  if(elapsed_ms >= ctx->soft_eyeballs_timeout_ms) {
224
0
    if(cf_hc_baller_reply_ms(&ctx->h3_baller, data) < 0) {
225
0
      CURL_TRC_CF(data, cf, "soft timeout of %dms reached, h3 has not "
226
0
                  "seen any data, starting h21",
227
0
                  ctx->soft_eyeballs_timeout_ms);
228
0
      return TRUE;
229
0
    }
230
    /* set the effective hard timeout again */
231
0
    Curl_expire(data, ctx->hard_eyeballs_timeout_ms - elapsed_ms,
232
0
                EXPIRE_ALPN_EYEBALLS);
233
0
  }
234
0
  return FALSE;
235
0
}
236
237
static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
238
                              struct Curl_easy *data,
239
                              bool blocking, bool *done)
240
0
{
241
0
  struct cf_hc_ctx *ctx = cf->ctx;
242
0
  struct curltime now;
243
0
  CURLcode result = CURLE_OK;
244
245
0
  (void)blocking;
246
0
  if(cf->connected) {
247
0
    *done = TRUE;
248
0
    return CURLE_OK;
249
0
  }
250
251
0
  *done = FALSE;
252
0
  now = Curl_now();
253
0
  switch(ctx->state) {
254
0
  case CF_HC_INIT:
255
0
    DEBUGASSERT(!ctx->h3_baller.cf);
256
0
    DEBUGASSERT(!ctx->h21_baller.cf);
257
0
    DEBUGASSERT(!cf->next);
258
0
    CURL_TRC_CF(data, cf, "connect, init");
259
0
    ctx->started = now;
260
0
    if(ctx->h3_baller.enabled) {
261
0
      cf_hc_baller_init(&ctx->h3_baller, cf, data, "h3", TRNSPRT_QUIC);
262
0
      if(ctx->h21_baller.enabled)
263
0
        Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
264
0
    }
265
0
    else if(ctx->h21_baller.enabled)
266
0
      cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
267
0
                       cf->conn->transport);
268
0
    ctx->state = CF_HC_CONNECT;
269
0
    FALLTHROUGH();
270
271
0
  case CF_HC_CONNECT:
272
0
    if(cf_hc_baller_is_active(&ctx->h3_baller)) {
273
0
      result = cf_hc_baller_connect(&ctx->h3_baller, cf, data, done);
274
0
      if(!result && *done) {
275
0
        result = baller_connected(cf, data, &ctx->h3_baller);
276
0
        goto out;
277
0
      }
278
0
    }
279
280
0
    if(time_to_start_h21(cf, data, now)) {
281
0
      cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
282
0
                        cf->conn->transport);
283
0
    }
284
285
0
    if(cf_hc_baller_is_active(&ctx->h21_baller)) {
286
0
      CURL_TRC_CF(data, cf, "connect, check h21");
287
0
      result = cf_hc_baller_connect(&ctx->h21_baller, cf, data, done);
288
0
      if(!result && *done) {
289
0
        result = baller_connected(cf, data, &ctx->h21_baller);
290
0
        goto out;
291
0
      }
292
0
    }
293
294
0
    if((!ctx->h3_baller.enabled || ctx->h3_baller.result) &&
295
0
       (!ctx->h21_baller.enabled || ctx->h21_baller.result)) {
296
      /* both failed or disabled. we give up */
297
0
      CURL_TRC_CF(data, cf, "connect, all failed");
298
0
      result = ctx->result = ctx->h3_baller.enabled?
299
0
                              ctx->h3_baller.result : ctx->h21_baller.result;
300
0
      ctx->state = CF_HC_FAILURE;
301
0
      goto out;
302
0
    }
303
0
    result = CURLE_OK;
304
0
    *done = FALSE;
305
0
    break;
306
307
0
  case CF_HC_FAILURE:
308
0
    result = ctx->result;
309
0
    cf->connected = FALSE;
310
0
    *done = FALSE;
311
0
    break;
312
313
0
  case CF_HC_SUCCESS:
314
0
    result = CURLE_OK;
315
0
    cf->connected = TRUE;
316
0
    *done = TRUE;
317
0
    break;
318
0
  }
319
320
0
out:
321
0
  CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
322
0
  return result;
323
0
}
324
325
static void cf_hc_adjust_pollset(struct Curl_cfilter *cf,
326
                                  struct Curl_easy *data,
327
                                  struct easy_pollset *ps)
328
0
{
329
0
  if(!cf->connected) {
330
0
    struct cf_hc_ctx *ctx = cf->ctx;
331
0
    struct cf_hc_baller *ballers[2];
332
0
    size_t i;
333
334
0
    ballers[0] = &ctx->h3_baller;
335
0
    ballers[1] = &ctx->h21_baller;
336
0
    for(i = 0; i < sizeof(ballers)/sizeof(ballers[0]); i++) {
337
0
      struct cf_hc_baller *b = ballers[i];
338
0
      if(!cf_hc_baller_is_active(b))
339
0
        continue;
340
0
      Curl_conn_cf_adjust_pollset(b->cf, data, ps);
341
0
    }
342
0
    CURL_TRC_CF(data, cf, "adjust_pollset -> %d socks", ps->num);
343
0
  }
344
0
}
345
346
static bool cf_hc_data_pending(struct Curl_cfilter *cf,
347
                               const struct Curl_easy *data)
348
0
{
349
0
  struct cf_hc_ctx *ctx = cf->ctx;
350
351
0
  if(cf->connected)
352
0
    return cf->next->cft->has_data_pending(cf->next, data);
353
354
0
  CURL_TRC_CF((struct Curl_easy *)data, cf, "data_pending");
355
0
  return cf_hc_baller_data_pending(&ctx->h3_baller, data)
356
0
         || cf_hc_baller_data_pending(&ctx->h21_baller, data);
357
0
}
358
359
static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
360
                                              struct Curl_easy *data,
361
                                              int query)
362
0
{
363
0
  struct cf_hc_ctx *ctx = cf->ctx;
364
0
  struct Curl_cfilter *cfb;
365
0
  struct curltime t, tmax;
366
367
0
  memset(&tmax, 0, sizeof(tmax));
368
0
  memset(&t, 0, sizeof(t));
369
0
  cfb = ctx->h21_baller.enabled? ctx->h21_baller.cf : NULL;
370
0
  if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
371
0
    if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
372
0
      tmax = t;
373
0
  }
374
0
  memset(&t, 0, sizeof(t));
375
0
  cfb = ctx->h3_baller.enabled? ctx->h3_baller.cf : NULL;
376
0
  if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
377
0
    if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
378
0
      tmax = t;
379
0
  }
380
0
  return tmax;
381
0
}
382
383
static CURLcode cf_hc_query(struct Curl_cfilter *cf,
384
                            struct Curl_easy *data,
385
                            int query, int *pres1, void *pres2)
386
0
{
387
0
  if(!cf->connected) {
388
0
    switch(query) {
389
0
    case CF_QUERY_TIMER_CONNECT: {
390
0
      struct curltime *when = pres2;
391
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
392
0
      return CURLE_OK;
393
0
    }
394
0
    case CF_QUERY_TIMER_APPCONNECT: {
395
0
      struct curltime *when = pres2;
396
0
      *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
397
0
      return CURLE_OK;
398
0
    }
399
0
    default:
400
0
      break;
401
0
    }
402
0
  }
403
0
  return cf->next?
404
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
405
0
    CURLE_UNKNOWN_OPTION;
406
0
}
407
408
static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data)
409
0
{
410
0
  CURL_TRC_CF(data, cf, "close");
411
0
  cf_hc_reset(cf, data);
412
0
  cf->connected = FALSE;
413
414
0
  if(cf->next) {
415
0
    cf->next->cft->do_close(cf->next, data);
416
0
    Curl_conn_cf_discard_chain(&cf->next, data);
417
0
  }
418
0
}
419
420
static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
421
0
{
422
0
  struct cf_hc_ctx *ctx = cf->ctx;
423
424
0
  (void)data;
425
0
  CURL_TRC_CF(data, cf, "destroy");
426
0
  cf_hc_reset(cf, data);
427
0
  Curl_safefree(ctx);
428
0
}
429
430
struct Curl_cftype Curl_cft_http_connect = {
431
  "HTTPS-CONNECT",
432
  0,
433
  CURL_LOG_LVL_NONE,
434
  cf_hc_destroy,
435
  cf_hc_connect,
436
  cf_hc_close,
437
  Curl_cf_def_get_host,
438
  cf_hc_adjust_pollset,
439
  cf_hc_data_pending,
440
  Curl_cf_def_send,
441
  Curl_cf_def_recv,
442
  Curl_cf_def_cntrl,
443
  Curl_cf_def_conn_is_alive,
444
  Curl_cf_def_conn_keep_alive,
445
  cf_hc_query,
446
};
447
448
static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
449
                             struct Curl_easy *data,
450
                             const struct Curl_dns_entry *remotehost,
451
                             bool try_h3, bool try_h21)
452
0
{
453
0
  struct Curl_cfilter *cf = NULL;
454
0
  struct cf_hc_ctx *ctx;
455
0
  CURLcode result = CURLE_OK;
456
457
0
  (void)data;
458
0
  ctx = calloc(1, sizeof(*ctx));
459
0
  if(!ctx) {
460
0
    result = CURLE_OUT_OF_MEMORY;
461
0
    goto out;
462
0
  }
463
0
  ctx->remotehost = remotehost;
464
0
  ctx->h3_baller.enabled = try_h3;
465
0
  ctx->h21_baller.enabled = try_h21;
466
467
0
  result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
468
0
  if(result)
469
0
    goto out;
470
0
  ctx = NULL;
471
0
  cf_hc_reset(cf, data);
472
473
0
out:
474
0
  *pcf = result? NULL : cf;
475
0
  free(ctx);
476
0
  return result;
477
0
}
478
479
static CURLcode cf_http_connect_add(struct Curl_easy *data,
480
                                    struct connectdata *conn,
481
                                    int sockindex,
482
                                    const struct Curl_dns_entry *remotehost,
483
                                    bool try_h3, bool try_h21)
484
0
{
485
0
  struct Curl_cfilter *cf;
486
0
  CURLcode result = CURLE_OK;
487
488
0
  DEBUGASSERT(data);
489
0
  result = cf_hc_create(&cf, data, remotehost, try_h3, try_h21);
490
0
  if(result)
491
0
    goto out;
492
0
  Curl_conn_cf_add(data, conn, sockindex, cf);
493
0
out:
494
0
  return result;
495
0
}
496
497
CURLcode Curl_cf_https_setup(struct Curl_easy *data,
498
                             struct connectdata *conn,
499
                             int sockindex,
500
                             const struct Curl_dns_entry *remotehost)
501
0
{
502
0
  bool try_h3 = FALSE, try_h21 = TRUE; /* defaults, for now */
503
0
  CURLcode result = CURLE_OK;
504
505
0
  (void)sockindex;
506
0
  (void)remotehost;
507
508
0
  if(!conn->bits.tls_enable_alpn)
509
0
    goto out;
510
511
0
  if(data->state.httpwant == CURL_HTTP_VERSION_3ONLY) {
512
0
    result = Curl_conn_may_http3(data, conn);
513
0
    if(result) /* can't do it */
514
0
      goto out;
515
0
    try_h3 = TRUE;
516
0
    try_h21 = FALSE;
517
0
  }
518
0
  else if(data->state.httpwant >= CURL_HTTP_VERSION_3) {
519
    /* We assume that silently not even trying H3 is ok here */
520
    /* TODO: should we fail instead? */
521
0
    try_h3 = (Curl_conn_may_http3(data, conn) == CURLE_OK);
522
0
    try_h21 = TRUE;
523
0
  }
524
525
0
  result = cf_http_connect_add(data, conn, sockindex, remotehost,
526
0
                               try_h3, try_h21);
527
0
out:
528
0
  return result;
529
0
}
530
531
#endif /* !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER) */