xref: /illumos-gate/usr/src/cmd/ipf/lib/hostname.c (revision f3ac6781)
17663b816Sml /*
27663b816Sml  * Copyright (C) 2003 by Darren Reed.
37663b816Sml  *
47663b816Sml  * See the IPFILTER.LICENCE file for details on licencing.
57663b816Sml  *
6ab25eeb5Syz  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
77663b816Sml  * Use is subject to license terms.
87663b816Sml  */
97663b816Sml 
107c478bd9Sstevel@tonic-gate #include "ipf.h"
117c478bd9Sstevel@tonic-gate 
hostname(v,ip)127c478bd9Sstevel@tonic-gate char *hostname(v, ip)
137c478bd9Sstevel@tonic-gate int v;
147c478bd9Sstevel@tonic-gate void *ip;
157c478bd9Sstevel@tonic-gate {
16ab25eeb5Syz 	static char hostbuf[MAXHOSTNAMELEN+1];
17ab25eeb5Syz 	struct hostent *hp;
187c478bd9Sstevel@tonic-gate 	struct in_addr ipa;
19ab25eeb5Syz 	struct netent *np;
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate 	if (v == 4) {
227c478bd9Sstevel@tonic-gate 		ipa.s_addr = *(u_32_t *)ip;
23ab25eeb5Syz 		if (ipa.s_addr == htonl(0xfedcba98))
24ab25eeb5Syz 			return "test.host.dots";
25ab25eeb5Syz 	}
26ab25eeb5Syz 
27ab25eeb5Syz 	if ((opts & OPT_NORESOLVE) == 0) {
28ab25eeb5Syz 		if (v == 4) {
29ab25eeb5Syz 			hp = gethostbyaddr(ip, 4, AF_INET);
30ab25eeb5Syz 			if (hp != NULL && hp->h_name != NULL &&
31ab25eeb5Syz 			    *hp->h_name != '\0') {
32ab25eeb5Syz 				strncpy(hostbuf, hp->h_name, sizeof(hostbuf));
33ab25eeb5Syz 				hostbuf[sizeof(hostbuf) - 1] = '\0';
34ab25eeb5Syz 				return hostbuf;
35ab25eeb5Syz 			}
36ab25eeb5Syz 
37ab25eeb5Syz 			np = getnetbyaddr(ipa.s_addr, AF_INET);
38ab25eeb5Syz 			if (np != NULL && np->n_name != NULL &&
39ab25eeb5Syz 			    *np->n_name != '\0') {
40ab25eeb5Syz 				strncpy(hostbuf, np->n_name, sizeof(hostbuf));
41ab25eeb5Syz 				hostbuf[sizeof(hostbuf) - 1] = '\0';
42ab25eeb5Syz 				return hostbuf;
43ab25eeb5Syz 			}
44ab25eeb5Syz 		}
45ab25eeb5Syz 	}
46ab25eeb5Syz 
47ab25eeb5Syz 	if (v == 4) {
487c478bd9Sstevel@tonic-gate 		return inet_ntoa(ipa);
497c478bd9Sstevel@tonic-gate 	}
507c478bd9Sstevel@tonic-gate #ifdef  USE_INET6
51ab25eeb5Syz 	(void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
52ab25eeb5Syz 	hostbuf[MAXHOSTNAMELEN] = '\0';
537c478bd9Sstevel@tonic-gate 	return hostbuf;
547c478bd9Sstevel@tonic-gate #else
557c478bd9Sstevel@tonic-gate 	return "IPv6";
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate }
58