Coverage Report

Created: 2026-09-01 06:59

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
#if !defined(CURL_DISABLE_ALTSVC) || defined(USE_HTTPSRR)
42
43
enum alpnid Curl_alpn2alpnid(const unsigned char *name, size_t len)
44
0
{
45
0
  if(len == 2) {
46
0
    if(!memcmp(name, "h1", 2))
47
0
      return ALPN_h1;
48
0
    if(!memcmp(name, "h2", 2))
49
0
      return ALPN_h2;
50
0
    if(!memcmp(name, "h3", 2))
51
0
      return ALPN_h3;
52
0
  }
53
0
  else if(len == 8) {
54
0
    if(!memcmp(name, "http/1.1", 8))
55
0
      return ALPN_h1;
56
0
  }
57
0
  return ALPN_none; /* unknown, probably rubbish input */
58
0
}
59
60
#endif
61
62
/*
63
 * timeleft_now_ms() returns the amount of milliseconds left allowed for the
64
 * transfer/connection. If the value is 0, there is no timeout (ie there is
65
 * infinite time left). If the value is negative, the timeout time has already
66
 * elapsed.
67
 *
68
 * @unittest 1303
69
 */
70
UNITTEST timediff_t timeleft_now_ms(struct Curl_easy *data,
71
                                    const struct curltime *pnow);
72
UNITTEST timediff_t timeleft_now_ms(struct Curl_easy *data,
73
                                    const struct curltime *pnow)
74
98.4k
{
75
98.4k
  timediff_t timeleft_ms = 0;
76
98.4k
  timediff_t ctimeleft_ms = 0;
77
78
98.4k
  if(data->conn && Curl_shutdown_started(data->conn, FIRSTSOCKET))
79
981
    return Curl_shutdown_timeleft(data, data->conn, FIRSTSOCKET);
80
97.4k
  else if(Curl_is_connecting(data)) {
81
64.3k
    timediff_t ctimeout_ms = (data->set.connecttimeout > 0) ?
82
62.7k
      data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT;
83
64.3k
    ctimeleft_ms = ctimeout_ms -
84
64.3k
      Curl_pgrs_since_ms(data, pnow, TIMER_STARTSINGLE);
85
64.3k
    if(!ctimeleft_ms)
86
1
      ctimeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
87
64.3k
  }
88
33.1k
  else if(!data->set.timeout || data->set.connect_only) {
89
818
    return 0; /* no timeout in place or checked, return "no limit" */
90
818
  }
91
92
96.6k
  if(data->set.timeout) {
93
96.6k
    timeleft_ms = data->set.timeout -
94
96.6k
                  Curl_pgrs_since_ms(data, pnow, TIMER_STARTOP);
95
96.6k
    if(!timeleft_ms)
96
5
      timeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
97
96.6k
  }
98
99
96.6k
  if(!ctimeleft_ms)
100
32.3k
    return timeleft_ms;
101
64.3k
  else if(!timeleft_ms)
102
58
    return ctimeleft_ms;
103
64.2k
  return CURLMIN(ctimeleft_ms, timeleft_ms);
104
96.6k
}
105
106
timediff_t Curl_timeleft_ms(struct Curl_easy *data)
107
40.6k
{
108
40.6k
  return timeleft_now_ms(data, Curl_pgrs_now(data));
109
40.6k
}
110
111
timediff_t Curl_timeleft_now_ms(struct Curl_easy *data,
112
                                const struct curltime *pnow)
113
57.8k
{
114
57.8k
  return timeleft_now_ms(data, pnow);
115
57.8k
}
116
117
void Curl_shutdown_start(struct Curl_easy *data, int8_t sockindex,
118
                         int timeout_ms)
