2002-09-03 15:52:59 +04:00
|
|
|
/***************************************************************************
|
2004-06-10 15:56:02 +04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
1999-12-29 17:20:26 +03:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2019-02-08 11:33:42 +03:00
|
|
|
* Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 17:20:26 +03:00
|
|
|
*
|
2002-09-03 15:52:59 +04:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-03 02:19:02 +03:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2004-06-10 15:56:02 +04:00
|
|
|
*
|
2001-01-03 12:29:33 +03: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 15:52:59 +04:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
1999-12-29 17:20:26 +03:00
|
|
|
*
|
2001-01-03 12:29:33 +03:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 17:20:26 +03:00
|
|
|
*
|
2002-09-03 15:52:59 +04:00
|
|
|
***************************************************************************/
|
1999-12-29 17:20:26 +03:00
|
|
|
|
2013-01-06 22:06:49 +04:00
|
|
|
#include "curl_setup.h"
|
2000-08-24 18:26:33 +04:00
|
|
|
|
2000-08-23 11:23:42 +04:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
1999-12-29 17:20:26 +03:00
|
|
|
#include <netinet/in.h>
|
2000-08-23 11:23:42 +04:00
|
|
|
#endif
|
2017-12-05 19:41:27 +03:00
|
|
|
#ifdef HAVE_NETINET_IN6_H
|
|
|
|
#include <netinet/in6.h>
|
|
|
|
#endif
|
2000-08-23 11:23:42 +04:00
|
|
|
#ifdef HAVE_NETDB_H
|
1999-12-29 17:20:26 +03:00
|
|
|
#include <netdb.h>
|
2000-08-23 11:23:42 +04:00
|
|
|
#endif
|
1999-12-29 17:20:26 +03:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2009-12-30 20:59:56 +03:00
|
|
|
#ifdef __VMS
|
2002-02-20 16:46:53 +03:00
|
|
|
#include <in.h>
|
2001-08-06 16:19:26 +04:00
|
|
|
#include <inet.h>
|
|
|
|
#endif
|
1999-12-29 17:20:26 +03:00
|
|
|
|
2002-06-11 19:10:18 +04:00
|
|
|
#ifdef HAVE_SETJMP_H
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
2008-09-30 01:02:22 +04:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2002-06-11 19:10:18 +04:00
|
|
|
|
2006-04-26 21:23:28 +04:00
|
|
|
#ifdef HAVE_PROCESS_H
|
2004-04-06 18:51:14 +04:00
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "hash.h"
|
2018-03-17 22:10:04 +03:00
|
|
|
#include "rand.h"
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "share.h"
|
|
|
|
#include "strerror.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "inet_ntop.h"
|
2018-02-14 20:20:43 +03:00
|
|
|
#include "multiif.h"
|
2018-09-06 10:16:02 +03:00
|
|
|
#include "doh.h"
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "warnless.h"
|
2016-04-29 16:46:40 +03:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 14:36:18 +03:00
|
|
|
#include "curl_printf.h"
|
2009-04-21 15:46:16 +04:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "memdebug.h"
|
2000-10-09 15:12:34 +04:00
|
|
|
|
2010-01-23 16:53:33 +03:00
|
|
|
#if defined(CURLRES_SYNCH) && \
|
|
|
|
defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP)
|
2008-09-30 01:02:22 +04:00
|
|
|
/* alarm-based timeouts can only be used with all the dependencies satisfied */
|
|
|
|
#define USE_ALARM_TIMEOUT
|
|
|
|
#endif
|
|
|
|
|
2019-02-10 02:10:18 +03:00
|
|
|
#define MAX_HOSTCACHE_LEN (255 + 7) /* max FQDN + colon + port number + zero */
|
|
|
|
|
2004-04-26 11:20:11 +04:00
|
|
|
/*
|
2013-01-04 05:50:28 +04:00
|
|
|
* hostip.c explained
|
|
|
|
* ==================
|
2004-04-26 11:20:11 +04:00
|
|
|
*
|
2013-01-04 05:50:28 +04:00
|
|
|
* The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
|
2004-04-26 11:20:11 +04: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 14:23:15 +04:00
|
|
|
* asynchronous name resolves. This can be Windows or *nix.
|
2004-04-26 11:20:11 +04: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 05:50:28 +04:00
|
|
|
* The host*.c sources files are split up like this:
|
2004-04-26 11:20:11 +04:00
|
|
|
*
|
2013-01-04 05:50:28 +04:00
|
|
|
* hostip.c - method-independent resolver functions and utility functions
|
|
|
|
* hostasyn.c - functions for asynchronous name resolves
|
|
|
|
* hostsyn.c - functions for synchronous name resolves
|
2014-12-27 14:09:01 +03:00
|
|
|
* hostip4.c - IPv4 specific functions
|
|
|
|
* hostip6.c - IPv6 specific functions
|
2004-04-26 11:20:11 +04:00
|
|
|
*
|
2011-04-25 21:50:56 +04:00
|
|
|
* The two asynchronous name resolver backends are implemented in:
|
2013-01-04 05:50:28 +04:00
|
|
|
* asyn-ares.c - functions for ares-using name resolves
|
|
|
|
* asyn-thread.c - functions for threaded name resolves
|
2011-04-25 21:50:56 +04:00
|
|
|
|
2013-01-04 05:50:28 +04:00
|
|
|
* The hostip.h is the united header file for all this. It defines the
|
2013-01-06 22:06:49 +04:00
|
|
|
* CURLRES_* defines based on the config*.h and curl_setup.h defines.
|
2004-04-26 11:20:11 +04:00
|
|
|
*/
|
2004-03-31 16:55:24 +04:00
|
|
|
|
2004-03-30 17:02:07 +04:00
|
|
|
static void freednsentry(void *freethis);
|
|
|
|
|
2004-06-10 15:06:21 +04:00
|
|
|
/*
|
2017-03-26 18:02:22 +03:00
|
|
|
* Return # of addresses in a Curl_addrinfo struct
|
2004-06-10 15:06:21 +04:00
|
|
|
*/
|
|
|
|
int Curl_num_addresses(const Curl_addrinfo *addr)
|
|
|
|
{
|
2008-10-17 16:53:53 +04:00
|
|
|
int i = 0;
|
|
|
|
while(addr) {
|
|
|
|
addr = addr->ai_next;
|
|
|
|
i++;
|
|
|
|
}
|
2004-06-24 11:43:48 +04:00
|
|
|
return i;
|
2004-06-10 15:06:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-06-10 15:55:37 +04:00
|
|
|
* Curl_printable_address() returns a printable version of the 1st address
|
2008-10-19 05:02:18 +04:00
|
|
|
* given in the 'ai' argument. The result will be stored in the buf that is
|
2004-06-10 15:55:37 +04:00
|
|
|
* bufsize bytes big.
|
2004-06-10 15:06:21 +04:00
|
|
|
*
|
|
|
|
* If the conversion fails, it returns NULL.
|
|
|
|
*/
|
2008-10-19 05:02:18 +04:00
|
|
|
const char *
|
|
|
|
Curl_printable_address(const Curl_addrinfo *ai, char *buf, size_t bufsize)
|
2004-06-10 15:06:21 +04:00
|
|
|
{
|
2008-10-20 02:46:54 +04:00
|
|
|
const struct sockaddr_in *sa4;
|
|
|
|
const struct in_addr *ipaddr4;
|
2008-10-19 05:02:18 +04:00
|
|
|
#ifdef ENABLE_IPV6
|
2008-10-20 02:46:54 +04:00
|
|
|
const struct sockaddr_in6 *sa6;
|
|
|
|
const struct in6_addr *ipaddr6;
|
2004-06-10 15:06:21 +04:00
|
|
|
#endif
|
2004-06-24 11:43:48 +04:00
|
|
|
|
2016-12-14 01:34:59 +03:00
|
|
|
switch(ai->ai_family) {
|
2008-10-19 05:02:18 +04:00
|
|
|
case AF_INET:
|
2008-10-20 02:46:54 +04:00
|
|
|
sa4 = (const void *)ai->ai_addr;
|
2008-10-19 05:02:18 +04:00
|
|
|
ipaddr4 = &sa4->sin_addr;
|
2010-01-23 02:21:39 +03:00
|
|
|
return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf,
|
|
|
|
bufsize);
|
2008-10-19 05:02:18 +04:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
case AF_INET6:
|
2008-10-20 02:46:54 +04:00
|
|
|
sa6 = (const void *)ai->ai_addr;
|
2008-10-19 05:02:18 +04:00
|
|
|
ipaddr6 = &sa6->sin6_addr;
|
2010-01-23 02:21:39 +03:00
|
|
|
return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf,
|
|
|
|
bufsize);
|
2008-10-19 05:02:18 +04:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
2004-06-10 15:06:21 +04:00
|
|
|
}
|
|
|
|
|
2004-03-30 17:02:07 +04:00
|
|
|
/*
|
2019-02-10 02:10:18 +03:00
|
|
|
* Create a hostcache id string for the provided host + port, to be used by
|
|
|
|
* the DNS caching. Without alloc.
|
2004-03-30 17:02:07 +04:00
|
|
|
*/
|
2019-02-10 02:10:18 +03:00
|
|
|
static void
|
|
|
|
create_hostcache_id(const char *name, int port, char *ptr, size_t buflen)
|
2002-01-17 09:55:37 +03:00
|
|
|
{
|
2019-02-10 02:10:18 +03:00
|
|
|
size_t len = strlen(name);
|
|
|
|
if(len > (buflen - 7))
|
|
|
|
len = buflen - 7;
|
|
|
|
/* store and lower case the name */
|
|
|
|
while(len--)
|
|
|
|
*ptr++ = (char)TOLOWER(*name++);
|
|
|
|
msnprintf(ptr, 7, ":%u", port);
|
2002-01-17 09:55:37 +03:00
|
|
|
}
|
|
|
|
|
2002-04-27 17:07:51 +04:00
|
|
|
struct hostcache_prune_data {
|
2007-08-26 09:53:26 +04:00
|
|
|
long cache_timeout;
|
2004-03-10 12:36:43 +03:00
|
|
|
time_t now;
|
2002-04-18 00:13:55 +04:00
|
|
|
};
|
|
|
|
|
2004-03-30 17:02:07 +04: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 00:13:55 +04:00
|
|
|
static int
|
2002-11-05 13:51:41 +03:00
|
|
|
hostcache_timestamp_remove(void *datap, void *hc)
|
2002-04-18 00:13:55 +04:00
|
|
|
{
|
2004-06-10 15:56:02 +04:00
|
|
|
struct hostcache_prune_data *data =
|
2002-04-27 17:07:51 +04:00
|
|
|
(struct hostcache_prune_data *) datap;
|
2002-11-05 13:51:41 +03:00
|
|
|
struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
|
2004-06-10 15:56:02 +04:00
|
|
|
|
2015-03-17 11:09:43 +03:00
|
|
|
return (0 != c->timestamp)
|
|
|
|
&& (data->now - c->timestamp >= data->cache_timeout);
|
2002-04-18 00:13:55 +04:00
|
|
|
}
|
|
|
|
|
2004-03-30 17:02:07 +04:00
|
|
|
/*
|
|
|
|
* Prune the DNS cache. This assumes that a lock has already been taken.
|
|
|
|
*/
|
2002-04-18 00:13:55 +04:00
|
|
|
static void
|
2007-08-26 09:53:26 +04:00
|
|
|
hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
|
2002-04-18 00:13:55 +04:00
|
|
|
{
|
2002-04-27 17:07:51 +04:00
|
|
|
struct hostcache_prune_data user;
|
2002-04-18 00:13:55 +04:00
|
|
|
|
|
|
|
user.cache_timeout = cache_timeout;
|
|
|
|
user.now = now;
|
2004-02-15 19:57:53 +03:00
|
|
|
|
2004-06-10 15:56:02 +04:00
|
|
|
Curl_hash_clean_with_criterium(hostcache,
|
|
|
|
(void *) &user,
|
2002-11-05 13:51:41 +03:00
|
|
|
hostcache_timestamp_remove);
|
2002-04-18 00:13:55 +04:00
|
|
|
}
|
|
|
|
|
2004-03-30 17:02:07 +04:00
|
|
|
/*
|
|
|
|
* Library-wide function for pruning the DNS cache. This function takes and
|
|
|
|
* returns the appropriate locks.
|
|
|
|
*/
|
2016-06-21 16:47:12 +03:00
|
|
|
void Curl_hostcache_prune(struct Curl_easy *data)
|
2003-12-15 20:33:49 +03:00
|
|
|
{
|
|
|
|
time_t now;
|
2004-02-18 10:56:18 +03:00
|
|
|
|
2006-07-08 02:58:06 +04:00
|
|
|
if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
|
2004-10-04 14:36:51 +04:00
|
|
|
/* cache forever means never prune, and NULL hostcache means
|
|
|
|
we can't do it */
|
2004-02-18 10:56:18 +03:00
|
|
|
return;
|
|
|
|
|
2003-12-15 20:33:49 +03: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 02:58:06 +04:00
|
|
|
hostcache_prune(data->dns.hostcache,
|
2003-12-15 20:33:49 +03:00
|
|
|
data->set.dns_cache_timeout,
|
|
|
|
now);
|
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
}
|
|
|
|
|
2003-08-05 18:40:59 +04:00
|
|
|
#ifdef HAVE_SIGSETJMP
|
2004-03-30 17:02:07 +04: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 18:40:59 +04:00
|
|
|
sigjmp_buf curl_jmpenv;
|
|
|
|
#endif
|
|
|
|
|
2015-03-17 11:26:36 +03:00
|
|
|
/* lookup address, returns entry if found and not stale */
|
|
|
|
static struct Curl_dns_entry *
|
|
|
|
fetch_addr(struct connectdata *conn,
|
2014-08-21 01:31:53 +04:00
|
|
|
const char *hostname,
|
2015-01-07 01:01:43 +03:00
|
|
|
int port)
|
2014-08-21 01:31:53 +04:00
|
|
|
{
|
|
|
|
struct Curl_dns_entry *dns = NULL;
|
|
|
|
size_t entry_len;
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = conn->data;
|
2019-02-10 02:10:18 +03:00
|
|
|
char entry_id[MAX_HOSTCACHE_LEN];
|
2014-08-21 01:31:53 +04:00
|
|
|
|
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2019-02-10 02:10:18 +03:00
|
|
|
create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
|
2014-08-21 01:31:53 +04:00
|
|
|
entry_len = strlen(entry_id);
|
|
|
|
|
|
|
|
/* See if its already in our dns cache */
|
2017-09-10 00:55:08 +03:00
|
|
|
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
|
2014-08-21 01:31:53 +04:00
|
|
|
|
2018-12-27 16:23:13 +03:00
|
|
|
/* No entry found in cache, check if we might have a wildcard entry */
|
|
|
|
if(!dns && data->change.wildcard_resolve) {
|
2019-02-10 02:10:18 +03:00
|
|
|
create_hostcache_id("*", port, entry_id, sizeof(entry_id));
|
2018-12-27 16:23:13 +03:00
|
|
|
entry_len = strlen(entry_id);
|
|
|
|
|
|
|
|
/* See if it's already in our dns cache */
|
|
|
|
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
|
|
|
|
}
|
|
|
|
|
2017-09-12 10:29:01 +03:00
|
|
|
if(dns && (data->set.dns_cache_timeout != -1)) {
|
2015-03-17 11:26:36 +03:00
|
|
|
/* See whether the returned entry is stale. Done before we release lock */
|
|
|
|
struct hostcache_prune_data user;
|
|
|
|
|
|
|
|
time(&user.now);
|
|
|
|
user.cache_timeout = data->set.dns_cache_timeout;
|
|
|
|
|
|
|
|
if(hostcache_timestamp_remove(&user, dns)) {
|
|
|
|
infof(data, "Hostname in DNS cache was stale, zapped\n");
|
|
|
|
dns = NULL; /* the memory deallocation is being handled by the hash */
|
2017-09-10 00:55:08 +03:00
|
|
|
Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
|
2015-03-17 11:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
|
|
|
|
*
|
|
|
|
* Curl_resolv() checks initially and multi_runsingle() checks each time
|
|
|
|
* it discovers the handle in the state WAITRESOLVE whether the hostname
|
|
|
|
* has already been resolved and the address has already been stored in
|
|
|
|
* the DNS cache. This short circuits waiting for a lot of pending
|
|
|
|
* lookups for the same hostname requested by different handles.
|
|
|
|
*
|
|
|
|
* Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
|
|
|
|
*
|
|
|
|
* The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
|
|
|
|
* use, or we'll leak memory!
|
|
|
|
*/
|
|
|
|
struct Curl_dns_entry *
|
|
|
|
Curl_fetch_addr(struct connectdata *conn,
|
|
|
|
const char *hostname,
|
|
|
|
int port)
|
|
|
|
{
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-03-17 11:26:36 +03:00
|
|
|
struct Curl_dns_entry *dns = NULL;
|
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
|
|
|
dns = fetch_addr(conn, hostname, port);
|
|
|
|
|
2016-03-29 16:19:21 +03:00
|
|
|
if(dns)
|
|
|
|
dns->inuse++; /* we use it! */
|
2015-03-17 11:26:36 +03:00
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2014-08-21 01:31:53 +04:00
|
|
|
|
|
|
|
return dns;
|
|
|
|
}
|
2003-08-05 18:40:59 +04:00
|
|
|
|
2019-02-08 11:33:42 +03:00
|
|
|
UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
|
|
|
|
Curl_addrinfo **addr);
|
|
|
|
|
2018-03-17 22:10:04 +03:00
|
|
|
/*
|
|
|
|
* Curl_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo'
|
|
|
|
* struct by re-linking its linked list.
|
|
|
|
*
|
|
|
|
* The addr argument should be the address of a pointer to the head node of a
|
|
|
|
* `Curl_addrinfo` list and it will be modified to point to the new head after
|
|
|
|
* shuffling.
|
|
|
|
*
|
|
|
|
* Not declared static only to make it easy to use in a unit test!
|
|
|
|
*
|
|
|
|
* @unittest: 1608
|
|
|
|
*/
|
2019-02-08 11:33:42 +03:00
|
|
|
UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
|
|
|
|
Curl_addrinfo **addr)
|
2018-03-17 22:10:04 +03:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
const int num_addrs = Curl_num_addresses(*addr);
|
|
|
|
|
|
|
|
if(num_addrs > 1) {
|
|
|
|
Curl_addrinfo **nodes;
|
|
|
|
infof(data, "Shuffling %i addresses", num_addrs);
|
|
|
|
|
|
|
|
nodes = malloc(num_addrs*sizeof(*nodes));
|
|
|
|
if(nodes) {
|
|
|
|
int i;
|
|
|
|
unsigned int *rnd;
|
|
|
|
const size_t rnd_size = num_addrs * sizeof(*rnd);
|
|
|
|
|
|
|
|
/* build a plain array of Curl_addrinfo pointers */
|
|
|
|
nodes[0] = *addr;
|
|
|
|
for(i = 1; i < num_addrs; i++) {
|
|
|
|
nodes[i] = nodes[i-1]->ai_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
rnd = malloc(rnd_size);
|
|
|
|
if(rnd) {
|
|
|
|
/* Fisher-Yates shuffle */
|
|
|
|
if(Curl_rand(data, (unsigned char *)rnd, rnd_size) == CURLE_OK) {
|
|
|
|
Curl_addrinfo *swap_tmp;
|
|
|
|
for(i = num_addrs - 1; i > 0; i--) {
|
|
|
|
swap_tmp = nodes[rnd[i] % (i + 1)];
|
|
|
|
nodes[rnd[i] % (i + 1)] = nodes[i];
|
|
|
|
nodes[i] = swap_tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* relink list in the new order */
|
|
|
|
for(i = 1; i < num_addrs; i++) {
|
|
|
|
nodes[i-1]->ai_next = nodes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[num_addrs-1]->ai_next = NULL;
|
|
|
|
*addr = nodes[0];
|
|
|
|
}
|
|
|
|
free(rnd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
free(nodes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-03-30 17:02:07 +04:00
|
|
|
/*
|
2004-04-26 11:20:11 +04:00
|
|
|
* Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
|
2004-03-30 17:02:07 +04: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 11:20:11 +04:00
|
|
|
struct Curl_dns_entry *
|
2016-06-21 16:47:12 +03:00
|
|
|
Curl_cache_addr(struct Curl_easy *data,
|
2004-04-26 11:20:11 +04:00
|
|
|
Curl_addrinfo *addr,
|
2006-07-21 09:51:12 +04:00
|
|
|
const char *hostname,
|
2004-04-26 11:20:11 +04:00
|
|
|
int port)
|
2002-11-05 13:51:41 +03:00
|
|
|
{
|
2019-02-10 02:10:18 +03:00
|
|
|
char entry_id[MAX_HOSTCACHE_LEN];
|
2004-02-23 11:35:57 +03:00
|
|
|
size_t entry_len;
|
2003-08-05 18:40:59 +04:00
|
|
|
struct Curl_dns_entry *dns;
|
2004-05-04 17:40:30 +04:00
|
|
|
struct Curl_dns_entry *dns2;
|
2003-08-05 18:40:59 +04:00
|
|
|
|
2018-03-17 22:10:04 +03:00
|
|
|
/* shuffle addresses if requested */
|
|
|
|
if(data->set.dns_shuffle_addresses) {
|
|
|
|
CURLcode result = Curl_shuffle_addr(data, &addr);
|
2018-10-07 19:18:03 +03:00
|
|
|
if(result)
|
2018-03-17 22:10:04 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-08-05 18:40:59 +04:00
|
|
|
/* Create a new cache entry */
|
2009-11-18 13:33:54 +03:00
|
|
|
dns = calloc(1, sizeof(struct Curl_dns_entry));
|
2007-11-07 12:21:35 +03:00
|
|
|
if(!dns) {
|
2003-08-05 18:40:59 +04:00
|
|
|
return NULL;
|
2002-11-05 13:51:41 +03:00
|
|
|
}
|
|
|
|
|
2019-02-10 02:10:18 +03:00
|
|
|
/* Create an entry id, based upon the hostname and port */
|
|
|
|
create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
|
|
|
|
entry_len = strlen(entry_id);
|
|
|
|
|
2015-03-17 11:09:43 +03:00
|
|
|
dns->inuse = 1; /* the cache has the first reference */
|
2003-09-15 01:17:54 +04:00
|
|
|
dns->addr = addr; /* this is the address(es) */
|
2009-11-11 12:31:37 +03:00
|
|
|
time(&dns->timestamp);
|
|
|
|
if(dns->timestamp == 0)
|
2015-03-17 11:09:43 +03:00
|
|
|
dns->timestamp = 1; /* zero indicates CURLOPT_RESOLVE entry */
|
2003-09-15 01:17:54 +04:00
|
|
|
|
2009-11-11 12:31:37 +03:00
|
|
|
/* Store the resolved data in our DNS cache. */
|
2017-09-10 00:55:08 +03:00
|
|
|
dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len + 1,
|
2006-07-08 02:58:06 +04:00
|
|
|
(void *)dns);
|
2004-05-04 17:40:30 +04:00
|
|
|
if(!dns2) {
|
|
|
|
free(dns);
|
2003-09-15 01:17:54 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-08-05 18:40:59 +04:00
|
|
|
|
2009-11-11 12:31:37 +03:00
|
|
|
dns = dns2;
|
2003-08-05 18:40:59 +04:00
|
|
|
dns->inuse++; /* mark entry as in-use */
|
|
|
|
return dns;
|
2002-01-17 10:38:25 +03:00
|
|
|
}
|
2002-01-17 09:55:37 +03:00
|
|
|
|
2004-03-30 17:02:07 +04: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 18:27:01 +04: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 17:02:07 +04:00
|
|
|
* Return codes:
|
|
|
|
*
|
2004-04-26 18:18:00 +04:00
|
|
|
* CURLRESOLV_ERROR (-1) = error, no pointer
|
|
|
|
* CURLRESOLV_RESOLVED (0) = OK, pointer provided
|
|
|
|
* CURLRESOLV_PENDING (1) = waiting for response, no pointer
|
2004-03-30 17:02:07 +04:00
|
|
|
*/
|
2004-04-26 18:18:00 +04:00
|
|
|
|
2003-08-05 18:40:59 +04:00
|
|
|
int Curl_resolv(struct connectdata *conn,
|
2006-07-21 09:51:12 +04:00
|
|
|
const char *hostname,
|
2003-08-05 18:40:59 +04:00
|
|
|
int port,
|
2019-05-09 11:58:04 +03:00
|
|
|
bool allowDOH,
|
2003-08-05 18:40:59 +04:00
|
|
|
struct Curl_dns_entry **entry)
|
2002-01-03 13:22:59 +03:00
|
|
|
{
|
2002-11-05 13:51:41 +03:00
|
|
|
struct Curl_dns_entry *dns = NULL;
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = conn->data;
|
2003-11-14 14:56:15 +03:00
|
|
|
CURLcode result;
|
2015-01-07 01:01:43 +03:00
|
|
|
int rc = CURLRESOLV_ERROR; /* default to failure */
|
2002-01-08 07:26:47 +03:00
|
|
|
|
2008-09-30 01:02:22 +04:00
|
|
|
*entry = NULL;
|
2002-06-11 19:10:18 +04:00
|
|
|
|
2003-02-05 02:48:46 +03:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2015-03-17 11:26:36 +03:00
|
|
|
dns = fetch_addr(conn, hostname, port);
|
2008-03-12 01:55:23 +03:00
|
|
|
|
|
|
|
if(dns) {
|
2015-01-07 01:01:43 +03:00
|
|
|
infof(data, "Hostname %s was found in DNS cache\n", hostname);
|
2008-03-12 01:55:23 +03:00
|
|
|
dns->inuse++; /* we use it! */
|
|
|
|
rc = CURLRESOLV_RESOLVED;
|
|
|
|
}
|
|
|
|
|
2003-10-12 17:57:41 +04:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2002-01-08 07:26:47 +03:00
|
|
|
|
2007-11-07 12:21:35 +03:00
|
|
|
if(!dns) {
|
2003-08-05 18:40:59 +04:00
|
|
|
/* The entry was not in the cache. Resolve it to IP address */
|
2004-04-26 11:20:11 +04:00
|
|
|
|
|
|
|
Curl_addrinfo *addr;
|
2019-02-12 20:43:55 +03:00
|
|
|
int respwait = 0;
|
2004-04-26 11:20:11 +04:00
|
|
|
|
|
|
|
/* Check what IP specifics the app has requested and if we can provide it.
|
|
|
|
* If not, bail out. */
|
2010-11-11 16:51:39 +03:00
|
|
|
if(!Curl_ipvalid(conn))
|
2004-04-26 18:18:00 +04:00
|
|
|
return CURLRESOLV_ERROR;
|
2004-04-26 11:20:11 +04:00
|
|
|
|
2018-02-14 20:20:43 +03:00
|
|
|
/* notify the resolver start callback */
|
|
|
|
if(data->set.resolver_start) {
|
|
|
|
int st;
|
|
|
|
Curl_set_in_callback(data, true);
|
|
|
|
st = data->set.resolver_start(data->state.resolver, NULL,
|
|
|
|
data->set.resolver_start_client);
|
|
|
|
Curl_set_in_callback(data, false);
|
|
|
|
if(st)
|
|
|
|
return CURLRESOLV_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-05-09 11:58:04 +03:00
|
|
|
if(allowDOH && data->set.doh) {
|
2018-09-06 10:16:02 +03:00
|
|
|
addr = Curl_doh(conn, hostname, port, &respwait);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* 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,
|
2009-09-01 18:27:01 +04:00
|
|
|
#ifdef DEBUGBUILD
|
2018-09-06 10:16:02 +03:00
|
|
|
(data->set.str[STRING_DEVICE]
|
|
|
|
&& !strcmp(data->set.str[STRING_DEVICE],
|
|
|
|
"LocalHost"))?"localhost":
|
2009-09-01 18:27:01 +04:00
|
|
|
#endif
|
2018-09-06 10:16:02 +03:00
|
|
|
hostname, port, &respwait);
|
|
|
|
}
|
2007-11-07 12:21:35 +03:00
|
|
|
if(!addr) {
|
2007-09-28 22:47:59 +04:00
|
|
|
if(respwait) {
|
2004-06-10 15:56:02 +04:00
|
|
|
/* the response to our resolve call will come asynchronously at
|
2003-08-05 18:40:59 +04:00
|
|
|
a later time, good or bad */
|
2003-10-04 18:50:45 +04:00
|
|
|
/* First, check that we haven't received the info by now */
|
2018-09-06 10:16:02 +03:00
|
|
|
result = Curl_resolv_check(conn, &dns);
|
2003-11-14 14:56:15 +03:00
|
|
|
if(result) /* error detected */
|
2004-04-26 18:18:00 +04:00
|
|
|
return CURLRESOLV_ERROR;
|
2003-10-04 18:50:45 +04:00
|
|
|
if(dns)
|
2004-04-26 18:18:00 +04:00
|
|
|
rc = CURLRESOLV_RESOLVED; /* pointer provided */
|
2003-10-04 18:50:45 +04:00
|
|
|
else
|
2004-04-26 18:18:00 +04:00
|
|
|
rc = CURLRESOLV_PENDING; /* no info yet */
|
2003-10-04 18:50:45 +04:00
|
|
|
}
|
2002-11-05 13:51:41 +03:00
|
|
|
}
|
2004-01-14 12:05:54 +03:00
|
|
|
else {
|
2003-10-12 17:57:41 +04:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2003-08-05 18:40:59 +04:00
|
|
|
/* we got a response, store it in the cache */
|
2004-04-26 11:20:11 +04:00
|
|
|
dns = Curl_cache_addr(data, addr, hostname, port);
|
2004-06-10 15:56:02 +04:00
|
|
|
|
2003-10-12 17:57:41 +04:00
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2003-10-24 16:57:23 +04:00
|
|
|
|
2004-01-14 12:05:54 +03:00
|
|
|
if(!dns)
|
2003-10-24 16:57:23 +04:00
|
|
|
/* returned failure, bail out nicely */
|
|
|
|
Curl_freeaddrinfo(addr);
|
2003-12-03 10:55:51 +03:00
|
|
|
else
|
2004-04-26 18:18:00 +04:00
|
|
|
rc = CURLRESOLV_RESOLVED;
|
2003-10-12 17:57:41 +04:00
|
|
|
}
|
2002-01-17 10:38:25 +03:00
|
|
|
}
|
2002-01-08 07:26:47 +03:00
|
|
|
|
2003-08-05 18:40:59 +04:00
|
|
|
*entry = dns;
|
2002-01-03 13:22:59 +03:00
|
|
|
|
2003-08-05 18:40:59 +04:00
|
|
|
return rc;
|
2002-01-03 13:22:59 +03:00
|
|
|
}
|
|
|
|
|
2009-09-01 18:27:01 +04:00
|
|
|
#ifdef USE_ALARM_TIMEOUT
|
2008-09-30 01:02:22 +04: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);
|
|
|
|
}
|
|
|
|
#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,
|
2016-11-18 12:07:08 +03:00
|
|
|
time_t timeoutms)
|
2008-09-30 01:02:22 +04:00
|
|
|
{
|
2009-09-01 18:27:01 +04:00
|
|
|
#ifdef USE_ALARM_TIMEOUT
|
2008-09-30 01:02:22 +04:00
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
struct sigaction keep_sigact; /* store the old struct here */
|
2017-03-26 18:02:22 +03:00
|
|
|
volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */
|
2008-09-30 01:02:22 +04: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 02:58:09 +04:00
|
|
|
volatile long timeout;
|
2010-04-13 18:34:13 +04:00
|
|
|
volatile unsigned int prev_alarm = 0;
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = conn->data;
|
2008-09-30 01:02:22 +04:00
|
|
|
#endif /* USE_ALARM_TIMEOUT */
|
2008-10-01 21:34:24 +04:00
|
|
|
int rc;
|
2008-09-30 01:02:22 +04:00
|
|
|
|
|
|
|
*entry = NULL;
|
|
|
|
|
2011-09-01 19:09:46 +04:00
|
|
|
if(timeoutms < 0)
|
|
|
|
/* got an already expired timeout */
|
|
|
|
return CURLRESOLV_TIMEDOUT;
|
|
|
|
|
2009-09-01 18:27:01 +04:00
|
|
|
#ifdef USE_ALARM_TIMEOUT
|
2011-04-20 17:17:42 +04:00
|
|
|
if(data->set.no_signal)
|
2008-09-30 01:02:22 +04:00
|
|
|
/* Ignore the timeout when signals are disabled */
|
|
|
|
timeout = 0;
|
2008-10-03 02:58:09 +04:00
|
|
|
else
|
2017-05-20 20:39:51 +03:00
|
|
|
timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms;
|
2008-09-30 01:02:22 +04:00
|
|
|
|
2010-04-05 01:35:21 +04:00
|
|
|
if(!timeout)
|
|
|
|
/* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
|
2019-05-09 11:58:04 +03:00
|
|
|
return Curl_resolv(conn, hostname, port, TRUE, entry);
|
2010-04-05 01:35:21 +04:00
|
|
|
|
2016-10-10 14:08:24 +03:00
|
|
|
if(timeout < 1000) {
|
2008-09-30 01:02:22 +04: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. */
|
2016-10-10 14:08:24 +03:00
|
|
|
failf(data,
|
|
|
|
"remaining timeout of %ld too small to resolve via SIGALRM method",
|
|
|
|
timeout);
|
2008-09-30 01:02:22 +04:00
|
|
|
return CURLRESOLV_TIMEDOUT;
|
2016-10-10 14:08:24 +03:00
|
|
|
}
|
2010-04-05 01:35:21 +04: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 01:02:22 +04:00
|
|
|
}
|
2015-03-14 16:32:42 +03:00
|
|
|
else {
|
|
|
|
/*************************************************************
|
|
|
|
* Set signal handler to catch SIGALRM
|
|
|
|
* Store the old value to be able to set it back later!
|
|
|
|
*************************************************************/
|
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
sigaction(SIGALRM, NULL, &sigact);
|
|
|
|
keep_sigact = sigact;
|
|
|
|
keep_copysig = TRUE; /* yes, we have a copy */
|
|
|
|
sigact.sa_handler = alarmfunc;
|
|
|
|
#ifdef SA_RESTART
|
|
|
|
/* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
|
|
|
|
sigact.sa_flags &= ~SA_RESTART;
|
|
|
|
#endif
|
|
|
|
/* now set the new struct */
|
|
|
|
sigaction(SIGALRM, &sigact, NULL);
|
|
|
|
#else /* HAVE_SIGACTION */
|
|
|
|
/* no sigaction(), revert to the much lamer signal() */
|
|
|
|
#ifdef HAVE_SIGNAL
|
|
|
|
keep_sigact = signal(SIGALRM, alarmfunc);
|
|
|
|
#endif
|
|
|
|
#endif /* HAVE_SIGACTION */
|
|
|
|
|
|
|
|
/* alarm() makes a signal get sent when the timeout fires off, and that
|
|
|
|
will abort system calls */
|
|
|
|
prev_alarm = alarm(curlx_sltoui(timeout/1000L));
|
|
|
|
}
|
2008-09-30 01:02:22 +04:00
|
|
|
|
|
|
|
#else
|
|
|
|
#ifndef CURLRES_ASYNCH
|
2008-10-03 06:25:52 +04:00
|
|
|
if(timeoutms)
|
2008-09-30 03:45:43 +04:00
|
|
|
infof(conn->data, "timeout on name lookup is not supported\n");
|
|
|
|
#else
|
2008-10-03 04:35:22 +04:00
|
|
|
(void)timeoutms; /* timeoutms not used with an async resolver */
|
2008-09-30 01:02:22 +04:00
|
|
|
#endif
|
|
|
|
#endif /* USE_ALARM_TIMEOUT */
|
|
|
|
|
|
|
|
/* Perform the actual name resolution. This might be interrupted by an
|
|
|
|
* alarm if it takes too long.
|
|
|
|
*/
|
2019-05-09 11:58:04 +03:00
|
|
|
rc = Curl_resolv(conn, hostname, port, TRUE, entry);
|
2008-09-30 01:02:22 +04:00
|
|
|
|
2010-03-24 19:00:20 +03:00
|
|
|
#ifdef USE_ALARM_TIMEOUT
|
2010-03-22 23:57:48 +03:00
|
|
|
clean_up:
|
|
|
|
|
2010-04-05 01:35:21 +04:00
|
|
|
if(!prev_alarm)
|
|
|
|
/* deactivate a possibly active alarm before uninstalling the handler */
|
|
|
|
alarm(0);
|
2008-09-30 01:02:22 +04:00
|
|
|
|
|
|
|
#ifdef HAVE_SIGACTION
|
2010-04-05 01:35:21 +04: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 01:02:22 +04:00
|
|
|
#else
|
|
|
|
#ifdef HAVE_SIGNAL
|
2010-04-05 01:35:21 +04:00
|
|
|
/* restore the previous SIGALRM handler */
|
|
|
|
signal(SIGALRM, keep_sigact);
|
2008-09-30 01:02:22 +04:00
|
|
|
#endif
|
|
|
|
#endif /* HAVE_SIGACTION */
|
|
|
|
|
2010-04-05 01:35:21 +04: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 */
|
2017-10-25 12:59:43 +03:00
|
|
|
timediff_t elapsed_secs = Curl_timediff(Curl_now(),
|
2017-10-23 13:05:49 +03:00
|
|
|
conn->created) / 1000;
|
2010-04-05 01:35:21 +04:00
|
|
|
|
|
|
|
/* the alarm period is counted in even number of seconds */
|
2017-05-20 20:39:51 +03:00
|
|
|
unsigned long alarm_set = prev_alarm - elapsed_secs;
|
2010-04-05 01:35:21 +04:00
|
|
|
|
|
|
|
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 01:02:22 +04:00
|
|
|
}
|
2010-04-05 01:35:21 +04:00
|
|
|
else
|
|
|
|
alarm((unsigned int)alarm_set);
|
2008-09-30 01:02:22 +04:00
|
|
|
}
|
|
|
|
#endif /* USE_ALARM_TIMEOUT */
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2004-03-30 17:02:07 +04: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.
|
2013-08-08 15:01:16 +04:00
|
|
|
*
|
|
|
|
* May be called with 'data' == NULL for global cache.
|
2004-03-30 17:02:07 +04:00
|
|
|
*/
|
2016-06-21 16:47:12 +03:00
|
|
|
void Curl_resolv_unlock(struct Curl_easy *data, struct Curl_dns_entry *dns)
|
2003-02-05 02:48:46 +03:00
|
|
|
{
|
2013-08-08 15:01:16 +04:00
|
|
|
if(data && data->share)
|
2003-02-05 02:48:46 +03:00
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
2003-07-23 16:55:24 +04:00
|
|
|
|
2015-03-17 11:09:43 +03:00
|
|
|
freednsentry(dns);
|
2003-07-23 16:55:24 +04:00
|
|
|
|
2013-08-08 15:01:16 +04:00
|
|
|
if(data && data->share)
|
2003-02-05 02:48:46 +03:00
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
}
|
|
|
|
|
2002-11-26 12:41:54 +03:00
|
|
|
/*
|
2015-03-17 11:09:43 +03:00
|
|
|
* File-internal: release cache dns entry reference, free if inuse drops to 0
|
2002-11-26 12:41:54 +03:00
|
|
|
*/
|
2004-03-30 17:02:07 +04:00
|
|
|
static void freednsentry(void *freethis)
|
2002-11-26 12:41:54 +03:00
|
|
|
{
|
2015-03-17 11:09:43 +03:00
|
|
|
struct Curl_dns_entry *dns = (struct Curl_dns_entry *) freethis;
|
|
|
|
DEBUGASSERT(dns && (dns->inuse>0));
|
2002-11-26 12:41:54 +03:00
|
|
|
|
2015-03-17 11:09:43 +03:00
|
|
|
dns->inuse--;
|
|
|
|
if(dns->inuse == 0) {
|
|
|
|
Curl_freeaddrinfo(dns->addr);
|
|
|
|
free(dns);
|
2008-10-30 16:45:25 +03:00
|
|
|
}
|
2001-10-04 17:36:11 +04:00
|
|
|
}
|
|
|
|
|
2004-03-30 17:02:07 +04:00
|
|
|
/*
|
2015-05-12 10:46:53 +03:00
|
|
|
* Curl_mk_dnscache() inits a new DNS cache and returns success/failure.
|
2004-03-30 17:02:07 +04:00
|
|
|
*/
|
2015-05-12 10:46:53 +03:00
|
|
|
int Curl_mk_dnscache(struct curl_hash *hash)
|
2004-03-30 17:02:07 +04:00
|
|
|
{
|
2015-05-12 10:46:53 +03:00
|
|
|
return Curl_hash_init(hash, 7, Curl_hash_str, Curl_str_key_compare,
|
|
|
|
freednsentry);
|
2004-03-30 17:02:07 +04:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:01:16 +04:00
|
|
|
/*
|
|
|
|
* Curl_hostcache_clean()
|
|
|
|
*
|
|
|
|
* This _can_ be called with 'data' == NULL but then of course no locking
|
|
|
|
* can be done!
|
|
|
|
*/
|
|
|
|
|
2016-06-21 16:47:12 +03:00
|
|
|
void Curl_hostcache_clean(struct Curl_easy *data,
|
2013-08-08 15:01:16 +04:00
|
|
|
struct curl_hash *hash)
|
2011-12-23 17:30:43 +04:00
|
|
|
{
|
2015-03-17 11:09:43 +03:00
|
|
|
if(data && data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2015-04-04 00:41:58 +03:00
|
|
|
Curl_hash_clean(hash);
|
2015-03-17 11:09:43 +03:00
|
|
|
|
|
|
|
if(data && data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2012-11-18 19:17:37 +04:00
|
|
|
}
|
2011-12-23 17:30:43 +04:00
|
|
|
|
2011-12-31 13:28:10 +04:00
|
|
|
|
2016-06-21 16:47:12 +03:00
|
|
|
CURLcode Curl_loadhostpairs(struct Curl_easy *data)
|
2011-12-31 13:28:10 +04:00
|
|
|
{
|
|
|
|
struct curl_slist *hostp;
|
|
|
|
char hostname[256];
|
2018-01-31 03:33:51 +03:00
|
|
|
int port = 0;
|
2011-12-31 13:28:10 +04:00
|
|
|
|
2018-12-27 16:23:13 +03:00
|
|
|
/* Default is no wildcard found */
|
|
|
|
data->change.wildcard_resolve = false;
|
|
|
|
|
2016-04-03 21:28:34 +03:00
|
|
|
for(hostp = data->change.resolve; hostp; hostp = hostp->next) {
|
2019-02-10 02:10:18 +03:00
|
|
|
char entry_id[MAX_HOSTCACHE_LEN];
|
2011-12-31 13:28:10 +04:00
|
|
|
if(!hostp->data)
|
|
|
|
continue;
|
|
|
|
if(hostp->data[0] == '-') {
|
2015-03-17 11:15:25 +03:00
|
|
|
size_t entry_len;
|
|
|
|
|
|
|
|
if(2 != sscanf(hostp->data + 1, "%255[^:]:%d", hostname, &port)) {
|
|
|
|
infof(data, "Couldn't parse CURLOPT_RESOLVE removal entry '%s'!\n",
|
|
|
|
hostp->data);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2019-02-10 02:10:18 +03:00
|
|
|
create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
|
2015-03-17 11:15:25 +03:00
|
|
|
entry_len = strlen(entry_id);
|
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
|
|
|
/* delete entry, ignore if it didn't exist */
|
2017-09-10 00:55:08 +03:00
|
|
|
Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
|
2015-03-17 11:15:25 +03:00
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2011-12-31 13:28:10 +04:00
|
|
|
}
|
2015-03-17 11:15:25 +03:00
|
|
|
else {
|
2011-12-31 13:28:10 +04:00
|
|
|
struct Curl_dns_entry *dns;
|
2018-01-31 03:33:51 +03:00
|
|
|
Curl_addrinfo *head = NULL, *tail = NULL;
|
2011-12-31 13:28:10 +04:00
|
|
|
size_t entry_len;
|
2018-01-31 03:33:51 +03:00
|
|
|
char address[64];
|
2018-08-02 09:24:27 +03:00
|
|
|
#if !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
2018-03-13 01:52:28 +03:00
|
|
|
char *addresses = NULL;
|
2018-08-02 09:24:27 +03:00
|
|
|
#endif
|
2018-01-31 03:33:51 +03:00
|
|
|
char *addr_begin;
|
|
|
|
char *addr_end;
|
|
|
|
char *port_ptr;
|
|
|
|
char *end_ptr;
|
|
|
|
char *host_end;
|
|
|
|
unsigned long tmp_port;
|
|
|
|
bool error = true;
|
|
|
|
|
|
|
|
host_end = strchr(hostp->data, ':');
|
|
|
|
if(!host_end ||
|
|
|
|
((host_end - hostp->data) >= (ptrdiff_t)sizeof(hostname)))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
memcpy(hostname, hostp->data, host_end - hostp->data);
|
|
|
|
hostname[host_end - hostp->data] = '\0';
|
|
|
|
|
|
|
|
port_ptr = host_end + 1;
|
|
|
|
tmp_port = strtoul(port_ptr, &end_ptr, 10);
|
2018-02-20 22:29:50 +03:00
|
|
|
if(tmp_port > USHRT_MAX || end_ptr == port_ptr || *end_ptr != ':')
|
2018-01-31 03:33:51 +03:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
port = (int)tmp_port;
|
2018-08-02 09:24:27 +03:00
|
|
|
#if !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
2018-01-31 03:33:51 +03:00
|
|
|
addresses = end_ptr + 1;
|
2018-08-02 09:24:27 +03:00
|
|
|
#endif
|
2018-01-31 03:33:51 +03:00
|
|
|
|
|
|
|
while(*end_ptr) {
|
|
|
|
size_t alen;
|
|
|
|
Curl_addrinfo *ai;
|
|
|
|
|
|
|
|
addr_begin = end_ptr + 1;
|
|
|
|
addr_end = strchr(addr_begin, ',');
|
|
|
|
if(!addr_end)
|
|
|
|
addr_end = addr_begin + strlen(addr_begin);
|
|
|
|
end_ptr = addr_end;
|
|
|
|
|
|
|
|
/* allow IP(v6) address within [brackets] */
|
|
|
|
if(*addr_begin == '[') {
|
|
|
|
if(addr_end == addr_begin || *(addr_end - 1) != ']')
|
|
|
|
goto err;
|
|
|
|
++addr_begin;
|
|
|
|
--addr_end;
|
|
|
|
}
|
2011-12-31 13:28:10 +04:00
|
|
|
|
2018-01-31 03:33:51 +03:00
|
|
|
alen = addr_end - addr_begin;
|
|
|
|
if(!alen)
|
|
|
|
continue;
|
2015-03-17 11:15:25 +03:00
|
|
|
|
2018-01-31 03:33:51 +03:00
|
|
|
if(alen >= sizeof(address))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
memcpy(address, addr_begin, alen);
|
|
|
|
address[alen] = '\0';
|
|
|
|
|
|
|
|
#ifndef ENABLE_IPV6
|
|
|
|
if(strchr(address, ':')) {
|
|
|
|
infof(data, "Ignoring resolve address '%s', missing IPv6 support.\n",
|
|
|
|
address);
|
2017-11-17 13:21:12 +03:00
|
|
|
continue;
|
2018-01-31 03:33:51 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ai = Curl_str2addr(address, port);
|
|
|
|
if(!ai) {
|
|
|
|
infof(data, "Resolve address '%s' found illegal!\n", address);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tail) {
|
|
|
|
tail->ai_next = ai;
|
|
|
|
tail = tail->ai_next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
head = tail = ai;
|
|
|
|
}
|
2017-11-17 13:21:12 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 03:33:51 +03:00
|
|
|
if(!head)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
error = false;
|
|
|
|
err:
|
|
|
|
if(error) {
|
|
|
|
infof(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'!\n",
|
|
|
|
hostp->data);
|
|
|
|
Curl_freeaddrinfo(head);
|
2011-12-31 13:28:10 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2019-02-10 02:10:18 +03:00
|
|
|
create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
|
2011-12-31 13:28:10 +04: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 */
|
2017-09-10 00:55:08 +03:00
|
|
|
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
|
2011-12-31 13:28:10 +04:00
|
|
|
|
2018-05-30 17:23:35 +03:00
|
|
|
if(dns) {
|
|
|
|
infof(data, "RESOLVE %s:%d is - old addresses discarded!\n",
|
|
|
|
hostname, port);
|
|
|
|
/* delete old entry entry, there are two reasons for this
|
|
|
|
1. old entry may have different addresses.
|
|
|
|
2. even if entry with correct addresses is already in the cache,
|
|
|
|
but if it is close to expire, then by the time next http
|
|
|
|
request is made, it can get expired and pruned because old
|
|
|
|
entry is not necessarily marked as added by CURLOPT_RESOLVE. */
|
|
|
|
|
|
|
|
Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
|
|
|
|
}
|
2011-12-31 13:28:10 +04:00
|
|
|
|
2018-05-30 17:23:35 +03:00
|
|
|
/* put this new host in the cache */
|
|
|
|
dns = Curl_cache_addr(data, head, hostname, port);
|
|
|
|
if(dns) {
|
|
|
|
dns->timestamp = 0; /* mark as added by CURLOPT_RESOLVE */
|
|
|
|
/* release the returned reference; the cache itself will keep the
|
|
|
|
* entry alive: */
|
|
|
|
dns->inuse--;
|
2017-12-10 02:41:57 +03:00
|
|
|
}
|
2011-12-31 13:28:10 +04:00
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
|
|
|
|
if(!dns) {
|
2018-01-31 03:33:51 +03:00
|
|
|
Curl_freeaddrinfo(head);
|
2011-12-31 13:28:10 +04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
infof(data, "Added %s:%d:%s to DNS cache\n",
|
2018-01-31 03:33:51 +03:00
|
|
|
hostname, port, addresses);
|
2018-12-27 16:23:13 +03:00
|
|
|
|
|
|
|
/* Wildcard hostname */
|
|
|
|
if(hostname[0] == '*' && hostname[1] == '\0') {
|
|
|
|
infof(data, "RESOLVE %s:%d is wildcard, enabling wildcard checks\n",
|
|
|
|
hostname, port);
|
|
|
|
data->change.wildcard_resolve = true;
|
|
|
|
}
|
2011-12-31 13:28:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
data->change.resolve = NULL; /* dealt with now */
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2018-09-06 10:16:02 +03:00
|
|
|
|
|
|
|
CURLcode Curl_resolv_check(struct connectdata *conn,
|
|
|
|
struct Curl_dns_entry **dns)
|
|
|
|
{
|
|
|
|
if(conn->data->set.doh)
|
|
|
|
return Curl_doh_is_resolved(conn, dns);
|
|
|
|
return Curl_resolver_is_resolved(conn, dns);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Curl_resolv_getsock(struct connectdata *conn,
|
|
|
|
curl_socket_t *socks,
|
|
|
|
int numsocks)
|
|
|
|
{
|
|
|
|
#ifdef CURLRES_ASYNCH
|
|
|
|
if(conn->data->set.doh)
|
|
|
|
/* nothing to wait for during DOH resolve, those handles have their own
|
|
|
|
sockets */
|
|
|
|
return GETSOCK_BLANK;
|
|
|
|
return Curl_resolver_getsock(conn, socks, numsocks);
|
|
|
|
#else
|
|
|
|
(void)conn;
|
|
|
|
(void)socks;
|
|
|
|
(void)numsocks;
|
|
|
|
return GETSOCK_BLANK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call this function after Curl_connect() has returned async=TRUE and
|
|
|
|
then a successful name resolve has been received.
|
|
|
|
|
|
|
|
Note: this function disconnects and frees the conn data in case of
|
|
|
|
resolve failure */
|
|
|
|
CURLcode Curl_once_resolved(struct connectdata *conn,
|
|
|
|
bool *protocol_done)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
if(conn->async.dns) {
|
|
|
|
conn->dns_entry = conn->async.dns;
|
|
|
|
conn->async.dns = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = Curl_setup_conn(conn, protocol_done);
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
/* We're not allowed to return failure with memory left allocated
|
|
|
|
in the connectdata struct, free those here */
|
|
|
|
Curl_disconnect(conn->data, conn, TRUE); /* close the connection */
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|