Skip to main content

rtci_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 component = checked_component_and_priority(&self.base_config)?;
18        let mut candidate_id = self.base_config.candidate_id;
19        if candidate_id.is_empty() {
20            candidate_id = generate_cand_id();
21        }
22
23        let (resolved_addr, network_type) = if !self.base_config.address.ends_with(".local") {
24            let ip: IpAddr = match self.base_config.address.parse() {
25                Ok(ip) => ip,
26                Err(_) => return Err(Error::ErrAddressParseFailed),
27            };
28            (
29                SocketAddr::new(ip, self.base_config.port),
30                determine_network_type(&self.base_config.network, &ip)?,
31            )
32        } else {
33            // An mDNS name (draft-ietf-mmusic-mdns-ice-candidates section 3.1.2.4): the family is
34            // known only once the name resolves (`set_ip`), but the transport is the
35            // candidate's own. A TCP candidate under a name stays TCP (RFC 6544 section 4.5),
36            // or it would be signalled, and paired, as UDP on the TCP port.
37            let network_type = if self.base_config.network.to_lowercase().starts_with("tcp") {
38                NetworkType::Tcp4
39            } else {
40                NetworkType::Udp4
41            };
42            (SocketAddr::new(IpAddr::from([0, 0, 0, 0]), 0), network_type)
43        };
44
45        Ok(Candidate {
46            id: candidate_id,
47            network_type,
48            candidate_type: CandidateType::Host,
49            address: self.base_config.address,
50            port: self.base_config.port,
51            resolved_addr,
52            component,
53            foundation_override: self.base_config.foundation,
54            priority_override: self.base_config.priority,
55            network: self.base_config.network,
56            tcp_type: self.tcp_type,
57            ..Candidate::default()
58        })
59    }
60}