Skip to content

Commit 429f1a0

Browse files
committed
revised 3rd party engines (no need to call builtin engine)
1 parent 1dfd66b commit 429f1a0

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

pcapkit/foundation/engines/dpkt.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from typing import TYPE_CHECKING, cast
1414

1515
from pcapkit.const.reg.linktype import LinkType as Enum_LinkType
16-
from pcapkit.foundation.engines import engine
1716
from pcapkit.foundation.engines.engine import Engine
1817
from pcapkit.utilities.exceptions import FormatError, stacklevel
1918
from pcapkit.utilities.warnings import AttributeWarning, DPKTWarning, warn
@@ -90,6 +89,7 @@ def run(self) -> 'None':
9089
9190
"""
9291
from pcapkit.foundation.engines.pcap import PCAP
92+
from pcapkit.foundation.engines.pcapng import PCAPNG
9393

9494
ext = self._extractor
9595
dpkt = self._expkg
@@ -106,12 +106,13 @@ def run(self) -> 'None':
106106
f'Frame {e._frnum:>3d}: {packet2chain(f)}' # pylint: disable=protected-access
107107
) # pylint: disable=logging-fstring-interpolation
108108

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())
115116

116117
if self._dlink == Enum_LinkType.ETHERNET:
117118
pkg = dpkt.ethernet.Ethernet
@@ -139,7 +140,7 @@ def unpack(ext, buf: 'bytes') -> 'None':
139140

140141
# extract & analyse file
141142
self._expkg = pkg
142-
self._extmp = iter(dpkt.pcap.Reader(ext._ifile))
143+
self._extmp = reader
143144

144145
def read_frame(self) -> 'DPKTPacket':
145146
"""Read frames with DPKT engine.
@@ -172,6 +173,8 @@ def read_frame(self) -> 'DPKTPacket':
172173
ofile(info, name=frnum)
173174
else:
174175
ext._ofile(info, name=frnum)
176+
ofile = ext._ofile
177+
ext._offmt = ofile.kind
175178

176179
# record fragments
177180
if ext._flag_r:

pcapkit/foundation/engines/pcapng.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ class PCAPNG(Engine[P_PCAPNG]):
9292
#: File context storage.
9393
_ctx_list: 'list[Context]'
9494

95-
MAGIC_NUMBER = b'\x0a\x0d\x0d\x0a'
95+
MAGIC_NUMBER = (
96+
b'\x0a\x0d\x0d\x0a',
97+
)
9698

9799
##########################################################################
98100
# Properties.

pcapkit/foundation/engines/pyshark.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
.. _PyShark: https://kiminewt.github.io/pyshark
1111
1212
"""
13-
#import os
1413
from typing import TYPE_CHECKING, cast
1514

1615
from pcapkit.foundation.engines.engine import Engine
@@ -101,10 +100,6 @@ def run(self) -> 'None':
101100
f'Frame {e._frnum:>3d}: {f.frame_info.protocols}' # pylint: disable=protected-access
102101
) # pylint: disable=logging-fstring-interpolation
103102

104-
# extract global header
105-
#ext.record_header()
106-
#ext._ifile.seek(0, os.SEEK_SET)
107-
108103
# extract & analyse file
109104
self._extmp = self._expkg.FileCapture(ext._ifnm, keep_packets=False)
110105

@@ -137,6 +132,8 @@ def read_frame(self) -> 'PySharkPacket':
137132
ofile(info, name=frnum)
138133
else:
139134
ext._ofile(info, name=frnum)
135+
ofile = ext._ofile
136+
ext._offmt = ofile.kind
140137

141138
# trace flows
142139
if ext._flag_t:

0 commit comments

Comments
 (0)