Coverage Report

Created: 2026-10-22 06:56

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
660k
{
62
660k
  struct cf_setup_ctx *ctx = cf->ctx;
63
660k
  CURLcode result = CURLE_OK;
64
65
660k
  if(ctx->state < CF_SETUP_CNNCT_HAPROXY) {
66
544k
    if(data->set.haproxyprotocol) {
67
1.35k
      if(ctx->transport == TRNSPRT_QUIC) {
68
0
        failf(data, "haproxy protocol does not support QUIC");
69
0
        return CURLE_UNSUPPORTED_PROTOCOL;
70
0
      }
71
1.35k
      result = Curl_cf_haproxy_insert_after(cf, data);
72
1.35k
      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
1.35k
      CURL_TRC_CF(data, cf, "added HAPROXY filter");
78
1.35k
    }
79
544k
    ctx->state = CF_SETUP_CNNCT_HAPROXY;
80
544k
  }
81
660k
  return result;
82
660k
}
83
84
static CURLcode cf_setup_add_socks(struct Curl_cfilter *cf,
85
                                   struct Curl_easy *data)
86
719k
{
87
719k
  struct cf_setup_ctx *ctx = cf->ctx;
88
719k
  CURLcode result = CURLE_OK;
89
719k
  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
17.7k
    struct Curl_peer *second_peer;
92
93
17.7k
    if(cf->conn->http_proxy.peer)
94
793
      second_peer = cf->conn->http_proxy.peer;
95
16.9k
    else
96
16.9k
      second_peer = Curl_conn_get_destination(cf->conn, cf->sockindex);
97
17.7k
    if(!second_peer)
98
0
      return CURLE_FAILED_INIT;
99
100
17.7k
    result = Curl_cf_socks_proxy_insert_after(
101
17.7k
      cf, data, second_peer, cf->conn->ip_version,
102
17.7k
      cf->conn->socks_proxy.proxytype,
103
17.7k
      cf->conn->socks_proxy.creds);
104
17.7k
    if(result) {
105
5
      CURL_TRC_CF(data, cf, "adding SOCKS filter failed -> %d", (int)result);
106
5
      return result;
107
5
    }
108
109
17.7k
    CURL_TRC_CF(data, cf, "added SOCKS filter to %s:%u",
110
17.7k
                second_peer->hostname, second_peer->port);
111
17.7k
    ctx->state = CF_SETUP_CNNCT_SOCKS;
112
17.7k
  }
113
719k
  return result;
114
719k
}
115
116
#ifndef CURL_DISABLE_HTTP
117
static CURLcode cf_setup_add_http_proxy(struct Curl_cfilter *cf,
118
                                        struct Curl_easy *data)
119
701k
{
120
701k
  struct cf_setup_ctx *ctx = cf->ctx;
121
701k
  CURLcode result = CURLE_OK;
122
123
701k
  if(ctx->state < CF_SETUP_CNNCT_HTTP_PROXY &&
124
577k
     cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
125
41.4k
    struct Curl_peer *peer = cf->conn->http_proxy.peer;
126
41.4k
    struct Curl_peer *tunnel_peer =
127
41.4k
      Curl_conn_get_destination(cf->conn, cf->sockindex);
128
129
41.4k
#ifdef USE_SSL
130
41.4k
    if(CURL_PROXY_IS_HTTPS(cf->conn->http_proxy.proxytype) &&
131
12.4k
       !Curl_conn_is_ssl(cf->conn, cf->sockindex)) {
132
12.4k
      result = Curl_cf_ssl_proxy_insert_after(
133
12.4k
        cf, data, cf->conn->http_proxy.peer);
134
12.4k
      if(result) {
135
14
        CURL_TRC_CF(data, cf, "adding SSL filter for HTTP proxy failed -> %d",
136
14
                    (int)result);
137
14
        return result;
138
14
      }
139
12.3k
      CURL_TRC_CF(data, cf, "added SSL filter for HTTP proxy");
140
12.3k
    }
141
41.4k
#endif /* USE_SSL */
142
143
41.4k
    result = Curl_cf_http_proxy_insert_after(
144
41.4k
      cf, data, peer, tunnel_peer,
145
41.4k
      ctx->transport, cf->conn->http_proxy.proxytype);
146
41.4k
    if(result) {
147
9
      CURL_TRC_CF(data, cf, "adding HTTP proxy tunnel filter failed -> %d",
148
9
                  (int)result);
149
9
      return result;
150
9
    }
151
41.3k
    CURL_TRC_CF(data, cf, "added HTTP proxy tunnel filter");
152
41.3k
    ctx->state = CF_SETUP_CNNCT_HTTP_PROXY;
153
41.3k
  }
154
701k
  return result;
155
701k
}
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
                                             int8_t sockindex)
