Coverage Report

Created: 2026-10-22 06:56

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
129k
{
45
129k
  if(len == 2) {
46
81.9k
    if(!memcmp(name, "h1", 2))
47
43.9k
      return ALPN_h1;
48
38.0k
    if(!memcmp(name, "h2", 2))
49
13.0k
      return ALPN_h2;
50
25.0k
    if(!memcmp(name, "h3", 2))
51
5.42k
      return ALPN_h3;
52
25.0k
  }
53
47.7k
  else if(len == 8 && !memcmp(name, "http/1.1", 8))
54
975
    return ALPN_h1;
55
66.3k
  return ALPN_none; /* unknown, probably rubbish input */
56
129k
}
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
20.0M
{
73
20.0M
  timediff_t timeleft_ms = 0;
74
20.0M
  timediff_t ctimeleft_ms = 0;
75
76
20.0M
  if(data->conn && CURL_CONN_IN_SHUTDOWN(data->conn, FIRSTSOCKET))
77
5.09M
    return Curl_cshutdn_timeleft_ms(data, data->conn, FIRSTSOCKET);
78
14.9M
  else if(Curl_is_connecting(data)) {
79
6.07M
    timediff_t ctimeout_ms = (data->set.connecttimeout > 0) ?
80
4.57M
      data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT;
81
6.07M
    ctimeleft_ms = ctimeout_ms -
82
6.07M
      Curl_pgrs_since_ms(data, pnow, TIMER_STARTSINGLE);
83
6.07M
    if(!ctimeleft_ms)
84
46
      ctimeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
85
6.07M
  }
86
8.85M
  else if(!data->set.timeout || data->set.connect_only) {
87
199k
    return 0; /* no timeout in place or checked, return "no limit" */
88
199k
  }
89
90
14.7M
  if(data->set.timeout) {
91
14.7M
    timeleft_ms = data->set.timeout -
92
14.7M
                  Curl_pgrs_since_ms(data, pnow, TIMER_STARTOP);
93
14.7M
    if(!timeleft_ms)
94
2.61k
      timeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */
95
14.7M
  }
96
97
14.7M
  if(!ctimeleft_ms)
98
8.65M
    return timeleft_ms;
99
6.07M
  else if(!timeleft_ms)
100
7.18k
    return ctimeleft_ms;
101
6.07M
  return CURLMIN(ctimeleft_ms, timeleft_ms);
102
14.7M
}
103
104
timediff_t Curl_timeleft_ms(struct Curl_easy *data)
105
8.52M
{
106
8.52M
  return timeleft_now_ms(data, Curl_pgrs_now(data));
107
8.52M
}
108
109
timediff_t Curl_timeleft_now_ms(struct Curl_easy *data,
110
                                const struct curltime *pnow)
111
11.5M
{
112
11.5M
  return timeleft_now_ms(data, pnow);
113
11.5M
}
114
115
/*
116
 * Used to extract socket and connectdata struct for the most recent
117
 * transfer on the given Curl_easy.
118
 *
119
 * The returned socket will be CURL_SOCKET_BAD in case of failure!
120
 */
121
curl_socket_t Curl_getconnectinfo(struct Curl_easy *data,
122
                                  struct connectdata **connp)
123
409k
{
124
  /* this works for an easy handle:
125
   * - that has been used for curl_easy_perform()
126
   * - that is associated with a multi handle, and whose connection
127
   *   was detached with CURLOPT_CONNECT_ONLY
128
   */
129
409k
  struct connectdata *conn = Curl_cpool_get_last_conn(data);
130
409k
  if(connp)
131
110k
    *connp = conn;
132
409k
  return conn ? conn->sock[FIRSTSOCKET] : CURL_SOCKET_BAD;
133
409k
}
134
135
void Curl_conncontrol(struct connectdata *conn, int ctrl)
136
1.18M
{
137
1.18M
  if(!conn) {
138
0
    DEBUGASSERT(0);
139
0
    return;
140
0
  }
141
1.18M
  switch(ctrl) {
142
126
    case CONNCTRL_CONN_KEEP:
143
126
      conn->bits.close = FALSE;
144
126
      break;
145
835k
    case CONNCTRL_CONN_CLOSE:
146
835k
      conn->bits.close = TRUE;
147
835k
      break;
148
345k
    case CONNCTRL_STREAM_CLOSE:
149
      /* stream close when multiplexing does not affect connection */
150
345k
      if(!Curl_conn_is_multiplex(conn, FIRSTSOCKET))
151
212k
        conn->bits.close = TRUE;
152
345k
      break;
153
0
    default:
154
0
      DEBUGASSERT(0);
155
0
      break;
156
1.18M
  }
157
1.18M
}
158
159
CURLcode Curl_conn_setup(struct Curl_easy *data,
160
                         struct connectdata *conn,
161
                         int8_t sockindex,
162
                         int ssl_mode)
