Z39.50 session/connect timeout in parameters
[pazpar2-moved-to-github.git] / src / getaddrinfo.c
1 /* $Id: getaddrinfo.c,v 1.7 2007-07-25 11:41:32 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #if HAVE_CONFIG_H
23 #include "cconfig.h"
24 #endif
25
26 #include "sel_thread.h"
27
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <pthread.h>
31 #include <assert.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netdb.h>
35 #include <netinet/in.h>
36
37 #include <yaz/log.h>
38 #include <yaz/nmem.h>
39 #include <yaz/tcpip.h>
40
41 #include "pazpar2.h"
42 #include "connection.h"
43 #include "host.h"
44
45 struct work {
46     char *hostport;  /* hostport to be resolved in separate thread */
47     char *ipport;    /* result or NULL if it could not be resolved */
48     struct host *host; /* host that we're dealing with - mother thread */
49 };
50
51 static int log_level = YLOG_LOG;
52
53 void perform_getaddrinfo(struct work *w)
54 {
55     int res = 0;
56     char *port;
57 #if HAVE_GETADDRINFO
58     struct addrinfo *addrinfo, hints;
59 #else
60     struct hostent *hp;
61 #endif
62     char *hostport = xstrdup(w->hostport);
63     
64     if ((port = strchr(hostport, ':')))
65         *(port++) = '\0';
66     else
67         port = "210";
68
69     yaz_log(YLOG_LOG, "Resolving %s", w->hostport);
70     sleep(10);
71 #if HAVE_GETADDRINFO
72     hints.ai_flags = 0;
73     hints.ai_family = PF_INET;
74     hints.ai_socktype = SOCK_STREAM;
75     hints.ai_protocol = IPPROTO_TCP;
76     hints.ai_addrlen = 0;
77     hints.ai_addr = 0;
78     hints.ai_canonname = 0;
79     hints.ai_next = 0;
80     // This is not robust code. It assumes that getaddrinfo always
81     // returns AF_INET address.
82     if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
83     {
84         yaz_log(YLOG_WARN, "Failed to resolve %s: %s", 
85                 w->hostport, gai_strerror(res));
86     }
87     else
88     {
89         char ipport[128];
90         unsigned char addrbuf[4];
91         assert(addrinfo->ai_family == PF_INET);
92         memcpy(addrbuf, 
93                &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
94         sprintf(ipport, "%u.%u.%u.%u:%s",
95                 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
96         freeaddrinfo(addrinfo);
97         w->ipport = xstrdup(ipport);
98         yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
99     }
100 #else
101     hp = gethostbyname(hostport);
102     if (!hp)
103     {
104         yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to resolve %s", hostport);
105     }
106     else
107     {
108         char ipport[128];
109         unsigned char addrbuf[4];
110
111         memcpy(addrbuf, *hp->h_addr_list, 4 * sizeof(unsigned char));
112         sprintf(ipport, "%u.%u.%u.%u:%s",
113                 addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
114         w->ipport = xstrdup(ipport);
115         yaz_log(log_level, "Resolved %s -> %s", hostport, ipport);
116     }
117 #endif
118     xfree(hostport);
119 }
120
121 static void work_handler(void *vp)
122 {
123     struct work *w = vp;
124
125     int sec = 0;  /* >0 for debugging/testing purposes */
126     if (sec)
127     {
128         yaz_log(log_level, "waiting %d seconds", sec);
129         sleep(sec);
130     }
131     perform_getaddrinfo(w);
132 }
133
134 void iochan_handler(struct iochan *i, int event)
135 {
136     sel_thread_t p = iochan_getdata(i);
137
138     if (event & EVENT_INPUT)
139     {
140         struct work *w = sel_thread_result(p);
141         w->host->ipport = w->ipport;
142         connect_resolver_host(w->host);
143         xfree(w);
144     }
145 }
146
147 static sel_thread_t resolver_thread = 0;
148
149 static void getaddrinfo_start(void)
150 {
151     int fd;
152     sel_thread_t p = resolver_thread = 
153         sel_thread_create(work_handler, 0 /* work_destroy */, &fd,
154                           3 /* no of resolver threads */);
155     if (!p)
156     {
157         yaz_log(YLOG_FATAL|YLOG_ERRNO, "sel_create_create failed");
158         exit(1);
159     }
160     else
161     {
162         IOCHAN chan = iochan_create(fd, iochan_handler, EVENT_INPUT);
163         iochan_setdata(chan, p);
164         pazpar2_add_channel(chan);
165     }
166     yaz_log(log_level, "resolver start");
167     resolver_thread = p;
168 }
169
170 int host_getaddrinfo(struct host *host)
171 {
172     struct work *w = xmalloc(sizeof(*w));
173     int use_thread = 1; /* =0 to disable threading entirely */
174
175     w->hostport = host->hostport;
176     w->ipport = 0;
177     w->host = host;
178     if (use_thread)
179     {
180         if (resolver_thread == 0)
181             getaddrinfo_start();
182         assert(resolver_thread);
183         sel_thread_add(resolver_thread, w);
184     }
185     else
186     {
187         perform_getaddrinfo(w);
188         host->ipport = w->ipport;
189         xfree(w);
190         if (!host->ipport)
191             return -1;
192     }
193     return 0;
194 }
195
196 /*
197  * Local variables:
198  * c-basic-offset: 4
199  * indent-tabs-mode: nil
200  * End:
201  * vim: shiftwidth=4 tabstop=8 expandtab
202  */