rtc_ice/mdns/
mod.rs

1//#[cfg(test)]
2//mod mdns_test;
3
4use mdns::Mdns;
5use mdns::MdnsConfig;
6use std::net::{IpAddr, Ipv4Addr};
7use std::time::Duration;
8use uuid::Uuid;
9
10use shared::error::Result;
11
12/// Represents the different Multicast modes that ICE can run.
13#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
14pub enum MulticastDnsMode {
15    /// Means remote mDNS candidates will be discarded, and local host candidates will use IPs.
16    Disabled,
17
18    /// Means remote mDNS candidates will be accepted, and local host candidates will use IPs.
19    #[default]
20    QueryOnly,
21
22    /// Means remote mDNS candidates will be accepted, and local host candidates will use mDNS.
23    QueryAndGather,
24}
25
26pub(crate) fn generate_multicast_dns_name() -> String {
27    // https://tools.ietf.org/id/draft-ietf-rtcweb-mdns-ice-candidates-02.html#gathering
28    // The unique name MUST consist of a version 4 UUID as defined in [RFC4122], followed by “.local”.
29    let u = Uuid::new_v4();
30    format!("{u}.local")
31}
32
33pub(crate) fn create_multicast_dns(
34    mdns_mode: MulticastDnsMode,
35    mdns_local_name: &str,
36    mdns_local_ip: &Option<IpAddr>,
37    mdns_query_timeout: &Option<Duration>,
38) -> Result<Option<Mdns>> {
39    if mdns_mode == MulticastDnsMode::Disabled {
40        return Ok(None);
41    }
42
43    let mut config = if mdns_mode == MulticastDnsMode::QueryAndGather {
44        let local_ip = if let Some(local_ip) = mdns_local_ip {
45            *local_ip
46        } else {
47            IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))
48        };
49        log::info!("mDNS is using {local_ip} as local ip");
50
51        MdnsConfig::new()
52            .with_local_names(vec![mdns_local_name.to_owned()])
53            .with_local_ip(local_ip)
54    } else {
55        MdnsConfig::new()
56    };
57
58    if let Some(query_timeout) = mdns_query_timeout {
59        config = config.with_query_timeout(*query_timeout);
60    }
61
62    let mdns_server = Mdns::new(config);
63
64    Ok(Some(mdns_server))
65}