blob: 6ef084ca63a0a9084c8d6e84586ee3e6b9ede0f1 [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
82uint32_t PortAllocatorSession::generation() {
83 return generation_;
84}
85
86void PortAllocatorSession::set_generation(uint32_t generation) {
87 generation_ = generation;
88}
89
90PortAllocator::PortAllocator()
91 : flags_(kDefaultPortAllocatorFlags),
92 min_port_(0),
93 max_port_(0),
94 max_ipv6_networks_(kDefaultMaxIPv6Networks),
95 step_delay_(kDefaultStepDelay),
96 allow_tcp_listen_(true),
97 candidate_filter_(CF_ALL) {}
98
99PortAllocator::~PortAllocator() = default;
100
deadbeef6de92f92016-12-12 18:49:32 -0800101bool PortAllocator::SetConfiguration(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700102 const ServerAddresses& stun_servers,
103 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700104 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200105 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800106 webrtc::TurnCustomizer* turn_customizer,
107 const rtc::Optional<int>& stun_candidate_keepalive_interval) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700108 bool ice_servers_changed =
109 (stun_servers != stun_servers_ || turn_servers != turn_servers_);
110 stun_servers_ = stun_servers;
111 turn_servers_ = turn_servers;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700112 prune_turn_ports_ = prune_turn_ports;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700113
deadbeef42a42632017-03-10 15:18:00 -0800114 if (candidate_pool_frozen_) {
115 if (candidate_pool_size != candidate_pool_size_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_ERROR)
117 << "Trying to change candidate pool size after pool was "
118 << "frozen.";
deadbeef42a42632017-03-10 15:18:00 -0800119 return false;
120 }
121 return true;
deadbeef6de92f92016-12-12 18:49:32 -0800122 }
deadbeef42a42632017-03-10 15:18:00 -0800123
deadbeef6de92f92016-12-12 18:49:32 -0800124 if (candidate_pool_size < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100125 RTC_LOG(LS_ERROR) << "Can't set negative pool size.";
deadbeef6de92f92016-12-12 18:49:32 -0800126 return false;
127 }
deadbeef6de92f92016-12-12 18:49:32 -0800128
deadbeef42a42632017-03-10 15:18:00 -0800129 candidate_pool_size_ = candidate_pool_size;
deadbeef6de92f92016-12-12 18:49:32 -0800130
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700131 // If ICE servers changed, throw away any existing pooled sessions and create
132 // new ones.
133 if (ice_servers_changed) {
134 pooled_sessions_.clear();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700135 }
136
Jonas Orelandbdcee282017-10-10 14:01:40 +0200137 turn_customizer_ = turn_customizer;
138
deadbeef42a42632017-03-10 15:18:00 -0800139 // If |candidate_pool_size_| is less than the number of pooled sessions, get
140 // rid of the extras.
141 while (candidate_pool_size_ < static_cast<int>(pooled_sessions_.size())) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700142 pooled_sessions_.front().reset(nullptr);
143 pooled_sessions_.pop_front();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700144 }
deadbeef6de92f92016-12-12 18:49:32 -0800145
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800146 // |stun_candidate_keepalive_interval_| will be used in STUN port allocation
147 // in future sessions. We also update the ready ports in the pooled sessions.
148 // Ports in sessions that are taken and owned by P2PTransportChannel will be
149 // updated there via IceConfig.
150 stun_candidate_keepalive_interval_ = stun_candidate_keepalive_interval;
151 for (const auto& session : pooled_sessions_) {
152 session->SetStunKeepaliveIntervalForReadyPorts(
153 stun_candidate_keepalive_interval_);
154 }
155
deadbeef42a42632017-03-10 15:18:00 -0800156 // If |candidate_pool_size_| is greater than the number of pooled sessions,
deadbeef6de92f92016-12-12 18:49:32 -0800157 // create new sessions.
deadbeef42a42632017-03-10 15:18:00 -0800158 while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700159 PortAllocatorSession* pooled_session = CreateSessionInternal("", 0, "", "");
160 pooled_session->StartGettingPorts();
161 pooled_sessions_.push_back(
162 std::unique_ptr<PortAllocatorSession>(pooled_session));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700163 }
deadbeef6de92f92016-12-12 18:49:32 -0800164 return true;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700165}
166
167std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000168 const std::string& content_name,
169 int component,
170 const std::string& ice_ufrag,
171 const std::string& ice_pwd) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700172 auto session = std::unique_ptr<PortAllocatorSession>(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700173 CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700174 session->SetCandidateFilter(candidate_filter());
175 return session;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700176}
177
178std::unique_ptr<PortAllocatorSession> PortAllocator::TakePooledSession(
179 const std::string& content_name,
180 int component,
181 const std::string& ice_ufrag,
182 const std::string& ice_pwd) {
183 RTC_DCHECK(!ice_ufrag.empty());
184 RTC_DCHECK(!ice_pwd.empty());
185 if (pooled_sessions_.empty()) {
186 return nullptr;
187 }
188 std::unique_ptr<PortAllocatorSession> ret =
189 std::move(pooled_sessions_.front());
190 ret->SetIceParameters(content_name, component, ice_ufrag, ice_pwd);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700191 // According to JSEP, a pooled session should filter candidates only after
192 // it's taken out of the pool.
193 ret->SetCandidateFilter(candidate_filter());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700194 pooled_sessions_.pop_front();
195 return ret;
196}
197
198const PortAllocatorSession* PortAllocator::GetPooledSession() const {
199 if (pooled_sessions_.empty()) {
200 return nullptr;
201 }
202 return pooled_sessions_.front().get();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203}
204
deadbeef42a42632017-03-10 15:18:00 -0800205void PortAllocator::FreezeCandidatePool() {
206 candidate_pool_frozen_ = true;
207}
208
209void PortAllocator::DiscardCandidatePool() {
210 pooled_sessions_.clear();
211}
212
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213} // namespace cricket