Another miracle of the 20th century: gethostby* over HTTP.
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@4542 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
238
lib/roken/roken_gethostby.c
Normal file
238
lib/roken/roken_gethostby.c
Normal file
@@ -0,0 +1,238 @@
|
||||
/*
|
||||
* Copyright (c) 1998 Kungliga Tekniska H<>gskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by Kungliga Tekniska
|
||||
* H<>gskolan and its contributors.
|
||||
*
|
||||
* 4. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
RCSID("$Id$");
|
||||
#endif
|
||||
|
||||
#include <roken.h>
|
||||
#ifdef HAVE_NETDB_H
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
#ifdef HAVE_ARPA_INET_H
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#undef roken_gethostbyname
|
||||
#undef roken_gethostbyaddr
|
||||
|
||||
static struct sockaddr_in dns_addr;
|
||||
static char *dns_req;
|
||||
|
||||
static int
|
||||
make_address(const char *address, struct in_addr *ip)
|
||||
{
|
||||
if(inet_aton(address, ip) == 0){
|
||||
/* try to resolve as hostname, it might work if the address we
|
||||
are trying to lookup is local, for instance a web proxy */
|
||||
struct hostent *he = gethostbyname(address);
|
||||
if(he) {
|
||||
unsigned char *p = (unsigned char*)he->h_addr;
|
||||
ip->s_addr = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
roken_gethostby_setup(const char *proxy_host, short proxy_port,
|
||||
const char *dns_host, short dns_port,
|
||||
const char *dns_path)
|
||||
{
|
||||
memset(&dns_addr, 0, sizeof(dns_addr));
|
||||
if(dns_req)
|
||||
free(dns_req);
|
||||
if(proxy_host) {
|
||||
if(make_address(proxy_host, &dns_addr.sin_addr) != 0)
|
||||
return -1;
|
||||
dns_addr.sin_port = htons(proxy_port);
|
||||
asprintf(&dns_req, "http://%s:%d%s", dns_host, dns_port, dns_path);
|
||||
} else {
|
||||
if(make_address(dns_host, &dns_addr.sin_addr) != 0)
|
||||
return -1;
|
||||
dns_addr.sin_port = htons(dns_port);
|
||||
asprintf(&dns_req, "%s", dns_path);
|
||||
}
|
||||
dns_addr.sin_family = AF_INET;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Try to lookup a name or an ip-address using http as transport
|
||||
mechanism. See the end of this file for an example program. */
|
||||
static struct hostent*
|
||||
roken_gethostby(const char *hostname)
|
||||
{
|
||||
int s;
|
||||
struct sockaddr_in sin;
|
||||
char *request;
|
||||
char buf[1024];
|
||||
int offset = 0;
|
||||
int n;
|
||||
char *p, *foo;
|
||||
|
||||
if(dns_addr.sin_family == 0)
|
||||
return NULL; /* no configured host */
|
||||
sin = dns_addr;
|
||||
asprintf(&request, "GET %s?%s HTTP/1.0\r\n\r\n", dns_req, hostname);
|
||||
if(request == NULL)
|
||||
return NULL;
|
||||
s = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(s < 0) {
|
||||
free(request);
|
||||
return NULL;
|
||||
}
|
||||
if(connect(s, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
|
||||
close(s);
|
||||
free(request);
|
||||
return NULL;
|
||||
}
|
||||
if(write(s, request, strlen(request)) != strlen(request)) {
|
||||
close(s);
|
||||
free(request);
|
||||
return NULL;
|
||||
}
|
||||
free(request);
|
||||
while(1) {
|
||||
n = read(s, buf + offset, sizeof(buf) - offset);
|
||||
if(n <= 0)
|
||||
break;
|
||||
offset += n;
|
||||
}
|
||||
buf[offset] = '\0';
|
||||
close(s);
|
||||
p = strstr(buf, "\r\n\r\n"); /* find end of header */
|
||||
if(p) p += 4;
|
||||
else return NULL;
|
||||
foo = NULL;
|
||||
p = strtok_r(p, " \t\r\n", &foo);
|
||||
if(p == NULL)
|
||||
return NULL;
|
||||
{
|
||||
/* make a hostent to return */
|
||||
#define MAX_ADDRS 16
|
||||
static struct hostent he;
|
||||
static char addrs[4 * MAX_ADDRS];
|
||||
static char *addr_list[MAX_ADDRS];
|
||||
int num_addrs = 0;
|
||||
|
||||
he.h_name = p;
|
||||
he.h_aliases = NULL;
|
||||
he.h_addrtype = AF_INET;
|
||||
he.h_length = 4;
|
||||
|
||||
while((p = strtok_r(NULL, " \t\r\n", &foo)) && num_addrs < MAX_ADDRS) {
|
||||
struct in_addr ip;
|
||||
inet_aton(p, &ip);
|
||||
ip.s_addr = ntohl(ip.s_addr);
|
||||
addr_list[num_addrs] = &addrs[num_addrs * 4];
|
||||
addrs[num_addrs * 4 + 0] = (ip.s_addr >> 24) & 0xff;
|
||||
addrs[num_addrs * 4 + 1] = (ip.s_addr >> 16) & 0xff;
|
||||
addrs[num_addrs * 4 + 2] = (ip.s_addr >> 8) & 0xff;
|
||||
addrs[num_addrs * 4 + 3] = (ip.s_addr >> 0) & 0xff;
|
||||
addr_list[++num_addrs] = NULL;
|
||||
}
|
||||
he.h_addr_list = addr_list;
|
||||
return &he;
|
||||
}
|
||||
}
|
||||
|
||||
struct hostent*
|
||||
roken_gethostbyname(const char *hostname)
|
||||
{
|
||||
struct hostent *he;
|
||||
he = gethostbyname(hostname);
|
||||
if(he)
|
||||
return he;
|
||||
return roken_gethostby(hostname);
|
||||
}
|
||||
|
||||
struct hostent*
|
||||
roken_gethostbyaddr(const void *addr, size_t len, int type)
|
||||
{
|
||||
struct in_addr a;
|
||||
const char *p;
|
||||
struct hostent *he;
|
||||
he = gethostbyaddr(addr, len, type);
|
||||
if(he)
|
||||
return he;
|
||||
if(type != AF_INET || len != 4)
|
||||
return NULL;
|
||||
p = addr;
|
||||
a.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
|
||||
return roken_gethostby(inet_ntoa(a));
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
/* this program can be used as a cgi `script' to lookup names and
|
||||
ip-addresses */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *query = getenv("QUERY_STRING");
|
||||
char host[MAXHOSTNAMELEN];
|
||||
int i;
|
||||
struct hostent *he;
|
||||
|
||||
printf("Content-type: text/plain\n\n");
|
||||
if(query == NULL)
|
||||
exit(0);
|
||||
he = gethostbyname(query);
|
||||
strncpy(host, he->h_name, sizeof(host));
|
||||
host[sizeof(host) - 1] = '\0';
|
||||
he = gethostbyaddr(he->h_addr, he->h_length, AF_INET);
|
||||
printf("%s\n", he->h_name);
|
||||
for(i = 0; he->h_addr_list[i]; i++) {
|
||||
struct in_addr ip;
|
||||
unsigned char *p = (unsigned char*)he->h_addr_list[i];
|
||||
ip.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
|
||||
printf("%s\n", inet_ntoa(ip));
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user