Skip to content

Commit ecbb99d

Browse files
committed
revised Extractor
* added PCAPNG engine support * bugfix in engine module mapping * added magic_number property
1 parent 429f1a0 commit ecbb99d

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

pcapkit/foundation/extraction.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from pcapkit.dumpkit.common import make_dumper
2424
from pcapkit.foundation.engines.pcap import PCAP as PCAP_Engine
25+
from pcapkit.foundation.engines.pcapng import PCAPNG as PCAPNG_Engine
2526
from pcapkit.foundation.reassembly import ReassemblyManager
2627
from pcapkit.foundation.traceflow import TraceFlowManager
2728
from pcapkit.utilities.exceptions import (CallableError, FileNotFound, FormatError, IterableError,
@@ -153,9 +154,9 @@ class Extractor(Generic[P]):
153154
#: dict[str, tuple[str, str]]: Engine mapping for extracting frames.
154155
#: The values should be a tuple representing the module name and class name.
155156
__engine__ = {
156-
'scapy': ('pcapkit.foundation.engine.scapy', 'Scapy'),
157-
'dpkt': ('pcapkit.foundation.engine.dpkt', 'DPKT'),
158-
'pyshark': ('pcapkit.foundation.engine.pyshark', 'PyShark'),
157+
'scapy': ('pcapkit.foundation.engines.scapy', 'Scapy'),
158+
'dpkt': ('pcapkit.foundation.engines.dpkt', 'DPKT'),
159+
'pyshark': ('pcapkit.foundation.engines.pyshark', 'PyShark'),
159160
} # type: dict[str, tuple[str, str]]
160161

161162
##########################################################################
@@ -240,6 +241,11 @@ def engine(self) -> 'Engine':
240241
"""PCAP extraction engine."""
241242
return self._exeng
242243

244+
@property
245+
def magic_number(self) -> 'bytes':
246+
"""Magic number of input PCAP file."""
247+
return self._magic
248+
243249
##########################################################################
244250
# Methods.
245251
##########################################################################
@@ -319,7 +325,12 @@ def run(self) -> 'None': # pylint: disable=inconsistent-return-statements
319325
'using default engine instead', EngineWarning, stacklevel=stacklevel())
320326
self._exnam = 'default' # using default/pcapkit engine
321327

322-
self._exeng = cast('Engine[P]', PCAP_Engine(self))
328+
if self._magic in PCAP_Engine.MAGIC_NUMBER:
329+
self._exeng = cast('Engine[P]', PCAP_Engine(self))
330+
elif self._magic in PCAPNG_Engine.MAGIC_NUMBER:
331+
self._exeng = cast('Engine[P]', PCAPNG_Engine(self))
332+
else:
333+
raise FormatError(f'unknown file format: {self._magic!r}')
323334
self._exeng.run()
324335

325336
# start iteration
@@ -445,7 +456,15 @@ def record_header(self) -> 'Engine':
445456

446457
self._ifile.seek(0, os.SEEK_SET)
447458
return engine
448-
raise FormatError(f'unknown PCAP file format: {self._magic!r}')
459+
460+
if self._magic in PCAPNG_Engine.MAGIC_NUMBER:
461+
engine = PCAPNG_Engine(self) # type: ignore[assignment]
462+
engine.run()
463+
464+
self._ifile.seek(0, os.SEEK_SET)
465+
return engine
466+
467+
raise FormatError(f'unknown file format: {self._magic!r}')
449468

450469
def record_frames(self) -> 'None':
451470
"""Read packet frames.

0 commit comments

Comments
 (0)