Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/connect.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 "curl_trc.h"
28
#include "strerror.h"
29
#include "cfilters.h"
30
#include "connect.h"
31
#include "cf-https-connect.h"
32
#include "cf-setup.h"
33
#include "multiif.h"
34
#include "progress.h"
35
#include "conncache.h"
36
#include "multihandle.h"
37
#include "select.h"
38
#include "vdns/cf-dns.h"
39
#include "curlx/strparse.h"
40
41
static struct Curl_peer *cfchain_get_destination(struct Curl_cfchain *chain)
42
2.06k
{
43
2.06k
  struct connectdata *conn = chain->conn;
44
2.06k
  return chain->primary ?
45
2.06k
    (conn->via_peer ? conn->via_peer : conn->origin) :
46
2.06k
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2);
47
2.06k
}
48
49
struct Curl_peer *Curl_cf_get_destination(struct Curl_cfilter *cf)
50
0
{
51
0
  return cfchain_get_destination(cf->chain);
52
0
}
53
54
static struct Curl_peer *cfchain_get_first_peer(struct Curl_cfchain *chain)
55
35.8k
{
56
35.8k
  struct connectdata *conn = chain->conn;
57
35.8k
#ifndef CURL_DISABLE_PROXY
58
35.8k
  if(conn->socks_proxy.peer)
59
0
    return conn->socks_proxy.peer;
60
35.8k
  if(conn->http_proxy.peer)
61
0
    return conn->http_proxy.peer;
62
35.8k
#endif
63
35.8k
  return !chain->primary ?
64
0
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2) :
65
35.8k
    (conn->via_peer ? conn->via_peer : conn->origin);
66
35.8k
}
67
68
struct Curl_peer *Curl_cf_get_first_peer(struct Curl_cfilter *cf)
69
17.7k
{
70
17.7k
  return cfchain_get_first_peer(cf->chain);
71
17.7k
}
72
73
struct Curl_peer *Curl_cfchain_get_via_peer(struct Curl_cfchain *chain)
74
0
{
75
0
  struct connectdata *conn = chain->conn;
76
0
  return chain->primary ? conn->via_peer : conn->via_peer2;
77
0
}
78
79
#if !defined(CURL_DISABLE_ALTSVC) || defined(USE_HTTPSRR)
80
81
enum alpnid Curl_alpn2alpnid(const unsigned char *name, size_t len)
82
2.19k
{
83
2.19k
  if(len == 2) {
84
2.19k
    if(!memcmp(name, "h1", 2))
85
0
      return ALPN_h1;
86
2.19k
    if(!memcmp(name, "h2", 2))
87
1.09k
      return ALPN_h2;
88
1.09k
    if(!memcmp(name, "h3", 2))
89
1.09k
      return ALPN_h3;
90
1.09k
  }
91
0
  else if(len == 8 && !memcmp(name, "http/1.1", 8))
92
0
    return ALPN_h1;
93
0
  return ALPN_none; /* unknown, probably rubbish input */
94
2.19k
}
95
96
#endif
97
98
/* Milliseconds left of the connect timeout. */
99
static timediff_t connect_left_ms(struct Curl_easy *data,
100
                                  const struct curltime *pnow)
101
0
{
102
0
  timediff_t timeout_ms = (data->set.connecttimeout > 0) ?
103
0
    data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT;
104
0
  timediff_t elapsed_ms =
105
0
    curlx_ptimediff_ms(pnow, &data->conn->created) -
106
0
    data->conn->connect_start_ms;
107
0
  timediff_t left_ms = timeout_ms - elapsed_ms;
108
0
  DEBUGASSERT(data->conn->connect_start_ms >= 0);
109
0
  return left_ms ? left_ms : -1; /* 0 is "no limit", fake 1 ms expiry */
110
0
}
111
112
/* Common code for timeleft_now_ms() and Curl_timeleft_total_now_ms():
113
   `with_connect` controls whether the connect timeout is considered. */
114
static timediff_t timeleft_now_ms_int(struct Curl_easy *data,
115
                                      const struct curltime *pnow,
116
                                      bool with_connect)
