Skip to content

Commit e1cc6f5

Browse files
committed
Add support for 'comment' parameter in GenericPcapWriter and RawPcapWriter classes for back compatibility
1 parent 5f55c44 commit e1cc6f5

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

scapy/utils.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,10 +1648,10 @@ class RawPcapNgReader(RawPcapReader):
16481648
PacketMetadata = collections.namedtuple("PacketMetadataNg", # type: ignore
16491649
["linktype", "tsresol",
16501650
"tshigh", "tslow", "wirelen",
1651-
"comments", "ifname", "direction",
1652-
"process_information"])
1651+
"comment", "ifname", "direction",
1652+
"process_information", "comments", ])
16531653

1654-
def __init__(self, filename, fdesc=None, magic=None): # type: ignore
1654+
def __init__(self, filename, fdesc=None, magic=None, comments=None): # type: ignore
16551655
# type: (str, IO[bytes], bytes) -> None
16561656
self.filename = filename
16571657
self.f = fdesc
@@ -1675,6 +1675,7 @@ def __init__(self, filename, fdesc=None, magic=None): # type: ignore
16751675
}
16761676
self.endian = "!" # Will be overwritten by first SHB
16771677
self.process_information = [] # type: List[Dict[str, Any]]
1678+
self.comments = comments
16781679

16791680
if magic != b"\x0a\x0d\x0d\x0a": # PcapNg:
16801681
raise Scapy_Exception(
@@ -1802,9 +1803,12 @@ def _read_options(self, options):
18021803
"%d !" % len(options))
18031804
raise EOFError
18041805
if code != 0 and 4 + length <= len(options):
1805-
if code not in opts:
1806-
opts[code] = []
1807-
opts[code].append(options[4:4 + length])
1806+
if code in [1, 2988, 2989, 19372, 19373]:
1807+
if code not in opts:
1808+
opts[code] = []
1809+
opts[code].append(options[4:4 + length])
1810+
else:
1811+
opts[code] = options[4:4 + length]
18081812
if code == 0:
18091813
if length != 0:
18101814
warning("PcapNg: invalid option "
@@ -1893,6 +1897,7 @@ def _read_block_epb(self, block, size):
18931897
"(%d/%d) !" % (proc_index, len(self.process_information)))
18941898

18951899
comments = options.get(1, None)
1900+
comment = comments[-1] if comments is not None and len(comments) > 0 else None
18961901
epb_flags_raw = options.get(2, None)
18971902
if epb_flags_raw:
18981903
try:
@@ -1915,10 +1920,11 @@ def _read_block_epb(self, block, size):
19151920
tshigh=tshigh,
19161921
tslow=tslow,
19171922
wirelen=wirelen,
1918-
comments=comments,
1923+
comment=comment,
19191924
ifname=ifname,
19201925
direction=direction,
1921-
process_information=process_information))
1926+
process_information=process_information,
1927+
comments=comments))
19221928

19231929
def _read_block_spb(self, block, size):
19241930
# type: (bytes, int) -> Tuple[bytes, RawPcapNgReader.PacketMetadata]
@@ -1945,7 +1951,8 @@ def _read_block_spb(self, block, size):
19451951
comment=None,
19461952
ifname=None,
19471953
direction=None,
1948-
process_information={}))
1954+
process_information={},
1955+
comments=None))
19491956

19501957
def _read_block_pkt(self, block, size):
19511958
# type: (bytes, int) -> Tuple[bytes, RawPcapNgReader.PacketMetadata]
@@ -1969,7 +1976,8 @@ def _read_block_pkt(self, block, size):
19691976
comment=None,
19701977
ifname=None,
19711978
direction=None,
1972-
process_information={}))
1979+
process_information={},
1980+
comments=None))
19731981

19741982
def _read_block_dsb(self, block, size):
19751983
# type: (bytes, int) -> None
@@ -2070,7 +2078,7 @@ def read_packet(self, size=MTU, **kwargs):
20702078
rp = super(PcapNgReader, self)._read_packet(size=size)
20712079
if rp is None:
20722080
raise EOFError
2073-
s, (linktype, tsresol, tshigh, tslow, wirelen, comments, ifname, direction, process_information) = rp # noqa: E501
2081+
s, (linktype, tsresol, tshigh, tslow, wirelen, comment, ifname, direction, process_information, comments) = rp # noqa: E501
20742082
try:
20752083
cls = conf.l2types.num2layer[linktype] # type: Type[Packet]
20762084
p = cls(s, **kwargs) # type: Packet
@@ -2086,7 +2094,11 @@ def read_packet(self, size=MTU, **kwargs):
20862094
if tshigh is not None:
20872095
p.time = EDecimal((tshigh << 32) + tslow) / tsresol
20882096
p.wirelen = wirelen
2089-
p.comments = comments
2097+
p.comment = comment
2098+
if comments is not None:
2099+
comments.remove(comment)
2100+
if len(comments) > 0:
2101+
p.comments = comments
20902102
p.direction = direction
20912103
p.process_information = process_information.copy()
20922104
if ifname is not None:
@@ -2112,6 +2124,7 @@ def _write_packet(self,
21122124
usec=None, # type: Optional[int]
21132125
caplen=None, # type: Optional[int]
21142126
wirelen=None, # type: Optional[int]
2127+
comment=None, # type: Optional[bytes]
21152128
ifname=None, # type: Optional[bytes]
21162129
direction=None, # type: Optional[int]
21172130
comments=None, # type: Optional[List[bytes]]
@@ -2195,6 +2208,7 @@ def write_packet(self,
21952208
if wirelen is None:
21962209
wirelen = caplen
21972210

2211+
comment = getattr(packet, "comment", None)
21982212
comments = getattr(packet, "comments", None)
21992213
ifname = getattr(packet, "sniffed_on", None)
22002214
direction = getattr(packet, "direction", None)
@@ -2210,10 +2224,11 @@ def write_packet(self,
22102224
rawpkt,
22112225
sec=f_sec, usec=usec,
22122226
caplen=caplen, wirelen=wirelen,
2227+
comment=comment,
22132228
ifname=ifname,
22142229
direction=direction,
22152230
linktype=linktype,
2216-
comments=comments
2231+
comments=comments,
22172232
)
22182233

22192234

@@ -2368,6 +2383,7 @@ def _write_packet(self,
23682383
comment=None, # type: Optional[bytes]
23692384
ifname=None, # type: Optional[bytes]
23702385
direction=None, # type: Optional[int]
2386+
comments=None, # type: Optional[List[bytes]
23712387
):
23722388
# type: (...) -> None
23732389
"""
@@ -2600,9 +2616,10 @@ def _write_packet(self, # type: ignore
26002616
usec=None, # type: Optional[int]
26012617
caplen=None, # type: Optional[int]
26022618
wirelen=None, # type: Optional[int]
2603-
comments=None, # type: Optional[List[bytes]]
2619+
comment=None, # type: Optional[bytes]
26042620
ifname=None, # type: Optional[bytes]
26052621
direction=None, # type: Optional[int]
2622+
comments=None, # type: Optional[List[bytes]]
26062623
):
26072624
# type: (...) -> None
26082625
"""

0 commit comments

Comments
 (0)