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:
field (
NumberField) – Field instance.
Notes
byteorderis the key a caller has to seed, andpacket_byteorder()is what reads it.Frame.packandFrame.unpackboth write it from the global header’s magic number. A PCAP-NG Section Header Block writes it from its own Byte-Order Magic (seeshb_byteorder_callback()), and the blocks, options and records of that section read it, nested or not. The fallback tosys.byteorderis 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. Seetests/protocols/misc/pcap/test_frame_endian_runtime.pyfor 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
packetbelongs to.A nested schema is handed its parent’s packet data under a
__packet__key (seeSchemaField.pack), so the byte order may live one level up.
- pcapkit.corekit.schema.byteorder.family_byteorder(family, linktype=None)[source]¶
Byte order a loopback header’s address family was written in.
- Parameters:
- Return type:
Literal['big','little']- Returns:
'big'forLINKTYPE_LOOP(108), whose family is in network byte order. Otherwise, forLINKTYPE_NULL(0),'big'if the octets read as a smaller number big-endian than little-endian, else'little'.
Note
A
LINKTYPE_NULLfamily is in the byte order of the host that captured the packet, which the capture file does not record. Every address family is below0x10000, 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. ALINKTYPE_LOOPfamily 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).