]> The Tcpdump Group git mirrors - tcpdump/blobdiff - util.c
Define HAVE_PCAP_DUMP_FLUSH, as the current CVS version of WinPcap has
[tcpdump] / util.c
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, ...)