Skip to content

Commit d021ade

Browse files
committed
working on pcapng protocol impl (make done)
1 parent 92da1a2 commit d021ade

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

pcapkit/protocols/misc/pcapng.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@
167167
if TYPE_CHECKING:
168168
from decimal import Decimal
169169
from typing import IO, Any, Callable, DefaultDict, Optional, Type, Union
170+
from enum import IntEnum as StdlibEnum
171+
from aenum import IntEnum as AenumEnum
170172

171173
from mypy_extensions import DefaultArg, KwArg, NamedArg
172174
from typing_extensions import Literal
@@ -250,7 +252,7 @@ class PCAPNG(Protocol[Data_PCAPNG, Schema_PCAPNG],
250252
"""PCAP-NG file block extractor.
251253
252254
The class currently supports parsing of the following protocols, which are
253-
registered in the :attr:`self.__proto__ <pcapkit.protocols.misc.pcap.frame.Frame.__proto__>`
255+
registered in the :attr:`self.__proto__ <pcapkit.protocols.misc.pcapng.PCAPNG.__proto__>`
254256
attribute:
255257
256258
.. list-table::
@@ -792,16 +794,53 @@ def read(self, length: 'Optional[int]' = None, *, _read: 'bool' = True,
792794
return block
793795

794796
def make(self,
797+
type: 'Enum_BlockType | StdlibEnum | AenumEnum | str | int' = Enum_BlockType.Simple_Packet_Block,
798+
type_default: 'Optional[int]' = None,
799+
type_namespace: 'Optional[dict[str, int] | dict[int, str] | Type[StdlibEnum] | Type[AenumEnum]]' = None, # pylint: disable=line-too-long
800+
type_reversed: 'bool' = False,
801+
block: 'bytes | Data_PCAPNG | Schema_BlockType | dict[str, Any]' = b'',
795802
**kwargs: 'Any') -> 'Schema_PCAPNG':
796-
"""Make frame packet data.
803+
"""Make PCAP-NG block data.
797804
798805
Args:
806+
type: Block type.
807+
type_default: Default block type.
808+
type_namespace: Block type namespace.
809+
type_reversed: Whether to reverse block type namespace.
810+
block: Block data.
799811
**kwargs: Arbitrary keyword arguments.
800812
801813
Returns:
802814
Constructed packet data.
803815
804816
"""
817+
type_val = self._make_index(type, type_default, namespace=type_namespace, # type: ignore[call-overload]
818+
reversed=type_reversed, pack=False)
819+
820+
if isinstance(block, bytes):
821+
block_val = block # type: bytes | Schema_BlockType
822+
elif isinstance(block, (dict, Data_PCAPNG)):
823+
name = self.__block__[type_val]
824+
if isinstance(name, str):
825+
meth_name = f'_make_block_{name}'
826+
meth = cast('BlockConstructor',
827+
getattr(self, meth_name, self._make_block_unknown))
828+
else:
829+
meth = name[1]
830+
831+
if isinstance(block, dict):
832+
block_val = meth(type_val, **block)
833+
else:
834+
block_val = meth(type_val, block)
835+
elif isinstance(block, Schema):
836+
block_val = block
837+
else:
838+
raise ProtocolError(f'PCAP-NG: [Type {type_val}] invalid format')
839+
840+
return Schema_PCAPNG(
841+
type=type_val,
842+
block=block_val,
843+
)
805844

806845
##########################################################################
807846
# Data models.
@@ -833,7 +872,7 @@ def __post_init__(self, file: 'Optional[IO[bytes] | bytes]' = None, length: 'Opt
833872
For construction argument, please refer to :meth:`make`.
834873
835874
"""
836-
#: int: frame index number
875+
#: int: Block index number.
837876
self._fnum = num
838877
#: pcapkit.foundation.engins.pcapng.Context: Context of the PCAP-NG file.
839878
self._ctx = ctx
@@ -860,15 +899,15 @@ def __length_hint__(self) -> 'Literal[12]':
860899
# NOTE: This is a hack to make the ``__index__`` method work both as a
861900
# class method and an instance method.
862901
def __index__(self: 'Optional[PCAPNG]' = None) -> 'int': # type: ignore[override]
863-
"""Index of the frame.
902+
"""Index of the block.
864903
865904
Args:
866905
self: :class:`PCAPNG` object or :obj:`None`.
867906
868907
Returns:
869-
If the object is initiated, i.e. :attr:`self._fnum <pcapkit.protocols.misc.pcap.frame.Frame._fnum>`
908+
If the object is initiated, i.e. :attr:`self._fnum <pcapkit.protocols.misc.pcapng.PCAPNG._fnum>`
870909
exists, and is of a packet block (EPB, ISB or Packet), returns the
871-
frame index number of itself; else raises :exc:`UnsupportedCall`.
910+
block index number of itself; else raises :exc:`UnsupportedCall`.
872911
873912
Raises:
874913
UnsupportedCall: This protocol has no registry entry.

pcapkit/protocols/schema/misc/pcapng.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class PCAPNG(Schema):
221221
)
222222

223223
if TYPE_CHECKING:
224-
def __init__(self, type: 'Enum_BlockType', block: 'BlockType') -> 'None': ...
224+
def __init__(self, type: 'Enum_BlockType', block: 'BlockType | bytes') -> 'None': ...
225225

226226

227227
class BlockType(Schema):
@@ -1260,7 +1260,7 @@ class DecryptionSecretsBlock(BlockType):
12601260

12611261
if TYPE_CHECKING:
12621262
def __init__(self, type: 'Enum_BlockType', length: 'int', secrets_type: 'Enum_SecretsType',
1263-
secrets_length: 'int', secrets_data: 'bytes', padding: 'bytes',
1263+
secrets_length: 'int', secrets_data: 'DSBSecrets | bytes', padding: 'bytes',
12641264
options: 'list[Option | bytes] | bytes', length2: 'int') -> 'None': ...
12651265

12661266

0 commit comments

Comments
 (0)