Bug 1186590 - Part 2 - Move hard-coded interface priority list into nrinterfaceprioritizer, and simplify some functions. r=drno

This commit is contained in:
Byron Campen [:bwc] 2015-09-04 15:25:37 -05:00
parent 38d87bbb95
commit 3e3e4965ea
9 changed files with 105 additions and 69 deletions

View File

@ -378,7 +378,6 @@ void NrIceCtx::trickle_cb(void *arg, nr_ice_ctx *ice_ctx,
RefPtr<NrIceCtx> NrIceCtx::Create(const std::string& name,
bool offerer,
bool set_interface_priorities,
bool allow_loopback,
bool tcp_enabled,
bool allow_link_local,
@ -404,33 +403,6 @@ RefPtr<NrIceCtx> NrIceCtx::Create(const std::string& name,
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);
if (set_interface_priorities) {
NR_reg_set_uchar((char *)"ice.pref.interface.rl0", 255);
NR_reg_set_uchar((char *)"ice.pref.interface.wi0", 254);
NR_reg_set_uchar((char *)"ice.pref.interface.lo0", 253);
NR_reg_set_uchar((char *)"ice.pref.interface.en1", 252);
NR_reg_set_uchar((char *)"ice.pref.interface.en0", 251);
NR_reg_set_uchar((char *)"ice.pref.interface.eth0", 252);
NR_reg_set_uchar((char *)"ice.pref.interface.eth1", 251);
NR_reg_set_uchar((char *)"ice.pref.interface.eth2", 249);
NR_reg_set_uchar((char *)"ice.pref.interface.ppp", 250);
NR_reg_set_uchar((char *)"ice.pref.interface.ppp0", 249);
NR_reg_set_uchar((char *)"ice.pref.interface.en2", 248);
NR_reg_set_uchar((char *)"ice.pref.interface.en3", 247);
NR_reg_set_uchar((char *)"ice.pref.interface.em0", 251);
NR_reg_set_uchar((char *)"ice.pref.interface.em1", 252);
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet0", 240);
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet1", 241);
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet3", 239);
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet4", 238);
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet5", 237);
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet6", 236);
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet7", 235);
NR_reg_set_uchar((char *)"ice.pref.interface.vmnet8", 234);
NR_reg_set_uchar((char *)"ice.pref.interface.virbr0", 233);
NR_reg_set_uchar((char *)"ice.pref.interface.wlan0", 232);
}
int32_t stun_client_maximum_transmits = 7;
int32_t ice_trickle_grace_period = 5000;
int32_t ice_tcp_so_sock_count = 3;

View File

@ -216,7 +216,6 @@ class NrIceCtx {
// TODO(ekr@rtfm.com): Too many bools here. Bug 1193437.
static RefPtr<NrIceCtx> Create(const std::string& name,
bool offerer,
bool set_interface_priorities = true,
bool allow_loopback = false,
bool tcp_enabled = true,
bool allow_link_local = false,

View File

@ -1,9 +1,11 @@
/* 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/. */
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "logging.h"
#include "nrinterfaceprioritizer.h"
#include "nsCOMPtr.h"
@ -15,20 +17,32 @@ namespace {
class LocalAddress {
public:
LocalAddress()
: key_(),
: ifname_(),
addr_(),
key_(),
is_vpn_(-1),
estimated_speed_(-1),
type_preference_(-1),
ip_version_(-1) {}
bool Init(const nr_local_addr& local_addr) {
ifname_ = local_addr.addr.ifname;
char buf[MAXIFNAME + 41];
int r = nr_transport_addr_fmt_ifname_addr_string(&local_addr.addr, buf, sizeof(buf));
if (r) {
MOZ_MTLOG(ML_ERROR, "Error formatting interface address string.");
MOZ_MTLOG(ML_ERROR, "Error formatting interface key.");
return false;
}
key_ = buf;
r = nr_transport_addr_get_addrstring(&local_addr.addr, buf, sizeof(buf));
if (r) {
MOZ_MTLOG(ML_ERROR, "Error formatting address string.");
return false;
}
addr_ = buf;
is_vpn_ = (local_addr.interface.type & NR_INTERFACE_TYPE_VPN) != 0 ? 1 : 0;
estimated_speed_ = local_addr.interface.estimated_speed;
type_preference_ = GetNetworkTypePreference(local_addr.interface.type);
@ -56,13 +70,28 @@ public:
return estimated_speed_ > rhs.estimated_speed_;
}
// See if our hard-coded pref list helps us.
auto thisindex = std::find(interface_preference_list().begin(),
interface_preference_list().end(),
ifname_);
auto rhsindex = std::find(interface_preference_list().begin(),
interface_preference_list().end(),
rhs.ifname_);
if (thisindex != rhsindex) {
return thisindex < rhsindex;
}
// Prefer IPV6 over IPV4
if (ip_version_ != rhs.ip_version_) {
return ip_version_ > rhs.ip_version_;
}
// All things above are the same, we can at least sort with key.
return key_ < rhs.key_;
// Now we start getting into arbitrary stuff
if (ifname_ != rhs.ifname_) {
return ifname_ < rhs.ifname_;
}
return addr_ < rhs.addr_;
}
const std::string& GetKey() const {
@ -85,6 +114,46 @@ private:
return 4;
}
// TODO(bug 895790): Once we can get useful interface properties on Darwin,
// we should remove this stuff.
static const std::vector<std::string>& interface_preference_list()
{
static std::vector<std::string> list(build_interface_preference_list());
return list;
}
static std::vector<std::string> build_interface_preference_list()
{
std::vector<std::string> result;
result.push_back("rl0");
result.push_back("wi0");
result.push_back("en0");
result.push_back("en1");
result.push_back("en2");
result.push_back("en3");
result.push_back("eth0");
result.push_back("eth1");
result.push_back("eth2");
result.push_back("em1");
result.push_back("em0");
result.push_back("ppp");
result.push_back("ppp0");
result.push_back("vmnet1");
result.push_back("vmnet0");
result.push_back("vmnet3");
result.push_back("vmnet4");
result.push_back("vmnet5");
result.push_back("vmnet6");
result.push_back("vmnet7");
result.push_back("vmnet8");
result.push_back("virbr0");
result.push_back("wlan0");
result.push_back("lo0");
return result;
}
std::string ifname_;
std::string addr_;
std::string key_;
int is_vpn_;
int estimated_speed_;

View File

@ -88,11 +88,10 @@ namespace {
enum TrickleMode { TRICKLE_NONE, TRICKLE_SIMULATE, TRICKLE_REAL };
const unsigned int ICE_TEST_PEER_OFFERER = (1 << 0);
const unsigned int ICE_TEST_PEER_SET_PRIORITIES = (1 << 1);
const unsigned int ICE_TEST_PEER_ALLOW_LOOPBACK = (1 << 2);
const unsigned int ICE_TEST_PEER_ENABLED_TCP = (1 << 3);
const unsigned int ICE_TEST_PEER_ALLOW_LINK_LOCAL = (1 << 4);
const unsigned int ICE_TEST_PEER_HIDE_NON_DEFAULT = (1 << 5);
const unsigned int ICE_TEST_PEER_ALLOW_LOOPBACK = (1 << 1);
const unsigned int ICE_TEST_PEER_ENABLED_TCP = (1 << 2);
const unsigned int ICE_TEST_PEER_ALLOW_LINK_LOCAL = (1 << 3);
const unsigned int ICE_TEST_PEER_HIDE_NON_DEFAULT = (1 << 4);
typedef std::string (*CandidateFilter)(const std::string& candidate);
@ -259,11 +258,11 @@ class IceTestPeer : public sigslot::has_slots<> {
public:
// TODO(ekr@rtfm.com): Convert to flags when NrIceCtx::Create() does.
// Bug 1193437.
IceTestPeer(const std::string& name, bool offerer, bool set_priorities,
IceTestPeer(const std::string& name, bool offerer,
bool allow_loopback = false, bool enable_tcp = true,
bool allow_link_local = false, bool hide_non_default = false) :
name_(name),
ice_ctx_(NrIceCtx::Create(name, offerer, set_priorities, allow_loopback,
ice_ctx_(NrIceCtx::Create(name, offerer, allow_loopback,
enable_tcp, allow_link_local, hide_non_default)),
streams_(),
candidates_(),
@ -1160,7 +1159,6 @@ class IceGatherTest : public ::testing::Test {
if (!peer_) {
peer_ = new IceTestPeer("P1",
flags & ICE_TEST_PEER_OFFERER,
flags & ICE_TEST_PEER_SET_PRIORITIES,
flags & ICE_TEST_PEER_ALLOW_LOOPBACK,
flags & ICE_TEST_PEER_ENABLED_TCP,
flags & ICE_TEST_PEER_ALLOW_LINK_LOCAL,
@ -1329,7 +1327,7 @@ class IceConnectTest : public ::testing::Test {
}
void AddStream(const std::string& name, int components) {
Init(false, false, false);
Init(false, false);
p1_->AddStream(components);
p2_->AddStream(components);
}
@ -1339,12 +1337,11 @@ class IceConnectTest : public ::testing::Test {
p2_->RemoveStream(index);
}
void Init(bool set_priorities, bool allow_loopback, bool enable_tcp,
bool default_only = false) {
void Init(bool allow_loopback, bool enable_tcp, bool default_only = false) {
if (!initted_) {
p1_ = new IceTestPeer("P1", true, set_priorities, allow_loopback,
p1_ = new IceTestPeer("P1", true, allow_loopback,
enable_tcp, false, default_only);
p2_ = new IceTestPeer("P2", false, set_priorities, allow_loopback,
p2_ = new IceTestPeer("P2", false, allow_loopback,
enable_tcp, false, default_only);
}
initted_ = true;
@ -1352,7 +1349,7 @@ class IceConnectTest : public ::testing::Test {
bool Gather(unsigned int waitTime = kDefaultTimeout,
bool setupStunServers = true) {
Init(false, false, false);
Init(false, false);
if (use_nat_) {
// If we enable nat simulation, but still use a real STUN server somewhere
// on the internet, we will see failures if there is a real NAT in
@ -1706,7 +1703,7 @@ TEST_F(IceGatherTest, TestGatherStunServerIpAddressDefaultRouteOnly) {
return;
}
peer_ = new IceTestPeer("P1", true, false, false, false, false, true);
peer_ = new IceTestPeer("P1", true, false, false, false, true);
peer_->AddStream(1);
peer_->SetStunServer(g_stun_server_address, kDefaultStunServerPort);
peer_->SetFakeResolver();
@ -1895,7 +1892,7 @@ TEST_F(IceGatherTest, TestGatherVerifyNoLoopback) {
TEST_F(IceGatherTest, TestGatherAllowLoopback) {
// Set up peer with loopback allowed.
peer_ = new IceTestPeer("P1", true, false, true);
peer_ = new IceTestPeer("P1", true, true);
peer_->AddStream(1);
Gather();
ASSERT_TRUE(StreamHasMatchingCandidate(0, "127.0.0.1"));
@ -1903,7 +1900,7 @@ TEST_F(IceGatherTest, TestGatherAllowLoopback) {
TEST_F(IceGatherTest, TestGatherTcpDisabled) {
// Set up peer with tcp disabled.
peer_ = new IceTestPeer("P1", true, false, false, false);
peer_ = new IceTestPeer("P1", true, false, false);
peer_->AddStream(1);
Gather();
ASSERT_FALSE(StreamHasMatchingCandidate(0, " TCP "));
@ -2007,7 +2004,7 @@ TEST_F(IceGatherTest, TestStunServerTrickle) {
// Test default route only with our fake STUN server and
// apparently NATted.
TEST_F(IceGatherTest, TestFakeStunServerNatedDefaultRouteOnly) {
peer_ = new IceTestPeer("P1", true, false, false, false, false, true);
peer_ = new IceTestPeer("P1", true, false, false, false, true);
peer_->AddStream(1);
UseFakeStunUdpServerWithResponse("192.0.2.1", 3333);
Gather(0);
@ -2025,7 +2022,7 @@ TEST_F(IceGatherTest, TestFakeStunServerNatedDefaultRouteOnly) {
// Test default route only with our fake STUN server and
// apparently non-NATted.
TEST_F(IceGatherTest, TestFakeStunServerNoNatDefaultRouteOnly) {
peer_ = new IceTestPeer("P1", true, false, false, false, false, true);
peer_ = new IceTestPeer("P1", true, false, false, false, true);
peer_->AddStream(1);
UseTestStunServer();
Gather(0);
@ -2065,13 +2062,13 @@ TEST_F(IceConnectTest, TestGather) {
}
TEST_F(IceConnectTest, TestGatherTcp) {
Init(false, false, true);
Init(false, true);
AddStream("first", 1);
ASSERT_TRUE(Gather());
}
TEST_F(IceConnectTest, TestGatherAutoPrioritize) {
Init(false, false, false);
Init(false, false);
AddStream("first", 1);
ASSERT_TRUE(Gather());
}
@ -2084,7 +2081,7 @@ TEST_F(IceConnectTest, TestConnect) {
}
TEST_F(IceConnectTest, TestConnectTcp) {
Init(false, false, true);
Init(false, true);
AddStream("first", 1);
ASSERT_TRUE(Gather());
SetCandidateFilter(IsTcpCandidate);
@ -2096,7 +2093,7 @@ TEST_F(IceConnectTest, TestConnectTcp) {
//TCP SO tests works on localhost only with delay applied:
// tc qdisc add dev lo root netem delay 10ms
TEST_F(IceConnectTest, DISABLED_TestConnectTcpSo) {
Init(false, false, true);
Init(false, true);
AddStream("first", 1);
ASSERT_TRUE(Gather());
SetCandidateFilter(IsTcpSoCandidate);
@ -2107,7 +2104,7 @@ TEST_F(IceConnectTest, DISABLED_TestConnectTcpSo) {
// Disabled because this breaks with hairpinning.
TEST_F(IceConnectTest, DISABLED_TestConnectDefaultRouteOnly) {
Init(false, false, false, true);
Init(false, false, true);
AddStream("first", 1);
ASSERT_TRUE(Gather());
SetExpectedTypes(NrIceCandidate::Type::ICE_SERVER_REFLEXIVE,
@ -2116,7 +2113,7 @@ TEST_F(IceConnectTest, DISABLED_TestConnectDefaultRouteOnly) {
}
TEST_F(IceConnectTest, TestLoopbackOnlySortOf) {
Init(false, true, false);
Init(true, false);
AddStream("first", 1);
SetCandidateFilter(IsLoopbackCandidate);
ASSERT_TRUE(Gather());
@ -2199,7 +2196,7 @@ TEST_F(IceConnectTest, TestGatherFullCone) {
}
TEST_F(IceConnectTest, TestGatherFullConeAutoPrioritize) {
Init(false, true, false);
Init(true, false);
AddStream("first", 1);
UseNat();
SetFilteringType(TestNat::ENDPOINT_INDEPENDENT);
@ -2220,7 +2217,7 @@ TEST_F(IceConnectTest, TestConnectFullCone) {
}
TEST_F(IceConnectTest, TestConnectNoNatRouteOnly) {
Init(false, false, false, true);
Init(false, false, true);
AddStream("first", 1);
UseTestStunServer();
// Because we are connecting from our host candidate to the
@ -2233,7 +2230,7 @@ TEST_F(IceConnectTest, TestConnectNoNatRouteOnly) {
}
TEST_F(IceConnectTest, TestConnectFullConeDefaultRouteOnly) {
Init(false, false, false, true);
Init(false, false, true);
AddStream("first", 1);
UseNat();
SetFilteringType(TestNat::ENDPOINT_INDEPENDENT);
@ -2406,7 +2403,7 @@ TEST_F(IceConnectTest, TestConnectP2ThenP1TrickleTwoComponents) {
}
TEST_F(IceConnectTest, TestConnectAutoPrioritize) {
Init(false, false, false);
Init(false, false);
AddStream("first", 1);
ASSERT_TRUE(Gather());
Connect();
@ -2589,7 +2586,7 @@ TEST_F(IceConnectTest, TestSendReceive) {
}
TEST_F(IceConnectTest, TestSendReceiveTcp) {
Init(false, false, true);
Init(false, true);
AddStream("first", 1);
ASSERT_TRUE(Gather());
SetCandidateFilter(IsTcpCandidate);
@ -2602,7 +2599,7 @@ TEST_F(IceConnectTest, TestSendReceiveTcp) {
//TCP SO tests works on localhost only with delay applied:
// tc qdisc add dev lo root netem delay 10ms
TEST_F(IceConnectTest, DISABLED_TestSendReceiveTcpSo) {
Init(false, false, true);
Init(false, true);
AddStream("first", 1);
ASSERT_TRUE(Gather());
SetCandidateFilter(IsTcpSoCandidate);

View File

@ -539,7 +539,7 @@ int TestStunTcpServer::TryOpenListenSocket(nr_local_addr* addr, uint16_t port) {
return r;
if (ice_ctx_ == NULL)
ice_ctx_ = NrIceCtx::Create("stun", false, false, false, false, false, false);
ice_ctx_ = NrIceCtx::Create("stun", false, false, false, false, false);
//TODO (nils@mozilla.com) can we replace this with a more basic TCP socket
// alternative which would allow us to remove the framing argument from the

View File

@ -504,7 +504,7 @@ int main(int argc, char **argv)
std::string dummy("dummy");
RUN_ON_THREAD(test_utils->sts_target(),
WrapRunnableNM(&NrIceCtx::Create,
dummy, false, false, false, false, false, false,
dummy, false, false, false, false, false,
NrIceCtx::ICE_POLICY_ALL),
NS_DISPATCH_SYNC);

View File

@ -256,7 +256,7 @@ int nr_ip6_port_to_transport_addr(struct in6_addr* addr6, UINT2 port, int protoc
return(_status);
}
int nr_transport_addr_get_addrstring(nr_transport_addr *addr, char *str, int maxlen)
int nr_transport_addr_get_addrstring(const nr_transport_addr *addr, char *str, int maxlen)
{
int _status;
const char *res;

View File

@ -78,7 +78,7 @@ int nr_ip4_port_to_transport_addr(UINT4 ip4, UINT2 port, int protocol, nr_transp
int nr_str_port_to_transport_addr(const char *str, UINT2 port, int protocol, nr_transport_addr *addr);
int nr_ip6_port_to_transport_addr(struct in6_addr* addr6, UINT2 port, int protocol, nr_transport_addr *addr);
int nr_transport_addr_get_addrstring(nr_transport_addr *addr, char *str, int maxlen);
int nr_transport_addr_get_addrstring(const nr_transport_addr *addr, char *str, int maxlen);
int nr_transport_addr_get_port(nr_transport_addr *addr, int *port);
int nr_transport_addr_cmp(nr_transport_addr *addr1,nr_transport_addr *addr2,int mode);
#define NR_TRANSPORT_ADDR_CMP_MODE_VERSION 1

View File

@ -316,7 +316,6 @@ nsresult PeerConnectionMedia::Init(const std::vector<NrIceStunServer>& stun_serv
// Looks like a bug in the NrIceCtx API.
mIceCtx = NrIceCtx::Create("PC:" + mParentName,
true, // Offerer
true, // Explicitly set priorities
mParent->GetAllowIceLoopback(),
ice_tcp,
mParent->GetAllowIceLinkLocal(),