1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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"
60 #define _MPRINTF_REPLACE /* use our functions only */
61 #include <curl/mprintf.h>
63 #include "curl_memory.h"
64 /* The last #include file should be: */
67 #if defined(CURLRES_SYNCH) && \
68 defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP)
69 /* alarm-based timeouts can only be used with all the dependencies satisfied */
70 #define USE_ALARM_TIMEOUT
77 * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
78 * source file are these:
80 * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
81 * that. The host may not be able to resolve IPv6, but we don't really have to
82 * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
85 * CURLRES_ARES - is defined if libcurl is built to use c-ares for
86 * asynchronous name resolves. This can be Windows or *nix.
88 * CURLRES_THREADED - is defined if libcurl is built to run under (native)
89 * Windows, and then the name resolve will be done in a new thread, and the
90 * supported API will be the same as for ares-builds.
92 * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
93 * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
96 * The host*.c sources files are split up like this:
98 * hostip.c - method-independent resolver functions and utility functions
99 * hostasyn.c - functions for asynchronous name resolves
100 * hostsyn.c - functions for synchronous name resolves
101 * hostip4.c - ipv4-specific functions
102 * hostip6.c - ipv6-specific functions
104 * The two asynchronous name resolver backends are implemented in:
105 * asyn-ares.c - functions for ares-using name resolves
106 * asyn-thread.c - functions for threaded name resolves
108 * The hostip.h is the united header file for all this. It defines the
109 * CURLRES_* defines based on the config*.h and curl_setup.h defines.
112 /* These two symbols are for the global DNS cache */
113 static struct curl_hash hostname_cache;
114 static int host_cache_initialized;
116 static void freednsentry(void *freethis);
119 * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
120 * Global DNS cache is general badness. Do not use. This will be removed in
121 * a future version. Use the share interface instead!
123 * Returns a struct curl_hash pointer on success, NULL on failure.
125 struct curl_hash *Curl_global_host_cache_init(void)
128 if(!host_cache_initialized) {
129 rc = Curl_hash_init(&hostname_cache, 7, Curl_hash_str,
130 Curl_str_key_compare, freednsentry);
132 host_cache_initialized = 1;
134 return rc?NULL:&hostname_cache;
138 * Destroy and cleanup the global DNS cache
140 void Curl_global_host_cache_dtor(void)
142 if(host_cache_initialized) {
143 /* first make sure that any custom "CURLOPT_RESOLVE" names are
145 Curl_hostcache_clean(NULL, &hostname_cache);
146 /* then free the remaining hash completely */
147 Curl_hash_clean(&hostname_cache);
148 host_cache_initialized = 0;
153 * Return # of adresses in a Curl_addrinfo struct
155 int Curl_num_addresses(const Curl_addrinfo *addr)
159 addr = addr->ai_next;
166 * Curl_printable_address() returns a printable version of the 1st address
167 * given in the 'ai' argument. The result will be stored in the buf that is
170 * If the conversion fails, it returns NULL.
173 Curl_printable_address(const Curl_addrinfo *ai, char *buf, size_t bufsize)
175 const struct sockaddr_in *sa4;
176 const struct in_addr *ipaddr4;
178 const struct sockaddr_in6 *sa6;
179 const struct in6_addr *ipaddr6;
182 switch (ai->ai_family) {
184 sa4 = (const void *)ai->ai_addr;
185 ipaddr4 = &sa4->sin_addr;
186 return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf,
190 sa6 = (const void *)ai->ai_addr;
191 ipaddr6 = &sa6->sin6_addr;
192 return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf,
202 * Return a hostcache id string for the provided host + port, to be used by
206 create_hostcache_id(const char *name, int port)
208 /* create and return the new allocated entry */
209 char *id = aprintf("%s:%d", name, port);
212 /* lower case the name part */
213 while(*ptr && (*ptr != ':')) {
214 *ptr = (char)TOLOWER(*ptr);
221 struct hostcache_prune_data {
227 * This function is set as a callback to be called for every entry in the DNS
228 * cache when we want to prune old unused entries.
230 * Returning non-zero means remove the entry, return 0 to keep it in the
234 hostcache_timestamp_remove(void *datap, void *hc)
236 struct hostcache_prune_data *data =
237 (struct hostcache_prune_data *) datap;
238 struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
240 return (data->now - c->timestamp >= data->cache_timeout);
244 * Prune the DNS cache. This assumes that a lock has already been taken.
247 hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
249 struct hostcache_prune_data user;
251 user.cache_timeout = cache_timeout;
254 Curl_hash_clean_with_criterium(hostcache,
256 hostcache_timestamp_remove);
260 * Library-wide function for pruning the DNS cache. This function takes and
261 * returns the appropriate locks.
263 void Curl_hostcache_prune(struct SessionHandle *data)
267 if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
268 /* cache forever means never prune, and NULL hostcache means
273 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
277 /* Remove outdated and unused entries from the hostcache */
278 hostcache_prune(data->dns.hostcache,
279 data->set.dns_cache_timeout,
283 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
287 * Check if the entry should be pruned. Assumes a locked cache.
290 remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
292 struct hostcache_prune_data user;
294 if(!dns || (data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
295 /* cache forever means never prune, and NULL hostcache means
300 user.cache_timeout = data->set.dns_cache_timeout;
302 if(!hostcache_timestamp_remove(&user,dns) )
305 Curl_hash_clean_with_criterium(data->dns.hostcache,
307 hostcache_timestamp_remove);
313 #ifdef HAVE_SIGSETJMP
314 /* Beware this is a global and unique instance. This is used to store the
315 return address that we can jump back to from inside a signal handler. This
316 is not thread-safe stuff. */
317 sigjmp_buf curl_jmpenv;
322 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
324 * When calling Curl_resolv() has resulted in a response with a returned
325 * address, we call this function to store the information in the dns
328 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
330 struct Curl_dns_entry *
331 Curl_cache_addr(struct SessionHandle *data,
333 const char *hostname,
338 struct Curl_dns_entry *dns;
339 struct Curl_dns_entry *dns2;
341 /* Create an entry id, based upon the hostname and port */
342 entry_id = create_hostcache_id(hostname, port);
343 /* If we can't create the entry id, fail */
346 entry_len = strlen(entry_id);
348 /* Create a new cache entry */
349 dns = calloc(1, sizeof(struct Curl_dns_entry));
355 dns->inuse = 0; /* init to not used */
356 dns->addr = addr; /* this is the address(es) */
357 time(&dns->timestamp);
358 if(dns->timestamp == 0)
359 dns->timestamp = 1; /* zero indicates that entry isn't in hash table */
361 /* Store the resolved data in our DNS cache. */
362 dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
371 dns->inuse++; /* mark entry as in-use */
373 /* free the allocated entry_id */
380 * Curl_resolv() is the main name resolve function within libcurl. It resolves
381 * a name and returns a pointer to the entry in the 'entry' argument (if one
382 * is provided). This function might return immediately if we're using asynch
383 * resolves. See the return codes.
385 * The cache entry we return will get its 'inuse' counter increased when this
386 * function is used. You MUST call Curl_resolv_unlock() later (when you're
387 * done using this struct) to decrease the counter again.
389 * In debug mode, we specifically test for an interface name "LocalHost"
390 * and resolve "localhost" instead as a means to permit test cases
391 * to connect to a local test server with any host name.
395 * CURLRESOLV_ERROR (-1) = error, no pointer
396 * CURLRESOLV_RESOLVED (0) = OK, pointer provided
397 * CURLRESOLV_PENDING (1) = waiting for response, no pointer
400 int Curl_resolv(struct connectdata *conn,
401 const char *hostname,
403 struct Curl_dns_entry **entry)
405 char *entry_id = NULL;
406 struct Curl_dns_entry *dns = NULL;
408 struct SessionHandle *data = conn->data;
410 int rc = CURLRESOLV_ERROR; /* default to failure */
414 /* Create an entry id, based upon the hostname and port */
415 entry_id = create_hostcache_id(hostname, port);
416 /* If we can't create the entry id, fail */
420 entry_len = strlen(entry_id);
423 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
425 /* See if its already in our dns cache */
426 dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
428 /* free the allocated entry_id again */
431 /* See whether the returned entry is stale. Done before we release lock */
432 if(remove_entry_if_stale(data, dns))
433 dns = NULL; /* the memory deallocation is being handled by the hash */
436 dns->inuse++; /* we use it! */
437 rc = CURLRESOLV_RESOLVED;
441 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
444 /* The entry was not in the cache. Resolve it to IP address */
449 /* Check what IP specifics the app has requested and if we can provide it.
450 * If not, bail out. */
451 if(!Curl_ipvalid(conn))
452 return CURLRESOLV_ERROR;
454 /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
455 non-zero value indicating that we need to wait for the response to the
457 addr = Curl_getaddrinfo(conn,
459 (data->set.str[STRING_DEVICE]
460 && !strcmp(data->set.str[STRING_DEVICE],
461 "LocalHost"))?"localhost":
463 hostname, port, &respwait);
467 /* the response to our resolve call will come asynchronously at
468 a later time, good or bad */
469 /* First, check that we haven't received the info by now */
470 result = Curl_resolver_is_resolved(conn, &dns);
471 if(result) /* error detected */
472 return CURLRESOLV_ERROR;
474 rc = CURLRESOLV_RESOLVED; /* pointer provided */
476 rc = CURLRESOLV_PENDING; /* no info yet */
481 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
483 /* we got a response, store it in the cache */
484 dns = Curl_cache_addr(data, addr, hostname, port);
487 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
490 /* returned failure, bail out nicely */
491 Curl_freeaddrinfo(addr);
493 rc = CURLRESOLV_RESOLVED;
502 #ifdef USE_ALARM_TIMEOUT
504 * This signal handler jumps back into the main libcurl code and continues
505 * execution. This effectively causes the remainder of the application to run
506 * within a signal handler which is nonportable and could lead to problems.
509 RETSIGTYPE alarmfunc(int sig)
511 /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
513 siglongjmp(curl_jmpenv, 1);
516 #endif /* USE_ALARM_TIMEOUT */
519 * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
520 * timeout. This function might return immediately if we're using asynch
521 * resolves. See the return codes.
523 * The cache entry we return will get its 'inuse' counter increased when this
524 * function is used. You MUST call Curl_resolv_unlock() later (when you're
525 * done using this struct) to decrease the counter again.
527 * If built with a synchronous resolver and use of signals is not
528 * disabled by the application, then a nonzero timeout will cause a
529 * timeout after the specified number of milliseconds. Otherwise, timeout
534 * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
535 * CURLRESOLV_ERROR (-1) = error, no pointer
536 * CURLRESOLV_RESOLVED (0) = OK, pointer provided
537 * CURLRESOLV_PENDING (1) = waiting for response, no pointer
540 int Curl_resolv_timeout(struct connectdata *conn,
541 const char *hostname,
543 struct Curl_dns_entry **entry,
546 #ifdef USE_ALARM_TIMEOUT
547 #ifdef HAVE_SIGACTION
548 struct sigaction keep_sigact; /* store the old struct here */
549 volatile bool keep_copysig = FALSE; /* wether old sigact has been saved */
550 struct sigaction sigact;
553 void (*keep_sigact)(int); /* store the old handler here */
554 #endif /* HAVE_SIGNAL */
555 #endif /* HAVE_SIGACTION */
556 volatile long timeout;
557 volatile unsigned int prev_alarm = 0;
558 struct SessionHandle *data = conn->data;
559 #endif /* USE_ALARM_TIMEOUT */
565 /* got an already expired timeout */
566 return CURLRESOLV_TIMEDOUT;
568 #ifdef USE_ALARM_TIMEOUT
569 if(data->set.no_signal)
570 /* Ignore the timeout when signals are disabled */
576 /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
577 return Curl_resolv(conn, hostname, port, entry);
580 /* The alarm() function only provides integer second resolution, so if
581 we want to wait less than one second we must bail out already now. */
582 return CURLRESOLV_TIMEDOUT;
584 /*************************************************************
585 * Set signal handler to catch SIGALRM
586 * Store the old value to be able to set it back later!
587 *************************************************************/
588 #ifdef HAVE_SIGACTION
589 sigaction(SIGALRM, NULL, &sigact);
590 keep_sigact = sigact;
591 keep_copysig = TRUE; /* yes, we have a copy */
592 sigact.sa_handler = alarmfunc;
594 /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
595 sigact.sa_flags &= ~SA_RESTART;
597 /* now set the new struct */
598 sigaction(SIGALRM, &sigact, NULL);
599 #else /* HAVE_SIGACTION */
600 /* no sigaction(), revert to the much lamer signal() */
602 keep_sigact = signal(SIGALRM, alarmfunc);
604 #endif /* HAVE_SIGACTION */
606 /* alarm() makes a signal get sent when the timeout fires off, and that
607 will abort system calls */
608 prev_alarm = alarm(curlx_sltoui(timeout/1000L));
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;
624 #ifndef CURLRES_ASYNCH
626 infof(conn->data, "timeout on name lookup is not supported\n");
628 (void)timeoutms; /* timeoutms not used with an async resolver */
630 #endif /* USE_ALARM_TIMEOUT */
632 /* Perform the actual name resolution. This might be interrupted by an
633 * alarm if it takes too long.
635 rc = Curl_resolv(conn, hostname, port, entry);
637 #ifdef USE_ALARM_TIMEOUT
641 /* deactivate a possibly active alarm before uninstalling the handler */
644 #ifdef HAVE_SIGACTION
646 /* we got a struct as it looked before, now put that one back nice
648 sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
652 /* restore the previous SIGALRM handler */
653 signal(SIGALRM, keep_sigact);
655 #endif /* HAVE_SIGACTION */
657 /* switch back the alarm() to either zero or to what it was before minus
658 the time we spent until now! */
660 /* there was an alarm() set before us, now put it back */
661 unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
663 /* the alarm period is counted in even number of seconds */
664 unsigned long alarm_set = prev_alarm - elapsed_ms/1000;
667 ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
668 /* if the alarm time-left reached zero or turned "negative" (counted
669 with unsigned values), we should fire off a SIGALRM here, but we
670 won't, and zero would be to switch it off so we never set it to
673 rc = CURLRESOLV_TIMEDOUT;
674 failf(data, "Previous alarm fired off!");
677 alarm((unsigned int)alarm_set);
679 #endif /* USE_ALARM_TIMEOUT */
685 * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
686 * made, the struct may be destroyed due to pruning. It is important that only
687 * one unlock is made for each Curl_resolv() call.
689 * May be called with 'data' == NULL for global cache.
691 void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
693 DEBUGASSERT(dns && (dns->inuse>0));
695 if(data && data->share)
696 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
699 /* only free if nobody is using AND it is not in hostcache (timestamp ==
701 if(dns->inuse == 0 && dns->timestamp == 0) {
702 Curl_freeaddrinfo(dns->addr);
706 if(data && data->share)
707 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
711 * File-internal: free a cache dns entry.
713 static void freednsentry(void *freethis)
715 struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
717 /* mark the entry as not in hostcache */
720 Curl_freeaddrinfo(p->addr);
726 * Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
728 struct curl_hash *Curl_mk_dnscache(void)
730 return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
733 static int hostcache_inuse(void *data, void *hc)
735 struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
738 Curl_resolv_unlock(data, c);
740 return 1; /* free all entries */
744 * Curl_hostcache_clean()
746 * This _can_ be called with 'data' == NULL but then of course no locking
750 void Curl_hostcache_clean(struct SessionHandle *data,
751 struct curl_hash *hash)
753 /* Entries added to the hostcache with the CURLOPT_RESOLVE function are
754 * still present in the cache with the inuse counter set to 1. Detect them
757 Curl_hash_clean_with_criterium(hash, data, hostcache_inuse);
761 CURLcode Curl_loadhostpairs(struct SessionHandle *data)
763 struct curl_slist *hostp;
768 for(hostp = data->change.resolve; hostp; hostp = hostp->next ) {
771 if(hostp->data[0] == '-') {
772 /* TODO: mark an entry for removal */
774 else if(3 == sscanf(hostp->data, "%255[^:]:%d:%255s", hostname, &port,
776 struct Curl_dns_entry *dns;
781 addr = Curl_str2addr(address, port);
783 infof(data, "Resolve %s found illegal!\n", hostp->data);
787 /* Create an entry id, based upon the hostname and port */
788 entry_id = create_hostcache_id(hostname, port);
789 /* If we can't create the entry id, fail */
791 Curl_freeaddrinfo(addr);
792 return CURLE_OUT_OF_MEMORY;
795 entry_len = strlen(entry_id);
798 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
800 /* See if its already in our dns cache */
801 dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
803 /* free the allocated entry_id again */
807 /* if not in the cache already, put this host in the cache */
808 dns = Curl_cache_addr(data, addr, hostname, port);
810 /* this is a duplicate, free it again */
811 Curl_freeaddrinfo(addr);
814 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
817 Curl_freeaddrinfo(addr);
818 return CURLE_OUT_OF_MEMORY;
820 infof(data, "Added %s:%d:%s to DNS cache\n",
821 hostname, port, address);
824 data->change.resolve = NULL; /* dealt with now */