blob: c6ea6fd765049bde471bdffe50e86b42a0de719e [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "p2p/base/port_allocator.h"
Steve Anton6c38cc72017-11-29 10:25:58 -080012
Jonas Oreland1cd39fa2018-10-11 07:47:12 +020013#include <iterator>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <set>
Steve Anton6c38cc72017-11-29 10:25:58 -080015#include <utility>
16
Steve Anton10542f22019-01-11 09:11:00 -080017#include "p2p/base/ice_credentials_iterator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/logging.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021namespace cricket {
22
Steve Anton7995d8c2017-10-30 16:23:38 -070023RelayServerConfig::RelayServerConfig(RelayType type) : type(type) {}
24
25RelayServerConfig::RelayServerConfig(const rtc::SocketAddress& address,
26 const std::string& username,
27 const std::string& password,
28 ProtocolType proto)
29 : type(RELAY_TURN), credentials(username, password) {
30 ports.push_back(ProtocolAddress(address, proto));
31}
32
33RelayServerConfig::RelayServerConfig(const std::string& address,
34 int port,
35 const std::string& username,
36 const std::string& password,
37 ProtocolType proto)
38 : RelayServerConfig(rtc::SocketAddress(address, port),
39 username,
40 password,
41 proto) {}
42
43// Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS.
44RelayServerConfig::RelayServerConfig(const std::string& address,
45 int port,
46 const std::string& username,
47 const std::string& password,
48 ProtocolType proto,
49 bool secure)
50 : RelayServerConfig(address,
51 port,
52 username,
53 password,
54 (proto == PROTO_TCP && secure ? PROTO_TLS : proto)) {}
55
56RelayServerConfig::RelayServerConfig(const RelayServerConfig&) = default;
57
58RelayServerConfig::~RelayServerConfig() = default;
59
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060PortAllocatorSession::PortAllocatorSession(const std::string& content_name,
61 int component,
62 const std::string& ice_ufrag,
63 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +020064 uint32_t flags)
Taylor Brandstettera1c30352016-05-13 08:15:11 -070065 : flags_(flags),
deadbeefc55fb302016-05-12 12:51:38 -070066 generation_(0),
Taylor Brandstettera1c30352016-05-13 08:15:11 -070067 content_name_(content_name),
68 component_(component),
deadbeefcbecd352015-09-23 11:50:27 -070069 ice_ufrag_(ice_ufrag),
70 ice_pwd_(ice_pwd) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -070071 // Pooled sessions are allowed to be created with empty content name,
72 // component, ufrag and password.
73 RTC_DCHECK(ice_ufrag.empty() == ice_pwd.empty());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074}
75
Steve Anton7995d8c2017-10-30 16:23:38 -070076PortAllocatorSession::~PortAllocatorSession() = default;
77
78bool PortAllocatorSession::IsCleared() const {
79 return false;
80}
81
82bool PortAllocatorSession::IsStopped() const {
83 return false;
84}
85
Qingsi Wang72a43a12018-02-20 16:03:18 -080086void PortAllocatorSession::GetCandidateStatsFromReadyPorts(
87 CandidateStatsList* candidate_stats_list) const {
88 auto ports = ReadyPorts();
89 for (auto* port : ports) {
90 auto candidates = port->Candidates();
91 for (const auto& candidate : candidates) {
92 CandidateStats candidate_stats(candidate);
93 port->GetStunStats(&candidate_stats.stun_stats);
Qingsi Wang1dac6d82018-12-12 15:28:47 -080094 bool mdns_obfuscation_enabled =
95 port->Network()->GetMdnsResponder() != nullptr;
96 if (mdns_obfuscation_enabled) {
97 bool use_hostname_address = candidate.type() == LOCAL_PORT_TYPE;
98 bool filter_related_address = candidate.type() == STUN_PORT_TYPE;
99 candidate_stats.candidate = candidate_stats.candidate.ToSanitizedCopy(
100 use_hostname_address, filter_related_address);
101 }
Qingsi Wang72a43a12018-02-20 16:03:18 -0800102 candidate_stats_list->push_back(std::move(candidate_stats));
103 }
104 }
105}
106
Steve Anton7995d8c2017-10-30 16:23:38 -0700107uint32_t PortAllocatorSession::generation() {
108 return generation_;
109}
110
111void PortAllocatorSession::set_generation(uint32_t generation) {
112 generation_ = generation;
113}
114
115PortAllocator::PortAllocator()
116 : flags_(kDefaultPortAllocatorFlags),
117 min_port_(0),
118 max_port_(0),
119 max_ipv6_networks_(kDefaultMaxIPv6Networks),
120 step_delay_(kDefaultStepDelay),
121 allow_tcp_listen_(true),
Qingsi Wanga2d60672018-04-11 16:57:45 -0700122 candidate_filter_(CF_ALL) {
123 // The allocator will be attached to a thread in Initialize.
124 thread_checker_.DetachFromThread();
125}
Steve Anton7995d8c2017-10-30 16:23:38 -0700126
Qingsi Wanga2d60672018-04-11 16:57:45 -0700127void PortAllocator::Initialize() {
128 RTC_DCHECK(thread_checker_.CalledOnValidThread());
129 initialized_ = true;
130}
131
132PortAllocator::~PortAllocator() {
133 CheckRunOnValidThreadIfInitialized();
134}
Steve Anton7995d8c2017-10-30 16:23:38 -0700135
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200136void PortAllocator::set_restrict_ice_credentials_change(bool value) {
137 restrict_ice_credentials_change_ = value;
138}
139
deadbeef6de92f92016-12-12 18:49:32 -0800140bool PortAllocator::SetConfiguration(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700141 const ServerAddresses& stun_servers,
142 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700143 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200144 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800145 webrtc::TurnCustomizer* turn_customizer,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200146 const absl::optional<int>& stun_candidate_keepalive_interval) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700147 CheckRunOnValidThreadIfInitialized();
Qingsi Wange6ded162018-10-02 16:00:41 -0700148 // A positive candidate pool size would lead to the creation of a pooled
149 // allocator session and starting getting ports, which we should only do on
150 // the network thread.
151 RTC_DCHECK(candidate_pool_size == 0 || thread_checker_.CalledOnValidThread());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700152 bool ice_servers_changed =
153 (stun_servers != stun_servers_ || turn_servers != turn_servers_);
154 stun_servers_ = stun_servers;
155 turn_servers_ = turn_servers;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700156 prune_turn_ports_ = prune_turn_ports;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700157
deadbeef42a42632017-03-10 15:18:00 -0800158 if (candidate_pool_frozen_) {
159 if (candidate_pool_size != candidate_pool_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100160 RTC_LOG(LS_ERROR)
Jonas Olssond7d762d2018-03-28 09:47:51 +0200161 << "Trying to change candidate pool size after pool was frozen.";
deadbeef42a42632017-03-10 15:18:00 -0800162 return false;
163 }
164 return true;
deadbeef6de92f92016-12-12 18:49:32 -0800165 }
deadbeef42a42632017-03-10 15:18:00 -0800166
deadbeef6de92f92016-12-12 18:49:32 -0800167 if (candidate_pool_size < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100168 RTC_LOG(LS_ERROR) << "Can't set negative pool size.";
deadbeef6de92f92016-12-12 18:49:32 -0800169 return false;
170 }
deadbeef6de92f92016-12-12 18:49:32 -0800171
deadbeef42a42632017-03-10 15:18:00 -0800172 candidate_pool_size_ = candidate_pool_size;
deadbeef6de92f92016-12-12 18:49:32 -0800173
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700174 // If ICE servers changed, throw away any existing pooled sessions and create
175 // new ones.
176 if (ice_servers_changed) {
177 pooled_sessions_.clear();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700178 }
179
Jonas Orelandbdcee282017-10-10 14:01:40 +0200180 turn_customizer_ = turn_customizer;
181
deadbeef42a42632017-03-10 15:18:00 -0800182 // If |candidate_pool_size_| is less than the number of pooled sessions, get
183 // rid of the extras.
184 while (candidate_pool_size_ < static_cast<int>(pooled_sessions_.size())) {
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200185 pooled_sessions_.back().reset(nullptr);
186 pooled_sessions_.pop_back();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700187 }
deadbeef6de92f92016-12-12 18:49:32 -0800188
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800189 // |stun_candidate_keepalive_interval_| will be used in STUN port allocation
190 // in future sessions. We also update the ready ports in the pooled sessions.
191 // Ports in sessions that are taken and owned by P2PTransportChannel will be
192 // updated there via IceConfig.
193 stun_candidate_keepalive_interval_ = stun_candidate_keepalive_interval;
194 for (const auto& session : pooled_sessions_) {
195 session->SetStunKeepaliveIntervalForReadyPorts(
196 stun_candidate_keepalive_interval_);
197 }
198
deadbeef42a42632017-03-10 15:18:00 -0800199 // If |candidate_pool_size_| is greater than the number of pooled sessions,
deadbeef6de92f92016-12-12 18:49:32 -0800200 // create new sessions.
deadbeef42a42632017-03-10 15:18:00 -0800201 while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) {
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200202 IceParameters iceCredentials =
203 IceCredentialsIterator::CreateRandomIceCredentials();
204 PortAllocatorSession* pooled_session =
205 CreateSessionInternal("", 0, iceCredentials.ufrag, iceCredentials.pwd);
206 pooled_session->set_pooled(true);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700207 pooled_session->StartGettingPorts();
208 pooled_sessions_.push_back(
209 std::unique_ptr<PortAllocatorSession>(pooled_session));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700210 }
deadbeef6de92f92016-12-12 18:49:32 -0800211 return true;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700212}
213
214std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215 const std::string& content_name,
216 int component,
217 const std::string& ice_ufrag,
218 const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700219 CheckRunOnValidThreadAndInitialized();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700220 auto session = std::unique_ptr<PortAllocatorSession>(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700221 CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700222 session->SetCandidateFilter(candidate_filter());
223 return session;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700224}
225
226std::unique_ptr<PortAllocatorSession> PortAllocator::TakePooledSession(
227 const std::string& content_name,
228 int component,
229 const std::string& ice_ufrag,
230 const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700231 CheckRunOnValidThreadAndInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700232 RTC_DCHECK(!ice_ufrag.empty());
233 RTC_DCHECK(!ice_pwd.empty());
234 if (pooled_sessions_.empty()) {
235 return nullptr;
236 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200237
238 IceParameters credentials(ice_ufrag, ice_pwd, false);
239 // If restrict_ice_credentials_change_ is TRUE, then call FindPooledSession
240 // with ice credentials. Otherwise call it with nullptr which means
241 // "find any" pooled session.
242 auto cit = FindPooledSession(restrict_ice_credentials_change_ ? &credentials
243 : nullptr);
244 if (cit == pooled_sessions_.end()) {
245 return nullptr;
246 }
247
248 auto it =
249 pooled_sessions_.begin() + std::distance(pooled_sessions_.cbegin(), cit);
250 std::unique_ptr<PortAllocatorSession> ret = std::move(*it);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700251 ret->SetIceParameters(content_name, component, ice_ufrag, ice_pwd);
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200252 ret->set_pooled(false);
253 // According to JSEP, a pooled session should filter candidates only
254 // after it's taken out of the pool.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700255 ret->SetCandidateFilter(candidate_filter());
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200256 pooled_sessions_.erase(it);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700257 return ret;
258}
259
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200260const PortAllocatorSession* PortAllocator::GetPooledSession(
261 const IceParameters* ice_credentials) const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700262 CheckRunOnValidThreadAndInitialized();
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200263 auto it = FindPooledSession(ice_credentials);
264 if (it == pooled_sessions_.end()) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700265 return nullptr;
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200266 } else {
267 return it->get();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700268 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200269}
270
271std::vector<std::unique_ptr<PortAllocatorSession>>::const_iterator
272PortAllocator::FindPooledSession(const IceParameters* ice_credentials) const {
273 for (auto it = pooled_sessions_.begin(); it != pooled_sessions_.end(); ++it) {
274 if (ice_credentials == nullptr ||
275 ((*it)->ice_ufrag() == ice_credentials->ufrag &&
276 (*it)->ice_pwd() == ice_credentials->pwd)) {
277 return it;
278 }
279 }
280 return pooled_sessions_.end();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000281}
282
deadbeef42a42632017-03-10 15:18:00 -0800283void PortAllocator::FreezeCandidatePool() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700284 CheckRunOnValidThreadAndInitialized();
deadbeef42a42632017-03-10 15:18:00 -0800285 candidate_pool_frozen_ = true;
286}
287
288void PortAllocator::DiscardCandidatePool() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700289 CheckRunOnValidThreadIfInitialized();
deadbeef42a42632017-03-10 15:18:00 -0800290 pooled_sessions_.clear();
291}
292
Qingsi Wang72a43a12018-02-20 16:03:18 -0800293void PortAllocator::GetCandidateStatsFromPooledSessions(
294 CandidateStatsList* candidate_stats_list) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700295 CheckRunOnValidThreadAndInitialized();
Qingsi Wang72a43a12018-02-20 16:03:18 -0800296 for (const auto& session : pooled_sessions()) {
297 session->GetCandidateStatsFromReadyPorts(candidate_stats_list);
298 }
299}
300
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200301std::vector<IceParameters> PortAllocator::GetPooledIceCredentials() {
302 CheckRunOnValidThreadAndInitialized();
303 std::vector<IceParameters> list;
304 for (const auto& session : pooled_sessions_) {
305 list.push_back(
306 IceParameters(session->ice_ufrag(), session->ice_pwd(), false));
307 }
308 return list;
309}
310
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000311} // namespace cricket