1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 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"
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
37 #include <yaz/tcpip.h>
40 #include "connection.h"
44 char *hostport; /* hostport to be resolved in separate thread */
45 char *ipport; /* result or NULL if it could not be resolved */
46 struct host *host; /* host that we're dealing with - mother thread */
49 static int log_level = YLOG_LOG;
51 void perform_getaddrinfo(struct work *w)
56 struct addrinfo *addrinfo, hints;
60 char *hostport = xstrdup(w->hostport);
62 if ((port = strchr(hostport, ':')))
69 hints.ai_family = PF_INET;
70 hints.ai_socktype = SOCK_STREAM;
71 hints.ai_protocol = IPPROTO_TCP;
74 hints.ai_canonname = 0;
76 // This is not robust code. It assumes that getaddrinfo always
77 // returns AF_INET address.
78 if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
80 yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
81 w->hostport, gai_strerror(res));
86 unsigned char addrbuf[4];
87 assert(addrinfo->ai_family == PF_INET);
89 &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
90 sprintf(ipport, "%u.%u.%u.%u:%s",
91 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
92 freeaddrinfo(addrinfo);
93 w->ipport = xstrdup(ipport);
94 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
97 hp = gethostbyname(hostport);
100 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to resolve %s", hostport);
105 unsigned char addrbuf[4];
107 memcpy(addrbuf, *hp->h_addr_list, 4 * sizeof(unsigned char));
108 sprintf(ipport, "%u.%u.%u.%u:%s",
109 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
110 w->ipport = xstrdup(ipport);
111 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
117 static void work_handler(void *vp)
121 int sec = 0; /* >0 for debugging/testing purposes */
124 yaz_log(log_level, "waiting %d seconds", sec);
127 perform_getaddrinfo(w);
130 void iochan_handler(struct iochan *i, int event)
132 sel_thread_t p = iochan_getdata(i);
134 if (event & EVENT_INPUT)
136 struct work *w = sel_thread_result(p);
137 w->host->ipport = w->ipport;
138 connect_resolver_host(w->host);
143 static sel_thread_t resolver_thread = 0;
145 static void getaddrinfo_start(void)
148 sel_thread_t p = resolver_thread =
149 sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
150 3 /* no of resolver threads */);
153 yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
158 IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT);
159 iochan_setdata(chan, p);
160 pazpar2_add_channel(chan);
162 yaz_log(log_level, "resolver start");
166 int host_getaddrinfo(struct host *host)
168 struct work *w = xmalloc(sizeof(*w));
169 int use_thread = 1; /* =0 to disable threading entirely */
171 w->hostport = host->hostport;
176 if (resolver_thread == 0)
178 assert(resolver_thread);
179 sel_thread_add(resolver_thread, w);
183 perform_getaddrinfo(w);
184 host->ipport = w->ipport;
195 * indent-tabs-mode: nil
197 * vim: shiftwidth=4 tabstop=8 expandtab