Choosing Between utilities and corekit

Which of the two support packages a module belongs in. Settled by the owner on #1712, over sorting modules by packet knowledge or by who uses them: this rule can be checked from the import graph alone, and needed the fewest moves.

The page’s questions, as one decision:

        flowchart TD
    NEW["a new support module,<br/>helper, constant<br/>or ContextVar"] --> CONST{"a constant?"}
    CONST -->|"referenced beyond its<br/>protocol or family"| SHARED["corekit.const<br/>one module per topic"]
    CONST -->|"referenced by its<br/>protocol or family alone"| OWNC["the const module of its<br/>package or family base"]
    CONST -->|no| LAYER{"a schema-layer<br/>helper used<br/>outside it?"}
    LAYER -->|no| OWNED{"a helper in<br/>one protocol's<br/>own modules?"}
    LAYER -->|yes| SCHEMA["corekit.schema<br/>one module per group"]
    OWNED -->|"in its schema, used<br/>beyond its protocol<br/>or family"| SCHEMA
    OWNED -->|"elsewhere, used<br/>beyond its protocol<br/>or family"| UTIL
    OWNED -->|no| VAR{"a ContextVar?"}
    OWNED -->|"used by its protocol<br/>or family alone"| STAY["stays in its<br/>protocol or family"]
    VAR -->|"set or read by<br/>several modules"| CTX["utilities.contextvars"]
    VAR -->|no| UTIL{"usable by any<br/>code, importing<br/>no pcapkit but<br/>utilities?"}
    VAR -->|"used by one<br/>module only"| PRIV["private, in<br/>that module"]
    UTIL -->|yes| VER{"differs by<br/>interpreter<br/>version?"}
    UTIL -->|no| CORE["corekit"]
    VER -->|yes| COMPAT["utilities.compat, behind<br/>a sys.version_info gate"]
    VER -->|no| OWN["a utilities module,<br/>not compat"]
    

pcapkit.utilities is for cross-cutting services: exceptions, warnings, logging, the compatibility layer, configuration, and wrappers around third-party libraries such as chardet. Ask of a module: could any code use this service, and does it import nothing from pcapkit outside utilities? Both halves must hold, and nothing covers top-level, in-function and TYPE_CHECKING imports alike. tests/project/test_utilities_import_boundary_1735_unit.py checks every module under pcapkit/utilities/.

pcapkit.utilities.compat holds compatibility implementations and re-exports only. Where a name or behaviour differs by interpreter version, compat re-exports the right one behind a sys.version_info gate rather than a getattr or hasattr probe. Ruled in review of the threading excepthook and AppType union fixes for #1691 and #1694, whose threads do not hold it, and applied to the other probes on #1709. tests/project/test_stdlib_feature_probes_1709_unit.py fails on a getattr or hasattr call naming its attribute in a string literal, where the receiver is a builtin class or a module the file binds with an import statement, or where the attribute is one the test lists with the interpreter version that added it, unless the test exempts the call. A helper that must behave the same on every platform is neither a compatibility implementation nor a re-export, so it gets its own module: fromtimestamp() is in pcapkit.utilities.datetime, ruled in review of the timestamp fix for #2099, whose thread does not hold it.

pcapkit.utilities stays flat: a module there is named after the standard-library module it extends, as inspect and datetime are, or after its topic, as config is, and one holding a single function is fine. The standard-library name tells a reader where to look; a helpers package nesting the small modules would move their import paths without reducing their number; and merging unrelated helpers into one module would break the one module per topic that Shared Helpers asks for. Ruled by the owner in session and recorded on #2908.

pcapkit.corekit is for the types the packet model is built from: what pcapkit.protocols, pcapkit.const and pcapkit.foundation subclass, instantiate or decorate with. Ask: is this part of what a protocol or its schema is made of? A support module that fails the utilities question belongs here. pcapkit.corekit.decorators acts on a protocol’s file and falls back to protocol classes, so it moved out of utilities on #1735. corekit may import utilities; the rule forbids the reverse.

pcapkit.corekit.checksum, settled on #2580, fails the utilities question because ChecksumStatus subclasses EnumLookup. The protocols compute and verify checksums through it, so it imports pcapkit only from corekit and utilities, never from pcapkit.protocols, as tests/corekit/test_checksum_import_boundary_2580_unit.py checks. What needs IPv4 and IPv6 stays beside IP_Pseudo. The package has a module per checksum family, so a new family sits beside internet.

Shared Schema Helpers

