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) – if True, 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 unknown

  • store (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; None selects __timeout__, which for SCTP disables expiry

Note

A DATA message is a run of consecutive TSNs, from a fragment with the B bit set to one with the E bit set, all on one stream with one U bit and, when ordered, one stream sequence number (RFC 9260 Section 6.9). An I-DATA message is found by its stream, U bit 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 Extractor feeds it through pcapkit.toolkit.pcap.sctp_reassembly(), and sctp_message() parses what it reassembles. A message’s packet holds the same parse, run on first read, by the analyze of __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_name__: str = 'SCTP'

Protocol name of current reassembly object.

__protocol_type__: Type[ProtocolBase] = <class 'pcapkit.protocols.transport.sctp.protocol.SCTP'>

Protocol of current reassembly object, whose analyze parses a message’s user data, as SCTP.analyze does.

Methods

reassembly(info)[source]

Reassembly procedure.

Parameters:

info (Packet[_AT]) – info record of packets to be reassembled

submit(buf, *, bufid, expired=False, store=False)[source]

Submit reassembled payload.

Parameters:
  • buf (Buffer) – buffer dict of reassembled packets

  • bufid (tuple[int, int, int]) – buffer identifier

  • expired (bool) – whether this buffer is being submitted because expire() abandoned it under the reassembly timeout

  • store (bool) – whether buf has left _buffer for good, so its messages join _dtgram before the callbacks run, and one that raises cannot lose them

Return type:

list[Datagram[_AT]]

Returns:

Every message still held in buf, DATA runs first, each reported TIMEOUT or PARTIAL, since a message is reported as it completes.

Utilities

_add_data(buf, info)[source]

Add a DATA fragment to the run of consecutive TSNs it extends.

Parameters:
  • buf (Buffer) – buffer the fragment is held under

  • info (Packet[_AT]) – the fragment

Return type:

Fragment | None

Returns:

The message the fragment completes, taken out of buf, or None if it completes none.

static _continues(lower, upper)[source]

Whether one run of DATA fragments carries on the message of the run just below it in TSN.

Parameters:
  • lower (Fragment) – the run whose last TSN is just below upper’s first

  • upper (Fragment) – the run that may carry on lower’s message

Return type:

bool

Returns:

True unless lower ends a message, upper begins one, or the two differ in stream, U bit 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:
  • buf (Buffer) – buffer the runs are held under, whose TSNs are re-pointed

  • lower (Fragment) – the run whose last TSN is just below upper’s first

  • upper (Fragment) – the run carrying on lower’s message

Returns:

the longer of the two, grown by the other, so only the shorter one’s TSNs are re-pointed.

Return type:

Fragment

_add_idata(buf, info)[source]

Add an I-DATA fragment to the message its message ID names.

Parameters:
  • buf (Buffer) – buffer the fragment is held under

  • info (Packet[_AT]) – the fragment

Return type:

Fragment | None

Returns:

The message the fragment completes, taken out of buf, or None if it completes none.

static _complete(frag)[source]

Whether a message under construction is whole.

Parameters:

frag (Fragment) – the message under construction

Return type:

bool

Returns:

True if its lowest fragment has the B bit set, its highest the E bit, and every sequence number between them is held.

_datagram_from_fragment(frag, bufid, completion)[source]

Build a Datagram from a message’s buffered Fragment.

Parameters:
  • frag (Fragment) – message under construction to report

  • bufid (tuple[int, int, int]) – buffer identifier it was held under

  • completion (Completion) – how completely it was reassembled

Return type:

Datagram[_AT]

Returns:

The reassembled (or partially reassembled) message. Its packet is the parse sctp_message() runs, by this reassembly’s own protocol, deferred to its first read, for a contiguous payload, and None for the tuple of runs strict=True reports.

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) is Packet – one DATA or I-DATA chunk’s bufid, the addresses of the packet carrying it, chunk type, TSN, stream identifier, U, B and E bits, stream sequence number or message identifier and FSN, payload protocol identifier and user data, built by pcapkit.toolkit.pcap.sctp_reassembly() or pcapkit.toolkit.pcapng.sctp_reassembly().

reasm.sctp.datagram

Data structure for reassembled SCTP user message (element of SCTP.datagram) is Datagram – completed, the original packet identifier, the packet numbers that built it, the message’s stream, ordering and TSNs, payload, the reassembled user data (a contiguous bytes, or – under strict=True and an incomplete message – a tuple of the runs actually received), and packet, what sctp_message() parses payload to, on first read, as an IP or TCP datagram’s is.

reasm.sctp.buffer

