Artifactspublic-v0

curl

master
ply4 git calls276 ms298 KiB2 cachedprotocol v2
1183 lines36 KiBRaw
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#ifdef HAVE_NETINET_IN_H
27#include <netinet/in.h>
28#endif
29#ifdef HAVE_NETINET_IN6_H
30#include <netinet/in6.h>
31#endif
32#ifdef HAVE_NETDB_H
33#include <netdb.h>
34#endif
35#ifdef HAVE_ARPA_INET_H
36#include <arpa/inet.h>
37#endif
38#ifdef __VMS
39#include <in.h>
40#include <inet.h>
41#endif
43#include <setjmp.h> /* for sigjmp_buf, sigsetjmp() */
44#include <signal.h>
46#include "urldata.h"
47#include "curl_addrinfo.h"
48#include "curl_trc.h"
49#include "url.h"
50#include "multiif.h"
51#include "progress.h"
52#include "select.h"
53#include "strcase.h"
54#include "easy_lock.h"
55#include "vdns/dnscache.h"
56#include "vdns/doh.h"
57#include "vdns/hostip.h"
58#include "vdns/httpsrr.h"
59#include "curlx/inet_ntop.h"
60#include "curlx/inet_pton.h"
61#include "curlx/strcopy.h"
62#include "curlx/strparse.h"
64#if defined(CURLRES_SYNCH) && \
65 defined(HAVE_ALARM) && \
66 defined(SIGALRM) && \
67 defined(HAVE_SIGSETJMP) && \
68 defined(GLOBAL_INIT_IS_THREADSAFE)
69/* alarm-based timeouts can only be used with all the dependencies satisfied */
70#define USE_ALARM_TIMEOUT
71#endif
73#define RESOLV_FAIL(for_proxy) \
74 ((for_proxy) ? CURLE_COULDNT_RESOLVE_PROXY : CURLE_COULDNT_RESOLVE_HOST)
76#define IS_RESOLV_FAIL(result) \
77 (((result) == CURLE_COULDNT_RESOLVE_HOST) || \
78 ((result) == CURLE_COULDNT_RESOLVE_PROXY))
79/*
80 * ipv6works() returns TRUE if IPv6 seems to work.
81 */
82#ifdef USE_IPV6
83static bool ipv6works(struct Curl_easy *data);
84#else
85#define ipv6works(x) FALSE
86#endif
88/*
89 * hostip.c explained
90 * ==================
91 *
92 * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
93 * source file are these:
94 *
95 * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
96 * that. The host may not be able to resolve IPv6, but we do not really have to
97 * take that into account. Hosts that are not IPv6-enabled have CURLRES_IPV4
98 * defined.
99 *
100 * USE_RESOLV_ARES - is defined if libcurl is built to use c-ares for
101 * asynchronous name resolves. This can be Windows or *nix.
102 *
103 * USE_RESOLV_THREADED - is defined if libcurl is built to run under (native)
104 * Windows, and then the name resolve will be done in a new thread, and the
105 * supported API will be the same as for ares-builds.
106 *
107 * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
108 * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
109 * defined.
110 *
111 * The host*.c sources files are split up like this:
112 *
113 * hostip.c - method-independent resolver functions and utility functions
114 * hostip4.c - IPv4 specific functions
115 * hostip6.c - IPv6 specific functions
116 * asyn.h - common functions for all async resolvers
117 * The two asynchronous name resolver backends are implemented in:
118 * asyn-ares.c - async resolver using c-ares
119 * asyn-thread.c - async resolver using POSIX threads
120 *
121 * The hostip.h is the united header file for all this. It defines the
122 * CURLRES_* defines based on the config*.h and curl_setup.h defines.
123 */
125uint8_t Curl_resolv_dns_queries(struct Curl_easy *data, uint8_t ip_version)
127 (void)data;
128 switch(ip_version) {
129 case CURL_IPRESOLVE_V6:
130 return CURL_DNSQ_AAAA;
131 case CURL_IPRESOLVE_V4:
132 return CURL_DNSQ_A;
133 default:
134 if(ipv6works(data))
135 return (CURL_DNSQ_A | CURL_DNSQ_AAAA);
136 else
137 return CURL_DNSQ_A;
138 }
141#ifdef CURLVERBOSE
142const char *Curl_resolv_query_str(uint8_t dns_queries)
144 switch(dns_queries) {
145 case (CURL_DNSQ_A | CURL_DNSQ_AAAA | CURL_DNSQ_HTTPS):
146 return "A+AAAA+HTTPS";
147 case (CURL_DNSQ_A | CURL_DNSQ_AAAA):
148 return "A+AAAA";
149 case (CURL_DNSQ_AAAA | CURL_DNSQ_HTTPS):
150 return "AAAA+HTTPS";
151 case (CURL_DNSQ_AAAA):
152 return "AAAA";
153 case (CURL_DNSQ_A | CURL_DNSQ_HTTPS):
154 return "A+HTTPS";
155 case (CURL_DNSQ_A):
156 return "A";
157 case (CURL_DNSQ_HTTPS):
158 return "HTTPS";
159 case 0:
160 return "-";
161 default:
162 DEBUGASSERT(0);
163 return "???";
164 }
166#endif
168/*
169 * Curl_printable_address() stores a printable version of the 1st address
170 * given in the 'ai' argument. The result will be stored in the buf that is
171 * bufsize bytes big.
172 *
173 * If the conversion fails, the target buffer is empty.
174 */
175void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf,
176 size_t bufsize)
178 DEBUGASSERT(bufsize);
179 buf[0] = 0;
181 switch(ai->ai_family) {
182 case AF_INET: {
183 const struct sockaddr_in *sa4 = (const void *)ai->ai_addr;
184 const struct in_addr *ipaddr4 = &sa4->sin_addr;
185 (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
186 break;
187 }
188#ifdef USE_IPV6
189 case AF_INET6: {
190 const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr;
191 const struct in6_addr *ipaddr6 = &sa6->sin6_addr;
192 (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
193 break;
194 }
195#endif
196 default:
197 break;
198 }
201#ifdef USE_ALARM_TIMEOUT
202/* Beware this is a global and unique instance. This is used to store the
203 return address that we can jump back to from inside a signal handler. This
204 is not thread-safe stuff. */
205static sigjmp_buf curl_jmpenv;
206static curl_simple_lock curl_jmpenv_lock = CURL_SIMPLE_LOCK_INIT;
207#endif
209#ifdef USE_IPV6
210/* return a static IPv6 ::1 for the name */
211static struct Curl_addrinfo *get_localhost6(uint16_t port, const char *name)
213 struct Curl_addrinfo *ca;
214 const size_t ss_size = sizeof(struct sockaddr_in6);
215 const size_t hostlen = strlen(name);
216 struct sockaddr_in6 sa6;
217 unsigned char ipv6[16];
218 unsigned short port16 = (unsigned short)(port & 0xffff);
219 ca = curlx_calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1);
220 if(!ca)
221 return NULL;
223 memset(&sa6, 0, sizeof(sa6));
224 sa6.sin6_family = AF_INET6;
225 sa6.sin6_port = htons(port16);
227 (void)curlx_inet_pton(AF_INET6, "::1", ipv6);
228 memcpy(&sa6.sin6_addr, ipv6, sizeof(ipv6));
230 ca->ai_flags = 0;
231 ca->ai_family = AF_INET6;
232 ca->ai_socktype = SOCK_STREAM;
233 ca->ai_protocol = IPPROTO_TCP;
234 ca->ai_addrlen = (curl_socklen_t)ss_size;
235 ca->ai_next = NULL;
236 ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
237 memcpy(ca->ai_addr, &sa6, ss_size);
238 ca->ai_canonname = (char *)ca->ai_addr + ss_size;
239 curlx_strcopy(ca->ai_canonname, hostlen + 1, name, hostlen);
240 return ca;
242#else
243#define get_localhost6(x, y) NULL
244#endif
246/* return a static IPv4 127.0.0.1 for the given name */
247static struct Curl_addrinfo *get_localhost(uint16_t port, const char *name)
249 struct Curl_addrinfo *ca;
250 struct Curl_addrinfo *ca6;
251 const size_t ss_size = sizeof(struct sockaddr_in);
252 const size_t hostlen = strlen(name);
253 struct sockaddr_in sa;
254 unsigned int ipv4;
255 unsigned short port16 = (unsigned short)(port & 0xffff);
257 /* memset to clear the sa.sin_zero field */
258 memset(&sa, 0, sizeof(sa));
259 sa.sin_family = AF_INET;
260 sa.sin_port = htons(port16);
261 if(curlx_inet_pton(AF_INET, "127.0.0.1", (char *)&ipv4) < 1)
262 return NULL;
263 memcpy(&sa.sin_addr, &ipv4, sizeof(ipv4));
265 ca = curlx_calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1);
266 if(!ca)
267 return NULL;
268 ca->ai_flags = 0;
269 ca->ai_family = AF_INET;
270 ca->ai_socktype = SOCK_STREAM;
271 ca->ai_protocol = IPPROTO_TCP;
272 ca->ai_addrlen = (curl_socklen_t)ss_size;
273 ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
274 memcpy(ca->ai_addr, &sa, ss_size);
275 ca->ai_canonname = (char *)ca->ai_addr + ss_size;
276 curlx_strcopy(ca->ai_canonname, hostlen + 1, name, hostlen);
278 ca6 = get_localhost6(port, name);
279 if(!ca6)
280 return ca;
281 ca6->ai_next = ca;
282 return ca6;
285#ifdef USE_IPV6
286/* the nature of most systems is that IPv6 status does not come and go during a
287 program's lifetime so we only probe the first time and then we have the
288 info kept for fast reuse */
289CURLcode Curl_probeipv6(struct Curl_multi *multi)
291 /* probe to see if we have a working IPv6 stack */
292 curl_socket_t s = CURL_SOCKET(PF_INET6, SOCK_DGRAM, 0);
293 multi->ipv6_works = FALSE;
294 if(s == CURL_SOCKET_BAD) {
295 if(SOCKERRNO == SOCKENOMEM)
296 return CURLE_OUT_OF_MEMORY;
297 }
298 else {
299 multi->ipv6_works = TRUE;
300 sclose(s);
301 }
302 return CURLE_OK;
305/*
306 * ipv6works() returns TRUE if IPv6 seems to work.
307 */
308static bool ipv6works(struct Curl_easy *data)
310 DEBUGASSERT(data);
311 DEBUGASSERT(data->multi);
312 return data ? data->multi->ipv6_works : FALSE;
314#endif /* USE_IPV6 */
316/*
317 * Curl_host_is_ipnum() returns TRUE if the given string is a numerical IPv4
318 * (or IPv6 if supported) address.
319 */
320bool Curl_host_is_ipnum(const char *hostname)
322 struct in_addr in;
323#ifdef USE_IPV6
324 struct in6_addr in6;
325#endif
326 if(curlx_inet_pton(AF_INET, hostname, &in) > 0
327#ifdef USE_IPV6
328 || curlx_inet_pton(AF_INET6, hostname, &in6) > 0
329#endif
330 )
331 return TRUE;
332 return FALSE;
335/* return TRUE if 'part' is a case insensitive tail of 'full' */
336static bool tailmatch(const char *full, size_t flen,
337 const char *part, size_t plen)
339 if(plen > flen)
340 return FALSE;
341 return curl_strnequal(part, &full[flen - plen], plen);
344static CURLcode hostip_resolv_failed(struct Curl_easy *data,
345 struct Curl_peer *peer,
346 bool for_proxy)
348 failf(data, "Could not resolve %s: %s",
349 for_proxy ? "proxy" : "host", peer->hostname);
350 return RESOLV_FAIL(for_proxy);
353static bool can_resolve_dns_queries(struct Curl_easy *data,
354 uint8_t dns_queries)
356 (void)data;
357 if((CURL_DNSQ_IS_ADDR(dns_queries) == CURL_DNSQ_AAAA) && !ipv6works(data))
358 return FALSE;
359 return TRUE;
362CURLcode Curl_resolv_announce_start(struct Curl_easy *data,
363 void *resolver)
365 if(data->set.resolver_start) {
366 struct Curl_mapi_guard guard;
367 int rc;
369 CURL_TRC_DNS(data, "announcing resolve to application");
370 CURL_CBAPI_START(&guard, data, easy_resolver_start);
371 rc = data->set.resolver_start(resolver, NULL,
372 data->set.resolver_start_client);
373 CURL_CBAPI_END(&guard);
374 if(rc) {
375 CURL_TRC_DNS(data, "application aborted resolve");
376 return CURLE_ABORTED_BY_CALLBACK;
377 }
378 }
379 return CURLE_OK;
382#ifdef USE_CURL_ASYNC
384static struct Curl_resolv_async *hostip_async_new(struct Curl_easy *data,
385 uint8_t dns_queries,
386 struct Curl_peer *peer,
387 uint8_t transport,
388 bool for_proxy,
389 timediff_t timeout_ms)
391 struct Curl_resolv_async *async;
393 if(!data->multi) {
394 DEBUGASSERT(0);
395 return NULL;
396 }
398 /* struct size already includes the null-terminator for hostname */
399 async = curlx_calloc(1, sizeof(*async));
400 if(!async)
401 return NULL;
403 /* Give every async resolve operation a "unique" id. This may
404 * wrap around after a long time, making collisions highly unlikely.
405 * As we keep the async structs at the easy handle, chances of
406 * easy `mid plus resolv->id` colliding should be astronomical.
407 * `resolv_id == 0` is never used. */
408 if(data->multi->last_resolv_id == UINT32_MAX)
409 data->multi->last_resolv_id = 1; /* wrap around */
410 else
411 data->multi->last_resolv_id++;
412 async->id = data->multi->last_resolv_id;
413 Curl_peer_link(&async->peer, peer);
414 async->dns_queries = dns_queries;
415 async->transport = transport;
416 async->for_proxy = for_proxy;
417 async->start = *Curl_pgrs_now(data);
418 async->timeout_ms = timeout_ms;
419 async->is_ipaddr = Curl_is_ipaddr(peer->hostname);
420 if(async->is_ipaddr)
421 async->is_ipv4addr = Curl_is_ipv4addr(peer->hostname);
423 return async;
426static CURLcode hostip_resolv_take_result(struct Curl_easy *data,
427 struct Curl_resolv_async *async,
428 struct Curl_dns_entry **pdns)
430 CURLcode result;
432 /* If async resolving is ongoing, this must be set */
433 if(!async)
434 return CURLE_FAILED_INIT;
436#ifndef CURL_DISABLE_DOH
437 if(async->doh)
438 result = Curl_doh_take_result(data, async, pdns);
439 else
440#endif
441 result = Curl_async_take_result(data, async, pdns);
443 if(result == CURLE_AGAIN) {
444 CURL_TRC_DNS(data, "[%s] resolve incomplete, responses=%s, "
445 "ongoing=%d for %s:%d",
446 Curl_resolv_query_str(async->dns_queries),
447 Curl_resolv_query_str(async->dns_responses),
448 async->queries_ongoing,
449 async->peer->hostname, async->peer->port);
450 result = CURLE_OK;
451 }
452 else if(IS_RESOLV_FAIL(result)) {
453 result = Curl_async_failed(data, async, NULL);
454 }
455 else if(result) {
456 /* a local failure, not a resolve answer. Keep the error as it
457 is so it does not get treated as one. */
458 CURL_TRC_DNS(data, "[%s] resolve error %d for %s:%u",
459 Curl_resolv_query_str(async->dns_queries),
460 (int)result, async->peer->hostname, async->peer->port);
461 }
462 else {
463 CURL_TRC_DNS(data, "[%s] resolve complete for %s:%u",
464 Curl_resolv_query_str(async->dns_queries),
465 async->peer->hostname, async->peer->port);
466 DEBUGASSERT(*pdns);
467 }
469 return result;
472timediff_t Curl_resolv_elapsed_ms(struct Curl_easy *data,
473 uint32_t resolv_id,
474 const struct curltime *pnow)
476 struct Curl_resolv_async *async = Curl_async_get(data, resolv_id);
477 if(!async)
478 return CURL_TIMEOUT_RESOLVE_MS;
479 return curlx_ptimediff_ms(pnow, &async->start);
482bool Curl_resolv_has_answers(struct Curl_easy *data,
483 uint32_t resolv_id, uint8_t dns_queries)
485 struct Curl_resolv_async *async = Curl_async_get(data, resolv_id);
486 uint8_t check_queries;
487 /* a no longer existing/running resolve has all answers. */
488 if(!async || async->done)
489 return TRUE;
490 /* Relevant are only queries undertaken. Others are considered answered. */
491 check_queries = (dns_queries & async->dns_queries);
492 if((check_queries & async->dns_responses) != check_queries) {
493 return FALSE;
494 }
495 return TRUE;
498const struct Curl_addrinfo *Curl_resolv_get_ai(struct Curl_easy *data,
499 uint32_t resolv_id,
500 int ai_family,
501 unsigned int index)
503 struct Curl_resolv_async *async = Curl_async_get(data, resolv_id);
504 if(!async || !CURL_DNSQ_IS_ADDR(async->dns_queries))
505 return NULL;
506 switch(ai_family) {
507 case AF_INET:
508 return Curl_addrinfo_get(async->ai_A, ai_family, index);
509#ifdef USE_IPV6
510 case AF_INET6:
511 return Curl_addrinfo_get(async->ai_AAAA, ai_family, index);
512#endif
513 default:
514 return NULL;
515 }
518#ifdef USE_HTTPSRR
520CURLcode Curl_resolv_https(struct Curl_easy *data,
521 struct Curl_peer *peer,
522 bool for_proxy,
523 timediff_t timeout_ms,
524 uint32_t *presolv_id,
525 struct Curl_dns_entry **pdns)
527 return Curl_resolv(data, peer, CURL_DNSQ_HTTPS, TRNSPRT_TCP,
528 for_proxy, timeout_ms, presolv_id, pdns);
531const struct Curl_https_rrinfo *
532Curl_resolv_get_https(struct Curl_easy *data, uint32_t resolv_id)
534 struct Curl_resolv_async *async;
535 if(!Curl_resolv_knows_https(data, resolv_id))
536 return NULL;
537 async = Curl_async_get(data, resolv_id);
538 return async ? async->httpsrr : NULL;
541bool Curl_resolv_knows_https(struct Curl_easy *data, uint32_t resolv_id)
543 struct Curl_resolv_async *async = Curl_async_get(data, resolv_id);
544 if(async && (async->dns_queries & CURL_DNSQ_HTTPS))
545 return ((async->dns_responses & CURL_DNSQ_HTTPS) ||
546 !async->queries_ongoing);
547 return TRUE; /* we know it will never come */
550#endif /* USE_HTTPSRR */
552#endif /* USE_CURL_ASYNC */
554/* Start resolving. `*pnegative` is only meaningful when this returns
555 a CURLE_COULDNT_RESOLVE_* failure: TRUE when the resolver answered
556 that the name does not exist, FALSE on transient or local failures
557 that must not be cached as negative entries. */
558static CURLcode hostip_resolv_start(struct Curl_easy *data,
559 uint8_t dns_queries,
560 struct Curl_peer *peer,
561 uint8_t transport,
562 bool for_proxy,
563 timediff_t timeout_ms,
564 bool allowDOH,
565 uint32_t *presolv_id,
566 struct Curl_dns_entry **pdns,
567 bool *pnegative)
569#ifdef USE_CURL_ASYNC
570 struct Curl_resolv_async *async = NULL;
571#endif
572 struct Curl_addrinfo *addr = NULL;
573 size_t hostname_len;
574 bool addr_queries = (dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA));
575 CURLcode result = CURLE_OK;
577 *pnegative = FALSE;
579 (void)timeout_ms; /* not in all ifdefs */
580 *presolv_id = 0;
581 *pdns = NULL;
583 /* Check for "known" things to resolve ourselves. */
584 if(addr_queries) {
585#ifndef USE_RESOLVE_ON_IPS
586 if(Curl_is_ipaddr(peer->hostname)) {
587 /* test655 verifies that the announce is done, even though there
588 * is no real resolving. So, keep doing this. */
589 result = Curl_resolv_announce_start(data, NULL);
590 if(result)
591 goto out;
592 /* shortcut literal IP addresses, if we are not told to resolve them. */
593 result = Curl_str2addr(peer->hostname, peer->port, &addr);
594 goto out;
595 }
596#endif
598 hostname_len = strlen(peer->hostname);
599 if(curl_strequal(peer->hostname, "localhost") ||
600 curl_strequal(peer->hostname, "localhost.") ||
601 tailmatch(peer->hostname, hostname_len, STRCONST(".localhost")) ||
602 tailmatch(peer->hostname, hostname_len, STRCONST(".localhost."))) {
603 result = Curl_resolv_announce_start(data, NULL);
604 if(result)
605 goto out;
606 addr = get_localhost(peer->port, peer->hostname);
607 if(!addr)
608 result = CURLE_OUT_OF_MEMORY;
609 goto out;
610 }
611 }
612#ifndef CURL_DISABLE_DOH
613 if(!Curl_is_ipaddr(peer->hostname) && allowDOH && data->set.doh) {
614 result = Curl_resolv_announce_start(data, NULL);
615 if(result)
616 goto out;
617 if(!async) {
618 async = hostip_async_new(data, dns_queries, peer, transport,
619 for_proxy, timeout_ms);
620 if(!async) {
621 result = CURLE_OUT_OF_MEMORY;
622 goto out;
623 }
624 }
625 result = Curl_doh(data, async);
626 goto out;
627 }
628#else
629 (void)allowDOH;
630#endif
632 /* Can we provide the requested IP specifics in resolving? */
633 if(!can_resolve_dns_queries(data, dns_queries)) {
634 result = RESOLV_FAIL(for_proxy);
635 goto out;
636 }
638#ifdef CURLRES_ASYNCH
639 (void)addr;
640 if(!async) {
641 async = hostip_async_new(data, dns_queries, peer, transport,
642 for_proxy, timeout_ms);
643 if(!async) {
644 result = CURLE_OUT_OF_MEMORY;
645 goto out;
646 }
647 }
648 result = Curl_async_getaddrinfo(data, async);
649 if(result == CURLE_AGAIN) {
650 /* the answer might be there already. Check. */
651 CURLcode r2 = hostip_resolv_take_result(data, async, pdns);
652 if(r2)
653 result = r2;
654 else if(*pdns)
655 result = CURLE_OK;
656 }
657#else
658 result = Curl_resolv_announce_start(data, NULL);
659 if(result)
660 goto out;
661 addr = Curl_sync_getaddrinfo(data, dns_queries, peer->hostname, peer->port,
662 transport);
663 if(!addr) {
664 result = RESOLV_FAIL(for_proxy);
665 /* the synchronous resolvers do not tell a transient failure from
666 an authoritative negative answer, treat it as before */
667 *pnegative = TRUE;
668 }
669#endif
671out:
672 if(!result) {
673 if(addr) {
674 /* we got a response, create a dns entry, add to cache, return */
675 DEBUGASSERT(!*pdns);
676 *pdns = Curl_dnsc_mk_addr(data, dns_queries, &addr, peer);
677 if(!*pdns)
678 result = CURLE_OUT_OF_MEMORY;
679 }
680 else if(!*pdns)
681 result = CURLE_AGAIN;
682 }
683 else if(*pdns)
684 Curl_dns_entry_unlink(data, pdns);
685 else if(addr)
686 Curl_freeaddrinfo(addr);
688#ifdef USE_CURL_ASYNC
689 if(async) {
690 if(result == CURLE_AGAIN) { /* still need it, link, return id. */
691 *presolv_id = async->id;
692 async->next = data->state.async;
693 data->state.async = async;
694 }
695 else {
696 *pnegative = !!async->negative_answer;
697 Curl_async_destroy(data, async);
698 }
699 }
700#endif
701 return result;
704static CURLcode hostip_resolv(struct Curl_easy *data,
705 uint8_t dns_queries,
706 struct Curl_peer *peer,
707 uint8_t transport,
708 bool for_proxy,
709 timediff_t timeout_ms,
710 bool allowDOH,
711 uint32_t *presolv_id,
712 struct Curl_dns_entry **pdns)
714 size_t hostname_len;
715 CURLcode result = RESOLV_FAIL(for_proxy);
716 bool cache_dns = FALSE;
717 bool negative = FALSE;
719 (void)timeout_ms; /* not used in all ifdefs */
720 *presolv_id = 0;
721 *pdns = NULL;
723#ifdef CURL_DISABLE_DOH
724 (void)allowDOH;
725#endif
727 /* We should intentionally error and not resolve .onion TLDs */
728 hostname_len = strlen(peer->hostname);
729 DEBUGASSERT(hostname_len);
730 if(hostname_len >= 7 &&
731 (curl_strequal(&peer->hostname[hostname_len - 6], ".onion") ||
732 curl_strequal(&peer->hostname[hostname_len - 7], ".onion."))) {
733 failf(data, "Not resolving .onion address (RFC 7686)");
734 goto out;
735 }
737#ifdef DEBUGBUILD
738 CURL_TRC_DNS(data, "[%s] hostip_resolv(%s:%u)",
739 Curl_resolv_query_str(dns_queries), peer->hostname, peer->port);
740 if((CURL_DNSQ_IS_ADDR(dns_queries) == CURL_DNSQ_AAAA) &&
741 getenv("CURL_DBG_RESOLV_FAIL_IPV6")) {
742 infof(data, "DEBUG fail ipv6 resolve");
743 result = hostip_resolv_failed(data, peer, for_proxy);
744 goto out;
745 }
746#endif
747 /* Let's check our DNS cache first */
748 result = Curl_dnscache_get(data, dns_queries, peer, pdns);
749 if(*pdns) {
750 infof(data, "Hostname %s was found in DNS cache", peer->hostname);
751 result = CURLE_OK;
752 }
753 else if(result) {
754 infof(data, "Negative DNS entry");
755 result = hostip_resolv_failed(data, peer, for_proxy);
756 }
757 else {
758 /* No luck, we need to start resolving. */
759 cache_dns = TRUE;
760 result = hostip_resolv_start(data, dns_queries, peer, transport,
761 for_proxy, timeout_ms, allowDOH,
762 presolv_id, pdns, &negative);
763 CURL_TRC_DNS(data, "[%s] hostip_resolv started -> %d",
764 Curl_resolv_query_str(dns_queries), (int)result);
765 }
767out:
768 if(result && (result != CURLE_AGAIN)) {
769 Curl_dns_entry_unlink(data, pdns);
770 if(IS_RESOLV_FAIL(result)) {
771 if(cache_dns && negative)
772 Curl_dnscache_add_negative(data, dns_queries, peer);
773 if(dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA))
774 failf(data, "Could not resolve: %s:%u", peer->hostname, peer->port);
775 }
776 else {
777 failf(data, "Error %d resolving %s:%u",
778 (int)result, peer->hostname, peer->port);
779 }
780 }
781 else if(cache_dns && *pdns) {
782 result = Curl_dnscache_add(data, *pdns);
783 if(result)
784 Curl_dns_entry_unlink(data, pdns);
785 }
787 return result;
790CURLcode Curl_resolv_blocking(struct Curl_easy *data,
791 uint8_t dns_queries,
792 const char *hostname,
793 uint16_t port,
794 uint8_t transport,
795 struct Curl_dns_entry **pdns)
797 struct Curl_peer *peer = NULL;
798 CURLcode result;
799 uint32_t resolv_id;
801 DEBUGASSERT(hostname && *hostname);
802 *pdns = NULL;
804 result = Curl_peer_create(data, data->conn->scheme, hostname, port, &peer);
805 if(result)
806 goto out;
808 /* We cannot do a blocking resolve using DoH currently */
809 result = hostip_resolv(data, dns_queries, peer, transport, FALSE, 0, FALSE,
810 &resolv_id, pdns);
811 switch(result) {
812 case CURLE_OK:
813 DEBUGASSERT(*pdns);
814 break;
815#ifdef USE_CURL_ASYNC
816 case CURLE_AGAIN:
817 DEBUGASSERT(!*pdns);
818 result = Curl_async_await(data, resolv_id, pdns);
819 Curl_resolv_destroy(data, resolv_id);
820 break;
821#endif
822 default:
823 break;
824 }
826out:
827 Curl_peer_unlink(&peer);
828 return result;
831#ifdef USE_ALARM_TIMEOUT
832/*
833 * This signal handler jumps back into the main libcurl code and continues
834 * execution. This effectively causes the remainder of the application to run
835 * within a signal handler which is nonportable and could lead to problems.
836 */
837CURL_NORETURN static void alarmfunc(int sig)
839 (void)sig;
840 siglongjmp(curl_jmpenv, 1);
843static CURLcode resolv_alarm_timeout(struct Curl_easy *data,
844 uint8_t dns_queries,
845 struct Curl_peer *peer,
846 uint8_t transport,
847 bool for_proxy,
848 timediff_t timeout_ms,
849 uint32_t *presolv_id,
850 struct Curl_dns_entry **entry)
852#ifdef HAVE_SIGACTION
853 struct sigaction keep_sigact; /* store the old struct here */
854 volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */
855 struct sigaction sigact;
856#else
857#ifdef HAVE_SIGNAL
858 void (*keep_sigact)(int); /* store the old handler here */
859#endif /* HAVE_SIGNAL */
860#endif /* HAVE_SIGACTION */
861 volatile long timeout;
862 volatile unsigned int prev_alarm = 0;
863 CURLcode result;
865 DEBUGASSERT(peer->hostname && *peer->hostname);
866 DEBUGASSERT(timeout_ms > 0);
867 DEBUGASSERT(!data->set.no_signal);
868#ifndef CURL_DISABLE_DOH
869 DEBUGASSERT(!data->set.doh);
870#endif
872 *entry = NULL;
873 timeout = (timeout_ms > LONG_MAX) ? LONG_MAX : (long)timeout_ms;
874 if(timeout < 1000) {
875 /* The alarm() function only provides integer second resolution, so if
876 we want to wait less than one second we must bail out already now. */
877 failf(data,
878 "remaining timeout of %ld too small to resolve via SIGALRM method",
879 timeout);
880 return CURLE_OPERATION_TIMEDOUT;
881 }
882 /* This allows us to time-out from the name resolver, as the timeout
883 will generate a signal and we will siglongjmp() from that here.
884 This technique has problems (see alarmfunc).
885 This should be the last thing we do before calling Curl_resolv(),
886 as otherwise we would have to worry about variables that get modified
887 before we invoke Curl_resolv() (and thus use "volatile"). */
888 curl_simple_lock_lock(&curl_jmpenv_lock);
890 if(sigsetjmp(curl_jmpenv, 1)) {
891 /* this is coming from a siglongjmp() after an alarm signal */
892 failf(data, "name lookup timed out");
893 result = CURLE_OPERATION_TIMEDOUT;
894 goto clean_up;
895 }
896 else {
897 /*************************************************************
898 * Set signal handler to catch SIGALRM
899 * Store the old value to be able to set it back later!
900 *************************************************************/
901#ifdef HAVE_SIGACTION
902 sigaction(SIGALRM, NULL, &sigact);
903 keep_sigact = sigact;
904 keep_copysig = TRUE; /* yes, we have a copy */
905 sigact.sa_handler = alarmfunc;
906#ifdef SA_RESTART
907 /* HP-UX does not have SA_RESTART but defaults to that behavior! */
908 sigact.sa_flags &= ~SA_RESTART;
909#endif
910 /* now set the new struct */
911 sigaction(SIGALRM, &sigact, NULL);
912#else /* HAVE_SIGACTION */
913 /* no sigaction(), revert to the much lamer signal() */
914#ifdef HAVE_SIGNAL
915 keep_sigact = signal(SIGALRM, alarmfunc);
916#endif
917#endif /* HAVE_SIGACTION */
919 /* alarm() makes a signal get sent when the timeout fires off, and that
920 will abort system calls */
921 prev_alarm = alarm(curlx_sltoui(timeout / 1000L));
922 }
924 /* Perform the actual name resolution. This might be interrupted by an
925 * alarm if it takes too long. */
926 result = hostip_resolv(data, dns_queries, peer, transport,
927 for_proxy, timeout_ms, FALSE, presolv_id, entry);
929clean_up:
930 if(!prev_alarm)
931 /* deactivate a possibly active alarm before uninstalling the handler */
932 alarm(0);
934#ifdef HAVE_SIGACTION
935 if(keep_copysig) {
936 /* we got a struct as it looked before, now put that one back nice
937 and clean */
938 sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
939 }
940#else
941#ifdef HAVE_SIGNAL
942 /* restore the previous SIGALRM handler */
943 signal(SIGALRM, keep_sigact);
944#endif
945#endif /* HAVE_SIGACTION */
947 curl_simple_lock_unlock(&curl_jmpenv_lock);
949 /* switch back the alarm() to either zero or to what it was before minus
950 the time we spent until now! */
951 if(prev_alarm) {
952 /* there was an alarm() set before us, now put it back */
953 timediff_t elapsed_secs =
954 Curl_cpool_conn_age_ms(data, data->conn, Curl_pgrs_now(data)) / 1000;
956 /* the alarm period is counted in even number of seconds */
957 unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs);
959 if(!alarm_set ||
960 ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000))) {
961 /* if the alarm time-left reached zero or turned "negative" (counted
962 with unsigned values), we should fire off a SIGALRM here, but we
963 will not, and zero would be to switch it off so we never set it to
964 less than 1! */
965 alarm(1);
966 result = CURLE_OPERATION_TIMEDOUT;
967 failf(data, "Previous alarm fired off");
968 }
969 else
970 alarm((unsigned int)alarm_set);
971 }
973 return result;
976#endif /* USE_ALARM_TIMEOUT */
978#ifdef USE_UNIX_SOCKETS
979static CURLcode resolv_unix(struct Curl_easy *data,
980 struct Curl_peer *peer,
981 struct Curl_dns_entry **pdns)
983 struct Curl_addrinfo *addr;
984 CURLcode result;
986 DEBUGASSERT(peer->unix_socket);
987 *pdns = NULL;
989 result = Curl_unix2addr(peer->hostname, (bool)peer->abstract_uds, &addr);
990 if(result) {
991 if(result == CURLE_TOO_LARGE) {
992 /* Long paths are not supported for now */
993 failf(data, "Unix socket path too long: '%s'", peer->hostname);
994 result = CURLE_COULDNT_RESOLVE_HOST;
995 }
996 return result;
997 }
999 *pdns = Curl_dnsc_mk_addr(data, 0, &addr, peer);
1000 return *pdns ? CURLE_OK : CURLE_OUT_OF_MEMORY;
1002#endif /* USE_UNIX_SOCKETS */
1005 * Curl_resolv() is the main name resolve function within libcurl. It resolves
1006 * a name and returns a pointer to the entry in the 'entry' argument. This
1007 * function might return immediately if we are using asynch resolves. See the
1008 * return codes.
1010 * The cache entry we return will get its 'inuse' counter increased when this
1011 * function is used. You MUST call Curl_dns_entry_unlink() later (when you are
1012 * done using this struct) to decrease the reference counter again.
1014 * If built with a synchronous resolver and use of signals is not
1015 * disabled by the application, then a nonzero timeout will cause a
1016 * timeout after the specified number of milliseconds. Otherwise, timeout
1017 * is ignored.
1019 * Return codes:
1020 * CURLE_OK = success, *pdns set to non-NULL
1021 * CURLE_AGAIN = resolving in progress, *pdns == NULL
1022 * any other CURLcode error, *pdns == NULL
1023 */
1024CURLcode Curl_resolv(struct Curl_easy *data,
1025 struct Curl_peer *peer,
1026 uint8_t dns_queries,
1027 uint8_t transport,
1028 bool for_proxy,
1029 timediff_t timeout_ms,
1030 uint32_t *presolv_id,
1031 struct Curl_dns_entry **pdns)
1033 *presolv_id = 0;
1034 *pdns = NULL;
1036 if(timeout_ms < 0)
1037 /* got an already expired timeout */
1038 return CURLE_OPERATION_TIMEDOUT;
1039 else if(!timeout_ms)
1040 timeout_ms = CURL_TIMEOUT_RESOLVE_MS;
1042#ifdef USE_UNIX_SOCKETS
1043 if((dns_queries & CURL_DNSQ_ADDR) && peer->unix_socket)
1044 return resolv_unix(data, peer, pdns);
1045#else
1046 if(peer->unix_socket)
1047 return hostip_resolv_failed(data, peer, for_proxy);
1048#endif
1050#ifdef USE_ALARM_TIMEOUT
1051 if(dns_queries & CURL_DNSQ_ADDR) {
1052 if(timeout_ms && data->set.no_signal) {
1053 /* Cannot use ALARM when signals are disabled */
1054 timeout_ms = 0;
1056 if(timeout_ms && !Curl_doh_wanted(data)) {
1057 return resolv_alarm_timeout(data, dns_queries, peer, transport,
1058 for_proxy, timeout_ms, presolv_id, pdns);
1061#endif /* !USE_ALARM_TIMEOUT */
1063#ifndef CURLRES_ASYNCH
1064 if(timeout_ms)
1065 infof(data, "timeout on name lookup is not supported");
1066#endif
1068 return hostip_resolv(data, dns_queries, peer, transport,
1069 for_proxy, timeout_ms, TRUE, presolv_id, pdns);
1072#ifdef USE_CURL_ASYNC
1074struct Curl_resolv_async *Curl_async_get(struct Curl_easy *data,
1075 uint32_t resolv_id)
1077 struct Curl_resolv_async *async = data->state.async;
1078 for(; async; async = async->next) {
1079 if(async->id == resolv_id)
1080 return async;
1082 return NULL;
1085CURLcode Curl_resolv_take_result(struct Curl_easy *data, uint32_t resolv_id,
1086 struct Curl_dns_entry **pdns)
1088 struct Curl_resolv_async *async = Curl_async_get(data, resolv_id);
1089 CURLcode result;
1091 /* If async resolving is ongoing, this must be set */
1092 if(!async)
1093 return CURLE_FAILED_INIT;
1095 /* check if we have the name resolved by now (from someone else) */
1096 result = Curl_dnscache_get(data, async->dns_queries, async->peer, pdns);
1097 if(*pdns) {
1098 /* Tell a possibly async resolver we no longer need the results. */
1099 infof(data, "Hostname '%s' was found in DNS cache", async->peer->hostname);
1100 Curl_async_shutdown(data, async);
1101 return CURLE_OK;
1103 else if(result) {
1104 Curl_async_shutdown(data, async);
1105 return Curl_async_failed(data, async, NULL);
1108 result = hostip_resolv_take_result(data, async, pdns);
1110 if(*pdns) {
1111 /* Add to cache */
1112 result = Curl_dnscache_add(data, *pdns);
1113 if(result)
1114 Curl_dns_entry_unlink(data, pdns);
1116 else if(IS_RESOLV_FAIL(result)) {
1117 /* Only cache the failure when the resolver answered that the
1118 name does not exist. Transient failures, e.g. an unreachable
1119 or overloaded DNS server or local resource shortages, say
1120 nothing about the name and would poison the cache for every
1121 transfer using it. */
1122 if(async->negative_answer)
1123 Curl_dnscache_add_negative(data, async->dns_queries, async->peer);
1124 if(async->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA))
1125 failf(data, "Could not resolve: %s:%u",
1126 async->peer->hostname, async->peer->port);
1128 else if(result) {
1129 failf(data, "Error %d resolving %s:%u",
1130 (int)result, async->peer->hostname, async->peer->port);
1132 return result;
1135CURLcode Curl_resolv_pollset(struct Curl_easy *data,
1136 struct easy_pollset *ps)
1138 struct Curl_resolv_async *async = data->state.async;
1139 CURLcode result = CURLE_OK;
1141 (void)ps;
1142 for(; async && !result; async = async->next) {
1143#ifndef CURL_DISABLE_DOH
1144 if(async->doh) /* DoH has nothing for the pollset */
1145 continue;
1146#endif
1147 result = Curl_async_pollset(data, async, ps);
1149 return result;
1152void Curl_resolv_destroy(struct Curl_easy *data, uint32_t resolv_id)
1154 struct Curl_resolv_async **panchor = &data->state.async;
1156 for(; *panchor; panchor = &(*panchor)->next) {
1157 struct Curl_resolv_async *async = *panchor;
1158 if(async->id == resolv_id) {
1159 *panchor = async->next;
1160 Curl_async_destroy(data, async);
1161 break;
1166void Curl_resolv_shutdown_all(struct Curl_easy *data)
1168 struct Curl_resolv_async *async = data->state.async;
1169 for(; async; async = async->next) {
1170 Curl_async_shutdown(data, async);
1174void Curl_resolv_destroy_all(struct Curl_easy *data)
1176 while(data->state.async) {
1177 struct Curl_resolv_async *async = data->state.async;
1178 data->state.async = async->next;
1179 Curl_async_destroy(data, async);
1183#endif /* USE_CURL_ASYNC */
Diagnostics4 git calls · 276 ms
CallStatusSizeTime0276 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
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=3
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=86
Open pack map
fetch20011 KiB270 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=270
git.response.bytes=11110
git.response.cache=MISS
git.response.duration_ms=270
git.fetch.wants=222e86b58cb6eff348a68a77307af26e08b94bb7
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=11110
git.pkt.payload.max=11089
git.sideband.band1.bytes=11088
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
a5e9ef3ff87f0acb
Request
d202c397…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format