]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Add an "fn_printzp()" routine for printing null-padded strings (strings
authorguy <guy>
Fri, 6 May 2005 07:56:51 +0000 (07:56 +0000)
committerguy <guy>
Fri, 6 May 2005 07:56:51 +0000 (07:56 +0000)
with a maximum length, where a string shorter than that length is padded
with NULs), as "fn_print()" won't handle the maximum length *and* the
snapshot length and "fn_printn()" won't stop on a null string.  Use it
where appropriate.

Always pass "snapend" to "fn_print()" and "fn_printn()" if they're
passed a pointer into the packet data; only pass NULL if they're being
handed a pointer into a buffer that's not part of the packet data.

Always check the return value of "fn_print()", "fn_printn()", and
"fn_printzp()" if they're passed "snapend", and do the appropriate
string termination and "packet truncated" indication if they return 1.

interface.h
print-hsrp.c
print-ipx.c
print-nfs.c
print-ntp.c
print-ospf.c
print-snmp.c
print-vrrp.c
util.c

index a48500c06d0d40db4d1ede490219a52d593b7620..7fccd5233ee3e81c130ce051cd9ce0d0401e09b9 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.249 2005-05-03 20:35:41 hannes Exp $ (LBL)
+ * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.250 2005-05-06 07:56:51 guy Exp $ (LBL)
  */
 
 #ifndef tcpdump_interface_h
@@ -137,6 +137,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 int fn_printzp(const u_char *, u_int, const u_char *);
 extern int mask2plen(u_int32_t);
 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)
index fe49538e45dfc46d1897f06e65c8459496bba69f..06304fd5677b8695330d190232f4047915502f4b 100644 (file)
@@ -31,7 +31,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-hsrp.c,v 1.9 2003-11-16 09:36:22 guy Exp $";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-hsrp.c,v 1.10 2005-05-06 07:56:52 guy Exp $";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -127,7 +127,11 @@ hsrp_print(register const u_int8_t *bp, register u_int len)
                relts_print(hp->hsrp_holdtime);
                printf(" priority=%d", hp->hsrp_priority);
                printf(" auth=\"");
-               fn_printn(hp->hsrp_authdata, sizeof(hp->hsrp_authdata), NULL);
+               if (fn_printn(hp->hsrp_authdata, sizeof(hp->hsrp_authdata),
+                   snapend)) {
+                       printf("\"");
+                       goto trunc;
+               }
                printf("\"");
        }
        return;
