1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 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 */
67 iochan_man_t iochan_man; /* iochan manager */
70 static int log_level = YLOG_LOG;
72 void perform_getaddrinfo(struct work *w)
77 struct addrinfo *addrinfo, hints;
81 char *hostport = xstrdup(w->hostport);
83 if ((port = strchr(hostport, ':')))
90 hints.ai_family = PF_INET;
91 hints.ai_socktype = SOCK_STREAM;
92 hints.ai_protocol = IPPROTO_TCP;
95 hints.ai_canonname = 0;
97 // This is not robust code. It assumes that getaddrinfo always
98 // returns AF_INET address.
99 if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
101 yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
102 w->hostport, gai_strerror(res));
107 unsigned char addrbuf[4];
108 assert(addrinfo->ai_family == PF_INET);
110 &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
111 sprintf(ipport, "%u.%u.%u.%u:%s",
112 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
113 freeaddrinfo(addrinfo);
114 w->ipport = xstrdup(ipport);
115 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
118 hp = gethostbyname(hostport);
121 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to resolve %s", hostport);
126 unsigned char addrbuf[4];
128 memcpy(addrbuf, *hp->h_addr_list, 4 * sizeof(unsigned char));
129 sprintf(ipport, "%u.%u.%u.%u:%s",
130 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
131 w->ipport = xstrdup(ipport);
132 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
138 static void work_handler(void *vp)
142 int sec = 0; /* >0 for debugging/testing purposes */
145 yaz_log(log_level, "waiting %d seconds", sec);
150 perform_getaddrinfo(w);
153 #if USE_THREADED_RESOLVER
154 void iochan_handler(struct iochan *i, int event)
156 sel_thread_t p = iochan_getdata(i);
158 if (event & EVENT_INPUT)
160 struct work *w = sel_thread_result(p);
161 w->host->ipport = w->ipport;
162 connect_resolver_host(w->host, w->iochan_man);
167 static sel_thread_t resolver_thread = 0;
169 static void getaddrinfo_start(iochan_man_t iochan_man)
172 sel_thread_t p = resolver_thread =
173 sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
174 3 /* no of resolver threads */);
177 yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
182 IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT,
183 "getaddrinfo_socket");
184 iochan_setdata(chan, p);
185 iochan_add(iochan_man, chan);
187 yaz_log(log_level, "resolver start");
192 int host_getaddrinfo(struct host *host, iochan_man_t iochan_man)
194 struct work *w = xmalloc(sizeof(*w));
195 int use_thread = 0; /* =0 to disable threading entirely */
197 w->hostport = host->hostport;
200 w->iochan_man = iochan_man;
201 #if USE_THREADED_RESOLVER
204 if (resolver_thread == 0)
205 getaddrinfo_start(iochan_man);
206 assert(resolver_thread);
207 sel_thread_add(resolver_thread, w);
211 perform_getaddrinfo(w);
212 host->ipport = w->ipport;
222 * c-file-style: "Stroustrup"
223 * indent-tabs-mode: nil
225 * vim: shiftwidth=4 tabstop=8 expandtab