]> The Tcpdump Group git mirrors - tcpdump/commitdiff
util.c: Add the `mask62plen' utility function.
authorFlorian Forster <[email protected]>
Sat, 16 May 2009 22:05:29 +0000 (00:05 +0200)
committerGuy Harris <[email protected]>
Thu, 21 May 2009 17:29:24 +0000 (10:29 -0700)
The function does the same as `mask2plen' but for IPv6.

Signed-off-by: Florian Forster <[email protected]>
interface.h
util.c

index 688a2ac90dcf2d570daafc88715aa93c70eb62ff..f6a4a191fea6ea860877d08cb322fe0a1d782199 100644 (file)
@@ -336,6 +336,7 @@ extern void ripng_print(const u_char *, unsigned int);
 extern int rt6_print(const u_char *, const u_char *);
 extern void ospf6_print(const u_char *, u_int);
 extern void dhcp6_print(const u_char *, u_int);
+extern int mask62plen(const u_char *);
 #endif /*INET6*/
 extern u_short in_cksum(const u_short *, register u_int, int);
 extern u_int16_t in_cksum_shouldbe(u_int16_t, u_int16_t);
diff --git a/util.c b/util.c
index 784b09b788b03f17e12dccc099321613e50a2ae3..526a0611e1af7a8d23be5d43c431fac7cbde6112 100644 (file)
--- a/util.c
+++ b/util.c
@@ -439,6 +439,35 @@ mask2plen (u_int32_t mask)
        return (prefix_len);
 }
 
+#ifdef INET6
+int mask62plen(const u_char *mask)
+{
+       u_char bitmasks[] =
+       {
+               0x00, 0x80, 0xc0, 0xe0,
+               0xf0, 0xf8, 0xfc, 0xfe, 0xff
+       };
+       int cidr_len = 0;
+       int byte;
+
+       for (byte = 0; byte < 16; byte++)
+       {
+               int bits;
+
+               for (bits = 0; bits < (sizeof (bitmasks) / sizeof (bitmasks[0])); bits++)
+                       if (mask[byte] == bitmasks[bits])
+                       {
+                               cidr_len += bits;
+                               break;
+                       }
+
+               if (mask[byte] != 0xff)
+                       break;
+       }
+       return (cidr_len);
+}
+#endif /* INET6 */
+
 /* VARARGS */
 void
 error(const char *fmt, ...)