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