diff options
Diffstat (limited to 'webrtc/p2p/client')
-rw-r--r-- | webrtc/p2p/client/basicportallocator.cc | 67 | ||||
-rw-r--r-- | webrtc/p2p/client/basicportallocator.h | 68 | ||||
-rw-r--r-- | webrtc/p2p/client/fakeportallocator.h | 88 | ||||
-rw-r--r-- | webrtc/p2p/client/httpportallocator.cc | 1 | ||||
-rw-r--r-- | webrtc/p2p/client/portallocator_unittest.cc | 233 |
5 files changed, 294 insertions, 163 deletions
diff --git a/webrtc/p2p/client/basicportallocator.cc b/webrtc/p2p/client/basicportallocator.cc index 21c8921f40..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(); } @@ -104,15 +106,19 @@ BasicPortAllocator::BasicPortAllocator( stun_servers_(stun_servers) { RelayServerConfig config(RELAY_GTURN); - if (!relay_address_udp.IsNil()) + if (!relay_address_udp.IsNil()) { config.ports.push_back(ProtocolAddress(relay_address_udp, PROTO_UDP)); - if (!relay_address_tcp.IsNil()) + } + if (!relay_address_tcp.IsNil()) { config.ports.push_back(ProtocolAddress(relay_address_tcp, PROTO_TCP)); - if (!relay_address_ssl.IsNil()) + } + if (!relay_address_ssl.IsNil()) { config.ports.push_back(ProtocolAddress(relay_address_ssl, PROTO_SSLTCP)); + } - if (!config.ports.empty()) - AddRelay(config); + if (!config.ports.empty()) { + AddTurnServer(config); + } Construct(); } @@ -241,8 +247,8 @@ void BasicPortAllocatorSession::GetPortConfigurations() { username(), password()); - for (size_t i = 0; i < allocator_->relays().size(); ++i) { - config->AddRelay(allocator_->relays()[i]); + for (const RelayServerConfig& turn_server : allocator_->turn_servers()) { + config->AddRelay(turn_server); } ConfigReady(config); } @@ -253,8 +259,9 @@ void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) { // Adds a configuration to the list. void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) { - if (config) + if (config) { configs_.push_back(config); + } AllocatePorts(); } @@ -322,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, @@ -436,7 +449,8 @@ void BasicPortAllocatorSession::AddAllocatedPort(Port* port, // When adapter enumeration is disabled, disable CF_HOST at port level so // local address is not leaked by stunport in the candidate's related address. - if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) { + if ((flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) && + (flags() & PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE)) { candidate_filter &= ~CF_HOST; } port->set_candidate_filter(candidate_filter); @@ -600,25 +614,6 @@ bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) { return true; } - // If PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE is specified and it's - // loopback address, we should allow it as it's for demo page connectivity - // when no TURN/STUN specified. - if (c.address().IsLoopbackIP() && - (flags() & PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE) != 0) { - return true; - } - - // This is just to prevent the case when binding to any address (all 0s), if - // somehow the host candidate address is not all 0s. Either because local - // installed proxy changes the address or a packet has been sent for any - // reason before getsockname is called. - if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) { - LOG(LS_WARNING) << "Received non-0 host address: " - << c.address().ToString() - << " when adapter enumeration is disabled"; - return false; - } - return ((filter & CF_HOST) != 0); } return false; @@ -882,19 +877,19 @@ void AllocationSequence::CreateUDPPorts() { // TODO(mallinath) - Remove UDPPort creating socket after shared socket // is enabled completely. UDPPort* port = NULL; - bool emit_localhost_for_anyaddress = - IsFlagSet(PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE); + bool emit_local_candidate_for_anyaddress = + !IsFlagSet(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) { port = UDPPort::Create( session_->network_thread(), session_->socket_factory(), network_, udp_socket_.get(), session_->username(), session_->password(), - session_->allocator()->origin(), emit_localhost_for_anyaddress); + session_->allocator()->origin(), emit_local_candidate_for_anyaddress); } else { port = UDPPort::Create( session_->network_thread(), session_->socket_factory(), network_, ip_, session_->allocator()->min_port(), session_->allocator()->max_port(), session_->username(), session_->password(), - session_->allocator()->origin(), emit_localhost_for_anyaddress); + session_->allocator()->origin(), emit_local_candidate_for_anyaddress); } if (port) { diff --git a/webrtc/p2p/client/basicportallocator.h b/webrtc/p2p/client/basicportallocator.h index c8bcad21a9..ca1a23aaf2 100644 --- a/webrtc/p2p/client/basicportallocator.h +++ b/webrtc/p2p/client/basicportallocator.h @@ -14,7 +14,6 @@ #include <string> #include <vector> -#include "webrtc/p2p/base/port.h" #include "webrtc/p2p/base/portallocator.h" #include "webrtc/base/messagequeue.h" #include "webrtc/base/network.h" @@ -23,28 +22,6 @@ namespace cricket { -struct RelayCredentials { - RelayCredentials() {} - RelayCredentials(const std::string& username, - const std::string& password) - : username(username), - password(password) { - } - - std::string username; - std::string password; -}; - -typedef std::vector<ProtocolAddress> PortList; -struct RelayServerConfig { - RelayServerConfig(RelayType type) : type(type), priority(0) {} - - RelayType type; - PortList ports; - RelayCredentials credentials; - int priority; -}; - class BasicPortAllocator : public PortAllocator { public: BasicPortAllocator(rtc::NetworkManager* network_manager, @@ -60,6 +37,23 @@ class BasicPortAllocator : public PortAllocator { const rtc::SocketAddress& relay_server_ssl); virtual ~BasicPortAllocator(); + void SetIceServers( + const ServerAddresses& stun_servers, + const std::vector<RelayServerConfig>& turn_servers) override { + stun_servers_ = stun_servers; + 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 @@ -70,27 +64,28 @@ class BasicPortAllocator : public PortAllocator { return stun_servers_; } - const std::vector<RelayServerConfig>& relays() const { - return relays_; + const std::vector<RelayServerConfig>& turn_servers() const { + return turn_servers_; } - virtual void AddRelay(const RelayServerConfig& relay) { - relays_.push_back(relay); + virtual void AddTurnServer(const RelayServerConfig& turn_server) { + turn_servers_.push_back(turn_server); } - virtual PortAllocatorSession* CreateSessionInternal( + PortAllocatorSession* CreateSessionInternal( const std::string& content_name, int component, const std::string& ice_ufrag, - const std::string& ice_pwd); + const std::string& ice_pwd) override; private: void Construct(); rtc::NetworkManager* network_manager_; rtc::PacketSocketFactory* socket_factory_; - const ServerAddresses stun_servers_; - std::vector<RelayServerConfig> relays_; + ServerAddresses stun_servers_; + std::vector<RelayServerConfig> turn_servers_; bool allow_tcp_listen_; + int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask; }; struct PortConfiguration; @@ -110,10 +105,10 @@ class BasicPortAllocatorSession : public PortAllocatorSession, rtc::Thread* network_thread() { return network_thread_; } rtc::PacketSocketFactory* socket_factory() { return socket_factory_; } - virtual void StartGettingPorts(); - virtual void StopGettingPorts(); - virtual void ClearGettingPorts(); - virtual bool IsGettingPorts() { return running_; } + void StartGettingPorts() override; + void StopGettingPorts() override; + void ClearGettingPorts() override; + bool IsGettingPorts() override { return running_; } protected: // Starts the process of getting the port configurations. @@ -124,7 +119,7 @@ class BasicPortAllocatorSession : public PortAllocatorSession, virtual void ConfigReady(PortConfiguration* config); // MessageHandler. Can be overriden if message IDs do not conflict. - virtual void OnMessage(rtc::Message *message); + void OnMessage(rtc::Message* message) override; private: class PortData { @@ -204,6 +199,7 @@ class BasicPortAllocatorSession : public PortAllocatorSession, }; // Records configuration information useful in creating ports. +// TODO(deadbeef): Rename "relay" to "turn_server" in this struct. struct PortConfiguration : public rtc::MessageData { // TODO(jiayl): remove |stun_address| when Chrome is updated. rtc::SocketAddress stun_address; diff --git a/webrtc/p2p/client/fakeportallocator.h b/webrtc/p2p/client/fakeportallocator.h index dca86f633e..fb188261a2 100644 --- a/webrtc/p2p/client/fakeportallocator.h +++ b/webrtc/p2p/client/fakeportallocator.h @@ -24,6 +24,62 @@ class Thread; namespace cricket { +class TestUDPPort : public UDPPort { + public: + static TestUDPPort* Create(rtc::Thread* thread, + rtc::PacketSocketFactory* factory, + rtc::Network* network, + const rtc::IPAddress& ip, + uint16_t min_port, + uint16_t max_port, + const std::string& username, + const std::string& password, + const std::string& origin, + bool emit_localhost_for_anyaddress) { + TestUDPPort* port = new TestUDPPort(thread, factory, network, ip, min_port, + max_port, username, password, origin, + emit_localhost_for_anyaddress); + if (!port->Init()) { + delete port; + port = nullptr; + } + return port; + } + void SendBindingResponse(StunMessage* request, + const rtc::SocketAddress& addr) override { + UDPPort::SendBindingResponse(request, addr); + sent_binding_response_ = true; + } + bool sent_binding_response() { return sent_binding_response_; } + void set_sent_binding_response(bool response) { + sent_binding_response_ = response; + } + + protected: + TestUDPPort(rtc::Thread* thread, + rtc::PacketSocketFactory* factory, + rtc::Network* network, + const rtc::IPAddress& ip, + uint16_t min_port, + uint16_t max_port, + const std::string& username, + const std::string& password, + const std::string& origin, + bool emit_localhost_for_anyaddress) + : UDPPort(thread, + factory, + network, + ip, + min_port, + max_port, + username, + password, + origin, + emit_localhost_for_anyaddress) {} + + bool sent_binding_response_ = false; +}; + class FakePortAllocatorSession : public PortAllocatorSession { public: FakePortAllocatorSession(rtc::Thread* worker_thread, @@ -45,16 +101,9 @@ class FakePortAllocatorSession : public PortAllocatorSession { virtual void StartGettingPorts() { if (!port_) { - port_.reset(cricket::UDPPort::Create(worker_thread_, - factory_, - &network_, - network_.GetBestIP(), - 0, - 0, - username(), - password(), - std::string(), - false)); + port_.reset(TestUDPPort::Create(worker_thread_, factory_, &network_, + network_.GetBestIP(), 0, 0, username(), + password(), std::string(), false)); AddPort(port_.get()); } ++port_config_count_; @@ -101,11 +150,26 @@ class FakePortAllocator : public cricket::PortAllocator { } } + void SetIceServers( + const ServerAddresses& stun_servers, + const std::vector<RelayServerConfig>& turn_servers) override { + stun_servers_ = stun_servers; + 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 { + return turn_servers_; + } + virtual cricket::PortAllocatorSession* CreateSessionInternal( const std::string& content_name, int component, const std::string& ice_ufrag, - const std::string& ice_pwd) { + const std::string& ice_pwd) override { return new FakePortAllocatorSession( worker_thread_, factory_, content_name, component, ice_ufrag, ice_pwd); } @@ -114,6 +178,8 @@ class FakePortAllocator : public cricket::PortAllocator { rtc::Thread* worker_thread_; rtc::PacketSocketFactory* factory_; rtc::scoped_ptr<rtc::BasicPacketSocketFactory> owned_factory_; + ServerAddresses stun_servers_; + std::vector<RelayServerConfig> turn_servers_; }; } // namespace cricket diff --git a/webrtc/p2p/client/httpportallocator.cc b/webrtc/p2p/client/httpportallocator.cc index a2d5038f90..1342cf70e9 100644 --- a/webrtc/p2p/client/httpportallocator.cc +++ b/webrtc/p2p/client/httpportallocator.cc @@ -13,7 +13,6 @@ #include <algorithm> #include <map> -#include "webrtc/base/basicdefs.h" #include "webrtc/base/common.h" #include "webrtc/base/helpers.h" #include "webrtc/base/httpcommon.h" diff --git a/webrtc/p2p/client/portallocator_unittest.cc b/webrtc/p2p/client/portallocator_unittest.cc index 9617688302..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,17 @@ 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 + // which the endpoint observes. This can occur if the route to the public + // endpoint like 8.8.8.8 (specified as the default local address) is + // different from the route to the STUN server (the default route). void AddInterfaceAsDefaultRoute(const SocketAddress& addr) { AddInterface(addr); // When a binding comes from the any address, the |addr| will be used as the @@ -148,19 +160,19 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { void AddTurnServers(const rtc::SocketAddress& udp_turn, const rtc::SocketAddress& tcp_turn) { - cricket::RelayServerConfig relay_server(cricket::RELAY_TURN); + cricket::RelayServerConfig turn_server(cricket::RELAY_TURN); cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword); - relay_server.credentials = credentials; + turn_server.credentials = credentials; if (!udp_turn.IsNil()) { - relay_server.ports.push_back(cricket::ProtocolAddress( - kTurnUdpIntAddr, cricket::PROTO_UDP, false)); + turn_server.ports.push_back( + cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false)); } if (!tcp_turn.IsNil()) { - relay_server.ports.push_back(cricket::ProtocolAddress( - kTurnTcpIntAddr, cricket::PROTO_TCP, false)); + turn_server.ports.push_back( + cricket::ProtocolAddress(kTurnTcpIntAddr, cricket::PROTO_TCP, false)); } - allocator_->AddRelay(relay_server); + allocator_->AddTurnServer(turn_server); } bool CreateSession(int component) { @@ -254,6 +266,8 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { const rtc::IPAddress& stun_candidate_addr, const rtc::IPAddress& relay_candidate_udp_transport_addr, const rtc::IPAddress& relay_candidate_tcp_transport_addr) { + network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(), + rtc::IPAddress()); if (!session_) { EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); } @@ -268,16 +282,20 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { if (!host_candidate_addr.IsNil()) { EXPECT_PRED5(CheckCandidate, candidates_[total_candidates], cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", - rtc::SocketAddress(host_candidate_addr, 0)); + rtc::SocketAddress(kPrivateAddr.ipaddr(), 0)); ++total_candidates; } if (!stun_candidate_addr.IsNil()) { EXPECT_PRED5(CheckCandidate, candidates_[total_candidates], cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", rtc::SocketAddress(stun_candidate_addr, 0)); - EXPECT_EQ(rtc::EmptySocketAddressWithFamily( - candidates_[total_candidates].address().family()), - candidates_[total_candidates].related_address()); + rtc::IPAddress related_address = host_candidate_addr; + if (host_candidate_addr.IsNil()) { + related_address = + rtc::GetAnyIP(candidates_[total_candidates].address().family()); + } + EXPECT_EQ(related_address, + candidates_[total_candidates].related_address().ipaddr()); ++total_candidates; } if (!relay_candidate_udp_transport_addr.IsNil()) { @@ -320,8 +338,8 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { } bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) { - for (size_t i = 0; i < allocator_->relays().size(); ++i) { - cricket::RelayServerConfig server_config = allocator_->relays()[i]; + for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) { + cricket::RelayServerConfig server_config = allocator_->turn_servers()[i]; cricket::PortList::const_iterator relay_port; for (relay_port = server_config.ports.begin(); relay_port != server_config.ports.end(); ++relay_port) { @@ -374,11 +392,11 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { TEST_F(PortAllocatorTest, TestBasic) { EXPECT_EQ(&network_manager_, allocator().network_manager()); EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin()); - ASSERT_EQ(1u, allocator().relays().size()); - EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type); + ASSERT_EQ(1u, allocator().turn_servers().size()); + EXPECT_EQ(cricket::RELAY_GTURN, allocator().turn_servers()[0].type); // Empty relay credentials are used for GTURN. - EXPECT_TRUE(allocator().relays()[0].credentials.username.empty()); - EXPECT_TRUE(allocator().relays()[0].credentials.password.empty()); + EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty()); + EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty()); EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress( kRelayUdpIntAddr, cricket::PROTO_UDP))); EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress( @@ -388,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)); @@ -403,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 | @@ -589,7 +652,6 @@ TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) { // candidate_filter() is set to CF_RELAY and no relay is specified. TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) { - AddInterfaceAsDefaultRoute(kClientAddr); ResetWithStunServerNoNat(kStunAddr); allocator().set_candidate_filter(cricket::CF_RELAY); // Expect to see no ports and no candidates. @@ -597,86 +659,96 @@ TEST_F(PortAllocatorTest, rtc::IPAddress(), rtc::IPAddress()); } -// Test that we should only get STUN and TURN candidates when adapter -// enumeration is disabled. -TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNat) { - AddInterface(kClientAddr); - // GTURN is not configured here. - ResetWithStunServerAndNat(kStunAddr); - AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); - // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and - // TURN/UDP candidates. - CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kNatUdpAddr.ipaddr(), - kTurnUdpExtAddr.ipaddr(), rtc::IPAddress()); -} - -// Test that even with multiple interfaces, the result should still be one STUN -// and one TURN candidate since we bind to any address (i.e. all 0s). +// Test that even with multiple interfaces, the result should still be a single +// default private, one STUN and one TURN candidate since we bind to any address +// (i.e. all 0s). TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatMultipleInterfaces) { AddInterface(kPrivateAddr); AddInterface(kPrivateAddr2); ResetWithStunServerAndNat(kStunAddr); AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); - // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and + + // Enable IPv6 here. Since the network_manager doesn't have IPv6 default + // address set and we have no IPv6 STUN server, there should be no IPv6 + // candidates. + EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); + session_->set_flags(cricket::PORTALLOCATOR_ENABLE_IPV6); + + // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports + // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and // TURN/UDP candidates. - CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kNatUdpAddr.ipaddr(), - kTurnUdpExtAddr.ipaddr(), rtc::IPAddress()); + CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(), + kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(), + rtc::IPAddress()); } -// Test that we should get STUN, TURN/UDP and TURN/TCP candidates when a -// TURN/TCP server is specified. +// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP +// candidates when both TURN/UDP and TURN/TCP servers are specified. TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) { turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); - AddInterface(kClientAddr); - // GTURN is not configured here. + AddInterface(kPrivateAddr); ResetWithStunServerAndNat(kStunAddr); AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); - // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. STUN, - // TURN/UDP, and TURN/TCP candidates. - CheckDisableAdapterEnumeration(4U, rtc::IPAddress(), kNatUdpAddr.ipaddr(), - kTurnUdpExtAddr.ipaddr(), + // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default + // private, STUN, TURN/UDP, and TURN/TCP candidates. + CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(), + kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(), kTurnUdpExtAddr.ipaddr()); } -// Test that we should only get STUN and TURN candidates when adapter -// enumeration is disabled. Since the endpoint is not behind NAT, the srflx -// address should be the public client interface. -TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNat) { - AddInterfaceAsDefaultRoute(kClientAddr); - ResetWithStunServerNoNat(kStunAddr); - AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); - // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, but only both STUN and - // TURN candidates. The STUN candidate should have kClientAddr as srflx - // address, and TURN candidate with kClientAddr as the related address. - CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kClientAddr.ipaddr(), - kTurnUdpExtAddr.ipaddr(), rtc::IPAddress()); -} - // Test that when adapter enumeration is disabled, for endpoints without -// STUN/TURN specified, no candidate is generated. +// STUN/TURN specified, a default private candidate is still generated. TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatOrServers) { - AddInterfaceAsDefaultRoute(kClientAddr); ResetWithNoServersOrNat(); - // Expect to see 2 ports: STUN and TCP ports, but no candidate. - CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(), + // Expect to see 2 ports: STUN and TCP ports, one default private candidate. + CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(), rtc::IPAddress(), rtc::IPAddress()); } // Test that when adapter enumeration is disabled, with -// PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind -// a NAT, there are a localhost candidate in addition to a STUN candidate. +// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind +// a NAT, there is no local candidate. TEST_F(PortAllocatorTest, - TestDisableAdapterEnumerationWithoutNatLocalhostCandidateRequested) { - AddInterfaceAsDefaultRoute(kClientAddr); + TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) { ResetWithStunServerNoNat(kStunAddr); EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); - session_->set_flags(cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE); + session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN // candidate. - CheckDisableAdapterEnumeration(2U, rtc::GetLoopbackIP(AF_INET), - kClientAddr.ipaddr(), rtc::IPAddress(), - rtc::IPAddress()); + CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(), + rtc::IPAddress(), rtc::IPAddress()); +} + +// Test that when adapter enumeration is disabled, with +// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind +// a NAT, there is no local candidate. However, this specified default route +// (kClientAddr) which was discovered when sending STUN requests, will become +// the srflx addresses. +TEST_F( + PortAllocatorTest, + TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) { + ResetWithStunServerNoNat(kStunAddr); + AddInterfaceAsDefaultRoute(kClientAddr); + EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); + session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); + // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN + // candidate. + CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(), + rtc::IPAddress(), rtc::IPAddress()); +} + +// Test that when adapter enumeration is disabled, with +// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a +// NAT, there is only one STUN candidate. +TEST_F(PortAllocatorTest, + TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) { + ResetWithStunServerAndNat(kStunAddr); + EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); + session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); + // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate. + CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(), + rtc::IPAddress(), rtc::IPAddress()); } // Test that we disable relay over UDP, and only TCP is used when connecting to @@ -1026,13 +1098,12 @@ TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) { cricket::PROTO_UDP); AddInterface(kClientAddr); allocator_.reset(new cricket::BasicPortAllocator(&network_manager_)); - cricket::RelayServerConfig relay_server(cricket::RELAY_TURN); + cricket::RelayServerConfig turn_server(cricket::RELAY_TURN); cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword); - relay_server.credentials = credentials; - relay_server.ports.push_back(cricket::ProtocolAddress( - rtc::SocketAddress("localhost", 3478), - cricket::PROTO_UDP, false)); - allocator_->AddRelay(relay_server); + turn_server.credentials = credentials; + turn_server.ports.push_back(cricket::ProtocolAddress( + rtc::SocketAddress("localhost", 3478), cricket::PROTO_UDP, false)); + allocator_->AddTurnServer(turn_server); allocator_->set_step_delay(cricket::kMinimumStepDelay); allocator_->set_flags(allocator().flags() | @@ -1244,7 +1315,8 @@ TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) { // adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified // automatically. TEST_F(PortAllocatorTest, TestNetworkPermissionBlocked) { - AddInterface(kClientAddr); + network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(), + rtc::IPAddress()); network_manager_.set_enumeration_permission( rtc::NetworkManager::ENUMERATION_BLOCKED); allocator().set_flags(allocator().flags() | @@ -1258,7 +1330,10 @@ TEST_F(PortAllocatorTest, TestNetworkPermissionBlocked) { cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION); session_->StartGettingPorts(); EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout); - EXPECT_EQ(0U, candidates_.size()); + EXPECT_EQ(1U, candidates_.size()); + EXPECT_PRED5(CheckCandidate, candidates_[0], + cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", + kPrivateAddr); EXPECT_TRUE((session_->flags() & cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) != 0); } |