]> The Tcpdump Group git mirrors - tcpdump/commitdiff
add mask2plen function to util.c
authorhannes <hannes>
Sun, 22 Dec 2002 01:26:48 +0000 (01:26 +0000)
committerhannes <hannes>
Sun, 22 Dec 2002 01:26:48 +0000 (01:26 +0000)
interface.h
util.c

index 647403de6834df49f8494091f2630e83dc65dac9..9d2a41fcefaab619f28a44062521d6c74f8839c5 100644 (file)
@@ -18,7 +18,7 @@
  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
- * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.203 2002-12-19 09:39:10 guy Exp $ (LBL)
+ * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.204 2002-12-22 01:26:48 hannes Exp $ (LBL)
  */
 
 #ifndef tcpdump_interface_h
@@ -158,6 +158,7 @@ extern void relts_print(int);
 extern int fn_print(const u_char *, const u_char *);
 extern int fn_printn(const u_char *, u_int, const u_char *);
 extern const char *tok2str(const struct tok *, const char *, int);
+extern int mask2plen(u_int32_t);
 extern char *bittok2str(const struct tok *, const char *, int);
 extern const char *tok2strary_internal(const char **, int, const char *, int);
 #define        tok2strary(a,f,i) tok2strary_internal(a, sizeof(a)/sizeof(a[0]),f,i)
diff --git a/util.c b/util.c
index 84d4a076122d8c0144908b709edf8e63e808d3a6..a1c9f094c66673cb302836eba6cc7f62efc20c3c 100644 (file)
--- a/util.c
+++ b/util.c
@@ -21,7 +21,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.81 2002-11-07 20:07:58 hannes Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.82 2002-12-22 01:26:49 hannes Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -311,6 +311,37 @@ tok2strary_internal(register const char **lp, int n, register const char *fmt,
        return (buf);
 }
 
+/*
+ * Convert a 32-bit netmask to prefixlen if possible
+ * the function returns the prefix-len; if plen == -1
+ * then conversion was not possible;
+ */
+
+int
+mask2plen (u_int32_t mask)
+{
+       u_int32_t bitmasks[33] = {
+               0x00000000,
+               0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
+               0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
+               0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
+               0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
+               0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
+               0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
+               0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
+               0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff
+       };
+       int prefix_len = 33;
+
+        /* lets see if we can transform the mask into a prefixlen */
+        while (prefix_len >= 0) {
+            if (bitmasks[prefix_len] == mask)
+                break;
+            prefix_len--;
+        }
+        return (prefix_len);
+}
+
 /* VARARGS */
 void
 error(const char *fmt, ...)