Skip to main content

rtc_ice/candidate/
candidate_peer_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 `CandidatePeerReflexive`.
7#[derive(Default)]
8pub struct CandidatePeerReflexiveConfig {
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
18impl CandidatePeerReflexiveConfig {
19    /// Creates a new peer reflective candidate.
20    pub fn new_candidate_peer_reflexive(self) -> Result<Candidate> {
21        let mut candidate_id = self.base_config.candidate_id;
22        if candidate_id.is_empty() {
23            candidate_id = generate_cand_id();
24        }
25
26        let ip: IpAddr = match self.base_config.address.parse() {
27            Ok(ip) => ip,
28            Err(_) => return Err(Error::ErrAddressParseFailed),
29        };
30        let network_type = determine_network_type(&self.base_config.network, &ip)?;
31
32        Ok(Candidate {
33            id: candidate_id,
34            network_type,
35            candidate_type: CandidateType::PeerReflexive,
36            address: self.base_config.address,
37            port: self.base_config.port,
38            resolved_addr: SocketAddr::new(ip, self.base_config.port),
39            component: self.base_config.component,
40            foundation_override: self.base_config.foundation,
41            priority_override: self.base_config.priority,
42            related_address: Some(CandidateRelatedAddress {
43                address: self.rel_addr,
44                port: self.rel_port,
45            }),
46            ..Candidate::default()
47        })
48    }
49}