@@ -1647,7 +1647,7 @@ class RawPcapNgReader(RawPcapReader):
1647
1647
PacketMetadata = collections .namedtuple ("PacketMetadataNg" , # type: ignore
1648
1648
["linktype" , "tsresol" ,
1649
1649
"tshigh" , "tslow" , "wirelen" ,
1650
- "comment" , "ifname" , "direction" ,
1650
+ "comment" ,"ifname" , "intid " , "direction" ,
1651
1651
"process_information" ])
1652
1652
1653
1653
def __init__ (self , filename , fdesc = None , magic = None ): # type: ignore
@@ -1656,6 +1656,7 @@ def __init__(self, filename, fdesc=None, magic=None): # type: ignore
1656
1656
self .f = fdesc
1657
1657
# A list of (linktype, snaplen, tsresol); will be populated by IDBs.
1658
1658
self .interfaces = [] # type: List[Tuple[int, int, Dict[str, Any]]]
1659
+ self .interface_names = []
1659
1660
self .default_options = {
1660
1661
"tsresol" : 1000000
1661
1662
}
@@ -1781,7 +1782,7 @@ def _read_block_shb(self):
1781
1782
def _read_packet (self , size = MTU ): # type: ignore
1782
1783
# type: (int) -> Tuple[bytes, RawPcapNgReader.PacketMetadata]
1783
1784
"""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 )),
1785
1786
where packet is a string.
1786
1787
1787
1788
"""
@@ -1800,7 +1801,7 @@ def _read_options(self, options):
1800
1801
warning ("PcapNg: options header is too small "
1801
1802
"%d !" % len (options ))
1802
1803
raise EOFError
1803
- if code != 0 and 4 + length <= len (options ):
1804
+ if code != 0 and 4 + length < len (options ):
1804
1805
opts [code ] = options [4 :4 + length ]
1805
1806
if code == 0 :
1806
1807
if length != 0 :
@@ -1860,6 +1861,7 @@ def _read_block_epb(self, block, size):
1860
1861
self .endian + "5I" ,
1861
1862
block [:20 ],
1862
1863
)
1864
+ ifname = self .interface_names [intid ] if intid < len (self .interface_names ) else None
1863
1865
except struct .error :
1864
1866
warning ("PcapNg: EPB is too small %d/20 !" % len (block ))
1865
1867
raise EOFError
@@ -1914,6 +1916,7 @@ def _read_block_epb(self, block, size):
1914
1916
wirelen = wirelen ,
1915
1917
comment = comment ,
1916
1918
ifname = ifname ,
1919
+ intid = intid ,
1917
1920
direction = direction ,
1918
1921
process_information = process_information ))
1919
1922
@@ -1952,6 +1955,7 @@ def _read_block_pkt(self, block, size):
1952
1955
self .endian + "HH4I" ,
1953
1956
block [:20 ],
1954
1957
)
1958
+ ifname = self .interface_names [intid ] if intid < len (self .interface_names ) else None
1955
1959
except struct .error :
1956
1960
warning ("PcapNg: PKT is too small %d/20 !" % len (block ))
1957
1961
raise EOFError
@@ -2067,7 +2071,7 @@ def read_packet(self, size=MTU, **kwargs):
2067
2071
rp = super (PcapNgReader , self )._read_packet (size = size )
2068
2072
if rp is None :
2069
2073
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
2071
2075
try :
2072
2076
cls = conf .l2types .num2layer [linktype ] # type: Type[Packet]
2073
2077
p = cls (s , ** kwargs ) # type: Packet
@@ -2088,6 +2092,7 @@ def read_packet(self, size=MTU, **kwargs):
2088
2092
p .process_information = process_information .copy ()
2089
2093
if ifname is not None :
2090
2094
p .sniffed_on = ifname .decode ('utf-8' , 'backslashreplace' )
2095
+ p .intid = intid
2091
2096
return p
2092
2097
2093
2098
def recv (self , size : int = MTU , ** kwargs : Any ) -> 'Packet' : # type: ignore
@@ -2210,7 +2215,7 @@ def write_packet(self,
2210
2215
comment = comment ,
2211
2216
ifname = ifname ,
2212
2217
direction = direction ,
2213
- linktype = linktype
2218
+ linktype = linktype ,
2214
2219
)
2215
2220
2216
2221
@@ -2640,6 +2645,7 @@ def _write_packet(self, # type: ignore
2640
2645
if wirelen is None :
2641
2646
wirelen = caplen
2642
2647
2648
+
2643
2649
ifid = self .interfaces2id .get (ifname , None )
2644
2650
if ifid is None :
2645
2651
ifid = max (self .interfaces2id .values ()) + 1
0 commit comments