Coverage Report

Created: 2026-12-01 06:24

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