# -*- coding: utf-8 -*-"""Raw Packet Data=====================.. module:: pcapkit.protocols.misc.raw:mod:`pcapkit.protocols.misc.raw` contains:class:`~pcapkit.protocols.misc.raw.Raw` only, which implementsextractor for unknown protocol, and constructs a:class:`~pcapkit.protocols.protocol.Protocol` like object."""importiofromtypingimportTYPE_CHECKING,overloadfrompcapkit.corekit.protochainimportProtoChainfrompcapkit.protocols.data.misc.rawimportRawasData_Rawfrompcapkit.protocols.misc.nullimportNoPayloadfrompcapkit.protocols.protocolimportProtocolBaseasProtocolfrompcapkit.protocols.schema.misc.rawimportRawasSchema_Rawfrompcapkit.utilities.exceptionsimportUnsupportedCallifTYPE_CHECKING:fromtypingimportIO,Any,NoReturn,Optionalfromtyping_extensionsimportLiteral__all__=['Raw']
[docs]classRaw(Protocol[Data_Raw,Schema_Raw],schema=Schema_Raw,data=Data_Raw):"""This class implements universal unknown protocol."""########################################################################### Properties.########################################################################### name of current protocol@propertydefname(self)->'Literal["Unknown"]':"""Name of current protocol."""return'Unknown'# header length of current protocol@propertydeflength(self)->'Literal[0]':"""Header length of current protocol."""return0# name of next layer protocol@propertydefprotocol(self)->'NoReturn':"""Name of next layer protocol. Raises: UnsupportedCall: This protocol doesn't support :attr:`protocol`. """raiseUnsupportedCall(f"{self.__class__.__name__!r} object has no attribute 'protocol'")########################################################################### Methods.##########################################################################
[docs]defread(self,length:'Optional[int]'=None,*,error:'Optional[Exception]'=None,# pylint: disable=arguments-differalias:'Optional[int]'=None,**kwargs:'Any')->'Data_Raw':# pylint: disable=unused-argument"""Read raw packet data. Args: length: Length of packet data. error: Parsing errors if any. alias: Original enumeration of the unknown protocol. **kwargs: Arbitrary keyword arguments. Returns: The parsed packet data. """raw=Data_Raw(protocol=alias,error=error,)returnraw
[docs]defmake(self,packet:'bytes'=b'',**kwargs:'Any')->'Schema_Raw':"""Make raw packet data. Args: packet: Raw packet data. **kwargs: Arbitrary keyword arguments. Returns: Constructed packet schema. """returnSchema_Raw(packet=packet)
########################################################################### Data models.##########################################################################@overloaddef__post_init__(self,file:'IO[bytes] | bytes',length:'Optional[int]'=...,**kwargs:'Any')->'None':...@overloaddef__post_init__(self,**kwargs:'Any')->'None':...# pylint: disable=arguments-differ
[docs]def__post_init__(self,file:'Optional[IO[bytes] | bytes]'=None,length:'Optional[int]'=None,**kwargs:'Any')->'None':"""Post initialisation hook. Would :mod:`pcapkit` encounter malformed packets, the original parsing error instance will be provided as in ``error``. Args: file: Source packet stream. length: Length of packet data. error (Optional[Exception]): Parsing errors if any (for parsing). alias (Optional[int]): Original enumeration of the unknown protocol. **kwargs: Arbitrary keyword arguments. See Also: For construction argument, please refer to :meth:`make`. """iffileisNone:_data=self.pack(**kwargs)else:_data=fileifisinstance(file,bytes)elsefile.read(length)# type: ignore[arg-type]#: bytes: Raw packet data.self._data=_data#: io.BytesIO: Source packet stream.self._file=io.BytesIO(self._data)#: pcapkit.protocols.data.misc.raw.Raw: Parsed packet data.self._info=self.unpack(length,**kwargs)ifself._info.protocolisnotNoneandhasattr(self._info.protocol,'name'):alias=self._info.protocol.nameelse:alias=self.alias#: pcapkit.protocols.null.NoPayload: Next layer (no payload).self._next=NoPayload()#: pcapkit.corekit.protochain.ProtoChain: Protocol chain from current layer.self._protos=ProtoChain(self.__class__,alias)
[docs]@classmethoddef__index__(cls)->'NoReturn':"""Numeral registry index of the protocol. Raises: UnsupportedCall: This protocol has no registry entry. """raiseUnsupportedCall(f'{cls.__name__!r} object cannot be interpreted as an integer')