xref: /webrtc/ice/src/mdns/mod.rs (revision 2e07f543)
1 #[cfg(test)]
2 mod mdns_test;
3 
4 use crate::error::Result;
5 
6 use mdns::config::*;
7 use mdns::conn::*;
8 
9 use std::net::SocketAddr;
10 use std::str::FromStr;
11 use std::sync::Arc;
12 use uuid::Uuid;
13 
14 /// Represents the different Multicast modes that ICE can run.
15 #[derive(PartialEq, Eq, Debug, Copy, Clone)]
16 pub 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 
27 impl Default for MulticastDnsMode {
default() -> Self28     fn default() -> Self {
29         Self::QueryOnly
30     }
31 }
32 
generate_multicast_dns_name() -> String33 pub(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 
create_multicast_dns( mdns_mode: MulticastDnsMode, mdns_name: &str, dest_addr: &str, ) -> Result<Option<Arc<DnsConn>>>40 pub(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 }
72