Protocol Fields#

pcapkit.corekit.fields is collection of protocol fields, descriptive of the structure of protocol headers.

Base Field#

class pcapkit.corekit.fields.field._Field(*args, **kwargs)#

Bases: Generic[_T]

Internal base class for protocol fields.

Important

A negative value of length indicates that the field is variable-length (i.e., length unspecified) and thus pack() should be considerate of the template format and the actual value provided for packing.

Parameters:
  • *args (Any) – Arbitrary positional arguments.

  • **kwargs (Any) – Arbitrary keyword arguments.

property name: str#

Field name.

property default: _T | NoValueType#

Field default value.

property template: str#

Field template.

property length: int#

Field size.

property optional: bool#

Field is optional.

__call__(packet)#

Update field attributes.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

Self

Returns:

Updated field instance.

This method will return a new instance of _Field instead of updating the current instance.

__set_name__(owner, name)#

Set field name and update field list (if applicable).

This method is to be called by the metaclass during class creation. It is used to set the field name and update the field list, i.e., Schema.__fields__ mapping dictionary.

Return type:

None

Parameters:
pre_process(value, packet)#

Process field value before construction (packing).

Parameters:
Return type:

Any

Returns:

Processed field value.

pack(value, packet)#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

post_process(value, packet)#

Process field value after parsing (unpacking).

Parameters:
  • value (Any) – Field value.

  • packet (dict[str, Any]) – Packet data.

Return type:

TypeVar(_T)

Returns:

Processed field value.

unpack(buffer, packet)#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_T)

Returns:

Unpacked field value.

class pcapkit.corekit.fields.field.Field(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function Field.<lambda>>)#

Bases: _Field[_T], Generic[_T]

Base class for protocol fields.

Parameters:
property template: str#

Field template.

__call__(packet)#

Update field attributes.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

Self

Returns:

New instance of Field.

This method will return a new instance of Field instead of updating the current instance.

Auxiliary#

class pcapkit.corekit.fields.field.NoValueType#

Bases: object

Default value for fields.

pcapkit.corekit.fields.field.NoValue#

Default value for _Field.default.

Type:

NoValueType

Numerical Fields#

class pcapkit.corekit.fields.numbers.NumberField(length=None, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, callback=<function NumberField.<lambda>>)#

Bases: Field[int], Generic[_T]

Numerical value for protocol fields.

Parameters:
  • length (Union[int, Callable[[dict[str, Any]], int], None]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.

  • default (int | NoValueType) – Field default value, if any.

  • signed (bool) – Whether the field is signed.

  • byteorder (Literal['little', 'big']) – Field byte order.

  • bit_length (Optional[int]) – Field bit length.

  • callback (Callable[[Self, dict[str, Any]], None]) – Callback function to be called upon self.__call__.

property bit_length: int#

Field bit length.

__call__(packet)#

Update field attributes.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

Self

Returns:

New instance of NumberField.

This method will return a new instance of NumberField instead of updating the current instance.

build_template(length, signed)#

Build template for field.

Parameters:
  • length (int) – Field size (in bytes)

  • signed (bool) – Whether the field is signed

Return type:

str

Returns:

Template for field.

pre_process(value, packet)#

Process field value before construction (packing).

Parameters:
  • value (int) – Field value.

  • packet (dict[str, Any]) – Packet data.

Return type:

int | bytes

Returns:

Processed field value.

post_process(value, packet)#

Process field value after parsing (unpacked).

Parameters:
Return type:

int

Returns:

Processed field value.

class pcapkit.corekit.fields.numbers.Int32Field(length=None, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, callback=<function NumberField.<lambda>>)#

Bases: NumberField

Integer value for protocol fields.

Parameters:
class pcapkit.corekit.fields.numbers.UInt32Field(length=None, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, callback=<function NumberField.<lambda>>)#

Bases: NumberField

Unsigned integer value for protocol fields.

Parameters:
class pcapkit.corekit.fields.numbers.Int16Field(length=None, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, callback=<function NumberField.<lambda>>)#

Bases: NumberField

Short integer value for protocol fields.

Parameters:
class pcapkit.corekit.fields.numbers.UInt16Field(length=None, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, callback=<function NumberField.<lambda>>)#

Bases: NumberField

Unsigned short integer value for protocol fields.

Parameters:
class pcapkit.corekit.fields.numbers.Int64Field(length=None, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, callback=<function NumberField.<lambda>>)#

Bases: NumberField

Long integer value for protocol fields.

Parameters:
class pcapkit.corekit.fields.numbers.UInt64Field(length=None, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, callback=<function NumberField.<lambda>>)#

Bases: NumberField

Unsigned long integer value for protocol fields.

Parameters:
class pcapkit.corekit.fields.numbers.Int8Field(length=None, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, callback=<function NumberField.<lambda>>)#

Bases: NumberField

Byte value for protocol fields.

Parameters:
class pcapkit.corekit.fields.numbers.UInt8Field(length=None, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, callback=<function NumberField.<lambda>>)#

Bases: NumberField

Unsigned byte value for protocol fields.

Parameters:
class pcapkit.corekit.fields.numbers.EnumField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, signed=False, byteorder='big', bit_length=None, namespace=None, callback=<function EnumField.<lambda>>)#

