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    pub base_config: CandidateConfig,
8
9    pub tcp_type: TcpType,
10}
11
12impl CandidateHostConfig {
13    /// Creates a new host candidate.
14    pub fn new_candidate_host(self) -> Result<Candidate> {
15        let mut candidate_id = self.base_config.candidate_id;
16        if candidate_id.is_empty() {
17            candidate_id = generate_cand_id();
18        }
19
20        let (resolved_addr, network_type) = if !self.base_config.address.ends_with(".local") {
21            let ip: IpAddr = match self.base_config.address.parse() {
22                Ok(ip) => ip,
23                Err(_) => return Err(Error::ErrAddressParseFailed),
24            };
25            (
26                SocketAddr::new(ip, self.base_config.port),
27                determine_network_type(&self.base_config.network, &ip)?,
28            )
29        } else {
30            (
31                SocketAddr::new(IpAddr::from([0, 0, 0, 0]), 0),
32                NetworkType::Udp4,
33            )
34        };
35
36        Ok(Candidate {
37            id: candidate_id,
38            network_type,
39            candidate_type: CandidateType::Host,
40            address: self.base_config.address,
41            port: self.base_config.port,
42            resolved_addr,
43            component: self.base_config.component,
44            foundation_override: self.base_config.foundation,
45            priority_override: self.base_config.priority,
46            network: self.base_config.network,
47            tcp_type: self.tcp_type,
48            ..Candidate::default()
49        })
50    }
51}