163
624k
{
164
624k
#ifndef CURL_DISABLE_PROXY
165
624k
  if(conn->socks_proxy.peer)
166
18.1k
    return conn->socks_proxy.peer;
167
606k
  if(conn->http_proxy.peer)
168
56.0k
    return conn->http_proxy.peer;
169
549k
#endif
170
549k
  return (sockindex == SECONDARYSOCKET) ? conn->origin2 : conn->origin;
171
606k
}
172
173
static CURLcode cf_setup_add_ip_happy(struct Curl_cfilter *cf,
174
                                      struct Curl_easy *data)
175
1.34M
{
176
1.34M
  struct cf_setup_ctx *ctx = cf->ctx;
177
1.34M
  CURLcode result = CURLE_OK;
178
179
1.34M
  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
624k
    struct Curl_peer *first_origin =
184
624k
      conn_get_first_origin(cf->conn, cf->sockindex);
185
624k
    struct Curl_peer *first_peer =
186
624k
      Curl_conn_get_first_peer(cf->conn, cf->sockindex);
187
624k
    struct Curl_peer *tunnel_peer = NULL;
188
624k
    uint8_t first_transport = ctx->transport;
189
190
624k
    if(!first_peer)
191
0
      return CURLE_FAILED_INIT;
192
193
624k
#if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
194
624k
    if(cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
195
42.2k
      first_transport =
196
42.2k
        Curl_http_proxy_transport(cf->conn->http_proxy.proxytype);
197
42.2k
      tunnel_peer = Curl_conn_get_destination(cf->conn, cf->sockindex);
198
42.2k
      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
42.2k
    }
203
624k
#endif /* !CURL_DISABLE_PROXY && !CURL_DISABLE_HTTP */
204
205
624k
    result = Curl_cf_ip_happy_insert_after(cf, data, first_origin, first_peer,
206
624k
                                           first_transport,
207
624k
                                           tunnel_peer, ctx->transport);
208
624k
    if(result) {
209
120
      CURL_TRC_CF(data, cf, "adding happy eyeballs failed -> %d", (int)result);
210
120
      return result;
211
120
    }
212
213
624k
    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
624k
    else {
219
624k
      CURL_TRC_CF(data, cf, "happy eyeballing to %s %s:%u",
220
624k
                  tunnel_peer ? "proxy" : "origin",
221
624k
                  first_peer->hostname, first_peer->port);
222
624k
      ctx->state = CF_SETUP_CNNCT_EYEBALLS;
223
624k
    }
224
624k
  }
225
1.34M
  return result;
226
1.34M
}
227
228
static CURLcode cf_setup_add_origin_filters(struct Curl_cfilter *cf,
229
                                            struct Curl_easy *data)
