1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
28 #ifdef HAVE_NETINET_IN6_H
29 #include <netinet/in6.h>
34 #ifdef HAVE_ARPA_INET_H
35 #include <arpa/inet.h>
61 #include "inet_ntop.h"
62 #include "inet_pton.h"
66 /* The last 3 #include files should be in this order */
67 #include "curl_printf.h"
68 #include "curl_memory.h"
71 #if defined(CURLRES_SYNCH) && \
72 defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP)
73 /* alarm-based timeouts can only be used with all the dependencies satisfied */
74 #define USE_ALARM_TIMEOUT
77 #define MAX_HOSTCACHE_LEN (255 + 7) /* max FQDN + colon + port number + zero */
83 * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
84 * source file are these:
86 * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
87 * that. The host may not be able to resolve IPv6, but we don't really have to
88 * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
91 * CURLRES_ARES - is defined if libcurl is built to use c-ares for
92 * asynchronous name resolves. This can be Windows or *nix.
94 * CURLRES_THREADED - is defined if libcurl is built to run under (native)
95 * Windows, and then the name resolve will be done in a new thread, and the
96 * supported API will be the same as for ares-builds.
98 * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
99 * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
102 * The host*.c sources files are split up like this:
104 * hostip.c - method-independent resolver functions and utility functions
105 * hostasyn.c - functions for asynchronous name resolves
106 * hostsyn.c - functions for synchronous name resolves
107 * hostip4.c - IPv4 specific functions
108 * hostip6.c - IPv6 specific functions
110 * The two asynchronous name resolver backends are implemented in:
111 * asyn-ares.c - functions for ares-using name resolves
112 * asyn-thread.c - functions for threaded name resolves
114 * The hostip.h is the united header file for all this. It defines the
115 * CURLRES_* defines based on the config*.h and curl_setup.h defines.
118 static void freednsentry(void *freethis);
121 * Return # of addresses in a Curl_addrinfo struct
123 int Curl_num_addresses(const struct Curl_addrinfo *addr)
127 addr = addr->ai_next;
134 * Curl_printable_address() stores a printable version of the 1st address
135 * given in the 'ai' argument. The result will be stored in the buf that is
138 * If the conversion fails, the target buffer is empty.
140 void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf,
143 DEBUGASSERT(bufsize);
146 switch(ai->ai_family) {
148 const struct sockaddr_in *sa4 = (const void *)ai->ai_addr;
149 const struct in_addr *ipaddr4 = &sa4->sin_addr;
150 (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
155 const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr;
156 const struct in6_addr *ipaddr6 = &sa6->sin6_addr;
157 (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
167 * Create a hostcache id string for the provided host + port, to be used by
168 * the DNS caching. Without alloc.
171 create_hostcache_id(const char *name, int port, char *ptr, size_t buflen)
173 size_t len = strlen(name);
174 if(len > (buflen - 7))
176 /* store and lower case the name */
178 *ptr++ = (char)TOLOWER(*name++);
179 msnprintf(ptr, 7, ":%u", port);
182 struct hostcache_prune_data {
188 * This function is set as a callback to be called for every entry in the DNS
189 * cache when we want to prune old unused entries.
191 * Returning non-zero means remove the entry, return 0 to keep it in the
195 hostcache_timestamp_remove(void *datap, void *hc)
197 struct hostcache_prune_data *data =
198 (struct hostcache_prune_data *) datap;
199 struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
201 return (0 != c->timestamp)
202 && (data->now - c->timestamp >= data->cache_timeout);
206 * Prune the DNS cache. This assumes that a lock has already been taken.
209 hostcache_prune(struct Curl_hash *hostcache, long cache_timeout, time_t now)
211 struct hostcache_prune_data user;
213 user.cache_timeout = cache_timeout;
216 Curl_hash_clean_with_criterium(hostcache,
218 hostcache_timestamp_remove);
222 * Library-wide function for pruning the DNS cache. This function takes and
223 * returns the appropriate locks.
225 void Curl_hostcache_prune(struct Curl_easy *data)
229 if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
230 /* cache forever means never prune, and NULL hostcache means
235 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
239 /* Remove outdated and unused entries from the hostcache */
240 hostcache_prune(data->dns.hostcache,
241 data->set.dns_cache_timeout,
245 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
248 #ifdef HAVE_SIGSETJMP
249 /* Beware this is a global and unique instance. This is used to store the
250 return address that we can jump back to from inside a signal handler. This
251 is not thread-safe stuff. */
252 sigjmp_buf curl_jmpenv;
255 /* lookup address, returns entry if found and not stale */
256 static struct Curl_dns_entry *
257 fetch_addr(struct connectdata *conn,
258 const char *hostname,
261 struct Curl_dns_entry *dns = NULL;
263 struct Curl_easy *data = conn->data;
264 char entry_id[MAX_HOSTCACHE_LEN];
266 /* Create an entry id, based upon the hostname and port */
267 create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
268 entry_len = strlen(entry_id);
270 /* See if its already in our dns cache */
271 dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
273 /* No entry found in cache, check if we might have a wildcard entry */
274 if(!dns && data->change.wildcard_resolve) {
275 create_hostcache_id("*", port, entry_id, sizeof(entry_id));
276 entry_len = strlen(entry_id);
278 /* See if it's already in our dns cache */
279 dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
282 if(dns && (data->set.dns_cache_timeout != -1)) {
283 /* See whether the returned entry is stale. Done before we release lock */
284 struct hostcache_prune_data user;
287 user.cache_timeout = data->set.dns_cache_timeout;
289 if(hostcache_timestamp_remove(&user, dns)) {
290 infof(data, "Hostname in DNS cache was stale, zapped\n");
291 dns = NULL; /* the memory deallocation is being handled by the hash */
292 Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
300 * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
302 * Curl_resolv() checks initially and multi_runsingle() checks each time
303 * it discovers the handle in the state WAITRESOLVE whether the hostname
304 * has already been resolved and the address has already been stored in
305 * the DNS cache. This short circuits waiting for a lot of pending
306 * lookups for the same hostname requested by different handles.
308 * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
310 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
311 * use, or we'll leak memory!
313 struct Curl_dns_entry *
314 Curl_fetch_addr(struct connectdata *conn,
315 const char *hostname,
318 struct Curl_easy *data = conn->data;
319 struct Curl_dns_entry *dns = NULL;
322 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
324 dns = fetch_addr(conn, hostname, port);
327 dns->inuse++; /* we use it! */
330 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
335 #ifndef CURL_DISABLE_SHUFFLE_DNS
336 UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
337 struct Curl_addrinfo **addr);
339 * Curl_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo'
340 * struct by re-linking its linked list.
342 * The addr argument should be the address of a pointer to the head node of a
343 * `Curl_addrinfo` list and it will be modified to point to the new head after
346 * Not declared static only to make it easy to use in a unit test!
350 UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
351 struct Curl_addrinfo **addr)
353 CURLcode result = CURLE_OK;
354 const int num_addrs = Curl_num_addresses(*addr);
357 struct Curl_addrinfo **nodes;
358 infof(data, "Shuffling %i addresses", num_addrs);
360 nodes = malloc(num_addrs*sizeof(*nodes));
364 const size_t rnd_size = num_addrs * sizeof(*rnd);
366 /* build a plain array of Curl_addrinfo pointers */
368 for(i = 1; i < num_addrs; i++) {
369 nodes[i] = nodes[i-1]->ai_next;
372 rnd = malloc(rnd_size);
374 /* Fisher-Yates shuffle */
375 if(Curl_rand(data, (unsigned char *)rnd, rnd_size) == CURLE_OK) {
376 struct Curl_addrinfo *swap_tmp;
377 for(i = num_addrs - 1; i > 0; i--) {
378 swap_tmp = nodes[rnd[i] % (i + 1)];
379 nodes[rnd[i] % (i + 1)] = nodes[i];
383 /* relink list in the new order */
384 for(i = 1; i < num_addrs; i++) {
385 nodes[i-1]->ai_next = nodes[i];
388 nodes[num_addrs-1]->ai_next = NULL;
394 result = CURLE_OUT_OF_MEMORY;
398 result = CURLE_OUT_OF_MEMORY;
405 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
407 * When calling Curl_resolv() has resulted in a response with a returned
408 * address, we call this function to store the information in the dns
411 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
413 struct Curl_dns_entry *
414 Curl_cache_addr(struct Curl_easy *data,
415 struct Curl_addrinfo *addr,
416 const char *hostname,
419 char entry_id[MAX_HOSTCACHE_LEN];
421 struct Curl_dns_entry *dns;
422 struct Curl_dns_entry *dns2;
424 #ifndef CURL_DISABLE_SHUFFLE_DNS
425 /* shuffle addresses if requested */
426 if(data->set.dns_shuffle_addresses) {
427 CURLcode result = Curl_shuffle_addr(data, &addr);
433 /* Create a new cache entry */
434 dns = calloc(1, sizeof(struct Curl_dns_entry));
439 /* Create an entry id, based upon the hostname and port */
440 create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
441 entry_len = strlen(entry_id);
443 dns->inuse = 1; /* the cache has the first reference */
444 dns->addr = addr; /* this is the address(es) */
445 time(&dns->timestamp);
446 if(dns->timestamp == 0)
447 dns->timestamp = 1; /* zero indicates CURLOPT_RESOLVE entry */
449 /* Store the resolved data in our DNS cache. */
450 dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len + 1,
458 dns->inuse++; /* mark entry as in-use */
463 * Curl_resolv() is the main name resolve function within libcurl. It resolves
464 * a name and returns a pointer to the entry in the 'entry' argument (if one
465 * is provided). This function might return immediately if we're using asynch
466 * resolves. See the return codes.
468 * The cache entry we return will get its 'inuse' counter increased when this
469 * function is used. You MUST call Curl_resolv_unlock() later (when you're
470 * done using this struct) to decrease the counter again.
472 * In debug mode, we specifically test for an interface name "LocalHost"
473 * and resolve "localhost" instead as a means to permit test cases
474 * to connect to a local test server with any host name.
478 * CURLRESOLV_ERROR (-1) = error, no pointer
479 * CURLRESOLV_RESOLVED (0) = OK, pointer provided
480 * CURLRESOLV_PENDING (1) = waiting for response, no pointer
483 enum resolve_t Curl_resolv(struct connectdata *conn,
484 const char *hostname,
487 struct Curl_dns_entry **entry)
489 struct Curl_dns_entry *dns = NULL;
490 struct Curl_easy *data = conn->data;
492 enum resolve_t rc = CURLRESOLV_ERROR; /* default to failure */
495 conn->bits.doh = FALSE; /* default is not */
498 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
500 dns = fetch_addr(conn, hostname, port);
503 infof(data, "Hostname %s was found in DNS cache\n", hostname);
504 dns->inuse++; /* we use it! */
505 rc = CURLRESOLV_RESOLVED;
509 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
512 /* The entry was not in the cache. Resolve it to IP address */
514 struct Curl_addrinfo *addr = NULL;
517 #ifndef USE_RESOLVE_ON_IPS
522 /* notify the resolver start callback */
523 if(data->set.resolver_start) {
525 Curl_set_in_callback(data, true);
526 st = data->set.resolver_start(data->state.resolver, NULL,
527 data->set.resolver_start_client);
528 Curl_set_in_callback(data, false);
530 return CURLRESOLV_ERROR;
533 #ifndef USE_RESOLVE_ON_IPS
534 /* First check if this is an IPv4 address string */
535 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
536 /* This is a dotted IP address 123.123.123.123-style */
537 addr = Curl_ip2addr(AF_INET, &in, hostname, port);
541 /* check if this is an IPv6 address string */
542 if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
543 /* This is an IPv6 address literal */
544 addr = Curl_ip2addr(AF_INET6, &in6, hostname, port);
546 #endif /* ENABLE_IPV6 */
548 #else /* if USE_RESOLVE_ON_IPS */
549 /* First check if this is an IPv4 address string */
550 if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
551 /* This is a dotted IP address 123.123.123.123-style */
556 /* check if this is an IPv6 address string */
557 if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
558 /* This is an IPv6 address literal */
561 #endif /* ENABLE_IPV6 */
563 #endif /* !USE_RESOLVE_ON_IPS */
566 /* Check what IP specifics the app has requested and if we can provide
567 * it. If not, bail out. */
568 if(!Curl_ipvalid(conn))
569 return CURLRESOLV_ERROR;
571 if(allowDOH && data->set.doh && !ipnum) {
572 addr = Curl_doh(conn, hostname, port, &respwait);
575 /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
576 non-zero value indicating that we need to wait for the response to
578 addr = Curl_getaddrinfo(conn,
580 (data->set.str[STRING_DEVICE]
581 && !strcmp(data->set.str[STRING_DEVICE],
582 "LocalHost"))?"localhost":
584 hostname, port, &respwait);
589 /* the response to our resolve call will come asynchronously at
590 a later time, good or bad */
591 /* First, check that we haven't received the info by now */
592 result = Curl_resolv_check(conn, &dns);
593 if(result) /* error detected */
594 return CURLRESOLV_ERROR;
596 rc = CURLRESOLV_RESOLVED; /* pointer provided */
598 rc = CURLRESOLV_PENDING; /* no info yet */
603 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
605 /* we got a response, store it in the cache */
606 dns = Curl_cache_addr(data, addr, hostname, port);
609 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
612 /* returned failure, bail out nicely */
613 Curl_freeaddrinfo(addr);
615 rc = CURLRESOLV_RESOLVED;
624 #ifdef USE_ALARM_TIMEOUT
626 * This signal handler jumps back into the main libcurl code and continues
627 * execution. This effectively causes the remainder of the application to run
628 * within a signal handler which is nonportable and could lead to problems.
631 RETSIGTYPE alarmfunc(int sig)
633 /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
635 siglongjmp(curl_jmpenv, 1);
637 #endif /* USE_ALARM_TIMEOUT */
640 * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
641 * timeout. This function might return immediately if we're using asynch
642 * resolves. See the return codes.
644 * The cache entry we return will get its 'inuse' counter increased when this
645 * function is used. You MUST call Curl_resolv_unlock() later (when you're
646 * done using this struct) to decrease the counter again.
648 * If built with a synchronous resolver and use of signals is not
649 * disabled by the application, then a nonzero timeout will cause a
650 * timeout after the specified number of milliseconds. Otherwise, timeout
655 * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
656 * CURLRESOLV_ERROR (-1) = error, no pointer
657 * CURLRESOLV_RESOLVED (0) = OK, pointer provided
658 * CURLRESOLV_PENDING (1) = waiting for response, no pointer
661 enum resolve_t Curl_resolv_timeout(struct connectdata *conn,
662 const char *hostname,
664 struct Curl_dns_entry **entry,
665 timediff_t timeoutms)
667 #ifdef USE_ALARM_TIMEOUT
668 #ifdef HAVE_SIGACTION
669 struct sigaction keep_sigact; /* store the old struct here */
670 volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */
671 struct sigaction sigact;
674 void (*keep_sigact)(int); /* store the old handler here */
675 #endif /* HAVE_SIGNAL */
676 #endif /* HAVE_SIGACTION */
677 volatile long timeout;
678 volatile unsigned int prev_alarm = 0;
679 struct Curl_easy *data = conn->data;
680 #endif /* USE_ALARM_TIMEOUT */
686 /* got an already expired timeout */
687 return CURLRESOLV_TIMEDOUT;
689 #ifdef USE_ALARM_TIMEOUT
690 if(data->set.no_signal)
691 /* Ignore the timeout when signals are disabled */
694 timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms;
697 /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
698 return Curl_resolv(conn, hostname, port, TRUE, entry);
701 /* The alarm() function only provides integer second resolution, so if
702 we want to wait less than one second we must bail out already now. */
704 "remaining timeout of %ld too small to resolve via SIGALRM method",
706 return CURLRESOLV_TIMEDOUT;
708 /* This allows us to time-out from the name resolver, as the timeout
709 will generate a signal and we will siglongjmp() from that here.
710 This technique has problems (see alarmfunc).
711 This should be the last thing we do before calling Curl_resolv(),
712 as otherwise we'd have to worry about variables that get modified
713 before we invoke Curl_resolv() (and thus use "volatile"). */
714 if(sigsetjmp(curl_jmpenv, 1)) {
715 /* this is coming from a siglongjmp() after an alarm signal */
716 failf(data, "name lookup timed out");
717 rc = CURLRESOLV_ERROR;
721 /*************************************************************
722 * Set signal handler to catch SIGALRM
723 * Store the old value to be able to set it back later!
724 *************************************************************/
725 #ifdef HAVE_SIGACTION
726 sigaction(SIGALRM, NULL, &sigact);
727 keep_sigact = sigact;
728 keep_copysig = TRUE; /* yes, we have a copy */
729 sigact.sa_handler = alarmfunc;
731 /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
732 sigact.sa_flags &= ~SA_RESTART;
734 /* now set the new struct */
735 sigaction(SIGALRM, &sigact, NULL);
736 #else /* HAVE_SIGACTION */
737 /* no sigaction(), revert to the much lamer signal() */
739 keep_sigact = signal(SIGALRM, alarmfunc);
741 #endif /* HAVE_SIGACTION */
743 /* alarm() makes a signal get sent when the timeout fires off, and that
744 will abort system calls */
745 prev_alarm = alarm(curlx_sltoui(timeout/1000L));
749 #ifndef CURLRES_ASYNCH
751 infof(conn->data, "timeout on name lookup is not supported\n");
753 (void)timeoutms; /* timeoutms not used with an async resolver */
755 #endif /* USE_ALARM_TIMEOUT */
757 /* Perform the actual name resolution. This might be interrupted by an
758 * alarm if it takes too long.
760 rc = Curl_resolv(conn, hostname, port, TRUE, entry);
762 #ifdef USE_ALARM_TIMEOUT
766 /* deactivate a possibly active alarm before uninstalling the handler */
769 #ifdef HAVE_SIGACTION
771 /* we got a struct as it looked before, now put that one back nice
773 sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
777 /* restore the previous SIGALRM handler */
778 signal(SIGALRM, keep_sigact);
780 #endif /* HAVE_SIGACTION */
782 /* switch back the alarm() to either zero or to what it was before minus
783 the time we spent until now! */
785 /* there was an alarm() set before us, now put it back */
786 timediff_t elapsed_secs = Curl_timediff(Curl_now(),
787 conn->created) / 1000;
789 /* the alarm period is counted in even number of seconds */
790 unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs);
793 ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
794 /* if the alarm time-left reached zero or turned "negative" (counted
795 with unsigned values), we should fire off a SIGALRM here, but we
796 won't, and zero would be to switch it off so we never set it to
799 rc = CURLRESOLV_TIMEDOUT;
800 failf(data, "Previous alarm fired off!");
803 alarm((unsigned int)alarm_set);
805 #endif /* USE_ALARM_TIMEOUT */
811 * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
812 * made, the struct may be destroyed due to pruning. It is important that only
813 * one unlock is made for each Curl_resolv() call.
815 * May be called with 'data' == NULL for global cache.
817 void Curl_resolv_unlock(struct Curl_easy *data, struct Curl_dns_entry *dns)
819 if(data && data->share)
820 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
824 if(data && data->share)
825 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
829 * File-internal: release cache dns entry reference, free if inuse drops to 0
831 static void freednsentry(void *freethis)
833 struct Curl_dns_entry *dns = (struct Curl_dns_entry *) freethis;
834 DEBUGASSERT(dns && (dns->inuse>0));
837 if(dns->inuse == 0) {
838 Curl_freeaddrinfo(dns->addr);
844 * Curl_mk_dnscache() inits a new DNS cache and returns success/failure.
846 int Curl_mk_dnscache(struct Curl_hash *hash)
848 return Curl_hash_init(hash, 7, Curl_hash_str, Curl_str_key_compare,
853 * Curl_hostcache_clean()
855 * This _can_ be called with 'data' == NULL but then of course no locking
859 void Curl_hostcache_clean(struct Curl_easy *data,
860 struct Curl_hash *hash)
862 if(data && data->share)
863 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
865 Curl_hash_clean(hash);
867 if(data && data->share)
868 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
872 CURLcode Curl_loadhostpairs(struct Curl_easy *data)
874 struct curl_slist *hostp;
878 /* Default is no wildcard found */
879 data->change.wildcard_resolve = false;
881 for(hostp = data->change.resolve; hostp; hostp = hostp->next) {
882 char entry_id[MAX_HOSTCACHE_LEN];
885 if(hostp->data[0] == '-') {
888 if(2 != sscanf(hostp->data + 1, "%255[^:]:%d", hostname, &port)) {
889 infof(data, "Couldn't parse CURLOPT_RESOLVE removal entry '%s'!\n",
894 /* Create an entry id, based upon the hostname and port */
895 create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
896 entry_len = strlen(entry_id);
899 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
901 /* delete entry, ignore if it didn't exist */
902 Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
905 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
908 struct Curl_dns_entry *dns;
909 struct Curl_addrinfo *head = NULL, *tail = NULL;
912 #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
913 char *addresses = NULL;
920 unsigned long tmp_port;
923 host_end = strchr(hostp->data, ':');
925 ((host_end - hostp->data) >= (ptrdiff_t)sizeof(hostname)))
928 memcpy(hostname, hostp->data, host_end - hostp->data);
929 hostname[host_end - hostp->data] = '\0';
931 port_ptr = host_end + 1;
932 tmp_port = strtoul(port_ptr, &end_ptr, 10);
933 if(tmp_port > USHRT_MAX || end_ptr == port_ptr || *end_ptr != ':')
936 port = (int)tmp_port;
937 #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
938 addresses = end_ptr + 1;
943 struct Curl_addrinfo *ai;
945 addr_begin = end_ptr + 1;
946 addr_end = strchr(addr_begin, ',');
948 addr_end = addr_begin + strlen(addr_begin);
951 /* allow IP(v6) address within [brackets] */
952 if(*addr_begin == '[') {
953 if(addr_end == addr_begin || *(addr_end - 1) != ']')
959 alen = addr_end - addr_begin;
963 if(alen >= sizeof(address))
966 memcpy(address, addr_begin, alen);
967 address[alen] = '\0';
970 if(strchr(address, ':')) {
971 infof(data, "Ignoring resolve address '%s', missing IPv6 support.\n",
977 ai = Curl_str2addr(address, port);
979 infof(data, "Resolve address '%s' found illegal!\n", address);
985 tail = tail->ai_next;
998 infof(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'!\n",
1000 Curl_freeaddrinfo(head);
1004 /* Create an entry id, based upon the hostname and port */
1005 create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
1006 entry_len = strlen(entry_id);
1009 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
1011 /* See if its already in our dns cache */
1012 dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
1015 infof(data, "RESOLVE %s:%d is - old addresses discarded!\n",
1017 /* delete old entry entry, there are two reasons for this
1018 1. old entry may have different addresses.
1019 2. even if entry with correct addresses is already in the cache,
1020 but if it is close to expire, then by the time next http
1021 request is made, it can get expired and pruned because old
1022 entry is not necessarily marked as added by CURLOPT_RESOLVE. */
1024 Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
1027 /* put this new host in the cache */
1028 dns = Curl_cache_addr(data, head, hostname, port);
1030 dns->timestamp = 0; /* mark as added by CURLOPT_RESOLVE */
1031 /* release the returned reference; the cache itself will keep the
1037 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
1040 Curl_freeaddrinfo(head);
1041 return CURLE_OUT_OF_MEMORY;
1043 infof(data, "Added %s:%d:%s to DNS cache\n",
1044 hostname, port, addresses);
1046 /* Wildcard hostname */
1047 if(hostname[0] == '*' && hostname[1] == '\0') {
1048 infof(data, "RESOLVE %s:%d is wildcard, enabling wildcard checks\n",
1050 data->change.wildcard_resolve = true;
1054 data->change.resolve = NULL; /* dealt with now */
1059 CURLcode Curl_resolv_check(struct connectdata *conn,
1060 struct Curl_dns_entry **dns)
1062 #if defined(CURL_DISABLE_DOH) && !defined(CURLRES_ASYNCH)
1067 return Curl_doh_is_resolved(conn, dns);
1068 return Curl_resolver_is_resolved(conn, dns);
1071 int Curl_resolv_getsock(struct connectdata *conn,
1072 curl_socket_t *socks)
1074 #ifdef CURLRES_ASYNCH
1076 /* nothing to wait for during DOH resolve, those handles have their own
1078 return GETSOCK_BLANK;
1079 return Curl_resolver_getsock(conn, socks);
1083 return GETSOCK_BLANK;
1087 /* Call this function after Curl_connect() has returned async=TRUE and
1088 then a successful name resolve has been received.
1090 Note: this function disconnects and frees the conn data in case of
1092 CURLcode Curl_once_resolved(struct connectdata *conn,
1093 bool *protocol_done)
1097 if(conn->async.dns) {
1098 conn->dns_entry = conn->async.dns;
1099 conn->async.dns = NULL;
1102 result = Curl_setup_conn(conn, protocol_done);
1105 struct Curl_easy *data = conn->data;
1107 Curl_detach_connnection(data);
1108 Curl_conncache_remove_conn(data, conn, TRUE);
1109 Curl_disconnect(data, conn, TRUE);