117
326k
{
118
326k
  struct Curl_cfchain *chain = data->conn ?
119
326k
                               &data->conn->chain : NULL;
120
326k
  timediff_t timeleft_ms = 0;
121
326k
  timediff_t ctimeleft_ms = 0;
122
123
326k
  if(chain && CURL_CFCHAIN_IN_SHUTDOWN(chain))
124
0
    return Curl_cshutdn_timeleft_ms(data, chain);
125
326k
  else if(with_connect && Curl_is_connecting(data)) {
126
    /* transfer has not reached DO phase yet */
127
118k
    timediff_t ctimeout_ms = (data->set.connecttimeout > 0) ?
128
88.1k
      data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT;
129
118k
    ctimeleft_ms = ctimeout_ms -
130
118k
      Curl_pgrs_since_ms(data, pnow, TIMER_STARTSINGLE);
131
118k
    if(!ctimeleft_ms)
132
0
      ctimeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
133
118k
  }
134
207k
  else if(with_connect && data->conn && data->conn->connect_start_ms >= 0) {
135
    /* ongoing connect of filters, pbly FTP second socket */
136
0
    ctimeleft_ms = connect_left_ms(data, pnow);
137
0
  }
138
207k
  else if(!data->set.timeout ||
139
205k
          (data->set.connect_only && !Curl_is_connecting(data))) {
140
    /* no timeout in place, or a CONNECT_ONLY transfer that is done
141
       connecting: return "no limit" */
142
2.13k
    return 0;
143
2.13k
  }
144
145
324k
  if(data->set.timeout) {
146
322k
    timeleft_ms = data->set.timeout -
147
322k
                  Curl_pgrs_since_ms(data, pnow, TIMER_STARTOP);
148
322k
    if(!timeleft_ms)
149
0
      timeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
150
322k
  }
151
152
324k
  if(!ctimeleft_ms)
153
205k
    return timeleft_ms;
154
118k
  else if(!timeleft_ms)
155
1.96k
    return ctimeleft_ms;
156
116k
  return CURLMIN(ctimeleft_ms, timeleft_ms);
157
324k
}
158
159
/*
160
 * timeleft_now_ms() returns the amount of milliseconds left allowed for the
161
 * transfer/connection. If the value is 0, there is no timeout (ie there is
162
 * infinite time left). If the value is negative, the timeout time has already
163
 * elapsed.
164
 *
165
 * @unittest 1303
166
 */
167
UNITTEST timediff_t timeleft_now_ms(struct Curl_easy *data,
168
                                    const struct curltime *pnow);
169
UNITTEST timediff_t timeleft_now_ms(struct Curl_easy *data,
170
                                    const struct curltime *pnow)
171
162k
{
172
162k
  return timeleft_now_ms_int(data, pnow, TRUE);
173
162k
}
174
175
timediff_t Curl_timeleft_ms(struct Curl_easy *data)
176
41.2k
{
177
41.2k
  return timeleft_now_ms(data, Curl_pgrs_now(data));
178
41.2k
}
179
180
/* Like Curl_timeleft_now_ms() but without the connect timeout: only the
181
   overall timeout is considered. */
182
timediff_t Curl_timeleft_total_now_ms(struct Curl_easy *data,
183
                                      const struct curltime *pnow)
184
163k
{
185
163k
  return timeleft_now_ms_int(data, pnow, FALSE);
186
163k
}
187
188
timediff_t Curl_timeleft_now_ms(struct Curl_easy *data,
189
                                const struct curltime *pnow)
190
121k
{
191
121k
  return timeleft_now_ms(data, pnow);
192
121k
}
193
194
/*
195
 * Used to extract socket and connectdata struct for the most recent
196
 * transfer on the given Curl_easy.
197
 *
198
 * The returned socket will be CURL_SOCKET_BAD in case of failure!
199
 */
200
curl_socket_t Curl_getconnectinfo(struct Curl_easy *data,
201
                                  struct connectdata **connp)
202
14.1k
{
203
  /* this works for an easy handle:
204
   * - that has been used for curl_easy_perform()
205
   * - that is associated with a multi handle, and whose connection
206
   *   was detached with CURLOPT_CONNECT_ONLY
207
   */
208
14.1k
  struct connectdata *conn = Curl_cpool_get_last_conn(data);
209
14.1k
  if(connp)
210
0
    *connp = conn;
211
14.1k
  return conn ? conn->chain.sock : CURL_SOCKET_BAD;
212
14.1k
}
213
214
void Curl_conncontrol(struct connectdata *conn, int ctrl)
215
43.4k
{
216
43.4k
  if(!conn) {
217
0
    DEBUGASSERT(0);
218
0
    return;
219
0
  }
220
43.4k
  switch(ctrl) {
221
0
    case CONNCTRL_CONN_KEEP:
222
0
      conn->bits.close = FALSE;
223
0
      break;
224
31.8k
    case CONNCTRL_CONN_CLOSE:
225
31.8k
      conn->bits.close = TRUE;
226
31.8k
      break;
227
11.5k
    case CONNCTRL_STREAM_CLOSE:
228
      /* stream close when multiplexing does not affect connection */
229
11.5k
      if(!Curl_conn_is_multiplex(conn))
230
4.59k
        conn->bits.close = TRUE;
231
11.5k
      break;
232
0
    default:
233
0
      DEBUGASSERT(0);
234
0
      break;
235
43.4k
  }
236
43.4k
}
237
238
CURLcode Curl_conn_setup(struct Curl_easy *data,
239
                         struct connectdata *conn,
240
                         struct Curl_cfchain *chain,
241
                         int ssl_mode)
