From: guy Date: Wed, 6 Jul 2005 20:53:30 +0000 (+0000) Subject: Have "ascii_print()" do the "-A" output, and nothing but the "-A" X-Git-Tag: tcpdump-4.0.0~375 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/e085b97e4b488bfd158529331e20ebaeb2b79f0f Have "ascii_print()" do the "-A" output, and nothing but the "-A" output. Have "hex_and_ascii_print_with_offset()" and "hex_and_ascii_print()" to the "-X"-style offset, leaving "hex_print_with_offset()" and "hex_print()" doing the "-x"-style output. Don't have any of them check "xflag", "Xflag", or "Aflag" - they print what they're intended to print, and the caller should check the flags in question. Don't have "-A" set "xflag" or "Xflag". This cleans up some problems with "-A" printing hex informatioin - it's not supposed to. --- diff --git a/interface.h b/interface.h index f30aeaf0..2c798812 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.255 2005-06-20 07:39:21 hannes Exp $ (LBL) + * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.256 2005-07-06 20:53:30 guy Exp $ (LBL) */ #ifndef tcpdump_interface_h @@ -166,11 +166,13 @@ extern const char *dnnum_string(u_short); #include extern int print_unknown_data(const u_char *, const char *,int); -extern void ascii_print_with_offset(const char *, const u_char *, u_int, u_int); -extern void ascii_print(const char *, const u_char *, u_int); +extern void ascii_print(const u_char *, u_int); +extern void hex_and_ascii_print_with_offset(const char *, const u_char *, + u_int, u_int); +extern void hex_and_ascii_print(const char *, const u_char *, u_int); extern void hex_print_with_offset(const char *, const u_char *, u_int, u_int); -extern void telnet_print(const u_char *, u_int); extern void hex_print(const char *, const u_char *, u_int); +extern void telnet_print(const u_char *, u_int); extern int ether_encap_print(u_short, const u_char *, u_int, u_int, u_short *); extern int llc_print(const u_char *, u_int, u_int, const u_char *, const u_char *, u_short *); diff --git a/netdissect.h b/netdissect.h index c4e34eed..09dc676f 100644 --- a/netdissect.h +++ b/netdissect.h @@ -21,7 +21,7 @@ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * @(#) $Header: /tcpdump/master/tcpdump/netdissect.h,v 1.16 2005-04-07 00:28:17 mcr Exp $ (LBL) + * @(#) $Header: /tcpdump/master/tcpdump/netdissect.h,v 1.17 2005-07-06 20:53:31 guy Exp $ (LBL) */ #ifndef netdissect_h @@ -261,13 +261,14 @@ extern void ip_print_inner(netdissect_options *ndo, /* stuff that has not yet been rototiled */ #if 0 -extern void ascii_print_with_offset(netdissect_options *, const char *, - u_int, u_int); -extern void ascii_print(netdissect_options *,const char *, u_int); +extern void ascii_print(netdissect_options *,u_int); +extern void hex_and_ascii_print_with_offset(netdissect_options *,const char *, + u_int, u_int); +extern void hex_and_ascii_print(netdissect_options *,const char *, u_int); extern void hex_print_with_offset(netdissect_options *,const char *, - u_int, u_int); -extern void telnet_print(netdissect_options *,const u_char *, u_int); -extern void hex_print(netdissect_options *,const char *, u_int); + u_int, u_int); +extern void hex_print(netdissect_options *,const char *, u_int); +extern void telnet_print(netdissect_options *,const u_char *, u_int); extern int ether_encap_print(netdissect_options *,u_short, const u_char *, u_int, u_int, u_short *); extern int llc_print(netdissect_options *, diff --git a/print-ascii.c b/print-ascii.c index 1b894850..fa8793cb 100644 --- a/print-ascii.c +++ b/print-ascii.c @@ -42,7 +42,7 @@ #ifndef lint static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.16 2004-07-21 22:00:10 guy Exp $"; + "@(#) $Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.17 2005-07-06 20:53:32 guy Exp $"; #endif #include #include @@ -57,78 +57,74 @@ static const char rcsid[] _U_ = (HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE) void -ascii_print_with_offset(register const char *ident, register const u_char *cp, register u_int length, - register u_int oset) +ascii_print(register const u_char *cp, register u_int length) +{ + register int s; + + putchar('\n'); + while (length > 0) { + s = *cp++; + length--; + if (!isgraph(s) && + (s != '\t' && s != ' ' && s != '\n' && s != '\r')) + putchar('.'); + else + putchar(s); + } +} + +void +hex_and_ascii_print_with_offset(register const char *ident, + register const u_char *cp, register u_int length, register u_int oset) { register u_int i; register int s1, s2; register int nshorts; char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp; char asciistuff[ASCII_LINELENGTH+1], *asp; - u_int maxlength = (Aflag ? ASCII_LINELENGTH : HEXDUMP_SHORTS_PER_LINE); nshorts = length / sizeof(u_short); i = 0; hsp = hexstuff; asp = asciistuff; - if (Aflag) *(asp++) = '\n'; while (--nshorts >= 0) { s1 = *cp++; s2 = *cp++; - if (Aflag) { - i += 2; - *(asp++) = (isgraph(s1) ? s1 : (s1 != '\t' && s1 != ' ' && s1 != '\n' && s1 != '\r' ? '.' : s1) ); - *(asp++) = (isgraph(s2) ? s2 : (s2 != '\t' && s2 != ' ' && s2 != '\n' && s2 != '\r' ? '.' : s2) ); - if (s1 == '\n' || s2 == '\n') i = maxlength; - - } else { - (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), - " %02x%02x", s1, s2); - hsp += HEXDUMP_HEXSTUFF_PER_SHORT; - *(asp++) = (isgraph(s1) ? s1 : '.'); - *(asp++) = (isgraph(s2) ? s2 : '.'); - i++; - } - if (i >= maxlength) { + (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), + " %02x%02x", s1, s2); + hsp += HEXDUMP_HEXSTUFF_PER_SHORT; + *(asp++) = (isgraph(s1) ? s1 : '.'); + *(asp++) = (isgraph(s2) ? s2 : '.'); + i++; + if (i >= HEXDUMP_SHORTS_PER_LINE) { *hsp = *asp = '\0'; - if (Aflag) { - (void)printf("%s", asciistuff); - } else { - (void)printf("%s0x%04x: %-*s %s", - ident, oset, HEXDUMP_HEXSTUFF_PER_LINE, - hexstuff, asciistuff); - } + (void)printf("%s0x%04x: %-*s %s", + ident, oset, HEXDUMP_HEXSTUFF_PER_LINE, + hexstuff, asciistuff); i = 0; hsp = hexstuff; asp = asciistuff; oset += HEXDUMP_BYTES_PER_LINE; } } if (length & 1) { s1 = *cp++; - if (Aflag) { - *(asp++) = (isgraph(s1) ? s1 : (s1 != '\t' && s1 != ' ' && s1 != '\n' && s1 != '\r' ? '.' : s1) ); - } else { - (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), - " %02x", s1); - hsp += 3; - *(asp++) = (isgraph(s1) ? s1 : '.'); - } + (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), + " %02x", s1); + hsp += 3; + *(asp++) = (isgraph(s1) ? s1 : '.'); ++i; } if (i > 0) { *hsp = *asp = '\0'; - if (Aflag) { - (void)printf("%s%s", ident, asciistuff); - } else { - (void)printf("%s0x%04x: %-*s %s", - ident, oset, HEXDUMP_HEXSTUFF_PER_LINE, - hexstuff, asciistuff); - } + (void)printf("%s0x%04x: %-*s %s", + ident, oset, HEXDUMP_HEXSTUFF_PER_LINE, + hexstuff, asciistuff); } } void -ascii_print(register const char *ident, register const u_char *cp, register u_int length) +hex_and_ascii_print(register const char *ident, register const u_char *cp, + register u_int length) { - ascii_print_with_offset(ident, cp, length, 0); + hex_and_ascii_print_with_offset(ident, cp, length, 0); } /* @@ -171,15 +167,17 @@ hex_print(register const char *ident, register const u_char *cp, register u_int int main(int argc, char *argv[]) { - hex_print("Hello, World!\n", 14); + hex_print("\n\t", "Hello, World!\n", 14); + printf("\n"); + hex_and_ascii_print("\n\t", "Hello, World!\n", 14); printf("\n"); ascii_print("Hello, World!\n", 14); printf("\n"); #define TMSG "Now is the winter of our discontent...\n" - ascii_print_with_offset(TMSG, sizeof(TMSG) - 1, 0x100); + hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100); + printf("\n"); + hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100); printf("\n"); exit(0); } #endif /* MAIN */ - - diff --git a/tcpdump.c b/tcpdump.c index bb6b88b7..3cb39fcc 100644 --- a/tcpdump.c +++ b/tcpdump.c @@ -30,7 +30,7 @@ static const char copyright[] _U_ = "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\ The Regents of the University of California. All rights reserved.\n"; static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.258 2005-06-03 22:08:53 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.259 2005-07-06 20:53:32 guy Exp $ (LBL)"; #endif /* @@ -492,8 +492,6 @@ main(int argc, char **argv) break; case 'A': - ++xflag; - ++Xflag; ++Aflag; break; @@ -1224,9 +1222,28 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) snapend = sp + h->caplen; hdrlen = (*print_info->printer)(h, sp); - if (xflag) { + if (Xflag) { /* - * Print the raw packet data. + * Print the raw packet data in hex and ASCII. + */ + if (Xflag > 1) { + /* + * Include the link-layer header. + */ + hex_and_ascii_print("\n\t", sp, h->caplen); + } else { + /* + * Don't include the link-layer header - and if + * we have nothing past the link-layer header, + * print nothing. + */ + if (h->caplen > hdrlen) + hex_and_ascii_print("\n\t", sp + hdrlen, + h->caplen - hdrlen); + } + } else if (xflag) { + /* + * Print the raw packet data in hex. */ if (xflag > 1) { /* @@ -1243,15 +1260,15 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) hex_print("\n\t", sp + hdrlen, h->caplen - hdrlen); } - } else if (Xflag) { + } else if (Aflag) { /* - * Print the raw packet data. + * Print the raw packet data in ASCII. */ - if (Xflag > 1) { + if (Aflag > 1) { /* * Include the link-layer header. */ - ascii_print("\n\t", sp, h->caplen); + ascii_print(sp, h->caplen); } else { /* * Don't include the link-layer header - and if @@ -1259,8 +1276,7 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) * print nothing. */ if (h->caplen > hdrlen) - ascii_print("\n\t", sp + hdrlen, - h->caplen - hdrlen); + ascii_print(sp + hdrlen, h->caplen - hdrlen); } } @@ -1299,12 +1315,12 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) #endif /* - * By default, print the specified data out in hex. + * By default, print the specified data out in hex and ASCII. */ static void ndo_default_print(netdissect_options *ndo _U_, const u_char *bp, u_int length) { - ascii_print("\n\t", bp, length); /* pass on lf and identation string */ + hex_and_ascii_print("\n\t", bp, length); /* pass on lf and identation string */ } void