SCTP User-Message Reassembly¶
pcapkit.foundation.reassembly.sctp.reassembly contains
SCTP only, which reconstructs
an SCTP user message fragmented across DATA chunks (RFC 9260 Section 6.9) or
I-DATA chunks (RFC 8260 Section 2.2.3) back to origin.
Added in version 1.9.0.
- class pcapkit.foundation.reassembly.sctp.reassembly.SCTP[_PT: Info, _DT: Info, _IT: tuple, _BT: Info](*, strict=True, store=True, timeout=None)[source]¶
Bases:
ReassemblyBase[Packet[_AT],Datagram[_AT],tuple[int,int,int],Buffer],Generic[_AT]Reassembly for SCTP user messages.
- Parameters:
strict (
bool) – ifTrue, report a message that is not completely reassembled as the tuple of its received runs; otherwise as one contiguous payload, the octets received up to its first hole, since a missing fragment’s length is unknownstore (
bool) – if store reassembled datagram in memory, i.e.,self._dtgram(if not, datagram will be discarded after callback)timeout (
float|None) – reassembly timeout in seconds, on the capture’s own clock;Noneselects__timeout__, which for SCTP disables expiry
Note
A DATA message is a run of consecutive TSNs, from a fragment with the
Bbit set to one with theEbit set, all on one stream with oneUbit and, when ordered, one stream sequence number (RFC 9260 Section 6.9). An I-DATA message is found by its stream,Ubit and message identifier, and its fragments are ordered by FSN rather than TSN (RFC 8260 Section 2.2.3). A fragment whose TSN, or FSN, is already held is a duplicate, and the one received first is kept (RFC 9260 Section 6.2).The buffer ID is the packet’s ports and verification tag, which one direction of one association carries on every path, so a fragment retransmitted to an alternate destination address (RFC 9260 Section 6.4) joins its message; a message reports the addresses of its lowest fragment held. A fragment retransmitted after its message completed is reported as a message of its own. The
Extractorfeeds it throughpcapkit.toolkit.pcap.sctp_reassembly(), andsctp_message()parses what it reassembles. A message’spacketholds the same parse, run on first read, by theanalyzeof__protocol_type__.Example
>>> from pcapkit.foundation.reassembly.sctp.reassembly import SCTP # Initialise instance: >>> sctp_reassembly = SCTP() # Call reassembly: >>> sctp_reassembly(packet_dict) # Fetch result: >>> result = sctp_reassembly.datagram
Defaults
- __protocol_type__: Type[ProtocolBase] = <class 'pcapkit.protocols.transport.sctp.protocol.SCTP'>¶
Protocol of current reassembly object, whose
analyzeparses a message’s user data, asSCTP.analyzedoes.
Methods
- submit(buf, *, bufid, expired=False, store=False)[source]¶
Submit reassembled payload.
- Parameters:
buf (
Buffer) – buffer dict of reassembled packetsexpired (
bool) – whether this buffer is being submitted becauseexpire()abandoned it under the reassembly timeoutstore (
bool) – whetherbufhas left_bufferfor good, so its messages join_dtgrambefore the callbacks run, and one that raises cannot lose them
- Return type:
- Returns:
Every message still held in
buf, DATA runs first, each reportedTIMEOUTorPARTIAL, since a message is reported as it completes.
Utilities
- static _continues(lower, upper)[source]¶
Whether one run of DATA fragments carries on the message of the run just below it in TSN.
- Parameters:
- Return type:
- Returns:
Trueunlesslowerends a message,upperbegins one, or the two differ in stream,Ubit or, when ordered, stream sequence number, which every fragment of a message shares (RFC 9260 Section 6.9).
- static _merge(buf, lower, upper)[source]¶
Join two adjacent runs of DATA fragments into one.
- Parameters:
- Returns:
the longer of the two, grown by the other, so only the shorter one’s TSNs are re-pointed.
- Return type:
- _datagram_from_fragment(frag, bufid, completion)[source]¶
Build a
Datagramfrom a message’s bufferedFragment.- Parameters:
- Return type:
- Returns:
The reassembled (or partially reassembled) message. Its
packetis the parsesctp_message()runs, by this reassembly’s ownprotocol, deferred to its first read, for a contiguous payload, andNonefor the tuple of runsstrict=Truereports.
Algorithm¶
A fragment names the message it belongs to differently in each chunk:
flowchart TD
F["a DATA or I-DATA chunk,<br/>B and E not both set"]
Q{"chunk type?"}
D["DATA: joins the run of TSNs either side of it,<br/>unless that run ends a message, it begins one,<br/>or stream, U bit or ordered SSN differ"]
I["I-DATA: held under its stream, U bit and MID,<br/>by its FSN"]
W{"B at the lowest held,<br/>E at the highest,<br/>none between missing?"}
C["COMPLETE: the message leaves the buffer"]
H["held until the buffer is submitted:<br/>PARTIAL, or TIMEOUT under a timeout"]
F --> Q
Q -->|DATA| D
Q -->|I-DATA| I
D & I --> W
W -->|yes| C
W -->|no| H
A DATA message’s fragments carry strictly sequential TSNs and one stream sequence number (RFC 9260 Section 3.3.1), so a missing TSN splits what is held into runs, each reported as a message of its own. An I-DATA message’s fragments are numbered by FSN from 0, and its TSNs say nothing of their order (RFC 8260 Section 2.2.3). Neither chunk says how long a missing fragment is, so a hole is never zero-filled.
The TSN space belongs to the association, and a multi-homed endpoint may retransmit a chunk to an alternate destination address (RFC 9260 Section 6.4). So a buffer is keyed on the ports and verification tag alone, which one direction of one association carries on every path, and a message reports the addresses of its lowest fragment held.
Terminology¶
- reasm.sctp.packet¶
Data structure for SCTP user-message fragment reassembly (
SCTP.reassembly) isPacket– one DATA or I-DATA chunk’sbufid, the addresses of the packet carrying it, chunk type, TSN, stream identifier,U,BandEbits, stream sequence number or message identifier and FSN, payload protocol identifier and user data, built bypcapkit.toolkit.pcap.sctp_reassembly()orpcapkit.toolkit.pcapng.sctp_reassembly().- reasm.sctp.datagram¶
Data structure for reassembled SCTP user message (element of
SCTP.datagram) isDatagram–completed, the original packet identifier, the packet numbers that built it, the message’s stream, ordering and TSNs,payload, the reassembled user data (a contiguousbytes, or – understrict=Trueand an incomplete message – atupleof the runs actually received), andpacket, whatsctp_message()parsespayloadto, on first read, as an IP or TCP datagram’s is.- reasm.sctp.buffer¶
Data structure for internal buffering (
SCTP._buffer) isBuffer– every message under reconstruction for oneBufferID(one direction of one association, over every path), each held as aFragment: the DATA ones by each TSN they hold, the I-DATA ones byMessageID.
Data Models¶
- class pcapkit.foundation.reassembly.sctp.data.Packet[_AT: (IPv4Address, IPv6Address)](bufid, src, dst, num, chunk, tsn, stream_id, unordered, beginning, ending, ssn, mid, fsn, ppid, payload, timestamp)[source]¶
-
Data model for SCTP user-message fragment representation, one DATA or I-DATA chunk.
- ssn: int | None¶
Stream sequence number of a DATA chunk, as received;
Nonefor I-DATA, which has none.
- fsn: int | None¶
Fragment sequence number of an I-DATA chunk,
0on its first fragment;Nonefor DATA.
- ppid: PayloadProtocolIdentifier | None¶
Payload protocol identifier, carried by every DATA chunk and by the first fragment of an I-DATA user message alone;
Noneon a later one.
- pcapkit.foundation.reassembly.sctp.data.BufferID¶
Buffer ID – the source and destination ports of the SCTP packet, plus its verification tag, which tells the association from an earlier one between the same endpoints (RFC 9260 Section 1.3). No address is in it, as the TSN space is the association’s and a chunk may be retransmitted to an alternate destination address (RFC 9260 Section 6.4).
- pcapkit.foundation.reassembly.sctp.data.MessageID¶
Message ID of an I-DATA user message within its buffer, made of its stream identifier, its
Ubit and its message identifier, which together find the message a fragment belongs to (RFC 8260 Section 2.2.3).
- class pcapkit.foundation.reassembly.sctp.data.DatagramID[_AT: (IPv4Address, IPv6Address)](src, sport, dst, dport, vtag)[source]¶
-
Data model for SCTP original packet identifier.
Its addresses are those of the message’s lowest fragment held, which is its first fragment once the message is complete: a multi-homed association may carry the others over other paths.
- class pcapkit.foundation.reassembly.sctp.data.Datagram[_AT: (IPv4Address, IPv6Address)](completed, id, index, chunk, stream_id, unordered, ssn, mid, tsn, ppid, payload, packet)[source]¶
Bases:
DeferredPacket,Info,Generic[_AT]Data model for SCTP reassembled user message.
- completed: Completion¶
How completely the message was reassembled, and why reassembly stopped; see
Completion.
- id: DatagramID[_AT]¶
Original packet identifier.
- ssn: int | None¶
Stream sequence number of an ordered DATA message;
Nonefor an unordered one, whose number the receiver ignores (RFC 9260 Section 3.3.1), and for I-DATA.
- ppid: PayloadProtocolIdentifier | None¶
Payload protocol identifier, as the first fragment held carries it;
Nonefor an I-DATA message whose first fragment is missing.
- packet: ProtocolBase | None¶
Parsed user data, as
sctp_message()returns it unless the reassembly’s__protocol_type__names another protocol, which then parses it instead. Parsed on first read, not at construction time, andNonefor the tuple of runsstrict=Truereports. ADeferredmay be passed in its place, and reading this attribute then runs it and keeps the result.
- class pcapkit.foundation.reassembly.sctp.data.Fragment[_AT: (IPv4Address, IPv6Address)](ind, chunk, stream_id, unordered, ssn, mid, ppid, src, dst, head, tail, beginning, ending, parts)[source]¶
-
Data model for SCTP user message under construction.
Its fragments are ordered by a sequence number: the TSN for DATA (RFC 9260 Section 6.9) and the FSN for I-DATA (RFC 8260 Section 2.2.3), both 32-bit serial numbers.
- ppid: PayloadProtocolIdentifier | None¶
Payload protocol identifier, that of the lowest DATA fragment held or of the first I-DATA fragment;
Noneuntil it arrives.
- class pcapkit.foundation.reassembly.sctp.data.Buffer(tsn, msg, timestamp)[source]¶
Bases:
InfoData model for SCTP buffer entry – every user message being reassembled under one
BufferID, i.e. one direction of one association, over every path it takes.- tsn: dict[int, Fragment]¶
DATA messages under construction, by the TSN of every fragment each holds: a run of consecutive TSNs (RFC 9260 Section 3.3.1).
Type Variables¶
- pcapkit.foundation.reassembly.sctp.data._AT: ipaddress.IPv4Address | ipaddress.IPv6Address¶
IP address type.