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
0
{
43
0
  struct connectdata *conn = chain->conn;
44
0
  return chain->primary ?
45
0
    (conn->via_peer ? conn->via_peer : conn->origin) :
46
0
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2);
47
0
}
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
71.4k
{
56
71.4k
  struct connectdata *conn = chain->conn;
57
71.4k
#ifndef CURL_DISABLE_PROXY
58
71.4k
  if(conn->socks_proxy.peer)
59
0
    return conn->socks_proxy.peer;
60
71.4k
  if(conn->http_proxy.peer)
61
0
    return conn->http_proxy.peer;
62
71.4k
#endif
63
71.4k
  return !chain->primary ?
64
0
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2) :
65
71.4k
    (conn->via_peer ? conn->via_peer : conn->origin);
66
71.4k
}
67
68
struct Curl_peer *Curl_cf_get_first_peer(struct Curl_cfilter *cf)
69
36.0k
{
70
36.0k
  return cfchain_get_first_peer(cf->chain);
71
36.0k
}
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
0
{
83
0
  if(len == 2) {
84
0
    if(!memcmp(name, "h1", 2))
85
0
      return ALPN_h1;
86
0
    if(!memcmp(name, "h2", 2))
87
0
      return ALPN_h2;
88
0
    if(!memcmp(name, "h3", 2))
89
0
      return ALPN_h3;
90
0
  }
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
0
}
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
1.07M
{
118
1.07M
  struct Curl_cfchain *chain = data->conn ?
119
1.07M
                               &data->conn->chain : NULL;
120
1.07M
  timediff_t timeleft_ms = 0;
121
1.07M
  timediff_t ctimeleft_ms = 0;
122
123
1.07M
  if(chain && CURL_CFCHAIN_IN_SHUTDOWN(chain))
124
0
    return Curl_cshutdn_timeleft_ms(data, chain);
125
1.07M
  else if(with_connect && Curl_is_connecting(data)) {
126
    /* transfer has not reached DO phase yet */
127
167k
    timediff_t ctimeout_ms = (data->set.connecttimeout > 0) ?
128
167k
      data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT;
129
167k
    ctimeleft_ms = ctimeout_ms -
130
167k
      Curl_pgrs_since_ms(data, pnow, TIMER_STARTSINGLE);
131
167k
    if(!ctimeleft_ms)
132
0
      ctimeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
133
167k
  }
134
904k
  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
904k
  else if(!data->set.timeout ||
139
904k
          (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.58k
    return 0;
143
2.58k
  }
144
145
1.06M
  if(data->set.timeout) {
146
1.06M
    timeleft_ms = data->set.timeout -
147
1.06M
                  Curl_pgrs_since_ms(data, pnow, TIMER_STARTOP);
148
1.06M
    if(!timeleft_ms)
149
49
      timeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
150
1.06M
  }
151
152
1.06M
  if(!ctimeleft_ms)
153
902k
    return timeleft_ms;
154
167k
  else if(!timeleft_ms)
155
0
    return ctimeleft_ms;
156
167k
  return CURLMIN(ctimeleft_ms, timeleft_ms);
157
1.06M
}
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
515k
{
172
515k
  return timeleft_now_ms_int(data, pnow, TRUE);
173
515k
}
174
175
timediff_t Curl_timeleft_ms(struct Curl_easy *data)
176
72.1k
{
177
72.1k
  return timeleft_now_ms(data, Curl_pgrs_now(data));
178
72.1k
}
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
556k
{
185
556k
  return timeleft_now_ms_int(data, pnow, FALSE);
186
556k
}
187
188
timediff_t Curl_timeleft_now_ms(struct Curl_easy *data,
189
                                const struct curltime *pnow)
190
443k
{
191
443k
  return timeleft_now_ms(data, pnow);
192
443k
}
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
19.2k
{
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
19.2k
  struct connectdata *conn = Curl_cpool_get_last_conn(data);
209
19.2k
  if(connp)
210
1.14k
    *connp = conn;
211
19.2k
  return conn ? conn->chain.sock : CURL_SOCKET_BAD;
212
19.2k
}
213
214
void Curl_conncontrol(struct connectdata *conn, int ctrl)
215
83.8k
{
216
83.8k
  if(!conn) {
217
0
    DEBUGASSERT(0);
218
0
    return;
219
0
  }
220
83.8k
  switch(ctrl) {
221
0
    case CONNCTRL_CONN_KEEP:
222
0
      conn->bits.close = FALSE;
223
0
      break;
224
66.3k
    case CONNCTRL_CONN_CLOSE:
225
66.3k
      conn->bits.close = TRUE;
226
66.3k
      break;
227
17.4k
    case CONNCTRL_STREAM_CLOSE:
228
      /* stream close when multiplexing does not affect connection */
229
17.4k
      if(!Curl_conn_is_multiplex(conn))
230
10.4k
        conn->bits.close = TRUE;
231
17.4k
      break;
232
0
    default:
233
0
      DEBUGASSERT(0);
234
0
      break;
235
83.8k
  }
236
83.8k
}
237
238
CURLcode Curl_conn_setup(struct Curl_easy *data,
239
                         struct connectdata *conn,
240
                         struct Curl_cfchain *chain,
241
                         int ssl_mode)
242
35.4k
{
243
35.4k
  struct Curl_peer *first_peer = cfchain_get_first_peer(chain);
244
35.4k
  CURLcode result = CURLE_OK;
245
35.4k
  uint8_t dns_queries;
246
247
35.4k
  DEBUGASSERT(data);
248
35.4k
  DEBUGASSERT(conn->scheme);
249
35.4k
  DEBUGASSERT(!chain->cf);
250
251
35.4k
  if(!first_peer)
252
0
    return CURLE_FAILED_INIT;
253
254
35.4k
#ifndef CURL_DISABLE_HTTP
255
35.4k
  if(!chain->cf &&
256
35.4k
     conn->scheme->protocol == CURLPROTO_HTTPS) {
257
0
    DEBUGASSERT(ssl_mode != CURL_CF_SSL_DISABLE);
258
0
    result = Curl_cf_https_setup(
259
0
      data, cfchain_get_destination(chain), chain);
260
0
    if(result)
261
0
      goto out;
262
0
  }
263
35.4k
#endif /* !CURL_DISABLE_HTTP */
264
265
  /* Still no cfilter set, apply default. */
266
35.4k
  if(!chain->cf) {
267
35.4k
    result = Curl_cf_setup_add(chain, data, conn->transport_wanted, ssl_mode);
268
35.4k
    if(result)
269
17
      goto out;
270
35.4k
  }
271
272
35.4k
  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
35.4k
  dns_queries = Curl_resolv_dns_queries(data, conn->ip_version);
277
35.4k
  result = Curl_dns_cf_add_addr_resolve(data, chain->cf,
278
35.4k
                                        first_peer, dns_queries,
279
35.4k
                                        conn->transport_wanted);
280
35.4k
  if(result)
281
5
    goto out;
282
283
35.4k
out:
284
35.4k
  return result;
285
35.4k
}
286
287
#ifdef CURLVERBOSE
288
static CURLcode conn_connect_trace(struct Curl_easy *data,
289
                                   struct Curl_cfilter *cf)
290
34.2k
{
291
34.2k
  if(Curl_trc_is_verbose(data)) {
292
8.05k
    struct ip_quadruple ipquad;
293
8.05k
    bool is_ipv6;
294
8.05k
    CURLcode result;
295
296
8.05k
    result = Curl_conn_cf_get_ip_info(cf, data, &is_ipv6, &ipquad);
297
8.05k
    if(result)
298
0
      return result;
299
300
8.05k
    infof(data, "Established %sconnection to %s (%s port %u) from %s port %u ",
301
8.05k
          !CF_IS_PRIMARY(cf) ? "2nd " : "",
302
8.05k
          CURL_CONN_HOST_DISPNAME(data->conn),
303
8.05k
          ipquad.remote_ip, ipquad.remote_port,
304
8.05k
          ipquad.local_ip, ipquad.local_port);
305
8.05k
  }
306
34.2k
  return CURLE_OK;
307
34.2k
}
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
35.2k
{
316
  /* We only gather stats for the primary chain */
317
35.2k
  if(chain->primary)
318
35.2k
    Curl_cfchain_cntrl_report_stats(data, chain);
319
35.2k
}
320
321
void Curl_cfchain_report_failed(struct Curl_easy *data,
322
                                struct Curl_cfchain *chain)
323
963
{
324
963
  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
963
  (void)Curl_conn_cf_cntrl(chain->cf, data, TRUE,
330
963
                           CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
331
963
  conn_report_stats(data, chain);
332
963
}
333
334
CURLcode Curl_cfchain_connect(struct Curl_easy *data,
335
                              struct Curl_cfchain *chain,
336
                              bool blocking,
337
                              bool *done)
338
55.2k
{
339
55.2k
#define CF_CONN_NUM_POLLS_ON_STACK 5
340
55.2k
  struct pollfd a_few_on_stack[CF_CONN_NUM_POLLS_ON_STACK];
341
55.2k
  struct easy_pollset ps;
342
55.2k
  struct curl_pollfds cpfds;
343
55.2k
  struct Curl_cfilter *cf;
344
55.2k
  CURLcode result = CURLE_OK;
345
346
55.2k
  DEBUGASSERT(data);
347
55.2k
  DEBUGASSERT(data->conn);
348
349
55.2k
  if(data->conn->scheme->flags & PROTOPT_NONETWORK) {
350
0
    *done = TRUE;
351
0
    return CURLE_OK;
352
0
  }
353
354
55.2k
  cf = chain->cf;
355
55.2k
  if(!cf) {
356
0
    *done = FALSE;
357
0
    return CURLE_FAILED_INIT;
358
0
  }
359
360
55.2k
  *done = (bool)cf->connected;
361
55.2k
  if(*done)
362
1.53k
    return CURLE_OK;
363
364
53.7k
  if(data->conn->connect_start_ms < 0) {
365
35.3k
    data->conn->connect_start_ms =
366
35.3k
      curlx_ptimediff_ms(Curl_pgrs_now(data), &data->conn->created);
367
35.3k
  }
368
369
53.7k
  Curl_pollset_init(&ps);
370
53.7k
  Curl_pollfds_init(&cpfds, a_few_on_stack, CF_CONN_NUM_POLLS_ON_STACK);
371
53.7k
  while(!*done) {
372
53.7k
    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
53.7k
    result = cf->cft->do_connect(cf, data, done);
381
53.7k
    CURL_TRC_CF(data, cf, "Curl_conn_connect(block=%d) -> %d, done=%d",
382
53.7k
                blocking, (int)result, *done);
383
53.7k
    if(!result && *done) {
384
      /* A final sanity check on connection security */
385
34.2k
      if((data->state.origin->scheme->flags & PROTOPT_SSL) &&
386
0
         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
34.2k
      Curl_conn_cntrl_update_info(data, data->conn);
396
34.2k
      conn_report_stats(data, chain);
397
34.2k
      Curl_cpool_conn_was_used(data, data->conn, Curl_pgrs_now(data));
398
34.2k
      VERBOSE(result = conn_connect_trace(data, cf));
399
34.2k
      VERBOSE(Curl_cfchain_trc(data, NULL, chain, "connected"));
400
34.2k
      Curl_cfchain_remove_setup_filters(data, chain);
401
34.2k
      VERBOSE(Curl_cfchain_trc(data, NULL, chain, "reduced to"));
402
34.2k
      data->conn->connect_start_ms = -1;
403
34.2k
      goto out;
404
34.2k
    }
405
19.4k
    else if(result) {
406
955
      CURL_TRC_CF(data, cf, "Curl_conn_connect(), filter returned %d",
407
955
                  (int)result);
408
955
      VERBOSE(Curl_cfchain_trc(data, NULL, chain, "failed to connect"));
409
955
      Curl_cfchain_report_failed(data, chain);
410
955
      goto out;
411
955
    }
412
413
18.5k
    if(!blocking)
414
18.5k
      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
18.5k
  }
455
456
53.7k
out:
457
53.7k
  Curl_pollset_cleanup(&ps);
458
53.7k
  Curl_pollfds_cleanup(&cpfds);
459
53.7k
  return result;
460
53.7k
}
461
462
CURLcode Curl_conn_connect(struct Curl_easy *data,
463
                           bool blocking,
464
                           bool *done)
465
55.2k
{
466
55.2k
  return Curl_cfchain_connect(data, &data->conn->chain,
467
55.2k
                              blocking, done);
468
55.2k
}
469
470
void Curl_conn_set_multiplex(struct Curl_cfchain *chain, uint8_t http_version)
471
18.4k
{
472
18.4k
  if(chain->primary) {
473
18.4k
    chain->conn->httpversion_seen = http_version;
474
18.4k
    if(!chain->conn->bits.multiplex) {
475
18.4k
      chain->conn->bits.multiplex = TRUE;
476
18.4k
      if(chain->conn->attached_multi) {
477
18.4k
        Curl_multi_connchanged(chain->conn->attached_multi);
478
18.4k
      }
479
18.4k
    }
480
18.4k
  }
481
18.4k
}
482
483
struct Curl_peer *Curl_conn_get_origin(struct connectdata *conn)
484
0
{
485
0
  return conn->origin;
486
0
}
487
488
struct Curl_peer *Curl_conn_get_destination(struct connectdata *conn)
489
36.9k
{
490
36.9k
  return conn->via_peer ? conn->via_peer : conn->origin;
491
36.9k
}
492
493
struct Curl_peer *Curl_conn_get_first_peer(struct connectdata *conn)
494
73.9k
{
495
73.9k
#ifndef CURL_DISABLE_PROXY
496
73.9k
  if(conn->socks_proxy.peer)
497
0
    return conn->socks_proxy.peer;
498
73.9k
  if(conn->http_proxy.peer)
499
0
    return conn->http_proxy.peer;
500
73.9k
#endif
501
73.9k
  return conn->via_peer ? conn->via_peer : conn->origin;
502
73.9k
}
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
}