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