Remove dead code [CID-10]
[heimdal.git] / lib / roken / roken_gethostby.c
blobd87a49a04b2d63d338c45a51a54ca54d568724c7
1 /*
2 * Copyright (c) 1998 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include <config.h>
36 #include "roken.h"
38 #undef roken_gethostbyname
39 #undef roken_gethostbyaddr
41 static struct sockaddr_in dns_addr;
42 static char *dns_req;
44 static int
45 make_address(const char *address, struct in_addr *ip)
47 if(inet_aton(address, ip) == 0){
48 /* try to resolve as hostname, it might work if the address we
49 are trying to lookup is local, for instance a web proxy */
50 struct hostent *he = gethostbyname(address);
51 if(he) {
52 unsigned char *p = (unsigned char*)he->h_addr;
53 ip->s_addr = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
54 } else {
55 return -1;
58 return 0;
61 static int
62 setup_int(const char *proxy_host, short proxy_port,
63 const char *dns_host, short dns_port,
64 const char *dns_path)
66 memset(&dns_addr, 0, sizeof(dns_addr));
67 if(dns_req)
68 free(dns_req);
69 if(proxy_host) {
70 if(make_address(proxy_host, &dns_addr.sin_addr) != 0)
71 return -1;
72 dns_addr.sin_port = htons(proxy_port);
73 asprintf(&dns_req, "http://%s:%d%s", dns_host, dns_port, dns_path);
74 } else {
75 if(make_address(dns_host, &dns_addr.sin_addr) != 0)
76 return -1;
77 dns_addr.sin_port = htons(dns_port);
78 asprintf(&dns_req, "%s", dns_path);
80 dns_addr.sin_family = AF_INET;
81 return 0;
84 static void
85 split_spec(const char *spec, char **host, int *port, char **path, int def_port)
87 char *p;
88 *host = strdup(spec);
89 p = strchr(*host, ':');
90 if(p) {
91 *p++ = '\0';
92 if(sscanf(p, "%d", port) != 1)
93 *port = def_port;
94 } else
95 *port = def_port;
96 p = strchr(p ? p : *host, '/');
97 if(p) {
98 if(path)
99 *path = strdup(p);
100 *p = '\0';
101 }else
102 if(path)
103 *path = NULL;
107 int ROKEN_LIB_FUNCTION
108 roken_gethostby_setup(const char *proxy_spec, const char *dns_spec)
110 char *proxy_host = NULL;
111 int proxy_port = 0;
112 char *dns_host, *dns_path;
113 int dns_port;
115 int ret = -1;
117 split_spec(dns_spec, &dns_host, &dns_port, &dns_path, 80);
118 if(dns_path == NULL)
119 goto out;
120 if(proxy_spec)
121 split_spec(proxy_spec, &proxy_host, &proxy_port, NULL, 80);
122 ret = setup_int(proxy_host, proxy_port, dns_host, dns_port, dns_path);
123 out:
124 free(proxy_host);
125 free(dns_host);
126 free(dns_path);
127 return ret;
131 /* Try to lookup a name or an ip-address using http as transport
132 mechanism. See the end of this file for an example program. */
133 static struct hostent*
134 roken_gethostby(const char *hostname)
136 int s;
137 struct sockaddr_in addr;
138 char *request;
139 char buf[1024];
140 int offset = 0;
141 int n;
142 char *p, *foo;
144 if(dns_addr.sin_family == 0)
145 return NULL; /* no configured host */
146 addr = dns_addr;
147 asprintf(&request, "GET %s?%s HTTP/1.0\r\n\r\n", dns_req, hostname);
148 if(request == NULL)
149 return NULL;
150 s = socket(AF_INET, SOCK_STREAM, 0);
151 if(s < 0) {
152 free(request);
153 return NULL;
155 if(connect(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
156 close(s);
157 free(request);
158 return NULL;
160 if(write(s, request, strlen(request)) != strlen(request)) {
161 close(s);
162 free(request);
163 return NULL;
165 free(request);
166 while(1) {
167 n = read(s, buf + offset, sizeof(buf) - offset);
168 if(n <= 0)
169 break;
170 offset += n;
172 buf[offset] = '\0';
173 close(s);
174 p = strstr(buf, "\r\n\r\n"); /* find end of header */
175 if(p) p += 4;
176 else return NULL;
177 foo = NULL;
178 p = strtok_r(p, " \t\r\n", &foo);
179 if(p == NULL)
180 return NULL;
182 /* make a hostent to return */
183 #define MAX_ADDRS 16
184 static struct hostent he;
185 static char addrs[4 * MAX_ADDRS];
186 static char *addr_list[MAX_ADDRS + 1];
187 int num_addrs = 0;
189 he.h_name = p;
190 he.h_aliases = NULL;
191 he.h_addrtype = AF_INET;
192 he.h_length = 4;
194 while((p = strtok_r(NULL, " \t\r\n", &foo)) && num_addrs < MAX_ADDRS) {
195 struct in_addr ip;
196 inet_aton(p, &ip);
197 ip.s_addr = ntohl(ip.s_addr);
198 addr_list[num_addrs] = &addrs[num_addrs * 4];
199 addrs[num_addrs * 4 + 0] = (ip.s_addr >> 24) & 0xff;
200 addrs[num_addrs * 4 + 1] = (ip.s_addr >> 16) & 0xff;
201 addrs[num_addrs * 4 + 2] = (ip.s_addr >> 8) & 0xff;
202 addrs[num_addrs * 4 + 3] = (ip.s_addr >> 0) & 0xff;
203 addr_list[++num_addrs] = NULL;
205 he.h_addr_list = addr_list;
206 return &he;
210 struct hostent*
211 roken_gethostbyname(const char *hostname)
213 struct hostent *he;
214 he = gethostbyname(hostname);
215 if(he)
216 return he;
217 return roken_gethostby(hostname);
220 struct hostent* ROKEN_LIB_FUNCTION
221 roken_gethostbyaddr(const void *addr, size_t len, int type)
223 struct in_addr a;
224 const char *p;
225 struct hostent *he;
226 he = gethostbyaddr(addr, len, type);
227 if(he)
228 return he;
229 if(type != AF_INET || len != 4)
230 return NULL;
231 p = addr;
232 a.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
233 return roken_gethostby(inet_ntoa(a));
236 #if 0
238 /* this program can be used as a cgi `script' to lookup names and
239 ip-addresses */
241 #include <stdio.h>
242 #include <stdlib.h>
243 #include <netdb.h>
244 #include <sys/param.h>
247 main(int argc, char **argv)
249 char *query = getenv("QUERY_STRING");
250 char host[MAXHOSTNAMELEN];
251 int i;
252 struct hostent *he;
254 printf("Content-type: text/plain\n\n");
255 if(query == NULL)
256 exit(0);
257 he = gethostbyname(query);
258 strncpy(host, he->h_name, sizeof(host));
259 host[sizeof(host) - 1] = '\0';
260 he = gethostbyaddr(he->h_addr, he->h_length, AF_INET);
261 printf("%s\n", he->h_name);
262 for(i = 0; he->h_addr_list[i]; i++) {
263 struct in_addr ip;
264 unsigned char *p = (unsigned char*)he->h_addr_list[i];
265 ip.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
266 printf("%s\n", inet_ntoa(ip));
268 exit(0);
271 #endif