119
3.66k
{
120
3.66k
  struct connectdata *conn = data->conn;
121
122
3.66k
  DEBUGASSERT(conn);
123
3.66k
  conn->shutdown.start[sockindex] = *Curl_pgrs_now(data);
124
3.66k
  conn->shutdown.timeout_ms = (timeout_ms > 0) ?
125
0
    (timediff_t)timeout_ms :
126
3.66k
    ((data->set.shutdowntimeout > 0) ?
127
3.66k
     data->set.shutdowntimeout : DEFAULT_SHUTDOWN_TIMEOUT_MS);
128
  /* Set a timer, unless we operate on the admin handle */
129
3.66k
  if(data->mid)
130
593
    Curl_expire(data, conn->shutdown.timeout_ms, EXPIRE_SHUTDOWN);
131
3.66k
  CURL_TRC_M(data, "shutdown start on%s connection",
132
3.66k
             sockindex ? " secondary" : "");
133
3.66k
}
134
135
timediff_t Curl_shutdown_timeleft(struct Curl_easy *data,
136
                                  struct connectdata *conn,
137
                                  int8_t sockindex)
138
4.05k
{
139
4.05k
  timediff_t left_ms;
140
141
4.05k
  if(!conn->shutdown.start[sockindex].tv_sec ||
142
4.05k
     (conn->shutdown.timeout_ms <= 0))
143
0
    return 0; /* not started or no limits */
144
145
4.05k
  left_ms = conn->shutdown.timeout_ms -
146
4.05k
            curlx_ptimediff_ms(Curl_pgrs_now(data),
147
4.05k
                               &conn->shutdown.start[sockindex]);
148
4.05k
  return left_ms ? left_ms : -1;
149
4.05k
}
150
151
timediff_t Curl_conn_shutdown_timeleft(struct Curl_easy *data,
152
                                       struct connectdata *conn)
153
0
{
154
0
  timediff_t left_ms = 0, ms;
155
0
  int8_t i;
156
157
0
  for(i = 0; conn->shutdown.timeout_ms && (i < 2); ++i) {
158
0
    if(!conn->shutdown.start[i].tv_sec)
159
0
      continue;
160
0
    ms = Curl_shutdown_timeleft(data, conn, i);
161
0
    if(ms && (!left_ms || ms < left_ms))
162
0
      left_ms = ms;
163
0
  }
164
0
  return left_ms;
165
0
}
166
167
void Curl_shutdown_clear(struct Curl_easy *data, int8_t sockindex)
168
0
{
169
0
  struct curltime *pt = &data->conn->shutdown.start[sockindex];
170
0
  memset(pt, 0, sizeof(*pt));
171
0
}
172
173
bool Curl_shutdown_started(struct connectdata *conn, int8_t sockindex)
174
98.3k
{
175
98.3k
  const struct curltime *pt = &conn->shutdown.start[sockindex];
176
98.3k
  return (pt->tv_sec > 0) || (pt->tv_usec > 0);
177
98.3k
}
178
179
/*
180
 * Used to extract socket and connectdata struct for the most recent
181
 * transfer on the given Curl_easy.
182
 *
183
 * The returned socket will be CURL_SOCKET_BAD in case of failure!
184
 */
185
curl_socket_t Curl_getconnectinfo(struct Curl_easy *data,
186
                                  struct connectdata **connp)
187
221
{
188
221
  DEBUGASSERT(data);
189
190
  /* this works for an easy handle:
191
   * - that has been used for curl_easy_perform()
192
   * - that is associated with a multi handle, and whose connection
193
   *   was detached with CURLOPT_CONNECT_ONLY
194
   */
195
221
  if(data->state.lastconnect_id != -1) {
196
220
    struct connectdata *conn;
197
198
220
    conn = Curl_cpool_get_conn(data, data->state.lastconnect_id);
199
220
    if(!conn) {
200
205
      data->state.lastconnect_id = -1;
201
205
      if(connp)
202
205
        *connp = NULL;
203
205
      return CURL_SOCKET_BAD;
204
205
    }
205
206
15
    if(connp)
207
15
      *connp = conn;
208
15
    return conn->sock[FIRSTSOCKET];
209
220
  }
210
1
  if(connp)
211
1
    *connp = NULL;
212
1
  return CURL_SOCKET_BAD;
213
221
}
214
215
void Curl_conncontrol(struct connectdata *conn, int ctrl)
216
17.8k
{
217
17.8k
  if(!conn) {
218
0
    DEBUGASSERT(0);
219
0
    return;
220
0
  }
221
17.8k
  switch(ctrl) {
222
0
    case CONNCTRL_CONN_KEEP:
223
0
      conn->bits.close = FALSE;
224
0
      break;
225
14.7k
    case CONNCTRL_CONN_CLOSE:
226
14.7k
      conn->bits.close = TRUE;
227
14.7k
      break;
228
3.08k
    case CONNCTRL_STREAM_CLOSE:
229
      /* stream close when multiplexing does not affect connection */
230
3.08k
      if(!Curl_conn_is_multiplex(conn, FIRSTSOCKET))
231
3.08k
        conn->bits.close = TRUE;
232
3.08k
      break;
233
0
    default:
234
0
      DEBUGASSERT(0);
235
0
      break;
236
17.8k
  }
237
17.8k
}
238
239
CURLcode Curl_conn_setup(struct Curl_easy *data,
240
                         struct connectdata *conn,
241
                         int8_t sockindex,
242
                         int ssl_mode)
