xref: /illumos-gate/usr/src/cmd/ipf/lib/common/hostname.c (revision ab25eeb5)
1 /*
2  * Copyright (C) 2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
7  * Use is subject to license terms.
8  */
9 
10 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11 
12 #include "ipf.h"
13 
hostname(v,ip)14 char *hostname(v, ip)
15 int v;
16 void *ip;
17 {
18 	static char hostbuf[MAXHOSTNAMELEN+1];
19 	struct hostent *hp;
20 	struct in_addr ipa;
21 	struct netent *np;
22 
23 	if (v == 4) {
24 		ipa.s_addr = *(u_32_t *)ip;
25 		if (ipa.s_addr == htonl(0xfedcba98))
26 			return "test.host.dots";
27 	}
28 
29 	if ((opts & OPT_NORESOLVE) == 0) {
30 		if (v == 4) {
31 			hp = gethostbyaddr(ip, 4, AF_INET);
32 			if (hp != NULL && hp->h_name != NULL &&
33 			    *hp->h_name != '\0') {
34 				strncpy(hostbuf, hp->h_name, sizeof(hostbuf));
35 				hostbuf[sizeof(hostbuf) - 1] = '\0';
36 				return hostbuf;
37 			}
38 
39 			np = getnetbyaddr(ipa.s_addr, AF_INET);
40 			if (np != NULL && np->n_name != NULL &&
41 			    *np->n_name != '\0') {
42 				strncpy(hostbuf, np->n_name, sizeof(hostbuf));
43 				hostbuf[sizeof(hostbuf) - 1] = '\0';
44 				return hostbuf;
45 			}
46 		}
47 	}
48 
49 	if (v == 4) {
50 		return inet_ntoa(ipa);
51 	}
52 #ifdef  USE_INET6
53 	(void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
54 	hostbuf[MAXHOSTNAMELEN] = '\0';
55 	return hostbuf;
56 #else
57 	return "IPv6";
58 #endif
59 }
60