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 Unspecified, 18 19 /// Means remote mDNS candidates will be discarded, and local host candidates will use IPs. 20 Disabled, 21 22 /// Means remote mDNS candidates will be accepted, and local host candidates will use IPs. 23 QueryOnly, 24 25 /// Means remote mDNS candidates will be accepted, and local host candidates will use mDNS. 26 QueryAndGather, 27 } 28 29 impl Default for MulticastDnsMode { 30 fn default() -> Self { 31 Self::Unspecified 32 } 33 } 34 35 pub(crate) fn generate_multicast_dns_name() -> String { 36 // https://tools.ietf.org/id/draft-ietf-rtcweb-mdns-ice-candidates-02.html#gathering 37 // The unique name MUST consist of a version 4 UUID as defined in [RFC4122], followed by “.local”. 38 let u = Uuid::new_v4(); 39 format!("{}.local", u) 40 } 41 42 pub(crate) fn create_multicast_dns( 43 mdns_mode: MulticastDnsMode, 44 mdns_name: &str, 45 dest_addr: &str, 46 ) -> Result<Option<Arc<DnsConn>>> { 47 if mdns_mode == MulticastDnsMode::Disabled { 48 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 match mdns_mode { 64 MulticastDnsMode::QueryOnly => { 65 let conn = DnsConn::server(addr, Config::default())?; 66 Ok(Some(Arc::new(conn))) 67 } 68 MulticastDnsMode::QueryAndGather => { 69 let conn = DnsConn::server( 70 addr, 71 Config { 72 local_names: vec![mdns_name.to_owned()], 73 ..Config::default() 74 }, 75 )?; 76 Ok(Some(Arc::new(conn))) 77 } 78 _ => Ok(None), 79 } 80 } 81