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