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