index b5f6de6d2b574892fb2f10b571f283d88eb41312..d5803b9debd37451e79162d39e6be5e6f4aca11a 100644 (file)
@@ -24,7 +24,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-ipx.c,v 1.40 2004-05-26 19:57:57 guy Exp $";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-ipx.c,v 1.41 2005-05-06 07:56:52 guy Exp $";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -161,7 +161,10 @@ ipx_sap_print(const u_short *ipx, u_int length)
        for (i = 0; i < 8 && length > 0; i++) {
            TCHECK2(ipx[25], 10);
            (void)printf(" %s '", ipxsap_string(htons(EXTRACT_16BITS(&ipx[0]))));
-           fn_print((u_char *)&ipx[1], (u_char *)&ipx[1] + 48);
+           if (fn_printzp((u_char *)&ipx[1], 48, snapend)) {
+               printf("'");
+               goto trunc;
+           }
            printf("' addr %s",
                ipxaddr_string(EXTRACT_32BITS(&ipx[25]), (u_char *)&ipx[27]));
            ipx += 32;
index 9d13e03dfa7893efb243dab60fe12492b9240f99..a81f4b765021b1ca64499c4a3b024cbec24594f3 100644 (file)
@@ -21,7 +21,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-nfs.c,v 1.107 2005-04-20 21:52:53 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-nfs.c,v 1.108 2005-05-06 07:56:52 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -388,9 +388,11 @@ parsefn(register const u_int32_t *dp)
        cp = (u_char *)dp;
        /* Update 32-bit pointer (NFS filenames padded to 32-bit boundaries) */
        dp += ((len + 3) & ~3) / sizeof(*dp);
-       /* XXX seems like we should be checking the length */
        putchar('"');
-       (void) fn_printn(cp, len, NULL);
+       if (fn_printn(cp, len, snapend)) {
+               putchar('"');
+               goto trunc;
+       }
        putchar('"');
 
        return (dp);
index f9b65b2596abdfdab4156714c0742bd3cd65d5df..4960dc829c50b02eb2a0d95fc9d47e8b6a32f353 100644 (file)
@@ -25,7 +25,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.41 2004-01-28 14:54:50 hannes Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-ntp.c,v 1.42 2005-05-06 07:56:53 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -133,7 +133,8 @@ ntp_print(register const u_char *cp, u_int length)
                break;
 
        case PRIM_REF:
-               fn_printn((u_char *)&(bp->refid), 4, NULL);
+               if (fn_printn((u_char *)&(bp->refid), 4, snapend))
+                       goto trunc;
                break;
 
        case INFO_QUERY:
index 6fd33b28088662798d1bfb1e59eb252d53a8429c..af91c6876420048d42de3701d4f96cd5f641bb41 100644 (file)
@@ -23,7 +23,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-ospf.c,v 1.57 2005-04-20 21:55:14 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-ospf.c,v 1.58 2005-05-06 07:56:53 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -932,8 +932,11 @@ ospf_print(register const u_char *bp, register u_int length,
                        break;
 
                case OSPF_AUTH_SIMPLE:
-                       (void)fn_printn(op->ospf_authdata,
-                           sizeof(op->ospf_authdata), NULL);
+                       if (fn_printn(op->ospf_authdata,
+                           sizeof(op->ospf_authdata), snapend)) {
+                               printf("\"");
+                               goto trunc;
+                       }
                        printf("\"");
                        break;
 
index 60ba4d22507c3931de72f846d91e34d4e853c02b..728da6beb79ebca0bdeac14455d50173676bd2fa 100644 (file)
@@ -58,7 +58,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-snmp.c,v 1.63 2005-04-18 00:07:31 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-snmp.c,v 1.64 2005-05-06 07:56:53 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -786,7 +786,10 @@ asn1_print(struct be *elem)
                p = elem->data.str;
                if (printable) {
                        putchar('"');
-                       (void)fn_print(p, p + asnlen);
+                       if (fn_printn(p, asnlen, snapend)) {
+                               putchar('"');
+                               goto trunc;
+                       }
                        putchar('"');
                } else
                        for (i = asnlen; i-- > 0; p++) {
index 12fbe7b30764db286971fcc5753d6b2d92f13ffa..899542dac42938bb703addd9c8b3374f90f4d17b 100644 (file)
@@ -25,7 +25,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-vrrp.c,v 1.9 2003-11-16 09:36:41 guy Exp $";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-vrrp.c,v 1.10 2005-05-06 07:56:54 guy Exp $";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -128,7 +128,10 @@ vrrp_print(register const u_char *bp, register u_int len, int ttl)
                if (auth_type == VRRP_AUTH_SIMPLE) { /* simple text password */
                        TCHECK(bp[7]);
                        printf(" auth \"");
-                       fn_printn(bp, 8, NULL);
+                       if (fn_printn(bp, 8, snapend)) {
+                               printf("\"");
+                               goto trunc;
+                       }
                        printf("\"");
                }
        }
diff --git a/util.c b/util.c
index eddf8c72c7214f258c183c9bd3c557442f188553..d4882d72cc9a7311389c721d359fc9244949c234 100644 (file)
--- a/util.c
+++ b/util.c
@@ -21,7 +21,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.97 2005-04-26 03:43:27 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.98 2005-05-06 07:56:54 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -45,7 +45,7 @@ static const char rcsid[] _U_ =
 #include "interface.h"
 
 /*
- * Print out a filename (or other ascii string).
+ * Print out a null-terminated filename (or other ascii string).
  * If ep is NULL, assume no truncation check is needed.
  * Return true if truncated.
  */
@@ -104,6 +104,40 @@ fn_printn(register const u_char *s, register u_int n,
        return (n == 0) ? 0 : 1;
 }
 
+/*
+ * Print out a null-padded filename (or other ascii string).
+ * If ep is NULL, assume no truncation check is needed.
+ * Return true if truncated.
+ */
+int
+fn_printzp(register const u_char *s, register u_int n,
+         register const u_char *ep)
+{
+       register int ret;
+       register u_char c;
+
+       ret = 1;                        /* assume truncated */
+       while (n > 0 && (ep == NULL || s < ep)) {
+               n--;
+               c = *s++;
+               if (c == '\0') {
+                       ret = 0;
+                       break;
+               }
+               if (!isascii(c)) {
+                       c = toascii(c);
+                       putchar('M');
+                       putchar('-');
+               }
+               if (!isprint(c)) {
+                       c ^= 0x40;      /* DEL to ?, others to alpha */
+                       putchar('^');
+               }
+               putchar(c);
+       }
+       return (n == 0) ? 0 : 1;
+}
+
 /*
  * Print the timestamp
  */