2002-09-03 19:52:59 +08:00
|
|
|
/***************************************************************************
|
2004-06-10 19:56:02 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
1999-12-29 22:20:26 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2013-01-04 09:50:28 +08:00
|
|
|
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2002-09-03 19:52:59 +08:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
2004-06-10 19:56:02 +08:00
|
|
|
*
|
2001-01-03 17:29:33 +08:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
2002-09-03 19:52:59 +08:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2001-01-03 17:29:33 +08:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2002-09-03 19:52:59 +08:00
|
|
|
***************************************************************************/
|
1999-12-29 22:20:26 +08:00
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2000-08-24 22:26:33 +08:00
|
|
|
|
2000-08-23 15:23:42 +08:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
1999-12-29 22:20:26 +08:00
|
|
|
#include <netinet/in.h>
|
2000-08-23 15:23:42 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
1999-12-29 22:20:26 +08:00
|
|
|
#include <netdb.h>
|
2000-08-23 15:23:42 +08:00
|
|
|
#endif
|
1999-12-29 22:20:26 +08:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2009-12-31 01:59:56 +08:00
|
|
|
#ifdef __VMS
|
2002-02-20 21:46:53 +08:00
|
|
|
#include <in.h>
|
2001-08-06 20:19:26 +08:00
|
|
|
#include <inet.h>
|
|
|
|
#endif
|
1999-12-29 22:20:26 +08:00
|
|
|
|
2002-06-11 23:10:18 +08:00
|
|
|
#ifdef HAVE_SETJMP_H
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
2008-09-30 05:02:22 +08:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2002-06-11 23:10:18 +08:00
|
|
|
|
2006-04-27 01:23:28 +08:00
|
|
|
#ifdef HAVE_PROCESS_H
|
2004-04-06 22:51:14 +08:00
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "share.h"
|
|
|
|
#include "strerror.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "inet_ntop.h"
|
|
|
|
#include "warnless.h"
|
1999-12-29 22:20:26 +08:00
|
|
|
|
2002-01-17 16:03:48 +08:00
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
2009-04-21 19:46:16 +08:00
|
|
|
#include "curl_memory.h"
|
2000-10-09 19:12:34 +08:00
|
|
|
/* The last #include file should be: */
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2000-10-09 19:12:34 +08:00
|
|
|
|
2010-01-23 21:53:33 +08:00
|
|
|
#if defined(CURLRES_SYNCH) && \
|
|
|
|
defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP)
|
2008-09-30 05:02:22 +08:00
|
|
|
/* alarm-based timeouts can only be used with all the dependencies satisfied */
|
|
|
|
#define USE_ALARM_TIMEOUT
|
|
|
|
#endif
|
|
|
|
|
2004-04-26 15:20:11 +08:00
|
|
|
/*
|
2013-01-04 09:50:28 +08:00
|
|
|
* hostip.c explained
|
|
|
|
* ==================
|
2004-04-26 15:20:11 +08:00
|
|
|
*
|
2013-01-04 09:50:28 +08:00
|
|
|
* The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
|
2004-04-26 15:20:11 +08:00
|
|
|
* source file are these:
|
|
|
|
*
|
|
|
|
* CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
|
|
|
|
* that. The host may not be able to resolve IPv6, but we don't really have to
|
|
|
|
* take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
|
|
|
|
* defined.
|
|
|
|
*
|
|
|
|
* CURLRES_ARES - is defined if libcurl is built to use c-ares for
|
2006-07-25 18:23:15 +08:00
|
|
|
* asynchronous name resolves. This can be Windows or *nix.
|
2004-04-26 15:20:11 +08:00
|
|
|
*
|
|
|
|
* CURLRES_THREADED - is defined if libcurl is built to run under (native)
|
|
|
|
* Windows, and then the name resolve will be done in a new thread, and the
|
|
|
|
* supported API will be the same as for ares-builds.
|
|
|
|
*
|
|
|
|
* If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
|
|
|
|
* libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
|
|
|
|
* defined.
|
|
|
|
*
|
2013-01-04 09:50:28 +08:00
|
|
|
* The host*.c sources files are split up like this:
|
2004-04-26 15:20:11 +08:00
|
|
|
*
|
2013-01-04 09:50:28 +08:00
|
|
|
* hostip.c - method-independent resolver functions and utility functions
|
|
|
|
* hostasyn.c - functions for asynchronous name resolves
|
|
|
|
* hostsyn.c - functions for synchronous name resolves
|
|
|
|
* hostip4.c - ipv4-specific functions
|
|
|
|
* hostip6.c - ipv6-specific functions
|
2004-04-26 15:20:11 +08:00
|
|
|
*
|
2011-04-26 01:50:56 +08:00
|
|
|
* The two asynchronous name resolver backends are implemented in:
|
2013-01-04 09:50:28 +08:00
|
|
|
* asyn-ares.c - functions for ares-using name resolves
|
|
|
|
* asyn-thread.c - functions for threaded name resolves
|
2011-04-26 01:50:56 +08:00
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
* The hostip.h is the united header file for all this. It defines the
|
2013-01-07 02:06:49 +08:00
|
|
|
* CURLRES_* defines based on the config*.h and curl_setup.h defines.
|
2004-04-26 15:20:11 +08:00
|
|
|
*/
|
2004-03-31 20:55:24 +08:00
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/* These two symbols are for the global DNS cache */
|
2005-01-25 08:06:29 +08:00
|
|
|
static struct curl_hash hostname_cache;
|
2002-01-08 04:52:32 +08:00
|
|
|
static int host_cache_initialized;
|
2002-01-03 18:22:59 +08:00
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
static void freednsentry(void *freethis);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_global_host_cache_init() initializes and sets up a global DNS cache.
|
|
|
|
* Global DNS cache is general badness. Do not use. This will be removed in
|
|
|
|
* a future version. Use the share interface instead!
|
2008-01-16 06:15:55 +08:00
|
|
|
*
|
2008-01-16 06:44:12 +08:00
|
|
|
* Returns a struct curl_hash pointer on success, NULL on failure.
|
2004-03-30 21:02:07 +08:00
|
|
|
*/
|
2008-01-16 06:44:12 +08:00
|
|
|
struct curl_hash *Curl_global_host_cache_init(void)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
2008-01-16 06:15:55 +08:00
|
|
|
int rc = 0;
|
2007-11-07 17:21:35 +08:00
|
|
|
if(!host_cache_initialized) {
|
2008-01-16 06:15:55 +08:00
|
|
|
rc = Curl_hash_init(&hostname_cache, 7, Curl_hash_str,
|
|
|
|
Curl_str_key_compare, freednsentry);
|
|
|
|
if(!rc)
|
|
|
|
host_cache_initialized = 1;
|
2002-01-08 04:52:32 +08:00
|
|
|
}
|
2008-01-16 06:44:12 +08:00
|
|
|
return rc?NULL:&hostname_cache;
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
|
|
|
* Destroy and cleanup the global DNS cache
|
|
|
|
*/
|
2002-01-08 04:52:32 +08:00
|
|
|
void Curl_global_host_cache_dtor(void)
|
|
|
|
{
|
2007-11-07 17:21:35 +08:00
|
|
|
if(host_cache_initialized) {
|
2002-04-27 21:07:51 +08:00
|
|
|
Curl_hash_clean(&hostname_cache);
|
2002-01-08 04:55:35 +08:00
|
|
|
host_cache_initialized = 0;
|
2002-01-08 04:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
2002-01-03 18:22:59 +08:00
|
|
|
|
2004-06-10 19:06:21 +08:00
|
|
|
/*
|
|
|
|
* Return # of adresses in a Curl_addrinfo struct
|
|
|
|
*/
|
|
|
|
int Curl_num_addresses(const Curl_addrinfo *addr)
|
|
|
|
{
|
2008-10-17 20:53:53 +08:00
|
|
|
int i = 0;
|
|
|
|
while(addr) {
|
|
|
|
addr = addr->ai_next;
|
|
|
|
i++;
|
|
|
|
}
|
2004-06-24 15:43:48 +08:00
|
|
|
return i;
|
2004-06-10 19:06:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-06-10 19:55:37 +08:00
|
|
|
* Curl_printable_address() returns a printable version of the 1st address
|
2008-10-19 09:02:18 +08:00
|
|
|
* given in the 'ai' argument. The result will be stored in the buf that is
|
2004-06-10 19:55:37 +08:00
|
|
|
* bufsize bytes big.
|
2004-06-10 19:06:21 +08:00
|
|
|
*
|
|
|
|
* If the conversion fails, it returns NULL.
|
|
|
|
*/
|
2008-10-19 09:02:18 +08:00
|
|
|
const char *
|
|
|
|
Curl_printable_address(const Curl_addrinfo *ai, char *buf, size_t bufsize)
|
2004-06-10 19:06:21 +08:00
|
|
|
{
|
2008-10-20 06:46:54 +08:00
|
|
|
const struct sockaddr_in *sa4;
|
|
|
|
const struct in_addr *ipaddr4;
|
2008-10-19 09:02:18 +08:00
|
|
|
#ifdef ENABLE_IPV6
|
2008-10-20 06:46:54 +08:00
|
|
|
const struct sockaddr_in6 *sa6;
|
|
|
|
const struct in6_addr *ipaddr6;
|
2004-06-10 19:06:21 +08:00
|
|
|
#endif
|
2004-06-24 15:43:48 +08:00
|
|
|
|
2008-10-19 09:02:18 +08:00
|
|
|
switch (ai->ai_family) {
|
|
|
|
case AF_INET:
|
2008-10-20 06:46:54 +08:00
|
|
|
sa4 = (const void *)ai->ai_addr;
|
2008-10-19 09:02:18 +08:00
|
|
|
ipaddr4 = &sa4->sin_addr;
|
2010-01-23 07:21:39 +08:00
|
|
|
return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf,
|
|
|
|
bufsize);
|
2008-10-19 09:02:18 +08:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
case AF_INET6:
|
2008-10-20 06:46:54 +08:00
|
|
|
sa6 = (const void *)ai->ai_addr;
|
2008-10-19 09:02:18 +08:00
|
|
|
ipaddr6 = &sa6->sin6_addr;
|
2010-01-23 07:21:39 +08:00
|
|
|
return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf,
|
|
|
|
bufsize);
|
2008-10-19 09:02:18 +08:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
2004-06-10 19:06:21 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
2011-12-31 05:46:57 +08:00
|
|
|
* Return a hostcache id string for the provided host + port, to be used by
|
2004-04-26 15:20:11 +08:00
|
|
|
* the DNS caching.
|
2004-03-30 21:02:07 +08:00
|
|
|
*/
|
2002-01-17 14:55:37 +08:00
|
|
|
static char *
|
2011-12-31 05:46:57 +08:00
|
|
|
create_hostcache_id(const char *name, int port)
|
2002-01-17 14:55:37 +08:00
|
|
|
{
|
2004-06-24 19:54:11 +08:00
|
|
|
/* create and return the new allocated entry */
|
2011-12-31 05:46:57 +08:00
|
|
|
char *id = aprintf("%s:%d", name, port);
|
|
|
|
char *ptr = id;
|
|
|
|
if(ptr) {
|
|
|
|
/* lower case the name part */
|
2012-01-02 20:44:56 +08:00
|
|
|
while(*ptr && (*ptr != ':')) {
|
2011-12-31 05:46:57 +08:00
|
|
|
*ptr = (char)TOLOWER(*ptr);
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id;
|
2002-01-17 14:55:37 +08:00
|
|
|
}
|
|
|
|
|
2002-04-27 21:07:51 +08:00
|
|
|
struct hostcache_prune_data {
|
2007-08-26 13:53:26 +08:00
|
|
|
long cache_timeout;
|
2004-03-10 17:36:43 +08:00
|
|
|
time_t now;
|
2002-04-18 04:13:55 +08:00
|
|
|
};
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
|
|
|
* This function is set as a callback to be called for every entry in the DNS
|
|
|
|
* cache when we want to prune old unused entries.
|
|
|
|
*
|
|
|
|
* Returning non-zero means remove the entry, return 0 to keep it in the
|
|
|
|
* cache.
|
|
|
|
*/
|
2002-04-18 04:13:55 +08:00
|
|
|
static int
|
2002-11-05 18:51:41 +08:00
|
|
|
hostcache_timestamp_remove(void *datap, void *hc)
|
2002-04-18 04:13:55 +08:00
|
|
|
{
|
2004-06-10 19:56:02 +08:00
|
|
|
struct hostcache_prune_data *data =
|
2002-04-27 21:07:51 +08:00
|
|
|
(struct hostcache_prune_data *) datap;
|
2002-11-05 18:51:41 +08:00
|
|
|
struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
|
2004-06-10 19:56:02 +08:00
|
|
|
|
2009-11-11 17:31:37 +08:00
|
|
|
return (data->now - c->timestamp >= data->cache_timeout);
|
2002-04-18 04:13:55 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
|
|
|
* Prune the DNS cache. This assumes that a lock has already been taken.
|
|
|
|
*/
|
2002-04-18 04:13:55 +08:00
|
|
|
static void
|
2007-08-26 13:53:26 +08:00
|
|
|
hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
|
2002-04-18 04:13:55 +08:00
|
|
|
{
|
2002-04-27 21:07:51 +08:00
|
|
|
struct hostcache_prune_data user;
|
2002-04-18 04:13:55 +08:00
|
|
|
|
|
|
|
user.cache_timeout = cache_timeout;
|
|
|
|
user.now = now;
|
2004-02-16 00:57:53 +08:00
|
|
|
|
2004-06-10 19:56:02 +08:00
|
|
|
Curl_hash_clean_with_criterium(hostcache,
|
|
|
|
(void *) &user,
|
2002-11-05 18:51:41 +08:00
|
|
|
hostcache_timestamp_remove);
|
2002-04-18 04:13:55 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
|
|
|
* Library-wide function for pruning the DNS cache. This function takes and
|
|
|
|
* returns the appropriate locks.
|
|
|
|
*/
|
2003-12-16 01:33:49 +08:00
|
|
|
void Curl_hostcache_prune(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
time_t now;
|
2004-02-18 15:56:18 +08:00
|
|
|
|
2006-07-08 06:58:06 +08:00
|
|
|
if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
|
2004-10-04 18:36:51 +08:00
|
|
|
/* cache forever means never prune, and NULL hostcache means
|
|
|
|
we can't do it */
|
2004-02-18 15:56:18 +08:00
|
|
|
return;
|
|
|
|
|
2003-12-16 01:33:49 +08:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
/* Remove outdated and unused entries from the hostcache */
|
2006-07-08 06:58:06 +08:00
|
|
|
hostcache_prune(data->dns.hostcache,
|
2003-12-16 01:33:49 +08:00
|
|
|
data->set.dns_cache_timeout,
|
|
|
|
now);
|
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
}
|
|
|
|
|
2008-03-12 06:55:23 +08:00
|
|
|
/*
|
|
|
|
* Check if the entry should be pruned. Assumes a locked cache.
|
|
|
|
*/
|
2006-02-17 07:42:32 +08:00
|
|
|
static int
|
|
|
|
remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
|
|
|
|
{
|
|
|
|
struct hostcache_prune_data user;
|
|
|
|
|
2011-04-23 05:01:30 +08:00
|
|
|
if(!dns || (data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
|
2006-02-17 07:42:32 +08:00
|
|
|
/* cache forever means never prune, and NULL hostcache means
|
|
|
|
we can't do it */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
time(&user.now);
|
|
|
|
user.cache_timeout = data->set.dns_cache_timeout;
|
|
|
|
|
2011-04-23 05:01:30 +08:00
|
|
|
if(!hostcache_timestamp_remove(&user,dns) )
|
2006-02-17 07:42:32 +08:00
|
|
|
return 0;
|
|
|
|
|
2006-07-08 06:58:06 +08:00
|
|
|
Curl_hash_clean_with_criterium(data->dns.hostcache,
|
2006-02-17 07:42:32 +08:00
|
|
|
(void *) &user,
|
|
|
|
hostcache_timestamp_remove);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-05 22:40:59 +08:00
|
|
|
#ifdef HAVE_SIGSETJMP
|
2004-03-30 21:02:07 +08:00
|
|
|
/* Beware this is a global and unique instance. This is used to store the
|
|
|
|
return address that we can jump back to from inside a signal handler. This
|
|
|
|
is not thread-safe stuff. */
|
2003-08-05 22:40:59 +08:00
|
|
|
sigjmp_buf curl_jmpenv;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
2004-04-26 15:20:11 +08:00
|
|
|
* Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
|
2004-03-30 21:02:07 +08:00
|
|
|
*
|
|
|
|
* When calling Curl_resolv() has resulted in a response with a returned
|
|
|
|
* address, we call this function to store the information in the dns
|
|
|
|
* cache etc
|
|
|
|
*
|
|
|
|
* Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
|
|
|
|
*/
|
2004-04-26 15:20:11 +08:00
|
|
|
struct Curl_dns_entry *
|
|
|
|
Curl_cache_addr(struct SessionHandle *data,
|
|
|
|
Curl_addrinfo *addr,
|
2006-07-21 13:51:12 +08:00
|
|
|
const char *hostname,
|
2004-04-26 15:20:11 +08:00
|
|
|
int port)
|
2002-11-05 18:51:41 +08:00
|
|
|
{
|
2003-08-05 22:40:59 +08:00
|
|
|
char *entry_id;
|
2004-02-23 16:35:57 +08:00
|
|
|
size_t entry_len;
|
2003-08-05 22:40:59 +08:00
|
|
|
struct Curl_dns_entry *dns;
|
2004-05-04 21:40:30 +08:00
|
|
|
struct Curl_dns_entry *dns2;
|
2003-08-05 22:40:59 +08:00
|
|
|
|
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2004-06-24 19:54:11 +08:00
|
|
|
entry_id = create_hostcache_id(hostname, port);
|
2003-08-05 22:40:59 +08:00
|
|
|
/* If we can't create the entry id, fail */
|
2007-11-07 17:21:35 +08:00
|
|
|
if(!entry_id)
|
2003-08-05 22:40:59 +08:00
|
|
|
return NULL;
|
2004-06-24 19:54:11 +08:00
|
|
|
entry_len = strlen(entry_id);
|
2003-08-05 22:40:59 +08:00
|
|
|
|
|
|
|
/* Create a new cache entry */
|
2009-11-18 18:33:54 +08:00
|
|
|
dns = calloc(1, sizeof(struct Curl_dns_entry));
|
2007-11-07 17:21:35 +08:00
|
|
|
if(!dns) {
|
2003-08-05 22:40:59 +08:00
|
|
|
free(entry_id);
|
|
|
|
return NULL;
|
2002-11-05 18:51:41 +08:00
|
|
|
}
|
|
|
|
|
2003-09-15 05:17:54 +08:00
|
|
|
dns->inuse = 0; /* init to not used */
|
|
|
|
dns->addr = addr; /* this is the address(es) */
|
2009-11-11 17:31:37 +08:00
|
|
|
time(&dns->timestamp);
|
|
|
|
if(dns->timestamp == 0)
|
|
|
|
dns->timestamp = 1; /* zero indicates that entry isn't in hash table */
|
2003-09-15 05:17:54 +08:00
|
|
|
|
2009-11-11 17:31:37 +08:00
|
|
|
/* Store the resolved data in our DNS cache. */
|
2006-07-08 06:58:06 +08:00
|
|
|
dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
|
|
|
|
(void *)dns);
|
2004-05-04 21:40:30 +08:00
|
|
|
if(!dns2) {
|
|
|
|
free(dns);
|
2003-09-15 05:17:54 +08:00
|
|
|
free(entry_id);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-08-05 22:40:59 +08:00
|
|
|
|
2009-11-11 17:31:37 +08:00
|
|
|
dns = dns2;
|
2003-08-05 22:40:59 +08:00
|
|
|
dns->inuse++; /* mark entry as in-use */
|
|
|
|
|
2010-11-27 01:18:26 +08:00
|
|
|
/* free the allocated entry_id */
|
2003-08-05 22:40:59 +08:00
|
|
|
free(entry_id);
|
|
|
|
|
|
|
|
return dns;
|
2002-01-17 15:38:25 +08:00
|
|
|
}
|
2002-01-17 14:55:37 +08:00
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
|
|
|
* Curl_resolv() is the main name resolve function within libcurl. It resolves
|
|
|
|
* a name and returns a pointer to the entry in the 'entry' argument (if one
|
|
|
|
* is provided). This function might return immediately if we're using asynch
|
|
|
|
* resolves. See the return codes.
|
|
|
|
*
|
|
|
|
* The cache entry we return will get its 'inuse' counter increased when this
|
|
|
|
* function is used. You MUST call Curl_resolv_unlock() later (when you're
|
|
|
|
* done using this struct) to decrease the counter again.
|
|
|
|
*
|
2009-09-01 22:27:01 +08:00
|
|
|
* In debug mode, we specifically test for an interface name "LocalHost"
|
|
|
|
* and resolve "localhost" instead as a means to permit test cases
|
|
|
|
* to connect to a local test server with any host name.
|
|
|
|
*
|
2004-03-30 21:02:07 +08:00
|
|
|
* Return codes:
|
|
|
|
*
|
2004-04-26 22:18:00 +08:00
|
|
|
* CURLRESOLV_ERROR (-1) = error, no pointer
|
|
|
|
* CURLRESOLV_RESOLVED (0) = OK, pointer provided
|
|
|
|
* CURLRESOLV_PENDING (1) = waiting for response, no pointer
|
2004-03-30 21:02:07 +08:00
|
|
|
*/
|
2004-04-26 22:18:00 +08:00
|
|
|
|
2003-08-05 22:40:59 +08:00
|
|
|
int Curl_resolv(struct connectdata *conn,
|
2006-07-21 13:51:12 +08:00
|
|
|
const char *hostname,
|
2003-08-05 22:40:59 +08:00
|
|
|
int port,
|
|
|
|
struct Curl_dns_entry **entry)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
2002-01-17 15:38:25 +08:00
|
|
|
char *entry_id = NULL;
|
2002-11-05 18:51:41 +08:00
|
|
|
struct Curl_dns_entry *dns = NULL;
|
2004-02-23 16:35:57 +08:00
|
|
|
size_t entry_len;
|
2003-08-05 22:40:59 +08:00
|
|
|
struct SessionHandle *data = conn->data;
|
2003-11-14 19:56:15 +08:00
|
|
|
CURLcode result;
|
2008-03-12 06:55:23 +08:00
|
|
|
int rc = CURLRESOLV_ERROR; /* default to failure */
|
2002-01-08 12:26:47 +08:00
|
|
|
|
2008-09-30 05:02:22 +08:00
|
|
|
*entry = NULL;
|
2002-06-11 23:10:18 +08:00
|
|
|
|
2002-01-17 15:38:25 +08:00
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2004-06-24 19:54:11 +08:00
|
|
|
entry_id = create_hostcache_id(hostname, port);
|
2002-11-05 18:51:41 +08:00
|
|
|
/* If we can't create the entry id, fail */
|
2007-11-07 17:21:35 +08:00
|
|
|
if(!entry_id)
|
2008-03-12 06:55:23 +08:00
|
|
|
return rc;
|
2003-02-05 07:48:46 +08:00
|
|
|
|
2004-06-24 19:54:11 +08:00
|
|
|
entry_len = strlen(entry_id);
|
|
|
|
|
2003-02-05 07:48:46 +08:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2002-01-08 12:26:47 +08:00
|
|
|
/* See if its already in our dns cache */
|
2006-07-08 06:58:06 +08:00
|
|
|
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
|
2004-06-10 19:56:02 +08:00
|
|
|
|
2010-11-06 05:31:40 +08:00
|
|
|
/* free the allocated entry_id again */
|
|
|
|
free(entry_id);
|
|
|
|
|
2008-03-12 06:55:23 +08:00
|
|
|
/* See whether the returned entry is stale. Done before we release lock */
|
2011-04-23 05:01:30 +08:00
|
|
|
if(remove_entry_if_stale(data, dns))
|
2008-03-12 06:55:23 +08:00
|
|
|
dns = NULL; /* the memory deallocation is being handled by the hash */
|
|
|
|
|
|
|
|
if(dns) {
|
|
|
|
dns->inuse++; /* we use it! */
|
|
|
|
rc = CURLRESOLV_RESOLVED;
|
|
|
|
}
|
|
|
|
|
2003-10-12 21:57:41 +08:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2002-01-08 12:26:47 +08:00
|
|
|
|
2007-11-07 17:21:35 +08:00
|
|
|
if(!dns) {
|
2003-08-05 22:40:59 +08:00
|
|
|
/* The entry was not in the cache. Resolve it to IP address */
|
2004-04-26 15:20:11 +08:00
|
|
|
|
|
|
|
Curl_addrinfo *addr;
|
2007-09-29 02:47:59 +08:00
|
|
|
int respwait;
|
2004-04-26 15:20:11 +08:00
|
|
|
|
|
|
|
/* Check what IP specifics the app has requested and if we can provide it.
|
|
|
|
* If not, bail out. */
|
2010-11-11 21:51:39 +08:00
|
|
|
if(!Curl_ipvalid(conn))
|
2004-04-26 22:18:00 +08:00
|
|
|
return CURLRESOLV_ERROR;
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2007-09-29 02:47:59 +08:00
|
|
|
/* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
|
|
|
|
non-zero value indicating that we need to wait for the response to the
|
|
|
|
resolve call */
|
2009-09-01 22:27:01 +08:00
|
|
|
addr = Curl_getaddrinfo(conn,
|
|
|
|
#ifdef DEBUGBUILD
|
|
|
|
(data->set.str[STRING_DEVICE]
|
|
|
|
&& !strcmp(data->set.str[STRING_DEVICE],
|
|
|
|
"LocalHost"))?"localhost":
|
|
|
|
#endif
|
|
|
|
hostname, port, &respwait);
|
2004-06-10 19:56:02 +08:00
|
|
|
|
2007-11-07 17:21:35 +08:00
|
|
|
if(!addr) {
|
2007-09-29 02:47:59 +08:00
|
|
|
if(respwait) {
|
2004-06-10 19:56:02 +08:00
|
|
|
/* the response to our resolve call will come asynchronously at
|
2003-08-05 22:40:59 +08:00
|
|
|
a later time, good or bad */
|
2003-10-04 22:50:45 +08:00
|
|
|
/* First, check that we haven't received the info by now */
|
2011-01-31 07:10:35 +08:00
|
|
|
result = Curl_resolver_is_resolved(conn, &dns);
|
2003-11-14 19:56:15 +08:00
|
|
|
if(result) /* error detected */
|
2004-04-26 22:18:00 +08:00
|
|
|
return CURLRESOLV_ERROR;
|
2003-10-04 22:50:45 +08:00
|
|
|
if(dns)
|
2004-04-26 22:18:00 +08:00
|
|
|
rc = CURLRESOLV_RESOLVED; /* pointer provided */
|
2003-10-04 22:50:45 +08:00
|
|
|
else
|
2004-04-26 22:18:00 +08:00
|
|
|
rc = CURLRESOLV_PENDING; /* no info yet */
|
2003-10-04 22:50:45 +08:00
|
|
|
}
|
2002-11-05 18:51:41 +08:00
|
|
|
}
|
2004-01-14 17:05:54 +08:00
|
|
|
else {
|
2003-10-12 21:57:41 +08:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2003-08-05 22:40:59 +08:00
|
|
|
/* we got a response, store it in the cache */
|
2004-04-26 15:20:11 +08:00
|
|
|
dns = Curl_cache_addr(data, addr, hostname, port);
|
2004-06-10 19:56:02 +08:00
|
|
|
|
2003-10-12 21:57:41 +08:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2003-10-24 20:57:23 +08:00
|
|
|
|
2004-01-14 17:05:54 +08:00
|
|
|
if(!dns)
|
2003-10-24 20:57:23 +08:00
|
|
|
/* returned failure, bail out nicely */
|
|
|
|
Curl_freeaddrinfo(addr);
|
2003-12-03 15:55:51 +08:00
|
|
|
else
|
2004-04-26 22:18:00 +08:00
|
|
|
rc = CURLRESOLV_RESOLVED;
|
2003-10-12 21:57:41 +08:00
|
|
|
}
|
2002-01-17 15:38:25 +08:00
|
|
|
}
|
2002-01-08 12:26:47 +08:00
|
|
|
|
2003-08-05 22:40:59 +08:00
|
|
|
*entry = dns;
|
2002-01-03 18:22:59 +08:00
|
|
|
|
2003-08-05 22:40:59 +08:00
|
|
|
return rc;
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
|
|
|
|
2009-09-01 22:27:01 +08:00
|
|
|
#ifdef USE_ALARM_TIMEOUT
|
2008-09-30 05:02:22 +08:00
|
|
|
/*
|
|
|
|
* This signal handler jumps back into the main libcurl code and continues
|
|
|
|
* execution. This effectively causes the remainder of the application to run
|
|
|
|
* within a signal handler which is nonportable and could lead to problems.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
RETSIGTYPE alarmfunc(int sig)
|
|
|
|
{
|
|
|
|
/* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
|
|
|
|
(void)sig;
|
|
|
|
siglongjmp(curl_jmpenv, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* USE_ALARM_TIMEOUT */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
|
|
|
|
* timeout. This function might return immediately if we're using asynch
|
|
|
|
* resolves. See the return codes.
|
|
|
|
*
|
|
|
|
* The cache entry we return will get its 'inuse' counter increased when this
|
|
|
|
* function is used. You MUST call Curl_resolv_unlock() later (when you're
|
|
|
|
* done using this struct) to decrease the counter again.
|
|
|
|
*
|
|
|
|
* If built with a synchronous resolver and use of signals is not
|
|
|
|
* disabled by the application, then a nonzero timeout will cause a
|
|
|
|
* timeout after the specified number of milliseconds. Otherwise, timeout
|
|
|
|
* is ignored.
|
|
|
|
*
|
|
|
|
* Return codes:
|
|
|
|
*
|
|
|
|
* CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
|
|
|
|
* CURLRESOLV_ERROR (-1) = error, no pointer
|
|
|
|
* CURLRESOLV_RESOLVED (0) = OK, pointer provided
|
|
|
|
* CURLRESOLV_PENDING (1) = waiting for response, no pointer
|
|
|
|
*/
|
|
|
|
|
|
|
|
int Curl_resolv_timeout(struct connectdata *conn,
|
|
|
|
const char *hostname,
|
|
|
|
int port,
|
|
|
|
struct Curl_dns_entry **entry,
|
2008-10-03 06:58:09 +08:00
|
|
|
long timeoutms)
|
2008-09-30 05:02:22 +08:00
|
|
|
{
|
2009-09-01 22:27:01 +08:00
|
|
|
#ifdef USE_ALARM_TIMEOUT
|
2008-09-30 05:02:22 +08:00
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
struct sigaction keep_sigact; /* store the old struct here */
|
2010-04-13 22:34:13 +08:00
|
|
|
volatile bool keep_copysig = FALSE; /* wether old sigact has been saved */
|
2008-09-30 05:02:22 +08:00
|
|
|
struct sigaction sigact;
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_SIGNAL
|
|
|
|
void (*keep_sigact)(int); /* store the old handler here */
|
|
|
|
#endif /* HAVE_SIGNAL */
|
|
|
|
#endif /* HAVE_SIGACTION */
|
2008-10-03 06:58:09 +08:00
|
|
|
volatile long timeout;
|
2010-04-13 22:34:13 +08:00
|
|
|
volatile unsigned int prev_alarm = 0;
|
2008-09-30 07:45:43 +08:00
|
|
|
struct SessionHandle *data = conn->data;
|
2008-09-30 05:02:22 +08:00
|
|
|
#endif /* USE_ALARM_TIMEOUT */
|
2008-10-02 01:34:24 +08:00
|
|
|
int rc;
|
2008-09-30 05:02:22 +08:00
|
|
|
|
|
|
|
*entry = NULL;
|
|
|
|
|
2011-09-01 23:09:46 +08:00
|
|
|
if(timeoutms < 0)
|
|
|
|
/* got an already expired timeout */
|
|
|
|
return CURLRESOLV_TIMEDOUT;
|
|
|
|
|
2009-09-01 22:27:01 +08:00
|
|
|
#ifdef USE_ALARM_TIMEOUT
|
2011-04-20 21:17:42 +08:00
|
|
|
if(data->set.no_signal)
|
2008-09-30 05:02:22 +08:00
|
|
|
/* Ignore the timeout when signals are disabled */
|
|
|
|
timeout = 0;
|
2008-10-03 06:58:09 +08:00
|
|
|
else
|
|
|
|
timeout = timeoutms;
|
2008-09-30 05:02:22 +08:00
|
|
|
|
2010-04-05 05:35:21 +08:00
|
|
|
if(!timeout)
|
|
|
|
/* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
|
|
|
|
return Curl_resolv(conn, hostname, port, entry);
|
|
|
|
|
|
|
|
if(timeout < 1000)
|
2008-09-30 05:02:22 +08:00
|
|
|
/* The alarm() function only provides integer second resolution, so if
|
|
|
|
we want to wait less than one second we must bail out already now. */
|
|
|
|
return CURLRESOLV_TIMEDOUT;
|
|
|
|
|
2010-04-05 05:35:21 +08:00
|
|
|
/*************************************************************
|
|
|
|
* Set signal handler to catch SIGALRM
|
|
|
|
* Store the old value to be able to set it back later!
|
|
|
|
*************************************************************/
|
2008-09-30 05:02:22 +08:00
|
|
|
#ifdef HAVE_SIGACTION
|
2010-04-05 05:35:21 +08:00
|
|
|
sigaction(SIGALRM, NULL, &sigact);
|
|
|
|
keep_sigact = sigact;
|
|
|
|
keep_copysig = TRUE; /* yes, we have a copy */
|
|
|
|
sigact.sa_handler = alarmfunc;
|
2008-09-30 05:02:22 +08:00
|
|
|
#ifdef SA_RESTART
|
2010-04-05 05:35:21 +08:00
|
|
|
/* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
|
|
|
|
sigact.sa_flags &= ~SA_RESTART;
|
2008-09-30 05:02:22 +08:00
|
|
|
#endif
|
2010-04-05 05:35:21 +08:00
|
|
|
/* now set the new struct */
|
|
|
|
sigaction(SIGALRM, &sigact, NULL);
|
2008-09-30 05:02:22 +08:00
|
|
|
#else /* HAVE_SIGACTION */
|
2010-04-05 05:35:21 +08:00
|
|
|
/* no sigaction(), revert to the much lamer signal() */
|
2008-09-30 05:02:22 +08:00
|
|
|
#ifdef HAVE_SIGNAL
|
2010-04-05 05:35:21 +08:00
|
|
|
keep_sigact = signal(SIGALRM, alarmfunc);
|
2008-09-30 05:02:22 +08:00
|
|
|
#endif
|
|
|
|
#endif /* HAVE_SIGACTION */
|
|
|
|
|
2010-04-05 05:35:21 +08:00
|
|
|
/* alarm() makes a signal get sent when the timeout fires off, and that
|
|
|
|
will abort system calls */
|
2010-12-02 06:33:43 +08:00
|
|
|
prev_alarm = alarm(curlx_sltoui(timeout/1000L));
|
2010-04-05 05:35:21 +08:00
|
|
|
|
|
|
|
/* This allows us to time-out from the name resolver, as the timeout
|
|
|
|
will generate a signal and we will siglongjmp() from that here.
|
|
|
|
This technique has problems (see alarmfunc).
|
|
|
|
This should be the last thing we do before calling Curl_resolv(),
|
|
|
|
as otherwise we'd have to worry about variables that get modified
|
|
|
|
before we invoke Curl_resolv() (and thus use "volatile"). */
|
|
|
|
if(sigsetjmp(curl_jmpenv, 1)) {
|
|
|
|
/* this is coming from a siglongjmp() after an alarm signal */
|
|
|
|
failf(data, "name lookup timed out");
|
|
|
|
rc = CURLRESOLV_ERROR;
|
|
|
|
goto clean_up;
|
2008-09-30 05:02:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#ifndef CURLRES_ASYNCH
|
2008-10-03 10:25:52 +08:00
|
|
|
if(timeoutms)
|
2008-09-30 07:45:43 +08:00
|
|
|
infof(conn->data, "timeout on name lookup is not supported\n");
|
|
|
|
#else
|
2008-10-03 08:35:22 +08:00
|
|
|
(void)timeoutms; /* timeoutms not used with an async resolver */
|
2008-09-30 05:02:22 +08:00
|
|
|
#endif
|
|
|
|
#endif /* USE_ALARM_TIMEOUT */
|
|
|
|
|
|
|
|
/* Perform the actual name resolution. This might be interrupted by an
|
|
|
|
* alarm if it takes too long.
|
|
|
|
*/
|
|
|
|
rc = Curl_resolv(conn, hostname, port, entry);
|
|
|
|
|
2010-03-25 00:00:20 +08:00
|
|
|
#ifdef USE_ALARM_TIMEOUT
|
2010-03-23 04:57:48 +08:00
|
|
|
clean_up:
|
|
|
|
|
2010-04-05 05:35:21 +08:00
|
|
|
if(!prev_alarm)
|
|
|
|
/* deactivate a possibly active alarm before uninstalling the handler */
|
|
|
|
alarm(0);
|
2008-09-30 05:02:22 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_SIGACTION
|
2010-04-05 05:35:21 +08:00
|
|
|
if(keep_copysig) {
|
|
|
|
/* we got a struct as it looked before, now put that one back nice
|
|
|
|
and clean */
|
|
|
|
sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
|
|
|
|
}
|
2008-09-30 05:02:22 +08:00
|
|
|
#else
|
|
|
|
#ifdef HAVE_SIGNAL
|
2010-04-05 05:35:21 +08:00
|
|
|
/* restore the previous SIGALRM handler */
|
|
|
|
signal(SIGALRM, keep_sigact);
|
2008-09-30 05:02:22 +08:00
|
|
|
#endif
|
|
|
|
#endif /* HAVE_SIGACTION */
|
|
|
|
|
2010-04-05 05:35:21 +08:00
|
|
|
/* switch back the alarm() to either zero or to what it was before minus
|
|
|
|
the time we spent until now! */
|
|
|
|
if(prev_alarm) {
|
|
|
|
/* there was an alarm() set before us, now put it back */
|
|
|
|
unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
|
|
|
|
|
|
|
|
/* the alarm period is counted in even number of seconds */
|
|
|
|
unsigned long alarm_set = prev_alarm - elapsed_ms/1000;
|
|
|
|
|
|
|
|
if(!alarm_set ||
|
|
|
|
((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
|
|
|
|
/* if the alarm time-left reached zero or turned "negative" (counted
|
|
|
|
with unsigned values), we should fire off a SIGALRM here, but we
|
|
|
|
won't, and zero would be to switch it off so we never set it to
|
|
|
|
less than 1! */
|
|
|
|
alarm(1);
|
|
|
|
rc = CURLRESOLV_TIMEDOUT;
|
|
|
|
failf(data, "Previous alarm fired off!");
|
2008-09-30 05:02:22 +08:00
|
|
|
}
|
2010-04-05 05:35:21 +08:00
|
|
|
else
|
|
|
|
alarm((unsigned int)alarm_set);
|
2008-09-30 05:02:22 +08:00
|
|
|
}
|
|
|
|
#endif /* USE_ALARM_TIMEOUT */
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
|
|
|
* Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
|
|
|
|
* made, the struct may be destroyed due to pruning. It is important that only
|
|
|
|
* one unlock is made for each Curl_resolv() call.
|
|
|
|
*/
|
2003-02-05 07:48:46 +08:00
|
|
|
void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
|
|
|
|
{
|
2007-02-22 03:03:20 +08:00
|
|
|
DEBUGASSERT(dns && (dns->inuse>0));
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2003-02-05 07:48:46 +08:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
2003-07-23 20:55:24 +08:00
|
|
|
|
2003-02-05 07:48:46 +08:00
|
|
|
dns->inuse--;
|
2009-11-11 17:31:37 +08:00
|
|
|
/* only free if nobody is using AND it is not in hostcache (timestamp ==
|
|
|
|
0) */
|
2011-04-20 21:17:42 +08:00
|
|
|
if(dns->inuse == 0 && dns->timestamp == 0) {
|
2009-11-11 17:31:37 +08:00
|
|
|
Curl_freeaddrinfo(dns->addr);
|
|
|
|
free(dns);
|
|
|
|
}
|
2003-07-23 20:55:24 +08:00
|
|
|
|
2003-02-05 07:48:46 +08:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
}
|
|
|
|
|
2002-11-26 17:41:54 +08:00
|
|
|
/*
|
2004-03-30 21:02:07 +08:00
|
|
|
* File-internal: free a cache dns entry.
|
2002-11-26 17:41:54 +08:00
|
|
|
*/
|
2004-03-30 21:02:07 +08:00
|
|
|
static void freednsentry(void *freethis)
|
2002-11-26 17:41:54 +08:00
|
|
|
{
|
|
|
|
struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
|
|
|
|
|
2009-11-11 17:31:37 +08:00
|
|
|
/* mark the entry as not in hostcache */
|
|
|
|
p->timestamp = 0;
|
2011-04-20 21:17:42 +08:00
|
|
|
if(p->inuse == 0) {
|
2008-10-30 21:45:25 +08:00
|
|
|
Curl_freeaddrinfo(p->addr);
|
|
|
|
free(p);
|
|
|
|
}
|
2001-10-04 21:36:11 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
|
|
|
* Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
|
|
|
|
*/
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_hash *Curl_mk_dnscache(void)
|
2004-03-30 21:02:07 +08:00
|
|
|
{
|
2007-06-27 05:09:28 +08:00
|
|
|
return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
|
2004-03-30 21:02:07 +08:00
|
|
|
}
|
|
|
|
|
2011-12-23 21:30:43 +08:00
|
|
|
static int hostcache_inuse(void *data, void *hc)
|
|
|
|
{
|
|
|
|
struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
|
|
|
|
|
|
|
|
if(c->inuse == 1)
|
|
|
|
Curl_resolv_unlock(data, c);
|
|
|
|
|
|
|
|
return 1; /* free all entries */
|
|
|
|
}
|
2001-10-04 21:25:12 +08:00
|
|
|
|
2012-11-18 23:17:37 +08:00
|
|
|
void Curl_hostcache_clean(struct SessionHandle *data)
|
2011-12-23 21:30:43 +08:00
|
|
|
{
|
|
|
|
/* Entries added to the hostcache with the CURLOPT_RESOLVE function are
|
|
|
|
* still present in the cache with the inuse counter set to 1. Detect them
|
|
|
|
* and cleanup!
|
|
|
|
*/
|
|
|
|
Curl_hash_clean_with_criterium(data->dns.hostcache, data, hostcache_inuse);
|
2012-11-18 23:17:37 +08:00
|
|
|
}
|
2011-12-23 21:30:43 +08:00
|
|
|
|
2012-11-18 23:17:37 +08:00
|
|
|
void Curl_hostcache_destroy(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
Curl_hostcache_clean(data);
|
2011-12-23 21:30:43 +08:00
|
|
|
Curl_hash_destroy(data->dns.hostcache);
|
|
|
|
data->dns.hostcachetype = HCACHE_NONE;
|
|
|
|
data->dns.hostcache = NULL;
|
|
|
|
}
|
2011-12-31 17:28:10 +08:00
|
|
|
|
|
|
|
CURLcode Curl_loadhostpairs(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
struct curl_slist *hostp;
|
|
|
|
char hostname[256];
|
|
|
|
char address[256];
|
|
|
|
int port;
|
|
|
|
|
|
|
|
for(hostp = data->change.resolve; hostp; hostp = hostp->next ) {
|
|
|
|
if(!hostp->data)
|
|
|
|
continue;
|
|
|
|
if(hostp->data[0] == '-') {
|
|
|
|
/* TODO: mark an entry for removal */
|
|
|
|
}
|
|
|
|
else if(3 == sscanf(hostp->data, "%255[^:]:%d:%255s", hostname, &port,
|
|
|
|
address)) {
|
|
|
|
struct Curl_dns_entry *dns;
|
|
|
|
Curl_addrinfo *addr;
|
|
|
|
char *entry_id;
|
|
|
|
size_t entry_len;
|
|
|
|
|
|
|
|
addr = Curl_str2addr(address, port);
|
|
|
|
if(!addr) {
|
|
|
|
infof(data, "Resolve %s found illegal!\n", hostp->data);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an entry id, based upon the hostname and port */
|
|
|
|
entry_id = create_hostcache_id(hostname, port);
|
|
|
|
/* If we can't create the entry id, fail */
|
2012-01-02 20:41:09 +08:00
|
|
|
if(!entry_id) {
|
|
|
|
Curl_freeaddrinfo(addr);
|
2011-12-31 17:28:10 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2012-01-02 20:41:09 +08:00
|
|
|
}
|
2011-12-31 17:28:10 +08:00
|
|
|
|
|
|
|
entry_len = strlen(entry_id);
|
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
|
|
|
/* See if its already in our dns cache */
|
|
|
|
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
|
|
|
|
|
|
|
|
/* free the allocated entry_id again */
|
|
|
|
free(entry_id);
|
|
|
|
|
|
|
|
if(!dns)
|
|
|
|
/* if not in the cache already, put this host in the cache */
|
|
|
|
dns = Curl_cache_addr(data, addr, hostname, port);
|
|
|
|
else
|
|
|
|
/* this is a duplicate, free it again */
|
|
|
|
Curl_freeaddrinfo(addr);
|
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
|
|
|
|
if(!dns) {
|
|
|
|
Curl_freeaddrinfo(addr);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
infof(data, "Added %s:%d:%s to DNS cache\n",
|
|
|
|
hostname, port, address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data->change.resolve = NULL; /* dealt with now */
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|