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