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¶
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).A new engine needs a new
pcapkit.toolkithelper that declares which output engines (dpkt,pyshark,scapy, the default) support it.A new engine’s reassembled outcome needs a new
pcapkit.foundation.analysismethod that parses it into a usable result, rather than leaving the caller raw reassembled bytes. The package mirrorspcapkit.foundation.reassemblywith a module per engine, whose public names the package,pcapkit.foundationandpcapkit.allexport (#2496).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:
Its
reassemblyortraceflowmodule holds the class, anddataits data models where it has its own.Its
__init__exports the class alone, and binds each classdatadefines asData_<name>outside__all__, aspcapkit.foundation.reassembly.tcpdoes.The IP base’s package,
pcapkit.foundation.reassembly.ip, holds the data models IPv4 and IPv6 share, sopcapkit.foundation.reassembly.ipv4andpcapkit.foundation.reassembly.ipv6have nodataof their own.The bases stay in
pcapkit.foundation.reassembly.reassemblyandpcapkit.foundation.traceflow.traceflow, with their data models beside them inpcapkit.foundation.reassembly.dataandpcapkit.foundation.traceflow.data.
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.analyzefor IPv4 and IPv6,Transport.analyzefor TCP, andDTLS.analyze_handshakefor a DTLS handshake message.Each
pcapkit.foundation.analysisfunction is a public wrapper that calls that method and holds no parsing of its own.The reassembly
Deferredcalls 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()andtcp_datagram()take them as keyword-onlylayer,protocolandcontext, so given the same they return thatpacket(#2947). The reassembler a TCP flow tracer builds foranalyzetakes them the same way (#2946). For IP both also hand a protocol class’sanalyzethe 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.foundationto do its own work; the dependency runs fromfoundationto the protocols. The protocol base’s import frompcapkit.foundation.registry.protocolsregisters a class rather than parsing.tests/project/test_protocols_import_boundary_2622_unit.pyfails on any other import ofpcapkit.foundationunderpcapkit/protocols/outside aTYPE_CHECKINGblock, the rest ofpcapkit.foundation.registryincluded.
flowchart TD
ENG["pcapkit.foundation.engines<br/>an already-parsed frame"]
TK["the pcapkit.toolkit.<engine> 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.