--- tcpdump-3.4/tcpdump.c Sun Oct 19 05:50:17 1997 +++ tcpdump-3.4-new/tcpdump.c Tue Mar 23 14:29:08 2021 @@ -39,6 +39,9 @@ #include #include +#include +#include +#include #include #include @@ -54,6 +57,7 @@ #include "gmt2local.h" int aflag; /* translate network and broadcast addresses */ +int Aflag; /* print packet in visible character */ int dflag; /* print filter code */ int eflag; /* print ethernet header */ int fflag; /* don't translate "foreign" IP address */ @@ -149,7 +153,8 @@ opterr = 0; while ( - (op = getopt(argc, argv, "ac:defF:i:lnNOpqr:s:StT:vw:xY")) != EOF) + /* (op = getopt(argc, argv, "ac:defF:i:lnNOpqr:s:StT:vw:xY")) != EOF) */ + (op = getopt(argc, argv, "Aac:defF:i:lnNOpqr:s:StT:vw:xY")) != EOF) switch (op) { case 'a': @@ -262,7 +267,11 @@ case 'x': ++xflag; break; - + case 'A': + ++Aflag; ++xflag; + if (snaplen < MAX_SNAPLEN) + snaplen = MAX_SNAPLEN ; + break; default: usage(); /* NOTREACHED */ @@ -403,6 +412,74 @@ } } +int check_tcpdata(const u_char *bp, u_int length, int *datlen) +{ + const struct ip *ip ; + u_int hlen, len, off ; + + if ( length < sizeof( struct ip )) + return 0; + if ( *bp != 0x45 ) + return 0; + ip = ( const struct ip *)bp ; + if ( ip->ip_p != IPPROTO_TCP) + return 0; + + hlen = ip->ip_hl * 4 ; + len = ntohs(ip->ip_len) ; + off = ntohs(ip->ip_off) ; + +/* + printf( "\n### hlen =%d, len=%d, off=%d, length=%d, tcphdr=%d", + hlen, len, off, length, sizeof(struct tcphdr)) ; +*/ + if ( len < length ) + *datlen = len - hlen - sizeof(struct tcphdr) ; + else + *datlen = length - hlen - sizeof( struct tcphdr) ; + if (( off & 0x1fff) != 0) + { + return 0 ; + } + return ip->ip_hl * 4 + sizeof(struct tcphdr) ; +} + +void Adump(const u_char *cp, int datlen) +{ + int i,c ; + + if ( datlen > 0 ) + { + printf("\n\t\t\t\"") ; + for ( i = 0 ; i < datlen ; i++ ) + { + c = *cp++ ; + switch(c) + { + case '\t': fputs( "\\t", stdout) ; break ; + case '\v': fputs( "\\t", stdout) ; break ; + case '\b': fputs( "\\t", stdout) ; break ; + case '\r': fputs( "\\t", stdout) ; break ; + case '\n': fputs( "\\t", stdout) ; break ; + case '\f': fputs( "\\t", stdout) ; break ; + case 0x07: fputs( "\\t", stdout) ; break ; + case '\\': fputs( "\\t", stdout) ; break ; + case '"': fputs( "\\t", stdout) ; break ; + default : + if ( ' ' <= c && c <= 126 ) + putchar(c) ; + else + { + char buff[5] ; + sprintf( buff, "\\x%02x", c) ; + fputs(buff, stdout) ; + } + } + } + putchar('"') ; + } +} + /* * By default, print the packet out in hex. * @@ -411,11 +488,12 @@ void default_print(register const u_char *bp, register u_int length) { - register const u_short *sp; +/* register const u_short *sp; register u_int i; - register int nshorts; + register int nshorts;*/ + int hdrlen, datlen ; - if ((long)bp & 1) { +/* if ((long)bp & 1) { default_print_unaligned(bp, length); return; } @@ -431,10 +509,41 @@ if ((i % 8) == 0) (void)printf("\n\t\t\t"); (void)printf(" %02x", *(u_char *)sp); - } + }*/ + + if (Aflag && xflag > 1 || !Aflag && xflag > 0 ) + { + register const u_short *sp ; + register u_int i ; + register int nshorts ; + + if (( long )bp & 1 ) + { + default_print_unaligned( bp, length) ; + return ; + } + sp = ( u_short * )bp ; + nshorts = ( u_int )length / sizeof( u_short ) ; + i = 0 ; + while ( --nshorts >= 0 ) + { + if (( i++ % 8 ) == 0 ) + (void)printf( "\n\t\t\t") ; + (void)printf( " %04x", ntohs(*sp++)) ; + } + if ( length & 1 ) + { + if (( i % 8 ) == 0 ) + (void)printf("\n\t\t\t") ; + (void)printf(" %02x", *(u_char *)sp) ; + } + } + if (Aflag && ( hdrlen = check_tcpdata( bp, length, &datlen )) > 0) + Adump(bp + hdrlen, datlen ) ; } -__dead void + +__dead void usage(void) { extern char version[]; @@ -443,7 +552,8 @@ (void)fprintf(stderr, "%s version %s\n", program_name, version); (void)fprintf(stderr, "libpcap version %s\n", pcap_version); (void)fprintf(stderr, -"Usage: %s [-adeflnNOpqStvx] [-c count] [ -F file ]\n", program_name); +/*"Usage: %s [-adeflnNOpqStvx] [-c count] [ -F file ]\n", program_name);*/ +"Usage: %s [-AadeflnNOpqStvx] [-c count] [ -F file ]\n", program_name); (void)fprintf(stderr, "\t\t[ -i interface ] [ -r file ] [ -s snaplen ]\n"); (void)fprintf(stderr,