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