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