230
658k
{
231
658k
  struct cf_setup_ctx *ctx = cf->ctx;
232
658k
  CURLcode result = CURLE_OK;
233
234
658k
  (void)data; /* not used in all builds */
235
658k
  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
5.46k
    if(ctx->transport == TRNSPRT_QUIC &&
243
5.46k
       cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
244
1.31k
      struct Curl_peer *origin = Curl_conn_get_origin(cf->conn, cf->sockindex);
245
1.31k
      struct Curl_peer *peer =
246
1.31k
        Curl_conn_get_destination(cf->conn, cf->sockindex);
247
248
1.31k
      result = Curl_cf_capsule_insert_after(cf, data);
249
1.31k
      if(result) {
250
3
        CURL_TRC_CF(data, cf, "adding capsule filter failed -> %d",
251
3
                    (int)result);
252
3
        return result;
253
3
      }
254
1.31k
      result = Curl_cf_quic_insert_after(cf, data, origin, peer);
255
1.31k
      if(result) {
256
4
        CURL_TRC_CF(data, cf, "adding QUIC filter failed -> %d", (int)result);
257
4
        return result;
258
4
      }
259
1.31k
      CURL_TRC_CF(data, cf, "added QUIC filter for origin");
260
1.31k
    }
261
4.14k
    else
262
4.14k
#endif /* !CURL_DISABLE_HTTP && USE_HTTP3 && CURL_DISABLE_PROXY */
263
4.14k
#ifdef USE_SSL
264
542k
    if((ctx->ssl_mode == CURL_CF_SSL_ENABLE ||
265
426k
        (ctx->ssl_mode != CURL_CF_SSL_DISABLE &&
266
420k
         cf->conn->scheme->flags & PROTOPT_SSL)) && /* we want SSL */
267
158k
       !Curl_conn_is_ssl(cf->conn, cf->sockindex)) { /* it is missing */
268
269
154k
#ifndef CURL_DISABLE_PROXY
270
154k
      if(cf->conn->bits.origin_is_proxy) {
271
0
        result = Curl_cf_ssl_proxy_insert_after(cf, data, cf->conn->origin);
272
0
      }
273
154k
      else
274
154k
#endif
275
154k
      {
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
154k
        result = Curl_cf_ssl_insert_after(cf, data,
283
154k
          Curl_conn_get_origin(cf->conn, FIRSTSOCKET),
284
154k
          Curl_conn_get_destination(cf->conn, FIRSTSOCKET));
285
154k
      }
286
154k
      if(result) {
287
411
        CURL_TRC_CF(data, cf, "adding SSL filter for origin failed -> %d",
288
411
                    (int)result);
289
411
        return result;
290
411
      }
291
154k
      CURL_TRC_CF(data, cf, "added SSL filter for origin");
292
154k
    }
293
543k
#endif /* USE_SSL */
294
543k
    ctx->state = CF_SETUP_CNNCT_SSL;
295
543k
  }
296
658k
  return result;
297
658k
}
cf-setup.c:cf_setup_add_origin_filters
Line
Count
Source
230
653k
{
231
653k
  struct cf_setup_ctx *ctx = cf->ctx;
232
653k
  CURLcode result = CURLE_OK;
233
234
653k
  (void)data; /* not used in all builds */
235
653k
  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, 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
538k
#ifdef USE_SSL
264
538k
    if((ctx->ssl_mode == CURL_CF_SSL_ENABLE ||
265
426k
        (ctx->ssl_mode != CURL_CF_SSL_DISABLE &&
266
420k
         cf->conn->scheme->flags & PROTOPT_SSL)) && /* we want SSL */
267
154k
       !Curl_conn_is_ssl(cf->conn, cf->sockindex)) { /* it is missing */
268
269
154k
#ifndef CURL_DISABLE_PROXY
270
154k
      if(cf->conn->bits.origin_is_proxy) {
271
0
        result = Curl_cf_ssl_proxy_insert_after(cf, data, cf->conn->origin);
272
0
      }
273
154k
      else
274
154k
#endif
275
154k
      {
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
154k
        result = Curl_cf_ssl_insert_after(cf, data,
283
154k
          Curl_conn_get_origin(cf->conn, FIRSTSOCKET),
284
154k
          Curl_conn_get_destination(cf->conn, FIRSTSOCKET));
285
154k
      }
286
154k
      if(result) {
287
411
        CURL_TRC_CF(data, cf, "adding SSL filter for origin failed -> %d",
288
411
                    (int)result);
289
411
        return result;
290
411
      }
291
154k
      CURL_TRC_CF(data, cf, "added SSL filter for origin");
292
154k
    }
293
538k
#endif /* USE_SSL */
294
538k
    ctx->state = CF_SETUP_CNNCT_SSL;
295
538k
  }
296
653k
  return result;
