Skip to main content

rtc_sctp/
config.rs

1use crate::util::{AssociationIdGenerator, RandomAssociationIdGenerator};
2
3use crate::TimerConfig;
4use std::fmt;
5use std::sync::Arc;
6
7/// MTU for inbound packet (from DTLS)
8pub(crate) const RECEIVE_MTU: usize = 8192;
9/// initial MTU for outgoing packets (to DTLS)
10pub(crate) const INITIAL_MTU: u32 = 1228;
11pub(crate) const INITIAL_RECV_BUF_SIZE: u32 = 1024 * 1024;
12pub(crate) const COMMON_HEADER_SIZE: u32 = 12;
13pub(crate) const DATA_CHUNK_HEADER_SIZE: u32 = 16;
14pub(crate) const DEFAULT_MAX_MESSAGE_SIZE: u32 = 262144;
15
16/// Config collects the arguments to create_association construction into
17/// a single structure
18#[derive(Debug, Clone)]
19pub struct TransportConfig {
20    sctp_port: u16,
21    max_receive_buffer_size: u32,
22    max_message_size: u32,
23    max_num_outbound_streams: u16,
24    max_num_inbound_streams: u16,
25    timer_config: TimerConfig,
26}
27
28impl Default for TransportConfig {
29    fn default() -> Self {
30        TransportConfig {
31            sctp_port: 5000,
32            max_receive_buffer_size: INITIAL_RECV_BUF_SIZE,
33            max_message_size: DEFAULT_MAX_MESSAGE_SIZE,
34            max_num_outbound_streams: u16::MAX,
35            max_num_inbound_streams: u16::MAX,
36            timer_config: TimerConfig::default(),
37        }
38    }
39}
40
41impl TransportConfig {
42    /// Sets the SCTP port. WebRTC always uses 5000.
43    pub fn with_sctp_port(mut self, value: u16) -> Self {
44        self.sctp_port = value;
45        self
46    }
47
48    /// Sets the advertised receive window (a_rwnd), bounding how much unacknowledged data a peer
49    /// may have in flight toward this endpoint.
50    pub fn with_max_receive_buffer_size(mut self, value: u32) -> Self {
51        self.max_receive_buffer_size = value;
52        self
53    }
54
55    /// Sets the largest message this endpoint will accept.
56    pub fn with_max_message_size(mut self, value: u32) -> Self {
57        self.max_message_size = value;
58        self
59    }
60
61    /// Sets how many outbound streams to request during the handshake.
62    pub fn with_max_num_outbound_streams(mut self, value: u16) -> Self {
63        self.max_num_outbound_streams = value;
64        self
65    }
66
67    /// Sets how many inbound streams this endpoint will accept.
68    pub fn with_max_num_inbound_streams(mut self, value: u16) -> Self {
69        self.max_num_inbound_streams = value;
70        self
71    }
72
73    /// Overrides the retransmission limits; see [`TimerConfig`].
74    pub fn with_timer_config(mut self, value: TimerConfig) -> Self {
75        self.timer_config = value;
76        self
77    }
78
79    /// The configured SCTP port.
80    pub fn sctp_port(&self) -> u16 {
81        self.sctp_port
82    }
83
84    /// The configured receive window in bytes.
85    pub fn max_receive_buffer_size(&self) -> u32 {
86        self.max_receive_buffer_size
87    }
88
89    /// The configured maximum message size in bytes.
90    pub fn max_message_size(&self) -> u32 {
91        self.max_message_size
92    }
93
94    /// The configured outbound stream count.
95    pub fn max_num_outbound_streams(&self) -> u16 {
96        self.max_num_outbound_streams
97    }
98
99    /// The configured inbound stream count.
100    pub fn max_num_inbound_streams(&self) -> u16 {
101        self.max_num_inbound_streams
102    }
103
104    /// The configured retransmission limits.
105    pub fn timer_config(&self) -> TimerConfig {
106        self.timer_config
107    }
108}
109
110/// Global configuration for the endpoint, affecting all associations
111///
112/// Default values should be suitable for most internet applications.
113#[derive(Clone)]
114pub struct EndpointConfig {
115    pub(crate) max_payload_size: u32,
116
117    /// AID generator factory
118    ///
119    /// Create a aid generator for local aid in Endpoint struct
120    pub(crate) aid_generator_factory:
121        Arc<dyn (Fn() -> Box<dyn AssociationIdGenerator + Send>) + Send + Sync>,
122}
123
124impl Default for EndpointConfig {
125    fn default() -> Self {
126        Self::new()
127    }
128}
129
130impl EndpointConfig {
131    /// Create a default configuration
132    pub fn new() -> Self {
133        let aid_factory: fn() -> Box<dyn AssociationIdGenerator + Send> =
134            || Box::<RandomAssociationIdGenerator>::default();
135        Self {
136            max_payload_size: INITIAL_MTU - (COMMON_HEADER_SIZE + DATA_CHUNK_HEADER_SIZE),
137            aid_generator_factory: Arc::new(aid_factory),
138        }
139    }
140
141    /// Supply a custom Association ID generator factory
142    ///
143    /// Called once by each `Endpoint` constructed from this configuration to obtain the AID
144    /// generator which will be used to generate the AIDs used for incoming packets on all
145    /// associations involving that  `Endpoint`. A custom AID generator allows applications to embed
146    /// information in local association IDs, e.g. to support stateless packet-level load balancers.
147    ///
148    /// `EndpointConfig::new()` applies a default random AID generator factory. This functions
149    /// accepts any customized AID generator to reset AID generator factory that implements
150    /// the `AssociationIdGenerator` trait.
151    pub fn aid_generator<
152        F: Fn() -> Box<dyn AssociationIdGenerator + Send> + Send + Sync + 'static,
153    >(
154        &mut self,
155        factory: F,
156    ) -> &mut Self {
157        self.aid_generator_factory = Arc::new(factory);
158        self
159    }
160
161    /// Maximum payload size accepted from peers.
162    ///
163    /// The default is suitable for typical internet applications. Applications which expect to run
164    /// on networks supporting Ethernet jumbo frames or similar should set this appropriately.
165    pub fn max_payload_size(&mut self, value: u32) -> &mut Self {
166        self.max_payload_size = value;
167        self
168    }
169
170    /// Get the current value of `max_payload_size`
171    ///
172    /// While most parameters don't need to be readable, this must be exposed to allow higher-level
173    /// layers to determine how large a receive buffer to allocate to
174    /// support an externally-defined `EndpointConfig`.
175    ///
176    /// While `get_` accessors are typically unidiomatic in Rust, we favor concision for setters,
177    /// which will be used far more heavily.
178    pub fn get_max_payload_size(&self) -> u32 {
179        self.max_payload_size
180    }
181}
182
183impl fmt::Debug for EndpointConfig {
184    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
185        fmt.debug_struct("EndpointConfig")
186            .field("max_payload_size", &self.max_payload_size)
187            .field("aid_generator_factory", &"[ elided ]")
188            .finish()
189    }
190}
191
192/// Parameters governing incoming associations
193///
194/// Default values should be suitable for most internet applications.
195#[derive(Debug, Clone)]
196pub struct ServerConfig {
197    /// Transport configuration to use for incoming associations
198    pub transport: Arc<TransportConfig>,
199
200    /// Maximum number of concurrent associations
201    pub(crate) concurrent_associations: u32,
202}
203
204impl Default for ServerConfig {
205    fn default() -> Self {
206        ServerConfig {
207            transport: Arc::new(TransportConfig::default()),
208            concurrent_associations: 100_000,
209        }
210    }
211}
212
213impl ServerConfig {
214    /// Create a default configuration with a particular handshake token key
215    pub fn new(transport: TransportConfig) -> Self {
216        ServerConfig {
217            transport: Arc::new(transport),
218            concurrent_associations: 100_000,
219        }
220    }
221}
222
223/// Configuration for outgoing associations
224///
225/// Default values should be suitable for most internet applications.
226#[derive(Debug, Clone)]
227pub struct ClientConfig {
228    /// Transport configuration to use
229    pub transport: Arc<TransportConfig>,
230}
231
232impl Default for ClientConfig {
233    fn default() -> Self {
234        ClientConfig {
235            transport: Arc::new(TransportConfig::default()),
236        }
237    }
238}
239
240impl ClientConfig {
241    /// Create a default configuration with a particular cryptographic configuration
242    pub fn new(transport: TransportConfig) -> Self {
243        ClientConfig {
244            transport: Arc::new(transport),
245        }
246    }
247}