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