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>
44 #include <netinet/in.h>
49 #include <yaz/tcpip.h>
52 #include "connection.h"
55 /* Only use a threaded resolver on Unix that offers getaddrinfo.
56 gethostbyname is NOT reentrant.
59 #define USE_THREADED_RESOLVER 1
63 char *hostport; /* hostport to be resolved in separate thread */
64 char *ipport; /* result or NULL if it could not be resolved */
65 struct host *host; /* host that we're dealing with - mother thread */
66 iochan_man_t iochan_man; /* iochan manager */
69 static int log_level = YLOG_LOG;
71 void perform_getaddrinfo(struct work *w)
73 struct addrinfo hints, *res;
79 hints.ai_family = AF_UNSPEC;
80 hints.ai_socktype = SOCK_STREAM;
81 hints.ai_protocol = 0;
84 hints.ai_canonname = NULL;
87 strncpy(host, w->hostport, sizeof(host)-1);
88 host[sizeof(host)-1] = 0;
89 if ((cp = strrchr(host, ':')))
94 error = getaddrinfo(host, port ? port : "210", &hints, &res);
97 yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
98 w->hostport, gai_strerror(error));
102 if (getnameinfo((struct sockaddr *) res->ai_addr, res->ai_addrlen,
103 host, sizeof(host)-1,
105 NI_NUMERICHOST) == 0)
107 w->ipport = xmalloc(strlen(host) + (port ? strlen(port) : 0) + 2);
108 strcpy(w->ipport, host);
111 strcat(w->ipport, ":");
112 strcat(w->ipport, port);
114 yaz_log(log_level, "Resolved %s -> %s", w->hostport, w->ipport);
118 yaz_log(YLOG_LOG|YLOG_ERRNO, "getnameinfo failed for %s",
125 static void work_handler(void *vp)
129 int sec = 0; /* >0 for debugging/testing purposes */
132 yaz_log(log_level, "waiting %d seconds", sec);
137 perform_getaddrinfo(w);
140 #if USE_THREADED_RESOLVER
141 void iochan_handler(struct iochan *i, int event)
143 sel_thread_t p = iochan_getdata(i);
145 if (event & EVENT_INPUT)
147 struct work *w = sel_thread_result(p);
148 w->host->ipport = w->ipport;
149 connect_resolver_host(w->host, w->iochan_man);
154 static sel_thread_t resolver_thread = 0;
156 static void getaddrinfo_start(iochan_man_t iochan_man)
159 sel_thread_t p = resolver_thread =
160 sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
161 3 /* no of resolver threads */);
164 yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
169 IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT,
170 "getaddrinfo_socket");
171 iochan_setdata(chan, p);
172 iochan_add(iochan_man, chan);
174 yaz_log(log_level, "resolver start");
179 int host_getaddrinfo(struct host *host, iochan_man_t iochan_man)
181 struct work *w = xmalloc(sizeof(*w));
182 int use_thread = 0; /* =0 to disable threading entirely */
184 w->hostport = host->tproxy ? host->tproxy : host->proxy;
187 w->iochan_man = iochan_man;
188 #if USE_THREADED_RESOLVER
191 if (resolver_thread == 0)
192 getaddrinfo_start(iochan_man);
193 assert(resolver_thread);
194 sel_thread_add(resolver_thread, w);
198 perform_getaddrinfo(w);
199 host->ipport = w->ipport;
209 * c-file-style: "Stroustrup"
210 * indent-tabs-mode: nil
212 * vim: shiftwidth=4 tabstop=8 expandtab