Coverage Report

Created: 2026-07-16 06:34

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_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
81.7k
{
62
81.7k
  struct cf_setup_ctx *ctx = cf->ctx;
63
81.7k
  CURLcode result = CURLE_OK;
64
65
81.7k
  if(ctx->state < CF_SETUP_CNNCT_HAPROXY) {
66
81.1k
    if(data->set.haproxyprotocol) {
67
579
      if(ctx->transport == TRNSPRT_QUIC) {
68
0
        failf(data, "haproxy protocol does not support QUIC");
69
0
        return CURLE_UNSUPPORTED_PROTOCOL;
70
0
      }
71
579
      result = Curl_cf_haproxy_insert_after(cf, data);
72
579
      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
579
      CURL_TRC_CF(data, cf, "added HAPROXY filter");
78
579
    }
79
81.1k
    ctx->state = CF_SETUP_CNNCT_HAPROXY;
80
81.1k
  }
81
81.7k
  return result;
82
81.7k
}
83
84
static CURLcode cf_setup_add_socks(struct Curl_cfilter *cf,
85
                                   struct Curl_easy *data)
86
112k
{
87
112k
  struct cf_setup_ctx *ctx = cf->ctx;
88
112k
  CURLcode result = CURLE_OK;
89
112k
  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
1.68k
    struct Curl_peer *second_peer;
92
93
1.68k
    if(cf->conn->http_proxy.peer)
94
346
      second_peer = cf->conn->http_proxy.peer;
95
1.34k
    else
96
1.34k
      second_peer = Curl_conn_get_destination(cf->conn, cf->sockindex);
97
1.68k
    if(!second_peer)
98
0
      return CURLE_FAILED_INIT;
99
100
1.68k
    result = Curl_cf_socks_proxy_insert_after(
101
1.68k
      cf, data, second_peer, cf->conn->ip_version,
102
1.68k
      cf->conn->socks_proxy.proxytype,
103
1.68k
      cf->conn->socks_proxy.creds);
104
1.68k
    if(result) {
105
0
      CURL_TRC_CF(data, cf, "adding SOCKS filter failed -> %d", (int)result);
106
0
      return result;
107
0
    }
108
109
1.68k
    CURL_TRC_CF(data, cf, "added SOCKS filter to %s:%u",
110
1.68k
                second_peer->hostname, second_peer->port);
111
1.68k
    ctx->state = CF_SETUP_CNNCT_SOCKS;
112
1.68k
  }
113
112k
  return result;
114
112k
}
115
116
#ifndef CURL_DISABLE_HTTP
117
static CURLcode cf_setup_add_http_proxy(struct Curl_cfilter *cf,
118
                                        struct Curl_easy *data)
119
110k
{
120
110k
  struct cf_setup_ctx *ctx = cf->ctx;
121
110k
  CURLcode result = CURLE_OK;
122
123
110k
  if(ctx->state < CF_SETUP_CNNCT_HTTP_PROXY &&
124
109k
     cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
125
28.8k
    struct Curl_peer *peer = cf->conn->http_proxy.peer;
126
28.8k
    struct Curl_peer *tunnel_peer =
127
28.8k
      Curl_conn_get_destination(cf->conn, cf->sockindex);
128
129
28.8k
#ifdef USE_SSL
130
28.8k
    if(CURL_PROXY_IS_HTTPS(cf->conn->http_proxy.proxytype) &&
131
12.5k
       !Curl_conn_is_ssl(cf->conn, cf->sockindex)) {
132
12.5k
      result = Curl_cf_ssl_proxy_insert_after(
133
12.5k
        cf, data, cf->conn->http_proxy.peer);
134
12.5k
      if(result) {
135
38
        CURL_TRC_CF(data, cf, "adding SSL filter for HTTP proxy failed -> %d",
136
38
                    (int)result);
137
38
        return result;
138
38
      }
139
12.5k
      CURL_TRC_CF(data, cf, "added SSL filter for HTTP proxy");
140
12.5k
    }
141
28.7k
#endif /* USE_SSL */
142
143
28.7k
    result = Curl_cf_http_proxy_insert_after(
144
28.7k
      cf, data, peer, tunnel_peer,
145
28.7k
      ctx->transport, cf->conn->http_proxy.proxytype);
146
28.7k
    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
28.7k
    CURL_TRC_CF(data, cf, "added HTTP proxy tunnel filter");
152
28.7k
    ctx->state = CF_SETUP_CNNCT_HTTP_PROXY;
153
28.7k
  }
154
110k
  return result;
155
110k
}
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
116k
{
164
116k
#ifndef CURL_DISABLE_PROXY
165
116k
  if(conn->socks_proxy.peer)
166
2.30k
    return conn->socks_proxy.peer;
167
113k
  if(conn->http_proxy.peer)
168
31.0k
    return conn->http_proxy.peer;
169
82.8k
#endif
170
82.8k
  return (sockindex == SECONDARYSOCKET) ? conn->origin2 : conn->origin;
171
113k
}
172
173
static CURLcode cf_setup_add_ip_happy(struct Curl_cfilter *cf,
174
                                      struct Curl_easy *data)
