Coverage Report

Created: 2026-07-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/cf-setup.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
#include "curl_setup.h"
25
26
#include "urldata.h"
27
#include "cfilters.h"
28
#include "cf-haproxy.h"
29
#include "cf-ip-happy.h"
30
#include "cf-setup.h"
31
#include "curl_trc.h"
32
#include "connect.h"
33
#include "http_proxy.h"
34
#include "socks.h"
35
#include "vquic/cf-capsule.h"
36
#include "vquic/vquic.h"
37
#include "vtls/vtls.h"
38
39
40
typedef enum {
41
  CF_SETUP_INIT,
42
  CF_SETUP_CNNCT_EYEBALLS,
43
  CF_SETUP_CNNCT_SOCKS,
44
  CF_SETUP_CNNCT_HTTP_PROXY,
45
  CF_SETUP_CNNCT_HAPROXY,
46
  CF_SETUP_CNNCT_SSL,
47
  CF_SETUP_DONE
48
} cf_setup_state;
49
50
struct cf_setup_ctx {
51
  cf_setup_state state;
52
  int ssl_mode;
53
  uint8_t transport;
54
  uint8_t retry_count;
55
};
56
57
#ifndef CURL_DISABLE_PROXY
58
59
static CURLcode cf_setup_add_haproxy(struct Curl_cfilter *cf,
60
                                     struct Curl_easy *data)
61
0
{
62
0
  struct cf_setup_ctx *ctx = cf->ctx;
63
0
  CURLcode result = CURLE_OK;
64
65
0
  if(ctx->state < CF_SETUP_CNNCT_HAPROXY) {
66
0
    if(data->set.haproxyprotocol) {
67
0
      if(ctx->transport == TRNSPRT_QUIC) {
68
0
        failf(data, "haproxy protocol does not support QUIC");
69
0
        return CURLE_UNSUPPORTED_PROTOCOL;
70
0
      }
71
0
      result = Curl_cf_haproxy_insert_after(cf, data);
72
0
      if(result) {
73
0
        CURL_TRC_CF(data, cf, "adding HAPROXY filter failed -> %d",
74
0
                    (int)result);
75
0
        return result;
76
0
      }
77
0
      CURL_TRC_CF(data, cf, "added HAPROXY filter");
78
0
    }
79
0
    ctx->state = CF_SETUP_CNNCT_HAPROXY;
80
0
  }
81
0
  return result;
82
0
}
83
84
static CURLcode cf_setup_add_socks(struct Curl_cfilter *cf,
85
                                   struct Curl_easy *data)
86
0
{
87
0
  struct cf_setup_ctx *ctx = cf->ctx;
88
0
  CURLcode result = CURLE_OK;
89
0
  if(ctx->state < CF_SETUP_CNNCT_SOCKS && cf->conn->socks_proxy.peer) {
90
    /* Add a SOCKS proxy to go through `first_peer` to `second_peer`*/
91
0
    struct Curl_peer *second_peer;
92
93
0
    if(cf->conn->http_proxy.peer)
94
0
      second_peer = cf->conn->http_proxy.peer;
95
0
    else
96
0
      second_peer = Curl_conn_get_destination(cf->conn, cf->sockindex);
97
0
    if(!second_peer)
98
0
      return CURLE_FAILED_INIT;
99
100
0
    result = Curl_cf_socks_proxy_insert_after(
101
0
      cf, data, second_peer, cf->conn->ip_version,
102
0
      cf->conn->socks_proxy.proxytype,
103
0
      cf->conn->socks_proxy.creds);
104
0
    if(result) {
105
0
      CURL_TRC_CF(data, cf, "adding SOCKS filter failed -> %d", (int)result);
106
0
      return result;
107
0
    }
108
109
0
    CURL_TRC_CF(data, cf, "added SOCKS filter to %s:%u",
110
0
                second_peer->hostname, second_peer->port);
111
0
    ctx->state = CF_SETUP_CNNCT_SOCKS;
112
0
  }
113
0
  return result;
114
0
}
115
116
#ifndef CURL_DISABLE_HTTP
117
static CURLcode cf_setup_add_http_proxy(struct Curl_cfilter *cf,
118
                                        struct Curl_easy *data)
