]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/lib/hostname.c
This commit was generated by cvs2svn to compensate for changes in r147013,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / lib / hostname.c
1 /*      $FreeBSD$       */
2
3
4 #include "ipf.h"
5
6 char *hostname(v, ip)
7 int v;
8 void *ip;
9 {
10         static char hostbuf[MAXHOSTNAMELEN+1];
11         struct hostent *hp;
12         struct in_addr ipa;
13         struct netent *np;
14
15         if (v == 4) {
16                 ipa.s_addr = *(u_32_t *)ip;
17                 if (ipa.s_addr == htonl(0xfedcba98))
18                         return "test.host.dots";
19         }
20
21         if ((opts & OPT_NORESOLVE) == 0) {
22                 if (v == 4) {
23                         hp = gethostbyaddr(ip, 4, AF_INET);
24                         if (hp != NULL && hp->h_name != NULL &&
25                             *hp->h_name != '\0') {
26                                 strncpy(hostbuf, hp->h_name, sizeof(hostbuf));
27                                 hostbuf[sizeof(hostbuf) - 1] = '\0';
28                                 return hostbuf;
29                         }
30
31                         np = getnetbyaddr(ipa.s_addr, AF_INET);
32                         if (np != NULL && np->n_name != NULL &&
33                             *np->n_name != '\0') {
34                                 strncpy(hostbuf, np->n_name, sizeof(hostbuf));
35                                 hostbuf[sizeof(hostbuf) - 1] = '\0';
36                                 return hostbuf;
37                         }
38                 }
39         }
40
41         if (v == 4) {
42                 return inet_ntoa(ipa);
43         }
44 #ifdef  USE_INET6
45         (void) inet_ntop(AF_INET6, ip, hostbuf, sizeof(hostbuf) - 1);
46         hostbuf[MAXHOSTNAMELEN] = '\0';
47         return hostbuf;
48 #else
49         return "IPv6";
50 #endif
51 }