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