13
13
from typing import TYPE_CHECKING , cast
14
14
15
15
from pcapkit .const .reg .linktype import LinkType as Enum_LinkType
16
- from pcapkit .foundation .engines import engine
17
16
from pcapkit .foundation .engines .engine import Engine
18
17
from pcapkit .utilities .exceptions import FormatError , stacklevel
19
18
from pcapkit .utilities .warnings import AttributeWarning , DPKTWarning , warn
@@ -90,6 +89,7 @@ def run(self) -> 'None':
90
89
91
90
"""
92
91
from pcapkit .foundation .engines .pcap import PCAP
92
+ from pcapkit .foundation .engines .pcapng import PCAPNG
93
93
94
94
ext = self ._extractor
95
95
dpkt = self ._expkg
@@ -106,12 +106,13 @@ def run(self) -> 'None':
106
106
f'Frame { e ._frnum :>3d} : { packet2chain (f )} ' # pylint: disable=protected-access
107
107
) # pylint: disable=logging-fstring-interpolation
108
108
109
- # extract global header
110
- engine = ext .record_header ()
111
- if not isinstance (engine , PCAP ):
112
- raise FormatError (f'unsupported file format: { engine .name } ' )
113
- self ._gbhdr = engine .header
114
- self ._dlink = self ._gbhdr .protocol
109
+ if ext .magic_number in PCAP .MAGIC_NUMBER :
110
+ reader = dpkt .pcap .Reader (ext ._ifile )
111
+ elif ext .magic_number in PCAPNG .MAGIC_NUMBER :
112
+ reader = dpkt .pcapng .Reader (ext ._ifile )
113
+ else :
114
+ raise FormatError (f'unsupported file format: { ext .magic_number !r} ' )
115
+ self ._dlink = Enum_LinkType .get (reader .datalink ())
115
116
116
117
if self ._dlink == Enum_LinkType .ETHERNET :
117
118
pkg = dpkt .ethernet .Ethernet
@@ -139,7 +140,7 @@ def unpack(ext, buf: 'bytes') -> 'None':
139
140
140
141
# extract & analyse file
141
142
self ._expkg = pkg
142
- self ._extmp = iter ( dpkt . pcap . Reader ( ext . _ifile ))
143
+ self ._extmp = reader
143
144
144
145
def read_frame (self ) -> 'DPKTPacket' :
145
146
"""Read frames with DPKT engine.
@@ -172,6 +173,8 @@ def read_frame(self) -> 'DPKTPacket':
172
173
ofile (info , name = frnum )
173
174
else :
174
175
ext ._ofile (info , name = frnum )
176
+ ofile = ext ._ofile
177
+ ext ._offmt = ofile .kind
175
178
176
179
# record fragments
177
180
if ext ._flag_r :
0 commit comments