rtci_sctp/lib.rs
1//! SCTP for the Sans-I/O WebRTC stack.
2//!
3//! The Stream Control Transmission Protocol ([RFC 4960]) with the extensions WebRTC data
4//! channels need: partial reliability ([RFC 3758]) and stream reset / reconfiguration
5//! ([RFC 6525]). In WebRTC, SCTP runs *over* DTLS rather than over IP, and carries the data
6//! channels described by [`rtci-datachannel`].
7//!
8//! This is a fully deterministic implementation of the protocol logic. It contains no
9//! networking code and reads no clock of its own: you feed it datagrams and time, and poll it
10//! for the datagrams and events it produces. That is what makes it testable without a network
11//! and reusable under any executor.
12//!
13//! # Structure
14//!
15//! * [`Endpoint`] — the protocol state for one socket. It holds configuration and dispatches
16//! inbound datagrams to the right association.
17//! * [`Association`] — the bulk of the logic for a single
18//! association: handshake, congestion control, retransmission, and its streams.
19//! * [`Stream`] — one stream's reads, writes and reliability
20//! settings.
21//! * [`Chunks`] — a reassembled inbound message, delivered once every fragment has arrived.
22//!
23//! # Example
24//!
25//! Configuration is plain data, and the association is driven entirely by the caller — feed it
26//! datagrams and time, poll it for output:
27//!
28//! ```
29//! use rtci_sctp::{EndpointConfig, TransportConfig};
30//! use std::sync::Arc;
31//!
32//! let transport = TransportConfig::default()
33//! .with_max_message_size(65_536)
34//! .with_max_num_outbound_streams(1024);
35//!
36//! let endpoint_config = Arc::new(EndpointConfig::new());
37//! assert_eq!(transport.max_message_size(), 65_536);
38//! # let _ = endpoint_config;
39//! ```
40//!
41//! Most applications do not depend on this crate directly — the [`rtci`](https://docs.rs/rtci)
42//! crate drives it as one layer of the peer-connection pipeline and exposes data channels,
43//! and [`webrtc`](https://docs.rs/webrtc) wraps that in an async API.
44//!
45//! [RFC 4960]: https://datatracker.ietf.org/doc/html/rfc4960
46//! [RFC 3758]: https://datatracker.ietf.org/doc/html/rfc3758
47//! [RFC 6525]: https://datatracker.ietf.org/doc/html/rfc6525
48//! [`rtci-datachannel`]: https://docs.rs/rtci-datachannel
49
50#![warn(rust_2018_idioms)]
51#![warn(missing_docs)]
52#![allow(dead_code)]
53#![allow(clippy::bool_to_int_with_if)]
54
55use bytes::Bytes;
56use std::{fmt, ops};
57
58mod association;
59pub use crate::association::{
60 Association, AssociationError, AssociationStatus, Event, ReconfigEvent,
61 pmtud::{PathMtuDiscoveryConfig, PathMtuState},
62 stats::AssociationStats,
63 stream::{ReliabilityType, Stream, StreamEvent, StreamId, StreamState},
64 timer::{RtoConfig, TimerConfig},
65 undelivered::{UndeliveredMessage, UndeliveredMode},
66};
67
68pub(crate) mod chunk;
69pub(crate) mod cookie;
70pub use crate::chunk::{
71 ErrorCauseCode,
72 chunk_payload_data::{ChunkPayloadData, PayloadProtocolIdentifier},
73};
74
75mod config;
76pub use crate::config::{
77 ClientConfig, EndpointConfig, MIN_DATA_PACKET_MTU, ServerConfig, StreamScheduler,
78 TransportConfig, max_payload_size_for_mtu,
79};
80
81mod endpoint;
82pub use crate::endpoint::{AssociationHandle, ConnectError, DatagramEvent, Endpoint};
83
84mod packet;
85
86mod shared;
87pub use crate::shared::{AssociationEvent, AssociationId, EndpointEvent};
88
89pub(crate) mod param;
90
91pub(crate) mod queue;
92pub use crate::queue::reassembly_queue::{Chunk, Chunks, MessagePart};
93
94pub(crate) mod util;
95
96/// The checks the fuzz targets in `fuzz/` run, shared with the replay test of
97/// `fuzz/regressions/`. Built with `cfg(fuzzing)` (cargo-fuzz) or for this crate's tests; not
98/// part of the supported API.
99#[cfg(any(fuzzing, test))]
100#[doc(hidden)]
101pub mod fuzz_oracles;
102#[cfg(test)]
103mod fuzz_regressions_test;
104/// Entry points for fuzz targets and benchmarks.
105///
106/// Thin wrappers that drive one encode or decode step over a raw byte slice, so a fuzzer or
107/// a benchmark can reach the packet codec without setting up an association. Gated behind
108/// `cfg(fuzzing)`, this crate's tests (the replayable fuzz targets) or the `bench` feature; not part of the supported API.
109#[cfg(any(fuzzing, test, feature = "bench"))]
110pub mod fuzzing;
111
112/// Whether an endpoint was the initiator of an association
113#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
114pub enum Side {
115 /// The initiator of an association
116 #[default]
117 Client = 0,
118 /// The acceptor of an association
119 Server = 1,
120}
121
122impl fmt::Display for Side {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 let s = match *self {
125 Side::Client => "Client",
126 Side::Server => "Server",
127 };
128 write!(f, "{}", s)
129 }
130}
131
132impl Side {
133 #[inline]
134 /// Shorthand for `self == Side::Client`
135 pub fn is_client(self) -> bool {
136 self == Side::Client
137 }
138
139 #[inline]
140 /// Shorthand for `self == Side::Server`
141 pub fn is_server(self) -> bool {
142 self == Side::Server
143 }
144}
145
146impl ops::Not for Side {
147 type Output = Side;
148 fn not(self) -> Side {
149 match self {
150 Side::Client => Side::Server,
151 Side::Server => Side::Client,
152 }
153 }
154}
155
156use crate::packet::PartialDecode;
157
158/// Payload in Incoming/outgoing Transmit
159#[derive(Debug)]
160#[non_exhaustive]
161pub enum Payload {
162 /// An inbound packet whose header has been decoded but whose chunks have not.
163 PartialDecode(PartialDecode),
164 /// Outbound packets, already encoded and ready to hand to the transport.
165 RawEncode(Vec<Bytes>),
166}