Skip to content

Commit ad116e9

Browse files
author
Eric Marchand
committed
Report the interface ID used to capture pcapng files
* On pcapng files, you can have several capture interfaces. We report this information to be able to use it on another classes
1 parent 0648c0d commit ad116e9

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

scapy/packet.py

+8
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class Packet(
101101
"direction", "sniffed_on",
102102
# handle snaplen Vs real length
103103
"wirelen",
104+
"intid",
104105
"comment",
105106
"process_information"
106107
]
@@ -177,6 +178,7 @@ def __init__(self,
177178
self.raw_packet_cache = None # type: Optional[bytes]
178179
self.raw_packet_cache_fields = None # type: Optional[Dict[str, Any]] # noqa: E501
179180
self.wirelen = None # type: Optional[int]
181+
self.intid = None # type: Optional[int]
180182
self.direction = None # type: Optional[int]
181183
self.sniffed_on = None # type: Optional[_GlobInterfaceType]
182184
self.comment = None # type: Optional[bytes]
@@ -232,6 +234,7 @@ def __reduce__(self):
232234
self.direction,
233235
self.sniffed_on,
234236
self.wirelen,
237+
self.intid,
235238
self.comment
236239
))
237240

@@ -431,6 +434,7 @@ def copy(self) -> Self:
431434
self.raw_packet_cache_fields
432435
)
433436
clone.wirelen = self.wirelen
437+
clone.intid = self.intid
434438
clone.post_transforms = self.post_transforms[:]
435439
clone.payload = self.payload.copy()
436440
clone.payload.add_underlayer(clone)
@@ -500,6 +504,7 @@ def setfieldval(self, attr, val):
500504
self.raw_packet_cache = None
501505
self.raw_packet_cache_fields = None
502506
self.wirelen = None
507+
self.intid = None
503508
elif attr == "payload":
504509
self.remove_payload()
505510
self.add_payload(val)
@@ -524,6 +529,7 @@ def delfieldval(self, attr):
524529
self.raw_packet_cache = None
525530
self.raw_packet_cache_fields = None
526531
self.wirelen = None
532+
self.intid = None
527533
elif attr in self.default_fields:
528534
pass
529535
elif attr == "payload":
@@ -703,6 +709,7 @@ def self_build(self):
703709
self.raw_packet_cache = None
704710
self.raw_packet_cache_fields = None
705711
self.wirelen = None
712+
self.intid = None
706713
break
707714
if self.raw_packet_cache is not None:
708715
return self.raw_packet_cache
@@ -1145,6 +1152,7 @@ def clone_with(self, payload=None, **kargs):
11451152
self.raw_packet_cache_fields
11461153
)
11471154
pkt.wirelen = self.wirelen
1155+
pkt.intid = self.intid
11481156
pkt.comment = self.comment
11491157
pkt.sniffed_on = self.sniffed_on
11501158
pkt.direction = self.direction

scapy/utils.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,7 @@ class RawPcapNgReader(RawPcapReader):
16471647
PacketMetadata = collections.namedtuple("PacketMetadataNg", # type: ignore
16481648
["linktype", "tsresol",
16491649
"tshigh", "tslow", "wirelen",
1650-
"comment", "ifname", "direction",
1650+
"comment", "ifname", "intid", "direction",
16511651
"process_information"])
16521652

16531653
def __init__(self, filename, fdesc=None, magic=None): # type: ignore
@@ -1656,6 +1656,7 @@ def __init__(self, filename, fdesc=None, magic=None): # type: ignore
16561656
self.f = fdesc
16571657
# A list of (linktype, snaplen, tsresol); will be populated by IDBs.
16581658
self.interfaces = [] # type: List[Tuple[int, int, Dict[str, Any]]]
1659+
self.interface_names = []
16591660
self.default_options = {
16601661
"tsresol": 1000000
16611662
}
@@ -1781,7 +1782,7 @@ def _read_block_shb(self):
17811782
def _read_packet(self, size=MTU): # type: ignore
17821783
# type: (int) -> Tuple[bytes, RawPcapNgReader.PacketMetadata]
17831784
"""Read blocks until it reaches either EOF or a packet, and
1784-
returns None or (packet, (linktype, sec, usec, wirelen)),
1785+
returns None or (packet, (linktype, sec, usec, wirelen, intid)),
17851786
where packet is a string.
17861787
17871788
"""
@@ -1860,6 +1861,7 @@ def _read_block_epb(self, block, size):
18601861
self.endian + "5I",
18611862
block[:20],
18621863
)
1864+
ifname = self.interface_names[intid] if intid < len(self.interface_names) else None
18631865
except struct.error:
18641866
warning("PcapNg: EPB is too small %d/20 !" % len(block))
18651867
raise EOFError
@@ -1914,6 +1916,7 @@ def _read_block_epb(self, block, size):
19141916
wirelen=wirelen,
19151917
comment=comment,
19161918
ifname=ifname,
1919+
intid=intid,
19171920
direction=direction,
19181921
process_information=process_information))
19191922

@@ -1952,6 +1955,7 @@ def _read_block_pkt(self, block, size):
19521955
self.endian + "HH4I",
19531956
block[:20],
19541957
)
1958+
ifname = self.interface_names[intid] if intid < len(self.interface_names) else None
19551959
except struct.error:
19561960
warning("PcapNg: PKT is too small %d/20 !" % len(block))
19571961
raise EOFError
@@ -2067,7 +2071,7 @@ def read_packet(self, size=MTU, **kwargs):
20672071
rp = super(PcapNgReader, self)._read_packet(size=size)
20682072
if rp is None:
20692073
raise EOFError
2070-
s, (linktype, tsresol, tshigh, tslow, wirelen, comment, ifname, direction, process_information) = rp # noqa: E501
2074+
s, (linktype, tsresol, tshigh, tslow, wirelen, comment, ifname, intid, direction, process_information) = rp # noqa: E501
20712075
try:
20722076
cls = conf.l2types.num2layer[linktype] # type: Type[Packet]
20732077
p = cls(s, **kwargs) # type: Packet
@@ -2088,6 +2092,7 @@ def read_packet(self, size=MTU, **kwargs):
20882092
p.process_information = process_information.copy()
20892093
if ifname is not None:
20902094
p.sniffed_on = ifname.decode('utf-8', 'backslashreplace')
2095+
p.intid = intid
20912096
return p
20922097

20932098
def recv(self, size: int = MTU, **kwargs: Any) -> 'Packet': # type: ignore

0 commit comments

Comments
 (0)