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 | 354k | { |
73 | 354k | timediff_t timeleft_ms = 0; |
74 | 354k | timediff_t ctimeleft_ms = 0; |
75 | | |
76 | 354k | if(data->conn && CURL_CONN_IN_SHUTDOWN(data->conn, FIRSTSOCKET)) |
77 | 1.15k | return Curl_cshutdn_timeleft_ms(data, data->conn, FIRSTSOCKET); |
78 | 353k | else if(Curl_is_connecting(data)) { |
79 | 227k | timediff_t ctimeout_ms = (data->set.connecttimeout > 0) ? |
80 | 220k | data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT; |
81 | 227k | ctimeleft_ms = ctimeout_ms - |
82 | 227k | Curl_pgrs_since_ms(data, pnow, TIMER_STARTSINGLE); |
83 | 227k | if(!ctimeleft_ms) |
84 | 1 | ctimeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */ |
85 | 227k | } |
86 | 125k | else if(!data->set.timeout || data->set.connect_only) { |
87 | 510 | return 0; /* no timeout in place or checked, return "no limit" */ |
88 | 510 | } |
89 | | |
90 | 352k | if(data->set.timeout) { |
91 | 352k | timeleft_ms = data->set.timeout - |
92 | 352k | Curl_pgrs_since_ms(data, pnow, TIMER_STARTOP); |
93 | 352k | if(!timeleft_ms) |
94 | 38 | timeleft_ms = -1; /* 0 is "no limit", fake 1 ms expiry */ |
95 | 352k | } |
96 | | |
97 | 352k | if(!ctimeleft_ms) |
98 | 124k | return timeleft_ms; |
99 | 227k | else if(!timeleft_ms) |
100 | 50 | return ctimeleft_ms; |
101 | 227k | return CURLMIN(ctimeleft_ms, timeleft_ms); |
102 | 352k | } |
103 | | |
104 | | timediff_t Curl_timeleft_ms(struct Curl_easy *data) |
105 | 74.1k | { |
106 | 74.1k | return timeleft_now_ms(data, Curl_pgrs_now(data)); |
107 | 74.1k | } |
108 | | |
109 | | timediff_t Curl_timeleft_now_ms(struct Curl_easy *data, |
110 | | const struct curltime *pnow) |
111 | 280k | { |
112 | 280k | return timeleft_now_ms(data, pnow); |
113 | 280k | } |
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 | 66 | { |
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 | 66 | struct connectdata *conn = Curl_cpool_get_last_conn(data); |
130 | 66 | if(connp) |
131 | 66 | *connp = conn; |
132 | 66 | return conn ? conn->sock[FIRSTSOCKET] : CURL_SOCKET_BAD; |
133 | 66 | } |
134 | | |
135 | | void Curl_conncontrol(struct connectdata *conn, int ctrl) |
136 | 56.2k | { |
137 | 56.2k | if(!conn) { |
138 | 0 | DEBUGASSERT(0); |
139 | 0 | return; |
140 | 0 | } |
141 | 56.2k | switch(ctrl) { |
142 | 0 | case CONNCTRL_CONN_KEEP: |
143 | 0 | conn->bits.close = FALSE; |
144 | 0 | break; |
145 | 41.1k | case CONNCTRL_CONN_CLOSE: |
146 | 41.1k | conn->bits.close = TRUE; |
147 | 41.1k | break; |
148 | 15.0k | case CONNCTRL_STREAM_CLOSE: |
149 | | /* stream close when multiplexing does not affect connection */ |
150 | 15.0k | if(!Curl_conn_is_multiplex(conn, FIRSTSOCKET)) |
151 | 11.2k | conn->bits.close = TRUE; |
152 | 15.0k | break; |
153 | 0 | default: |
154 | 0 | DEBUGASSERT(0); |
155 | 0 | break; |
156 | 56.2k | } |
157 | 56.2k | } |
158 | | |
159 | | CURLcode Curl_conn_setup(struct Curl_easy *data, |
160 | | struct connectdata *conn, |
161 | | int8_t sockindex, |
162 | | int ssl_mode) |
163 | 27.3k | { |
164 | 27.3k | struct Curl_peer *first_peer = Curl_conn_get_first_peer(conn, sockindex); |
165 | 27.3k | CURLcode result = CURLE_OK; |
166 | 27.3k | uint8_t dns_queries; |
167 | | |
168 | 27.3k | DEBUGASSERT(data); |
169 | 27.3k | DEBUGASSERT(conn->scheme); |
170 | 27.3k | DEBUGASSERT(!conn->cfilter[sockindex]); |
171 | | |
172 | 27.3k | if(!first_peer) |
173 | 0 | return CURLE_FAILED_INIT; |
174 | | |
175 | 27.3k | #ifndef CURL_DISABLE_HTTP |
176 | 27.3k | if(!conn->cfilter[sockindex] && |
177 | 27.3k | conn->scheme->protocol == CURLPROTO_HTTPS) { |
178 | 1.78k | DEBUGASSERT(ssl_mode != CURL_CF_SSL_DISABLE); |
179 | 1.78k | result = Curl_cf_https_setup( |
180 | 1.78k | data, Curl_conn_get_destination(conn, sockindex), conn, sockindex); |
181 | 1.78k | if(result) |
182 | 0 | goto out; |
183 | 1.78k | } |
184 | 27.3k | #endif /* !CURL_DISABLE_HTTP */ |
185 | | |
186 | | /* Still no cfilter set, apply default. */ |
187 | 27.3k | if(!conn->cfilter[sockindex]) { |
188 | 25.5k | result = Curl_cf_setup_add(data, conn, sockindex, |
189 | 25.5k | conn->transport_wanted, ssl_mode); |
190 | 25.5k | if(result) |
191 | 0 | goto out; |
192 | 25.5k | } |
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 | 27.3k | dns_queries = Curl_resolv_dns_queries(data, conn->ip_version); |
198 | 27.3k | result = Curl_conn_dns_add_addr_resolve(data, conn, sockindex, |
199 | 27.3k | first_peer, dns_queries, |
200 | 27.3k | conn->transport_wanted); |
201 | 27.3k | if(result) |
202 | 0 | goto out; |
203 | | |
204 | 27.3k | DEBUGASSERT(conn->cfilter[sockindex]); |
205 | 27.3k | out: |
206 | 27.3k | return result; |
207 | 27.3k | } |
208 | | |
209 | | #ifdef CURLVERBOSE |
210 | | static CURLcode conn_connect_trace(struct Curl_easy *data, |
211 | | struct Curl_cfilter *cf) |
212 | 23.2k | { |
213 | 23.2k | if(Curl_trc_is_verbose(data)) { |
214 | 0 | struct ip_quadruple ipquad; |
215 | 0 | bool is_ipv6; |
216 | 0 | CURLcode result; |
217 | |
|
218 | 0 | result = Curl_conn_cf_get_ip_info(cf, data, &is_ipv6, &ipquad); |
219 | 0 | if(result) |
220 | 0 | return result; |
221 | | |
222 | 0 | infof(data, "Established %sconnection to %s (%s port %u) from %s port %u ", |
223 | 0 | (cf->sockindex == SECONDARYSOCKET) ? "2nd " : "", |
224 | 0 | CURL_CONN_HOST_DISPNAME(data->conn), |
225 | 0 | ipquad.remote_ip, ipquad.remote_port, |
226 | 0 | ipquad.local_ip, ipquad.local_port); |
227 | 0 | } |
228 | 23.2k | return CURLE_OK; |
229 | 23.2k | } |
230 | | #endif |
231 | | |
232 | | /** |
233 | | * Update connection statistics |
234 | | */ |
235 | | static void conn_report_stats(struct Curl_easy *data, int sockindex) |
236 | 26.9k | { |
237 | | /* We do gather stats for the second socket...yet */ |
238 | 26.9k | if(sockindex == FIRSTSOCKET) { |
239 | 26.3k | Curl_conn_cntrl_report_stats(data, data->conn, sockindex); |
240 | 26.3k | } |
241 | 26.9k | } |
242 | | |
243 | | void Curl_conn_report_failed(struct Curl_easy *data, int8_t sockindex) |
244 | 3.78k | { |
245 | 3.78k | if(!data->conn || (sockindex != FIRSTSOCKET)) |
246 | 117 | 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 | 3.66k | (void)Curl_conn_cf_cntrl(data->conn->cfilter[sockindex], data, TRUE, |
251 | 3.66k | CF_CTRL_CONN_INFO_UPDATE, 0, NULL); |
252 | 3.66k | conn_report_stats(data, sockindex); |
253 | 3.66k | } |
254 | | |
255 | | CURLcode Curl_conn_connect(struct Curl_easy *data, |
256 | | int8_t sockindex, |
257 | | bool blocking, |
258 | | bool *done) |
259 | 36.8k | { |
260 | 36.8k | #define CF_CONN_NUM_POLLS_ON_STACK 5 |
261 | 36.8k | struct pollfd a_few_on_stack[CF_CONN_NUM_POLLS_ON_STACK]; |
262 | 36.8k | struct easy_pollset ps; |
263 | 36.8k | struct curl_pollfds cpfds; |
264 | 36.8k | struct Curl_cfilter *cf; |
265 | 36.8k | CURLcode result = CURLE_OK; |
266 | | |
267 | 36.8k | DEBUGASSERT(data); |
268 | 36.8k | DEBUGASSERT(data->conn); |
269 | 36.8k | if(!CONN_SOCK_IDX_VALID(sockindex)) |
270 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
271 | | |
272 | 36.8k | if(data->conn->scheme->flags & PROTOPT_NONETWORK) { |
273 | 0 | *done = TRUE; |
274 | 0 | return CURLE_OK; |
275 | 0 | } |
276 | | |
277 | 36.8k | cf = data->conn->cfilter[sockindex]; |
278 | 36.8k | if(!cf) { |
279 | 0 | *done = FALSE; |
280 | 0 | return CURLE_FAILED_INIT; |
281 | 0 | } |
282 | | |
283 | 36.8k | *done = (bool)cf->connected; |
284 | 36.8k | if(*done) |
285 | 8.90k | return CURLE_OK; |
286 | | |
287 | 27.9k | Curl_pollset_init(&ps); |
288 | 27.9k | Curl_pollfds_init(&cpfds, a_few_on_stack, CF_CONN_NUM_POLLS_ON_STACK); |
289 | 27.9k | while(!*done) { |
290 | 27.9k | if(Curl_conn_needs_flush(data, sockindex)) { |
291 | 0 | DEBUGF(infof(data, "Curl_conn_connect(index=%d), flush", sockindex)); |
292 | 0 | result = Curl_conn_flush(data, sockindex); |
293 | 0 | if(result && (result != CURLE_AGAIN)) |
294 | 0 | goto out; |
295 | 0 | } |
296 | | |
297 | 27.9k | result = cf->cft->do_connect(cf, data, done); |
298 | 27.9k | CURL_TRC_CF(data, cf, "Curl_conn_connect(block=%d) -> %d, done=%d", |
299 | 27.9k | blocking, (int)result, *done); |
300 | 27.9k | if(!result && *done) { |
301 | | /* A final sanity check on connection security */ |
302 | 23.2k | if((data->state.origin->scheme->flags & PROTOPT_SSL) && |
303 | 0 | (sockindex == FIRSTSOCKET) && |
304 | 0 | !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 | 23.2k | Curl_conn_cntrl_update_info(data, data->conn); |
314 | 23.2k | conn_report_stats(data, sockindex); |
315 | 23.2k | Curl_cpool_conn_was_used(data, data->conn, Curl_pgrs_now(data)); |
316 | 23.2k | VERBOSE(result = conn_connect_trace(data, cf)); |
317 | 23.2k | VERBOSE(Curl_conn_trc_filters(data, sockindex, "connected")); |
318 | 23.2k | Curl_conn_remove_setup_filters(data, sockindex); |
319 | 23.2k | VERBOSE(Curl_conn_trc_filters(data, sockindex, "reduced to")); |
320 | 23.2k | goto out; |
321 | 23.2k | } |
322 | 4.64k | else if(result) { |
323 | 3.77k | CURL_TRC_CF(data, cf, "Curl_conn_connect(), filter returned %d", |
324 | 3.77k | (int)result); |
325 | 3.77k | VERBOSE(Curl_conn_trc_filters(data, sockindex, "failed to connect")); |
326 | 3.77k | Curl_conn_report_failed(data, sockindex); |
327 | 3.77k | goto out; |
328 | 3.77k | } |
329 | | |
330 | 868 | if(!blocking) |
331 | 868 | goto out; |
332 | 0 | else { |
333 | | /* check allowed time left */ |
334 | 0 | const timediff_t timeout_ms = Curl_timeleft_ms(data); |
335 | 0 | curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data); |
336 | 0 | int rc; |
337 | |
|
338 | 0 | if(timeout_ms < 0) { |
339 | | /* no need to continue if time already is up */ |
340 | 0 | failf(data, "connect timeout"); |
341 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
342 | 0 | goto out; |
343 | 0 | } |
344 | | |
345 | 0 | CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), do poll"); |
346 | 0 | Curl_pollset_reset(&ps); |
347 | 0 | Curl_pollfds_reset(&cpfds); |
348 | | /* In general, we want to send after connect, wait on that. */ |
349 | 0 | if(sockfd != CURL_SOCKET_BAD) |
350 | 0 | result = Curl_pollset_set_out_only(data, &ps, sockfd); |
351 | 0 | if(!result) |
352 | 0 | result = Curl_conn_adjust_pollset(data, data->conn, &ps); |
353 | 0 | if(result) |
354 | 0 | goto out; |
355 | 0 | result = Curl_pollfds_add_ps(&cpfds, &ps); |
356 | 0 | if(result) |
357 | 0 | goto out; |
358 | | |
359 | 0 | rc = Curl_poll(cpfds.pfds, cpfds.n, |
360 | 0 | CURLMIN(timeout_ms ? timeout_ms : 1000, |
361 | 0 | (cpfds.n ? 1000 : 10))); |
362 | 0 | CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), Curl_poll() -> %d", |
363 | 0 | rc); |
364 | 0 | if(rc < 0) { |
365 | 0 | result = CURLE_COULDNT_CONNECT; |
366 | 0 | goto out; |
367 | 0 | } |
368 | | /* continue iterating */ |
369 | 0 | } |
370 | 868 | } |
371 | | |
372 | 27.9k | out: |
373 | 27.9k | Curl_pollset_cleanup(&ps); |
374 | 27.9k | Curl_pollfds_cleanup(&cpfds); |
375 | 27.9k | return result; |
376 | 27.9k | } |
377 | | |
378 | | void Curl_conn_set_multiplex(struct connectdata *conn) |
379 | 5.20k | { |
380 | 5.20k | if(!conn->bits.multiplex) { |
381 | 5.20k | conn->bits.multiplex = TRUE; |
382 | 5.20k | if(conn->attached_multi) { |
383 | 5.20k | Curl_multi_connchanged(conn->attached_multi); |
384 | 5.20k | } |
385 | 5.20k | } |
386 | 5.20k | } |
387 | | |
388 | | struct Curl_peer *Curl_conn_get_origin(struct connectdata *conn, |
389 | | int8_t sockindex) |
390 | 1.73k | { |
391 | 1.73k | return (sockindex == SECONDARYSOCKET) ? |
392 | 1.73k | conn->origin2 : conn->origin; |
393 | 1.73k | } |
394 | | |
395 | | struct Curl_peer *Curl_conn_get_destination(struct connectdata *conn, |
396 | | int8_t sockindex) |
397 | 39.5k | { |
398 | 39.5k | return (sockindex == SECONDARYSOCKET) ? |
399 | 0 | (conn->via_peer2 ? conn->via_peer2 : conn->origin2) : |
400 | 39.5k | (conn->via_peer ? conn->via_peer : conn->origin); |
401 | 39.5k | } |
402 | | |
403 | | struct Curl_peer *Curl_conn_get_first_peer(struct connectdata *conn, |
404 | | int8_t sockindex) |
405 | 123k | { |
406 | 123k | #ifndef CURL_DISABLE_PROXY |
407 | 123k | if(conn->socks_proxy.peer) |
408 | 285 | return conn->socks_proxy.peer; |
409 | 123k | if(conn->http_proxy.peer) |
410 | 9.76k | return conn->http_proxy.peer; |
411 | 113k | #endif |
412 | 113k | return (sockindex == SECONDARYSOCKET) ? |
413 | 1.41k | (conn->via_peer2 ? conn->via_peer2 : conn->origin2) : |
414 | 113k | (conn->via_peer ? conn->via_peer : conn->origin); |
415 | 123k | } |