Pazpar2 1.4.4-2 for squeeze
[pazpar2-moved-to-github.git] / src / getaddrinfo.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "sel_thread.h"
25
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include <stdlib.h>
30
31 #include <assert.h>
32 #include <sys/types.h>
33 #if HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef WIN32
37 #include <winsock.h>
38 #endif
39 #if HAVE_NETDB_H
40 #include <netdb.h>
41 #endif
42 #if HAVE_NETINET_IN_H
43 #include <netinet/in.h>
44 #endif
45
46 #include <yaz/log.h>
47 #include <yaz/nmem.h>
48 #include <yaz/tcpip.h>
49
50 #include "session.h"
51 #include "connection.h"
52 #include "host.h"
53
54 /* Only use a threaded resolver on Unix that offers getaddrinfo.
55    gethostbyname is NOT reentrant.
56  */
57 #if HAVE_GETADDRINFO
58 #ifndef WIN32
59 #define USE_THREADED_RESOLVER 1
60 #endif
61 #endif
62
63 struct work {
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 */
68 };
69
70 static int log_level = YLOG_LOG;
71
72 void perform_getaddrinfo(struct work *w)
73 {
74     int res = 0;
75     char *port;
76 #if HAVE_GETADDRINFO
77     struct addrinfo *addrinfo, hints;
78 #else
79     struct hostent *hp;
80 #endif
81     char *hostport = xstrdup(w->hostport);
82     
83     if ((port = strchr(hostport, ':')))
84         *(port++) = '\0';
85     else
86         port = "210";
87
88 #if HAVE_GETADDRINFO
89     hints.ai_flags = 0;
90     hints.ai_family = PF_INET;
91     hints.ai_socktype = SOCK_STREAM;
92     hints.ai_protocol = IPPROTO_TCP;
93     hints.ai_addrlen = 0;
94     hints.ai_addr = 0;
95     hints.ai_canonname = 0;
96     hints.ai_next = 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)))
100     {
101         yaz_log(YLOG_WARN, "Failed to resolve %s: %s", 
102                 w->hostport, gai_strerror(res));
103     }
104     else
105     {
106         char ipport[128];
107         unsigned char addrbuf[4];
108         assert(addrinfo->ai_family == PF_INET);
109         memcpy(addrbuf, 
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);
116     }
117 #else
118     hp = gethostbyname(hostport);
119     if (!hp)
120     {
121         yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to resolve %s", hostport);
122     }
123     else
124     {
125         char ipport[128];
126         unsigned char addrbuf[4];
127
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);
133     }
134 #endif
135     xfree(hostport);
136 }
137
138 static void work_handler(void *vp)
139 {
140     struct work *w = vp;
141
142     int sec = 0;  /* >0 for debugging/testing purposes */
143     if (sec)
144     {
145         yaz_log(log_level, "waiting %d seconds", sec);
146 #if HAVE_UNISTD_H
147         sleep(sec);
148 #endif
149     }
150     perform_getaddrinfo(w);
151 }
152
153 #if USE_THREADED_RESOLVER
154 void iochan_handler(struct iochan *i, int event)
155 {
156     sel_thread_t p = iochan_getdata(i);
157
158     if (event & EVENT_INPUT)
159     {
160         struct work *w = sel_thread_result(p);
161         w->host->ipport = w->ipport;
162         connect_resolver_host(w->host, w->iochan_man);
163         xfree(w);
164     }
165 }
166
167 static sel_thread_t resolver_thread = 0;
168
169 static void getaddrinfo_start(iochan_man_t iochan_man)
170 {
171     int fd;
172     sel_thread_t p = resolver_thread = 
173         sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
174                           3 /* no of resolver threads */);
175     if (!p)
176     {
177         yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
178         exit(1);
179     }
180     else
181     {
182         IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT,
183             "getaddrinfo_socket");
184         iochan_setdata(chan, p);
185         iochan_add(iochan_man, chan);
186     }
187     yaz_log(log_level, "resolver start");
188     resolver_thread = p;
189 }
190 #endif
191
192 int host_getaddrinfo(struct host *host, iochan_man_t iochan_man)
193 {
194     struct work *w = xmalloc(sizeof(*w));
195     int use_thread = 0; /* =0 to disable threading entirely */
196
197     w->hostport = host->hostport;
198     w->ipport = 0;
199     w->host = host;
200     w->iochan_man = iochan_man;
201 #if USE_THREADED_RESOLVER
202     if (use_thread)
203     {
204         if (resolver_thread == 0)
205             getaddrinfo_start(iochan_man);
206         assert(resolver_thread);
207         sel_thread_add(resolver_thread, w);
208         return 0;
209     }
210 #endif
211     perform_getaddrinfo(w);
212     host->ipport = w->ipport;
213     xfree(w);
214     if (!host->ipport)
215         return -1;
216     return 0;
217 }
218
219 /*
220  * Local variables:
221  * c-basic-offset: 4
222  * c-file-style: "Stroustrup"
223  * indent-tabs-mode: nil
224  * End:
225  * vim: shiftwidth=4 tabstop=8 expandtab
226  */
227