Miscellaneous Fields¶
Conditional Fields¶
- class pcapkit.corekit.fields.misc.SwitchField[_TC](selector=lambda _: ...)[source]¶
-
Conditional type-switching field for protocol schema.
- Parameters:
selector (
Callable[[dict[str,Any]],FieldBase[_TC]]) – Callable function to select field type, which should accept the current packet as its only argument and return a field instance.
- property default: _TC | NoValueType¶
Field default value.
- __call__(packet)[source]¶
Call field.
- Parameters:
- Return type:
SwitchField[_TC]- Returns:
New field instance.
This method will return a new instance of
SwitchFieldinstead of updating the current instance.
- class pcapkit.corekit.fields.misc.ConditionalField[_TC](field, condition)[source]¶
-
Conditional value for protocol fields.
- Parameters:
- property default: _TC | NoValueType¶
Field default value.
- __call__(packet)[source]¶
Update field attributes.
- Parameters:
- Return type:
- Returns:
Updated field instance.
This method will return a new instance of
ConditionalFieldinstead of updating the current instance.
- class pcapkit.corekit.fields.misc.ForwardMatchField[_TC](field)[source]¶
-
Schema field for non-capturing forward matching.
- Parameters:
field (
FieldBase[_TC]) – Field to forward match.
- property default: _TC | NoValueType¶
Field default value.
- __call__(packet)[source]¶
Update field attributes.
- Parameters:
- Return type:
- Returns:
Updated field instance.
This method will return a new instance of
ForwardMatchFieldinstead of updating the current instance.
Generic Data Fields¶
- class pcapkit.corekit.fields.misc.SchemaField[_TS: Schema](length=lambda _: ..., schema=None, default=NO_VALUE, packet=None, callback=lambda *_: ...)[source]¶
-
Schema field for protocol schema.
- Parameters:
length (
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 (
_TS|NoValueType|bytes) – Default value for field.packet (
dict[str,Any] |None) – 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.
- __call__(packet)[source]¶
Update field attributes.
This method will return a new instance of
SchemaFieldinstead of updating the current instance.
- pack(value, packet)[source]¶
Pack field value into
bytes.- Parameters:
- Return type:
- Returns:
Packed field value.
Notes
packetis reachable from the nested schema’s own field callbacks both under a__packet__key and, for a name the nested schema does not itself declare, directly – seenested_packet_context().
- unpack(buffer, packet)[source]¶
Unpack field value from
bytes.- Parameters:
- Return type:
_TS- Returns:
Unpacked field value.
Notes
packetis reachable from the nested schema’s own field callbacks both under a__packet__key and, for a name the nested schema does not itself declare, directly – seenested_packet_context().
- pcapkit.corekit.fields.misc.nested_packet_context(packet)[source]¶
Build the packet context handed to a nested schema’s field callbacks.
- Parameters:
packet (
dict[str,Any]) – The enclosing schema’s own packet data.- Return type:
- Returns:
A plain
dictholding a shallow copy ofpacket’s own names, plus the reserved__packet__key bound topacketitself.
Notes
A nested schema’s field callbacks are written exactly like a top-level schema’s –
length=lambda pkt: pkt['length']– so a name the nested schema does not itself declare has to resolve to the enclosing schema’s value rather than raiseKeyError. Copying the enclosing names in is what gives that, with no lookup protocol to implement: every mapping operation isdict’s own, sopkt[key],key in pkt,get(),setdefault(),pop(),==, iteration anddict(**pkt)all behave exactly as a caller reading the code would expect, and none of them needs an override.The enclosing schema is also reachable unconditionally under the reserved
__packet__key, for a callback that needs to name the outer schema specifically rather than whichever schema happens to declare a given field – seepcapkit.corekit.schema.byteorder.packet_byteorder()andpost_process()for why that distinction matters; both hand-roll this exact fallback themselves rather than route through this function.Nothing written through the returned mapping reaches
packet, because the returned mapping is a copy: a nested schema can set – or shadow – a name also declared by the enclosing schema without the write ever touching the enclosing schema’s own data, and without the write silently disappearing either. That matters concretely rather than hypothetically:CGAExtensiondeclares its ownlengthwhile the option enclosing it declareslengthtoo, so handing a nested schema the enclosing mapping itself would let the innerlengthoverwrite the outer one mid-pack.Its being a copy rather than a live view has consequences, each deliberate and none reached by any call site in this package. A name deleted from the returned mapping is simply gone, rather than reverting to the enclosing schema’s value. And the copy is taken when this function is called, so a later mutation of
packetis not observed through it –__packet__remains bound to the live enclosing mapping for any callback that needs the current value.No dedicated class and no
ChainMap. A plaindictsatisfies everypacket: 'dict[str, Any]'annotation on the rest of the field classes natively, so nocast()is needed at the call site, and it cannot interact withABCMetaat all becausedictis not anABCMeta-based class. That also settles a suspicion, raised on #439, that aChainMaphere would corrupt theABCMetacacheSchemasubclasses shared on CPython <= 3.10. The suspicion is probably wrong, and this code cannot test it: the cache keys on the exact type queried, so asking about aChainMapinstance caches lookups forChainMapand not fordict, the poisoning observed in #439 came from ordinary code askingisinstance()about a plaindict, and #439 is fixed directly – everySchemasubclass gets its own_abc_impl. Against that, on CPython 3.10 choosing between aChainMapand a plain literal here was observed to fliptest_pcapng_remaining_constructor_branches_and_custom_dispatchbetween passing and failing, in both directions; the likeliest reading is that the choice changes which types flow through unrelatedisinstance()calls rather than causing the corruption. It does not affect correctness either way.
- class pcapkit.corekit.fields.misc.PayloadField[_TP: ProtocolBase](length=lambda _: ..., default=NO_VALUE, protocol=None, callback=lambda *_: ...)[source]¶
-
Payload value for protocol fields.
- Parameters:
length (
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 (
_TP|NoValueType|bytes) – Field default value.protocol (
Type[_TP] |str|None) – Payload protocol, as a class or as a registered protocol name; seethe property setter, through which this argument is resolved.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
- __call__(packet)[source]¶
Update field attributes.
- Parameters:
- Return type:
- Returns:
Updated field instance.
This method will return a new instance of
PayloadFieldinstead of updating the current instance.
Type Variables¶
- pcapkit.corekit.fields.misc._TS: pcapkit.protocols.schema.Schema¶
- pcapkit.corekit.fields.misc._TP: pcapkit.protocols.protocol.ProtocolBase¶
- pcapkit.corekit.fields.misc._TN: pcapkit.corekit.sentinels.NoValueType¶