243
11.8k
{
244
11.8k
  struct Curl_peer *first_peer = Curl_conn_get_first_peer(conn, sockindex);
245
11.8k
  CURLcode result = CURLE_OK;
246
11.8k
  uint8_t dns_queries;
247
248
11.8k
  DEBUGASSERT(data);
249
11.8k
  DEBUGASSERT(conn->scheme);
250
11.8k
  DEBUGASSERT(!conn->cfilter[sockindex]);
251
252
11.8k
  if(!first_peer)
253
0
    return CURLE_FAILED_INIT;
254
255
11.8k
#ifndef CURL_DISABLE_HTTP
256
11.8k
  if(!conn->cfilter[sockindex] &&
257
11.8k
     conn->scheme->protocol == CURLPROTO_HTTPS) {
258
121
    DEBUGASSERT(ssl_mode != CURL_CF_SSL_DISABLE);
259
121
    result = Curl_cf_https_setup(
260
121
      data, Curl_conn_get_destination(conn, sockindex), conn, sockindex);
261
121
    if(result)
262
0
      goto out;
263
121
  }
264
11.8k
#endif /* !CURL_DISABLE_HTTP */
265
266
  /* Still no cfilter set, apply default. */
267
11.8k
  if(!conn->cfilter[sockindex]) {
268
11.7k
    result = Curl_cf_setup_add(data, conn, sockindex,
269
11.7k
                               conn->transport_wanted, ssl_mode);
270
11.7k
    if(result)
271
0
      goto out;
272
11.7k
  }
273
274
  /* Whatever the filter chain will be in the end, it will need the
275
   * resolving of `first_peer`. Add that now so the resolve is started
276
   * right away. */
277
11.8k
  dns_queries = Curl_resolv_dns_queries(data, conn->ip_version);
278
11.8k
  result = Curl_conn_dns_add_addr_resolve(data, conn, sockindex,
279
11.8k
                                          first_peer, dns_queries,
280
11.8k
                                          conn->transport_wanted);
281
11.8k
  if(result)
282
0
    goto out;
283
284
11.8k
  DEBUGASSERT(conn->cfilter[sockindex]);
285
11.8k
out:
286
11.8k
  return result;
287
11.8k
}
288
289
#ifdef CURLVERBOSE
290
static CURLcode conn_connect_trace(struct Curl_easy *data,
291
                                   struct Curl_cfilter *cf)
292
7.21k
{
293
7.21k
  if(Curl_trc_is_verbose(data)) {
294
0
    struct ip_quadruple ipquad;
295
0
    bool is_ipv6;
296
0
    CURLcode result;
297
298
0
    result = Curl_conn_cf_get_ip_info(cf, data, &is_ipv6, &ipquad);
299
0
    if(result)
300
0
      return result;
301
302
0
    infof(data, "Established %sconnection to %s (%s port %u) from %s port %u ",
303
0
          (cf->sockindex == SECONDARYSOCKET) ? "2nd " : "",
304
0
          CURL_CONN_HOST_DISPNAME(data->conn),
305
0
          ipquad.remote_ip, ipquad.remote_port,
306
0
          ipquad.local_ip, ipquad.local_port);
307
0
  }
308
7.21k
  return CURLE_OK;
309
7.21k
}
310
#endif
311
312
/**
313
 * Update connection statistics
314
 */
