webrtc_ice/mdns/
mod.rs

1#[cfg(test)]
2mod mdns_test;
3
4use std::net::SocketAddr;
5use std::str::FromStr;
6use std::sync::Arc;
7
8use mdns::config::*;
9use mdns::conn::*;
10use uuid::Uuid;
11
12use crate::error::Result;
13
14/// Represents the different Multicast modes that ICE can run.
15#[derive(PartialEq, Eq, Debug, Copy, Clone)]
16pub enum MulticastDnsMode {
17    /// Means remote mDNS candidates will be discarded, and local host candidates will use IPs.
18    Disabled,
19
20    /// Means remote mDNS candidates will be accepted, and local host candidates will use IPs.
21    QueryOnly,
22
23    /// Means remote mDNS candidates will be accepted, and local host candidates will use mDNS.
24    QueryAndGather,
25}
26
27impl Default for MulticastDnsMode {
28    fn default() -> Self {
29        Self::QueryOnly
30    }
31}
32
33pub(crate) fn generate_multicast_dns_name() -> String {
34    // https://tools.ietf.org/id/draft-ietf-rtcweb-mdns-ice-candidates-02.html#gathering
35    // The unique name MUST consist of a version 4 UUID as defined in [RFC4122], followed by “.local”.
36    let u = Uuid::new_v4();
37    format!("{u}.local")
38}
39
40pub(crate) fn create_multicast_dns(
41    mdns_mode: MulticastDnsMode,
42    mdns_name: &str,
43    dest_addr: &str,
44) -> Result<Option<Arc<DnsConn>>> {
45    let local_names = match mdns_mode {
46        MulticastDnsMode::QueryOnly => vec![],
47        MulticastDnsMode::QueryAndGather => vec![mdns_name.to_owned()],
48        MulticastDnsMode::Disabled => return Ok(None),
49    };
50
51    let addr = if dest_addr.is_empty() {
52        //TODO: why DEFAULT_DEST_ADDR doesn't work on Mac/Win?
53        if cfg!(target_os = "linux") {
54            SocketAddr::from_str(DEFAULT_DEST_ADDR)?
55        } else {
56            SocketAddr::from_str("0.0.0.0:5353")?
57        }
58    } else {
59        SocketAddr::from_str(dest_addr)?
60    };
61    log::info!("mDNS is using {} as dest_addr", addr);
62
63    let conn = DnsConn::server(
64        addr,
65        Config {
66            local_names,
67            ..Config::default()
68        },
69    )?;
70    Ok(Some(Arc::new(conn)))
71}