Merge branch 'paz-1008-a'
[pazpar2-moved-to-github.git] / src / getaddrinfo.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 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 <winsock2.h>
38 #include <ws2tcpip.h>
39 #endif
40 #if HAVE_NETDB_H
41 #include <netdb.h>
42 #endif
43
44 #include <yaz/log.h>
45 #include <yaz/nmem.h>
46 #include <yaz/tcpip.h>
47
48 #include "session.h"
49 #include "connection.h"
50 #include "host.h"
51
52 /* Only use a threaded resolver on Unix that offers getaddrinfo.
53    gethostbyname is NOT reentrant.
54  */
55 #ifndef WIN32
56 #define USE_THREADED_RESOLVER 1
57 #endif
58
59 struct work {
60     char *hostport;  /* hostport to be resolved in separate thread */
61     char *ipport;    /* result or NULL if it could not be resolved */
62     struct host *host; /* host that we're dealing with - mother thread */
63     iochan_man_t iochan_man; /* iochan manager */
64     int error;
65 };
66
67 static int log_level = YLOG_LOG;
68
69 static void perform_getaddrinfo(struct work *w)
70 {
71     struct addrinfo hints, *res;
72     char host[512], *cp;
73     char *port = "210";
74     int error;
75
76     hints.ai_flags = 0;
77     hints.ai_family = AF_UNSPEC;
78     hints.ai_socktype = SOCK_STREAM;
79     hints.ai_protocol = 0;
80     hints.ai_addrlen        = 0;
81     hints.ai_addr           = NULL;
82     hints.ai_canonname      = NULL;
83     hints.ai_next           = NULL;
84
85     if (!strncmp(w->hostport, "http://", 7))
86     {
87         port = "80";
88         strncpy(host, w->hostport + 7, sizeof(host)-1);
89     }
90     else if (!strncmp(w->hostport, "https://", 8))
91     {
92         port = "443";
93         strncpy(host, w->hostport + 8, sizeof(host)-1);
94     }
95     else
96     {
97         strncpy(host, w->hostport, sizeof(host)-1);
98     }
99     host[sizeof(host)-1] = 0;
100     if ((cp = strrchr(host, ':')))
101     {
102         *cp = '\0';
103         port = cp + 1;
104     }
105     error = getaddrinfo(host, port, &hints, &res);
106     if (error)
107     {
108         yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
109                 w->hostport, gai_strerror(error));
110         w->error = error;
111     }
112     else
113     {
114         char n_host[512];
115         if (getnameinfo((struct sockaddr *) res->ai_addr, res->ai_addrlen,
116                         n_host, sizeof(n_host)-1,
117                         0, 0,
118                         NI_NUMERICHOST) == 0)
119         {
120             w->ipport = xmalloc(strlen(n_host) + (port ? strlen(port) : 0) + 2);
121             strcpy(w->ipport, n_host);
122             if (port)
123             {
124                 strcat(w->ipport, ":");
125                 strcat(w->ipport, port);
126             }
127             yaz_log(log_level, "Resolved %s -> %s", w->hostport, w->ipport);
128         }
129         else
130         {
131             yaz_log(YLOG_LOG|YLOG_ERRNO, "getnameinfo failed for %s",
132                     w->hostport);
133         }
134         freeaddrinfo(res);
135     }
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         w->host->error = w->error;
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 = 1; /* =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     w->error = 0;
203 #if USE_THREADED_RESOLVER
204     if (use_thread)
205     {
206         if (resolver_thread == 0)
207             getaddrinfo_start(iochan_man);
208         assert(resolver_thread);
209         sel_thread_add(resolver_thread, w);
210         return 0;
211     }
212 #endif
213     perform_getaddrinfo(w);
214     host->ipport = w->ipport;
215     host->error = w->error;
216     xfree(w);
217     if (!host->ipport)
218         return -1;
219     return 0;
220 }
221
222 /*
223  * Local variables:
224  * c-basic-offset: 4
225  * c-file-style: "Stroustrup"
226  * indent-tabs-mode: nil
227  * End:
228  * vim: shiftwidth=4 tabstop=8 expandtab
229  */
230