A helper that code outside the schema layer calls lives in pcapkit.corekit.schema, one module per group, rather than in the module that first needed it; the schema layer is pcapkit.corekit.fields and pcapkit.protocols.schema. It keeps no re-export at its old path. Ruled by the owner in session on #2674, grouping these helpers in one package rather than placing each by the questions above. So pad_length() is in pcapkit.corekit.schema.padding, and keep_short_read() and replay_short_read() in pcapkit.corekit.schema.shortread. The header schema framework stays pcapkit.protocols.schema and its Schema; pcapkit.corekit.schema holds only the helpers shared across the layer.

A capture cut belongs to the built-in short read of Schema. Schema.unpack records the field the data ends inside, through _note_short_read, as __short_read__; _cut_short_read() packs back only the octets read; and keep_short_read() and replay_short_read() carry the record through info and cut a rebuild back by it. A protocol reads and rebuilds a cut header through them, never through an unpack override of its own or a length callable clamped to the octets present, which hides the cut from the record. Ruled in review of #2682 (#2680), and applied to option areas on #2694.

A rebuild is read at the width its parse read the header at. keep_short_read() records that width beside the record, as __short_width__, and Protocol.from_data reads the rebuild at it, so it holds the values and the next layer its parse did. Ruled by the owner on #2761, over a clamp inside IPv6_Ext, whose overrunning length showed the gap.

