Miscellaneous Fields#

Conditional Fields#

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

Bases: FieldBase[_TC]

Conditional type-switching field for protocol schema.

Parameters:

selector (Callable[[dict[str, Any]], FieldBase[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: FieldBase[_TC]#

Field instance.

__call__(packet)[source]#

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)[source]#

Process field value before construction (packing).

Parameters:
Return type:

Any

Returns:

Processed field value.

pack(value, packet)[source]#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

post_process(value, packet)[source]#

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)[source]#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TC)

Returns:

Unpacked field value.

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

Bases: FieldBase[_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: FieldBase[_TC]#

Field instance.

__call__(packet)[source]#

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)[source]#

Process field value before construction (packing).

Parameters:
Return type:

Any

Returns:

Processed field value.

pack(value, packet)[source]#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

post_process(value, packet)[source]#

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)[source]#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TC)

Returns:

Unpacked field value.

test(packet)[source]#

Test field condition.

Parameters:

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

Returns:

Test result.

Return type:

bool

class pcapkit.corekit.fields.misc.ForwardMatchField(field)[source]#

Bases: FieldBase[_TC]

Schema field for non-capturing forward matching.

Parameters:

field (FieldBase[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: FieldBase[_TC]#

Field instance.

__call__(packet)[source]#

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)[source]#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

unpack(buffer, packet)[source]#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TC)

Returns:

Unpacked field value.

Generic Data Fields#

class pcapkit.corekit.fields.misc.SchemaField(length=<function SchemaField.<lambda>>, schema=None, default=<pcapkit.corekit.fields.field.NoValueType object>, packet=None, callback=<function SchemaField.<lambda>>)[source]#

Bases: FieldBase[_TS]

Schema field for protocol schema.

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.

  • schema (Optional[Type[TypeVar(_TS, bound= Schema)]]) – Field schema.

  • default (Union[TypeVar(_TS, bound= Schema), NoValueType, bytes]) – Default value for field.

  • packet (Optional[dict[str, Any]]) – Optional packet data for unpacking and/or packing purposes.

  • callback (Callable[[Self, dict[str, Any]], None]) – Callback function to process field value, which should accept the current field and the current packet as its arguments.

property length: int#

Field size.

property optional: bool#

Field is optional.

property schema: Type[_TS]#

Field schema.

__call__(packet)[source]#

Update field attributes.

Parameters:

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

Return type:

Self

Returns:

New field instance.

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

pack(value, packet)[source]#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

Notes

We will use packet as a __packet__ key in the packet context passed to the underlying Schema for packing purposes.

unpack(buffer, packet)[source]#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TS, bound= Schema)

Returns:

Unpacked field value.

Notes

We will use packet as a __packet__ key in the packet context passed to the underlying Schema for unpacking purposes.

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

Bases: FieldBase[_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)[source]#

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)[source]#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

unpack(buffer, packet)[source]#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TP, bound= Protocol)

Returns:

Unpacked field value.

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

Bases: FieldBase[_TN]

Schema field for no value type (or None).

property template: str#

Field template.

property length: int#

Field size.

pack(value, packet)[source]#

Pack field value into bytes.

Parameters:
Return type:

bytes

Returns:

Packed field value.

unpack(buffer, packet)[source]#

Unpack field value from bytes.

Parameters:
Return type:

TypeVar(_TN, bound= NoValueType)

Returns:

Unpacked field value.

Type Variables#

pcapkit.corekit.fields.misc._TC: Any#
pcapkit.corekit.fields.misc._TS: pcapkit.protocols.schema.schema.Schema#
pcapkit.corekit.fields.misc._TP: pcapkit.protocols.protocol.Protocol#
pcapkit.corekit.fields.misc._TN: pcapkit.corekit.fields.field.NoValueType#