summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancois-Xavier Le Bail <devel.fx.lebail@orange.fr>2020-02-26 16:55:21 +0100
committerFrancois-Xavier Le Bail <devel.fx.lebail@orange.fr>2020-02-26 20:12:30 +0100
commit4d810e062dc6bdfd75f7c70a24c90eeaf3094636 (patch)
tree31d0d173d37240fce940dd6deb0fa7ff9cb3ed6b
parenta3bbf1e278bcfd4479f06b499d3c993adecda69f (diff)
downloadtcpdump-4d810e062dc6bdfd75f7c70a24c90eeaf3094636.tar.gz
Print an optional packet count on stderr instead of printing packets
This change add an option to print only on stderr the packet count when reading capture file(s) instead of parsing/printing the packets. If a filter is specified on the command line, tcpdump counts only packets that were matched by the filter expression. The option name is '--count'.
-rw-r--r--tcpdump.1.in9
-rw-r--r--tcpdump.c16
2 files changed, 23 insertions, 2 deletions
diff --git a/tcpdump.1.in b/tcpdump.1.in
index ccecde6a..39bfe22b 100644
--- a/tcpdump.1.in
+++ b/tcpdump.1.in
@@ -39,6 +39,9 @@ tcpdump \- dump traffic on a network
.I count
]
[
+.B \-\-count
+]
+[
.B \-C
.I file_size
]
@@ -242,6 +245,12 @@ units of KiB (1024 bytes).
.BI \-c " count"
Exit after receiving \fIcount\fP packets.
.TP
+.BI \-\-count
+Print only on stderr the packet count when reading capture file(s) instead
+of parsing/printing the packets. If a filter is specified on the command
+line, \fItcpdump\fP counts only packets that were matched by the filter
+expression.
+.TP
.BI \-C " file_size"
Before writing a raw packet to a savefile, check whether the file is
currently larger than \fIfile_size\fP and, if so, close the current
diff --git a/tcpdump.c b/tcpdump.c
index cd277042..2f126391 100644
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -220,6 +220,7 @@ static int timeout = 1000; /* default timeout = 1000 ms = 1 s */
#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
static int immediate_mode;
#endif
+static int count_mode;
static int infodelay;
static int infoprint;
@@ -690,6 +691,7 @@ show_remote_devices_and_exit(void)
#define OPTION_TSTAMP_MICRO 133
#define OPTION_TSTAMP_NANO 134
#define OPTION_FP_TYPE 135
+#define OPTION_COUNT 136
static const struct option longopts[] = {
#if defined(HAVE_PCAP_CREATE) || defined(_WIN32)
@@ -733,6 +735,7 @@ static const struct option longopts[] = {
{ "debug-filter-parser", no_argument, NULL, 'Y' },
#endif
{ "relinquish-privileges", required_argument, NULL, 'Z' },
+ { "count", no_argument, NULL, OPTION_COUNT },
{ "fp-type", no_argument, NULL, OPTION_FP_TYPE },
{ "number", no_argument, NULL, '#' },
{ "print", no_argument, NULL, OPTION_PRINT },
@@ -1908,6 +1911,10 @@ main(int argc, char **argv)
float_type_check(0x4e93312d);
return 0;
+ case OPTION_COUNT:
+ count_mode = 1;
+ break;
+
default:
print_usage();
exit_tcpdump(S_ERR_HOST_PROGRAM);
@@ -2595,6 +2602,10 @@ DIAG_ON_CLANG(assign-enum)
}
while (ret != NULL);
+ if (count_mode && RFileName != NULL)
+ fprintf(stderr, "%u packet%s\n", packets_captured,
+ PLURAL_SUFFIX(packets_captured));
+
free(cmdbuf);
pcap_freecode(&fcode);
exit_tcpdump(status == -1 ? 1 : 0);
@@ -3027,7 +3038,8 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
++infodelay;
- pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
+ if (!count_mode)
+ pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
--infodelay;
if (infoprint)
@@ -3125,7 +3137,7 @@ print_usage(void)
{
print_version();
(void)fprintf(stderr,
-"Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ]\n", program_name);
+"Usage: %s [-aAbd" D_FLAG "efhH" I_FLAG J_FLAG "KlLnNOpqStu" U_FLAG "vxX#]" B_FLAG_USAGE " [ -c count ] [--count]\n", program_name);
(void)fprintf(stderr,
"\t\t[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]\n");
(void)fprintf(stderr,