119
0
{
120
0
  struct cf_setup_ctx *ctx = cf->ctx;
121
0
  CURLcode result = CURLE_OK;
122
123
0
  if(ctx->state < CF_SETUP_CNNCT_HTTP_PROXY &&
124
0
     cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
125
0
    struct Curl_peer *peer = cf->conn->http_proxy.peer;
126
0
    struct Curl_peer *tunnel_peer =
127
0
      Curl_conn_get_destination(cf->conn, cf->sockindex);
128
129
#ifdef USE_SSL
130
    if(CURL_PROXY_IS_HTTPS(cf->conn->http_proxy.proxytype) &&
131
       !Curl_conn_is_ssl(cf->conn, cf->sockindex)) {
132
      result = Curl_cf_ssl_proxy_insert_after(
133
        cf, data, cf->conn->http_proxy.peer);
134
      if(result) {
135
        CURL_TRC_CF(data, cf, "adding SSL filter for HTTP proxy failed -> %d",
136
                    (int)result);
137
        return result;
138
      }
139
      CURL_TRC_CF(data, cf, "added SSL filter for HTTP proxy");
140
    }
141
#endif /* USE_SSL */
142
143
0
    result = Curl_cf_http_proxy_insert_after(
144
0
      cf, data, peer, tunnel_peer,
145
0
      ctx->transport, cf->conn->http_proxy.proxytype);
146
0
    if(result) {
147
0
      CURL_TRC_CF(data, cf, "adding HTTP proxy tunnel filter failed -> %d",
148
0
                  (int)result);
149
0
      return result;
150
0
    }
151
0
    CURL_TRC_CF(data, cf, "added HTTP proxy tunnel filter");
152
0
    ctx->state = CF_SETUP_CNNCT_HTTP_PROXY;
153
0
  }
154
0
  return result;
155
0
}
156
#endif /* !CURL_DISABLE_HTTP */
157
#endif /* CURL_DISABLE_PROXY */
158
159
/* Get the origin curl connects its socket to.
160
 * Can be origin or the first proxy. */
161
static struct Curl_peer *conn_get_first_origin(struct connectdata *conn,
162
                                             int sockindex)
163
0
{
164
0
#ifndef CURL_DISABLE_PROXY
165
0
  if(conn->socks_proxy.peer)
166
0
    return conn->socks_proxy.peer;
167
0
  if(conn->http_proxy.peer)
168
0
    return conn->http_proxy.peer;
169
0
#endif
170
0
  return (sockindex == SECONDARYSOCKET) ? conn->origin2 : conn->origin;
171
0
}
172
173
static CURLcode cf_setup_add_ip_happy(struct Curl_cfilter *cf,
174
                                      struct Curl_easy *data)
