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#

Bases: Generic[_T]

Internal base class for protocol fields.

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:

_Field

Returns:

Updated field instance.

Notes

This method will return a new instance of _Field 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(_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.

property length: int#

Field size.

__call__(packet)#

Update field attributes.

Parameters:

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

Return type:

Field

Returns:

New instance of Field.

Notes

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:
property bit_length: int#

Field bit length.

__call__(packet)#

Update field attributes.

Parameters:

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

Return type:

NumberField

Returns:

New instance of NumberField.

Notes

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[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:
__call__(packet)#

Update field attributes.

Parameters:

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

Return type:

_TextField

Returns:

New instance of _TextField.

Notes

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:
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[[StringField, 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:

IP Address Fields#

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

Bases: Field[_T]

Internal IP address value for protocol fields.

Parameters:
abstract property version: int#

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:

TypeVar(_T, IPv4Address, IPv6Address)

Returns:

Processed field value.

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

Bases: _IPField[IPv4Address]

IPv4 address value for protocol fields.

Parameters:
property version: Literal[4]#

IP version number.

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

Bases: _IPField[IPv6Address]

IPv6 address value for protocol fields.

Parameters:
property version: Literal[6]#

IP version number.

Container Fields#

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

Bases: _Field[list[_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 optional: bool#

Field is optional.

__call__(packet)#

Update field attributes.

Parameters:

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

Return type:

ListField

Returns:

Updated field instance.

Notes

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

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[Schema]]) – Base schema for option fields.

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

  • registry (Optional[defaultdict[int | IntEnum | IntEnum, Type[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[[ListField, 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[Schema]#

Base schema.

property type_name: str#

Type name.

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

Option registry.

property eool: int | IntEnum | IntEnum#

EOOL option.

unpack(buffer, packet)#

Unpack field value from bytes.

Parameters:
Return type:

list[Schema]

Returns:

Unpacked field value.

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:

ConditionalField

Returns:

Updated field instance.

Notes

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:

PayloadField

Returns:

Updated field instance.

Notes

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(length=<function SwitchField.<lambda>>, selector=<function SwitchField.<lambda>>)#

Bases: _Field[_TC]

Conditional type-switching field for protocol schema.

Parameters:
property optional: bool#

Field is optional.

__call__(packet)#

Call field.

Parameters:

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

Return type:

SwitchField[TypeVar(_TC)]

Returns:

New field instance.

Notes

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.

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#

Bases: _Field[_TN]

Schema field for no value type (or None).

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.