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 | |
| 82 | uint32_t PortAllocatorSession::generation() { |
| 83 | return generation_; |
| 84 | } |
| 85 | |
| 86 | void PortAllocatorSession::set_generation(uint32_t generation) { |
| 87 | generation_ = generation; |
| 88 | } |
| 89 | |
| 90 | PortAllocator::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 | |
| 99 | PortAllocator::~PortAllocator() = default; |
| 100 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 101 | bool PortAllocator::SetConfiguration( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 102 | const ServerAddresses& stun_servers, |
| 103 | const std::vector<RelayServerConfig>& turn_servers, |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 104 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 105 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 106 | webrtc::TurnCustomizer* turn_customizer, |
| 107 | const rtc::Optional<int>& stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 108 | bool ice_servers_changed = |
| 109 | (stun_servers != stun_servers_ || turn_servers != turn_servers_); |
| 110 | stun_servers_ = stun_servers; |
| 111 | turn_servers_ = turn_servers; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 112 | prune_turn_ports_ = prune_turn_ports; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 113 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 114 | if (candidate_pool_frozen_) { |
| 115 | if (candidate_pool_size != candidate_pool_size_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 116 | RTC_LOG(LS_ERROR) |
| 117 | << "Trying to change candidate pool size after pool was " |
| 118 | << "frozen."; |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | return true; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 122 | } |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 123 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 124 | if (candidate_pool_size < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 125 | RTC_LOG(LS_ERROR) << "Can't set negative pool size."; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 126 | return false; |
| 127 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 128 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 129 | candidate_pool_size_ = candidate_pool_size; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 130 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 131 | // 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 Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 137 | turn_customizer_ = turn_customizer; |
| 138 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 139 | // 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 Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 142 | pooled_sessions_.front().reset(nullptr); |
| 143 | pooled_sessions_.pop_front(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 144 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 145 | |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 146 | // |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 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 156 | // If |candidate_pool_size_| is greater than the number of pooled sessions, |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 157 | // create new sessions. |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 158 | while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 159 | PortAllocatorSession* pooled_session = CreateSessionInternal("", 0, "", ""); |
| 160 | pooled_session->StartGettingPorts(); |
| 161 | pooled_sessions_.push_back( |
| 162 | std::unique_ptr<PortAllocatorSession>(pooled_session)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 163 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 164 | return true; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 168 | const std::string& content_name, |
| 169 | int component, |
| 170 | const std::string& ice_ufrag, |
| 171 | const std::string& ice_pwd) { |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 172 | auto session = std::unique_ptr<PortAllocatorSession>( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 173 | CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd)); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 174 | session->SetCandidateFilter(candidate_filter()); |
| 175 | return session; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | std::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 Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 191 | // 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 Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 194 | pooled_sessions_.pop_front(); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | const PortAllocatorSession* PortAllocator::GetPooledSession() const { |
| 199 | if (pooled_sessions_.empty()) { |
| 200 | return nullptr; |
| 201 | } |
| 202 | return pooled_sessions_.front().get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 203 | } |
| 204 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 205 | void PortAllocator::FreezeCandidatePool() { |
| 206 | candidate_pool_frozen_ = true; |
| 207 | } |
| 208 | |
| 209 | void PortAllocator::DiscardCandidatePool() { |
| 210 | pooled_sessions_.clear(); |
| 211 | } |
| 212 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 213 | } // namespace cricket |