Short-Read Replay

pcapkit.corekit.schema.shortread carries a header cut short by the end of the capture through to the protocol’s info, by which a protocol rebuilt from that info is cut back to the octets its header had (#1458), for the layer base classes and the PCAP global header alike, and tells a protocol’s checks of a header parsed on its own where the cut fell (#2880). It names the protocol and schema classes for type checking only, since those import it.

Added in version 1.6.0: Moved here from pcapkit.protocols.schema.schema (#2674).

pcapkit.corekit.schema.shortread.keep_short_read(protocol, length=None)[source]

Carry a parsed header’s short read into the protocol’s info.

Parameters:
  • protocol (ProtocolBase) – Protocol instance, just parsed.

  • length (int | None) – Length of data the parse was given, None for the octets it holds.

Return type:

None

Called by the layer base classes, and by the PCAP global Header, once a parse is done. When the data ended inside one of the header’s fields, info gains the __short_read__ record Schema.unpack made, so that Protocol.from_data hands it to the rebuild, whose Protocol.pack cuts the header back to what was captured before it is read (#1458, #2703). Beside it, __short_width__ records the length the parse read the header at, which the rebuild is read at in turn, so that it holds the values its parse read (#2761). Both keys are listed in Data.__included__, so Info.to_dict carries them and from_data(info.to_dict()) cuts its rebuild back too; they are absent for a header that was captured whole.

Where the capture cut the frame short, as frame_length gives its lengths, a layer whose message runs to the end of the frame records the cut too, on the field it runs to the end in, through Schema._note_short_read, so its rebuild reads as cut, as the layer did inside the frame (#2711). A layer whose own header declares where its message ends records none, whether the cut falls at that end or past it; see Schema._frame_end_field.

pcapkit.corekit.schema.shortread.replay_short_read(protocol, data)[source]

Hold a protocol rebuilt by from_data to the octets its header had.

Parameters:
  • protocol (_PB) – Protocol instance, just built by from_data.

  • data (Any) – The info it was built from.

Return type:

_PB

Returns:

protocol itself.

The layer base classes and the PCAP global Header call it on what Protocol.from_data returns, which is cut back before it gets here: from_data hands the construction the __short_read__ record that keep_short_read() left in data, Protocol.pack cuts the header it makes back to the octets captured through Schema._cut_short_read, and Protocol.__post_init__ has read() parse those octets at the width __short_width__ records, the one its parse read them at, so the rebuild is read and verified as its capture was (#2703, #2761).

The record names the field the capture ended inside and how much of it there was. Everything in the rebuild from that point on is dropped, which on a rebuild cut back as above is nothing, and the header and the info keep the record (#1458). A header cut short has no payload past it, so that point ends the rebuilt data, and what the protocol writes ahead of the header is kept: the HTTP/2 connection preface HTTP.pack writes (#2665). This holds while pack writes nothing after the header schema, as no protocol rebuilt here does; a trailer would keep as many of the cut octets.

pcapkit.corekit.schema.shortread.hoist_short_read(schema, name, parts)[source]

Record on schema a short read only the schemas in its field name recorded.

Parameters:
  • schema (Schema) – The header just parsed.

  • name (str) – Its field holding parts.

  • parts (Iterable[Schema | None]) – The schemas read in that field, None for one absent.

Each of parts must read to the end of the data, so a field there that reads short has reached it, and the record names name with every octet of it read. Elsewhere a nested record need not mean that: hoisting one in keep_short_read() marks a complete TCP segment with a malformed MPTCP DSS option as cut (#2660). IGMPv3 and ICMPv6 hoist the record of a message body counting records the data ends before.

Added in version 1.7.0.

pcapkit.corekit.schema.shortread.cut_on_its_own(header, name=None, end=None)[source]

Tell a check of a header parsed on its own whether the data ended before what it reads.

Parameters:
  • header (Schema) – The header just parsed.

  • name (str | None) – Its field the check reads; None for the whole header.

  • end (int | None) – How many octets of name, from its start, the check reads; None for all of them.

Return type:

bool

Returns:

Whether header was parsed with no layer before it, as frame_depth tells, and the short read it records ends the data before those octets of name, or anywhere in it without a name.

On its own the end of the data is a cut (#2880), so a value past it is a zero the cut left, not one read. A check that would refuse such a value leaves it to the short read, as a field callback does (#2727), and so does one refusing a header length the data ends inside, refused on #1404 because the rebuild filled the zeros in, which the short read now cuts back. Inside a frame its lengths say where the message ends, so such a check refuses as before.

Added in version 1.7.0.

pcapkit.corekit.schema.shortread.fill_short_options(data, options, size, zero, padding=None)[source]

Fill out an options area a short read ended inside, so a rebuild writes its length.

Parameters:
  • data (Any) – The data model a protocol is rebuilt from.

  • options (Any) – Its options, keyed by code, as the protocol’s read gives them, or None for none.

  • size (int) – Octets its header length gives the options area.

  • zero (tuple[Any, Any]) – The code and data model of the one-octet option a zero octet reads as.

  • padding (bytes | None) – Octets it records after an End of Option List, or None where the protocol records none.

Return type:

tuple[Any, bytes | None]

Returns:

options and padding to size octets where data records a short read: past size, trailing zero options dropped; short of it, filled out with zero octets, after padding where the options end with zero, else as that many zero options.

A header length the data ends before is read as parsed on its own (cut_on_its_own()), but make writes the length of the options it is given, which past the cut are a cut option’s whole length and the end of the list its zeros read as. Whatever is filled or dropped lies past the cut, so the rebuild’s cut back leaves the octets and the header length its parse read (#2880).

Added in version 1.7.0.

pcapkit.corekit.schema.shortread.ends_inside_option(options, count)[source]

Tell whether the first count octets of an options area end inside one of its options.

Parameters:
  • options (Any) – The options read from the area, keyed by code, each with the length it declares.

  • count (int) – Octets of the area the data holds.

Return type:

bool

Returns:

Whether an option starts before count octets and declares more than them.

Such an option rebuilds by the values it holds rather than by the length it declares, so neither its own length nor the header length before it would rebuild as parsed: a header the data ends inside an option of stays refused on its own too (#1404, #2880).

Added in version 1.7.0.