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