175
228k
{
176
228k
  struct cf_setup_ctx *ctx = cf->ctx;
177
228k
  CURLcode result = CURLE_OK;
178
179
228k
  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
116k
    struct Curl_peer *first_origin =
184
116k
      conn_get_first_origin(cf->conn, cf->sockindex);
185
116k
    struct Curl_peer *first_peer =
186
116k
      Curl_conn_get_first_peer(cf->conn, cf->sockindex);
187
116k
    struct Curl_peer *tunnel_peer = NULL;
188
116k
    uint8_t first_transport = ctx->transport;
189
190
116k
    if(!first_peer)
191
0
      return CURLE_FAILED_INIT;
192
193
116k
#if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
194
116k
    if(cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
195
29.4k
      first_transport =
196
29.4k
        Curl_http_proxy_transport(cf->conn->http_proxy.proxytype);
197
29.4k
      tunnel_peer = Curl_conn_get_destination(cf->conn, cf->sockindex);
198
29.4k
      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
29.4k
    }
203
116k
#endif /* !CURL_DISABLE_PROXY && !CURL_DISABLE_HTTP */
204
205
116k
    result = Curl_cf_ip_happy_insert_after(cf, data, first_origin, first_peer,
206
116k
                                           first_transport,
207
116k
                                           tunnel_peer, ctx->transport);
208
116k
    if(result) {
209
0
      CURL_TRC_CF(data, cf, "adding happy eyeballs failed -> %d", (int)result);
210
0
      return result;
211
0
    }
212
213
116k
    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
116k
    else {
219
116k
      CURL_TRC_CF(data, cf, "happy eyeballing to %s %s:%u",
220
116k
                  tunnel_peer ? "proxy" : "origin",
221
116k
                  first_peer->hostname, first_peer->port);
222
116k
      ctx->state = CF_SETUP_CNNCT_EYEBALLS;
223
116k
    }
224
116k
  }
225
228k
  return result;
226
228k
}
227
228
static CURLcode cf_setup_add_origin_filters(struct Curl_cfilter *cf,
229
                                            struct Curl_easy *data)
230
81.1k
{
231
81.1k
  struct cf_setup_ctx *ctx = cf->ctx;
232
81.1k
  CURLcode result = CURLE_OK;
233
234
81.1k
  (void)data; /* not used in all builds */
235
81.1k
  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
81.1k
#ifdef USE_SSL
264
81.1k
    if((ctx->ssl_mode == CURL_CF_SSL_ENABLE ||
265
78.4k
        (ctx->ssl_mode != CURL_CF_SSL_DISABLE &&
266
77.4k
         cf->conn->scheme->flags & PROTOPT_SSL)) && /* we want SSL */
267
20.5k
       !Curl_conn_is_ssl(cf->conn, cf->sockindex)) { /* it is missing */
268
269
20.5k
#ifndef CURL_DISABLE_PROXY
270
20.5k
      if(cf->conn->bits.origin_is_proxy) {
271
3
        result = Curl_cf_ssl_proxy_insert_after(cf, data, cf->conn->origin);
272
3
      }
273
20.5k
      else
274
20.5k
#endif
275
20.5k
      {
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 same peers as FIRSTSOCKET. See #22225
281
         * Meaning: cf->sockindex does not matter here. */
282
20.5k
        result = Curl_cf_ssl_insert_after(cf, data,
283
20.5k
          Curl_conn_get_origin(cf->conn, FIRSTSOCKET),
284
20.5k
          Curl_conn_get_destination(cf->conn, FIRSTSOCKET));
285
20.5k
      }
286
20.5k
      if(result) {
287
115
        CURL_TRC_CF(data, cf, "adding SSL filter for origin failed -> %d",
288
115
                    (int)result);
289
115
        return result;
290
115
      }
291
20.4k
      CURL_TRC_CF(data, cf, "added SSL filter for origin");
292
20.4k
    }
293
81.0k
#endif /* USE_SSL */
294
81.0k
    ctx->state = CF_SETUP_CNNCT_SSL;
295
81.0k
  }
