2002-09-03 13:52:59 +02:00
|
|
|
/***************************************************************************
|
2004-06-10 13:56:02 +02:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
1999-12-29 15:20:26 +01:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2008-01-15 23:15:55 +01:00
|
|
|
* Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 15:20:26 +01:00
|
|
|
*
|
2002-09-03 13:52:59 +02: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 13:56:02 +02:00
|
|
|
*
|
2001-01-03 10:29:33 +01: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 13:52:59 +02:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
1999-12-29 15:20:26 +01:00
|
|
|
*
|
2001-01-03 10:29:33 +01:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 15:20:26 +01:00
|
|
|
*
|
2001-01-03 10:29:33 +01:00
|
|
|
* $Id$
|
2002-09-03 13:52:59 +02:00
|
|
|
***************************************************************************/
|
1999-12-29 15:20:26 +01:00
|
|
|
|
2000-08-24 16:26:33 +02:00
|
|
|
#include "setup.h"
|
|
|
|
|
1999-12-29 15:20:26 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2006-07-25 15:49:49 +02:00
|
|
|
#ifdef NEED_MALLOC_H
|
2003-10-14 14:00:45 +02:00
|
|
|
#include <malloc.h>
|
2006-04-26 19:23:28 +02:00
|
|
|
#endif
|
1999-12-29 15:20:26 +01:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2000-08-23 09:23:42 +02:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
1999-12-29 15:20:26 +01:00
|
|
|
#include <netinet/in.h>
|
2000-08-23 09:23:42 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
1999-12-29 15:20:26 +01:00
|
|
|
#include <netdb.h>
|
2000-08-23 09:23:42 +02:00
|
|
|
#endif
|
1999-12-29 15:20:26 +01:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2001-12-04 14:03:08 +01:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-10-06 09:50:18 +02:00
|
|
|
#include <stdlib.h> /* required for free() prototypes */
|
2001-12-04 14:03:08 +01:00
|
|
|
#endif
|
2004-04-21 13:18:42 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h> /* for the close() proto */
|
|
|
|
#endif
|
2004-10-06 09:50:18 +02:00
|
|
|
#ifdef VMS
|
2002-02-20 14:46:53 +01:00
|
|
|
#include <in.h>
|
2001-08-06 14:19:26 +02:00
|
|
|
#include <inet.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
1999-12-29 15:20:26 +01:00
|
|
|
|
2002-06-11 17:10:18 +02:00
|
|
|
#ifdef HAVE_SETJMP_H
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
2006-04-26 19:23:28 +02:00
|
|
|
#ifdef HAVE_PROCESS_H
|
2004-04-06 16:51:14 +02:00
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
1999-12-29 15:20:26 +01:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
2001-09-28 09:05:26 +02:00
|
|
|
#include "hostip.h"
|
2002-01-03 11:22:59 +01:00
|
|
|
#include "hash.h"
|
2003-02-05 00:48:46 +01:00
|
|
|
#include "share.h"
|
2004-04-06 16:51:14 +02:00
|
|
|
#include "strerror.h"
|
2003-08-05 16:40:59 +02:00
|
|
|
#include "url.h"
|
2004-06-10 13:06:21 +02:00
|
|
|
#include "inet_ntop.h"
|
1999-12-29 15:20:26 +01:00
|
|
|
|
2002-01-17 09:03:48 +01:00
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
2000-08-24 14:33:16 +02:00
|
|
|
#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
|
2000-05-30 00:51:13 +02:00
|
|
|
#include "inet_ntoa_r.h"
|
|
|
|
#endif
|
|
|
|
|
2004-05-11 13:30:23 +02:00
|
|
|
#include "memory.h"
|
2000-10-09 13:12:34 +02:00
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2004-04-26 09:20:11 +02:00
|
|
|
/*
|
|
|
|
* hostip.c explained
|
|
|
|
* ==================
|
|
|
|
*
|
|
|
|
* The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
|
|
|
|
* 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 12:23:15 +02:00
|
|
|
* asynchronous name resolves. This can be Windows or *nix.
|
2004-04-26 09:20:11 +02: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.
|
|
|
|
*
|
|
|
|
* The host*.c sources files are split up like this:
|
|
|
|
*
|
|
|
|
* hostip.c - method-independent resolver functions and utility functions
|
|
|
|
* hostasyn.c - functions for asynchronous name resolves
|
|
|
|
* hostsyn.c - functions for synchronous name resolves
|
|
|
|
* hostares.c - functions for ares-using name resolves
|
|
|
|
* hostthre.c - functions for threaded name resolves
|
|
|
|
* hostip4.c - ipv4-specific functions
|
|
|
|
* hostip6.c - ipv6-specific functions
|
|
|
|
*
|
|
|
|
* The hostip.h is the united header file for all this. It defines the
|
|
|
|
* CURLRES_* defines based on the config*.h and setup.h defines.
|
|
|
|
*/
|
2004-03-31 14:55:24 +02:00
|
|
|
|
2004-03-30 15:02:07 +02:00
|
|
|
/* These two symbols are for the global DNS cache */
|
2005-01-25 01:06:29 +01:00
|
|
|
static struct curl_hash hostname_cache;
|
2002-01-07 21:52:32 +01:00
|
|
|
static int host_cache_initialized;
|
2002-01-03 11:22:59 +01:00
|
|
|
|
2004-03-30 15:02:07 +02: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-15 23:15:55 +01:00
|
|
|
*
|
2008-01-15 23:44:12 +01:00
|
|
|
* Returns a struct curl_hash pointer on success, NULL on failure.
|
2004-03-30 15:02:07 +02:00
|
|
|
*/
|
2008-01-15 23:44:12 +01:00
|
|
|
struct curl_hash *Curl_global_host_cache_init(void)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
2008-01-15 23:15:55 +01:00
|
|
|
int rc = 0;
|
2007-11-07 10:21:35 +01:00
|
|
|
if(!host_cache_initialized) {
|
2008-01-15 23:15:55 +01:00
|
|
|
rc = Curl_hash_init(&hostname_cache, 7, Curl_hash_str,
|
|
|
|
Curl_str_key_compare, freednsentry);
|
|
|
|
if(!rc)
|
|
|
|
host_cache_initialized = 1;
|
2002-01-07 21:52:32 +01:00
|
|
|
}
|
2008-01-15 23:44:12 +01:00
|
|
|
return rc?NULL:&hostname_cache;
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
|
2004-03-30 15:02:07 +02:00
|
|
|
/*
|
|
|
|
* Destroy and cleanup the global DNS cache
|
|
|
|
*/
|
2002-01-07 21:52:32 +01:00
|
|
|
void Curl_global_host_cache_dtor(void)
|
|
|
|
{
|
2007-11-07 10:21:35 +01:00
|
|
|
if(host_cache_initialized) {
|
2002-04-27 15:07:51 +02:00
|
|
|
Curl_hash_clean(&hostname_cache);
|
2002-01-07 21:55:35 +01:00
|
|
|
host_cache_initialized = 0;
|
2002-01-07 21:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
2002-01-03 11:22:59 +01:00
|
|
|
|
2004-06-10 13:06:21 +02:00
|
|
|
/*
|
|
|
|
* Return # of adresses in a Curl_addrinfo struct
|
|
|
|
*/
|
|
|
|
int Curl_num_addresses(const Curl_addrinfo *addr)
|
|
|
|
{
|
|
|
|
int i;
|
2004-11-18 15:04:40 +01:00
|
|
|
for (i = 0; addr; addr = addr->ai_next, i++)
|
|
|
|
; /* empty loop */
|
2004-06-24 09:43:48 +02:00
|
|
|
return i;
|
2004-06-10 13:06:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-06-10 13:55:37 +02:00
|
|
|
* Curl_printable_address() returns a printable version of the 1st address
|
|
|
|
* given in the 'ip' argument. The result will be stored in the buf that is
|
|
|
|
* bufsize bytes big.
|
2004-06-10 13:06:21 +02:00
|
|
|
*
|
|
|
|
* If the conversion fails, it returns NULL.
|
|
|
|
*/
|
2004-06-24 09:43:48 +02:00
|
|
|
const char *Curl_printable_address(const Curl_addrinfo *ip,
|
2004-06-10 13:06:21 +02:00
|
|
|
char *buf, size_t bufsize)
|
|
|
|
{
|
|
|
|
const void *ip4 = &((const struct sockaddr_in*)ip->ai_addr)->sin_addr;
|
|
|
|
int af = ip->ai_family;
|
2004-06-24 09:43:48 +02:00
|
|
|
#ifdef CURLRES_IPV6
|
|
|
|
const void *ip6 = &((const struct sockaddr_in6*)ip->ai_addr)->sin6_addr;
|
2004-06-10 13:06:21 +02:00
|
|
|
#else
|
2004-06-24 09:43:48 +02:00
|
|
|
const void *ip6 = NULL;
|
2004-06-10 13:06:21 +02:00
|
|
|
#endif
|
2004-06-24 09:43:48 +02:00
|
|
|
|
|
|
|
return Curl_inet_ntop(af, af == AF_INET ? ip4 : ip6, buf, bufsize);
|
2004-06-10 13:06:21 +02:00
|
|
|
}
|
|
|
|
|
2004-03-30 15:02:07 +02:00
|
|
|
/*
|
2004-04-26 09:20:11 +02:00
|
|
|
* Return a hostcache id string for the providing host + port, to be used by
|
|
|
|
* the DNS caching.
|
2004-03-30 15:02:07 +02:00
|
|
|
*/
|
2002-01-17 07:55:37 +01:00
|
|
|
static char *
|
2006-07-21 07:51:12 +02:00
|
|
|
create_hostcache_id(const char *server, int port)
|
2002-01-17 07:55:37 +01:00
|
|
|
{
|
2004-06-24 13:54:11 +02:00
|
|
|
/* create and return the new allocated entry */
|
|
|
|
return aprintf("%s:%d", server, port);
|
2002-01-17 07:55:37 +01:00
|
|
|
}
|
|
|
|
|
2002-04-27 15:07:51 +02:00
|
|
|
struct hostcache_prune_data {
|
2007-08-26 07:53:26 +02:00
|
|
|
long cache_timeout;
|
2004-03-10 10:36:43 +01:00
|
|
|
time_t now;
|
2002-04-17 22:13:55 +02:00
|
|
|
};
|
|
|
|
|
2004-03-30 15:02:07 +02: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-17 22:13:55 +02:00
|
|
|
static int
|
2002-11-05 11:51:41 +01:00
|
|
|
hostcache_timestamp_remove(void *datap, void *hc)
|
2002-04-17 22:13:55 +02:00
|
|
|
{
|
2004-06-10 13:56:02 +02:00
|
|
|
struct hostcache_prune_data *data =
|
2002-04-27 15:07:51 +02:00
|
|
|
(struct hostcache_prune_data *) datap;
|
2002-11-05 11:51:41 +01:00
|
|
|
struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
|
2004-06-10 13:56:02 +02:00
|
|
|
|
2007-11-07 10:21:35 +01:00
|
|
|
if((data->now - c->timestamp < data->cache_timeout) ||
|
2002-11-05 11:51:41 +01:00
|
|
|
c->inuse) {
|
|
|
|
/* please don't remove */
|
2002-04-17 22:13:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-06-10 13:56:02 +02:00
|
|
|
|
2002-11-05 11:51:41 +01:00
|
|
|
/* fine, remove */
|
2002-04-17 22:13:55 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-30 15:02:07 +02:00
|
|
|
/*
|
|
|
|
* Prune the DNS cache. This assumes that a lock has already been taken.
|
|
|
|
*/
|
2002-04-17 22:13:55 +02:00
|
|
|
static void
|
2007-08-26 07:53:26 +02:00
|
|
|
hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
|
2002-04-17 22:13:55 +02:00
|
|
|
{
|
2002-04-27 15:07:51 +02:00
|
|
|
struct hostcache_prune_data user;
|
2002-04-17 22:13:55 +02:00
|
|
|
|
|
|
|
user.cache_timeout = cache_timeout;
|
|
|
|
user.now = now;
|
2004-02-15 17:57:53 +01:00
|
|
|
|
2004-06-10 13:56:02 +02:00
|
|
|
Curl_hash_clean_with_criterium(hostcache,
|
|
|
|
(void *) &user,
|
2002-11-05 11:51:41 +01:00
|
|
|
hostcache_timestamp_remove);
|
2002-04-17 22:13:55 +02:00
|
|
|
}
|
|
|
|
|
2004-03-30 15:02:07 +02:00
|
|
|
/*
|
|
|
|
* Library-wide function for pruning the DNS cache. This function takes and
|
|
|
|
* returns the appropriate locks.
|
|
|
|
*/
|
2003-12-15 18:33:49 +01:00
|
|
|
void Curl_hostcache_prune(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
time_t now;
|
2004-02-18 08:56:18 +01:00
|
|
|
|
2006-07-08 00:58:06 +02:00
|
|
|
if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
|
2004-10-04 12:36:51 +02:00
|
|
|
/* cache forever means never prune, and NULL hostcache means
|
|
|
|
we can't do it */
|
2004-02-18 08:56:18 +01:00
|
|
|
return;
|
|
|
|
|
2003-12-15 18:33:49 +01: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 00:58:06 +02:00
|
|
|
hostcache_prune(data->dns.hostcache,
|
2003-12-15 18:33:49 +01:00
|
|
|
data->set.dns_cache_timeout,
|
|
|
|
now);
|
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
}
|
|
|
|
|
2008-03-11 23:55:23 +01:00
|
|
|
/*
|
|
|
|
* Check if the entry should be pruned. Assumes a locked cache.
|
|
|
|
*/
|
2006-02-17 00:42:32 +01:00
|
|
|
static int
|
|
|
|
remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
|
|
|
|
{
|
|
|
|
struct hostcache_prune_data user;
|
|
|
|
|
2006-07-08 00:58:06 +02:00
|
|
|
if( !dns || (data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
|
2006-02-17 00:42:32 +01: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;
|
|
|
|
|
2007-11-07 10:21:35 +01:00
|
|
|
if( !hostcache_timestamp_remove(&user,dns) )
|
2006-02-17 00:42:32 +01:00
|
|
|
return 0;
|
|
|
|
|
2006-07-08 00:58:06 +02:00
|
|
|
Curl_hash_clean_with_criterium(data->dns.hostcache,
|
2006-02-17 00:42:32 +01:00
|
|
|
(void *) &user,
|
|
|
|
hostcache_timestamp_remove);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-05 16:40:59 +02:00
|
|
|
#ifdef HAVE_SIGSETJMP
|
2004-03-30 15:02:07 +02: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 16:40:59 +02:00
|
|
|
sigjmp_buf curl_jmpenv;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-03-30 15:02:07 +02:00
|
|
|
/*
|
2004-04-26 09:20:11 +02:00
|
|
|
* Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
|
2004-03-30 15:02:07 +02: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 09:20:11 +02:00
|
|
|
struct Curl_dns_entry *
|
|
|
|
Curl_cache_addr(struct SessionHandle *data,
|
|
|
|
Curl_addrinfo *addr,
|
2006-07-21 07:51:12 +02:00
|
|
|
const char *hostname,
|
2004-04-26 09:20:11 +02:00
|
|
|
int port)
|
2002-11-05 11:51:41 +01:00
|
|
|
{
|
2003-08-05 16:40:59 +02:00
|
|
|
char *entry_id;
|
2004-02-23 09:35:57 +01:00
|
|
|
size_t entry_len;
|
2003-08-05 16:40:59 +02:00
|
|
|
struct Curl_dns_entry *dns;
|
2004-05-04 15:40:30 +02:00
|
|
|
struct Curl_dns_entry *dns2;
|
2003-08-05 16:40:59 +02:00
|
|
|
time_t now;
|
|
|
|
|
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2004-06-24 13:54:11 +02:00
|
|
|
entry_id = create_hostcache_id(hostname, port);
|
2003-08-05 16:40:59 +02:00
|
|
|
/* If we can't create the entry id, fail */
|
2007-11-07 10:21:35 +01:00
|
|
|
if(!entry_id)
|
2003-08-05 16:40:59 +02:00
|
|
|
return NULL;
|
2004-06-24 13:54:11 +02:00
|
|
|
entry_len = strlen(entry_id);
|
2003-08-05 16:40:59 +02:00
|
|
|
|
|
|
|
/* Create a new cache entry */
|
2006-07-21 07:51:12 +02:00
|
|
|
dns = (struct Curl_dns_entry *) calloc(sizeof(struct Curl_dns_entry), 1);
|
2007-11-07 10:21:35 +01:00
|
|
|
if(!dns) {
|
2003-08-05 16:40:59 +02:00
|
|
|
free(entry_id);
|
|
|
|
return NULL;
|
2002-11-05 11:51:41 +01:00
|
|
|
}
|
|
|
|
|
2003-09-14 23:17:54 +02:00
|
|
|
dns->inuse = 0; /* init to not used */
|
|
|
|
dns->addr = addr; /* this is the address(es) */
|
|
|
|
|
|
|
|
/* Store the resolved data in our DNS cache. This function may return a
|
|
|
|
pointer to an existing struct already present in the hash, and it may
|
|
|
|
return the same argument we pass in. Make no assumptions. */
|
2006-07-08 00:58:06 +02:00
|
|
|
dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
|
|
|
|
(void *)dns);
|
2004-05-04 15:40:30 +02:00
|
|
|
if(!dns2) {
|
|
|
|
/* Major badness, run away. */
|
|
|
|
free(dns);
|
2003-09-14 23:17:54 +02:00
|
|
|
free(entry_id);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-08-05 16:40:59 +02:00
|
|
|
time(&now);
|
2004-05-04 15:40:30 +02:00
|
|
|
dns = dns2;
|
2003-08-05 16:40:59 +02:00
|
|
|
|
2003-09-14 23:17:54 +02:00
|
|
|
dns->timestamp = now; /* used now */
|
2003-08-05 16:40:59 +02:00
|
|
|
dns->inuse++; /* mark entry as in-use */
|
|
|
|
|
|
|
|
/* free the allocated entry_id again */
|
|
|
|
free(entry_id);
|
|
|
|
|
|
|
|
return dns;
|
2002-01-17 08:38:25 +01:00
|
|
|
}
|
2002-01-17 07:55:37 +01:00
|
|
|
|
2004-03-30 15:02:07 +02: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.
|
|
|
|
*
|
|
|
|
* Return codes:
|
|
|
|
*
|
2004-04-26 16:18:00 +02:00
|
|
|
* CURLRESOLV_ERROR (-1) = error, no pointer
|
|
|
|
* CURLRESOLV_RESOLVED (0) = OK, pointer provided
|
|
|
|
* CURLRESOLV_PENDING (1) = waiting for response, no pointer
|
2004-03-30 15:02:07 +02:00
|
|
|
*/
|
2004-04-26 16:18:00 +02:00
|
|
|
|
2003-08-05 16:40:59 +02:00
|
|
|
int Curl_resolv(struct connectdata *conn,
|
2006-07-21 07:51:12 +02:00
|
|
|
const char *hostname,
|
2003-08-05 16:40:59 +02:00
|
|
|
int port,
|
|
|
|
struct Curl_dns_entry **entry)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
2002-01-17 08:38:25 +01:00
|
|
|
char *entry_id = NULL;
|
2002-11-05 11:51:41 +01:00
|
|
|
struct Curl_dns_entry *dns = NULL;
|
2004-02-23 09:35:57 +01:00
|
|
|
size_t entry_len;
|
2003-08-05 16:40:59 +02:00
|
|
|
struct SessionHandle *data = conn->data;
|
2003-11-14 12:56:15 +01:00
|
|
|
CURLcode result;
|
2008-03-11 23:55:23 +01:00
|
|
|
int rc = CURLRESOLV_ERROR; /* default to failure */
|
2003-08-05 16:40:59 +02:00
|
|
|
*entry = NULL;
|
2002-01-08 05:26:47 +01:00
|
|
|
|
2002-06-11 17:10:18 +02:00
|
|
|
#ifdef HAVE_SIGSETJMP
|
2002-11-05 11:51:41 +01: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 */
|
2006-08-16 20:48:27 +02:00
|
|
|
if(!data->set.no_signal) {
|
2007-11-07 10:21:35 +01:00
|
|
|
if(sigsetjmp(curl_jmpenv, 1)) {
|
2006-08-16 20:48:27 +02:00
|
|
|
/* this is coming from a siglongjmp() */
|
|
|
|
failf(data, "name lookup timed out");
|
2008-03-11 23:55:23 +01:00
|
|
|
return rc;
|
2006-08-16 20:48:27 +02:00
|
|
|
}
|
2002-06-11 17:10:18 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-01-17 08:38:25 +01:00
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2004-06-24 13:54:11 +02:00
|
|
|
entry_id = create_hostcache_id(hostname, port);
|
2002-11-05 11:51:41 +01:00
|
|
|
/* If we can't create the entry id, fail */
|
2007-11-07 10:21:35 +01:00
|
|
|
if(!entry_id)
|
2008-03-11 23:55:23 +01:00
|
|
|
return rc;
|
2003-02-05 00:48:46 +01:00
|
|
|
|
2004-06-24 13:54:11 +02:00
|
|
|
entry_len = strlen(entry_id);
|
|
|
|
|
2003-02-05 00:48:46 +01:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2002-01-08 05:26:47 +01:00
|
|
|
/* See if its already in our dns cache */
|
2006-07-08 00:58:06 +02:00
|
|
|
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
|
2004-06-10 13:56:02 +02:00
|
|
|
|
2008-03-11 23:55:23 +01:00
|
|
|
/* See whether the returned entry is stale. Done before we release lock */
|
|
|
|
if( remove_entry_if_stale(data, dns) )
|
|
|
|
dns = NULL; /* the memory deallocation is being handled by the hash */
|
|
|
|
|
|
|
|
if(dns) {
|
|
|
|
dns->inuse++; /* we use it! */
|
|
|
|
rc = CURLRESOLV_RESOLVED;
|
|
|
|
}
|
|
|
|
|
2003-10-12 15:57:41 +02:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2002-01-08 05:26:47 +01:00
|
|
|
|
2003-08-05 16:40:59 +02:00
|
|
|
/* free the allocated entry_id again */
|
|
|
|
free(entry_id);
|
|
|
|
|
2007-11-07 10:21:35 +01:00
|
|
|
if(!dns) {
|
2003-08-05 16:40:59 +02:00
|
|
|
/* The entry was not in the cache. Resolve it to IP address */
|
2004-04-26 09:20:11 +02:00
|
|
|
|
|
|
|
Curl_addrinfo *addr;
|
2007-09-28 20:47:59 +02:00
|
|
|
int respwait;
|
2004-04-26 09:20:11 +02:00
|
|
|
|
|
|
|
/* Check what IP specifics the app has requested and if we can provide it.
|
|
|
|
* If not, bail out. */
|
|
|
|
if(!Curl_ipvalid(data))
|
2004-04-26 16:18:00 +02:00
|
|
|
return CURLRESOLV_ERROR;
|
2004-04-26 09:20:11 +02:00
|
|
|
|
2007-09-28 20:47:59 +02: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 */
|
|
|
|
addr = Curl_getaddrinfo(conn, hostname, port, &respwait);
|
2004-06-10 13:56:02 +02:00
|
|
|
|
2007-11-07 10:21:35 +01:00
|
|
|
if(!addr) {
|
2007-09-28 20:47:59 +02:00
|
|
|
if(respwait) {
|
2004-06-10 13:56:02 +02:00
|
|
|
/* the response to our resolve call will come asynchronously at
|
2003-08-05 16:40:59 +02:00
|
|
|
a later time, good or bad */
|
2003-10-04 16:50:45 +02:00
|
|
|
/* First, check that we haven't received the info by now */
|
2003-11-14 12:56:15 +01:00
|
|
|
result = Curl_is_resolved(conn, &dns);
|
|
|
|
if(result) /* error detected */
|
2004-04-26 16:18:00 +02:00
|
|
|
return CURLRESOLV_ERROR;
|
2003-10-04 16:50:45 +02:00
|
|
|
if(dns)
|
2004-04-26 16:18:00 +02:00
|
|
|
rc = CURLRESOLV_RESOLVED; /* pointer provided */
|
2003-10-04 16:50:45 +02:00
|
|
|
else
|
2004-04-26 16:18:00 +02:00
|
|
|
rc = CURLRESOLV_PENDING; /* no info yet */
|
2003-10-04 16:50:45 +02:00
|
|
|
}
|
2002-11-05 11:51:41 +01:00
|
|
|
}
|
2004-01-14 10:05:54 +01:00
|
|
|
else {
|
2003-10-12 15:57:41 +02:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2003-08-05 16:40:59 +02:00
|
|
|
/* we got a response, store it in the cache */
|
2004-04-26 09:20:11 +02:00
|
|
|
dns = Curl_cache_addr(data, addr, hostname, port);
|
2004-06-10 13:56:02 +02:00
|
|
|
|
2003-10-12 15:57:41 +02:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2003-10-24 14:57:23 +02:00
|
|
|
|
2004-01-14 10:05:54 +01:00
|
|
|
if(!dns)
|
2003-10-24 14:57:23 +02:00
|
|
|
/* returned failure, bail out nicely */
|
|
|
|
Curl_freeaddrinfo(addr);
|
2003-12-03 08:55:51 +01:00
|
|
|
else
|
2004-04-26 16:18:00 +02:00
|
|
|
rc = CURLRESOLV_RESOLVED;
|
2003-10-12 15:57:41 +02:00
|
|
|
}
|
2002-01-17 08:38:25 +01:00
|
|
|
}
|
2002-01-08 05:26:47 +01:00
|
|
|
|
2003-08-05 16:40:59 +02:00
|
|
|
*entry = dns;
|
2002-01-03 11:22:59 +01:00
|
|
|
|
2003-08-05 16:40:59 +02:00
|
|
|
return rc;
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
|
2004-03-30 15:02:07 +02: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 00:48:46 +01:00
|
|
|
void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
|
|
|
|
{
|
2007-02-21 20:03:20 +01:00
|
|
|
DEBUGASSERT(dns && (dns->inuse>0));
|
2004-04-26 09:20:11 +02:00
|
|
|
|
2003-02-05 00:48:46 +01:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
2003-07-23 14:55:24 +02:00
|
|
|
|
2003-02-05 00:48:46 +01:00
|
|
|
dns->inuse--;
|
2003-07-23 14:55:24 +02:00
|
|
|
|
2003-02-05 00:48:46 +01:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
}
|
|
|
|
|
2002-11-26 10:41:54 +01:00
|
|
|
/*
|
2004-03-30 15:02:07 +02:00
|
|
|
* File-internal: free a cache dns entry.
|
2002-11-26 10:41:54 +01:00
|
|
|
*/
|
2004-03-30 15:02:07 +02:00
|
|
|
static void freednsentry(void *freethis)
|
2002-11-26 10:41:54 +01:00
|
|
|
{
|
|
|
|
struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
|
|
|
|
|
|
|
|
Curl_freeaddrinfo(p->addr);
|
2002-01-08 05:26:47 +01:00
|
|
|
|
|
|
|
free(p);
|
2001-10-04 15:36:11 +02:00
|
|
|
}
|
|
|
|
|
2004-03-30 15:02:07 +02:00
|
|
|
/*
|
|
|
|
* Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
|
|
|
|
*/
|
2005-01-25 01:06:29 +01:00
|
|
|
struct curl_hash *Curl_mk_dnscache(void)
|
2004-03-30 15:02:07 +02:00
|
|
|
{
|
2007-06-26 23:09:28 +02:00
|
|
|
return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
|
2004-03-30 15:02:07 +02:00
|
|
|
}
|
|
|
|
|
2004-04-26 09:20:11 +02:00
|
|
|
#ifdef CURLRES_ADDRINFO_COPY
|
2001-10-04 15:25:12 +02:00
|
|
|
|
2004-04-26 09:20:11 +02:00
|
|
|
/* align on even 64bit boundaries */
|
|
|
|
#define MEMALIGN(x) ((x)+(8-(((unsigned long)(x))&0x7)))
|
2004-03-30 15:02:07 +02:00
|
|
|
|
2003-08-05 16:40:59 +02:00
|
|
|
/*
|
2004-04-26 09:20:11 +02:00
|
|
|
* Curl_addrinfo_copy() performs a "deep" copy of a hostent into a buffer and
|
|
|
|
* returns a pointer to the malloc()ed copy. You need to call free() on the
|
|
|
|
* returned buffer when you're done with it.
|
2003-08-05 16:40:59 +02:00
|
|
|
*/
|
2006-07-21 08:21:46 +02:00
|
|
|
Curl_addrinfo *Curl_addrinfo_copy(const void *org, int port)
|
2001-10-03 23:42:04 +02:00
|
|
|
{
|
2006-07-21 08:21:46 +02:00
|
|
|
const struct hostent *orig = org;
|
2002-05-17 09:49:28 +02:00
|
|
|
|
2004-06-24 12:43:50 +02:00
|
|
|
return Curl_he2ai(orig, port);
|
2001-10-03 23:42:04 +02:00
|
|
|
}
|
2004-04-26 09:20:11 +02:00
|
|
|
#endif /* CURLRES_ADDRINFO_COPY */
|
2006-07-25 12:31:31 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Only for plain-ipv4 and c-ares builds
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#if defined(CURLRES_IPV4) || defined(CURLRES_ARES)
|
|
|
|
/*
|
|
|
|
* This is a function for freeing name information in a protocol independent
|
|
|
|
* way.
|
|
|
|
*/
|
|
|
|
void Curl_freeaddrinfo(Curl_addrinfo *ai)
|
|
|
|
{
|
|
|
|
Curl_addrinfo *next;
|
|
|
|
|
|
|
|
/* walk over the list and free all entries */
|
|
|
|
while(ai) {
|
|
|
|
next = ai->ai_next;
|
2007-07-02 00:01:18 +02:00
|
|
|
if(ai->ai_canonname)
|
|
|
|
free(ai->ai_canonname);
|
2006-07-25 12:31:31 +02:00
|
|
|
free(ai);
|
|
|
|
ai = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct namebuf {
|
|
|
|
struct hostent hostentry;
|
|
|
|
char *h_addr_list[2];
|
|
|
|
struct in_addr addrentry;
|
|
|
|
char h_name[16]; /* 123.123.123.123 = 15 letters is maximum */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_ip2addr() takes a 32bit ipv4 internet address as input parameter
|
|
|
|
* together with a pointer to the string version of the address, and it
|
|
|
|
* returns a Curl_addrinfo chain filled in correctly with information for this
|
|
|
|
* address/host.
|
|
|
|
*
|
|
|
|
* The input parameters ARE NOT checked for validity but they are expected
|
|
|
|
* to have been checked already when this is called.
|
|
|
|
*/
|
|
|
|
Curl_addrinfo *Curl_ip2addr(in_addr_t num, const char *hostname, int port)
|
|
|
|
{
|
|
|
|
Curl_addrinfo *ai;
|
2007-04-25 05:00:10 +02:00
|
|
|
|
|
|
|
#if defined(VMS) && \
|
|
|
|
defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
|
|
|
|
#pragma pointer_size save
|
|
|
|
#pragma pointer_size short
|
|
|
|
#pragma message disable PTRMISMATCH
|
|
|
|
#endif
|
|
|
|
|
2006-07-25 12:31:31 +02:00
|
|
|
struct hostent *h;
|
|
|
|
struct in_addr *addrentry;
|
|
|
|
struct namebuf buffer;
|
|
|
|
struct namebuf *buf = &buffer;
|
|
|
|
|
|
|
|
h = &buf->hostentry;
|
|
|
|
h->h_addr_list = &buf->h_addr_list[0];
|
|
|
|
addrentry = &buf->addrentry;
|
2006-08-22 00:28:19 +02:00
|
|
|
#ifdef _CRAYC
|
|
|
|
/* On UNICOS, s_addr is a bit field and for some reason assigning to it
|
|
|
|
* doesn't work. There must be a better fix than this ugly hack.
|
|
|
|
*/
|
|
|
|
memcpy(addrentry, &num, SIZEOF_in_addr);
|
|
|
|
#else
|
2006-07-25 12:31:31 +02:00
|
|
|
addrentry->s_addr = num;
|
2006-08-22 00:28:19 +02:00
|
|
|
#endif
|
2006-07-25 12:31:31 +02:00
|
|
|
h->h_addr_list[0] = (char*)addrentry;
|
|
|
|
h->h_addr_list[1] = NULL;
|
|
|
|
h->h_addrtype = AF_INET;
|
|
|
|
h->h_length = sizeof(*addrentry);
|
|
|
|
h->h_name = &buf->h_name[0];
|
|
|
|
h->h_aliases = NULL;
|
|
|
|
|
|
|
|
/* Now store the dotted version of the address */
|
|
|
|
snprintf((char *)h->h_name, 16, "%s", hostname);
|
|
|
|
|
2007-04-25 05:00:10 +02:00
|
|
|
#if defined(VMS) && \
|
|
|
|
defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
|
|
|
|
#pragma pointer_size restore
|
|
|
|
#pragma message enable PTRMISMATCH
|
|
|
|
#endif
|
|
|
|
|
2006-07-25 12:31:31 +02:00
|
|
|
ai = Curl_he2ai(h, port);
|
|
|
|
|
|
|
|
return ai;
|
|
|
|
}
|
2007-04-25 05:00:10 +02:00
|
|
|
#endif /* CURLRES_IPV4 || CURLRES_ARES */
|
2006-07-25 20:48:12 +02:00
|
|
|
|
|
|
|
|