Bases: NumberField[Union[IntEnum, IntEnum]]

Enumerated value for protocol fields.

Parameters:
post_process(value, packet)#

Process field value after parsing (unpacked).

Parameters:
Return type:

IntEnum | IntEnum

Returns:

Processed field value.

Text Fields#

class pcapkit.corekit.fields.strings._TextField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function _TextField.<lambda>>)#

Bases: Field[_T], Generic[_T]

Internal text value for protocol fields.

Parameters:
  • length (Union[int, Callable[[dict[str, Any]], int]]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.

  • default (Union[TypeVar(_T, str, bytes, dict[str, Any]), NoValueType]) – Field default value, if any.

  • callback (Callable[[Self, dict[str, Any]], None]) – Callback function to be called upon self.__call__.

__call__(packet)#

Update field attributes.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

Self

Returns:

New instance of _TextField.

This method will return a new instance of _TextField instead of updating the current instance.

class pcapkit.corekit.fields.strings.BytesField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function _TextField.<lambda>>)#

Bases: _TextField[bytes]

Bytes value for protocol fields.

Parameters:
  • length (Union[int, Callable[[dict[str, Any]], int]]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.

  • default (Union[TypeVar(_T, str, bytes, dict[str, Any]), NoValueType]) – Field default value, if any.

  • callback (Callable[[Self, dict[str, Any]], None]) – Callback function to be called upon self.__call__.

pre_process(value, packet)#

Process field value before construction (packing).

Parameters:
Return type:

bytes

Returns:

Processed field value.

class pcapkit.corekit.fields.strings.StringField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, encoding=None, errors='strict', unquote=False, callback=<function StringField.<lambda>>)#

Bases: _TextField[str]

String value for protocol fields.

Parameters:
  • length (Union[int, Callable[[dict[str, Any]], int]]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.

  • default (str | NoValueType) – Field default value, if any.

  • encoding (Optional[str]) – The encoding with which to decode the bytes. If not provided, pcapkit will first try detecting its encoding using chardet. The fallback encoding would is UTF-8.

  • errors (Literal['strict', 'ignore', 'replace']) – The error handling scheme to use for the handling of decoding errors. The default is 'strict' meaning that decoding errors raise a UnicodeDecodeError. Other possible values are 'ignore' and 'replace' as well as any other name registered with codecs.register_error() that can handle UnicodeDecodeError.

  • unquote (bool) – Whether to unquote the decoded string as a URL. Should decoding failed , the method will try again replacing '%' with '\x' then decoding the url as 'utf-8' with 'replace' for error handling.

  • callback (Callable[[Self, dict[str, Any]], None]) – Callback function to be called upon self.__call__.

pre_process(value, packet)#

Process field value before construction (packing).

Parameters:
  • value (str) – Field value.

  • packet (dict[str, Any]) – Packet data.

Return type:

bytes

Returns:

Processed field value.

post_process(value, packet)#

Process field value after parsing (unpacked).

Parameters:
Return type:

str

Returns:

Processed field value.

class pcapkit.corekit.fields.strings.BitField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, namespace=None, callback=<function BitField.<lambda>>)#

Bases: _TextField[dict[str, Any]]

Bit value for protocol fields.

Parameters:
pre_process(value, packet)#

Process field value before construction (packing).

Parameters:
Return type:

bytes

Returns:

Processed field value.

post_process(value, packet)#

Process field value after parsing (unpacked).

Parameters:
Return type:

dict[str, Any]

Returns:

Processed field value.

class pcapkit.corekit.fields.strings.PaddingField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function _TextField.<lambda>>)#

Bases: BytesField

Bytes value for protocol fields.

Parameters:
  • length (Union[int, Callable[[dict[str, Any]], int]]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.

  • default (Union[TypeVar(_T, str, bytes, dict[str, Any]), NoValueType]) – Field default value, if any.

  • callback (Callable[[Self, dict[str, Any]], None]) – Callback function to be called upon self.__call__.

IP Address Fields#

class pcapkit.corekit.fields.ipaddress._IPField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function Field.<lambda>>)#

