Remove trailing whitespace
[pazpar2-moved-to-github.git] / src / getaddrinfo.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2012 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     if ((port = strchr(hostport, ':')))
83         *(port++) = '\0';
84     else
85     {
86         port = "210";
87     }
88
89 #if HAVE_GETADDRINFO
90     hints.ai_flags = 0;
91     hints.ai_family = PF_INET;
92     hints.ai_socktype = SOCK_STREAM;
93     hints.ai_protocol = IPPROTO_TCP;
94     hints.ai_addrlen = 0;
95     hints.ai_addr = 0;
96     hints.ai_canonname = 0;
97     hints.ai_next = 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)))
101     {
102         yaz_log(YLOG_WARN, "Failed to resolve %s %s",
103                 w->hostport, gai_strerror(res));
104     }
105     else
106     {
107         char ipport[128];
108         unsigned char addrbuf[4];
109         assert(addrinfo->ai_family == PF_INET);
110         memcpy(addrbuf,
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);
117     }
118 #else
119     hp = gethostbyname(hostport);
120     if (!hp)
121     {
122         yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to resolve %s", hostport);
123     }
124     else
125     {
126         char ipport[128];
127         unsigned char addrbuf[4];
128
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);
134     }
135 #endif
136     xfree(hostport);
137 }
138
139 static void work_handler(void *vp)
140 {
141     struct work *w = vp;
142
143     int sec = 0;  /* >0 for debugging/testing purposes */
144     if (sec)
145     {
146         yaz_log(log_level, "waiting %d seconds", sec);
147 #if HAVE_UNISTD_H
148         sleep(sec);
149 #endif
150     }
151     perform_getaddrinfo(w);
152 }
153
154 #if USE_THREADED_RESOLVER
155 void iochan_handler(struct iochan *i, int event)
156 {
157     sel_thread_t p = iochan_getdata(i);
158
159     if (event & EVENT_INPUT)
160     {
161         struct work *w = sel_thread_result(p);
162         w->host->ipport = w->ipport;
163         connect_resolver_host(w->host, w->iochan_man);
164         xfree(w);
165     }
166 }
167
168 static sel_thread_t resolver_thread = 0;
169
170 static void getaddrinfo_start(iochan_man_t iochan_man)
171 {
172     int fd;
173     sel_thread_t p = resolver_thread =
174         sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
175                           3 /* no of resolver threads */);
176     if (!p)
177     {
178         yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
179         exit(1);
180     }
181     else
182     {
183         IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT,
184             "getaddrinfo_socket");
185         iochan_setdata(chan, p);
186         iochan_add(iochan_man, chan);
187     }
188     yaz_log(log_level, "resolver start");
189     resolver_thread = p;
190 }
191 #endif
192
193 int host_getaddrinfo(struct host *host, iochan_man_t iochan_man)
194 {
195     struct work *w = xmalloc(sizeof(*w));
196     int use_thread = 0; /* =0 to disable threading entirely */
197
198     w->hostport = host->tproxy ? host->tproxy : host->proxy;
199     w->ipport = 0;
200     w->host = host;
201     w->iochan_man = iochan_man;
202 #if USE_THREADED_RESOLVER
203     if (use_thread)
204     {
205         if (resolver_thread == 0)
206             getaddrinfo_start(iochan_man);
207         assert(resolver_thread);
208         sel_thread_add(resolver_thread, w);
209         return 0;
210     }
211 #endif
212     perform_getaddrinfo(w);
213     host->ipport = w->ipport;
214     xfree(w);
215     if (!host->ipport)
216         return -1;
217     return 0;
218 }
219
220 /*
221  * Local variables:
222  * c-basic-offset: 4
223  * c-file-style: "Stroustrup"
224  * indent-tabs-mode: nil
225  * End:
226  * vim: shiftwidth=4 tabstop=8 expandtab
227  */
228