xref: /netbsd-src/external/bsd/ipf/dist/lib/hostname.c (revision c9d5dc6c77aa32fd07899a7a63638e95ffa433dd)
1 /*	$NetBSD: hostname.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  * $Id: hostname.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $
9  */
10 
11 #include "ipf.h"
12 
13 char *hostname(family, ip)
14 	int family;
15 	void *ip;
16 {
17 	static char hostbuf[MAXHOSTNAMELEN+1];
18 	struct hostent *hp;
19 	struct in_addr ipa;
20 	struct netent *np;
21 
22 	memset(&ipa, 0, sizeof(ipa));	/* XXX gcc */
23 
24 	if (family == AF_INET) {
25 		ipa.s_addr = *(u_32_t *)ip;
26 		if (ipa.s_addr == htonl(0xfedcba98))
27 			return "test.host.dots";
28 	}
29 
30 	if ((opts & OPT_NORESOLVE) == 0) {
31 		if (family == AF_INET) {
32 			hp = gethostbyaddr(ip, 4, AF_INET);
33 			if (hp != NULL && hp->h_name != NULL &&
34 			    *hp->h_name != '\0') {
35 				strncpy(hostbuf, hp->h_name, sizeof(hostbuf));
36 				hostbuf[sizeof(hostbuf) - 1] = '\0';
37 				return hostbuf;
38 			}
39 
40 			np = getnetbyaddr(ipa.s_addr, AF_INET);
41 			if (np != NULL && np->n_name != NULL &&
42 			    *np->n_name != '\0') {
43 				strncpy(hostbuf, np->n_name, sizeof(hostbuf));
44 				hostbuf[sizeof(hostbuf) - 1] = '\0';
45 				return hostbuf;
46 			}
47 		}
48 	}
49 
50 	if (family == AF_INET) {
51 		return inet_ntoa(ipa);
52 	}
53 #ifdef  USE_INET6
54 	(void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
55 	hostbuf[MAXHOSTNAMELEN] = '\0';
56 	return hostbuf;
57 #else
58 	return "IPv6";
59 #endif
60 }
61