henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "p2p/base/portallocator.h" |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 12 | |
| 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 17 | namespace cricket { |
| 18 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 19 | RelayServerConfig::RelayServerConfig(RelayType type) : type(type) {} |
| 20 | |
| 21 | RelayServerConfig::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 | |
| 29 | RelayServerConfig::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. |
| 40 | RelayServerConfig::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 | |
| 52 | RelayServerConfig::RelayServerConfig(const RelayServerConfig&) = default; |
| 53 | |
| 54 | RelayServerConfig::~RelayServerConfig() = default; |
| 55 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 56 | PortAllocatorSession::PortAllocatorSession(const std::string& content_name, |
| 57 | int component, |
| 58 | const std::string& ice_ufrag, |
| 59 | const std::string& ice_pwd, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 60 | uint32_t flags) |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 61 | : flags_(flags), |
deadbeef | c55fb30 | 2016-05-12 12:51:38 -0700 | [diff] [blame] | 62 | generation_(0), |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 63 | content_name_(content_name), |
| 64 | component_(component), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 65 | ice_ufrag_(ice_ufrag), |
| 66 | ice_pwd_(ice_pwd) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 67 | // 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.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 72 | PortAllocatorSession::~PortAllocatorSession() = default; |
| 73 | |
| 74 | bool PortAllocatorSession::IsCleared() const { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | bool PortAllocatorSession::IsStopped() const { |
| 79 | return false; |
| 80 | } |
| 81 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 82 | void 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 Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 95 | uint32_t PortAllocatorSession::generation() { |
| 96 | return generation_; |
| 97 | } |
| 98 | |
| 99 | void PortAllocatorSession::set_generation(uint32_t generation) { |
| 100 | generation_ = generation; |
| 101 | } |
| 102 | |
| 103 | PortAllocator::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), |
| 110 | candidate_filter_(CF_ALL) {} |
| 111 | |
| 112 | PortAllocator::~PortAllocator() = default; |
| 113 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 114 | bool PortAllocator::SetConfiguration( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 115 | const ServerAddresses& stun_servers, |
| 116 | const std::vector<RelayServerConfig>& turn_servers, |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 117 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 118 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 119 | webrtc::TurnCustomizer* turn_customizer, |
| 120 | const rtc::Optional<int>& stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 121 | bool ice_servers_changed = |
| 122 | (stun_servers != stun_servers_ || turn_servers != turn_servers_); |
| 123 | stun_servers_ = stun_servers; |
| 124 | turn_servers_ = turn_servers; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 125 | prune_turn_ports_ = prune_turn_ports; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 126 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 127 | if (candidate_pool_frozen_) { |
| 128 | if (candidate_pool_size != candidate_pool_size_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 129 | RTC_LOG(LS_ERROR) |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame^] | 130 | << "Trying to change candidate pool size after pool was frozen."; |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | return true; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 134 | } |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 135 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 136 | if (candidate_pool_size < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 137 | RTC_LOG(LS_ERROR) << "Can't set negative pool size."; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 138 | return false; |
| 139 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 140 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 141 | candidate_pool_size_ = candidate_pool_size; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 142 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 143 | // If ICE servers changed, throw away any existing pooled sessions and create |
| 144 | // new ones. |
| 145 | if (ice_servers_changed) { |
| 146 | pooled_sessions_.clear(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 149 | turn_customizer_ = turn_customizer; |
| 150 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 151 | // If |candidate_pool_size_| is less than the number of pooled sessions, get |
| 152 | // rid of the extras. |
| 153 | while (candidate_pool_size_ < static_cast<int>(pooled_sessions_.size())) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 154 | pooled_sessions_.front().reset(nullptr); |
| 155 | pooled_sessions_.pop_front(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 156 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 157 | |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 158 | // |stun_candidate_keepalive_interval_| will be used in STUN port allocation |
| 159 | // in future sessions. We also update the ready ports in the pooled sessions. |
| 160 | // Ports in sessions that are taken and owned by P2PTransportChannel will be |
| 161 | // updated there via IceConfig. |
| 162 | stun_candidate_keepalive_interval_ = stun_candidate_keepalive_interval; |
| 163 | for (const auto& session : pooled_sessions_) { |
| 164 | session->SetStunKeepaliveIntervalForReadyPorts( |
| 165 | stun_candidate_keepalive_interval_); |
| 166 | } |
| 167 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 168 | // If |candidate_pool_size_| is greater than the number of pooled sessions, |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 169 | // create new sessions. |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 170 | while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 171 | PortAllocatorSession* pooled_session = CreateSessionInternal("", 0, "", ""); |
| 172 | pooled_session->StartGettingPorts(); |
| 173 | pooled_sessions_.push_back( |
| 174 | std::unique_ptr<PortAllocatorSession>(pooled_session)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 175 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 176 | return true; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 180 | const std::string& content_name, |
| 181 | int component, |
| 182 | const std::string& ice_ufrag, |
| 183 | const std::string& ice_pwd) { |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 184 | auto session = std::unique_ptr<PortAllocatorSession>( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 185 | CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd)); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 186 | session->SetCandidateFilter(candidate_filter()); |
| 187 | return session; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | std::unique_ptr<PortAllocatorSession> PortAllocator::TakePooledSession( |
| 191 | const std::string& content_name, |
| 192 | int component, |
| 193 | const std::string& ice_ufrag, |
| 194 | const std::string& ice_pwd) { |
| 195 | RTC_DCHECK(!ice_ufrag.empty()); |
| 196 | RTC_DCHECK(!ice_pwd.empty()); |
| 197 | if (pooled_sessions_.empty()) { |
| 198 | return nullptr; |
| 199 | } |
| 200 | std::unique_ptr<PortAllocatorSession> ret = |
| 201 | std::move(pooled_sessions_.front()); |
| 202 | ret->SetIceParameters(content_name, component, ice_ufrag, ice_pwd); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 203 | // According to JSEP, a pooled session should filter candidates only after |
| 204 | // it's taken out of the pool. |
| 205 | ret->SetCandidateFilter(candidate_filter()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 206 | pooled_sessions_.pop_front(); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | const PortAllocatorSession* PortAllocator::GetPooledSession() const { |
| 211 | if (pooled_sessions_.empty()) { |
| 212 | return nullptr; |
| 213 | } |
| 214 | return pooled_sessions_.front().get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 215 | } |
| 216 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 217 | void PortAllocator::FreezeCandidatePool() { |
| 218 | candidate_pool_frozen_ = true; |
| 219 | } |
| 220 | |
| 221 | void PortAllocator::DiscardCandidatePool() { |
| 222 | pooled_sessions_.clear(); |
| 223 | } |
| 224 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 225 | void PortAllocator::GetCandidateStatsFromPooledSessions( |
| 226 | CandidateStatsList* candidate_stats_list) { |
| 227 | for (const auto& session : pooled_sessions()) { |
| 228 | session->GetCandidateStatsFromReadyPorts(candidate_stats_list); |
| 229 | } |
| 230 | } |
| 231 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 232 | } // namespace cricket |