Skip to main content

rtc_ice/candidate/
candidate_host.rs

1use super::*;
2use crate::rand::generate_cand_id;
3
4/// The config required to create a new `CandidateHost`.
5#[derive(Default)]
6pub struct CandidateHostConfig {
7    /// The fields shared by every candidate type.
8    pub base_config: CandidateConfig,
9
10    /// The TCP role, for ICE-TCP host candidates.
11    pub tcp_type: TcpType,
12}
13
14impl CandidateHostConfig {
15    /// Creates a new host candidate.
16    pub fn new_candidate_host(self) -> Result<Candidate> {
17        let mut candidate_id = self.base_config.candidate_id;
18        if candidate_id.is_empty() {
19            candidate_id = generate_cand_id();
20        }
21
22        let (resolved_addr, network_type) = if !self.base_config.address.ends_with(".local") {
23            let ip: IpAddr = match self.base_config.address.parse() {
24                Ok(ip) => ip,
25                Err(_) => return Err(Error::ErrAddressParseFailed),
26            };
27            (
28                SocketAddr::new(ip, self.base_config.port),
29                determine_network_type(&self.base_config.network, &ip)?,
30            )
31        } else {
32            (
33                SocketAddr::new(IpAddr::from([0, 0, 0, 0]), 0),
34                NetworkType::Udp4,
35            )
36        };
37
38        Ok(Candidate {
39            id: candidate_id,
40            network_type,
41            candidate_type: CandidateType::Host,
42            address: self.base_config.address,
43            port: self.base_config.port,
44            resolved_addr,
45            component: self.base_config.component,
46            foundation_override: self.base_config.foundation,
47            priority_override: self.base_config.priority,
48            network: self.base_config.network,
49            tcp_type: self.tcp_type,
50            ..Candidate::default()
51        })
52    }
53}