Sync usage with man page.
[netbsd-mini2440.git] / dist / ipf / lib / hostname.c
blobd46e3df4f51fc7cf61f58f73a85e0b9cf4d73251
1 /* $NetBSD$ */
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 */
11 #include "ipf.h"
13 char *hostname(v, ip)
14 int v;
15 void *ip;
17 static char hostbuf[MAXHOSTNAMELEN+1];
18 struct hostent *hp;
19 struct in_addr ipa;
20 struct netent *np;
22 memset(&ipa, 0, sizeof(ipa)); /* XXX gcc */
24 if (v == 4) {
25 ipa.s_addr = *(u_32_t *)ip;
26 if (ipa.s_addr == htonl(0xfedcba98))
27 return "test.host.dots";
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;
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;
50 if (v == 4) {
51 return inet_ntoa(ipa);
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