diff options
author | Taylor Brandstetter <deadbeef@webrtc.org> | 2015-12-29 14:14:52 -0800 |
---|---|---|
committer | Taylor Brandstetter <deadbeef@webrtc.org> | 2015-12-29 22:15:02 +0000 |
commit | 0c7e9f540b282d60b94081f601a1694054d8646e (patch) | |
tree | 22f5a36998dd1eb8c7591285b2cc3915e118fd82 /webrtc/p2p | |
parent | e86e15b2a2ee208e2d8edbb1c2d73274d9b8aec7 (diff) | |
download | webrtc-0c7e9f540b282d60b94081f601a1694054d8646e.tar.gz |
Removing webrtc::PortAllocatorFactoryInterface.
ICE servers are now passed directly into PortAllocator,
making PortAllocatorFactoryInterface redundant. This CL also
moves SetNetworkIgnoreMask to PortAllocator.
R=phoglund@webrtc.org, pthatcher@webrtc.org, tkchin@webrtc.org
Review URL: https://codereview.webrtc.org/1520963002 .
Cr-Commit-Position: refs/heads/master@{#11139}
Diffstat (limited to 'webrtc/p2p')
-rw-r--r-- | webrtc/p2p/base/portallocator.h | 18 | ||||
-rw-r--r-- | webrtc/p2p/client/basicportallocator.cc | 16 | ||||
-rw-r--r-- | webrtc/p2p/client/basicportallocator.h | 11 | ||||
-rw-r--r-- | webrtc/p2p/client/fakeportallocator.h | 2 | ||||
-rw-r--r-- | webrtc/p2p/client/portallocator_unittest.cc | 53 |
5 files changed, 95 insertions, 5 deletions
diff --git a/webrtc/p2p/base/portallocator.h b/webrtc/p2p/base/portallocator.h index 723a0000e5..6fb79b065e 100644 --- a/webrtc/p2p/base/portallocator.h +++ b/webrtc/p2p/base/portallocator.h @@ -91,6 +91,17 @@ typedef std::vector<ProtocolAddress> PortList; struct RelayServerConfig { RelayServerConfig(RelayType type) : type(type), priority(0) {} + RelayServerConfig(const std::string& address, + int port, + const std::string& username, + const std::string& password, + ProtocolType proto, + bool secure) + : type(RELAY_TURN), credentials(username, password) { + ports.push_back( + ProtocolAddress(rtc::SocketAddress(address, port), proto, secure)); + } + RelayType type; PortList ports; RelayCredentials credentials; @@ -168,6 +179,13 @@ class PortAllocator : public sigslot::has_slots<> { const ServerAddresses& stun_servers, const std::vector<RelayServerConfig>& turn_servers) = 0; + // Sets the network types to ignore. + // Values are defined by the AdapterType enum. + // For instance, calling this with + // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and + // loopback interfaces. + virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0; + PortAllocatorSession* CreateSession( const std::string& sid, const std::string& content_name, diff --git a/webrtc/p2p/client/basicportallocator.cc b/webrtc/p2p/client/basicportallocator.cc index a14f85b6f2..e45d2c8f0f 100644 --- a/webrtc/p2p/client/basicportallocator.cc +++ b/webrtc/p2p/client/basicportallocator.cc @@ -10,6 +10,7 @@ #include "webrtc/p2p/client/basicportallocator.h" +#include <algorithm> #include <string> #include <vector> @@ -70,15 +71,16 @@ BasicPortAllocator::BasicPortAllocator( : network_manager_(network_manager), socket_factory_(socket_factory), stun_servers_() { - ASSERT(socket_factory_ != NULL); + ASSERT(network_manager_ != nullptr); + ASSERT(socket_factory_ != nullptr); Construct(); } -BasicPortAllocator::BasicPortAllocator( - rtc::NetworkManager* network_manager) +BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager) : network_manager_(network_manager), - socket_factory_(NULL), + socket_factory_(nullptr), stun_servers_() { + ASSERT(network_manager_ != nullptr); Construct(); } @@ -327,6 +329,12 @@ void BasicPortAllocatorSession::GetNetworks( } else { network_manager->GetNetworks(networks); } + networks->erase(std::remove_if(networks->begin(), networks->end(), + [this](rtc::Network* network) { + return allocator_->network_ignore_mask() & + network->type(); + }), + networks->end()); } // For each network, see if we have a sequence that covers it already. If not, diff --git a/webrtc/p2p/client/basicportallocator.h b/webrtc/p2p/client/basicportallocator.h index 6c301de302..ca1a23aaf2 100644 --- a/webrtc/p2p/client/basicportallocator.h +++ b/webrtc/p2p/client/basicportallocator.h @@ -44,6 +44,16 @@ class BasicPortAllocator : public PortAllocator { turn_servers_ = turn_servers; } + // Set to kDefaultNetworkIgnoreMask by default. + void SetNetworkIgnoreMask(int network_ignore_mask) override { + // TODO(phoglund): implement support for other types than loopback. + // See https://code.google.com/p/webrtc/issues/detail?id=4288. + // Then remove set_network_ignore_list from NetworkManager. + network_ignore_mask_ = network_ignore_mask; + } + + int network_ignore_mask() const { return network_ignore_mask_; } + rtc::NetworkManager* network_manager() { return network_manager_; } // If socket_factory() is set to NULL each PortAllocatorSession @@ -75,6 +85,7 @@ class BasicPortAllocator : public PortAllocator { ServerAddresses stun_servers_; std::vector<RelayServerConfig> turn_servers_; bool allow_tcp_listen_; + int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask; }; struct PortConfiguration; diff --git a/webrtc/p2p/client/fakeportallocator.h b/webrtc/p2p/client/fakeportallocator.h index d9af4b3f47..fb188261a2 100644 --- a/webrtc/p2p/client/fakeportallocator.h +++ b/webrtc/p2p/client/fakeportallocator.h @@ -157,6 +157,8 @@ class FakePortAllocator : public cricket::PortAllocator { turn_servers_ = turn_servers; } + void SetNetworkIgnoreMask(int network_ignore_mask) override {} + const ServerAddresses& stun_servers() const { return stun_servers_; } const std::vector<RelayServerConfig>& turn_servers() const { diff --git a/webrtc/p2p/client/portallocator_unittest.cc b/webrtc/p2p/client/portallocator_unittest.cc index baa4807ab2..5fce3b5762 100644 --- a/webrtc/p2p/client/portallocator_unittest.cc +++ b/webrtc/p2p/client/portallocator_unittest.cc @@ -32,6 +32,7 @@ #include "webrtc/base/virtualsocketserver.h" using cricket::ServerAddresses; +using rtc::IPAddress; using rtc::SocketAddress; using rtc::Thread; @@ -114,6 +115,11 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { void AddInterface(const SocketAddress& addr, const std::string& if_name) { network_manager_.AddInterface(addr, if_name); } + void AddInterface(const SocketAddress& addr, + const std::string& if_name, + rtc::AdapterType type) { + network_manager_.AddInterface(addr, if_name, type); + } // The default route is the public address that STUN server will observe when // the endpoint is sitting on the public internet and the local port is bound // to the "any" address. This may be different from the default local address @@ -400,6 +406,50 @@ TEST_F(PortAllocatorTest, TestBasic) { EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); } +// Tests that our network filtering works properly. +TEST_F(PortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) { + AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0", + rtc::ADAPTER_TYPE_ETHERNET); + AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0", + rtc::ADAPTER_TYPE_WIFI); + AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0", + rtc::ADAPTER_TYPE_CELLULAR); + AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0", + rtc::ADAPTER_TYPE_VPN); + AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo", + rtc::ADAPTER_TYPE_LOOPBACK); + EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); + session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | + cricket::PORTALLOCATOR_DISABLE_RELAY | + cricket::PORTALLOCATOR_DISABLE_TCP); + session_->StartGettingPorts(); + EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); + EXPECT_EQ(4U, candidates_.size()); + for (cricket::Candidate candidate : candidates_) { + EXPECT_LT(candidate.address().ip(), 0x12345604U); + } +} + +TEST_F(PortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) { + AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0", + rtc::ADAPTER_TYPE_ETHERNET); + AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0", + rtc::ADAPTER_TYPE_WIFI); + AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0", + rtc::ADAPTER_TYPE_CELLULAR); + allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET | + rtc::ADAPTER_TYPE_LOOPBACK | + rtc::ADAPTER_TYPE_WIFI); + EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); + session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | + cricket::PORTALLOCATOR_DISABLE_RELAY | + cricket::PORTALLOCATOR_DISABLE_TCP); + session_->StartGettingPorts(); + EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); + EXPECT_EQ(1U, candidates_.size()); + EXPECT_EQ(0x12345602U, candidates_[0].address().ip()); +} + // Tests that we allocator session not trying to allocate ports for every 250ms. TEST_F(PortAllocatorTest, TestNoNetworkInterface) { EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); @@ -415,7 +465,8 @@ TEST_F(PortAllocatorTest, TestNoNetworkInterface) { // Test that we could use loopback interface as host candidate. TEST_F(PortAllocatorTest, TestLoopbackNetworkInterface) { - AddInterface(kLoopbackAddr); + AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK); + allocator_->SetNetworkIgnoreMask(0); EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | cricket::PORTALLOCATOR_DISABLE_RELAY | |