Skip to content

Commit 85c73dd

Browse files
committed
Write a PCAP header when no packets are provided
1 parent 2c5bf91 commit 85c73dd

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

scapy/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from scapy.config import conf
2626
from scapy.consts import DARWIN, WINDOWS
27-
from scapy.data import MTU
27+
from scapy.data import MTU, DLT_EN10MB
2828
from scapy.compat import *
2929
from scapy.error import log_runtime, log_loading, log_interactive, Scapy_Exception, warning
3030
from scapy.base_classes import BasePacketList
@@ -1155,6 +1155,7 @@ def write(self, pkt):
11551155
try:
11561156
p = next(pkt)
11571157
except StopIteration:
1158+
self._write_header(None)
11581159
return
11591160
self._write_header(p)
11601161
self._write_packet(p)
@@ -1208,7 +1209,7 @@ def _write_header(self, pkt):
12081209
self.linktype = conf.l2types[pkt.__class__]
12091210
except KeyError:
12101211
warning("PcapWriter: unknown LL type for %s. Using type 1 (Ethernet)", pkt.__class__.__name__)
1211-
self.linktype = 1
1212+
self.linktype = DLT_EN10MB
12121213
RawPcapWriter._write_header(self, pkt)
12131214

12141215
def _write_packet(self, packet):

test/regression.uts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5880,6 +5880,15 @@ wrpcap(filename, [IP()/UDP(), IPv6()/UDP()], linktype=DLT_RAW)
58805880
packets = rdpcap(filename)
58815881
assert(isinstance(packets[0], IP) and isinstance(packets[1], IPv6))
58825882

5883+
= Check wrpcap() with no packet
5884+
5885+
import tempfile
5886+
filename = tempfile.mktemp(suffix=".pcap")
5887+
wrpcap(filename, [])
5888+
fstat = os.stat(filename)
5889+
assert fstat.st_size != 0
5890+
os.remove(filename)
5891+
58835892
############
58845893
############
58855894
+ LLMNR protocol

0 commit comments

Comments
 (0)