Bug 1554976 - Use StunAddrs to set interface for mdns_service; r=mjf

The Rust get_if_addrs library previously used does not build on Android with
our build system. Since we're already using nICEr to determine the local
interface addresses, rather than fix the Rust library, we can use those
addresses to set the interface on which mdns_service listens.

Differential Revision: https://phabricator.services.mozilla.com/D42760

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Dan Minor 2019-08-28 13:12:26 +00:00
Родитель 2507ef392a
Коммит a81f8d6f24
5 изменённых файлов: 57 добавлений и 34 удалений

Просмотреть файл

@ -13,6 +13,10 @@
#include "../mdns_service/mdns_service.h"
extern "C" {
#include "local_addr.h"
}
using namespace mozilla::ipc;
namespace mozilla {
@ -24,16 +28,6 @@ StunAddrsRequestParent::StunAddrsRequestParent() : mIPCClosed(false) {
nsresult res;
mSTSThread = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &res);
MOZ_ASSERT(mSTSThread);
// This means that the mDNS service will continue running until shutdown
// once started. The StunAddrsRequestParent destructor does not run until
// shutdown anyway (see Bug 1569311), so there is not much we can do about
// this here. One option would be to add a check if there are no hostnames
// registered after UnregisterHostname is called, and if so, stop the mDNS
// service at that time (see Bug 1569955.)
if (!mSharedMDNSService) {
mSharedMDNSService = new MDNSServiceWrapper;
}
}
StunAddrsRequestParent::~StunAddrsRequestParent() {
@ -126,6 +120,26 @@ void StunAddrsRequestParent::SendStunAddrs_m(const NrIceStunAddrArray& addrs) {
return;
}
// This means that the mDNS service will continue running until shutdown
// once started. The StunAddrsRequestParent destructor does not run until
// shutdown anyway (see Bug 1569311), so there is not much we can do about
// this here. One option would be to add a check if there are no hostnames
// registered after UnregisterHostname is called, and if so, stop the mDNS
// service at that time (see Bug 1569955.)
if (!mSharedMDNSService) {
for (auto& addr : addrs) {
nr_transport_addr* local_addr =
const_cast<nr_transport_addr*>(&addr.localAddr().addr);
if (addr.localAddr().addr.ip_version == NR_IPV4 &&
!nr_transport_addr_is_loopback(local_addr)) {
char addrstring[16];
nr_transport_addr_get_addrstring(local_addr, addrstring, 16);
mSharedMDNSService = new MDNSServiceWrapper(addrstring);
break;
}
}
}
// send the new addresses back to the child
Unused << SendOnStunAddrsAvailable(addrs);
}
@ -136,6 +150,10 @@ StaticRefPtr<StunAddrsRequestParent::MDNSServiceWrapper>
NS_IMPL_ADDREF(StunAddrsRequestParent)
NS_IMPL_RELEASE(StunAddrsRequestParent)
StunAddrsRequestParent::MDNSServiceWrapper::MDNSServiceWrapper(
const std::string& ifaddr)
: ifaddr(ifaddr) {}
void StunAddrsRequestParent::MDNSServiceWrapper::RegisterHostname(
const char* hostname, const char* address) {
StartIfRequired();
@ -161,7 +179,7 @@ StunAddrsRequestParent::MDNSServiceWrapper::~MDNSServiceWrapper() {
void StunAddrsRequestParent::MDNSServiceWrapper::StartIfRequired() {
if (!mMDNSService) {
mMDNSService = mdns_service_start();
mMDNSService = mdns_service_start(ifaddr.c_str());
}
}

Просмотреть файл

@ -50,6 +50,7 @@ class StunAddrsRequestParent : public PStunAddrsRequestParent {
class MDNSServiceWrapper {
public:
explicit MDNSServiceWrapper(const std::string& ifaddr);
void RegisterHostname(const char* hostname, const char* address);
void UnregisterHostname(const char* hostname);
@ -64,6 +65,7 @@ class StunAddrsRequestParent : public PStunAddrsRequestParent {
virtual ~MDNSServiceWrapper();
void StartIfRequired();
std::string ifaddr;
MDNSService* mMDNSService = nullptr;
};

Просмотреть файл

@ -38,7 +38,21 @@ include("/ipc/chromium/chromium-config.mozbuild")
FINAL_LIBRARY = 'xul'
DEFINES['R_DEFINED_INT2'] = 'int16_t'
DEFINES['R_DEFINED_UINT2'] = 'uint16_t'
DEFINES['R_DEFINED_INT4'] = 'int32_t'
DEFINES['R_DEFINED_UINT4'] = 'uint32_t'
# These are defined to avoid a conflict between typedefs in winsock2.h and
# r_types.h. This is safe because these types are unused by the code here,
# but still deeply unfortunate. There is similar code in the win32 version of
# csi_platform.h, but that trick does not work here, even if that file is
# directly included.
DEFINES['R_DEFINED_INT8'] = 'int8_t'
DEFINES['R_DEFINED_UINT8'] = 'uint8_t'
LOCAL_INCLUDES += [
'/media/mtransport/third_party/nICEr/src/net',
'/media/mtransport/third_party/nrappkit/src/util/libekr',
'/media/webrtc',
'/media/webrtc/signaling/src/peerconnection',
'/netwerk/base',

Просмотреть файл

@ -10,7 +10,7 @@ extern "C" {
void mdns_service_register_hostname(MDNSService* serv, const char* hostname,
const char* addr);
MDNSService* mdns_service_start();
MDNSService* mdns_service_start(const char* ifaddr);
void mdns_service_stop(MDNSService* serv);

Просмотреть файл

@ -3,7 +3,6 @@
* 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;
@ -110,23 +109,7 @@ impl MDNSService {
}
}
fn get_ipv4_interface(&self) -> Option<std::net::Ipv4Addr> {
if let Ok(interfaces) = get_if_addrs::get_if_addrs() {
for interface in interfaces {
if let get_if_addrs::IfAddr::V4(addr) = &interface.addr {
if !interface.is_loopback() {
info!("Found interface {} -> {:?}", interface.name, interface.addr);
return Some(addr.ip);
}
}
}
}
None
}
fn start(&mut self) -> io::Result<()> {
let addr = self.get_ipv4_interface().unwrap();
fn start(&mut self, addr: std::net::Ipv4Addr) -> io::Result<()> {
let (sender, receiver) = channel();
self.sender = Some(sender);
@ -276,12 +259,18 @@ pub extern "C" fn mdns_service_register_hostname(
}
#[no_mangle]
pub extern "C" fn mdns_service_start() -> *mut MDNSService {
pub extern "C" fn mdns_service_start(ifaddr: *const c_char) -> *mut MDNSService {
let mut r = Box::new(MDNSService::new());
if let Err(err) = r.start() {
warn!("Could not start mDNS Service: {}", err);
unsafe {
let ifaddr = CStr::from_ptr(ifaddr).to_string_lossy();
if let Ok(addr) = ifaddr.parse() {
if let Err(err) = r.start(addr) {
warn!("Could not start mDNS Service: {}", err);
}
} else {
warn!("Could not parse interface address: {}", ifaddr);
}
}
Box::into_raw(r)
}