296
81.0k
  return result;
297
81.1k
}
298
299
static CURLcode cf_setup_connect_steps(struct Curl_cfilter *cf,
300
                                       struct Curl_easy *data,
301
                                       bool *done)
302
128k
{
303
128k
  struct cf_setup_ctx *ctx = cf->ctx;
304
128k
  CURLcode result = CURLE_OK;
305
306
128k
  if(cf->connected) {
307
0
    *done = TRUE;
308
0
    return CURLE_OK;
309
0
  }
310
311
  /* connect current sub-chain */
312
296k
connect_sub_chain:
313
296k
  VERBOSE(Curl_conn_trc_filters(data, cf->sockindex, "cf_setup_connect"));
314
315
296k
  if(cf->next && !cf->next->connected) {
316
180k
    result = Curl_conn_cf_connect(cf->next, data, done);
317
180k
    if(result || !*done)
318
67.8k
      return result;
319
180k
  }
320
321
228k
  result = cf_setup_add_ip_happy(cf, data);
322
228k
  if(result)
323
0
    return result;
324
228k
  if(!cf->next || !cf->next->connected)
325
116k
    goto connect_sub_chain;
326
327
112k
#ifndef CURL_DISABLE_PROXY
328
112k
  result = cf_setup_add_socks(cf, data);
329
112k
  if(result)
330
0
    return result;
331
112k
  if(!cf->next || !cf->next->connected)
332
1.68k
    goto connect_sub_chain;
333
334
110k
#ifndef CURL_DISABLE_HTTP
335
110k
  result = cf_setup_add_http_proxy(cf, data);
336
110k
  if(result)
337
38
    return result;
338
110k
  if(!cf->next || !cf->next->connected)
339
28.7k
    goto connect_sub_chain;
340
81.7k
#endif /* !CURL_DISABLE_HTTP */
341
342
81.7k
  result = cf_setup_add_haproxy(cf, data);
343
81.7k
  if(result)
344
0
    return result;
345
81.7k
  if(!cf->next || !cf->next->connected)
346
579
    goto connect_sub_chain;
347
81.1k
#endif /* !CURL_DISABLE_PROXY */
348
349
81.1k
  result = cf_setup_add_origin_filters(cf, data);
350
81.1k
  if(result)
351
115
    return result;
352
81.0k
  if(!cf->next || !cf->next->connected)
353
20.4k
    goto connect_sub_chain;
354
355
60.6k
  ctx->state = CF_SETUP_DONE;
356
60.6k
  cf->connected = TRUE;
357
60.6k
  *done = TRUE;
358
60.6k
  return CURLE_OK;
359
81.0k
}
360
361
static CURLcode cf_setup_connect(struct Curl_cfilter *cf,
362
                                 struct Curl_easy *data,
363
                                 bool *done)
