xref: /illumos-gate/usr/src/cmd/ipf/lib/common/hostname.c (revision ab25eeb5)
17663b816Sml /*
27663b816Sml  * Copyright (C) 2003 by Darren Reed.
37663b816Sml  *
47663b816Sml  * See the IPFILTER.LICENCE file for details on licencing.
57663b816Sml  *
6*ab25eeb5Syz  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
77663b816Sml  * Use is subject to license terms.
87663b816Sml  */
97663b816Sml 
107663b816Sml #pragma ident	"%Z%%M%	%I%	%E% SMI"
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include "ipf.h"
137c478bd9Sstevel@tonic-gate 
hostname(v,ip)147c478bd9Sstevel@tonic-gate char *hostname(v, ip)
157c478bd9Sstevel@tonic-gate int v;
167c478bd9Sstevel@tonic-gate void *ip;
177c478bd9Sstevel@tonic-gate {
18*ab25eeb5Syz 	static char hostbuf[MAXHOSTNAMELEN+1];
19*ab25eeb5Syz 	struct hostent *hp;
207c478bd9Sstevel@tonic-gate 	struct in_addr ipa;
21*ab25eeb5Syz 	struct netent *np;
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate 	if (v == 4) {
247c478bd9Sstevel@tonic-gate 		ipa.s_addr = *(u_32_t *)ip;
25*ab25eeb5Syz 		if (ipa.s_addr == htonl(0xfedcba98))
26*ab25eeb5Syz 			return "test.host.dots";
27*ab25eeb5Syz 	}
28*ab25eeb5Syz 
29*ab25eeb5Syz 	if ((opts & OPT_NORESOLVE) == 0) {
30*ab25eeb5Syz 		if (v == 4) {
31*ab25eeb5Syz 			hp = gethostbyaddr(ip, 4, AF_INET);
32*ab25eeb5Syz 			if (hp != NULL && hp->h_name != NULL &&
33*ab25eeb5Syz 			    *hp->h_name != '\0') {
34*ab25eeb5Syz 				strncpy(hostbuf, hp->h_name, sizeof(hostbuf));
35*ab25eeb5Syz 				hostbuf[sizeof(hostbuf) - 1] = '\0';
36*ab25eeb5Syz 				return hostbuf;
37*ab25eeb5Syz 			}
38*ab25eeb5Syz 
39*ab25eeb5Syz 			np = getnetbyaddr(ipa.s_addr, AF_INET);
40*ab25eeb5Syz 			if (np != NULL && np->n_name != NULL &&
41*ab25eeb5Syz 			    *np->n_name != '\0') {
42*ab25eeb5Syz 				strncpy(hostbuf, np->n_name, sizeof(hostbuf));
43*ab25eeb5Syz 				hostbuf[sizeof(hostbuf) - 1] = '\0';
44*ab25eeb5Syz 				return hostbuf;
45*ab25eeb5Syz 			}
46*ab25eeb5Syz 		}
47*ab25eeb5Syz 	}
48*ab25eeb5Syz 
49*ab25eeb5Syz 	if (v == 4) {
507c478bd9Sstevel@tonic-gate 		return inet_ntoa(ipa);
517c478bd9Sstevel@tonic-gate 	}
527c478bd9Sstevel@tonic-gate #ifdef  USE_INET6
53*ab25eeb5Syz 	(void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
54*ab25eeb5Syz 	hostbuf[MAXHOSTNAMELEN] = '\0';
557c478bd9Sstevel@tonic-gate 	return hostbuf;
567c478bd9Sstevel@tonic-gate #else
577c478bd9Sstevel@tonic-gate 	return "IPv6";
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate }
60