1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 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>
43 #include <netinet/in.h>
48 #include <yaz/tcpip.h>
51 #include "connection.h"
54 /* Only use a threaded resolver on Unix that offers getaddrinfo.
55 gethostbyname is NOT reentrant.
59 #define USE_THREADED_RESOLVER 1
64 char *hostport; /* hostport to be resolved in separate thread */
65 char *ipport; /* result or NULL if it could not be resolved */
66 struct host *host; /* host that we're dealing with - mother thread */
69 static int log_level = YLOG_LOG;
71 void perform_getaddrinfo(struct work *w)
76 struct addrinfo *addrinfo, hints;
80 char *hostport = xstrdup(w->hostport);
82 if ((port = strchr(hostport, ':')))
89 hints.ai_family = PF_INET;
90 hints.ai_socktype = SOCK_STREAM;
91 hints.ai_protocol = IPPROTO_TCP;
94 hints.ai_canonname = 0;
96 // This is not robust code. It assumes that getaddrinfo always
97 // returns AF_INET address.
98 if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
100 yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
101 w->hostport, gai_strerror(res));
106 unsigned char addrbuf[4];
107 assert(addrinfo->ai_family == PF_INET);
109 &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
110 sprintf(ipport, "%u.%u.%u.%u:%s",
111 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
112 freeaddrinfo(addrinfo);
113 w->ipport = xstrdup(ipport);
114 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
117 hp = gethostbyname(hostport);
120 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to resolve %s", hostport);
125 unsigned char addrbuf[4];
127 memcpy(addrbuf, *hp->h_addr_list, 4 * sizeof(unsigned char));
128 sprintf(ipport, "%u.%u.%u.%u:%s",
129 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
130 w->ipport = xstrdup(ipport);
131 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
137 static void work_handler(void *vp)
141 int sec = 0; /* >0 for debugging/testing purposes */
144 yaz_log(log_level, "waiting %d seconds", sec);
149 perform_getaddrinfo(w);
152 #if USE_THREADED_RESOLVER
153 void iochan_handler(struct iochan *i, int event)
155 sel_thread_t p = iochan_getdata(i);
157 if (event & EVENT_INPUT)
159 struct work *w = sel_thread_result(p);
160 w->host->ipport = w->ipport;
161 connect_resolver_host(w->host);
166 static sel_thread_t resolver_thread = 0;
168 static void getaddrinfo_start(void)
171 sel_thread_t p = resolver_thread =
172 sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
173 3 /* no of resolver threads */);
176 yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
181 IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT);
182 iochan_setdata(chan, p);
183 pazpar2_add_channel(chan);
185 yaz_log(log_level, "resolver start");
190 int host_getaddrinfo(struct host *host)
192 struct work *w = xmalloc(sizeof(*w));
193 int use_thread = 1; /* =0 to disable threading entirely */
195 w->hostport = host->hostport;
198 #if USE_THREADED_RESOLVER
201 if (resolver_thread == 0)
203 assert(resolver_thread);
204 sel_thread_add(resolver_thread, w);
208 perform_getaddrinfo(w);
209 host->ipport = w->ipport;
219 * c-file-style: "Stroustrup"
220 * indent-tabs-mode: nil
222 * vim: shiftwidth=4 tabstop=8 expandtab