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. |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 698 | bool pruned = false; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 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) { |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 703 | pruned = PruneTurnPorts(port); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 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); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 709 | port->KeepAliveUntilPruned(); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 710 | } |
Honghai Zhang | 17aac05 | 2016-06-29 21:41:53 -0700 | [diff] [blame] | 711 | } |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 712 | |
| 713 | ProtocolType pvalue; |
| 714 | bool candidate_protocol_enabled = |
| 715 | StringToProto(c.protocol().c_str(), &pvalue) && |
| 716 | data->sequence()->ProtocolEnabled(pvalue); |
| 717 | |
| 718 | if (data->ready() && CheckCandidateFilter(c) && candidate_protocol_enabled) { |
| 719 | std::vector<Candidate> candidates; |
| 720 | candidates.push_back(SanitizeRelatedAddress(c)); |
| 721 | SignalCandidatesReady(this, candidates); |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame] | 722 | } else if (!candidate_protocol_enabled) { |
| 723 | LOG(LS_INFO) |
| 724 | << "Not yet signaling candidate because protocol is not yet enabled."; |
| 725 | } else { |
| 726 | LOG(LS_INFO) << "Discarding candidate because it doesn't match filter."; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | // If we have pruned any port, maybe need to signal port allocation done. |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 730 | if (pruned) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 731 | MaybeSignalCandidatesAllocationDone(); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | Port* BasicPortAllocatorSession::GetBestTurnPortForNetwork( |
| 736 | const std::string& network_name) const { |
| 737 | Port* best_turn_port = nullptr; |
| 738 | for (const PortData& data : ports_) { |
| 739 | if (data.port()->Network()->name() == network_name && |
| 740 | data.port()->Type() == RELAY_PORT_TYPE && data.ready() && |
| 741 | (!best_turn_port || ComparePort(data.port(), best_turn_port) > 0)) { |
| 742 | best_turn_port = data.port(); |
| 743 | } |
| 744 | } |
| 745 | return best_turn_port; |
| 746 | } |
| 747 | |
| 748 | bool BasicPortAllocatorSession::PruneTurnPorts(Port* newly_pairable_turn_port) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 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 | |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 757 | bool pruned = false; |
| 758 | std::vector<PortInterface*> pruned_ports; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 759 | for (PortData& data : ports_) { |
| 760 | if (data.port()->Network()->name() == network_name && |
| 761 | data.port()->Type() == RELAY_PORT_TYPE && !data.pruned() && |
| 762 | ComparePort(data.port(), best_turn_port) < 0) { |
| 763 | data.set_pruned(); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 764 | pruned = true; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 765 | data.port()->Prune(); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 766 | if (data.port() != newly_pairable_turn_port) { |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 767 | pruned_ports.push_back(data.port()); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | } |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 771 | if (!pruned_ports.empty()) { |
| 772 | LOG(LS_INFO) << "Pruned " << pruned_ports.size() << " ports"; |
| 773 | SignalPortsPruned(this, pruned_ports); |
| 774 | } |
| 775 | return pruned; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 778 | void BasicPortAllocatorSession::PruneAllPorts() { |
| 779 | for (PortData& data : ports_) { |
| 780 | data.port()->Prune(); |
| 781 | } |
| 782 | } |
| 783 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 784 | void BasicPortAllocatorSession::OnPortComplete(Port* port) { |
| 785 | ASSERT(rtc::Thread::Current() == network_thread_); |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame] | 786 | LOG_J(LS_INFO, port) << "Port completed gathering candidates."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 787 | PortData* data = FindPort(port); |
| 788 | ASSERT(data != NULL); |
| 789 | |
| 790 | // Ignore any late signals. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 791 | if (!data->inprogress()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 792 | return; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 793 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 794 | |
| 795 | // Moving to COMPLETE state. |
| 796 | data->set_complete(); |
| 797 | // Send candidate allocation complete signal if this was the last port. |
| 798 | MaybeSignalCandidatesAllocationDone(); |
| 799 | } |
| 800 | |
| 801 | void BasicPortAllocatorSession::OnPortError(Port* port) { |
| 802 | ASSERT(rtc::Thread::Current() == network_thread_); |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame] | 803 | LOG_J(LS_INFO, port) << "Port encountered error while gathering candidates."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 804 | PortData* data = FindPort(port); |
| 805 | ASSERT(data != NULL); |
| 806 | // We might have already given up on this port and stopped it. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 807 | if (!data->inprogress()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 808 | return; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 809 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 810 | |
| 811 | // SignalAddressError is currently sent from StunPort/TurnPort. |
| 812 | // But this signal itself is generic. |
| 813 | data->set_error(); |
| 814 | // Send candidate allocation complete signal if this was the last port. |
| 815 | MaybeSignalCandidatesAllocationDone(); |
| 816 | } |
| 817 | |
| 818 | void BasicPortAllocatorSession::OnProtocolEnabled(AllocationSequence* seq, |
| 819 | ProtocolType proto) { |
| 820 | std::vector<Candidate> candidates; |
| 821 | for (std::vector<PortData>::iterator it = ports_.begin(); |
| 822 | it != ports_.end(); ++it) { |
| 823 | if (it->sequence() != seq) |
| 824 | continue; |
| 825 | |
| 826 | const std::vector<Candidate>& potentials = it->port()->Candidates(); |
| 827 | for (size_t i = 0; i < potentials.size(); ++i) { |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 828 | if (!CheckCandidateFilter(potentials[i])) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 829 | continue; |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 830 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 831 | ProtocolType pvalue; |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 832 | bool candidate_protocol_enabled = |
| 833 | StringToProto(potentials[i].protocol().c_str(), &pvalue) && |
| 834 | pvalue == proto; |
| 835 | if (candidate_protocol_enabled) { |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame] | 836 | LOG(LS_INFO) << "Signaling candidate because protocol was enabled: " |
| 837 | << potentials[i].ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 838 | candidates.push_back(potentials[i]); |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (!candidates.empty()) { |
| 844 | SignalCandidatesReady(this, candidates); |
| 845 | } |
| 846 | } |
| 847 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 848 | bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) const { |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 849 | uint32_t filter = candidate_filter_; |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 850 | |
| 851 | // When binding to any address, before sending packets out, the getsockname |
| 852 | // returns all 0s, but after sending packets, it'll be the NIC used to |
| 853 | // send. All 0s is not a valid ICE candidate address and should be filtered |
| 854 | // out. |
| 855 | if (c.address().IsAnyIP()) { |
| 856 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 857 | } |
| 858 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 859 | if (c.type() == RELAY_PORT_TYPE) { |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 860 | return ((filter & CF_RELAY) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 861 | } else if (c.type() == STUN_PORT_TYPE) { |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 862 | return ((filter & CF_REFLEXIVE) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 863 | } else if (c.type() == LOCAL_PORT_TYPE) { |
| 864 | if ((filter & CF_REFLEXIVE) && !c.address().IsPrivateIP()) { |
| 865 | // We allow host candidates if the filter allows server-reflexive |
| 866 | // candidates and the candidate is a public IP. Because we don't generate |
| 867 | // server-reflexive candidates if they have the same IP as the host |
| 868 | // candidate (i.e. when the host candidate is a public IP), filtering to |
| 869 | // only server-reflexive candidates won't work right when the host |
| 870 | // candidates have public IPs. |
| 871 | return true; |
| 872 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 873 | |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 874 | return ((filter & CF_HOST) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 875 | } |
| 876 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 879 | bool BasicPortAllocatorSession::CandidatePairable(const Candidate& c, |
| 880 | const Port* port) const { |
| 881 | bool candidate_signalable = CheckCandidateFilter(c); |
| 882 | |
| 883 | // When device enumeration is disabled (to prevent non-default IP addresses |
| 884 | // from leaking), we ping from some local candidates even though we don't |
| 885 | // signal them. However, if host candidates are also disabled (for example, to |
| 886 | // prevent even default IP addresses from leaking), we still don't want to |
| 887 | // ping from them, even if device enumeration is disabled. Thus, we check for |
| 888 | // both device enumeration and host candidates being disabled. |
| 889 | bool network_enumeration_disabled = c.address().IsAnyIP(); |
| 890 | bool can_ping_from_candidate = |
| 891 | (port->SharedSocket() || c.protocol() == TCP_PROTOCOL_NAME); |
| 892 | bool host_candidates_disabled = !(candidate_filter_ & CF_HOST); |
| 893 | |
| 894 | return candidate_signalable || |
| 895 | (network_enumeration_disabled && can_ping_from_candidate && |
| 896 | !host_candidates_disabled); |
| 897 | } |
| 898 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 899 | void BasicPortAllocatorSession::OnPortAllocationComplete( |
| 900 | AllocationSequence* seq) { |
| 901 | // Send candidate allocation complete signal if all ports are done. |
| 902 | MaybeSignalCandidatesAllocationDone(); |
| 903 | } |
| 904 | |
| 905 | void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 906 | if (CandidatesAllocationDone()) { |
| 907 | if (pooled()) { |
| 908 | LOG(LS_INFO) << "All candidates gathered for pooled session."; |
| 909 | } else { |
| 910 | LOG(LS_INFO) << "All candidates gathered for " << content_name() << ":" |
| 911 | << component() << ":" << generation(); |
| 912 | } |
| 913 | SignalCandidatesAllocationDone(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 914 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | void BasicPortAllocatorSession::OnPortDestroyed( |
| 918 | PortInterface* port) { |
| 919 | ASSERT(rtc::Thread::Current() == network_thread_); |
| 920 | for (std::vector<PortData>::iterator iter = ports_.begin(); |
| 921 | iter != ports_.end(); ++iter) { |
| 922 | if (port == iter->port()) { |
| 923 | ports_.erase(iter); |
| 924 | LOG_J(LS_INFO, port) << "Removed port from allocator (" |
| 925 | << static_cast<int>(ports_.size()) << " remaining)"; |
| 926 | return; |
| 927 | } |
| 928 | } |
| 929 | ASSERT(false); |
| 930 | } |
| 931 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 932 | BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort( |
| 933 | Port* port) { |
| 934 | for (std::vector<PortData>::iterator it = ports_.begin(); |
| 935 | it != ports_.end(); ++it) { |
| 936 | if (it->port() == port) { |
| 937 | return &*it; |
| 938 | } |
| 939 | } |
| 940 | return NULL; |
| 941 | } |
| 942 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 943 | // Removes ports and candidates created on a given list of networks. |
| 944 | void BasicPortAllocatorSession::RemovePortsAndCandidates( |
| 945 | const std::vector<rtc::Network*>& networks) { |
| 946 | std::vector<PortInterface*> ports_to_remove; |
| 947 | std::vector<Candidate> candidates_to_remove; |
| 948 | for (PortData& data : ports_) { |
| 949 | if (std::find(networks.begin(), networks.end(), |
| 950 | data.sequence()->network()) == networks.end()) { |
| 951 | continue; |
| 952 | } |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 953 | // Prune the port so that it may be destroyed. |
| 954 | data.port()->Prune(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 955 | ports_to_remove.push_back(data.port()); |
| 956 | if (data.has_pairable_candidate()) { |
| 957 | GetCandidatesFromPort(data, &candidates_to_remove); |
| 958 | // Mark the port as having no pairable candidates so that its candidates |
| 959 | // won't be removed multiple times. |
| 960 | data.set_has_pairable_candidate(false); |
| 961 | } |
| 962 | } |
| 963 | if (!ports_to_remove.empty()) { |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 964 | LOG(LS_INFO) << "Removed " << ports_to_remove.size() << " ports"; |
| 965 | SignalPortsPruned(this, ports_to_remove); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 966 | } |
| 967 | if (!candidates_to_remove.empty()) { |
| 968 | SignalCandidatesRemoved(this, candidates_to_remove); |
| 969 | } |
| 970 | } |
| 971 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 972 | // AllocationSequence |
| 973 | |
| 974 | AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session, |
| 975 | rtc::Network* network, |
| 976 | PortConfiguration* config, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 977 | uint32_t flags) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 978 | : session_(session), |
| 979 | network_(network), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 980 | ip_(network->GetBestIP()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 981 | config_(config), |
| 982 | state_(kInit), |
| 983 | flags_(flags), |
| 984 | udp_socket_(), |
| 985 | udp_port_(NULL), |
| 986 | phase_(0) { |
| 987 | } |
| 988 | |
| 989 | bool AllocationSequence::Init() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 990 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 991 | udp_socket_.reset(session_->socket_factory()->CreateUdpSocket( |
| 992 | rtc::SocketAddress(ip_, 0), session_->allocator()->min_port(), |
| 993 | session_->allocator()->max_port())); |
| 994 | if (udp_socket_) { |
| 995 | udp_socket_->SignalReadPacket.connect( |
| 996 | this, &AllocationSequence::OnReadPacket); |
| 997 | } |
| 998 | // Continuing if |udp_socket_| is NULL, as local TCP and RelayPort using TCP |
| 999 | // are next available options to setup a communication channel. |
| 1000 | } |
| 1001 | return true; |
| 1002 | } |
| 1003 | |
| 1004 | void AllocationSequence::Clear() { |
| 1005 | udp_port_ = NULL; |
| 1006 | turn_ports_.clear(); |
| 1007 | } |
| 1008 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1009 | void AllocationSequence::OnNetworkFailed() { |
| 1010 | RTC_DCHECK(!network_failed_); |
| 1011 | network_failed_ = true; |
| 1012 | // Stop the allocation sequence if its network failed. |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 1013 | Stop(); |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1016 | AllocationSequence::~AllocationSequence() { |
| 1017 | session_->network_thread()->Clear(this); |
| 1018 | } |
| 1019 | |
| 1020 | void AllocationSequence::DisableEquivalentPhases(rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1021 | PortConfiguration* config, uint32_t* flags) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1022 | if (network_failed_) { |
| 1023 | // If the network of this allocation sequence has ever become failed, |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 1024 | // it won't be equivalent to the new network. |
| 1025 | return; |
| 1026 | } |
| 1027 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1028 | if (!((network == network_) && (ip_ == network->GetBestIP()))) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1029 | // Different network setup; nothing is equivalent. |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | // Else turn off the stuff that we've already got covered. |
| 1034 | |
| 1035 | // Every config implicitly specifies local, so turn that off right away. |
| 1036 | *flags |= PORTALLOCATOR_DISABLE_UDP; |
| 1037 | *flags |= PORTALLOCATOR_DISABLE_TCP; |
| 1038 | |
| 1039 | if (config_ && config) { |
| 1040 | if (config_->StunServers() == config->StunServers()) { |
| 1041 | // Already got this STUN servers covered. |
| 1042 | *flags |= PORTALLOCATOR_DISABLE_STUN; |
| 1043 | } |
| 1044 | if (!config_->relays.empty()) { |
| 1045 | // Already got relays covered. |
| 1046 | // NOTE: This will even skip a _different_ set of relay servers if we |
| 1047 | // were to be given one, but that never happens in our codebase. Should |
| 1048 | // probably get rid of the list in PortConfiguration and just keep a |
| 1049 | // single relay server in each one. |
| 1050 | *flags |= PORTALLOCATOR_DISABLE_RELAY; |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | void AllocationSequence::Start() { |
| 1056 | state_ = kRunning; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1057 | session_->network_thread()->Post(RTC_FROM_HERE, this, MSG_ALLOCATION_PHASE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | void AllocationSequence::Stop() { |
| 1061 | // If the port is completed, don't set it to stopped. |
| 1062 | if (state_ == kRunning) { |
| 1063 | state_ = kStopped; |
| 1064 | session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | void AllocationSequence::OnMessage(rtc::Message* msg) { |
| 1069 | ASSERT(rtc::Thread::Current() == session_->network_thread()); |
| 1070 | ASSERT(msg->message_id == MSG_ALLOCATION_PHASE); |
| 1071 | |
| 1072 | const char* const PHASE_NAMES[kNumPhases] = { |
| 1073 | "Udp", "Relay", "Tcp", "SslTcp" |
| 1074 | }; |
| 1075 | |
| 1076 | // Perform all of the phases in the current step. |
| 1077 | LOG_J(LS_INFO, network_) << "Allocation Phase=" |
| 1078 | << PHASE_NAMES[phase_]; |
| 1079 | |
| 1080 | switch (phase_) { |
| 1081 | case PHASE_UDP: |
| 1082 | CreateUDPPorts(); |
| 1083 | CreateStunPorts(); |
| 1084 | EnableProtocol(PROTO_UDP); |
| 1085 | break; |
| 1086 | |
| 1087 | case PHASE_RELAY: |
| 1088 | CreateRelayPorts(); |
| 1089 | break; |
| 1090 | |
| 1091 | case PHASE_TCP: |
| 1092 | CreateTCPPorts(); |
| 1093 | EnableProtocol(PROTO_TCP); |
| 1094 | break; |
| 1095 | |
| 1096 | case PHASE_SSLTCP: |
| 1097 | state_ = kCompleted; |
| 1098 | EnableProtocol(PROTO_SSLTCP); |
| 1099 | break; |
| 1100 | |
| 1101 | default: |
| 1102 | ASSERT(false); |
| 1103 | } |
| 1104 | |
| 1105 | if (state() == kRunning) { |
| 1106 | ++phase_; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1107 | session_->network_thread()->PostDelayed(RTC_FROM_HERE, |
| 1108 | session_->allocator()->step_delay(), |
| 1109 | this, MSG_ALLOCATION_PHASE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1110 | } else { |
| 1111 | // If all phases in AllocationSequence are completed, no allocation |
| 1112 | // steps needed further. Canceling pending signal. |
| 1113 | session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE); |
| 1114 | SignalPortAllocationComplete(this); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | void AllocationSequence::EnableProtocol(ProtocolType proto) { |
| 1119 | if (!ProtocolEnabled(proto)) { |
| 1120 | protocols_.push_back(proto); |
| 1121 | session_->OnProtocolEnabled(this, proto); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | bool AllocationSequence::ProtocolEnabled(ProtocolType proto) const { |
| 1126 | for (ProtocolList::const_iterator it = protocols_.begin(); |
| 1127 | it != protocols_.end(); ++it) { |
| 1128 | if (*it == proto) |
| 1129 | return true; |
| 1130 | } |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
| 1134 | void AllocationSequence::CreateUDPPorts() { |
| 1135 | if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP)) { |
| 1136 | LOG(LS_VERBOSE) << "AllocationSequence: UDP ports disabled, skipping."; |
| 1137 | return; |
| 1138 | } |
| 1139 | |
| 1140 | // TODO(mallinath) - Remove UDPPort creating socket after shared socket |
| 1141 | // is enabled completely. |
| 1142 | UDPPort* port = NULL; |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1143 | bool emit_local_candidate_for_anyaddress = |
| 1144 | !IsFlagSet(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1145 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) { |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 1146 | port = UDPPort::Create( |
| 1147 | session_->network_thread(), session_->socket_factory(), network_, |
| 1148 | udp_socket_.get(), session_->username(), session_->password(), |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1149 | session_->allocator()->origin(), emit_local_candidate_for_anyaddress); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1150 | } else { |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 1151 | port = UDPPort::Create( |
| 1152 | session_->network_thread(), session_->socket_factory(), network_, ip_, |
| 1153 | session_->allocator()->min_port(), session_->allocator()->max_port(), |
| 1154 | session_->username(), session_->password(), |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1155 | session_->allocator()->origin(), emit_local_candidate_for_anyaddress); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | if (port) { |
| 1159 | // If shared socket is enabled, STUN candidate will be allocated by the |
| 1160 | // UDPPort. |
| 1161 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 1162 | udp_port_ = port; |
jiayl@webrtc.org | 7e5b380 | 2015-01-22 21:28:39 +0000 | [diff] [blame] | 1163 | port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1164 | |
| 1165 | // If STUN is not disabled, setting stun server address to port. |
| 1166 | if (!IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1167 | if (config_ && !config_->StunServers().empty()) { |
| 1168 | LOG(LS_INFO) << "AllocationSequence: UDPPort will be handling the " |
| 1169 | << "STUN candidate generation."; |
| 1170 | port->set_server_addresses(config_->StunServers()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | session_->AddAllocatedPort(port, this, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | void AllocationSequence::CreateTCPPorts() { |
| 1180 | if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) { |
| 1181 | LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping."; |
| 1182 | return; |
| 1183 | } |
| 1184 | |
| 1185 | Port* port = TCPPort::Create(session_->network_thread(), |
| 1186 | session_->socket_factory(), |
| 1187 | network_, ip_, |
| 1188 | session_->allocator()->min_port(), |
| 1189 | session_->allocator()->max_port(), |
| 1190 | session_->username(), session_->password(), |
| 1191 | session_->allocator()->allow_tcp_listen()); |
| 1192 | if (port) { |
| 1193 | session_->AddAllocatedPort(port, this, true); |
| 1194 | // Since TCPPort is not created using shared socket, |port| will not be |
| 1195 | // added to the dequeue. |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | void AllocationSequence::CreateStunPorts() { |
| 1200 | if (IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) { |
| 1201 | LOG(LS_VERBOSE) << "AllocationSequence: STUN ports disabled, skipping."; |
| 1202 | return; |
| 1203 | } |
| 1204 | |
| 1205 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 1206 | return; |
| 1207 | } |
| 1208 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1209 | if (!(config_ && !config_->StunServers().empty())) { |
| 1210 | LOG(LS_WARNING) |
| 1211 | << "AllocationSequence: No STUN server configured, skipping."; |
| 1212 | return; |
| 1213 | } |
| 1214 | |
| 1215 | StunPort* port = StunPort::Create(session_->network_thread(), |
| 1216 | session_->socket_factory(), |
| 1217 | network_, ip_, |
| 1218 | session_->allocator()->min_port(), |
| 1219 | session_->allocator()->max_port(), |
| 1220 | session_->username(), session_->password(), |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1221 | config_->StunServers(), |
| 1222 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1223 | if (port) { |
| 1224 | session_->AddAllocatedPort(port, this, true); |
| 1225 | // Since StunPort is not created using shared socket, |port| will not be |
| 1226 | // added to the dequeue. |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | void AllocationSequence::CreateRelayPorts() { |
| 1231 | if (IsFlagSet(PORTALLOCATOR_DISABLE_RELAY)) { |
| 1232 | LOG(LS_VERBOSE) << "AllocationSequence: Relay ports disabled, skipping."; |
| 1233 | return; |
| 1234 | } |
| 1235 | |
| 1236 | // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we |
| 1237 | // ought to have a relay list for them here. |
| 1238 | ASSERT(config_ && !config_->relays.empty()); |
| 1239 | if (!(config_ && !config_->relays.empty())) { |
| 1240 | LOG(LS_WARNING) |
| 1241 | << "AllocationSequence: No relay server configured, skipping."; |
| 1242 | return; |
| 1243 | } |
| 1244 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1245 | for (RelayServerConfig& relay : config_->relays) { |
| 1246 | if (relay.type == RELAY_GTURN) { |
| 1247 | CreateGturnPort(relay); |
| 1248 | } else if (relay.type == RELAY_TURN) { |
| 1249 | CreateTurnPort(relay); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1250 | } else { |
| 1251 | ASSERT(false); |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) { |
| 1257 | // TODO(mallinath) - Rename RelayPort to GTurnPort. |
| 1258 | RelayPort* port = RelayPort::Create(session_->network_thread(), |
| 1259 | session_->socket_factory(), |
| 1260 | network_, ip_, |
| 1261 | session_->allocator()->min_port(), |
| 1262 | session_->allocator()->max_port(), |
| 1263 | config_->username, config_->password); |
| 1264 | if (port) { |
| 1265 | // Since RelayPort is not created using shared socket, |port| will not be |
| 1266 | // added to the dequeue. |
| 1267 | // Note: We must add the allocated port before we add addresses because |
| 1268 | // the latter will create candidates that need name and preference |
| 1269 | // settings. However, we also can't prepare the address (normally |
| 1270 | // done by AddAllocatedPort) until we have these addresses. So we |
| 1271 | // wait to do that until below. |
| 1272 | session_->AddAllocatedPort(port, this, false); |
| 1273 | |
| 1274 | // Add the addresses of this protocol. |
| 1275 | PortList::const_iterator relay_port; |
| 1276 | for (relay_port = config.ports.begin(); |
| 1277 | relay_port != config.ports.end(); |
| 1278 | ++relay_port) { |
| 1279 | port->AddServerAddress(*relay_port); |
| 1280 | port->AddExternalAddress(*relay_port); |
| 1281 | } |
| 1282 | // Start fetching an address for this port. |
| 1283 | port->PrepareAddress(); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | void AllocationSequence::CreateTurnPort(const RelayServerConfig& config) { |
| 1288 | PortList::const_iterator relay_port; |
| 1289 | for (relay_port = config.ports.begin(); |
| 1290 | relay_port != config.ports.end(); ++relay_port) { |
| 1291 | TurnPort* port = NULL; |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 1292 | |
| 1293 | // Skip UDP connections to relay servers if it's disallowed. |
| 1294 | if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP_RELAY) && |
| 1295 | relay_port->proto == PROTO_UDP) { |
| 1296 | continue; |
| 1297 | } |
| 1298 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1299 | // Shared socket mode must be enabled only for UDP based ports. Hence |
| 1300 | // don't pass shared socket for ports which will create TCP sockets. |
| 1301 | // TODO(mallinath) - Enable shared socket mode for TURN ports. Disabled |
| 1302 | // due to webrtc bug https://code.google.com/p/webrtc/issues/detail?id=3537 |
| 1303 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 1304 | relay_port->proto == PROTO_UDP && udp_socket_) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1305 | port = TurnPort::Create(session_->network_thread(), |
| 1306 | session_->socket_factory(), |
| 1307 | network_, udp_socket_.get(), |
| 1308 | session_->username(), session_->password(), |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1309 | *relay_port, config.credentials, config.priority, |
| 1310 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1311 | turn_ports_.push_back(port); |
| 1312 | // Listen to the port destroyed signal, to allow AllocationSequence to |
| 1313 | // remove entrt from it's map. |
| 1314 | port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed); |
| 1315 | } else { |
| 1316 | port = TurnPort::Create(session_->network_thread(), |
| 1317 | session_->socket_factory(), |
| 1318 | network_, ip_, |
| 1319 | session_->allocator()->min_port(), |
| 1320 | session_->allocator()->max_port(), |
| 1321 | session_->username(), |
| 1322 | session_->password(), |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 1323 | *relay_port, config.credentials, config.priority, |
| 1324 | session_->allocator()->origin()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1325 | } |
| 1326 | ASSERT(port != NULL); |
| 1327 | session_->AddAllocatedPort(port, this, true); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | void AllocationSequence::OnReadPacket( |
| 1332 | rtc::AsyncPacketSocket* socket, const char* data, size_t size, |
| 1333 | const rtc::SocketAddress& remote_addr, |
| 1334 | const rtc::PacketTime& packet_time) { |
| 1335 | ASSERT(socket == udp_socket_.get()); |
| 1336 | |
| 1337 | bool turn_port_found = false; |
| 1338 | |
| 1339 | // Try to find the TurnPort that matches the remote address. Note that the |
| 1340 | // message could be a STUN binding response if the TURN server is also used as |
| 1341 | // a STUN server. We don't want to parse every message here to check if it is |
| 1342 | // a STUN binding response, so we pass the message to TurnPort regardless of |
| 1343 | // the message type. The TurnPort will just ignore the message since it will |
| 1344 | // not find any request by transaction ID. |
Sergey Ulanov | 17fa672 | 2016-05-10 10:20:47 -0700 | [diff] [blame] | 1345 | for (TurnPort* port : turn_ports_) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1346 | if (port->server_address().address == remote_addr) { |
Sergey Ulanov | 17fa672 | 2016-05-10 10:20:47 -0700 | [diff] [blame] | 1347 | if (port->HandleIncomingPacket(socket, data, size, remote_addr, |
| 1348 | packet_time)) { |
| 1349 | return; |
| 1350 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1351 | turn_port_found = true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | if (udp_port_) { |
| 1356 | const ServerAddresses& stun_servers = udp_port_->server_addresses(); |
| 1357 | |
| 1358 | // Pass the packet to the UdpPort if there is no matching TurnPort, or if |
| 1359 | // the TURN server is also a STUN server. |
| 1360 | if (!turn_port_found || |
| 1361 | stun_servers.find(remote_addr) != stun_servers.end()) { |
Sergey Ulanov | 17fa672 | 2016-05-10 10:20:47 -0700 | [diff] [blame] | 1362 | RTC_DCHECK(udp_port_->SharedSocket()); |
| 1363 | udp_port_->HandleIncomingPacket(socket, data, size, remote_addr, |
| 1364 | packet_time); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | void AllocationSequence::OnPortDestroyed(PortInterface* port) { |
| 1370 | if (udp_port_ == port) { |
| 1371 | udp_port_ = NULL; |
| 1372 | return; |
| 1373 | } |
| 1374 | |
jiayl@webrtc.org | 7e5b380 | 2015-01-22 21:28:39 +0000 | [diff] [blame] | 1375 | auto it = std::find(turn_ports_.begin(), turn_ports_.end(), port); |
| 1376 | if (it != turn_ports_.end()) { |
| 1377 | turn_ports_.erase(it); |
| 1378 | } else { |
| 1379 | LOG(LS_ERROR) << "Unexpected OnPortDestroyed for nonexistent port."; |
| 1380 | ASSERT(false); |
| 1381 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | // PortConfiguration |
| 1385 | PortConfiguration::PortConfiguration( |
| 1386 | const rtc::SocketAddress& stun_address, |
| 1387 | const std::string& username, |
| 1388 | const std::string& password) |
| 1389 | : stun_address(stun_address), username(username), password(password) { |
| 1390 | if (!stun_address.IsNil()) |
| 1391 | stun_servers.insert(stun_address); |
| 1392 | } |
| 1393 | |
| 1394 | PortConfiguration::PortConfiguration(const ServerAddresses& stun_servers, |
| 1395 | const std::string& username, |
| 1396 | const std::string& password) |
| 1397 | : stun_servers(stun_servers), |
| 1398 | username(username), |
| 1399 | password(password) { |
| 1400 | if (!stun_servers.empty()) |
| 1401 | stun_address = *(stun_servers.begin()); |
| 1402 | } |
| 1403 | |
| 1404 | ServerAddresses PortConfiguration::StunServers() { |
| 1405 | if (!stun_address.IsNil() && |
| 1406 | stun_servers.find(stun_address) == stun_servers.end()) { |
| 1407 | stun_servers.insert(stun_address); |
| 1408 | } |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 1409 | // Every UDP TURN server should also be used as a STUN server. |
| 1410 | ServerAddresses turn_servers = GetRelayServerAddresses(RELAY_TURN, PROTO_UDP); |
| 1411 | for (const rtc::SocketAddress& turn_server : turn_servers) { |
| 1412 | if (stun_servers.find(turn_server) == stun_servers.end()) { |
| 1413 | stun_servers.insert(turn_server); |
| 1414 | } |
| 1415 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1416 | return stun_servers; |
| 1417 | } |
| 1418 | |
| 1419 | void PortConfiguration::AddRelay(const RelayServerConfig& config) { |
| 1420 | relays.push_back(config); |
| 1421 | } |
| 1422 | |
| 1423 | bool PortConfiguration::SupportsProtocol( |
| 1424 | const RelayServerConfig& relay, ProtocolType type) const { |
| 1425 | PortList::const_iterator relay_port; |
| 1426 | for (relay_port = relay.ports.begin(); |
| 1427 | relay_port != relay.ports.end(); |
| 1428 | ++relay_port) { |
| 1429 | if (relay_port->proto == type) |
| 1430 | return true; |
| 1431 | } |
| 1432 | return false; |
| 1433 | } |
| 1434 | |
| 1435 | bool PortConfiguration::SupportsProtocol(RelayType turn_type, |
| 1436 | ProtocolType type) const { |
| 1437 | for (size_t i = 0; i < relays.size(); ++i) { |
| 1438 | if (relays[i].type == turn_type && |
| 1439 | SupportsProtocol(relays[i], type)) |
| 1440 | return true; |
| 1441 | } |
| 1442 | return false; |
| 1443 | } |
| 1444 | |
| 1445 | ServerAddresses PortConfiguration::GetRelayServerAddresses( |
| 1446 | RelayType turn_type, ProtocolType type) const { |
| 1447 | ServerAddresses servers; |
| 1448 | for (size_t i = 0; i < relays.size(); ++i) { |
| 1449 | if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) { |
| 1450 | servers.insert(relays[i].ports.front().address); |
| 1451 | } |
| 1452 | } |
| 1453 | return servers; |
| 1454 | } |
| 1455 | |
| 1456 | } // namespace cricket |