X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fgetaddrinfo.c;h=4bce64eb3633c21519140426b5b6fa174726db38;hb=9526c9e3e4eadee80a36329934c8d926fd9b6f46;hp=a5f6a3a6ec241a8e2b8c5f7c46b9ef8759ab15b5;hpb=f9504dae81fde52bb1c1669fdd0c9e3aba818a75;p=pazpar2-moved-to-github.git diff --git a/src/getaddrinfo.c b/src/getaddrinfo.c index a5f6a3a..4bce64e 100644 --- a/src/getaddrinfo.c +++ b/src/getaddrinfo.c @@ -1,5 +1,5 @@ /* This file is part of Pazpar2. - Copyright (C) 2006-2013 Index Data + Copyright (C) Index Data Pazpar2 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -34,14 +34,12 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #endif #ifdef WIN32 -#include +#include +#include #endif #if HAVE_NETDB_H #include #endif -#if HAVE_NETINET_IN_H -#include -#endif #include #include @@ -63,15 +61,16 @@ struct work { char *ipport; /* result or NULL if it could not be resolved */ struct host *host; /* host that we're dealing with - mother thread */ iochan_man_t iochan_man; /* iochan manager */ + int error; }; static int log_level = YLOG_LOG; -void perform_getaddrinfo(struct work *w) +static void perform_getaddrinfo(struct work *w) { struct addrinfo hints, *res; char host[512], *cp; - const char *port = 0; + char *port = "210"; int error; hints.ai_flags = 0; @@ -83,28 +82,43 @@ void perform_getaddrinfo(struct work *w) hints.ai_canonname = NULL; hints.ai_next = NULL; - strncpy(host, w->hostport, sizeof(host)-1); + if (!strncmp(w->hostport, "http://", 7)) + { + port = "80"; + strncpy(host, w->hostport + 7, sizeof(host)-1); + } + else if (!strncmp(w->hostport, "https://", 8)) + { + port = "443"; + strncpy(host, w->hostport + 8, sizeof(host)-1); + } + else + { + strncpy(host, w->hostport, sizeof(host)-1); + } host[sizeof(host)-1] = 0; if ((cp = strrchr(host, ':'))) { *cp = '\0'; port = cp + 1; } - error = getaddrinfo(host, port ? port : "210", &hints, &res); + error = getaddrinfo(host, port, &hints, &res); if (error) { yaz_log(YLOG_WARN, "Failed to resolve %s: %s", w->hostport, gai_strerror(error)); + w->error = error; } else { + char n_host[512]; if (getnameinfo((struct sockaddr *) res->ai_addr, res->ai_addrlen, - host, sizeof(host)-1, + n_host, sizeof(n_host)-1, 0, 0, NI_NUMERICHOST) == 0) { - w->ipport = xmalloc(strlen(host) + (port ? strlen(port) : 0) + 2); - strcpy(w->ipport, host); + w->ipport = xmalloc(strlen(n_host) + (port ? strlen(port) : 0) + 2); + strcpy(w->ipport, n_host); if (port) { strcat(w->ipport, ":"); @@ -145,6 +159,7 @@ void iochan_handler(struct iochan *i, int event) { struct work *w = sel_thread_result(p); w->host->ipport = w->ipport; + w->host->error = w->error; connect_resolver_host(w->host, w->iochan_man); xfree(w); } @@ -178,12 +193,13 @@ static void getaddrinfo_start(iochan_man_t iochan_man) int host_getaddrinfo(struct host *host, iochan_man_t iochan_man) { struct work *w = xmalloc(sizeof(*w)); - int use_thread = 0; /* =0 to disable threading entirely */ + int use_thread = 1; /* =0 to disable threading entirely */ w->hostport = host->tproxy ? host->tproxy : host->proxy; w->ipport = 0; w->host = host; w->iochan_man = iochan_man; + w->error = 0; #if USE_THREADED_RESOLVER if (use_thread) { @@ -196,6 +212,7 @@ int host_getaddrinfo(struct host *host, iochan_man_t iochan_man) #endif perform_getaddrinfo(w); host->ipport = w->ipport; + host->error = w->error; xfree(w); if (!host->ipport) return -1;