Bug 1554976 - Use mdns_service to generate UUIDs; r=mjf
authorDan Minor <dminor@mozilla.com>
Wed, 28 Aug 2019 19:50:44 +0000
changeset 551128 691cab7deca800994aa0f2011423b1862afd9015
parent 551127 3d3731561f2dcaf678d35219b0a24db252ed6e71
child 551129 fa71a90e3ed05141226905f6fac21dbeb293b3d4
push id11865
push userbtara@mozilla.com
push dateMon, 02 Sep 2019 08:54:37 +0000
treeherdermozilla-beta@37f59c4671b3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmjf
bugs1554976
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1554976 - Use mdns_service to generate UUIDs; r=mjf With the move to the socket process, the UUID service is no longer available in nricectx. This adds a pair of helper functions to mdns_service to generate UUIDs and uses them to generate hostnames inside nricectx. Differential Revision: https://phabricator.services.mozilla.com/D42150
Cargo.lock
media/mtransport/mdns_service/Cargo.toml
media/mtransport/mdns_service/mdns_service.h
media/mtransport/mdns_service/src/lib.rs
media/mtransport/nricectx.cpp
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1730,16 +1730,17 @@ source = "registry+https://github.com/ru
 [[package]]
 name = "mdns_service"
 version = "0.1.0"
 dependencies = [
  "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "dns-parser 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.4.6 (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]]
 name = "memchr"
 version = "1.0.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)",
--- a/media/mtransport/mdns_service/Cargo.toml
+++ b/media/mtransport/mdns_service/Cargo.toml
@@ -4,8 +4,9 @@ version = "0.1.0"
 authors = ["Dan Minor <dminor@mozilla.com>"]
 edition = "2018"
 
 [dependencies]
 byteorder = "1.3.1"
 dns-parser = "0.8.0"
 log = "0.4"
 socket2 = { version = "0.3.9", features = ["reuseport"] }
+uuid = { version = "0.7", features = ["v4"] }
--- a/media/mtransport/mdns_service/mdns_service.h
+++ b/media/mtransport/mdns_service/mdns_service.h
@@ -11,9 +11,13 @@ void mdns_service_register_hostname(MDNS
                                     const char* addr);
 
 MDNSService* mdns_service_start();
 
 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"
--- a/media/mtransport/mdns_service/src/lib.rs
+++ b/media/mtransport/mdns_service/src/lib.rs
@@ -2,22 +2,24 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 use byteorder::{BigEndian, WriteBytesExt};
 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;
 
 // This code is derived from code for creating questions in the dns-parser
 // crate. It would be nice to upstream this, or something similar.
 fn create_answer(id: u16, answers: &Vec<(String, &[u8])>) -> Result<Vec<u8>, &'static str> {
     let mut buf = Vec::with_capacity(512);
@@ -296,8 +298,26 @@ pub extern "C" fn mdns_service_unregiste
     serv: *mut MDNSService,
     hostname: *const c_char,
 ) {
     unsafe {
         let hostname = CStr::from_ptr(hostname).to_string_lossy();
         (*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);
+    }
+}
--- a/media/mtransport/nricectx.cpp
+++ b/media/mtransport/nricectx.cpp
@@ -92,16 +92,20 @@ extern "C" {
 // Local includes
 #include "nricectx.h"
 #include "nricemediastream.h"
 #include "nr_socket_prsock.h"
 #include "nrinterfaceprioritizer.h"
 #include "rlogconnector.h"
 #include "test_nr_socket.h"
 
+extern "C" {
+#include "mdns_service/mdns_service.h"
+}
+
 namespace mozilla {
 
 using std::shared_ptr;
 
 TimeStamp nr_socket_short_term_violation_time() {
   return NrSocketBase::short_term_violation_time();
 }
 
@@ -1055,38 +1059,21 @@ void NrIceCtx::GenerateObfuscatedAddress
     }
 
     *actual_address = addr;
 
     const auto& iter = obfuscated_host_addresses_.find(*actual_address);
     if (iter != obfuscated_host_addresses_.end()) {
       *mdns_address = iter->second;
     } else {
-      nsresult rv;
-      nsCOMPtr<nsIUUIDGenerator> 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;
     }
     candidate->mdns_addr = r_strdup(mdns_address->c_str());
   }
 }
 
 }  // namespace mozilla