315
static void conn_report_stats(struct Curl_easy *data, int sockindex)
316
11.5k
{
317
  /* We do gather stats for the second socket...yet */
318
11.5k
  if(sockindex == FIRSTSOCKET) {
319
10.4k
    Curl_conn_cntrl_report_stats(data, data->conn, sockindex);
320
10.4k
  }
321
11.5k
}
322
323
CURLcode Curl_conn_connect(struct Curl_easy *data,
324
                           int8_t sockindex,
325
                           bool blocking,
326
                           bool *done)
327
14.1k
{
328
14.1k
#define CF_CONN_NUM_POLLS_ON_STACK 5
329
14.1k
  struct pollfd a_few_on_stack[CF_CONN_NUM_POLLS_ON_STACK];
330
14.1k
  struct easy_pollset ps;
331
14.1k
  struct curl_pollfds cpfds;
332
14.1k
  struct Curl_cfilter *cf;
333
14.1k
  CURLcode result = CURLE_OK;
334
335
14.1k
  DEBUGASSERT(data);
336
14.1k
  DEBUGASSERT(data->conn);
337
14.1k
  if(!CONN_SOCK_IDX_VALID(sockindex))
338
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
339
340
14.1k
  if(data->conn->scheme->flags & PROTOPT_NONETWORK) {
341
0
    *done = TRUE;
342
0
    return CURLE_OK;
343
0
  }
344
345
14.1k
  cf = data->conn->cfilter[sockindex];
346
14.1k
  if(!cf) {
347
0
    *done = FALSE;
348
0
    return CURLE_FAILED_INIT;
349
0
  }
350
351
14.1k
  *done = (bool)cf->connected;
352
14.1k
  if(*done)
353
1.81k
    return CURLE_OK;
354
355
12.3k
  Curl_pollset_init(&ps);
356
12.3k
  Curl_pollfds_init(&cpfds, a_few_on_stack, CF_CONN_NUM_POLLS_ON_STACK);
357
12.3k
  while(!*done) {
358
12.3k
    if(Curl_conn_needs_flush(data, sockindex)) {
359
0
      DEBUGF(infof(data, "Curl_conn_connect(index=%d), flush", sockindex));
360
0
      result = Curl_conn_flush(data, sockindex);
361
0
      if(result && (result != CURLE_AGAIN))
362
0
        goto out;
363
0
    }
364
365
12.3k
    result = cf->cft->do_connect(cf, data, done);
366
12.3k
    CURL_TRC_CF(data, cf, "Curl_conn_connect(block=%d) -> %d, done=%d",
367
12.3k
                blocking, (int)result, *done);
368
12.3k
    if(!result && *done) {
369
      /* A final sanity check on connection security */
370
7.21k
      if((data->state.origin->scheme->flags & PROTOPT_SSL) &&
371
0
         (sockindex == FIRSTSOCKET) &&
372
0
         !Curl_conn_is_ssl(data->conn, FIRSTSOCKET)) {
373
0
        DEBUGASSERT(0);
374
0
        failf(data, "transfer requires SSL, but not connected via SSL");
375
0
        result = CURLE_FAILED_INIT;
376
0
        goto out;
377
0
      }
378
      /* Now that the complete filter chain is connected, let all filters
379
       * persist information at the connection. E.g. cf-socket sets the
380
       * socket and ip related information. */
381
7.21k
      Curl_conn_cntrl_update_info(data, data->conn);
382
7.21k
      conn_report_stats(data, sockindex);
383
7.21k
      data->conn->lastupkeep = *Curl_pgrs_now(data);
384
7.21k
      VERBOSE(result = conn_connect_trace(data, cf));
385
7.21k
      VERBOSE(Curl_conn_trc_filters(data, sockindex, "connected"));
386
7.21k
      Curl_conn_remove_setup_filters(data, sockindex);
387
7.21k
      VERBOSE(Curl_conn_trc_filters(data, sockindex, "reduced to"));
388
7.21k
      goto out;
389
7.21k
    }
390
5.13k
    else if(result) {
391
4.29k
      CURL_TRC_CF(data, cf, "Curl_conn_connect(), filter returned %d",
392
4.29k
                  (int)result);
393
4.29k
      VERBOSE(Curl_conn_trc_filters(data, sockindex, "failed to connect"));
394
4.29k
      conn_report_stats(data, sockindex);
395
4.29k
      goto out;
396
4.29k
    }
397
398
837
    if(!blocking)
399
837
      goto out;
400
0
    else {
401
      /* check allowed time left */
402
0
      const timediff_t timeout_ms = Curl_timeleft_ms(data);
403
0
      curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
404
0
      int rc;
405
406
0
      if(timeout_ms < 0) {
407
        /* no need to continue if time already is up */
408
0
        failf(data, "connect timeout");
409
0
        result = CURLE_OPERATION_TIMEDOUT;
410
0
        goto out;
411
0
      }
412
413
0
      CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), do poll");
414
0
      Curl_pollset_reset(&ps);
415
0
      Curl_pollfds_reset(&cpfds);
416
      /* In general, we want to send after connect, wait on that. */
417
0
      if(sockfd != CURL_SOCKET_BAD)
418
0
        result = Curl_pollset_set_out_only(data, &ps, sockfd);
419
0
      if(!result)
420
0
        result = Curl_conn_adjust_pollset(data, data->conn, &ps);
421
0
      if(result)
422
0
        goto out;
423
0
      result = Curl_pollfds_add_ps(&cpfds, &ps);
424
0
      if(result)
425
0
        goto out;
426
427
0
      rc = Curl_poll(cpfds.pfds, cpfds.n,
428
0
                     CURLMIN(timeout_ms, (cpfds.n ? 1000 : 10)));
429
0
      CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), Curl_poll() -> %d",
430
0
                  rc);
