Skip to content

Commit d0c66a2

Browse files
committed
Add --skip option to skip some packets before writing or printing
With this change, we can write/print some contiguous packets from a file. We can also skip some packets doing a live capture. The '--skip 0' option is allowed to help some loop in a shell script. Examples: Skip 3 packets when printing: tcpdump -#n --skip 3 -r in.pcap Write the sixth packet, if any: tcpdump --skip 5 -c 1 -r in.pcap -w out.pcap Write up to 5 packets after skipping 3: tcpdump --skip 3 -c 5 -r in.pcap -w out.pcap
1 parent 07a730a commit d0c66a2

File tree

7 files changed

+52
-4
lines changed

7 files changed

+52
-4
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group
3737
Add optional unit suffix on -C file size.
3838
Add --print-sampling to print every Nth packet instead of all.
3939
Add --lengths option to print the captured and original packet lengths.
40+
Add --skip option to skip some packets before writing or printing.
4041
Source code:
4142
Drop support for building with versions of libpcap that don't
4243
support all the libpcap 1.0 APIs.

tcpdump.1.in

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,21 @@ tcpdump \- dump traffic on a network
108108
.I snaplen
109109
]
110110
[
111+
.B \-\-skip
112+
.I count
113+
]
114+
[
111115
.B \-T
112116
.I type
113117
]
114118
[
115119
.B \-\-version
116120
]
121+
.ti +8
117122
[
118123
.B \-V
119124
.I file
120125
]
121-
.ti +8
122126
[
123127
.B \-w
124128
.I file
@@ -275,7 +279,10 @@ Set the operating system capture buffer size to \fIbuffer_size\fP, in
275279
units of KiB (1024 bytes).
276280
.TP
277281
.BI \-c " count"
278-
Exit after receiving \fIcount\fP packets.
282+
Exit after receiving or reading \fIcount\fP packets.
283+
If the
284+
.B --skip
285+
option is used, the \fIcount\fP starts after the skipped packets.
279286
.TP
280287
.BI \-\-count
281288
Print only on stdout the packet count when reading capture file(s) instead
@@ -761,6 +768,10 @@ protocol information you're interested in. Setting
761768
for backwards compatibility with recent older versions of
762769
.IR tcpdump .
763770
.TP
771+
.BI \-\-skip " count"
772+
Skip \fIcount\fP packets before writing or printing.
773+
\fIcount\fP with value 0 is allowed.
774+
.TP
764775
.BI \-T " type"
765776
Force packets selected by "\fIexpression\fP" to be interpreted the
766777
specified \fItype\fR.

tcpdump.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ static int timeout = 1000; /* default timeout = 1000 ms = 1 s */
211211
static int immediate_mode;
212212
#endif
213213
static int count_mode;
214+
static u_int packets_skipped;
214215

215216
static int infodelay;
216217
static int infoprint;
@@ -642,6 +643,7 @@ show_remote_devices_and_exit(void)
642643
#define OPTION_PRINT_SAMPLING 137
643644
#define OPTION_LENGTHS 138
644645
#define OPTION_TIME_T_SIZE 139
646+
#define OPTION_SKIP 140
645647

646648
static const struct option longopts[] = {
647649
{ "buffer-size", required_argument, NULL, 'B' },
@@ -684,6 +686,7 @@ static const struct option longopts[] = {
684686
{ "print-sampling", required_argument, NULL, OPTION_PRINT_SAMPLING },
685687
{ "lengths", no_argument, NULL, OPTION_LENGTHS },
686688
{ "time-t-size", no_argument, NULL, OPTION_TIME_T_SIZE },
689+
{ "skip", required_argument, NULL, OPTION_SKIP },
687690
{ "version", no_argument, NULL, OPTION_VERSION },
688691
{ NULL, 0, NULL, 0 }
689692
};
@@ -1943,6 +1946,14 @@ main(int argc, char **argv)
19431946
error("invalid print sampling %s", optarg);
19441947
break;
19451948

1949+
case OPTION_SKIP:
1950+
errno = 0;
1951+
packets_skipped = (u_int)strtoul(optarg, &end, 0);
1952+
if (optarg[0] == '-' || optarg == end || *end != '\0' ||
1953+
errno != 0)
1954+
error("invalid packet skipped %s", optarg);
1955+
break;
1956+
19461957
#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
19471958
case OPTION_TSTAMP_MICRO:
19481959
ndo->ndo_tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
@@ -2569,7 +2580,9 @@ DIAG_ON_ASSIGN_ENUM
25692580
#endif /* HAVE_CAPSICUM */
25702581

25712582
do {
2572-
status = pcap_loop(pd, cnt, callback, pcap_userdata);
2583+
status = pcap_loop(pd,
2584+
cnt + (cnt == -1 ? 0 : packets_skipped),
2585+
callback, pcap_userdata);
25732586
if (WFileName == NULL) {
25742587
/*
25752588
* We're printing packets. Flush the printed output,
@@ -2931,6 +2944,9 @@ dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *s
29312944

29322945
dump_info = (struct dump_info *)user;
29332946

2947+
if (packets_captured <= packets_skipped)
2948+
return;
2949+
29342950
/*
29352951
* XXX - this won't force the file to rotate on the specified time
29362952
* boundary, but it will rotate on the first packet received after the
@@ -3060,6 +3076,9 @@ dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
30603076

30613077
dump_info = (struct dump_info *)user;
30623078

3079+
if (packets_captured <= packets_skipped)
3080+
return;
3081+
30633082
pcap_dump((u_char *)dump_info->pdd, h, sp);
30643083
if (Uflag)
30653084
pcap_dump_flush(dump_info->pdd);
@@ -3079,7 +3098,7 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
30793098

30803099
++infodelay;
30813100

3082-
if (!count_mode)
3101+
if (!count_mode && packets_captured > packets_skipped)
30833102
pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
30843103

30853104
--infodelay;

tests/TESTLIST

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ dns_udp dns_udp.pcap dns_udp.out
352352
dns_udp-v dns_udp.pcap dns_udp-v.out -v
353353
dns_udp-vv dns_udp.pcap dns_udp-vv.out -vv
354354
dns_udp-vvv dns_udp.pcap dns_udp-vvv.out -vvv
355+
# tests with --skip option
356+
dns_tcp-skip-3 dns_tcp.pcap dns_tcp-skip-3.out --skip 3
357+
dns_tcp-skip-3-c-4 dns_tcp.pcap dns_tcp-skip-3-c-4.out --skip 3 -c 4
358+
dns_tcp-skip-3-c-1 dns_tcp.pcap dns_tcp-skip-3-c-1.out --skip 3 -c 1
355359

356360
# DNS on non-standard ports.
357361
dns_tcp_8053 dns_tcp_8053.pcap dns_tcp_8053.out -vv

tests/dns_tcp-skip-3-c-1.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4 2020-06-10 09:21:03.847323 IP 192.168.1.11.33779 > 209.87.249.18.53: Flags [P.], seq 603899917:603899975, ack 2043824404, win 64240, length 58 17177+ [1au] A? www.tcpdump.org. (56)

tests/dns_tcp-skip-3-c-4.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
4 2020-06-10 09:21:03.847323 IP 192.168.1.11.33779 > 209.87.249.18.53: Flags [P.], seq 603899917:603899975, ack 2043824404, win 64240, length 58 17177+ [1au] A? www.tcpdump.org. (56)
2+
5 2020-06-10 09:21:03.847457 IP 209.87.249.18.53 > 192.168.1.11.33779: Flags [.], ack 58, win 64240, length 0
3+
6 2020-06-10 09:21:03.973180 IP 209.87.249.18.53 > 192.168.1.11.33779: Flags [P.], seq 1:227, ack 58, win 64240, length 226 17177*- 2/2/5 A 192.139.46.66, A 198.199.88.104 (224)
4+
7 2020-06-10 09:21:03.973220 IP 192.168.1.11.33779 > 209.87.249.18.53: Flags [.], ack 227, win 64014, length 0

tests/dns_tcp-skip-3.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
4 2020-06-10 09:21:03.847323 IP 192.168.1.11.33779 > 209.87.249.18.53: Flags [P.], seq 603899917:603899975, ack 2043824404, win 64240, length 58 17177+ [1au] A? www.tcpdump.org. (56)
2+
5 2020-06-10 09:21:03.847457 IP 209.87.249.18.53 > 192.168.1.11.33779: Flags [.], ack 58, win 64240, length 0
3+
6 2020-06-10 09:21:03.973180 IP 209.87.249.18.53 > 192.168.1.11.33779: Flags [P.], seq 1:227, ack 58, win 64240, length 226 17177*- 2/2/5 A 192.139.46.66, A 198.199.88.104 (224)
4+
7 2020-06-10 09:21:03.973220 IP 192.168.1.11.33779 > 209.87.249.18.53: Flags [.], ack 227, win 64014, length 0
5+
8 2020-06-10 09:21:03.974844 IP 192.168.1.11.33779 > 209.87.249.18.53: Flags [F.], seq 58, ack 227, win 64014, length 0
6+
9 2020-06-10 09:21:03.975246 IP 209.87.249.18.53 > 192.168.1.11.33779: Flags [.], ack 59, win 64239, length 0
7+
10 2020-06-10 09:21:04.101184 IP 209.87.249.18.53 > 192.168.1.11.33779: Flags [FP.], seq 227, ack 59, win 64239, length 0
8+
11 2020-06-10 09:21:04.101256 IP 192.168.1.11.33779 > 209.87.249.18.53: Flags [.], ack 228, win 64014, length 0

0 commit comments

Comments
 (0)