163
629k
{
164
629k
  struct Curl_peer *first_peer = Curl_conn_get_first_peer(conn, sockindex);
165
629k
  CURLcode result = CURLE_OK;
166
629k
  uint8_t dns_queries;
167
168
629k
  DEBUGASSERT(data);
169
629k
  DEBUGASSERT(conn->scheme);
170
629k
  DEBUGASSERT(!conn->cfilter[sockindex]);
171
172
629k
  if(!first_peer)
173
0
    return CURLE_FAILED_INIT;
174
175
629k
#ifndef CURL_DISABLE_HTTP
176
629k
  if(!conn->cfilter[sockindex] &&
177
629k
     conn->scheme->protocol == CURLPROTO_HTTPS) {
178
131k
    DEBUGASSERT(ssl_mode != CURL_CF_SSL_DISABLE);
179
131k
    result = Curl_cf_https_setup(
180
131k
      data, Curl_conn_get_destination(conn, sockindex), conn, sockindex);
181
131k
    if(result)
182
26
      goto out;
183
131k
  }
184
629k
#endif /* !CURL_DISABLE_HTTP */
185
186
  /* Still no cfilter set, apply default. */
187
629k
  if(!conn->cfilter[sockindex]) {
188
499k
    result = Curl_cf_setup_add(data, conn, sockindex,
189
499k
                               conn->transport_wanted, ssl_mode);
190
499k
    if(result)
191
71
      goto out;
192
499k
  }
193
194
  /* Whatever the filter chain will be in the end, it will need the
195
   * resolving of `first_peer`. Add that now so the resolve is started
196
   * right away. */
197
629k
  dns_queries = Curl_resolv_dns_queries(data, conn->ip_version);
198
629k
  result = Curl_conn_dns_add_addr_resolve(data, conn, sockindex,
199
629k
                                          first_peer, dns_queries,
200
629k
                                          conn->transport_wanted);
201
629k
  if(result)
202
109
    goto out;
203
204
629k
  DEBUGASSERT(conn->cfilter[sockindex]);
205
629k
out:
206
629k
  return result;
207
629k
}
208
209
#ifdef CURLVERBOSE
210
static CURLcode conn_connect_trace(struct Curl_easy *data,
211
                                   struct Curl_cfilter *cf)
212
503k
{
213
503k
  if(Curl_trc_is_verbose(data)) {
214
38.3k
    struct ip_quadruple ipquad;
215
38.3k
    bool is_ipv6;
216
38.3k
    CURLcode result;
217
218
38.3k
    result = Curl_conn_cf_get_ip_info(cf, data, &is_ipv6, &ipquad);
219
38.3k
    if(result)
220
0
      return result;
221
222
38.3k
    infof(data, "Established %sconnection to %s (%s port %u) from %s port %u ",
223
38.3k
          (cf->sockindex == SECONDARYSOCKET) ? "2nd " : "",
224
38.3k
          CURL_CONN_HOST_DISPNAME(data->conn),
225
38.3k
          ipquad.remote_ip, ipquad.remote_port,
226
38.3k
          ipquad.local_ip, ipquad.local_port);
227
38.3k
  }
228
503k
  return CURLE_OK;
229
503k
}
230
#endif
231
232
/**
233
 * Update connection statistics
234
 */