242
18.0k
{
243
18.0k
  struct Curl_peer *first_peer = cfchain_get_first_peer(chain);
244
18.0k
  CURLcode result = CURLE_OK;
245
18.0k
  uint8_t dns_queries;
246
247
18.0k
  DEBUGASSERT(data);
248
18.0k
  DEBUGASSERT(conn->scheme);
249
18.0k
  DEBUGASSERT(!chain->cf);
250
251
18.0k
  if(!first_peer)
252
0
    return CURLE_FAILED_INIT;
253
254
18.0k
#ifndef CURL_DISABLE_HTTP
255
18.0k
  if(!chain->cf &&
256
18.0k
     conn->scheme->protocol == CURLPROTO_HTTPS) {
257
2.06k
    DEBUGASSERT(ssl_mode != CURL_CF_SSL_DISABLE);
258
2.06k
    result = Curl_cf_https_setup(
259
2.06k
      data, cfchain_get_destination(chain), chain);
260
2.06k
    if(result)
261
30
      goto out;
262
2.06k
  }
263
18.0k
#endif /* !CURL_DISABLE_HTTP */
264
265
  /* Still no cfilter set, apply default. */
266
18.0k
  if(!chain->cf) {
267
16.0k
    result = Curl_cf_setup_add(chain, data, conn->transport_wanted, ssl_mode);
268
16.0k
    if(result)
269
14
      goto out;
270
16.0k
  }
271
272
18.0k
  DEBUGASSERT(chain->cf);
273
  /* Whatever the filter chain will be in the end, it will need the
274
   * resolving of `first_peer`. Add that now so the resolve is started
275
   * right away. */
276
18.0k
  dns_queries = Curl_resolv_dns_queries(data, conn->ip_version);
277
18.0k
  result = Curl_dns_cf_add_addr_resolve(data, chain->cf,
278
18.0k
                                        first_peer, dns_queries,
279
18.0k
                                        conn->transport_wanted);
280
18.0k
  if(result)
281
23
    goto out;
282
283
18.0k
out:
284
18.0k
  return result;
285
18.0k
}
286
287
#ifdef CURLVERBOSE
288
static CURLcode conn_connect_trace(struct Curl_easy *data,
289
                                   struct Curl_cfilter *cf)
290
16.7k
{
291
16.7k
  if(Curl_trc_is_verbose(data)) {
292
2.62k
    struct ip_quadruple ipquad;
293
2.62k
    bool is_ipv6;
294
2.62k
    CURLcode result;
295
296
2.62k
    result = Curl_conn_cf_get_ip_info(cf, data, &is_ipv6, &ipquad);
297
2.62k
    if(result)
298
0
      return result;
299
300
2.62k
    infof(data, "Established %sconnection to %s (%s port %u) from %s port %u ",
301
2.62k
          !CF_IS_PRIMARY(cf) ? "2nd " : "",
302
2.62k
          CURL_CONN_HOST_DISPNAME(data->conn),
303
2.62k
          ipquad.remote_ip, ipquad.remote_port,
304
2.62k
          ipquad.local_ip, ipquad.local_port);
305
2.62k
  }
306
16.7k
  return CURLE_OK;
307
16.7k
}
308
#endif
309
310
/**
311
 * Update connection statistics
312
 */
313
static void conn_report_stats(struct Curl_easy *data,
314
                              struct Curl_cfchain *chain)
315
17.6k
{
316
  /* We only gather stats for the primary chain */
317
17.6k
  if(chain->primary)
318
17.6k
    Curl_cfchain_cntrl_report_stats(data, chain);
319
17.6k
}
320
321
void Curl_cfchain_report_failed(struct Curl_easy *data,
322
                                struct Curl_cfchain *chain)
