Skip to content

Commit

Permalink
Fix --time-stamp-precision when used with -V
Browse files Browse the repository at this point in the history
Capture files were opened in two different bits of code. Only the
first bit of code acquired support for --time-stamp-precision, so
the 2nd and subsequent files opened with -V would always have the
default timestamp precision. This fix unifies file opening into a
single routine, ensuring consistent behavior for each opened file.
  • Loading branch information
jtippet committed Aug 28, 2023
1 parent e570e6b commit 87501b7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 57 deletions.
110 changes: 54 additions & 56 deletions tcpdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,56 @@ get_next_file(FILE *VFile, char *ptr)
return ret;
}

static int
open_pcap_file(const char *path, const netdissect_options *ndo)
{
int dlt;
const char *dlt_name;
char ebuf[PCAP_ERRBUF_SIZE];
#ifdef DLT_LINUX_SLL2
static int sll_warning_printed = 0;
#endif /* DLT_LINUX_SLL2 */
#ifdef HAVE_CAPSICUM
cap_rights_t rights;
#endif /* HAVE_CAPSICUM */

#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
pd = pcap_open_offline_with_tstamp_precision(path,
ndo->ndo_tstamp_precision, ebuf);
#else
pd = pcap_open_offline(path, ebuf);
#endif

if (pd == NULL)
error("%s", ebuf);
#ifdef HAVE_CAPSICUM
cap_rights_init(&rights, CAP_READ);
if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 &&
errno != ENOSYS) {
error("unable to limit pcap descriptor");
}
#endif
dlt = pcap_datalink(pd);
dlt_name = pcap_datalink_val_to_name(dlt);
fprintf(stderr, "reading from file %s", path);
if (dlt_name == NULL) {
fprintf(stderr, ", link-type %u", dlt);
} else {
fprintf(stderr, ", link-type %s (%s)", dlt_name,
pcap_datalink_val_to_description(dlt));
}
fprintf(stderr, ", snapshot length %d\n", pcap_snapshot(pd));
#ifdef DLT_LINUX_SLL2
if (!sll_warning_printed && dlt == DLT_LINUX_SLL2)
{
fprintf(stderr, "Warning: interface names might be incorrect\n");
sll_warning_printed = 1;
}
#endif

return dlt;
}

#ifdef HAVE_CASPER
static cap_channel_t *
capdns_setup(void)
Expand Down Expand Up @@ -1476,7 +1526,6 @@ main(int argc, char **argv)
char *endp;
pcap_handler callback;
int dlt;
const char *dlt_name;
struct bpf_program fcode;
#ifndef _WIN32
void (*oldhandler)(int);
Expand Down Expand Up @@ -2121,36 +2170,7 @@ main(int argc, char **argv)
RFileName = VFileLine;
}

#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
pd = pcap_open_offline_with_tstamp_precision(RFileName,
ndo->ndo_tstamp_precision, ebuf);
#else
pd = pcap_open_offline(RFileName, ebuf);
#endif

