]> The Tcpdump Group git mirrors - tcpdump/blobdiff - util.c
From Peter Fales <[email protected]>: add support for
[tcpdump] / util.c
diff --git a/util.c b/util.c
index 9595dc10d10c13b75b30b0654fe00ba55d0923d4..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.79 2002-08-06 04:42:07 guy 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
@@ -206,6 +206,9 @@ print_unknown_data(const u_char *cp,const char *lf,int len)
 {
         int i;
 
+        if (len ==0)
+           return(0);
+
        printf("%s0x0000: ",lf);
        for(i=0;i<len;i++) {
            if (!TTEST2(*(cp+i), 1)) {
@@ -243,6 +246,51 @@ tok2str(register const struct tok *lp, register const char *fmt,
        return (buf);
 }
 
+/*
+ * Convert a bit token value to a string; use "fmt" if not found.
+ * this is useful for parsing bitfields, the output strings are comma seperated
+ */
+char *
+bittok2str(register const struct tok *lp, register const char *fmt,
+       register int v)
+{
+        static char buf[256]; /* our stringbuffer */
+        int buflen=0;
+        register int rotbit; /* this is the bit we rotate through all bitpositions */
+        register int tokval;
+
+       while (lp->s != NULL) {
+            tokval=lp->v;   /* load our first value */
+            rotbit=1;
+            while (rotbit != 0) {
+                /*
+                 * lets AND the rotating bit with our token value
+                 * and see if we have got a match
+                 */
+               if (tokval == (v&rotbit)) {
+                    /* ok we have found something */
+                    buflen+=snprintf(buf+buflen, sizeof(buf)-buflen, "%s, ",lp->s);
+                    break;
+                }
+                rotbit=rotbit<<1; /* no match - lets shift and try again */
+            }
+            lp++;
+       }
+
+        if (buflen != 0) { /* did we find anything */
+            /* yep, set the the trailing zero 2 bytes before to eliminate the last comma & whitespace */
+            buf[buflen-2] = '\0';
+            return (buf);
+        }
+        else {
+            /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */
+            if (fmt == NULL)
+               fmt = "#%d";
+            (void)snprintf(buf, sizeof(buf), fmt, v);
+            return (buf);
+        }
+}
+
 /*
  * Convert a value to a string using an array; the macro
  * tok2strary() in <interface.h> is the public interface to
@@ -263,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, ...)