323
895
{
324
895
  if(!chain->primary)
325
0
    return;
326
  /* Only filters connected at IP level have info to update. Filters
327
   * still racing for a connection, like happy eyeballing, pass it on
328
   * to a connected one only. */
329
895
  (void)Curl_conn_cf_cntrl(chain->cf, data, TRUE,
330
895
                           CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
331
895
  conn_report_stats(data, chain);
332
895
}
333
334
CURLcode Curl_cfchain_connect(struct Curl_easy *data,
335
                              struct Curl_cfchain *chain,
336
                              bool blocking,
337
                              bool *done)
338
45.9k
{
339
45.9k
#define CF_CONN_NUM_POLLS_ON_STACK 5
340
45.9k
  struct pollfd a_few_on_stack[CF_CONN_NUM_POLLS_ON_STACK];
341
45.9k
  struct easy_pollset ps;
342
45.9k
  struct curl_pollfds cpfds;
343
45.9k
  struct Curl_cfilter *cf;
344
45.9k
  CURLcode result = CURLE_OK;
345
346
45.9k
  DEBUGASSERT(data);
347
45.9k
  DEBUGASSERT(data->conn);
348
349
45.9k
  if(data->conn->scheme->flags & PROTOPT_NONETWORK) {
350
0
    *done = TRUE;
351
0
    return CURLE_OK;
352
0
  }
353
354
45.9k
  cf = chain->cf;
355
45.9k
  if(!cf) {
356
0
    *done = FALSE;
357
0
    return CURLE_FAILED_INIT;
358
0
  }
359
360
45.9k
  *done = (bool)cf->connected;
361
45.9k
  if(*done)
362
0
    return CURLE_OK;
363
364
45.9k
  if(data->conn->connect_start_ms < 0) {
365
17.9k
    data->conn->connect_start_ms =
366
17.9k
      curlx_ptimediff_ms(Curl_pgrs_now(data), &data->conn->created);
367
17.9k
  }
368
369
45.9k
  Curl_pollset_init(&ps);
370
45.9k
  Curl_pollfds_init(&cpfds, a_few_on_stack, CF_CONN_NUM_POLLS_ON_STACK);
371
45.9k
  while(!*done) {
372
45.9k
    if(Curl_cfchain_needs_flush(data, chain)) {
373
0
      DEBUGF(infof(data, "Curl_conn_connect(%s), flush",
374
0
             chain->primary ? "" : "2nd"));
375
0
      result = Curl_cfchain_flush(data, chain);
376
0
      if(result && (result != CURLE_AGAIN))
377
0
        goto out;
378
0
    }
379
380
45.9k
    result = cf->cft->do_connect(cf, data, done);
381
45.9k
    CURL_TRC_CF(data, cf, "Curl_conn_connect(block=%d) -> %d, done=%d",
382
45.9k
                blocking, (int)result, *done);
383
45.9k
    if(!result && *done) {
384
      /* A final sanity check on connection security */
385
16.7k
      if((data->state.origin->scheme->flags & PROTOPT_SSL) &&
386
1.38k
         chain->primary && !Curl_conn_is_ssl(data->conn)) {
387
0
        DEBUGASSERT(0);
388
0
        failf(data, "transfer requires SSL, but not connected via SSL");
389
0
        result = CURLE_FAILED_INIT;
390
0
        goto out;
391
0
      }
392
      /* Now that the complete filter chain is connected, let all filters
393
       * persist information at the connection. E.g. cf-socket sets the
394
       * socket and ip related information. */
395
16.7k
      Curl_conn_cntrl_update_info(data, data->conn);
396
16.7k
      conn_report_stats(data, chain);
397
16.7k
      Curl_cpool_conn_was_used(data, data->conn, Curl_pgrs_now(data));
398
16.7k
      VERBOSE(result = conn_connect_trace(data, cf));
399
16.7k
      VERBOSE(Curl_cfchain_trc(data, NULL, chain, "connected"));
400
16.7k
      Curl_cfchain_remove_setup_filters(data, chain);
401
16.7k
      VERBOSE(Curl_cfchain_trc(data, NULL, chain, "reduced to"));
402
16.7k
      data->conn->connect_start_ms = -1;
403
16.7k
      goto out;
404
16.7k
    }
405
29.2k
    else if(result) {
406
895
      CURL_TRC_CF(data, cf, "Curl_conn_connect(), filter returned %d",
407
895
                  (int)result);
408
895
      VERBOSE(Curl_cfchain_trc(data, NULL, chain, "failed to connect"));
409
895
      Curl_cfchain_report_failed(data, chain);
410
895
      goto out;
411
895
    }
412
413
28.3k
    if(!blocking)
414
28.3k
      goto out;
415
0
    else {
416
      /* check allowed time left */
417
0
      const struct curltime *pnow = Curl_pgrs_now(data);
418
0
      timediff_t timeout_ms = Curl_timeleft_now_ms(data, pnow);
419
0
      curl_socket_t sockfd = cf->chain->sock;
420
0
      int rc;
421
422
0
      if(timeout_ms < 0) {
423
        /* no need to continue if time already is up */
424
0
        failf(data, "connect timeout");
425
0
        result = CURLE_OPERATION_TIMEDOUT;
426
0
        goto out;
427
0
      }
428
429
0
      CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), do poll");
430
0
      Curl_pollset_reset(&ps);
431
0
      Curl_pollfds_reset(&cpfds);
432
      /* In general, we want to send after connect, wait on that. */
433
0
      if(sockfd != CURL_SOCKET_BAD)
434
0
        result = Curl_pollset_set_out_only(data, &ps, sockfd);
435
0
      if(!result)
436
0
        result = Curl_conn_adjust_pollset(data, data->conn, &ps);
437
0
      if(result)
438
0
        goto out;
439
0
      result = Curl_pollfds_add_ps(&cpfds, &ps);
440
0
      if(result)
441
0
        goto out;
442
443
0
      rc = Curl_poll(cpfds.pfds, cpfds.n,
444
0
                     CURLMIN(timeout_ms ? timeout_ms : 1000,
445
0
                             (cpfds.n ? 1000 : 10)));
446
0
      CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), Curl_poll() -> %d",
447
0
                  rc);
448
0
      if(rc < 0) {
449
0
        result = CURLE_COULDNT_CONNECT;
450
0
        goto out;
451
0
      }
452
      /* continue iterating */
453
0
    }
