rtc_ice/candidate/
mod.rs

1#[cfg(test)]
2mod candidate_pair_test;
3#[cfg(test)]
4mod candidate_test;
5
6//TODO: #[cfg(test)]
7//TODO: mod candidate_relay_test;
8/*TODO: #[cfg(test)]
9TODO: mod candidate_server_reflexive_test;
10*/
11
12pub mod candidate_host;
13pub mod candidate_pair;
14pub mod candidate_peer_reflexive;
15pub mod candidate_relay;
16pub mod candidate_server_reflexive;
17
18use crate::network_type::NetworkType;
19use crate::tcp_type::TcpType;
20use crc::{CRC_32_ISCSI, Crc};
21use serde::{Deserialize, Serialize};
22use shared::error::*;
23use std::fmt;
24use std::net::{IpAddr, SocketAddr};
25use std::time::Instant;
26
27use crate::candidate::candidate_host::CandidateHostConfig;
28use crate::candidate::candidate_peer_reflexive::CandidatePeerReflexiveConfig;
29use crate::candidate::candidate_relay::CandidateRelayConfig;
30use crate::candidate::candidate_server_reflexive::CandidateServerReflexiveConfig;
31use crate::network_type::determine_network_type;
32
33pub(crate) const RECEIVE_MTU: usize = 8192;
34pub(crate) const DEFAULT_LOCAL_PREFERENCE: u16 = 65535;
35
36/// Indicates that the candidate is used for RTP.
37pub(crate) const COMPONENT_RTP: u16 = 1;
38/// Indicates that the candidate is used for RTCP.
39pub(crate) const COMPONENT_RTCP: u16 = 0;
40
41/// Represents the type of candidate `CandidateType` enum.
42#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
43pub enum CandidateType {
44    #[default]
45    #[serde(rename = "unspecified")]
46    Unspecified,
47    #[serde(rename = "host")]
48    Host,
49    #[serde(rename = "srflx")]
50    ServerReflexive,
51    #[serde(rename = "prflx")]
52    PeerReflexive,
53    #[serde(rename = "relay")]
54    Relay,
55}
56
57// String makes CandidateType printable
58impl fmt::Display for CandidateType {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        let s = match *self {
61            CandidateType::Host => "host",
62            CandidateType::ServerReflexive => "srflx",
63            CandidateType::PeerReflexive => "prflx",
64            CandidateType::Relay => "relay",
65            CandidateType::Unspecified => "Unknown candidate type",
66        };
67        write!(f, "{s}")
68    }
69}
70
71impl CandidateType {
72    /// Returns the preference weight of a `CandidateType`.
73    ///
74    /// 4.1.2.2.  Guidelines for Choosing Type and Local Preferences
75    /// The RECOMMENDED values are 126 for host candidates, 100
76    /// for server reflexive candidates, 110 for peer reflexive candidates,
77    /// and 0 for relayed candidates.
78    #[must_use]
79    pub const fn preference(self) -> u16 {
80        match self {
81            Self::Host => 126,
82            Self::PeerReflexive => 110,
83            Self::ServerReflexive => 100,
84            Self::Relay | CandidateType::Unspecified => 0,
85        }
86    }
87}
88
89pub(crate) fn contains_candidate_type(
90    candidate_type: CandidateType,
91    candidate_type_list: &[CandidateType],
92) -> bool {
93    if candidate_type_list.is_empty() {
94        return false;
95    }
96    for ct in candidate_type_list {
97        if *ct == candidate_type {
98            return true;
99        }
100    }
101    false
102}
103
104/// Convey transport addresses related to the candidate, useful for diagnostics and other purposes.
105#[derive(PartialEq, Eq, Debug, Clone)]
106pub struct CandidateRelatedAddress {
107    pub address: String,
108    pub port: u16,
109}
110
111// String makes CandidateRelatedAddress printable
112impl fmt::Display for CandidateRelatedAddress {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        write!(f, " related {}:{}", self.address, self.port)
115    }
116}
117
118#[derive(Default)]
119pub struct CandidateConfig {
120    pub candidate_id: String,
121    pub network: String,
122    pub address: String,
123    pub port: u16,
124    pub component: u16,
125    pub priority: u32,
126    pub foundation: String,
127}
128
129#[derive(Clone)]
130pub struct Candidate {
131    pub(crate) id: String,
132    pub(crate) network_type: NetworkType,
133    pub(crate) candidate_type: CandidateType,
134
135    pub(crate) component: u16,
136    pub(crate) address: String,
137    pub(crate) port: u16,
138    pub(crate) related_address: Option<CandidateRelatedAddress>,
139    pub(crate) tcp_type: TcpType,
140
141    pub(crate) resolved_addr: SocketAddr,
142
143    pub(crate) last_sent: Instant,
144    pub(crate) last_received: Instant,
145
146    pub(crate) foundation_override: String,
147    pub(crate) priority_override: u32,
148
149    pub(crate) network: String,
150}
151
152impl Default for Candidate {
153    fn default() -> Self {
154        Self {
155            id: String::new(),
156            network_type: NetworkType::Unspecified,
157            candidate_type: CandidateType::default(),
158
159            component: 0,
160            address: String::new(),
161            port: 0,
162            related_address: None,
163            tcp_type: TcpType::default(),
164
165            resolved_addr: SocketAddr::new(IpAddr::from([0, 0, 0, 0]), 0),
166
167            last_sent: Instant::now(),
168            last_received: Instant::now(),
169
170            foundation_override: String::new(),
171            priority_override: 0,
172            network: String::new(),
173        }
174    }
175}
176
177// String makes the candidateBase printable
178impl fmt::Display for Candidate {
179    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180        if let Some(related_address) = self.related_address() {
181            write!(
182                f,
183                "{} {} {}:{}{}",
184                self.network_type(),
185                self.candidate_type(),
186                self.address(),
187                self.port(),
188                related_address,
189            )
190        } else {
191            write!(
192                f,
193                "{} {} {}:{}",
194                self.network_type(),
195                self.candidate_type(),
196                self.address(),
197                self.port(),
198            )
199        }
200    }
201}
202
203impl Candidate {
204    pub fn foundation(&self) -> String {
205        if !self.foundation_override.is_empty() {
206            return self.foundation_override.clone();
207        }
208
209        let mut buf = vec![];
210        buf.extend_from_slice(self.candidate_type().to_string().as_bytes());
211        buf.extend_from_slice(self.address.as_bytes());
212        buf.extend_from_slice(self.network_type().to_string().as_bytes());
213
214        let checksum = Crc::<u32>::new(&CRC_32_ISCSI).checksum(&buf);
215
216        format!("{checksum}")
217    }
218
219    /// Returns Candidate ID.
220    pub fn id(&self) -> String {
221        self.id.clone()
222    }
223
224    /// Returns candidate component.
225    pub fn component(&self) -> u16 {
226        self.component
227    }
228
229    /// Sets candidate component.
230    pub fn set_component(&mut self, component: u16) {
231        self.component = component;
232    }
233
234    /// Returns a time indicating the last time this candidate was received.
235    pub fn last_received(&self) -> Instant {
236        self.last_received
237    }
238
239    /// Returns a time indicating the last time this candidate was sent.
240    pub fn last_sent(&self) -> Instant {
241        self.last_sent
242    }
243
244    /// Returns candidate NetworkType.
245    pub fn network_type(&self) -> NetworkType {
246        self.network_type
247    }
248
249    /// Returns Candidate Address.
250    pub fn address(&self) -> &str {
251        self.address.as_str()
252    }
253
254    /// Returns Candidate Port.
255    pub fn port(&self) -> u16 {
256        self.port
257    }
258
259    /// Computes the priority for this ICE Candidate.
260    pub fn priority(&self) -> u32 {
261        if self.priority_override != 0 {
262            return self.priority_override;
263        }
264
265        // The local preference MUST be an integer from 0 (lowest preference) to
266        // 65535 (highest preference) inclusive.  When there is only a single IP
267        // address, this value SHOULD be set to 65535.  If there are multiple
268        // candidates for a particular component for a particular data stream
269        // that have the same type, the local preference MUST be unique for each
270        // one.
271        (1 << 24) * u32::from(self.candidate_type().preference())
272            + (1 << 8) * u32::from(self.local_preference())
273            + (256 - u32::from(self.component()))
274    }
275
276    /// Returns `Option<CandidateRelatedAddress>`.
277    pub fn related_address(&self) -> Option<CandidateRelatedAddress> {
278        self.related_address.as_ref().cloned()
279    }
280
281    /// Returns candidate type.
282    pub fn candidate_type(&self) -> CandidateType {
283        self.candidate_type
284    }
285
286    pub fn tcp_type(&self) -> TcpType {
287        self.tcp_type
288    }
289
290    /// Returns the string representation of the ICECandidate.
291    pub fn marshal(&self) -> String {
292        let mut val = format!(
293            "{} {} {} {} {} {} typ {}",
294            self.foundation(),
295            self.component(),
296            self.network_type().network_short(),
297            self.priority(),
298            self.address(),
299            self.port(),
300            self.candidate_type()
301        );
302
303        if self.tcp_type != TcpType::Unspecified {
304            val += format!(" tcptype {}", self.tcp_type()).as_str();
305        }
306
307        if let Some(related_address) = self.related_address() {
308            val += format!(
309                " raddr {} rport {}",
310                related_address.address, related_address.port,
311            )
312            .as_str();
313        }
314
315        val
316    }
317
318    pub fn addr(&self) -> SocketAddr {
319        self.resolved_addr
320    }
321
322    pub fn seen(&mut self, outbound: bool) {
323        let now = Instant::now();
324
325        if outbound {
326            self.set_last_sent(now);
327        } else {
328            self.set_last_received(now);
329        }
330    }
331
332    /// Used to compare two candidateBases.
333    pub fn equal(&self, other: &Candidate) -> bool {
334        self.network_type() == other.network_type()
335            && self.candidate_type() == other.candidate_type()
336            && self.address() == other.address()
337            && self.port() == other.port()
338            && self.tcp_type() == other.tcp_type()
339            && self.related_address() == other.related_address()
340    }
341
342    pub fn set_ip(&mut self, ip: &IpAddr) -> Result<()> {
343        self.network_type = determine_network_type(&self.network, ip)?;
344        self.resolved_addr = SocketAddr::new(*ip, self.port); //TODO:  create_addr(network_type, *ip, self.port);
345        Ok(())
346    }
347}
348
349impl Candidate {
350    pub fn set_last_received(&mut self, now: Instant) {
351        self.last_received = now;
352    }
353
354    pub fn set_last_sent(&mut self, now: Instant) {
355        self.last_sent = now;
356    }
357
358    /// Returns the local preference for this candidate.
359    pub fn local_preference(&self) -> u16 {
360        if self.network_type().is_tcp() {
361            // RFC 6544, section 4.2
362            //
363            // In Section 4.1.2.1 of [RFC5245], a recommended formula for UDP ICE
364            // candidate prioritization is defined.  For TCP candidates, the same
365            // formula and candidate type preferences SHOULD be used, and the
366            // RECOMMENDED type preferences for the new candidate types defined in
367            // this document (see Section 5) are 105 for NAT-assisted candidates and
368            // 75 for UDP-tunneled candidates.
369            //
370            // (...)
371            //
372            // With TCP candidates, the local preference part of the recommended
373            // priority formula is updated to also include the directionality
374            // (active, passive, or simultaneous-open) of the TCP connection.  The
375            // RECOMMENDED local preference is then defined as:
376            //
377            //     local preference = (2^13) * direction-pref + other-pref
378            //
379            // The direction-pref MUST be between 0 and 7 (both inclusive), with 7
380            // being the most preferred.  The other-pref MUST be between 0 and 8191
381            // (both inclusive), with 8191 being the most preferred.  It is
382            // RECOMMENDED that the host, UDP-tunneled, and relayed TCP candidates
383            // have the direction-pref assigned as follows: 6 for active, 4 for
384            // passive, and 2 for S-O.  For the NAT-assisted and server reflexive
385            // candidates, the RECOMMENDED values are: 6 for S-O, 4 for active, and
386            // 2 for passive.
387            //
388            // (...)
389            //
390            // If any two candidates have the same type-preference and direction-
391            // pref, they MUST have a unique other-pref.  With this specification,
392            // this usually only happens with multi-homed hosts, in which case
393            // other-pref is the preference for the particular IP address from which
394            // the candidate was obtained.  When there is only a single IP address,
395            // this value SHOULD be set to the maximum allowed value (8191).
396            let other_pref: u16 = 8191;
397
398            let direction_pref: u16 = match self.candidate_type() {
399                CandidateType::Host | CandidateType::Relay => match self.tcp_type() {
400                    TcpType::Active => 6,
401                    TcpType::Passive => 4,
402                    TcpType::SimultaneousOpen => 2,
403                    TcpType::Unspecified => 0,
404                },
405                CandidateType::PeerReflexive | CandidateType::ServerReflexive => {
406                    match self.tcp_type() {
407                        TcpType::SimultaneousOpen => 6,
408                        TcpType::Active => 4,
409                        TcpType::Passive => 2,
410                        TcpType::Unspecified => 0,
411                    }
412                }
413                CandidateType::Unspecified => 0,
414            };
415
416            (1 << 13) * direction_pref + other_pref
417        } else {
418            DEFAULT_LOCAL_PREFERENCE
419        }
420    }
421}
422
423/// Creates a Candidate from its string representation.
424pub fn unmarshal_candidate(raw: &str) -> Result<Candidate> {
425    let split: Vec<&str> = raw.split_whitespace().collect();
426    if split.len() < 8 {
427        return Err(Error::Other(format!(
428            "{:?} ({})",
429            Error::ErrAttributeTooShortIceCandidate,
430            split.len()
431        )));
432    }
433
434    // Foundation
435    let foundation = split[0].to_owned();
436
437    // Component
438    let component: u16 = split[1].parse()?;
439
440    // Network
441    let network = split[2].to_owned();
442
443    // Priority
444    let priority: u32 = split[3].parse()?;
445
446    // Address
447    let address = split[4].to_owned();
448
449    // Port
450    let port: u16 = split[5].parse()?;
451
452    let typ = split[7];
453
454    let mut rel_addr = String::new();
455    let mut rel_port = 0;
456    let mut tcp_type = TcpType::Unspecified;
457
458    if split.len() > 8 {
459        let split2 = &split[8..];
460
461        if split2[0] == "raddr" {
462            if split2.len() < 4 {
463                return Err(Error::Other(format!(
464                    "{:?}: incorrect length",
465                    Error::ErrParseRelatedAddr
466                )));
467            }
468
469            // RelatedAddress
470            split2[1].clone_into(&mut rel_addr);
471
472            // RelatedPort
473            rel_port = split2[3].parse()?;
474        } else if split2[0] == "tcptype" {
475            if split2.len() < 2 {
476                return Err(Error::Other(format!(
477                    "{:?}: incorrect length",
478                    Error::ErrParseType
479                )));
480            }
481
482            tcp_type = TcpType::from(split2[1]);
483        }
484    }
485
486    match typ {
487        "host" => {
488            let config = CandidateHostConfig {
489                base_config: CandidateConfig {
490                    network,
491                    address,
492                    port,
493                    component,
494                    priority,
495                    foundation,
496                    ..CandidateConfig::default()
497                },
498                tcp_type,
499            };
500            config.new_candidate_host()
501        }
502        "srflx" => {
503            let config = CandidateServerReflexiveConfig {
504                base_config: CandidateConfig {
505                    network,
506                    address,
507                    port,
508                    component,
509                    priority,
510                    foundation,
511                    ..CandidateConfig::default()
512                },
513                rel_addr,
514                rel_port,
515            };
516            config.new_candidate_server_reflexive()
517        }
518        "prflx" => {
519            let config = CandidatePeerReflexiveConfig {
520                base_config: CandidateConfig {
521                    network,
522                    address,
523                    port,
524                    component,
525                    priority,
526                    foundation,
527                    ..CandidateConfig::default()
528                },
529                rel_addr,
530                rel_port,
531            };
532
533            config.new_candidate_peer_reflexive()
534        }
535        "relay" => {
536            let config = CandidateRelayConfig {
537                base_config: CandidateConfig {
538                    network,
539                    address,
540                    port,
541                    component,
542                    priority,
543                    foundation,
544                    ..CandidateConfig::default()
545                },
546                rel_addr,
547                rel_port,
548            };
549            config.new_candidate_relay()
550        }
551        _ => Err(Error::Other(format!(
552            "{:?} ({})",
553            Error::ErrUnknownCandidateType,
554            typ
555        ))),
556    }
557}