/src/curl/lib/cf-https-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 | | #ifndef CURL_DISABLE_HTTP |
27 | | |
28 | | #include "urldata.h" |
29 | | #include "curl_trc.h" |
30 | | #include "cfilters.h" |
31 | | #include "cf-setup.h" |
32 | | #include "connect.h" |
33 | | #include "multiif.h" |
34 | | #include "cf-https-connect.h" |
35 | | #include "http2.h" |
36 | | #include "progress.h" |
37 | | #include "select.h" |
38 | | #include "vdns/cf-dns.h" |
39 | | #include "vdns/hostip.h" |
40 | | #include "vdns/httpsrr.h" |
41 | | #include "vquic/vquic.h" |
42 | | |
43 | | typedef enum { |
44 | | CF_HC_RESOLV, |
45 | | CF_HC_INIT, |
46 | | CF_HC_CONNECT, |
47 | | CF_HC_SUCCESS, |
48 | | CF_HC_FAILURE |
49 | | } cf_hc_state; |
50 | | |
51 | | struct cf_hc_baller { |
52 | | const char *name; |
53 | | struct Curl_cfchain chain; |
54 | | CURLcode result; |
55 | | int reply_ms; |
56 | | uint8_t transport; |
57 | | enum alpnid alpn_id; |
58 | | BIT(shutdown); |
59 | | }; |
60 | | |
61 | | static void cf_hc_baller_discard(struct cf_hc_baller *b, |
62 | | struct Curl_easy *data) |
63 | 6 | { |
64 | 6 | Curl_cfchain_cleanup(&b->chain, data); |
65 | 6 | } |
66 | | |
67 | | static bool cf_hc_baller_is_connecting(struct cf_hc_baller *b) |
68 | 12 | { |
69 | 12 | return b->chain.cf && !b->result; |
70 | 12 | } |
71 | | |
72 | | static bool cf_hc_baller_has_started(struct cf_hc_baller *b) |
73 | 0 | { |
74 | 0 | return !!b->chain.cf; |
75 | 0 | } |
76 | | |
77 | | static int cf_hc_baller_reply_ms(struct cf_hc_baller *b, |
78 | | struct Curl_easy *data) |
79 | 0 | { |
80 | 0 | if(b->chain.cf && (b->reply_ms < 0)) |
81 | 0 | b->chain.cf->cft->query(b->chain.cf, data, CF_QUERY_CONNECT_REPLY_MS, |
82 | 0 | &b->reply_ms, NULL); |
83 | 0 | return b->reply_ms; |
84 | 0 | } |
85 | | |
86 | | static bool cf_hc_baller_data_pending(struct cf_hc_baller *b, |
87 | | const struct Curl_easy *data) |
88 | 0 | { |
89 | 0 | return b->chain.cf && !b->result && |
90 | 0 | b->chain.cf->cft->has_data_pending(b->chain.cf, data); |
91 | 0 | } |
92 | | |
93 | | static bool cf_hc_baller_needs_flush(struct cf_hc_baller *b, |
94 | | struct Curl_easy *data) |
95 | 0 | { |
96 | 0 | return b->chain.cf && !b->result && |
97 | 0 | Curl_cfchain_needs_flush(data, &b->chain); |
98 | 0 | } |
99 | | |
100 | | static CURLcode cf_hc_baller_cntrl(struct cf_hc_baller *b, |
101 | | struct Curl_easy *data, |
102 | | int event, int arg1, void *arg2) |
103 | 6 | { |
104 | 6 | if(b->chain.cf && !b->result) |
105 | 0 | return Curl_conn_cf_cntrl(b->chain.cf, data, FALSE, event, arg1, arg2); |
106 | 6 | return CURLE_OK; |
107 | 6 | } |
108 | | |
109 | | struct cf_hc_ctx { |
110 | | cf_hc_state state; |
111 | | struct Curl_peer *destination; /* who we ultimately want to talk to */ |
112 | | struct curltime started; /* when connect started */ |
113 | | CURLcode result; /* overall result */ |
114 | | CURLcode check_h3_result; |
115 | | struct cf_hc_baller ballers[2]; |
116 | | size_t baller_count; |
117 | | timediff_t soft_eyeballs_timeout_ms; |
118 | | timediff_t hard_eyeballs_timeout_ms; |
119 | | uint8_t def_transport; |
120 | | BIT(httpsrr_resolved); |
121 | | BIT(checked_h3); |
122 | | BIT(ballers_complete); |
123 | | }; |
124 | | |
125 | | static void cf_hc_ctx_destroy(struct Curl_easy *data, |
126 | | struct cf_hc_ctx *ctx) |
127 | 17 | { |
128 | 17 | if(ctx) { |
129 | 9 | size_t i; |
130 | 15 | for(i = 0; i < ctx->baller_count; ++i) |
131 | 6 | cf_hc_baller_discard(&ctx->ballers[i], data); |
132 | 9 | Curl_peer_unlink(&ctx->destination); |
133 | 9 | curlx_free(ctx); |
134 | 9 | } |
135 | 17 | } |
136 | | |
137 | | static void cf_hc_baller_assign(struct cf_hc_baller *b, |
138 | | struct Curl_cfilter *cf, |
139 | | enum alpnid alpn_id, |
140 | | uint8_t def_transport) |
141 | 6 | { |
142 | 6 | b->alpn_id = alpn_id; |
143 | 6 | b->transport = def_transport; |
144 | 6 | b->result = CURLE_OK; |
145 | 6 | b->reply_ms = -1; |
146 | 6 | b->shutdown = FALSE; |
147 | 6 | Curl_cfchain_init_child(&b->chain, cf->chain); |
148 | 6 | switch(b->alpn_id) { |
149 | 0 | case ALPN_h3: |
150 | 0 | b->name = "h3"; |
151 | 0 | b->transport = TRNSPRT_QUIC; |
152 | 0 | break; |
153 | 6 | case ALPN_h2: |
154 | 6 | b->name = "h2"; |
155 | 6 | break; |
156 | 0 | case ALPN_h1: |
157 | 0 | b->name = "h1"; |
158 | 0 | break; |
159 | 0 | case ALPN_none: |
160 | 0 | b->name = "no-alpn"; |
161 | 0 | break; |
162 | 0 | default: |
163 | 0 | b->result = CURLE_FAILED_INIT; |
164 | 0 | break; |
165 | 6 | } |
166 | 6 | } |
167 | | |
168 | | static void cf_hc_baller_init(struct cf_hc_baller *b, |
169 | | struct Curl_easy *data) |
170 | 6 | { |
171 | 6 | DEBUGASSERT(b->chain.conn); |
172 | 6 | b->result = Curl_cf_setup_add(&b->chain, data, b->transport, |
173 | 6 | CURL_CF_SSL_ENABLE); |
174 | 6 | } |
175 | | |
176 | | static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b, |
177 | | struct Curl_easy *data, |
178 | | struct Curl_cfilter *cf, |
179 | | bool *done) |
180 | 6 | { |
181 | 6 | (void)cf; |
182 | 6 | VERBOSE(Curl_cfchain_trc(data, cf, &b->chain, b->name)); |
183 | 6 | b->result = Curl_conn_cf_connect(b->chain.cf, data, done); |
184 | 6 | return b->result; |
185 | 6 | } |
186 | | |
187 | | static CURLcode baller_connected(struct Curl_cfilter *cf, |
188 | | struct Curl_easy *data, |
189 | | struct cf_hc_baller *winner) |
190 | 0 | { |
191 | 0 | struct cf_hc_ctx *ctx = cf->ctx; |
192 | | |
193 | | /* Make the winner's connection filter out own sub-filter, check, move, |
194 | | * close all remaining. */ |
195 | 0 | if(cf->next) { |
196 | 0 | DEBUGASSERT(0); |
197 | 0 | return CURLE_FAILED_INIT; |
198 | 0 | } |
199 | 0 | if(!winner->chain.cf) { |
200 | 0 | DEBUGASSERT(0); |
201 | 0 | return CURLE_FAILED_INIT; |
202 | 0 | } |
203 | | |
204 | 0 | Curl_cfchain_merge(cf->chain, &winner->chain); |
205 | 0 | ctx->state = CF_HC_SUCCESS; |
206 | 0 | cf->connected = TRUE; |
207 | 0 | VERBOSE(Curl_cfchain_trc(data, cf, cf->chain, "merged winner")); |
208 | | |
209 | | /* ballers may have failf()'d, the winner resets it, so our |
210 | | * errorbuf is clean again. */ |
211 | 0 | Curl_reset_fail(data); |
212 | 0 | return CURLE_OK; |
213 | 0 | } |
214 | | |
215 | | static bool time_to_start_baller2(struct Curl_cfilter *cf, |
216 | | struct Curl_easy *data, |
217 | | const struct curltime *pnow) |
218 | 6 | { |
219 | 6 | struct cf_hc_ctx *ctx = cf->ctx; |
220 | 6 | timediff_t elapsed_ms; |
221 | | |
222 | 6 | if(ctx->baller_count < 2) |
223 | 6 | return FALSE; |
224 | 0 | else if(cf_hc_baller_has_started(&ctx->ballers[1])) |
225 | 0 | return FALSE; |
226 | 0 | else if(ctx->ballers[0].result) { |
227 | 0 | CURL_TRC_CF(data, cf, "%s baller failed, starting %s", |
228 | 0 | ctx->ballers[0].name, ctx->ballers[1].name); |
229 | 0 | return TRUE; |
230 | 0 | } |
231 | | |
232 | 0 | elapsed_ms = curlx_ptimediff_ms(pnow, &ctx->started); |
233 | 0 | if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) { |
234 | 0 | CURL_TRC_CF(data, cf, "%s inconclusive after %" FMT_TIMEDIFF_T ", " |
235 | 0 | "starting %s", ctx->ballers[0].name, |
236 | 0 | ctx->hard_eyeballs_timeout_ms, ctx->ballers[1].name); |
237 | 0 | return TRUE; |
238 | 0 | } |
239 | 0 | else if(elapsed_ms >= ctx->soft_eyeballs_timeout_ms) { |
240 | 0 | if(cf_hc_baller_reply_ms(&ctx->ballers[0], data) < 0) { |
241 | 0 | CURL_TRC_CF(data, cf, "%s has not seen any data after %" |
242 | 0 | FMT_TIMEDIFF_T "ms, starting %s", |
243 | 0 | ctx->ballers[0].name, ctx->soft_eyeballs_timeout_ms, |
244 | 0 | ctx->ballers[1].name); |
245 | 0 | return TRUE; |
246 | 0 | } |
247 | | /* set the effective hard timeout again */ |
248 | 0 | Curl_expire_set(data, EXPIRE_ALPN_EYEBALLS, |
249 | 0 | ctx->hard_eyeballs_timeout_ms - elapsed_ms, pnow); |
250 | 0 | } |
251 | 0 | return FALSE; |
252 | 0 | } |
253 | | |
254 | | static bool cf_hc_may_h3(struct Curl_cfilter *cf, |
255 | | struct Curl_easy *data) |
256 | 0 | { |
257 | 0 | struct cf_hc_ctx *ctx = cf->ctx; |
258 | 0 | if(!ctx->checked_h3) { |
259 | 0 | ctx->check_h3_result = |
260 | 0 | Curl_conn_may_http3(data, CF_CONN(cf)); |
261 | 0 | ctx->checked_h3 = TRUE; |
262 | 0 | } |
263 | 0 | return !ctx->check_h3_result; |
264 | 0 | } |
265 | | |
266 | | static bool cf_hc_may_choose_alpn(struct Curl_cfilter *cf, |
267 | | struct Curl_easy *data, |
268 | | enum alpnid maybe, |
269 | | enum alpnid already_using) |
270 | 18 | { |
271 | 18 | http_majors allowed = data->state.http_neg.allowed; |
272 | | |
273 | 18 | if(maybe == already_using) |
274 | 6 | return FALSE; |
275 | 12 | switch(maybe) { |
276 | 6 | case ALPN_h1: |
277 | 6 | return (allowed & CURL_HTTP_V1x) != 0; |
278 | 0 | case ALPN_h2: |
279 | 0 | return (allowed & CURL_HTTP_V2x) != 0; |
280 | 6 | case ALPN_h3: |
281 | 6 | return (allowed & CURL_HTTP_V3x) && cf_hc_may_h3(cf, data); |
282 | 0 | default: |
283 | 0 | return FALSE; |
284 | 12 | } |
285 | 12 | } |
286 | | |
287 | | static enum alpnid cf_hc_get_httpsrr_alpn(struct Curl_cfilter *cf, |
288 | | struct Curl_easy *data, |
289 | | enum alpnid not_this_one) |
290 | 6 | { |
291 | 6 | #ifdef USE_HTTPSRR |
292 | 6 | struct cf_hc_ctx *ctx = cf->ctx; |
293 | | /* Is there an HTTPSRR use its ALPNs here. |
294 | | * We are here after having selected a connection to a host+port and |
295 | | * can no longer change that. Any HTTPSRR advice for other hosts and ports |
296 | | * we need to ignore. */ |
297 | 6 | const struct Curl_https_rrinfo *rr; |
298 | 6 | size_t i; |
299 | | |
300 | | /* Do we have HTTPS-RR information? */ |
301 | 6 | rr = Curl_dns_cf_get_https(data, cf, ctx->destination); |
302 | | |
303 | 6 | CURL_TRC_CF(data, cf, "HTTPS-RR %savailable", rr ? "" : "not "); |
304 | | /* We do not support `rr->no_def_alpn`. */ |
305 | 6 | if(Curl_httpsrr_applicable(data, rr) && !rr->no_def_alpn) { |
306 | 0 | for(i = 0; i < CURL_ARRAYSIZE(rr->alpns); ++i) { |
307 | 0 | enum alpnid alpn_rr = (enum alpnid)rr->alpns[i]; |
308 | 0 | if(alpn_rr == not_this_one) /* do not want this one */ |
309 | 0 | continue; |
310 | 0 | switch(alpn_rr) { |
311 | 0 | case ALPN_h3: |
312 | 0 | if((data->state.http_neg.allowed & CURL_HTTP_V3x) && |
313 | 0 | cf_hc_may_h3(cf, data)) { |
314 | 0 | return alpn_rr; |
315 | 0 | } |
316 | 0 | break; |
317 | 0 | case ALPN_h2: |
318 | 0 | if(data->state.http_neg.allowed & CURL_HTTP_V2x) { |
319 | 0 | return alpn_rr; |
320 | 0 | } |
321 | 0 | break; |
322 | 0 | case ALPN_h1: |
323 | 0 | if(data->state.http_neg.allowed & CURL_HTTP_V1x) { |
324 | 0 | return alpn_rr; |
325 | 0 | } |
326 | 0 | break; |
327 | 0 | default: /* ignore */ |
328 | 0 | break; |
329 | 0 | } |
330 | 0 | } |
331 | 0 | } |
332 | | #else |
333 | | (void)cf; |
334 | | (void)data; |
335 | | (void)not_this_one; |
336 | | #endif |
337 | 6 | return ALPN_none; |
338 | 6 | } |
339 | | |
340 | | static enum alpnid cf_hc_get_pref_alpn(struct Curl_cfilter *cf, |
341 | | struct Curl_easy *data, |
342 | | enum alpnid not_this_one) |
343 | 6 | { |
344 | 6 | if((data->state.http_neg.preferred & data->state.http_neg.allowed)) { |
345 | 0 | switch(data->state.http_neg.preferred) { |
346 | 0 | case CURL_HTTP_V3x: |
347 | 0 | if(cf_hc_may_h3(cf, data) && (ALPN_h3 != not_this_one)) |
348 | 0 | return ALPN_h3; |
349 | 0 | break; |
350 | 0 | case CURL_HTTP_V2x: |
351 | 0 | if(ALPN_h2 != not_this_one) |
352 | 0 | return ALPN_h2; |
353 | 0 | break; |
354 | 0 | case CURL_HTTP_V1x: |
355 | | /* If we are trying h2 already, h1 is already used as fallback */ |
356 | 0 | if((ALPN_h1 != not_this_one) && (ALPN_h2 != not_this_one)) |
357 | 0 | return ALPN_h1; |
358 | 0 | break; |
359 | 0 | default: |
360 | 0 | break; |
361 | 0 | } |
362 | 0 | } |
363 | 6 | return ALPN_none; |
364 | 6 | } |
365 | | |
366 | | static enum alpnid cf_hc_get_first_alpn(struct Curl_cfilter *cf, |
367 | | struct Curl_easy *data, |
368 | | http_majors choices, |
369 | | enum alpnid not_this_one) |
370 | 6 | { |
371 | | /* When told to not try h2, we also do not try h1 and vice versa */ |
372 | 6 | bool allow_h1_or_h2 = (not_this_one != ALPN_h1) && |
373 | 6 | (not_this_one != ALPN_h2); |
374 | 6 | if((ALPN_h3 != not_this_one) && (choices & CURL_HTTP_V3x) && |
375 | 0 | cf_hc_may_h3(cf, data)) { |
376 | 0 | return ALPN_h3; |
377 | 0 | } |
378 | 6 | if(allow_h1_or_h2 && (choices & CURL_HTTP_V2x)) { |
379 | 6 | return ALPN_h2; |
380 | 6 | } |
381 | 0 | if(allow_h1_or_h2 && (choices & CURL_HTTP_V1x)) { |
382 | 0 | return ALPN_h1; |
383 | 0 | } |
384 | 0 | return ALPN_none; |
385 | 0 | } |
386 | | |
387 | | static CURLcode cf_hc_set_baller1(struct Curl_cfilter *cf, |
388 | | struct Curl_easy *data) |
389 | 6 | { |
390 | 6 | struct cf_hc_ctx *ctx = cf->ctx; |
391 | 6 | enum alpnid alpn1 = ALPN_none; |
392 | 6 | VERBOSE(const char *source = "HTTPS-RR"); |
393 | | |
394 | 6 | DEBUGASSERT(CF_CONN(cf)->bits.tls_enable_alpn); |
395 | | |
396 | 6 | alpn1 = cf_hc_get_httpsrr_alpn(cf, data, ALPN_none); |
397 | 6 | if(alpn1 == ALPN_none) { |
398 | | /* preference is configured and allowed, can we use it? */ |
399 | 6 | VERBOSE(source = "preferred version"); |
400 | 6 | alpn1 = cf_hc_get_pref_alpn(cf, data, ALPN_none); |
401 | 6 | } |
402 | 6 | if(alpn1 == ALPN_none) { |
403 | 6 | VERBOSE(source = "wanted versions"); |
404 | 6 | alpn1 = cf_hc_get_first_alpn(cf, data, |
405 | 6 | data->state.http_neg.wanted, |
406 | 6 | ALPN_none); |
407 | 6 | } |
408 | 6 | if(alpn1 == ALPN_none) { |
409 | 0 | VERBOSE(source = "allowed versions"); |
410 | 0 | alpn1 = cf_hc_get_first_alpn(cf, data, |
411 | 0 | data->state.http_neg.allowed, |
412 | 0 | ALPN_none); |
413 | 0 | } |
414 | | |
415 | 6 | if(alpn1 == ALPN_none) { |
416 | | /* None of the wanted/allowed HTTP versions could be chosen */ |
417 | 0 | if(ctx->check_h3_result) { |
418 | 0 | CURL_TRC_CF(data, cf, "unable to use HTTP/3"); |
419 | 0 | return ctx->check_h3_result; |
420 | 0 | } |
421 | 0 | CURL_TRC_CF(data, cf, "unable to select HTTP version"); |
422 | 0 | return CURLE_FAILED_INIT; |
423 | 0 | } |
424 | | |
425 | 6 | cf_hc_baller_assign(&ctx->ballers[0], cf, alpn1, ctx->def_transport); |
426 | 6 | ctx->baller_count = 1; |
427 | 6 | CURL_TRC_CF(data, cf, "1st attempt uses %s from %s", |
428 | 6 | ctx->ballers[0].name, source); |
429 | | |
430 | 6 | switch(alpn1) { |
431 | 0 | case ALPN_h1: |
432 | | /* We really want h1, switch off h2 to make it disappear in ALPN */ |
433 | 0 | data->state.http_neg.wanted &= (uint8_t)~CURL_HTTP_V2x; |
434 | 0 | break; |
435 | 6 | default: |
436 | 6 | break; |
437 | 6 | } |
438 | | |
439 | 6 | return CURLE_OK; |
440 | 6 | } |
441 | | |
442 | | static void cf_hc_set_baller2(struct Curl_cfilter *cf, |
443 | | struct Curl_easy *data) |
444 | 6 | { |
445 | 6 | struct cf_hc_ctx *ctx = cf->ctx; |
446 | 6 | enum alpnid alpn2 = ALPN_none, alpn1 = ctx->ballers[0].alpn_id; |
447 | 6 | VERBOSE(const char *source = "HTTPS-RR"); |
448 | | |
449 | 6 | if(ctx->ballers_complete) |
450 | 0 | return; /* already done */ |
451 | | /* HTTPS-RR can select h1 and h2 as separate attempts. Only finish |
452 | | * selection early when no other allowed ALPN can be used. */ |
453 | 6 | if(!cf_hc_may_choose_alpn(cf, data, ALPN_h1, alpn1) && |
454 | 6 | !cf_hc_may_choose_alpn(cf, data, ALPN_h2, alpn1) && |
455 | 6 | !cf_hc_may_choose_alpn(cf, data, ALPN_h3, alpn1)) { |
456 | 6 | ctx->ballers_complete = TRUE; |
457 | 6 | return; |
458 | 6 | } |
459 | 0 | if(!ctx->httpsrr_resolved) |
460 | 0 | return; /* HTTPS-RR pending */ |
461 | | |
462 | 0 | alpn2 = cf_hc_get_httpsrr_alpn(cf, data, alpn1); |
463 | 0 | if(alpn2 == ALPN_none) { |
464 | | /* preference is configured and allowed, can we use it? */ |
465 | 0 | VERBOSE(source = "preferred version"); |
466 | 0 | alpn2 = cf_hc_get_pref_alpn(cf, data, alpn1); |
467 | 0 | } |
468 | 0 | if(alpn2 == ALPN_none) { |
469 | 0 | VERBOSE(source = "wanted versions"); |
470 | 0 | alpn2 = cf_hc_get_first_alpn(cf, data, |
471 | 0 | data->state.http_neg.wanted, |
472 | 0 | alpn1); |
473 | 0 | } |
474 | |
|
475 | 0 | if(alpn2 != ALPN_none) { |
476 | 0 | cf_hc_baller_assign(&ctx->ballers[1], cf, alpn2, ctx->def_transport); |
477 | 0 | ctx->baller_count = 2; |
478 | 0 | CURL_TRC_CF(data, cf, "2nd attempt uses %s from %s", |
479 | 0 | ctx->ballers[1].name, source); |
480 | 0 | } |
481 | 0 | ctx->ballers_complete = TRUE; |
482 | 0 | } |
483 | | |
484 | | static CURLcode cf_hc_connect(struct Curl_cfilter *cf, |
485 | | struct Curl_easy *data, |
486 | | bool *done) |
487 | 6 | { |
488 | 6 | struct cf_hc_ctx *ctx = cf->ctx; |
489 | 6 | CURLcode result = CURLE_OK; |
490 | 6 | const struct curltime *pnow = NULL; |
491 | | |
492 | 6 | if(cf->connected) { |
493 | 0 | *done = TRUE; |
494 | 0 | return CURLE_OK; |
495 | 0 | } |
496 | | |
497 | 6 | *done = FALSE; |
498 | | |
499 | 6 | if(!ctx->httpsrr_resolved) { |
500 | 6 | ctx->httpsrr_resolved = |
501 | 6 | Curl_dns_cf_resolved_https(data, cf, ctx->destination); |
502 | 6 | #ifdef DEBUGBUILD |
503 | 6 | if(!ctx->httpsrr_resolved && getenv("CURL_DBG_AWAIT_HTTPSRR")) { |
504 | 0 | CURL_TRC_CF(data, cf, "awaiting HTTPS-RR"); |
505 | 0 | return CURLE_OK; |
506 | 0 | } |
507 | 6 | #endif |
508 | 6 | } |
509 | | |
510 | 6 | switch(ctx->state) { |
511 | 6 | case CF_HC_RESOLV: |
512 | 6 | ctx->state = CF_HC_INIT; |
513 | 6 | FALLTHROUGH(); |
514 | | |
515 | 6 | case CF_HC_INIT: |
516 | 6 | DEBUGASSERT(!cf->next); |
517 | 6 | CURL_TRC_CF(data, cf, "connect, init"); |
518 | 6 | result = cf_hc_set_baller1(cf, data); |
519 | 6 | if(result) { |
520 | 0 | ctx->result = result; |
521 | 0 | ctx->state = CF_HC_FAILURE; |
522 | 0 | goto out; |
523 | 0 | } |
524 | 6 | cf_hc_set_baller2(cf, data); |
525 | 6 | pnow = Curl_pgrs_now(data); |
526 | 6 | ctx->started = *pnow; |
527 | 6 | cf_hc_baller_init(&ctx->ballers[0], data); |
528 | 6 | if((ctx->baller_count > 1) || !ctx->ballers_complete) { |
529 | 0 | Curl_expire_set(data, EXPIRE_ALPN_EYEBALLS, |
530 | 0 | ctx->soft_eyeballs_timeout_ms, pnow); |
531 | 0 | } |
532 | 6 | ctx->state = CF_HC_CONNECT; |
533 | 6 | FALLTHROUGH(); |
534 | | |
535 | 6 | case CF_HC_CONNECT: |
536 | 6 | if(!ctx->ballers_complete) |
537 | 0 | cf_hc_set_baller2(cf, data); |
538 | | |
539 | 6 | if(cf_hc_baller_is_connecting(&ctx->ballers[0])) { |
540 | 6 | result = cf_hc_baller_connect(&ctx->ballers[0], data, cf, done); |
541 | 6 | if(!result && *done) { |
542 | 0 | result = baller_connected(cf, data, &ctx->ballers[0]); |
543 | 0 | goto out; |
544 | 0 | } |
545 | 6 | } |
546 | | |
547 | 6 | pnow = Curl_pgrs_now(data); |
548 | 6 | if(time_to_start_baller2(cf, data, pnow)) { |
549 | 0 | cf_hc_baller_init(&ctx->ballers[1], data); |
550 | 0 | } |
551 | | |
552 | 6 | if(cf_hc_baller_is_connecting(&ctx->ballers[1])) { |
553 | 0 | result = cf_hc_baller_connect(&ctx->ballers[1], data, cf, done); |
554 | 0 | if(!result && *done) { |
555 | 0 | result = baller_connected(cf, data, &ctx->ballers[1]); |
556 | 0 | goto out; |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | 6 | if(ctx->ballers[0].result && |
561 | 6 | (ctx->ballers[1].result || |
562 | 6 | (ctx->ballers_complete && (ctx->baller_count < 2)))) { |
563 | | /* all have failed. we give up */ |
564 | 6 | CURL_TRC_CF(data, cf, "connect, all attempts failed"); |
565 | 6 | ctx->result = result = ctx->ballers[0].result; |
566 | 6 | ctx->state = CF_HC_FAILURE; |
567 | 6 | goto out; |
568 | 6 | } |
569 | 0 | result = CURLE_OK; |
570 | 0 | *done = FALSE; |
571 | 0 | break; |
572 | | |
573 | 0 | case CF_HC_FAILURE: |
574 | 0 | result = ctx->result; |
575 | 0 | cf->connected = FALSE; |
576 | 0 | *done = FALSE; |
577 | 0 | break; |
578 | | |
579 | 0 | case CF_HC_SUCCESS: |
580 | 0 | result = CURLE_OK; |
581 | 0 | cf->connected = TRUE; |
582 | 0 | *done = TRUE; |
583 | 0 | break; |
584 | 6 | } |
585 | | |
586 | 6 | out: |
587 | 6 | CURL_TRC_CF(data, cf, "connect -> %d, done=%d", (int)result, *done); |
588 | 6 | return result; |
589 | 6 | } |
590 | | |
591 | | static CURLcode cf_hc_shutdown(struct Curl_cfilter *cf, |
592 | | struct Curl_easy *data, bool *done) |
593 | 0 | { |
594 | 0 | struct cf_hc_ctx *ctx = cf->ctx; |
595 | 0 | size_t i; |
596 | 0 | CURLcode result = CURLE_OK; |
597 | |
|
598 | 0 | DEBUGASSERT(data); |
599 | 0 | if(cf->connected) { |
600 | 0 | *done = TRUE; |
601 | 0 | return CURLE_OK; |
602 | 0 | } |
603 | | |
604 | | /* shutdown all ballers that have not done so already. If one fails, |
605 | | * continue shutting down others until all are shutdown. */ |
606 | 0 | for(i = 0; i < ctx->baller_count; i++) { |
607 | 0 | struct cf_hc_baller *b = &ctx->ballers[i]; |
608 | 0 | bool bdone = FALSE; |
609 | 0 | if(!cf_hc_baller_is_connecting(b) || b->shutdown) |
610 | 0 | continue; |
611 | 0 | b->result = b->chain.cf->cft->do_shutdown(b->chain.cf, data, &bdone); |
612 | 0 | if(b->result || bdone) |
613 | 0 | b->shutdown = TRUE; /* treat a failed shutdown as done */ |
614 | 0 | } |
615 | |
|
616 | 0 | *done = TRUE; |
617 | 0 | for(i = 0; i < ctx->baller_count; i++) { |
618 | 0 | if(!ctx->ballers[i].shutdown) |
619 | 0 | *done = FALSE; |
620 | 0 | } |
621 | 0 | if(*done) { |
622 | 0 | for(i = 0; i < ctx->baller_count; i++) { |
623 | 0 | if(ctx->ballers[i].result) |
624 | 0 | result = ctx->ballers[i].result; |
625 | 0 | } |
626 | 0 | } |
627 | 0 | CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", (int)result, *done); |
628 | 0 | return result; |
629 | 0 | } |
630 | | |
631 | | static CURLcode cf_hc_adjust_pollset(struct Curl_cfilter *cf, |
632 | | struct Curl_easy *data, |
633 | | struct easy_pollset *ps) |
634 | 0 | { |
635 | 0 | CURLcode result = CURLE_OK; |
636 | 0 | if(!cf->connected) { |
637 | 0 | struct cf_hc_ctx *ctx = cf->ctx; |
638 | 0 | size_t i; |
639 | |
|
640 | 0 | for(i = 0; (i < ctx->baller_count) && !result; i++) { |
641 | 0 | struct cf_hc_baller *b = &ctx->ballers[i]; |
642 | 0 | if(!cf_hc_baller_is_connecting(b)) |
643 | 0 | continue; |
644 | 0 | result = Curl_conn_cf_adjust_pollset(b->chain.cf, data, ps); |
645 | 0 | } |
646 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %u socks", (int)result, |
647 | 0 | ps->n); |
648 | 0 | } |
649 | 0 | return result; |
650 | 0 | } |
651 | | |
652 | | static bool cf_hc_data_pending(struct Curl_cfilter *cf, |
653 | | const struct Curl_easy *data) |
654 | 0 | { |
655 | 0 | struct cf_hc_ctx *ctx = cf->ctx; |
656 | 0 | size_t i; |
657 | |
|
658 | 0 | if(cf->connected) |
659 | 0 | return cf->next->cft->has_data_pending(cf->next, data); |
660 | | |
661 | 0 | for(i = 0; i < ctx->baller_count; i++) |
662 | 0 | if(cf_hc_baller_data_pending(&ctx->ballers[i], data)) |
663 | 0 | return TRUE; |
664 | 0 | return FALSE; |
665 | 0 | } |
666 | | |
667 | | static CURLcode cf_hc_query(struct Curl_cfilter *cf, |
668 | | struct Curl_easy *data, |
669 | | int query, int *pres1, void *pres2) |
670 | 7 | { |
671 | 7 | struct cf_hc_ctx *ctx = cf->ctx; |
672 | 7 | size_t i; |
673 | | |
674 | 7 | if(!cf->connected) { |
675 | 7 | switch(query) { |
676 | 7 | case CF_QUERY_NEED_FLUSH: { |
677 | 7 | for(i = 0; i < ctx->baller_count; i++) |
678 | 0 | if(cf_hc_baller_needs_flush(&ctx->ballers[i], data)) { |
679 | 0 | *pres1 = TRUE; |
680 | 0 | return CURLE_OK; |
681 | 0 | } |
682 | 7 | break; |
683 | 7 | } |
684 | 7 | default: |
685 | 0 | break; |
686 | 7 | } |
687 | 7 | } |
688 | 7 | return cf->next ? |
689 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
690 | 7 | CURLE_UNKNOWN_OPTION; |
691 | 7 | } |
692 | | |
693 | | static CURLcode cf_hc_cntrl(struct Curl_cfilter *cf, |
694 | | struct Curl_easy *data, |
695 | | int event, int arg1, void *arg2) |
696 | 21 | { |
697 | 21 | struct cf_hc_ctx *ctx = cf->ctx; |
698 | 21 | CURLcode result = CURLE_OK; |
699 | 21 | size_t i; |
700 | | |
701 | 21 | if(!cf->connected) { |
702 | 21 | switch(event) { |
703 | 7 | case CF_CTRL_REPORT_STATS: |
704 | 14 | case CF_CTRL_CONN_INFO_UPDATE: |
705 | 26 | for(i = 0; i < ctx->baller_count; i++) { |
706 | | /* Make the first baller that connected at network level report */ |
707 | 12 | if(Curl_conn_cf_is_ip_connected(ctx->ballers[i].chain.cf, data)) { |
708 | 0 | Curl_conn_cf_cntrl(ctx->ballers[i].chain.cf, data, TRUE, |
709 | 0 | event, arg1, arg2); |
710 | 0 | break; |
711 | 0 | } |
712 | 12 | } |
713 | 14 | break; |
714 | 7 | default: |
715 | 13 | for(i = 0; i < ctx->baller_count; i++) { |
716 | 6 | result = cf_hc_baller_cntrl(&ctx->ballers[i], data, event, arg1, arg2); |
717 | 6 | if(result && (result != CURLE_AGAIN)) |
718 | 0 | goto out; |
719 | 6 | } |
720 | 7 | result = CURLE_OK; |
721 | 7 | break; |
722 | 21 | } |
723 | 21 | } |
724 | 21 | out: |
725 | 21 | return result; |
726 | 21 | } |
727 | | |
728 | | static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
729 | 8 | { |
730 | 8 | struct cf_hc_ctx *ctx = cf->ctx; |
731 | | |
732 | 8 | CURL_TRC_CF(data, cf, "destroy"); |
733 | 8 | cf_hc_ctx_destroy(data, ctx); |
734 | 8 | } |
735 | | |
736 | | struct Curl_cftype Curl_cft_http_connect = { |
737 | | "HTTPS-CONNECT", |
738 | | CF_TYPE_SETUP, |
739 | | CURL_LOG_LVL_NONE, |
740 | | cf_hc_destroy, |
741 | | cf_hc_connect, |
742 | | cf_hc_shutdown, |
743 | | cf_hc_adjust_pollset, |
744 | | cf_hc_data_pending, |
745 | | Curl_cf_def_send, |
746 | | Curl_cf_def_recv, |
747 | | cf_hc_cntrl, |
748 | | Curl_cf_def_conn_is_alive, |
749 | | Curl_cf_def_conn_keep_alive, |
750 | | cf_hc_query, |
751 | | }; |
752 | | |
753 | | static CURLcode cf_hc_create(struct Curl_cfilter **pcf, |
754 | | struct Curl_easy *data, |
755 | | struct Curl_peer *destination, |
756 | | uint8_t def_transport) |
757 | 9 | { |
758 | 9 | struct Curl_cfilter *cf = NULL; |
759 | 9 | struct cf_hc_ctx *ctx; |
760 | 9 | CURLcode result = CURLE_OK; |
761 | | |
762 | 9 | ctx = curlx_calloc(1, sizeof(*ctx)); |
763 | 9 | if(!ctx) { |
764 | 0 | result = CURLE_OUT_OF_MEMORY; |
765 | 0 | goto out; |
766 | 0 | } |
767 | 9 | Curl_peer_link(&ctx->destination, destination); |
768 | 9 | ctx->def_transport = def_transport; |
769 | 9 | ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout; |
770 | 9 | ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 2; |
771 | | |
772 | 9 | result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx); |
773 | 9 | if(result) |
774 | 1 | goto out; |
775 | 8 | ctx = NULL; |
776 | | |
777 | 9 | out: |
778 | 9 | *pcf = result ? NULL : cf; |
779 | 9 | cf_hc_ctx_destroy(data, ctx); |
780 | 9 | return result; |
781 | 8 | } |
782 | | |
783 | | static CURLcode cf_hc_add(struct Curl_easy *data, |
784 | | struct Curl_peer *destination, |
785 | | struct Curl_cfchain *chain, |
786 | | uint8_t def_transport) |
787 | 9 | { |
788 | 9 | struct Curl_cfilter *cf; |
789 | 9 | CURLcode result = CURLE_OK; |
790 | | |
791 | 9 | DEBUGASSERT(data); |
792 | 9 | result = cf_hc_create(&cf, data, destination, def_transport); |
793 | 9 | if(result) |
794 | 1 | goto out; |
795 | 8 | Curl_cfchain_add(chain, cf); |
796 | | |
797 | 8 | #ifdef USE_HTTPSRR |
798 | 8 | result = Curl_dns_cf_add_https_resolve(data, cf, destination); |
799 | 8 | #endif |
800 | 9 | out: |
801 | 9 | return result; |
802 | 8 | } |
803 | | |
804 | | CURLcode Curl_cf_https_setup(struct Curl_easy *data, |
805 | | struct Curl_peer *destination, |
806 | | struct Curl_cfchain *chain) |
807 | 9 | { |
808 | 9 | struct connectdata *conn = chain->conn; |
809 | 9 | CURLcode result = CURLE_OK; |
810 | | |
811 | 9 | DEBUGASSERT(conn->scheme->protocol == CURLPROTO_HTTPS); |
812 | | |
813 | | /* This filter is intended for HTTPS using ALPN and does |
814 | | * not support HTTPS Eyeballing to a proxy. */ |
815 | 9 | if((conn->scheme->protocol != CURLPROTO_HTTPS) || |
816 | 9 | #ifndef CURL_DISABLE_PROXY |
817 | 9 | conn->bits.origin_is_proxy || |
818 | 9 | #endif |
819 | 9 | !conn->bits.tls_enable_alpn) |
820 | 0 | goto out; |
821 | | |
822 | 9 | result = cf_hc_add(data, destination, chain, |
823 | 9 | conn->transport_wanted); |
824 | | |
825 | 9 | out: |
826 | 9 | return result; |
827 | 9 | } |
828 | | |
829 | | #endif /* !CURL_DISABLE_HTTP */ |