summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Schlyter <jakob@cvs.openbsd.org>2000-01-16 12:07:30 +0000
committerJakob Schlyter <jakob@cvs.openbsd.org>2000-01-16 12:07:30 +0000
commit1f422276195f7e3d9e8dc7ec2eba1b7c3aafea2c (patch)
treedd9e21fd07a351ad648425f959be9dc4e89b6c7e
parent309e3f7a5c479705e2d7447ebe30c607003943bf (diff)
INET6 address resolution (from KAME)
-rw-r--r--usr.sbin/tcpdump/addrtoname.c107
-rw-r--r--usr.sbin/tcpdump/addrtoname.h11
2 files changed, 116 insertions, 2 deletions
diff --git a/usr.sbin/tcpdump/addrtoname.c b/usr.sbin/tcpdump/addrtoname.c
index cd7d88d1990..c15f0bceb71 100644
--- a/usr.sbin/tcpdump/addrtoname.c
+++ b/usr.sbin/tcpdump/addrtoname.c
@@ -23,7 +23,7 @@
*/
#ifndef lint
static const char rcsid[] =
- "@(#) $Header: /cvs/OpenBSD/src/usr.sbin/tcpdump/addrtoname.c,v 1.9 1999/10/06 01:46:40 deraadt Exp $ (LBL)";
+ "@(#) $Header: /cvs/OpenBSD/src/usr.sbin/tcpdump/addrtoname.c,v 1.10 2000/01/16 12:07:29 jakob Exp $ (LBL)";
#endif
#include <sys/types.h>
@@ -39,13 +39,22 @@ struct rtentry;
#include <netinet/in.h>
#include <netinet/if_ether.h>
+#ifdef INET6
+#include <netinet6/ip6.h>
+#endif
+
#include <arpa/inet.h>
#include <ctype.h>
#include <netdb.h>
#include <pcap.h>
#include <pcap-namedb.h>
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
+#ifdef HAVE_MEMORY_H
#include <memory.h>
+#endif
#include <signal.h>
#include <stdio.h>
#include <string.h>
@@ -80,6 +89,16 @@ struct hnamemem eprototable[HASHNAMESIZE];
struct hnamemem dnaddrtable[HASHNAMESIZE];
struct hnamemem llcsaptable[HASHNAMESIZE];
+#ifdef INET6
+struct h6namemem {
+ struct in6_addr addr;
+ char *name;
+ struct h6namemem *nxt;
+};
+
+struct h6namemem h6nametable[HASHNAMESIZE];
+#endif /* INET6 */
+
struct enamemem {
u_short e_addr0;
u_short e_addr1;
@@ -244,6 +263,71 @@ getname(const u_char *ap)
return (p->name);
}
+#ifdef INET6
+/*
+ * Return a name for the IP6 address pointed to by ap. This address
+ * is assumed to be in network byte order.
+ */
+char *
+getname6(const u_char *ap)
+{
+ register struct hostent *hp;
+ struct in6_addr addr;
+ static struct h6namemem *p; /* static for longjmp() */
+ register char *cp;
+ char ntop_buf[INET6_ADDRSTRLEN];
+
+ memcpy(&addr, ap, sizeof(addr));
+ p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)];
+ for (; p->nxt; p = p->nxt) {
+ if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
+ return (p->name);
+ }
+ p->addr = addr;
+ p->nxt = newh6namemem();
+
+ /*
+ * Only print names when:
+ * (1) -n was not given.
+ * (2) Address is foreign and -f was given. (If -f was not
+ * give, f_netmask and f_local are 0 and the test
+ * evaluates to true)
+ * (3) -a was given or the host portion is not all ones
+ * nor all zeros (i.e. not a network or broadcast address)
+ */
+ if (!nflag
+#if 0
+ &&
+ (addr & f_netmask) == f_localnet &&
+ (aflag ||
+ !((addr & ~netmask) == 0 || (addr | netmask) == 0xffffffff))
+#endif
+ ) {
+ if (!setjmp(getname_env)) {
+ (void)setsignal(SIGALRM, nohostname);
+ (void)alarm(20);
+ hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
+ (void)alarm(0);
+ if (hp) {
+ char *dotp;
+
+ p->name = savestr(hp->h_name);
+ if (Nflag) {
+ /* Remove domain qualifications */
+ dotp = strchr(p->name, '.');
+ if (dotp)
+ *dotp = '\0';
+ }
+ return (p->name);
+ }
+ }
+ }
+ cp = (char *)inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
+ p->name = savestr(cp);
+ return (p->name);
+}
+#endif /* INET6 */
+
static char hex[] = "0123456789abcdef";
@@ -759,3 +843,24 @@ newhnamemem(void)
p = ptr++;
return (p);
}
+
+#ifdef INET6
+/* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
+struct h6namemem *
+newh6namemem(void)
+{
+ register struct h6namemem *p;
+ static struct h6namemem *ptr = NULL;
+ static u_int num = 0;
+
+ if (num <= 0) {
+ num = 64;
+ ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
+ if (ptr == NULL)
+ error("newh6namemem: calloc");
+ }
+ --num;
+ p = ptr++;
+ return (p);
+}
+#endif /* INET6 */
diff --git a/usr.sbin/tcpdump/addrtoname.h b/usr.sbin/tcpdump/addrtoname.h
index c3be7ea8a61..bd35301ec91 100644
--- a/usr.sbin/tcpdump/addrtoname.h
+++ b/usr.sbin/tcpdump/addrtoname.h
@@ -18,7 +18,7 @@
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
- * @(#) $Header: /cvs/OpenBSD/src/usr.sbin/tcpdump/addrtoname.h,v 1.6 1999/07/28 20:41:35 jakob Exp $ (LBL)
+ * @(#) $Header: /cvs/OpenBSD/src/usr.sbin/tcpdump/addrtoname.h,v 1.7 2000/01/16 12:07:29 jakob Exp $ (LBL)
*/
/* Name to address translation routines. */
@@ -28,9 +28,18 @@ extern char *etherproto_string(u_short);
extern char *tcpport_string(u_short);
extern char *udpport_string(u_short);
extern char *getname(const u_char *);
+#ifdef INET6
+extern char *getname6(const u_char *);
+#endif
extern char *intoa(u_int32_t);
extern void init_addrtoname(u_int32_t, u_int32_t);
extern struct hnamemem *newhnamemem(void);
+#ifdef INET6
+extern struct h6namemem *newh6namemem(void);
+#endif
#define ipaddr_string(p) getname((const u_char *)(p))
+#ifdef INET6
+#define ip6addr_string(p) getname6((const u_char *)(p))
+#endif