Coverage Report

Created: 2026-12-01 06:33

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
211k
{
43
211k
  struct connectdata *conn = chain->conn;
44
211k
  return chain->primary ?
45
211k
    (conn->via_peer ? conn->via_peer : conn->origin) :
46
211k
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2);
47
211k
}
48
49
struct Curl_peer *Curl_cf_get_destination(struct Curl_cfilter *cf)
50
53.4k
{
51
53.4k
  return cfchain_get_destination(cf->chain);
52
53.4k
}
53
54
static struct Curl_peer *cfchain_get_first_peer(struct Curl_cfchain *chain)
55
1.14M
{
56
1.14M
  struct connectdata *conn = chain->conn;
57
1.14M
#ifndef CURL_DISABLE_PROXY
58
1.14M
  if(conn->socks_proxy.peer)
59
31.6k
    return conn->socks_proxy.peer;
60
1.11M
  if(conn->http_proxy.peer)
61
54.9k
    return conn->http_proxy.peer;
62
1.06M
#endif
63
1.06M
  return !chain->primary ?
64
9.40k
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2) :
65
1.06M
    (conn->via_peer ? conn->via_peer : conn->origin);
66
1.11M
}
67
68
struct Curl_peer *Curl_cf_get_first_peer(struct Curl_cfilter *cf)
69
592k
{
70
592k
  return cfchain_get_first_peer(cf->chain);
71
592k
}
72
73
struct Curl_peer *Curl_cfchain_get_via_peer(struct Curl_cfchain *chain)
74
593
{
75
593
  struct connectdata *conn = chain->conn;
76
593
  return chain->primary ? conn->via_peer : conn->via_peer2;
77
593
}
78
79
#if !defined(CURL_DISABLE_ALTSVC) || defined(USE_HTTPSRR)
80
81
enum alpnid Curl_alpn2alpnid(const unsigned char *name, size_t len)
82
139k
{
83
139k
  if(len == 2) {
84
105k
    if(!memcmp(name, "h1", 2))
85
55.8k
      return ALPN_h1;
86
49.9k
    if(!memcmp(name, "h2", 2))
87
20.0k
      return ALPN_h2;
88
29.8k
    if(!memcmp(name, "h3", 2))
89
5.86k
      return ALPN_h3;
90
29.8k
  }
91
33.5k
  else if(len == 8 && !memcmp(name, "http/1.1", 8))
92
243
    return ALPN_h1;
93
57.3k
  return ALPN_none; /* unknown, probably rubbish input */
94
139k
}
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
17.1k
{
102
17.1k
  timediff_t timeout_ms = (data->set.connecttimeout > 0) ?
103
17.1k
    data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT;
104
17.1k
  timediff_t elapsed_ms =
105
17.1k
    curlx_ptimediff_ms(pnow, &data->conn->created) -
106
17.1k
    data->conn->connect_start_ms;
107
17.1k
  timediff_t left_ms = timeout_ms - elapsed_ms;
108
17.1k
  DEBUGASSERT(data->conn->connect_start_ms >= 0);
109
17.1k
  return left_ms ? left_ms : -1; /* 0 is "no limit", fake 1 ms expiry */
110
17.1k
}
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
167M
{
118
167M
  struct Curl_cfchain *chain = data->conn ?
119
167M
                               &data->conn->chain : NULL;
120
167M
  timediff_t timeleft_ms = 0;
121
167M
  timediff_t ctimeleft_ms = 0;
122
123
167M
  if(chain && CURL_CFCHAIN_IN_SHUTDOWN(chain))
124
130M
    return Curl_cshutdn_timeleft_ms(data, chain);
125
37.2M
  else if(with_connect && Curl_is_connecting(data)) {
126
    /* transfer has not reached DO phase yet */
127
3.46M
    timediff_t ctimeout_ms = (data->set.connecttimeout > 0) ?
128
3.43M
      data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT;
129
3.46M
    ctimeleft_ms = ctimeout_ms -
130
3.46M
      Curl_pgrs_since_ms(data, pnow, TIMER_STARTSINGLE);
131
3.46M
    if(!ctimeleft_ms)
132
1
      ctimeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
133
3.46M
  }
134
33.7M
  else if(with_connect && data->conn && data->conn->connect_start_ms >= 0) {
135
    /* ongoing connect of filters, pbly FTP second socket */
136
17.1k
    ctimeleft_ms = connect_left_ms(data, pnow);
137
17.1k
  }
138
33.7M
  else if(!data->set.timeout ||
139
33.6M
          (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
224k
    return 0;
143
224k
  }
144
145
37.0M
  if(data->set.timeout) {
146
36.9M
    timeleft_ms = data->set.timeout -
147
36.9M
                  Curl_pgrs_since_ms(data, pnow, TIMER_STARTOP);
148
36.9M
    if(!timeleft_ms)
149
4.00k
      timeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
150
36.9M
  }
151
152
37.0M
  if(!ctimeleft_ms)
153
33.5M
    return timeleft_ms;
154
3.47M
  else if(!timeleft_ms)
155
13.6k
    return ctimeleft_ms;
156
3.46M
  return CURLMIN(ctimeleft_ms, timeleft_ms);
157
37.0M
}
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
160M
{
172
160M
  return timeleft_now_ms_int(data, pnow, TRUE);
173
160M
}
174
175
timediff_t Curl_timeleft_ms(struct Curl_easy *data)
176
1.26M
{
177
1.26M
  return timeleft_now_ms(data, Curl_pgrs_now(data));
178
1.26M
}
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
7.12M
{
185
7.12M
  return timeleft_now_ms_int(data, pnow, FALSE);
186
7.12M
}
187
188
timediff_t Curl_timeleft_now_ms(struct Curl_easy *data,
189
                                const struct curltime *pnow)
190
159M
{
191
159M
  return timeleft_now_ms(data, pnow);
192
159M
}
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
441k
{
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
441k
  struct connectdata *conn = Curl_cpool_get_last_conn(data);
209
441k
  if(connp)
210
104k
    *connp = conn;
211
441k
  return conn ? conn->chain.sock : CURL_SOCKET_BAD;
212
441k
}
213
214
void Curl_conncontrol(struct connectdata *conn, int ctrl)
215
1.09M
{
216
1.09M
  if(!conn) {
217
0
    DEBUGASSERT(0);
218
0
    return;
219
0
  }
220
1.09M
  switch(ctrl) {
221
1.95k
    case CONNCTRL_CONN_KEEP:
222
1.95k
      conn->bits.close = FALSE;
223
1.95k
      break;
224
773k
    case CONNCTRL_CONN_CLOSE:
225
773k
      conn->bits.close = TRUE;
226
773k
      break;
227
320k
    case CONNCTRL_STREAM_CLOSE:
228
      /* stream close when multiplexing does not affect connection */
229
320k
      if(!Curl_conn_is_multiplex(conn))
230
198k
        conn->bits.close = TRUE;
231
320k
      break;
232
0
    default:
233
0
      DEBUGASSERT(0);
234
0
      break;
235
1.09M
  }
236
1.09M
}
237
238
CURLcode Curl_conn_setup(struct Curl_easy *data,
239
                         struct connectdata *conn,
240
                         struct Curl_cfchain *chain,
241
                         int ssl_mode)
242
554k
{
243
554k
  struct Curl_peer *first_peer = cfchain_get_first_peer(chain);
244
554k
  CURLcode result = CURLE_OK;
245
554k
  uint8_t dns_queries;
246
247
554k
  DEBUGASSERT(data);
248
554k
  DEBUGASSERT(conn->scheme);
249
554k
  DEBUGASSERT(!chain->cf);
250
251
554k
  if(!first_peer)
252
0
    return CURLE_FAILED_INIT;
253
254
554k
#ifndef CURL_DISABLE_HTTP
255
554k
  if(!chain->cf &&
256
554k
     conn->scheme->protocol == CURLPROTO_HTTPS) {
257
157k
    DEBUGASSERT(ssl_mode != CURL_CF_SSL_DISABLE);
258
157k
    result = Curl_cf_https_setup(
259
157k
      data, cfchain_get_destination(chain), chain);
260
157k
    if(result)
261
122
      goto out;
262
157k
  }
263
554k
#endif /* !CURL_DISABLE_HTTP */
264
265
  /* Still no cfilter set, apply default. */
266
554k
  if(!chain->cf) {
267
403k
    result = Curl_cf_setup_add(chain, data, conn->transport_wanted, ssl_mode);
268
403k
    if(result)
269
281
      goto out;
270
403k
  }
271
272
554k
  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
554k
  dns_queries = Curl_resolv_dns_queries(data, conn->ip_version);
277
554k
  result = Curl_dns_cf_add_addr_resolve(data, chain->cf,
278
554k
                                        first_peer, dns_queries,
279
554k
                                        conn->transport_wanted);
280
554k
  if(result)
281
242
    goto out;
282
283
554k
out:
284
554k
  return result;
285
554k
}
286
287
#ifdef CURLVERBOSE
288
static CURLcode conn_connect_trace(struct Curl_easy *data,
289
                                   struct Curl_cfilter *cf)
290
462k
{
291
462k
  if(Curl_trc_is_verbose(data)) {
292
129k
    struct ip_quadruple ipquad;
293
129k
    bool is_ipv6;
294
129k
    CURLcode result;
295
296
129k
    result = Curl_conn_cf_get_ip_info(cf, data, &is_ipv6, &ipquad);
297
129k
    if(result)
298
0
      return result;
299
300
129k
    infof(data, "Established %sconnection to %s (%s port %u) from %s port %u ",
301
129k
          !CF_IS_PRIMARY(cf) ? "2nd " : "",
302
129k
          CURL_CONN_HOST_DISPNAME(data->conn),
303
129k
          ipquad.remote_ip, ipquad.remote_port,
304
129k
          ipquad.local_ip, ipquad.local_port);
305
129k
  }
306
462k
  return CURLE_OK;
307
462k
}
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
537k
{
316
  /* We only gather stats for the primary chain */
317
537k
  if(chain->primary)
318
533k
    Curl_cfchain_cntrl_report_stats(data, chain);
319
537k
}
320
321
void Curl_cfchain_report_failed(struct Curl_easy *data,
322
                                struct Curl_cfchain *chain)
323
75.5k
{
324
75.5k
  if(!chain->primary)
325
93
    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
75.4k
  (void)Curl_conn_cf_cntrl(chain->cf, data, TRUE,
330
75.4k
                           CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
331
75.4k
  conn_report_stats(data, chain);
332
75.4k
}
333
334
CURLcode Curl_cfchain_connect(struct Curl_easy *data,
335
                              struct Curl_cfchain *chain,
336
                              bool blocking,
337
                              bool *done)
338
1.27M
{
339
1.27M
#define CF_CONN_NUM_POLLS_ON_STACK 5
340
1.27M
  struct pollfd a_few_on_stack[CF_CONN_NUM_POLLS_ON_STACK];
341
1.27M
  struct easy_pollset ps;
342
1.27M
  struct curl_pollfds cpfds;
343
1.27M
  struct Curl_cfilter *cf;
344
1.27M
  CURLcode result = CURLE_OK;
345
346
1.27M
  DEBUGASSERT(data);
347
1.27M
  DEBUGASSERT(data->conn);
348
349
1.27M
  if(data->conn->scheme->flags & PROTOPT_NONETWORK) {
350
0
    *done = TRUE;
351
0
    return CURLE_OK;
352
0
  }
353
354
1.27M
  cf = chain->cf;
355
1.27M
  if(!cf) {
356
0
    *done = FALSE;
357
0
    return CURLE_FAILED_INIT;
358
0
  }
359
360
1.27M
  *done = (bool)cf->connected;
361
1.27M
  if(*done)
362
194k
    return CURLE_OK;
363
364
1.07M
  if(data->conn->connect_start_ms < 0) {
365
554k
    data->conn->connect_start_ms =
366
554k
      curlx_ptimediff_ms(Curl_pgrs_now(data), &data->conn->created);
367
554k
  }
368
369
1.07M
  Curl_pollset_init(&ps);
370
1.07M
  Curl_pollfds_init(&cpfds, a_few_on_stack, CF_CONN_NUM_POLLS_ON_STACK);
371
1.07M
  while(!*done) {
372
1.07M
    if(Curl_cfchain_needs_flush(data, chain)) {
373
5.75k
      DEBUGF(infof(data, "Curl_conn_connect(%s), flush",
374
5.75k
             chain->primary ? "" : "2nd"));
375
5.75k
      result = Curl_cfchain_flush(data, chain);
376
5.75k
      if(result && (result != CURLE_AGAIN))
377
0
        goto out;
378
5.75k
    }
379
380
1.07M
    result = cf->cft->do_connect(cf, data, done);
381
1.07M
    CURL_TRC_CF(data, cf, "Curl_conn_connect(block=%d) -> %d, done=%d",
382
1.07M
                blocking, (int)result, *done);
383
1.07M
    if(!result && *done) {
384
      /* A final sanity check on connection security */
385
462k
      if((data->state.origin->scheme->flags & PROTOPT_SSL) &&
386
150k
         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
462k
      Curl_conn_cntrl_update_info(data, data->conn);
396
462k
      conn_report_stats(data, chain);
397
462k
      Curl_cpool_conn_was_used(data, data->conn, Curl_pgrs_now(data));
398
462k
      VERBOSE(result = conn_connect_trace(data, cf));
399
462k
      VERBOSE(Curl_cfchain_trc(data, NULL, chain, "connected"));
400
462k
      Curl_cfchain_remove_setup_filters(data, chain);
401
462k
      VERBOSE(Curl_cfchain_trc(data, NULL, chain, "reduced to"));
402
462k
      data->conn->connect_start_ms = -1;
403
462k
      goto out;
404
462k
    }
405
614k
    else if(result) {
406
75.5k
      CURL_TRC_CF(data, cf, "Curl_conn_connect(), filter returned %d",
407
75.5k
                  (int)result);
408
75.5k
      VERBOSE(Curl_cfchain_trc(data, NULL, chain, "failed to connect"));
409
75.5k
      Curl_cfchain_report_failed(data, chain);
410
75.5k
      goto out;
411
75.5k
    }
412
413
538k
    if(!blocking)
414
538k
      goto out;
415
12
    else {
416
      /* check allowed time left */
417
12
      const struct curltime *pnow = Curl_pgrs_now(data);
418
12
      timediff_t timeout_ms = Curl_timeleft_now_ms(data, pnow);
419
12
      curl_socket_t sockfd = cf->chain->sock;
420
12
      int rc;
421
422
12
      if(timeout_ms < 0) {
423
        /* no need to continue if time already is up */
424
6
        failf(data, "connect timeout");
425
6
        result = CURLE_OPERATION_TIMEDOUT;
426
6
        goto out;
427
6
      }
428
429
6
      CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), do poll");
430
6
      Curl_pollset_reset(&ps);
431
6
      Curl_pollfds_reset(&cpfds);
432
      /* In general, we want to send after connect, wait on that. */
433
6
      if(sockfd != CURL_SOCKET_BAD)
434
6
        result = Curl_pollset_set_out_only(data, &ps, sockfd);
435
6
      if(!result)
436
6
        result = Curl_conn_adjust_pollset(data, data->conn, &ps);
437
6
      if(result)
438
0
        goto out;
439
6
      result = Curl_pollfds_add_ps(&cpfds, &ps);
440
6
      if(result)
441
0
        goto out;
442
443
6
      rc = Curl_poll(cpfds.pfds, cpfds.n,
444
6
                     CURLMIN(timeout_ms ? timeout_ms : 1000,
445
6
                             (cpfds.n ? 1000 : 10)));
446
6
      CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), Curl_poll() -> %d",
447
6
                  rc);
448
6
      if(rc < 0) {
449
0
        result = CURLE_COULDNT_CONNECT;
450
0
        goto out;
451
0
      }
452
      /* continue iterating */
453
6
    }
454
538k
  }
455
456
1.07M
out:
457
1.07M
  Curl_pollset_cleanup(&ps);
458
1.07M
  Curl_pollfds_cleanup(&cpfds);
459
1.07M
  return result;
460
1.07M
}
461
462
CURLcode Curl_conn_connect(struct Curl_easy *data,
463
                           bool blocking,
464
                           bool *done)
