]> The Tcpdump Group git mirrors - tcpdump/blobdiff - addrtoname.c
Bring in KAME IPv6 tcpdump. replaces esp/ah/isakmp decoder.
[tcpdump] / addrtoname.c
index bc7c22bde88e7a8414bc85ef7d3059813e252a3c..5af1275b8840fbee55b05cb7f20f2fa7de66896a 100644 (file)
@@ -23,7 +23,7 @@
  */
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.61 1999-10-07 23:47:09 mcr Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.62 1999-10-30 05:11:06 itojun Exp $ (LBL)";
 #endif
 
 #include <sys/types.h>
@@ -39,6 +39,10 @@ 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>
@@ -85,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;
@@ -171,39 +185,7 @@ getname(const u_char *ap)
 #ifndef LBL_ALIGN
        addr = *(const u_int32_t *)ap;
 #else
-       /*
-        * Extract 32 bits in network order, dealing with alignment.
-        */
-       switch ((long)ap & 3) {
-
-       case 0:
-               addr = *(u_int32_t *)ap;
-               break;
-
-       case 2:
-#ifdef WORDS_BIGENDIAN
-               addr = ((u_int32_t)*(u_short *)ap << 16) |
-                       (u_int32_t)*(u_short *)(ap + 2);
-#else
-               addr = ((u_int32_t)*(u_short *)(ap + 2) << 16) |
-                       (u_int32_t)*(u_short *)ap;
-#endif
-               break;
-
-       default:
-#ifdef WORDS_BIGENDIAN
-               addr = ((u_int32_t)ap[0] << 24) |
-                       ((u_int32_t)ap[1] << 16) |
-                       ((u_int32_t)ap[2] << 8) |
-                       (u_int32_t)ap[3];
-#else
-               addr = ((u_int32_t)ap[3] << 24) |
-                       ((u_int32_t)ap[2] << 16) |
-                       ((u_int32_t)ap[1] << 8) |
-                       (u_int32_t)ap[0];
-#endif
-               break;
-       }
+       memcpy(&addr, ap, sizeof(addr));
 #endif
        p = &hnametable[addr & (HASHNAMESIZE-1)];
        for (; p->nxt; p = p->nxt) {
@@ -249,6 +231,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[15] & (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";
 
 
@@ -764,3 +811,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 */