xref: /illumos-gate/usr/src/cmd/ipf/lib/hostname.c (revision 09fbbb7d1b5a956a9b81304070dcff318a4a158e)
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 #include "ipf.h"
11 
12 char *hostname(v, ip)
13 int v;
14 void *ip;
15 {
16 	static char hostbuf[MAXHOSTNAMELEN+1];
17 	struct hostent *hp;
18 	struct in_addr ipa;
19 	struct netent *np;
20 
21 	if (v == 4) {
22 		ipa.s_addr = *(u_32_t *)ip;
23 		if (ipa.s_addr == htonl(0xfedcba98))
24 			return "test.host.dots";
25 	}
26 
27 	if ((opts & OPT_NORESOLVE) == 0) {
28 		if (v == 4) {
29 			hp = gethostbyaddr(ip, 4, AF_INET);
30 			if (hp != NULL && hp->h_name != NULL &&
31 			    *hp->h_name != '\0') {
32 				strncpy(hostbuf, hp->h_name, sizeof(hostbuf));
33 				hostbuf[sizeof(hostbuf) - 1] = '\0';
34 				return hostbuf;
35 			}
36 
37 			np = getnetbyaddr(ipa.s_addr, AF_INET);
38 			if (np != NULL && np->n_name != NULL &&
39 			    *np->n_name != '\0') {
40 				strncpy(hostbuf, np->n_name, sizeof(hostbuf));
41 				hostbuf[sizeof(hostbuf) - 1] = '\0';
42 				return hostbuf;
43 			}
44 		}
45 	}
46 
47 	if (v == 4) {
48 		return inet_ntoa(ipa);
49 	}
50 #ifdef  USE_INET6
51 	(void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
52 	hostbuf[MAXHOSTNAMELEN] = '\0';
53 	return hostbuf;
54 #else
55 	return "IPv6";
56 #endif
57 }
58