Bug 1193437: Use config structs in NrIceCtx, and centralize pref lookups some. r=mjf

Differential Revision: https://phabricator.services.mozilla.com/D82549
This commit is contained in:
Byron Campen [:bwc] 2020-07-09 15:36:07 +00:00
Родитель f498accc17
Коммит 9806d64c46
8 изменённых файлов: 614 добавлений и 233 удалений

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

@ -265,7 +265,7 @@ nsresult NrIceTurnServer::ToNicerTurnStruct(nr_ice_turn_server* server) const {
return NS_OK;
}
NrIceCtx::NrIceCtx(const std::string& name, Policy policy)
NrIceCtx::NrIceCtx(const std::string& name, const Config& aConfig)
: connection_state_(ICE_CTX_INIT),
gathering_state_(ICE_CTX_GATHER_INIT),
name_(name),
@ -276,19 +276,15 @@ NrIceCtx::NrIceCtx(const std::string& name, Policy policy)
ice_handler_vtbl_(nullptr),
ice_handler_(nullptr),
trickle_(true),
policy_(policy),
config_(aConfig),
nat_(nullptr),
proxy_config_(nullptr),
obfuscate_host_addresses_(false) {}
/* static */
RefPtr<NrIceCtx> NrIceCtx::Create(const std::string& name, bool allow_loopback,
bool tcp_enabled, bool allow_link_local,
Policy policy) {
// InitializeGlobals only executes once
NrIceCtx::InitializeGlobals(allow_loopback, tcp_enabled, allow_link_local);
RefPtr<NrIceCtx> ctx = new NrIceCtx(name, policy);
RefPtr<NrIceCtx> NrIceCtx::Create(const std::string& aName,
const Config& aConfig) {
RefPtr<NrIceCtx> ctx = new NrIceCtx(aName, aConfig);
if (!ctx->Initialize()) {
return nullptr;
@ -457,8 +453,7 @@ void NrIceCtx::trickle_cb(void* arg, nr_ice_ctx* ice_ctx,
s->SignalCandidate(s, candidate_str, stream->ufrag, mdns_addr, actual_addr);
}
void NrIceCtx::InitializeGlobals(bool allow_loopback, bool tcp_enabled,
bool allow_link_local) {
void NrIceCtx::InitializeGlobals(const GlobalConfig& aConfig) {
RLogConnector::CreateInstance();
// Initialize the crypto callbacks and logging stuff
if (!initialized) {
@ -476,57 +471,27 @@ void NrIceCtx::InitializeGlobals(bool allow_loopback, bool tcp_enabled,
NR_reg_set_uchar((char*)NR_ICE_REG_PREF_TYPE_PEER_RFLX_TCP, 109);
NR_reg_set_uchar((char*)NR_ICE_REG_PREF_TYPE_HOST_TCP, 125);
NR_reg_set_uchar((char*)NR_ICE_REG_PREF_TYPE_RELAYED_TCP, 0);
int32_t stun_client_maximum_transmits = 7;
int32_t ice_trickle_grace_period = 5000;
int32_t ice_tcp_so_sock_count = 3;
int32_t ice_tcp_listen_backlog = 10;
nsAutoCString force_net_interface;
nsresult res;
nsCOMPtr<nsIPrefService> prefs =
do_GetService("@mozilla.org/preferences-service;1", &res);
if (NS_SUCCEEDED(res)) {
nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(prefs);
if (branch) {
branch->GetIntPref(
"media.peerconnection.ice.stun_client_maximum_transmits",
&stun_client_maximum_transmits);
branch->GetIntPref("media.peerconnection.ice.trickle_grace_period",
&ice_trickle_grace_period);
branch->GetIntPref("media.peerconnection.ice.tcp_so_sock_count",
&ice_tcp_so_sock_count);
branch->GetIntPref("media.peerconnection.ice.tcp_listen_backlog",
&ice_tcp_listen_backlog);
branch->GetCharPref("media.peerconnection.ice.force_interface",
force_net_interface);
}
}
NR_reg_set_uint4((char*)"stun.client.maximum_transmits",
stun_client_maximum_transmits);
aConfig.mStunClientMaxTransmits);
NR_reg_set_uint4((char*)NR_ICE_REG_TRICKLE_GRACE_PERIOD,
ice_trickle_grace_period);
aConfig.mTrickleIceGracePeriod);
NR_reg_set_int4((char*)NR_ICE_REG_ICE_TCP_SO_SOCK_COUNT,
ice_tcp_so_sock_count);
aConfig.mIceTcpSoSockCount);
NR_reg_set_int4((char*)NR_ICE_REG_ICE_TCP_LISTEN_BACKLOG,
ice_tcp_listen_backlog);
aConfig.mIceTcpListenBacklog);
NR_reg_set_char((char*)NR_ICE_REG_ICE_TCP_DISABLE, !tcp_enabled);
NR_reg_set_char((char*)NR_ICE_REG_ICE_TCP_DISABLE, !aConfig.mTcpEnabled);
if (allow_loopback) {
if (aConfig.mAllowLoopback) {
NR_reg_set_char((char*)NR_STUN_REG_PREF_ALLOW_LOOPBACK_ADDRS, 1);
}
if (allow_link_local) {
if (aConfig.mAllowLinkLocal) {
NR_reg_set_char((char*)NR_STUN_REG_PREF_ALLOW_LINK_LOCAL_ADDRS, 1);
}
if (force_net_interface.Length() > 0) {
// Stupid cast.... but needed
const nsCString& flat =
PromiseFlatCString(static_cast<nsACString&>(force_net_interface));
if (!aConfig.mForceNetInterface.Length()) {
NR_reg_set_string((char*)NR_ICE_REG_PREF_FORCE_INTERFACE_NAME,
const_cast<char*>(flat.get()));
const_cast<char*>(aConfig.mForceNetInterface.get()));
}
}
}
@ -583,7 +548,7 @@ bool NrIceCtx::Initialize() {
int r;
UINT4 flags = NR_ICE_CTX_FLAGS_AGGRESSIVE_NOMINATION;
switch (policy_) {
switch (config_.mPolicy) {
case ICE_POLICY_RELAY:
flags |= NR_ICE_CTX_FLAGS_RELAY_ONLY;
break;
@ -632,38 +597,14 @@ bool NrIceCtx::Initialize() {
}
}
nsAutoCString mapping_type;
nsAutoCString filtering_type;
bool block_udp = false;
bool block_tcp = false;
nsresult rv;
nsCOMPtr<nsIPrefService> pref_service =
do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIPrefBranch> pref_branch;
rv = pref_service->GetBranch(nullptr, getter_AddRefs(pref_branch));
if (NS_SUCCEEDED(rv)) {
Unused << pref_branch->GetCharPref(
"media.peerconnection.nat_simulator.mapping_type", mapping_type);
Unused << pref_branch->GetCharPref(
"media.peerconnection.nat_simulator.filtering_type", filtering_type);
Unused << pref_branch->GetBoolPref(
"media.peerconnection.nat_simulator.block_udp", &block_udp);
Unused << pref_branch->GetBoolPref(
"media.peerconnection.nat_simulator.block_tcp", &block_tcp);
}
}
if (!mapping_type.IsEmpty() && !filtering_type.IsEmpty()) {
MOZ_MTLOG(ML_DEBUG, "NAT filtering type: " << filtering_type.get());
MOZ_MTLOG(ML_DEBUG, "NAT mapping type: " << mapping_type.get());
if (config_.mNatSimulatorConfig.isSome()) {
TestNat* test_nat = new TestNat;
test_nat->filtering_type_ = TestNat::ToNatBehavior(filtering_type.get());
test_nat->mapping_type_ = TestNat::ToNatBehavior(mapping_type.get());
test_nat->block_udp_ = block_udp;
test_nat->block_tcp_ = block_tcp;
test_nat->filtering_type_ = TestNat::ToNatBehavior(
config_.mNatSimulatorConfig->mFilteringType.get());
test_nat->mapping_type_ =
TestNat::ToNatBehavior(config_.mNatSimulatorConfig->mMappingType.get());
test_nat->block_udp_ = config_.mNatSimulatorConfig->mBlockUdp;
test_nat->block_tcp_ = config_.mNatSimulatorConfig->mBlockTcp;
test_nat->enabled_ = true;
SetNat(test_nat);
}
@ -692,6 +633,7 @@ bool NrIceCtx::Initialize() {
return false;
}
nsresult rv;
sts_target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
if (!NS_SUCCEEDED(rv)) return false;
@ -784,11 +726,6 @@ NrIceCtx::Controlling NrIceCtx::GetControlling() {
return (peer_->controlling) ? ICE_CONTROLLING : ICE_CONTROLLED;
}
nsresult NrIceCtx::SetPolicy(Policy policy) {
policy_ = policy;
return NS_OK;
}
nsresult NrIceCtx::SetStunServers(
const std::vector<NrIceStunServer>& stun_servers) {
if (stun_servers.empty()) return NS_OK;

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

@ -63,6 +63,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "mozilla/UniquePtr.h"
#include "nsIEventTarget.h"
#include "nsTArray.h"
#include "mozilla/Maybe.h"
#include "m_cpp_utils.h"
#include "nricestunaddr.h"
@ -201,20 +202,39 @@ class NrIceCtx {
enum Policy { ICE_POLICY_RELAY, ICE_POLICY_NO_HOST, ICE_POLICY_ALL };
static RefPtr<NrIceCtx> Create(
const std::string& name, bool allow_loopback = false,
bool tcp_enabled = true, bool allow_link_local = false,
NrIceCtx::Policy policy = NrIceCtx::ICE_POLICY_ALL);
struct NatSimulatorConfig {
bool mBlockTcp = false;
bool mBlockUdp = false;
nsCString mMappingType = "ENDPOINT_INDEPENDENT"_ns;
nsCString mFilteringType = "ENDPOINT_INDEPENDENT"_ns;
};
struct Config {
NrIceCtx::Policy mPolicy = NrIceCtx::ICE_POLICY_ALL;
Maybe<NatSimulatorConfig> mNatSimulatorConfig;
};
static RefPtr<NrIceCtx> Create(const std::string& aName,
const Config& aConfig);
RefPtr<NrIceMediaStream> CreateStream(const std::string& id,
const std::string& name,
int components);
void DestroyStream(const std::string& id);
struct GlobalConfig {
bool mAllowLinkLocal = false;
bool mAllowLoopback = false;
bool mTcpEnabled = true;
int mStunClientMaxTransmits = 7;
int mTrickleIceGracePeriod = 5000;
int mIceTcpSoSockCount = 3;
int mIceTcpListenBacklog = 10;
nsCString mForceNetInterface;
};
// initialize ICE globals, crypto, and logging
static void InitializeGlobals(bool allow_loopback = false,
bool tcp_enabled = true,
bool allow_link_local = false);
static void InitializeGlobals(const GlobalConfig& aConfig);
void SetTargetForDefaultLocalAddressLookup(const std::string& target_ip,
uint16_t target_port);
@ -278,12 +298,6 @@ class NrIceCtx {
Controlling GetControlling();
// Set whether we're allowed to produce none, relay or all candidates.
// TODO(jib@mozilla.com): Work out what change means mid-connection (1181768)
nsresult SetPolicy(Policy policy);
Policy policy() const { return policy_; }
// Set the STUN servers. Must be called before StartGathering
// (if at all).
nsresult SetStunServers(const std::vector<NrIceStunServer>& stun_servers);
@ -339,7 +353,7 @@ class NrIceCtx {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceCtx)
private:
NrIceCtx(const std::string& name, Policy policy);
NrIceCtx(const std::string& name, const Config& aConfig);
virtual ~NrIceCtx();
@ -388,7 +402,7 @@ class NrIceCtx {
nr_ice_handler* ice_handler_; // Must be pointer
bool trickle_;
nsCOMPtr<nsIEventTarget> sts_target_; // The thread to run on
Policy policy_;
Config config_;
RefPtr<TestNat> nat_;
std::shared_ptr<NrSocketProxyConfig> proxy_config_;
bool obfuscate_host_addresses_;

Разница между файлами не показана из-за своего большого размера Загрузить разницу

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

@ -43,7 +43,8 @@ class MultiTcpSocketTest : public MtransportTest {
void SetUp() {
MtransportTest::SetUp();
ice_ctx_ = NrIceCtx::Create("stun", true);
NrIceCtx::InitializeGlobals(NrIceCtx::GlobalConfig());
ice_ctx_ = NrIceCtx::Create("stun", NrIceCtx::Config());
test_utils_->sts_target()->Dispatch(
WrapRunnableNM(&TestStunTcpServer::GetInstance, AF_INET),

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

@ -307,7 +307,7 @@ class TestNrSocketIceUnitTest : public ::testing::Test {
test_utils_ = new MtransportTestUtils();
test_utils2_ = new MtransportTestUtils();
NrIceCtx::InitializeGlobals(false, false, false);
NrIceCtx::InitializeGlobals(NrIceCtx::GlobalConfig());
}
void TearDown() override {

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

@ -442,7 +442,7 @@ class TransportTestPeer : public sigslot::has_slots<> {
lossy_(new TransportLayerLossy()),
dtls_(new TransportLayerDtls()),
identity_(DtlsIdentity::Generate()),
ice_ctx_(NrIceCtx::Create(name)),
ice_ctx_(),
streams_(),
peer_(nullptr),
gathering_complete_(false),
@ -450,6 +450,8 @@ class TransportTestPeer : public sigslot::has_slots<> {
enabled_cipersuites_(),
disabled_cipersuites_(),
test_utils_(utils) {
NrIceCtx::InitializeGlobals(NrIceCtx::GlobalConfig());
ice_ctx_ = NrIceCtx::Create(name, NrIceCtx::Config());
std::vector<NrIceStunServer> stun_servers;
UniquePtr<NrIceStunServer> server(NrIceStunServer::Create(
std::string((char*)"stun.services.mozilla.com"), 3478));

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

@ -96,7 +96,7 @@ class TurnClient : public MtransportTest {
~TurnClient() = default;
static void SetUpTestCase() {
NrIceCtx::InitializeGlobals(false, false, false);
NrIceCtx::InitializeGlobals(NrIceCtx::GlobalConfig());
}
void SetTcp() { protocol_ = IPPROTO_TCP; }

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

@ -159,8 +159,8 @@ class MediaTransportHandlerSTS : public MediaTransportHandler,
RefPtr<NrIceResolver> mDNSResolver;
std::map<std::string, Transport> mTransports;
bool mObfuscateHostAddresses = false;
uint32_t minDtlsVersion = 0;
uint32_t maxDtlsVersion = 0;
uint32_t mMinDtlsVersion = 0;
uint32_t mMaxDtlsVersion = 0;
std::set<std::string> mSignaledAddresses;
@ -340,6 +340,56 @@ nsresult MediaTransportHandler::ConvertIceServers(
return NS_OK;
}
static NrIceCtx::GlobalConfig GetGlobalConfig() {
NrIceCtx::GlobalConfig config;
config.mAllowLinkLocal =
Preferences::GetBool("media.peerconnection.ice.link_local", false);
config.mAllowLoopback =
Preferences::GetBool("media.peerconnection.ice.loopback", false);
config.mTcpEnabled =
Preferences::GetBool("media.peerconnection.ice.tcp", false);
config.mStunClientMaxTransmits = Preferences::GetInt(
"media.peerconnection.ice.stun_client_maximum_transmits",
config.mStunClientMaxTransmits);
config.mTrickleIceGracePeriod =
Preferences::GetInt("media.peerconnection.ice.trickle_grace_period",
config.mTrickleIceGracePeriod);
config.mIceTcpSoSockCount = Preferences::GetInt(
"media.peerconnection.ice.tcp_so_sock_count", config.mIceTcpSoSockCount);
config.mIceTcpListenBacklog =
Preferences::GetInt("media.peerconnection.ice.tcp_listen_backlog",
config.mIceTcpListenBacklog);
(void)Preferences::GetCString("media.peerconnection.ice.force_interface",
config.mForceNetInterface);
return config;
}
static Maybe<NrIceCtx::NatSimulatorConfig> GetNatConfig() {
bool block_tcp = Preferences::GetBool(
"media.peerconnection.nat_simulator.block_tcp", false);
bool block_udp = Preferences::GetBool(
"media.peerconnection.nat_simulator.block_udp", false);
nsAutoCString mapping_type;
(void)Preferences::GetCString(
"media.peerconnection.nat_simulator.mapping_type", mapping_type);
nsAutoCString filtering_type;
(void)Preferences::GetCString(
"media.peerconnection.nat_simulator.filtering_type", filtering_type);
if (block_udp || block_tcp || !mapping_type.IsEmpty() ||
!filtering_type.IsEmpty()) {
CSFLogDebug(LOGTAG, "NAT filtering type: %s", filtering_type.get());
CSFLogDebug(LOGTAG, "NAT mapping type: %s", mapping_type.get());
NrIceCtx::NatSimulatorConfig natConfig;
natConfig.mBlockUdp = block_udp;
natConfig.mBlockTcp = block_tcp;
natConfig.mFilteringType = filtering_type;
natConfig.mMappingType = mapping_type;
return Some(natConfig);
}
return Nothing();
}
nsresult MediaTransportHandlerSTS::CreateIceCtx(
const std::string& aName, const nsTArray<dom::RTCIceServer>& aIceServers,
dom::RTCIceTransportPolicy aIcePolicy) {
@ -371,72 +421,80 @@ nsresult MediaTransportHandlerSTS::CreateIceCtx(
mozilla::psm::DisableMD5();
}
// This stuff will probably live on the other side of IPC; errors down
// here will either need to be ignored, or plumbed back in some way
// other than the return.
bool allowLoopback =
Preferences::GetBool("media.peerconnection.ice.loopback", false);
bool tcpEnabled =
Preferences::GetBool("media.peerconnection.ice.tcp", false);
bool allowLinkLocal =
Preferences::GetBool("media.peerconnection.ice.link_local", false);
mIceCtx = NrIceCtx::Create(aName, allowLoopback, tcpEnabled,
allowLinkLocal, toNrIcePolicy(aIcePolicy));
if (!mIceCtx) {
return InitPromise::CreateAndReject("NrIceCtx::Create failed",
__func__);
static bool globalInitDone = false;
if (!globalInitDone) {
mStsThread->Dispatch(
WrapRunnableNM(&NrIceCtx::InitializeGlobals, GetGlobalConfig()),
NS_DISPATCH_NORMAL);
globalInitDone = true;
}
mIceCtx->SignalGatheringStateChange.connect(
this, &MediaTransportHandlerSTS::OnGatheringStateChange);
mIceCtx->SignalConnectionStateChange.connect(
this, &MediaTransportHandlerSTS::OnConnectionStateChange);
nsresult rv;
if (NS_FAILED(rv = mIceCtx->SetStunServers(stunServers))) {
CSFLogError(LOGTAG, "%s: Failed to set stun servers", __FUNCTION__);
return InitPromise::CreateAndReject("Failed to set stun servers",
__func__);
}
// Give us a way to globally turn off TURN support
bool disabled =
bool turnDisabled =
Preferences::GetBool("media.peerconnection.turn.disable", false);
if (!disabled) {
if (NS_FAILED(rv = mIceCtx->SetTurnServers(turnServers))) {
CSFLogError(LOGTAG, "%s: Failed to set turn servers", __FUNCTION__);
return InitPromise::CreateAndReject("Failed to set turn servers",
__func__);
}
} else if (!turnServers.empty()) {
CSFLogError(LOGTAG, "%s: Setting turn servers disabled",
__FUNCTION__);
}
mDNSResolver = new NrIceResolver;
if (NS_FAILED(rv = mDNSResolver->Init())) {
CSFLogError(LOGTAG, "%s: Failed to initialize dns resolver",
__FUNCTION__);
return InitPromise::CreateAndReject(
"Failed to initialize dns resolver", __func__);
}
if (NS_FAILED(
rv = mIceCtx->SetResolver(mDNSResolver->AllocateResolver()))) {
CSFLogError(LOGTAG, "%s: Failed to get dns resolver", __FUNCTION__);
return InitPromise::CreateAndReject("Failed to get dns resolver",
__func__);
}
// We are reading these here, because when we setup the DTLS transport
// we are on the wrong thread to read prefs
minDtlsVersion =
mMinDtlsVersion =
Preferences::GetUint("media.peerconnection.dtls.version.min");
maxDtlsVersion =
mMaxDtlsVersion =
Preferences::GetUint("media.peerconnection.dtls.version.max");
CSFLogDebug(LOGTAG, "%s done", __func__);
return InitPromise::CreateAndResolve(true, __func__);
NrIceCtx::Config config;
config.mPolicy = toNrIcePolicy(aIcePolicy);
config.mNatSimulatorConfig = GetNatConfig();
return InvokeAsync(
mStsThread, __func__,
[=, self = RefPtr<MediaTransportHandlerSTS>(this)]() {
mIceCtx = NrIceCtx::Create(aName, config);
if (!mIceCtx) {
return InitPromise::CreateAndReject("NrIceCtx::Create failed",
__func__);
}
mIceCtx->SignalGatheringStateChange.connect(
this, &MediaTransportHandlerSTS::OnGatheringStateChange);
mIceCtx->SignalConnectionStateChange.connect(
this, &MediaTransportHandlerSTS::OnConnectionStateChange);
nsresult rv;
if (NS_FAILED(rv = mIceCtx->SetStunServers(stunServers))) {
CSFLogError(LOGTAG, "%s: Failed to set stun servers",
__FUNCTION__);
return InitPromise::CreateAndReject(
"Failed to set stun servers", __func__);
}
if (!turnDisabled) {
if (NS_FAILED(rv = mIceCtx->SetTurnServers(turnServers))) {
CSFLogError(LOGTAG, "%s: Failed to set turn servers",
__FUNCTION__);
return InitPromise::CreateAndReject(
"Failed to set turn servers", __func__);
}
} else if (!turnServers.empty()) {
CSFLogError(LOGTAG, "%s: Setting turn servers disabled",
__FUNCTION__);
}
mDNSResolver = new NrIceResolver;
if (NS_FAILED(rv = mDNSResolver->Init())) {
CSFLogError(LOGTAG, "%s: Failed to initialize dns resolver",
__FUNCTION__);
return InitPromise::CreateAndReject(
"Failed to initialize dns resolver", __func__);
}
if (NS_FAILED(rv = mIceCtx->SetResolver(
mDNSResolver->AllocateResolver()))) {
CSFLogError(LOGTAG, "%s: Failed to get dns resolver",
__FUNCTION__);
return InitPromise::CreateAndReject(
"Failed to get dns resolver", __func__);
}
CSFLogDebug(LOGTAG, "%s done", __func__);
return InitPromise::CreateAndResolve(true, __func__);
});
});
return NS_OK;
}
@ -1193,8 +1251,8 @@ RefPtr<TransportFlow> MediaTransportHandlerSTS::CreateTransportFlow(
dtls->SetIdentity(aDtlsIdentity);
dtls->SetMinMaxVersion(
static_cast<TransportLayerDtls::Version>(minDtlsVersion),
static_cast<TransportLayerDtls::Version>(maxDtlsVersion));
static_cast<TransportLayerDtls::Version>(mMinDtlsVersion),
static_cast<TransportLayerDtls::Version>(mMaxDtlsVersion));
for (const auto& digest : aDigests) {
rv = dtls->SetVerificationDigest(digest);