1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "sel_thread.h"
32 #include <sys/types.h>
34 #include <sys/socket.h>
46 #include <yaz/tcpip.h>
49 #include "connection.h"
52 /* Only use a threaded resolver on Unix that offers getaddrinfo.
53 gethostbyname is NOT reentrant.
56 #define USE_THREADED_RESOLVER 1
60 char *hostport; /* hostport to be resolved in separate thread */
61 char *ipport; /* result or NULL if it could not be resolved */
62 struct host *host; /* host that we're dealing with - mother thread */
63 iochan_man_t iochan_man; /* iochan manager */
66 static int log_level = YLOG_LOG;
68 void perform_getaddrinfo(struct work *w)
70 struct addrinfo hints, *res;
72 char portbuf[512]; // very big ports
77 hints.ai_family = AF_UNSPEC;
78 hints.ai_socktype = SOCK_STREAM;
79 hints.ai_protocol = 0;
82 hints.ai_canonname = NULL;
85 strncpy(host, w->hostport, sizeof(host)-1);
86 host[sizeof(host)-1] = 0;
87 if ((cp = strrchr(host, ':')))
91 strncpy(port, cp + 1, sizeof(port));
93 error = getaddrinfo(host, port ? port : "210", &hints, &res);
96 yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
97 w->hostport, gai_strerror(error));
101 if (getnameinfo((struct sockaddr *) res->ai_addr, res->ai_addrlen,
102 host, sizeof(host)-1,
104 NI_NUMERICHOST) == 0)
106 w->ipport = xmalloc(strlen(host) + (port ? strlen(port) : 0) + 2);
107 strcpy(w->ipport, host);
110 strcat(w->ipport, ":");
111 strcat(w->ipport, port);
113 yaz_log(log_level, "Resolved %s -> %s", w->hostport, w->ipport);
117 yaz_log(YLOG_LOG|YLOG_ERRNO, "getnameinfo failed for %s",
124 static void work_handler(void *vp)
128 int sec = 0; /* >0 for debugging/testing purposes */
131 yaz_log(log_level, "waiting %d seconds", sec);
136 perform_getaddrinfo(w);
139 #if USE_THREADED_RESOLVER
140 void iochan_handler(struct iochan *i, int event)
142 sel_thread_t p = iochan_getdata(i);
144 if (event & EVENT_INPUT)
146 struct work *w = sel_thread_result(p);
147 w->host->ipport = w->ipport;
148 connect_resolver_host(w->host, w->iochan_man);
153 static sel_thread_t resolver_thread = 0;
155 static void getaddrinfo_start(iochan_man_t iochan_man)
158 sel_thread_t p = resolver_thread =
159 sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
160 3 /* no of resolver threads */);
163 yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
168 IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT,
169 "getaddrinfo_socket");
170 iochan_setdata(chan, p);
171 iochan_add(iochan_man, chan);
173 yaz_log(log_level, "resolver start");
178 int host_getaddrinfo(struct host *host, iochan_man_t iochan_man)
180 struct work *w = xmalloc(sizeof(*w));
181 int use_thread = 0; /* =0 to disable threading entirely */
183 w->hostport = host->tproxy ? host->tproxy : host->proxy;
186 w->iochan_man = iochan_man;
187 #if USE_THREADED_RESOLVER
190 if (resolver_thread == 0)
191 getaddrinfo_start(iochan_man);
192 assert(resolver_thread);
193 sel_thread_add(resolver_thread, w);
197 perform_getaddrinfo(w);
198 host->ipport = w->ipport;
208 * c-file-style: "Stroustrup"
209 * indent-tabs-mode: nil
211 * vim: shiftwidth=4 tabstop=8 expandtab