454
28.3k
  }
455
456
45.9k
out:
457
45.9k
  Curl_pollset_cleanup(&ps);
458
45.9k
  Curl_pollfds_cleanup(&cpfds);
459
45.9k
  return result;
460
45.9k
}
461
462
CURLcode Curl_conn_connect(struct Curl_easy *data,
463
                           bool blocking,
464
                           bool *done)
465
45.9k
{
466
45.9k
  return Curl_cfchain_connect(data, &data->conn->chain,
467
45.9k
                              blocking, done);
468
45.9k
}
469
470
void Curl_conn_set_multiplex(struct Curl_cfchain *chain, uint8_t http_version)
471
7.69k
{
472
7.69k
  if(chain->primary) {
473
7.69k
    chain->conn->httpversion_seen = http_version;
474
7.69k
    if(!chain->conn->bits.multiplex) {
475
7.69k
      chain->conn->bits.multiplex = TRUE;
476
7.69k
      if(chain->conn->attached_multi) {
477
7.69k
        Curl_multi_connchanged(chain->conn->attached_multi);
478
7.69k
      }
479
7.69k
    }
480
7.69k
  }
481
7.69k
}
482
483
struct Curl_peer *Curl_conn_get_origin(struct connectdata *conn)
484
1.68k
{
485
1.68k
  return conn->origin;
486
1.68k
}
487
488
struct Curl_peer *Curl_conn_get_destination(struct connectdata *conn)
489
20.4k
{
490
20.4k
  return conn->via_peer ? conn->via_peer : conn->origin;
491
20.4k
}
492
493
struct Curl_peer *Curl_conn_get_first_peer(struct connectdata *conn)
494
37.4k
{
495
37.4k
#ifndef CURL_DISABLE_PROXY
496
37.4k
  if(conn->socks_proxy.peer)
497
0
    return conn->socks_proxy.peer;
498
37.4k
  if(conn->http_proxy.peer)
499
0
    return conn->http_proxy.peer;
500
37.4k
#endif
501
37.4k
  return conn->via_peer ? conn->via_peer : conn->origin;
502
37.4k
}
503
504
struct Curl_peer *Curl_cf_get_origin(struct Curl_cfilter *cf)
505
0
{
506
0
  struct connectdata *conn = CF_CONN(cf);
507
0
  return CF_IS_PRIMARY(cf) ? conn->origin : conn->origin2;
508
0
}