1b528cefcSMark Murray /*
2b528cefcSMark Murray  * Copyright (c) 1998 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include <config.h>
35b528cefcSMark Murray 
36c19800e8SDoug Rabson #include "roken.h"
37b528cefcSMark Murray 
38b528cefcSMark Murray #undef roken_gethostbyname
39c19800e8SDoug Rabson #undef roken_gethostbyaddr
40b528cefcSMark Murray 
41b528cefcSMark Murray static struct sockaddr_in dns_addr;
42b528cefcSMark Murray static char *dns_req;
43b528cefcSMark Murray 
44b528cefcSMark Murray static int
make_address(const char * address,struct in_addr * ip)45b528cefcSMark Murray make_address(const char *address, struct in_addr *ip)
46b528cefcSMark Murray {
47b528cefcSMark Murray     if(inet_aton(address, ip) == 0){
48b528cefcSMark Murray 	/* try to resolve as hostname, it might work if the address we
49b528cefcSMark Murray            are trying to lookup is local, for instance a web proxy */
50b528cefcSMark Murray 	struct hostent *he = gethostbyname(address);
51b528cefcSMark Murray 	if(he) {
52b528cefcSMark Murray 	    unsigned char *p = (unsigned char*)he->h_addr;
53b528cefcSMark Murray 	    ip->s_addr = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
54b528cefcSMark Murray 	} else {
55b528cefcSMark Murray 	    return -1;
56b528cefcSMark Murray 	}
57b528cefcSMark Murray     }
58b528cefcSMark Murray     return 0;
59b528cefcSMark Murray }
60b528cefcSMark Murray 
61b528cefcSMark Murray static int
setup_int(const char * proxy_host,short proxy_port,const char * dns_host,short dns_port,const char * dns_path)62b528cefcSMark Murray setup_int(const char *proxy_host, short proxy_port,
63b528cefcSMark Murray 	  const char *dns_host, short dns_port,
64b528cefcSMark Murray 	  const char *dns_path)
65b528cefcSMark Murray {
66b528cefcSMark Murray     memset(&dns_addr, 0, sizeof(dns_addr));
67b528cefcSMark Murray     if(dns_req)
68b528cefcSMark Murray 	free(dns_req);
69b528cefcSMark Murray     dns_req = NULL;
70b528cefcSMark Murray     if(proxy_host) {
71b528cefcSMark Murray 	if(make_address(proxy_host, &dns_addr.sin_addr) != 0)
72b528cefcSMark Murray 	    return -1;
73b528cefcSMark Murray 	dns_addr.sin_port = htons(proxy_port);
74b528cefcSMark Murray 	if (asprintf(&dns_req, "http://%s:%d%s", dns_host, dns_port, dns_path) < 0)
75b528cefcSMark Murray 	    return -1;
76b528cefcSMark Murray     } else {
77b528cefcSMark Murray 	if(make_address(dns_host, &dns_addr.sin_addr) != 0)
78b528cefcSMark Murray 	    return -1;
79b528cefcSMark Murray 	dns_addr.sin_port = htons(dns_port);
80b528cefcSMark Murray 	asprintf(&dns_req, "%s", dns_path);
81b528cefcSMark Murray     }
82b528cefcSMark Murray     dns_addr.sin_family = AF_INET;
83b528cefcSMark Murray     return 0;
84b528cefcSMark Murray }
85b528cefcSMark Murray 
86b528cefcSMark Murray static void
split_spec(const char * spec,char ** host,int * port,char ** path,int def_port)87b528cefcSMark Murray split_spec(const char *spec, char **host, int *port, char **path, int def_port)
88b528cefcSMark Murray {
89b528cefcSMark Murray     char *p;
90b528cefcSMark Murray     *host = strdup(spec);
91b528cefcSMark Murray     p = strchr(*host, ':');
92b528cefcSMark Murray     if(p) {
93b528cefcSMark Murray 	*p++ = '\0';
94b528cefcSMark Murray 	if(sscanf(p, "%d", port) != 1)
95b528cefcSMark Murray 	    *port = def_port;
96b528cefcSMark Murray     } else
97b528cefcSMark Murray 	*port = def_port;
98b528cefcSMark Murray     p = strchr(p ? p : *host, '/');
99b528cefcSMark Murray     if(p) {
100b528cefcSMark Murray 	if(path)
101b528cefcSMark Murray 	    *path = strdup(p);
102b528cefcSMark Murray 	*p = '\0';
103b528cefcSMark Murray     }else
104b528cefcSMark Murray 	if(path)
105b528cefcSMark Murray 	    *path = NULL;
106b528cefcSMark Murray }
107b528cefcSMark Murray 
108b528cefcSMark Murray 
109b528cefcSMark Murray ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
roken_gethostby_setup(const char * proxy_spec,const char * dns_spec)110c19800e8SDoug Rabson roken_gethostby_setup(const char *proxy_spec, const char *dns_spec)
111b528cefcSMark Murray {
112b528cefcSMark Murray     char *proxy_host = NULL;
113b528cefcSMark Murray     int proxy_port = 0;
114c19800e8SDoug Rabson     char *dns_host, *dns_path;
115b528cefcSMark Murray     int dns_port;
116b528cefcSMark Murray 
117b528cefcSMark Murray     int ret = -1;
118b528cefcSMark Murray 
119b528cefcSMark Murray     split_spec(dns_spec, &dns_host, &dns_port, &dns_path, 80);
120b528cefcSMark Murray     if(dns_path == NULL)
121b528cefcSMark Murray 	goto out;
122b528cefcSMark Murray     if(proxy_spec)
123b528cefcSMark Murray 	split_spec(proxy_spec, &proxy_host, &proxy_port, NULL, 80);
124b528cefcSMark Murray     ret = setup_int(proxy_host, proxy_port, dns_host, dns_port, dns_path);
125b528cefcSMark Murray out:
126b528cefcSMark Murray     free(proxy_host);
127b528cefcSMark Murray     free(dns_host);
128b528cefcSMark Murray     free(dns_path);
129b528cefcSMark Murray     return ret;
130b528cefcSMark Murray }
131b528cefcSMark Murray 
132b528cefcSMark Murray 
133b528cefcSMark Murray /* Try to lookup a name or an ip-address using http as transport
134b528cefcSMark Murray    mechanism. See the end of this file for an example program. */
135b528cefcSMark Murray static struct hostent*
roken_gethostby(const char * hostname)136b528cefcSMark Murray roken_gethostby(const char *hostname)
137b528cefcSMark Murray {
138b528cefcSMark Murray     int s;
139b528cefcSMark Murray     struct sockaddr_in addr;
140c19800e8SDoug Rabson     char *request = NULL;
141b528cefcSMark Murray     char buf[1024];
142b528cefcSMark Murray     int offset = 0;
143b528cefcSMark Murray     int n;
144b528cefcSMark Murray     char *p, *foo;
145b528cefcSMark Murray     size_t len;
146b528cefcSMark Murray 
147b528cefcSMark Murray     if(dns_addr.sin_family == 0)
148b528cefcSMark Murray 	return NULL; /* no configured host */
149c19800e8SDoug Rabson     addr = dns_addr;
150b528cefcSMark Murray     if (asprintf(&request, "GET %s?%s HTTP/1.0\r\n\r\n", dns_req, hostname) < 0)
151b528cefcSMark Murray 	return NULL;
152b528cefcSMark Murray     if(request == NULL)
153b528cefcSMark Murray 	return NULL;
154b528cefcSMark Murray     s  = socket(AF_INET, SOCK_STREAM, 0);
155b528cefcSMark Murray     if(s < 0) {
156b528cefcSMark Murray 	free(request);
157b528cefcSMark Murray 	return NULL;
158c19800e8SDoug Rabson     }
159b528cefcSMark Murray     if(connect(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
160b528cefcSMark Murray 	close(s);
161b528cefcSMark Murray 	free(request);
162b528cefcSMark Murray 	return NULL;
163b528cefcSMark Murray     }
164b528cefcSMark Murray 
165b528cefcSMark Murray     len = strlen(request);
166b528cefcSMark Murray     if(write(s, request, len) != (ssize_t)len) {
167b528cefcSMark Murray 	close(s);
168b528cefcSMark Murray 	free(request);
169b528cefcSMark Murray 	return NULL;
170b528cefcSMark Murray     }
171b528cefcSMark Murray     free(request);
172b528cefcSMark Murray     while(1) {
173b528cefcSMark Murray 	n = read(s, buf + offset, sizeof(buf) - offset);
174b528cefcSMark Murray 	if(n <= 0)
175b528cefcSMark Murray 	    break;
176b528cefcSMark Murray 	offset += n;
177b528cefcSMark Murray     }
178b528cefcSMark Murray     buf[offset] = '\0';
179b528cefcSMark Murray     close(s);
180b528cefcSMark Murray     p = strstr(buf, "\r\n\r\n"); /* find end of header */
181b528cefcSMark Murray     if(p) p += 4;
182b528cefcSMark Murray     else return NULL;
183b528cefcSMark Murray     foo = NULL;
184b528cefcSMark Murray     p = strtok_r(p, " \t\r\n", &foo);
185b528cefcSMark Murray     if(p == NULL)
186b528cefcSMark Murray 	return NULL;
187b528cefcSMark Murray     {
188b528cefcSMark Murray 	/* make a hostent to return */
189c19800e8SDoug Rabson #define MAX_ADDRS 16
190b528cefcSMark Murray 	static struct hostent he;
191b528cefcSMark Murray 	static char addrs[4 * MAX_ADDRS];
192b528cefcSMark Murray 	static char *addr_list[MAX_ADDRS + 1];
193b528cefcSMark Murray 	int num_addrs = 0;
194b528cefcSMark Murray 
195b528cefcSMark Murray 	he.h_name = p;
196b528cefcSMark Murray 	he.h_aliases = NULL;
197b528cefcSMark Murray 	he.h_addrtype = AF_INET;
198b528cefcSMark Murray 	he.h_length = 4;
199b528cefcSMark Murray 
200b528cefcSMark Murray 	while((p = strtok_r(NULL, " \t\r\n", &foo)) && num_addrs < MAX_ADDRS) {
201b528cefcSMark Murray 	    struct in_addr ip;
202b528cefcSMark Murray 	    inet_aton(p, &ip);
203b528cefcSMark Murray 	    ip.s_addr = ntohl(ip.s_addr);
204b528cefcSMark Murray 	    addr_list[num_addrs] = &addrs[num_addrs * 4];
205b528cefcSMark Murray 	    addrs[num_addrs * 4 + 0] = (ip.s_addr >> 24) & 0xff;
206b528cefcSMark Murray 	    addrs[num_addrs * 4 + 1] = (ip.s_addr >> 16) & 0xff;
207b528cefcSMark Murray 	    addrs[num_addrs * 4 + 2] = (ip.s_addr >> 8) & 0xff;
208b528cefcSMark Murray 	    addrs[num_addrs * 4 + 3] = (ip.s_addr >> 0) & 0xff;
209b528cefcSMark Murray 	    addr_list[++num_addrs] = NULL;
210b528cefcSMark Murray 	}
211b528cefcSMark Murray 	he.h_addr_list = addr_list;
212b528cefcSMark Murray 	return &he;
213b528cefcSMark Murray     }
214b528cefcSMark Murray }
215b528cefcSMark Murray 
216b528cefcSMark Murray ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL
roken_gethostbyname(const char * hostname)217b528cefcSMark Murray roken_gethostbyname(const char *hostname)
218b528cefcSMark Murray {
219b528cefcSMark Murray     struct hostent *he;
220b528cefcSMark Murray     he = gethostbyname(hostname);
221b528cefcSMark Murray     if(he)
222b528cefcSMark Murray 	return he;
223c19800e8SDoug Rabson     return roken_gethostby(hostname);
224b528cefcSMark Murray }
225b528cefcSMark Murray 
226b528cefcSMark Murray ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL
roken_gethostbyaddr(const void * addr,size_t len,int type)227b528cefcSMark Murray roken_gethostbyaddr(const void *addr, size_t len, int type)
228b528cefcSMark Murray {
229b528cefcSMark Murray     struct in_addr a;
230b528cefcSMark Murray     const char *p;
231b528cefcSMark Murray     struct hostent *he;
232b528cefcSMark Murray     he = gethostbyaddr(addr, len, type);
233b528cefcSMark Murray     if(he)
234b528cefcSMark Murray 	return he;
235b528cefcSMark Murray     if(type != AF_INET || len != 4)
236b528cefcSMark Murray 	return NULL;
237b528cefcSMark Murray     p = addr;
238b528cefcSMark Murray     a.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
239b528cefcSMark Murray     return roken_gethostby(inet_ntoa(a));
240b528cefcSMark Murray }
241b528cefcSMark Murray 
242b528cefcSMark Murray #if 0
243b528cefcSMark Murray 
244b528cefcSMark Murray /* this program can be used as a cgi `script' to lookup names and
245b528cefcSMark Murray    ip-addresses */
246b528cefcSMark Murray 
247b528cefcSMark Murray #include <stdio.h>
248b528cefcSMark Murray #include <stdlib.h>
249b528cefcSMark Murray #include <netdb.h>
250b528cefcSMark Murray #include <sys/param.h>
251b528cefcSMark Murray 
252b528cefcSMark Murray int
253b528cefcSMark Murray main(int argc, char **argv)
254b528cefcSMark Murray {
255b528cefcSMark Murray     char *query = getenv("QUERY_STRING");
256b528cefcSMark Murray     char host[MAXHOSTNAMELEN];
257b528cefcSMark Murray     int i;
258b528cefcSMark Murray     struct hostent *he;
259b528cefcSMark Murray 
260b528cefcSMark Murray     printf("Content-type: text/plain\n\n");
261b528cefcSMark Murray     if(query == NULL)
262b528cefcSMark Murray 	exit(0);
263b528cefcSMark Murray     he = gethostbyname(query);
264b528cefcSMark Murray     strncpy(host, he->h_name, sizeof(host));
265b528cefcSMark Murray     host[sizeof(host) - 1] = '\0';
266b528cefcSMark Murray     he = gethostbyaddr(he->h_addr, he->h_length, AF_INET);
267b528cefcSMark Murray     printf("%s\n", he->h_name);
268b528cefcSMark Murray     for(i = 0; he->h_addr_list[i]; i++) {
269b528cefcSMark Murray 	struct in_addr ip;
270b528cefcSMark Murray 	unsigned char *p = (unsigned char*)he->h_addr_list[i];
271b528cefcSMark Murray 	ip.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
272b528cefcSMark Murray 	printf("%s\n", inet_ntoa(ip));
273b528cefcSMark Murray     }
274b528cefcSMark Murray     exit(0);
275 }
276 
277 #endif
278