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/base/portallocator.h" |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 12 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 13 | #include <iterator> |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 16 | #include "p2p/base/icecredentialsiterator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 18 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 19 | namespace cricket { |
| 20 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 21 | RelayServerConfig::RelayServerConfig(RelayType type) : type(type) {} |
| 22 | |
| 23 | RelayServerConfig::RelayServerConfig(const rtc::SocketAddress& address, |
| 24 | const std::string& username, |
| 25 | const std::string& password, |
| 26 | ProtocolType proto) |
| 27 | : type(RELAY_TURN), credentials(username, password) { |
| 28 | ports.push_back(ProtocolAddress(address, proto)); |
| 29 | } |
| 30 | |
| 31 | RelayServerConfig::RelayServerConfig(const std::string& address, |
| 32 | int port, |
| 33 | const std::string& username, |
| 34 | const std::string& password, |
| 35 | ProtocolType proto) |
| 36 | : RelayServerConfig(rtc::SocketAddress(address, port), |
| 37 | username, |
| 38 | password, |
| 39 | proto) {} |
| 40 | |
| 41 | // Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS. |
| 42 | RelayServerConfig::RelayServerConfig(const std::string& address, |
| 43 | int port, |
| 44 | const std::string& username, |
| 45 | const std::string& password, |
| 46 | ProtocolType proto, |
| 47 | bool secure) |
| 48 | : RelayServerConfig(address, |
| 49 | port, |
| 50 | username, |
| 51 | password, |
| 52 | (proto == PROTO_TCP && secure ? PROTO_TLS : proto)) {} |
| 53 | |
| 54 | RelayServerConfig::RelayServerConfig(const RelayServerConfig&) = default; |
| 55 | |
| 56 | RelayServerConfig::~RelayServerConfig() = default; |
| 57 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 58 | PortAllocatorSession::PortAllocatorSession(const std::string& content_name, |
| 59 | int component, |
| 60 | const std::string& ice_ufrag, |
| 61 | const std::string& ice_pwd, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 62 | uint32_t flags) |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 63 | : flags_(flags), |
deadbeef | c55fb30 | 2016-05-12 12:51:38 -0700 | [diff] [blame] | 64 | generation_(0), |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 65 | content_name_(content_name), |
| 66 | component_(component), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 67 | ice_ufrag_(ice_ufrag), |
| 68 | ice_pwd_(ice_pwd) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 69 | // Pooled sessions are allowed to be created with empty content name, |
| 70 | // component, ufrag and password. |
| 71 | RTC_DCHECK(ice_ufrag.empty() == ice_pwd.empty()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 74 | PortAllocatorSession::~PortAllocatorSession() = default; |
| 75 | |
| 76 | bool PortAllocatorSession::IsCleared() const { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | bool PortAllocatorSession::IsStopped() const { |
| 81 | return false; |
| 82 | } |
| 83 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 84 | void PortAllocatorSession::GetCandidateStatsFromReadyPorts( |
| 85 | CandidateStatsList* candidate_stats_list) const { |
| 86 | auto ports = ReadyPorts(); |
| 87 | for (auto* port : ports) { |
| 88 | auto candidates = port->Candidates(); |
| 89 | for (const auto& candidate : candidates) { |
| 90 | CandidateStats candidate_stats(candidate); |
| 91 | port->GetStunStats(&candidate_stats.stun_stats); |
| 92 | candidate_stats_list->push_back(std::move(candidate_stats)); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 97 | uint32_t PortAllocatorSession::generation() { |
| 98 | return generation_; |
| 99 | } |
| 100 | |
| 101 | void PortAllocatorSession::set_generation(uint32_t generation) { |
| 102 | generation_ = generation; |
| 103 | } |
| 104 | |
| 105 | PortAllocator::PortAllocator() |
| 106 | : flags_(kDefaultPortAllocatorFlags), |
| 107 | min_port_(0), |
| 108 | max_port_(0), |
| 109 | max_ipv6_networks_(kDefaultMaxIPv6Networks), |
| 110 | step_delay_(kDefaultStepDelay), |
| 111 | allow_tcp_listen_(true), |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 112 | candidate_filter_(CF_ALL) { |
| 113 | // The allocator will be attached to a thread in Initialize. |
| 114 | thread_checker_.DetachFromThread(); |
| 115 | } |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 116 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 117 | void PortAllocator::Initialize() { |
| 118 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 119 | initialized_ = true; |
| 120 | } |
| 121 | |
| 122 | PortAllocator::~PortAllocator() { |
| 123 | CheckRunOnValidThreadIfInitialized(); |
| 124 | } |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 125 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 126 | void PortAllocator::set_restrict_ice_credentials_change(bool value) { |
| 127 | restrict_ice_credentials_change_ = value; |
| 128 | } |
| 129 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 130 | bool PortAllocator::SetConfiguration( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 131 | const ServerAddresses& stun_servers, |
| 132 | const std::vector<RelayServerConfig>& turn_servers, |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 133 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 134 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 135 | webrtc::TurnCustomizer* turn_customizer, |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 136 | const absl::optional<int>& stun_candidate_keepalive_interval) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 137 | CheckRunOnValidThreadIfInitialized(); |
Qingsi Wang | e6ded16 | 2018-10-02 16:00:41 -0700 | [diff] [blame] | 138 | // A positive candidate pool size would lead to the creation of a pooled |
| 139 | // allocator session and starting getting ports, which we should only do on |
| 140 | // the network thread. |
| 141 | RTC_DCHECK(candidate_pool_size == 0 || thread_checker_.CalledOnValidThread()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 142 | bool ice_servers_changed = |
| 143 | (stun_servers != stun_servers_ || turn_servers != turn_servers_); |
| 144 | stun_servers_ = stun_servers; |
| 145 | turn_servers_ = turn_servers; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 146 | prune_turn_ports_ = prune_turn_ports; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 147 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 148 | if (candidate_pool_frozen_) { |
| 149 | if (candidate_pool_size != candidate_pool_size_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 150 | RTC_LOG(LS_ERROR) |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 151 | << "Trying to change candidate pool size after pool was frozen."; |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | return true; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 155 | } |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 156 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 157 | if (candidate_pool_size < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 158 | RTC_LOG(LS_ERROR) << "Can't set negative pool size."; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 159 | return false; |
| 160 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 161 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 162 | candidate_pool_size_ = candidate_pool_size; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 163 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 164 | // If ICE servers changed, throw away any existing pooled sessions and create |
| 165 | // new ones. |
| 166 | if (ice_servers_changed) { |
| 167 | pooled_sessions_.clear(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 170 | turn_customizer_ = turn_customizer; |
| 171 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 172 | // If |candidate_pool_size_| is less than the number of pooled sessions, get |
| 173 | // rid of the extras. |
| 174 | while (candidate_pool_size_ < static_cast<int>(pooled_sessions_.size())) { |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 175 | pooled_sessions_.back().reset(nullptr); |
| 176 | pooled_sessions_.pop_back(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 177 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 178 | |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 179 | // |stun_candidate_keepalive_interval_| will be used in STUN port allocation |
| 180 | // in future sessions. We also update the ready ports in the pooled sessions. |
| 181 | // Ports in sessions that are taken and owned by P2PTransportChannel will be |
| 182 | // updated there via IceConfig. |
| 183 | stun_candidate_keepalive_interval_ = stun_candidate_keepalive_interval; |
| 184 | for (const auto& session : pooled_sessions_) { |
| 185 | session->SetStunKeepaliveIntervalForReadyPorts( |
| 186 | stun_candidate_keepalive_interval_); |
| 187 | } |
| 188 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 189 | // If |candidate_pool_size_| is greater than the number of pooled sessions, |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 190 | // create new sessions. |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 191 | while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) { |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 192 | IceParameters iceCredentials = |
| 193 | IceCredentialsIterator::CreateRandomIceCredentials(); |
| 194 | PortAllocatorSession* pooled_session = |
| 195 | CreateSessionInternal("", 0, iceCredentials.ufrag, iceCredentials.pwd); |
| 196 | pooled_session->set_pooled(true); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 197 | pooled_session->StartGettingPorts(); |
| 198 | pooled_sessions_.push_back( |
| 199 | std::unique_ptr<PortAllocatorSession>(pooled_session)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 200 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 201 | return true; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 205 | const std::string& content_name, |
| 206 | int component, |
| 207 | const std::string& ice_ufrag, |
| 208 | const std::string& ice_pwd) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 209 | CheckRunOnValidThreadAndInitialized(); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 210 | auto session = std::unique_ptr<PortAllocatorSession>( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 211 | CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd)); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 212 | session->SetCandidateFilter(candidate_filter()); |
| 213 | return session; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | std::unique_ptr<PortAllocatorSession> PortAllocator::TakePooledSession( |
| 217 | const std::string& content_name, |
| 218 | int component, |
| 219 | const std::string& ice_ufrag, |
| 220 | const std::string& ice_pwd) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 221 | CheckRunOnValidThreadAndInitialized(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 222 | RTC_DCHECK(!ice_ufrag.empty()); |
| 223 | RTC_DCHECK(!ice_pwd.empty()); |
| 224 | if (pooled_sessions_.empty()) { |
| 225 | return nullptr; |
| 226 | } |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 227 | |
| 228 | IceParameters credentials(ice_ufrag, ice_pwd, false); |
| 229 | // If restrict_ice_credentials_change_ is TRUE, then call FindPooledSession |
| 230 | // with ice credentials. Otherwise call it with nullptr which means |
| 231 | // "find any" pooled session. |
| 232 | auto cit = FindPooledSession(restrict_ice_credentials_change_ ? &credentials |
| 233 | : nullptr); |
| 234 | if (cit == pooled_sessions_.end()) { |
| 235 | return nullptr; |
| 236 | } |
| 237 | |
| 238 | auto it = |
| 239 | pooled_sessions_.begin() + std::distance(pooled_sessions_.cbegin(), cit); |
| 240 | std::unique_ptr<PortAllocatorSession> ret = std::move(*it); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 241 | ret->SetIceParameters(content_name, component, ice_ufrag, ice_pwd); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 242 | ret->set_pooled(false); |
| 243 | // According to JSEP, a pooled session should filter candidates only |
| 244 | // after it's taken out of the pool. |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 245 | ret->SetCandidateFilter(candidate_filter()); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 246 | pooled_sessions_.erase(it); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 247 | return ret; |
| 248 | } |
| 249 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 250 | const PortAllocatorSession* PortAllocator::GetPooledSession( |
| 251 | const IceParameters* ice_credentials) const { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 252 | CheckRunOnValidThreadAndInitialized(); |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 253 | auto it = FindPooledSession(ice_credentials); |
| 254 | if (it == pooled_sessions_.end()) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 255 | return nullptr; |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 256 | } else { |
| 257 | return it->get(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 258 | } |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 259 | } |
| 260 | |
| 261 | std::vector<std::unique_ptr<PortAllocatorSession>>::const_iterator |
| 262 | PortAllocator::FindPooledSession(const IceParameters* ice_credentials) const { |
| 263 | for (auto it = pooled_sessions_.begin(); it != pooled_sessions_.end(); ++it) { |
| 264 | if (ice_credentials == nullptr || |
| 265 | ((*it)->ice_ufrag() == ice_credentials->ufrag && |
| 266 | (*it)->ice_pwd() == ice_credentials->pwd)) { |
| 267 | return it; |
| 268 | } |
| 269 | } |
| 270 | return pooled_sessions_.end(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 271 | } |
| 272 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 273 | void PortAllocator::FreezeCandidatePool() { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 274 | CheckRunOnValidThreadAndInitialized(); |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 275 | candidate_pool_frozen_ = true; |
| 276 | } |
| 277 | |
| 278 | void PortAllocator::DiscardCandidatePool() { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 279 | CheckRunOnValidThreadIfInitialized(); |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 280 | pooled_sessions_.clear(); |
| 281 | } |
| 282 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 283 | void PortAllocator::GetCandidateStatsFromPooledSessions( |
| 284 | CandidateStatsList* candidate_stats_list) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 285 | CheckRunOnValidThreadAndInitialized(); |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 286 | for (const auto& session : pooled_sessions()) { |
| 287 | session->GetCandidateStatsFromReadyPorts(candidate_stats_list); |
| 288 | } |
| 289 | } |
| 290 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame^] | 291 | std::vector<IceParameters> PortAllocator::GetPooledIceCredentials() { |
| 292 | CheckRunOnValidThreadAndInitialized(); |
| 293 | std::vector<IceParameters> list; |
| 294 | for (const auto& session : pooled_sessions_) { |
| 295 | list.push_back( |
| 296 | IceParameters(session->ice_ufrag(), session->ice_pwd(), false)); |
| 297 | } |
| 298 | return list; |
| 299 | } |
| 300 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 301 | } // namespace cricket |