Data structure for internal buffering (SCTP._buffer) is Buffer – every message under reconstruction for one BufferID (one direction of one association, over every path), each held as a Fragment: the DATA ones by each TSN they hold, the I-DATA ones by MessageID.

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]

Bases: Info, Generic[_AT]

Data model for SCTP user-message fragment representation, one DATA or I-DATA chunk.

bufid: tuple[int, int, int]

Buffer ID.

src: _AT

Source address of the packet carrying the chunk.

dst: _AT

Destination address of the packet carrying the chunk.

num: int

Original packet range number.

chunk: Chunk

Chunk type, DATA or I-DATA.

tsn: int

Transmission sequence number.

stream_id: int

Stream identifier.

unordered: bool

Whether the U bit is set, so the user message is unordered.

beginning: bool

Whether the B bit is set, marking the first fragment of a user message.

ending: bool

Whether the E bit is set, marking the last fragment of a user message.

ssn: int | None

Stream sequence number of a DATA chunk, as received; None for I-DATA, which has none.

mid: int | None

Message identifier of an I-DATA chunk; None for DATA.

fsn: int | None

Fragment sequence number of an I-DATA chunk, 0 on its first fragment; None for DATA.

ppid: PayloadProtocolIdentifier | None

Payload protocol identifier, carried by every DATA chunk and by the first fragment of an I-DATA user message alone; None on a later one.

payload: bytes

User data of the chunk.

timestamp: float

Capture timestamp of the fragment, in seconds since the Unix epoch, i.e. the capture’s clock.

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).

alias of tuple[int, int, int]

pcapkit.foundation.reassembly.sctp.data.MessageID

Message ID of an I-DATA user message within its buffer, made of its stream identifier, its U bit and its message identifier, which together find the message a fragment belongs to (RFC 8260 Section 2.2.3).

alias of tuple[int, bool, int]

class pcapkit.foundation.reassembly.sctp.data.DatagramID[_AT: (IPv4Address, IPv6Address)](src, sport, dst, dport, vtag)[source]

Bases: Info, Generic[_AT]

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.

src: _AT

Source address.

sport: int

Source port.

dst: _AT

Destination address.

dport: int

Destination port.

vtag: int

Verification tag the message was carried under.

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.

index: tuple[int, ...]

Packet numbers of the fragments that built this message.

chunk: Chunk

Chunk type the message was carried in, DATA or I-DATA.

stream_id: int

Stream identifier.

unordered: bool

Whether the message is unordered.

ssn: int | None

Stream sequence number of an ordered DATA message; None for an unordered one, whose number the receiver ignores (RFC 9260 Section 3.3.1), and for I-DATA.

mid: int | None

Message identifier of an I-DATA message; None for DATA.

tsn: tuple[int, ...]

Transmission sequence numbers of the fragments held, in message order.

ppid: PayloadProtocolIdentifier | None

Payload protocol identifier, as the first fragment held carries it; None for an I-DATA message whose first fragment is missing.

payload: bytes | tuple[bytes, ...]

Reassembled user data.

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, and None for the tuple of runs strict=True reports. A Deferred may 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]

Bases: Info, Generic[_AT]

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.

ind: list[int]

Numbers of the packets merged into this message.

chunk: Chunk

Chunk type, DATA or I-DATA.

stream_id: int

Stream identifier.

unordered: bool

Whether the message is unordered.

ssn: int | None

Stream sequence number of an ordered DATA message; None otherwise.

mid: int | None

Message identifier of an I-DATA message; None for DATA.

ppid: PayloadProtocolIdentifier | None

Payload protocol identifier, that of the lowest DATA fragment held or of the first I-DATA fragment; None until it arrives.

src: _AT

Source address of the packet carrying the lowest fragment held.

dst: _AT

Destination address of the packet carrying the lowest fragment held.

head: int

Sequence number of the lowest fragment held.

tail: int

Sequence number of the highest fragment held.

beginning: bool

Whether the fragment at head has the B bit set.

ending: bool

Whether the fragment at tail has the E bit set.

parts: dict[int, tuple[int, bytes]]

Each fragment held, by sequence number, as its TSN and user data.

class pcapkit.foundation.reassembly.sctp.data.Buffer(tsn, msg, timestamp)[source]

Bases: Info

Data 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).

msg: dict[tuple[int, bool, int], Fragment]

I-DATA messages under construction, by MessageID.

timestamp: float

Capture timestamp of the first fragment buffered under this buffer ID, in seconds since the Unix epoch.

Type Variables

pcapkit.foundation.reassembly.sctp.data._AT: ipaddress.IPv4Address | ipaddress.IPv6Address

IP address type.