|
22 | 22 |
|
23 | 23 | from pcapkit.dumpkit.common import make_dumper
|
24 | 24 | from pcapkit.foundation.engines.pcap import PCAP as PCAP_Engine
|
| 25 | +from pcapkit.foundation.engines.pcapng import PCAPNG as PCAPNG_Engine |
25 | 26 | from pcapkit.foundation.reassembly import ReassemblyManager
|
26 | 27 | from pcapkit.foundation.traceflow import TraceFlowManager
|
27 | 28 | from pcapkit.utilities.exceptions import (CallableError, FileNotFound, FormatError, IterableError,
|
@@ -153,9 +154,9 @@ class Extractor(Generic[P]):
|
153 | 154 | #: dict[str, tuple[str, str]]: Engine mapping for extracting frames.
|
154 | 155 | #: The values should be a tuple representing the module name and class name.
|
155 | 156 | __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'), |
159 | 160 | } # type: dict[str, tuple[str, str]]
|
160 | 161 |
|
161 | 162 | ##########################################################################
|
@@ -240,6 +241,11 @@ def engine(self) -> 'Engine':
|
240 | 241 | """PCAP extraction engine."""
|
241 | 242 | return self._exeng
|
242 | 243 |
|
| 244 | + @property |
| 245 | + def magic_number(self) -> 'bytes': |
| 246 | + """Magic number of input PCAP file.""" |
| 247 | + return self._magic |
| 248 | + |
243 | 249 | ##########################################################################
|
244 | 250 | # Methods.
|
245 | 251 | ##########################################################################
|
@@ -319,7 +325,12 @@ def run(self) -> 'None': # pylint: disable=inconsistent-return-statements
|
319 | 325 | 'using default engine instead', EngineWarning, stacklevel=stacklevel())
|
320 | 326 | self._exnam = 'default' # using default/pcapkit engine
|
321 | 327 |
|
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}') |
323 | 334 | self._exeng.run()
|
324 | 335 |
|
325 | 336 | # start iteration
|
@@ -445,7 +456,15 @@ def record_header(self) -> 'Engine':
|
445 | 456 |
|
446 | 457 | self._ifile.seek(0, os.SEEK_SET)
|
447 | 458 | 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}') |
449 | 468 |
|
450 | 469 | def record_frames(self) -> 'None':
|
451 | 470 | """Read packet frames.
|
|
0 commit comments