297
653k
}
cf-setup.c:cf_setup_add_origin_filters
Line
Count
Source
230
5.46k
{
231
5.46k
  struct cf_setup_ctx *ctx = cf->ctx;
232
5.46k
  CURLcode result = CURLE_OK;
233
234
5.46k
  (void)data; /* not used in all builds */
235
5.46k
  if(ctx->state < CF_SETUP_CNNCT_SSL) {
236
5.46k
#if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3) && \
237
5.46k
    !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
5.46k
    if(ctx->transport == TRNSPRT_QUIC &&
243
5.46k
       cf->conn->http_proxy.peer && !cf->conn->bits.origin_is_proxy) {
244
1.31k
      struct Curl_peer *origin = Curl_conn_get_origin(cf->conn, cf->sockindex);
245
1.31k
      struct Curl_peer *peer =
246
1.31k
        Curl_conn_get_destination(cf->conn, cf->sockindex);
247
248
1.31k
      result = Curl_cf_capsule_insert_after(cf, data);
249
1.31k
      if(result) {
250
3
        CURL_TRC_CF(data, cf, "adding capsule filter failed -> %d",
251
3
                    (int)result);
252
3
        return result;
253
3
      }
254
1.31k
      result = Curl_cf_quic_insert_after(cf, data, origin, peer);
255
1.31k
      if(result) {
256
4
        CURL_TRC_CF(data, cf, "adding QUIC filter failed -> %d", (int)result);
257
4
        return result;
258
4
      }
259
1.31k
      CURL_TRC_CF(data, cf, "added QUIC filter for origin");
260
1.31k
    }
261
4.14k
    else
262
4.14k
#endif /* !CURL_DISABLE_HTTP && USE_HTTP3 && CURL_DISABLE_PROXY */
263
4.14k
#ifdef USE_SSL
264
4.14k
    if((ctx->ssl_mode == CURL_CF_SSL_ENABLE ||
265
0
        (ctx->ssl_mode != CURL_CF_SSL_DISABLE &&
266
0
         cf->conn->scheme->flags & PROTOPT_SSL)) && /* we want SSL */
267
4.14k
       !Curl_conn_is_ssl(cf->conn, cf->sockindex)) { /* it is missing */
268
269
0
#ifndef CURL_DISABLE_PROXY
270
0
      if(cf->conn->bits.origin_is_proxy) {
271
0
        result = Curl_cf_ssl_proxy_insert_after(cf, data, cf->conn->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 same peers as FIRSTSOCKET. See #22225
281
         * Meaning: cf->sockindex does not matter here. */
282
0
        result = Curl_cf_ssl_insert_after(cf, data,
283
0
          Curl_conn_get_origin(cf->conn, FIRSTSOCKET),
284
0
          Curl_conn_get_destination(cf->conn, FIRSTSOCKET));
285
0
      }
286
0
      if(result) {
287
0
        CURL_TRC_CF(data, cf, "adding SSL filter for origin failed -> %d",
288
0
                    (int)result);
289
0
        return result;
290
0
      }
291
0
      CURL_TRC_CF(data, cf, "added SSL filter for origin");
292
0
    }
293
5.45k
#endif /* USE_SSL */
294
5.45k
    ctx->state = CF_SETUP_CNNCT_SSL;
295
5.45k
  }
296
5.45k
  return result;
297
5.46k
}
298
299
static CURLcode cf_setup_connect_steps(struct Curl_cfilter *cf,
300
                                       struct Curl_easy *data,
301
                                       bool *done)
302
961k
{
303
961k
  struct cf_setup_ctx *ctx = cf->ctx;
304
961k
  CURLcode result = CURLE_OK;
305
306
961k
  if(cf->connected) {
307
0
    *done = TRUE;
308
0
    return CURLE_OK;
309
0
  }
310
311
  /* connect current sub-chain */
312
1.80M
connect_sub_chain:
313
1.80M
  VERBOSE(Curl_conn_trc_filters(data, cf->sockindex, "cf_setup_connect"));
314
315
1.80M
  if(cf->next && !cf->next->connected) {
316
1.17M
    result = Curl_conn_cf_connect(cf->next, data, done);
317
1.17M
    if(result || !*done)
318
457k
      return result;
319
1.17M
  }
320
321
1.34M
  result = cf_setup_add_ip_happy(cf, data);
322
1.34M
  if(result)
323
120
    return result;
324
1.34M
  if(!cf->next || !cf->next->connected)
325
624k
    goto connect_sub_chain;
326
327
719k
#ifndef CURL_DISABLE_PROXY
328
719k
  result = cf_setup_add_socks(cf, data);
329
719k
  if(result)
330
5
    return result;
331
719k
  if(!cf->next || !cf->next->connected)
332
17.7k
    goto connect_sub_chain;
333
334
701k
#ifndef CURL_DISABLE_HTTP
335
701k
  result = cf_setup_add_http_proxy(cf, data);
336
701k
  if(result)
337
23
    return result;
338
701k
  if(!cf->next || !cf->next->connected)
339
41.3k
    goto connect_sub_chain;
340
660k
#endif /* !CURL_DISABLE_HTTP */
341
342
660k
  result = cf_setup_add_haproxy(cf, data);
343
660k
  if(result)
344
0
    return result;
345
660k
  if(!cf->next || !cf->next->connected)
346
1.35k
    goto connect_sub_chain;
347
658k
#endif /* !CURL_DISABLE_PROXY */
348
349
658k
  result = cf_setup_add_origin_filters(cf, data);
350
658k
  if(result)
351
418
    return result;
352
658k
  if(!cf->next || !cf->next->connected)
353
155k
    goto connect_sub_chain;
354
355
503k
  ctx->state = CF_SETUP_DONE;
356
503k
  cf->connected = TRUE;
357
503k
  *done = TRUE;
358
503k
  return CURLE_OK;
359
658k
}
360
361
static CURLcode cf_setup_connect(struct Curl_cfilter *cf,
362
                                 struct Curl_easy *data,
363
                                 bool *done)
364
961k
{
365
961k
  struct cf_setup_ctx *ctx = cf->ctx;
366
961k
  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
961k
retry:
374
961k
  result = cf_setup_connect_steps(cf, data, done);
375
376
961k
  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
961k
  return result;
387
961k
}
388
389
static void cf_setup_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
390
628k
{
391
628k
  struct cf_setup_ctx *ctx = cf->ctx;
392
393
628k
  CURL_TRC_CF(data, cf, "destroy");
394
628k
  curlx_safefree(ctx);
395
628k
}
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
628k
{
419
628k
  struct Curl_cfilter *cf = NULL;
420
628k
  struct cf_setup_ctx *ctx;
421
628k
  CURLcode result = CURLE_OK;
422
423
628k
  (void)data;
424
628k
  ctx = curlx_calloc(1, sizeof(*ctx));
425
628k
  if(!ctx) {
426
46
    result = CURLE_OUT_OF_MEMORY;
427
46
    goto out;
428
46
  }
429
628k
  ctx->state = CF_SETUP_INIT;
430
628k
  ctx->ssl_mode = ssl_mode;
431
628k
  ctx->transport = transport;
432
433
628k
  result = Curl_cf_create(&cf, &Curl_cft_setup, ctx);
434
628k
  if(result)
435
44
    goto out;
436
628k
  ctx = NULL;
437
438
628k
out:
439
628k
  *pcf = result ? NULL : cf;
440
628k
  if(ctx) {
441
44
    curlx_free(ctx);
442
44
  }
443
628k
  return result;
444
628k
}
445
446
CURLcode Curl_cf_setup_add(struct Curl_easy *data,
447
                           struct connectdata *conn,
448
                           int8_t sockindex,
449
                           uint8_t transport,
450
                           int ssl_mode)
451
499k
{
452
499k
  struct Curl_cfilter *cf;
453
499k
  CURLcode result = CURLE_OK;
454
455
499k
  DEBUGASSERT(data);
456
499k
  result = cf_setup_create(&cf, data, transport, ssl_mode);
457
499k
  if(result)
458
71
    goto out;
459
499k
  Curl_conn_cf_add(data, conn, sockindex, cf);
460
499k
out:
461
499k
  return result;
462
499k
}
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
129k
{
469
129k
  struct Curl_cfilter *cf;
470
129k
  CURLcode result;
471
472
129k
  DEBUGASSERT(data);
473
129k
  result = cf_setup_create(&cf, data, transport, ssl_mode);
474
129k
  if(result)
475
19
    goto out;
476
129k
  Curl_conn_cf_insert_after(cf_at, cf);
477
129k
out:
478
129k
  return result;
479
129k
}