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)]
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    pub fn with_sctp_port(mut self, value: u16) -> Self {
43        self.sctp_port = value;
44        self
45    }
46
47    pub fn with_max_receive_buffer_size(mut self, value: u32) -> Self {
48        self.max_receive_buffer_size = value;
49        self
50    }
51
52    pub fn with_max_message_size(mut self, value: u32) -> Self {
53        self.max_message_size = value;
54        self
55    }
56
57    pub fn with_max_num_outbound_streams(mut self, value: u16) -> Self {
58        self.max_num_outbound_streams = value;
59        self
60    }
61
62    pub fn with_max_num_inbound_streams(mut self, value: u16) -> Self {
63        self.max_num_inbound_streams = value;
64        self
65    }
66
67    pub fn with_timer_config(mut self, value: TimerConfig) -> Self {
68        self.timer_config = value;
69        self
70    }
71
72    pub fn sctp_port(&self) -> u16 {
73        self.sctp_port
74    }
75
76    pub fn max_receive_buffer_size(&self) -> u32 {
77        self.max_receive_buffer_size
78    }
79
80    pub fn max_message_size(&self) -> u32 {
81        self.max_message_size
82    }
83
84    pub fn max_num_outbound_streams(&self) -> u16 {
85        self.max_num_outbound_streams
86    }
87
88    pub fn max_num_inbound_streams(&self) -> u16 {
89        self.max_num_inbound_streams
90    }
91
92    pub fn timer_config(&self) -> TimerConfig {
93        self.timer_config
94    }
95}
96
97/// Global configuration for the endpoint, affecting all associations
98///
99/// Default values should be suitable for most internet applications.
100#[derive(Clone)]
101pub struct EndpointConfig {
102    pub(crate) max_payload_size: u32,
103
104    /// AID generator factory
105    ///
106    /// Create a aid generator for local aid in Endpoint struct
107    pub(crate) aid_generator_factory:
108        Arc<dyn (Fn() -> Box<dyn AssociationIdGenerator + Send>) + Send + Sync>,
109}
110
111impl Default for EndpointConfig {
112    fn default() -> Self {
113        Self::new()
114    }
115}
116
117impl EndpointConfig {
118    /// Create a default config
119    pub fn new() -> Self {
120        let aid_factory: fn() -> Box<dyn AssociationIdGenerator + Send> =
121            || Box::<RandomAssociationIdGenerator>::default();
122        Self {
123            max_payload_size: INITIAL_MTU - (COMMON_HEADER_SIZE + DATA_CHUNK_HEADER_SIZE),
124            aid_generator_factory: Arc::new(aid_factory),
125        }
126    }
127
128    /// Supply a custom Association ID generator factory
129    ///
130    /// Called once by each `Endpoint` constructed from this configuration to obtain the AID
131    /// generator which will be used to generate the AIDs used for incoming packets on all
132    /// associations involving that  `Endpoint`. A custom AID generator allows applications to embed
133    /// information in local association IDs, e.g. to support stateless packet-level load balancers.
134    ///
135    /// `EndpointConfig::new()` applies a default random AID generator factory. This functions
136    /// accepts any customized AID generator to reset AID generator factory that implements
137    /// the `AssociationIdGenerator` trait.
138    pub fn aid_generator<
139        F: Fn() -> Box<dyn AssociationIdGenerator + Send> + Send + Sync + 'static,
140    >(
141        &mut self,
142        factory: F,
143    ) -> &mut Self {
144        self.aid_generator_factory = Arc::new(factory);
145        self
146    }
147
148    /// Maximum payload size accepted from peers.
149    ///
150    /// The default is suitable for typical internet applications. Applications which expect to run
151    /// on networks supporting Ethernet jumbo frames or similar should set this appropriately.
152    pub fn max_payload_size(&mut self, value: u32) -> &mut Self {
153        self.max_payload_size = value;
154        self
155    }
156
157    /// Get the current value of `max_payload_size`
158    ///
159    /// While most parameters don't need to be readable, this must be exposed to allow higher-level
160    /// layers to determine how large a receive buffer to allocate to
161    /// support an externally-defined `EndpointConfig`.
162    ///
163    /// While `get_` accessors are typically unidiomatic in Rust, we favor concision for setters,
164    /// which will be used far more heavily.
165    pub fn get_max_payload_size(&self) -> u32 {
166        self.max_payload_size
167    }
168}
169
170impl fmt::Debug for EndpointConfig {
171    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
172        fmt.debug_struct("EndpointConfig")
173            .field("max_payload_size", &self.max_payload_size)
174            .field("aid_generator_factory", &"[ elided ]")
175            .finish()
176    }
177}
178
179/// Parameters governing incoming associations
180///
181/// Default values should be suitable for most internet applications.
182#[derive(Debug, Clone)]
183pub struct ServerConfig {
184    /// Transport configuration to use for incoming associations
185    pub transport: Arc<TransportConfig>,
186
187    /// Maximum number of concurrent associations
188    pub(crate) concurrent_associations: u32,
189}
190
191impl Default for ServerConfig {
192    fn default() -> Self {
193        ServerConfig {
194            transport: Arc::new(TransportConfig::default()),
195            concurrent_associations: 100_000,
196        }
197    }
198}
199
200impl ServerConfig {
201    /// Create a default config with a particular handshake token key
202    pub fn new() -> Self {
203        ServerConfig::default()
204    }
205}
206
207/// Configuration for outgoing associations
208///
209/// Default values should be suitable for most internet applications.
210#[derive(Debug, Clone)]
211pub struct ClientConfig {
212    /// Transport configuration to use
213    pub transport: Arc<TransportConfig>,
214}
215
216impl Default for ClientConfig {
217    fn default() -> Self {
218        ClientConfig {
219            transport: Arc::new(TransportConfig::default()),
220        }
221    }
222}
223
224impl ClientConfig {
225    /// Create a default config with a particular cryptographic config
226    pub fn new() -> Self {
227        ClientConfig::default()
228    }
229}