#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
#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
#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"
#include "inet_ntop.h"
#include "curl_threads.h"
#define _MPRINTF_REPLACE
#include <curl/mprintf.h>
#include "curl_memory.h"
#include "memdebug.h"
#ifdef CURLRES_THREADED
static bool init_resolve_thread(struct connectdata *conn,
const char *hostname, int port,
const struct addrinfo *hints);
struct thread_sync_data {
curl_mutex_t * mtx;
int done;
char * hostname;
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;
};
static struct thread_sync_data * conn_thread_sync_data(struct connectdata *conn)
{
return &(((struct thread_data *)conn->async.os_specific)->tsd);
}
#define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
static
void destroy_thread_sync_data(struct thread_sync_data * tsd)
{
if (tsd->mtx) {
Curl_mutex_destroy(tsd->mtx);
free(tsd->mtx);
}
if(tsd->hostname)
free(tsd->hostname);
if (tsd->res)
Curl_freeaddrinfo(tsd->res);
memset(tsd,0,sizeof(*tsd));
}
static
int init_thread_sync_data(struct thread_sync_data * tsd,
const char * hostname,
int port,
const struct addrinfo *hints)
{
memset(tsd, 0, sizeof(*tsd));
tsd->port = port;
#ifdef CURLRES_IPV6
DEBUGASSERT(hints);
tsd->hints = *hints;
#else
(void) hints;
#endif
tsd->mtx = malloc(sizeof(curl_mutex_t));
if (tsd->mtx == NULL) goto err_exit;
Curl_mutex_init(tsd->mtx);
tsd->sock_error = CURL_ASYNC_SUCCESS;
tsd->hostname = strdup(hostname);
if (!tsd->hostname) goto err_exit;
return 1;
err_exit:
destroy_thread_sync_data(tsd);
return 0;
}
static int getaddrinfo_complete(struct connectdata *conn)
{
struct thread_sync_data *tsd = conn_thread_sync_data(conn);
int rc;
rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
tsd->res = NULL;
return rc;
}
#ifdef HAVE_GETADDRINFO
static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
{
struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
char service [NI_MAXSERV];
int rc;
snprintf(service, sizeof(service), "%d", tsd->port);
rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
if (rc != 0) {
tsd->sock_error = rc;
}
Curl_mutex_acquire(tsd->mtx);
tsd->done = 1;
Curl_mutex_release(tsd->mtx);
return 0;
}
#else
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);
if (!tsd->res) {
tsd->sock_error = SOCKERRNO;
if (tsd->sock_error == 0)
tsd->sock_error = ENOMEM;
}
Curl_mutex_acquire(tsd->mtx);
tsd->done = 1;
Curl_mutex_release(tsd->mtx);
return 0;
}
#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;
if (td->dummy_sock != CURL_SOCKET_BAD)
sclose(td->dummy_sock);
if (td->thread_hnd != curl_thread_t_null)
Curl_thread_join(&td->thread_hnd);
destroy_thread_sync_data(&td->tsd);
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(1, sizeof(struct thread_data));
int err = ENOMEM;
conn->async.os_specific = (void*) td;
if(!td)
goto err_exit;
conn->async.port = port;
conn->async.done = FALSE;
conn->async.status = 0;
conn->async.dns = NULL;
td->dummy_sock = CURL_SOCKET_BAD;
td->thread_hnd = curl_thread_t_null;
if (!init_thread_sync_data(&td->tsd, hostname, port, hints))
goto err_exit;
Curl_safefree(conn->async.hostname);
conn->async.hostname = strdup(hostname);
if(!conn->async.hostname)
goto err_exit;
#ifdef WIN32
td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0);
if (td->dummy_sock == CURL_SOCKET_BAD)
goto err_exit;
#endif
#ifdef HAVE_GETADDRINFO
td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
#else
td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
#endif
if(!td->thread_hnd) {
#ifndef _WIN32_WCE
err = errno;
#endif
goto err_exit;
}
return TRUE;
err_exit:
Curl_destroy_thread_data(&conn->async);
SET_ERRNO(err);
return FALSE;
}
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;
CURLcode rc = CURLE_OK;
DEBUGASSERT(conn && td);
if (Curl_thread_join(&td->thread_hnd)) {
rc = getaddrinfo_complete(conn);
} else {
DEBUGASSERT(0);
}
conn->async.done = TRUE;
if(entry)
*entry = conn->async.dns;
if(!conn->async.dns) {
if (conn->bits.httpproxy) {
failf(data, "Could not resolve proxy: %s; %s",
conn->async.hostname, gai_strerror(conn->async.status));
rc = CURLE_COULDNT_RESOLVE_PROXY;
} else {
failf(data, "Could not resolve host: %s; %s",
conn->async.hostname, gai_strerror(conn->async.status));
rc = CURLE_COULDNT_RESOLVE_HOST;
}
}
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;
struct thread_data *td = (struct thread_data*) conn->async.os_specific;
int done = 0;
*entry = NULL;
if (!td) {
DEBUGASSERT(td);
return CURLE_COULDNT_RESOLVE_HOST;
}
Curl_mutex_acquire(td->tsd.mtx);
done = td->tsd.done;
Curl_mutex_release(td->tsd.mtx);
if (done) {
getaddrinfo_complete(conn);
Curl_destroy_thread_data(&conn->async);
if(!conn->async.dns) {
failf(data, "Could not resolve host: %s; %s",
conn->host.name, gai_strerror(conn->async.status));
return CURLE_COULDNT_RESOLVE_HOST;
}
*entry = conn->async.dns;
} else {
int elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
if (elapsed < 0)
elapsed = 0;
if (td->poll_interval == 0)
td->poll_interval = 1;
else if (elapsed >= td->interval_end)
td->poll_interval *= 2;
if (td->poll_interval > 250)
td->poll_interval = 250;
td->interval_end = elapsed + td->poll_interval;
Curl_expire(conn->data, td->poll_interval);
}
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_READSOCK(0);
}
}
return 0;
}
#ifndef HAVE_GETADDRINFO
Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
const char *hostname,
int port,
int *waitp)
{
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 = 1;
return NULL;
}
return Curl_ipv4_resolve_r(hostname, port);
}
#else
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 = PF_INET;
struct SessionHandle *data = conn->data;
*waitp = 0;
#ifndef CURLRES_IPV4
switch(conn->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);
}
}
#endif
memset(&hints, 0, sizeof(hints));
hints.ai_family = pf;
hints.ai_socktype = conn->socktype;
snprintf(sbuf, sizeof(sbuf), "%d", port);
if(init_resolve_thread(conn, hostname, port, &hints)) {
*waitp = 1;
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