465
1.24M
{
466
1.24M
  return Curl_cfchain_connect(data, &data->conn->chain,
467
1.24M
                              blocking, done);
468
1.24M
}
469
470
void Curl_conn_set_multiplex(struct Curl_cfchain *chain, uint8_t http_version)
471
158k
{
472
158k
  if(chain->primary) {
473
158k
    chain->conn->httpversion_seen = http_version;
474
158k
    if(!chain->conn->bits.multiplex) {
475
158k
      chain->conn->bits.multiplex = TRUE;
476
158k
      if(chain->conn->attached_multi) {
477
158k
        Curl_multi_connchanged(chain->conn->attached_multi);
478
158k
      }
479
158k
    }
480
158k
  }
481
158k
}
482
483
struct Curl_peer *Curl_conn_get_origin(struct connectdata *conn)
484
167k
{
485
167k
  return conn->origin;
486
167k
}
487
488
struct Curl_peer *Curl_conn_get_destination(struct connectdata *conn)
489
923k
{
490
923k
  return conn->via_peer ? conn->via_peer : conn->origin;
491
923k
}
492
493
struct Curl_peer *Curl_conn_get_first_peer(struct connectdata *conn)
494
1.52M
{
495
1.52M
#ifndef CURL_DISABLE_PROXY
496
1.52M
  if(conn->socks_proxy.peer)
497
30.5k
    return conn->socks_proxy.peer;
498
1.49M
  if(conn->http_proxy.peer)
499
77.2k
    return conn->http_proxy.peer;
500
1.42M
#endif
501
1.42M
  return conn->via_peer ? conn->via_peer : conn->origin;
502
1.49M
}
503
504
struct Curl_peer *Curl_cf_get_origin(struct Curl_cfilter *cf)
505
1.38k
{
506
1.38k
  struct connectdata *conn = CF_CONN(cf);
507
1.38k
  return CF_IS_PRIMARY(cf) ? conn->origin : conn->origin2;
508
1.38k
}