Skip to content

Commit 4c5bef9

Browse files
committed
Add --lengths option to print the captured and original packet lengths
The lengths will be printed at the beginning of the line or after the packet number, if any. 'caplen' is the captured length. 'len' is the original (on wire) length. Examples 1) With -#n 1 caplen 80 len 98 14:41:53.503612 IP 192.168.1.11.43966 > [...] 2) With -n caplen 80 len 98 14:43:38.185603 IP 192.168.1.11.43966 > [...] Add a test file with one packet not truncated, the other truncated. [skip ci]
1 parent 5ee91ef commit 4c5bef9

File tree

8 files changed

+31
-2
lines changed

8 files changed

+31
-2
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group
2121
User interface:
2222
Add optional unit suffix on -C file size.
2323
Add --print-sampling to print every Nth packet instead of all.
24+
Add --lengths option to print the captured and original packet lengths.
2425
Source code:
2526
Use %zu when printing a sizeof to squelch compiler warnings
2627
Remove unused missing/snprintf.c.

netdissect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ struct netdissect_options {
228228
jmp_buf ndo_early_end; /* jmp_buf for setjmp()/longjmp() */
229229
void *ndo_last_mem_p; /* pointer to the last allocated memory chunk */
230230
int ndo_packet_number; /* print a packet number in the beginning of line */
231+
int ndo_lengths; /* print packet header caplen and len */
231232
int ndo_print_sampling; /* print every Nth packet */
232233
int ndo_suppress_default_print; /* don't use default_print() for unknown packet types */
233234
int ndo_tstamp_precision; /* requested time stamp precision */

print.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h,
329329
if (ndo->ndo_packet_number)
330330
ND_PRINT("%5u ", packets_captured);
331331

332+
if (ndo->ndo_lengths)
333+
ND_PRINT("caplen %u len %u ", h->caplen, h->len);
334+
332335
/* Sanity checks on packet length / capture length */
333336
if (h->caplen == 0) {
334337
invalid_header = 1;

tcpdump.1.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ tcpdump \- dump traffic on a network
7070
.B \-j
7171
.I tstamp_type
7272
]
73+
.ti +8
74+
[
75+
.BI \-\-lengths
76+
]
7377
[
7478
.B \-m
7579
.I module
@@ -610,6 +614,13 @@ and another set of data link types when in monitor mode (for example, it
610614
might support 802.11 headers, or 802.11 headers with radio information,
611615
only in monitor mode).
612616
.TP
617+
.BI \-\-lengths
618+
Print the captured and original packet lengths.
619+
The lengths are printed at the beginning of the line or after the packet
620+
number, if any.
621+
\fIcaplen\fP is the captured packet length (See \fB-s\fP option).
622+
\fIlen\fP is the original (on wire) packet length.
623+
.TP
613624
.BI \-m " module"
614625
Load SMI MIB module definitions from file \fImodule\fR.
615626
This option

tcpdump.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ show_remote_devices_and_exit(void)
705705
#define OPTION_FP_TYPE 135
706706
#define OPTION_COUNT 136
707707
#define OPTION_PRINT_SAMPLING 137
708+
#define OPTION_LENGTHS 138
708709

709710
static const struct option longopts[] = {
710711
#if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
@@ -753,12 +754,13 @@ static const struct option longopts[] = {
753754
{ "number", no_argument, NULL, '#' },
754755
{ "print", no_argument, NULL, OPTION_PRINT },
755756
{ "print-sampling", required_argument, NULL, OPTION_PRINT_SAMPLING },
757+
{ "lengths", no_argument, NULL, OPTION_LENGTHS },
756758
{ "version", no_argument, NULL, OPTION_VERSION },
757759
{ NULL, 0, NULL, 0 }
758760
};
759761

760762
#ifdef HAVE_PCAP_FINDALLDEVS_EX
761-
#define LIST_REMOTE_INTERFACES_USAGE "[ --list-remote-interfaces remote-source ]"
763+
#define LIST_REMOTE_INTERFACES_USAGE " [ --list-remote-interfaces remote-source ]"
762764
#else
763765
#define LIST_REMOTE_INTERFACES_USAGE
764766
#endif
@@ -1990,6 +1992,10 @@ main(int argc, char **argv)
19901992
ndo->ndo_packet_number = 1;
19911993
break;
19921994

1995+
case OPTION_LENGTHS:
1996+
ndo->ndo_lengths = 1;
1997+
break;
1998+
19931999
case OPTION_VERSION:
19942000
print_version(stdout);
19952001
exit_tcpdump(S_SUCCESS);
@@ -3340,7 +3346,7 @@ print_usage(FILE *f)
33403346
"\t\t[ -i interface ]" IMMEDIATE_MODE_USAGE j_FLAG_USAGE "\n");
33413347
#ifdef HAVE_PCAP_FINDALLDEVS_EX
33423348
(void)fprintf(f,
3343-
"\t\t" LIST_REMOTE_INTERFACES_USAGE "\n");
3349+
"\t\t[ --lengths ]" LIST_REMOTE_INTERFACES_USAGE "\n");
33443350
#endif
33453351
#ifdef USE_LIBSMI
33463352
(void)fprintf(f,

tests/TESTLIST

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ dns_tcp_8053-T dns_tcp_8053.pcap dns_tcp_8053-T.out -vv -T domain
347347
dns_udp_8053 dns_udp_8053.pcap dns_udp_8053.out -vv
348348
dns_udp_8053-T dns_udp_8053.pcap dns_udp_8053-T.out -vv -T domain
349349

350+
# test with --lengths option
351+
dns_udp_2--lengths dns_udp_2.pcap dns_udp_2.out --lengths -vv
352+
350353
# DNSSEC from https://bugzilla.redhat.com/show_bug.cgi?id=205842, -vv exposes EDNS DO
351354
dnssec-vv dnssec.pcap dnssec-vv.out -vv
352355

tests/dns_udp_2.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 caplen 98 len 98 09:37:33.129402 IP (tos 0x0, ttl 64, id 22989, offset 0, flags [none], proto UDP (17), length 84)
2+
192.168.1.11.43966 > 209.87.249.18.53: [udp sum ok] 22836+ [1au] A? www.tcpdump.org. ar: . OPT UDPsize=4096 [COOKIE 42f5d00996f90b13] (56)
3+
2 caplen 98 len 266 09:37:33.259762 IP (tos 0x0, ttl 128, id 45, offset 0, flags [none], proto UDP (17), length 252)
4+
209.87.249.18.53 > 192.168.1.11.43966: 22836*- q: A? www.tcpdump.org. 2/2/5 www.tcpdump.org. A 192.139.46.66, www.tcpdump.org. [|domain]

tests/dns_udp_2.pcap

252 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)