431
0
      if(rc < 0) {
432
0
        result = CURLE_COULDNT_CONNECT;
433
0
        goto out;
434
0
      }
435
      /* continue iterating */
436
0
    }
437
837
  }
438
439
12.3k
out:
440
12.3k
  Curl_pollset_cleanup(&ps);
441
12.3k
  Curl_pollfds_cleanup(&cpfds);
442
12.3k
  return result;
443
12.3k
}
444
445
void Curl_conn_set_multiplex(struct connectdata *conn)
446
0
{
447
0
  if(!conn->bits.multiplex) {
448
0
    conn->bits.multiplex = TRUE;
449
0
    if(conn->attached_multi) {
450
0
      Curl_multi_connchanged(conn->attached_multi);
451
0
    }
452
0
  }
453
0
}
454
455
struct Curl_peer *Curl_conn_get_origin(struct connectdata *conn,
456
                                       int8_t sockindex)
457
2.39k
{
458
2.39k
  return (sockindex == SECONDARYSOCKET) ?
459
2.39k
    conn->origin2 : conn->origin;
460
2.39k
}
461
462
struct Curl_peer *Curl_conn_get_destination(struct connectdata *conn,
463
                                            int8_t sockindex)
464
16.8k
{
465
16.8k
  return (sockindex == SECONDARYSOCKET) ?
466
124
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2) :
467
16.8k
    (conn->via_peer ? conn->via_peer : conn->origin);
468
16.8k
}
469
470
struct Curl_peer *Curl_conn_get_first_peer(struct connectdata *conn,
471
                                           int8_t sockindex)
472
45.1k
{
473
45.1k
#ifndef CURL_DISABLE_PROXY
474
45.1k
  if(conn->socks_proxy.peer)
475
150
    return conn->socks_proxy.peer;
476
44.9k
  if(conn->http_proxy.peer)
477
20.0k
    return conn->http_proxy.peer;
478
24.9k
#endif
479
24.9k
  return (sockindex == SECONDARYSOCKET) ?
480
2.20k
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2) :
481
24.9k
    (conn->via_peer ? conn->via_peer : conn->origin);
482
44.9k
}