Add ws2tcpip.h for getaddrinfo on Windows
[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 #if HAVE_NETINET_IN_H
44 #include <netinet/in.h>
45 #endif
46
47 #include <yaz/log.h>
48 #include <yaz/nmem.h>
49 #include <yaz/tcpip.h>
50
51 #include "session.h"
52 #include "connection.h"
53 #include "host.h"
54
55 /* Only use a threaded resolver on Unix that offers getaddrinfo.
56    gethostbyname is NOT reentrant.
57  */
58 #ifndef WIN32
59 #define USE_THREADED_RESOLVER 1
60 #endif
61
62 struct work {
63     char *hostport;  /* hostport to be resolved in separate thread */
64     char *ipport;    /* result or NULL if it could not be resolved */
65     struct host *host; /* host that we're dealing with - mother thread */
66     iochan_man_t iochan_man; /* iochan manager */
67 };
68
69 static int log_level = YLOG_LOG;
70
71 void perform_getaddrinfo(struct work *w)
72 {
73     struct addrinfo hints, *res;
74     char host[512], *cp;
75     const char *port = 0;
76     int error;
77
78     hints.ai_flags = 0;
79     hints.ai_family = AF_UNSPEC;
80     hints.ai_socktype = SOCK_STREAM;
81     hints.ai_protocol = 0;
82     hints.ai_addrlen        = 0;
83     hints.ai_addr           = NULL;
84     hints.ai_canonname      = NULL;
85     hints.ai_next           = NULL;
86
87     strncpy(host, w->hostport, sizeof(host)-1);
88     host[sizeof(host)-1] = 0;
89     if ((cp = strrchr(host, ':')))
90     {
91         *cp = '\0';
92         port = cp + 1;
93     }
94     error = getaddrinfo(host, port ? port : "210", &hints, &res);
95     if (error)
96     {
97         yaz_log(YLOG_WARN, "Failed to resolve %s: %s",
98                 w->hostport, gai_strerror(error));
99     }
100     else
101     {
102         if (getnameinfo((struct sockaddr *) res->ai_addr, res->ai_addrlen,
103                         host, sizeof(host)-1,
104                         0, 0,
105                         NI_NUMERICHOST) == 0)
106         {
107             w->ipport = xmalloc(strlen(host) + (port ? strlen(port) : 0) + 2);
108             strcpy(w->ipport, host);
109             if (port)
110             {
111                 strcat(w->ipport, ":");
112                 strcat(w->ipport, port);
113             }
114             yaz_log(log_level, "Resolved %s -> %s", w->hostport, w->ipport);
115         }
116         else
117         {
118             yaz_log(YLOG_LOG|YLOG_ERRNO, "getnameinfo failed for %s",
119                     w->hostport);
120         }
121         freeaddrinfo(res);
122     }
123 }
124
125 static void work_handler(void *vp)
126 {
127     struct work *w = vp;
128
129     int sec = 0;  /* >0 for debugging/testing purposes */
130     if (sec)
131     {
132         yaz_log(log_level, "waiting %d seconds", sec);
133 #if HAVE_UNISTD_H
134         sleep(sec);
135 #endif
136     }
137     perform_getaddrinfo(w);
138 }
139
140 #if USE_THREADED_RESOLVER
141 void iochan_handler(struct iochan *i, int event)
142 {
143     sel_thread_t p = iochan_getdata(i);
144
145     if (event & EVENT_INPUT)
146     {
147         struct work *w = sel_thread_result(p);
148         w->host->ipport = w->ipport;
149         connect_resolver_host(w->host, w->iochan_man);
150         xfree(w);
151     }
152 }
153
154 static sel_thread_t resolver_thread = 0;
155
156 static void getaddrinfo_start(iochan_man_t iochan_man)
157 {
158     int fd;
159     sel_thread_t p = resolver_thread =
160         sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
161                           3 /* no of resolver threads */);
162     if (!p)
163     {
164         yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
165         exit(1);
166     }
167     else
168     {
169         IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT,
170             "getaddrinfo_socket");
171         iochan_setdata(chan, p);
172         iochan_add(iochan_man, chan);
173     }
174     yaz_log(log_level, "resolver start");
175     resolver_thread = p;
176 }
177 #endif
178
179 int host_getaddrinfo(struct host *host, iochan_man_t iochan_man)
180 {
181     struct work *w = xmalloc(sizeof(*w));
182     int use_thread = 0; /* =0 to disable threading entirely */
183
184     w->hostport = host->tproxy ? host->tproxy : host->proxy;
185     w->ipport = 0;
186     w->host = host;
187     w->iochan_man = iochan_man;
188 #if USE_THREADED_RESOLVER
189     if (use_thread)
190     {
191         if (resolver_thread == 0)
192             getaddrinfo_start(iochan_man);
193         assert(resolver_thread);
194         sel_thread_add(resolver_thread, w);
195         return 0;
196     }
197 #endif
198     perform_getaddrinfo(w);
199     host->ipport = w->ipport;
200     xfree(w);
201     if (!host->ipport)
202         return -1;
203     return 0;
204 }
205
206 /*
207  * Local variables:
208  * c-basic-offset: 4
209  * c-file-style: "Stroustrup"
210  * indent-tabs-mode: nil
211  * End:
212  * vim: shiftwidth=4 tabstop=8 expandtab
213  */
214