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