235
static void conn_report_stats(struct Curl_easy *data, int sockindex)
236
614k
{
237
  /* We do gather stats for the second socket...yet */
238
614k
  if(sockindex == FIRSTSOCKET) {
239
608k
    Curl_conn_cntrl_report_stats(data, data->conn, sockindex);
240
608k
  }
241
614k
}
242
243
void Curl_conn_report_failed(struct Curl_easy *data, int8_t sockindex)
244
112k
{
245
112k
  if(!data->conn || (sockindex != FIRSTSOCKET))
246
925
    return;
247
  /* Only filters connected at IP level have info to update. Filters
248
   * still racing for a connection, like happy eyeballing, pass it on
249
   * to a connected one only. */
250
111k
  (void)Curl_conn_cf_cntrl(data->conn->cfilter[sockindex], data, TRUE,
251
111k
                           CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
252
111k
  conn_report_stats(data, sockindex);
253
111k
}
254
255
CURLcode Curl_conn_connect(struct Curl_easy *data,
256
                           int8_t sockindex,
257
                           bool blocking,
258
                           bool *done)
259
1.22M
{
260
1.22M
#define CF_CONN_NUM_POLLS_ON_STACK 5
261
1.22M
  struct pollfd a_few_on_stack[CF_CONN_NUM_POLLS_ON_STACK];
262
1.22M
  struct easy_pollset ps;
263
1.22M
  struct curl_pollfds cpfds;
264
1.22M
  struct Curl_cfilter *cf;
265
1.22M
  CURLcode result = CURLE_OK;
266
267
1.22M
  DEBUGASSERT(data);
268
1.22M
  DEBUGASSERT(data->conn);
269
1.22M
  if(!CONN_SOCK_IDX_VALID(sockindex))
270
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
271
272
1.22M
  if(data->conn->scheme->flags & PROTOPT_NONETWORK) {
273
0
    *done = TRUE;
274
0
    return CURLE_OK;
275
0
  }
276
277
1.22M
  cf = data->conn->cfilter[sockindex];
278
1.22M
  if(!cf) {
279
0
    *done = FALSE;
280
0
    return CURLE_FAILED_INIT;
281
0
  }
282
283
1.22M
  *done = (bool)cf->connected;
284
1.22M
  if(*done)
285
229k
    return CURLE_OK;
286
287
992k
  Curl_pollset_init(&ps);
288
992k
  Curl_pollfds_init(&cpfds, a_few_on_stack, CF_CONN_NUM_POLLS_ON_STACK);
289
992k
  while(!*done) {
290
992k
    if(Curl_conn_needs_flush(data, sockindex)) {
291
5.35k
      DEBUGF(infof(data, "Curl_conn_connect(index=%d), flush", sockindex));
292
5.35k
      result = Curl_conn_flush(data, sockindex);
293
5.35k
      if(result && (result != CURLE_AGAIN))
294
0
        goto out;
295
5.35k
    }
296
297
992k
    result = cf->cft->do_connect(cf, data, done);
298
992k
    CURL_TRC_CF(data, cf, "Curl_conn_connect(block=%d) -> %d, done=%d",
299
992k
                blocking, (int)result, *done);
300
992k
    if(!result && *done) {
301
      /* A final sanity check on connection security */
302
503k
      if((data->state.origin->scheme->flags & PROTOPT_SSL) &&
303
118k
         (sockindex == FIRSTSOCKET) &&
304
118k
         !Curl_conn_is_ssl(data->conn, FIRSTSOCKET)) {
305
0
        DEBUGASSERT(0);
306
0
        failf(data, "transfer requires SSL, but not connected via SSL");
307
0
        result = CURLE_FAILED_INIT;
308
0
        goto out;
309
0
      }
310
      /* Now that the complete filter chain is connected, let all filters
311
       * persist information at the connection. E.g. cf-socket sets the
312
       * socket and ip related information. */
313
503k
      Curl_conn_cntrl_update_info(data, data->conn);
314
503k
      conn_report_stats(data, sockindex);
315
503k
      Curl_cpool_conn_was_used(data, data->conn, Curl_pgrs_now(data));
316
503k
      VERBOSE(result = conn_connect_trace(data, cf));
317
503k
      VERBOSE(Curl_conn_trc_filters(data, sockindex, "connected"));
318
503k
      Curl_conn_remove_setup_filters(data, sockindex);
319
503k
      VERBOSE(Curl_conn_trc_filters(data, sockindex, "reduced to"));
320
503k
      goto out;
321
503k
    }
322
489k
    else if(result) {
323
111k
      CURL_TRC_CF(data, cf, "Curl_conn_connect(), filter returned %d",
324
111k
                  (int)result);
325
111k
      VERBOSE(Curl_conn_trc_filters(data, sockindex, "failed to connect"));
326
111k
      Curl_conn_report_failed(data, sockindex);
327
111k
      goto out;
328
111k
    }
329
330
377k
    if(!blocking)
331
377k
      goto out;
332
39
    else {
333
      /* check allowed time left */
334
39
      const timediff_t timeout_ms = Curl_timeleft_ms(data);
335
39
      curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
336
39
      int rc;
337
338
39
      if(timeout_ms < 0) {
339
        /* no need to continue if time already is up */
340
20
        failf(data, "connect timeout");
341
20
        result = CURLE_OPERATION_TIMEDOUT;
342
20
        goto out;
343
20
      }
344
345
19
      CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), do poll");
346
19
      Curl_pollset_reset(&ps);
347
19
      Curl_pollfds_reset(&cpfds);
348
      /* In general, we want to send after connect, wait on that. */
349
19
      if(sockfd != CURL_SOCKET_BAD)
350
19
        result = Curl_pollset_set_out_only(data, &ps, sockfd);
351
19
      if(!result)
352
19
        result = Curl_conn_adjust_pollset(data, data->conn, &ps);
353
19
      if(result)
354
0
        goto out;
355
19
      result = Curl_pollfds_add_ps(&cpfds, &ps);
356
19
      if(result)
357
0
        goto out;
358
359
19
      rc = Curl_poll(cpfds.pfds, cpfds.n,
360
19
                     CURLMIN(timeout_ms ? timeout_ms : 1000,
361
19
                             (cpfds.n ? 1000 : 10)));
362
19
      CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), Curl_poll() -> %d",
363
19
                  rc);
364
19
      if(rc < 0) {
365
0
        result = CURLE_COULDNT_CONNECT;
366
0
        goto out;
367
0
      }
368
      /* continue iterating */
369
19
    }
