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