175
0
{
176
0
  struct cf_setup_ctx *ctx = cf->ctx;
177
0
  CURLcode result = CURLE_OK;
178
179
0
  if(ctx->state < CF_SETUP_CNNCT_EYEBALLS) {
180
    /* What is the first hop we directly connect to and what transport
181
     * do we use for it? Only on the first hop we can do Happy Eyeballs.
182
     * first_origin and first_peer differ on --connect-to. */
183
0
    struct Curl_peer *first_origin =
184
0
      conn_get_first_origin(cf->conn, cf->sockindex);
185
0
    struct Curl_peer *first_peer =
186
0
      Curl_conn_get_first_peer(cf->conn, cf->sockindex);
187
0
    struct Curl_peer *tunnel_peer = NULL;
188
0
    uint8_t first_transport = ctx->transport;
189
190
0
    if(!first_peer)
191
0
      return CURLE_FAILED_INIT;
192
193
0
#if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
194
0
    if(cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
195
0
      first_transport =
196
0
        Curl_http_proxy_transport(cf->conn->http_proxy.proxytype);
197
0
      tunnel_peer = Curl_conn_get_destination(cf->conn, cf->sockindex);
198
0
      if((first_transport == TRNSPRT_QUIC) && cf->conn->socks_proxy.peer) {
199
0
        failf(data, "HTTP/3 proxy not possible via SOCKS");
200
0
        return CURLE_UNSUPPORTED_PROTOCOL;
201
0
      }
202
0
    }
203
0
#endif /* !CURL_DISABLE_PROXY && !CURL_DISABLE_HTTP */
204
205
0
    result = cf_ip_happy_insert_after(cf, data, first_origin, first_peer,
206
0
                                      first_transport,
207
0
                                      tunnel_peer, ctx->transport);
208
0
    if(result) {
209
0
      CURL_TRC_CF(data, cf, "adding happy eyeballs failed -> %d", (int)result);
210
0
      return result;
211
0
    }
212
213
0
    if(tunnel_peer && (first_transport == TRNSPRT_QUIC)) {
214
0
      CURL_TRC_CF(data, cf, "happy eyeballing to HTTP/3 proxy %s:%u",
215
0
                  first_peer->hostname, first_peer->port);
216
0
      ctx->state = CF_SETUP_CNNCT_HTTP_PROXY;
217
0
    }
218
0
    else {
219
0
      CURL_TRC_CF(data, cf, "happy eyeballing to %s %s:%u",
220
0
                  tunnel_peer ? "proxy" : "origin",
221
0
                  first_peer->hostname, first_peer->port);
222
0
      ctx->state = CF_SETUP_CNNCT_EYEBALLS;
223
0
    }
224
0
  }
225
0
  return result;
226
0
}
227
228
static CURLcode cf_setup_add_origin_filters(struct Curl_cfilter *cf,
229
                                            struct Curl_easy *data)
230
0
{
231
0
  struct cf_setup_ctx *ctx = cf->ctx;
232
0
  CURLcode result = CURLE_OK;
233
234
0
  (void)data; /* not used in all builds */
235
0
  if(ctx->state < CF_SETUP_CNNCT_SSL) {
236
#if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3) && \
237
    !defined(CURL_DISABLE_PROXY)
238
239
    /* Wanting QUIC with an HTTP tunneling filter, we now need to add
240
     * the QUIC filter on top. Without tunneling, this has already
241
     * happened in the Happy Eyeball filter. */
242
    if(ctx->transport == TRNSPRT_QUIC &&
243
       cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
244
      struct Curl_peer *origin = Curl_conn_get_origin(cf->conn, cf->sockindex);
245
      struct Curl_peer *peer =
246
        Curl_conn_get_destination(cf->conn, cf->sockindex);
247
248
      result = Curl_cf_capsule_insert_after(cf, data);
249
      if(result) {
250
        CURL_TRC_CF(data, cf, "adding capsule filter failed -> %d",
251
                    (int)result);
252
        return result;
253
      }
254
      result = Curl_cf_quic_insert_after(cf, origin, peer);
255
      if(result) {
256
        CURL_TRC_CF(data, cf, "adding QUIC filter failed -> %d", (int)result);
257
        return result;
258
      }
259
      CURL_TRC_CF(data, cf, "added QUIC filter for origin");
260
    }
261
    else
262
#endif /* !CURL_DISABLE_HTTP && USE_HTTP3 && CURL_DISABLE_PROXY */
263
#ifdef USE_SSL
264
    if((ctx->ssl_mode == CURL_CF_SSL_ENABLE ||
265
        (ctx->ssl_mode != CURL_CF_SSL_DISABLE &&
266
         cf->conn->scheme->flags & PROTOPT_SSL)) && /* we want SSL */
267
       !Curl_conn_is_ssl(cf->conn, cf->sockindex)) { /* it is missing */
268
269
#ifndef CURL_DISABLE_PROXY
270
      if(cf->conn->bits.origin_is_proxy) {
271
        result = Curl_cf_ssl_proxy_insert_after(cf, data, cf->conn->origin);
272
      }
273
      else
274
#endif
275
      {
276
        /* Another FTP quirk: when adding SSL verification, to a DATA
277
         * connection, always verify against the control's origin */
278
        struct Curl_peer *origin = Curl_conn_get_origin(cf->conn, FIRSTSOCKET);
279
        struct Curl_peer *peer =
280
          Curl_conn_get_destination(cf->conn, cf->sockindex);
281
        result = Curl_cf_ssl_insert_after(cf, data, origin, peer);
282
      }
283
      if(result) {
284
        CURL_TRC_CF(data, cf, "adding SSL filter for origin failed -> %d",
285
                    (int)result);
286
        return result;
287
      }
288
      CURL_TRC_CF(data, cf, "added SSL filter for origin");
289
    }
290
#endif /* USE_SSL */
291
0
    ctx->state = CF_SETUP_CNNCT_SSL;
292
0
  }
293
0
  return result;
294
0
}
295
296
static CURLcode cf_setup_connect_steps(struct Curl_cfilter *cf,
297
                                       struct Curl_easy *data,
298
                                       bool *done)
