/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 "connect.h" |
32 | | #include "hostip.h" |
33 | | #include "httpsrr.h" |
34 | | #include "multiif.h" |
35 | | #include "cf-https-connect.h" |
36 | | #include "http2.h" |
37 | | #include "progress.h" |
38 | | #include "select.h" |
39 | | #include "vquic/vquic.h" |
40 | | |
41 | | typedef enum { |
42 | | CF_HC_INIT, |
43 | | CF_HC_CONNECT, |
44 | | CF_HC_SUCCESS, |
45 | | CF_HC_FAILURE |
46 | | } cf_hc_state; |
47 | | |
48 | | struct cf_hc_baller { |
49 | | const char *name; |
50 | | struct Curl_cfilter *cf; |
51 | | CURLcode result; |
52 | | struct curltime started; |
53 | | int reply_ms; |
54 | | uint8_t transport; |
55 | | enum alpnid alpn_id; |
56 | | BIT(shutdown); |
57 | | }; |
58 | | |
59 | | static void cf_hc_baller_reset(struct cf_hc_baller *b, |
60 | | struct Curl_easy *data) |
61 | 16.8k | { |
62 | 16.8k | if(b->cf) { |
63 | 5.60k | Curl_conn_cf_close(b->cf, data); |
64 | 5.60k | Curl_conn_cf_discard_chain(&b->cf, data); |
65 | 5.60k | b->cf = NULL; |
66 | 5.60k | } |
67 | 16.8k | b->result = CURLE_OK; |
68 | 16.8k | b->reply_ms = -1; |
69 | 16.8k | } |
70 | | |
71 | | static bool cf_hc_baller_is_active(struct cf_hc_baller *b) |
72 | 4.13M | { |
73 | 4.13M | return b->cf && !b->result; |
74 | 4.13M | } |
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 | 2.06M | { |
99 | 2.06M | return b->cf && !b->result && Curl_conn_cf_needs_flush(b->cf, data); |
100 | 2.06M | } |
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 | 5.60k | { |
106 | 5.60k | if(b->cf && !b->result) |
107 | 434 | return Curl_conn_cf_cntrl(b->cf, data, FALSE, event, arg1, arg2); |
108 | 5.17k | return CURLE_OK; |
109 | 5.60k | } |
110 | | |
111 | | struct cf_hc_ctx { |
112 | | cf_hc_state state; |
113 | | struct curltime started; /* when connect started */ |
114 | | CURLcode result; /* overall 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 | | }; |
120 | | |
121 | | static void cf_hc_baller_assign(struct cf_hc_baller *b, |
122 | | enum alpnid alpn_id, |
123 | | uint8_t def_transport) |
124 | 5.60k | { |
125 | 5.60k | b->alpn_id = alpn_id; |
126 | 5.60k | b->transport = def_transport; |
127 | 5.60k | switch(b->alpn_id) { |
128 | 0 | case ALPN_h3: |
129 | 0 | b->name = "h3"; |
130 | 0 | b->transport = TRNSPRT_QUIC; |
131 | 0 | break; |
132 | 5.60k | case ALPN_h2: |
133 | 5.60k | b->name = "h2"; |
134 | 5.60k | break; |
135 | 4 | case ALPN_h1: |
136 | 4 | b->name = "h1"; |
137 | 4 | break; |
138 | 0 | default: |
139 | 0 | b->result = CURLE_FAILED_INIT; |
140 | 0 | break; |
141 | 5.60k | } |
142 | 5.60k | } |
143 | | |
144 | | static void cf_hc_baller_init(struct cf_hc_baller *b, |
145 | | struct Curl_cfilter *cf, |
146 | | struct Curl_easy *data, |
147 | | uint8_t transport) |
148 | 5.60k | { |
149 | 5.60k | struct Curl_cfilter *save = cf->next; |
150 | | |
151 | 5.60k | cf->next = NULL; |
152 | 5.60k | b->started = *Curl_pgrs_now(data); |
153 | 5.60k | switch(b->alpn_id) { |
154 | 0 | case ALPN_h3: |
155 | 0 | transport = TRNSPRT_QUIC; |
156 | 0 | break; |
157 | 5.60k | default: |
158 | 5.60k | break; |
159 | 5.60k | } |
160 | | |
161 | 5.60k | if(!b->result) |
162 | 5.60k | b->result = Curl_cf_setup_insert_after(cf, data, transport, |
163 | 5.60k | CURL_CF_SSL_ENABLE); |
164 | 5.60k | b->cf = cf->next; |
165 | 5.60k | cf->next = save; |
166 | 5.60k | } |
167 | | |
168 | | static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b, |
169 | | struct Curl_cfilter *cf, |
170 | | struct Curl_easy *data, |
171 | | bool *done) |
172 | 2.06M | { |
173 | 2.06M | struct Curl_cfilter *save = cf->next; |
174 | | |
175 | 2.06M | cf->next = b->cf; |
176 | 2.06M | b->result = Curl_conn_cf_connect(cf->next, data, done); |
177 | 2.06M | b->cf = cf->next; /* it might mutate */ |
178 | 2.06M | cf->next = save; |
179 | 2.06M | return b->result; |
180 | 2.06M | } |
181 | | |
182 | | static void cf_hc_reset(struct Curl_cfilter *cf, struct Curl_easy *data) |
183 | 16.8k | { |
184 | 16.8k | struct cf_hc_ctx *ctx = cf->ctx; |
185 | 16.8k | size_t i; |
186 | | |
187 | 16.8k | if(ctx) { |
188 | 33.6k | for(i = 0; i < ctx->baller_count; ++i) |
189 | 16.8k | cf_hc_baller_reset(&ctx->ballers[i], data); |
190 | 16.8k | ctx->state = CF_HC_INIT; |
191 | 16.8k | ctx->result = CURLE_OK; |
192 | 16.8k | ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout; |
193 | 16.8k | ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 4; |
194 | 16.8k | } |
195 | 16.8k | } |
196 | | |
197 | | static CURLcode baller_connected(struct Curl_cfilter *cf, |
198 | | struct Curl_easy *data, |
199 | | struct cf_hc_baller *winner) |
200 | 0 | { |
201 | 0 | struct cf_hc_ctx *ctx = cf->ctx; |
202 | 0 | CURLcode result = CURLE_OK; |
203 | 0 | int reply_ms; |
204 | 0 | size_t i; |
205 | |
|
206 | 0 | DEBUGASSERT(winner->cf); |
207 | 0 | for(i = 0; i < ctx->baller_count; ++i) |
208 | 0 | if(winner != &ctx->ballers[i]) |
209 | 0 | cf_hc_baller_reset(&ctx->ballers[i], data); |
210 | |
|
211 | 0 | reply_ms = cf_hc_baller_reply_ms(winner, data); |
212 | 0 | if(reply_ms >= 0) |
213 | 0 | CURL_TRC_CF(data, cf, "connect+handshake %s: %dms, 1st data: %dms", |
214 | 0 | winner->name, |
215 | 0 | (int)curlx_ptimediff_ms(Curl_pgrs_now(data), |
216 | 0 | &winner->started), reply_ms); |
217 | 0 | else |
218 | 0 | CURL_TRC_CF(data, cf, "deferred handshake %s: %dms", |
219 | 0 | winner->name, (int)curlx_ptimediff_ms(Curl_pgrs_now(data), |
220 | 0 | &winner->started)); |
221 | | |
222 | | /* install the winning filter below this one. */ |
223 | 0 | cf->next = winner->cf; |
224 | 0 | winner->cf = NULL; |
225 | | /* whatever errors where reported by ballers, clear our errorbuf */ |
226 | 0 | Curl_reset_fail(data); |
227 | |
|
228 | 0 | #ifdef USE_NGHTTP2 |
229 | 0 | { |
230 | | /* Using nghttp2, we add the filter "below" us, so when the conn |
231 | | * closes, we tear it down for a fresh reconnect */ |
232 | 0 | const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data); |
233 | 0 | if(alpn && !strcmp("h2", alpn)) { |
234 | 0 | result = Curl_http2_switch_at(cf, data); |
235 | 0 | if(result) { |
236 | 0 | ctx->state = CF_HC_FAILURE; |
237 | 0 | ctx->result = result; |
238 | 0 | return result; |
239 | 0 | } |
240 | 0 | } |
241 | 0 | } |
242 | 0 | #endif |
243 | | |
244 | 0 | ctx->state = CF_HC_SUCCESS; |
245 | 0 | cf->connected = TRUE; |
246 | 0 | return result; |
247 | 0 | } |
248 | | |
249 | | static bool time_to_start_next(struct Curl_cfilter *cf, |
250 | | struct Curl_easy *data, |
251 | | size_t idx, struct curltime now) |
252 | 2.06M | { |
253 | 2.06M | struct cf_hc_ctx *ctx = cf->ctx; |
254 | 2.06M | timediff_t elapsed_ms; |
255 | 2.06M | size_t i; |
256 | | |
257 | 2.06M | if(idx >= ctx->baller_count) |
258 | 2.06M | return FALSE; |
259 | 0 | if(cf_hc_baller_has_started(&ctx->ballers[idx])) |
260 | 0 | return FALSE; |
261 | 0 | for(i = 0; i < idx; i++) { |
262 | 0 | if(!ctx->ballers[i].result) |
263 | 0 | break; |
264 | 0 | } |
265 | 0 | if(i == idx) { |
266 | 0 | CURL_TRC_CF(data, cf, "all previous attempts failed, starting %s", |
267 | 0 | ctx->ballers[idx].name); |
268 | 0 | return TRUE; |
269 | 0 | } |
270 | 0 | elapsed_ms = curlx_ptimediff_ms(&now, &ctx->started); |
271 | 0 | if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) { |
272 | 0 | CURL_TRC_CF(data, cf, "hard timeout of %" FMT_TIMEDIFF_T "ms reached, " |
273 | 0 | "starting %s", |
274 | 0 | ctx->hard_eyeballs_timeout_ms, ctx->ballers[idx].name); |
275 | 0 | return TRUE; |
276 | 0 | } |
277 | | |
278 | 0 | if((idx > 0) && (elapsed_ms >= ctx->soft_eyeballs_timeout_ms)) { |
279 | 0 | if(cf_hc_baller_reply_ms(&ctx->ballers[idx - 1], data) < 0) { |
280 | 0 | CURL_TRC_CF(data, cf, "soft timeout of %" FMT_TIMEDIFF_T "ms reached, " |
281 | 0 | "%s has not seen any data, starting %s", |
282 | 0 | ctx->soft_eyeballs_timeout_ms, |
283 | 0 | ctx->ballers[idx - 1].name, ctx->ballers[idx].name); |
284 | 0 | return TRUE; |
285 | 0 | } |
286 | | /* set the effective hard timeout again */ |
287 | 0 | Curl_expire(data, ctx->hard_eyeballs_timeout_ms - elapsed_ms, |
288 | 0 | EXPIRE_ALPN_EYEBALLS); |
289 | 0 | } |
290 | 0 | return FALSE; |
291 | 0 | } |
292 | | |
293 | | static CURLcode cf_hc_connect(struct Curl_cfilter *cf, |
294 | | struct Curl_easy *data, |
295 | | bool *done) |
296 | 2.06M | { |
297 | 2.06M | struct cf_hc_ctx *ctx = cf->ctx; |
298 | 2.06M | CURLcode result = CURLE_OK; |
299 | 2.06M | size_t i, failed_ballers; |
300 | | |
301 | 2.06M | if(cf->connected) { |
302 | 0 | *done = TRUE; |
303 | 0 | return CURLE_OK; |
304 | 0 | } |
305 | | |
306 | 2.06M | *done = FALSE; |
307 | 2.06M | switch(ctx->state) { |
308 | 5.60k | case CF_HC_INIT: |
309 | 5.60k | DEBUGASSERT(!cf->next); |
310 | 11.2k | for(i = 0; i < ctx->baller_count; i++) |
311 | 5.60k | DEBUGASSERT(!ctx->ballers[i].cf); |
312 | 5.60k | CURL_TRC_CF(data, cf, "connect, init"); |
313 | 5.60k | ctx->started = *Curl_pgrs_now(data); |
314 | 5.60k | cf_hc_baller_init(&ctx->ballers[0], cf, data, ctx->ballers[0].transport); |
315 | 5.60k | if(ctx->baller_count > 1) { |
316 | 0 | Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS); |
317 | 0 | CURL_TRC_CF(data, cf, "set next attempt to start in %" FMT_TIMEDIFF_T |
318 | 0 | "ms", ctx->soft_eyeballs_timeout_ms); |
319 | 0 | } |
320 | 5.60k | ctx->state = CF_HC_CONNECT; |
321 | 5.60k | FALLTHROUGH(); |
322 | | |
323 | 2.06M | case CF_HC_CONNECT: |
324 | 2.06M | if(cf_hc_baller_is_active(&ctx->ballers[0])) { |
325 | 2.06M | result = cf_hc_baller_connect(&ctx->ballers[0], cf, data, done); |
326 | 2.06M | if(!result && *done) { |
327 | 0 | result = baller_connected(cf, data, &ctx->ballers[0]); |
328 | 0 | goto out; |
329 | 0 | } |
330 | 2.06M | } |
331 | | |
332 | 2.06M | if(time_to_start_next(cf, data, 1, *Curl_pgrs_now(data))) { |
333 | 0 | cf_hc_baller_init(&ctx->ballers[1], cf, data, ctx->ballers[1].transport); |
334 | 0 | } |
335 | | |
336 | 2.06M | if((ctx->baller_count > 1) && cf_hc_baller_is_active(&ctx->ballers[1])) { |
337 | 0 | CURL_TRC_CF(data, cf, "connect, check %s", ctx->ballers[1].name); |
338 | 0 | result = cf_hc_baller_connect(&ctx->ballers[1], cf, data, done); |
339 | 0 | if(!result && *done) { |
340 | 0 | result = baller_connected(cf, data, &ctx->ballers[1]); |
341 | 0 | goto out; |
342 | 0 | } |
343 | 0 | } |
344 | | |
345 | 2.06M | failed_ballers = 0; |
346 | 4.13M | for(i = 0; i < ctx->baller_count; i++) { |
347 | 2.06M | if(ctx->ballers[i].result) |
348 | 5.17k | ++failed_ballers; |
349 | 2.06M | } |
350 | | |
351 | 2.06M | if(failed_ballers == ctx->baller_count) { |
352 | | /* all have failed. we give up */ |
353 | 5.17k | CURL_TRC_CF(data, cf, "connect, all attempts failed"); |
354 | 5.17k | for(i = 0; i < ctx->baller_count; i++) { |
355 | 5.17k | if(ctx->ballers[i].result) { |
356 | 5.17k | result = ctx->ballers[i].result; |
357 | 5.17k | break; |
358 | 5.17k | } |
359 | 5.17k | } |
360 | 5.17k | ctx->state = CF_HC_FAILURE; |
361 | 5.17k | goto out; |
362 | 5.17k | } |
363 | 2.06M | result = CURLE_OK; |
364 | 2.06M | *done = FALSE; |
365 | 2.06M | break; |
366 | | |
367 | 0 | case CF_HC_FAILURE: |
368 | 0 | result = ctx->result; |
369 | 0 | cf->connected = FALSE; |
370 | 0 | *done = FALSE; |
371 | 0 | break; |
372 | | |
373 | 0 | case CF_HC_SUCCESS: |
374 | 0 | result = CURLE_OK; |
375 | 0 | cf->connected = TRUE; |
376 | 0 | *done = TRUE; |
377 | 0 | break; |
378 | 2.06M | } |
379 | | |
380 | 2.06M | out: |
381 | 2.06M | CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done); |
382 | 2.06M | return result; |
383 | 2.06M | } |
384 | | |
385 | | static CURLcode cf_hc_shutdown(struct Curl_cfilter *cf, |
386 | | struct Curl_easy *data, bool *done) |
387 | 0 | { |
388 | 0 | struct cf_hc_ctx *ctx = cf->ctx; |
389 | 0 | size_t i; |
390 | 0 | CURLcode result = CURLE_OK; |
391 | |
|
392 | 0 | DEBUGASSERT(data); |
393 | 0 | if(cf->connected) { |
394 | 0 | *done = TRUE; |
395 | 0 | return CURLE_OK; |
396 | 0 | } |
397 | | |
398 | | /* shutdown all ballers that have not done so already. If one fails, |
399 | | * continue shutting down others until all are shutdown. */ |
400 | 0 | for(i = 0; i < ctx->baller_count; i++) { |
401 | 0 | struct cf_hc_baller *b = &ctx->ballers[i]; |
402 | 0 | bool bdone = FALSE; |
403 | 0 | if(!cf_hc_baller_is_active(b) || b->shutdown) |
404 | 0 | continue; |
405 | 0 | b->result = b->cf->cft->do_shutdown(b->cf, data, &bdone); |
406 | 0 | if(b->result || bdone) |
407 | 0 | b->shutdown = TRUE; /* treat a failed shutdown as done */ |
408 | 0 | } |
409 | |
|
410 | 0 | *done = TRUE; |
411 | 0 | for(i = 0; i < ctx->baller_count; i++) { |
412 | 0 | if(!ctx->ballers[i].shutdown) |
413 | 0 | *done = FALSE; |
414 | 0 | } |
415 | 0 | if(*done) { |
416 | 0 | for(i = 0; i < ctx->baller_count; i++) { |
417 | 0 | if(ctx->ballers[i].result) |
418 | 0 | result = ctx->ballers[i].result; |
419 | 0 | } |
420 | 0 | } |
421 | 0 | CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", result, *done); |
422 | 0 | return result; |
423 | 0 | } |
424 | | |
425 | | static CURLcode cf_hc_adjust_pollset(struct Curl_cfilter *cf, |
426 | | struct Curl_easy *data, |
427 | | struct easy_pollset *ps) |
428 | 2.06M | { |
429 | 2.06M | CURLcode result = CURLE_OK; |
430 | 2.06M | if(!cf->connected) { |
431 | 2.06M | struct cf_hc_ctx *ctx = cf->ctx; |
432 | 2.06M | size_t i; |
433 | | |
434 | 4.12M | for(i = 0; (i < ctx->baller_count) && !result; i++) { |
435 | 2.06M | struct cf_hc_baller *b = &ctx->ballers[i]; |
436 | 2.06M | if(!cf_hc_baller_is_active(b)) |
437 | 0 | continue; |
438 | 2.06M | result = Curl_conn_cf_adjust_pollset(b->cf, data, ps); |
439 | 2.06M | } |
440 | 2.06M | CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %d socks", result, ps->n); |
441 | 2.06M | } |
442 | 2.06M | return result; |
443 | 2.06M | } |
444 | | |
445 | | static bool cf_hc_data_pending(struct Curl_cfilter *cf, |
446 | | const struct Curl_easy *data) |
447 | 0 | { |
448 | 0 | struct cf_hc_ctx *ctx = cf->ctx; |
449 | 0 | size_t i; |
450 | |
|
451 | 0 | if(cf->connected) |
452 | 0 | return cf->next->cft->has_data_pending(cf->next, data); |
453 | | |
454 | 0 | for(i = 0; i < ctx->baller_count; i++) |
455 | 0 | if(cf_hc_baller_data_pending(&ctx->ballers[i], data)) |
456 | 0 | return TRUE; |
457 | 0 | return FALSE; |
458 | 0 | } |
459 | | |
460 | | static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf, |
461 | | struct Curl_easy *data, |
462 | | int query) |
463 | 10.3k | { |
464 | 10.3k | struct cf_hc_ctx *ctx = cf->ctx; |
465 | 10.3k | struct curltime t, tmax; |
466 | 10.3k | size_t i; |
467 | | |
468 | 10.3k | memset(&tmax, 0, sizeof(tmax)); |
469 | 20.6k | for(i = 0; i < ctx->baller_count; i++) { |
470 | 10.3k | struct Curl_cfilter *cfb = ctx->ballers[i].cf; |
471 | 10.3k | memset(&t, 0, sizeof(t)); |
472 | 10.3k | if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) { |
473 | 8.77k | if((t.tv_sec || t.tv_usec) && curlx_ptimediff_us(&t, &tmax) > 0) |
474 | 4.59k | tmax = t; |
475 | 8.77k | } |
476 | 10.3k | } |
477 | 10.3k | return tmax; |
478 | 10.3k | } |
479 | | |
480 | | static CURLcode cf_hc_query(struct Curl_cfilter *cf, |
481 | | struct Curl_easy *data, |
482 | | int query, int *pres1, void *pres2) |
483 | 4.14M | { |
484 | 4.14M | struct cf_hc_ctx *ctx = cf->ctx; |
485 | 4.14M | size_t i; |
486 | | |
487 | 4.14M | if(!cf->connected) { |
488 | 4.14M | switch(query) { |
489 | 5.17k | case CF_QUERY_TIMER_CONNECT: { |
490 | 5.17k | struct curltime *when = pres2; |
491 | 5.17k | *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT); |
492 | 5.17k | return CURLE_OK; |
493 | 0 | } |
494 | 5.17k | case CF_QUERY_TIMER_APPCONNECT: { |
495 | 5.17k | struct curltime *when = pres2; |
496 | 5.17k | *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT); |
497 | 5.17k | return CURLE_OK; |
498 | 0 | } |
499 | 2.06M | case CF_QUERY_NEED_FLUSH: { |
500 | 4.13M | for(i = 0; i < ctx->baller_count; i++) |
501 | 2.06M | if(cf_hc_baller_needs_flush(&ctx->ballers[i], data)) { |
502 | 0 | *pres1 = TRUE; |
503 | 0 | return CURLE_OK; |
504 | 0 | } |
505 | 2.06M | break; |
506 | 2.06M | } |
507 | 2.06M | default: |
508 | 2.06M | break; |
509 | 4.14M | } |
510 | 4.14M | } |
511 | 4.13M | return cf->next ? |
512 | 104 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
513 | 4.13M | CURLE_UNKNOWN_OPTION; |
514 | 4.14M | } |
515 | | |
516 | | static CURLcode cf_hc_cntrl(struct Curl_cfilter *cf, |
517 | | struct Curl_easy *data, |
518 | | int event, int arg1, void *arg2) |
519 | 5.60k | { |
520 | 5.60k | struct cf_hc_ctx *ctx = cf->ctx; |
521 | 5.60k | CURLcode result = CURLE_OK; |
522 | 5.60k | size_t i; |
523 | | |
524 | 5.60k | if(!cf->connected) { |
525 | 11.2k | for(i = 0; i < ctx->baller_count; i++) { |
526 | 5.60k | result = cf_hc_baller_cntrl(&ctx->ballers[i], data, event, arg1, arg2); |
527 | 5.60k | if(result && (result != CURLE_AGAIN)) |
528 | 0 | goto out; |
529 | 5.60k | } |
530 | 5.60k | result = CURLE_OK; |
531 | 5.60k | } |
532 | 5.60k | out: |
533 | 5.60k | return result; |
534 | 5.60k | } |
535 | | |
536 | | static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
537 | 5.60k | { |
538 | 5.60k | CURL_TRC_CF(data, cf, "close"); |
539 | 5.60k | cf_hc_reset(cf, data); |
540 | 5.60k | cf->connected = FALSE; |
541 | | |
542 | 5.60k | if(cf->next) { |
543 | 0 | cf->next->cft->do_close(cf->next, data); |
544 | 0 | Curl_conn_cf_discard_chain(&cf->next, data); |
545 | 0 | } |
546 | 5.60k | } |
547 | | |
548 | | static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
549 | 5.60k | { |
550 | 5.60k | struct cf_hc_ctx *ctx = cf->ctx; |
551 | | |
552 | 5.60k | CURL_TRC_CF(data, cf, "destroy"); |
553 | 5.60k | cf_hc_reset(cf, data); |
554 | 5.60k | Curl_safefree(ctx); |
555 | 5.60k | } |
556 | | |
557 | | struct Curl_cftype Curl_cft_http_connect = { |
558 | | "HTTPS-CONNECT", |
559 | | 0, |
560 | | CURL_LOG_LVL_NONE, |
561 | | cf_hc_destroy, |
562 | | cf_hc_connect, |
563 | | cf_hc_close, |
564 | | cf_hc_shutdown, |
565 | | cf_hc_adjust_pollset, |
566 | | cf_hc_data_pending, |
567 | | Curl_cf_def_send, |
568 | | Curl_cf_def_recv, |
569 | | cf_hc_cntrl, |
570 | | Curl_cf_def_conn_is_alive, |
571 | | Curl_cf_def_conn_keep_alive, |
572 | | cf_hc_query, |
573 | | }; |
574 | | |
575 | | static CURLcode cf_hc_create(struct Curl_cfilter **pcf, |
576 | | struct Curl_easy *data, |
577 | | enum alpnid *alpnids, size_t alpn_count, |
578 | | uint8_t def_transport) |
579 | 5.60k | { |
580 | 5.60k | struct Curl_cfilter *cf = NULL; |
581 | 5.60k | struct cf_hc_ctx *ctx; |
582 | 5.60k | CURLcode result = CURLE_OK; |
583 | 5.60k | size_t i; |
584 | | |
585 | 5.60k | ctx = curlx_calloc(1, sizeof(*ctx)); |
586 | 5.60k | if(!ctx) { |
587 | 0 | result = CURLE_OUT_OF_MEMORY; |
588 | 0 | goto out; |
589 | 0 | } |
590 | | |
591 | 5.60k | DEBUGASSERT(alpnids); |
592 | 5.60k | DEBUGASSERT(alpn_count); |
593 | 5.60k | DEBUGASSERT(alpn_count <= CURL_ARRAYSIZE(ctx->ballers)); |
594 | 5.60k | if(!alpn_count || (alpn_count > CURL_ARRAYSIZE(ctx->ballers))) { |
595 | 0 | failf(data, "https-connect filter create with unsupported %zu ALPN ids", |
596 | 0 | alpn_count); |
597 | 0 | result = CURLE_FAILED_INIT; |
598 | 0 | goto out; |
599 | 0 | } |
600 | | |
601 | 11.2k | for(i = 0; i < alpn_count; ++i) |
602 | 5.60k | cf_hc_baller_assign(&ctx->ballers[i], alpnids[i], def_transport); |
603 | 11.2k | for(; i < CURL_ARRAYSIZE(ctx->ballers); ++i) |
604 | 5.60k | ctx->ballers[i].alpn_id = ALPN_none; |
605 | 5.60k | ctx->baller_count = alpn_count; |
606 | | |
607 | 5.60k | result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx); |
608 | 5.60k | if(result) |
609 | 0 | goto out; |
610 | 5.60k | ctx = NULL; |
611 | 5.60k | cf_hc_reset(cf, data); |
612 | | |
613 | 5.60k | out: |
614 | 5.60k | *pcf = result ? NULL : cf; |
615 | 5.60k | curlx_free(ctx); |
616 | 5.60k | return result; |
617 | 5.60k | } |
618 | | |
619 | | static CURLcode cf_http_connect_add(struct Curl_easy *data, |
620 | | struct connectdata *conn, |
621 | | int sockindex, |
622 | | enum alpnid *alpn_ids, size_t alpn_count, |
623 | | uint8_t def_transport) |
624 | 5.60k | { |
625 | 5.60k | struct Curl_cfilter *cf; |
626 | 5.60k | CURLcode result = CURLE_OK; |
627 | | |
628 | 5.60k | DEBUGASSERT(data); |
629 | 5.60k | result = cf_hc_create(&cf, data, alpn_ids, alpn_count, def_transport); |
630 | 5.60k | if(result) |
631 | 0 | goto out; |
632 | 5.60k | Curl_conn_cf_add(data, conn, sockindex, cf); |
633 | 5.60k | out: |
634 | 5.60k | return result; |
635 | 5.60k | } |
636 | | |
637 | | static bool cf_https_alpns_contain(enum alpnid id, |
638 | | enum alpnid *list, size_t len) |
639 | 5.60k | { |
640 | 5.60k | size_t i; |
641 | 5.60k | for(i = 0; i < len; ++i) { |
642 | 0 | if(id == list[i]) |
643 | 0 | return TRUE; |
644 | 0 | } |
645 | 5.60k | return FALSE; |
646 | 5.60k | } |
647 | | |
648 | | CURLcode Curl_cf_https_setup(struct Curl_easy *data, |
649 | | struct connectdata *conn, |
650 | | int sockindex) |
651 | 5.65k | { |
652 | 5.65k | enum alpnid alpn_ids[2]; |
653 | 5.65k | size_t alpn_count = 0; |
654 | 5.65k | CURLcode result = CURLE_OK; |
655 | 5.65k | struct Curl_cfilter cf_fake, *cf = NULL; |
656 | | |
657 | 5.65k | (void)sockindex; |
658 | | /* we want to log for the filter before we create it, fake it. */ |
659 | 5.65k | memset(&cf_fake, 0, sizeof(cf_fake)); |
660 | 5.65k | cf_fake.cft = &Curl_cft_http_connect; |
661 | 5.65k | cf = &cf_fake; |
662 | | |
663 | 5.65k | if(conn->bits.tls_enable_alpn) { |
664 | | #ifdef USE_HTTPSRR |
665 | | /* Is there an HTTPSRR use its ALPNs here. |
666 | | * We are here after having selected a connection to a host+port and |
667 | | * can no longer change that. Any HTTPSRR advice for other hosts and ports |
668 | | * we need to ignore. */ |
669 | | struct Curl_dns_entry *dns = data->state.dns[sockindex]; |
670 | | struct Curl_https_rrinfo *rr = dns ? dns->hinfo : NULL; |
671 | | if(rr && !rr->no_def_alpn && /* ALPNs are defaults */ |
672 | | (!rr->target || /* for same host */ |
673 | | !rr->target[0] || |
674 | | (rr->target[0] == '.' && |
675 | | !rr->target[1])) && |
676 | | (rr->port < 0 || /* for same port */ |
677 | | rr->port == conn->remote_port)) { |
678 | | size_t i; |
679 | | for(i = 0; i < CURL_ARRAYSIZE(rr->alpns) && |
680 | | alpn_count < CURL_ARRAYSIZE(alpn_ids); ++i) { |
681 | | enum alpnid alpn = rr->alpns[i]; |
682 | | if(cf_https_alpns_contain(alpn, alpn_ids, alpn_count)) |
683 | | continue; |
684 | | switch(alpn) { |
685 | | case ALPN_h3: |
686 | | if(Curl_conn_may_http3(data, conn, conn->transport_wanted)) |
687 | | break; /* not possible */ |
688 | | if(data->state.http_neg.allowed & CURL_HTTP_V3x) { |
689 | | CURL_TRC_CF(data, cf, "adding h3 via HTTPS-RR"); |
690 | | alpn_ids[alpn_count++] = alpn; |
691 | | } |
692 | | break; |
693 | | case ALPN_h2: |
694 | | if(data->state.http_neg.allowed & CURL_HTTP_V2x) { |
695 | | CURL_TRC_CF(data, cf, "adding h2 via HTTPS-RR"); |
696 | | alpn_ids[alpn_count++] = alpn; |
697 | | } |
698 | | break; |
699 | | case ALPN_h1: |
700 | | if(data->state.http_neg.allowed & CURL_HTTP_V1x) { |
701 | | CURL_TRC_CF(data, cf, "adding h1 via HTTPS-RR"); |
702 | | alpn_ids[alpn_count++] = alpn; |
703 | | } |
704 | | break; |
705 | | default: /* ignore */ |
706 | | break; |
707 | | } |
708 | | } |
709 | | } |
710 | | #endif |
711 | | |
712 | | /* Add preferred HTTP version ALPN first */ |
713 | 5.60k | if(data->state.http_neg.preferred && |
714 | 0 | (alpn_count < CURL_ARRAYSIZE(alpn_ids)) && |
715 | 0 | (data->state.http_neg.preferred & data->state.http_neg.allowed)) { |
716 | 0 | enum alpnid alpn_pref = ALPN_none; |
717 | 0 | switch(data->state.http_neg.preferred) { |
718 | 0 | case CURL_HTTP_V3x: |
719 | 0 | if(!Curl_conn_may_http3(data, conn, conn->transport_wanted)) |
720 | 0 | alpn_pref = ALPN_h3; |
721 | 0 | break; |
722 | 0 | case CURL_HTTP_V2x: |
723 | 0 | alpn_pref = ALPN_h2; |
724 | 0 | break; |
725 | 0 | case CURL_HTTP_V1x: |
726 | 0 | alpn_pref = ALPN_h1; |
727 | 0 | break; |
728 | 0 | default: |
729 | 0 | break; |
730 | 0 | } |
731 | 0 | if(alpn_pref && |
732 | 0 | !cf_https_alpns_contain(alpn_pref, alpn_ids, alpn_count)) { |
733 | 0 | alpn_ids[alpn_count++] = alpn_pref; |
734 | 0 | } |
735 | 0 | } |
736 | | |
737 | 5.60k | if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) && |
738 | 5.60k | (data->state.http_neg.wanted & CURL_HTTP_V3x) && |
739 | 0 | !cf_https_alpns_contain(ALPN_h3, alpn_ids, alpn_count)) { |
740 | 0 | result = Curl_conn_may_http3(data, conn, conn->transport_wanted); |
741 | 0 | if(!result) { |
742 | 0 | CURL_TRC_CF(data, cf, "adding wanted h3"); |
743 | 0 | alpn_ids[alpn_count++] = ALPN_h3; |
744 | 0 | } |
745 | 0 | else if(data->state.http_neg.wanted == CURL_HTTP_V3x) |
746 | 0 | goto out; /* only h3 allowed, not possible, error out */ |
747 | 0 | } |
748 | 5.60k | if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) && |
749 | 5.60k | (data->state.http_neg.wanted & CURL_HTTP_V2x) && |
750 | 5.60k | !cf_https_alpns_contain(ALPN_h2, alpn_ids, alpn_count)) { |
751 | 5.60k | CURL_TRC_CF(data, cf, "adding wanted h2"); |
752 | 5.60k | alpn_ids[alpn_count++] = ALPN_h2; |
753 | 5.60k | } |
754 | 4 | else if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) && |
755 | 4 | (data->state.http_neg.wanted & CURL_HTTP_V1x) && |
756 | 4 | !cf_https_alpns_contain(ALPN_h1, alpn_ids, alpn_count)) { |
757 | 4 | CURL_TRC_CF(data, cf, "adding wanted h1"); |
758 | 4 | alpn_ids[alpn_count++] = ALPN_h1; |
759 | 4 | } |
760 | 5.60k | } |
761 | | |
762 | | /* If we identified ALPNs to use, install our filter. Otherwise, |
763 | | * install nothing, so our call will use a default connect setup. */ |
764 | 5.65k | if(alpn_count) { |
765 | 5.60k | result = cf_http_connect_add(data, conn, sockindex, |
766 | 5.60k | alpn_ids, alpn_count, |
767 | 5.60k | conn->transport_wanted); |
768 | 5.60k | } |
769 | | |
770 | 5.65k | out: |
771 | 5.65k | return result; |
772 | 5.65k | } |
773 | | |
774 | | #endif /* !CURL_DISABLE_HTTP */ |