Use getaddrinfo for HTTP listener
[pazpar2-moved-to-github.git] / src / getaddrinfo.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2013 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 };
65
66 static int log_level = YLOG_LOG;
67
68 void perform_getaddrinfo(struct work *w)
69 {
70     struct addrinfo hints, *res;
71     char host[512], *cp;
72     const char *port = 0;
73     int error;
74
75     hints.ai_flags = 0;
76     hints.ai_family = AF_UNSPEC;
77     hints.ai_socktype = SOCK_STREAM;
78     hints.ai_protocol = 0;
79     hints.ai_addrlen        = 0;
80     hints.ai_addr           = NULL;
81     hints.ai_canonname      = NULL;
82     hints.ai_next           = NULL;
83
84     strncpy(host, w->hostport, sizeof(host)-1);
85     host[sizeof(host)-1] = 0;
86     if ((cp = strrchr(host, ':')))
87     {
88         *cp = '\0';
89         port = cp + 1;
90     }
91     error = getaddrinfo(host, port ? port : "210", &hints, &res);
92     if (error)
93     {
94         yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
95                 w->hostport, gai_strerror(error));
96     }
97     else
98     {
99         if (getnameinfo((struct sockaddr *) res->ai_addr, res->ai_addrlen,
100                         host, sizeof(host)-1,
101                         0, 0,
102                         NI_NUMERICHOST) == 0)
103         {
104             w->ipport = xmalloc(strlen(host) + (port ? strlen(port) : 0) + 2);
105             strcpy(w->ipport, host);
106             if (port)
107             {
108                 strcat(w->ipport, ":");
109                 strcat(w->ipport, port);
110             }
111             yaz_log(log_level, "Resolved %s -> %s", w->hostport, w->ipport);
112         }
113         else
114         {
115             yaz_log(YLOG_LOG|YLOG_ERRNO, "getnameinfo failed for %s",
116                     w->hostport);
117         }
118         freeaddrinfo(res);
119     }
120 }
121
122 static void work_handler(void *vp)
123 {
124     struct work *w = vp;
125
126     int sec = 0;  /* >0 for debugging/testing purposes */
127     if (sec)
128     {
129         yaz_log(log_level, "waiting %d seconds", sec);
130 #if HAVE_UNISTD_H
131         sleep(sec);
132 #endif
133     }
134     perform_getaddrinfo(w);
135 }
136
137 #if USE_THREADED_RESOLVER
138 void iochan_handler(struct iochan *i, int event)
139 {
140     sel_thread_t p = iochan_getdata(i);
141
142     if (event & EVENT_INPUT)
143     {
144         struct work *w = sel_thread_result(p);
145         w->host->ipport = w->ipport;
146         connect_resolver_host(w->host, w->iochan_man);
147         xfree(w);
148     }
149 }
150
151 static sel_thread_t resolver_thread = 0;
152
153 static void getaddrinfo_start(iochan_man_t iochan_man)
154 {
155     int fd;
156     sel_thread_t p = resolver_thread =
157         sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
158                           3 /* no of resolver threads */);
159     if (!p)
160     {
161         yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
162         exit(1);
163     }
164     else
165     {
166         IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT,
167             "getaddrinfo_socket");
168         iochan_setdata(chan, p);
169         iochan_add(iochan_man, chan);
170     }
171     yaz_log(log_level, "resolver start");
172     resolver_thread = p;
173 }
174 #endif
175
176 int host_getaddrinfo(struct host *host, iochan_man_t iochan_man)
177 {
178     struct work *w = xmalloc(sizeof(*w));
179     int use_thread = 0; /* =0 to disable threading entirely */
180
181     w->hostport = host->tproxy ? host->tproxy : host->proxy;
182     w->ipport = 0;
183     w->host = host;
184     w->iochan_man = iochan_man;
185 #if USE_THREADED_RESOLVER
186     if (use_thread)
187     {
188         if (resolver_thread == 0)
189             getaddrinfo_start(iochan_man);
190         assert(resolver_thread);
191         sel_thread_add(resolver_thread, w);
192         return 0;
193     }
194 #endif
195     perform_getaddrinfo(w);
196     host->ipport = w->ipport;
197     xfree(w);
198     if (!host->ipport)
199         return -1;
200     return 0;
201 }
202
203 /*
204  * Local variables:
205  * c-basic-offset: 4
206  * c-file-style: "Stroustrup"
207  * indent-tabs-mode: nil
208  * End:
209  * vim: shiftwidth=4 tabstop=8 expandtab
210  */
211