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