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" |
| 12 | #include "rtc_base/checks.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 14 | namespace cricket { |
| 15 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 16 | RelayServerConfig::RelayServerConfig(RelayType type) : type(type) {} |
| 17 | |
| 18 | RelayServerConfig::RelayServerConfig(const rtc::SocketAddress& address, |
| 19 | const std::string& username, |
| 20 | const std::string& password, |
| 21 | ProtocolType proto) |
| 22 | : type(RELAY_TURN), credentials(username, password) { |
| 23 | ports.push_back(ProtocolAddress(address, proto)); |
| 24 | } |
| 25 | |
| 26 | RelayServerConfig::RelayServerConfig(const std::string& address, |
| 27 | int port, |
| 28 | const std::string& username, |
| 29 | const std::string& password, |
| 30 | ProtocolType proto) |
| 31 | : RelayServerConfig(rtc::SocketAddress(address, port), |
| 32 | username, |
| 33 | password, |
| 34 | proto) {} |
| 35 | |
| 36 | // Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS. |
| 37 | RelayServerConfig::RelayServerConfig(const std::string& address, |
| 38 | int port, |
| 39 | const std::string& username, |
| 40 | const std::string& password, |
| 41 | ProtocolType proto, |
| 42 | bool secure) |
| 43 | : RelayServerConfig(address, |
| 44 | port, |
| 45 | username, |
| 46 | password, |
| 47 | (proto == PROTO_TCP && secure ? PROTO_TLS : proto)) {} |
| 48 | |
| 49 | RelayServerConfig::RelayServerConfig(const RelayServerConfig&) = default; |
| 50 | |
| 51 | RelayServerConfig::~RelayServerConfig() = default; |
| 52 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 53 | PortAllocatorSession::PortAllocatorSession(const std::string& content_name, |
| 54 | int component, |
| 55 | const std::string& ice_ufrag, |
| 56 | const std::string& ice_pwd, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 57 | uint32_t flags) |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 58 | : flags_(flags), |
deadbeef | c55fb30 | 2016-05-12 12:51:38 -0700 | [diff] [blame] | 59 | generation_(0), |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 60 | content_name_(content_name), |
| 61 | component_(component), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 62 | ice_ufrag_(ice_ufrag), |
| 63 | ice_pwd_(ice_pwd) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 64 | // Pooled sessions are allowed to be created with empty content name, |
| 65 | // component, ufrag and password. |
| 66 | RTC_DCHECK(ice_ufrag.empty() == ice_pwd.empty()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 69 | PortAllocatorSession::~PortAllocatorSession() = default; |
| 70 | |
| 71 | bool PortAllocatorSession::IsCleared() const { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | bool PortAllocatorSession::IsStopped() const { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | uint32_t PortAllocatorSession::generation() { |
| 80 | return generation_; |
| 81 | } |
| 82 | |
| 83 | void PortAllocatorSession::set_generation(uint32_t generation) { |
| 84 | generation_ = generation; |
| 85 | } |
| 86 | |
| 87 | PortAllocator::PortAllocator() |
| 88 | : flags_(kDefaultPortAllocatorFlags), |
| 89 | min_port_(0), |
| 90 | max_port_(0), |
| 91 | max_ipv6_networks_(kDefaultMaxIPv6Networks), |
| 92 | step_delay_(kDefaultStepDelay), |
| 93 | allow_tcp_listen_(true), |
| 94 | candidate_filter_(CF_ALL) {} |
| 95 | |
| 96 | PortAllocator::~PortAllocator() = default; |
| 97 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 98 | bool PortAllocator::SetConfiguration( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 99 | const ServerAddresses& stun_servers, |
| 100 | const std::vector<RelayServerConfig>& turn_servers, |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 101 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 102 | bool prune_turn_ports, |
| 103 | webrtc::TurnCustomizer* turn_customizer) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 104 | bool ice_servers_changed = |
| 105 | (stun_servers != stun_servers_ || turn_servers != turn_servers_); |
| 106 | stun_servers_ = stun_servers; |
| 107 | turn_servers_ = turn_servers; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 108 | prune_turn_ports_ = prune_turn_ports; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 109 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 110 | if (candidate_pool_frozen_) { |
| 111 | if (candidate_pool_size != candidate_pool_size_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 112 | RTC_LOG(LS_ERROR) |
| 113 | << "Trying to change candidate pool size after pool was " |
| 114 | << "frozen."; |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | return true; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 118 | } |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 119 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 120 | if (candidate_pool_size < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 121 | RTC_LOG(LS_ERROR) << "Can't set negative pool size."; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 122 | return false; |
| 123 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 124 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 125 | candidate_pool_size_ = candidate_pool_size; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 126 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 127 | // If ICE servers changed, throw away any existing pooled sessions and create |
| 128 | // new ones. |
| 129 | if (ice_servers_changed) { |
| 130 | pooled_sessions_.clear(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 133 | turn_customizer_ = turn_customizer; |
| 134 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 135 | // If |candidate_pool_size_| is less than the number of pooled sessions, get |
| 136 | // rid of the extras. |
| 137 | while (candidate_pool_size_ < static_cast<int>(pooled_sessions_.size())) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 138 | pooled_sessions_.front().reset(nullptr); |
| 139 | pooled_sessions_.pop_front(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 140 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 141 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 142 | // If |candidate_pool_size_| is greater than the number of pooled sessions, |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 143 | // create new sessions. |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 144 | while (static_cast<int>(pooled_sessions_.size()) < candidate_pool_size_) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 145 | PortAllocatorSession* pooled_session = CreateSessionInternal("", 0, "", ""); |
| 146 | pooled_session->StartGettingPorts(); |
| 147 | pooled_sessions_.push_back( |
| 148 | std::unique_ptr<PortAllocatorSession>(pooled_session)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 149 | } |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 150 | return true; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | std::unique_ptr<PortAllocatorSession> PortAllocator::CreateSession( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 154 | const std::string& content_name, |
| 155 | int component, |
| 156 | const std::string& ice_ufrag, |
| 157 | const std::string& ice_pwd) { |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 158 | auto session = std::unique_ptr<PortAllocatorSession>( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 159 | CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd)); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 160 | session->SetCandidateFilter(candidate_filter()); |
| 161 | return session; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | std::unique_ptr<PortAllocatorSession> PortAllocator::TakePooledSession( |
| 165 | const std::string& content_name, |
| 166 | int component, |
| 167 | const std::string& ice_ufrag, |
| 168 | const std::string& ice_pwd) { |
| 169 | RTC_DCHECK(!ice_ufrag.empty()); |
| 170 | RTC_DCHECK(!ice_pwd.empty()); |
| 171 | if (pooled_sessions_.empty()) { |
| 172 | return nullptr; |
| 173 | } |
| 174 | std::unique_ptr<PortAllocatorSession> ret = |
| 175 | std::move(pooled_sessions_.front()); |
| 176 | ret->SetIceParameters(content_name, component, ice_ufrag, ice_pwd); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 177 | // According to JSEP, a pooled session should filter candidates only after |
| 178 | // it's taken out of the pool. |
| 179 | ret->SetCandidateFilter(candidate_filter()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 180 | pooled_sessions_.pop_front(); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | const PortAllocatorSession* PortAllocator::GetPooledSession() const { |
| 185 | if (pooled_sessions_.empty()) { |
| 186 | return nullptr; |
| 187 | } |
| 188 | return pooled_sessions_.front().get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 189 | } |
| 190 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 191 | void PortAllocator::FreezeCandidatePool() { |
| 192 | candidate_pool_frozen_ = true; |
| 193 | } |
| 194 | |
| 195 | void PortAllocator::DiscardCandidatePool() { |
| 196 | pooled_sessions_.clear(); |
| 197 | } |
| 198 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 199 | } // namespace cricket |