Container Fields¶
Generic List Fields¶
- class pcapkit.corekit.fields.collections.ListField[_TL: (Schema, FieldBase, bytes)](length=lambda _: ..., item_type=None, callback=lambda *_: ..., count=-1)[source]¶
Bases:
FieldBase[list[_TL]],Generic[_TL]Field list 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.item_type (
FieldBase|None) – Field type of the contained items.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.count (
int|Callable[[dict[str,Any]],int]) – Number of items a field before this one counts, for a list running to the end of the data; if a callable is given, it should return an integer value and accept the current packet as its only argument. A negative value, the default, counts none.
This field is used to represent a list of fields, as in the case of lists of constant-length-field items in a protocol.
A list whose octets hold fewer items than
countended with the data before the rest, and reports them asshortfall, so thatSchema.unpackrecords the cut; the items absent are not made up. A schema item it counts is read with__data_end__in its packet, since its octets end where the data does, so an item the data ends inside records the cut as its short read rather than warn that its fields run past the length it was given.Changed in version 1.6.0: Takes
count(#2711), and reads a schema item it counts as ending with the data (#2749).- __call__(packet)[source]¶
Update field attributes.
- Parameters:
- Return type:
- Returns:
Updated field instance.
This method will return a new instance of
ListFieldinstead of updating the current instance.
- pack(value, packet)[source]¶
Pack field value into
bytes.- Parameters:
- Return type:
- Returns:
Packed field value.
Without an
item_type,unpack()returns the field’s octets as a singlebytes, so such a value is written as-is rather than iterated item by item.
- unpack(buffer, packet)[source]¶
Unpack field value from
bytes.- Parameters:
- Return type:
- Returns:
Unpacked field value.
- Raises:
FieldValueError – If the items overrun the field, if a schema item consumes nothing from
buffer, or if schema items short of the field’s length cannot be zero-filled.
When
bufferfalls short of the field’s length, the item it ends inside is read as though its missing octets were zeros, asFieldBase.unpackreads any other field, and the list ends with that item: none is read from past the data, which holds no octet of it. A__data_present__key inpacketsays thatbufferholds the data that many octets in, and past them the zeros an enclosing list of schema items was filled out with, asSchema.unpacktells the fields of the item the data ends inside; the list then ends with the data there.A
__zero_tail__key inpacket, true, says instead that the octets pastbufferare zeros of the data, as radiotap.org reads the octets a producer leaves off a TLV item (#2655), so the list reads its items on through them to its length.Changed in version 1.6.0: A list the data ends inside ends with the data, the item it ends inside read with its missing octets as zeros, where a schema item past the data was refused as consuming nothing and a plain value past it was read as zeros, and so does a list in an item the data ends inside (#2686, #2756, #2760).
- _unpack_items(file, packet, is_schema, present=-1)[source]¶
Unpack the field’s items from
file.- Parameters:
- Return type:
list[_TL]- Returns:
Unpacked items.
- Raises:
FieldValueError – If the items overrun the field, or if a schema item consumes nothing from
file– see the note below.
Option List Fields¶
- class pcapkit.corekit.fields.collections.OptionField[_TS: Schema](length=lambda _: ..., base_schema=None, type_name='type', registry=None, eool=None, callback=lambda *_: ..., option_length=None)[source]¶
Bases:
ListField,Generic[_TS]Field list for protocol options.
- 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.base_schema (
Type[_TS] |None) – Base schema for option fields.type_name (
str) – Name of the option type field.registry (
defaultdict[int|IntEnum|aenum.IntEnum,Type[_TS]] |None) – Option registry, as in a mapping from option types (enumeration values) to option schemas, with the default value being the unknown option schema.eool (
int|IntEnum|aenum.IntEnum|None) – Enumeration of the EOOL (end-of-option-list, or equivalent) optioncallback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.option_length (
Callable[[_TS],int] |None) – Octets an option takes as its Length declares them, given the option unpacked, or a negative value for one declaring none;Nonetakes the octets its fields read.
This field is used to represent a list of fields, as in the case of lists of options and/or parameters in a protocol.
Given
option_length, each option ends where its Length says, whatever its fields read, so the options after one whose Length disagrees with its fields are read where they begin (#3236). Octets the Length declares past the fields are kept as the option’s__remainder__, at the__remainder_offset__they begin at, asSchema.to_dictdescribes, andpack()writes them back. A Length ending inside the fields is read as a short read, whichSchema.unpackrecords, so the option packs back the octets it declares alone. Either way the option breaks its format, whichnonconforming()reports, unless_accepts_length_mismatch()refuses such an option outright.Note
self.unpackselects an option’s schema by reading thetype_namefield ofbase_schemaalone off the front of the option, instead of unpacking the whole base schema and keeping only that one value. That is only the same read when the following hold of the type field:it is the base schema’s first field, so that it is what sits at the front of the option;
it is a
NumberField, so thatSchema.unpackreads it through its ordinary per-field branch;its length is a fixed integer rather than a callable, so that its width does not depend on packet data the shortcut has not read.
All of that is true of every
OptionFielddeclared in this package. A base schema registered from outside it – cf.pcapkit.foundation.registry– need not satisfy it, and is not rejected: such a base schema is unpacked in full. It parses correctly and pays the cost of the second parse. Only the shortcut is withheld, so a base schema whose type field comes second cannot be silently misread.Changed in version 1.10.0: Takes
option_length(#3236).- property option_length: Callable[[_TS], int] | None¶
Octets an option takes as its Length declares them, if given.
Added in version 1.10.0.
- pack(value, packet)[source]¶
Pack field value into
bytes.- Parameters:
- Return type:
- Returns:
Packed field value.
Given
option_length, an option holding a__remainder__, octets its Length declares past its fields, is written with them after it, while it packs to the__remainder_offset__they were read at, as a nested schema’s remainder is (#3236).Added in version 1.10.0.
- unpack(buffer, packet)[source]¶
Unpack field value from
bytes.- Parameters:
- Return type:
list[_TS]- Returns:
Unpacked field value.
Important
If the option list ended before the specified size limit,
self.option_paddingis set to the remaining length, whichSchema.unpackrecords inpacketas__option_padding__so that the next fields can be aware of it.A
__capture_cut__key inpacket, false, says that the data ends at the length the enclosing layer declares for it, so that an area the data ends inside runs past that length rather than being cut short by the capture, asIPv6tells the extension headers it walks (#2694). Absent, the data may end at a cut.- Raises:
FieldValueError – If an option consumes nothing from
buffer, since the loop below has then no way to get past it.ProtocolError – If an option that begins with octets present reads past the end of the data, unless the data ends inside the area, the option’s fields fit it and
_accepts_cut()keeps it, or_accepts_short_read()accepts it. Or if the area runs past the end of the data wherepacketgives__capture_cut__as false, the data ending at the length the enclosing layer declares rather than at a cut. Or, givenoption_length, as_read_by_length()refuses an option’s Length.
- Warns:
ProtocolWarning– Givenoption_length, if an option’s Length disagrees with its fields, as_read_by_length()reports.
Changed in version 1.5.0: An option that begins with octets present and reads past the end of the data raises
ProtocolError, where it was parsed from zero-filled octets (#1325).Changed in version 1.6.0: Where the data ends inside the area, as a capture cut short leaves it, the list ends with the data, and an option it ends inside is kept if its fields fit the area, so
Schema.unpackrecords the cut as a short read rather than either error being raised (#2694). See_accepts_cut(), and__capture_cut__above.Changed in version 1.10.0: Given
option_length, each option ends where its Length says, rather than where its fields end (#3236).
- _read_by_length(file, option, schema, packet, offset, length, where)[source]¶
Take the octets an option’s Length declares, rather than those its fields read.
- Parameters:
file (
_ShortReadTracker) – Stream the option was read from, just past its fields.option (
_TS) – Option just unpacked.schema (
Type[_TS]) – Schema it was unpacked with.offset (
int) – Stream position the option begins at.length (
int) – Octets of the area left from there.where (
str) – The option’s code and offset, for a message.
- Return type:
_TS- Returns:
option, holding the octets its Length declares past its fields as__remainder__, or read again up to the end its Length declares inside them, as a short read. Where the data ends inside the fields, only a Length ending inside the octets read, past the start of the field the data ends inside, is taken: a Length the cut leaves as zero, or one past the cut, is the cut’s, whichunpack()handles (#2727).- Raises:
ProtocolError – If the Length declares no octet, more than the area has left, or an end inside the fields declaring it, since nothing past the option can then be read where it begins. If it disagrees with the fields: where
_accepts_length_mismatch()refuses the option, or, throughnonconforming(), on a construction.- Warns:
ProtocolWarning– If the Length disagrees with the fields, throughnonconforming().
Added in version 1.10.0.
- _accepts_short_read(option)[source]¶
Whether an option that read past the end of the data may stand.
- Parameters:
option (
_TS) – Option just unpacked, which read short and ran past its area or past the data at the end of the area.- Return type:
- Returns:
False, so thatunpack()refuses the option (#1325): padding cut short may be content the option’s own length declares, as aPadNoption’s is. A list whose items’ lengths leave out their trailing padding may override this, asPaddedOptionFielddoes;Schema.packthen writes back only the octets read.
- _accepts_cut(option)[source]¶
Whether an option the data ends inside, inside its area, may stand.
- Parameters:
option (
_TS) – Option just unpacked, which read short where the data ends short of the area declared, and whose fields fit the area.- Return type:
- Returns:
True, so thatunpack()keeps the option, as the short readSchema.unpackrecords (#2694). A list that keeps such an area as the octets captured instead may override this, asOptionAreaFielddoes.
- _accepts_length_mismatch(option)[source]¶
Whether an option whose Length disagrees with its fields is read on.
- Parameters:
option (
_TS) – Option just unpacked, whose Length, givenoption_length, runs past its fields or ends inside them.- Return type:
- Returns:
True, so that_read_by_length()keeps the surplus or reads the option short, and reports it (#3236). A list that keeps such an area as the octets captured instead may override this, asOptionAreaFielddoes.
Added in version 1.10.0.
Type Variables¶
- pcapkit.corekit.fields.collections._TL: pcapkit.protocols.schema.Schema | pcapkit.corekit.fields.field.FieldBase | bytes¶
- pcapkit.corekit.fields.collections._TS: pcapkit.protocols.schema.Schema¶