Artifactspublic-v0

curl

master
ply4 git calls374 ms295 KiB2 cachedprotocol v2
1040 lines30 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 "urldata.h"
44#include "curl_addrinfo.h"
45#include "curl_share.h"
46#include "curl_trc.h"
47#include "hash.h"
48#include "progress.h"
49#include "rand.h"
50#include "strcase.h"
51#include "vdns/dnscache.h"
52#include "vdns/hostip.h"
53#include "vdns/httpsrr.h"
54#include "curlx/inet_ntop.h"
55#include "curlx/inet_pton.h"
56#include "curlx/strcopy.h"
57#include "curlx/strparse.h"
59#define MAX_DNS_CACHE_SIZE 29999
61/* @unittest 3217 */
62UNITTEST void dnsc_peer2id(struct dnsc_id *pid, char type,
63 struct Curl_peer *peer);
64UNITTEST void dnsc_peer2id(struct dnsc_id *pid, char type,
65 struct Curl_peer *peer)
67 curlx_str_assign(&pid->name, peer->hostname, strlen(peer->hostname));
68 pid->port = peer->port;
69 pid->type = type;
72static void dnsc_str2id(struct dnsc_id *pid, char type,
73 struct Curl_str *name, uint16_t port)
75 pid->name = *name;
76 pid->port = port;
77 pid->type = type;
80/*
81 * Create a hostcache id string for the provided host + port, to be used by
82 * the DNS caching. Without alloc. Return length of the id string.
83 *
84 * @unittest 3217
85 */
86UNITTEST void dnsc_id2key(struct dnsc_key *key, struct dnsc_id *id);
87UNITTEST void dnsc_id2key(struct dnsc_key *key, struct dnsc_id *id)
89 size_t namelen = curlx_strlen(&id->name);
90 if(namelen > (MAX_HOSTCACHE_LEN - 3))
91 namelen = MAX_HOSTCACHE_LEN - 3;
92 /* store and lower case the name */
93 key->data[0] = id->type;
94 key->data[1] = (uint8_t)((id->port >> 8) & 0xff);
95 key->data[2] = (uint8_t)(id->port & 0xff);
96 Curl_strntolower((char *)key->data + 3, curlx_str(&id->name), namelen);
97 key->len = namelen + 3;
100/* @unittest 3271 */
101UNITTEST bool dnsc_id_matches_entry(struct dnsc_id *id,
102 struct Curl_dns_entry *dns);
103UNITTEST bool dnsc_id_matches_entry(struct dnsc_id *id,
104 struct Curl_dns_entry *dns)
106 return dns &&
107 /* Neither the requested nor the cached key was truncated. */
108 (((dns->hostlen <= (MAX_HOSTCACHE_LEN - 3)) &&
109 (id->name.len <= (MAX_HOSTCACHE_LEN - 3))) ||
110 /* port and host string are the same, case insensitive */
111 ((id->port == dns->port) && (id->name.len == dns->hostlen) &&
112 /* for most lookups, case is the same, try that first */
113 (!strcmp(id->name.str, dns->hostname) ||
114 curl_strequal(id->name.str, dns->hostname))));
117static void dnscache_entry_free(struct Curl_dns_entry *dns)
119 Curl_freeaddrinfo(dns->addr);
120#ifdef USE_HTTPSRR
121 Curl_httpsrr_destroy(dns->hinfo);
122#endif
123 curlx_free(dns);
126struct dnscache_prune_data {
127 const struct curltime *pnow;
128 timediff_t oldest_ms; /* oldest time in cache not pruned. */
129 timediff_t max_age_ms;
130};
132/*
133 * This function is set as a callback to be called for every entry in the DNS
134 * cache when we want to prune old unused entries.
135 *
136 * Returning non-zero means remove the entry, return 0 to keep it in the
137 * cache.
138 */
139static int dnscache_entry_is_stale(void *datap, void *hc)
141 struct dnscache_prune_data *prune = (struct dnscache_prune_data *)datap;
142 struct Curl_dns_entry *dns = (struct Curl_dns_entry *)hc;
144 if(!dns->permanent) {
145 /* get age in milliseconds */
146 timediff_t age_ms = curlx_ptimediff_ms(prune->pnow, &dns->added);
147 switch(dns->type) {
148 case CURL_DNST_ADDR:
149 if(!dns->addr)
150 age_ms *= 2; /* negative entries age twice as fast */
151 break;
152#ifdef USE_HTTPSRR
153 case CURL_DNST_HTTPS:
154 if(!dns->hinfo)
155 age_ms *= 2; /* negative entries age twice as fast */
156 break;
157#endif
158 default:
159 break;
160 }
161 if(age_ms >= prune->max_age_ms)
162 return TRUE;
163 if(age_ms > prune->oldest_ms)
164 prune->oldest_ms = age_ms;
165 }
166 return FALSE;
169/*
170 * Prune the DNS cache. This assumes that a lock has already been taken.
171 * Returns the 'age' of the oldest still kept entry - in milliseconds.
172 */
173static timediff_t dnscache_prune(struct Curl_hash *hostcache,
174 timediff_t cache_timeout_ms,
175 const struct curltime *pnow)
177 struct dnscache_prune_data user;
179 user.max_age_ms = cache_timeout_ms;
180 user.pnow = pnow;
181 user.oldest_ms = 0;
183 Curl_hash_clean_with_criterium(hostcache,
184 (void *)&user,
185 dnscache_entry_is_stale);
187 return user.oldest_ms;
190static struct Curl_dnscache *dnscache_get(struct Curl_easy *data)
192 if(data->share && data->share->specifier & (1 << CURL_LOCK_DATA_DNS))
193 return &data->share->dnscache;
194 if(data->multi)
195 return &data->multi->dnscache;
196 return NULL;
199static void dnscache_lock(struct Curl_easy *data,
200 struct Curl_dnscache *dnscache)
202 if(data->share && dnscache == &data->share->dnscache)
203 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
206static void dnscache_unlock(struct Curl_easy *data,
207 struct Curl_dnscache *dnscache)
209 if(data->share && dnscache == &data->share->dnscache)
210 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
213/*
214 * Library-wide function for pruning the DNS cache. This function takes and
215 * returns the appropriate locks.
216 */
217void Curl_dnscache_prune(struct Curl_easy *data, const struct curltime *pnow)
219 struct Curl_dnscache *dnscache = dnscache_get(data);
220 /* the timeout may be set -1 (forever) */
221 timediff_t timeout_ms = data->set.dns_cache_timeout_ms;
223 if(!dnscache || (timeout_ms == -1))
224 /* NULL hostcache means we cannot do it */
225 return;
227 dnscache_lock(data, dnscache);
229 do {
230 /* Remove outdated and unused entries from the hostcache */
231 timediff_t oldest_ms =
232 dnscache_prune(&dnscache->entries, timeout_ms, pnow);
234 if(Curl_hash_count(&dnscache->entries) > MAX_DNS_CACHE_SIZE)
235 /* prune the ones over half this age */
236 timeout_ms = oldest_ms / 2;
237 else
238 break;
240 /* if the cache size is still too big, use the oldest age as new prune
241 limit */
242 } while(timeout_ms);
244 dnscache_unlock(data, dnscache);
247void Curl_dnscache_clear(struct Curl_easy *data)
249 struct Curl_dnscache *dnscache = dnscache_get(data);
250 if(dnscache) {
251 dnscache_lock(data, dnscache);
252 Curl_hash_clean(&dnscache->entries);
253 dnscache_unlock(data, dnscache);
254 }
257/* lookup address, returns entry if found and not stale */
258static CURLcode fetch_entry(struct Curl_easy *data,
259 struct Curl_dnscache *dnscache,
260 uint8_t dns_queries,
261 struct Curl_peer *peer,
262 struct Curl_dns_entry **pdns)
264 struct Curl_dns_entry *dns = NULL;
265 struct dnsc_id id;
266 struct dnsc_key key;
267 char type = CURL_DNSQ_IS_ADDR(dns_queries) ?
268 CURL_DNST_ADDR : CURL_DNST_HTTPS;
269 CURLcode result = CURLE_OK;
271 *pdns = NULL;
272 if(!dnscache)
273 return CURLE_OK;
275 dnsc_peer2id(&id, type, peer);
276 dnsc_id2key(&key, &id);
278 /* See if it is already in our dns cache */
279 dns = Curl_hash_pick(&dnscache->entries, key.data, key.len);
281 if(dns && !dnsc_id_matches_entry(&id, dns)) {
282 /* May happen on hostnames longer than MAX_HOSTCACHE_LEN */
283 dns = NULL;
284 }
286 /* No entry found in cache, check if we might have a wildcard entry */
287 if(!dns && (type == CURL_DNST_ADDR) && data->state.wildcard_resolve) {
288 struct Curl_str wildname;
290 curlx_str_assign(&wildname, "*", 1);
291 dnsc_str2id(&id, CURL_DNST_ADDR, &wildname, peer->port);
292 dnsc_id2key(&key, &id);
294 /* See if it is already in our dns cache */
295 dns = Curl_hash_pick(&dnscache->entries, key.data, key.len);
296 }
298 if(dns && (data->set.dns_cache_timeout_ms != -1)) {
299 /* See whether the returned entry is stale. Done before we release lock */
300 struct dnscache_prune_data user;
302 user.pnow = Curl_pgrs_now(data);
303 user.max_age_ms = data->set.dns_cache_timeout_ms;
304 user.oldest_ms = 0;
306 if(dnscache_entry_is_stale(&user, dns)) {
307 infof(data, "Hostname in DNS cache was stale, zapped");
308 dns = NULL; /* the memory deallocation is being handled by the hash */
309 Curl_hash_delete(&dnscache->entries, key.data, key.len);
310 }
311 }
313 /* We need to cache address information and HTTPS-RR separately. */
314 if(dns && CURL_DNSQ_IS_ADDR(dns_queries)) {
315 if((uint8_t)(dns->dns_queries & dns_queries) !=
316 (uint8_t)(dns_queries & CURL_DNSQ_ADDR)) {
317 /* The entry does not cover all wanted address queries, a miss. */
318 dns = NULL;
319 }
320 else if(!(dns->dns_responses & dns_queries)) {
321 /* The entry has no responses for the wanted DNS queries. */
322 CURL_TRC_DNS(data, "cache entry does not have type=%s addresses",
323 Curl_resolv_query_str(dns_queries));
324 dns = NULL;
325 result = CURLE_COULDNT_RESOLVE_HOST;
326 }
327 else if(dns && !dns->addr) { /* negative entry */
328 dns = NULL;
329 result = CURLE_COULDNT_RESOLVE_HOST;
330 }
331 }
333 *pdns = dns;
334 return result;
337/*
338 * Curl_dnscache_get() fetches a 'Curl_dns_entry' already in the DNS cache.
339 *
340 * Curl_resolv() checks initially and multi_runsingle() checks each time
341 * it discovers the handle in the state WAITRESOLVE whether the hostname
342 * has already been resolved and the address has already been stored in
343 * the DNS cache. This short circuits waiting for a lot of pending
344 * lookups for the same hostname requested by different handles.
345 *
346 * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
347 *
348 * The returned data *MUST* be "released" with Curl_dns_entry_unlink() after
349 * use, or we will leak memory!
350 */
351CURLcode Curl_dnscache_get(struct Curl_easy *data,
352 uint8_t dns_queries,
353 struct Curl_peer *peer,
354 struct Curl_dns_entry **pentry)
356 struct Curl_dnscache *dnscache = dnscache_get(data);
357 struct Curl_dns_entry *dns = NULL;
358 CURLcode result = CURLE_OK;
360 dnscache_lock(data, dnscache);
361 result = fetch_entry(data, dnscache, dns_queries, peer, &dns);
362 if(!result && dns)
363 dns->refcount++; /* we pass out a reference */
364 else if(result) {
365 DEBUGASSERT(!dns);
366 dns = NULL;
367 }
368 dnscache_unlock(data, dnscache);
370 CURL_TRC_DNS(data, "cache lookup %s:%u queries=%s -> %d %sfound",
371 peer->hostname, peer->port, Curl_resolv_query_str(dns_queries),
372 (int)result, dns ? "" : "not ");
373 *pentry = dns;
374 return result;
377#ifndef CURL_DISABLE_SHUFFLE_DNS
378/*
379 * Return # of addresses in a Curl_addrinfo struct
380 */
381static int num_addresses(const struct Curl_addrinfo *addr)
383 int i = 0;
384 while(addr) {
385 addr = addr->ai_next;
386 i++;
387 }
388 return i;
391/*
392 * dns_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo'
393 * struct by re-linking its linked list.
394 *
395 * The addr argument should be the address of a pointer to the head node of a
396 * `Curl_addrinfo` list and it will be modified to point to the new head after
397 * shuffling.
398 *
399 * Not declared static only to make it easy to use in a unit test!
400 *
401 * @unittest 1608
402 */
403UNITTEST CURLcode dns_shuffle_addr(struct Curl_easy *data,
404 struct Curl_addrinfo **addr);
405UNITTEST CURLcode dns_shuffle_addr(struct Curl_easy *data,
406 struct Curl_addrinfo **addr)
408 CURLcode result = CURLE_OK;
409 const int num_addrs = num_addresses(*addr);
411 if(num_addrs > 1) {
412 struct Curl_addrinfo **nodes;
413 CURL_TRC_DNS(data, "Shuffling %d addresses", num_addrs);
415 nodes = curlx_malloc(num_addrs * sizeof(*nodes));
416 if(nodes) {
417 int i;
418 unsigned int *rnd;
419 const size_t rnd_size = num_addrs * sizeof(*rnd);
421 /* build a plain array of Curl_addrinfo pointers */
422 nodes[0] = *addr;
423 for(i = 1; i < num_addrs; i++) {
424 nodes[i] = nodes[i - 1]->ai_next;
425 }
427 rnd = curlx_malloc(rnd_size);
428 if(rnd) {
429 /* Fisher-Yates shuffle */
430 if(Curl_rand(data, (unsigned char *)rnd, rnd_size) == CURLE_OK) {
431 struct Curl_addrinfo *swap_tmp;
432 for(i = num_addrs - 1; i > 0; i--) {
433 swap_tmp = nodes[rnd[i] % (unsigned int)(i + 1)];
434 nodes[rnd[i] % (unsigned int)(i + 1)] = nodes[i];
435 nodes[i] = swap_tmp;
436 }
438 /* relink list in the new order */
439 for(i = 1; i < num_addrs; i++) {
440 nodes[i - 1]->ai_next = nodes[i];
441 }
443 nodes[num_addrs - 1]->ai_next = NULL;
444 *addr = nodes[0];
445 }
446 curlx_free(rnd);
447 }
448 else
449 result = CURLE_OUT_OF_MEMORY;
450 curlx_free(nodes);
451 }
452 else
453 result = CURLE_OUT_OF_MEMORY;
454 }
455 return result;
457#endif
459static bool dnscache_ai_has_family(struct Curl_addrinfo *ai, int ai_family)
461 for(; ai; ai = ai->ai_next) {
462 if(ai->ai_family == ai_family)
463 return TRUE;
464 }
465 return FALSE;
468static struct Curl_dns_entry *dnsc_entry_create(struct dnsc_id *pid,
469 bool permanent)
471 struct Curl_dns_entry *dns = NULL;
473 if(curlx_strlen(&pid->name) > UINT16_MAX)
474 goto out;
476 /* Create a new cache entry,
477 struct already has the hostname null-terminator */
478 dns = curlx_calloc(1, sizeof(struct Curl_dns_entry) +
479 curlx_strlen(&pid->name));
480 if(!dns)
481 goto out;
483 dns->refcount = 1; /* the cache has the first reference */
484 dns->port = pid->port;
485 dns->hostlen = (uint16_t)curlx_strlen(&pid->name);
486 if(dns->hostlen)
487 memcpy(dns->hostname, curlx_str(&pid->name), dns->hostlen);
488 dns->permanent = permanent;
490out:
491 return dns;
494static struct Curl_dns_entry *dnsc_entry_assign_addr(
495 struct Curl_easy *data,
496 struct Curl_dns_entry *dns,
497 uint8_t dns_queries,
498 struct Curl_addrinfo **paddr1,
499 struct Curl_addrinfo **paddr2)
501 if(!dns)
502 goto out;
503 /* only do this when this is the only reference */
504 DEBUGASSERT(dns->refcount == 1);
505 DEBUGASSERT(dns->type == CURL_DNST_INIT);
507 dns->type = CURL_DNST_ADDR;
508 /* queries should only be about addresses */
509 DEBUGASSERT(!(dns_queries & ~CURL_DNSQ_ADDR));
510 dns->dns_queries = (dns_queries & CURL_DNSQ_ADDR);
512 /* Take the given address lists into the entry */
513 if(paddr1 && *paddr1) {
514 dns->addr = *paddr1;
515 *paddr1 = NULL;
516 }
517 if(paddr2 && *paddr2) {
518 struct Curl_addrinfo **phead = &dns->addr;
519 while(*phead)
520 phead = &(*phead)->ai_next;
521 *phead = *paddr2;
522 *paddr2 = NULL;
523 }
525 if((dns_queries & CURL_DNSQ_A) &&
526 dnscache_ai_has_family(dns->addr, PF_INET))
527 dns->dns_responses |= CURL_DNSQ_A;
529#ifdef USE_IPV6
530 if((dns_queries & CURL_DNSQ_AAAA) &&
531 dnscache_ai_has_family(dns->addr, PF_INET6))
532 dns->dns_responses |= CURL_DNSQ_AAAA;
533#endif /* USE_IPV6 */
535#ifndef CURL_DISABLE_SHUFFLE_DNS
536 /* shuffle addresses if requested */
537 if(data->set.dns_shuffle_addresses && dns->addr) {
538 CURLcode result = dns_shuffle_addr(data, &dns->addr);
539 if(result) {
540 /* free without lock, we are the sole owner */
541 dnscache_entry_free(dns);
542 dns = NULL;
543 goto out;
544 }
545 }
546#else
547 (void)data;
548#endif
550out:
551 if(paddr1 && *paddr1) {
552 Curl_freeaddrinfo(*paddr1);
553 *paddr1 = NULL;
554 }
555 if(paddr2 && *paddr2) {
556 Curl_freeaddrinfo(*paddr2);
557 *paddr2 = NULL;
558 }
559 return dns;
562struct Curl_dns_entry *Curl_dnsc_mk_addr(struct Curl_easy *data,
563 uint8_t dns_queries,
564 struct Curl_addrinfo **paddr,
565 struct Curl_peer *peer)
567 struct dnsc_id id;
568 struct Curl_dns_entry *dns;
570 dnsc_peer2id(&id, CURL_DNST_ADDR, peer);
571 dns = dnsc_entry_create(&id, FALSE);
572 dns = dnsc_entry_assign_addr(data, dns, dns_queries, paddr, NULL);
573 return dns;
576struct Curl_dns_entry *Curl_dnsc_mk_addr2(struct Curl_easy *data,
577 uint8_t dns_queries,
578 struct Curl_addrinfo **paddr1,
579 struct Curl_addrinfo **paddr2,
580 struct Curl_peer *peer)
582 struct dnsc_id id;
583 struct Curl_dns_entry *dns;
585 dnsc_peer2id(&id, CURL_DNST_ADDR, peer);
586 dns = dnsc_entry_create(&id, FALSE);
587 dns = dnsc_entry_assign_addr(data, dns, dns_queries, paddr1, paddr2);
588 return dns;
591static struct Curl_dns_entry *dnsc_add_entry(struct Curl_easy *data,
592 struct Curl_dnscache *dnscache,
593 struct dnsc_key *key,
594 struct Curl_dns_entry *entry)
596 entry->added = *Curl_pgrs_now(data);
597 return Curl_hash_add(&dnscache->entries, key->data, key->len, entry);
600#ifdef USE_HTTPSRR
601static struct Curl_dns_entry *dnsc_entry_assign_https(
602 struct Curl_dns_entry *dns,
603 struct Curl_https_rrinfo **phinfo)
605 if(!dns)
606 goto out;
607 /* only do this when this is the only reference */
608 DEBUGASSERT(dns->refcount == 1);
609 DEBUGASSERT(dns->type == CURL_DNST_INIT);
611 if(dns->hinfo) {
612 Curl_httpsrr_destroy(dns->hinfo);
613 dns->hinfo = NULL;
614 }
615 dns->type = CURL_DNST_HTTPS;
616 dns->dns_responses = dns->dns_queries = CURL_DNSQ_HTTPS;
617 if(phinfo) {
618 dns->hinfo = *phinfo;
619 *phinfo = NULL;
620 }
621out:
622 if(phinfo && *phinfo) {
623 Curl_httpsrr_destroy(*phinfo);
624 *phinfo = NULL;
625 }
626 return dns;
629struct Curl_dns_entry *Curl_dnsc_mk_https(struct Curl_easy *data,
630 struct Curl_https_rrinfo **phinfo,
631 struct Curl_peer *peer)
633 struct dnsc_id id;
634 struct Curl_dns_entry *dns;
636 (void)data;
637 dnsc_peer2id(&id, CURL_DNST_HTTPS, peer);
638 dns = dnsc_entry_create(&id, FALSE);
639 dns = dnsc_entry_assign_https(dns, phinfo);
640 return dns;
643static struct Curl_dns_entry *dnsc_add_https(struct Curl_easy *data,
644 struct Curl_dnscache *dnscache,
645 struct Curl_https_rrinfo **phinfo,
646 struct dnsc_id *id)
648 struct Curl_dns_entry *dns, *dns2;
649 struct dnsc_key key;
651 dns = dnsc_entry_create(id, FALSE);
652 dns = dnsc_entry_assign_https(dns, phinfo);
653 if(!dns)
654 return NULL;
656 /* Store the resolved data in our DNS cache. */
657 dnsc_id2key(&key, id);
658 dns2 = dnsc_add_entry(data, dnscache, &key, dns);
659 if(!dns2) {
660 dnscache_entry_free(dns);
661 return NULL;
662 }
664 dns = dns2;
665 dns->refcount++; /* mark entry as in-use */
666 return dns;
668#endif /* USE_HTTPSRR */
670static struct Curl_dns_entry *dnsc_add_addr(struct Curl_easy *data,
671 struct Curl_dnscache *dnscache,
672 uint8_t dns_queries,
673 struct Curl_addrinfo **paddr,
674 struct dnsc_id *id,
675 struct dnsc_key *key,
676 bool permanent)
678 struct Curl_dns_entry *dns;
679 struct Curl_dns_entry *dns2;
681 dns = dnsc_entry_create(id, permanent);
682 dns = dnsc_entry_assign_addr(data, dns, dns_queries, paddr, NULL);
683 if(!dns)
684 return NULL;
686 /* Store the resolved data in our DNS cache. */
687 dns2 = dnsc_add_entry(data, dnscache, key, dns);
688 if(!dns2) {
689 dnscache_entry_free(dns);
690 return NULL;
691 }
693 dns = dns2;
694 dns->refcount++; /* mark entry as in-use */
695 return dns;
698static struct Curl_dns_entry *dnsc_add_peer_addr(
699 struct Curl_easy *data,
700 struct Curl_dnscache *dnscache,
701 uint8_t dns_queries,
702 struct Curl_addrinfo **paddr,
703 struct dnsc_id *id,
704 bool permanent)
706 struct Curl_dns_entry *dns;
707 struct Curl_dns_entry *dns2;
708 struct dnsc_key key;
710 dns = dnsc_entry_create(id, permanent);
711 dns = dnsc_entry_assign_addr(data, dns, dns_queries, paddr, NULL);
712 if(!dns)
713 return NULL;
715 /* Store the resolved data in our DNS cache. */
716 dnsc_id2key(&key, id);
717 dns2 = dnsc_add_entry(data, dnscache, &key, dns);
718 if(!dns2) {
719 dnscache_entry_free(dns);
720 return NULL;
721 }
723 dns = dns2;
724 dns->refcount++; /* mark entry as in-use */
725 return dns;
728CURLcode Curl_dnscache_add(struct Curl_easy *data,
729 struct Curl_dns_entry *entry)
731 struct Curl_dnscache *dnscache = dnscache_get(data);
732 struct Curl_str name;
733 struct dnsc_id id;
734 struct dnsc_key key;
736 if(!dnscache)
737 return CURLE_FAILED_INIT;
738 if(!entry || (entry->type == CURL_DNST_INIT))
739 return CURLE_BAD_FUNCTION_ARGUMENT;
741 curlx_str_assign(&name, entry->hostname, entry->hostlen);
742 dnsc_str2id(&id, entry->type, &name, entry->port);
743 dnsc_id2key(&key, &id);
745 /* Store the resolved data in our DNS cache and up ref count */
746 dnscache_lock(data, dnscache);
747 if(!dnsc_add_entry(data, dnscache, &key, entry)) {
748 dnscache_unlock(data, dnscache);
749 return CURLE_OUT_OF_MEMORY;
750 }
751 entry->refcount++;
752 dnscache_unlock(data, dnscache);
753 CURL_TRC_DNS(data, "cached entry for %s:%u queries=%s",
754 entry->hostname, entry->port,
755 Curl_resolv_query_str(entry->dns_queries));
756 return CURLE_OK;
759CURLcode Curl_dnscache_add_negative(struct Curl_easy *data,
760 uint8_t dns_queries,
761 struct Curl_peer *peer)
763 struct Curl_dnscache *dnscache = dnscache_get(data);
764 struct Curl_dns_entry *dns = NULL;
765 struct dnsc_id id;
766 CURLcode result = CURLE_OK;
768 DEBUGASSERT(dnscache);
769 if(!dnscache)
770 return CURLE_FAILED_INIT;
772 dnscache_lock(data, dnscache);
774 if(dns_queries & CURL_DNSQ_ADDR) {
775 /* put this new host in the cache */
776 dnsc_peer2id(&id, CURL_DNST_ADDR, peer);
777 dns = dnsc_add_peer_addr(data, dnscache, dns_queries, NULL, &id, FALSE);
778 if(!dns)
779 result = CURLE_OUT_OF_MEMORY;
780 }
781#ifdef USE_HTTPSRR
782 else if(dns_queries == CURL_DNSQ_HTTPS) {
783 dnsc_peer2id(&id, CURL_DNST_HTTPS, peer);
784 dns = dnsc_add_https(data, dnscache, NULL, &id);
785 if(!dns)
786 result = CURLE_OUT_OF_MEMORY;
787 }
788#endif
789 else {
790 /* a query we do not know, just cache nothing */
791 DEBUGASSERT(0);
792 }
794 if(dns) {
795 /* release the returned reference; the cache itself will keep the
796 * entry alive: */
797 dns->refcount--;
798 CURL_TRC_DNS(data, "cache negative name resolve for %s:%d type=%s",
799 peer->hostname, peer->port,
800 Curl_resolv_query_str(dns_queries));
801 }
802 dnscache_unlock(data, dnscache);
803 return result;
806/*
807 * Curl_dns_entry_unlink() releases a reference to the given cached DNS entry.
808 * When the reference count reaches 0, the entry is destroyed. It is important
809 * that only one unlink is made for each Curl_resolv() call.
810 *
811 * May be called with 'data' == NULL for global cache.
812 */
813void Curl_dns_entry_unlink(struct Curl_easy *data,
814 struct Curl_dns_entry **pdns)
816 if(*pdns) {
817 struct Curl_dnscache *dnscache = dnscache_get(data);
818 struct Curl_dns_entry *dns = *pdns;
819 *pdns = NULL;
820 dnscache_lock(data, dnscache);
821 dns->refcount--;
822 if(dns->refcount == 0)
823 dnscache_entry_free(dns);
824 dnscache_unlock(data, dnscache);
825 }
828static void dnscache_entry_dtor(void *entry)
830 struct Curl_dns_entry *dns = (struct Curl_dns_entry *)entry;
831 DEBUGASSERT(dns && (dns->refcount > 0));
832 dns->refcount--;
833 if(dns->refcount == 0)
834 dnscache_entry_free(dns);
837/*
838 * Curl_dnscache_init() inits a new DNS cache.
839 */
840void Curl_dnscache_init(struct Curl_dnscache *dns, size_t size)
842 Curl_hash_init(&dns->entries, size, CURL_HASH_TYPE_BYTES,
843 dnscache_entry_dtor);
846void Curl_dnscache_destroy(struct Curl_dnscache *dns)
848 Curl_hash_destroy(&dns->entries);
851CURLcode Curl_loadhostpairs(struct Curl_easy *data)
853 struct Curl_dnscache *dnscache = dnscache_get(data);
854 struct curl_slist *hostp;
855 struct dnsc_id id;
856 struct dnsc_key key;
858 if(!dnscache)
859 return CURLE_FAILED_INIT;
861 /* Default is no wildcard found */
862 data->state.wildcard_resolve = FALSE;
864 for(hostp = data->state.resolve; hostp; hostp = hostp->next) {
865 const char *host = hostp->data;
866 struct Curl_str source;
867 if(!host)
868 continue;
869 if(*host == '-') {
870 curl_off_t num = 0;
871 host++;
872 if(!curlx_str_single(&host, '[')) {
873 if(curlx_str_until(&host, &source, MAX_IPADR_LEN, ']') ||
874 curlx_str_single(&host, ']') ||
875 curlx_str_single(&host, ':'))
876 continue;
877 }
878 else {
879 if(curlx_str_until(&host, &source, 4096, ':') ||
880 curlx_str_single(&host, ':')) {
881 continue;
882 }
883 }
885 if(!curlx_str_number(&host, &num, 0xffff)) {
886 /* Create an entry id, based upon the hostname and port */
887 dnsc_str2id(&id, CURL_DNST_ADDR, &source, (uint16_t)num);
888 dnsc_id2key(&key, &id);
889 dnscache_lock(data, dnscache);
890 /* delete entry, ignore if it did not exist */
891 Curl_hash_delete(&dnscache->entries, key.data, key.len);
892 dnscache_unlock(data, dnscache);
893 }
894 }
895 else {
896 struct Curl_dns_entry *dns;
897 struct Curl_addrinfo *head = NULL, *tail = NULL;
898 char address[64];
899 curl_off_t tmpofft = 0;
900 uint16_t port = 0;
901 bool permanent = TRUE;
902 bool error = TRUE;
903 VERBOSE(const char *addresses = NULL);
905 if(*host == '+') {
906 host++;
907 permanent = FALSE;
908 }
909 if(!curlx_str_single(&host, '[')) {
910 if(curlx_str_until(&host, &source, MAX_IPADR_LEN, ']') ||
911 curlx_str_single(&host, ']'))
912 continue;
913 }
914 else {
915 if(curlx_str_until(&host, &source, 4096, ':'))
916 continue;
917 }
918 if(curlx_str_single(&host, ':') ||
919 curlx_str_number(&host, &tmpofft, 0xffff) ||
920 curlx_str_single(&host, ':'))
921 goto err;
922 port = (uint16_t)tmpofft;
924 VERBOSE(addresses = host);
926 /* start the address section */
927 while(*host) {
928 struct Curl_str target;
929 struct Curl_addrinfo *ai;
930 CURLcode result;
932 if(!curlx_str_single(&host, '[')) {
933 if(curlx_str_until(&host, &target, MAX_IPADR_LEN, ']') ||
934 curlx_str_single(&host, ']'))
935 goto err;
936 }
937 else {
938 if(curlx_str_until(&host, &target, 4096, ',')) {
939 if(curlx_str_single(&host, ','))
940 goto err;
941 /* survive nothing but a comma */
942 continue;
943 }
944 }
945#ifndef USE_IPV6
946 if(memchr(curlx_str(&target), ':', curlx_strlen(&target))) {
947 infof(data, "Ignoring resolve address '%.*s', missing IPv6 support.",
948 (int)curlx_strlen(&target), curlx_str(&target));
949 if(curlx_str_single(&host, ','))
950 goto err;
951 continue;
952 }
953#endif
955 if(curlx_strlen(&target) >= sizeof(address))
956 goto err;
958 memcpy(address, curlx_str(&target), curlx_strlen(&target));
959 address[curlx_strlen(&target)] = '\0';
961 result = Curl_str2addr(address, port, &ai);
962 if(result) {
963 infof(data, "Resolve IP address '%s' found is illegal", address);
964 goto err;
965 }
967 if(tail) {
968 tail->ai_next = ai;
969 tail = tail->ai_next;
970 }
971 else {
972 head = tail = ai;
973 }
974 if(curlx_str_single(&host, ','))
975 break;
976 }
978 if(!head)
979 goto err;
981 error = FALSE;
982err:
983 if(error) {
984 failf(data, "Could not parse CURLOPT_RESOLVE entry '%s'", hostp->data);
985 Curl_freeaddrinfo(head);
986 return CURLE_SETOPT_OPTION_SYNTAX;
987 }
989 dnsc_str2id(&id, CURL_DNST_ADDR, &source, port);
990 dnsc_id2key(&key, &id);
991 dnscache_lock(data, dnscache);
993 /* See if it is already in our dns cache */
994 dns = Curl_hash_pick(&dnscache->entries, key.data, key.len);
996 if(dns) {
997 infof(data, "RESOLVE %.*s:%u - old addresses discarded",
998 (int)curlx_strlen(&source), curlx_str(&source), port);
999 /* delete old entry, there are two reasons for this
1000 1. old entry may have different addresses.
1001 2. even if entry with correct addresses is already in the cache,
1002 but if it is close to expire, then by the time next http
1003 request is made, it can get expired and pruned because old
1004 entry is not necessarily marked as permanent.
1005 3. when adding a non-permanent entry, we want it to remove and
1006 replace an existing permanent entry.
1007 4. when adding a non-permanent entry, we want it to get a "fresh"
1008 timeout that starts _now_. */
1010 Curl_hash_delete(&dnscache->entries, key.data, key.len);
1013 /* put this new host in the cache, override all address queries */
1014 dns = dnsc_add_addr(data, dnscache, CURL_DNSQ_ADDR, &head,
1015 &id, &key, permanent);
1016 if(dns)
1017 /* release the returned reference; the cache itself will keep the
1018 * entry alive: */
1019 dns->refcount--;
1021 dnscache_unlock(data, dnscache);
1023 if(!dns)
1024 return CURLE_OUT_OF_MEMORY;
1026 infof(data, "[DNS] added %.*s:%u:%s to cache%s",
1027 (int)curlx_strlen(&id.name), curlx_str(&id.name), id.port,
1028 addresses, permanent ? "" : " (non-permanent)");
1030 /* Wildcard hostname */
1031 if(curlx_str_casecompare(&source, "*")) {
1032 infof(data, "RESOLVE *:%u using wildcard", port);
1033 data->state.wildcard_resolve = TRUE;
1037 data->state.resolve = NULL; /* dealt with now */
1039 return CURLE_OK;
Diagnostics4 git calls · 374 ms
CallStatusSizeTime0374 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
fetch2008.5 KiB368 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=368
git.response.bytes=8734
git.response.cache=MISS
git.response.duration_ms=368
git.fetch.wants=303e11b18c05858387cb719e83d27e124ab490ee
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=8734
git.pkt.payload.max=8713
git.sideband.band1.bytes=8712
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
a5e9ef7b2beb0acb
Request
4e8b1350…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format