1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 http://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>
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
57 #include "inet_ntop.h"
59 #include "curl_printf.h"
60 #include "curl_memory.h"
61 /* The last #include file should be: */
64 #if defined(CURLRES_SYNCH) && \
65 defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP)
66 /* alarm-based timeouts can only be used with all the dependencies satisfied */
67 #define USE_ALARM_TIMEOUT
74 * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
75 * source file are these:
77 * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
78 * that. The host may not be able to resolve IPv6, but we don't really have to
79 * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
82 * CURLRES_ARES - is defined if libcurl is built to use c-ares for
83 * asynchronous name resolves. This can be Windows or *nix.
85 * CURLRES_THREADED - is defined if libcurl is built to run under (native)
86 * Windows, and then the name resolve will be done in a new thread, and the
87 * supported API will be the same as for ares-builds.
89 * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
90 * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
93 * The host*.c sources files are split up like this:
95 * hostip.c - method-independent resolver functions and utility functions
96 * hostasyn.c - functions for asynchronous name resolves
97 * hostsyn.c - functions for synchronous name resolves
98 * hostip4.c - IPv4 specific functions
99 * hostip6.c - IPv6 specific functions
101 * The two asynchronous name resolver backends are implemented in:
102 * asyn-ares.c - functions for ares-using name resolves
103 * asyn-thread.c - functions for threaded name resolves
105 * The hostip.h is the united header file for all this. It defines the
106 * CURLRES_* defines based on the config*.h and curl_setup.h defines.
109 /* These two symbols are for the global DNS cache */
110 static struct curl_hash hostname_cache;
111 static int host_cache_initialized;
113 static void freednsentry(void *freethis);
116 * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
117 * Global DNS cache is general badness. Do not use. This will be removed in
118 * a future version. Use the share interface instead!
120 * Returns a struct curl_hash pointer on success, NULL on failure.
122 struct curl_hash *Curl_global_host_cache_init(void)
125 if(!host_cache_initialized) {
126 rc = Curl_hash_init(&hostname_cache, 7, Curl_hash_str,
127 Curl_str_key_compare, freednsentry);
129 host_cache_initialized = 1;
131 return rc?NULL:&hostname_cache;
135 * Destroy and cleanup the global DNS cache
137 void Curl_global_host_cache_dtor(void)
139 if(host_cache_initialized) {
140 Curl_hash_destroy(&hostname_cache);
141 host_cache_initialized = 0;
146 * Return # of adresses in a Curl_addrinfo struct
148 int Curl_num_addresses(const Curl_addrinfo *addr)
152 addr = addr->ai_next;
159 * Curl_printable_address() returns a printable version of the 1st address
160 * given in the 'ai' argument. The result will be stored in the buf that is
163 * If the conversion fails, it returns NULL.
166 Curl_printable_address(const Curl_addrinfo *ai, char *buf, size_t bufsize)
168 const struct sockaddr_in *sa4;
169 const struct in_addr *ipaddr4;
171 const struct sockaddr_in6 *sa6;
172 const struct in6_addr *ipaddr6;
175 switch (ai->ai_family) {
177 sa4 = (const void *)ai->ai_addr;
178 ipaddr4 = &sa4->sin_addr;
179 return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf,
183 sa6 = (const void *)ai->ai_addr;
184 ipaddr6 = &sa6->sin6_addr;
185 return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf,
195 * Return a hostcache id string for the provided host + port, to be used by
199 create_hostcache_id(const char *name, int port)
201 /* create and return the new allocated entry */
202 char *id = aprintf("%s:%d", name, port);
205 /* lower case the name part */
206 while(*ptr && (*ptr != ':')) {
207 *ptr = (char)TOLOWER(*ptr);
214 struct hostcache_prune_data {
220 * This function is set as a callback to be called for every entry in the DNS
221 * cache when we want to prune old unused entries.
223 * Returning non-zero means remove the entry, return 0 to keep it in the
227 hostcache_timestamp_remove(void *datap, void *hc)
229 struct hostcache_prune_data *data =
230 (struct hostcache_prune_data *) datap;
231 struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
233 return (0 != c->timestamp)
234 && (data->now - c->timestamp >= data->cache_timeout);
238 * Prune the DNS cache. This assumes that a lock has already been taken.
241 hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
243 struct hostcache_prune_data user;
245 user.cache_timeout = cache_timeout;
248 Curl_hash_clean_with_criterium(hostcache,
250 hostcache_timestamp_remove);
254 * Library-wide function for pruning the DNS cache. This function takes and
255 * returns the appropriate locks.
257 void Curl_hostcache_prune(struct SessionHandle *data)
261 if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
262 /* cache forever means never prune, and NULL hostcache means
267 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
271 /* Remove outdated and unused entries from the hostcache */
272 hostcache_prune(data->dns.hostcache,
273 data->set.dns_cache_timeout,
277 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
280 #ifdef HAVE_SIGSETJMP
281 /* Beware this is a global and unique instance. This is used to store the
282 return address that we can jump back to from inside a signal handler. This
283 is not thread-safe stuff. */
284 sigjmp_buf curl_jmpenv;
287 /* lookup address, returns entry if found and not stale */
288 static struct Curl_dns_entry *
289 fetch_addr(struct connectdata *conn,
290 const char *hostname,
293 char *entry_id = NULL;
294 struct Curl_dns_entry *dns = NULL;
296 struct SessionHandle *data = conn->data;
298 /* Create an entry id, based upon the hostname and port */
299 entry_id = create_hostcache_id(hostname, port);
300 /* If we can't create the entry id, fail */
304 entry_len = strlen(entry_id);
306 /* See if its already in our dns cache */
307 dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
309 if(dns && (data->set.dns_cache_timeout != -1)) {
310 /* See whether the returned entry is stale. Done before we release lock */
311 struct hostcache_prune_data user;
314 user.cache_timeout = data->set.dns_cache_timeout;
316 if(hostcache_timestamp_remove(&user, dns)) {
317 infof(data, "Hostname in DNS cache was stale, zapped\n");
318 dns = NULL; /* the memory deallocation is being handled by the hash */
319 Curl_hash_delete(data->dns.hostcache, entry_id, entry_len+1);
323 /* free the allocated entry_id again */
330 * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
332 * Curl_resolv() checks initially and multi_runsingle() checks each time
333 * it discovers the handle in the state WAITRESOLVE whether the hostname
334 * has already been resolved and the address has already been stored in
335 * the DNS cache. This short circuits waiting for a lot of pending
336 * lookups for the same hostname requested by different handles.
338 * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
340 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
341 * use, or we'll leak memory!
343 struct Curl_dns_entry *
344 Curl_fetch_addr(struct connectdata *conn,
345 const char *hostname,
348 struct SessionHandle *data = conn->data;
349 struct Curl_dns_entry *dns = NULL;
352 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
354 dns = fetch_addr(conn, hostname, port);
356 if(dns) dns->inuse++; /* we use it! */
359 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
365 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
367 * When calling Curl_resolv() has resulted in a response with a returned
368 * address, we call this function to store the information in the dns
371 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
373 struct Curl_dns_entry *
374 Curl_cache_addr(struct SessionHandle *data,
376 const char *hostname,
381 struct Curl_dns_entry *dns;
382 struct Curl_dns_entry *dns2;
384 /* Create an entry id, based upon the hostname and port */
385 entry_id = create_hostcache_id(hostname, port);
386 /* If we can't create the entry id, fail */
389 entry_len = strlen(entry_id);
391 /* Create a new cache entry */
392 dns = calloc(1, sizeof(struct Curl_dns_entry));
398 dns->inuse = 1; /* the cache has the first reference */
399 dns->addr = addr; /* this is the address(es) */
400 time(&dns->timestamp);
401 if(dns->timestamp == 0)
402 dns->timestamp = 1; /* zero indicates CURLOPT_RESOLVE entry */
404 /* Store the resolved data in our DNS cache. */
405 dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
414 dns->inuse++; /* mark entry as in-use */
416 /* free the allocated entry_id */
423 * Curl_resolv() is the main name resolve function within libcurl. It resolves
424 * a name and returns a pointer to the entry in the 'entry' argument (if one
425 * is provided). This function might return immediately if we're using asynch
426 * resolves. See the return codes.
428 * The cache entry we return will get its 'inuse' counter increased when this
429 * function is used. You MUST call Curl_resolv_unlock() later (when you're
430 * done using this struct) to decrease the counter again.
432 * In debug mode, we specifically test for an interface name "LocalHost"
433 * and resolve "localhost" instead as a means to permit test cases
434 * to connect to a local test server with any host name.
438 * CURLRESOLV_ERROR (-1) = error, no pointer
439 * CURLRESOLV_RESOLVED (0) = OK, pointer provided
440 * CURLRESOLV_PENDING (1) = waiting for response, no pointer
443 int Curl_resolv(struct connectdata *conn,
444 const char *hostname,
446 struct Curl_dns_entry **entry)
448 struct Curl_dns_entry *dns = NULL;
449 struct SessionHandle *data = conn->data;
451 int rc = CURLRESOLV_ERROR; /* default to failure */
456 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
458 dns = fetch_addr(conn, hostname, port);
461 infof(data, "Hostname %s was found in DNS cache\n", hostname);
462 dns->inuse++; /* we use it! */
463 rc = CURLRESOLV_RESOLVED;
467 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
470 /* The entry was not in the cache. Resolve it to IP address */
475 /* Check what IP specifics the app has requested and if we can provide it.
476 * If not, bail out. */
477 if(!Curl_ipvalid(conn))
478 return CURLRESOLV_ERROR;
480 /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
481 non-zero value indicating that we need to wait for the response to the
483 addr = Curl_getaddrinfo(conn,
485 (data->set.str[STRING_DEVICE]
486 && !strcmp(data->set.str[STRING_DEVICE],
487 "LocalHost"))?"localhost":
489 hostname, port, &respwait);
493 /* the response to our resolve call will come asynchronously at
494 a later time, good or bad */
495 /* First, check that we haven't received the info by now */
496 result = Curl_resolver_is_resolved(conn, &dns);
497 if(result) /* error detected */
498 return CURLRESOLV_ERROR;
500 rc = CURLRESOLV_RESOLVED; /* pointer provided */
502 rc = CURLRESOLV_PENDING; /* no info yet */
507 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
509 /* we got a response, store it in the cache */
510 dns = Curl_cache_addr(data, addr, hostname, port);
513 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
516 /* returned failure, bail out nicely */
517 Curl_freeaddrinfo(addr);
519 rc = CURLRESOLV_RESOLVED;
528 #ifdef USE_ALARM_TIMEOUT
530 * This signal handler jumps back into the main libcurl code and continues
531 * execution. This effectively causes the remainder of the application to run
532 * within a signal handler which is nonportable and could lead to problems.
535 RETSIGTYPE alarmfunc(int sig)
537 /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
539 siglongjmp(curl_jmpenv, 1);
542 #endif /* USE_ALARM_TIMEOUT */
545 * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
546 * timeout. This function might return immediately if we're using asynch
547 * resolves. See the return codes.
549 * The cache entry we return will get its 'inuse' counter increased when this
550 * function is used. You MUST call Curl_resolv_unlock() later (when you're
551 * done using this struct) to decrease the counter again.
553 * If built with a synchronous resolver and use of signals is not
554 * disabled by the application, then a nonzero timeout will cause a
555 * timeout after the specified number of milliseconds. Otherwise, timeout
560 * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
561 * CURLRESOLV_ERROR (-1) = error, no pointer
562 * CURLRESOLV_RESOLVED (0) = OK, pointer provided
563 * CURLRESOLV_PENDING (1) = waiting for response, no pointer
566 int Curl_resolv_timeout(struct connectdata *conn,
567 const char *hostname,
569 struct Curl_dns_entry **entry,
572 #ifdef USE_ALARM_TIMEOUT
573 #ifdef HAVE_SIGACTION
574 struct sigaction keep_sigact; /* store the old struct here */
575 volatile bool keep_copysig = FALSE; /* wether old sigact has been saved */
576 struct sigaction sigact;
579 void (*keep_sigact)(int); /* store the old handler here */
580 #endif /* HAVE_SIGNAL */
581 #endif /* HAVE_SIGACTION */
582 volatile long timeout;
583 volatile unsigned int prev_alarm = 0;
584 struct SessionHandle *data = conn->data;
585 #endif /* USE_ALARM_TIMEOUT */
591 /* got an already expired timeout */
592 return CURLRESOLV_TIMEDOUT;
594 #ifdef USE_ALARM_TIMEOUT
595 if(data->set.no_signal)
596 /* Ignore the timeout when signals are disabled */
602 /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
603 return Curl_resolv(conn, hostname, port, entry);
606 /* The alarm() function only provides integer second resolution, so if
607 we want to wait less than one second we must bail out already now. */
608 return CURLRESOLV_TIMEDOUT;
610 /* This allows us to time-out from the name resolver, as the timeout
611 will generate a signal and we will siglongjmp() from that here.
612 This technique has problems (see alarmfunc).
613 This should be the last thing we do before calling Curl_resolv(),
614 as otherwise we'd have to worry about variables that get modified
615 before we invoke Curl_resolv() (and thus use "volatile"). */
616 if(sigsetjmp(curl_jmpenv, 1)) {
617 /* this is coming from a siglongjmp() after an alarm signal */
618 failf(data, "name lookup timed out");
619 rc = CURLRESOLV_ERROR;
623 /*************************************************************
624 * Set signal handler to catch SIGALRM
625 * Store the old value to be able to set it back later!
626 *************************************************************/
627 #ifdef HAVE_SIGACTION
628 sigaction(SIGALRM, NULL, &sigact);
629 keep_sigact = sigact;
630 keep_copysig = TRUE; /* yes, we have a copy */
631 sigact.sa_handler = alarmfunc;
633 /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
634 sigact.sa_flags &= ~SA_RESTART;
636 /* now set the new struct */
637 sigaction(SIGALRM, &sigact, NULL);
638 #else /* HAVE_SIGACTION */
639 /* no sigaction(), revert to the much lamer signal() */
641 keep_sigact = signal(SIGALRM, alarmfunc);
643 #endif /* HAVE_SIGACTION */
645 /* alarm() makes a signal get sent when the timeout fires off, and that
646 will abort system calls */
647 prev_alarm = alarm(curlx_sltoui(timeout/1000L));
651 #ifndef CURLRES_ASYNCH
653 infof(conn->data, "timeout on name lookup is not supported\n");
655 (void)timeoutms; /* timeoutms not used with an async resolver */
657 #endif /* USE_ALARM_TIMEOUT */
659 /* Perform the actual name resolution. This might be interrupted by an
660 * alarm if it takes too long.
662 rc = Curl_resolv(conn, hostname, port, entry);
664 #ifdef USE_ALARM_TIMEOUT
668 /* deactivate a possibly active alarm before uninstalling the handler */
671 #ifdef HAVE_SIGACTION
673 /* we got a struct as it looked before, now put that one back nice
675 sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
679 /* restore the previous SIGALRM handler */
680 signal(SIGALRM, keep_sigact);
682 #endif /* HAVE_SIGACTION */
684 /* switch back the alarm() to either zero or to what it was before minus
685 the time we spent until now! */
687 /* there was an alarm() set before us, now put it back */
688 unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
690 /* the alarm period is counted in even number of seconds */
691 unsigned long alarm_set = prev_alarm - elapsed_ms/1000;
694 ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
695 /* if the alarm time-left reached zero or turned "negative" (counted
696 with unsigned values), we should fire off a SIGALRM here, but we
697 won't, and zero would be to switch it off so we never set it to
700 rc = CURLRESOLV_TIMEDOUT;
701 failf(data, "Previous alarm fired off!");
704 alarm((unsigned int)alarm_set);
706 #endif /* USE_ALARM_TIMEOUT */
712 * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
713 * made, the struct may be destroyed due to pruning. It is important that only
714 * one unlock is made for each Curl_resolv() call.
716 * May be called with 'data' == NULL for global cache.
718 void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
720 if(data && data->share)
721 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
725 if(data && data->share)
726 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
730 * File-internal: release cache dns entry reference, free if inuse drops to 0
732 static void freednsentry(void *freethis)
734 struct Curl_dns_entry *dns = (struct Curl_dns_entry *) freethis;
735 DEBUGASSERT(dns && (dns->inuse>0));
738 if(dns->inuse == 0) {
739 Curl_freeaddrinfo(dns->addr);
745 * Curl_mk_dnscache() inits a new DNS cache and returns success/failure.
747 int Curl_mk_dnscache(struct curl_hash *hash)
749 return Curl_hash_init(hash, 7, Curl_hash_str, Curl_str_key_compare,
754 * Curl_hostcache_clean()
756 * This _can_ be called with 'data' == NULL but then of course no locking
760 void Curl_hostcache_clean(struct SessionHandle *data,
761 struct curl_hash *hash)
763 if(data && data->share)
764 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
766 Curl_hash_clean(hash);
768 if(data && data->share)
769 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
773 CURLcode Curl_loadhostpairs(struct SessionHandle *data)
775 struct curl_slist *hostp;
780 for(hostp = data->change.resolve; hostp; hostp = hostp->next ) {
783 if(hostp->data[0] == '-') {
787 if(2 != sscanf(hostp->data + 1, "%255[^:]:%d", hostname, &port)) {
788 infof(data, "Couldn't parse CURLOPT_RESOLVE removal entry '%s'!\n",
793 /* Create an entry id, based upon the hostname and port */
794 entry_id = create_hostcache_id(hostname, port);
795 /* If we can't create the entry id, fail */
797 return CURLE_OUT_OF_MEMORY;
800 entry_len = strlen(entry_id);
803 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
805 /* delete entry, ignore if it didn't exist */
806 Curl_hash_delete(data->dns.hostcache, entry_id, entry_len+1);
809 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
811 /* free the allocated entry_id again */
815 struct Curl_dns_entry *dns;
820 if(3 != sscanf(hostp->data, "%255[^:]:%d:%255s", hostname, &port,
822 infof(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'!\n",
827 addr = Curl_str2addr(address, port);
829 infof(data, "Address in '%s' found illegal!\n", hostp->data);
833 /* Create an entry id, based upon the hostname and port */
834 entry_id = create_hostcache_id(hostname, port);
835 /* If we can't create the entry id, fail */
837 Curl_freeaddrinfo(addr);
838 return CURLE_OUT_OF_MEMORY;
841 entry_len = strlen(entry_id);
844 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
846 /* See if its already in our dns cache */
847 dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
849 /* free the allocated entry_id again */
853 /* if not in the cache already, put this host in the cache */
854 dns = Curl_cache_addr(data, addr, hostname, port);
856 dns->timestamp = 0; /* mark as added by CURLOPT_RESOLVE */
857 /* release the returned reference; the cache itself will keep the
863 /* this is a duplicate, free it again */
864 Curl_freeaddrinfo(addr);
867 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
870 Curl_freeaddrinfo(addr);
871 return CURLE_OUT_OF_MEMORY;
873 infof(data, "Added %s:%d:%s to DNS cache\n",
874 hostname, port, address);
877 data->change.resolve = NULL; /* dealt with now */