diff --git a/Cargo.lock b/Cargo.lock index f825aa32949f..2341439c56c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1754,6 +1754,7 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "socket2 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)", + "uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/media/mtransport/mdns_service/Cargo.toml b/media/mtransport/mdns_service/Cargo.toml index 2e20c3875d55..11d29ba9d5a1 100644 --- a/media/mtransport/mdns_service/Cargo.toml +++ b/media/mtransport/mdns_service/Cargo.toml @@ -10,3 +10,4 @@ dns-parser = "0.8.0" log = "0.4" mio = "0.6" socket2 = { version = "0.3.9", features = ["reuseport"] } +uuid = { version = "0.7", features = ["v4"] } diff --git a/media/mtransport/mdns_service/mdns_service.h b/media/mtransport/mdns_service/mdns_service.h index cc826cf8dce6..b2b5bdffbfc6 100644 --- a/media/mtransport/mdns_service/mdns_service.h +++ b/media/mtransport/mdns_service/mdns_service.h @@ -16,4 +16,8 @@ void mdns_service_stop(MDNSService* serv); void mdns_service_unregister_hostname(MDNSService* serv, const char* hostname); +const char* mdns_service_generate_uuid(); + +void mdns_service_free_uuid(const char* uuid); + } // extern "C" diff --git a/media/mtransport/mdns_service/src/lib.rs b/media/mtransport/mdns_service/src/lib.rs index 4b1c152a25eb..6a9783a058a1 100644 --- a/media/mtransport/mdns_service/src/lib.rs +++ b/media/mtransport/mdns_service/src/lib.rs @@ -7,12 +7,14 @@ use get_if_addrs; use socket2::{Domain, Socket, Type}; use std::collections::HashMap; use std::ffi::CStr; +use std::ffi::CString; use std::io; use std::net; use std::os::raw::c_char; use std::sync::mpsc::channel; use std::thread; use std::time; +use uuid::Uuid; #[macro_use] extern crate log; @@ -301,3 +303,21 @@ pub extern "C" fn mdns_service_unregister_hostname( (*serv).unregister_hostname(&hostname); } } + +#[no_mangle] +pub extern "C" fn mdns_service_generate_uuid() -> *const c_char { + let uuid = Uuid::new_v4().to_hyphenated().to_string(); + match CString::new(uuid) { + Ok(uuid) => { + uuid.into_raw() + } + Err(_) => unreachable!() // UUID should not contain 0 byte + } +} + +#[no_mangle] +pub extern "C" fn mdns_service_free_uuid(uuid: *mut c_char) { + unsafe { + CString::from_raw(uuid); + } +} diff --git a/media/mtransport/nricectx.cpp b/media/mtransport/nricectx.cpp index 7da260bc46d0..cb82eee0df53 100644 --- a/media/mtransport/nricectx.cpp +++ b/media/mtransport/nricectx.cpp @@ -97,6 +97,10 @@ extern "C" { #include "rlogconnector.h" #include "test_nr_socket.h" +extern "C" { +#include "mdns_service/mdns_service.h" +} + namespace mozilla { using std::shared_ptr; @@ -1060,28 +1064,11 @@ void NrIceCtx::GenerateObfuscatedAddress(nr_ice_candidate* candidate, if (iter != obfuscated_host_addresses_.end()) { *mdns_address = iter->second; } else { - nsresult rv; - nsCOMPtr uuidgen = - do_GetService("@mozilla.org/uuid-generator;1", &rv); - - if (NS_FAILED(rv)) { - return; - } - - nsID id; - rv = uuidgen->GenerateUUIDInPlace(&id); - - if (NS_FAILED(rv)) { - return; - } - - char chars[NSID_LENGTH]; - id.ToProvidedString(chars); - + const char* uuid = mdns_service_generate_uuid(); std::ostringstream o; - chars[NSID_LENGTH - 2] = 0; // trim trailing } from uuid - o << &chars[1] << ".local"; // trim leading { from uuid + o << uuid << ".local"; *mdns_address = o.str(); + mdns_service_free_uuid(uuid); obfuscated_host_addresses_[*actual_address] = *mdns_address; }