2004-04-26 03:20:11 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2011-03-23 06:10:55 -04: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.
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
#if defined(USE_THREADS_POSIX)
|
|
|
|
# ifdef HAVE_PTHREAD_H
|
|
|
|
# include <pthread.h>
|
|
|
|
# endif
|
|
|
|
#elif defined(USE_THREADS_WIN32)
|
|
|
|
# ifdef HAVE_PROCESS_H
|
|
|
|
# include <process.h>
|
|
|
|
# endif
|
2004-04-26 03:20:11 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
|
|
|
|
#undef in_addr_t
|
|
|
|
#define in_addr_t unsigned long
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 07:07:49 -04:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
# define RESOLVER_ENOMEM EAI_MEMORY
|
|
|
|
#else
|
|
|
|
# define RESOLVER_ENOMEM ENOMEM
|
|
|
|
#endif
|
|
|
|
|
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-04-12 09:54:07 -04:00
|
|
|
#include "multiif.h"
|
2008-11-06 12:19:56 -05:00
|
|
|
#include "inet_pton.h"
|
2010-01-25 18:50:13 -05:00
|
|
|
#include "inet_ntop.h"
|
|
|
|
#include "curl_threads.h"
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
/***********************************************************************
|
2010-01-25 18:50:13 -05:00
|
|
|
* Only for threaded name resolves builds
|
2004-04-26 03:20:11 -04:00
|
|
|
**********************************************************************/
|
|
|
|
#ifdef CURLRES_THREADED
|
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
/*
|
2011-01-29 19:00:52 -05:00
|
|
|
* Curl_resolver_global_init()
|
2011-01-29 14:12:10 -05:00
|
|
|
* Called from curl_global_init() to initialize global resolver environment.
|
|
|
|
* Does nothing here.
|
|
|
|
*/
|
2011-01-29 19:00:52 -05:00
|
|
|
int Curl_resolver_global_init(void)
|
2011-01-29 14:12:10 -05:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
* Does nothing here.
|
|
|
|
*/
|
2011-01-29 19:00:52 -05:00
|
|
|
void Curl_resolver_global_cleanup(void)
|
2011-01-29 14:12:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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). Does nothing here.
|
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
|
|
|
{
|
|
|
|
(void)resolver;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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). Does nothing here.
|
2011-01-29 14:12:10 -05:00
|
|
|
*/
|
|
|
|
void Curl_resolver_cleanup(void *resolver)
|
|
|
|
{
|
|
|
|
(void)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). Does nothing
|
|
|
|
* here.
|
2011-01-29 14:12:10 -05:00
|
|
|
*/
|
|
|
|
int Curl_resolver_duphandle(void **to, void *from)
|
|
|
|
{
|
|
|
|
(void)to;
|
|
|
|
(void)from;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_async_data(struct Curl_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
|
|
|
{
|
|
|
|
destroy_async_data(&conn->async);
|
|
|
|
}
|
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
/* This function is used to init a threaded resolve */
|
|
|
|
static bool init_resolve_thread(struct connectdata *conn,
|
|
|
|
const char *hostname, int port,
|
2008-11-01 11:16:47 -04:00
|
|
|
const struct addrinfo *hints);
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
|
2005-08-29 10:23:53 -04:00
|
|
|
/* Data for synchronization between resolver thread and its parent */
|
|
|
|
struct thread_sync_data {
|
2010-01-25 18:50:13 -05:00
|
|
|
curl_mutex_t * mtx;
|
|
|
|
int done;
|
|
|
|
|
2005-08-29 10:23:53 -04:00
|
|
|
char * hostname; /* hostname to resolve, Curl_async.hostname
|
|
|
|
duplicate */
|
2010-01-25 18:50:13 -05:00
|
|
|
int port;
|
|
|
|
int sock_error;
|
|
|
|
Curl_addrinfo *res;
|
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
struct addrinfo hints;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct thread_data {
|
|
|
|
curl_thread_t thread_hnd;
|
|
|
|
curl_socket_t dummy_sock;
|
|
|
|
unsigned int poll_interval;
|
|
|
|
int interval_end;
|
|
|
|
struct thread_sync_data tsd;
|
2005-08-29 10:23:53 -04:00
|
|
|
};
|
|
|
|
|
2011-01-29 19:00:52 -05:00
|
|
|
static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
|
2010-01-25 18:50:13 -05:00
|
|
|
{
|
|
|
|
return &(((struct thread_data *)conn->async.os_specific)->tsd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
|
|
|
|
|
2005-08-29 10:23:53 -04:00
|
|
|
/* Destroy resolver thread synchronization data */
|
2005-11-24 15:39:00 -05:00
|
|
|
static
|
2005-08-29 10:23:53 -04:00
|
|
|
void destroy_thread_sync_data(struct thread_sync_data * tsd)
|
|
|
|
{
|
2011-04-20 09:17:42 -04:00
|
|
|
if(tsd->mtx) {
|
2010-01-25 18:50:13 -05:00
|
|
|
Curl_mutex_destroy(tsd->mtx);
|
|
|
|
free(tsd->mtx);
|
|
|
|
}
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(tsd->hostname)
|
2005-08-29 10:23:53 -04:00
|
|
|
free(tsd->hostname);
|
2010-02-13 13:29:59 -05:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(tsd->res)
|
2010-01-25 18:50:13 -05:00
|
|
|
Curl_freeaddrinfo(tsd->res);
|
|
|
|
|
2007-06-11 00:33:21 -04:00
|
|
|
memset(tsd,0,sizeof(*tsd));
|
2005-08-29 10:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize resolver thread synchronization data */
|
2005-11-24 15:39:00 -05:00
|
|
|
static
|
2010-01-25 18:50:13 -05:00
|
|
|
int init_thread_sync_data(struct thread_sync_data * tsd,
|
2007-06-11 00:33:21 -04:00
|
|
|
const char * hostname,
|
2010-02-13 13:29:59 -05:00
|
|
|
int port,
|
|
|
|
const struct addrinfo *hints)
|
2005-08-29 10:23:53 -04:00
|
|
|
{
|
2006-10-17 16:34:11 -04:00
|
|
|
memset(tsd, 0, sizeof(*tsd));
|
2010-01-25 18:50:13 -05:00
|
|
|
|
|
|
|
tsd->port = port;
|
|
|
|
#ifdef CURLRES_IPV6
|
|
|
|
DEBUGASSERT(hints);
|
|
|
|
tsd->hints = *hints;
|
|
|
|
#else
|
|
|
|
(void) hints;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tsd->mtx = malloc(sizeof(curl_mutex_t));
|
2011-04-20 09:17:42 -04:00
|
|
|
if(tsd->mtx == NULL)
|
|
|
|
goto err_exit;
|
2010-01-25 18:50:13 -05:00
|
|
|
|
|
|
|
Curl_mutex_init(tsd->mtx);
|
|
|
|
|
|
|
|
tsd->sock_error = CURL_ASYNC_SUCCESS;
|
|
|
|
|
2005-08-29 10:23:53 -04:00
|
|
|
/* Copying hostname string because original can be destroyed by parent
|
|
|
|
* thread during gethostbyname execution.
|
|
|
|
*/
|
|
|
|
tsd->hostname = strdup(hostname);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!tsd->hostname)
|
|
|
|
goto err_exit;
|
2005-08-29 10:23:53 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
return 1;
|
2005-08-29 10:23:53 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
err_exit:
|
|
|
|
/* Memory allocation failed */
|
|
|
|
destroy_thread_sync_data(tsd);
|
|
|
|
return 0;
|
2005-08-29 10:23:53 -04:00
|
|
|
}
|
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
static int getaddrinfo_complete(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct thread_sync_data *tsd = conn_thread_sync_data(conn);
|
|
|
|
int rc;
|
2004-10-16 08:59:05 -04:00
|
|
|
|
2010-02-13 13:29:59 -05:00
|
|
|
rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
|
2011-01-29 19:00:52 -05:00
|
|
|
/* The tsd->res structure has been copied to async.dns and perhaps the DNS
|
|
|
|
cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
|
|
|
|
*/
|
2010-01-25 18:50:13 -05:00
|
|
|
tsd->res = NULL;
|
2004-10-16 08:59:05 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
return rc;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
|
2010-02-13 13:39:47 -05:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
/*
|
2010-01-25 18:50:13 -05:00
|
|
|
* getaddrinfo_thread() resolves a name and then exits.
|
2004-04-26 03:20:11 -04:00
|
|
|
*
|
|
|
|
* For builds without ARES, but with ENABLE_IPV6, create a resolver thread
|
|
|
|
* and wait on it.
|
|
|
|
*/
|
2010-01-25 18:50:13 -05:00
|
|
|
static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
|
2004-04-26 03:20:11 -04:00
|
|
|
{
|
2010-01-25 18:50:13 -05:00
|
|
|
struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
|
2004-04-26 03:20:11 -04:00
|
|
|
char service [NI_MAXSERV];
|
2010-01-25 18:50:13 -05:00
|
|
|
int rc;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
snprintf(service, sizeof(service), "%d", tsd->port);
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
|
2004-10-16 08:59:05 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc != 0) {
|
2011-04-27 07:07:49 -04:00
|
|
|
tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(tsd->sock_error == 0)
|
2011-04-27 07:07:49 -04:00
|
|
|
tsd->sock_error = RESOLVER_ENOMEM;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
2004-10-16 08:59:05 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
Curl_mutex_acquire(tsd->mtx);
|
|
|
|
tsd->done = 1;
|
|
|
|
Curl_mutex_release(tsd->mtx);
|
2004-10-16 08:59:05 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
return 0;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
2010-01-25 18:50:13 -05:00
|
|
|
|
2010-02-13 13:39:47 -05:00
|
|
|
#else /* HAVE_GETADDRINFO */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* gethostbyname_thread() resolves a name and then exits.
|
|
|
|
*/
|
|
|
|
static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
|
|
|
|
{
|
|
|
|
struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
|
|
|
|
|
|
|
|
tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!tsd->res) {
|
2010-02-13 13:39:47 -05:00
|
|
|
tsd->sock_error = SOCKERRNO;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(tsd->sock_error == 0)
|
2011-04-27 07:07:49 -04:00
|
|
|
tsd->sock_error = RESOLVER_ENOMEM;
|
2010-02-13 13:39:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Curl_mutex_acquire(tsd->mtx);
|
|
|
|
tsd->done = 1;
|
|
|
|
Curl_mutex_release(tsd->mtx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
#endif /* HAVE_GETADDRINFO */
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
/*
|
2011-01-29 14:12:10 -05:00
|
|
|
* destroy_async_data() cleans up async resolver data and thread handle.
|
2004-04-26 03:20:11 -04:00
|
|
|
*/
|
2011-01-29 14:12:10 -05:00
|
|
|
static void destroy_async_data (struct Curl_async *async)
|
2004-04-26 03:20:11 -04:00
|
|
|
{
|
2007-11-05 04:45:09 -05:00
|
|
|
if(async->hostname)
|
2004-04-26 03:20:11 -04:00
|
|
|
free(async->hostname);
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(async->os_specific) {
|
2004-10-16 08:59:05 -04:00
|
|
|
struct thread_data *td = (struct thread_data*) async->os_specific;
|
2005-08-29 10:23:53 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(td->dummy_sock != CURL_SOCKET_BAD)
|
2010-01-25 18:50:13 -05:00
|
|
|
sclose(td->dummy_sock);
|
2005-06-14 10:47:21 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(td->thread_hnd != curl_thread_t_null)
|
2010-01-25 18:50:13 -05:00
|
|
|
Curl_thread_join(&td->thread_hnd);
|
2010-02-13 13:29:59 -05:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
destroy_thread_sync_data(&td->tsd);
|
2010-02-13 13:29:59 -05:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
free(async->os_specific);
|
|
|
|
}
|
|
|
|
async->hostname = NULL;
|
|
|
|
async->os_specific = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* init_resolve_thread() starts a new thread that performs the actual
|
|
|
|
* resolve. This function returns before the resolve is done.
|
|
|
|
*
|
|
|
|
* Returns FALSE in case of failure, otherwise TRUE.
|
|
|
|
*/
|
|
|
|
static bool init_resolve_thread (struct connectdata *conn,
|
|
|
|
const char *hostname, int port,
|
2008-11-01 11:16:47 -04:00
|
|
|
const struct addrinfo *hints)
|
2004-04-26 03:20:11 -04:00
|
|
|
{
|
2009-11-18 05:33:54 -05:00
|
|
|
struct thread_data *td = calloc(1, sizeof(struct thread_data));
|
2011-04-27 07:07:49 -04:00
|
|
|
int err = RESOLVER_ENOMEM;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
conn->async.os_specific = (void*) td;
|
2010-02-13 13:29:59 -05:00
|
|
|
if(!td)
|
2010-01-25 18:50:13 -05:00
|
|
|
goto err_exit;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
conn->async.port = port;
|
|
|
|
conn->async.done = FALSE;
|
|
|
|
conn->async.status = 0;
|
|
|
|
conn->async.dns = NULL;
|
|
|
|
td->dummy_sock = CURL_SOCKET_BAD;
|
2010-01-25 18:50:13 -05:00
|
|
|
td->thread_hnd = curl_thread_t_null;
|
2004-10-16 08:59:05 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!init_thread_sync_data(&td->tsd, hostname, port, hints))
|
2010-01-25 18:50:13 -05:00
|
|
|
goto err_exit;
|
2004-10-16 08:59:05 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
Curl_safefree(conn->async.hostname);
|
|
|
|
conn->async.hostname = strdup(hostname);
|
|
|
|
if(!conn->async.hostname)
|
|
|
|
goto err_exit;
|
2004-10-16 08:59:05 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
#ifdef WIN32
|
2011-01-30 18:10:35 -05:00
|
|
|
/* This socket is only to keep Curl_resolver_fdset() and select() happy;
|
2010-01-25 18:50:13 -05:00
|
|
|
* should never become signalled for read since it's unbound but
|
|
|
|
* Windows needs at least 1 socket in select().
|
|
|
|
*/
|
|
|
|
td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(td->dummy_sock == CURL_SOCKET_BAD)
|
2010-01-25 18:50:13 -05:00
|
|
|
goto err_exit;
|
2004-11-02 05:12:22 -05:00
|
|
|
#endif
|
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
|
2004-04-26 03:20:11 -04:00
|
|
|
#else
|
2010-01-25 18:50:13 -05:00
|
|
|
td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
|
2004-04-26 03:20:11 -04:00
|
|
|
#endif
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(!td->thread_hnd) {
|
2008-08-06 04:05:09 -04:00
|
|
|
#ifndef _WIN32_WCE
|
2010-01-25 18:50:13 -05:00
|
|
|
err = errno;
|
2006-07-07 03:49:16 -04:00
|
|
|
#endif
|
2010-01-25 18:50:13 -05:00
|
|
|
goto err_exit;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
2010-01-25 18:50:13 -05:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
return TRUE;
|
2010-01-25 18:50:13 -05:00
|
|
|
|
|
|
|
err_exit:
|
2011-01-29 14:12:10 -05:00
|
|
|
destroy_async_data(&conn->async);
|
2010-01-25 18:50:13 -05:00
|
|
|
|
|
|
|
SET_ERRNO(err);
|
|
|
|
|
|
|
|
return FALSE;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
2011-04-27 07:20:27 -04:00
|
|
|
#if defined(HAVE_GETADDRINFO) && !defined(HAVE_GAI_STRERROR) && !defined(WIN32)
|
|
|
|
/* NetWare has getaddrinfo but lacks gai_strerror.
|
|
|
|
Windows has a gai_strerror but it is bad (not thread-safe) and the generic
|
|
|
|
socket error string function can be used for this pupose. */
|
|
|
|
static const char *gai_strerror(int ecode)
|
|
|
|
{
|
2011-07-04 16:10:32 -04:00
|
|
|
switch (ecode) {
|
2011-04-27 07:20:27 -04:00
|
|
|
case EAI_AGAIN:
|
|
|
|
return "The name could not be resolved at this time";
|
|
|
|
case EAI_BADFLAGS:
|
|
|
|
return "The flags parameter had an invalid value";
|
|
|
|
case EAI_FAIL:
|
|
|
|
return "A non-recoverable error occurred when attempting to "
|
|
|
|
"resolve the name";
|
|
|
|
case EAI_FAMILY:
|
|
|
|
return "The address family was not recognized";
|
|
|
|
case EAI_MEMORY:
|
|
|
|
return "Out of memory";
|
|
|
|
case EAI_NONAME:
|
|
|
|
return "The name does not resolve for the supplied parameters";
|
|
|
|
case EAI_SERVICE:
|
|
|
|
return "The service passed was not recognized for the "
|
|
|
|
"specified socket type"
|
|
|
|
case EAI_SOCKTYPE:
|
|
|
|
return "The intended socket type was not recognized"
|
|
|
|
case EAI_SYSTEM:
|
|
|
|
return "A system error occurred";
|
|
|
|
case EAI_OVERFLOW:
|
|
|
|
return "An argument buffer overflowed";
|
|
|
|
default:
|
|
|
|
return "Unknown error";
|
|
|
|
|
|
|
|
/* define this now as this is a private implementation of said function */
|
|
|
|
#define HAVE_GAI_STRERROR
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-04-27 07:07:49 -04:00
|
|
|
/*
|
|
|
|
* resolver_error() calls failf() with the appropriate message after a resolve
|
|
|
|
* error
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void resolver_error(struct connectdata *conn, const char *host_or_proxy)
|
|
|
|
{
|
|
|
|
failf(conn->data, "Could not resolve %s: %s; %s", host_or_proxy,
|
|
|
|
conn->async.hostname,
|
|
|
|
#ifdef HAVE_GAI_STRERROR
|
|
|
|
/* NetWare doesn't have gai_strerror and on Windows it isn't deemed
|
|
|
|
thread-safe */
|
|
|
|
gai_strerror(conn->async.status)
|
|
|
|
#else
|
2011-04-29 10:33:45 -04:00
|
|
|
Curl_strerror(conn, conn->async.status)
|
2011-04-27 07:07:49 -04:00
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
/*
|
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
|
|
|
|
*
|
|
|
|
* This is the version for resolves-in-a-thread.
|
|
|
|
*/
|
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
|
|
|
{
|
|
|
|
struct thread_data *td = (struct thread_data*) conn->async.os_specific;
|
2010-02-02 11:23:42 -05:00
|
|
|
CURLcode rc = CURLE_OK;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2007-02-21 14:03:20 -05:00
|
|
|
DEBUGASSERT(conn && td);
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2004-10-16 08:59:05 -04:00
|
|
|
/* wait for the thread to resolve the name */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(Curl_thread_join(&td->thread_hnd))
|
2010-01-25 18:50:13 -05:00
|
|
|
rc = getaddrinfo_complete(conn);
|
2011-04-20 09:17:42 -04:00
|
|
|
else
|
2010-02-13 13:29:59 -05:00
|
|
|
DEBUGASSERT(0);
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
conn->async.done = TRUE;
|
2010-02-13 13:29:59 -05:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
if(entry)
|
|
|
|
*entry = conn->async.dns;
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(!conn->async.dns) {
|
2004-04-26 03:20:11 -04:00
|
|
|
/* a name was not resolved */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->bits.httpproxy) {
|
2011-04-27 07:07:49 -04:00
|
|
|
resolver_error(conn, "proxy");
|
2010-01-25 18:50:13 -05:00
|
|
|
rc = CURLE_COULDNT_RESOLVE_PROXY;
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else {
|
2011-04-27 07:07:49 -04:00
|
|
|
resolver_error(conn, "host");
|
2010-01-25 18:50:13 -05:00
|
|
|
rc = CURLE_COULDNT_RESOLVE_HOST;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-29 14:12:10 -05:00
|
|
|
destroy_async_data(&conn->async);
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2005-02-08 07:36:13 -05:00
|
|
|
if(!conn->async.dns)
|
|
|
|
conn->bits.close = TRUE;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
return (rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
2011-01-30 18:10:35 -05:00
|
|
|
CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
|
|
|
|
struct Curl_dns_entry **entry)
|
2004-04-26 03:20:11 -04:00
|
|
|
{
|
2008-02-09 23:20:09 -05:00
|
|
|
struct SessionHandle *data = conn->data;
|
2010-01-25 18:50:13 -05:00
|
|
|
struct thread_data *td = (struct thread_data*) conn->async.os_specific;
|
|
|
|
int done = 0;
|
2010-02-13 13:29:59 -05:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
*entry = NULL;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!td) {
|
2010-01-25 18:50:13 -05:00
|
|
|
DEBUGASSERT(td);
|
|
|
|
return CURLE_COULDNT_RESOLVE_HOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
Curl_mutex_acquire(td->tsd.mtx);
|
|
|
|
done = td->tsd.done;
|
|
|
|
Curl_mutex_release(td->tsd.mtx);
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(done) {
|
2010-01-25 18:50:13 -05:00
|
|
|
getaddrinfo_complete(conn);
|
2011-01-29 14:12:10 -05:00
|
|
|
destroy_async_data(&conn->async);
|
2010-01-25 18:50:13 -05:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(!conn->async.dns) {
|
2011-04-27 07:07:49 -04:00
|
|
|
resolver_error(conn, "host");
|
2004-04-26 03:20:11 -04:00
|
|
|
return CURLE_COULDNT_RESOLVE_HOST;
|
|
|
|
}
|
|
|
|
*entry = conn->async.dns;
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-01-25 18:50:13 -05:00
|
|
|
/* poll for name lookup done with exponential backoff up to 250ms */
|
2010-09-01 10:47:42 -04:00
|
|
|
int elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(elapsed < 0)
|
2010-01-25 18:50:13 -05:00
|
|
|
elapsed = 0;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(td->poll_interval == 0)
|
2010-01-25 18:50:13 -05:00
|
|
|
/* Start at 1ms poll interval */
|
|
|
|
td->poll_interval = 1;
|
2011-04-20 09:17:42 -04:00
|
|
|
else if(elapsed >= td->interval_end)
|
2010-01-25 18:50:13 -05:00
|
|
|
/* Back-off exponentially if last interval expired */
|
|
|
|
td->poll_interval *= 2;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(td->poll_interval > 250)
|
2010-01-25 18:50:13 -05:00
|
|
|
td->poll_interval = 250;
|
|
|
|
|
|
|
|
td->interval_end = elapsed + td->poll_interval;
|
|
|
|
Curl_expire(conn->data, td->poll_interval);
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
2010-01-25 18:50:13 -05:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
const struct thread_data *td =
|
|
|
|
(const struct thread_data *) conn->async.os_specific;
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(td && td->dummy_sock != CURL_SOCKET_BAD) {
|
2006-04-11 03:23:30 -04:00
|
|
|
if(numsocks) {
|
2010-01-25 18:50:13 -05:00
|
|
|
/* return one socket waiting for readable, even though this is just
|
2006-04-11 03:23:30 -04:00
|
|
|
a dummy */
|
|
|
|
socks[0] = td->dummy_sock;
|
2010-01-25 18:50:13 -05:00
|
|
|
return GETSOCK_READSOCK(0);
|
2006-04-11 03:23:30 -04:00
|
|
|
}
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
2006-04-11 03:23:30 -04:00
|
|
|
return 0;
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
2010-02-13 13:39:47 -05:00
|
|
|
#ifndef HAVE_GETADDRINFO
|
2004-04-26 03:20:11 -04:00
|
|
|
/*
|
2010-01-25 18:50:13 -05:00
|
|
|
* Curl_getaddrinfo() - for platforms without getaddrinfo
|
2004-04-26 03:20:11 -04:00
|
|
|
*/
|
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
|
|
|
{
|
2008-11-06 12:19:56 -05:00
|
|
|
struct in_addr in;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
*waitp = 0; /* default to synchronous response */
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2008-11-06 12:19:56 -05:00
|
|
|
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-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
/* fire up a new resolver thread! */
|
2007-11-05 04:45:09 -05:00
|
|
|
if(init_resolve_thread(conn, hostname, port, NULL)) {
|
2010-01-25 18:50:13 -05:00
|
|
|
*waitp = 1; /* expect asynchronous response */
|
2004-04-26 03:20:11 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fall-back to blocking version */
|
2010-01-25 18:50:13 -05:00
|
|
|
return Curl_ipv4_resolve_r(hostname, port);
|
2004-04-26 03:20:11 -04:00
|
|
|
}
|
|
|
|
|
2010-02-13 13:39:47 -05:00
|
|
|
#else /* !HAVE_GETADDRINFO */
|
2010-01-25 18:50:13 -05:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
/*
|
2011-01-30 18:10:35 -05:00
|
|
|
* Curl_resolver_getaddrinfo() - for getaddrinfo
|
2004-04-26 03:20:11 -04:00
|
|
|
*/
|
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
|
|
|
{
|
2008-10-30 09:45:25 -04:00
|
|
|
struct addrinfo hints;
|
|
|
|
Curl_addrinfo *res;
|
2004-04-26 03:20:11 -04:00
|
|
|
int error;
|
|
|
|
char sbuf[NI_MAXSERV];
|
2010-01-25 18:50:13 -05:00
|
|
|
int pf = PF_INET;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
*waitp = 0; /* default to synchronous response */
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-02-13 13:39:47 -05:00
|
|
|
#ifndef CURLRES_IPV4
|
2008-07-29 14:26:35 -04:00
|
|
|
/*
|
|
|
|
* Check if a limited name resolve has been requested.
|
|
|
|
*/
|
2010-11-11 08:51:39 -05:00
|
|
|
switch(conn->ip_version) {
|
2008-07-29 14:26:35 -04:00
|
|
|
case CURL_IPRESOLVE_V4:
|
2004-04-26 03:20:11 -04:00
|
|
|
pf = PF_INET;
|
2008-07-29 14:26:35 -04:00
|
|
|
break;
|
|
|
|
case CURL_IPRESOLVE_V6:
|
|
|
|
pf = PF_INET6;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pf = PF_UNSPEC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-23 06:10:55 -04:00
|
|
|
if((pf != PF_INET) && !Curl_ipv6works())
|
|
|
|
/* the stack seems to be a non-ipv6 one */
|
|
|
|
pf = PF_INET;
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
#endif /* !CURLRES_IPV4 */
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = pf;
|
2005-09-16 17:30:08 -04:00
|
|
|
hints.ai_socktype = conn->socktype;
|
2010-04-16 09:03:52 -04:00
|
|
|
|
2010-01-25 18:50:13 -05:00
|
|
|
snprintf(sbuf, sizeof(sbuf), "%d", port);
|
2004-04-26 03:20:11 -04:00
|
|
|
|
|
|
|
/* fire up a new resolver thread! */
|
2007-11-05 04:45:09 -05:00
|
|
|
if(init_resolve_thread(conn, hostname, port, &hints)) {
|
2010-01-25 18:50:13 -05:00
|
|
|
*waitp = 1; /* expect asynchronous response */
|
2004-04-26 03:20:11 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fall-back to blocking version */
|
2011-06-21 10:06:56 -04:00
|
|
|
infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
|
2007-02-16 13:19:35 -05:00
|
|
|
hostname, Curl_strerror(conn, ERRNO));
|
2004-04-26 03:20:11 -04:00
|
|
|
|
2008-10-30 09:45:25 -04:00
|
|
|
error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(error) {
|
2011-06-21 10:06:56 -04:00
|
|
|
infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n",
|
2007-02-16 13:19:35 -05:00
|
|
|
hostname, port, Curl_strerror(conn, SOCKERRNO));
|
2004-04-26 03:20:11 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2010-01-25 18:50:13 -05:00
|
|
|
|
2010-02-13 13:39:47 -05:00
|
|
|
#endif /* !HAVE_GETADDRINFO */
|
2010-01-25 18:50:13 -05:00
|
|
|
|
2004-04-26 03:20:11 -04:00
|
|
|
#endif /* CURLRES_THREADED */
|