henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/p2p/client/basicportallocator.h" |
| 12 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 13 | #include <algorithm> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 18 | #include "webrtc/p2p/base/common.h" |
| 19 | #include "webrtc/p2p/base/port.h" |
| 20 | #include "webrtc/p2p/base/relayport.h" |
| 21 | #include "webrtc/p2p/base/stunport.h" |
| 22 | #include "webrtc/p2p/base/tcpport.h" |
| 23 | #include "webrtc/p2p/base/turnport.h" |
| 24 | #include "webrtc/p2p/base/udpport.h" |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 25 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 26 | #include "webrtc/base/common.h" |
| 27 | #include "webrtc/base/helpers.h" |
| 28 | #include "webrtc/base/logging.h" |
| 29 | |
| 30 | using rtc::CreateRandomId; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
| 33 | |
| 34 | enum { |
| 35 | MSG_CONFIG_START, |
| 36 | MSG_CONFIG_READY, |
| 37 | MSG_ALLOCATE, |
| 38 | MSG_ALLOCATION_PHASE, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 39 | MSG_SEQUENCEOBJECTS_CREATED, |
| 40 | MSG_CONFIG_STOP, |
| 41 | }; |
| 42 | |
| 43 | const int PHASE_UDP = 0; |
| 44 | const int PHASE_RELAY = 1; |
| 45 | const int PHASE_TCP = 2; |
| 46 | const int PHASE_SSLTCP = 3; |
| 47 | |
| 48 | const int kNumPhases = 4; |
| 49 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 50 | // Gets protocol priority: UDP > TCP > SSLTCP. |
| 51 | int GetProtocolPriority(cricket::ProtocolType protocol) { |
| 52 | switch (protocol) { |
| 53 | case cricket::PROTO_UDP: |
| 54 | return 2; |
| 55 | case cricket::PROTO_TCP: |
| 56 | return 1; |
| 57 | case cricket::PROTO_SSLTCP: |
| 58 | return 0; |
| 59 | default: |
| 60 | RTC_DCHECK(false); |
| 61 | return 0; |
| 62 | } |
| 63 | } |
| 64 | // Gets address family priority: IPv6 > IPv4 > Unspecified. |
| 65 | int GetAddressFamilyPriority(int ip_family) { |
| 66 | switch (ip_family) { |
| 67 | case AF_INET6: |
| 68 | return 2; |
| 69 | case AF_INET: |
| 70 | return 1; |
| 71 | default: |
| 72 | RTC_DCHECK(false); |
| 73 | return 0; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Returns positive if a is better, negative if b is better, and 0 otherwise. |
| 78 | int ComparePort(const cricket::Port* a, const cricket::Port* b) { |
| 79 | int a_protocol = GetProtocolPriority(a->GetProtocol()); |
| 80 | int b_protocol = GetProtocolPriority(b->GetProtocol()); |
| 81 | int cmp_protocol = a_protocol - b_protocol; |
| 82 | if (cmp_protocol != 0) { |
| 83 | return cmp_protocol; |
| 84 | } |
| 85 | |
| 86 | int a_family = GetAddressFamilyPriority(a->Network()->GetBestIP().family()); |
| 87 | int b_family = GetAddressFamilyPriority(b->Network()->GetBestIP().family()); |
| 88 | return a_family - b_family; |
| 89 | } |
| 90 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 91 | } // namespace |
| 92 | |
| 93 | namespace cricket { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 94 | const uint32_t DISABLE_ALL_PHASES = |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 95 | PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_TCP | |
| 96 | PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | |
| 98 | // BasicPortAllocator |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 99 | BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager, |
| 100 | rtc::PacketSocketFactory* socket_factory) |
| 101 | : network_manager_(network_manager), socket_factory_(socket_factory) { |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 102 | ASSERT(network_manager_ != nullptr); |
| 103 | ASSERT(socket_factory_ != nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 104 | Construct(); |
| 105 | } |
| 106 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 107 | BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager) |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 108 | : network_manager_(network_manager), socket_factory_(nullptr) { |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 109 | ASSERT(network_manager_ != nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 110 | Construct(); |
| 111 | } |
| 112 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 113 | BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager, |
| 114 | rtc::PacketSocketFactory* socket_factory, |
| 115 | const ServerAddresses& stun_servers) |
| 116 | : network_manager_(network_manager), socket_factory_(socket_factory) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 117 | ASSERT(socket_factory_ != NULL); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 118 | SetConfiguration(stun_servers, std::vector<RelayServerConfig>(), 0, false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 119 | Construct(); |
| 120 | } |
| 121 | |
| 122 | BasicPortAllocator::BasicPortAllocator( |
| 123 | rtc::NetworkManager* network_manager, |
| 124 | const ServerAddresses& stun_servers, |
| 125 | const rtc::SocketAddress& relay_address_udp, |
| 126 | const rtc::SocketAddress& relay_address_tcp, |
| 127 | const rtc::SocketAddress& relay_address_ssl) |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 128 | : network_manager_(network_manager), socket_factory_(NULL) { |
| 129 | std::vector<RelayServerConfig> turn_servers; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 130 | RelayServerConfig config(RELAY_GTURN); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 131 | if (!relay_address_udp.IsNil()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 132 | config.ports.push_back(ProtocolAddress(relay_address_udp, PROTO_UDP)); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 133 | } |
| 134 | if (!relay_address_tcp.IsNil()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 135 | config.ports.push_back(ProtocolAddress(relay_address_tcp, PROTO_TCP)); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 136 | } |
| 137 | if (!relay_address_ssl.IsNil()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 138 | config.ports.push_back(ProtocolAddress(relay_address_ssl, PROTO_SSLTCP)); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 139 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 140 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 141 | if (!config.ports.empty()) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 142 | turn_servers.push_back(config); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 143 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 144 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 145 | SetConfiguration(stun_servers, turn_servers, 0, false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 146 | Construct(); |
| 147 | } |
| 148 | |
| 149 | void BasicPortAllocator::Construct() { |
| 150 | allow_tcp_listen_ = true; |
| 151 | } |
| 152 | |
| 153 | BasicPortAllocator::~BasicPortAllocator() { |
| 154 | } |
| 155 | |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 156 | PortAllocatorSession* BasicPortAllocator::CreateSessionInternal( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 157 | const std::string& content_name, int component, |
| 158 | const std::string& ice_ufrag, const std::string& ice_pwd) { |
| 159 | return new BasicPortAllocatorSession( |
| 160 | this, content_name, component, ice_ufrag, ice_pwd); |
| 161 | } |
| 162 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 163 | void BasicPortAllocator::AddTurnServer(const RelayServerConfig& turn_server) { |
| 164 | std::vector<RelayServerConfig> new_turn_servers = turn_servers(); |
| 165 | new_turn_servers.push_back(turn_server); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 166 | SetConfiguration(stun_servers(), new_turn_servers, candidate_pool_size(), |
| 167 | prune_turn_ports()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 168 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 169 | |
| 170 | // BasicPortAllocatorSession |
| 171 | BasicPortAllocatorSession::BasicPortAllocatorSession( |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 172 | BasicPortAllocator* allocator, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 173 | const std::string& content_name, |
| 174 | int component, |
| 175 | const std::string& ice_ufrag, |
| 176 | const std::string& ice_pwd) |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 177 | : PortAllocatorSession(content_name, |
| 178 | component, |
| 179 | ice_ufrag, |
| 180 | ice_pwd, |
| 181 | allocator->flags()), |
| 182 | allocator_(allocator), |
| 183 | network_thread_(NULL), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 184 | socket_factory_(allocator->socket_factory()), |
| 185 | allocation_started_(false), |
| 186 | network_manager_started_(false), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 187 | allocation_sequences_created_(false), |
| 188 | prune_turn_ports_(allocator->prune_turn_ports()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 189 | allocator_->network_manager()->SignalNetworksChanged.connect( |
| 190 | this, &BasicPortAllocatorSession::OnNetworksChanged); |
| 191 | allocator_->network_manager()->StartUpdating(); |
| 192 | } |
| 193 | |
| 194 | BasicPortAllocatorSession::~BasicPortAllocatorSession() { |
| 195 | allocator_->network_manager()->StopUpdating(); |
| 196 | if (network_thread_ != NULL) |
| 197 | network_thread_->Clear(this); |
| 198 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 199 | for (uint32_t i = 0; i < sequences_.size(); ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 200 | // AllocationSequence should clear it's map entry for turn ports before |
| 201 | // ports are destroyed. |
| 202 | sequences_[i]->Clear(); |
| 203 | } |
| 204 | |
| 205 | std::vector<PortData>::iterator it; |
| 206 | for (it = ports_.begin(); it != ports_.end(); it++) |
| 207 | delete it->port(); |
| 208 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 209 | for (uint32_t i = 0; i < configs_.size(); ++i) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 210 | delete configs_[i]; |
| 211 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 212 | for (uint32_t i = 0; i < sequences_.size(); ++i) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 213 | delete sequences_[i]; |
| 214 | } |
| 215 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 216 | void BasicPortAllocatorSession::SetCandidateFilter(uint32_t filter) { |
| 217 | if (filter == candidate_filter_) { |
| 218 | return; |
| 219 | } |
| 220 | // We assume the filter will only change from "ALL" to something else. |
| 221 | RTC_DCHECK(candidate_filter_ == CF_ALL); |
| 222 | candidate_filter_ = filter; |
| 223 | for (PortData& port : ports_) { |
| 224 | if (!port.has_pairable_candidate()) { |
| 225 | continue; |
| 226 | } |
| 227 | const auto& candidates = port.port()->Candidates(); |
| 228 | // Setting a filter may cause a ready port to become non-ready |
| 229 | // if it no longer has any pairable candidates. |
| 230 | if (!std::any_of(candidates.begin(), candidates.end(), |
| 231 | [this, &port](const Candidate& candidate) { |
| 232 | return CandidatePairable(candidate, port.port()); |
| 233 | })) { |
| 234 | port.set_has_pairable_candidate(false); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 239 | void BasicPortAllocatorSession::StartGettingPorts() { |
| 240 | network_thread_ = rtc::Thread::Current(); |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 241 | state_ = SessionState::GATHERING; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 242 | if (!socket_factory_) { |
| 243 | owned_socket_factory_.reset( |
| 244 | new rtc::BasicPacketSocketFactory(network_thread_)); |
| 245 | socket_factory_ = owned_socket_factory_.get(); |
| 246 | } |
| 247 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 248 | network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_START); |
Honghai Zhang | d78ecf7 | 2016-07-01 14:40:40 -0700 | [diff] [blame] | 249 | |
| 250 | LOG(LS_INFO) << "Pruning turn ports " |
| 251 | << (prune_turn_ports_ ? "enabled" : "disabled"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void BasicPortAllocatorSession::StopGettingPorts() { |
| 255 | ASSERT(rtc::Thread::Current() == network_thread_); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 256 | network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_STOP); |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 257 | ClearGettingPorts(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 258 | // Note: this must be called after ClearGettingPorts because both may set the |
| 259 | // session state and we should set the state to STOPPED. |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 260 | state_ = SessionState::STOPPED; |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void BasicPortAllocatorSession::ClearGettingPorts() { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 264 | ASSERT(rtc::Thread::Current() == network_thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 265 | network_thread_->Clear(this, MSG_ALLOCATE); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 266 | for (uint32_t i = 0; i < sequences_.size(); ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 267 | sequences_[i]->Stop(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 268 | } |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 269 | state_ = SessionState::CLEARED; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | std::vector<rtc::Network*> BasicPortAllocatorSession::GetFailedNetworks() { |
| 273 | std::vector<rtc::Network*> networks = GetNetworks(); |
| 274 | |
| 275 | // A network interface may have both IPv4 and IPv6 networks. Only if |
| 276 | // neither of the networks has any connections, the network interface |
| 277 | // is considered failed and need to be regathered on. |
| 278 | std::set<std::string> networks_with_connection; |
| 279 | for (const PortData& data : ports_) { |
| 280 | Port* port = data.port(); |
| 281 | if (!port->connections().empty()) { |
| 282 | networks_with_connection.insert(port->Network()->name()); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | networks.erase( |
| 287 | std::remove_if(networks.begin(), networks.end(), |
| 288 | [networks_with_connection](rtc::Network* network) { |
| 289 | // If a network does not have any connection, it is |
| 290 | // considered failed. |
| 291 | return networks_with_connection.find(network->name()) != |
| 292 | networks_with_connection.end(); |
| 293 | }), |
| 294 | networks.end()); |
| 295 | return networks; |
| 296 | } |
| 297 | |
| 298 | void BasicPortAllocatorSession::RegatherOnFailedNetworks() { |
| 299 | // Find the list of networks that have no connection. |
| 300 | std::vector<rtc::Network*> failed_networks = GetFailedNetworks(); |
| 301 | if (failed_networks.empty()) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | // Mark a sequence as "network failed" if its network is in the list of failed |
| 306 | // networks, so that it won't be considered as equivalent when the session |
| 307 | // regathers ports and candidates. |
| 308 | for (AllocationSequence* sequence : sequences_) { |
| 309 | if (!sequence->network_failed() && |
| 310 | std::find(failed_networks.begin(), failed_networks.end(), |
| 311 | sequence->network()) != failed_networks.end()) { |
| 312 | sequence->set_network_failed(); |
| 313 | } |
| 314 | } |
| 315 | // Remove ports from being used locally and send signaling to remove |
| 316 | // the candidates on the remote side. |
| 317 | RemovePortsAndCandidates(failed_networks); |
| 318 | |
| 319 | if (allocation_started_ && network_manager_started_) { |
| 320 | DoAllocate(); |
| 321 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 324 | std::vector<PortInterface*> BasicPortAllocatorSession::ReadyPorts() const { |
| 325 | std::vector<PortInterface*> ret; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 326 | for (const PortData& data : ports_) { |
| 327 | if (data.ready()) { |
| 328 | ret.push_back(data.port()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | std::vector<Candidate> BasicPortAllocatorSession::ReadyCandidates() const { |
| 335 | std::vector<Candidate> candidates; |
| 336 | for (const PortData& data : ports_) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 337 | if (!data.ready()) { |
| 338 | continue; |
| 339 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 340 | GetCandidatesFromPort(data, &candidates); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 341 | } |
| 342 | return candidates; |
| 343 | } |
| 344 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 345 | void BasicPortAllocatorSession::GetCandidatesFromPort( |
| 346 | const PortData& data, |
| 347 | std::vector<Candidate>* candidates) const { |
| 348 | RTC_CHECK(candidates != nullptr); |
| 349 | for (const Candidate& candidate : data.port()->Candidates()) { |
| 350 | if (!CheckCandidateFilter(candidate)) { |
| 351 | continue; |
| 352 | } |
| 353 | ProtocolType pvalue; |
| 354 | if (!StringToProto(candidate.protocol().c_str(), &pvalue) || |
| 355 | !data.sequence()->ProtocolEnabled(pvalue)) { |
| 356 | continue; |
| 357 | } |
| 358 | candidates->push_back(SanitizeRelatedAddress(candidate)); |
| 359 | } |
| 360 | } |
| 361 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 362 | Candidate BasicPortAllocatorSession::SanitizeRelatedAddress( |
| 363 | const Candidate& c) const { |
| 364 | Candidate copy = c; |
| 365 | // If adapter enumeration is disabled or host candidates are disabled, |
| 366 | // clear the raddr of STUN candidates to avoid local address leakage. |
| 367 | bool filter_stun_related_address = |
| 368 | ((flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) && |
| 369 | (flags() & PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE)) || |
| 370 | !(candidate_filter_ & CF_HOST); |
| 371 | // If the candidate filter doesn't allow reflexive addresses, empty TURN raddr |
| 372 | // to avoid reflexive address leakage. |
| 373 | bool filter_turn_related_address = !(candidate_filter_ & CF_REFLEXIVE); |
| 374 | if ((c.type() == STUN_PORT_TYPE && filter_stun_related_address) || |
| 375 | (c.type() == RELAY_PORT_TYPE && filter_turn_related_address)) { |
| 376 | copy.set_related_address( |
| 377 | rtc::EmptySocketAddressWithFamily(copy.address().family())); |
| 378 | } |
| 379 | return copy; |
| 380 | } |
| 381 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 382 | bool BasicPortAllocatorSession::CandidatesAllocationDone() const { |
| 383 | // Done only if all required AllocationSequence objects |
| 384 | // are created. |
| 385 | if (!allocation_sequences_created_) { |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | // Check that all port allocation sequences are complete (not running). |
| 390 | if (std::any_of(sequences_.begin(), sequences_.end(), |
| 391 | [](const AllocationSequence* sequence) { |
| 392 | return sequence->state() == AllocationSequence::kRunning; |
| 393 | })) { |
| 394 | return false; |
| 395 | } |
| 396 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 397 | // If all allocated ports are no longer gathering, session must have got all |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 398 | // expected candidates. Session will trigger candidates allocation complete |
| 399 | // signal. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 400 | return std::none_of(ports_.begin(), ports_.end(), |
| 401 | [](const PortData& port) { return port.inprogress(); }); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 402 | } |
| 403 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 404 | void BasicPortAllocatorSession::OnMessage(rtc::Message *message) { |
| 405 | switch (message->message_id) { |
| 406 | case MSG_CONFIG_START: |
| 407 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 408 | GetPortConfigurations(); |
| 409 | break; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 410 | case MSG_CONFIG_READY: |
| 411 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 412 | OnConfigReady(static_cast<PortConfiguration*>(message->pdata)); |
| 413 | break; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 414 | case MSG_ALLOCATE: |
| 415 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 416 | OnAllocate(); |
| 417 | break; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 418 | case MSG_SEQUENCEOBJECTS_CREATED: |
| 419 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 420 | OnAllocationSequenceObjectsCreated(); |
| 421 | break; |
| 422 | case MSG_CONFIG_STOP: |
| 423 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 424 | OnConfigStop(); |
| 425 | break; |
| 426 | default: |
| 427 | ASSERT(false); |
| 428 | } |
| 429 | } |
| 430 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 431 | void BasicPortAllocatorSession::UpdateIceParametersInternal() { |
| 432 | for (PortData& port : ports_) { |
| 433 | port.port()->set_content_name(content_name()); |
| 434 | port.port()->SetIceParameters(component(), ice_ufrag(), ice_pwd()); |
| 435 | } |
| 436 | } |
| 437 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 438 | void BasicPortAllocatorSession::GetPortConfigurations() { |
| 439 | PortConfiguration* config = new PortConfiguration(allocator_->stun_servers(), |
| 440 | username(), |
| 441 | password()); |
| 442 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 443 | for (const RelayServerConfig& turn_server : allocator_->turn_servers()) { |
| 444 | config->AddRelay(turn_server); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 445 | } |
| 446 | ConfigReady(config); |
| 447 | } |
| 448 | |
| 449 | void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 450 | network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_READY, config); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // Adds a configuration to the list. |
| 454 | void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) { |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 455 | if (config) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 456 | configs_.push_back(config); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 457 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 458 | |
| 459 | AllocatePorts(); |
| 460 | } |
| 461 | |
| 462 | void BasicPortAllocatorSession::OnConfigStop() { |
| 463 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 464 | |
| 465 | // If any of the allocated ports have not completed the candidates allocation, |
| 466 | // mark those as error. Since session doesn't need any new candidates |
| 467 | // at this stage of the allocation, it's safe to discard any new candidates. |
| 468 | bool send_signal = false; |
| 469 | for (std::vector<PortData>::iterator it = ports_.begin(); |
| 470 | it != ports_.end(); ++it) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 471 | if (it->inprogress()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 472 | // Updating port state to error, which didn't finish allocating candidates |
| 473 | // yet. |
| 474 | it->set_error(); |
| 475 | send_signal = true; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // Did we stop any running sequences? |
| 480 | for (std::vector<AllocationSequence*>::iterator it = sequences_.begin(); |
| 481 | it != sequences_.end() && !send_signal; ++it) { |
| 482 | if ((*it)->state() == AllocationSequence::kStopped) { |
| 483 | send_signal = true; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // If we stopped anything that was running, send a done signal now. |
| 488 | if (send_signal) { |
| 489 | MaybeSignalCandidatesAllocationDone(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void BasicPortAllocatorSession::AllocatePorts() { |
| 494 | ASSERT(rtc::Thread::Current() == network_thread_); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 495 | network_thread_->Post(RTC_FROM_HERE, this, MSG_ALLOCATE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void BasicPortAllocatorSession::OnAllocate() { |
| 499 | if (network_manager_started_) |
| 500 | DoAllocate(); |
| 501 | |
| 502 | allocation_started_ = true; |
| 503 | } |
| 504 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 505 | std::vector<rtc::Network*> BasicPortAllocatorSession::GetNetworks() { |
| 506 | std::vector<rtc::Network*> networks; |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 507 | rtc::NetworkManager* network_manager = allocator_->network_manager(); |
| 508 | ASSERT(network_manager != nullptr); |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 509 | // If the network permission state is BLOCKED, we just act as if the flag has |
| 510 | // been passed in. |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 511 | if (network_manager->enumeration_permission() == |
guoweis | ea1012b | 2015-08-21 09:06:28 -0700 | [diff] [blame] | 512 | rtc::NetworkManager::ENUMERATION_BLOCKED) { |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 513 | set_flags(flags() | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION); |
| 514 | } |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 515 | // If the adapter enumeration is disabled, we'll just bind to any address |
| 516 | // instead of specific NIC. This is to ensure the same routing for http |
| 517 | // traffic by OS is also used here to avoid any local or public IP leakage |
| 518 | // during stun process. |
| 519 | if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 520 | network_manager->GetAnyAddressNetworks(&networks); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 521 | } else { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 522 | network_manager->GetNetworks(&networks); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 523 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 524 | networks.erase(std::remove_if(networks.begin(), networks.end(), |
| 525 | [this](rtc::Network* network) { |
| 526 | return allocator_->network_ignore_mask() & |
| 527 | network->type(); |
| 528 | }), |
| 529 | networks.end()); |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 530 | |
| 531 | if (flags() & PORTALLOCATOR_DISABLE_COSTLY_NETWORKS) { |
| 532 | uint16_t lowest_cost = rtc::kNetworkCostMax; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 533 | for (rtc::Network* network : networks) { |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 534 | lowest_cost = std::min<uint16_t>(lowest_cost, network->GetCost()); |
| 535 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 536 | networks.erase(std::remove_if(networks.begin(), networks.end(), |
| 537 | [lowest_cost](rtc::Network* network) { |
| 538 | return network->GetCost() > |
| 539 | lowest_cost + rtc::kNetworkCostLow; |
| 540 | }), |
| 541 | networks.end()); |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 542 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 543 | return networks; |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // For each network, see if we have a sequence that covers it already. If not, |
| 547 | // create a new sequence to create the appropriate ports. |
| 548 | void BasicPortAllocatorSession::DoAllocate() { |
| 549 | bool done_signal_needed = false; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 550 | std::vector<rtc::Network*> networks = GetNetworks(); |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 551 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 552 | if (networks.empty()) { |
| 553 | LOG(LS_WARNING) << "Machine has no networks; no ports will be allocated"; |
| 554 | done_signal_needed = true; |
| 555 | } else { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 556 | PortConfiguration* config = configs_.empty() ? nullptr : configs_.back(); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 557 | for (uint32_t i = 0; i < networks.size(); ++i) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 558 | uint32_t sequence_flags = flags(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 559 | if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { |
| 560 | // If all the ports are disabled we should just fire the allocation |
| 561 | // done event and return. |
| 562 | done_signal_needed = true; |
| 563 | break; |
| 564 | } |
| 565 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 566 | if (!config || config->relays.empty()) { |
| 567 | // No relay ports specified in this config. |
| 568 | sequence_flags |= PORTALLOCATOR_DISABLE_RELAY; |
| 569 | } |
| 570 | |
| 571 | if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6) && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 572 | networks[i]->GetBestIP().family() == AF_INET6) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 573 | // Skip IPv6 networks unless the flag's been set. |
| 574 | continue; |
| 575 | } |
| 576 | |
| 577 | // Disable phases that would only create ports equivalent to |
| 578 | // ones that we have already made. |
| 579 | DisableEquivalentPhases(networks[i], config, &sequence_flags); |
| 580 | |
| 581 | if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { |
| 582 | // New AllocationSequence would have nothing to do, so don't make it. |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | AllocationSequence* sequence = |
| 587 | new AllocationSequence(this, networks[i], config, sequence_flags); |
| 588 | if (!sequence->Init()) { |
| 589 | delete sequence; |
| 590 | continue; |
| 591 | } |
| 592 | done_signal_needed = true; |
| 593 | sequence->SignalPortAllocationComplete.connect( |
| 594 | this, &BasicPortAllocatorSession::OnPortAllocationComplete); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 595 | if (!IsStopped()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 596 | sequence->Start(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 597 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 598 | sequences_.push_back(sequence); |
| 599 | } |
| 600 | } |
| 601 | if (done_signal_needed) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 602 | network_thread_->Post(RTC_FROM_HERE, this, MSG_SEQUENCEOBJECTS_CREATED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | void BasicPortAllocatorSession::OnNetworksChanged() { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 607 | std::vector<rtc::Network*> networks = GetNetworks(); |
| 608 | std::vector<rtc::Network*> failed_networks; |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 609 | for (AllocationSequence* sequence : sequences_) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 610 | // Mark the sequence as "network failed" if its network is not in |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 611 | // |networks|. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 612 | if (!sequence->network_failed() && |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 613 | std::find(networks.begin(), networks.end(), sequence->network()) == |
| 614 | networks.end()) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 615 | sequence->OnNetworkFailed(); |
| 616 | failed_networks.push_back(sequence->network()); |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 619 | RemovePortsAndCandidates(failed_networks); |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 620 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 621 | network_manager_started_ = true; |
| 622 | if (allocation_started_) |
| 623 | DoAllocate(); |
| 624 | } |
| 625 | |
| 626 | void BasicPortAllocatorSession::DisableEquivalentPhases( |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 627 | rtc::Network* network, |
| 628 | PortConfiguration* config, |
| 629 | uint32_t* flags) { |
| 630 | for (uint32_t i = 0; i < sequences_.size() && |
| 631 | (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES; |
| 632 | ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 633 | sequences_[i]->DisableEquivalentPhases(network, config, flags); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | void BasicPortAllocatorSession::AddAllocatedPort(Port* port, |
| 638 | AllocationSequence * seq, |
| 639 | bool prepare_address) { |
| 640 | if (!port) |
| 641 | return; |
| 642 | |
| 643 | LOG(LS_INFO) << "Adding allocated port for " << content_name(); |
| 644 | port->set_content_name(content_name()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 645 | port->set_component(component()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 646 | port->set_generation(generation()); |
| 647 | if (allocator_->proxy().type != rtc::PROXY_NONE) |
| 648 | port->set_proxy(allocator_->user_agent(), allocator_->proxy()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 649 | port->set_send_retransmit_count_attribute( |
| 650 | (flags() & PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE) != 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 651 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 652 | PortData data(port, seq); |
| 653 | ports_.push_back(data); |
| 654 | |
| 655 | port->SignalCandidateReady.connect( |
| 656 | this, &BasicPortAllocatorSession::OnCandidateReady); |
| 657 | port->SignalPortComplete.connect(this, |
| 658 | &BasicPortAllocatorSession::OnPortComplete); |
| 659 | port->SignalDestroyed.connect(this, |
| 660 | &BasicPortAllocatorSession::OnPortDestroyed); |
| 661 | port->SignalPortError.connect( |
| 662 | this, &BasicPortAllocatorSession::OnPortError); |
| 663 | LOG_J(LS_INFO, port) << "Added port to allocator"; |
| 664 | |
| 665 | if (prepare_address) |
| 666 | port->PrepareAddress(); |
| 667 | } |
| 668 | |
| 669 | void BasicPortAllocatorSession::OnAllocationSequenceObjectsCreated() { |
| 670 | allocation_sequences_created_ = true; |
| 671 | // Send candidate allocation complete signal if we have no sequences. |
| 672 | MaybeSignalCandidatesAllocationDone(); |
| 673 | } |
| 674 | |
| 675 | void BasicPortAllocatorSession::OnCandidateReady( |
| 676 | Port* port, const Candidate& c) { |
| 677 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 678 | PortData* data = FindPort(port); |
| 679 | ASSERT(data != NULL); |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame^] | 680 | LOG_J(LS_INFO, port) << "Gathered candidate: " << c.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 681 | // Discarding any candidate signal if port allocation status is |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 682 | // already done with gathering. |
| 683 | if (!data->inprogress()) { |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame^] | 684 | LOG(LS_WARNING) |
| 685 | << "Discarding candidate because port is already done gathering."; |
danilchap | f4e8cf0 | 2016-06-30 01:55:03 -0700 | [diff] [blame] | 686 | return; |
Honghai Zhang | 17aac05 | 2016-06-29 21:41:53 -0700 | [diff] [blame] | 687 | } |
Honghai Zhang | 17aac05 | 2016-06-29 21:41:53 -0700 | [diff] [blame] | 688 | |
danilchap | f4e8cf0 | 2016-06-30 01:55:03 -0700 | [diff] [blame] | 689 | // Mark that the port has a pairable candidate, either because we have a |
| 690 | // usable candidate from the port, or simply because the port is bound to the |
| 691 | // any address and therefore has no host candidate. This will trigger the port |
| 692 | // to start creating candidate pairs (connections) and issue connectivity |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 693 | // checks. If port has already been marked as having a pairable candidate, |
| 694 | // do nothing here. |
| 695 | // Note: We should check whether any candidates may become ready after this |
| 696 | // because there we will check whether the candidate is generated by the ready |
| 697 | // ports, which may include this port. |
| 698 | bool pruned_port = false; |
| 699 | if (CandidatePairable(c, port) && !data->has_pairable_candidate()) { |
danilchap | f4e8cf0 | 2016-06-30 01:55:03 -0700 | [diff] [blame] | 700 | data->set_has_pairable_candidate(true); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 701 | |
| 702 | if (prune_turn_ports_ && port->Type() == RELAY_PORT_TYPE) { |
| 703 | pruned_port = PruneTurnPorts(port); |
| 704 | } |
| 705 | // If the current port is not pruned yet, SignalPortReady. |
| 706 | if (!data->pruned()) { |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame^] | 707 | LOG_J(LS_INFO, port) << "Port ready."; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 708 | SignalPortReady(this, port); |
| 709 | } |
Honghai Zhang | 17aac05 | 2016-06-29 21:41:53 -0700 | [diff] [blame] | 710 | } |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 711 | |
| 712 | ProtocolType pvalue; |
| 713 | bool candidate_protocol_enabled = |
| 714 | StringToProto(c.protocol().c_str(), &pvalue) && |
| 715 | data->sequence()->ProtocolEnabled(pvalue); |
| 716 | |
| 717 | if (data->ready() && CheckCandidateFilter(c) && candidate_protocol_enabled) { |
| 718 | std::vector<Candidate> candidates; |
| 719 | candidates.push_back(SanitizeRelatedAddress(c)); |
| 720 | SignalCandidatesReady(this, candidates); |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame^] | 721 | } else if (!candidate_protocol_enabled) { |
| 722 | LOG(LS_INFO) |
| 723 | << "Not yet signaling candidate because protocol is not yet enabled."; |
| 724 | } else { |
| 725 | LOG(LS_INFO) << "Discarding candidate because it doesn't match filter."; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | // If we have pruned any port, maybe need to signal port allocation done. |
| 729 | if (pruned_port) { |
| 730 | MaybeSignalCandidatesAllocationDone(); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | Port* BasicPortAllocatorSession::GetBestTurnPortForNetwork( |
| 735 | const std::string& network_name) const { |
| 736 | Port* best_turn_port = nullptr; |
| 737 | for (const PortData& data : ports_) { |
| 738 | if (data.port()->Network()->name() == network_name && |
| 739 | data.port()->Type() == RELAY_PORT_TYPE && data.ready() && |
| 740 | (!best_turn_port || ComparePort(data.port(), best_turn_port) > 0)) { |
| 741 | best_turn_port = data.port(); |
| 742 | } |
| 743 | } |
| 744 | return best_turn_port; |
| 745 | } |
| 746 | |
| 747 | bool BasicPortAllocatorSession::PruneTurnPorts(Port* newly_pairable_turn_port) { |
| 748 | bool pruned_port = false; |
| 749 | // Note: We determine the same network based only on their network names. So |
| 750 | // if an IPv4 address and an IPv6 address have the same network name, they |
| 751 | // are considered the same network here. |
| 752 | const std::string& network_name = newly_pairable_turn_port->Network()->name(); |
| 753 | Port* best_turn_port = GetBestTurnPortForNetwork(network_name); |
| 754 | // |port| is already in the list of ports, so the best port cannot be nullptr. |
| 755 | RTC_CHECK(best_turn_port != nullptr); |
| 756 | |
| 757 | for (PortData& data : ports_) { |
| 758 | if (data.port()->Network()->name() == network_name && |
| 759 | data.port()->Type() == RELAY_PORT_TYPE && !data.pruned() && |
| 760 | ComparePort(data.port(), best_turn_port) < 0) { |
| 761 | data.set_pruned(); |
| 762 | pruned_port = true; |
| 763 | if (data.port() != newly_pairable_turn_port) { |
| 764 | SignalPortPruned(this, data.port()); |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | return pruned_port; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void BasicPortAllocatorSession::OnPortComplete(Port* port) { |
| 772 | ASSERT(rtc::Thread::Current() == network_thread_); |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame^] | 773 | LOG_J(LS_INFO, port) << "Port completed gathering candidates."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 774 | PortData* data = FindPort(port); |
| 775 | ASSERT(data != NULL); |
| 776 | |
| 777 | // Ignore any late signals. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 778 | if (!data->inprogress()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 779 | return; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 780 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 781 | |
| 782 | // Moving to COMPLETE state. |
| 783 | data->set_complete(); |
| 784 | // Send candidate allocation complete signal if this was the last port. |
| 785 | MaybeSignalCandidatesAllocationDone(); |
| 786 | } |
| 787 | |
| 788 | void BasicPortAllocatorSession::OnPortError(Port* port) { |
| 789 | ASSERT(rtc::Thread::Current() == network_thread_); |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame^] | 790 | LOG_J(LS_INFO, port) << "Port encountered error while gathering candidates."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 791 | PortData* data = FindPort(port); |
| 792 | ASSERT(data != NULL); |
| 793 | // We might have already given up on this port and stopped it. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 794 | if (!data->inprogress()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 795 | return; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 796 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 797 | |
| 798 | // SignalAddressError is currently sent from StunPort/TurnPort. |
| 799 | // But this signal itself is generic. |
| 800 | data->set_error(); |
| 801 | // Send candidate allocation complete signal if this was the last port. |
| 802 | MaybeSignalCandidatesAllocationDone(); |
| 803 | } |
| 804 | |
| 805 | void BasicPortAllocatorSession::OnProtocolEnabled(AllocationSequence* seq, |
| 806 | ProtocolType proto) { |
| 807 | std::vector<Candidate> candidates; |
| 808 | for (std::vector<PortData>::iterator it = ports_.begin(); |
| 809 | it != ports_.end(); ++it) { |
| 810 | if (it->sequence() != seq) |
| 811 | continue; |
| 812 | |
| 813 | const std::vector<Candidate>& potentials = it->port()->Candidates(); |
| 814 | for (size_t i = 0; i < potentials.size(); ++i) { |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 815 | if (!CheckCandidateFilter(potentials[i])) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 816 | continue; |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 817 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 818 | ProtocolType pvalue; |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 819 | bool candidate_protocol_enabled = |
| 820 | StringToProto(potentials[i].protocol().c_str(), &pvalue) && |
| 821 | pvalue == proto; |
| 822 | if (candidate_protocol_enabled) { |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame^] | 823 | LOG(LS_INFO) << "Signaling candidate because protocol was enabled: " |
| 824 | << potentials[i].ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 825 | candidates.push_back(potentials[i]); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | if (!candidates.empty()) { |
| 831 | SignalCandidatesReady(this, candidates); |
| 832 | } |
| 833 | } |
| 834 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 835 | bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) const { |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 836 | uint32_t filter = candidate_filter_; |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 837 | |
| 838 | // When binding to any address, before sending packets out, the getsockname |
| 839 | // returns all 0s, but after sending packets, it'll be the NIC used to |
| 840 | // send. All 0s is not a valid ICE candidate address and should be filtered |
| 841 | // out. |
| 842 | if (c.address().IsAnyIP()) { |
| 843 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 844 | } |
| 845 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 846 | if (c.type() == RELAY_PORT_TYPE) { |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 847 | return ((filter & CF_RELAY) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 848 | } else if (c.type() == STUN_PORT_TYPE) { |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 849 | return ((filter & CF_REFLEXIVE) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 850 | } else if (c.type() == LOCAL_PORT_TYPE) { |
| 851 | if ((filter & CF_REFLEXIVE) && !c.address().IsPrivateIP()) { |
| 852 | // We allow host candidates if the filter allows server-reflexive |
| 853 | // candidates and the candidate is a public IP. Because we don't generate |
| 854 | // server-reflexive candidates if they have the same IP as the host |
| 855 | // candidate (i.e. when the host candidate is a public IP), filtering to |
| 856 | // only server-reflexive candidates won't work right when the host |
| 857 | // candidates have public IPs. |
| 858 | return true; |
| 859 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 860 | |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 861 | return ((filter & CF_HOST) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 862 | } |
| 863 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 866 | bool BasicPortAllocatorSession::CandidatePairable(const Candidate& c, |
| 867 | const Port* port) const { |
| 868 | bool candidate_signalable = CheckCandidateFilter(c); |
| 869 | |
| 870 | // When device enumeration is disabled (to prevent non-default IP addresses |
| 871 | // from leaking), we ping from some local candidates even though we don't |
| 872 | // signal them. However, if host candidates are also disabled (for example, to |
| 873 | // prevent even default IP addresses from leaking), we still don't want to |
| 874 | // ping from them, even if device enumeration is disabled. Thus, we check for |
| 875 | // both device enumeration and host candidates being disabled. |
| 876 | bool network_enumeration_disabled = c.address().IsAnyIP(); |
| 877 | bool can_ping_from_candidate = |
| 878 | (port->SharedSocket() || c.protocol() == TCP_PROTOCOL_NAME); |
| 879 | bool host_candidates_disabled = !(candidate_filter_ & CF_HOST); |
| 880 | |
| 881 | return candidate_signalable || |
| 882 | (network_enumeration_disabled && can_ping_from_candidate && |
| 883 | !host_candidates_disabled); |
| 884 | } |
| 885 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 886 | void BasicPortAllocatorSession::OnPortAllocationComplete( |
| 887 | AllocationSequence* seq) { |
| 888 | // Send candidate allocation complete signal if all ports are done. |
| 889 | MaybeSignalCandidatesAllocationDone(); |
| 890 | } |
| 891 | |
| 892 | void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 893 | if (CandidatesAllocationDone()) { |
| 894 | if (pooled()) { |
| 895 | LOG(LS_INFO) << "All candidates gathered for pooled session."; |
| 896 | } else { |
| 897 | LOG(LS_INFO) << "All candidates gathered for " << content_name() << ":" |
| 898 | << component() << ":" << generation(); |
| 899 | } |
| 900 | SignalCandidatesAllocationDone(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 901 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | void BasicPortAllocatorSession::OnPortDestroyed( |
| 905 | PortInterface* port) { |
| 906 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 907 | for (std::vector<PortData>::iterator iter = ports_.begin(); |
| 908 | iter != ports_.end(); ++iter) { |
| 909 | if (port == iter->port()) { |
| 910 | ports_.erase(iter); |
| 911 | LOG_J(LS_INFO, port) << "Removed port from allocator (" |
| 912 | << static_cast<int>(ports_.size()) << " remaining)"; |
| 913 | return; |
| 914 | } |
| 915 | } |
| 916 | ASSERT(false); |
| 917 | } |
| 918 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 919 | BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort( |
| 920 | Port* port) { |
| 921 | for (std::vector<PortData>::iterator it = ports_.begin(); |
| 922 | it != ports_.end(); ++it) { |
| 923 | if (it->port() == port) { |
| 924 | return &*it; |
| 925 | } |
| 926 | } |
| 927 | return NULL; |
| 928 | } |
| 929 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 930 | // Removes ports and candidates created on a given list of networks. |
| 931 | void BasicPortAllocatorSession::RemovePortsAndCandidates( |
| 932 | const std::vector<rtc::Network*>& networks) { |
| 933 | std::vector<PortInterface*> ports_to_remove; |
| 934 | std::vector<Candidate> candidates_to_remove; |
| 935 | for (PortData& data : ports_) { |
| 936 | if (std::find(networks.begin(), networks.end(), |
| 937 | data.sequence()->network()) == networks.end()) { |
| 938 | continue; |
| 939 | } |
| 940 | ports_to_remove.push_back(data.port()); |
| 941 | if (data.has_pairable_candidate()) { |
| 942 | GetCandidatesFromPort(data, &candidates_to_remove); |
| 943 | // Mark the port as having no pairable candidates so that its candidates |
| 944 | // won't be removed multiple times. |
| 945 | data.set_has_pairable_candidate(false); |
| 946 | } |
| 947 | } |
| 948 | if (!ports_to_remove.empty()) { |
| 949 | SignalPortsRemoved(this, ports_to_remove); |
| 950 | } |
| 951 | if (!candidates_to_remove.empty()) { |
| 952 | SignalCandidatesRemoved(this, candidates_to_remove); |
| 953 | } |
| 954 | } |
| 955 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 956 | // AllocationSequence |
| 957 | |
| 958 | AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session, |
| 959 | rtc::Network* network, |
| 960 | PortConfiguration* config, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 961 | uint32_t flags) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 962 | : session_(session), |
| 963 | network_(network), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 964 | ip_(network->GetBestIP()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 965 | config_(config), |
| 966 | state_(kInit), |
| 967 | flags_(flags), |
| 968 | udp_socket_(), |
| 969 | udp_port_(NULL), |
| 970 | phase_(0) { |
| 971 | } |
| 972 | |
| 973 | bool AllocationSequence::Init() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 974 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 975 | udp_socket_.reset(session_->socket_factory()->CreateUdpSocket( |
| 976 | rtc::SocketAddress(ip_, 0), session_->allocator()->min_port(), |
| 977 | session_->allocator()->max_port())); |
| 978 | if (udp_socket_) { |
| 979 | udp_socket_->SignalReadPacket.connect( |
| 980 | this, &AllocationSequence::OnReadPacket); |
| 981 | } |
| 982 | // Continuing if |udp_socket_| is NULL, as local TCP and RelayPort using TCP |
| 983 | // are next available options to setup a communication channel. |
| 984 | } |
| 985 | return true; |
| 986 | } |
| 987 | |
| 988 | void AllocationSequence::Clear() { |
| 989 | udp_port_ = NULL; |
| 990 | turn_ports_.clear(); |
| 991 | } |
| 992 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 993 | void AllocationSequence::OnNetworkFailed() { |
| 994 | RTC_DCHECK(!network_failed_); |
| 995 | network_failed_ = true; |
| 996 | // Stop the allocation sequence if its network failed. |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 997 | Stop(); |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 998 | } |
| 999 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1000 | AllocationSequence::~AllocationSequence() { |
| 1001 | session_->network_thread()->Clear(this); |
| 1002 | } |
| 1003 | |
| 1004 | void AllocationSequence::DisableEquivalentPhases(rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1005 | PortConfiguration* config, uint32_t* flags) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1006 | if (network_failed_) { |
| 1007 | // If the network of this allocation sequence has ever become failed, |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 1008 | // it won't be equivalent to the new network. |
| 1009 | return; |
| 1010 | } |
| 1011 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1012 | if (!((network == network_) && (ip_ == network->GetBestIP()))) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1013 | // Different network setup; nothing is equivalent. |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | // Else turn off the stuff that we've already got covered. |
| 1018 | |
| 1019 | // Every config implicitly specifies local, so turn that off right away. |
| 1020 | *flags |= PORTALLOCATOR_DISABLE_UDP; |
| 1021 | *flags |= PORTALLOCATOR_DISABLE_TCP; |
| 1022 | |
| 1023 | if (config_ && config) { |
| 1024 | if (config_->StunServers() == config->StunServers()) { |
| 1025 | // Already got this STUN servers covered. |
| 1026 | *flags |= PORTALLOCATOR_DISABLE_STUN; |
| 1027 | } |
| 1028 | if (!config_->relays.empty()) { |
| 1029 | // Already got relays covered. |
| 1030 | // NOTE: This will even skip a _different_ set of relay servers if we |
| 1031 | // were to be given one, but that never happens in our codebase. Should |
| 1032 | // probably get rid of the list in PortConfiguration and just keep a |
| 1033 | // single relay server in each one. |
| 1034 | *flags |= PORTALLOCATOR_DISABLE_RELAY; |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | void AllocationSequence::Start() { |
| 1040 | state_ = kRunning; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1041 | session_->network_thread()->Post(RTC_FROM_HERE, this, MSG_ALLOCATION_PHASE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | void AllocationSequence::Stop() { |
| 1045 | // If the port is completed, don't set it to stopped. |
| 1046 | if (state_ == kRunning) { |
| 1047 | state_ = kStopped; |
| 1048 | session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE); |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | void AllocationSequence::OnMessage(rtc::Message* msg) { |
| 1053 | ASSERT(rtc::Thread::Current() == session_->network_thread()); |
| 1054 | ASSERT(msg->message_id == MSG_ALLOCATION_PHASE); |
| 1055 | |
| 1056 | const char* const PHASE_NAMES[kNumPhases] = { |
| 1057 | "Udp", "Relay", "Tcp", "SslTcp" |
| 1058 | }; |
| 1059 | |
| 1060 | // Perform all of the phases in the current step. |
| 1061 | LOG_J(LS_INFO, network_) << "Allocation Phase=" |
| 1062 | << PHASE_NAMES[phase_]; |
| 1063 | |
| 1064 | switch (phase_) { |
| 1065 | case PHASE_UDP: |
| 1066 | CreateUDPPorts(); |
| 1067 | CreateStunPorts(); |
| 1068 | EnableProtocol(PROTO_UDP); |
| 1069 | break; |
| 1070 | |
| 1071 | case PHASE_RELAY: |
| 1072 | CreateRelayPorts(); |
| 1073 | break; |
| 1074 | |
| 1075 | case PHASE_TCP: |
| 1076 | CreateTCPPorts(); |
| 1077 | EnableProtocol(PROTO_TCP); |
| 1078 | break; |
| 1079 | |
| 1080 | case PHASE_SSLTCP: |
| 1081 | state_ = kCompleted; |
| 1082 | EnableProtocol(PROTO_SSLTCP); |
| 1083 | break; |
| 1084 | |
| 1085 | default: |
| 1086 | ASSERT(false); |
| 1087 | } |
| 1088 | |
| 1089 | if (state() == kRunning) { |
| 1090 | ++phase_; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1091 | session_->network_thread()->PostDelayed(RTC_FROM_HERE, |
| 1092 | session_->allocator()->step_delay(), |
| 1093 | this, MSG_ALLOCATION_PHASE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1094 | } else { |
| 1095 | // If all phases in AllocationSequence are completed, no allocation |
| 1096 | // steps needed further. Canceling pending signal. |
| 1097 | session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE); |
| 1098 | SignalPortAllocationComplete(this); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | void AllocationSequence::EnableProtocol(ProtocolType proto) { |
| 1103 | if (!ProtocolEnabled(proto)) { |
| 1104 | protocols_.push_back(proto); |
| 1105 | session_->OnProtocolEnabled(this, proto); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | bool AllocationSequence::ProtocolEnabled(ProtocolType proto) const { |
| 1110 | for (ProtocolList::const_iterator it = protocols_.begin(); |
| 1111 | it != protocols_.end(); ++it) { |
| 1112 | if (*it == proto) |
| 1113 | return true; |
| 1114 | } |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
| 1118 | void AllocationSequence::CreateUDPPorts() { |
| 1119 | if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP)) { |
| 1120 | LOG(LS_VERBOSE) << "AllocationSequence: UDP ports disabled, skipping."; |
| 1121 | return; |
| 1122 | } |
| 1123 | |
| 1124 | // TODO(mallinath) - Remove UDPPort creating socket after shared socket |
| 1125 | // is enabled completely. |
| 1126 | UDPPort* port = NULL; |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1127 | bool emit_local_candidate_for_anyaddress = |
| 1128 | !IsFlagSet(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1129 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) { |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 1130 | port = UDPPort::Create( |
| 1131 | session_->network_thread(), session_->socket_factory(), network_, |
| 1132 | udp_socket_.get(), session_->username(), session_->password(), |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1133 | session_->allocator()->origin(), emit_local_candidate_for_anyaddress); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1134 | } else { |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 1135 | port = UDPPort::Create( |
| 1136 | session_->network_thread(), session_->socket_factory(), network_, ip_, |
| 1137 | session_->allocator()->min_port(), session_->allocator()->max_port(), |
| 1138 | session_->username(), session_->password(), |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1139 | session_->allocator()->origin(), emit_local_candidate_for_anyaddress); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | if (port) { |
| 1143 | // If shared socket is enabled, STUN candidate will be allocated by the |
| 1144 | // UDPPort. |
| 1145 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 1146 | udp_port_ = port; |
jiayl@webrtc.org | 7e5b380 | 2015-01-22 21:28:39 +0000 | [diff] [blame] | 1147 | port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1148 | |
| 1149 | // If STUN is not disabled, setting stun server address to port. |
| 1150 | if (!IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1151 | if (config_ && !config_->StunServers().empty()) { |
| 1152 | LOG(LS_INFO) << "AllocationSequence: UDPPort will be handling the " |
| 1153 | << "STUN candidate generation."; |
| 1154 | port->set_server_addresses(config_->StunServers()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | session_->AddAllocatedPort(port, this, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | void AllocationSequence::CreateTCPPorts() { |
| 1164 | if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) { |
| 1165 | LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping."; |
| 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | Port* port = TCPPort::Create(session_->network_thread(), |
| 1170 | session_->socket_factory(), |
| 1171 | network_, ip_, |
| 1172 | session_->allocator()->min_port(), |
| 1173 | session_->allocator()->max_port(), |
| 1174 | session_->username(), session_->password(), |
| 1175 | session_->allocator()->allow_tcp_listen()); |
| 1176 | if (port) { |
| 1177 | session_->AddAllocatedPort(port, this, true); |
| 1178 | // Since TCPPort is not created using shared socket, |port| will not be |
| 1179 | // added to the dequeue. |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | void AllocationSequence::CreateStunPorts() { |
| 1184 | if (IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) { |
| 1185 | LOG(LS_VERBOSE) << "AllocationSequence: STUN ports disabled, skipping."; |
| 1186 | return; |
| 1187 | } |
| 1188 | |
| 1189 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 1190 | return; |
| 1191 | } |
| 1192 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1193 | if (!(config_ && !config_->StunServers().empty())) { |
| 1194 | LOG(LS_WARNING) |
| 1195 | << "AllocationSequence: No STUN server configured, skipping."; |
| 1196 | return; |
| 1197 | } |
| 1198 | |
| 1199 | StunPort* port = StunPort::Create(session_->network_thread(), |
| 1200 | session_->socket_factory(), |
| 1201 | network_, ip_, |
| 1202 | session_->allocator()->min_port(), |
| 1203 | session_->allocator()->max_port(), |
| 1204 | session_->username(), session_->password(), |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1205 | config_->StunServers(), |
| 1206 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1207 | if (port) { |
| 1208 | session_->AddAllocatedPort(port, this, true); |
| 1209 | // Since StunPort is not created using shared socket, |port| will not be |
| 1210 | // added to the dequeue. |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | void AllocationSequence::CreateRelayPorts() { |
| 1215 | if (IsFlagSet(PORTALLOCATOR_DISABLE_RELAY)) { |
| 1216 | LOG(LS_VERBOSE) << "AllocationSequence: Relay ports disabled, skipping."; |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we |
| 1221 | // ought to have a relay list for them here. |
| 1222 | ASSERT(config_ && !config_->relays.empty()); |
| 1223 | if (!(config_ && !config_->relays.empty())) { |
| 1224 | LOG(LS_WARNING) |
| 1225 | << "AllocationSequence: No relay server configured, skipping."; |
| 1226 | return; |
| 1227 | } |
| 1228 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1229 | for (RelayServerConfig& relay : config_->relays) { |
| 1230 | if (relay.type == RELAY_GTURN) { |
| 1231 | CreateGturnPort(relay); |
| 1232 | } else if (relay.type == RELAY_TURN) { |
| 1233 | CreateTurnPort(relay); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1234 | } else { |
| 1235 | ASSERT(false); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) { |
| 1241 | // TODO(mallinath) - Rename RelayPort to GTurnPort. |
| 1242 | RelayPort* port = RelayPort::Create(session_->network_thread(), |
| 1243 | session_->socket_factory(), |
| 1244 | network_, ip_, |
| 1245 | session_->allocator()->min_port(), |
| 1246 | session_->allocator()->max_port(), |
| 1247 | config_->username, config_->password); |
| 1248 | if (port) { |
| 1249 | // Since RelayPort is not created using shared socket, |port| will not be |
| 1250 | // added to the dequeue. |
| 1251 | // Note: We must add the allocated port before we add addresses because |
| 1252 | // the latter will create candidates that need name and preference |
| 1253 | // settings. However, we also can't prepare the address (normally |
| 1254 | // done by AddAllocatedPort) until we have these addresses. So we |
| 1255 | // wait to do that until below. |
| 1256 | session_->AddAllocatedPort(port, this, false); |
| 1257 | |
| 1258 | // Add the addresses of this protocol. |
| 1259 | PortList::const_iterator relay_port; |
| 1260 | for (relay_port = config.ports.begin(); |
| 1261 | relay_port != config.ports.end(); |
| 1262 | ++relay_port) { |
| 1263 | port->AddServerAddress(*relay_port); |
| 1264 | port->AddExternalAddress(*relay_port); |
| 1265 | } |
| 1266 | // Start fetching an address for this port. |
| 1267 | port->PrepareAddress(); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | void AllocationSequence::CreateTurnPort(const RelayServerConfig& config) { |
| 1272 | PortList::const_iterator relay_port; |
| 1273 | for (relay_port = config.ports.begin(); |
| 1274 | relay_port != config.ports.end(); ++relay_port) { |
| 1275 | TurnPort* port = NULL; |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 1276 | |
| 1277 | // Skip UDP connections to relay servers if it's disallowed. |
| 1278 | if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP_RELAY) && |
| 1279 | relay_port->proto == PROTO_UDP) { |
| 1280 | continue; |
| 1281 | } |
| 1282 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1283 | // Shared socket mode must be enabled only for UDP based ports. Hence |
| 1284 | // don't pass shared socket for ports which will create TCP sockets. |
| 1285 | // TODO(mallinath) - Enable shared socket mode for TURN ports. Disabled |
| 1286 | // due to webrtc bug https://code.google.com/p/webrtc/issues/detail?id=3537 |
| 1287 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 1288 | relay_port->proto == PROTO_UDP && udp_socket_) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1289 | port = TurnPort::Create(session_->network_thread(), |
| 1290 | session_->socket_factory(), |
| 1291 | network_, udp_socket_.get(), |
| 1292 | session_->username(), session_->password(), |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1293 | *relay_port, config.credentials, config.priority, |
| 1294 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1295 | turn_ports_.push_back(port); |
| 1296 | // Listen to the port destroyed signal, to allow AllocationSequence to |
| 1297 | // remove entrt from it's map. |
| 1298 | port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed); |
| 1299 | } else { |
| 1300 | port = TurnPort::Create(session_->network_thread(), |
| 1301 | session_->socket_factory(), |
| 1302 | network_, ip_, |
| 1303 | session_->allocator()->min_port(), |
| 1304 | session_->allocator()->max_port(), |
| 1305 | session_->username(), |
| 1306 | session_->password(), |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1307 | *relay_port, config.credentials, config.priority, |
| 1308 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1309 | } |
| 1310 | ASSERT(port != NULL); |
| 1311 | session_->AddAllocatedPort(port, this, true); |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | void AllocationSequence::OnReadPacket( |
| 1316 | rtc::AsyncPacketSocket* socket, const char* data, size_t size, |
| 1317 | const rtc::SocketAddress& remote_addr, |
| 1318 | const rtc::PacketTime& packet_time) { |
| 1319 | ASSERT(socket == udp_socket_.get()); |
| 1320 | |
| 1321 | bool turn_port_found = false; |
| 1322 | |
| 1323 | // Try to find the TurnPort that matches the remote address. Note that the |
| 1324 | // message could be a STUN binding response if the TURN server is also used as |
| 1325 | // a STUN server. We don't want to parse every message here to check if it is |
| 1326 | // a STUN binding response, so we pass the message to TurnPort regardless of |
| 1327 | // the message type. The TurnPort will just ignore the message since it will |
| 1328 | // not find any request by transaction ID. |
Sergey Ulanov | 17fa672 | 2016-05-10 10:20:47 -0700 | [diff] [blame] | 1329 | for (TurnPort* port : turn_ports_) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1330 | if (port->server_address().address == remote_addr) { |
Sergey Ulanov | 17fa672 | 2016-05-10 10:20:47 -0700 | [diff] [blame] | 1331 | if (port->HandleIncomingPacket(socket, data, size, remote_addr, |
| 1332 | packet_time)) { |
| 1333 | return; |
| 1334 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1335 | turn_port_found = true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | if (udp_port_) { |
| 1340 | const ServerAddresses& stun_servers = udp_port_->server_addresses(); |
| 1341 | |
| 1342 | // Pass the packet to the UdpPort if there is no matching TurnPort, or if |
| 1343 | // the TURN server is also a STUN server. |
| 1344 | if (!turn_port_found || |
| 1345 | stun_servers.find(remote_addr) != stun_servers.end()) { |
Sergey Ulanov | 17fa672 | 2016-05-10 10:20:47 -0700 | [diff] [blame] | 1346 | RTC_DCHECK(udp_port_->SharedSocket()); |
| 1347 | udp_port_->HandleIncomingPacket(socket, data, size, remote_addr, |
| 1348 | packet_time); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | void AllocationSequence::OnPortDestroyed(PortInterface* port) { |
| 1354 | if (udp_port_ == port) { |
| 1355 | udp_port_ = NULL; |
| 1356 | return; |
| 1357 | } |
| 1358 | |
jiayl@webrtc.org | 7e5b380 | 2015-01-22 21:28:39 +0000 | [diff] [blame] | 1359 | auto it = std::find(turn_ports_.begin(), turn_ports_.end(), port); |
| 1360 | if (it != turn_ports_.end()) { |
| 1361 | turn_ports_.erase(it); |
| 1362 | } else { |
| 1363 | LOG(LS_ERROR) << "Unexpected OnPortDestroyed for nonexistent port."; |
| 1364 | ASSERT(false); |
| 1365 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | // PortConfiguration |
| 1369 | PortConfiguration::PortConfiguration( |
| 1370 | const rtc::SocketAddress& stun_address, |
| 1371 | const std::string& username, |
| 1372 | const std::string& password) |
| 1373 | : stun_address(stun_address), username(username), password(password) { |
| 1374 | if (!stun_address.IsNil()) |
| 1375 | stun_servers.insert(stun_address); |
| 1376 | } |
| 1377 | |
| 1378 | PortConfiguration::PortConfiguration(const ServerAddresses& stun_servers, |
| 1379 | const std::string& username, |
| 1380 | const std::string& password) |
| 1381 | : stun_servers(stun_servers), |
| 1382 | username(username), |
| 1383 | password(password) { |
| 1384 | if (!stun_servers.empty()) |
| 1385 | stun_address = *(stun_servers.begin()); |
| 1386 | } |
| 1387 | |
| 1388 | ServerAddresses PortConfiguration::StunServers() { |
| 1389 | if (!stun_address.IsNil() && |
| 1390 | stun_servers.find(stun_address) == stun_servers.end()) { |
| 1391 | stun_servers.insert(stun_address); |
| 1392 | } |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 1393 | // Every UDP TURN server should also be used as a STUN server. |
| 1394 | ServerAddresses turn_servers = GetRelayServerAddresses(RELAY_TURN, PROTO_UDP); |
| 1395 | for (const rtc::SocketAddress& turn_server : turn_servers) { |
| 1396 | if (stun_servers.find(turn_server) == stun_servers.end()) { |
| 1397 | stun_servers.insert(turn_server); |
| 1398 | } |
| 1399 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1400 | return stun_servers; |
| 1401 | } |
| 1402 | |
| 1403 | void PortConfiguration::AddRelay(const RelayServerConfig& config) { |
| 1404 | relays.push_back(config); |
| 1405 | } |
| 1406 | |
| 1407 | bool PortConfiguration::SupportsProtocol( |
| 1408 | const RelayServerConfig& relay, ProtocolType type) const { |
| 1409 | PortList::const_iterator relay_port; |
| 1410 | for (relay_port = relay.ports.begin(); |
| 1411 | relay_port != relay.ports.end(); |
| 1412 | ++relay_port) { |
| 1413 | if (relay_port->proto == type) |
| 1414 | return true; |
| 1415 | } |
| 1416 | return false; |
| 1417 | } |
| 1418 | |
| 1419 | bool PortConfiguration::SupportsProtocol(RelayType turn_type, |
| 1420 | ProtocolType type) const { |
| 1421 | for (size_t i = 0; i < relays.size(); ++i) { |
| 1422 | if (relays[i].type == turn_type && |
| 1423 | SupportsProtocol(relays[i], type)) |
| 1424 | return true; |
| 1425 | } |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
| 1429 | ServerAddresses PortConfiguration::GetRelayServerAddresses( |
| 1430 | RelayType turn_type, ProtocolType type) const { |
| 1431 | ServerAddresses servers; |
| 1432 | for (size_t i = 0; i < relays.size(); ++i) { |
| 1433 | if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) { |
| 1434 | servers.insert(relays[i].ports.front().address); |
| 1435 | } |
| 1436 | } |
| 1437 | return servers; |
| 1438 | } |
| 1439 | |
| 1440 | } // namespace cricket |