blob: d9383610d629f4d01fb21df612dff8d631e6a4c6 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "p2p/base/portallocator.h"
Steve Anton6c38cc72017-11-29 10:25:58 -080012
13#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017namespace cricket {
18
Steve Anton7995d8c2017-10-30 16:23:38 -070019RelayServerConfig::RelayServerConfig(RelayType type) : type(type) {}
20
21RelayServerConfig::RelayServerConfig(const rtc::SocketAddress& address,
22 const std::string& username,
23 const std::string& password,
24 ProtocolType proto)
25 : type(RELAY_TURN), credentials(username, password) {
26 ports.push_back(ProtocolAddress(address, proto));
27}
28
29RelayServerConfig::RelayServerConfig(const std::string& address,
30 int port,
31 const std::string& username,
32 const std::string& password,
33 ProtocolType proto)
34 : RelayServerConfig(rtc::SocketAddress(address, port),
35 username,
36 password,
37 proto) {}
38
39// Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS.
40RelayServerConfig::RelayServerConfig(const std::string& address,
41 int port,
42 const std::string& username,
43 const std::string& password,
44 ProtocolType proto,
45 bool secure)
46 : RelayServerConfig(address,
47 port,
48 username,
49 password,
50 (proto == PROTO_TCP && secure ? PROTO_TLS : proto)) {}
51
52RelayServerConfig::RelayServerConfig(const RelayServerConfig&) = default;
53
54RelayServerConfig::~RelayServerConfig() = default;
55
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000056PortAllocatorSession::PortAllocatorSession(const std::string& content_name,
57 int component,
58 const std::string& ice_ufrag,
59 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +020060 uint32_t flags)
Taylor Brandstettera1c30352016-05-13 08:15:11 -070061 : flags_(flags),
deadbeefc55fb302016-05-12 12:51:38 -070062 generation_(0),
Taylor Brandstettera1c30352016-05-13 08:15:11 -070063 content_name_(content_name),
64 component_(component),
deadbeefcbecd352015-09-23 11:50:27 -070065 ice_ufrag_(ice_ufrag),
66 ice_pwd_(ice_pwd) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -070067 // Pooled sessions are allowed to be created with empty content name,
68 // component, ufrag and password.
69 RTC_DCHECK(ice_ufrag.empty() == ice_pwd.empty());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000070}
71
Steve Anton7995d8c2017-10-30 16:23:38 -070072PortAllocatorSession::~PortAllocatorSession() = default;
73
74bool PortAllocatorSession::IsCleared() const {
75 return false;
76}
77
78bool PortAllocatorSession::IsStopped() const {
79 return false;
80}
81
Qingsi Wang72a43a12018-02-20 16:03:18 -080082void PortAllocatorSession::GetCandidateStatsFromReadyPorts(
83 CandidateStatsList* candidate_stats_list) const {
84 auto ports = ReadyPorts();
85 for (auto* port : ports) {
86 auto candidates = port->Candidates();
87 for (const auto& candidate : candidates) {
88 CandidateStats candidate_stats(candidate);
89 port->GetStunStats(&candidate_stats.stun_stats);
90 candidate_stats_list->push_back(std::move(candidate_stats));
91 }
92 }
93}
94
Steve Anton7995d8c2017-10-30 16:23:38 -070095uint32_t PortAllocatorSession::generation() {
96 return generation_;
97}
98
99void PortAllocatorSession::set_generation(uint32_t generation) {
100 generation_ = generation;
101}
102
103PortAllocator::PortAllocator()
104 : flags_(kDefaultPortAllocatorFlags),
105 min_port_(0),
106 max_port_(0),
107 max_ipv6_networks_(kDefaultMaxIPv6Networks),
108 step_delay_(kDefaultStepDelay),
109 allow_tcp_listen_(true),
Qingsi Wanga2d60672018-04-11 16:57:45 -0700110 candidate_filter_(CF_ALL) {
111 // The allocator will be attached to a thread in Initialize.
112 thread_checker_.DetachFromThread();
113}
Steve Anton7995d8c2017-10-30 16:23:38 -0700114
Qingsi Wanga2d60672018-04-11 16:57:45 -0700115void PortAllocator::Initialize() {
116 RTC_DCHECK(thread_checker_.CalledOnValidThread());
117 initialized_ = true;
118}
119
120PortAllocator::~PortAllocator() {
121 CheckRunOnValidThreadIfInitialized();
122}
Steve Anton7995d8c2017-10-30 16:23:38 -0700123
deadbeef6de92f92016-12-12 18:49:32 -0800124bool PortAllocator::SetConfiguration(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700125 const ServerAddresses& stun_servers,
126 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700127 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200128 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800129 webrtc::TurnCustomizer* turn_customizer,
130 const rtc::Optional<int>& stun_candidate_keepalive_interval) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700131 CheckRunOnValidThreadIfInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700132 bool ice_servers_changed =
133 (stun_servers != stun_servers_ || turn_servers != turn_servers_);
134 stun_servers_ = stun_servers;
135 turn_servers_ = turn_servers;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700136 prune_turn_ports_ = prune_turn_ports;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700137
deadbeef42a42632017-03-10 15:18:00 -0800138 if (candidate_pool_frozen_) {
139 if (candidate_pool_size != candidate_pool_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100140 RTC_LOG(LS_ERROR)
Jonas Olssond7d762d2018-03-28 09:47:51 +0200141 << "Trying to change candidate pool size after pool was frozen.";
deadbeef42a42632017-03-10 15:18:00 -0800142 return false;
143 }
144 return true;
deadbeef6de92f92016-12-12 18:49:32 -0800145 }
deadbeef42a42632017-03-10 15:18:00 -0800146
deadbeef6de92f92016-12-12 18:49:32 -0800147 if (candidate_pool_size < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100148 RTC_LOG(LS_ERROR) << "Can't set negative pool size.";
deadbeef6de92f92016-12-12 18:49:32 -0800149 return false;
150 }
deadbeef6de92f92016-12-12 18:49:32 -0800151
deadbeef42a42632017-03-10 15:18:00 -0800152 candidate_pool_size_ = candidate_pool_size;
deadbeef6de92f92016-12-12 18:49:32 -0800153
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700154 // If ICE servers changed, throw away any existing pooled sessions and create
155 // new ones.
156 if (ice_servers_changed) {
157 pooled_sessions_.clear();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700158 }
159
Jonas Orelandbdcee282017-10-10 14:01:40 +0200160 turn_customizer_ = turn_customizer;
161
deadbeef42a42632017-03-10 15:18:00 -0800162 // If |candidate_pool_size_| is less than the number of pooled sessions, get
163 // rid of the extras.
164 while (candidate_pool_size_ < static_cast<int>(pooled_sessions_.size())) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700165 pooled_sessions_.front().reset(nullptr);
166 pooled_sessions_.pop_front();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700167 }
deadbeef6de92f92016-12-12 18:49:32 -0800168
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800169 // |stun_candidate_keepalive_interval_| will be used in STUN port allocation
170 // in future sessions. We also update the ready ports in the pooled sessions.
171 // Ports in sessions that are taken and owned by P2PTransportChannel will be
172 // updated there via IceConfig.
173 stun_candidate_keepalive_interval_ = stun_candidate_keepalive_interval;
174 for (const auto& session : pooled_sessions_) {
175 session->SetStunKeepaliveIntervalForReadyPorts(
176 stun_candidate_keepalive_interval_);
177 }
178
deadbeef42a42632017-03-10 15:18:00 -0800179 // If |candidate_pool_size_| is greater than the number of pooled sessions,
deadbeef6de92f92016-12-12 18:49:32 -0800180 // create new sessions.
deadbeef42a42632017-03-10 15:18:00 -0800181 while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700182 PortAllocatorSession* pooled_session = CreateSessionInternal("", 0, "", "");
183 pooled_session->StartGettingPorts();
184 pooled_sessions_.push_back(
185 std::unique_ptr<PortAllocatorSession>(pooled_session));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700186 }
deadbeef6de92f92016-12-12 18:49:32 -0800187 return true;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700188}
189
190std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 const std::string& content_name,
192 int component,
193 const std::string& ice_ufrag,
194 const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700195 CheckRunOnValidThreadAndInitialized();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700196 auto session = std::unique_ptr<PortAllocatorSession>(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700197 CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700198 session->SetCandidateFilter(candidate_filter());
199 return session;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700200}
201
202std::unique_ptr<PortAllocatorSession> PortAllocator::TakePooledSession(
203 const std::string& content_name,
204 int component,
205 const std::string& ice_ufrag,
206 const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700207 CheckRunOnValidThreadAndInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700208 RTC_DCHECK(!ice_ufrag.empty());
209 RTC_DCHECK(!ice_pwd.empty());
210 if (pooled_sessions_.empty()) {
211 return nullptr;
212 }
213 std::unique_ptr<PortAllocatorSession> ret =
214 std::move(pooled_sessions_.front());
215 ret->SetIceParameters(content_name, component, ice_ufrag, ice_pwd);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700216 // According to JSEP, a pooled session should filter candidates only after
217 // it's taken out of the pool.
218 ret->SetCandidateFilter(candidate_filter());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700219 pooled_sessions_.pop_front();
220 return ret;
221}
222
223const PortAllocatorSession* PortAllocator::GetPooledSession() const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700224 CheckRunOnValidThreadAndInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700225 if (pooled_sessions_.empty()) {
226 return nullptr;
227 }
228 return pooled_sessions_.front().get();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229}
230
deadbeef42a42632017-03-10 15:18:00 -0800231void PortAllocator::FreezeCandidatePool() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700232 CheckRunOnValidThreadAndInitialized();
deadbeef42a42632017-03-10 15:18:00 -0800233 candidate_pool_frozen_ = true;
234}
235
236void PortAllocator::DiscardCandidatePool() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700237 CheckRunOnValidThreadIfInitialized();
deadbeef42a42632017-03-10 15:18:00 -0800238 pooled_sessions_.clear();
239}
240
Qingsi Wang72a43a12018-02-20 16:03:18 -0800241void PortAllocator::GetCandidateStatsFromPooledSessions(
242 CandidateStatsList* candidate_stats_list) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700243 CheckRunOnValidThreadAndInitialized();
Qingsi Wang72a43a12018-02-20 16:03:18 -0800244 for (const auto& session : pooled_sessions()) {
245 session->GetCandidateStatsFromReadyPorts(candidate_stats_list);
246 }
247}
248
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249} // namespace cricket