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
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) 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
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.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
|
|
|
*
|
2015-03-17 16:09:43 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +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
|
2017-12-06 00:41:27 +08:00
|
|
|
#ifdef HAVE_NETINET_IN6_H
|
|
|
|
#include <netinet/in6.h>
|
|
|
|
#endif
|
2000-08-23 15:23:42 +08:00
|
|
|
#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
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "hash.h"
|
2018-03-18 03:10:04 +08:00
|
|
|
#include "rand.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "share.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "inet_ntop.h"
|
2020-01-06 19:56:44 +08:00
|
|
|
#include "inet_pton.h"
|
2018-02-15 01:20:43 +08:00
|
|
|
#include "multiif.h"
|
2018-09-06 15:16:02 +08:00
|
|
|
#include "doh.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "warnless.h"
|
2021-05-31 14:33:44 +08:00
|
|
|
#include "strcase.h"
|
2023-05-19 01:54:09 +08:00
|
|
|
#include "easy_lock.h"
|
2016-04-29 21:46:40 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 19:36:18 +08:00
|
|
|
#include "curl_printf.h"
|
2009-04-21 19:46:16 +08:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2000-10-09 19:12:34 +08:00
|
|
|
|
2023-04-25 15:22:26 +08:00
|
|
|
#if defined(CURLRES_SYNCH) && \
|
|
|
|
defined(HAVE_ALARM) && \
|
|
|
|
defined(SIGALRM) && \
|
|
|
|
defined(HAVE_SIGSETJMP) && \
|
|
|
|
defined(GLOBAL_INIT_IS_THREADSAFE)
|
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
|
|
|
|
|
2019-02-10 07:10:18 +08:00
|
|
|
#define MAX_HOSTCACHE_LEN (255 + 7) /* max FQDN + colon + port number + zero */
|
|
|
|
|
2023-05-08 17:11:36 +08:00
|
|
|
#define MAX_DNS_CACHE_SIZE 29999
|
|
|
|
|
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
|
2014-12-27 19:09:01 +08:00
|
|
|
* 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
|
|
|
static void freednsentry(void *freethis);
|
|
|
|
|
2004-06-10 19:06:21 +08:00
|
|
|
/*
|
2017-03-26 23:02:22 +08:00
|
|
|
* Return # of addresses in a Curl_addrinfo struct
|
2004-06-10 19:06:21 +08:00
|
|
|
*/
|
2023-05-22 15:00:16 +08:00
|
|
|
static int num_addresses(const struct Curl_addrinfo *addr)
|
2004-06-10 19:06:21 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-05-19 00:41:20 +08:00
|
|
|
* Curl_printable_address() stores 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
|
|
|
*
|
2020-05-19 00:41:20 +08:00
|
|
|
* If the conversion fails, the target buffer is empty.
|
2004-06-10 19:06:21 +08:00
|
|
|
*/
|
2020-05-19 00:41:20 +08:00
|
|
|
void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf,
|
|
|
|
size_t bufsize)
|
2004-06-10 19:06:21 +08:00
|
|
|
{
|
2020-05-19 00:41:20 +08:00
|
|
|
DEBUGASSERT(bufsize);
|
|
|
|
buf[0] = 0;
|
2004-06-24 15:43:48 +08:00
|
|
|
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(ai->ai_family) {
|
2020-05-19 00:41:20 +08:00
|
|
|
case AF_INET: {
|
|
|
|
const struct sockaddr_in *sa4 = (const void *)ai->ai_addr;
|
|
|
|
const struct in_addr *ipaddr4 = &sa4->sin_addr;
|
|
|
|
(void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
|
|
|
|
break;
|
|
|
|
}
|
2008-10-19 09:02:18 +08:00
|
|
|
#ifdef ENABLE_IPV6
|
2020-05-19 00:41:20 +08:00
|
|
|
case AF_INET6: {
|
|
|
|
const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr;
|
|
|
|
const struct in6_addr *ipaddr6 = &sa6->sin6_addr;
|
|
|
|
(void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
|
|
|
|
break;
|
|
|
|
}
|
2008-10-19 09:02:18 +08:00
|
|
|
#endif
|
2020-05-19 00:41:20 +08:00
|
|
|
default:
|
|
|
|
break;
|
2008-10-19 09:02:18 +08:00
|
|
|
}
|
2004-06-10 19:06:21 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
2019-02-10 07:10:18 +08:00
|
|
|
* Create a hostcache id string for the provided host + port, to be used by
|
2023-02-24 16:20:51 +08:00
|
|
|
* the DNS caching. Without alloc. Return length of the id string.
|
2004-03-30 21:02:07 +08:00
|
|
|
*/
|
2023-02-24 16:20:51 +08:00
|
|
|
static size_t
|
|
|
|
create_hostcache_id(const char *name,
|
|
|
|
size_t nlen, /* 0 or actual name length */
|
|
|
|
int port, char *ptr, size_t buflen)
|
2002-01-17 14:55:37 +08:00
|
|
|
{
|
2023-02-24 16:20:51 +08:00
|
|
|
size_t len = nlen ? nlen : strlen(name);
|
|
|
|
size_t olen = 0;
|
|
|
|
DEBUGASSERT(buflen >= MAX_HOSTCACHE_LEN);
|
2019-02-10 07:10:18 +08:00
|
|
|
if(len > (buflen - 7))
|
|
|
|
len = buflen - 7;
|
|
|
|
/* store and lower case the name */
|
2023-02-24 16:20:51 +08:00
|
|
|
while(len--) {
|
2022-09-06 06:17:36 +08:00
|
|
|
*ptr++ = Curl_raw_tolower(*name++);
|
2023-02-24 16:20:51 +08:00
|
|
|
olen++;
|
|
|
|
}
|
|
|
|
olen += msnprintf(ptr, 7, ":%u", port);
|
|
|
|
return olen;
|
2002-01-17 14:55:37 +08:00
|
|
|
}
|
|
|
|
|
2002-04-27 21:07:51 +08:00
|
|
|
struct hostcache_prune_data {
|
2004-03-10 17:36:43 +08:00
|
|
|
time_t now;
|
2023-05-09 16:01:52 +08:00
|
|
|
time_t oldest; /* oldest time in cache not pruned. */
|
2023-05-08 16:40:47 +08:00
|
|
|
int cache_timeout;
|
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
|
|
|
{
|
2023-05-08 17:11:36 +08:00
|
|
|
struct hostcache_prune_data *prune =
|
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
|
|
|
|
2023-05-08 17:11:36 +08:00
|
|
|
if(c->timestamp) {
|
|
|
|
/* age in seconds */
|
|
|
|
time_t age = prune->now - c->timestamp;
|
|
|
|
if(age >= prune->cache_timeout)
|
|
|
|
return TRUE;
|
|
|
|
if(age > prune->oldest)
|
2023-05-09 16:01:52 +08:00
|
|
|
prune->oldest = age;
|
2023-05-08 17:11:36 +08:00
|
|
|
}
|
|
|
|
return FALSE;
|
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.
|
2023-05-08 17:11:36 +08:00
|
|
|
* Returns the 'age' of the oldest still kept entry.
|
2004-03-30 21:02:07 +08:00
|
|
|
*/
|
2023-05-09 16:01:52 +08:00
|
|
|
static time_t
|
|
|
|
hostcache_prune(struct Curl_hash *hostcache, int 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;
|
2023-05-08 17:11:36 +08:00
|
|
|
user.oldest = 0;
|
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);
|
2023-05-08 17:11:36 +08:00
|
|
|
|
|
|
|
return user.oldest;
|
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.
|
|
|
|
*/
|
2016-06-21 21:47:12 +08:00
|
|
|
void Curl_hostcache_prune(struct Curl_easy *data)
|
2003-12-16 01:33:49 +08:00
|
|
|
{
|
|
|
|
time_t now;
|
2023-05-08 17:11:36 +08:00
|
|
|
/* the timeout may be set -1 (forever) */
|
|
|
|
int timeout = data->set.dns_cache_timeout;
|
2004-02-18 15:56:18 +08:00
|
|
|
|
2023-05-08 17:11:36 +08:00
|
|
|
if(!data->dns.hostcache)
|
|
|
|
/* 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);
|
|
|
|
|
2023-05-08 17:11:36 +08:00
|
|
|
do {
|
|
|
|
/* Remove outdated and unused entries from the hostcache */
|
2023-05-09 16:01:52 +08:00
|
|
|
time_t oldest = hostcache_prune(data->dns.hostcache, timeout, now);
|
2023-05-08 17:11:36 +08:00
|
|
|
|
2023-05-09 16:01:52 +08:00
|
|
|
if(oldest < INT_MAX)
|
|
|
|
timeout = (int)oldest; /* we know it fits */
|
|
|
|
else
|
|
|
|
timeout = INT_MAX - 1;
|
2023-05-08 17:11:36 +08:00
|
|
|
|
|
|
|
/* if the cache size is still too big, use the oldest age as new
|
|
|
|
prune limit */
|
|
|
|
} while(timeout && (data->dns.hostcache->size > MAX_DNS_CACHE_SIZE));
|
2003-12-16 01:33:49 +08:00
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:22:26 +08:00
|
|
|
#ifdef USE_ALARM_TIMEOUT
|
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. */
|
2023-05-17 05:40:42 +08:00
|
|
|
static sigjmp_buf curl_jmpenv;
|
|
|
|
static curl_simple_lock curl_jmpenv_lock;
|
2003-08-05 22:40:59 +08:00
|
|
|
#endif
|
|
|
|
|
2015-03-17 16:26:36 +08:00
|
|
|
/* lookup address, returns entry if found and not stale */
|
2021-01-20 17:15:43 +08:00
|
|
|
static struct Curl_dns_entry *fetch_addr(struct Curl_easy *data,
|
|
|
|
const char *hostname,
|
|
|
|
int port)
|
2014-08-21 05:31:53 +08:00
|
|
|
{
|
|
|
|
struct Curl_dns_entry *dns = NULL;
|
2019-02-10 07:10:18 +08:00
|
|
|
char entry_id[MAX_HOSTCACHE_LEN];
|
2014-08-21 05:31:53 +08:00
|
|
|
|
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2023-02-24 16:20:51 +08:00
|
|
|
size_t entry_len = create_hostcache_id(hostname, 0, port,
|
|
|
|
entry_id, sizeof(entry_id));
|
2014-08-21 05:31:53 +08:00
|
|
|
|
|
|
|
/* See if its already in our dns cache */
|
2017-09-10 05:55:08 +08:00
|
|
|
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
|
2014-08-21 05:31:53 +08:00
|
|
|
|
2018-12-27 21:23:13 +08:00
|
|
|
/* No entry found in cache, check if we might have a wildcard entry */
|
2021-03-26 21:25:45 +08:00
|
|
|
if(!dns && data->state.wildcard_resolve) {
|
2023-02-24 16:20:51 +08:00
|
|
|
entry_len = create_hostcache_id("*", 1, port, entry_id, sizeof(entry_id));
|
2018-12-27 21:23:13 +08:00
|
|
|
|
|
|
|
/* See if it's already in our dns cache */
|
|
|
|
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
|
|
|
|
}
|
|
|
|
|
2017-09-12 15:29:01 +08:00
|
|
|
if(dns && (data->set.dns_cache_timeout != -1)) {
|
2015-03-17 16:26:36 +08: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;
|
2023-05-08 17:11:36 +08:00
|
|
|
user.oldest = 0;
|
2015-03-17 16:26:36 +08:00
|
|
|
|
|
|
|
if(hostcache_timestamp_remove(&user, dns)) {
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "Hostname in DNS cache was stale, zapped");
|
2015-03-17 16:26:36 +08:00
|
|
|
dns = NULL; /* the memory deallocation is being handled by the hash */
|
2017-09-10 05:55:08 +08:00
|
|
|
Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
|
2015-03-17 16:26:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 04:59:35 +08:00
|
|
|
/* See if the returned entry matches the required resolve mode */
|
|
|
|
if(dns && data->conn->ip_version != CURL_IPRESOLVE_WHATEVER) {
|
|
|
|
int pf = PF_INET;
|
|
|
|
bool found = false;
|
|
|
|
struct Curl_addrinfo *addr = dns->addr;
|
|
|
|
|
2022-10-19 02:27:59 +08:00
|
|
|
#ifdef PF_INET6
|
2022-09-20 04:59:35 +08:00
|
|
|
if(data->conn->ip_version == CURL_IPRESOLVE_V6)
|
|
|
|
pf = PF_INET6;
|
2022-10-19 02:27:59 +08:00
|
|
|
#endif
|
2022-09-20 04:59:35 +08:00
|
|
|
|
|
|
|
while(addr) {
|
|
|
|
if(addr->ai_family == pf) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
addr = addr->ai_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!found) {
|
|
|
|
infof(data, "Hostname in DNS cache doesn't have needed family, zapped");
|
|
|
|
dns = NULL; /* the memory deallocation is being handled by the hash */
|
|
|
|
Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 16:26:36 +08: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 *
|
2021-01-25 06:41:58 +08:00
|
|
|
Curl_fetch_addr(struct Curl_easy *data,
|
2015-03-17 16:26:36 +08:00
|
|
|
const char *hostname,
|
|
|
|
int port)
|
|
|
|
{
|
|
|
|
struct Curl_dns_entry *dns = NULL;
|
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
dns = fetch_addr(data, hostname, port);
|
2015-03-17 16:26:36 +08:00
|
|
|
|
2016-03-29 21:19:21 +08:00
|
|
|
if(dns)
|
|
|
|
dns->inuse++; /* we use it! */
|
2015-03-17 16:26:36 +08:00
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2014-08-21 05:31:53 +08:00
|
|
|
|
|
|
|
return dns;
|
|
|
|
}
|
2003-08-05 22:40:59 +08:00
|
|
|
|
2019-05-05 23:08:22 +08:00
|
|
|
#ifndef CURL_DISABLE_SHUFFLE_DNS
|
2019-02-08 16:33:42 +08:00
|
|
|
UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo **addr);
|
2018-03-18 03:10:04 +08: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 16:33:42 +08:00
|
|
|
UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo **addr)
|
2018-03-18 03:10:04 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2023-05-22 15:00:16 +08:00
|
|
|
const int num_addrs = num_addresses(*addr);
|
2018-03-18 03:10:04 +08:00
|
|
|
|
|
|
|
if(num_addrs > 1) {
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo **nodes;
|
2018-03-18 03:10:04 +08:00
|
|
|
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) {
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo *swap_tmp;
|
2018-03-18 03:10:04 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-05-05 23:08:22 +08:00
|
|
|
#endif
|
2018-03-18 03:10:04 +08:00
|
|
|
|
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 *
|
2016-06-21 21:47:12 +08:00
|
|
|
Curl_cache_addr(struct Curl_easy *data,
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo *addr,
|
2006-07-21 13:51:12 +08:00
|
|
|
const char *hostname,
|
2023-02-24 16:20:51 +08:00
|
|
|
size_t hostlen, /* length or zero */
|
2004-04-26 15:20:11 +08:00
|
|
|
int port)
|
2002-11-05 18:51:41 +08:00
|
|
|
{
|
2019-02-10 07:10:18 +08:00
|
|
|
char entry_id[MAX_HOSTCACHE_LEN];
|
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
|
|
|
|
2019-05-05 23:08:22 +08:00
|
|
|
#ifndef CURL_DISABLE_SHUFFLE_DNS
|
2018-03-18 03:10:04 +08:00
|
|
|
/* shuffle addresses if requested */
|
|
|
|
if(data->set.dns_shuffle_addresses) {
|
|
|
|
CURLcode result = Curl_shuffle_addr(data, &addr);
|
2018-10-08 00:18:03 +08:00
|
|
|
if(result)
|
2018-03-18 03:10:04 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-05 23:08:22 +08:00
|
|
|
#endif
|
2018-03-18 03:10:04 +08:00
|
|
|
|
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
|
|
|
return NULL;
|
2002-11-05 18:51:41 +08:00
|
|
|
}
|
|
|
|
|
2019-02-10 07:10:18 +08:00
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2023-02-24 16:20:51 +08:00
|
|
|
entry_len = create_hostcache_id(hostname, hostlen, port,
|
|
|
|
entry_id, sizeof(entry_id));
|
2019-02-10 07:10:18 +08:00
|
|
|
|
2015-03-17 16:09:43 +08:00
|
|
|
dns->inuse = 1; /* the cache has the first reference */
|
2003-09-15 05:17:54 +08:00
|
|
|
dns->addr = addr; /* this is the address(es) */
|
2009-11-11 17:31:37 +08:00
|
|
|
time(&dns->timestamp);
|
|
|
|
if(dns->timestamp == 0)
|
2020-12-11 00:38:14 +08:00
|
|
|
dns->timestamp = 1; /* zero indicates permanent CURLOPT_RESOLVE entry */
|
2003-09-15 05:17:54 +08:00
|
|
|
|
2009-11-11 17:31:37 +08:00
|
|
|
/* Store the resolved data in our DNS cache. */
|
2017-09-10 05:55:08 +08:00
|
|
|
dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len + 1,
|
2006-07-08 06:58:06 +08:00
|
|
|
(void *)dns);
|
2004-05-04 21:40:30 +08:00
|
|
|
if(!dns2) {
|
|
|
|
free(dns);
|
2003-09-15 05:17:54 +08:00
|
|
|
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 */
|
|
|
|
return dns;
|
2002-01-17 15:38:25 +08:00
|
|
|
}
|
2002-01-17 14:55:37 +08:00
|
|
|
|
2021-05-31 14:33:44 +08:00
|
|
|
#ifdef ENABLE_IPV6
|
2022-08-11 17:32:22 +08:00
|
|
|
/* return a static IPv6 ::1 for the name */
|
|
|
|
static struct Curl_addrinfo *get_localhost6(int port, const char *name)
|
2021-05-31 14:33:44 +08:00
|
|
|
{
|
|
|
|
struct Curl_addrinfo *ca;
|
|
|
|
const size_t ss_size = sizeof(struct sockaddr_in6);
|
2022-08-11 17:32:22 +08:00
|
|
|
const size_t hostlen = strlen(name);
|
2021-05-31 14:33:44 +08:00
|
|
|
struct sockaddr_in6 sa6;
|
|
|
|
unsigned char ipv6[16];
|
|
|
|
unsigned short port16 = (unsigned short)(port & 0xffff);
|
|
|
|
ca = calloc(sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1, 1);
|
|
|
|
if(!ca)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sa6.sin6_family = AF_INET6;
|
|
|
|
sa6.sin6_port = htons(port16);
|
|
|
|
sa6.sin6_flowinfo = 0;
|
|
|
|
sa6.sin6_scope_id = 0;
|
2021-06-01 16:16:19 +08:00
|
|
|
if(Curl_inet_pton(AF_INET6, "::1", ipv6) < 1)
|
|
|
|
return NULL;
|
2021-05-31 14:33:44 +08:00
|
|
|
memcpy(&sa6.sin6_addr, ipv6, sizeof(ipv6));
|
|
|
|
|
|
|
|
ca->ai_flags = 0;
|
|
|
|
ca->ai_family = AF_INET6;
|
|
|
|
ca->ai_socktype = SOCK_STREAM;
|
|
|
|
ca->ai_protocol = IPPROTO_TCP;
|
|
|
|
ca->ai_addrlen = (curl_socklen_t)ss_size;
|
|
|
|
ca->ai_next = NULL;
|
|
|
|
ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
|
|
|
|
memcpy(ca->ai_addr, &sa6, ss_size);
|
|
|
|
ca->ai_canonname = (char *)ca->ai_addr + ss_size;
|
2022-08-11 17:32:22 +08:00
|
|
|
strcpy(ca->ai_canonname, name);
|
2021-05-31 14:33:44 +08:00
|
|
|
return ca;
|
|
|
|
}
|
|
|
|
#else
|
2022-08-11 17:32:22 +08:00
|
|
|
#define get_localhost6(x,y) NULL
|
2021-05-31 14:33:44 +08:00
|
|
|
#endif
|
|
|
|
|
2022-08-11 17:32:22 +08:00
|
|
|
/* return a static IPv4 127.0.0.1 for the given name */
|
|
|
|
static struct Curl_addrinfo *get_localhost(int port, const char *name)
|
2021-05-31 14:33:44 +08:00
|
|
|
{
|
|
|
|
struct Curl_addrinfo *ca;
|
2023-07-19 15:17:15 +08:00
|
|
|
struct Curl_addrinfo *ca6;
|
2021-05-31 14:33:44 +08:00
|
|
|
const size_t ss_size = sizeof(struct sockaddr_in);
|
2022-08-11 17:32:22 +08:00
|
|
|
const size_t hostlen = strlen(name);
|
2021-05-31 14:33:44 +08:00
|
|
|
struct sockaddr_in sa;
|
|
|
|
unsigned int ipv4;
|
|
|
|
unsigned short port16 = (unsigned short)(port & 0xffff);
|
|
|
|
|
2021-06-01 16:16:19 +08:00
|
|
|
/* memset to clear the sa.sin_zero field */
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
2021-05-31 14:33:44 +08:00
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
sa.sin_port = htons(port16);
|
2021-06-01 16:16:19 +08:00
|
|
|
if(Curl_inet_pton(AF_INET, "127.0.0.1", (char *)&ipv4) < 1)
|
|
|
|
return NULL;
|
2021-05-31 14:33:44 +08:00
|
|
|
memcpy(&sa.sin_addr, &ipv4, sizeof(ipv4));
|
|
|
|
|
2021-09-30 03:15:05 +08:00
|
|
|
ca = calloc(sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1, 1);
|
|
|
|
if(!ca)
|
|
|
|
return NULL;
|
2021-05-31 14:33:44 +08:00
|
|
|
ca->ai_flags = 0;
|
|
|
|
ca->ai_family = AF_INET;
|
|
|
|
ca->ai_socktype = SOCK_STREAM;
|
|
|
|
ca->ai_protocol = IPPROTO_TCP;
|
|
|
|
ca->ai_addrlen = (curl_socklen_t)ss_size;
|
|
|
|
ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
|
|
|
|
memcpy(ca->ai_addr, &sa, ss_size);
|
|
|
|
ca->ai_canonname = (char *)ca->ai_addr + ss_size;
|
2022-08-11 17:32:22 +08:00
|
|
|
strcpy(ca->ai_canonname, name);
|
2023-07-19 15:17:15 +08:00
|
|
|
|
|
|
|
ca6 = get_localhost6(port, name);
|
|
|
|
if(!ca6)
|
|
|
|
return ca;
|
|
|
|
ca6->ai_next = ca;
|
|
|
|
return ca6;
|
2021-05-31 14:33:44 +08:00
|
|
|
}
|
|
|
|
|
2021-08-03 15:03:00 +08:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
/*
|
|
|
|
* Curl_ipv6works() returns TRUE if IPv6 seems to work.
|
|
|
|
*/
|
|
|
|
bool Curl_ipv6works(struct Curl_easy *data)
|
|
|
|
{
|
|
|
|
if(data) {
|
|
|
|
/* the nature of most system is that IPv6 status doesn't come and go
|
|
|
|
during a program's lifetime so we only probe the first time and then we
|
|
|
|
have the info kept for fast re-use */
|
|
|
|
DEBUGASSERT(data);
|
|
|
|
DEBUGASSERT(data->multi);
|
2022-09-21 14:52:57 +08:00
|
|
|
if(data->multi->ipv6_up == IPV6_UNKNOWN) {
|
|
|
|
bool works = Curl_ipv6works(NULL);
|
|
|
|
data->multi->ipv6_up = works ? IPV6_WORKS : IPV6_DEAD;
|
|
|
|
}
|
|
|
|
return data->multi->ipv6_up == IPV6_WORKS;
|
2021-08-03 15:03:00 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int ipv6_works = -1;
|
|
|
|
/* probe to see if we have a working IPv6 stack */
|
|
|
|
curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
|
|
|
|
if(s == CURL_SOCKET_BAD)
|
|
|
|
/* an IPv6 address was requested but we can't get/use one */
|
|
|
|
ipv6_works = 0;
|
|
|
|
else {
|
|
|
|
ipv6_works = 1;
|
|
|
|
sclose(s);
|
|
|
|
}
|
|
|
|
return (ipv6_works>0)?TRUE:FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_IPV6 */
|
|
|
|
|
2021-05-30 05:57:58 +08:00
|
|
|
/*
|
|
|
|
* Curl_host_is_ipnum() returns TRUE if the given string is a numerical IPv4
|
|
|
|
* (or IPv6 if supported) address.
|
|
|
|
*/
|
|
|
|
bool Curl_host_is_ipnum(const char *hostname)
|
|
|
|
{
|
|
|
|
struct in_addr in;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct in6_addr in6;
|
|
|
|
#endif
|
|
|
|
if(Curl_inet_pton(AF_INET, hostname, &in) > 0
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
|| Curl_inet_pton(AF_INET6, hostname, &in6) > 0
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-08-11 17:32:22 +08:00
|
|
|
|
2022-08-31 22:31:01 +08:00
|
|
|
/* return TRUE if 'part' is a case insensitive tail of 'full' */
|
2022-08-11 17:32:22 +08:00
|
|
|
static bool tailmatch(const char *full, const char *part)
|
|
|
|
{
|
|
|
|
size_t plen = strlen(part);
|
|
|
|
size_t flen = strlen(full);
|
|
|
|
if(plen > flen)
|
|
|
|
return FALSE;
|
|
|
|
return strncasecompare(part, &full[flen - plen], plen);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
enum resolve_t Curl_resolv(struct Curl_easy *data,
|
2020-02-14 23:16:54 +08:00
|
|
|
const char *hostname,
|
|
|
|
int port,
|
|
|
|
bool allowDOH,
|
|
|
|
struct Curl_dns_entry **entry)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
2002-11-05 18:51:41 +08:00
|
|
|
struct Curl_dns_entry *dns = NULL;
|
2003-11-14 19:56:15 +08:00
|
|
|
CURLcode result;
|
2020-02-14 23:16:54 +08:00
|
|
|
enum resolve_t rc = CURLRESOLV_ERROR; /* default to failure */
|
2021-01-20 17:15:43 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2023-03-07 23:16:45 +08:00
|
|
|
/* We should intentionally error and not resolve .onion TLDs */
|
|
|
|
size_t hostname_len = strlen(hostname);
|
|
|
|
if(hostname_len >= 7 &&
|
|
|
|
(curl_strequal(&hostname[hostname_len - 6], ".onion") ||
|
|
|
|
curl_strequal(&hostname[hostname_len - 7], ".onion."))) {
|
|
|
|
failf(data, "Not resolving .onion address (RFC 7686)");
|
|
|
|
return CURLRESOLV_ERROR;
|
|
|
|
}
|
2008-09-30 05:02:22 +08:00
|
|
|
*entry = NULL;
|
2021-09-06 22:36:34 +08:00
|
|
|
#ifndef CURL_DISABLE_DOH
|
2020-05-26 17:07:06 +08:00
|
|
|
conn->bits.doh = FALSE; /* default is not */
|
2021-09-06 22:36:34 +08:00
|
|
|
#else
|
|
|
|
(void)allowDOH;
|
|
|
|
#endif
|
2002-06-11 23:10:18 +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);
|
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
dns = fetch_addr(data, hostname, port);
|
2008-03-12 06:55:23 +08:00
|
|
|
|
|
|
|
if(dns) {
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "Hostname %s was found in DNS cache", hostname);
|
2008-03-12 06:55:23 +08:00
|
|
|
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
|
|
|
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo *addr = NULL;
|
2019-02-13 01:43:55 +08:00
|
|
|
int respwait = 0;
|
2021-09-06 22:36:34 +08:00
|
|
|
#if !defined(CURL_DISABLE_DOH) || !defined(USE_RESOLVE_ON_IPS)
|
2020-01-06 19:56:44 +08:00
|
|
|
struct in_addr in;
|
2021-09-06 22:36:34 +08:00
|
|
|
#endif
|
|
|
|
#ifndef CURL_DISABLE_DOH
|
2020-05-26 17:07:06 +08:00
|
|
|
#ifndef USE_RESOLVE_ON_IPS
|
|
|
|
const
|
2020-01-06 19:56:44 +08:00
|
|
|
#endif
|
2020-05-26 17:07:06 +08:00
|
|
|
bool ipnum = FALSE;
|
2021-09-06 22:36:34 +08:00
|
|
|
#endif
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2018-02-15 01:20:43 +08:00
|
|
|
/* notify the resolver start callback */
|
|
|
|
if(data->set.resolver_start) {
|
|
|
|
int st;
|
|
|
|
Curl_set_in_callback(data, true);
|
2021-02-05 06:40:54 +08:00
|
|
|
st = data->set.resolver_start(
|
2021-02-13 15:45:33 +08:00
|
|
|
#ifdef USE_CURL_ASYNC
|
2021-02-05 06:40:54 +08:00
|
|
|
data->state.async.resolver,
|
2021-02-13 15:45:33 +08:00
|
|
|
#else
|
|
|
|
NULL,
|
2021-02-05 06:40:54 +08:00
|
|
|
#endif
|
|
|
|
NULL,
|
|
|
|
data->set.resolver_start_client);
|
2018-02-15 01:20:43 +08:00
|
|
|
Curl_set_in_callback(data, false);
|
|
|
|
if(st)
|
|
|
|
return CURLRESOLV_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-06-02 16:45:35 +08:00
|
|
|
#ifndef USE_RESOLVE_ON_IPS
|
2020-01-06 19:56:44 +08:00
|
|
|
/* First check if this is an IPv4 address string */
|
|
|
|
if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
|
|
|
|
/* This is a dotted IP address 123.123.123.123-style */
|
|
|
|
addr = Curl_ip2addr(AF_INET, &in, hostname, port);
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!addr) {
|
|
|
|
struct in6_addr in6;
|
|
|
|
/* check if this is an IPv6 address string */
|
|
|
|
if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
|
|
|
|
/* This is an IPv6 address literal */
|
|
|
|
addr = Curl_ip2addr(AF_INET6, &in6, hostname, port);
|
2018-09-06 15:16:02 +08:00
|
|
|
}
|
2020-01-06 19:56:44 +08:00
|
|
|
#endif /* ENABLE_IPV6 */
|
2020-05-26 17:07:06 +08:00
|
|
|
|
2020-06-02 16:45:35 +08:00
|
|
|
#else /* if USE_RESOLVE_ON_IPS */
|
2021-09-06 22:36:34 +08:00
|
|
|
#ifndef CURL_DISABLE_DOH
|
2020-06-02 16:45:35 +08:00
|
|
|
/* First check if this is an IPv4 address string */
|
|
|
|
if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
|
|
|
|
/* This is a dotted IP address 123.123.123.123-style */
|
2020-05-26 17:07:06 +08:00
|
|
|
ipnum = TRUE;
|
2020-06-02 16:45:35 +08:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else {
|
|
|
|
struct in6_addr in6;
|
|
|
|
/* check if this is an IPv6 address string */
|
|
|
|
if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
|
|
|
|
/* This is an IPv6 address literal */
|
|
|
|
ipnum = TRUE;
|
2020-05-26 17:07:06 +08:00
|
|
|
}
|
2020-06-02 16:45:35 +08:00
|
|
|
#endif /* ENABLE_IPV6 */
|
2021-09-06 22:36:34 +08:00
|
|
|
#endif /* CURL_DISABLE_DOH */
|
2020-06-02 16:45:35 +08:00
|
|
|
|
|
|
|
#endif /* !USE_RESOLVE_ON_IPS */
|
2020-01-06 19:56:44 +08:00
|
|
|
|
|
|
|
if(!addr) {
|
2021-08-03 15:03:00 +08:00
|
|
|
if(conn->ip_version == CURL_IPRESOLVE_V6 && !Curl_ipv6works(data))
|
2020-01-06 19:56:44 +08:00
|
|
|
return CURLRESOLV_ERROR;
|
|
|
|
|
2022-08-11 17:32:22 +08:00
|
|
|
if(strcasecompare(hostname, "localhost") ||
|
|
|
|
tailmatch(hostname, ".localhost"))
|
|
|
|
addr = get_localhost(port, hostname);
|
2021-09-06 22:36:34 +08:00
|
|
|
#ifndef CURL_DISABLE_DOH
|
2021-05-31 14:33:44 +08:00
|
|
|
else if(allowDOH && data->set.doh && !ipnum)
|
2021-01-20 17:15:43 +08:00
|
|
|
addr = Curl_doh(data, hostname, port, &respwait);
|
2021-09-06 22:36:34 +08:00
|
|
|
#endif
|
2020-01-06 19:56:44 +08:00
|
|
|
else {
|
2021-08-03 15:03:00 +08:00
|
|
|
/* Check what IP specifics the app has requested and if we can provide
|
|
|
|
* it. If not, bail out. */
|
|
|
|
if(!Curl_ipvalid(data, conn))
|
|
|
|
return CURLRESOLV_ERROR;
|
2020-01-06 19:56:44 +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 */
|
2021-05-11 15:49:31 +08:00
|
|
|
addr = Curl_getaddrinfo(data, hostname, port, &respwait);
|
2020-01-06 19:56:44 +08:00
|
|
|
}
|
2018-09-06 15:16: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 */
|
2021-01-20 17:15:43 +08:00
|
|
|
result = Curl_resolv_check(data, &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 */
|
2023-02-24 16:20:51 +08:00
|
|
|
dns = Curl_cache_addr(data, addr, hostname, 0, 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
|
2021-04-07 17:37:15 +08:00
|
|
|
void alarmfunc(int sig)
|
2008-09-30 05:02:22 +08:00
|
|
|
{
|
|
|
|
(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
|
|
|
|
*/
|
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
enum resolve_t Curl_resolv_timeout(struct Curl_easy *data,
|
2020-02-14 23:16:54 +08:00
|
|
|
const char *hostname,
|
|
|
|
int port,
|
|
|
|
struct Curl_dns_entry **entry,
|
|
|
|
timediff_t 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 */
|
2017-03-26 23:02:22 +08:00
|
|
|
volatile bool keep_copysig = FALSE; /* whether 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 05:02:22 +08:00
|
|
|
#endif /* USE_ALARM_TIMEOUT */
|
2020-02-14 23:16:54 +08:00
|
|
|
enum resolve_t 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
|
2017-05-21 01:39:51 +08:00
|
|
|
timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)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 */
|
2021-01-20 17:15:43 +08:00
|
|
|
return Curl_resolv(data, hostname, port, TRUE, entry);
|
2010-04-05 05:35:21 +08:00
|
|
|
|
2016-10-10 19:08:24 +08:00
|
|
|
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. */
|
2016-10-10 19:08:24 +08:00
|
|
|
failf(data,
|
|
|
|
"remaining timeout of %ld too small to resolve via SIGALRM method",
|
|
|
|
timeout);
|
2008-09-30 05:02:22 +08:00
|
|
|
return CURLRESOLV_TIMEDOUT;
|
2016-10-10 19:08:24 +08:00
|
|
|
}
|
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"). */
|
2023-04-25 15:22:26 +08:00
|
|
|
curl_simple_lock_lock(&curl_jmpenv_lock);
|
|
|
|
|
2010-04-05 05:35:21 +08:00
|
|
|
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
|
|
|
}
|
2015-03-14 21:32:42 +08: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
|
2020-12-31 17:11:49 +08:00
|
|
|
/* HPUX doesn't have SA_RESTART but defaults to that behavior! */
|
2015-03-14 21:32:42 +08:00
|
|
|
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 05:02:22 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
#ifndef CURLRES_ASYNCH
|
2008-10-03 10:25:52 +08:00
|
|
|
if(timeoutms)
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "timeout on name lookup is not supported");
|
2008-09-30 07:45:43 +08:00
|
|
|
#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.
|
|
|
|
*/
|
2021-01-20 17:15:43 +08:00
|
|
|
rc = Curl_resolv(data, hostname, port, TRUE, entry);
|
2008-09-30 05:02:22 +08:00
|
|
|
|
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 */
|
|
|
|
|
2023-04-25 15:22:26 +08:00
|
|
|
curl_simple_lock_unlock(&curl_jmpenv_lock);
|
|
|
|
|
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 */
|
2017-10-25 17:59:43 +08:00
|
|
|
timediff_t elapsed_secs = Curl_timediff(Curl_now(),
|
2021-01-20 17:15:43 +08:00
|
|
|
data->conn->created) / 1000;
|
2010-04-05 05:35:21 +08:00
|
|
|
|
|
|
|
/* the alarm period is counted in even number of seconds */
|
2019-10-01 06:24:50 +08:00
|
|
|
unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs);
|
2010-04-05 05:35:21 +08: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;
|
2022-04-16 17:55:05 +08:00
|
|
|
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.
|
2013-08-08 19:01:16 +08:00
|
|
|
*
|
|
|
|
* May be called with 'data' == NULL for global cache.
|
2004-03-30 21:02:07 +08:00
|
|
|
*/
|
2016-06-21 21:47:12 +08:00
|
|
|
void Curl_resolv_unlock(struct Curl_easy *data, struct Curl_dns_entry *dns)
|
2003-02-05 07:48:46 +08:00
|
|
|
{
|
2013-08-08 19:01:16 +08:00
|
|
|
if(data && data->share)
|
2003-02-05 07:48:46 +08:00
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
2003-07-23 20:55:24 +08:00
|
|
|
|
2015-03-17 16:09:43 +08:00
|
|
|
freednsentry(dns);
|
2003-07-23 20:55:24 +08:00
|
|
|
|
2013-08-08 19:01:16 +08:00
|
|
|
if(data && data->share)
|
2003-02-05 07:48:46 +08:00
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
}
|
|
|
|
|
2002-11-26 17:41:54 +08:00
|
|
|
/*
|
2015-03-17 16:09:43 +08:00
|
|
|
* File-internal: release cache dns entry reference, free if inuse drops to 0
|
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
|
|
|
{
|
2015-03-17 16:09:43 +08:00
|
|
|
struct Curl_dns_entry *dns = (struct Curl_dns_entry *) freethis;
|
|
|
|
DEBUGASSERT(dns && (dns->inuse>0));
|
2002-11-26 17:41:54 +08:00
|
|
|
|
2015-03-17 16:09:43 +08:00
|
|
|
dns->inuse--;
|
|
|
|
if(dns->inuse == 0) {
|
|
|
|
Curl_freeaddrinfo(dns->addr);
|
|
|
|
free(dns);
|
2008-10-30 21:45:25 +08:00
|
|
|
}
|
2001-10-04 21:36:11 +08:00
|
|
|
}
|
|
|
|
|
2004-03-30 21:02:07 +08:00
|
|
|
/*
|
2021-12-10 19:54:17 +08:00
|
|
|
* Curl_init_dnscache() inits a new DNS cache.
|
2004-03-30 21:02:07 +08:00
|
|
|
*/
|
2022-08-27 20:48:13 +08:00
|
|
|
void Curl_init_dnscache(struct Curl_hash *hash, int size)
|
2004-03-30 21:02:07 +08:00
|
|
|
{
|
2022-08-27 20:48:13 +08:00
|
|
|
Curl_hash_init(hash, size, Curl_hash_str, Curl_str_key_compare,
|
2021-12-10 19:54:17 +08:00
|
|
|
freednsentry);
|
2004-03-30 21:02:07 +08:00
|
|
|
}
|
|
|
|
|
2013-08-08 19:01:16 +08:00
|
|
|
/*
|
|
|
|
* Curl_hostcache_clean()
|
|
|
|
*
|
|
|
|
* This _can_ be called with 'data' == NULL but then of course no locking
|
|
|
|
* can be done!
|
|
|
|
*/
|
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
void Curl_hostcache_clean(struct Curl_easy *data,
|
2020-09-02 18:07:44 +08:00
|
|
|
struct Curl_hash *hash)
|
2011-12-23 21:30:43 +08:00
|
|
|
{
|
2015-03-17 16:09:43 +08:00
|
|
|
if(data && data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2015-04-04 05:41:58 +08:00
|
|
|
Curl_hash_clean(hash);
|
2015-03-17 16:09:43 +08:00
|
|
|
|
|
|
|
if(data && data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2012-11-18 23:17:37 +08:00
|
|
|
}
|
2011-12-23 21:30:43 +08:00
|
|
|
|
2011-12-31 17:28:10 +08:00
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
CURLcode Curl_loadhostpairs(struct Curl_easy *data)
|
2011-12-31 17:28:10 +08:00
|
|
|
{
|
|
|
|
struct curl_slist *hostp;
|
2023-02-24 16:20:51 +08:00
|
|
|
char *host_end;
|
2011-12-31 17:28:10 +08:00
|
|
|
|
2018-12-27 21:23:13 +08:00
|
|
|
/* Default is no wildcard found */
|
2021-03-26 21:25:45 +08:00
|
|
|
data->state.wildcard_resolve = false;
|
2018-12-27 21:23:13 +08:00
|
|
|
|
2021-03-26 21:25:45 +08:00
|
|
|
for(hostp = data->state.resolve; hostp; hostp = hostp->next) {
|
2019-02-10 07:10:18 +08:00
|
|
|
char entry_id[MAX_HOSTCACHE_LEN];
|
2011-12-31 17:28:10 +08:00
|
|
|
if(!hostp->data)
|
|
|
|
continue;
|
|
|
|
if(hostp->data[0] == '-') {
|
2023-02-24 16:20:51 +08:00
|
|
|
unsigned long num = 0;
|
2015-03-17 16:15:25 +08:00
|
|
|
size_t entry_len;
|
2023-02-24 16:20:51 +08:00
|
|
|
size_t hlen = 0;
|
|
|
|
host_end = strchr(&hostp->data[1], ':');
|
|
|
|
|
|
|
|
if(host_end) {
|
|
|
|
hlen = host_end - &hostp->data[1];
|
|
|
|
num = strtoul(++host_end, NULL, 10);
|
|
|
|
if(!hlen || (num > 0xffff))
|
|
|
|
host_end = NULL;
|
|
|
|
}
|
|
|
|
if(!host_end) {
|
|
|
|
infof(data, "Bad syntax CURLOPT_RESOLVE removal entry '%s'",
|
2015-03-17 16:15:25 +08:00
|
|
|
hostp->data);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2023-02-24 16:20:51 +08:00
|
|
|
entry_len = create_hostcache_id(&hostp->data[1], hlen, (int)num,
|
|
|
|
entry_id, sizeof(entry_id));
|
2015-03-17 16:15:25 +08:00
|
|
|
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 05:55:08 +08:00
|
|
|
Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
|
2015-03-17 16:15:25 +08:00
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
2011-12-31 17:28:10 +08:00
|
|
|
}
|
2015-03-17 16:15:25 +08:00
|
|
|
else {
|
2011-12-31 17:28:10 +08:00
|
|
|
struct Curl_dns_entry *dns;
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo *head = NULL, *tail = NULL;
|
2011-12-31 17:28:10 +08:00
|
|
|
size_t entry_len;
|
2018-01-31 08:33:51 +08:00
|
|
|
char address[64];
|
2018-08-02 14:24:27 +08:00
|
|
|
#if !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
2018-03-13 06:52:28 +08:00
|
|
|
char *addresses = NULL;
|
2018-08-02 14:24:27 +08:00
|
|
|
#endif
|
2018-01-31 08:33:51 +08:00
|
|
|
char *addr_begin;
|
|
|
|
char *addr_end;
|
|
|
|
char *port_ptr;
|
2023-02-24 16:20:51 +08:00
|
|
|
int port = 0;
|
2018-01-31 08:33:51 +08:00
|
|
|
char *end_ptr;
|
2020-12-11 00:38:14 +08:00
|
|
|
bool permanent = TRUE;
|
2018-01-31 08:33:51 +08:00
|
|
|
unsigned long tmp_port;
|
|
|
|
bool error = true;
|
2023-02-24 16:20:51 +08:00
|
|
|
char *host_begin = hostp->data;
|
|
|
|
size_t hlen = 0;
|
2018-01-31 08:33:51 +08:00
|
|
|
|
2020-12-11 00:38:14 +08:00
|
|
|
if(host_begin[0] == '+') {
|
|
|
|
host_begin++;
|
|
|
|
permanent = FALSE;
|
|
|
|
}
|
|
|
|
host_end = strchr(host_begin, ':');
|
2023-02-24 16:20:51 +08:00
|
|
|
if(!host_end)
|
2018-01-31 08:33:51 +08:00
|
|
|
goto err;
|
2023-02-24 16:20:51 +08:00
|
|
|
hlen = host_end - host_begin;
|
2018-01-31 08:33:51 +08:00
|
|
|
|
|
|
|
port_ptr = host_end + 1;
|
|
|
|
tmp_port = strtoul(port_ptr, &end_ptr, 10);
|
2018-02-21 03:29:50 +08:00
|
|
|
if(tmp_port > USHRT_MAX || end_ptr == port_ptr || *end_ptr != ':')
|
2018-01-31 08:33:51 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
port = (int)tmp_port;
|
2018-08-02 14:24:27 +08:00
|
|
|
#if !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
2018-01-31 08:33:51 +08:00
|
|
|
addresses = end_ptr + 1;
|
2018-08-02 14:24:27 +08:00
|
|
|
#endif
|
2018-01-31 08:33:51 +08:00
|
|
|
|
|
|
|
while(*end_ptr) {
|
|
|
|
size_t alen;
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo *ai;
|
2018-01-31 08:33:51 +08:00
|
|
|
|
|
|
|
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 17:28:10 +08:00
|
|
|
|
2018-01-31 08:33:51 +08:00
|
|
|
alen = addr_end - addr_begin;
|
|
|
|
if(!alen)
|
|
|
|
continue;
|
2015-03-17 16:15:25 +08:00
|
|
|
|
2018-01-31 08:33:51 +08:00
|
|
|
if(alen >= sizeof(address))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
memcpy(address, addr_begin, alen);
|
|
|
|
address[alen] = '\0';
|
|
|
|
|
|
|
|
#ifndef ENABLE_IPV6
|
|
|
|
if(strchr(address, ':')) {
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "Ignoring resolve address '%s', missing IPv6 support.",
|
2018-01-31 08:33:51 +08:00
|
|
|
address);
|
2017-11-17 18:21:12 +08:00
|
|
|
continue;
|
2018-01-31 08:33:51 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ai = Curl_str2addr(address, port);
|
|
|
|
if(!ai) {
|
2022-04-16 17:55:05 +08:00
|
|
|
infof(data, "Resolve address '%s' found illegal", address);
|
2018-01-31 08:33:51 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tail) {
|
|
|
|
tail->ai_next = ai;
|
|
|
|
tail = tail->ai_next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
head = tail = ai;
|
|
|
|
}
|
2017-11-17 18:21:12 +08:00
|
|
|
}
|
|
|
|
|
2018-01-31 08:33:51 +08:00
|
|
|
if(!head)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
error = false;
|
2023-05-18 12:54:18 +08:00
|
|
|
err:
|
2018-01-31 08:33:51 +08:00
|
|
|
if(error) {
|
2022-04-16 17:55:05 +08:00
|
|
|
failf(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'",
|
2018-01-31 08:33:51 +08:00
|
|
|
hostp->data);
|
|
|
|
Curl_freeaddrinfo(head);
|
2021-06-02 20:12:10 +08:00
|
|
|
return CURLE_SETOPT_OPTION_SYNTAX;
|
2011-12-31 17:28:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an entry id, based upon the hostname and port */
|
2023-02-24 16:20:51 +08:00
|
|
|
entry_len = create_hostcache_id(host_begin, hlen, port,
|
|
|
|
entry_id, sizeof(entry_id));
|
2011-12-31 17:28:10 +08:00
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
2020-12-11 00:38:14 +08:00
|
|
|
/* See if it's already in our dns cache */
|
2017-09-10 05:55:08 +08:00
|
|
|
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
|
2011-12-31 17:28:10 +08:00
|
|
|
|
2018-05-30 22:23:35 +08:00
|
|
|
if(dns) {
|
2023-02-24 16:20:51 +08:00
|
|
|
infof(data, "RESOLVE %.*s:%d is - old addresses discarded",
|
|
|
|
(int)hlen, host_begin, port);
|
2020-12-11 00:38:14 +08:00
|
|
|
/* delete old entry, there are two reasons for this
|
2018-05-30 22:23:35 +08:00
|
|
|
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
|
2020-12-11 00:38:14 +08:00
|
|
|
entry is not necessarily marked as permanent.
|
|
|
|
3. when adding a non-permanent entry, we want it to remove and
|
|
|
|
replace an existing permanent entry.
|
|
|
|
4. when adding a non-permanent entry, we want it to get a "fresh"
|
|
|
|
timeout that starts _now_. */
|
2018-05-30 22:23:35 +08:00
|
|
|
|
|
|
|
Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
|
|
|
|
}
|
2011-12-31 17:28:10 +08:00
|
|
|
|
2018-05-30 22:23:35 +08:00
|
|
|
/* put this new host in the cache */
|
2023-02-24 16:20:51 +08:00
|
|
|
dns = Curl_cache_addr(data, head, host_begin, hlen, port);
|
2018-05-30 22:23:35 +08:00
|
|
|
if(dns) {
|
2020-12-11 00:38:14 +08:00
|
|
|
if(permanent)
|
|
|
|
dns->timestamp = 0; /* mark as permanent */
|
2018-05-30 22:23:35 +08:00
|
|
|
/* release the returned reference; the cache itself will keep the
|
|
|
|
* entry alive: */
|
2020-12-11 00:38:14 +08:00
|
|
|
dns->inuse--;
|
2017-12-10 07:41:57 +08:00
|
|
|
}
|
2011-12-31 17:28:10 +08:00
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
|
|
|
|
if(!dns) {
|
2018-01-31 08:33:51 +08:00
|
|
|
Curl_freeaddrinfo(head);
|
2011-12-31 17:28:10 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2023-02-24 16:20:51 +08:00
|
|
|
infof(data, "Added %.*s:%d:%s to DNS cache%s",
|
|
|
|
(int)hlen, host_begin, port, addresses,
|
|
|
|
permanent ? "" : " (non-permanent)");
|
2018-12-27 21:23:13 +08:00
|
|
|
|
|
|
|
/* Wildcard hostname */
|
2023-02-24 16:20:51 +08:00
|
|
|
if((hlen == 1) && (host_begin[0] == '*')) {
|
|
|
|
infof(data, "RESOLVE *:%d using wildcard", port);
|
2021-03-26 21:25:45 +08:00
|
|
|
data->state.wildcard_resolve = true;
|
2018-12-27 21:23:13 +08:00
|
|
|
}
|
2011-12-31 17:28:10 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 21:25:45 +08:00
|
|
|
data->state.resolve = NULL; /* dealt with now */
|
2011-12-31 17:28:10 +08:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2018-09-06 15:16:02 +08:00
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
CURLcode Curl_resolv_check(struct Curl_easy *data,
|
2018-09-06 15:16:02 +08:00
|
|
|
struct Curl_dns_entry **dns)
|
|
|
|
{
|
2019-12-09 20:48:24 +08:00
|
|
|
#if defined(CURL_DISABLE_DOH) && !defined(CURLRES_ASYNCH)
|
2022-02-24 19:06:11 +08:00
|
|
|
(void)data;
|
2019-12-09 20:48:24 +08:00
|
|
|
(void)dns;
|
|
|
|
#endif
|
2021-09-06 22:36:34 +08:00
|
|
|
#ifndef CURL_DISABLE_DOH
|
2021-01-20 17:15:43 +08:00
|
|
|
if(data->conn->bits.doh)
|
|
|
|
return Curl_doh_is_resolved(data, dns);
|
2021-09-06 22:36:34 +08:00
|
|
|
#endif
|
2021-01-20 17:15:43 +08:00
|
|
|
return Curl_resolver_is_resolved(data, dns);
|
2018-09-06 15:16:02 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
int Curl_resolv_getsock(struct Curl_easy *data,
|
2019-07-30 17:02:03 +08:00
|
|
|
curl_socket_t *socks)
|
2018-09-06 15:16:02 +08:00
|
|
|
{
|
|
|
|
#ifdef CURLRES_ASYNCH
|
2021-09-06 22:36:34 +08:00
|
|
|
#ifndef CURL_DISABLE_DOH
|
2021-01-20 17:15:43 +08:00
|
|
|
if(data->conn->bits.doh)
|
2021-07-16 15:17:03 +08:00
|
|
|
/* nothing to wait for during DoH resolve, those handles have their own
|
2018-09-06 15:16:02 +08:00
|
|
|
sockets */
|
|
|
|
return GETSOCK_BLANK;
|
2021-09-06 22:36:34 +08:00
|
|
|
#endif
|
2021-01-20 17:15:43 +08:00
|
|
|
return Curl_resolver_getsock(data, socks);
|
2018-09-06 15:16:02 +08:00
|
|
|
#else
|
2021-01-20 17:15:43 +08:00
|
|
|
(void)data;
|
2018-09-06 15:16:02 +08:00
|
|
|
(void)socks;
|
|
|
|
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 */
|
2021-01-20 17:15:43 +08:00
|
|
|
CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_done)
|
2018-09-06 15:16:02 +08:00
|
|
|
{
|
|
|
|
CURLcode result;
|
2021-01-20 17:15:43 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2018-09-06 15:16:02 +08:00
|
|
|
|
2021-02-13 15:45:33 +08:00
|
|
|
#ifdef USE_CURL_ASYNC
|
2021-01-20 17:15:43 +08:00
|
|
|
if(data->state.async.dns) {
|
|
|
|
conn->dns_entry = data->state.async.dns;
|
|
|
|
data->state.async.dns = NULL;
|
2018-09-06 15:16:02 +08:00
|
|
|
}
|
2021-02-05 06:40:54 +08:00
|
|
|
#endif
|
2018-09-06 15:16:02 +08:00
|
|
|
|
2021-01-25 06:32:24 +08:00
|
|
|
result = Curl_setup_conn(data, protocol_done);
|
2018-09-06 15:16:02 +08:00
|
|
|
|
2020-04-27 06:33:21 +08:00
|
|
|
if(result) {
|
2022-04-27 12:04:33 +08:00
|
|
|
Curl_detach_connection(data);
|
2020-04-27 06:33:21 +08:00
|
|
|
Curl_conncache_remove_conn(data, conn, TRUE);
|
|
|
|
Curl_disconnect(data, conn, TRUE);
|
|
|
|
}
|
2018-09-06 15:16:02 +08:00
|
|
|
return result;
|
|
|
|
}
|
2021-02-18 20:14:55 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_resolver_error() calls failf() with the appropriate message after a
|
|
|
|
* resolve error
|
|
|
|
*/
|
|
|
|
|
2021-04-02 03:15:01 +08:00
|
|
|
#ifdef USE_CURL_ASYNC
|
2021-02-18 20:14:55 +08:00
|
|
|
CURLcode Curl_resolver_error(struct Curl_easy *data)
|
|
|
|
{
|
|
|
|
const char *host_or_proxy;
|
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
#ifndef CURL_DISABLE_PROXY
|
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
if(conn->bits.httpproxy) {
|
|
|
|
host_or_proxy = "proxy";
|
|
|
|
result = CURLE_COULDNT_RESOLVE_PROXY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
host_or_proxy = "host";
|
|
|
|
result = CURLE_COULDNT_RESOLVE_HOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
failf(data, "Could not resolve %s: %s", host_or_proxy,
|
|
|
|
data->state.async.hostname);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-04-02 03:15:01 +08:00
|
|
|
#endif /* USE_CURL_ASYNC */
|