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