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