Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions scapy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2211,9 +2211,12 @@ def write_packet(self,
ifname = getattr(packet, "sniffed_on", None)
direction = getattr(packet, "direction", None)
if not isinstance(packet, bytes):
linktype: int = conf.l2types.layer2num[
packet.__class__
]
# The class may not be bound to any linktype (e.g. conf.raw_layer),
# in which case fall back to the one write_header() settled on.
linktype: int = conf.l2types.layer2num.get(
packet.__class__,
self.linktype,
)
else:
linktype = self.linktype
if ifname is not None:
Expand Down
15 changes: 15 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,21 @@ with RawPcapWriter(fd) as w:
w.write(b"test")
assert w.linktype == 1

= Check PcapWriter with a layer that is not bound to any linktype
~ pcap

from io import BytesIO

f = BytesIO()
w = PcapWriter(f, linktype=DLT_RAW, sync=True)
w.write(Raw(b"test"))
assert w.linktype == DLT_RAW

f.seek(0) or None
r = RawPcapReader(f)
assert r.linktype == DLT_RAW
assert [x for (x, y) in r] == [b"test"]

= Check tcpdump()
~ tcpdump
from io import BytesIO
Expand Down
Loading