if (pd == NULL)
error("%s", ebuf);
#ifdef HAVE_CAPSICUM
cap_rights_init(&rights, CAP_READ);
if (cap_rights_limit(fileno(pcap_file(pd)), &rights) < 0 &&
errno != ENOSYS) {
error("unable to limit pcap descriptor");
}
#endif
dlt = pcap_datalink(pd);
dlt_name = pcap_datalink_val_to_name(dlt);
fprintf(stderr, "reading from file %s", RFileName);
if (dlt_name == NULL) {
fprintf(stderr, ", link-type %u", dlt);
} else {
fprintf(stderr, ", link-type %s (%s)", dlt_name,
pcap_datalink_val_to_description(dlt));
}
fprintf(stderr, ", snapshot length %d\n", pcap_snapshot(pd));
#ifdef DLT_LINUX_SLL2
if (dlt == DLT_LINUX_SLL2)
fprintf(stderr, "Warning: interface names might be incorrect\n");
#endif
dlt = open_pcap_file(RFileName, ndo);
} else if (dflag && !device) {
int dump_dlt = DLT_EN10MB;
/*
Expand Down Expand Up @@ -2604,6 +2624,8 @@ DIAG_ON_ASSIGN_ENUM
* to a file from the -V file). Print a message to
* the standard error on UN*X.
*/
const char *dlt_name;

if (!ndo->ndo_vflag && !WFileName) {
(void)fprintf(stderr,
"%s: verbose output suppressed, use -v[v]... for full protocol decode\n",
Expand Down Expand Up @@ -2683,17 +2705,7 @@ DIAG_ON_ASSIGN_ENUM
int new_dlt;

RFileName = VFileLine;
pd = pcap_open_offline(RFileName, ebuf);
if (pd == NULL)
error("%s", ebuf);
#ifdef HAVE_CAPSICUM
cap_rights_init(&rights, CAP_READ);
if (cap_rights_limit(fileno(pcap_file(pd)),
&rights) < 0 && errno != ENOSYS) {
error("unable to limit pcap descriptor");
}
#endif
new_dlt = pcap_datalink(pd);
new_dlt = open_pcap_file(RFileName, ndo);
if (new_dlt != dlt) {
/*
* The new file has a different
Expand Down Expand Up @@ -2735,20 +2747,6 @@ DIAG_ON_ASSIGN_ENUM
*/
if (pcap_setfilter(pd, &fcode) < 0)
error("%s", pcap_geterr(pd));

/*
* Report the new file.
*/
dlt_name = pcap_datalink_val_to_name(dlt);
fprintf(stderr, "reading from file %s", RFileName);
if (dlt_name == NULL) {
fprintf(stderr, ", link-type %u", dlt);
} else {
fprintf(stderr, ", link-type %s (%s)",
dlt_name,
pcap_datalink_val_to_description(dlt));
}
fprintf(stderr, ", snapshot length %d\n", pcap_snapshot(pd));
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/TESTLIST
Original file line number Diff line number Diff line change
Expand Up @@ -933,3 +933,8 @@ ip6-snmp-oid-unsigned ip6-snmp-oid-unsigned.pcap ip6-snmp-oid-unsigned.out
lwres-pointer-arithmetic-ub lwres-pointer-arithmetic-ub.pcap lwres-pointer-arithmetic-ub.out
ospf-signed-integer-ubsan ospf-signed-integer-ubsan.pcap ospf-signed-integer-ubsan.out -vv
bgp-ub bgp-ub.pcap bgp-ub.out -v

# Multi-file tests
# Note the input is not a pcap file, but a file that contains a list of pcap files
multifile-timestamp multifile-timestamp.pcap-list multifile-timestamp.out -V --nano

9 changes: 8 additions & 1 deletion tests/TESTrun
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,20 @@ sub runtest {
my $stderrlog = "tests/NEW/${outputbase}.stderr";
my $diffstat = 0;
my $errdiffstat = 0;
my $inputswitch = '-r';

if ($options =~ s/(^|\s+)-V\b//) {
# If the options contains '-V', remove it from there and place it
# before the input file name.
$inputswitch = '-V';
}

# we used to do this as a nice pipeline, but the problem is that $r fails to
# to be set properly if the tcpdump core dumps.
#
# Furthermore, on Windows, fc can't read the standard input, so we
# can't do it as a pipeline in any case.
$r = system "$TCPDUMP -# -n -r $input $options >tests/NEW/${outputbase} 2>${rawstderrlog}";
$r = system "$TCPDUMP -# -n $inputswitch $input $options >tests/NEW/${outputbase} 2>${rawstderrlog}";
if($r != 0) {
#
# Something other than "tcpdump opened the file, read it, and
Expand Down
2 changes: 2 additions & 0 deletions tests/multifile-timestamp.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 06:15:22.659099 1.0 Mb/s 2412 MHz 11b 100 sq antenna 0 57dB signal DeAuthentication (00:0d:93:82:36:3a): Reserved
2 06:15:22.659099 1.0 Mb/s 2412 MHz 11b 100 sq antenna 0 57dB signal DeAuthentication (00:0d:93:82:36:3a): Reserved
2 changes: 2 additions & 0 deletions tests/multifile-timestamp.pcap-list
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests/reason_code-0.pcap
tests/reason_code-0.pcap

0 comments on commit 87501b7

Please sign in to comment.