|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "curl_setup.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef CURLRES_ARES |
|
|
|
|
|
#include <limits.h> |
|
|
#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 |
|
|
#ifdef __VMS |
|
|
#include <in.h> |
|
|
#include <inet.h> |
|
|
#endif |
|
|
|
|
|
#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" |
|
|
#include "timediff.h" |
|
|
|
|
|
#if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \ |
|
|
defined(_WIN32) |
|
|
# define CARES_STATICLIB |
|
|
#endif |
|
|
#include <ares.h> |
|
|
#include <ares_version.h> |
|
|
|
|
|
|
|
|
#if ARES_VERSION >= 0x010500 |
|
|
|
|
|
#define HAVE_CARES_CALLBACK_TIMEOUTS 1 |
|
|
#endif |
|
|
|
|
|
#if ARES_VERSION >= 0x010601 |
|
|
|
|
|
#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 |
|
|
|
|
|
#define HAVE_CARES_GETADDRINFO 1 |
|
|
#endif |
|
|
|
|
|
|
|
|
#include "curl_printf.h" |
|
|
#include "curl_memory.h" |
|
|
#include "memdebug.h" |
|
|
|
|
|
struct thread_data { |
|
|
int num_pending; |
|
|
struct Curl_addrinfo *temp_ai; |
|
|
|
|
|
int last_status; |
|
|
#ifndef HAVE_CARES_GETADDRINFO |
|
|
struct curltime happy_eyeballs_dns_time; |
|
|
#endif |
|
|
char hostname[1]; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define HAPPY_EYEBALLS_DNS_TIMEOUT 5000 |
|
|
|
|
|
#define CARES_TIMEOUT_PER_ATTEMPT 2000 |
|
|
|
|
|
static int ares_ver = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Curl_resolver_global_init(void) |
|
|
{ |
|
|
#ifdef CARES_HAVE_ARES_LIBRARY_INIT |
|
|
if(ares_library_init(ARES_LIB_INIT_ALL)) { |
|
|
return CURLE_FAILED_INIT; |
|
|
} |
|
|
#endif |
|
|
ares_version(&ares_ver); |
|
|
return CURLE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Curl_resolver_global_cleanup(void) |
|
|
{ |
|
|
#ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP |
|
|
ares_library_cleanup(); |
|
|
#endif |
|
|
} |
|
|
|
|
|
|
|
|
static void sock_state_cb(void *data, ares_socket_t socket_fd, |
|
|
int readable, int writable) |
|
|
{ |
|
|
struct Curl_easy *easy = data; |
|
|
if(!readable && !writable) { |
|
|
DEBUGASSERT(easy); |
|
|
Curl_multi_closed(easy, socket_fd); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver) |
|
|
{ |
|
|
int status; |
|
|
struct ares_options options; |
|
|
int optmask = ARES_OPT_SOCK_STATE_CB; |
|
|
options.sock_state_cb = sock_state_cb; |
|
|
options.sock_state_cb_data = easy; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DEBUGASSERT(ares_ver); |
|
|
if(ares_ver < 0x011400) { |
|
|
options.timeout = CARES_TIMEOUT_PER_ATTEMPT; |
|
|
optmask |= ARES_OPT_TIMEOUTMS; |
|
|
} |
|
|
|
|
|
status = ares_init_options((ares_channel*)resolver, &options, optmask); |
|
|
if(status != ARES_SUCCESS) { |
|
|
if(status == ARES_ENOMEM) |
|
|
return CURLE_OUT_OF_MEMORY; |
|
|
else |
|
|
return CURLE_FAILED_INIT; |
|
|
} |
|
|
return CURLE_OK; |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Curl_resolver_cleanup(void *resolver) |
|
|
{ |
|
|
ares_destroy((ares_channel)resolver); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to, void *from) |
|
|
{ |
|
|
(void)from; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Curl_resolver_init(easy, to); |
|
|
} |
|
|
|
|
|
static void destroy_async_data(struct Curl_async *async); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Curl_resolver_cancel(struct Curl_easy *data) |
|
|
{ |
|
|
DEBUGASSERT(data); |
|
|
if(data->state.async.resolver) |
|
|
ares_cancel((ares_channel)data->state.async.resolver); |
|
|
destroy_async_data(&data->state.async); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Curl_resolver_kill(struct Curl_easy *data) |
|
|
{ |
|
|
|
|
|
|
|
|
Curl_resolver_cancel(data); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void destroy_async_data(struct Curl_async *async) |
|
|
{ |
|
|
if(async->tdata) { |
|
|
struct thread_data *res = async->tdata; |
|
|
if(res) { |
|
|
if(res->temp_ai) { |
|
|
Curl_freeaddrinfo(res->temp_ai); |
|
|
res->temp_ai = NULL; |
|
|
} |
|
|
free(res); |
|
|
} |
|
|
async->tdata = NULL; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Curl_resolver_getsock(struct Curl_easy *data, |
|
|
curl_socket_t *socks) |
|
|
{ |
|
|
struct timeval maxtime = { CURL_TIMEOUT_RESOLVE, 0 }; |
|
|
struct timeval timebuf; |
|
|
int max = ares_getsock((ares_channel)data->state.async.resolver, |
|
|
(ares_socket_t *)socks, MAX_SOCKSPEREASYHANDLE); |
|
|
struct timeval *timeout = |
|
|
ares_timeout((ares_channel)data->state.async.resolver, &maxtime, &timebuf); |
|
|
timediff_t milli = curlx_tvtoms(timeout); |
|
|
Curl_expire(data, milli, EXPIRE_ASYNC_NAME); |
|
|
return max; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int waitperform(struct Curl_easy *data, timediff_t timeout_ms) |
|
|
{ |
|
|
int nfds; |
|
|
int bitmask; |
|
|
ares_socket_t socks[ARES_GETSOCK_MAXNUM]; |
|
|
struct pollfd pfd[ARES_GETSOCK_MAXNUM]; |
|
|
int i; |
|
|
int num = 0; |
|
|
|
|
|
bitmask = ares_getsock((ares_channel)data->state.async.resolver, socks, |
|
|
ARES_GETSOCK_MAXNUM); |
|
|
|
|
|
for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) { |
|
|
pfd[i].events = 0; |
|
|
pfd[i].revents = 0; |
|
|
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; |
|
|
} |
|
|
if(pfd[i].events) |
|
|
num++; |
|
|
else |
|
|
break; |
|
|
} |
|
|
|
|
|
if(num) { |
|
|
nfds = Curl_poll(pfd, (unsigned int)num, timeout_ms); |
|
|
if(nfds < 0) |
|
|
return -1; |
|
|
} |
|
|
else |
|
|
nfds = 0; |
|
|
|
|
|
if(!nfds) |
|
|
|
|
|
|
|
|
ares_process_fd((ares_channel)data->state.async.resolver, ARES_SOCKET_BAD, |
|
|
ARES_SOCKET_BAD); |
|
|
else { |
|
|
|
|
|
for(i = 0; i < num; i++) |
|
|
ares_process_fd((ares_channel)data->state.async.resolver, |
|
|
(pfd[i].revents & (POLLRDNORM|POLLIN)) ? |
|
|
pfd[i].fd : ARES_SOCKET_BAD, |
|
|
(pfd[i].revents & (POLLWRNORM|POLLOUT)) ? |
|
|
pfd[i].fd : ARES_SOCKET_BAD); |
|
|
} |
|
|
return nfds; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CURLcode Curl_resolver_is_resolved(struct Curl_easy *data, |
|
|
struct Curl_dns_entry **dns) |
|
|
{ |
|
|
struct thread_data *res = data->state.async.tdata; |
|
|
CURLcode result = CURLE_OK; |
|
|
|
|
|
DEBUGASSERT(dns); |
|
|
*dns = NULL; |
|
|
|
|
|
if(waitperform(data, 0) < 0) |
|
|
return CURLE_UNRECOVERABLE_POLL; |
|
|
|
|
|
#ifndef HAVE_CARES_GETADDRINFO |
|
|
|
|
|
|
|
|
|
|
|
if(res |
|
|
&& res->num_pending |
|
|
|
|
|
&& (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)) { |
|
|
|
|
|
|
|
|
memset( |
|
|
&res->happy_eyeballs_dns_time, 0, sizeof(res->happy_eyeballs_dns_time)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ares_cancel((ares_channel)data->state.async.resolver); |
|
|
DEBUGASSERT(res->num_pending == 0); |
|
|
} |
|
|
#endif |
|
|
|
|
|
if(res && !res->num_pending) { |
|
|
(void)Curl_addrinfo_callback(data, res->last_status, res->temp_ai); |
|
|
|
|
|
|
|
|
res->temp_ai = NULL; |
|
|
|
|
|
if(!data->state.async.dns) |
|
|
result = Curl_resolver_error(data); |
|
|
else |
|
|
*dns = data->state.async.dns; |
|
|
|
|
|
destroy_async_data(&data->state.async); |
|
|
} |
|
|
|
|
|
return result; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data, |
|
|
struct Curl_dns_entry **entry) |
|
|
{ |
|
|
CURLcode result = CURLE_OK; |
|
|
timediff_t timeout; |
|
|
struct curltime now = Curl_now(); |
|
|
|
|
|
DEBUGASSERT(entry); |
|
|
*entry = NULL; |
|
|
|
|
|
timeout = Curl_timeleft(data, &now, TRUE); |
|
|
if(timeout < 0) { |
|
|
|
|
|
connclose(data->conn, "Timed out before name resolve started"); |
|
|
return CURLE_OPERATION_TIMEDOUT; |
|
|
} |
|
|
if(!timeout) |
|
|
timeout = CURL_TIMEOUT_RESOLVE * 1000; |
|
|
|
|
|
|
|
|
while(!result) { |
|
|
struct timeval *tvp, tv, store; |
|
|
int itimeout; |
|
|
timediff_t timeout_ms; |
|
|
|
|
|
#if TIMEDIFF_T_MAX > INT_MAX |
|
|
itimeout = (timeout > INT_MAX) ? INT_MAX : (int)timeout; |
|
|
#else |
|
|
itimeout = (int)timeout; |
|
|
#endif |
|
|
|
|
|
store.tv_sec = itimeout/1000; |
|
|
store.tv_usec = (itimeout%1000)*1000; |
|
|
|
|
|
tvp = ares_timeout((ares_channel)data->state.async.resolver, &store, &tv); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!tvp->tv_sec) |
|
|
timeout_ms = (timediff_t)(tvp->tv_usec/1000); |
|
|
else |
|
|
timeout_ms = 1000; |
|
|
|
|
|
if(waitperform(data, timeout_ms) < 0) |
|
|
return CURLE_UNRECOVERABLE_POLL; |
|
|
result = Curl_resolver_is_resolved(data, entry); |
|
|
|
|
|
if(result || data->state.async.done) |
|
|
break; |
|
|
|
|
|
if(Curl_pgrsUpdate(data)) |
|
|
result = CURLE_ABORTED_BY_CALLBACK; |
|
|
else { |
|
|
struct curltime now2 = Curl_now(); |
|
|
timediff_t timediff = Curl_timediff(now2, now); |
|
|
if(timediff <= 0) |
|
|
timeout -= 1; |
|
|
else if(timediff > timeout) |
|
|
timeout = -1; |
|
|
else |
|
|
timeout -= timediff; |
|
|
now = now2; |
|
|
} |
|
|
if(timeout < 0) |
|
|
result = CURLE_OPERATION_TIMEDOUT; |
|
|
} |
|
|
if(result) |
|
|
|
|
|
ares_cancel((ares_channel)data->state.async.resolver); |
|
|
|
|
|
|
|
|
|
|
|
if(entry) |
|
|
*entry = data->state.async.dns; |
|
|
|
|
|
if(result) |
|
|
|
|
|
|
|
|
connclose(data->conn, "c-ares resolve failed"); |
|
|
|
|
|
return result; |
|
|
} |
|
|
|
|
|
#ifndef HAVE_CARES_GETADDRINFO |
|
|
|
|
|
|
|
|
static void compound_results(struct thread_data *res, |
|
|
struct Curl_addrinfo *ai) |
|
|
{ |
|
|
if(!ai) |
|
|
return; |
|
|
|
|
|
#ifdef USE_IPV6 |
|
|
if(res->temp_ai && res->temp_ai->ai_family == PF_INET6) { |
|
|
|
|
|
|
|
|
struct Curl_addrinfo *temp_ai_tail = res->temp_ai; |
|
|
|
|
|
while(temp_ai_tail->ai_next) |
|
|
temp_ai_tail = temp_ai_tail->ai_next; |
|
|
|
|
|
temp_ai_tail->ai_next = ai; |
|
|
} |
|
|
else |
|
|
#endif |
|
|
{ |
|
|
|
|
|
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; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void query_completed_cb(void *arg, |
|
|
int status, |
|
|
#ifdef HAVE_CARES_CALLBACK_TIMEOUTS |
|
|
int timeouts, |
|
|
#endif |
|
|
struct hostent *hostent) |
|
|
{ |
|
|
struct Curl_easy *data = (struct Curl_easy *)arg; |
|
|
struct thread_data *res; |
|
|
|
|
|
#ifdef HAVE_CARES_CALLBACK_TIMEOUTS |
|
|
(void)timeouts; |
|
|
#endif |
|
|
|
|
|
if(ARES_EDESTRUCTION == status) |
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
res = data->state.async.tdata; |
|
|
if(res) { |
|
|
res->num_pending--; |
|
|
|
|
|
if(CURL_ASYNC_SUCCESS == status) { |
|
|
struct Curl_addrinfo *ai = Curl_he2ai(hostent, data->state.async.port); |
|
|
if(ai) { |
|
|
compound_results(res, ai); |
|
|
} |
|
|
} |
|
|
|
|
|
if(res->last_status != ARES_SUCCESS) |
|
|
res->last_status = status; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(res->num_pending |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
&& (status == ARES_SUCCESS || status == ARES_ENOTFOUND)) { |
|
|
|
|
|
|
|
|
DEBUGASSERT(res->num_pending == 1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res->happy_eyeballs_dns_time = Curl_now(); |
|
|
Curl_expire(data, HAPPY_EYEBALLS_DNS_TIMEOUT, |
|
|
EXPIRE_HAPPY_EYEBALLS_DNS); |
|
|
} |
|
|
} |
|
|
} |
|
|
#else |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static struct Curl_addrinfo *ares2addr(struct ares_addrinfo_node *node) |
|
|
{ |
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
if(ai->ai_family == AF_INET) |
|
|
ss_size = sizeof(struct sockaddr_in); |
|
|
#ifdef USE_IPV6 |
|
|
else if(ai->ai_family == AF_INET6) |
|
|
ss_size = sizeof(struct sockaddr_in6); |
|
|
#endif |
|
|
else |
|
|
continue; |
|
|
|
|
|
|
|
|
if(!ai->ai_addr || !(ai->ai_addrlen > 0)) |
|
|
continue; |
|
|
|
|
|
|
|
|
if((size_t)ai->ai_addrlen < ss_size) |
|
|
continue; |
|
|
|
|
|
ca = malloc(sizeof(struct Curl_addrinfo) + ss_size); |
|
|
if(!ca) { |
|
|
error = EAI_MEMORY; |
|
|
break; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(!cafirst) |
|
|
cafirst = ca; |
|
|
|
|
|
|
|
|
if(calast) |
|
|
calast->ai_next = ca; |
|
|
calast = ca; |
|
|
} |
|
|
|
|
|
|
|
|
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; |
|
|
ares_freeaddrinfo(result); |
|
|
} |
|
|
res->num_pending--; |
|
|
} |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data, |
|
|
const char *hostname, |
|
|
int port, |
|
|
int *waitp) |
|
|
{ |
|
|
struct thread_data *res = NULL; |
|
|
size_t namelen = strlen(hostname); |
|
|
*waitp = 0; |
|
|
|
|
|
res = calloc(1, sizeof(struct thread_data) + namelen); |
|
|
if(res) { |
|
|
strcpy(res->hostname, hostname); |
|
|
data->state.async.hostname = res->hostname; |
|
|
data->state.async.port = port; |
|
|
data->state.async.done = FALSE; |
|
|
data->state.async.status = 0; |
|
|
data->state.async.dns = NULL; |
|
|
data->state.async.tdata = res; |
|
|
|
|
|
|
|
|
res->last_status = ARES_ENOTFOUND; |
|
|
|
|
|
#ifdef HAVE_CARES_GETADDRINFO |
|
|
{ |
|
|
struct ares_addrinfo_hints hints; |
|
|
char service[12]; |
|
|
int pf = PF_INET; |
|
|
memset(&hints, 0, sizeof(hints)); |
|
|
#ifdef CURLRES_IPV6 |
|
|
if((data->conn->ip_version != CURL_IPRESOLVE_V4) && |
|
|
Curl_ipv6works(data)) { |
|
|
|
|
|
if(data->conn->ip_version == CURL_IPRESOLVE_V6) |
|
|
pf = PF_INET6; |
|
|
else |
|
|
pf = PF_UNSPEC; |
|
|
} |
|
|
#endif |
|
|
hints.ai_family = pf; |
|
|
hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP) ? |
|
|
SOCK_STREAM : SOCK_DGRAM; |
|
|
|
|
|
|
|
|
|
|
|
hints.ai_flags = ARES_AI_NUMERICSERV; |
|
|
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 |
|
|
|
|
|
#ifdef HAVE_CARES_IPV6 |
|
|
if((data->conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) { |
|
|
|
|
|
res->num_pending = 2; |
|
|
|
|
|
|
|
|
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); |
|
|
} |
|
|
else |
|
|
#endif |
|
|
{ |
|
|
res->num_pending = 1; |
|
|
|
|
|
|
|
|
ares_gethostbyname((ares_channel)data->state.async.resolver, |
|
|
hostname, PF_INET, |
|
|
query_completed_cb, data); |
|
|
} |
|
|
#endif |
|
|
*waitp = 1; |
|
|
} |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
CURLcode Curl_set_dns_servers(struct Curl_easy *data, |
|
|
char *servers) |
|
|
{ |
|
|
CURLcode result = CURLE_NOT_BUILT_IN; |
|
|
int ares_result; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!(servers && servers[0])) |
|
|
return CURLE_OK; |
|
|
|
|
|
#ifdef HAVE_CARES_SERVERS_CSV |
|
|
#ifdef HAVE_CARES_PORTS_CSV |
|
|
ares_result = ares_set_servers_ports_csv(data->state.async.resolver, |
|
|
servers); |
|
|
#else |
|
|
ares_result = ares_set_servers_csv(data->state.async.resolver, servers); |
|
|
#endif |
|
|
switch(ares_result) { |
|
|
case ARES_SUCCESS: |
|
|
result = CURLE_OK; |
|
|
break; |
|
|
case ARES_ENOMEM: |
|
|
result = CURLE_OUT_OF_MEMORY; |
|
|
break; |
|
|
case ARES_ENOTINITIALIZED: |
|
|
case ARES_ENODATA: |
|
|
case ARES_EBADSTR: |
|
|
default: |
|
|
DEBUGF(infof(data, "bad servers set")); |
|
|
result = CURLE_BAD_FUNCTION_ARGUMENT; |
|
|
break; |
|
|
} |
|
|
#else |
|
|
(void)data; |
|
|
(void)(ares_result); |
|
|
#endif |
|
|
return result; |
|
|
} |
|
|
|
|
|
CURLcode Curl_set_dns_interface(struct Curl_easy *data, |
|
|
const char *interf) |
|
|
{ |
|
|
#ifdef HAVE_CARES_LOCAL_DEV |
|
|
if(!interf) |
|
|
interf = ""; |
|
|
|
|
|
ares_set_local_dev((ares_channel)data->state.async.resolver, interf); |
|
|
|
|
|
return CURLE_OK; |
|
|
#else |
|
|
(void)data; |
|
|
(void)interf; |
|
|
return CURLE_NOT_BUILT_IN; |
|
|
#endif |
|
|
} |
|
|
|
|
|
CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data, |
|
|
const char *local_ip4) |
|
|
{ |
|
|
#ifdef HAVE_CARES_SET_LOCAL |
|
|
struct in_addr a4; |
|
|
|
|
|
if((!local_ip4) || (local_ip4[0] == 0)) { |
|
|
a4.s_addr = 0; |
|
|
} |
|
|
else { |
|
|
if(Curl_inet_pton(AF_INET, local_ip4, &a4) != 1) { |
|
|
DEBUGF(infof(data, "bad DNS IPv4 address")); |
|
|
return CURLE_BAD_FUNCTION_ARGUMENT; |
|
|
} |
|
|
} |
|
|
|
|
|
ares_set_local_ip4((ares_channel)data->state.async.resolver, |
|
|
ntohl(a4.s_addr)); |
|
|
|
|
|
return CURLE_OK; |
|
|
#else |
|
|
(void)data; |
|
|
(void)local_ip4; |
|
|
return CURLE_NOT_BUILT_IN; |
|
|
#endif |
|
|
} |
|
|
|
|
|
CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data, |
|
|
const char *local_ip6) |
|
|
{ |
|
|
#if defined(HAVE_CARES_SET_LOCAL) && defined(USE_IPV6) |
|
|
unsigned char a6[INET6_ADDRSTRLEN]; |
|
|
|
|
|
if((!local_ip6) || (local_ip6[0] == 0)) { |
|
|
|
|
|
memset(a6, 0, sizeof(a6)); |
|
|
} |
|
|
else { |
|
|
if(Curl_inet_pton(AF_INET6, local_ip6, a6) != 1) { |
|
|
DEBUGF(infof(data, "bad DNS IPv6 address")); |
|
|
return CURLE_BAD_FUNCTION_ARGUMENT; |
|
|
} |
|
|
} |
|
|
|
|
|
ares_set_local_ip6((ares_channel)data->state.async.resolver, a6); |
|
|
|
|
|
return CURLE_OK; |
|
|
#else |
|
|
(void)data; |
|
|
(void)local_ip6; |
|
|
return CURLE_NOT_BUILT_IN; |
|
|
#endif |
|
|
} |
|
|
#endif |
|
|
|