Reassembly Integration

Where a new reassembly engine under pcapkit.foundation.reassembly plugs into the package. Points 1 to 3 were ruled on the DTLS reassembly work in review of #2451 (#2440), whose issue does not hold them. Point 4 rests on #2469: reviewing how DTLS sizes a handshake message’s buffer, the owner had DTLS reassembly follow its RFCs. No issue holds that ruling.

The Rule

  1. A protocol class does not handle or integrate reassembly itself. The dissector stays a parser and does not reach into pcapkit.foundation.reassembly. A public method that parses a reassembled unit from its own octets is still a parser, so a protocol class may offer one (#2496).

  2. A new engine needs a new pcapkit.toolkit helper that declares which output engines (dpkt, pyshark, scapy, the default) support it.

  3. A new engine’s reassembled outcome needs a new pcapkit.foundation.analysis method that parses it into a usable result, rather than leaving the caller raw reassembled bytes. The package mirrors pcapkit.foundation.reassembly with a module per engine, whose public names the package, pcapkit.foundation and pcapkit.all export (#2496).

  4. Reassembly always follows the RFC’s own definition, never an algorithm this package invents. Where the RFC requires no algorithm – buffer the whole message, accept overlaps – the engine adds no timeout, cap or ordering rule of its own.

TCP’s gap cap is a safety limit, not a reassembly rule. No RFC calls for TCP_GAP_LIMIT, the widest gap TCP reassembly zero-fills to merge a segment. It bounds zero-filled memory, as MAX_ZERO_PAD_LENGTH does for a field, and -1 lifts it for the uncapped reassembly point 4 asks for. Ruled on #2546.

Every callback runs, and the first exception is re-raised, for reassembly and flow-tracing callbacks alike. One raising an Exception does not stop the ones after it: the first such error propagates once the last has run, and a later one is logged at debug level. Any other BaseException, such as KeyboardInterrupt, stops them at once. Both run them through run_callbacks(). Ruled in review of #2875 (#2855).

Package Layout

Each reassembly and flow tracing class has a package of its own, named after it, laid out as a protocol’s is (Protocol Family Grouping). Ruled on #2882:

tests/foundation/test_foundation_packages_2882_unit.py fails on a package out of this layout.

Parse Ownership

The protocol class owns the parse of a reassembled outcome, and pcapkit.foundation.analysis is a public layer over it. Ruled on #2622:

  • The parse is a method of the protocol class: Protocol.analyze for IPv4 and IPv6, Transport.analyze for TCP, and DTLS.analyze_handshake for a DTLS handshake message.

  • Each pcapkit.foundation.analysis function is a public wrapper that calls that method and holds no parsing of its own.

  • The reassembly Deferred calls the same method, on the reassembly’s own __protocol_type__. For IP and TCP it passes the stops and context the extraction’s engine parses its frames with: the default engine’s, and none from a third-party engine, whose frames ignore them (#2933). ip_datagram() and tcp_datagram() take them as keyword-only layer, protocol and context, so given the same they return that packet (#2947). The reassembler a TCP flow tracer builds for analyze takes them the same way (#2946). For IP both also hand a protocol class’s analyze the datagram’s addresses and IP version, as a frame’s IP layer hands its next layer, so its payload is dispatched as a frame’s is (#2945).

  • Protocol code never imports pcapkit.foundation to do its own work; the dependency runs from foundation to the protocols. The protocol base’s import from pcapkit.foundation.registry.protocols registers a class rather than parsing. tests/project/test_protocols_import_boundary_2622_unit.py fails on any other import of pcapkit.foundation under pcapkit/protocols/ outside a TYPE_CHECKING block, the rest of pcapkit.foundation.registry included.

        flowchart TD
    ENG["pcapkit.foundation.engines<br/>an already-parsed frame"]
    TK["the pcapkit.toolkit.&lt;engine&gt; adapter the engine's<br/>\_\_reassembly\_\_ names, e.g. toolkit.pcap.dtls\_reassembly"]
    RE["each enabled reassembler the Extractor holds,<br/>by its registered name, advanced to the frame's timestamp"]
    DEF["Deferred: a datagram's packet,<br/>parsed on first read"]
    AN["pcapkit.foundation.analysis<br/>ip\_datagram, tcp\_datagram, dtls\_handshake<br/>public wrappers, no parsing"]
    subgraph protocols ["pcapkit.protocols"]
        PM["the protocol class's parse method<br/>Protocol.analyze: IPv4, IPv6<br/>Transport.analyze: TCP<br/>DTLS.analyze\_handshake"]
    end
    REG["pcapkit.foundation.registry.protocols"]

    ENG --> TK --> RE --> DEF
    DEF -->|"on the reassembly's \_\_protocol\_type\_\_"| PM
    AN --> PM
    protocols -.->|"registers a class, nothing else"| REG
    

No edge leaves pcapkit.protocols for the reassembly or the analysis layer; tests/project/test_protocols_import_boundary_2622_unit.py fails on one.

Compliance, Measured

Point 1 holds for every engine. None of IPv4, IPv6, TCP and DTLS imports or calls into pcapkit.foundation.reassembly. Instead pcapkit.foundation.engines calls a pcapkit.toolkit.<engine> extraction function on an already-parsed frame and feeds the result to the reassembly instance the Extractor holds; for DTLS that function is pcapkit.toolkit.pcap.dtls_reassembly() (#2496). Every enabled reassembler is reached by the name it is registered under in Extractor.__reassembly__, through Extractor.make_reassembly and the engine’s adapter for that name, and each is advanced to every frame’s timestamp before the frame is fed (#2814). DTLS.read_handshake and DTLS.analyze_handshake are the parser methods the rule allows.

Point 2 holds through the engines, not through one pcapkit.toolkit helper. Each engine’s EngineBase.__reassembly__ maps every reassembler it supports to the adapter that feeds it: a toolkit one it ships, or one registered with EngineBase.register_reassembly. So the maps together declare which engines support a reassembler, and registering an adapter on an engine makes it support one. An enabled reassembler with no entry is disabled with an AttributeWarning, and an engine with no entries at all, as pcapkit.foundation.engines.pyshark, disables reassembly (#2814). DTLS has entries on the default engine alone.

Flow tracing takes the same shape. Every enabled flow tracer is made by the name it is registered under in Extractor.__traceflow__, through Extractor.make_traceflow, and finished at the end of the capture. Each engine’s EngineBase.__traceflow__ maps every flow tracer it supports to its toolkit adapter, written by EngineBase.register_traceflow. An enabled tracer with no entry is dropped with an AttributeWarning, and an engine with no entries, as pcapkit.foundation.engines.pypcap and pcapkit.foundation.engines.pcap_ct, disables flow tracing (#2843).

Every engine feeds what its maps hold. Each one’s read_frame calls EngineBase._reassemble and EngineBase._traceflow, so an adapter registered on an engine that ships none, as PyShark, PyPCAP and pcap-ct do for reassembly, is fed rather than refused: registering is how an engine gains support (#2893). Each engine declares both maps itself, empty or not, and documents there how it calls an adapter. tests/foundation/engines/test_reassembly_feed_2893_unit.py fails on an engine whose read_frame leaves either uncalled, or that inherits either map rather than declaring it.

Point 3 holds for every engine. ip_datagram() parses a reassembled IPv4 or IPv6 datagram’s payload by its payload protocol type, through Protocol.analyze; tcp_datagram() parses a reassembled TCP datagram’s by its ports, through Transport.analyze; and dtls_handshake() decodes a reassembled DTLS handshake message (#2496), through DTLS.analyze_handshake (#2622). IP and TCP have analysis modules of their own, each function named for the datagram its engine reassembles, by the owner’s choice on #2581; without them the rule held for DTLS alone.

Where the rule is silent, DTLS takes the IP/TCP shape (#2531). IPv4, IPv6, TCP and DTLS reassembly each set __protocol_type__ and report the parsed outcome as a datagram’s packet, deferred to first read by Deferred. Each Deferred runs the parse behind its engine’s analysis function, so a packet is what that function returns, and binds it to __protocol_type__ – analyze for IP and TCP (#2581), analyze_handshake for DTLS (#2622) – so a subclass naming another protocol has its packet parsed by that one. They differ on an incomplete outcome: IP and TCP parse a contiguous payload, complete or not, as strict=False reports one, and hold None only for the received runs strict=True reports; DTLS holds None for any incomplete message.