Coverage Report

Created: 2026-09-14 06:43

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