webrtc_ice/agent/
agent_config.rs1use std::net::IpAddr;
2use std::time::Duration;
3
4use util::vnet::net::*;
5
6use super::*;
7use crate::error::*;
8use crate::mdns::*;
9use crate::network_type::*;
10use crate::udp_network::UDPNetwork;
11use crate::url::*;
12
13pub(crate) const DEFAULT_CHECK_INTERVAL: Duration = Duration::from_millis(200);
15
16pub(crate) const DEFAULT_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(2);
18
19pub(crate) const DEFAULT_DISCONNECTED_TIMEOUT: Duration = Duration::from_secs(5);
21
22pub(crate) const DEFAULT_FAILED_TIMEOUT: Duration = Duration::from_secs(25);
24
25pub(crate) const DEFAULT_HOST_ACCEPTANCE_MIN_WAIT: Duration = Duration::from_secs(0);
27
28pub(crate) const DEFAULT_SRFLX_ACCEPTANCE_MIN_WAIT: Duration = Duration::from_millis(500);
30
31pub(crate) const DEFAULT_PRFLX_ACCEPTANCE_MIN_WAIT: Duration = Duration::from_millis(1000);
33
34pub(crate) const DEFAULT_RELAY_ACCEPTANCE_MIN_WAIT: Duration = Duration::from_millis(2000);
36
37pub(crate) const DEFAULT_MAX_BINDING_REQUESTS: u16 = 7;
39
40pub(crate) const MAX_BUFFER_SIZE: usize = 1000 * 1000; pub(crate) const MAX_BINDING_REQUEST_TIMEOUT: Duration = Duration::from_millis(4000);
45
46pub(crate) fn default_candidate_types() -> Vec<CandidateType> {
47 vec![
48 CandidateType::Host,
49 CandidateType::ServerReflexive,
50 CandidateType::Relay,
51 ]
52}
53
54pub type InterfaceFilterFn = Box<dyn (Fn(&str) -> bool) + Send + Sync>;
55pub type IpFilterFn = Box<dyn (Fn(IpAddr) -> bool) + Send + Sync>;
56
57#[derive(Default)]
60pub struct AgentConfig {
61 pub urls: Vec<Url>,
62
63 pub udp_network: UDPNetwork,
66
67 pub local_ufrag: String,
71 pub local_pwd: String,
75
76 pub multicast_dns_mode: MulticastDnsMode,
78
79 pub multicast_dns_host_name: String,
81
82 pub multicast_dns_dest_addr: String,
84
85 pub disconnected_timeout: Option<Duration>,
88
89 pub failed_timeout: Option<Duration>,
92
93 pub keepalive_interval: Option<Duration>,
97
98 pub network_types: Vec<NetworkType>,
100
101 pub candidate_types: Vec<CandidateType>,
103
104 pub check_interval: Duration,
108
109 pub max_binding_requests: Option<u16>,
113
114 pub is_controlling: bool,
115
116 pub lite: bool,
118
119 pub nat_1to1_ip_candidate_type: CandidateType,
125
126 pub nat_1to1_ips: Vec<String>,
130
131 pub host_acceptance_min_wait: Option<Duration>,
133 pub srflx_acceptance_min_wait: Option<Duration>,
135 pub prflx_acceptance_min_wait: Option<Duration>,
137 pub relay_acceptance_min_wait: Option<Duration>,
139
140 pub net: Option<Arc<Net>>,
143
144 pub interface_filter: Arc<Option<InterfaceFilterFn>>,
147
148 pub ip_filter: Arc<Option<IpFilterFn>>,
151
152 pub insecure_skip_verify: bool,
155
156 pub include_loopback: bool,
158}
159
160impl AgentConfig {
161 pub(crate) fn init_with_defaults(&self, a: &mut AgentInternal) {
163 if let Some(max_binding_requests) = self.max_binding_requests {
164 a.max_binding_requests = max_binding_requests;
165 } else {
166 a.max_binding_requests = DEFAULT_MAX_BINDING_REQUESTS;
167 }
168
169 if let Some(host_acceptance_min_wait) = self.host_acceptance_min_wait {
170 a.host_acceptance_min_wait = host_acceptance_min_wait;
171 } else {
172 a.host_acceptance_min_wait = DEFAULT_HOST_ACCEPTANCE_MIN_WAIT;
173 }
174
175 if let Some(srflx_acceptance_min_wait) = self.srflx_acceptance_min_wait {
176 a.srflx_acceptance_min_wait = srflx_acceptance_min_wait;
177 } else {
178 a.srflx_acceptance_min_wait = DEFAULT_SRFLX_ACCEPTANCE_MIN_WAIT;
179 }
180
181 if let Some(prflx_acceptance_min_wait) = self.prflx_acceptance_min_wait {
182 a.prflx_acceptance_min_wait = prflx_acceptance_min_wait;
183 } else {
184 a.prflx_acceptance_min_wait = DEFAULT_PRFLX_ACCEPTANCE_MIN_WAIT;
185 }
186
187 if let Some(relay_acceptance_min_wait) = self.relay_acceptance_min_wait {
188 a.relay_acceptance_min_wait = relay_acceptance_min_wait;
189 } else {
190 a.relay_acceptance_min_wait = DEFAULT_RELAY_ACCEPTANCE_MIN_WAIT;
191 }
192
193 if let Some(disconnected_timeout) = self.disconnected_timeout {
194 a.disconnected_timeout = disconnected_timeout;
195 } else {
196 a.disconnected_timeout = DEFAULT_DISCONNECTED_TIMEOUT;
197 }
198
199 if let Some(failed_timeout) = self.failed_timeout {
200 a.failed_timeout = failed_timeout;
201 } else {
202 a.failed_timeout = DEFAULT_FAILED_TIMEOUT;
203 }
204
205 if let Some(keepalive_interval) = self.keepalive_interval {
206 a.keepalive_interval = keepalive_interval;
207 } else {
208 a.keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
209 }
210
211 if self.check_interval == Duration::from_secs(0) {
212 a.check_interval = DEFAULT_CHECK_INTERVAL;
213 } else {
214 a.check_interval = self.check_interval;
215 }
216 }
217
218 pub(crate) fn init_ext_ip_mapping(
219 &self,
220 mdns_mode: MulticastDnsMode,
221 candidate_types: &[CandidateType],
222 ) -> Result<Option<ExternalIpMapper>> {
223 if let Some(ext_ip_mapper) =
224 ExternalIpMapper::new(self.nat_1to1_ip_candidate_type, &self.nat_1to1_ips)?
225 {
226 if ext_ip_mapper.candidate_type == CandidateType::Host {
227 if mdns_mode == MulticastDnsMode::QueryAndGather {
228 return Err(Error::ErrMulticastDnsWithNat1to1IpMapping);
229 }
230 let mut candi_host_enabled = false;
231 for candi_type in candidate_types {
232 if *candi_type == CandidateType::Host {
233 candi_host_enabled = true;
234 break;
235 }
236 }
237 if !candi_host_enabled {
238 return Err(Error::ErrIneffectiveNat1to1IpMappingHost);
239 }
240 } else if ext_ip_mapper.candidate_type == CandidateType::ServerReflexive {
241 let mut candi_srflx_enabled = false;
242 for candi_type in candidate_types {
243 if *candi_type == CandidateType::ServerReflexive {
244 candi_srflx_enabled = true;
245 break;
246 }
247 }
248 if !candi_srflx_enabled {
249 return Err(Error::ErrIneffectiveNat1to1IpMappingSrflx);
250 }
251 }
252
253 Ok(Some(ext_ip_mapper))
254 } else {
255 Ok(None)
256 }
257 }
258}