A field callback tells a zero the cut left from a zero read through the same record: Schema.unpack passes it to the fields after the one the data ends inside, as __short_read__ in their packet. A callback that would refuse such a zero leaves it to the short read, and refuses a read value with a ProtocolError, as the Opt Data Len helpers of pcapkit.protocols.internet.ipv6_ext.schema do (#2727).

On its own, the end of the data is a cut; inside a frame, the frame’s lengths say where the message ends. A layer parsed with no layer before it, as frame_depth tells, is given the octets captured, so Protocol.unpack unpacks its header with __data_end__. Past the cut, Schema.unpack then warns nothing of __length__ falling below zero and resolves no length below zero from the zeros the cut left, so those fields read nothing and the short read records the cut. Settled on #2880, completing #2848. Inside a frame a field running past the length the enclosing layer declares overruns it and warns, and the frame keeps a header its octets cannot hold as Raw.

A protocol’s own check follows the same rule through cut_on_its_own(). On its own, a version or length the cut left as zero, or a header length the data ends before, is the short read’s, as such a zero is for a field callback (#2727). fill_short_options() rebuilds that header length, which #1404 refused because the rebuild filled the zeros in. A check the cut leaves nothing to decide by still refuses: the PCAP magic number, IGMP’s classification by length, NGAP’s PER decoding, and the TLS, DTLS and HTTP/2 framing.

A helper in one protocol’s own schema module moves there too once code outside that protocol or its family uses it, so other implementors can find it; a helper only its own protocol calls stays beside the schema. Ruled on #2674, and widened to the rest of a protocol’s own modules on #2897 (Shared Helpers). A protocol’s own modules are its protocol, data, schema and const modules. So jumbo_payload_length(), which the IPv6 pseudo-header and the Scapy toolkit read as well as IPv6 itself, is in pcapkit.corekit.schema.jumbogram, while jumbogram_error(), which only IPv6 calls, stays in pcapkit.protocols.internet.ipv6.schema. The IPv6 option helpers in pcapkit.protocols.internet.ipv6_ext.schema belong to the HOPOPT and IPv6-Opts headers that share them, and stay there too.

A header’s length converts to and from a field counting 8-octet units not including the first 8 octets [RFC 6564 Section 4] through pcapkit.corekit.schema.extlen, MH’s and HIP’s included (#2854). AH counts 4-octet units with a bias of 2 [RFC 4302 Section 2.2], so it keeps its own arithmetic. tests/project/test_ext_len_arithmetic_2854_unit.py fails on (field + 1) * 8, split across a call or not, or its inverse written out anywhere else in pcapkit.

The field modules and the protocols import these helpers, so a helper module imports neither, nor pcapkit.foundation, outside an if TYPE_CHECKING: block. The package’s __init__.py imports none of its modules, so a field module loads only the group it asks for. tests/project/test_schema_helpers_boundary_2674_unit.py fails on a helper module that does, on a module outside the layer that refers to a function defined in a field module or in pcapkit.protocols.schema, and on a module outside a protocol that refers to a function defined in that protocol’s schema module, by import or as an attribute of the module.

Shared Constants

A constant the format or the RFCs fix that anything outside its own protocol references lives in pcapkit.corekit.const, in the module for its topic, not in the protocol that first needed it. Ruled by the owner on #2883, over a constants module beside each protocol’s schema, which adds a fourth module kind and keeps the cross-layer imports. The package of topic modules, beside pcapkit.corekit.schema since most of its readers are not schema code, was ruled in review of #2895 (#2883), over topic modules among the schema helpers and over one module holding every topic. pcapkit.const is no home, since Const-Vendor Pairing gives each of its modules a crawler; it holds the enumerations generated from the vendor registries.

What counts as a reference was ruled in review of #2895 (#2883) too. Any module counts – another protocol, the engines, the dumpers, IP reassembly and the toolkit alike – but a protocol’s own protocol, schema, data and const modules are one place, and so is a family. So a family base’s constant that its members reference stays with the family, as the IPv6 option helpers do (#2674): TLS and DTLS read SSL’s record content types, and HTTP/1 and HTTP/2 the HTTP base’s patterns.

Every layer imports these modules, so they import nothing from pcapkit but pcapkit.const outside an if TYPE_CHECKING: block, and the package’s __init__.py imports none of them. tests/project/test_shared_format_constants_2883_unit.py fails on a module there that does, and on a constant of a protocol’s own modules that a module outside that protocol or its family references, by import or as an attribute of the module.

Every other constant of a protocol’s own modules lives in its package’s const module, whichever of its protocol, schema and data modules reads it, and one a family shares in the family base’s: SSL’s for TLS and DTLS, HTTP’s for HTTP/1 and HTTP/2. Ruled by the owner in review of #2895 (#2883) and recorded on #2896. So GLOBAL_HEADER_LEN, which only the PCAP global header reads, is in pcapkit.protocols.misc.pcap.header.const.

A constant is a module-level name in capitals, such as LIMIT, _LIMIT or each name of a tuple bound at once, whose value is fixed when its module loads: a number, a string, a compiled pattern, an enumeration member, or a table of them. These are not constants, and stay beside their readers:

  • a ModuleDescriptor, a lazy import;

  • run-time state: a name a function rebinds under global, as _CRYPTO is, or a lock;

  • a value built from pcapkit beyond the registries and the constants – a field, a schema or a class of its own module – as CIPHER_SUITES is, which a const module could not import without importing its protocol back.

The modules that read a constant import it, so it stays reachable through them and needs no versionremoved (#2896); a released path whose module no longer reads it records one, as pcapkit.protocols.application.ssl.protocol.TLS13_VERSION does. Those modules import a const module, so it imports nothing from pcapkit but pcapkit.const, pcapkit.corekit.const and another const module outside an if TYPE_CHECKING: block, defines no function or class, and its __all__ lists its public constants. The same test fails on a constant a protocol, schema or data module binds, and on a const module that binds anything else or imports beyond these.

Shared Helpers

A function or class of a protocol’s own modules that anything outside its protocol or family references lives in a common home: the module of pcapkit.utilities or pcapkit.corekit for its topic, as the questions above place it, and never a generic helpers module. Ruled by the owner in review of #2895 (#2883) and recorded on #2897. References and places count as for the constants, and so does a ModuleDescriptor naming the helper, as a lazy import does. So family_byteorder(), which the DPKT engine reads as well as Loopback, is among the byte-order helpers of pcapkit.corekit.schema.byteorder: it reads LinkType, so fails the utilities question. The next-layer and link-address checks that LinuxSLL1 and LinuxSLL2 share stay in their family base’s pcapkit.protocols.link.linux_sll.protocol.

A protocol’s own structures are not helpers: the dissector, header schema and data model classes its modules hold (Protocol Family Grouping), every subclass of ProtocolBase, Schema or Info there, stay in its package whoever references them, as the framework bases do. A field class, an enumeration or a context its modules define is a helper.

The IP pseudo-header’s functions stay beside it. pseudo_header(), compute_upper() and verify_upper(), which the IP layers and the layers above them reach through a ModuleDescriptor, act on IP_Pseudo and through it on IPv4 and IPv6, which is what #2580 keeps in pcapkit.protocols.misc.checksum.

tests/project/test_shared_format_constants_2883_unit.py fails on a helper a module outside its protocol or family references, the pseudo-header functions aside, and imports a class to tell a structure from a helper.

Context Variables

Where a ContextVar lives depends on which modules use it. Settled by the owner on #2616:

  • One that more than one module sets or reads lives in pcapkit.utilities.contextvars, under a public name in its __all__. No other module re-exports it, so every reader imports it from there.

  • One that only its own module uses stays in that module, private, as _exthdr_rebuilt does in pcapkit.protocols.internet.internet.

A shared variable kept in one of its users makes the others import a private name and ties them to that module’s place in the import graph. A module of bare declarations importing nothing from pcapkit passes the utilities question, so any module may import it – pcapkit.corekit.checksum included, which must not reach pcapkit.protocols. tests/project/test_shared_contextvars_2616_unit.py fails on a module that takes a context variable from anywhere else, by name or as an attribute of an imported module, and on a variable there that is private, missing from __all__, or read by one module alone.