1 /* $Id: getaddrinfo.c,v 1.4 2007-04-23 08:06:21 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "sel_thread.h"
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
39 #include <yaz/tcpip.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)
55 struct addrinfo *addrinfo, hints;
56 char *hostport = xstrdup(w->hostport);
58 if ((port = strchr(hostport, ':')))
64 hints.ai_family = PF_INET;
65 hints.ai_socktype = SOCK_STREAM;
66 hints.ai_protocol = IPPROTO_TCP;
69 hints.ai_canonname = 0;
71 // This is not robust code. It assumes that getaddrinfo always
72 // returns AF_INET address.
73 if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
75 yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
76 w->hostport, gai_strerror(res));
81 unsigned char addrbuf[4];
82 assert(addrinfo->ai_family == PF_INET);
84 &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
85 sprintf(ipport, "%u.%u.%u.%u:%s",
86 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
87 freeaddrinfo(addrinfo);
88 w->ipport = xstrdup(ipport);
89 yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
94 static void work_handler(void *vp)
98 int sec = 0; /* >0 for debugging/testing purposes */
101 yaz_log(log_level, "waiting %d seconds", sec);
104 perform_getaddrinfo(w);
107 void iochan_handler(struct iochan *i, int event)
109 sel_thread_t p = iochan_getdata(i);
111 if (event & EVENT_INPUT)
113 struct work *w = sel_thread_result(p);
114 w->host->ipport = w->ipport;
115 connect_resolver_host(w->host);
120 static sel_thread_t resolver_thread = 0;
122 static void getaddrinfo_start(void)
125 sel_thread_t p = resolver_thread =
126 sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
127 3 /* no of resolver threads */);
130 yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
135 IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT);
136 iochan_setdata(chan, p);
137 pazpar2_add_channel(chan);
139 yaz_log(log_level, "resolver start");
143 int host_getaddrinfo(struct host *host)
145 struct work *w = xmalloc(sizeof(*w));
146 int use_thread = 1; /* =0 to disable threading entirely */
148 w->hostport = host->hostport;
153 if (resolver_thread == 0)
155 assert(resolver_thread);
156 sel_thread_add(resolver_thread, w);
160 perform_getaddrinfo(w);
161 host->ipport = w->ipport;
172 * indent-tabs-mode: nil
174 * vim: shiftwidth=4 tabstop=8 expandtab