tcpdump mailing list archives

[PATCH 1/2] util.c: Add the `mask62plen' utility function.


From: Florian Forster <tcpdump () nospam verplant org>
Date: Sun, 17 May 2009 00:05:29 +0200

From: Florian Forster <octo () leeloo lan home verplant org>

The function does the same as `mask2plen' but for IPv6.

Signed-off-by: Florian Forster <octo () leeloo lan home verplant org>
---
 interface.h |    1 +
 util.c      |   29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/interface.h b/interface.h
index 688a2ac..f6a4a19 100644
--- a/interface.h
+++ b/interface.h
@@ -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 784b09b..526a061 100644
--- 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, ...)
-- 
1.6.2.4

-
This is the tcpdump-workers list.
Visit https://cod.sandelman.ca/ to unsubscribe.


Current thread: