2004-04-26 03:20:11 -04:00
|
|
|
/***************************************************************************
|
2004-06-24 03:43:48 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2004-04-26 03:20:11 -04:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2011-01-04 17:07:58 -05:00
|
|
|
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2004-04-26 03:20:11 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
2004-06-24 03:43:48 -04:00
|
|
|
*
|
2004-04-26 03:20:11 -04: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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
|
2009-04-14 09:50:32 -04:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
2004-04-26 03:20:11 -04:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#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 HAVE_UNISTD_H
|
|
|
|
#include <unistd.h> /* for the close() proto */
|
|
|
|
#endif
|
2009-12-30 12:59:56 -05:00
|
|
|
#ifdef __VMS
|
2004-04-26 03:20:11 -04:00
|
|
|
#include <in.h>
|
|
|
|
#include <inet.h>
|
|
|
|
#endif
|
|
|
|
|
2006-04-26 13:23:28 -04:00
|
|
|
#ifdef HAVE_PROCESS_H
|
2004-04-26 03:20:11 -04:00
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
|
|
|
|
#undef in_addr_t
|
|
|
|
#define in_addr_t unsigned long
|
|
|
|
#endif
|
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
/***********************************************************************
|
|
|
|
* Only for ares-enabled builds
|
2011-01-30 18:10:35 -05:00
|
|
|
* And only for functions that fulfill the asynch resolver backend API
|
|
|
|
* as defined in asyn.h, nothing else belongs in this file!
|
2011-01-29 14:12:10 -05:00
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#ifdef CURLRES_ARES
|
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "share.h"
|
|
|
|
#include "strerror.h"
|
|
|
|
#include "url.h"
|
2006-10-17 04:06:27 -04:00
|
|
|
#include "multiif.h"
|
2008-08-20 19:32:50 -04:00
|
|
|
#include "inet_pton.h"
|
2007-02-16 13:19:35 -05:00
|
|
|
#include "connect.h"
|
2007-03-27 14:15:26 -04:00
|
|
|
#include "select.h"
|
2010-01-06 18:04:17 -05:00
|
|
|
#include "progress.h"
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
# if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
|
|
|
|
(defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__))
|
|
|
|
# define CARES_STATICLIB
|
|
|
|
# endif
|
|
|
|
# include <ares.h>
|
|
|
|
|
|
|
|
#if ARES_VERSION >= 0x010500
|
|
|
|
/* c-ares 1.5.0 or later, the callback proto is modified */
|
|
|
|
#define HAVE_CARES_CALLBACK_TIMEOUTS 1
|
|
|
|
#endif
|
|
|
|
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2004-04-26 03:20:11 -04:00
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
struct ResolverResults {
|
|
|
|
int num_pending; /* number of ares_gethostbyname() requests */
|
|
|
|
Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares parts */
|
|
|
|
int last_status;
|
|
|
|
};
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
/*
|
2011-01-29 19:00:52 -05: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-29 14:12:10 -05:00
|
|
|
*/
|
2011-01-29 19:00:52 -05:00
|
|
|
int Curl_resolver_global_init(void)
|
2011-01-29 14:12:10 -05: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-29 19:00:52 -05:00
|
|
|
* Curl_resolver_global_cleanup()
|
|
|
|
*
|
2011-01-29 14:12:10 -05:00
|
|
|
* Called from curl_global_cleanup() to destroy global resolver environment.
|
|
|
|
* Deinitializes ares library.
|
|
|
|
*/
|
2011-01-29 19:00:52 -05:00
|
|
|
void Curl_resolver_global_cleanup(void)
|
2011-01-29 14:12:10 -05:00
|
|
|
{
|
|
|
|
#ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
|
|
|
|
ares_library_cleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-29 19:00:52 -05: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-29 14:12:10 -05:00
|
|
|
*/
|
2011-05-21 08:10:17 -04:00
|
|
|
CURLcode Curl_resolver_init(void **resolver)
|
2011-01-29 14:12:10 -05:00
|
|
|
{
|
|
|
|
int status = ares_init((ares_channel*)resolver);
|
|
|
|
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-29 19:00:52 -05: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-29 14:12:10 -05:00
|
|
|
*/
|
|
|
|
void Curl_resolver_cleanup(void *resolver)
|
|
|
|
{
|
|
|
|
ares_destroy((ares_channel)resolver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-29 19:00:52 -05: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-29 14:12:10 -05:00
|
|
|
*/
|
|
|
|
int Curl_resolver_duphandle(void **to, void *from)
|
|
|
|
{
|
|
|
|
/* Clone the ares channel for the new handle */
|
|
|
|
if(ARES_SUCCESS != ares_dup((ares_channel*)to,(ares_channel)from))
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_async_data (struct Curl_async *async);
|
2011-01-30 18:10:35 -05:00
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
/*
|
|
|
|
* Cancel all possibly still on-going resolves for this connection.
|
|
|
|
*/
|
2011-01-30 18:10:35 -05:00
|
|
|
void Curl_resolver_cancel(struct connectdata *conn)
|
2011-01-29 14:12:10 -05:00
|
|
|
{
|
2011-04-22 17:01:30 -04:00
|
|
|
if(conn && conn->data && conn->data->state.resolver)
|
2011-01-29 14:12:10 -05:00
|
|
|
ares_cancel((ares_channel)conn->data->state.resolver);
|
|
|
|
destroy_async_data(&conn->async);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* destroy_async_data() cleans up async resolver data.
|
|
|
|
*/
|
|
|
|
static void destroy_async_data (struct Curl_async *async)
|
|
|
|
{
|
|
|
|
if(async->hostname)
|
|
|
|
free(async->hostname);
|
|
|
|
|
|
|
|
if(async->os_specific) {
|
|
|
|
struct ResolverResults *res = (struct ResolverResults *)async->os_specific;
|
2011-04-22 17:01:30 -04:00
|
|
|
if(res) {
|
|
|
|
if(res->temp_ai) {
|
2011-01-29 19:00:52 -05:00
|
|
|
Curl_freeaddrinfo(res->temp_ai);
|
|
|
|
res->temp_ai = NULL;
|
|
|
|
}
|
|
|
|
free(res);
|
2011-01-29 14:12:10 -05:00
|
|
|
}
|
|
|
|
async->os_specific = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
async->hostname = NULL;
|
|
|
|
}
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
/*
|
2011-01-30 18:10:35 -05:00
|
|
|
* Curl_resolver_fdset() is called when someone from the outside world (using
|
2004-04-26 03:20:11 -04:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Returns: CURLE_OK always!
|
|
|
|
*/
|
|
|
|
|
2011-01-30 18:10:35 -05:00
|
|
|
int Curl_resolver_getsock(struct connectdata *conn,
|
|
|
|
curl_socket_t *socks,
|
|
|
|
int numsocks)
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
{
|
2006-10-17 04:06:27 -04:00
|
|
|
struct timeval maxtime;
|
2007-10-03 09:19:34 -04:00
|
|
|
struct timeval timebuf;
|
|
|
|
struct timeval *timeout;
|
2011-01-29 14:12:10 -05:00
|
|
|
int max = ares_getsock((ares_channel)conn->data->state.resolver,
|
2010-03-11 13:53:42 -05:00
|
|
|
(ares_socket_t *)socks, numsocks);
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2006-10-17 04:06:27 -04:00
|
|
|
|
|
|
|
maxtime.tv_sec = CURL_TIMEOUT_RESOLVE;
|
|
|
|
maxtime.tv_usec = 0;
|
|
|
|
|
2011-01-29 19:00:52 -05:00
|
|
|
timeout = ares_timeout((ares_channel)conn->data->state.resolver, &maxtime,
|
|
|
|
&timebuf);
|
2006-10-17 04:06:27 -04:00
|
|
|
|
|
|
|
Curl_expire(conn->data,
|
2007-10-03 09:19:34 -04:00
|
|
|
(timeout->tv_sec * 1000) + (timeout->tv_usec/1000));
|
2006-10-17 04:06:27 -04:00
|
|
|
|
2006-04-10 11:00:53 -04:00
|
|
|
return max;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
2007-05-31 07:34:32 -04:00
|
|
|
/*
|
2010-01-06 18:04:17 -05:00
|
|
|
* waitperform()
|
2007-05-31 07:34:32 -04: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"
|
|
|
|
*
|
|
|
|
* return number of sockets it worked on
|
|
|
|
*/
|
|
|
|
|
2010-01-06 18:04:17 -05:00
|
|
|
static int waitperform(struct connectdata *conn, int timeout_ms)
|
2007-05-31 07:34:32 -04:00
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
int nfds;
|
|
|
|
int bitmask;
|
2010-03-11 13:53:42 -05:00
|
|
|
ares_socket_t socks[ARES_GETSOCK_MAXNUM];
|
2007-05-31 07:34:32 -04:00
|
|
|
struct pollfd pfd[ARES_GETSOCK_MAXNUM];
|
|
|
|
int i;
|
2008-04-29 00:18:02 -04:00
|
|
|
int num = 0;
|
2007-05-31 07:34:32 -04:00
|
|
|
|
2011-01-29 19:00:52 -05:00
|
|
|
bitmask = ares_getsock((ares_channel)data->state.resolver, socks,
|
|
|
|
ARES_GETSOCK_MAXNUM);
|
2007-05-31 07:34:32 -04:00
|
|
|
|
|
|
|
for(i=0; i < ARES_GETSOCK_MAXNUM; i++) {
|
|
|
|
pfd[i].events = 0;
|
2008-04-29 00:18:02 -04:00
|
|
|
pfd[i].revents = 0;
|
2007-05-31 07:34:32 -04: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;
|
|
|
|
}
|
2008-04-29 00:18:02 -04:00
|
|
|
if(pfd[i].events != 0)
|
|
|
|
num++;
|
|
|
|
else
|
2007-05-31 07:34:32 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(num)
|
|
|
|
nfds = Curl_poll(pfd, num, timeout_ms);
|
|
|
|
else
|
|
|
|
nfds = 0;
|
|
|
|
|
|
|
|
if(!nfds)
|
|
|
|
/* Call ares_process() unconditonally here, even if we simply timed out
|
|
|
|
above, as otherwise the ares name resolve won't timeout! */
|
2011-01-29 19:00:52 -05:00
|
|
|
ares_process_fd((ares_channel)data->state.resolver, ARES_SOCKET_BAD,
|
|
|
|
ARES_SOCKET_BAD);
|
2007-05-31 07:34:32 -04:00
|
|
|
else {
|
|
|
|
/* move through the descriptors and ask for processing on them */
|
|
|
|
for(i=0; i < num; i++)
|
2011-01-29 14:12:10 -05:00
|
|
|
ares_process_fd((ares_channel)data->state.resolver,
|
2009-09-14 20:07:25 -04:00
|
|
|
pfd[i].revents & (POLLRDNORM|POLLIN)?
|
2007-05-31 07:34:32 -04:00
|
|
|
pfd[i].fd:ARES_SOCKET_BAD,
|
2009-09-14 20:07:25 -04:00
|
|
|
pfd[i].revents & (POLLWRNORM|POLLOUT)?
|
2007-05-31 07:34:32 -04:00
|
|
|
pfd[i].fd:ARES_SOCKET_BAD);
|
|
|
|
}
|
|
|
|
return nfds;
|
|
|
|
}
|
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
/*
|
2011-01-30 18:10:35 -05: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 03:20:11 -04:00
|
|
|
*
|
|
|
|
* Returns normal CURLcode errors.
|
|
|
|
*/
|
2011-01-30 18:10:35 -05:00
|
|
|
CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
|
|
|
|
struct Curl_dns_entry **dns)
|
2004-04-26 03:20:11 -04:00
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
2011-01-29 19:00:52 -05:00
|
|
|
struct ResolverResults *res = (struct ResolverResults *)
|
|
|
|
conn->async.os_specific;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
*dns = NULL;
|
|
|
|
|
2010-01-06 18:04:17 -05:00
|
|
|
waitperform(conn, 0);
|
2007-05-31 07:34:32 -04:00
|
|
|
|
2011-04-22 17:01:30 -04:00
|
|
|
if(res && !res->num_pending) {
|
2011-01-29 14:12:10 -05:00
|
|
|
(void)Curl_addrinfo_callback(conn, res->last_status, res->temp_ai);
|
2011-01-29 19:00:52 -05:00
|
|
|
/* temp_ai ownership is moved to the connection, so we need not free-up
|
|
|
|
them */
|
2011-01-29 14:12:10 -05:00
|
|
|
res->temp_ai = NULL;
|
|
|
|
destroy_async_data(&conn->async);
|
2004-05-06 07:10:51 -04:00
|
|
|
if(!conn->async.dns) {
|
2004-05-07 02:18:47 -04:00
|
|
|
failf(data, "Could not resolve host: %s (%s)", conn->host.dispname,
|
2004-05-06 07:10:51 -04:00
|
|
|
ares_strerror(conn->async.status));
|
2004-04-26 03:20:11 -04:00
|
|
|
return CURLE_COULDNT_RESOLVE_HOST;
|
2004-05-06 07:10:51 -04:00
|
|
|
}
|
2004-04-26 03:20:11 -04:00
|
|
|
*dns = conn->async.dns;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-30 18:10:35 -05:00
|
|
|
* Curl_resolver_wait_resolv()
|
|
|
|
*
|
|
|
|
* waits for a resolve to finish. This function should be avoided since using
|
|
|
|
* this risk getting the multi interface to "hang".
|
2004-04-26 03:20:11 -04:00
|
|
|
*
|
|
|
|
* If 'entry' is non-NULL, make it point to the resolved dns entry
|
|
|
|
*
|
|
|
|
* Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved, and
|
|
|
|
* CURLE_OPERATION_TIMEDOUT if a time-out occurred.
|
|
|
|
*/
|
2011-01-30 18:10:35 -05:00
|
|
|
CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
|
|
|
|
struct Curl_dns_entry **entry)
|
2004-04-26 03:20:11 -04:00
|
|
|
{
|
|
|
|
CURLcode rc=CURLE_OK;
|
|
|
|
struct SessionHandle *data = conn->data;
|
2007-02-05 17:51:32 -05:00
|
|
|
long timeout;
|
2008-04-29 00:18:02 -04:00
|
|
|
struct timeval now = Curl_tvnow();
|
2011-01-29 14:12:10 -05:00
|
|
|
struct Curl_dns_entry *temp_entry;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2011-01-04 17:07:58 -05:00
|
|
|
timeout = Curl_timeleft(data, &now, TRUE);
|
2010-12-05 17:39:41 -05:00
|
|
|
if(!timeout)
|
2007-02-05 17:51:32 -05:00
|
|
|
timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
/* Wait for the name resolve query to complete. */
|
2010-11-07 22:03:11 -05:00
|
|
|
for(;;) {
|
2004-04-26 03:20:11 -04:00
|
|
|
struct timeval *tvp, tv, store;
|
|
|
|
long timediff;
|
2008-04-29 00:18:02 -04:00
|
|
|
int itimeout;
|
2010-01-06 18:04:17 -05:00
|
|
|
int timeout_ms;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2008-04-29 00:18:02 -04:00
|
|
|
itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
|
|
|
|
|
|
|
|
store.tv_sec = itimeout/1000;
|
|
|
|
store.tv_usec = (itimeout%1000)*1000;
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
tvp = ares_timeout((ares_channel)data->state.resolver, &store, &tv);
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-06 18:04:17 -05: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)
|
2010-05-20 14:15:52 -04:00
|
|
|
timeout_ms = (int)(tvp->tv_usec/1000);
|
2010-01-06 18:04:17 -05:00
|
|
|
else
|
|
|
|
timeout_ms = 1000;
|
|
|
|
|
|
|
|
waitperform(conn, timeout_ms);
|
2011-01-30 18:10:35 -05:00
|
|
|
Curl_resolver_is_resolved(conn,&temp_entry);
|
2007-06-11 09:32:49 -04:00
|
|
|
|
|
|
|
if(conn->async.done)
|
2007-05-31 07:34:32 -04:00
|
|
|
break;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-06 18:04:17 -05:00
|
|
|
if(Curl_pgrsUpdate(conn)) {
|
|
|
|
rc = CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
timeout = -1; /* trigger the cancel below */
|
|
|
|
}
|
|
|
|
else {
|
2010-12-05 17:39:41 -05:00
|
|
|
struct timeval now2 = Curl_tvnow();
|
|
|
|
timediff = Curl_tvdiff(now2, now); /* spent time */
|
2010-01-06 18:04:17 -05:00
|
|
|
timeout -= timediff?timediff:1; /* always deduct at least 1 */
|
2010-12-05 17:39:41 -05:00
|
|
|
now = now2; /* for next loop */
|
2010-01-06 18:04:17 -05:00
|
|
|
}
|
2007-11-07 04:21:35 -05:00
|
|
|
if(timeout < 0) {
|
2004-04-26 03:20:11 -04:00
|
|
|
/* our timeout, so we cancel the ares operation */
|
2011-01-29 14:12:10 -05:00
|
|
|
ares_cancel((ares_channel)data->state.resolver);
|
2004-04-26 03:20:11 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Operation complete, if the lookup was successful we now have the entry
|
|
|
|
in the cache. */
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
if(entry)
|
|
|
|
*entry = conn->async.dns;
|
|
|
|
|
|
|
|
if(!conn->async.dns) {
|
|
|
|
/* a name was not resolved */
|
|
|
|
if((timeout < 0) || (conn->async.status == ARES_ETIMEOUT)) {
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->bits.httpproxy) {
|
2010-01-26 17:59:43 -05:00
|
|
|
failf(data, "Resolving proxy timed out: %s", conn->proxy.dispname);
|
|
|
|
rc = CURLE_COULDNT_RESOLVE_PROXY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
failf(data, "Resolving host timed out: %s", conn->host.dispname);
|
|
|
|
rc = CURLE_COULDNT_RESOLVE_HOST;
|
|
|
|
}
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
else if(conn->async.done) {
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->bits.httpproxy) {
|
2010-01-26 17:59:43 -05:00
|
|
|
failf(data, "Could not resolve proxy: %s (%s)", conn->proxy.dispname,
|
|
|
|
ares_strerror(conn->async.status));
|
|
|
|
rc = CURLE_COULDNT_RESOLVE_PROXY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
failf(data, "Could not resolve host: %s (%s)", conn->host.dispname,
|
|
|
|
ares_strerror(conn->async.status));
|
|
|
|
rc = CURLE_COULDNT_RESOLVE_HOST;
|
|
|
|
}
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
rc = CURLE_OPERATION_TIMEDOUT;
|
|
|
|
|
|
|
|
/* close the connection, since we can't return failure here without
|
|
|
|
cleaning up this connection properly */
|
2005-02-08 02:36:57 -05:00
|
|
|
conn->bits.close = TRUE;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
/* Connects results to the list */
|
2011-01-29 19:00:52 -05:00
|
|
|
static void compound_results(struct ResolverResults *res,
|
|
|
|
Curl_addrinfo *ai)
|
2011-01-29 14:12:10 -05:00
|
|
|
{
|
2011-01-29 19:00:52 -05:00
|
|
|
Curl_addrinfo *ai_tail;
|
|
|
|
if(!ai)
|
|
|
|
return;
|
|
|
|
ai_tail = ai;
|
2011-01-29 14:12:10 -05:00
|
|
|
|
2011-04-22 17:01:30 -04:00
|
|
|
while(ai_tail->ai_next)
|
2011-01-29 19:00:52 -05:00
|
|
|
ai_tail = ai_tail->ai_next;
|
2011-01-29 14:12:10 -05:00
|
|
|
|
2011-01-29 19:00:52 -05:00
|
|
|
/* Add the new results to the list of old results. */
|
|
|
|
ai_tail->ai_next = res->temp_ai;
|
|
|
|
res->temp_ai = ai;
|
2011-01-29 14:12:10 -05:00
|
|
|
}
|
|
|
|
|
2010-01-22 01:36:52 -05: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-29 19:00:52 -05:00
|
|
|
static void query_completed_cb(void *arg, /* (struct connectdata *) */
|
|
|
|
int status,
|
2010-01-22 01:36:52 -05:00
|
|
|
#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
|
2011-01-29 19:00:52 -05:00
|
|
|
int timeouts,
|
2010-01-22 01:36:52 -05:00
|
|
|
#endif
|
2011-01-29 19:00:52 -05:00
|
|
|
struct hostent *hostent)
|
2010-01-22 01:36:52 -05:00
|
|
|
{
|
|
|
|
struct connectdata *conn = (struct connectdata *)arg;
|
2011-01-29 19:00:52 -05:00
|
|
|
struct ResolverResults *res;
|
2010-01-26 17:59:43 -05:00
|
|
|
|
2010-01-22 01:36:52 -05:00
|
|
|
#ifdef HAVE_CARES_CALLBACK_TIMEOUTS
|
|
|
|
(void)timeouts; /* ignored */
|
|
|
|
#endif
|
|
|
|
|
2011-01-29 19:00:52 -05: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;
|
|
|
|
|
|
|
|
res = (struct ResolverResults *)conn->async.os_specific;
|
2011-01-29 14:12:10 -05:00
|
|
|
res->num_pending--;
|
|
|
|
|
2011-01-29 19:00:52 -05:00
|
|
|
if(CURL_ASYNC_SUCCESS == status) {
|
|
|
|
Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port);
|
|
|
|
if(ai) {
|
|
|
|
compound_results(res, ai);
|
|
|
|
}
|
2010-01-22 01:36:52 -05:00
|
|
|
}
|
2011-01-29 19:00:52 -05:00
|
|
|
/* A successful result overwrites any previous error */
|
2011-04-22 17:01:30 -04:00
|
|
|
if(res->last_status != ARES_SUCCESS)
|
2011-01-29 14:12:10 -05:00
|
|
|
res->last_status = status;
|
2010-01-22 01:36:52 -05:00
|
|
|
}
|
2008-07-09 14:39:49 -04:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
/*
|
2011-01-30 18:10:35 -05:00
|
|
|
* Curl_resolver_getaddrinfo() - when using ares
|
2004-04-26 03:20:11 -04:00
|
|
|
*
|
|
|
|
* Returns name information about the given hostname and port number. If
|
|
|
|
* successful, the 'hostent' is returned and the forth argument will point to
|
|
|
|
* memory we need to free after use. That memory *MUST* be freed with
|
|
|
|
* Curl_freeaddrinfo(), nothing else.
|
|
|
|
*/
|
2011-01-30 18:10:35 -05:00
|
|
|
Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
|
|
|
|
const char *hostname,
|
|
|
|
int port,
|
|
|
|
int *waitp)
|
2004-04-26 03:20:11 -04:00
|
|
|
{
|
|
|
|
char *bufp;
|
|
|
|
struct SessionHandle *data = conn->data;
|
2008-11-06 12:19:56 -05:00
|
|
|
struct in_addr in;
|
2008-07-09 14:39:49 -04:00
|
|
|
int family = PF_INET;
|
|
|
|
#ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
|
|
|
|
struct in6_addr in6;
|
|
|
|
#endif /* CURLRES_IPV6 */
|
2010-01-25 18:50:13 -05:00
|
|
|
|
|
|
|
*waitp = 0; /* default to synchronous response */
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2008-11-06 12:19:56 -05:00
|
|
|
/* First check if this is an IPv4 address string */
|
|
|
|
if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
|
2004-04-26 03:20:11 -04:00
|
|
|
/* This is a dotted IP address 123.123.123.123-style */
|
2008-11-06 12:19:56 -05:00
|
|
|
return Curl_ip2addr(AF_INET, &in, hostname, port);
|
2004-06-24 03:43:48 -04:00
|
|
|
}
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2008-07-09 14:39:49 -04:00
|
|
|
#ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
|
2008-11-06 12:19:56 -05:00
|
|
|
/* Otherwise, check if this is an IPv6 address string */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
|
2008-07-09 14:39:49 -04:00
|
|
|
/* This must be an IPv6 address literal. */
|
2008-11-06 12:19:56 -05:00
|
|
|
return Curl_ip2addr(AF_INET6, &in6, hostname, port);
|
2008-07-09 14:39:49 -04:00
|
|
|
|
2010-11-11 08:51:39 -05:00
|
|
|
switch(conn->ip_version) {
|
2009-01-31 15:25:55 -05:00
|
|
|
default:
|
|
|
|
#if ARES_VERSION >= 0x010601
|
|
|
|
family = PF_UNSPEC; /* supported by c-ares since 1.6.1, so for older
|
|
|
|
c-ares versions this just falls through and defaults
|
|
|
|
to PF_INET */
|
|
|
|
break;
|
|
|
|
#endif
|
2008-07-09 14:39:49 -04:00
|
|
|
case CURL_IPRESOLVE_V4:
|
|
|
|
family = PF_INET;
|
|
|
|
break;
|
|
|
|
case CURL_IPRESOLVE_V6:
|
|
|
|
family = PF_INET6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* CURLRES_IPV6 */
|
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
bufp = strdup(hostname);
|
|
|
|
if(bufp) {
|
2011-01-29 14:12:10 -05:00
|
|
|
struct ResolverResults *res = NULL;
|
2004-04-26 03:20:11 -04:00
|
|
|
Curl_safefree(conn->async.hostname);
|
|
|
|
conn->async.hostname = bufp;
|
|
|
|
conn->async.port = port;
|
2010-12-18 16:31:39 -05:00
|
|
|
conn->async.done = FALSE; /* not done */
|
|
|
|
conn->async.status = 0; /* clear */
|
|
|
|
conn->async.dns = NULL; /* clear */
|
2011-06-02 13:42:24 -04:00
|
|
|
res = calloc(sizeof(struct ResolverResults),1);
|
2011-04-22 17:01:30 -04:00
|
|
|
if(!res) {
|
2011-01-29 14:12:10 -05:00
|
|
|
Curl_safefree(conn->async.hostname);
|
|
|
|
conn->async.hostname = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
conn->async.os_specific = res;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
/* initial status - failed */
|
|
|
|
res->last_status = ARES_ENOTFOUND;
|
2010-12-18 16:31:39 -05:00
|
|
|
#ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
|
|
|
|
if(family == PF_UNSPEC) {
|
2011-01-29 14:12:10 -05:00
|
|
|
res->num_pending = 2;
|
2010-12-18 16:31:39 -05:00
|
|
|
|
|
|
|
/* areschannel is already setup in the Curl_open() function */
|
2011-04-22 17:01:30 -04:00
|
|
|
ares_gethostbyname((ares_channel)data->state.resolver, hostname,
|
|
|
|
PF_INET, query_completed_cb, conn);
|
|
|
|
ares_gethostbyname((ares_channel)data->state.resolver, hostname,
|
|
|
|
PF_INET6, query_completed_cb, conn);
|
2010-12-18 16:31:39 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* CURLRES_IPV6 */
|
|
|
|
{
|
2011-01-29 14:12:10 -05:00
|
|
|
res->num_pending = 1;
|
2010-12-18 16:31:39 -05:00
|
|
|
|
|
|
|
/* areschannel is already setup in the Curl_open() function */
|
2011-01-29 14:12:10 -05:00
|
|
|
ares_gethostbyname((ares_channel)data->state.resolver, hostname, family,
|
2011-01-29 19:00:52 -05:00
|
|
|
query_completed_cb, conn);
|
2010-12-18 16:31:39 -05:00
|
|
|
}
|
2004-06-24 03:43:48 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
*waitp = 1; /* expect asynchronous response */
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
return NULL; /* no struct yet */
|
|
|
|
}
|
|
|
|
#endif /* CURLRES_ARES */
|