364
128k
{
365
128k
  struct cf_setup_ctx *ctx = cf->ctx;
366
128k
  CURLcode result;
367
368
  /* In some situations, a server/proxy may close the connection and
369
   * we need to connect again (HTTP/1.x proxy auth, for example).
370
   * We used to close the filters and reuse them for another attempt,
371
   * however that complicates filter code and it is simpler to tear them
372
   * all down and start over. */
373
128k
retry:
374
128k
  result = cf_setup_connect_steps(cf, data, done);
375
376
128k
  if(result == CURLE_AGAIN) {
377
0
    ++ctx->retry_count;
378
0
    if(ctx->retry_count > 5) /* arbitrary limit, better just timeout? */
379
0
      return CURLE_COULDNT_CONNECT;
380
381
0
    CURL_TRC_CF(data, cf, "retrying connect, %d. time", ctx->retry_count);
382
0
    Curl_conn_cf_discard_chain(&cf->next, data);
383
0
    ctx->state = CF_SETUP_INIT;
384
0
    goto retry;
385
0
  }
386
128k
  return result;
387
128k
}
388
389
static void cf_setup_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
390
121k
{
391
121k
  struct cf_setup_ctx *ctx = cf->ctx;
392
393
121k
  CURL_TRC_CF(data, cf, "destroy");
394
121k
  curlx_safefree(ctx);
395
121k
}
396
397
struct Curl_cftype Curl_cft_setup = {
398
  "SETUP",
399
  CF_TYPE_SETUP,
400
  CURL_LOG_LVL_NONE,
401
  cf_setup_destroy,
402
  cf_setup_connect,
403
  Curl_cf_def_shutdown,
404
  Curl_cf_def_adjust_pollset,
405
  Curl_cf_def_data_pending,
406
  Curl_cf_def_send,
407
  Curl_cf_def_recv,
408
  Curl_cf_def_cntrl,
409
  Curl_cf_def_conn_is_alive,
410
  Curl_cf_def_conn_keep_alive,
411
  Curl_cf_def_query,
412
};
413
414
static CURLcode cf_setup_create(struct Curl_cfilter **pcf,
415
                                struct Curl_easy *data,
416
                                uint8_t transport,
417
                                int ssl_mode)
418
121k
{
419
121k
  struct Curl_cfilter *cf = NULL;
420
121k
  struct cf_setup_ctx *ctx;
421
121k
  CURLcode result = CURLE_OK;
422
423
121k
  (void)data;
424
121k
  ctx = curlx_calloc(1, sizeof(*ctx));
425
121k
  if(!ctx) {
426
0
    result = CURLE_OUT_OF_MEMORY;
427
0
    goto out;
428
0
  }
429
121k
  ctx->state = CF_SETUP_INIT;
430
121k
  ctx->ssl_mode = ssl_mode;
431
121k
  ctx->transport = transport;
432
433
121k
  result = Curl_cf_create(&cf, &Curl_cft_setup, ctx);
434
121k
  if(result)
435
0
    goto out;
436
121k
  ctx = NULL;
437
438
121k
out:
439
121k
  *pcf = result ? NULL : cf;
440
121k
  if(ctx) {
441
0
    curlx_free(ctx);
442
0
  }
443
121k
  return result;
444
121k
}
445
446
CURLcode Curl_cf_setup_add(struct Curl_easy *data,
447
                           struct connectdata *conn,
448
                           int sockindex,
449
                           uint8_t transport,
450
                           int ssl_mode)
451
115k
{
452
115k
  struct Curl_cfilter *cf;
453
115k
  CURLcode result = CURLE_OK;
454
455
115k
  DEBUGASSERT(data);
456
115k
  result = cf_setup_create(&cf, data, transport, ssl_mode);
457
115k
  if(result)
458
0
    goto out;
459
115k
  Curl_conn_cf_add(data, conn, sockindex, cf);
460
115k
out:
461
115k
  return result;
462
115k
}
463
464
CURLcode Curl_cf_setup_insert_after(struct Curl_cfilter *cf_at,
465
                                    struct Curl_easy *data,
466
                                    uint8_t transport,
467
                                    int ssl_mode)
468
6.38k
{
469
6.38k
  struct Curl_cfilter *cf;
470
6.38k
  CURLcode result;
471
472
6.38k
  DEBUGASSERT(data);
473
6.38k
  result = cf_setup_create(&cf, data, transport, ssl_mode);
474
6.38k
  if(result)
475
0
    goto out;
476
6.38k
  Curl_conn_cf_insert_after(cf_at, cf);
477
6.38k
out:
478
6.38k
  return result;
479
6.38k
}