2007-04-15 00:34:18 +04:00
|
|
|
/* $NetBSD: hostname.c,v 1.3 2007/04/14 20:34:26 martin Exp $ */
|
2004-03-28 12:55:20 +04:00
|
|
|
|
2007-04-15 00:34:18 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002-2003 by Darren Reed.
|
|
|
|
*
|
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
|
|
|
*
|
|
|
|
* Id: hostname.c,v 1.6.2.2 2007/01/16 02:25:22 darrenr Exp
|
|
|
|
*/
|
2004-03-28 12:55:20 +04:00
|
|
|
|
|
|
|
#include "ipf.h"
|
|
|
|
|
|
|
|
char *hostname(v, ip)
|
|
|
|
int v;
|
|
|
|
void *ip;
|
|
|
|
{
|
|
|
|
static char hostbuf[MAXHOSTNAMELEN+1];
|
|
|
|
struct hostent *hp;
|
|
|
|
struct in_addr ipa;
|
|
|
|
struct netent *np;
|
|
|
|
|
2007-04-15 00:34:18 +04:00
|
|
|
memset(&ipa, 0, sizeof(ipa)); /* XXX gcc */
|
2006-05-11 01:53:14 +04:00
|
|
|
|
2004-03-28 12:55:20 +04:00
|
|
|
if (v == 4) {
|
|
|
|
ipa.s_addr = *(u_32_t *)ip;
|
|
|
|
if (ipa.s_addr == htonl(0xfedcba98))
|
|
|
|
return "test.host.dots";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((opts & OPT_NORESOLVE) == 0) {
|
|
|
|
if (v == 4) {
|
|
|
|
hp = gethostbyaddr(ip, 4, AF_INET);
|
|
|
|
if (hp != NULL && hp->h_name != NULL &&
|
|
|
|
*hp->h_name != '\0') {
|
|
|
|
strncpy(hostbuf, hp->h_name, sizeof(hostbuf));
|
|
|
|
hostbuf[sizeof(hostbuf) - 1] = '\0';
|
|
|
|
return hostbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
np = getnetbyaddr(ipa.s_addr, AF_INET);
|
|
|
|
if (np != NULL && np->n_name != NULL &&
|
|
|
|
*np->n_name != '\0') {
|
|
|
|
strncpy(hostbuf, np->n_name, sizeof(hostbuf));
|
|
|
|
hostbuf[sizeof(hostbuf) - 1] = '\0';
|
|
|
|
return hostbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v == 4) {
|
|
|
|
return inet_ntoa(ipa);
|
|
|
|
}
|
|
|
|
#ifdef USE_INET6
|
|
|
|
(void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
|
|
|
|
hostbuf[MAXHOSTNAMELEN] = '\0';
|
|
|
|
return hostbuf;
|
|
|
|
#else
|
|
|
|
return "IPv6";
|
|
|
|
#endif
|
|
|
|
}
|