Pazpar2 compiles on Windows. But does not yet work
[pazpar2-moved-to-github.git] / src / getaddrinfo.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 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 <winsock.h>
38 #endif
39 #if HAVE_NETDB_H
40 #include <netdb.h>
41 #endif
42 #if HAVE_NETINET_IN_H
43 #include <netinet/in.h>
44 #endif
45
46 #include <yaz/log.h>
47 #include <yaz/nmem.h>
48 #include <yaz/tcpip.h>
49
50 #include "pazpar2.h"
51 #include "connection.h"
52 #include "host.h"
53
54 struct work {
55     char *hostport;  /* hostport to be resolved in separate thread */
56     char *ipport;    /* result or NULL if it could not be resolved */
57     struct host *host; /* host that we're dealing with - mother thread */
58 };
59
60 static int log_level = YLOG_LOG;
61
62 void perform_getaddrinfo(struct work *w)
63 {
64     int res = 0;
65     char *port;
66 #if HAVE_GETADDRINFO
67     struct addrinfo *addrinfo, hints;
68 #else
69     struct hostent *hp;
70 #endif
71     char *hostport = xstrdup(w->hostport);
72     
73     if ((port = strchr(hostport, ':')))
74         *(port++) = '\0';
75     else
76         port = "210";
77
78 #if HAVE_GETADDRINFO
79     hints.ai_flags = 0;
80     hints.ai_family = PF_INET;
81     hints.ai_socktype = SOCK_STREAM;
82     hints.ai_protocol = IPPROTO_TCP;
83     hints.ai_addrlen = 0;
84     hints.ai_addr = 0;
85     hints.ai_canonname = 0;
86     hints.ai_next = 0;
87     // This is not robust code. It assumes that getaddrinfo always
88     // returns AF_INET address.
89     if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
90     {
91         yaz_log(YLOG_WARN, "Failed to resolve %s: %s", 
92                 w->hostport, gai_strerror(res));
93     }
94     else
95     {
96         char ipport[128];
97         unsigned char addrbuf[4];
98         assert(addrinfo->ai_family == PF_INET);
99         memcpy(addrbuf, 
100                &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
101         sprintf(ipport, "%u.%u.%u.%u:%s",
102                 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
103         freeaddrinfo(addrinfo);
104         w->ipport = xstrdup(ipport);
105         yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
106     }
107 #else
108     hp = gethostbyname(hostport);
109     if (!hp)
110     {
111         yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to resolve %s", hostport);
112     }
113     else
114     {
115         char ipport[128];
116         unsigned char addrbuf[4];
117
118         memcpy(addrbuf, *hp->h_addr_list, 4 * sizeof(unsigned char));
119         sprintf(ipport, "%u.%u.%u.%u:%s",
120                 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
121         w->ipport = xstrdup(ipport);
122         yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
123     }
124 #endif
125     xfree(hostport);
126 }
127
128 static void work_handler(void *vp)
129 {
130     struct work *w = vp;
131
132     int sec = 0;  /* >0 for debugging/testing purposes */
133     if (sec)
134     {
135         yaz_log(log_level, "waiting %d seconds", sec);
136 #if HAVE_UNISTD_H
137         sleep(sec);
138 #endif
139     }
140     perform_getaddrinfo(w);
141 }
142
143 #ifndef WIN32
144 void iochan_handler(struct iochan *i, int event)
145 {
146     sel_thread_t p = iochan_getdata(i);
147
148     if (event & EVENT_INPUT)
149     {
150         struct work *w = sel_thread_result(p);
151         w->host->ipport = w->ipport;
152         connect_resolver_host(w->host);
153         xfree(w);
154     }
155 }
156
157 static sel_thread_t resolver_thread = 0;
158
159 static void getaddrinfo_start(void)
160 {
161     int fd;
162     sel_thread_t p = resolver_thread = 
163         sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
164                           3 /* no of resolver threads */);
165     if (!p)
166     {
167         yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
168         exit(1);
169     }
170     else
171     {
172         IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT);
173         iochan_setdata(chan, p);
174         pazpar2_add_channel(chan);
175     }
176     yaz_log(log_level, "resolver start");
177     resolver_thread = p;
178 }
179 #endif
180
181 int host_getaddrinfo(struct host *host)
182 {
183     struct work *w = xmalloc(sizeof(*w));
184     int use_thread = 1; /* =0 to disable threading entirely */
185
186     w->hostport = host->hostport;
187     w->ipport = 0;
188     w->host = host;
189 #ifndef WIN32
190     if (use_thread)
191     {
192         if (resolver_thread == 0)
193             getaddrinfo_start();
194         assert(resolver_thread);
195         sel_thread_add(resolver_thread, w);
196         return 0;
197     }
198 #endif
199     perform_getaddrinfo(w);
200     host->ipport = w->ipport;
201     xfree(w);
202     if (!host->ipport)
203         return -1;
204     return 0;
205 }
206
207 /*
208  * Local variables:
209  * c-basic-offset: 4
210  * indent-tabs-mode: nil
211  * End:
212  * vim: shiftwidth=4 tabstop=8 expandtab
213  */