Bases: Field[_T], Generic[_T]

Internal IP related value for protocol fields.

Parameters:
abstract property version: int#

IP version number.

IP Addresses#

class pcapkit.corekit.fields.ipaddress._IPAddressField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function Field.<lambda>>)#

Bases: _IPField[_AT]

Internal IP address value for protocol fields.

Parameters:
pre_process(value, packet)#

Process field value before packing.

Parameters:
Return type:

bytes

Returns:

Processed field value.

post_process(value, packet)#

Process field value after parsing (unpacking).

Parameters:
Return type:

TypeVar(_AT, IPv4Address, IPv6Address)

Returns:

Processed field value.

class pcapkit.corekit.fields.ipaddress.IPv4AddressField(default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function IPv4AddressField.<lambda>>)#

Bases: _IPAddressField[IPv4Address]

IPv4 address value for protocol fields.

Parameters:
property version: Literal[4]#

IP version number.

class pcapkit.corekit.fields.ipaddress.IPv6AddressField(default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function IPv6AddressField.<lambda>>)#

Bases: _IPAddressField[IPv6Address]

IPv6 address value for protocol fields.

Parameters:
property version: Literal[6]#

IP version number.

IP Interface#

class pcapkit.corekit.fields.ipaddress._IPInterfaceField(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function Field.<lambda>>)#

Bases: _IPField[_IT]

Internal IP interface value for protocol fields.

Parameters:
class pcapkit.corekit.fields.ipaddress.IPv4InterfaceField(default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function IPv4InterfaceField.<lambda>>)#

Bases: _IPInterfaceField[IPv4Interface]

IPv4 interface value for protocol fields.

Parameters:
property version: Literal[4]#

IP version number.

pre_process(value, packet)#

Process field value before packing.

Parameters:
Return type:

bytes

Returns:

Processed field value.

post_process(value, packet)#

Process field value after parsing (unpacking).

Parameters:
Return type:

IPv4Interface

Returns:

Processed field value.

class pcapkit.corekit.fields.ipaddress.IPv6InterfaceField(default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function IPv6InterfaceField.<lambda>>)#

Bases: _IPInterfaceField[IPv6Interface]

IPv6 interface value for protocol fields.

Parameters:
property version: Literal[6]#

IP version number.

pre_process(value, packet)#

Process field value before packing.

Parameters:
Return type:

bytes

Returns:

Processed field value.

post_process(value, packet)#

Process field value after parsing (unpacking).

Parameters:
Return type:

IPv6Interface

Returns:

Processed field value.

Container Fields#

class pcapkit.corekit.fields.collections.ListField(length=<function ListField.<lambda>>, item_type=None, callback=<function ListField.<lambda>>)#

Bases: _Field[list[_TL]], Generic[_TL]

Field list for protocol fields.

Parameters:

This field is used to represent a list of fields, as in the case of lists of constrant-length-field items in a protocol.

property length: int#

Field size.

property optional: bool#

Field is optional.

__call__(packet)#

Update field attributes.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

Self

Returns:

Updated field instance.

This method will return a new instance of ListField instead of updating the current instance.

pack(value, packet)#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

unpack(buffer, packet)#

Unpack field value from bytes.

Parameters:
Return type:

bytes | list[TypeVar(_TL, Schema, _Field, bytes)]

Returns:

Unpacked field value.

class pcapkit.corekit.fields.collections.OptionField(length=<function OptionField.<lambda>>, base_schema=None, type_name='type', registry=None, eool=None, callback=<function OptionField.<lambda>>)#

Bases: ListField, Generic[_TS]

Field list for protocol options.

