add _kdc_db_fetch and _kdc_free_ent for digest-service
[heimdal.git] / lib / roken / roken_gethostby.c
blob1d6c8ffe8af10de876058451a42741853f5072e5
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 dns_req = NULL;
70 if(proxy_host) {
71 if(make_address(proxy_host, &dns_addr.sin_addr) != 0)
72 return -1;
73 dns_addr.sin_port = htons(proxy_port);
74 if (asprintf(&dns_req, "http://%s:%d%s", dns_host, dns_port, dns_path) < 0)
75 return -1;
76 } else {
77 if(make_address(dns_host, &dns_addr.sin_addr) != 0)
78 return -1;
79 dns_addr.sin_port = htons(dns_port);
80 asprintf(&dns_req, "%s", dns_path);
82 dns_addr.sin_family = AF_INET;
83 return 0;
86 static void
87 split_spec(const char *spec, char **host, int *port, char **path, int def_port)
89 char *p;
90 *host = strdup(spec);
91 p = strchr(*host, ':');
92 if(p) {
93 *p++ = '\0';
94 if(sscanf(p, "%d", port) != 1)
95 *port = def_port;
96 } else
97 *port = def_port;
98 p = strchr(p ? p : *host, '/');
99 if(p) {
100 if(path)
101 *path = strdup(p);
102 *p = '\0';
103 }else
104 if(path)
105 *path = NULL;
109 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
110 roken_gethostby_setup(const char *proxy_spec, const char *dns_spec)
112 char *proxy_host = NULL;
113 int proxy_port = 0;
114 char *dns_host, *dns_path;
115 int dns_port;
117 int ret = -1;
119 split_spec(dns_spec, &dns_host, &dns_port, &dns_path, 80);
120 if(dns_path == NULL)
121 goto out;
122 if(proxy_spec)
123 split_spec(proxy_spec, &proxy_host, &proxy_port, NULL, 80);
124 ret = setup_int(proxy_host, proxy_port, dns_host, dns_port, dns_path);
125 out:
126 free(proxy_host);
127 free(dns_host);
128 free(dns_path);
129 return ret;
133 /* Try to lookup a name or an ip-address using http as transport
134 mechanism. See the end of this file for an example program. */
135 static struct hostent*
136 roken_gethostby(const char *hostname)
138 int s;
139 struct sockaddr_in addr;
140 char *request = NULL;
141 char buf[1024];
142 int offset = 0;
143 int n;
144 char *p, *foo;
146 if(dns_addr.sin_family == 0)
147 return NULL; /* no configured host */
148 addr = dns_addr;
149 if (asprintf(&request, "GET %s?%s HTTP/1.0\r\n\r\n", dns_req, hostname) < 0)
150 return NULL;
151 if(request == NULL)
152 return NULL;
153 s = socket(AF_INET, SOCK_STREAM, 0);
154 if(s < 0) {
155 free(request);
156 return NULL;
158 if(connect(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
159 close(s);
160 free(request);
161 return NULL;
163 if(write(s, request, strlen(request)) != strlen(request)) {
164 close(s);
165 free(request);
166 return NULL;
168 free(request);
169 while(1) {
170 n = read(s, buf + offset, sizeof(buf) - offset);
171 if(n <= 0)
172 break;
173 offset += n;
175 buf[offset] = '\0';
176 close(s);
177 p = strstr(buf, "\r\n\r\n"); /* find end of header */
178 if(p) p += 4;
179 else return NULL;
180 foo = NULL;
181 p = strtok_r(p, " \t\r\n", &foo);
182 if(p == NULL)
183 return NULL;
185 /* make a hostent to return */
186 #define MAX_ADDRS 16
187 static struct hostent he;
188 static char addrs[4 * MAX_ADDRS];
189 static char *addr_list[MAX_ADDRS + 1];
190 int num_addrs = 0;
192 he.h_name = p;
193 he.h_aliases = NULL;
194 he.h_addrtype = AF_INET;
195 he.h_length = 4;
197 while((p = strtok_r(NULL, " \t\r\n", &foo)) && num_addrs < MAX_ADDRS) {
198 struct in_addr ip;
199 inet_aton(p, &ip);
200 ip.s_addr = ntohl(ip.s_addr);
201 addr_list[num_addrs] = &addrs[num_addrs * 4];
202 addrs[num_addrs * 4 + 0] = (ip.s_addr >> 24) & 0xff;
203 addrs[num_addrs * 4 + 1] = (ip.s_addr >> 16) & 0xff;
204 addrs[num_addrs * 4 + 2] = (ip.s_addr >> 8) & 0xff;
205 addrs[num_addrs * 4 + 3] = (ip.s_addr >> 0) & 0xff;
206 addr_list[++num_addrs] = NULL;
208 he.h_addr_list = addr_list;
209 return &he;
213 ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL
214 roken_gethostbyname(const char *hostname)
216 struct hostent *he;
217 he = gethostbyname(hostname);
218 if(he)
219 return he;
220 return roken_gethostby(hostname);
223 ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL
224 roken_gethostbyaddr(const void *addr, size_t len, int type)
226 struct in_addr a;
227 const char *p;
228 struct hostent *he;
229 he = gethostbyaddr(addr, len, type);
230 if(he)
231 return he;
232 if(type != AF_INET || len != 4)
233 return NULL;
234 p = addr;
235 a.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
236 return roken_gethostby(inet_ntoa(a));
239 #if 0
241 /* this program can be used as a cgi `script' to lookup names and
242 ip-addresses */
244 #include <stdio.h>
245 #include <stdlib.h>
246 #include <netdb.h>
247 #include <sys/param.h>
250 main(int argc, char **argv)
252 char *query = getenv("QUERY_STRING");
253 char host[MAXHOSTNAMELEN];
254 int i;
255 struct hostent *he;
257 printf("Content-type: text/plain\n\n");
258 if(query == NULL)
259 exit(0);
260 he = gethostbyname(query);
261 strncpy(host, he->h_name, sizeof(host));
262 host[sizeof(host) - 1] = '\0';
263 he = gethostbyaddr(he->h_addr, he->h_length, AF_INET);
264 printf("%s\n", he->h_name);
265 for(i = 0; he->h_addr_list[i]; i++) {
266 struct in_addr ip;
267 unsigned char *p = (unsigned char*)he->h_addr_list[i];
268 ip.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
269 printf("%s\n", inet_ntoa(ip));
271 exit(0);
274 #endif