sctp_proto/
config.rs

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