Skip to content

Commit 46bf315

Browse files
authored
Merge pull request #1253 from guedou/Issue_#1242
Write a PCAP header when no packets are provided
2 parents 94d5521 + 85c73dd commit 46bf315

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
@@ -1154,6 +1154,7 @@ def write(self, pkt):
11541154
try:
11551155
p = next(pkt)
11561156
except StopIteration:
1157+
self._write_header(None)
11571158
return
11581159
self._write_header(p)
11591160
self._write_packet(p)
@@ -1207,7 +1208,7 @@ def _write_header(self, pkt):
12071208
self.linktype = conf.l2types[pkt.__class__]
12081209
except KeyError:
12091210
warning("PcapWriter: unknown LL type for %s. Using type 1 (Ethernet)", pkt.__class__.__name__)
1210-
self.linktype = 1
1211+
self.linktype = DLT_EN10MB
12111212
RawPcapWriter._write_header(self, pkt)
12121213

12131214
def _write_packet(self, packet):

test/regression.uts

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

5920+
= Check wrpcap() with no packet
5921+
5922+
import tempfile
5923+
filename = tempfile.mktemp(suffix=".pcap")
5924+
wrpcap(filename, [])
5925+
fstat = os.stat(filename)
5926+
assert fstat.st_size != 0
5927+
os.remove(filename)
5928+
59205929
############
59215930
############
59225931
+ LLMNR protocol

0 commit comments

Comments
 (0)