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>
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);
82 if ((port = strchr(hostport, ':')))
91 hints.ai_family = PF_INET;
92 hints.ai_socktype = SOCK_STREAM;
93 hints.ai_protocol = IPPROTO_TCP;
96 hints.ai_canonname = 0;
98 // This is not robust code. It assumes that getaddrinfo always
99 // returns AF_INET address.
100 if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
102 yaz_log(YLOG_WARN, "Failed to resolve %s %s",
103 w->hostport, gai_strerror(res));
108 unsigned char addrbuf[4];
109 assert(addrinfo->ai_family == PF_INET);
111 &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
112 sprintf(ipport, "%u.%u.%u.%u:%s",
113 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
114 freeaddrinfo(addrinfo);
115 w->ipport = xstrdup(ipport);
116 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
119 hp = gethostbyname(hostport);
122 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to resolve %s", hostport);
127 unsigned char addrbuf[4];
129 memcpy(addrbuf, *hp->h_addr_list, 4 * sizeof(unsigned char));
130 sprintf(ipport, "%u.%u.%u.%u:%s",
131 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
132 w->ipport = xstrdup(ipport);
133 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
139 static void work_handler(void *vp)
143 int sec = 0; /* >0 for debugging/testing purposes */
146 yaz_log(log_level, "waiting %d seconds", sec);
151 perform_getaddrinfo(w);
154 #if USE_THREADED_RESOLVER
155 void iochan_handler(struct iochan *i, int event)
157 sel_thread_t p = iochan_getdata(i);
159 if (event & EVENT_INPUT)
161 struct work *w = sel_thread_result(p);
162 w->host->ipport = w->ipport;
163 connect_resolver_host(w->host, w->iochan_man);
168 static sel_thread_t resolver_thread = 0;
170 static void getaddrinfo_start(iochan_man_t iochan_man)
173 sel_thread_t p = resolver_thread =
174 sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
175 3 /* no of resolver threads */);
178 yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
183 IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT,
184 "getaddrinfo_socket");
185 iochan_setdata(chan, p);
186 iochan_add(iochan_man, chan);
188 yaz_log(log_level, "resolver start");
193 int host_getaddrinfo(struct host *host, iochan_man_t iochan_man)
195 struct work *w = xmalloc(sizeof(*w));
196 int use_thread = 0; /* =0 to disable threading entirely */
198 w->hostport = host->tproxy ? host->tproxy : host->proxy;
201 w->iochan_man = iochan_man;
202 #if USE_THREADED_RESOLVER
205 if (resolver_thread == 0)
206 getaddrinfo_start(iochan_man);
207 assert(resolver_thread);
208 sel_thread_add(resolver_thread, w);
212 perform_getaddrinfo(w);
213 host->ipport = w->ipport;
223 * c-file-style: "Stroustrup"
224 * indent-tabs-mode: nil
226 * vim: shiftwidth=4 tabstop=8 expandtab