/src/PROJ/curl/lib/cf-ip-happy.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 | | #ifdef HAVE_NETINET_IN_H |
28 | | #include <netinet/in.h> /* <netinet/tcp.h> may need it */ |
29 | | #endif |
30 | | #ifdef HAVE_SYS_UN_H |
31 | | #include <sys/un.h> /* for sockaddr_un */ |
32 | | #endif |
33 | | #ifdef HAVE_LINUX_TCP_H |
34 | | #include <linux/tcp.h> |
35 | | #elif defined(HAVE_NETINET_TCP_H) |
36 | | #include <netinet/tcp.h> |
37 | | #endif |
38 | | #ifdef HAVE_SYS_IOCTL_H |
39 | | #include <sys/ioctl.h> |
40 | | #endif |
41 | | #ifdef HAVE_NETDB_H |
42 | | #include <netdb.h> |
43 | | #endif |
44 | | #ifdef HAVE_FCNTL_H |
45 | | #include <fcntl.h> |
46 | | #endif |
47 | | #ifdef HAVE_ARPA_INET_H |
48 | | #include <arpa/inet.h> |
49 | | #endif |
50 | | |
51 | | #ifdef __VMS |
52 | | #include <in.h> |
53 | | #include <inet.h> |
54 | | #endif |
55 | | |
56 | | #include "urldata.h" |
57 | | #include "connect.h" |
58 | | #include "cfilters.h" |
59 | | #include "cf-ip-happy.h" |
60 | | #include "curl_trc.h" |
61 | | #include "multiif.h" |
62 | | #include "progress.h" |
63 | | #include "select.h" |
64 | | #include "vquic/vquic.h" /* for quic cfilters */ |
65 | | |
66 | | /* The last 3 #include files should be in this order */ |
67 | | #include "curl_printf.h" |
68 | | #include "curl_memory.h" |
69 | | #include "memdebug.h" |
70 | | |
71 | | |
72 | | struct transport_provider { |
73 | | int transport; |
74 | | cf_ip_connect_create *cf_create; |
75 | | }; |
76 | | |
77 | | static |
78 | | #ifndef UNITTESTS |
79 | | const |
80 | | #endif |
81 | | struct transport_provider transport_providers[] = { |
82 | | { TRNSPRT_TCP, Curl_cf_tcp_create }, |
83 | | #if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3) |
84 | | { TRNSPRT_QUIC, Curl_cf_quic_create }, |
85 | | #endif |
86 | | #ifndef CURL_DISABLE_TFTP |
87 | | { TRNSPRT_UDP, Curl_cf_udp_create }, |
88 | | #endif |
89 | | #ifdef USE_UNIX_SOCKETS |
90 | | { TRNSPRT_UNIX, Curl_cf_unix_create }, |
91 | | #endif |
92 | | }; |
93 | | |
94 | | static cf_ip_connect_create *get_cf_create(int transport) |
95 | 0 | { |
96 | 0 | size_t i; |
97 | 0 | for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) { |
98 | 0 | if(transport == transport_providers[i].transport) |
99 | 0 | return transport_providers[i].cf_create; |
100 | 0 | } |
101 | 0 | return NULL; |
102 | 0 | } |
103 | | |
104 | | #ifdef UNITTESTS |
105 | | /* used by unit2600.c */ |
106 | | void Curl_debug_set_transport_provider(int transport, |
107 | | cf_ip_connect_create *cf_create) |
108 | | { |
109 | | size_t i; |
110 | | for(i = 0; i < CURL_ARRAYSIZE(transport_providers); ++i) { |
111 | | if(transport == transport_providers[i].transport) { |
112 | | transport_providers[i].cf_create = cf_create; |
113 | | return; |
114 | | } |
115 | | } |
116 | | } |
117 | | #endif /* UNITTESTS */ |
118 | | |
119 | | |
120 | | struct cf_ai_iter { |
121 | | const struct Curl_addrinfo *head; |
122 | | const struct Curl_addrinfo *last; |
123 | | int ai_family; |
124 | | int n; |
125 | | }; |
126 | | |
127 | | static void cf_ai_iter_init(struct cf_ai_iter *iter, |
128 | | const struct Curl_addrinfo *list, |
129 | | int ai_family) |
130 | 0 | { |
131 | 0 | iter->head = list; |
132 | 0 | iter->ai_family = ai_family; |
133 | 0 | iter->last = NULL; |
134 | 0 | iter->n = -1; |
135 | 0 | } |
136 | | |
137 | | static const struct Curl_addrinfo *cf_ai_iter_next(struct cf_ai_iter *iter) |
138 | 0 | { |
139 | 0 | const struct Curl_addrinfo *addr; |
140 | 0 | if(iter->n < 0) { |
141 | 0 | iter->n++; |
142 | 0 | for(addr = iter->head; addr; addr = addr->ai_next) { |
143 | 0 | if(addr->ai_family == iter->ai_family) |
144 | 0 | break; |
145 | 0 | } |
146 | 0 | iter->last = addr; |
147 | 0 | } |
148 | 0 | else if(iter->last) { |
149 | 0 | iter->n++; |
150 | 0 | for(addr = iter->last->ai_next; addr; addr = addr->ai_next) { |
151 | 0 | if(addr->ai_family == iter->ai_family) |
152 | 0 | break; |
153 | 0 | } |
154 | 0 | iter->last = addr; |
155 | 0 | } |
156 | 0 | return iter->last; |
157 | 0 | } |
158 | | |
159 | | #ifdef USE_IPV6 |
160 | | static bool cf_ai_iter_done(struct cf_ai_iter *iter) |
161 | 0 | { |
162 | 0 | return (iter->n >= 0) && !iter->last; |
163 | 0 | } |
164 | | #endif |
165 | | |
166 | | struct cf_ip_attempt { |
167 | | struct cf_ip_attempt *next; |
168 | | const struct Curl_addrinfo *addr; /* List of addresses to try, not owned */ |
169 | | struct Curl_cfilter *cf; /* current sub-cfilter connecting */ |
170 | | cf_ip_connect_create *cf_create; |
171 | | struct curltime started; /* start of current attempt */ |
172 | | CURLcode result; |
173 | | int ai_family; |
174 | | int transport; |
175 | | int error; |
176 | | BIT(connected); /* cf has connected */ |
177 | | BIT(shutdown); /* cf has shutdown */ |
178 | | BIT(inconclusive); /* connect was not a hard failure, we |
179 | | * might talk to a restarting server */ |
180 | | }; |
181 | | |
182 | | static void cf_ip_attempt_free(struct cf_ip_attempt *a, |
183 | | struct Curl_easy *data) |
184 | 0 | { |
185 | 0 | if(a) { |
186 | 0 | if(a->cf) |
187 | 0 | Curl_conn_cf_discard_chain(&a->cf, data); |
188 | 0 | free(a); |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | | static CURLcode cf_ip_attempt_new(struct cf_ip_attempt **pa, |
193 | | struct Curl_cfilter *cf, |
194 | | struct Curl_easy *data, |
195 | | const struct Curl_addrinfo *addr, |
196 | | int ai_family, |
197 | | int transport, |
198 | | cf_ip_connect_create *cf_create) |
199 | 0 | { |
200 | 0 | struct Curl_cfilter *wcf; |
201 | 0 | struct cf_ip_attempt *a; |
202 | 0 | CURLcode result = CURLE_OK; |
203 | |
|
204 | 0 | *pa = NULL; |
205 | 0 | a = calloc(1, sizeof(*a)); |
206 | 0 | if(!a) |
207 | 0 | return CURLE_OUT_OF_MEMORY; |
208 | | |
209 | 0 | a->addr = addr; |
210 | 0 | a->ai_family = ai_family; |
211 | 0 | a->transport = transport; |
212 | 0 | a->result = CURLE_OK; |
213 | 0 | a->cf_create = cf_create; |
214 | 0 | *pa = a; |
215 | |
|
216 | 0 | result = a->cf_create(&a->cf, data, cf->conn, a->addr, transport); |
217 | 0 | if(result) |
218 | 0 | goto out; |
219 | | |
220 | | /* the new filter might have sub-filters */ |
221 | 0 | for(wcf = a->cf; wcf; wcf = wcf->next) { |
222 | 0 | wcf->conn = cf->conn; |
223 | 0 | wcf->sockindex = cf->sockindex; |
224 | 0 | } |
225 | |
|
226 | 0 | out: |
227 | 0 | if(result) { |
228 | 0 | cf_ip_attempt_free(a, data); |
229 | 0 | *pa = NULL; |
230 | 0 | } |
231 | 0 | return result; |
232 | 0 | } |
233 | | |
234 | | static CURLcode cf_ip_attempt_connect(struct cf_ip_attempt *a, |
235 | | struct Curl_easy *data, |
236 | | bool *connected) |
237 | 0 | { |
238 | 0 | *connected = a->connected; |
239 | 0 | if(!a->result && !*connected) { |
240 | | /* evaluate again */ |
241 | 0 | a->result = Curl_conn_cf_connect(a->cf, data, connected); |
242 | |
|
243 | 0 | if(!a->result) { |
244 | 0 | if(*connected) { |
245 | 0 | a->connected = TRUE; |
246 | 0 | } |
247 | 0 | } |
248 | 0 | else if(a->result == CURLE_WEIRD_SERVER_REPLY) |
249 | 0 | a->inconclusive = TRUE; |
250 | 0 | } |
251 | 0 | return a->result; |
252 | 0 | } |
253 | | |
254 | | struct cf_ip_ballers { |
255 | | struct cf_ip_attempt *running; |
256 | | struct cf_ip_attempt *winner; |
257 | | struct cf_ai_iter addr_iter; |
258 | | #ifdef USE_IPV6 |
259 | | struct cf_ai_iter ipv6_iter; |
260 | | #endif |
261 | | cf_ip_connect_create *cf_create; /* for creating cf */ |
262 | | struct curltime started; |
263 | | struct curltime last_attempt_started; |
264 | | timediff_t attempt_delay_ms; |
265 | | int last_attempt_ai_family; |
266 | | int transport; |
267 | | }; |
268 | | |
269 | | static CURLcode cf_ip_attempt_restart(struct cf_ip_attempt *a, |
270 | | struct Curl_cfilter *cf, |
271 | | struct Curl_easy *data) |
272 | 0 | { |
273 | 0 | struct Curl_cfilter *cf_prev = a->cf; |
274 | 0 | struct Curl_cfilter *wcf; |
275 | 0 | CURLcode result; |
276 | | |
277 | | /* When restarting, we tear down and existing filter *after* we |
278 | | * started up the new one. This gives us a new socket number and |
279 | | * probably a new local port. Which may prevent confusion. */ |
280 | 0 | a->result = CURLE_OK; |
281 | 0 | a->connected = FALSE; |
282 | 0 | a->inconclusive = FALSE; |
283 | 0 | a->cf = NULL; |
284 | |
|
285 | 0 | result = a->cf_create(&a->cf, data, cf->conn, a->addr, a->transport); |
286 | 0 | if(!result) { |
287 | 0 | bool dummy; |
288 | | /* the new filter might have sub-filters */ |
289 | 0 | for(wcf = a->cf; wcf; wcf = wcf->next) { |
290 | 0 | wcf->conn = cf->conn; |
291 | 0 | wcf->sockindex = cf->sockindex; |
292 | 0 | } |
293 | 0 | a->result = cf_ip_attempt_connect(a, data, &dummy); |
294 | 0 | } |
295 | 0 | if(cf_prev) |
296 | 0 | Curl_conn_cf_discard_chain(&cf_prev, data); |
297 | 0 | return result; |
298 | 0 | } |
299 | | |
300 | | static void cf_ip_ballers_clear(struct Curl_cfilter *cf, |
301 | | struct Curl_easy *data, |
302 | | struct cf_ip_ballers *bs) |
303 | 0 | { |
304 | 0 | (void)cf; |
305 | 0 | while(bs->running) { |
306 | 0 | struct cf_ip_attempt *a = bs->running; |
307 | 0 | bs->running = a->next; |
308 | 0 | cf_ip_attempt_free(a, data); |
309 | 0 | } |
310 | 0 | cf_ip_attempt_free(bs->winner, data); |
311 | 0 | bs->winner = NULL; |
312 | 0 | } |
313 | | |
314 | | static CURLcode cf_ip_ballers_init(struct cf_ip_ballers *bs, int ip_version, |
315 | | const struct Curl_addrinfo *addr_list, |
316 | | cf_ip_connect_create *cf_create, |
317 | | int transport, |
318 | | timediff_t attempt_delay_ms) |
319 | 0 | { |
320 | 0 | memset(bs, 0, sizeof(*bs)); |
321 | 0 | bs->cf_create = cf_create; |
322 | 0 | bs->transport = transport; |
323 | 0 | bs->attempt_delay_ms = attempt_delay_ms; |
324 | 0 | bs->last_attempt_ai_family = AF_INET; /* so AF_INET6 is next */ |
325 | |
|
326 | 0 | if(transport == TRNSPRT_UNIX) { |
327 | 0 | #ifdef USE_UNIX_SOCKETS |
328 | 0 | cf_ai_iter_init(&bs->addr_iter, addr_list, AF_UNIX); |
329 | | #else |
330 | | return CURLE_UNSUPPORTED_PROTOCOL; |
331 | | #endif |
332 | 0 | } |
333 | 0 | else { /* TCP/UDP/QUIC */ |
334 | 0 | #ifdef USE_IPV6 |
335 | 0 | if(ip_version == CURL_IPRESOLVE_V6) |
336 | 0 | cf_ai_iter_init(&bs->addr_iter, NULL, AF_INET); |
337 | 0 | else |
338 | 0 | cf_ai_iter_init(&bs->addr_iter, addr_list, AF_INET); |
339 | |
|
340 | 0 | if(ip_version == CURL_IPRESOLVE_V4) |
341 | 0 | cf_ai_iter_init(&bs->ipv6_iter, NULL, AF_INET6); |
342 | 0 | else |
343 | 0 | cf_ai_iter_init(&bs->ipv6_iter, addr_list, AF_INET6); |
344 | | #else |
345 | | (void)ip_version; |
346 | | cf_ai_iter_init(&bs->addr_iter, addr_list, AF_INET); |
347 | | #endif |
348 | 0 | } |
349 | 0 | return CURLE_OK; |
350 | 0 | } |
351 | | |
352 | | static CURLcode cf_ip_ballers_run(struct cf_ip_ballers *bs, |
353 | | struct Curl_cfilter *cf, |
354 | | struct Curl_easy *data, |
355 | | bool *connected) |
356 | 0 | { |
357 | 0 | CURLcode result = CURLE_OK; |
358 | 0 | struct cf_ip_attempt *a = NULL, **panchor; |
359 | 0 | bool do_more, more_possible; |
360 | 0 | struct curltime now; |
361 | 0 | timediff_t next_expire_ms; |
362 | 0 | int i, inconclusive, ongoing; |
363 | |
|
364 | 0 | if(bs->winner) |
365 | 0 | return CURLE_OK; |
366 | | |
367 | 0 | evaluate: |
368 | 0 | now = curlx_now(); |
369 | 0 | ongoing = inconclusive = 0; |
370 | 0 | more_possible = TRUE; |
371 | | |
372 | | /* check if a running baller connects now */ |
373 | 0 | i = -1; |
374 | 0 | for(panchor = &bs->running; *panchor; panchor = &((*panchor)->next)) { |
375 | 0 | ++i; |
376 | 0 | a = *panchor; |
377 | 0 | a->result = cf_ip_attempt_connect(a, data, connected); |
378 | 0 | if(!a->result) { |
379 | 0 | if(*connected) { |
380 | | /* connected, declare the winner, remove from running, |
381 | | * clear remaining running list. */ |
382 | 0 | CURL_TRC_CF(data, cf, "connect attempt #%d successful", i); |
383 | 0 | bs->winner = a; |
384 | 0 | *panchor = a->next; |
385 | 0 | a->next = NULL; |
386 | 0 | while(bs->running) { |
387 | 0 | a = bs->running; |
388 | 0 | bs->running = a->next; |
389 | 0 | cf_ip_attempt_free(a, data); |
390 | 0 | } |
391 | 0 | return CURLE_OK; |
392 | 0 | } |
393 | | /* still running */ |
394 | 0 | ++ongoing; |
395 | 0 | } |
396 | 0 | else if(a->inconclusive) /* failed, but inconclusive */ |
397 | 0 | ++inconclusive; |
398 | 0 | } |
399 | 0 | if(bs->running) |
400 | 0 | CURL_TRC_CF(data, cf, "checked connect attempts: " |
401 | 0 | "%d ongoing, %d inconclusive", ongoing, inconclusive); |
402 | | |
403 | | /* no attempt connected yet, start another one? */ |
404 | 0 | if(!ongoing) { |
405 | 0 | if(!bs->started.tv_sec && !bs->started.tv_usec) |
406 | 0 | bs->started = now; |
407 | 0 | do_more = TRUE; |
408 | 0 | } |
409 | 0 | else { |
410 | 0 | do_more = (curlx_timediff(now, bs->last_attempt_started) >= |
411 | 0 | bs->attempt_delay_ms); |
412 | 0 | if(do_more) |
413 | 0 | CURL_TRC_CF(data, cf, "happy eyeballs timeout expired, " |
414 | 0 | "start next attempt"); |
415 | 0 | } |
416 | |
|
417 | 0 | if(do_more) { |
418 | | /* start the next attempt if there is another ip address to try. |
419 | | * Alternate between address families when possible. */ |
420 | 0 | const struct Curl_addrinfo *addr = NULL; |
421 | 0 | int ai_family = 0; |
422 | 0 | #ifdef USE_IPV6 |
423 | 0 | if((bs->last_attempt_ai_family == AF_INET) || |
424 | 0 | cf_ai_iter_done(&bs->addr_iter)) { |
425 | 0 | addr = cf_ai_iter_next(&bs->ipv6_iter); |
426 | 0 | ai_family = bs->ipv6_iter.ai_family; |
427 | 0 | } |
428 | 0 | #endif |
429 | 0 | if(!addr) { |
430 | 0 | addr = cf_ai_iter_next(&bs->addr_iter); |
431 | 0 | ai_family = bs->addr_iter.ai_family; |
432 | 0 | } |
433 | |
|
434 | 0 | if(addr) { /* try another address */ |
435 | 0 | result = cf_ip_attempt_new(&a, cf, data, addr, ai_family, |
436 | 0 | bs->transport, bs->cf_create); |
437 | 0 | CURL_TRC_CF(data, cf, "starting %s attempt for ipv%s -> %d", |
438 | 0 | bs->running ? "next" : "first", |
439 | 0 | (ai_family == AF_INET) ? "4" : "6", result); |
440 | 0 | if(result) |
441 | 0 | goto out; |
442 | 0 | DEBUGASSERT(a); |
443 | | |
444 | | /* append to running list */ |
445 | 0 | panchor = &bs->running; |
446 | 0 | while(*panchor) |
447 | 0 | panchor = &((*panchor)->next); |
448 | 0 | *panchor = a; |
449 | 0 | bs->last_attempt_started = now; |
450 | 0 | bs->last_attempt_ai_family = ai_family; |
451 | | /* and run everything again */ |
452 | 0 | goto evaluate; |
453 | 0 | } |
454 | 0 | else if(inconclusive) { |
455 | | /* tried all addresses, no success but some where inconclusive. |
456 | | * Let's restart the inconclusive ones. */ |
457 | 0 | if(curlx_timediff(now, bs->last_attempt_started) >= |
458 | 0 | bs->attempt_delay_ms) { |
459 | 0 | CURL_TRC_CF(data, cf, "tried all addresses with inconclusive results" |
460 | 0 | ", restarting one"); |
461 | 0 | i = -1; |
462 | 0 | for(a = bs->running; a; a = a->next) { |
463 | 0 | ++i; |
464 | 0 | if(!a->inconclusive) |
465 | 0 | continue; |
466 | 0 | result = cf_ip_attempt_restart(a, cf, data); |
467 | 0 | CURL_TRC_CF(data, cf, "restarted baller %d -> %d", i, result); |
468 | 0 | if(result) /* serious failure */ |
469 | 0 | goto out; |
470 | 0 | bs->last_attempt_started = now; |
471 | 0 | goto evaluate; |
472 | 0 | } |
473 | 0 | DEBUGASSERT(0); /* should not come here */ |
474 | 0 | } |
475 | | /* attempt timeout for restart has not expired yet */ |
476 | 0 | goto out; |
477 | 0 | } |
478 | 0 | else if(ongoing) { |
479 | | /* no more addresses, no inconclusive attempts */ |
480 | 0 | more_possible = FALSE; |
481 | 0 | } |
482 | 0 | else { |
483 | 0 | CURL_TRC_CF(data, cf, "no more attempts to try"); |
484 | 0 | result = CURLE_COULDNT_CONNECT; |
485 | 0 | i = 0; |
486 | 0 | for(a = bs->running; a; a = a->next) { |
487 | 0 | CURL_TRC_CF(data, cf, "baller %d: result=%d", i, a->result); |
488 | 0 | if(a->result) |
489 | 0 | result = a->result; |
490 | 0 | } |
491 | 0 | } |
492 | 0 | } |
493 | | |
494 | 0 | out: |
495 | 0 | if(!result) { |
496 | | /* when do we need to be called again? */ |
497 | 0 | next_expire_ms = Curl_timeleft(data, &now, TRUE); |
498 | 0 | if(more_possible) { |
499 | 0 | timediff_t expire_ms, elapsed_ms; |
500 | 0 | elapsed_ms = curlx_timediff(now, bs->last_attempt_started); |
501 | 0 | expire_ms = CURLMAX(bs->attempt_delay_ms - elapsed_ms, 0); |
502 | 0 | next_expire_ms = CURLMIN(next_expire_ms, expire_ms); |
503 | 0 | } |
504 | |
|
505 | 0 | if(next_expire_ms <= 0) { |
506 | 0 | failf(data, "Connection timeout after %" FMT_OFF_T " ms", |
507 | 0 | curlx_timediff(now, data->progress.t_startsingle)); |
508 | 0 | return CURLE_OPERATION_TIMEDOUT; |
509 | 0 | } |
510 | 0 | Curl_expire(data, next_expire_ms, EXPIRE_HAPPY_EYEBALLS); |
511 | 0 | } |
512 | 0 | return result; |
513 | 0 | } |
514 | | |
515 | | static CURLcode cf_ip_ballers_shutdown(struct cf_ip_ballers *bs, |
516 | | struct Curl_easy *data, |
517 | | bool *done) |
518 | 0 | { |
519 | 0 | struct cf_ip_attempt *a; |
520 | | |
521 | | /* shutdown all ballers that have not done so already. If one fails, |
522 | | * continue shutting down others until all are shutdown. */ |
523 | 0 | *done = TRUE; |
524 | 0 | for(a = bs->running; a; a = a->next) { |
525 | 0 | bool bdone = FALSE; |
526 | 0 | if(a->shutdown) |
527 | 0 | continue; |
528 | 0 | a->result = a->cf->cft->do_shutdown(a->cf, data, &bdone); |
529 | 0 | if(a->result || bdone) |
530 | 0 | a->shutdown = TRUE; /* treat a failed shutdown as done */ |
531 | 0 | else |
532 | 0 | *done = FALSE; |
533 | 0 | } |
534 | 0 | return CURLE_OK; |
535 | 0 | } |
536 | | |
537 | | static CURLcode cf_ip_ballers_pollset(struct cf_ip_ballers *bs, |
538 | | struct Curl_easy *data, |
539 | | struct easy_pollset *ps) |
540 | 0 | { |
541 | 0 | struct cf_ip_attempt *a; |
542 | 0 | CURLcode result = CURLE_OK; |
543 | 0 | for(a = bs->running; a && !result; a = a->next) { |
544 | 0 | if(a->result) |
545 | 0 | continue; |
546 | 0 | result = Curl_conn_cf_adjust_pollset(a->cf, data, ps); |
547 | 0 | } |
548 | 0 | return result; |
549 | 0 | } |
550 | | |
551 | | static bool cf_ip_ballers_pending(struct cf_ip_ballers *bs, |
552 | | const struct Curl_easy *data) |
553 | 0 | { |
554 | 0 | struct cf_ip_attempt *a; |
555 | |
|
556 | 0 | for(a = bs->running; a; a = a->next) { |
557 | 0 | if(a->result) |
558 | 0 | continue; |
559 | 0 | if(a->cf->cft->has_data_pending(a->cf, data)) |
560 | 0 | return TRUE; |
561 | 0 | } |
562 | 0 | return FALSE; |
563 | 0 | } |
564 | | |
565 | | static struct curltime cf_ip_ballers_max_time(struct cf_ip_ballers *bs, |
566 | | struct Curl_easy *data, |
567 | | int query) |
568 | 0 | { |
569 | 0 | struct curltime t, tmax; |
570 | 0 | struct cf_ip_attempt *a; |
571 | |
|
572 | 0 | memset(&tmax, 0, sizeof(tmax)); |
573 | 0 | for(a = bs->running; a; a = a->next) { |
574 | 0 | memset(&t, 0, sizeof(t)); |
575 | 0 | if(!a->cf->cft->query(a->cf, data, query, NULL, &t)) { |
576 | 0 | if((t.tv_sec || t.tv_usec) && curlx_timediff_us(t, tmax) > 0) |
577 | 0 | tmax = t; |
578 | 0 | } |
579 | 0 | } |
580 | 0 | return tmax; |
581 | 0 | } |
582 | | |
583 | | static int cf_ip_ballers_min_reply_ms(struct cf_ip_ballers *bs, |
584 | | struct Curl_easy *data) |
585 | 0 | { |
586 | 0 | int reply_ms = -1, breply_ms; |
587 | 0 | struct cf_ip_attempt *a; |
588 | |
|
589 | 0 | for(a = bs->running; a; a = a->next) { |
590 | 0 | if(!a->cf->cft->query(a->cf, data, CF_QUERY_CONNECT_REPLY_MS, |
591 | 0 | &breply_ms, NULL)) { |
592 | 0 | if(breply_ms >= 0 && (reply_ms < 0 || breply_ms < reply_ms)) |
593 | 0 | reply_ms = breply_ms; |
594 | 0 | } |
595 | 0 | } |
596 | 0 | return reply_ms; |
597 | 0 | } |
598 | | |
599 | | |
600 | | typedef enum { |
601 | | SCFST_INIT, |
602 | | SCFST_WAITING, |
603 | | SCFST_DONE |
604 | | } cf_connect_state; |
605 | | |
606 | | struct cf_ip_happy_ctx { |
607 | | int transport; |
608 | | cf_ip_connect_create *cf_create; |
609 | | cf_connect_state state; |
610 | | struct cf_ip_ballers ballers; |
611 | | struct curltime started; |
612 | | }; |
613 | | |
614 | | |
615 | | static CURLcode is_connected(struct Curl_cfilter *cf, |
616 | | struct Curl_easy *data, |
617 | | bool *connected) |
618 | 0 | { |
619 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
620 | 0 | struct connectdata *conn = cf->conn; |
621 | 0 | CURLcode result; |
622 | |
|
623 | 0 | result = cf_ip_ballers_run(&ctx->ballers, cf, data, connected); |
624 | |
|
625 | 0 | if(!result) |
626 | 0 | return CURLE_OK; |
627 | | |
628 | 0 | { |
629 | 0 | const char *hostname, *proxy_name = NULL; |
630 | 0 | int port; |
631 | 0 | #ifndef CURL_DISABLE_PROXY |
632 | 0 | if(conn->bits.socksproxy) |
633 | 0 | proxy_name = conn->socks_proxy.host.name; |
634 | 0 | else if(conn->bits.httpproxy) |
635 | 0 | proxy_name = conn->http_proxy.host.name; |
636 | 0 | #endif |
637 | 0 | hostname = conn->bits.conn_to_host ? |
638 | 0 | conn->conn_to_host.name : conn->host.name; |
639 | |
|
640 | 0 | if(cf->sockindex == SECONDARYSOCKET) |
641 | 0 | port = conn->secondary_port; |
642 | 0 | else if(cf->conn->bits.conn_to_port) |
643 | 0 | port = conn->conn_to_port; |
644 | 0 | else |
645 | 0 | port = conn->remote_port; |
646 | |
|
647 | 0 | failf(data, "Failed to connect to %s port %u %s%s%safter " |
648 | 0 | "%" FMT_TIMEDIFF_T " ms: %s", |
649 | 0 | hostname, port, |
650 | 0 | proxy_name ? "via " : "", |
651 | 0 | proxy_name ? proxy_name : "", |
652 | 0 | proxy_name ? " " : "", |
653 | 0 | curlx_timediff(curlx_now(), data->progress.t_startsingle), |
654 | 0 | curl_easy_strerror(result)); |
655 | 0 | } |
656 | |
|
657 | 0 | #ifdef SOCKETIMEDOUT |
658 | 0 | if(SOCKETIMEDOUT == data->state.os_errno) |
659 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
660 | 0 | #endif |
661 | |
|
662 | 0 | return result; |
663 | 0 | } |
664 | | |
665 | | /* |
666 | | * Connect to the given host with timeout, proxy or remote does not matter. |
667 | | * There might be more than one IP address to try out. |
668 | | */ |
669 | | static CURLcode start_connect(struct Curl_cfilter *cf, |
670 | | struct Curl_easy *data) |
671 | 0 | { |
672 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
673 | 0 | struct Curl_dns_entry *dns = data->state.dns[cf->sockindex]; |
674 | |
|
675 | 0 | if(!dns) |
676 | 0 | return CURLE_FAILED_INIT; |
677 | | |
678 | 0 | if(Curl_timeleft(data, NULL, TRUE) < 0) { |
679 | | /* a precaution, no need to continue if time already is up */ |
680 | 0 | failf(data, "Connection time-out"); |
681 | 0 | return CURLE_OPERATION_TIMEDOUT; |
682 | 0 | } |
683 | | |
684 | 0 | CURL_TRC_CF(data, cf, "init ip ballers for transport %d", ctx->transport); |
685 | 0 | ctx->started = curlx_now(); |
686 | 0 | return cf_ip_ballers_init(&ctx->ballers, cf->conn->ip_version, |
687 | 0 | dns->addr, ctx->cf_create, ctx->transport, |
688 | 0 | data->set.happy_eyeballs_timeout); |
689 | 0 | } |
690 | | |
691 | | static void cf_ip_happy_ctx_clear(struct Curl_cfilter *cf, |
692 | | struct Curl_easy *data) |
693 | 0 | { |
694 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
695 | |
|
696 | 0 | DEBUGASSERT(ctx); |
697 | 0 | DEBUGASSERT(data); |
698 | 0 | cf_ip_ballers_clear(cf, data, &ctx->ballers); |
699 | 0 | } |
700 | | |
701 | | static CURLcode cf_ip_happy_shutdown(struct Curl_cfilter *cf, |
702 | | struct Curl_easy *data, |
703 | | bool *done) |
704 | 0 | { |
705 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
706 | 0 | CURLcode result = CURLE_OK; |
707 | |
|
708 | 0 | DEBUGASSERT(data); |
709 | 0 | if(cf->connected) { |
710 | 0 | *done = TRUE; |
711 | 0 | return CURLE_OK; |
712 | 0 | } |
713 | | |
714 | 0 | result = cf_ip_ballers_shutdown(&ctx->ballers, data, done); |
715 | 0 | CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", result, *done); |
716 | 0 | return result; |
717 | 0 | } |
718 | | |
719 | | static CURLcode cf_ip_happy_adjust_pollset(struct Curl_cfilter *cf, |
720 | | struct Curl_easy *data, |
721 | | struct easy_pollset *ps) |
722 | 0 | { |
723 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
724 | 0 | CURLcode result = CURLE_OK; |
725 | |
|
726 | 0 | if(!cf->connected) { |
727 | 0 | result = cf_ip_ballers_pollset(&ctx->ballers, data, ps); |
728 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %d socks", result, ps->n); |
729 | 0 | } |
730 | 0 | return result; |
731 | 0 | } |
732 | | |
733 | | static CURLcode cf_ip_happy_connect(struct Curl_cfilter *cf, |
734 | | struct Curl_easy *data, |
735 | | bool *done) |
736 | 0 | { |
737 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
738 | 0 | CURLcode result = CURLE_OK; |
739 | |
|
740 | 0 | if(cf->connected) { |
741 | 0 | *done = TRUE; |
742 | 0 | return CURLE_OK; |
743 | 0 | } |
744 | | |
745 | 0 | DEBUGASSERT(ctx); |
746 | 0 | *done = FALSE; |
747 | |
|
748 | 0 | switch(ctx->state) { |
749 | 0 | case SCFST_INIT: |
750 | 0 | DEBUGASSERT(CURL_SOCKET_BAD == Curl_conn_cf_get_socket(cf, data)); |
751 | 0 | DEBUGASSERT(!cf->connected); |
752 | 0 | result = start_connect(cf, data); |
753 | 0 | if(result) |
754 | 0 | return result; |
755 | 0 | ctx->state = SCFST_WAITING; |
756 | 0 | FALLTHROUGH(); |
757 | 0 | case SCFST_WAITING: |
758 | 0 | result = is_connected(cf, data, done); |
759 | 0 | if(!result && *done) { |
760 | 0 | DEBUGASSERT(ctx->ballers.winner); |
761 | 0 | DEBUGASSERT(ctx->ballers.winner->cf); |
762 | 0 | DEBUGASSERT(ctx->ballers.winner->cf->connected); |
763 | | /* we have a winner. Install and activate it. |
764 | | * close/free all others. */ |
765 | 0 | ctx->state = SCFST_DONE; |
766 | 0 | cf->connected = TRUE; |
767 | 0 | cf->next = ctx->ballers.winner->cf; |
768 | 0 | ctx->ballers.winner->cf = NULL; |
769 | 0 | cf_ip_happy_ctx_clear(cf, data); |
770 | |
|
771 | 0 | if(cf->conn->handler->protocol & PROTO_FAMILY_SSH) |
772 | 0 | Curl_pgrsTime(data, TIMER_APPCONNECT); /* we are connected already */ |
773 | 0 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
774 | 0 | if(Curl_trc_cf_is_verbose(cf, data)) { |
775 | 0 | struct ip_quadruple ipquad; |
776 | 0 | bool is_ipv6; |
777 | 0 | if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) { |
778 | 0 | const char *host; |
779 | 0 | int port; |
780 | 0 | Curl_conn_get_current_host(data, cf->sockindex, &host, &port); |
781 | 0 | CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u", |
782 | 0 | host, ipquad.remote_ip, ipquad.remote_port); |
783 | 0 | } |
784 | 0 | } |
785 | 0 | #endif |
786 | 0 | data->info.numconnects++; /* to track the # of connections made */ |
787 | 0 | } |
788 | 0 | break; |
789 | 0 | case SCFST_DONE: |
790 | 0 | *done = TRUE; |
791 | 0 | break; |
792 | 0 | } |
793 | 0 | return result; |
794 | 0 | } |
795 | | |
796 | | static void cf_ip_happy_close(struct Curl_cfilter *cf, |
797 | | struct Curl_easy *data) |
798 | 0 | { |
799 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
800 | |
|
801 | 0 | CURL_TRC_CF(data, cf, "close"); |
802 | 0 | cf_ip_happy_ctx_clear(cf, data); |
803 | 0 | cf->connected = FALSE; |
804 | 0 | ctx->state = SCFST_INIT; |
805 | |
|
806 | 0 | if(cf->next) { |
807 | 0 | cf->next->cft->do_close(cf->next, data); |
808 | 0 | Curl_conn_cf_discard_chain(&cf->next, data); |
809 | 0 | } |
810 | 0 | } |
811 | | |
812 | | static bool cf_ip_happy_data_pending(struct Curl_cfilter *cf, |
813 | | const struct Curl_easy *data) |
814 | 0 | { |
815 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
816 | |
|
817 | 0 | if(!cf->connected) { |
818 | 0 | return cf_ip_ballers_pending(&ctx->ballers, data); |
819 | 0 | } |
820 | 0 | return cf->next->cft->has_data_pending(cf->next, data); |
821 | 0 | } |
822 | | |
823 | | static CURLcode cf_ip_happy_query(struct Curl_cfilter *cf, |
824 | | struct Curl_easy *data, |
825 | | int query, int *pres1, void *pres2) |
826 | 0 | { |
827 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
828 | |
|
829 | 0 | if(!cf->connected) { |
830 | 0 | switch(query) { |
831 | 0 | case CF_QUERY_CONNECT_REPLY_MS: { |
832 | 0 | *pres1 = cf_ip_ballers_min_reply_ms(&ctx->ballers, data); |
833 | 0 | CURL_TRC_CF(data, cf, "query connect reply: %dms", *pres1); |
834 | 0 | return CURLE_OK; |
835 | 0 | } |
836 | 0 | case CF_QUERY_TIMER_CONNECT: { |
837 | 0 | struct curltime *when = pres2; |
838 | 0 | *when = cf_ip_ballers_max_time(&ctx->ballers, data, |
839 | 0 | CF_QUERY_TIMER_CONNECT); |
840 | 0 | return CURLE_OK; |
841 | 0 | } |
842 | 0 | case CF_QUERY_TIMER_APPCONNECT: { |
843 | 0 | struct curltime *when = pres2; |
844 | 0 | *when = cf_ip_ballers_max_time(&ctx->ballers, data, |
845 | 0 | CF_QUERY_TIMER_APPCONNECT); |
846 | 0 | return CURLE_OK; |
847 | 0 | } |
848 | 0 | default: |
849 | 0 | break; |
850 | 0 | } |
851 | 0 | } |
852 | | |
853 | 0 | return cf->next ? |
854 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
855 | 0 | CURLE_UNKNOWN_OPTION; |
856 | 0 | } |
857 | | |
858 | | static void cf_ip_happy_destroy(struct Curl_cfilter *cf, |
859 | | struct Curl_easy *data) |
860 | 0 | { |
861 | 0 | struct cf_ip_happy_ctx *ctx = cf->ctx; |
862 | |
|
863 | 0 | CURL_TRC_CF(data, cf, "destroy"); |
864 | 0 | if(ctx) { |
865 | 0 | cf_ip_happy_ctx_clear(cf, data); |
866 | 0 | } |
867 | | /* release any resources held in state */ |
868 | 0 | Curl_safefree(ctx); |
869 | 0 | } |
870 | | |
871 | | struct Curl_cftype Curl_cft_ip_happy = { |
872 | | "HAPPY-EYEBALLS", |
873 | | 0, |
874 | | CURL_LOG_LVL_NONE, |
875 | | cf_ip_happy_destroy, |
876 | | cf_ip_happy_connect, |
877 | | cf_ip_happy_close, |
878 | | cf_ip_happy_shutdown, |
879 | | cf_ip_happy_adjust_pollset, |
880 | | cf_ip_happy_data_pending, |
881 | | Curl_cf_def_send, |
882 | | Curl_cf_def_recv, |
883 | | Curl_cf_def_cntrl, |
884 | | Curl_cf_def_conn_is_alive, |
885 | | Curl_cf_def_conn_keep_alive, |
886 | | cf_ip_happy_query, |
887 | | }; |
888 | | |
889 | | /** |
890 | | * Create an IP happy eyeball connection filter that uses the, once resolved, |
891 | | * address information to connect on ip families based on connection |
892 | | * configuration. |
893 | | * @param pcf output, the created cfilter |
894 | | * @param data easy handle used in creation |
895 | | * @param conn connection the filter is created for |
896 | | * @param cf_create method to create the sub-filters performing the |
897 | | * actual connects. |
898 | | */ |
899 | | static CURLcode cf_ip_happy_create(struct Curl_cfilter **pcf, |
900 | | struct Curl_easy *data, |
901 | | struct connectdata *conn, |
902 | | cf_ip_connect_create *cf_create, |
903 | | int transport) |
904 | 0 | { |
905 | 0 | struct cf_ip_happy_ctx *ctx = NULL; |
906 | 0 | CURLcode result; |
907 | |
|
908 | 0 | (void)data; |
909 | 0 | (void)conn; |
910 | 0 | *pcf = NULL; |
911 | 0 | ctx = calloc(1, sizeof(*ctx)); |
912 | 0 | if(!ctx) { |
913 | 0 | result = CURLE_OUT_OF_MEMORY; |
914 | 0 | goto out; |
915 | 0 | } |
916 | 0 | ctx->transport = transport; |
917 | 0 | ctx->cf_create = cf_create; |
918 | |
|
919 | 0 | result = Curl_cf_create(pcf, &Curl_cft_ip_happy, ctx); |
920 | |
|
921 | 0 | out: |
922 | 0 | if(result) { |
923 | 0 | Curl_safefree(*pcf); |
924 | 0 | free(ctx); |
925 | 0 | } |
926 | 0 | return result; |
927 | 0 | } |
928 | | |
929 | | CURLcode cf_ip_happy_insert_after(struct Curl_cfilter *cf_at, |
930 | | struct Curl_easy *data, |
931 | | int transport) |
932 | 0 | { |
933 | 0 | cf_ip_connect_create *cf_create; |
934 | 0 | struct Curl_cfilter *cf; |
935 | 0 | CURLcode result; |
936 | | |
937 | | /* Need to be first */ |
938 | 0 | DEBUGASSERT(cf_at); |
939 | 0 | cf_create = get_cf_create(transport); |
940 | 0 | if(!cf_create) { |
941 | 0 | CURL_TRC_CF(data, cf_at, "unsupported transport type %d", transport); |
942 | 0 | return CURLE_UNSUPPORTED_PROTOCOL; |
943 | 0 | } |
944 | 0 | result = cf_ip_happy_create(&cf, data, cf_at->conn, cf_create, transport); |
945 | 0 | if(result) |
946 | 0 | return result; |
947 | | |
948 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
949 | 0 | return CURLE_OK; |
950 | 0 | } |