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