Skip to main content

rtci_ice/candidate/
candidate_server_reflexive.rs

1use super::*;
2use crate::network_type::determine_network_type;
3use crate::rand::generate_cand_id;
4use shared::error::*;
5
6/// The config required to create a new `CandidateServerReflexive`.
7#[derive(Default)]
8pub struct CandidateServerReflexiveConfig {
9    /// The fields shared by every candidate type.
10    pub base_config: CandidateConfig,
11
12    /// The base address this candidate was derived from, reported as `raddr`.
13    pub rel_addr: String,
14    /// The base port, reported as `rport`.
15    pub rel_port: u16,
16
17    /// The server this candidate was gathered from.
18    pub url: Option<String>,
19}
20
21impl CandidateServerReflexiveConfig {
22    /// Creates a new server reflective candidate.
23    pub fn new_candidate_server_reflexive(self) -> Result<Candidate> {
24        let component = checked_component_and_priority(&self.base_config)?;
25        let mut candidate_id = self.base_config.candidate_id;
26        if candidate_id.is_empty() {
27            candidate_id = generate_cand_id();
28        }
29
30        let ip: IpAddr = match self.base_config.address.parse() {
31            Ok(ip) => ip,
32            Err(_) => return Err(Error::ErrAddressParseFailed),
33        };
34        let network_type = determine_network_type(&self.base_config.network, &ip)?;
35
36        Ok(Candidate {
37            id: candidate_id,
38            network_type,
39            candidate_type: CandidateType::ServerReflexive,
40            address: self.base_config.address,
41            port: self.base_config.port,
42            resolved_addr: SocketAddr::new(ip, self.base_config.port),
43            component,
44            foundation_override: self.base_config.foundation,
45            priority_override: self.base_config.priority,
46            related_address: Some(CandidateRelatedAddress {
47                address: self.rel_addr,
48                port: self.rel_port,
49            }),
50            url: self.url,
51            ..Candidate::default()
52        })
53    }
54}