blob: b13896c4bc02f131d6794bb5c563c9f8967bf0d1 [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
Niels Möller191e38f2019-11-04 08:49:12 +010023RelayServerConfig::RelayServerConfig() {}
Steve Anton7995d8c2017-10-30 16:23:38 -070024
25RelayServerConfig::RelayServerConfig(const rtc::SocketAddress& address,
26 const std::string& username,
27 const std::string& password,
28 ProtocolType proto)
Niels Möller191e38f2019-11-04 08:49:12 +010029 : credentials(username, password) {
Steve Anton7995d8c2017-10-30 16:23:38 -070030 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
86uint32_t PortAllocatorSession::generation() {
87 return generation_;
88}
89
90void PortAllocatorSession::set_generation(uint32_t generation) {
91 generation_ = generation;
92}
93
94PortAllocator::PortAllocator()
95 : flags_(kDefaultPortAllocatorFlags),
96 min_port_(0),
97 max_port_(0),
98 max_ipv6_networks_(kDefaultMaxIPv6Networks),
99 step_delay_(kDefaultStepDelay),
100 allow_tcp_listen_(true),
Qingsi Wanga2d60672018-04-11 16:57:45 -0700101 candidate_filter_(CF_ALL) {
102 // The allocator will be attached to a thread in Initialize.
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200103 thread_checker_.Detach();
Qingsi Wanga2d60672018-04-11 16:57:45 -0700104}
Steve Anton7995d8c2017-10-30 16:23:38 -0700105
Qingsi Wanga2d60672018-04-11 16:57:45 -0700106void PortAllocator::Initialize() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200107 RTC_DCHECK(thread_checker_.IsCurrent());
Qingsi Wanga2d60672018-04-11 16:57:45 -0700108 initialized_ = true;
109}
110
111PortAllocator::~PortAllocator() {
112 CheckRunOnValidThreadIfInitialized();
113}
Steve Anton7995d8c2017-10-30 16:23:38 -0700114
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200115void PortAllocator::set_restrict_ice_credentials_change(bool value) {
116 restrict_ice_credentials_change_ = value;
117}
118
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700119// Deprecated
deadbeef6de92f92016-12-12 18:49:32 -0800120bool PortAllocator::SetConfiguration(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700121 const ServerAddresses& stun_servers,
122 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700123 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200124 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800125 webrtc::TurnCustomizer* turn_customizer,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200126 const absl::optional<int>& stun_candidate_keepalive_interval) {
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700127 webrtc::PortPrunePolicy turn_port_prune_policy =
128 prune_turn_ports ? webrtc::PRUNE_BASED_ON_PRIORITY : webrtc::NO_PRUNE;
129 return SetConfiguration(stun_servers, turn_servers, candidate_pool_size,
130 turn_port_prune_policy, turn_customizer,
131 stun_candidate_keepalive_interval);
132}
133
134bool PortAllocator::SetConfiguration(
135 const ServerAddresses& stun_servers,
136 const std::vector<RelayServerConfig>& turn_servers,
137 int candidate_pool_size,
138 webrtc::PortPrunePolicy turn_port_prune_policy,
139 webrtc::TurnCustomizer* turn_customizer,
140 const absl::optional<int>& stun_candidate_keepalive_interval) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700141 CheckRunOnValidThreadIfInitialized();
Qingsi Wange6ded162018-10-02 16:00:41 -0700142 // A positive candidate pool size would lead to the creation of a pooled
143 // allocator session and starting getting ports, which we should only do on
144 // the network thread.
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200145 RTC_DCHECK(candidate_pool_size == 0 || thread_checker_.IsCurrent());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700146 bool ice_servers_changed =
147 (stun_servers != stun_servers_ || turn_servers != turn_servers_);
148 stun_servers_ = stun_servers;
149 turn_servers_ = turn_servers;
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700150 turn_port_prune_policy_ = turn_port_prune_policy;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700151
deadbeef42a42632017-03-10 15:18:00 -0800152 if (candidate_pool_frozen_) {
153 if (candidate_pool_size != candidate_pool_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100154 RTC_LOG(LS_ERROR)
Jonas Olssond7d762d2018-03-28 09:47:51 +0200155 << "Trying to change candidate pool size after pool was frozen.";
deadbeef42a42632017-03-10 15:18:00 -0800156 return false;
157 }
158 return true;
deadbeef6de92f92016-12-12 18:49:32 -0800159 }
deadbeef42a42632017-03-10 15:18:00 -0800160
deadbeef6de92f92016-12-12 18:49:32 -0800161 if (candidate_pool_size < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100162 RTC_LOG(LS_ERROR) << "Can't set negative pool size.";
deadbeef6de92f92016-12-12 18:49:32 -0800163 return false;
164 }
deadbeef6de92f92016-12-12 18:49:32 -0800165
deadbeef42a42632017-03-10 15:18:00 -0800166 candidate_pool_size_ = candidate_pool_size;
deadbeef6de92f92016-12-12 18:49:32 -0800167
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700168 // If ICE servers changed, throw away any existing pooled sessions and create
169 // new ones.
170 if (ice_servers_changed) {
171 pooled_sessions_.clear();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700172 }
173
Jonas Orelandbdcee282017-10-10 14:01:40 +0200174 turn_customizer_ = turn_customizer;
175
deadbeef42a42632017-03-10 15:18:00 -0800176 // If |candidate_pool_size_| is less than the number of pooled sessions, get
177 // rid of the extras.
178 while (candidate_pool_size_ < static_cast<int>(pooled_sessions_.size())) {
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200179 pooled_sessions_.back().reset(nullptr);
180 pooled_sessions_.pop_back();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700181 }
deadbeef6de92f92016-12-12 18:49:32 -0800182
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800183 // |stun_candidate_keepalive_interval_| will be used in STUN port allocation
184 // in future sessions. We also update the ready ports in the pooled sessions.
185 // Ports in sessions that are taken and owned by P2PTransportChannel will be
186 // updated there via IceConfig.
187 stun_candidate_keepalive_interval_ = stun_candidate_keepalive_interval;
188 for (const auto& session : pooled_sessions_) {
189 session->SetStunKeepaliveIntervalForReadyPorts(
190 stun_candidate_keepalive_interval_);
191 }
192
deadbeef42a42632017-03-10 15:18:00 -0800193 // If |candidate_pool_size_| is greater than the number of pooled sessions,
deadbeef6de92f92016-12-12 18:49:32 -0800194 // create new sessions.
deadbeef42a42632017-03-10 15:18:00 -0800195 while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) {
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200196 IceParameters iceCredentials =
197 IceCredentialsIterator::CreateRandomIceCredentials();
198 PortAllocatorSession* pooled_session =
199 CreateSessionInternal("", 0, iceCredentials.ufrag, iceCredentials.pwd);
200 pooled_session->set_pooled(true);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700201 pooled_session->StartGettingPorts();
202 pooled_sessions_.push_back(
203 std::unique_ptr<PortAllocatorSession>(pooled_session));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700204 }
deadbeef6de92f92016-12-12 18:49:32 -0800205 return true;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700206}
207
208std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209 const std::string& content_name,
210 int component,
211 const std::string& ice_ufrag,
212 const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700213 CheckRunOnValidThreadAndInitialized();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700214 auto session = std::unique_ptr<PortAllocatorSession>(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700215 CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700216 session->SetCandidateFilter(candidate_filter());
217 return session;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700218}
219
220std::unique_ptr<PortAllocatorSession> PortAllocator::TakePooledSession(
221 const std::string& content_name,
222 int component,
223 const std::string& ice_ufrag,
224 const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700225 CheckRunOnValidThreadAndInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700226 RTC_DCHECK(!ice_ufrag.empty());
227 RTC_DCHECK(!ice_pwd.empty());
228 if (pooled_sessions_.empty()) {
229 return nullptr;
230 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200231
232 IceParameters credentials(ice_ufrag, ice_pwd, false);
233 // If restrict_ice_credentials_change_ is TRUE, then call FindPooledSession
234 // with ice credentials. Otherwise call it with nullptr which means
235 // "find any" pooled session.
236 auto cit = FindPooledSession(restrict_ice_credentials_change_ ? &credentials
237 : nullptr);
238 if (cit == pooled_sessions_.end()) {
239 return nullptr;
240 }
241
242 auto it =
243 pooled_sessions_.begin() + std::distance(pooled_sessions_.cbegin(), cit);
244 std::unique_ptr<PortAllocatorSession> ret = std::move(*it);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700245 ret->SetIceParameters(content_name, component, ice_ufrag, ice_pwd);
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200246 ret->set_pooled(false);
247 // According to JSEP, a pooled session should filter candidates only
248 // after it's taken out of the pool.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700249 ret->SetCandidateFilter(candidate_filter());
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200250 pooled_sessions_.erase(it);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700251 return ret;
252}
253
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200254const PortAllocatorSession* PortAllocator::GetPooledSession(
255 const IceParameters* ice_credentials) const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700256 CheckRunOnValidThreadAndInitialized();
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200257 auto it = FindPooledSession(ice_credentials);
258 if (it == pooled_sessions_.end()) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700259 return nullptr;
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200260 } else {
261 return it->get();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700262 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200263}
264
265std::vector<std::unique_ptr<PortAllocatorSession>>::const_iterator
266PortAllocator::FindPooledSession(const IceParameters* ice_credentials) const {
267 for (auto it = pooled_sessions_.begin(); it != pooled_sessions_.end(); ++it) {
268 if (ice_credentials == nullptr ||
269 ((*it)->ice_ufrag() == ice_credentials->ufrag &&
270 (*it)->ice_pwd() == ice_credentials->pwd)) {
271 return it;
272 }
273 }
274 return pooled_sessions_.end();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275}
276
deadbeef42a42632017-03-10 15:18:00 -0800277void PortAllocator::FreezeCandidatePool() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700278 CheckRunOnValidThreadAndInitialized();
deadbeef42a42632017-03-10 15:18:00 -0800279 candidate_pool_frozen_ = true;
280}
281
282void PortAllocator::DiscardCandidatePool() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700283 CheckRunOnValidThreadIfInitialized();
deadbeef42a42632017-03-10 15:18:00 -0800284 pooled_sessions_.clear();
285}
286
Qingsi Wangc129c352019-04-18 10:41:58 -0700287void PortAllocator::SetCandidateFilter(uint32_t filter) {
288 CheckRunOnValidThreadIfInitialized();
289 if (candidate_filter_ == filter) {
290 return;
291 }
292 uint32_t prev_filter = candidate_filter_;
293 candidate_filter_ = filter;
294 SignalCandidateFilterChanged(prev_filter, filter);
295}
296
Qingsi Wang72a43a12018-02-20 16:03:18 -0800297void PortAllocator::GetCandidateStatsFromPooledSessions(
298 CandidateStatsList* candidate_stats_list) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700299 CheckRunOnValidThreadAndInitialized();
Qingsi Wang72a43a12018-02-20 16:03:18 -0800300 for (const auto& session : pooled_sessions()) {
301 session->GetCandidateStatsFromReadyPorts(candidate_stats_list);
302 }
303}
304
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200305std::vector<IceParameters> PortAllocator::GetPooledIceCredentials() {
306 CheckRunOnValidThreadAndInitialized();
307 std::vector<IceParameters> list;
308 for (const auto& session : pooled_sessions_) {
309 list.push_back(
310 IceParameters(session->ice_ufrag(), session->ice_pwd(), false));
311 }
312 return list;
313}
314
Qingsi Wang7627fdd2019-08-19 16:07:40 -0700315Candidate PortAllocator::SanitizeCandidate(const Candidate& c) const {
316 CheckRunOnValidThreadAndInitialized();
317 // For a local host candidate, we need to conceal its IP address candidate if
318 // the mDNS obfuscation is enabled.
319 bool use_hostname_address =
320 c.type() == LOCAL_PORT_TYPE && MdnsObfuscationEnabled();
321 // If adapter enumeration is disabled or host candidates are disabled,
322 // clear the raddr of STUN candidates to avoid local address leakage.
323 bool filter_stun_related_address =
324 ((flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) &&
325 (flags() & PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE)) ||
326 !(candidate_filter_ & CF_HOST) || MdnsObfuscationEnabled();
327 // If the candidate filter doesn't allow reflexive addresses, empty TURN raddr
328 // to avoid reflexive address leakage.
329 bool filter_turn_related_address = !(candidate_filter_ & CF_REFLEXIVE);
330 bool filter_related_address =
331 ((c.type() == STUN_PORT_TYPE && filter_stun_related_address) ||
332 (c.type() == RELAY_PORT_TYPE && filter_turn_related_address));
333 return c.ToSanitizedCopy(use_hostname_address, filter_related_address);
334}
335
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000336} // namespace cricket