Coverage Report

Created: 2026-09-04 07:15

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