From 85c73ddaca2654c3803780ccd4437150d7e5f9e9 Mon Sep 17 00:00:00 2001 From: Guillaume Valadon Date: Thu, 15 Mar 2018 16:59:24 +0100 Subject: [PATCH] Write a PCAP header when no packets are provided --- scapy/utils.py | 5 +++-- test/regression.uts | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/scapy/utils.py b/scapy/utils.py index d51c3bb44..8fc15cda1 100644 --- a/scapy/utils.py +++ b/scapy/utils.py @@ -24,7 +24,7 @@ warnings.filterwarnings("ignore","tempnam",RuntimeWarning, __name__) from scapy.config import conf from scapy.consts import DARWIN, WINDOWS -from scapy.data import MTU +from scapy.data import MTU, DLT_EN10MB from scapy.compat import * from scapy.error import log_runtime, log_loading, log_interactive, Scapy_Exception, warning from scapy.base_classes import BasePacketList @@ -1155,6 +1155,7 @@ nano: use nanosecond-precision (requires libpcap >= 1.5.0) try: p = next(pkt) except StopIteration: + self._write_header(None) return self._write_header(p) self._write_packet(p) @@ -1208,7 +1209,7 @@ class PcapWriter(RawPcapWriter): self.linktype = conf.l2types[pkt.__class__] except KeyError: warning("PcapWriter: unknown LL type for %s. Using type 1 (Ethernet)", pkt.__class__.__name__) - self.linktype = 1 + self.linktype = DLT_EN10MB RawPcapWriter._write_header(self, pkt) def _write_packet(self, packet): diff --git a/test/regression.uts b/test/regression.uts index 713d351e8..670b6fac2 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -5880,6 +5880,15 @@ wrpcap(filename, [IP()/UDP(), IPv6()/UDP()], linktype=DLT_RAW) packets = rdpcap(filename) assert(isinstance(packets[0], IP) and isinstance(packets[1], IPv6)) += Check wrpcap() with no packet + +import tempfile +filename = tempfile.mktemp(suffix=".pcap") +wrpcap(filename, []) +fstat = os.stat(filename) +assert fstat.st_size != 0 +os.remove(filename) + ############ ############ + LLMNR protocol