370
377k
  }
371
372
992k
out:
373
992k
  Curl_pollset_cleanup(&ps);
374
992k
  Curl_pollfds_cleanup(&cpfds);
375
992k
  return result;
376
992k
}
377
378
void Curl_conn_set_multiplex(struct connectdata *conn)
379
167k
{
380
167k
  if(!conn->bits.multiplex) {
381
167k
    conn->bits.multiplex = TRUE;
382
167k
    if(conn->attached_multi) {
383
167k
      Curl_multi_connchanged(conn->attached_multi);
384
167k
    }
385
167k
  }
386
167k
}
387
388
struct Curl_peer *Curl_conn_get_origin(struct connectdata *conn,
389
                                       int8_t sockindex)
390
155k
{
391
155k
  return (sockindex == SECONDARYSOCKET) ?
392
155k
    conn->origin2 : conn->origin;
393
155k
}
394
395
struct Curl_peer *Curl_conn_get_destination(struct connectdata *conn,
396
                                            int8_t sockindex)
397
1.24M
{
398
1.24M
  return (sockindex == SECONDARYSOCKET) ?
399
151
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2) :
400
1.24M
    (conn->via_peer ? conn->via_peer : conn->origin);
401
1.24M
}
402
403
struct Curl_peer *Curl_conn_get_first_peer(struct connectdata *conn,
404
                                           int8_t sockindex)
405
3.03M
{
406
3.03M
#ifndef CURL_DISABLE_PROXY
407
3.03M
  if(conn->socks_proxy.peer)
408
73.5k
    return conn->socks_proxy.peer;
409
2.96M
  if(conn->http_proxy.peer)
410
255k
    return conn->http_proxy.peer;
411
2.70M
#endif
412
2.70M
  return (sockindex == SECONDARYSOCKET) ?
413
13.8k
    (conn->via_peer2 ? conn->via_peer2 : conn->origin2) :
414
2.70M
    (conn->via_peer ? conn->via_peer : conn->origin);
415
2.96M
}