Line data Source code
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 : #include <config.h>
35 :
36 : #include "roken.h"
37 :
38 : #undef roken_gethostbyname
39 : #undef roken_gethostbyaddr
40 :
41 : static struct sockaddr_in dns_addr;
42 : static char *dns_req;
43 :
44 : static int
45 0 : make_address(const char *address, struct in_addr *ip)
46 : {
47 0 : 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 0 : struct hostent *he = gethostbyname(address);
51 0 : if(he) {
52 0 : unsigned char *p = (unsigned char*)he->h_addr;
53 0 : ip->s_addr = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
54 : } else {
55 0 : return -1;
56 : }
57 : }
58 0 : return 0;
59 : }
60 :
61 : static int
62 0 : setup_int(const char *proxy_host, short proxy_port,
63 : const char *dns_host, short dns_port,
64 : const char *dns_path)
65 : {
66 0 : memset(&dns_addr, 0, sizeof(dns_addr));
67 0 : if(dns_req)
68 0 : free(dns_req);
69 0 : dns_req = NULL;
70 0 : if(proxy_host) {
71 0 : if(make_address(proxy_host, &dns_addr.sin_addr) != 0)
72 0 : return -1;
73 0 : dns_addr.sin_port = htons(proxy_port);
74 0 : if (asprintf(&dns_req, "http://%s:%d%s", dns_host, dns_port, dns_path) < 0)
75 0 : return -1;
76 : } else {
77 0 : if(make_address(dns_host, &dns_addr.sin_addr) != 0)
78 0 : return -1;
79 0 : dns_addr.sin_port = htons(dns_port);
80 0 : if (asprintf(&dns_req, "%s", dns_path) < 0)
81 0 : return -1;
82 : }
83 0 : dns_addr.sin_family = AF_INET;
84 0 : return 0;
85 : }
86 :
87 : static int
88 0 : split_spec(const char *spec, char **host, int *port, char **path, int def_port)
89 : {
90 0 : char *p;
91 0 : *host = strdup(spec);
92 0 : if (*host == NULL)
93 0 : return -1;
94 0 : p = strchr(*host, ':');
95 0 : if(p) {
96 0 : *p++ = '\0';
97 0 : if(sscanf(p, "%d", port) != 1)
98 0 : *port = def_port;
99 : } else
100 0 : *port = def_port;
101 0 : p = strchr(p ? p : *host, '/');
102 0 : if(p) {
103 0 : if(path) {
104 0 : *path = strdup(p);
105 0 : if (*path == NULL) {
106 0 : free(*host);
107 0 : *host = NULL;
108 0 : return -1;
109 : }
110 : }
111 0 : *p = '\0';
112 : }else
113 0 : if(path)
114 0 : *path = NULL;
115 0 : return 0;
116 : }
117 :
118 :
119 : ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
120 0 : roken_gethostby_setup(const char *proxy_spec, const char *dns_spec)
121 : {
122 0 : char *proxy_host = NULL;
123 0 : int proxy_port = 0;
124 0 : char *dns_host = NULL, *dns_path = NULL;
125 0 : int dns_port;
126 0 : int ret;
127 :
128 0 : ret = split_spec(dns_spec, &dns_host, &dns_port, &dns_path, 80);
129 0 : if(ret)
130 0 : goto out;
131 0 : if(proxy_spec) {
132 0 : ret = split_spec(proxy_spec, &proxy_host, &proxy_port, NULL, 80);
133 0 : if (ret)
134 0 : goto out;
135 : }
136 0 : ret = setup_int(proxy_host, proxy_port, dns_host, dns_port, dns_path);
137 0 : if (ret)
138 0 : goto out;
139 :
140 0 : out:
141 0 : free(proxy_host);
142 0 : free(dns_host);
143 0 : free(dns_path);
144 0 : return ret;
145 : }
146 :
147 :
148 : /* Try to lookup a name or an ip-address using http as transport
149 : mechanism. See the end of this file for an example program. */
150 : static struct hostent*
151 0 : roken_gethostby(const char *hostname)
152 : {
153 0 : int s;
154 0 : struct sockaddr_in addr;
155 0 : char *request = NULL;
156 0 : char buf[1024];
157 0 : int offset = 0;
158 0 : int n;
159 0 : char *p, *foo;
160 0 : size_t len;
161 :
162 0 : if(dns_addr.sin_family == 0)
163 0 : return NULL; /* no configured host */
164 0 : addr = dns_addr;
165 0 : if (asprintf(&request, "GET %s?%s HTTP/1.0\r\n\r\n", dns_req, hostname) < 0)
166 0 : return NULL;
167 0 : if(request == NULL)
168 0 : return NULL;
169 0 : s = socket(AF_INET, SOCK_STREAM, 0);
170 0 : if(s < 0) {
171 0 : free(request);
172 0 : return NULL;
173 : }
174 0 : if(connect(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
175 0 : close(s);
176 0 : free(request);
177 0 : return NULL;
178 : }
179 :
180 0 : len = strlen(request);
181 0 : if(write(s, request, len) != (ssize_t)len) {
182 0 : close(s);
183 0 : free(request);
184 0 : return NULL;
185 : }
186 0 : free(request);
187 0 : while(1) {
188 0 : n = read(s, buf + offset, sizeof(buf) - offset);
189 0 : if(n <= 0)
190 0 : break;
191 0 : offset += n;
192 : }
193 0 : buf[offset] = '\0';
194 0 : close(s);
195 0 : p = strstr(buf, "\r\n\r\n"); /* find end of header */
196 0 : if(p) p += 4;
197 0 : else return NULL;
198 0 : foo = NULL;
199 0 : p = strtok_r(p, " \t\r\n", &foo);
200 0 : if(p == NULL)
201 0 : return NULL;
202 : {
203 : /* make a hostent to return */
204 : #define MAX_ADDRS 16
205 0 : static struct hostent he;
206 0 : static char addrs[4 * MAX_ADDRS];
207 0 : static char *addr_list[MAX_ADDRS + 1];
208 0 : int num_addrs = 0;
209 :
210 0 : he.h_name = p;
211 0 : he.h_aliases = NULL;
212 0 : he.h_addrtype = AF_INET;
213 0 : he.h_length = 4;
214 :
215 0 : while((p = strtok_r(NULL, " \t\r\n", &foo)) && num_addrs < MAX_ADDRS) {
216 0 : struct in_addr ip;
217 0 : inet_aton(p, &ip);
218 0 : ip.s_addr = ntohl(ip.s_addr);
219 0 : addr_list[num_addrs] = &addrs[num_addrs * 4];
220 0 : addrs[num_addrs * 4 + 0] = (ip.s_addr >> 24) & 0xff;
221 0 : addrs[num_addrs * 4 + 1] = (ip.s_addr >> 16) & 0xff;
222 0 : addrs[num_addrs * 4 + 2] = (ip.s_addr >> 8) & 0xff;
223 0 : addrs[num_addrs * 4 + 3] = (ip.s_addr >> 0) & 0xff;
224 0 : addr_list[++num_addrs] = NULL;
225 : }
226 0 : he.h_addr_list = addr_list;
227 0 : return &he;
228 : }
229 : }
230 :
231 : ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL
232 0 : roken_gethostbyname(const char *hostname)
233 : {
234 0 : struct hostent *he;
235 0 : he = gethostbyname(hostname);
236 0 : if(he)
237 0 : return he;
238 0 : return roken_gethostby(hostname);
239 : }
240 :
241 : ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL
242 0 : roken_gethostbyaddr(const void *addr, size_t len, int type)
243 : {
244 0 : struct in_addr a;
245 0 : const char *p;
246 0 : struct hostent *he;
247 0 : he = gethostbyaddr(addr, len, type);
248 0 : if(he)
249 0 : return he;
250 0 : if(type != AF_INET || len != 4)
251 0 : return NULL;
252 0 : p = addr;
253 0 : a.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
254 0 : return roken_gethostby(inet_ntoa(a));
255 : }
256 :
257 : #if 0
258 :
259 : /* this program can be used as a cgi `script' to lookup names and
260 : ip-addresses */
261 :
262 : #include <stdio.h>
263 : #include <stdlib.h>
264 : #include <netdb.h>
265 : #include <sys/param.h>
266 :
267 : int
268 : main(int argc, char **argv)
269 : {
270 : char *query = getenv("QUERY_STRING");
271 : char host[MAXHOSTNAMELEN];
272 : int i;
273 : struct hostent *he;
274 :
275 : printf("Content-type: text/plain\n\n");
276 : if(query == NULL)
277 : exit(0);
278 : he = gethostbyname(query);
279 : strncpy(host, he->h_name, sizeof(host));
280 : host[sizeof(host) - 1] = '\0';
281 : he = gethostbyaddr(he->h_addr, he->h_length, AF_INET);
282 : printf("%s\n", he->h_name);
283 : for(i = 0; he->h_addr_list[i]; i++) {
284 : struct in_addr ip;
285 : unsigned char *p = (unsigned char*)he->h_addr_list[i];
286 : ip.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
287 : printf("%s\n", inet_ntoa(ip));
288 : }
289 : exit(0);
290 : }
291 :
292 : #endif
|