Protocol Family¶
pcapkit.protocols
is collection of all protocol families,
with detailed implementation and methods.
- PCAP File Headers
- Link Layer Protocols
- Internet Layer Protocols
- AH - Authentication Header
- HIP - Host Identity Protocol
- HOPOPT - IPv6 Hop-by-Hop Options
- IP - Internet Protocol
- IPsec - Internet Protocol Security
- IPv4 - Internet Protocol version 4
- IPv6-Frag - Fragment Header for IPv6
- IPv6-Opts - Destination Options for IPv6
- IPv6-Route - Routing Header for IPv6
- IPv6 - Internet Protocol version 6
- IPX - Internetwork Packet Exchange
- MH - Mobility Header
- Base Protocol
- Transport Layer Protocols
- Application Layer Protocols
- Miscellaneous Protocols
Base Protocol¶
-
class
pcapkit.protocols.protocol.
Protocol
(file=None, length=None, **kwargs)[source]¶ Bases:
object
Abstract base class for all protocol family.
-
__layer__
= None¶ Layer of protocol. Can be one of
Link
,Internet
,Transport
andApplication
.- Type
Literal[‘Link’, ‘Internet’, ‘Transport’, ‘Application’]
-
__proto__
= {}¶ Protocol index mapping for decoding next layer, c.f.
self._decode_next_layer
&self._import_next_layer
. The values should be a tuple representing the module name and class name.
-
__contains__
(name)[source]¶ Returns if
name
is inself._info
.- Parameters
name (Any) – name to search
- Returns
if
name
exists- Return type
-
__getitem__
(key)[source]¶ Subscription (
getitem
) support.If
key
is a :obj`slice` object,ProtocolUnbound
will be raised.If
key
is aProtocol
object, the method will fetch its indexes (id()
).Later, search the packet’s chain of protocols with the calculated
key
.If no matches, then raises
ProtocolNotFound
.
- Parameters
- Returns
The sub-packet from the current packet of indexed protocol.
- Return type
- Raises
ProtocolUnbound – If
key
is aslice
object.ProtocolNotFound – If
key
is not in the current packet.
-
abstract classmethod
__index__
()[source]¶ Numeral registry index of the protocol.
- Returns
Numeral registry index of the protocol.
- Return type
-
__init__
(file=None, length=None, **kwargs)[source]¶ Initialisation.
- Parameters
file (Optional[io.BytesIO]) – Source packet stream.
length (Optional[int]) – Length of packet data.
- Keyword Arguments
_error (bool) – If the object is initiated after parsing errors (
self._onerror
)._layer (str) – Parse packet until
_layer
(self._onerror
)._protocol (str) – Parse packet until
_protocol
(self._onerror
).**kwargs – Arbitrary keyword arguments.
-
__post_init__
(file=None, length=None, **kwargs)[source]¶ Post initialisation hook.
- Parameters
file (Optional[io.BytesIO]) – Source packet stream.
length (Optional[int]) – Length of packet data.
- Keyword Arguments
**kwargs – Arbitrary keyword arguments.
See also
For construction argument, please refer to
make()
.
-
__repr__
()[source]¶ Returns representation of parsed protocol data.
Example
>>> protocol <Frame Info(..., ethernet=Info(...), protocols='Ethernet:IPv6:Raw')>
-
_check_term_threshold
()[source]¶ Check if reached termination threshold.
- Returns
if reached termination threshold
- Return type
-
_import_next_layer
(proto, length=None)[source]¶ Import next layer extractor.
- Parameters
- Returns
instance of next layer
- Return type
-
classmethod
_make_index
(name, default=None, *, namespace=None, reversed=False, pack=False, size=4, signed=False, lilendian=False)[source]¶ Return first index of
name
from adict
or enumeration.- Parameters
name (Union[str, int, enum.IntEnum]) – item to be indexed
default (int) – default value
- Keyword Arguments
- Returns
Index of
name
from a dict or enumeration. Ifpacket
isTrue
, returnsbytes
; otherwise, returnsint
.- Return type
- Raises
ProtocolNotImplemented – If
name
is NOT innamespace
anddefault
isNone
.
-
classmethod
_make_pack
(integer, *, size=1, signed=False, lilendian=False)[source]¶ Pack integers to bytes.
-
_read_fileng
(*args, **kwargs)[source]¶ Read file buffer (
self._file
).This method wraps the
file.read()
call.- Parameters
*args – arbitrary positional arguments
- Keyword Arguments
**kwargs – arbitrary keyword arguments
- Returns
Data read from file buffer.
- Return type
-
_read_packet
(length=None, *, header=None, payload=None, discard=False)[source]¶ Read raw packet data.
- Parameters
length (int) – length of the packet
- Keyword Arguments
- Returns
If
header
omits, returns the whole packet data inbytes
.If
discard
is set asTrue
, returns the packet body (inbytes
) only.Otherwise, returns the header and payload data as a
dict
:class Packet(TypedDict): """Header and payload data.""" #: packet header header: bytes #: packet payload payload: bytes
-
_read_unpack
(size=1, *, signed=False, lilendian=False, quiet=False)[source]¶ Read bytes and unpack for integers.
- Parameters
size (int) – buffer size
- Keyword Arguments
- Returns
unpacked data upon success
- Return type
Optional[int]
- Raises
StructError – If unpack (
struct.pack()
) failed, andstruct.error
raised.
-
static
decode
(byte, *, encoding=None, errors='strict')[source]¶ -
Should decoding failed using
encoding
, the method will try again decoding thebytes
as'unicode_escape'
.- Parameters
byte (bytes) – Source bytestring.
- Keyword Arguments
encoding (Optional[str]) – The encoding with which to decode the
bytes
. If not provided,pcapkit
will first try detecting its encoding usingchardet
. The fallback encoding would is UTF-8.errors (str) – The error handling scheme to use for the handling of decoding errors. The default is
'strict'
meaning that decoding errors raise aUnicodeDecodeError
. Other possible values are'ignore'
and'replace'
as well as any other name registered withcodecs.register_error()
that can handleUnicodeDecodeError
.
- Returns
Decoede string.
- Return type
See also
-
classmethod
id
()[source]¶ Index ID of the protocol.
By default, it returns the name of the protocol.
-
abstract
make
(**kwargs)[source]¶ Make (construct) packet data.
- Keyword Arguments
**kwargs – Arbitrary keyword arguments.
- Returns
Constructed packet data.
- Return type
-
static
unquote
(url, *, encoding='utf-8', errors='replace')[source]¶ Unquote URLs into readable format.
Should decoding failed , the method will try again replacing
'%'
with'\x'
then decoding theurl
as'unicode_escape'
.- Parameters
url (str) – URL string.
- Keyword Arguments
encoding (str) – The encoding with which to decode the
bytes
.errors (str) – The error handling scheme to use for the handling of decoding errors. The default is
'strict'
meaning that decoding errors raise aUnicodeDecodeError
. Other possible values are'ignore'
and'replace'
as well as any other name registered withcodecs.register_error()
that can handleUnicodeDecodeError
.
- Returns
Unquoted string.
- Return type
See also
-
property
info
¶ Info dict of current instance.
- Return type
-
property
payload
¶ Payload of current instance.
- Return type
-
property
protochain
¶ Protocol chain of current instance.
- Return type
-