Coverage Report

Created: 2026-12-01 06:24

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