2004-04-26 15:20:11 +08:00
|
|
|
/***************************************************************************
|
2004-06-24 15:43:48 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2004-04-26 15:20:11 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2022-04-25 15:31:09 +08:00
|
|
|
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2004-04-26 15:20:11 +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-24 15:43:48 +08:00
|
|
|
*
|
2004-04-26 15:20:11 +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
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2011-01-30 08:00:52 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2004-04-26 15:20:11 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2017-10-27 02:52:22 +08:00
|
|
|
/***********************************************************************
|
|
|
|
* Only for ares-enabled builds
|
|
|
|
* And only for functions that fulfill the asynch resolver backend API
|
|
|
|
* as defined in asyn.h, nothing else belongs in this file!
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#ifdef CURLRES_ARES
|
|
|
|
|
2009-04-14 21:50:32 +08:00
|
|
|
#include <limits.h>
|
2004-04-26 15:20:11 +08:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2009-12-31 01:59:56 +08:00
|
|
|
#ifdef __VMS
|
2004-04-26 15:20:11 +08:00
|
|
|
#include <in.h>
|
|
|
|
#include <inet.h>
|
|
|
|
#endif
|
|
|
|
|
2006-04-27 01:23:28 +08:00
|
|
|
#ifdef HAVE_PROCESS_H
|
2004-04-26 15:20:11 +08:00
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
|
|
|
|
#undef in_addr_t
|
|
|
|
#define in_addr_t unsigned long
|
|
|
|
#endif
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "share.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "multiif.h"
|
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "connect.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "progress.h"
|
2020-08-29 18:54:30 +08:00
|
|
|
#include "timediff.h"
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2020-09-21 20:28:40 +08:00
|
|
|
# if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
|
|
|
|
defined(WIN32)
|
2011-01-30 03:12:10 +08:00
|
|
|
# define CARES_STATICLIB
|
|
|
|
# endif
|
|
|
|
# include <ares.h>
|
2012-10-24 05:06:38 +08:00
|
|
|
# include <ares_version.h> /* really old c-ares didn't include this by
|
|
|
|
itself */
|
2011-01-30 03:12:10 +08:00
|
|
|
|
|
|
|
#if ARES_VERSION >= 0x010500
|
|
|
|
/* c-ares 1.5.0 or later, the callback proto is modified */
|
|
|
|
#define HAVE_CARES_CALLBACK_TIMEOUTS 1
|
|
|
|
#endif
|
|
|
|
|
2021-08-11 14:53:08 +08:00
|
|
|
#if ARES_VERSION >= 0x010601
|
|
|
|
/* IPv6 supported since 1.6.1 */
|
|
|
|
#define HAVE_CARES_IPV6 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ARES_VERSION >= 0x010704
|
|
|
|
#define HAVE_CARES_SERVERS_CSV 1
|
|
|
|
#define HAVE_CARES_LOCAL_DEV 1
|
|
|
|
#define HAVE_CARES_SET_LOCAL 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ARES_VERSION >= 0x010b00
|
|
|
|
#define HAVE_CARES_PORTS_CSV 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ARES_VERSION >= 0x011000
|
|
|
|
/* 1.16.0 or later has ares_getaddrinfo */
|
|
|
|
#define HAVE_CARES_GETADDRINFO 1
|
|
|
|
#endif
|
|
|
|
|
2016-04-29 21:46:40 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#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"
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2020-11-23 21:54:10 +08:00
|
|
|
struct thread_data {
|
2021-08-10 22:11:51 +08:00
|
|
|
int num_pending; /* number of outstanding c-ares requests */
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares
|
|
|
|
parts */
|
2011-01-30 03:12:10 +08:00
|
|
|
int last_status;
|
2021-12-14 15:31:37 +08:00
|
|
|
#ifndef HAVE_CARES_GETADDRINFO
|
2019-01-16 02:39:41 +08:00
|
|
|
struct curltime happy_eyeballs_dns_time; /* when this timer started, or 0 */
|
2021-12-14 15:31:37 +08:00
|
|
|
#endif
|
2022-08-15 22:49:44 +08:00
|
|
|
char hostname[1];
|
2011-01-30 03:12:10 +08:00
|
|
|
};
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2019-01-16 02:39:41 +08:00
|
|
|
/* How long we are willing to wait for additional parallel responses after
|
|
|
|
obtaining a "definitive" one.
|
|
|
|
|
|
|
|
This is intended to equal the c-ares default timeout. cURL always uses that
|
|
|
|
default value. Unfortunately, c-ares doesn't expose its default timeout in
|
|
|
|
its API, but it is officially documented as 5 seconds.
|
|
|
|
|
|
|
|
See query_completed_cb() for an explanation of how this is used.
|
|
|
|
*/
|
|
|
|
#define HAPPY_EYEBALLS_DNS_TIMEOUT 5000
|
|
|
|
|
2011-01-30 03:12:10 +08:00
|
|
|
/*
|
2011-01-30 08:00:52 +08:00
|
|
|
* Curl_resolver_global_init() - the generic low-level asynchronous name
|
|
|
|
* resolve API. Called from curl_global_init() to initialize global resolver
|
|
|
|
* environment. Initializes ares library.
|
2011-01-30 03:12:10 +08:00
|
|
|
*/
|
2011-01-30 08:00:52 +08:00
|
|
|
int Curl_resolver_global_init(void)
|
2011-01-30 03:12:10 +08:00
|
|
|
{
|
|
|
|
#ifdef CARES_HAVE_ARES_LIBRARY_INIT
|
|
|
|
if(ares_library_init(ARES_LIB_INIT_ALL)) {
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-30 08:00:52 +08:00
|
|
|
* Curl_resolver_global_cleanup()
|
|
|
|
*
|
2011-01-30 03:12:10 +08:00
|
|
|
* Called from curl_global_cleanup() to destroy global resolver environment.
|
|
|
|
* Deinitializes ares library.
|
|
|
|
*/
|
2011-01-30 08:00:52 +08:00
|
|
|
void Curl_resolver_global_cleanup(void)
|
2011-01-30 03:12:10 +08:00
|
|
|
{
|
|
|
|
#ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
|
|
|
|
ares_library_cleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:01:19 +08:00
|
|
|
|
2021-01-13 18:43:35 +08:00
|
|
|
static void sock_state_cb(void *data, ares_socket_t socket_fd,
|
|
|
|
int readable, int writable)
|
2018-11-05 18:01:19 +08:00
|
|
|
{
|
|
|
|
struct Curl_easy *easy = data;
|
|
|
|
if(!readable && !writable) {
|
|
|
|
DEBUGASSERT(easy);
|
|
|
|
Curl_multi_closed(easy, socket_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-30 03:12:10 +08:00
|
|
|
/*
|
2011-01-30 08:00:52 +08:00
|
|
|
* Curl_resolver_init()
|
|
|
|
*
|
|
|
|
* Called from curl_easy_init() -> Curl_open() to initialize resolver
|
|
|
|
* URL-state specific environment ('resolver' member of the UrlState
|
|
|
|
* structure). Fills the passed pointer by the initialized ares_channel.
|
2011-01-30 03:12:10 +08:00
|
|
|
*/
|
2018-11-05 18:01:19 +08:00
|
|
|
CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver)
|
2011-01-30 03:12:10 +08:00
|
|
|
{
|
2018-11-05 18:01:19 +08:00
|
|
|
int status;
|
|
|
|
struct ares_options options;
|
|
|
|
int optmask = ARES_OPT_SOCK_STATE_CB;
|
2021-01-13 18:43:35 +08:00
|
|
|
options.sock_state_cb = sock_state_cb;
|
2018-11-05 18:01:19 +08:00
|
|
|
options.sock_state_cb_data = easy;
|
|
|
|
status = ares_init_options((ares_channel*)resolver, &options, optmask);
|
2011-01-30 03:12:10 +08:00
|
|
|
if(status != ARES_SUCCESS) {
|
|
|
|
if(status == ARES_ENOMEM)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
else
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
}
|
|
|
|
return CURLE_OK;
|
|
|
|
/* make sure that all other returns from this function should destroy the
|
|
|
|
ares channel before returning error! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-30 08:00:52 +08:00
|
|
|
* Curl_resolver_cleanup()
|
|
|
|
*
|
|
|
|
* Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
|
|
|
|
* URL-state specific environment ('resolver' member of the UrlState
|
|
|
|
* structure). Destroys the ares channel.
|
2011-01-30 03:12:10 +08:00
|
|
|
*/
|
|
|
|
void Curl_resolver_cleanup(void *resolver)
|
|
|
|
{
|
|
|
|
ares_destroy((ares_channel)resolver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-30 08:00:52 +08:00
|
|
|
* Curl_resolver_duphandle()
|
|
|
|
*
|
|
|
|
* Called from curl_easy_duphandle() to duplicate resolver URL-state specific
|
|
|
|
* environment ('resolver' member of the UrlState structure). Duplicates the
|
|
|
|
* 'from' ares channel and passes the resulting channel to the 'to' pointer.
|
2011-01-30 03:12:10 +08:00
|
|
|
*/
|
2018-11-05 18:01:19 +08:00
|
|
|
CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to, void *from)
|
2011-01-30 03:12:10 +08:00
|
|
|
{
|
2018-11-05 18:01:19 +08:00
|
|
|
(void)from;
|
|
|
|
/*
|
|
|
|
* it would be better to call ares_dup instead, but right now
|
|
|
|
* it is not possible to set 'sock_state_cb_data' outside of
|
|
|
|
* ares_init_options
|
|
|
|
*/
|
|
|
|
return Curl_resolver_init(easy, to);
|
2011-01-30 03:12:10 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 06:34:59 +08:00
|
|
|
static void destroy_async_data(struct Curl_async *async);
|
2011-01-31 07:10:35 +08:00
|
|
|
|
2011-01-30 03:12:10 +08:00
|
|
|
/*
|
|
|
|
* Cancel all possibly still on-going resolves for this connection.
|
|
|
|
*/
|
2021-01-20 17:15:43 +08:00
|
|
|
void Curl_resolver_cancel(struct Curl_easy *data)
|
2011-01-30 03:12:10 +08:00
|
|
|
{
|
2021-06-13 00:37:52 +08:00
|
|
|
DEBUGASSERT(data);
|
|
|
|
if(data->state.async.resolver)
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_cancel((ares_channel)data->state.async.resolver);
|
|
|
|
destroy_async_data(&data->state.async);
|
2011-01-30 03:12:10 +08:00
|
|
|
}
|
|
|
|
|
2018-12-15 05:18:22 +08:00
|
|
|
/*
|
|
|
|
* We're equivalent to Curl_resolver_cancel() for the c-ares resolver. We
|
|
|
|
* never block.
|
|
|
|
*/
|
2021-01-20 17:15:43 +08:00
|
|
|
void Curl_resolver_kill(struct Curl_easy *data)
|
2018-12-15 05:18:22 +08:00
|
|
|
{
|
|
|
|
/* We don't need to check the resolver state because we can be called safely
|
|
|
|
at any time and we always do the same thing. */
|
2021-01-20 17:15:43 +08:00
|
|
|
Curl_resolver_cancel(data);
|
2018-12-15 05:18:22 +08:00
|
|
|
}
|
|
|
|
|
2011-01-30 03:12:10 +08:00
|
|
|
/*
|
|
|
|
* destroy_async_data() cleans up async resolver data.
|
|
|
|
*/
|
2016-12-14 06:34:59 +08:00
|
|
|
static void destroy_async_data(struct Curl_async *async)
|
2011-01-30 03:12:10 +08:00
|
|
|
{
|
2020-11-23 21:54:10 +08:00
|
|
|
if(async->tdata) {
|
|
|
|
struct thread_data *res = async->tdata;
|
2011-04-23 05:01:30 +08:00
|
|
|
if(res) {
|
|
|
|
if(res->temp_ai) {
|
2011-01-30 08:00:52 +08:00
|
|
|
Curl_freeaddrinfo(res->temp_ai);
|
|
|
|
res->temp_ai = NULL;
|
|
|
|
}
|
|
|
|
free(res);
|
2011-01-30 03:12:10 +08:00
|
|
|
}
|
2020-11-23 21:54:10 +08:00
|
|
|
async->tdata = NULL;
|
2011-01-30 03:12:10 +08:00
|
|
|
}
|
|
|
|
}
|
2004-04-26 15:20:11 +08:00
|
|
|
|
|
|
|
/*
|
2013-01-13 05:33:38 +08:00
|
|
|
* Curl_resolver_getsock() is called when someone from the outside world
|
|
|
|
* (using curl_multi_fdset()) wants to get our fd_set setup and we're talking
|
|
|
|
* with ares. The caller must make sure that this function is only called when
|
|
|
|
* we have a working ares channel.
|
2004-04-26 15:20:11 +08:00
|
|
|
*
|
2013-01-13 05:33:38 +08:00
|
|
|
* Returns: sockets-in-use-bitmap
|
2004-04-26 15:20:11 +08:00
|
|
|
*/
|
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
int Curl_resolver_getsock(struct Curl_easy *data,
|
2019-07-30 17:02:03 +08:00
|
|
|
curl_socket_t *socks)
|
2004-04-26 15:20:11 +08:00
|
|
|
{
|
2006-10-17 16:06:27 +08:00
|
|
|
struct timeval maxtime;
|
2007-10-03 21:19:34 +08:00
|
|
|
struct timeval timebuf;
|
|
|
|
struct timeval *timeout;
|
2011-12-01 04:23:44 +08:00
|
|
|
long milli;
|
2021-01-20 17:15:43 +08:00
|
|
|
int max = ares_getsock((ares_channel)data->state.async.resolver,
|
2019-07-30 17:02:03 +08:00
|
|
|
(ares_socket_t *)socks, MAX_SOCKSPEREASYHANDLE);
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2006-10-17 16:06:27 +08:00
|
|
|
maxtime.tv_sec = CURL_TIMEOUT_RESOLVE;
|
|
|
|
maxtime.tv_usec = 0;
|
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
timeout = ares_timeout((ares_channel)data->state.async.resolver, &maxtime,
|
2011-01-30 08:00:52 +08:00
|
|
|
&timebuf);
|
2020-08-29 18:54:30 +08:00
|
|
|
milli = (long)curlx_tvtoms(timeout);
|
2011-12-01 04:23:44 +08:00
|
|
|
if(milli == 0)
|
|
|
|
milli += 10;
|
2021-01-20 17:15:43 +08:00
|
|
|
Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
|
2006-10-17 16:06:27 +08:00
|
|
|
|
2006-04-10 23:00:53 +08:00
|
|
|
return max;
|
2004-04-26 15:20:11 +08:00
|
|
|
}
|
|
|
|
|
2007-05-31 19:34:32 +08:00
|
|
|
/*
|
2010-01-07 07:04:17 +08:00
|
|
|
* waitperform()
|
2007-05-31 19:34:32 +08:00
|
|
|
*
|
|
|
|
* 1) Ask ares what sockets it currently plays with, then
|
|
|
|
* 2) wait for the timeout period to check for action on ares' sockets.
|
|
|
|
* 3) tell ares to act on all the sockets marked as "with action"
|
|
|
|
*
|
2022-06-08 17:03:07 +08:00
|
|
|
* return number of sockets it worked on, or -1 on error
|
2007-05-31 19:34:32 +08:00
|
|
|
*/
|
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
static int waitperform(struct Curl_easy *data, timediff_t timeout_ms)
|
2007-05-31 19:34:32 +08:00
|
|
|
{
|
|
|
|
int nfds;
|
|
|
|
int bitmask;
|
2010-03-12 02:53:42 +08:00
|
|
|
ares_socket_t socks[ARES_GETSOCK_MAXNUM];
|
2007-05-31 19:34:32 +08:00
|
|
|
struct pollfd pfd[ARES_GETSOCK_MAXNUM];
|
|
|
|
int i;
|
2008-04-29 12:18:02 +08:00
|
|
|
int num = 0;
|
2007-05-31 19:34:32 +08:00
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
bitmask = ares_getsock((ares_channel)data->state.async.resolver, socks,
|
2011-01-30 08:00:52 +08:00
|
|
|
ARES_GETSOCK_MAXNUM);
|
2007-05-31 19:34:32 +08:00
|
|
|
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
|
2007-05-31 19:34:32 +08:00
|
|
|
pfd[i].events = 0;
|
2008-04-29 12:18:02 +08:00
|
|
|
pfd[i].revents = 0;
|
2007-05-31 19:34:32 +08:00
|
|
|
if(ARES_GETSOCK_READABLE(bitmask, i)) {
|
|
|
|
pfd[i].fd = socks[i];
|
|
|
|
pfd[i].events |= POLLRDNORM|POLLIN;
|
|
|
|
}
|
|
|
|
if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
|
|
|
|
pfd[i].fd = socks[i];
|
|
|
|
pfd[i].events |= POLLWRNORM|POLLOUT;
|
|
|
|
}
|
2021-04-19 16:46:11 +08:00
|
|
|
if(pfd[i].events)
|
2008-04-29 12:18:02 +08:00
|
|
|
num++;
|
|
|
|
else
|
2007-05-31 19:34:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-08 17:03:07 +08:00
|
|
|
if(num) {
|
2007-05-31 19:34:32 +08:00
|
|
|
nfds = Curl_poll(pfd, num, timeout_ms);
|
2022-06-08 17:03:07 +08:00
|
|
|
if(nfds < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-31 19:34:32 +08:00
|
|
|
else
|
|
|
|
nfds = 0;
|
|
|
|
|
|
|
|
if(!nfds)
|
2021-10-23 02:59:11 +08:00
|
|
|
/* Call ares_process() unconditionally here, even if we simply timed out
|
2007-05-31 19:34:32 +08:00
|
|
|
above, as otherwise the ares name resolve won't timeout! */
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_process_fd((ares_channel)data->state.async.resolver, ARES_SOCKET_BAD,
|
2011-01-30 08:00:52 +08:00
|
|
|
ARES_SOCKET_BAD);
|
2007-05-31 19:34:32 +08:00
|
|
|
else {
|
|
|
|
/* move through the descriptors and ask for processing on them */
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; i < num; i++)
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_process_fd((ares_channel)data->state.async.resolver,
|
2019-05-11 20:51:24 +08:00
|
|
|
(pfd[i].revents & (POLLRDNORM|POLLIN))?
|
2007-05-31 19:34:32 +08:00
|
|
|
pfd[i].fd:ARES_SOCKET_BAD,
|
2019-05-11 20:51:24 +08:00
|
|
|
(pfd[i].revents & (POLLWRNORM|POLLOUT))?
|
2007-05-31 19:34:32 +08:00
|
|
|
pfd[i].fd:ARES_SOCKET_BAD);
|
|
|
|
}
|
|
|
|
return nfds;
|
|
|
|
}
|
|
|
|
|
2004-04-26 15:20:11 +08:00
|
|
|
/*
|
2011-01-31 07:10:35 +08:00
|
|
|
* Curl_resolver_is_resolved() is called repeatedly to check if a previous
|
|
|
|
* name resolve request has completed. It should also make sure to time-out if
|
|
|
|
* the operation seems to take too long.
|
2004-04-26 15:20:11 +08:00
|
|
|
*
|
|
|
|
* Returns normal CURLcode errors.
|
|
|
|
*/
|
2021-01-20 17:15:43 +08:00
|
|
|
CURLcode Curl_resolver_is_resolved(struct Curl_easy *data,
|
2011-01-31 07:10:35 +08:00
|
|
|
struct Curl_dns_entry **dns)
|
2004-04-26 15:20:11 +08:00
|
|
|
{
|
2021-01-20 17:15:43 +08:00
|
|
|
struct thread_data *res = data->state.async.tdata;
|
2014-12-15 00:19:59 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2020-05-02 22:59:38 +08:00
|
|
|
DEBUGASSERT(dns);
|
|
|
|
*dns = NULL;
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2022-06-08 17:03:07 +08:00
|
|
|
if(waitperform(data, 0) < 0)
|
|
|
|
return CURLE_UNRECOVERABLE_POLL;
|
2007-05-31 19:34:32 +08:00
|
|
|
|
2021-12-14 15:31:37 +08:00
|
|
|
#ifndef HAVE_CARES_GETADDRINFO
|
2019-01-16 02:39:41 +08:00
|
|
|
/* Now that we've checked for any last minute results above, see if there are
|
|
|
|
any responses still pending when the EXPIRE_HAPPY_EYEBALLS_DNS timer
|
|
|
|
expires. */
|
|
|
|
if(res
|
|
|
|
&& res->num_pending
|
|
|
|
/* This is only set to non-zero if the timer was started. */
|
|
|
|
&& (res->happy_eyeballs_dns_time.tv_sec
|
|
|
|
|| res->happy_eyeballs_dns_time.tv_usec)
|
|
|
|
&& (Curl_timediff(Curl_now(), res->happy_eyeballs_dns_time)
|
|
|
|
>= HAPPY_EYEBALLS_DNS_TIMEOUT)) {
|
|
|
|
/* Remember that the EXPIRE_HAPPY_EYEBALLS_DNS timer is no longer
|
|
|
|
running. */
|
|
|
|
memset(
|
|
|
|
&res->happy_eyeballs_dns_time, 0, sizeof(res->happy_eyeballs_dns_time));
|
|
|
|
|
|
|
|
/* Cancel the raw c-ares request, which will fire query_completed_cb() with
|
|
|
|
ARES_ECANCELLED synchronously for all pending responses. This will
|
|
|
|
leave us with res->num_pending == 0, which is perfect for the next
|
|
|
|
block. */
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_cancel((ares_channel)data->state.async.resolver);
|
2019-01-16 02:39:41 +08:00
|
|
|
DEBUGASSERT(res->num_pending == 0);
|
|
|
|
}
|
2021-12-14 15:31:37 +08:00
|
|
|
#endif
|
2019-01-16 02:39:41 +08:00
|
|
|
|
2011-04-23 05:01:30 +08:00
|
|
|
if(res && !res->num_pending) {
|
2021-01-20 17:15:43 +08:00
|
|
|
(void)Curl_addrinfo_callback(data, res->last_status, res->temp_ai);
|
2020-05-02 22:59:38 +08:00
|
|
|
/* temp_ai ownership is moved to the connection, so we need not free-up
|
|
|
|
them */
|
|
|
|
res->temp_ai = NULL;
|
|
|
|
|
2021-02-18 20:14:55 +08:00
|
|
|
if(!data->state.async.dns)
|
|
|
|
result = Curl_resolver_error(data);
|
2020-05-02 22:59:38 +08:00
|
|
|
else
|
2021-01-20 17:15:43 +08:00
|
|
|
*dns = data->state.async.dns;
|
2013-06-24 02:25:38 +08:00
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
destroy_async_data(&data->state.async);
|
2004-04-26 15:20:11 +08:00
|
|
|
}
|
|
|
|
|
2014-12-15 00:19:59 +08:00
|
|
|
return result;
|
2004-04-26 15:20:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-31 07:10:35 +08:00
|
|
|
* Curl_resolver_wait_resolv()
|
|
|
|
*
|
2018-12-15 05:18:22 +08:00
|
|
|
* Waits for a resolve to finish. This function should be avoided since using
|
2011-01-31 07:10:35 +08:00
|
|
|
* this risk getting the multi interface to "hang".
|
2004-04-26 15:20:11 +08:00
|
|
|
*
|
2020-05-02 22:59:38 +08:00
|
|
|
* 'entry' MUST be non-NULL.
|
2004-04-26 15:20:11 +08:00
|
|
|
*
|
2018-12-15 05:18:22 +08:00
|
|
|
* Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
|
|
|
|
* CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
|
2004-04-26 15:20:11 +08:00
|
|
|
*/
|
2021-01-20 17:15:43 +08:00
|
|
|
CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data,
|
2011-01-31 07:10:35 +08:00
|
|
|
struct Curl_dns_entry **entry)
|
2004-04-26 15:20:11 +08:00
|
|
|
{
|
2014-12-15 00:19:59 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2017-10-26 21:24:50 +08:00
|
|
|
timediff_t timeout;
|
2017-10-25 17:59:43 +08:00
|
|
|
struct curltime now = Curl_now();
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2020-05-02 22:59:38 +08:00
|
|
|
DEBUGASSERT(entry);
|
|
|
|
*entry = NULL; /* clear on entry */
|
2017-03-07 15:08:55 +08:00
|
|
|
|
2011-01-05 06:07:58 +08:00
|
|
|
timeout = Curl_timeleft(data, &now, TRUE);
|
2017-03-06 16:21:01 +08:00
|
|
|
if(timeout < 0) {
|
|
|
|
/* already expired! */
|
2021-01-20 17:15:43 +08:00
|
|
|
connclose(data->conn, "Timed out before name resolve started");
|
2017-03-06 16:21:01 +08:00
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
2010-12-06 06:39:41 +08:00
|
|
|
if(!timeout)
|
2007-02-06 06:51:32 +08:00
|
|
|
timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
|
2004-04-26 15:20:11 +08:00
|
|
|
|
|
|
|
/* Wait for the name resolve query to complete. */
|
2017-03-07 05:45:10 +08:00
|
|
|
while(!result) {
|
2004-04-26 15:20:11 +08:00
|
|
|
struct timeval *tvp, tv, store;
|
2008-04-29 12:18:02 +08:00
|
|
|
int itimeout;
|
2020-06-01 14:49:20 +08:00
|
|
|
timediff_t timeout_ms;
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2020-06-01 14:49:20 +08:00
|
|
|
#if TIMEDIFF_T_MAX > INT_MAX
|
|
|
|
itimeout = (timeout > INT_MAX) ? INT_MAX : (int)timeout;
|
|
|
|
#else
|
|
|
|
itimeout = (int)timeout;
|
|
|
|
#endif
|
2008-04-29 12:18:02 +08:00
|
|
|
|
|
|
|
store.tv_sec = itimeout/1000;
|
|
|
|
store.tv_usec = (itimeout%1000)*1000;
|
2004-06-24 15:43:48 +08:00
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
tvp = ares_timeout((ares_channel)data->state.async.resolver, &store, &tv);
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2010-01-07 07:04:17 +08:00
|
|
|
/* use the timeout period ares returned to us above if less than one
|
|
|
|
second is left, otherwise just use 1000ms to make sure the progress
|
|
|
|
callback gets called frequent enough */
|
|
|
|
if(!tvp->tv_sec)
|
2020-06-01 14:49:20 +08:00
|
|
|
timeout_ms = (timediff_t)(tvp->tv_usec/1000);
|
2010-01-07 07:04:17 +08:00
|
|
|
else
|
|
|
|
timeout_ms = 1000;
|
|
|
|
|
2022-06-08 17:03:07 +08:00
|
|
|
if(waitperform(data, timeout_ms) < 0)
|
|
|
|
return CURLE_UNRECOVERABLE_POLL;
|
2021-01-20 17:15:43 +08:00
|
|
|
result = Curl_resolver_is_resolved(data, entry);
|
2007-06-11 21:32:49 +08:00
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
if(result || data->state.async.done)
|
2007-05-31 19:34:32 +08:00
|
|
|
break;
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2021-01-18 18:56:50 +08:00
|
|
|
if(Curl_pgrsUpdate(data))
|
2014-12-15 00:19:59 +08:00
|
|
|
result = CURLE_ABORTED_BY_CALLBACK;
|
2010-01-07 07:04:17 +08:00
|
|
|
else {
|
2017-10-25 17:59:43 +08:00
|
|
|
struct curltime now2 = Curl_now();
|
2017-10-23 18:05:49 +08:00
|
|
|
timediff_t timediff = Curl_timediff(now2, now); /* spent time */
|
2017-05-21 01:39:51 +08:00
|
|
|
if(timediff <= 0)
|
|
|
|
timeout -= 1; /* always deduct at least 1 */
|
|
|
|
else if(timediff > timeout)
|
|
|
|
timeout = -1;
|
|
|
|
else
|
2020-06-01 14:49:20 +08:00
|
|
|
timeout -= timediff;
|
2010-12-06 06:39:41 +08:00
|
|
|
now = now2; /* for next loop */
|
2010-01-07 07:04:17 +08:00
|
|
|
}
|
2017-03-07 05:45:10 +08:00
|
|
|
if(timeout < 0)
|
|
|
|
result = CURLE_OPERATION_TIMEDOUT;
|
2004-04-26 15:20:11 +08:00
|
|
|
}
|
2017-03-07 05:45:10 +08:00
|
|
|
if(result)
|
|
|
|
/* failure, so we cancel the ares operation */
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_cancel((ares_channel)data->state.async.resolver);
|
2004-04-26 15:20:11 +08:00
|
|
|
|
|
|
|
/* Operation complete, if the lookup was successful we now have the entry
|
|
|
|
in the cache. */
|
|
|
|
if(entry)
|
2021-01-20 17:15:43 +08:00
|
|
|
*entry = data->state.async.dns;
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2014-12-15 00:19:59 +08:00
|
|
|
if(result)
|
2004-04-26 15:20:11 +08:00
|
|
|
/* close the connection, since we can't return failure here without
|
2019-05-14 22:36:15 +08:00
|
|
|
cleaning up this connection properly. */
|
2021-01-20 17:15:43 +08:00
|
|
|
connclose(data->conn, "c-ares resolve failed");
|
2004-06-24 15:43:48 +08:00
|
|
|
|
2014-12-15 00:19:59 +08:00
|
|
|
return result;
|
2004-04-26 15:20:11 +08:00
|
|
|
}
|
|
|
|
|
2021-08-11 14:53:08 +08:00
|
|
|
#ifndef HAVE_CARES_GETADDRINFO
|
2021-08-10 22:11:51 +08:00
|
|
|
|
2011-01-30 03:12:10 +08:00
|
|
|
/* Connects results to the list */
|
2020-11-23 21:54:10 +08:00
|
|
|
static void compound_results(struct thread_data *res,
|
2020-05-14 06:05:04 +08:00
|
|
|
struct Curl_addrinfo *ai)
|
2011-01-30 03:12:10 +08:00
|
|
|
{
|
2011-01-30 08:00:52 +08:00
|
|
|
if(!ai)
|
|
|
|
return;
|
2011-01-30 03:12:10 +08:00
|
|
|
|
2021-06-04 05:56:37 +08:00
|
|
|
#ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
|
|
|
|
if(res->temp_ai && res->temp_ai->ai_family == PF_INET6) {
|
|
|
|
/* We have results already, put the new IPv6 entries at the head of the
|
|
|
|
list. */
|
|
|
|
struct Curl_addrinfo *temp_ai_tail = res->temp_ai;
|
2011-01-30 03:12:10 +08:00
|
|
|
|
2021-06-04 05:56:37 +08:00
|
|
|
while(temp_ai_tail->ai_next)
|
|
|
|
temp_ai_tail = temp_ai_tail->ai_next;
|
|
|
|
|
|
|
|
temp_ai_tail->ai_next = ai;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* CURLRES_IPV6 */
|
|
|
|
{
|
|
|
|
/* Add the new results to the list of old results. */
|
|
|
|
struct Curl_addrinfo *ai_tail = ai;
|
|
|
|
while(ai_tail->ai_next)
|
|
|
|
ai_tail = ai_tail->ai_next;
|
|
|
|
|
|
|
|
ai_tail->ai_next = res->temp_ai;
|
|
|
|
res->temp_ai = ai;
|
|
|
|
}
|
2011-01-30 03:12:10 +08:00
|
|
|
}
|
|
|
|
|
2010-01-22 14:36:52 +08:00
|
|
|
/*
|
|
|
|
* ares_query_completed_cb() is the callback that ares will call when
|
|
|
|
* the host query initiated by ares_gethostbyname() from Curl_getaddrinfo(),
|
|
|
|
* when using ares, is completed either successfully or with failure.
|
|
|
|
*/
|
2011-01-30 08:00:52 +08:00
|
|
|
static void query_completed_cb(void *arg, /* (struct connectdata *) */
|
|
|
|
int status,
|
2010-01-22 14:36:52 +08:00
|
|
|
#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
|
2011-01-30 08:00:52 +08:00
|
|
|
int timeouts,
|
2010-01-22 14:36:52 +08:00
|
|
|
#endif
|
2011-01-30 08:00:52 +08:00
|
|
|
struct hostent *hostent)
|
2010-01-22 14:36:52 +08:00
|
|
|
{
|
2021-01-20 17:15:43 +08:00
|
|
|
struct Curl_easy *data = (struct Curl_easy *)arg;
|
2020-11-23 21:54:10 +08:00
|
|
|
struct thread_data *res;
|
2010-01-27 06:59:43 +08:00
|
|
|
|
2010-01-22 14:36:52 +08:00
|
|
|
#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
|
|
|
|
(void)timeouts; /* ignored */
|
|
|
|
#endif
|
|
|
|
|
2011-01-30 08:00:52 +08:00
|
|
|
if(ARES_EDESTRUCTION == status)
|
|
|
|
/* when this ares handle is getting destroyed, the 'arg' pointer may not
|
|
|
|
be valid so only defer it when we know the 'status' says its fine! */
|
|
|
|
return;
|
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
res = data->state.async.tdata;
|
2018-07-04 06:58:39 +08:00
|
|
|
if(res) {
|
|
|
|
res->num_pending--;
|
2011-01-30 03:12:10 +08:00
|
|
|
|
2018-07-04 06:58:39 +08:00
|
|
|
if(CURL_ASYNC_SUCCESS == status) {
|
2021-01-20 17:15:43 +08:00
|
|
|
struct Curl_addrinfo *ai = Curl_he2ai(hostent, data->state.async.port);
|
2018-07-04 06:58:39 +08:00
|
|
|
if(ai) {
|
|
|
|
compound_results(res, ai);
|
|
|
|
}
|
2011-01-30 08:00:52 +08:00
|
|
|
}
|
2018-07-04 06:58:39 +08:00
|
|
|
/* A successful result overwrites any previous error */
|
|
|
|
if(res->last_status != ARES_SUCCESS)
|
|
|
|
res->last_status = status;
|
2019-01-16 02:39:41 +08:00
|
|
|
|
|
|
|
/* If there are responses still pending, we presume they must be the
|
|
|
|
complementary IPv4 or IPv6 lookups that we started in parallel in
|
|
|
|
Curl_resolver_getaddrinfo() (for Happy Eyeballs). If we've got a
|
|
|
|
"definitive" response from one of a set of parallel queries, we need to
|
|
|
|
think about how long we're willing to wait for more responses. */
|
|
|
|
if(res->num_pending
|
|
|
|
/* Only these c-ares status values count as "definitive" for these
|
|
|
|
purposes. For example, ARES_ENODATA is what we expect when there is
|
|
|
|
no IPv6 entry for a domain name, and that's not a reason to get more
|
|
|
|
aggressive in our timeouts for the other response. Other errors are
|
|
|
|
either a result of bad input (which should affect all parallel
|
|
|
|
requests), local or network conditions, non-definitive server
|
|
|
|
responses, or us cancelling the request. */
|
|
|
|
&& (status == ARES_SUCCESS || status == ARES_ENOTFOUND)) {
|
|
|
|
/* Right now, there can only be up to two parallel queries, so don't
|
|
|
|
bother handling any other cases. */
|
|
|
|
DEBUGASSERT(res->num_pending == 1);
|
|
|
|
|
|
|
|
/* It's possible that one of these parallel queries could succeed
|
|
|
|
quickly, but the other could always fail or timeout (when we're
|
|
|
|
talking to a pool of DNS servers that can only successfully resolve
|
|
|
|
IPv4 address, for example).
|
|
|
|
|
|
|
|
It's also possible that the other request could always just take
|
|
|
|
longer because it needs more time or only the second DNS server can
|
|
|
|
fulfill it successfully. But, to align with the philosophy of Happy
|
|
|
|
Eyeballs, we don't want to wait _too_ long or users will think
|
|
|
|
requests are slow when IPv6 lookups don't actually work (but IPv4 ones
|
|
|
|
do).
|
|
|
|
|
|
|
|
So, now that we have a usable answer (some IPv4 addresses, some IPv6
|
|
|
|
addresses, or "no such domain"), we start a timeout for the remaining
|
|
|
|
pending responses. Even though it is typical that this resolved
|
|
|
|
request came back quickly, that needn't be the case. It might be that
|
|
|
|
this completing request didn't get a result from the first DNS server
|
|
|
|
or even the first round of the whole DNS server pool. So it could
|
|
|
|
already be quite some time after we issued the DNS queries in the
|
|
|
|
first place. Without modifying c-ares, we can't know exactly where in
|
|
|
|
its retry cycle we are. We could guess based on how much time has
|
|
|
|
gone by, but it doesn't really matter. Happy Eyeballs tells us that,
|
|
|
|
given usable information in hand, we simply don't want to wait "too
|
|
|
|
much longer" after we get a result.
|
|
|
|
|
|
|
|
We simply wait an additional amount of time equal to the default
|
|
|
|
c-ares query timeout. That is enough time for a typical parallel
|
|
|
|
response to arrive without being "too long". Even on a network
|
|
|
|
where one of the two types of queries is failing or timing out
|
|
|
|
constantly, this will usually mean we wait a total of the default
|
|
|
|
c-ares timeout (5 seconds) plus the round trip time for the successful
|
|
|
|
request, which seems bearable. The downside is that c-ares might race
|
|
|
|
with us to issue one more retry just before we give up, but it seems
|
|
|
|
better to "waste" that request instead of trying to guess the perfect
|
|
|
|
timeout to prevent it. After all, we don't even know where in the
|
|
|
|
c-ares retry cycle each request is.
|
|
|
|
*/
|
|
|
|
res->happy_eyeballs_dns_time = Curl_now();
|
2021-01-20 17:15:43 +08:00
|
|
|
Curl_expire(data, HAPPY_EYEBALLS_DNS_TIMEOUT,
|
|
|
|
EXPIRE_HAPPY_EYEBALLS_DNS);
|
2019-01-16 02:39:41 +08:00
|
|
|
}
|
2010-01-22 14:36:52 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 22:11:51 +08:00
|
|
|
#else
|
|
|
|
/* c-ares 1.16.0 or later */
|
2008-07-10 02:39:49 +08:00
|
|
|
|
2021-08-10 22:11:51 +08:00
|
|
|
/*
|
|
|
|
* ares2addr() converts an address list provided by c-ares to an internal
|
|
|
|
* libcurl compatible list
|
|
|
|
*/
|
|
|
|
static struct Curl_addrinfo *ares2addr(struct ares_addrinfo_node *node)
|
|
|
|
{
|
|
|
|
/* traverse the ares_addrinfo_node list */
|
|
|
|
struct ares_addrinfo_node *ai;
|
|
|
|
struct Curl_addrinfo *cafirst = NULL;
|
|
|
|
struct Curl_addrinfo *calast = NULL;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
for(ai = node; ai != NULL; ai = ai->ai_next) {
|
|
|
|
size_t ss_size;
|
|
|
|
struct Curl_addrinfo *ca;
|
|
|
|
/* ignore elements with unsupported address family, */
|
|
|
|
/* settle family-specific sockaddr structure size. */
|
|
|
|
if(ai->ai_family == AF_INET)
|
|
|
|
ss_size = sizeof(struct sockaddr_in);
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else if(ai->ai_family == AF_INET6)
|
|
|
|
ss_size = sizeof(struct sockaddr_in6);
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ignore elements without required address info */
|
|
|
|
if(!ai->ai_addr || !(ai->ai_addrlen > 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ignore elements with bogus address size */
|
|
|
|
if((size_t)ai->ai_addrlen < ss_size)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ca = malloc(sizeof(struct Curl_addrinfo) + ss_size);
|
|
|
|
if(!ca) {
|
|
|
|
error = EAI_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy each structure member individually, member ordering, */
|
|
|
|
/* size, or padding might be different for each platform. */
|
|
|
|
|
|
|
|
ca->ai_flags = ai->ai_flags;
|
|
|
|
ca->ai_family = ai->ai_family;
|
|
|
|
ca->ai_socktype = ai->ai_socktype;
|
|
|
|
ca->ai_protocol = ai->ai_protocol;
|
|
|
|
ca->ai_addrlen = (curl_socklen_t)ss_size;
|
|
|
|
ca->ai_addr = NULL;
|
|
|
|
ca->ai_canonname = NULL;
|
|
|
|
ca->ai_next = NULL;
|
|
|
|
|
|
|
|
ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
|
|
|
|
memcpy(ca->ai_addr, ai->ai_addr, ss_size);
|
|
|
|
|
|
|
|
/* if the return list is empty, this becomes the first element */
|
|
|
|
if(!cafirst)
|
|
|
|
cafirst = ca;
|
|
|
|
|
|
|
|
/* add this element last in the return list */
|
|
|
|
if(calast)
|
|
|
|
calast->ai_next = ca;
|
|
|
|
calast = ca;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we failed, destroy the Curl_addrinfo list */
|
|
|
|
if(error) {
|
|
|
|
Curl_freeaddrinfo(cafirst);
|
|
|
|
cafirst = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cafirst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addrinfo_cb(void *arg, int status, int timeouts,
|
|
|
|
struct ares_addrinfo *result)
|
|
|
|
{
|
|
|
|
struct Curl_easy *data = (struct Curl_easy *)arg;
|
|
|
|
struct thread_data *res = data->state.async.tdata;
|
|
|
|
(void)timeouts;
|
|
|
|
if(ARES_SUCCESS == status) {
|
|
|
|
res->temp_ai = ares2addr(result->nodes);
|
|
|
|
res->last_status = CURL_ASYNC_SUCCESS;
|
2021-08-20 20:39:14 +08:00
|
|
|
ares_freeaddrinfo(result);
|
2021-08-10 22:11:51 +08:00
|
|
|
}
|
|
|
|
res->num_pending--;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2004-04-26 15:20:11 +08:00
|
|
|
/*
|
2011-01-31 07:10:35 +08:00
|
|
|
* Curl_resolver_getaddrinfo() - when using ares
|
2004-04-26 15:20:11 +08:00
|
|
|
*
|
|
|
|
* Returns name information about the given hostname and port number. If
|
2022-09-19 05:23:42 +08:00
|
|
|
* successful, the 'hostent' is returned and the fourth argument will point to
|
2004-04-26 15:20:11 +08:00
|
|
|
* memory we need to free after use. That memory *MUST* be freed with
|
|
|
|
* Curl_freeaddrinfo(), nothing else.
|
|
|
|
*/
|
2021-01-20 17:15:43 +08:00
|
|
|
struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data,
|
2020-05-14 06:05:04 +08:00
|
|
|
const char *hostname,
|
|
|
|
int port,
|
|
|
|
int *waitp)
|
2004-04-26 15:20:11 +08:00
|
|
|
{
|
2022-08-15 22:49:44 +08:00
|
|
|
struct thread_data *res = NULL;
|
|
|
|
size_t namelen = strlen(hostname);
|
2010-01-26 07:50:13 +08:00
|
|
|
*waitp = 0; /* default to synchronous response */
|
2004-06-24 15:43:48 +08:00
|
|
|
|
2022-08-15 22:49:44 +08:00
|
|
|
res = calloc(sizeof(struct thread_data) + namelen, 1);
|
|
|
|
if(res) {
|
|
|
|
strcpy(res->hostname, hostname);
|
|
|
|
data->state.async.hostname = res->hostname;
|
2021-01-20 17:15:43 +08:00
|
|
|
data->state.async.port = port;
|
|
|
|
data->state.async.done = FALSE; /* not done */
|
|
|
|
data->state.async.status = 0; /* clear */
|
|
|
|
data->state.async.dns = NULL; /* clear */
|
|
|
|
data->state.async.tdata = res;
|
2004-04-26 15:20:11 +08:00
|
|
|
|
2011-01-30 03:12:10 +08:00
|
|
|
/* initial status - failed */
|
|
|
|
res->last_status = ARES_ENOTFOUND;
|
2010-12-19 05:31:39 +08:00
|
|
|
|
2021-08-11 14:53:08 +08:00
|
|
|
#ifdef HAVE_CARES_GETADDRINFO
|
2021-08-10 22:11:51 +08:00
|
|
|
{
|
|
|
|
struct ares_addrinfo_hints hints;
|
|
|
|
char service[12];
|
|
|
|
int pf = PF_INET;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
#ifdef CURLRES_IPV6
|
|
|
|
if(Curl_ipv6works(data))
|
|
|
|
/* The stack seems to be IPv6-enabled */
|
2022-09-20 04:59:35 +08:00
|
|
|
if(data->conn->ip_version != CURL_IPRESOLVE_V4)
|
|
|
|
pf = PF_UNSPEC;
|
2021-08-10 22:11:51 +08:00
|
|
|
#endif /* CURLRES_IPV6 */
|
|
|
|
hints.ai_family = pf;
|
|
|
|
hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP)?
|
|
|
|
SOCK_STREAM : SOCK_DGRAM;
|
|
|
|
msnprintf(service, sizeof(service), "%d", port);
|
|
|
|
res->num_pending = 1;
|
|
|
|
ares_getaddrinfo((ares_channel)data->state.async.resolver, hostname,
|
|
|
|
service, &hints, addrinfo_cb, data);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2021-08-11 14:53:08 +08:00
|
|
|
#ifdef HAVE_CARES_IPV6
|
2022-09-20 04:59:35 +08:00
|
|
|
if(Curl_ipv6works(data) && data->conn->ip_version != CURL_IPRESOLVE_V4) {
|
2021-04-25 03:04:53 +08:00
|
|
|
/* The stack seems to be IPv6-enabled */
|
|
|
|
res->num_pending = 2;
|
|
|
|
|
|
|
|
/* areschannel is already setup in the Curl_open() function */
|
|
|
|
ares_gethostbyname((ares_channel)data->state.async.resolver, hostname,
|
|
|
|
PF_INET, query_completed_cb, data);
|
|
|
|
ares_gethostbyname((ares_channel)data->state.async.resolver, hostname,
|
|
|
|
PF_INET6, query_completed_cb, data);
|
2010-12-19 05:31:39 +08:00
|
|
|
}
|
|
|
|
else
|
2021-08-11 14:53:08 +08:00
|
|
|
#endif
|
2010-12-19 05:31:39 +08:00
|
|
|
{
|
2011-01-30 03:12:10 +08:00
|
|
|
res->num_pending = 1;
|
2010-12-19 05:31:39 +08:00
|
|
|
|
|
|
|
/* areschannel is already setup in the Curl_open() function */
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_gethostbyname((ares_channel)data->state.async.resolver,
|
2021-04-25 03:04:53 +08:00
|
|
|
hostname, PF_INET,
|
2021-01-20 17:15:43 +08:00
|
|
|
query_completed_cb, data);
|
2010-12-19 05:31:39 +08:00
|
|
|
}
|
2021-08-10 22:11:51 +08:00
|
|
|
#endif
|
2010-01-26 07:50:13 +08:00
|
|
|
*waitp = 1; /* expect asynchronous response */
|
2004-04-26 15:20:11 +08:00
|
|
|
}
|
|
|
|
return NULL; /* no struct yet */
|
|
|
|
}
|
2011-04-12 23:34:28 +08:00
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
CURLcode Curl_set_dns_servers(struct Curl_easy *data,
|
2011-04-12 23:34:28 +08:00
|
|
|
char *servers)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_NOT_BUILT_IN;
|
2013-04-09 06:24:49 +08:00
|
|
|
int ares_result;
|
|
|
|
|
|
|
|
/* If server is NULL or empty, this would purge all DNS servers
|
|
|
|
* from ares library, which will cause any and all queries to fail.
|
|
|
|
* So, just return OK if none are configured and don't actually make
|
|
|
|
* any changes to c-ares. This lets c-ares use it's defaults, which
|
|
|
|
* it gets from the OS (for instance from /etc/resolv.conf on Linux).
|
|
|
|
*/
|
|
|
|
if(!(servers && servers[0]))
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2021-08-11 14:53:08 +08:00
|
|
|
#ifdef HAVE_CARES_SERVERS_CSV
|
|
|
|
#ifdef HAVE_CARES_PORTS_CSV
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_result = ares_set_servers_ports_csv(data->state.async.resolver,
|
|
|
|
servers);
|
2019-06-24 14:15:02 +08:00
|
|
|
#else
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_result = ares_set_servers_csv(data->state.async.resolver, servers);
|
2019-06-24 14:15:02 +08:00
|
|
|
#endif
|
2011-04-12 23:34:28 +08:00
|
|
|
switch(ares_result) {
|
|
|
|
case ARES_SUCCESS:
|
2011-12-01 12:39:02 +08:00
|
|
|
result = CURLE_OK;
|
2011-04-12 23:34:28 +08:00
|
|
|
break;
|
|
|
|
case ARES_ENOMEM:
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
case ARES_ENOTINITIALIZED:
|
|
|
|
case ARES_ENODATA:
|
|
|
|
case ARES_EBADSTR:
|
|
|
|
default:
|
|
|
|
result = CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else /* too old c-ares version! */
|
|
|
|
(void)data;
|
2013-04-09 06:24:49 +08:00
|
|
|
(void)(ares_result);
|
2011-04-12 23:34:28 +08:00
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
2013-09-08 00:45:50 +08:00
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
CURLcode Curl_set_dns_interface(struct Curl_easy *data,
|
2013-09-18 03:59:43 +08:00
|
|
|
const char *interf)
|
2013-09-08 00:45:50 +08:00
|
|
|
{
|
2021-08-11 14:53:08 +08:00
|
|
|
#ifdef HAVE_CARES_LOCAL_DEV
|
2013-09-18 03:59:43 +08:00
|
|
|
if(!interf)
|
|
|
|
interf = "";
|
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_set_local_dev((ares_channel)data->state.async.resolver, interf);
|
2013-09-18 03:59:43 +08:00
|
|
|
|
2013-09-08 00:45:50 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
#else /* c-ares version too old! */
|
|
|
|
(void)data;
|
2013-09-18 03:59:43 +08:00
|
|
|
(void)interf;
|
2013-09-08 00:45:50 +08:00
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
|
2013-09-08 00:45:50 +08:00
|
|
|
const char *local_ip4)
|
|
|
|
{
|
2021-08-11 14:53:08 +08:00
|
|
|
#ifdef HAVE_CARES_SET_LOCAL
|
2013-11-11 01:08:57 +08:00
|
|
|
struct in_addr a4;
|
2013-09-08 00:45:50 +08:00
|
|
|
|
|
|
|
if((!local_ip4) || (local_ip4[0] == 0)) {
|
2013-11-11 01:08:57 +08:00
|
|
|
a4.s_addr = 0; /* disabled: do not bind to a specific address */
|
2013-09-08 00:45:50 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(Curl_inet_pton(AF_INET, local_ip4, &a4) != 1) {
|
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
}
|
|
|
|
}
|
2013-09-18 03:59:43 +08:00
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_set_local_ip4((ares_channel)data->state.async.resolver,
|
|
|
|
ntohl(a4.s_addr));
|
2013-09-18 03:59:43 +08:00
|
|
|
|
2013-09-08 00:45:50 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
#else /* c-ares version too old! */
|
|
|
|
(void)data;
|
|
|
|
(void)local_ip4;
|
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
|
2013-09-08 00:45:50 +08:00
|
|
|
const char *local_ip6)
|
|
|
|
{
|
2021-08-11 14:53:08 +08:00
|
|
|
#if defined(HAVE_CARES_SET_LOCAL) && defined(ENABLE_IPV6)
|
2013-09-08 00:45:50 +08:00
|
|
|
unsigned char a6[INET6_ADDRSTRLEN];
|
|
|
|
|
|
|
|
if((!local_ip6) || (local_ip6[0] == 0)) {
|
|
|
|
/* disabled: do not bind to a specific address */
|
|
|
|
memset(a6, 0, sizeof(a6));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(Curl_inet_pton(AF_INET6, local_ip6, a6) != 1) {
|
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
}
|
|
|
|
}
|
2013-09-18 03:59:43 +08:00
|
|
|
|
2021-01-20 17:15:43 +08:00
|
|
|
ares_set_local_ip6((ares_channel)data->state.async.resolver, a6);
|
2013-09-18 03:59:43 +08:00
|
|
|
|
2013-09-08 00:45:50 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
#else /* c-ares version too old! */
|
|
|
|
(void)data;
|
|
|
|
(void)local_ip6;
|
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
|
|
|
}
|
2004-04-26 15:20:11 +08:00
|
|
|
#endif /* CURLRES_ARES */
|