299
0
{
300
0
  struct cf_setup_ctx *ctx = cf->ctx;
301
0
  CURLcode result = CURLE_OK;
302
303
0
  if(cf->connected) {
304
0
    *done = TRUE;
305
0
    return CURLE_OK;
306
0
  }
307
308
  /* connect current sub-chain */
309
0
connect_sub_chain:
310
0
  VERBOSE(Curl_conn_trc_filters(data, cf->sockindex, "cf_setup_connect"));
311
312
0
  if(cf->next && !cf->next->connected) {
313
0
    result = Curl_conn_cf_connect(cf->next, data, done);
314
0
    if(result || !*done)
315
0
      return result;
316
0
  }
317
318
0
  result = cf_setup_add_ip_happy(cf, data);
319
0
  if(result)
320
0
    return result;
321
0
  if(!cf->next || !cf->next->connected)
322
0
    goto connect_sub_chain;
323
324
0
#ifndef CURL_DISABLE_PROXY
325
0
  result = cf_setup_add_socks(cf, data);
326
0
  if(result)
327
0
    return result;
328
0
  if(!cf->next || !cf->next->connected)
329
0
    goto connect_sub_chain;
330
331
0
#ifndef CURL_DISABLE_HTTP
332
0
  result = cf_setup_add_http_proxy(cf, data);
333
0
  if(result)
334
0
    return result;
335
0
  if(!cf->next || !cf->next->connected)
336
0
    goto connect_sub_chain;
337
0
#endif /* !CURL_DISABLE_HTTP */
338
339
0
  result = cf_setup_add_haproxy(cf, data);
340
0
  if(result)
341
0
    return result;
342
0
  if(!cf->next || !cf->next->connected)
343
0
    goto connect_sub_chain;
344
0
#endif /* !CURL_DISABLE_PROXY */
345
346
0
  result = cf_setup_add_origin_filters(cf, data);
347
0
  if(result)
348
0
    return result;
349
0
  if(!cf->next || !cf->next->connected)
350
0
    goto connect_sub_chain;
351
352
0
  ctx->state = CF_SETUP_DONE;
353
0
  cf->connected = TRUE;
354
0
  *done = TRUE;
355
0
  return CURLE_OK;
356
0
}
357
358
static CURLcode cf_setup_connect(struct Curl_cfilter *cf,
359
                                 struct Curl_easy *data,
360
                                 bool *done)
