blob: b13896c4bc02f131d6794bb5c563c9f8967bf0d1 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:111/*
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 17:11:0011#include "p2p/base/port_allocator.h"
Steve Anton6c38cc72017-11-29 18:25:5812
Jonas Oreland1cd39fa2018-10-11 05:47:1213#include <iterator>
Yves Gerey3e707812018-11-28 15:47:4914#include <set>
Steve Anton6c38cc72017-11-29 18:25:5815#include <utility>
16
Steve Anton10542f22019-01-11 17:11:0017#include "p2p/base/ice_credentials_iterator.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 15:47:4919#include "rtc_base/logging.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:1120
henrike@webrtc.org269fb4b2014-10-28 22:20:1121namespace cricket {
22
Niels Möller191e38f2019-11-04 07:49:1223RelayServerConfig::RelayServerConfig() {}
Steve Anton7995d8c2017-10-30 23:23:3824
25RelayServerConfig::RelayServerConfig(const rtc::SocketAddress& address,
26 const std::string& username,
27 const std::string& password,
28 ProtocolType proto)
Niels Möller191e38f2019-11-04 07:49:1229 : credentials(username, password) {
Steve Anton7995d8c2017-10-30 23:23:3830 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:1160PortAllocatorSession::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 10:23:2164 uint32_t flags)
Taylor Brandstettera1c30352016-05-13 15:15:1165 : flags_(flags),
deadbeefc55fb302016-05-12 19:51:3866 generation_(0),
Taylor Brandstettera1c30352016-05-13 15:15:1167 content_name_(content_name),
68 component_(component),
deadbeefcbecd352015-09-23 18:50:2769 ice_ufrag_(ice_ufrag),
70 ice_pwd_(ice_pwd) {
Taylor Brandstettera1c30352016-05-13 15:15:1171 // 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:1174}
75
Steve Anton7995d8c2017-10-30 23:23:3876PortAllocatorSession::~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 23:57:45101 candidate_filter_(CF_ALL) {
102 // The allocator will be attached to a thread in Initialize.
Sebastian Janssonc01367d2019-04-08 13:20:44103 thread_checker_.Detach();
Qingsi Wanga2d60672018-04-11 23:57:45104}
Steve Anton7995d8c2017-10-30 23:23:38105
Qingsi Wanga2d60672018-04-11 23:57:45106void PortAllocator::Initialize() {
Sebastian Janssonc01367d2019-04-08 13:20:44107 RTC_DCHECK(thread_checker_.IsCurrent());
Qingsi Wanga2d60672018-04-11 23:57:45108 initialized_ = true;
109}
110
111PortAllocator::~PortAllocator() {
112 CheckRunOnValidThreadIfInitialized();
113}
Steve Anton7995d8c2017-10-30 23:23:38114
Jonas Oreland1cd39fa2018-10-11 05:47:12115void PortAllocator::set_restrict_ice_credentials_change(bool value) {
116 restrict_ice_credentials_change_ = value;
117}
118
Honghai Zhangf8998cf2019-10-14 18:27:50119// Deprecated
deadbeef6de92f92016-12-13 02:49:32120bool PortAllocator::SetConfiguration(
Taylor Brandstettera1c30352016-05-13 15:15:11121 const ServerAddresses& stun_servers,
122 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-07-01 03:52:02123 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 12:01:40124 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 22:45:49125 webrtc::TurnCustomizer* turn_customizer,
Danil Chapovalov00c718362018-06-15 13:58:38126 const absl::optional<int>& stun_candidate_keepalive_interval) {
Honghai Zhangf8998cf2019-10-14 18:27:50127 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 23:57:45141 CheckRunOnValidThreadIfInitialized();
Qingsi Wange6ded162018-10-02 23:00:41142 // 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 13:20:44145 RTC_DCHECK(candidate_pool_size == 0 || thread_checker_.IsCurrent());
Taylor Brandstettera1c30352016-05-13 15:15:11146 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 18:27:50150 turn_port_prune_policy_ = turn_port_prune_policy;
Taylor Brandstettera1c30352016-05-13 15:15:11151
deadbeef42a42632017-03-10 23:18:00152 if (candidate_pool_frozen_) {
153 if (candidate_pool_size != candidate_pool_size_) {
Mirko Bonadei675513b2017-11-09 10:09:25154 RTC_LOG(LS_ERROR)
Jonas Olssond7d762d2018-03-28 07:47:51155 << "Trying to change candidate pool size after pool was frozen.";
deadbeef42a42632017-03-10 23:18:00156 return false;
157 }
158 return true;
deadbeef6de92f92016-12-13 02:49:32159 }
deadbeef42a42632017-03-10 23:18:00160
deadbeef6de92f92016-12-13 02:49:32161 if (candidate_pool_size < 0) {
Mirko Bonadei675513b2017-11-09 10:09:25162 RTC_LOG(LS_ERROR) << "Can't set negative pool size.";
deadbeef6de92f92016-12-13 02:49:32163 return false;
164 }
deadbeef6de92f92016-12-13 02:49:32165
deadbeef42a42632017-03-10 23:18:00166 candidate_pool_size_ = candidate_pool_size;
deadbeef6de92f92016-12-13 02:49:32167
Taylor Brandstettera1c30352016-05-13 15:15:11168 // 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 15:15:11172 }
173
Jonas Orelandbdcee282017-10-10 12:01:40174 turn_customizer_ = turn_customizer;
175
deadbeef42a42632017-03-10 23:18:00176 // 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 05:47:12179 pooled_sessions_.back().reset(nullptr);
180 pooled_sessions_.pop_back();
Taylor Brandstettera1c30352016-05-13 15:15:11181 }
deadbeef6de92f92016-12-13 02:49:32182
Qingsi Wangdb53f8e2018-02-20 22:45:49183 // |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 23:18:00193 // If |candidate_pool_size_| is greater than the number of pooled sessions,
deadbeef6de92f92016-12-13 02:49:32194 // create new sessions.
deadbeef42a42632017-03-10 23:18:00195 while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) {
Jonas Oreland1cd39fa2018-10-11 05:47:12196 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 15:15:11201 pooled_session->StartGettingPorts();
202 pooled_sessions_.push_back(
203 std::unique_ptr<PortAllocatorSession>(pooled_session));
Taylor Brandstettera1c30352016-05-13 15:15:11204 }
deadbeef6de92f92016-12-13 02:49:32205 return true;
Taylor Brandstettera1c30352016-05-13 15:15:11206}
207
208std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11209 const std::string& content_name,
210 int component,
211 const std::string& ice_ufrag,
212 const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 23:57:45213 CheckRunOnValidThreadAndInitialized();
Taylor Brandstetter417eebe2016-05-23 23:02:19214 auto session = std::unique_ptr<PortAllocatorSession>(
Taylor Brandstettera1c30352016-05-13 15:15:11215 CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd));
Taylor Brandstetter417eebe2016-05-23 23:02:19216 session->SetCandidateFilter(candidate_filter());
217 return session;
Taylor Brandstettera1c30352016-05-13 15:15:11218}
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 23:57:45225 CheckRunOnValidThreadAndInitialized();
Taylor Brandstettera1c30352016-05-13 15:15:11226 RTC_DCHECK(!ice_ufrag.empty());
227 RTC_DCHECK(!ice_pwd.empty());
228 if (pooled_sessions_.empty()) {
229 return nullptr;
230 }
Jonas Oreland1cd39fa2018-10-11 05:47:12231
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 15:15:11245 ret->SetIceParameters(content_name, component, ice_ufrag, ice_pwd);
Jonas Oreland1cd39fa2018-10-11 05:47:12246 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 23:02:19249 ret->SetCandidateFilter(candidate_filter());
Jonas Oreland1cd39fa2018-10-11 05:47:12250 pooled_sessions_.erase(it);
Taylor Brandstettera1c30352016-05-13 15:15:11251 return ret;
252}
253
Jonas Oreland1cd39fa2018-10-11 05:47:12254const PortAllocatorSession* PortAllocator::GetPooledSession(
255 const IceParameters* ice_credentials) const {
Qingsi Wanga2d60672018-04-11 23:57:45256 CheckRunOnValidThreadAndInitialized();
Jonas Oreland1cd39fa2018-10-11 05:47:12257 auto it = FindPooledSession(ice_credentials);
258 if (it == pooled_sessions_.end()) {
Taylor Brandstettera1c30352016-05-13 15:15:11259 return nullptr;
Jonas Oreland1cd39fa2018-10-11 05:47:12260 } else {
261 return it->get();
Taylor Brandstettera1c30352016-05-13 15:15:11262 }
Jonas Oreland1cd39fa2018-10-11 05:47:12263}
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:11275}
276
deadbeef42a42632017-03-10 23:18:00277void PortAllocator::FreezeCandidatePool() {
Qingsi Wanga2d60672018-04-11 23:57:45278 CheckRunOnValidThreadAndInitialized();
deadbeef42a42632017-03-10 23:18:00279 candidate_pool_frozen_ = true;
280}
281
282void PortAllocator::DiscardCandidatePool() {
Qingsi Wanga2d60672018-04-11 23:57:45283 CheckRunOnValidThreadIfInitialized();
deadbeef42a42632017-03-10 23:18:00284 pooled_sessions_.clear();
285}
286
Qingsi Wangc129c352019-04-18 17:41:58287void 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-21 00:03:18297void PortAllocator::GetCandidateStatsFromPooledSessions(
298 CandidateStatsList* candidate_stats_list) {
Qingsi Wanga2d60672018-04-11 23:57:45299 CheckRunOnValidThreadAndInitialized();
Qingsi Wang72a43a12018-02-21 00:03:18300 for (const auto& session : pooled_sessions()) {
301 session->GetCandidateStatsFromReadyPorts(candidate_stats_list);
302 }
303}
304
Jonas Oreland1cd39fa2018-10-11 05:47:12305std::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 23:07:40315Candidate 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:11336} // namespace cricket