Byte-Order Helpers

pcapkit.corekit.schema.byteorder reads the byte order a capture file or section declares out of the packet data: byteorder_callback() sets it on a NumberField for the record header of a classic PCAP file (schema) and the blocks of a PCAP-NG file (schema), and packet_byteorder() is what the callback and the PCAP-NG makers (pcapkit.protocols.misc.pcapng.protocol) read it with. family_byteorder() reads the byte order a BSD loopback header’s address family was written in, for Loopback and the DPKT engine (pcapkit.foundation.engines.dpkt) alike.

Added in version 1.6.0: Moved here from pcapkit.corekit.fields.byteorder, with both its helpers (#2674).

Removed in version 1.6.0: pcapkit.corekit.fields.byteorder, with no alias (#2674).

pcapkit.corekit.schema.byteorder.byteorder_callback(field, packet)[source]

Update byte order of a PCAP or PCAP-NG file.

Parameters:

Notes

byteorder is the key a caller has to seed, and packet_byteorder() is what reads it. Frame.pack and Frame.unpack both write it from the global header’s magic number. A PCAP-NG Section Header Block writes it from its own Byte-Order Magic (see shb_byteorder_callback()), and the blocks, options and records of that section read it, nested or not. The fallback to sys.byteorder is for a schema packed or unpacked on its own, with no header to ask. That also means a misspelled key looks exactly like an absent one and reports nothing: every field would be read in the host’s order rather than the file’s, which is right by coincidence on a little-endian capture and byte-swapped on a big-endian one. See tests/protocols/misc/pcap/test_frame_endian_runtime.py for the fixtures that take the other side of the branch.

pcapkit.corekit.schema.byteorder.packet_byteorder(packet)[source]

Byte order declared for the file or section that packet belongs to.

A nested schema is handed its parent’s packet data under a __packet__ key (see SchemaField.pack), so the byte order may live one level up.

Parameters:

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

Return type:

Literal['big', 'little']

Returns:

Byte order of the enclosing file or section, falling back to the host byte order when the packet data declares none.

pcapkit.corekit.schema.byteorder.family_byteorder(family, linktype=None)[source]

Byte order a loopback header’s address family was written in.

Parameters:
  • family (bytes) – The 4 octets of the address family.

  • linktype (int | None) – Link type the frame was captured under; None reads as LINKTYPE_NULL.

Return type:

Literal['big', 'little']

Returns:

'big' for LINKTYPE_LOOP (108), whose family is in network byte order. Otherwise, for LINKTYPE_NULL (0), 'big' if the octets read as a smaller number big-endian than little-endian, else 'little'.

Note

A LINKTYPE_NULL family is in the byte order of the host that captured the packet, which the capture file does not record. Every address family is below 0x10000, so its zero octets lie at the high end, and the smaller of the two readings is the one in the writer’s byte order. A family that reads the same both ways, such as 0, packs to the same octets either way. A LINKTYPE_LOOP family is never inferred, so one written little-endian reads as a family no table holds.

Added in version 1.7.0: Moved here from pcapkit.protocols.link.loopback.protocol (#2897).