blob: a9d7cb6493c2c0e0229641c729781a5409381ff3 [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
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
deadbeef6de92f92016-12-12 18:49:32 -0800119bool PortAllocator::SetConfiguration(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700120 const ServerAddresses& stun_servers,
121 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700122 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200123 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800124 webrtc::TurnCustomizer* turn_customizer,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200125 const absl::optional<int>& stun_candidate_keepalive_interval) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700126 CheckRunOnValidThreadIfInitialized();
Qingsi Wange6ded162018-10-02 16:00:41 -0700127 // A positive candidate pool size would lead to the creation of a pooled
128 // allocator session and starting getting ports, which we should only do on
129 // the network thread.
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200130 RTC_DCHECK(candidate_pool_size == 0 || thread_checker_.IsCurrent());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700131 bool ice_servers_changed =
132 (stun_servers != stun_servers_ || turn_servers != turn_servers_);
133 stun_servers_ = stun_servers;
134 turn_servers_ = turn_servers;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700135 prune_turn_ports_ = prune_turn_ports;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700136
deadbeef42a42632017-03-10 15:18:00 -0800137 if (candidate_pool_frozen_) {
138 if (candidate_pool_size != candidate_pool_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100139 RTC_LOG(LS_ERROR)
Jonas Olssond7d762d2018-03-28 09:47:51 +0200140 << "Trying to change candidate pool size after pool was frozen.";
deadbeef42a42632017-03-10 15:18:00 -0800141 return false;
142 }
143 return true;
deadbeef6de92f92016-12-12 18:49:32 -0800144 }
deadbeef42a42632017-03-10 15:18:00 -0800145
deadbeef6de92f92016-12-12 18:49:32 -0800146 if (candidate_pool_size < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100147 RTC_LOG(LS_ERROR) << "Can't set negative pool size.";
deadbeef6de92f92016-12-12 18:49:32 -0800148 return false;
149 }
deadbeef6de92f92016-12-12 18:49:32 -0800150
deadbeef42a42632017-03-10 15:18:00 -0800151 candidate_pool_size_ = candidate_pool_size;
deadbeef6de92f92016-12-12 18:49:32 -0800152
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700153 // If ICE servers changed, throw away any existing pooled sessions and create
154 // new ones.
155 if (ice_servers_changed) {
156 pooled_sessions_.clear();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700157 }
158
Jonas Orelandbdcee282017-10-10 14:01:40 +0200159 turn_customizer_ = turn_customizer;
160
deadbeef42a42632017-03-10 15:18:00 -0800161 // If |candidate_pool_size_| is less than the number of pooled sessions, get
162 // rid of the extras.
163 while (candidate_pool_size_ < static_cast<int>(pooled_sessions_.size())) {
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200164 pooled_sessions_.back().reset(nullptr);
165 pooled_sessions_.pop_back();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700166 }
deadbeef6de92f92016-12-12 18:49:32 -0800167
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800168 // |stun_candidate_keepalive_interval_| will be used in STUN port allocation
169 // in future sessions. We also update the ready ports in the pooled sessions.
170 // Ports in sessions that are taken and owned by P2PTransportChannel will be
171 // updated there via IceConfig.
172 stun_candidate_keepalive_interval_ = stun_candidate_keepalive_interval;
173 for (const auto& session : pooled_sessions_) {
174 session->SetStunKeepaliveIntervalForReadyPorts(
175 stun_candidate_keepalive_interval_);
176 }
177
deadbeef42a42632017-03-10 15:18:00 -0800178 // If |candidate_pool_size_| is greater than the number of pooled sessions,
deadbeef6de92f92016-12-12 18:49:32 -0800179 // create new sessions.
deadbeef42a42632017-03-10 15:18:00 -0800180 while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) {
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200181 IceParameters iceCredentials =
182 IceCredentialsIterator::CreateRandomIceCredentials();
183 PortAllocatorSession* pooled_session =
184 CreateSessionInternal("", 0, iceCredentials.ufrag, iceCredentials.pwd);
185 pooled_session->set_pooled(true);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700186 pooled_session->StartGettingPorts();
187 pooled_sessions_.push_back(
188 std::unique_ptr<PortAllocatorSession>(pooled_session));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700189 }
deadbeef6de92f92016-12-12 18:49:32 -0800190 return true;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700191}
192
193std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194 const std::string& content_name,
195 int component,
196 const std::string& ice_ufrag,
197 const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700198 CheckRunOnValidThreadAndInitialized();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700199 auto session = std::unique_ptr<PortAllocatorSession>(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700200 CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700201 session->SetCandidateFilter(candidate_filter());
202 return session;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700203}
204
205std::unique_ptr<PortAllocatorSession> PortAllocator::TakePooledSession(
206 const std::string& content_name,
207 int component,
208 const std::string& ice_ufrag,
209 const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700210 CheckRunOnValidThreadAndInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700211 RTC_DCHECK(!ice_ufrag.empty());
212 RTC_DCHECK(!ice_pwd.empty());
213 if (pooled_sessions_.empty()) {
214 return nullptr;
215 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200216
217 IceParameters credentials(ice_ufrag, ice_pwd, false);
218 // If restrict_ice_credentials_change_ is TRUE, then call FindPooledSession
219 // with ice credentials. Otherwise call it with nullptr which means
220 // "find any" pooled session.
221 auto cit = FindPooledSession(restrict_ice_credentials_change_ ? &credentials
222 : nullptr);
223 if (cit == pooled_sessions_.end()) {
224 return nullptr;
225 }
226
227 auto it =
228 pooled_sessions_.begin() + std::distance(pooled_sessions_.cbegin(), cit);
229 std::unique_ptr<PortAllocatorSession> ret = std::move(*it);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700230 ret->SetIceParameters(content_name, component, ice_ufrag, ice_pwd);
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200231 ret->set_pooled(false);
232 // According to JSEP, a pooled session should filter candidates only
233 // after it's taken out of the pool.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700234 ret->SetCandidateFilter(candidate_filter());
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200235 pooled_sessions_.erase(it);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700236 return ret;
237}
238
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200239const PortAllocatorSession* PortAllocator::GetPooledSession(
240 const IceParameters* ice_credentials) const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700241 CheckRunOnValidThreadAndInitialized();
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200242 auto it = FindPooledSession(ice_credentials);
243 if (it == pooled_sessions_.end()) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700244 return nullptr;
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200245 } else {
246 return it->get();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700247 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200248}
249
250std::vector<std::unique_ptr<PortAllocatorSession>>::const_iterator
251PortAllocator::FindPooledSession(const IceParameters* ice_credentials) const {
252 for (auto it = pooled_sessions_.begin(); it != pooled_sessions_.end(); ++it) {
253 if (ice_credentials == nullptr ||
254 ((*it)->ice_ufrag() == ice_credentials->ufrag &&
255 (*it)->ice_pwd() == ice_credentials->pwd)) {
256 return it;
257 }
258 }
259 return pooled_sessions_.end();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000260}
261
deadbeef42a42632017-03-10 15:18:00 -0800262void PortAllocator::FreezeCandidatePool() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700263 CheckRunOnValidThreadAndInitialized();
deadbeef42a42632017-03-10 15:18:00 -0800264 candidate_pool_frozen_ = true;
265}
266
267void PortAllocator::DiscardCandidatePool() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700268 CheckRunOnValidThreadIfInitialized();
deadbeef42a42632017-03-10 15:18:00 -0800269 pooled_sessions_.clear();
270}
271
Qingsi Wangc129c352019-04-18 10:41:58 -0700272void PortAllocator::SetCandidateFilter(uint32_t filter) {
273 CheckRunOnValidThreadIfInitialized();
274 if (candidate_filter_ == filter) {
275 return;
276 }
277 uint32_t prev_filter = candidate_filter_;
278 candidate_filter_ = filter;
279 SignalCandidateFilterChanged(prev_filter, filter);
280}
281
Qingsi Wang72a43a12018-02-20 16:03:18 -0800282void PortAllocator::GetCandidateStatsFromPooledSessions(
283 CandidateStatsList* candidate_stats_list) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700284 CheckRunOnValidThreadAndInitialized();
Qingsi Wang72a43a12018-02-20 16:03:18 -0800285 for (const auto& session : pooled_sessions()) {
286 session->GetCandidateStatsFromReadyPorts(candidate_stats_list);
287 }
288}
289
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200290std::vector<IceParameters> PortAllocator::GetPooledIceCredentials() {
291 CheckRunOnValidThreadAndInitialized();
292 std::vector<IceParameters> list;
293 for (const auto& session : pooled_sessions_) {
294 list.push_back(
295 IceParameters(session->ice_ufrag(), session->ice_pwd(), false));
296 }
297 return list;
298}
299
Qingsi Wang7627fdd2019-08-19 16:07:40 -0700300Candidate PortAllocator::SanitizeCandidate(const Candidate& c) const {
301 CheckRunOnValidThreadAndInitialized();
302 // For a local host candidate, we need to conceal its IP address candidate if
303 // the mDNS obfuscation is enabled.
304 bool use_hostname_address =
305 c.type() == LOCAL_PORT_TYPE && MdnsObfuscationEnabled();
306 // If adapter enumeration is disabled or host candidates are disabled,
307 // clear the raddr of STUN candidates to avoid local address leakage.
308 bool filter_stun_related_address =
309 ((flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) &&
310 (flags() & PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE)) ||
311 !(candidate_filter_ & CF_HOST) || MdnsObfuscationEnabled();
312 // If the candidate filter doesn't allow reflexive addresses, empty TURN raddr
313 // to avoid reflexive address leakage.
314 bool filter_turn_related_address = !(candidate_filter_ & CF_REFLEXIVE);
315 bool filter_related_address =
316 ((c.type() == STUN_PORT_TYPE && filter_stun_related_address) ||
317 (c.type() == RELAY_PORT_TYPE && filter_turn_related_address));
318 return c.ToSanitizedCopy(use_hostname_address, filter_related_address);
319}
320
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000321} // namespace cricket