1
2 /*
3 * Copyright (C) 2012 by Darren Reed.
4 *
5 * See the IPFILTER.LICENCE file for details on licencing.
6 *
7 * $Id$
8 */
9
10 #include "ipf.h"
11
12 char *
hostname(int family,void * ip)13 hostname(int family, void *ip)
14 {
15 static char hostbuf[MAXHOSTNAMELEN+1];
16 struct hostent *hp;
17 struct in_addr ipa;
18 struct netent *np;
19
20 memset(&ipa, 0, sizeof(ipa)); /* XXX gcc */
21
22 if (family == AF_INET) {
23 ipa.s_addr = *(u_32_t *)ip;
24 if (ipa.s_addr == htonl(0xfedcba98))
25 return ("test.host.dots");
26 }
27
28 if ((opts & OPT_NORESOLVE) == 0) {
29 if (family == AF_INET) {
30 hp = gethostbyaddr(ip, 4, AF_INET);
31 if (hp != NULL && hp->h_name != NULL &&
32 *hp->h_name != '\0') {
33 strncpy(hostbuf, hp->h_name, sizeof(hostbuf));
34 hostbuf[sizeof(hostbuf) - 1] = '\0';
35 return (hostbuf);
36 }
37
38 np = getnetbyaddr(ipa.s_addr, AF_INET);
39 if (np != NULL && np->n_name != NULL &&
40 *np->n_name != '\0') {
41 strncpy(hostbuf, np->n_name, sizeof(hostbuf));
42 hostbuf[sizeof(hostbuf) - 1] = '\0';
43 return (hostbuf);
44 }
45 }
46 }
47
48 if (family == AF_INET) {
49 return (inet_ntoa(ipa));
50 }
51 #ifdef USE_INET6
52 (void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
53 hostbuf[MAXHOSTNAMELEN] = '\0';
54 return (hostbuf);
55 #else
56 return ("IPv6");
57 #endif
58 }
59