]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ipfilter/lib/hostname.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ipfilter / lib / hostname.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2002-2003 by Darren Reed.
5  * 
6  * See the IPFILTER.LICENCE file for details on licencing.  
7  *   
8  * $Id: hostname.c,v 1.6.2.2 2007/01/16 02:25:22 darrenr Exp $ 
9  */     
10
11 #include "ipf.h"
12
13 char *hostname(v, ip)
14 int v;
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 (v == 4) {
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 (v == 4) {
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 (v == 4) {
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 }