361
0
{
362
0
  struct cf_setup_ctx *ctx = cf->ctx;
363
0
  CURLcode result;
364
365
  /* In some situations, a server/proxy may close the connection and
366
   * we need to connect again (HTTP/1.x proxy auth, for example).
367
   * We used to close the filters and reuse them for another attempt,
368
   * however that complicates filter code and it is simpler to tear them
369
   * all down and start over. */
370
0
retry:
371
0
  result = cf_setup_connect_steps(cf, data, done);
372
373
0
  if(result == CURLE_AGAIN) {
374
0
    ++ctx->retry_count;
375
0
    if(ctx->retry_count > 5) /* arbitrary limit, better just timeout? */
376
0
      return CURLE_COULDNT_CONNECT;
377
378
0
    CURL_TRC_CF(data, cf, "retrying connect, %d. time", ctx->retry_count);
379
0
    Curl_conn_cf_discard_chain(&cf->next, data);
380
0
    ctx->state = CF_SETUP_INIT;
381
0
    goto retry;
382
0
  }
383
0
  return result;
384
0
}
385
386
static void cf_setup_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
387
0
{
388
0
  struct cf_setup_ctx *ctx = cf->ctx;
389
390
0
  CURL_TRC_CF(data, cf, "destroy");
391
0
  curlx_safefree(ctx);
392
0
}
393
394
struct Curl_cftype Curl_cft_setup = {
395
  "SETUP",
396
  CF_TYPE_SETUP,
397
  CURL_LOG_LVL_NONE,
398
  cf_setup_destroy,
399
  cf_setup_connect,
400
  Curl_cf_def_shutdown,
401
  Curl_cf_def_adjust_pollset,
402
  Curl_cf_def_data_pending,
403
  Curl_cf_def_send,
404
  Curl_cf_def_recv,
405
  Curl_cf_def_cntrl,
406
  Curl_cf_def_conn_is_alive,
407
  Curl_cf_def_conn_keep_alive,
408
  Curl_cf_def_query,
409
};
410
411
static CURLcode cf_setup_create(struct Curl_cfilter **pcf,
412
                                struct Curl_easy *data,
413
                                uint8_t transport,
414
                                int ssl_mode)
415
0
{
416
0
  struct Curl_cfilter *cf = NULL;
417
0
  struct cf_setup_ctx *ctx;
418
0
  CURLcode result = CURLE_OK;
419
420
0
  (void)data;
421
0
  ctx = curlx_calloc(1, sizeof(*ctx));
422
0
  if(!ctx) {
423
0
    result = CURLE_OUT_OF_MEMORY;
424
0
    goto out;
425
0
  }
426
0
  ctx->state = CF_SETUP_INIT;
427
0
  ctx->ssl_mode = ssl_mode;
428
0
  ctx->transport = transport;
429
430
0
  result = Curl_cf_create(&cf, &Curl_cft_setup, ctx);
431
0
  if(result)
432
0
    goto out;
433
0
  ctx = NULL;
434
435
0
out:
436
0
  *pcf = result ? NULL : cf;
437
0
  if(ctx) {
438
0
    curlx_free(ctx);
439
0
  }
440
0
  return result;
441
0
}
442
443
CURLcode Curl_cf_setup_add(struct Curl_easy *data,
444
                           struct connectdata *conn,
445
                           int sockindex,
446
                           uint8_t transport,
447
                           int ssl_mode)
448
0
{
449
0
  struct Curl_cfilter *cf;
450
0
  CURLcode result = CURLE_OK;
451
452
0
  DEBUGASSERT(data);
453
0
  result = cf_setup_create(&cf, data, transport, ssl_mode);
454
0
  if(result)
455
0
    goto out;
456
0
  Curl_conn_cf_add(data, conn, sockindex, cf);
457
0
out:
458
0
  return result;
459
0
}
460
461
CURLcode Curl_cf_setup_insert_after(struct Curl_cfilter *cf_at,
462
                                    struct Curl_easy *data,
463
                                    uint8_t transport,
464
                                    int ssl_mode)
465
0
{
466
0
  struct Curl_cfilter *cf;
467
0
  CURLcode result;
468
469
0
  DEBUGASSERT(data);
470
0
  result = cf_setup_create(&cf, data, transport, ssl_mode);
471
0
  if(result)
472
0
    goto out;
473
0
  Curl_conn_cf_insert_after(cf_at, cf);
474
0
out:
475
0
  return result;
476
0
}