#include "setup.h"
#include <string.h>
#include <errno.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_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef VMS
#include <in.h>
#include <inet.h>
#include <stdlib.h>
#endif
#ifdef HAVE_PROCESS_H
#include <process.h>
#endif
#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
#undef in_addr_t
#define in_addr_t unsigned long
#endif
#include "urldata.h"
#include "sendf.h"
#include "hostip.h"
#include "hash.h"
#include "share.h"
#include "strerror.h"
#include "url.h"
#include "multiif.h"
#include "inet_pton.h"
#define _MPRINTF_REPLACE
#include <curl/mprintf.h>
#include "inet_ntop.h"
#include "curl_memory.h"
#include "memdebug.h"
#if defined(_MSC_VER) && defined(CURL_NO__BEGINTHREADEX)
#pragma message ("No _beginthreadex() available in this RTL")
#endif
#ifdef CURLRES_THREADED
static bool init_resolve_thread(struct connectdata *conn,
const char *hostname, int port,
const struct addrinfo *hints);
#ifdef CURLRES_IPV4
#define THREAD_FUNC gethostbyname_thread
#define THREAD_NAME "gethostbyname_thread"
#else
#define THREAD_FUNC getaddrinfo_thread
#define THREAD_NAME "getaddrinfo_thread"
#endif
struct thread_data {
HANDLE thread_hnd;
unsigned thread_id;
DWORD thread_status;
curl_socket_t dummy_sock;
HANDLE mutex_waiting;
HANDLE event_resolved;
HANDLE event_thread_started;
HANDLE mutex_terminate;
HANDLE event_terminate;
#ifdef CURLRES_IPV6
struct addrinfo hints;
#endif
};
struct thread_sync_data {
HANDLE mutex_waiting;
HANDLE mutex_terminate;
HANDLE event_terminate;
char * hostname;
};
static
void destroy_thread_sync_data(struct thread_sync_data * tsd)
{
if(tsd->hostname)
free(tsd->hostname);
if(tsd->event_terminate)
CloseHandle(tsd->event_terminate);
if(tsd->mutex_terminate)
CloseHandle(tsd->mutex_terminate);
if(tsd->mutex_waiting)
CloseHandle(tsd->mutex_waiting);
memset(tsd,0,sizeof(*tsd));
}
static
BOOL init_thread_sync_data(struct thread_data * td,
const char * hostname,
struct thread_sync_data * tsd)
{
HANDLE curr_proc = GetCurrentProcess();
memset(tsd, 0, sizeof(*tsd));
if(!DuplicateHandle(curr_proc, td->mutex_waiting,
curr_proc, &tsd->mutex_waiting, 0, FALSE,
DUPLICATE_SAME_ACCESS)) {
destroy_thread_sync_data(tsd);
return FALSE;
}
if(!DuplicateHandle(curr_proc, td->mutex_terminate,
curr_proc, &tsd->mutex_terminate, 0, FALSE,
DUPLICATE_SAME_ACCESS)) {
destroy_thread_sync_data(tsd);
return FALSE;
}
if(!DuplicateHandle(curr_proc, td->event_terminate,
curr_proc, &tsd->event_terminate, 0, FALSE,
DUPLICATE_SAME_ACCESS)) {
destroy_thread_sync_data(tsd);
return FALSE;
}
tsd->hostname = strdup(hostname);
if(!tsd->hostname) {
destroy_thread_sync_data(tsd);
return FALSE;
}
return TRUE;
}
static
BOOL acquire_thread_sync(struct thread_sync_data * tsd)
{
if(WaitForSingleObject(tsd->mutex_waiting, 0) == WAIT_TIMEOUT) {
if(WaitForSingleObject(tsd->mutex_terminate, INFINITE) != WAIT_OBJECT_0) {
}
else {
if(WaitForSingleObject(tsd->event_terminate, 0) != WAIT_TIMEOUT) {
}
else {
return TRUE;
}
}
}
return FALSE;
}
static
void release_thread_sync(struct thread_sync_data * tsd)
{
ReleaseMutex(tsd->mutex_terminate);
}
#if defined(CURLRES_IPV4)
static unsigned __stdcall gethostbyname_thread (void *arg)
{
struct connectdata *conn = (struct connectdata*) arg;
struct thread_data *td = (struct thread_data*) conn->async.os_specific;
struct hostent *he;
int rc = 0;
struct thread_sync_data tsd = { 0,0,0,NULL };
if(!init_thread_sync_data(td, conn->async.hostname, &tsd)) {
return (unsigned)-1;
}
conn->async.status = NO_DATA;
SET_SOCKERRNO(conn->async.status);
SetEvent(td->event_thread_started);
he = gethostbyname (tsd.hostname);
if(acquire_thread_sync(&tsd)) {
SetEvent(td->event_resolved);
if(he) {
rc = Curl_addrinfo4_callback(conn, CURL_ASYNC_SUCCESS, he);
}
else {
rc = Curl_addrinfo4_callback(conn, SOCKERRNO, NULL);
}
release_thread_sync(&tsd);
}
destroy_thread_sync_data(&tsd);
return (rc);
}
#elif defined(CURLRES_IPV6)
static unsigned __stdcall getaddrinfo_thread (void *arg)
{
struct connectdata *conn = (struct connectdata*) arg;
struct thread_data *td = (struct thread_data*) conn->async.os_specific;
Curl_addrinfo *res;
char service [NI_MAXSERV];
int rc;
struct addrinfo hints = td->hints;
struct thread_sync_data tsd = { 0,0,0,NULL };
if(!init_thread_sync_data(td, conn->async.hostname, &tsd)) {
return -1;
}
itoa(conn->async.port, service, 10);
conn->async.status = NO_DATA;
SET_SOCKERRNO(conn->async.status);
SetEvent(td->event_thread_started);
rc = Curl_getaddrinfo_ex(tsd.hostname, service, &hints, &res);
if(acquire_thread_sync(&tsd)) {
SetEvent(td->event_resolved);
if(rc == 0) {
rc = Curl_addrinfo6_callback(conn, CURL_ASYNC_SUCCESS, res);
}
else {
rc = Curl_addrinfo6_callback(conn, SOCKERRNO, NULL);
}
release_thread_sync(&tsd);
}
destroy_thread_sync_data(&tsd);
return (rc);
}
#endif
void Curl_destroy_thread_data (struct Curl_async *async)
{
if(async->hostname)
free(async->hostname);
if(async->os_specific) {
struct thread_data *td = (struct thread_data*) async->os_specific;
curl_socket_t sock = td->dummy_sock;
if(td->mutex_terminate && td->event_terminate) {
if(WaitForSingleObject(td->mutex_terminate, INFINITE) == WAIT_OBJECT_0) {
SetEvent(td->event_terminate);
ReleaseMutex(td->mutex_terminate);
}
else {
}
}
if(td->mutex_terminate)
CloseHandle(td->mutex_terminate);
if(td->event_terminate)
CloseHandle(td->event_terminate);
if(td->event_thread_started)
CloseHandle(td->event_thread_started);
if(sock != CURL_SOCKET_BAD)
sclose(sock);
if(td->mutex_waiting)
CloseHandle(td->mutex_waiting);
td->mutex_waiting = NULL;
if(td->event_resolved)
CloseHandle(td->event_resolved);
if(td->thread_hnd)
CloseHandle(td->thread_hnd);
free(async->os_specific);
}
async->hostname = NULL;
async->os_specific = NULL;
}
static bool init_resolve_thread (struct connectdata *conn,
const char *hostname, int port,
const struct addrinfo *hints)
{
struct thread_data *td = calloc(sizeof(*td), 1);
HANDLE thread_and_event[2] = {0};
if(!td) {
SET_ERRNO(ENOMEM);
return FALSE;
}
Curl_safefree(conn->async.hostname);
conn->async.hostname = strdup(hostname);
if(!conn->async.hostname) {
free(td);
SET_ERRNO(ENOMEM);
return FALSE;
}
conn->async.port = port;
conn->async.done = FALSE;
conn->async.status = 0;
conn->async.dns = NULL;
conn->async.os_specific = (void*) td;
td->dummy_sock = CURL_SOCKET_BAD;
td->mutex_waiting = CreateMutex(NULL, TRUE, NULL);
if(td->mutex_waiting == NULL) {
Curl_destroy_thread_data(&conn->async);
SET_ERRNO(EAGAIN);
return FALSE;
}
td->event_resolved = CreateEvent(NULL, TRUE, FALSE, NULL);
if(td->event_resolved == NULL) {
Curl_destroy_thread_data(&conn->async);
SET_ERRNO(EAGAIN);
return FALSE;
}
td->mutex_terminate = CreateMutex(NULL, FALSE, NULL);
if(td->mutex_terminate == NULL) {
Curl_destroy_thread_data(&conn->async);
SET_ERRNO(EAGAIN);
return FALSE;
}
td->event_terminate = CreateEvent(NULL, TRUE, FALSE, NULL);
if(td->event_terminate == NULL) {
Curl_destroy_thread_data(&conn->async);
SET_ERRNO(EAGAIN);
return FALSE;
}
td->event_thread_started = CreateEvent(NULL, TRUE, FALSE, NULL);
if(td->event_thread_started == NULL) {
Curl_destroy_thread_data(&conn->async);
SET_ERRNO(EAGAIN);
return FALSE;
}
#ifdef _WIN32_WCE
td->thread_hnd = (HANDLE) CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE) THREAD_FUNC,
conn, 0, &td->thread_id);
#else
td->thread_hnd = (HANDLE) _beginthreadex(NULL, 0, THREAD_FUNC,
conn, 0, &td->thread_id);
#endif
#ifdef CURLRES_IPV6
DEBUGASSERT(hints);
td->hints = *hints;
#else
(void) hints;
#endif
if(!td->thread_hnd) {
#ifndef _WIN32_WCE
SET_ERRNO(errno);
#endif
Curl_destroy_thread_data(&conn->async);
return FALSE;
}
thread_and_event[0] = td->thread_hnd;
thread_and_event[1] = td->event_thread_started;
if(WaitForMultipleObjects(sizeof(thread_and_event) /
sizeof(thread_and_event[0]),
(const HANDLE*)thread_and_event, FALSE,
INFINITE) == WAIT_FAILED) {
}
td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0);
return TRUE;
}
CURLcode Curl_wait_for_resolv(struct connectdata *conn,
struct Curl_dns_entry **entry)
{
struct thread_data *td = (struct thread_data*) conn->async.os_specific;
struct SessionHandle *data = conn->data;
long timeout;
DWORD status;
CURLcode rc;
DEBUGASSERT(conn && td);
timeout =
conn->data->set.connecttimeout ? conn->data->set.connecttimeout :
conn->data->set.timeout ? conn->data->set.timeout :
CURL_TIMEOUT_RESOLVE * 1000;
status = WaitForSingleObject(td->event_resolved, timeout);
ReleaseMutex(td->mutex_waiting);
CloseHandle(td->mutex_waiting);
td->mutex_waiting = NULL;
CloseHandle(td->event_resolved);
td->event_resolved = NULL;
if(status == WAIT_OBJECT_0) {
if(WaitForSingleObject(td->thread_hnd, 5000) == WAIT_TIMEOUT) {
TerminateThread(td->thread_hnd, 0);
conn->async.done = TRUE;
td->thread_status = (DWORD)-1;
}
else {
SET_SOCKERRNO(conn->async.status);
GetExitCodeThread(td->thread_hnd, &td->thread_status);
}
}
else {
conn->async.done = TRUE;
td->thread_status = (DWORD)-1;
}
if(entry)
*entry = conn->async.dns;
rc = CURLE_OK;
if(!conn->async.dns) {
if(td->thread_status == CURLE_OUT_OF_MEMORY) {
rc = CURLE_OUT_OF_MEMORY;
failf(data, "Could not resolve host: %s", curl_easy_strerror(rc));
}
else if(conn->async.done) {
if(conn->bits.httpproxy) {
failf(data, "Could not resolve proxy: %s; %s",
conn->proxy.dispname, Curl_strerror(conn, conn->async.status));
rc = CURLE_COULDNT_RESOLVE_PROXY;
}
else {
failf(data, "Could not resolve host: %s; %s",
conn->host.name, Curl_strerror(conn, conn->async.status));
rc = CURLE_COULDNT_RESOLVE_HOST;
}
}
else if(td->thread_status == (DWORD)-1 || conn->async.status == NO_DATA) {
failf(data, "Resolving host timed out: %s", conn->host.name);
rc = CURLE_OPERATION_TIMEDOUT;
}
else
rc = CURLE_OPERATION_TIMEDOUT;
}
Curl_destroy_thread_data(&conn->async);
if(!conn->async.dns)
conn->bits.close = TRUE;
return (rc);
}
CURLcode Curl_is_resolved(struct connectdata *conn,
struct Curl_dns_entry **entry)
{
struct SessionHandle *data = conn->data;
*entry = NULL;
if(conn->async.done) {
Curl_destroy_thread_data(&conn->async);
if(!conn->async.dns) {
failf(data, "Could not resolve host: %s; %s",
conn->host.name, Curl_strerror(conn, conn->async.status));
return CURLE_COULDNT_RESOLVE_HOST;
}
*entry = conn->async.dns;
}
return CURLE_OK;
}
int Curl_resolv_getsock(struct connectdata *conn,
curl_socket_t *socks,
int numsocks)
{
const struct thread_data *td =
(const struct thread_data *) conn->async.os_specific;
if(td && td->dummy_sock != CURL_SOCKET_BAD) {
if(numsocks) {
socks[0] = td->dummy_sock;
return GETSOCK_WRITESOCK(0);
}
}
return 0;
}
#ifdef CURLRES_IPV4
Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
const char *hostname,
int port,
int *waitp)
{
struct hostent *h = NULL;
struct SessionHandle *data = conn->data;
struct in_addr in;
*waitp = 0;
if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
return Curl_ip2addr(AF_INET, &in, hostname, port);
if(init_resolve_thread(conn, hostname, port, NULL)) {
*waitp = TRUE;
return NULL;
}
infof(data, "init_resolve_thread() failed for %s; %s\n",
hostname, Curl_strerror(conn, ERRNO));
h = gethostbyname(hostname);
if(!h) {
infof(data, "gethostbyname(2) failed for %s:%d; %s\n",
hostname, port, Curl_strerror(conn, SOCKERRNO));
return NULL;
}
return Curl_he2ai(h, port);
}
#endif
#ifdef CURLRES_IPV6
Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
const char *hostname,
int port,
int *waitp)
{
struct addrinfo hints;
Curl_addrinfo *res;
int error;
char sbuf[NI_MAXSERV];
int pf;
struct SessionHandle *data = conn->data;
*waitp = FALSE;
switch(data->set.ip_version) {
case CURL_IPRESOLVE_V4:
pf = PF_INET;
break;
case CURL_IPRESOLVE_V6:
pf = PF_INET6;
break;
default:
pf = PF_UNSPEC;
break;
}
if (pf != PF_INET) {
curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
if(s == CURL_SOCKET_BAD) {
pf = PF_INET;
}
else {
sclose(s);
}
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = pf;
hints.ai_socktype = conn->socktype;
#if 0
hints.ai_flags = AI_CANONNAME;
#endif
itoa(port, sbuf, 10);
if(init_resolve_thread(conn, hostname, port, &hints)) {
*waitp = TRUE;
return NULL;
}
infof(data, "init_resolve_thread() failed for %s; %s\n",
hostname, Curl_strerror(conn, ERRNO));
error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
if(error) {
infof(data, "getaddrinfo() failed for %s:%d; %s\n",
hostname, port, Curl_strerror(conn, SOCKERRNO));
return NULL;
}
return res;
}
#endif
#endif