xref: /titanic_44/usr/src/cmd/ipf/lib/common/hostname.c (revision ab25eeb551a4be927a4b6ae2cf8aff7ed17decb4)
17663b816Sml37995 /*
27663b816Sml37995  * Copyright (C) 2003 by Darren Reed.
37663b816Sml37995  *
47663b816Sml37995  * See the IPFILTER.LICENCE file for details on licencing.
57663b816Sml37995  *
6*ab25eeb5Syz155240  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
77663b816Sml37995  * Use is subject to license terms.
87663b816Sml37995  */
97663b816Sml37995 
107663b816Sml37995 #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*ab25eeb5Syz155240 	static char hostbuf[MAXHOSTNAMELEN+1];
19*ab25eeb5Syz155240 	struct hostent *hp;
207c478bd9Sstevel@tonic-gate 	struct in_addr ipa;
21*ab25eeb5Syz155240 	struct netent *np;
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate 	if (v == 4) {
247c478bd9Sstevel@tonic-gate 		ipa.s_addr = *(u_32_t *)ip;
25*ab25eeb5Syz155240 		if (ipa.s_addr == htonl(0xfedcba98))
26*ab25eeb5Syz155240 			return "test.host.dots";
27*ab25eeb5Syz155240 	}
28*ab25eeb5Syz155240 
29*ab25eeb5Syz155240 	if ((opts & OPT_NORESOLVE) == 0) {
30*ab25eeb5Syz155240 		if (v == 4) {
31*ab25eeb5Syz155240 			hp = gethostbyaddr(ip, 4, AF_INET);
32*ab25eeb5Syz155240 			if (hp != NULL && hp->h_name != NULL &&
33*ab25eeb5Syz155240 			    *hp->h_name != '\0') {
34*ab25eeb5Syz155240 				strncpy(hostbuf, hp->h_name, sizeof(hostbuf));
35*ab25eeb5Syz155240 				hostbuf[sizeof(hostbuf) - 1] = '\0';
36*ab25eeb5Syz155240 				return hostbuf;
37*ab25eeb5Syz155240 			}
38*ab25eeb5Syz155240 
39*ab25eeb5Syz155240 			np = getnetbyaddr(ipa.s_addr, AF_INET);
40*ab25eeb5Syz155240 			if (np != NULL && np->n_name != NULL &&
41*ab25eeb5Syz155240 			    *np->n_name != '\0') {
42*ab25eeb5Syz155240 				strncpy(hostbuf, np->n_name, sizeof(hostbuf));
43*ab25eeb5Syz155240 				hostbuf[sizeof(hostbuf) - 1] = '\0';
44*ab25eeb5Syz155240 				return hostbuf;
45*ab25eeb5Syz155240 			}
46*ab25eeb5Syz155240 		}
47*ab25eeb5Syz155240 	}
48*ab25eeb5Syz155240 
49*ab25eeb5Syz155240 	if (v == 4) {
507c478bd9Sstevel@tonic-gate 		return inet_ntoa(ipa);
517c478bd9Sstevel@tonic-gate 	}
527c478bd9Sstevel@tonic-gate #ifdef  USE_INET6
53*ab25eeb5Syz155240 	(void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
54*ab25eeb5Syz155240 	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