From: guy Date: Fri, 6 May 2005 07:57:17 +0000 (+0000) Subject: Add an "fn_printzp()" routine for printing null-padded strings (strings X-Git-Tag: tcpdump-3.9.1~62 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/01fe0ebe27b2fe957f699ffa4dbdedc46e12f85b Add an "fn_printzp()" routine for printing null-padded strings (strings 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. --- diff --git a/interface.h b/interface.h index ca8701fc..f85e4412 100644 --- a/interface.h +++ b/interface.h @@ -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.244.2.5 2005-05-03 20:39:25 hannes Exp $ (LBL) + * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.244.2.6 2005-05-06 07:57:17 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) diff --git a/print-hsrp.c b/print-hsrp.c index fe49538e..21ec0540 100644 --- a/print-hsrp.c +++ b/print-hsrp.c @@ -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.9.2.1 2005-05-06 07:57:17 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; diff --git a/print-ipx.c b/print-ipx.c index b5f6de6d..77fbcb67 100644 --- a/print-ipx.c +++ b/print-ipx.c @@ -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.40.2.1 2005-05-06 07:57:18 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; diff --git a/print-nfs.c b/print-nfs.c index d74f43ff..7502823e 100644 --- a/print-nfs.c +++ b/print-nfs.c @@ -21,7 +21,7 @@ #ifndef lint static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/print-nfs.c,v 1.106.2.1 2005-04-20 21:52:31 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/print-nfs.c,v 1.106.2.2 2005-05-06 07:57:18 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); diff --git a/print-ntp.c b/print-ntp.c index f9b65b25..b408e087 100644 --- a/print-ntp.c +++ b/print-ntp.c @@ -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.41.2.1 2005-05-06 07:57:18 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: diff --git a/print-ospf.c b/print-ospf.c index f9cff38b..60cb6eb9 100644 --- a/print-ospf.c +++ b/print-ospf.c @@ -23,7 +23,7 @@ #ifndef lint static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/print-ospf.c,v 1.56.2.1 2005-04-20 21:55:32 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/print-ospf.c,v 1.56.2.2 2005-05-06 07:57:19 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; diff --git a/print-snmp.c b/print-snmp.c index 387a9d5a..15e440f5 100644 --- a/print-snmp.c +++ b/print-snmp.c @@ -58,7 +58,7 @@ #ifndef lint static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/print-snmp.c,v 1.62.2.1 2005-04-18 00:08:02 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/print-snmp.c,v 1.62.2.2 2005-05-06 07:57:19 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++) { diff --git a/print-vrrp.c b/print-vrrp.c index 12fbe7b3..12e47716 100644 --- a/print-vrrp.c +++ b/print-vrrp.c @@ -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.9.2.1 2005-05-06 07:57:20 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 27b36f49..f6443051 100644 --- 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.95.2.2 2005-04-26 03:44:36 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.95.2.3 2005-05-06 07:57:20 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 */