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"
55 char *hostport; /* hostport to be resolved in separate thread */
56 char *ipport; /* result or NULL if it could not be resolved */
57 struct host *host; /* host that we're dealing with - mother thread */
60 static int log_level = YLOG_LOG;
62 void perform_getaddrinfo(struct work *w)
67 struct addrinfo *addrinfo, hints;
71 char *hostport = xstrdup(w->hostport);
73 if ((port = strchr(hostport, ':')))
80 hints.ai_family = PF_INET;
81 hints.ai_socktype = SOCK_STREAM;
82 hints.ai_protocol = IPPROTO_TCP;
85 hints.ai_canonname = 0;
87 // This is not robust code. It assumes that getaddrinfo always
88 // returns AF_INET address.
89 if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
91 yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
92 w->hostport, gai_strerror(res));
97 unsigned char addrbuf[4];
98 assert(addrinfo->ai_family == PF_INET);
100 &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
101 sprintf(ipport, "%u.%u.%u.%u:%s",
102 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
103 freeaddrinfo(addrinfo);
104 w->ipport = xstrdup(ipport);
105 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
108 hp = gethostbyname(hostport);
111 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to resolve %s", hostport);
116 unsigned char addrbuf[4];
118 memcpy(addrbuf, *hp->h_addr_list, 4 * sizeof(unsigned char));
119 sprintf(ipport, "%u.%u.%u.%u:%s",
120 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
121 w->ipport = xstrdup(ipport);
122 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
128 static void work_handler(void *vp)
132 int sec = 0; /* >0 for debugging/testing purposes */
135 yaz_log(log_level, "waiting %d seconds", sec);
140 perform_getaddrinfo(w);
144 void iochan_handler(struct iochan *i, int event)
146 sel_thread_t p = iochan_getdata(i);
148 if (event & EVENT_INPUT)
150 struct work *w = sel_thread_result(p);
151 w->host->ipport = w->ipport;
152 connect_resolver_host(w->host);
157 static sel_thread_t resolver_thread = 0;
159 static void getaddrinfo_start(void)
162 sel_thread_t p = resolver_thread =
163 sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
164 3 /* no of resolver threads */);
167 yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
172 IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT);
173 iochan_setdata(chan, p);
174 pazpar2_add_channel(chan);
176 yaz_log(log_level, "resolver start");
181 int host_getaddrinfo(struct host *host)
183 struct work *w = xmalloc(sizeof(*w));
184 int use_thread = 1; /* =0 to disable threading entirely */
186 w->hostport = host->hostport;
192 if (resolver_thread == 0)
194 assert(resolver_thread);
195 sel_thread_add(resolver_thread, w);
199 perform_getaddrinfo(w);
200 host->ipport = w->ipport;
210 * c-file-style: "Stroustrup"
211 * indent-tabs-mode: nil
213 * vim: shiftwidth=4 tabstop=8 expandtab