Parameters:
  • length (Union[int, Callable[[dict[str, Any]], int]]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.

  • base_schema (Optional[Type[TypeVar(_TS, bound= Schema)]]) – Base schema for option fields.

  • type_name (str) – Name of the option type field.

  • registry (Optional[defaultdict[int | IntEnum | IntEnum, Type[TypeVar(_TS, bound= Schema)]]]) – Option registry, as in a mapping from option types (enumeration values) to option schemas, with the default value being the unknown option schema.

  • eool (Union[int, IntEnum, IntEnum, None]) – Enumeration of the EOOL (end-of-option-list, or equivalent) option

  • callback (Callable[[Self, dict[str, Any]], None]) – Callback function to be called upon self.__call__.

This field is used to represent a list of fields, as in the case of lists of options and/or parameters in a protocol.

property base_schema: Type[_TS]#

Base schema.

property type_name: str#

Type name.

property registry: defaultdict[int | IntEnum | IntEnum, Type[_TS]]#

Option registry.

property eool: int | IntEnum | IntEnum#

EOOL option.

property option_padding: int#

Length option padding data.

unpack(buffer, packet)#

Unpack field value from bytes.

Parameters:
Return type:

list[TypeVar(_TS, bound= Schema)]

Returns:

Unpacked field value.

Important

If the option list ended before the specified size limit, set self.option_padding as the remaining length to the packet argument such that the next fields can be aware of such informations.

Miscellaneous Fields#

class pcapkit.corekit.fields.misc.ConditionalField(field, condition)#

Bases: _Field[_TC]

Conditional value for protocol fields.

Parameters:
property name: str#

Field name.

property default: _TC | NoValueType#

Field default value.

property template: str#

Field template.

property length: int#

Field size.

property optional: bool#

Field is optional.

property field: _Field[_TC]#

Field instance.

__call__(packet)#

Update field attributes.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

Self

Returns:

Updated field instance.

This method will return a new instance of ConditionalField instead of updating the current instance.

pre_process(value, packet)#

Process field value before construction (packing).

Parameters:
Return type:

Any

Returns:

Processed field value.

pack(value, packet)#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

post_process(value, packet)#

Process field value after parsing (unpacking).

Parameters:
  • value (Any) – Field value.

  • packet (dict[str, Any]) – Packet data.

Return type:

TypeVar(_TC)

Returns:

Processed field value.

unpack(buffer, packet)#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TC)

Returns:

Unpacked field value.

test(packet)#

Test field condition.

Parameters:

packet (dict[str, Any]) – Current packet.

Returns:

Test result.

Return type:

bool

class pcapkit.corekit.fields.misc.PayloadField(length=<function PayloadField.<lambda>>, default=<pcapkit.corekit.fields.field.NoValueType object>, protocol=None, callback=<function PayloadField.<lambda>>)#

Bases: _Field[_TP]

Payload value for protocol fields.

Parameters:
property template: str#

Field template.

property length: int#

Field size.

property optional: bool#

Field is optional.

property protocol: Type[_TP]#

Payload protocol.

__call__(packet)#

Update field attributes.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

Self

Returns:

Updated field instance.

This method will return a new instance of PayloadField instead of updating the current instance.

pack(value, packet)#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

unpack(buffer, packet)#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TP, bound= Protocol)

Returns:

Unpacked field value.

class pcapkit.corekit.fields.misc.SwitchField(selector=<function SwitchField.<lambda>>)#

Bases: _Field[_TC]

Conditional type-switching field for protocol schema.

Parameters:

selector (Callable[[dict[str, Any]], _Field[TypeVar(_TC)]]) – Callable function to select field type, which should accept the current packet as its only argument and return a field instance.

property name: str#

Field name.

property default: _TC | NoValueType#

Field default value.

property template: str#

Field template.

property length: int#

Field size.

property optional: bool#

Field is optional.

property field: _Field[_TC]#

Field instance.

__call__(packet)#

Call field.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

SwitchField[TypeVar(_TC)]

Returns:

New field instance.

This method will return a new instance of SwitchField instead of updating the current instance.

pre_process(value, packet)#

Process field value before construction (packing).

Parameters:
Return type:

Any

Returns:

Processed field value.

pack(value, packet)#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

post_process(value, packet)#

Process field value after parsing (unpacking).

Parameters:
  • value (Any) – Field value.

  • packet (dict[str, Any]) – Packet data.

Return type:

TypeVar(_TC)

Returns:

Processed field value.

unpack(buffer, packet)#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TC)

Returns:

Unpacked field value.

class pcapkit.corekit.fields.misc.ForwardMatchField(field)#

Bases: _Field[_TC]

Schema field for non-capturing forward matching.

Parameters:

field (_Field[TypeVar(_TC)]) – Field to forward match.

property name: str#

Field name.

property default: _TC | NoValueType#

Field default value.

property template: str#

Field template.

property length: int#

Field size.

property optional: bool#

Field is optional.

property field: _Field[_TC]#

Field instance.

__call__(packet)#

Update field attributes.

Parameters:

packet (dict[str, Any]) – Packet data.

Return type:

Self

Returns:

Updated field instance.

This method will return a new instance of ConditionalField instead of updating the current instance.

pack(value, packet)#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

unpack(buffer, packet)#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TC)

Returns:

Unpacked field value.

class pcapkit.corekit.fields.misc.NoValueField(*args, **kwargs)#

Bases: _Field[_TN]

Schema field for no value type (or None).

Parameters:
  • args (Any) –

  • kwargs (Any) –

property template: str#

Field template.

property length: int#

Field size.

pack(value, packet)#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

unpack(buffer, packet)#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TN, bound= NoValueType)

Returns:

Unpacked field value.