master
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"
26/***********************************************************************
27 * Only for threaded name resolves builds
28 **********************************************************************/
29#ifdef USE_RESOLV_THREADED
31#ifdef HAVE_NETINET_IN_H
32#include <netinet/in.h>
33#endif
34#ifdef HAVE_NETDB_H
35#include <netdb.h>
36#endif
37#ifdef HAVE_ARPA_INET_H
38#include <arpa/inet.h>
39#endif
40#ifdef __VMS
41#include <in.h>
42#include <inet.h>
43#endif
45#ifdef HAVE_GETADDRINFO
46#define RESOLVER_ENOMEM EAI_MEMORY /* = WSA_NOT_ENOUGH_MEMORY on Windows */
47#else
48#define RESOLVER_ENOMEM SOCKENOMEM
49#endif
51#include "urldata.h"
52#include "cfilters.h"
53#include "curl_addrinfo.h"
54#include "curl_trc.h"
55#include "url.h"
56#include "multiif.h"
57#include "curl_threads.h"
58#include "progress.h"
59#include "rand.h"
60#include "select.h"
61#include "thrdqueue.h"
62#include "vdns/hostip.h"
63#include "vdns/httpsrr.h"
64#include "curlx/strparse.h"
65#include "curlx/wait.h"
67#ifdef USE_ARES
68#include <ares.h>
69#ifdef USE_HTTPSRR
70#define USE_HTTPSRR_ARES /* the combo */
71#endif
72#endif
75/*
76 * Curl_async_global_init()
77 * Called from curl_global_init() to initialize global resolver environment.
78 * Does nothing here.
79 */
80int Curl_async_global_init(void)
81{
82#if defined(USE_ARES) && defined(CARES_HAVE_ARES_LIBRARY_INIT)
83 if(ares_library_init(ARES_LIB_INIT_ALL)) {
84 return CURLE_FAILED_INIT;
85 }
86#endif
87 return CURLE_OK;
88}
90/*
91 * Curl_async_global_cleanup()
92 * Called from curl_global_cleanup() to destroy global resolver environment.
93 * Does nothing here.
94 */
95void Curl_async_global_cleanup(void)
96{
97#if defined(USE_ARES) && defined(CARES_HAVE_ARES_LIBRARY_INIT)
98 ares_library_cleanup();
99#endif
100}
102#ifdef CURLVERBOSE
103#define CURL_ASYN_ITEM_DESC_LEN 64
104#define async_item_description(x) (x)->description
105#else
106#define async_item_description(x) NULL
107#endif
109struct async_thrdd_item {
110 struct Curl_addrinfo *res;
111#ifdef CURLVERBOSE
112 char description[CURL_ASYN_ITEM_DESC_LEN];
113#endif
114 int sockerr;
115 uint32_t mid;
116 uint32_t resolv_id;
117 uint16_t port;
118 uint8_t transport;
119 uint8_t dns_queries;
120 BIT(negative); /* resolver answered that the name does not exist */
121#ifdef DEBUGBUILD
122 uint32_t delay_ms;
123 uint32_t delay_fail_ms;
124 BIT(dbg_negative);
125#endif
126 char hostname[1];
127};
129/* Give up reference to add_ctx */
130static void async_thrdd_item_destroy(struct async_thrdd_item *item)
131{
132 if(item) {
133 if(item->res)
134 Curl_freeaddrinfo(item->res);
135 curlx_free(item);
136 }
137}
139/* Initialize context for threaded resolver */
140static struct async_thrdd_item *async_thrdd_item_create(
141 struct Curl_easy *data,
142 uint32_t resolv_id, uint8_t dns_queries,
143 const char *hostname, uint16_t port,
144 uint8_t transport)
145{
146 size_t hostlen = strlen(hostname);
147 struct async_thrdd_item *item;
149 item = curlx_calloc(1, sizeof(*item) + hostlen);
150 if(!item)
151 return NULL;
153 if(hostlen) /* null-terminator of name already in struct size */
154 memcpy(item->hostname, hostname, hostlen);
155 item->mid = data->mid;
156 item->resolv_id = resolv_id;
157 item->dns_queries = dns_queries;
158 item->port = port;
159 item->transport = transport;
161#ifdef CURLVERBOSE
162 curl_msnprintf(item->description, sizeof(item->description),
163 "[%" FMT_OFF_T "/%u] %s %s:%u",
164 data->id, item->resolv_id,
165 Curl_resolv_query_str(dns_queries),
166 item->hostname, item->port);
167#endif
169#ifdef DEBUGBUILD
170 {
171 const char *p = getenv("CURL_DBG_RESOLV_DELAY");
172 if(p) {
173 curl_off_t l;
174 if(!curlx_str_number(&p, &l, UINT32_MAX)) {
175 item->delay_ms = (uint32_t)l;
176 }
177 }
178 p = getenv("CURL_DBG_RESOLV_FAIL_DELAY");
179 if(p) {
180 curl_off_t l;
181 if(!curlx_str_number(&p, &l, UINT32_MAX)) {
182 unsigned char c = 0;
183 Curl_rand_bytes(data, FALSE, &c, 1);
184 item->delay_fail_ms = (uint32_t)l + c;
185 }
186 }
187 if(getenv("CURL_DBG_RESOLV_FAIL_NEGATIVE"))
188 item->dbg_negative = TRUE;
189 }
190#endif
192 return item;
193}
195#ifdef USE_HTTPSRR_ARES
197static void async_thrdd_rr_done(void *user_data, ares_status_t status,
198 size_t timeouts,
199 const ares_dns_record_t *dnsrec)
200{
201 struct Curl_resolv_async *async = user_data;
202 struct async_thrdd_ctx *thrdd = async ? &async->thrdd : NULL;
204 (void)timeouts;
205 if(!thrdd)
206 return;
208 async->dns_responses |= CURL_DNSQ_HTTPS;
209 async->queries_ongoing--;
210 async->done = !async->queries_ongoing;
211 if((ARES_SUCCESS == status) && dnsrec)
212 async->result = Curl_httpsrr_from_ares(dnsrec, &async->httpsrr);
213}
215/* Initialize the c-ares channel used for the HTTPS RR side-query, without
216 * submitting any query on it yet. */
217static CURLcode async_rr_init(struct Curl_easy *data,
218 struct Curl_resolv_async *async)
219{
220 struct async_thrdd_ctx *thrdd = &async->thrdd;
221 int status;
223 (void)data;
224 DEBUGASSERT(!thrdd->rr.channel);
225 status = ares_init_options(&thrdd->rr.channel, NULL, 0);
226 if(status != ARES_SUCCESS) {
227 thrdd->rr.channel = NULL;
228 return CURLE_FAILED_INIT;
229 }
230#ifdef DEBUGBUILD
231 if(getenv("CURL_DNS_SERVER")) {
232 const char *servers = getenv("CURL_DNS_SERVER");
233 status = ares_set_servers_ports_csv(thrdd->rr.channel, servers);
234 if(status)
235 return CURLE_FAILED_INIT;
236 }
237#endif
238 return CURLE_OK;
239}
241/* Submit the HTTPS RR query on the already initialized channel. */
242static CURLcode async_rr_start(struct Curl_easy *data,
243 struct Curl_resolv_async *async)
244{
245 struct async_thrdd_ctx *thrdd = &async->thrdd;
246 char *https_name = NULL;
247 CURLcode result = CURLE_OK;
249 if(async->peer->port != 443) {
250 https_name = curl_maprintf("_%u._https.%s",
251 async->peer->port, async->peer->hostname);
252 if(!https_name) {
253 result = CURLE_OUT_OF_MEMORY;
254 goto out;
255 }
256 }
258 async->queries_ongoing++;
259 ares_query_dnsrec(thrdd->rr.channel,
260 https_name ? https_name : async->peer->hostname,
261 ARES_CLASS_IN, ARES_REC_TYPE_HTTPS,
262 async_thrdd_rr_done, async, NULL);
263 CURL_TRC_DNS(data, "[HTTPS] query records for %s",
264 https_name ? https_name : async->peer->hostname);
266out:
267 curlx_free(https_name);
268 return result;
269}
270#endif
272void Curl_async_thrdd_shutdown(struct Curl_easy *data,
273 struct Curl_resolv_async *async)
274{
275 Curl_async_thrdd_destroy(data, async);
276}
278struct async_thrdd_match_ctx {
279 uint32_t mid;
280 uint32_t resolv_id;
281};
283static bool async_thrdd_match_item(void *qitem, void *match_data)
284{
285 const struct async_thrdd_match_ctx *ctx = match_data;
286 struct async_thrdd_item *item = qitem;
287 return (item->mid == ctx->mid) && (item->resolv_id == ctx->resolv_id);
288}
290void Curl_async_thrdd_destroy(struct Curl_easy *data,
291 struct Curl_resolv_async *async)
292{
293 (void)data;
294 if(async->queries_ongoing && !async->done &&
295 data->multi && data->multi->resolv_thrdq) {
296 /* Remove any resolve items still queued */
297 struct async_thrdd_match_ctx mctx;
298 mctx.mid = data->mid;
299 mctx.resolv_id = async->id;
300 Curl_thrdq_clear(data->multi->resolv_thrdq,
301 async_thrdd_match_item, &mctx);
302 }
303#ifdef USE_HTTPSRR_ARES
304 if(async->thrdd.rr.channel) {
305 ares_destroy(async->thrdd.rr.channel);
306 async->thrdd.rr.channel = NULL;
307 }
308#endif
309 async_thrdd_item_destroy(async->thrdd.res_A);
310 async->thrdd.res_A = NULL;
311 async_thrdd_item_destroy(async->thrdd.res_AAAA);
312 async->thrdd.res_AAAA = NULL;
313}
315/*
316 * Waits for a resolve to finish. This function should be avoided since using
317 * this risk getting the multi interface to "hang".
318 */
319CURLcode Curl_async_await(struct Curl_easy *data, uint32_t resolv_id,
320 struct Curl_dns_entry **pdns)
321{
322 struct Curl_resolv_async *async = Curl_async_get(data, resolv_id);
323 struct async_thrdd_ctx *thrdd = async ? &async->thrdd : NULL;
324 timediff_t milli, ms;
325 CURLcode result = CURLE_AGAIN;
327 if(!thrdd)
328 return CURLE_FAILED_INIT;
330 while(result == CURLE_AGAIN) {
331 while(async->queries_ongoing && !async->done) {
332 Curl_async_thrdd_multi_process(data->multi);
333 if(async->done)
334 break;
336 ms = curlx_ptimediff_ms(Curl_pgrs_now(data), &async->start);
337 if(ms < 3)
338 milli = 0;
339 else if(ms <= 50)
340 milli = ms / 3;
341 else if(ms <= 250)
342 milli = 50;
343 else
344 milli = 200;
345 CURL_TRC_DNS(data, "await, waiting %" FMT_TIMEDIFF_T "ms", milli);
346 curlx_wait_ms(milli);
347 }
348 result = Curl_async_take_result(data, async, pdns);
349 }
350 return result;
351}
353#ifdef HAVE_GETADDRINFO
355/* Was the getaddrinfo() failure an authoritative negative answer,
356 i.e. the resolver responded that the name (or its data) does not
357 exist? Transient failures like EAI_AGAIN and local troubles must
358 not count as negative answers. */
359static bool gai_negative(int rc)
360{
361 switch(rc) {
362#ifdef EAI_NONAME
363 case EAI_NONAME:
364#endif
365#if defined(EAI_NODATA) && \
366 (!defined(EAI_NONAME) || (EAI_NODATA != EAI_NONAME))
367 case EAI_NODATA:
368#endif
369 return TRUE;
370 default:
371 return FALSE;
372 }
373}
375/* Process the item, using Curl_getaddrinfo_ex() */
376static void async_thrdd_item_process(void *arg)
377{
378 struct async_thrdd_item *item = arg;
379 struct addrinfo hints;
380 char service[12];
381 int pf = PF_INET;
382 int rc;
384#ifdef DEBUGBUILD
385 if(item->delay_ms) {
386 curlx_wait_ms(item->delay_ms);
387 }
388 if(item->delay_fail_ms) {
389 curlx_wait_ms(item->delay_fail_ms);
390 item->negative = item->dbg_negative;
391 return;
392 }
393#endif
395 memset(&hints, 0, sizeof(hints));
396#ifdef CURLRES_IPV6
397 if(item->dns_queries & CURL_DNSQ_AAAA) {
398 pf = (item->dns_queries & CURL_DNSQ_A) ? PF_UNSPEC : PF_INET6;
399 }
400#endif
401 hints.ai_family = pf;
402 hints.ai_socktype = Curl_socktype_for_transport(item->transport);
403 hints.ai_protocol = Curl_protocol_for_transport(item->transport);
404#ifdef __APPLE__
405 /* If we leave `ai_flags == 0` then macOS is looking for IPV4MAPPED
406 * when doing AAAA queries. We do not want this "help". */
407 hints.ai_flags = AI_ADDRCONFIG;
408#endif
410 curl_msnprintf(service, sizeof(service), "%u", item->port);
411#ifdef AI_NUMERICSERV
412 hints.ai_flags |= AI_NUMERICSERV;
413#endif
415 rc = Curl_getaddrinfo_ex(item->hostname, service, &hints, &item->res);
416 if(rc) {
417 item->sockerr = SOCKERRNO ? SOCKERRNO : rc;
418 if(item->sockerr == 0)
419 item->sockerr = RESOLVER_ENOMEM;
420 item->negative = gai_negative(rc);
421 }
422 else {
423 Curl_addrinfo_set_port(item->res, item->port);
424 }
425}
427#else /* HAVE_GETADDRINFO */
429/* Process the item, using Curl_ipv4_resolve_r() */
430static void async_thrdd_item_process(void *arg)
431{
432 struct async_thrdd_item *item = arg;
434#ifdef DEBUGBUILD
435 if(item->delay_ms) {
436 curlx_wait_ms(item->delay_ms);
437 }
438 if(item->delay_fail_ms) {
439 curlx_wait_ms(item->delay_fail_ms);
440 item->negative = item->dbg_negative;
441 return;
442 }
443#endif
444 item->res = Curl_ipv4_resolve_r(item->hostname, item->port);
445 if(!item->res) {
446 item->sockerr = SOCKERRNO;
447 if(item->sockerr == 0)
448 item->sockerr = RESOLVER_ENOMEM;
449 /* this resolver cannot tell a transient failure from an
450 authoritative negative answer, treat it as before */
451 item->negative = TRUE;
452 }
453}
455#endif /* HAVE_GETADDRINFO */
457#ifdef ENABLE_INTERNAL_WAKEUP
458static void async_thrdd_event(const struct curl_thrdq *tqueue,
459 Curl_thrdq_event ev,
460 void *user_data)
461{
462 struct Curl_multi *multi = user_data;
463 (void)tqueue;
464 switch(ev) {
465 case CURL_THRDQ_EV_ITEM_DONE:
466 Curl_multi_wakeup_internal(multi);
467 break;
468 default:
469 break;
470 }
471}
472#else
473#define async_thrdd_event NULL
474#endif
476static void async_thrdd_item_free(void *item)
477{
478 async_thrdd_item_destroy(item);
479}
481/* Create a thread queue for processing resolv items */
482CURLcode Curl_async_thrdd_multi_init(struct Curl_multi *multi,
483 uint32_t min_threads,
484 uint32_t max_threads,
485 uint32_t idle_time_ms)
486{
487 CURLcode result;
488 DEBUGASSERT(!multi->resolv_thrdq);
489 result = Curl_thrdq_create(&multi->resolv_thrdq, "DNS",
490 min_threads, max_threads, idle_time_ms,
491 async_thrdd_item_free,
492 async_thrdd_item_process,
493 async_thrdd_event,
494 multi);
495#ifdef DEBUGBUILD
496 if(!result) {
497 const char *p = getenv("CURL_DBG_RESOLV_MAX_THREADS");
498 if(p) {
499 curl_off_t l;
500 if(!curlx_str_number(&p, &l, UINT32_MAX)) {
501 result = Curl_async_thrdd_multi_set_props(
502 multi, min_threads, (uint32_t)l, idle_time_ms);
503 }
504 }
505 }
506#endif
507 return result;
508}
510/* Tear down the thread queue, joining active threads or detaching them */
511void Curl_async_thrdd_multi_destroy(struct Curl_multi *multi, bool join)
512{
513 if(multi->resolv_thrdq) {
514#ifdef CURLVERBOSE
515 CURL_TRC_DNS(multi->admin, "destroy thread queue+pool, join=%d", join);
516 CURL_THRDQ_TRACE(multi->resolv_thrdq, multi->admin);
517#endif
518 Curl_thrdq_destroy(multi->resolv_thrdq, join);
519 multi->resolv_thrdq = NULL;
520 }
521}
523#ifdef CURLVERBOSE
524static void async_thrdd_report_item(struct Curl_easy *data,
525 struct async_thrdd_item *item)
526{
527 char buf[MAX_IPADR_LEN];
528 struct dynbuf tmp;
529 const char *sep = "";
530 const struct Curl_addrinfo *ai = item->res;
531 CURLcode result;
532 int ai_family;
533#ifdef USE_IPV6
534 ai_family = (item->dns_queries & CURL_DNSQ_AAAA) ? AF_INET6 : AF_INET;
535#else
536 ai_family = AF_INET;
537#endif
539 if(!CURL_TRC_DNS_is_verbose(data))
540 return;
542 curlx_dyn_init(&tmp, 1024);
543 for(; ai; ai = ai->ai_next) {
544 if(ai->ai_family == ai_family) {
545 Curl_printable_address(ai, buf, sizeof(buf));
546 result = curlx_dyn_addf(&tmp, "%s%s", sep, buf);
547 if(result) {
548 CURL_TRC_DNS(data, "too many IP, cannot show");
549 goto out;
550 }
551 sep = ", ";
552 }
553 }
555 CURL_TRC_DNS(data, "Host %s:%u resolved IPv%c: %s",
556 item->hostname, item->port,
557 (item->dns_queries & CURL_DNSQ_AAAA) ? '6' : '4',
558 (curlx_dyn_len(&tmp) ? curlx_dyn_ptr(&tmp) : "(none)"));
559out:
560 curlx_dyn_free(&tmp);
561}
562#endif /* CURLVERBOSE */
564/* Process the receiving end of the thread queue, dispatching
565 * processed items to their transfer when it can still be found
566 * and has an `async` state present. Otherwise, destroy the item. */
567void Curl_async_thrdd_multi_process(struct Curl_multi *multi)
568{
569 struct Curl_easy *data;
570 void *qitem;
572 while(!Curl_thrdq_recv(multi->resolv_thrdq, &qitem)) {
573 /* dispatch resolve result */
574 struct async_thrdd_item *item = qitem;
575 struct Curl_resolv_async *async = NULL;
577 data = Curl_multi_get_easy(multi, item->mid);
578 if(data)
579 async = Curl_async_get(data, item->resolv_id);
580 if(async) {
581 struct async_thrdd_item **pdest = &async->thrdd.res_A;
583#ifdef CURLRES_IPV6
584 if(item->dns_queries & CURL_DNSQ_AAAA)
585 pdest = &async->thrdd.res_AAAA;
586#endif
587 if(!*pdest) {
588 *pdest = item;
589 item = NULL;
590 }
591 else
592 DEBUGASSERT(0); /* should not receive duplicates here */
594 --async->queries_ongoing;
595 Curl_multi_mark_dirty(data);
596 }
597 async_thrdd_item_free(item);
598 }
599 CURL_THRDQ_TRACE(multi->resolv_thrdq, multi->admin);
600}
602CURLcode Curl_async_thrdd_multi_set_props(struct Curl_multi *multi,
603 uint32_t min_threads,
604 uint32_t max_threads,
605 uint32_t idle_time_ms)
606{
607 return Curl_thrdq_set_props(multi->resolv_thrdq,
608 min_threads, max_threads, idle_time_ms);
609}
611static CURLcode async_thrdd_query(struct Curl_easy *data,
612 struct Curl_resolv_async *async,
613 uint8_t dns_queries)
614{
615 struct async_thrdd_item *item;
616 CURLcode result;
618 item = async_thrdd_item_create(data, async->id, dns_queries,
619 async->peer->hostname, async->peer->port,
620 async->transport);
621 if(!item) {
622 result = CURLE_OUT_OF_MEMORY;
623 goto out;
624 }
625 CURL_TRC_DNS(data, "queueing query %s", item->description);
626 result = Curl_thrdq_send(data->multi->resolv_thrdq, item,
627 async_item_description(item), async->timeout_ms);
628 if(result)
629 goto out;
630 item = NULL;
631 async->queries_ongoing++;
633out:
634 if(item)
635 async_thrdd_item_free(item);
636 return result;
637}
639CURLcode Curl_async_getaddrinfo(struct Curl_easy *data,
640 struct Curl_resolv_async *async)
641{
642 CURLcode result = CURLE_FAILED_INIT;
643 void *resolver = NULL;
645 if(async->queries_ongoing || async->done)
646 return CURLE_FAILED_INIT;
648#ifdef USE_HTTPSRR_ARES
649 DEBUGASSERT(!async->thrdd.rr.channel);
650 if((async->dns_queries & CURL_DNSQ_HTTPS) && !async->is_ipaddr) {
651 result = async_rr_init(data, async);
652 if(result)
653 goto out;
654 resolver = async->thrdd.rr.channel;
655 }
656#endif
658 result = Curl_resolv_announce_start(data, resolver);
659 if(result)
660 return result;
662#ifdef USE_HTTPSRR_ARES
663 if(resolver) {
664 result = async_rr_start(data, async);
665 if(result)
666 goto out;
667 }
668#endif
670#ifdef CURLRES_IPV6
671 /* Do not start an AAAA query for an IPv4 address when
672 * we will start an A query for it. */
673 if((async->dns_queries & CURL_DNSQ_AAAA) &&
674 !(async->is_ipv4addr && (async->dns_queries & CURL_DNSQ_A))) {
675 result = async_thrdd_query(data, async, CURL_DNSQ_AAAA);
676 if(result)
677 goto out;
678 }
679#endif
680 if(async->dns_queries & CURL_DNSQ_A) {
681 result = async_thrdd_query(data, async, CURL_DNSQ_A);
682 if(result)
683 goto out;
684 }
686 CURL_THRDQ_TRACE(data->multi->resolv_thrdq, data);
688out:
689 if(!async->queries_ongoing)
690 async->done = TRUE;
692 if(result)
693 CURL_TRC_DNS(data, "error queueing query %s:%d -> %d",
694 async->peer->hostname, async->peer->port, (int)result);
695 return result;
696}
698CURLcode Curl_async_pollset(struct Curl_easy *data,
699 struct Curl_resolv_async *async,
700 struct easy_pollset *ps)
701{
702 timediff_t timeout_ms;
704 timeout_ms = Curl_async_timeleft_ms(data, async);
705#ifdef USE_HTTPSRR_ARES
706 if(async->thrdd.rr.channel) {
707 CURLcode result = Curl_ares_pollset(data, async->thrdd.rr.channel, ps);
708 if(result)
709 return result;
710 timeout_ms = Curl_ares_timeout_ms(data, async, async->thrdd.rr.channel);
711 }
712#else
713 (void)ps;
714#endif
716 if(!async->done) {
717 const struct curltime *pnow = Curl_pgrs_now(data);
718#ifndef ENABLE_INTERNAL_WAKEUP
719 timediff_t stutter_ms, elapsed_ms;
720 elapsed_ms = curlx_ptimediff_ms(pnow, &async->start);
721 if(elapsed_ms < 3)
722 stutter_ms = 1;
723 else if(elapsed_ms <= 50)
724 stutter_ms = elapsed_ms / 3;
725 else if(elapsed_ms <= 250)
726 stutter_ms = 50;
727 else
728 stutter_ms = 200;
729 timeout_ms = CURLMIN(stutter_ms, timeout_ms);
730#else
731 if(async->queries_ongoing &&
732 !Curl_thrdq_check_started(data->multi->resolv_thrdq)) {
733 /* The queue has items but starting a worker thread to process
734 them just failed again; expire soon to check once more,
735 instead of sleeping on the full resolve timeout. */
736 CURL_TRC_DNS(data, "resolver thread start failed again, retrying");
737 timeout_ms = CURLMIN(100, timeout_ms);
738 }
739#endif
740 Curl_expire_set(data, EXPIRE_ASYNC_NAME, timeout_ms, pnow);
741 }
742 return CURLE_OK;
743}
745#if defined(USE_IPV6) && defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
746static bool async_thrdd_item_missing_scope(struct Curl_easy *data,
747 struct async_thrdd_item *item)
748{
749 const struct Curl_addrinfo *ai;
751 /* scope id already globally set */
752 if(data->conn && data->conn->scope_id)
753 return FALSE;
755 for(ai = item->res; ai; ai = ai->ai_next) {
756 if(ai->ai_family == AF_INET6) {
757 struct sockaddr_in6 *sa6 = (void *)ai->ai_addr;
758 if(IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) && !sa6->sin6_scope_id)
759 return TRUE;
760 }
761 }
762 return FALSE;
763}
765static void async_thrdd_item_strip_results(struct async_thrdd_item *item,
766 int ai_family)
767{
768 struct Curl_addrinfo *ai = item->res, **panchor = &item->res;
769 while(ai) {
770 if(ai->ai_family == ai_family) {
771 *panchor = ai->ai_next;
772 ai->ai_next = NULL;
773 Curl_freeaddrinfo(ai);
774 ai = *panchor;
775 }
776 else {
777 panchor = &ai->ai_next;
778 ai = ai->ai_next;
779 }
780 }
781}
783#endif /* USE_IPV6 && HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID */
785static CURLcode async_thrdd_check_done(struct Curl_easy *data,
786 struct Curl_resolv_async *async)
787{
788 struct async_thrdd_ctx *thrdd = &async->thrdd;
790 (void)data;
791 if(thrdd->res_A && !thrdd->processed_A) {
792 VERBOSE(async_thrdd_report_item(data, thrdd->res_A));
793 /* move addrinfos to the async result member */
794 async->dns_responses |= thrdd->res_A->dns_queries;
795 async->ai_A = thrdd->res_A->res;
796 thrdd->res_A->res = NULL;
797 thrdd->processed_A = TRUE;
798 }
800 if(thrdd->res_AAAA && !thrdd->processed_AAAA) {
801#if defined(USE_IPV6) && defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
802 /* do we accept the incoming AAAA response? */
803 if(!(thrdd->res_AAAA->dns_queries & CURL_DNSQ_A) &&
804 async_thrdd_item_missing_scope(data, thrdd->res_AAAA)) {
805 /* We queried "only" AF_INET6. This may be problematic when the
806 * result has ipv6 link-local addresses and did not give
807 * any scope id for it. glibc has a long outstanding bug
808 * <https://sourceware.org/bugzilla/show_bug.cgi?id=14413>
809 * that gives scope ids only on AF_UNSPEC queries. */
810 struct async_thrdd_item *item = thrdd->res_AAAA;
811 CURLcode result;
813 /* Reuse the item and queue it again, this time with added
814 * CURL_DNSQ_A which resolves using AF_UNSPEC. */
815 thrdd->res_AAAA = NULL;
816 item->dns_queries |= CURL_DNSQ_A;
817 if(item->res) {
818 Curl_freeaddrinfo(item->res);
819 item->res = NULL;
820 }
822 CURL_TRC_DNS(data, "re-queueing query %s for AF_UNSPEC resolve",
823 item->description);
824 result = Curl_thrdq_send(data->multi->resolv_thrdq, item,
825 async_item_description(item),
826 async->timeout_ms);
827 if(result) {
828 async_thrdd_item_free(item);
829 return result;
830 }
831 async->queries_ongoing++;
832 return CURLE_AGAIN;
833 }
834 /* accepting the AAAA result, strip it of any AF_INET entries,
835 * as we might have resolved it with AF_UNSPEC. */
836 async_thrdd_item_strip_results(thrdd->res_AAAA, AF_INET);
837#endif /* USE_IPV6 && HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID */
838 VERBOSE(async_thrdd_report_item(data, thrdd->res_AAAA));
839 /* move addrinfos to the async result member */
840 async->dns_responses |= thrdd->res_AAAA->dns_queries;
841 async->ai_AAAA = thrdd->res_AAAA->res;
842 thrdd->res_AAAA->res = NULL;
843 thrdd->processed_AAAA = TRUE;
844 }
846 if(async->queries_ongoing)
847 return CURLE_AGAIN;
848 async->done = TRUE;
849 return CURLE_OK;
850}
852/*
853 * Curl_async_take_result() is called repeatedly to check if a previous
854 * name resolve request has completed. It should also make sure to time-out if
855 * the operation seems to take too long.
856 */
857CURLcode Curl_async_take_result(struct Curl_easy *data,
858 struct Curl_resolv_async *async,
859 struct Curl_dns_entry **pdns)
860{
861 struct async_thrdd_ctx *thrdd = &async->thrdd;
862 struct Curl_dns_entry *dns = NULL;
863 CURLcode result = CURLE_OK;
865 DEBUGASSERT(pdns);
866 *pdns = NULL;
868#ifdef USE_HTTPSRR_ARES
869 /* best effort, ignore errors */
870 if(thrdd->rr.channel)
871 (void)Curl_ares_perform(thrdd->rr.channel, 0);
872#endif
873#ifndef ENABLE_INTERNAL_WAKEUP
874 Curl_async_thrdd_multi_process(data->multi);
875#endif
877 result = async_thrdd_check_done(data, async);
878 if(result)
879 return result;
881 Curl_expire_clear(data, EXPIRE_ASYNC_NAME);
883 /* A failure is an authoritative negative answer, eligible for
884 negative caching, only when every A/AAAA query performed came
885 back answering that the name does not exist. A query that
886 failed transiently or never returned is not an answer. */
887 {
888 const uint8_t ip_queries =
889 async->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA);
890 bool negative = (async->dns_responses & ip_queries) == ip_queries;
891 if(thrdd->res_A && (async->ai_A || !thrdd->res_A->negative))
892 negative = FALSE;
893 if(thrdd->res_AAAA && (async->ai_AAAA || !thrdd->res_AAAA->negative))
894 negative = FALSE;
895 if(!thrdd->res_A && !thrdd->res_AAAA)
896 negative = FALSE;
897 async->negative_answer = negative;
898 }
900 if(async->result) {
901 result = async->result;
902 goto out;
903 }
905 if(async->ai_A || async->ai_AAAA) {
906 dns = Curl_dnsc_mk_addr2(
907 data, async->dns_queries, &async->ai_A, &async->ai_AAAA, async->peer);
908 if(!dns) {
909 result = CURLE_OUT_OF_MEMORY;
910 goto out;
911 }
912 }
914#ifdef USE_HTTPSRR_ARES
915 if(!dns && thrdd->rr.channel) {
916 Curl_httpsrr_trace(data, async->httpsrr);
917 dns = Curl_dnsc_mk_https(data, &async->httpsrr, async->peer);
918 if(!dns) {
919 result = CURLE_OUT_OF_MEMORY;
920 goto out;
921 }
922 }
923#endif
925 if(dns) {
926 *pdns = dns;
927 dns = NULL;
928 }
929 CURL_THRDQ_TRACE(data->multi->resolv_thrdq, data);
931out:
932 Curl_dns_entry_unlink(data, &dns);
933 Curl_async_thrdd_shutdown(data, async);
934 if(!result && !*pdns)
935 result = Curl_async_failed(data, async, NULL);
936 if(result &&
937 (result != CURLE_COULDNT_RESOLVE_HOST) &&
938 (result != CURLE_COULDNT_RESOLVE_PROXY)) {
939 CURL_TRC_DNS(data, "Error %d resolving %s:%d",
940 (int)result, async->peer->hostname, async->peer->port);
941 }
942 return result;
943}
945#endif /* USE_RESOLV_THREADED */
Diagnostics4 git calls · 27 ms
CallStatusSizeTime027 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2008.1 KiB · hit21 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=8272 git.response.cache=HIT git.response.duration_ms=21 git.fetch.wants=e5086c4ebb24bcf225e854b3376e9e4aeba2cd89 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=8272 git.pkt.payload.max=8251 git.sideband.band1.bytes=8250 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e9f0045a0b0acb
- Request
- d5134890…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format