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/client/basicportallocator.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 12 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 13 | #include <algorithm> |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 14 | #include <functional> |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 15 | #include <set> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/umametrics.h" |
| 20 | #include "p2p/base/basicpacketsocketfactory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "p2p/base/port.h" |
| 22 | #include "p2p/base/relayport.h" |
| 23 | #include "p2p/base/stunport.h" |
| 24 | #include "p2p/base/tcpport.h" |
| 25 | #include "p2p/base/turnport.h" |
| 26 | #include "p2p/base/udpport.h" |
| 27 | #include "rtc_base/checks.h" |
| 28 | #include "rtc_base/helpers.h" |
| 29 | #include "rtc_base/logging.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 30 | |
| 31 | using rtc::CreateRandomId; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 32 | |
| 33 | namespace { |
| 34 | |
| 35 | enum { |
| 36 | MSG_CONFIG_START, |
| 37 | MSG_CONFIG_READY, |
| 38 | MSG_ALLOCATE, |
| 39 | MSG_ALLOCATION_PHASE, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 40 | MSG_SEQUENCEOBJECTS_CREATED, |
| 41 | MSG_CONFIG_STOP, |
| 42 | }; |
| 43 | |
| 44 | const int PHASE_UDP = 0; |
| 45 | const int PHASE_RELAY = 1; |
| 46 | const int PHASE_TCP = 2; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 47 | |
deadbeef | 1c5e6d0 | 2017-09-15 17:46:56 -0700 | [diff] [blame] | 48 | const int kNumPhases = 3; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | |
zhihuang | 696f8ca | 2017-06-27 15:11:24 -0700 | [diff] [blame] | 50 | // Gets protocol priority: UDP > TCP > SSLTCP == TLS. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 51 | int GetProtocolPriority(cricket::ProtocolType protocol) { |
| 52 | switch (protocol) { |
| 53 | case cricket::PROTO_UDP: |
| 54 | return 2; |
| 55 | case cricket::PROTO_TCP: |
| 56 | return 1; |
| 57 | case cricket::PROTO_SSLTCP: |
zhihuang | 696f8ca | 2017-06-27 15:11:24 -0700 | [diff] [blame] | 58 | case cricket::PROTO_TLS: |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 59 | return 0; |
| 60 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 61 | RTC_NOTREACHED(); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | } |
| 65 | // Gets address family priority: IPv6 > IPv4 > Unspecified. |
| 66 | int GetAddressFamilyPriority(int ip_family) { |
| 67 | switch (ip_family) { |
| 68 | case AF_INET6: |
| 69 | return 2; |
| 70 | case AF_INET: |
| 71 | return 1; |
| 72 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 73 | RTC_NOTREACHED(); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Returns positive if a is better, negative if b is better, and 0 otherwise. |
| 79 | int ComparePort(const cricket::Port* a, const cricket::Port* b) { |
| 80 | int a_protocol = GetProtocolPriority(a->GetProtocol()); |
| 81 | int b_protocol = GetProtocolPriority(b->GetProtocol()); |
| 82 | int cmp_protocol = a_protocol - b_protocol; |
| 83 | if (cmp_protocol != 0) { |
| 84 | return cmp_protocol; |
| 85 | } |
| 86 | |
| 87 | int a_family = GetAddressFamilyPriority(a->Network()->GetBestIP().family()); |
| 88 | int b_family = GetAddressFamilyPriority(b->Network()->GetBestIP().family()); |
| 89 | return a_family - b_family; |
| 90 | } |
| 91 | |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 92 | struct NetworkFilter { |
| 93 | using Predicate = std::function<bool(rtc::Network*)>; |
| 94 | NetworkFilter(Predicate pred, const std::string& description) |
| 95 | : pred(pred), description(description) {} |
| 96 | Predicate pred; |
| 97 | const std::string description; |
| 98 | }; |
| 99 | |
| 100 | using NetworkList = rtc::NetworkManager::NetworkList; |
| 101 | void FilterNetworks(NetworkList* networks, NetworkFilter filter) { |
| 102 | auto start_to_remove = |
| 103 | std::remove_if(networks->begin(), networks->end(), filter.pred); |
| 104 | if (start_to_remove == networks->end()) { |
| 105 | return; |
| 106 | } |
| 107 | RTC_LOG(INFO) << "Filtered out " << filter.description << " networks:"; |
| 108 | for (auto it = start_to_remove; it != networks->end(); ++it) { |
| 109 | RTC_LOG(INFO) << (*it)->ToString(); |
| 110 | } |
| 111 | networks->erase(start_to_remove, networks->end()); |
| 112 | } |
| 113 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 114 | } // namespace |
| 115 | |
| 116 | namespace cricket { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 117 | const uint32_t DISABLE_ALL_PHASES = |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 118 | PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_TCP | |
| 119 | PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 120 | |
| 121 | // BasicPortAllocator |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 122 | BasicPortAllocator::BasicPortAllocator( |
| 123 | rtc::NetworkManager* network_manager, |
| 124 | rtc::PacketSocketFactory* socket_factory, |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 125 | webrtc::TurnCustomizer* customizer, |
| 126 | RelayPortFactoryInterface* relay_port_factory) |
maxmorin | e9ef907 | 2017-08-29 04:49:00 -0700 | [diff] [blame] | 127 | : network_manager_(network_manager), socket_factory_(socket_factory) { |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 128 | InitRelayPortFactory(relay_port_factory); |
| 129 | RTC_DCHECK(relay_port_factory_ != nullptr); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 130 | RTC_DCHECK(network_manager_ != nullptr); |
| 131 | RTC_DCHECK(socket_factory_ != nullptr); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 132 | SetConfiguration(ServerAddresses(), std::vector<RelayServerConfig>(), 0, |
| 133 | false, customizer); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 134 | Construct(); |
| 135 | } |
| 136 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 137 | BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager) |
maxmorin | e9ef907 | 2017-08-29 04:49:00 -0700 | [diff] [blame] | 138 | : network_manager_(network_manager), socket_factory_(nullptr) { |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 139 | InitRelayPortFactory(nullptr); |
| 140 | RTC_DCHECK(relay_port_factory_ != nullptr); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 141 | RTC_DCHECK(network_manager_ != nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 142 | Construct(); |
| 143 | } |
| 144 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 145 | BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager, |
| 146 | rtc::PacketSocketFactory* socket_factory, |
| 147 | const ServerAddresses& stun_servers) |
maxmorin | e9ef907 | 2017-08-29 04:49:00 -0700 | [diff] [blame] | 148 | : network_manager_(network_manager), socket_factory_(socket_factory) { |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 149 | InitRelayPortFactory(nullptr); |
| 150 | RTC_DCHECK(relay_port_factory_ != nullptr); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 151 | RTC_DCHECK(socket_factory_ != NULL); |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 152 | SetConfiguration(stun_servers, std::vector<RelayServerConfig>(), 0, false, |
| 153 | nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 154 | Construct(); |
| 155 | } |
| 156 | |
| 157 | BasicPortAllocator::BasicPortAllocator( |
| 158 | rtc::NetworkManager* network_manager, |
| 159 | const ServerAddresses& stun_servers, |
| 160 | const rtc::SocketAddress& relay_address_udp, |
| 161 | const rtc::SocketAddress& relay_address_tcp, |
| 162 | const rtc::SocketAddress& relay_address_ssl) |
maxmorin | e9ef907 | 2017-08-29 04:49:00 -0700 | [diff] [blame] | 163 | : network_manager_(network_manager), socket_factory_(NULL) { |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 164 | InitRelayPortFactory(nullptr); |
| 165 | RTC_DCHECK(relay_port_factory_ != nullptr); |
| 166 | RTC_DCHECK(network_manager_ != nullptr); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 167 | std::vector<RelayServerConfig> turn_servers; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 168 | RelayServerConfig config(RELAY_GTURN); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 169 | if (!relay_address_udp.IsNil()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 170 | config.ports.push_back(ProtocolAddress(relay_address_udp, PROTO_UDP)); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 171 | } |
| 172 | if (!relay_address_tcp.IsNil()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 173 | config.ports.push_back(ProtocolAddress(relay_address_tcp, PROTO_TCP)); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 174 | } |
| 175 | if (!relay_address_ssl.IsNil()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 176 | config.ports.push_back(ProtocolAddress(relay_address_ssl, PROTO_SSLTCP)); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 177 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 178 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 179 | if (!config.ports.empty()) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 180 | turn_servers.push_back(config); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 181 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 182 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 183 | SetConfiguration(stun_servers, turn_servers, 0, false, nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 184 | Construct(); |
| 185 | } |
| 186 | |
| 187 | void BasicPortAllocator::Construct() { |
| 188 | allow_tcp_listen_ = true; |
| 189 | } |
| 190 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 191 | void BasicPortAllocator::OnIceRegathering(PortAllocatorSession* session, |
| 192 | IceRegatheringReason reason) { |
| 193 | if (!metrics_observer()) { |
| 194 | return; |
| 195 | } |
| 196 | // If the session has not been taken by an active channel, do not report the |
| 197 | // metric. |
| 198 | for (auto& allocator_session : pooled_sessions()) { |
| 199 | if (allocator_session.get() == session) { |
| 200 | return; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | metrics_observer()->IncrementEnumCounter( |
| 205 | webrtc::kEnumCounterIceRegathering, static_cast<int>(reason), |
| 206 | static_cast<int>(IceRegatheringReason::MAX_VALUE)); |
| 207 | } |
| 208 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 209 | BasicPortAllocator::~BasicPortAllocator() { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 210 | CheckRunOnValidThreadIfInitialized(); |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 211 | // Our created port allocator sessions depend on us, so destroy our remaining |
| 212 | // pooled sessions before anything else. |
| 213 | DiscardCandidatePool(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 216 | void BasicPortAllocator::SetNetworkIgnoreMask(int network_ignore_mask) { |
| 217 | // TODO(phoglund): implement support for other types than loopback. |
| 218 | // See https://code.google.com/p/webrtc/issues/detail?id=4288. |
| 219 | // Then remove set_network_ignore_list from NetworkManager. |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 220 | CheckRunOnValidThreadIfInitialized(); |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 221 | network_ignore_mask_ = network_ignore_mask; |
| 222 | } |
| 223 | |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 224 | PortAllocatorSession* BasicPortAllocator::CreateSessionInternal( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 225 | const std::string& content_name, |
| 226 | int component, |
| 227 | const std::string& ice_ufrag, |
| 228 | const std::string& ice_pwd) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 229 | CheckRunOnValidThreadAndInitialized(); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 230 | PortAllocatorSession* session = new BasicPortAllocatorSession( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 231 | this, content_name, component, ice_ufrag, ice_pwd); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 232 | session->SignalIceRegathering.connect(this, |
| 233 | &BasicPortAllocator::OnIceRegathering); |
| 234 | return session; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 237 | void BasicPortAllocator::AddTurnServer(const RelayServerConfig& turn_server) { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 238 | CheckRunOnValidThreadAndInitialized(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 239 | std::vector<RelayServerConfig> new_turn_servers = turn_servers(); |
| 240 | new_turn_servers.push_back(turn_server); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 241 | SetConfiguration(stun_servers(), new_turn_servers, candidate_pool_size(), |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 242 | prune_turn_ports(), turn_customizer()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 243 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 244 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 245 | void BasicPortAllocator::InitRelayPortFactory( |
| 246 | RelayPortFactoryInterface* relay_port_factory) { |
| 247 | if (relay_port_factory != nullptr) { |
| 248 | relay_port_factory_ = relay_port_factory; |
| 249 | } else { |
| 250 | default_relay_port_factory_.reset(new TurnPortFactory()); |
| 251 | relay_port_factory_ = default_relay_port_factory_.get(); |
| 252 | } |
| 253 | } |
| 254 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 255 | // BasicPortAllocatorSession |
| 256 | BasicPortAllocatorSession::BasicPortAllocatorSession( |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 257 | BasicPortAllocator* allocator, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 258 | const std::string& content_name, |
| 259 | int component, |
| 260 | const std::string& ice_ufrag, |
| 261 | const std::string& ice_pwd) |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 262 | : PortAllocatorSession(content_name, |
| 263 | component, |
| 264 | ice_ufrag, |
| 265 | ice_pwd, |
| 266 | allocator->flags()), |
| 267 | allocator_(allocator), |
| 268 | network_thread_(NULL), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 269 | socket_factory_(allocator->socket_factory()), |
| 270 | allocation_started_(false), |
| 271 | network_manager_started_(false), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 272 | allocation_sequences_created_(false), |
| 273 | prune_turn_ports_(allocator->prune_turn_ports()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 274 | allocator_->network_manager()->SignalNetworksChanged.connect( |
| 275 | this, &BasicPortAllocatorSession::OnNetworksChanged); |
| 276 | allocator_->network_manager()->StartUpdating(); |
| 277 | } |
| 278 | |
| 279 | BasicPortAllocatorSession::~BasicPortAllocatorSession() { |
| 280 | allocator_->network_manager()->StopUpdating(); |
| 281 | if (network_thread_ != NULL) |
| 282 | network_thread_->Clear(this); |
| 283 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 284 | for (uint32_t i = 0; i < sequences_.size(); ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 285 | // AllocationSequence should clear it's map entry for turn ports before |
| 286 | // ports are destroyed. |
| 287 | sequences_[i]->Clear(); |
| 288 | } |
| 289 | |
| 290 | std::vector<PortData>::iterator it; |
| 291 | for (it = ports_.begin(); it != ports_.end(); it++) |
| 292 | delete it->port(); |
| 293 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 294 | for (uint32_t i = 0; i < configs_.size(); ++i) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 295 | delete configs_[i]; |
| 296 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 297 | for (uint32_t i = 0; i < sequences_.size(); ++i) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 298 | delete sequences_[i]; |
| 299 | } |
| 300 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 301 | BasicPortAllocator* BasicPortAllocatorSession::allocator() { |
| 302 | return allocator_; |
| 303 | } |
| 304 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 305 | void BasicPortAllocatorSession::SetCandidateFilter(uint32_t filter) { |
| 306 | if (filter == candidate_filter_) { |
| 307 | return; |
| 308 | } |
| 309 | // We assume the filter will only change from "ALL" to something else. |
| 310 | RTC_DCHECK(candidate_filter_ == CF_ALL); |
| 311 | candidate_filter_ = filter; |
| 312 | for (PortData& port : ports_) { |
| 313 | if (!port.has_pairable_candidate()) { |
| 314 | continue; |
| 315 | } |
| 316 | const auto& candidates = port.port()->Candidates(); |
| 317 | // Setting a filter may cause a ready port to become non-ready |
| 318 | // if it no longer has any pairable candidates. |
| 319 | if (!std::any_of(candidates.begin(), candidates.end(), |
| 320 | [this, &port](const Candidate& candidate) { |
| 321 | return CandidatePairable(candidate, port.port()); |
| 322 | })) { |
| 323 | port.set_has_pairable_candidate(false); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 328 | void BasicPortAllocatorSession::StartGettingPorts() { |
| 329 | network_thread_ = rtc::Thread::Current(); |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 330 | state_ = SessionState::GATHERING; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 331 | if (!socket_factory_) { |
| 332 | owned_socket_factory_.reset( |
| 333 | new rtc::BasicPacketSocketFactory(network_thread_)); |
| 334 | socket_factory_ = owned_socket_factory_.get(); |
| 335 | } |
| 336 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 337 | network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_START); |
Honghai Zhang | d78ecf7 | 2016-07-01 14:40:40 -0700 | [diff] [blame] | 338 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 339 | RTC_LOG(LS_INFO) << "Start getting ports with prune_turn_ports " |
| 340 | << (prune_turn_ports_ ? "enabled" : "disabled"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void BasicPortAllocatorSession::StopGettingPorts() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 344 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 345 | ClearGettingPorts(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 346 | // Note: this must be called after ClearGettingPorts because both may set the |
| 347 | // session state and we should set the state to STOPPED. |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 348 | state_ = SessionState::STOPPED; |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void BasicPortAllocatorSession::ClearGettingPorts() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 352 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 353 | network_thread_->Clear(this, MSG_ALLOCATE); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 354 | for (uint32_t i = 0; i < sequences_.size(); ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 355 | sequences_[i]->Stop(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 356 | } |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 357 | network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_STOP); |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 358 | state_ = SessionState::CLEARED; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 361 | bool BasicPortAllocatorSession::IsGettingPorts() { |
| 362 | return state_ == SessionState::GATHERING; |
| 363 | } |
| 364 | |
| 365 | bool BasicPortAllocatorSession::IsCleared() const { |
| 366 | return state_ == SessionState::CLEARED; |
| 367 | } |
| 368 | |
| 369 | bool BasicPortAllocatorSession::IsStopped() const { |
| 370 | return state_ == SessionState::STOPPED; |
| 371 | } |
| 372 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 373 | std::vector<rtc::Network*> BasicPortAllocatorSession::GetFailedNetworks() { |
| 374 | std::vector<rtc::Network*> networks = GetNetworks(); |
| 375 | |
| 376 | // A network interface may have both IPv4 and IPv6 networks. Only if |
| 377 | // neither of the networks has any connections, the network interface |
| 378 | // is considered failed and need to be regathered on. |
| 379 | std::set<std::string> networks_with_connection; |
| 380 | for (const PortData& data : ports_) { |
| 381 | Port* port = data.port(); |
| 382 | if (!port->connections().empty()) { |
| 383 | networks_with_connection.insert(port->Network()->name()); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | networks.erase( |
| 388 | std::remove_if(networks.begin(), networks.end(), |
| 389 | [networks_with_connection](rtc::Network* network) { |
| 390 | // If a network does not have any connection, it is |
| 391 | // considered failed. |
| 392 | return networks_with_connection.find(network->name()) != |
| 393 | networks_with_connection.end(); |
| 394 | }), |
| 395 | networks.end()); |
| 396 | return networks; |
| 397 | } |
| 398 | |
| 399 | void BasicPortAllocatorSession::RegatherOnFailedNetworks() { |
| 400 | // Find the list of networks that have no connection. |
| 401 | std::vector<rtc::Network*> failed_networks = GetFailedNetworks(); |
| 402 | if (failed_networks.empty()) { |
| 403 | return; |
| 404 | } |
| 405 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 406 | RTC_LOG(LS_INFO) << "Regather candidates on failed networks"; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 407 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 408 | // Mark a sequence as "network failed" if its network is in the list of failed |
| 409 | // networks, so that it won't be considered as equivalent when the session |
| 410 | // regathers ports and candidates. |
| 411 | for (AllocationSequence* sequence : sequences_) { |
| 412 | if (!sequence->network_failed() && |
| 413 | std::find(failed_networks.begin(), failed_networks.end(), |
| 414 | sequence->network()) != failed_networks.end()) { |
| 415 | sequence->set_network_failed(); |
| 416 | } |
| 417 | } |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 418 | |
| 419 | bool disable_equivalent_phases = true; |
| 420 | Regather(failed_networks, disable_equivalent_phases, |
| 421 | IceRegatheringReason::NETWORK_FAILURE); |
| 422 | } |
| 423 | |
| 424 | void BasicPortAllocatorSession::RegatherOnAllNetworks() { |
| 425 | std::vector<rtc::Network*> networks = GetNetworks(); |
| 426 | if (networks.empty()) { |
| 427 | return; |
| 428 | } |
| 429 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 430 | RTC_LOG(LS_INFO) << "Regather candidates on all networks"; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 431 | |
| 432 | // We expect to generate candidates that are equivalent to what we have now. |
| 433 | // Force DoAllocate to generate them instead of skipping. |
| 434 | bool disable_equivalent_phases = false; |
| 435 | Regather(networks, disable_equivalent_phases, |
| 436 | IceRegatheringReason::OCCASIONAL_REFRESH); |
| 437 | } |
| 438 | |
| 439 | void BasicPortAllocatorSession::Regather( |
| 440 | const std::vector<rtc::Network*>& networks, |
| 441 | bool disable_equivalent_phases, |
| 442 | IceRegatheringReason reason) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 443 | // Remove ports from being used locally and send signaling to remove |
| 444 | // the candidates on the remote side. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 445 | std::vector<PortData*> ports_to_prune = GetUnprunedPorts(networks); |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 446 | if (!ports_to_prune.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 447 | RTC_LOG(LS_INFO) << "Prune " << ports_to_prune.size() << " ports"; |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 448 | PrunePortsAndRemoveCandidates(ports_to_prune); |
| 449 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 450 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 451 | if (allocation_started_ && network_manager_started_ && !IsStopped()) { |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 452 | SignalIceRegathering(this, reason); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 453 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 454 | DoAllocate(disable_equivalent_phases); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 455 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 458 | void BasicPortAllocatorSession::SetStunKeepaliveIntervalForReadyPorts( |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 459 | const absl::optional<int>& stun_keepalive_interval) { |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 460 | auto ports = ReadyPorts(); |
| 461 | for (PortInterface* port : ports) { |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 462 | // The port type and protocol can be used to identify different subclasses |
| 463 | // of Port in the current implementation. Note that a TCPPort has the type |
| 464 | // LOCAL_PORT_TYPE but uses the protocol PROTO_TCP. |
| 465 | if (port->Type() == STUN_PORT_TYPE || |
| 466 | (port->Type() == LOCAL_PORT_TYPE && port->GetProtocol() == PROTO_UDP)) { |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 467 | static_cast<UDPPort*>(port)->set_stun_keepalive_delay( |
| 468 | stun_keepalive_interval); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 473 | std::vector<PortInterface*> BasicPortAllocatorSession::ReadyPorts() const { |
| 474 | std::vector<PortInterface*> ret; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 475 | for (const PortData& data : ports_) { |
| 476 | if (data.ready()) { |
| 477 | ret.push_back(data.port()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | std::vector<Candidate> BasicPortAllocatorSession::ReadyCandidates() const { |
| 484 | std::vector<Candidate> candidates; |
| 485 | for (const PortData& data : ports_) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 486 | if (!data.ready()) { |
| 487 | continue; |
| 488 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 489 | GetCandidatesFromPort(data, &candidates); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 490 | } |
| 491 | return candidates; |
| 492 | } |
| 493 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 494 | void BasicPortAllocatorSession::GetCandidatesFromPort( |
| 495 | const PortData& data, |
| 496 | std::vector<Candidate>* candidates) const { |
| 497 | RTC_CHECK(candidates != nullptr); |
| 498 | for (const Candidate& candidate : data.port()->Candidates()) { |
| 499 | if (!CheckCandidateFilter(candidate)) { |
| 500 | continue; |
| 501 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 502 | candidates->push_back(SanitizeRelatedAddress(candidate)); |
| 503 | } |
| 504 | } |
| 505 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 506 | Candidate BasicPortAllocatorSession::SanitizeRelatedAddress( |
| 507 | const Candidate& c) const { |
| 508 | Candidate copy = c; |
| 509 | // If adapter enumeration is disabled or host candidates are disabled, |
| 510 | // clear the raddr of STUN candidates to avoid local address leakage. |
| 511 | bool filter_stun_related_address = |
| 512 | ((flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) && |
| 513 | (flags() & PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE)) || |
| 514 | !(candidate_filter_ & CF_HOST); |
| 515 | // If the candidate filter doesn't allow reflexive addresses, empty TURN raddr |
| 516 | // to avoid reflexive address leakage. |
| 517 | bool filter_turn_related_address = !(candidate_filter_ & CF_REFLEXIVE); |
| 518 | if ((c.type() == STUN_PORT_TYPE && filter_stun_related_address) || |
| 519 | (c.type() == RELAY_PORT_TYPE && filter_turn_related_address)) { |
| 520 | copy.set_related_address( |
| 521 | rtc::EmptySocketAddressWithFamily(copy.address().family())); |
| 522 | } |
| 523 | return copy; |
| 524 | } |
| 525 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 526 | bool BasicPortAllocatorSession::CandidatesAllocationDone() const { |
| 527 | // Done only if all required AllocationSequence objects |
| 528 | // are created. |
| 529 | if (!allocation_sequences_created_) { |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | // Check that all port allocation sequences are complete (not running). |
| 534 | if (std::any_of(sequences_.begin(), sequences_.end(), |
| 535 | [](const AllocationSequence* sequence) { |
| 536 | return sequence->state() == AllocationSequence::kRunning; |
| 537 | })) { |
| 538 | return false; |
| 539 | } |
| 540 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 541 | // If all allocated ports are no longer gathering, session must have got all |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 542 | // expected candidates. Session will trigger candidates allocation complete |
| 543 | // signal. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 544 | return std::none_of(ports_.begin(), ports_.end(), |
| 545 | [](const PortData& port) { return port.inprogress(); }); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 548 | void BasicPortAllocatorSession::OnMessage(rtc::Message* message) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 549 | switch (message->message_id) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 550 | case MSG_CONFIG_START: |
| 551 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
| 552 | GetPortConfigurations(); |
| 553 | break; |
| 554 | case MSG_CONFIG_READY: |
| 555 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
| 556 | OnConfigReady(static_cast<PortConfiguration*>(message->pdata)); |
| 557 | break; |
| 558 | case MSG_ALLOCATE: |
| 559 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
| 560 | OnAllocate(); |
| 561 | break; |
| 562 | case MSG_SEQUENCEOBJECTS_CREATED: |
| 563 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
| 564 | OnAllocationSequenceObjectsCreated(); |
| 565 | break; |
| 566 | case MSG_CONFIG_STOP: |
| 567 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
| 568 | OnConfigStop(); |
| 569 | break; |
| 570 | default: |
| 571 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 575 | void BasicPortAllocatorSession::UpdateIceParametersInternal() { |
| 576 | for (PortData& port : ports_) { |
| 577 | port.port()->set_content_name(content_name()); |
| 578 | port.port()->SetIceParameters(component(), ice_ufrag(), ice_pwd()); |
| 579 | } |
| 580 | } |
| 581 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 582 | void BasicPortAllocatorSession::GetPortConfigurations() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 583 | PortConfiguration* config = |
| 584 | new PortConfiguration(allocator_->stun_servers(), username(), password()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 585 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 586 | for (const RelayServerConfig& turn_server : allocator_->turn_servers()) { |
| 587 | config->AddRelay(turn_server); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 588 | } |
| 589 | ConfigReady(config); |
| 590 | } |
| 591 | |
| 592 | void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 593 | network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_READY, config); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // Adds a configuration to the list. |
| 597 | void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) { |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 598 | if (config) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 599 | configs_.push_back(config); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 600 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 601 | |
| 602 | AllocatePorts(); |
| 603 | } |
| 604 | |
| 605 | void BasicPortAllocatorSession::OnConfigStop() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 606 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 607 | |
| 608 | // If any of the allocated ports have not completed the candidates allocation, |
| 609 | // mark those as error. Since session doesn't need any new candidates |
| 610 | // at this stage of the allocation, it's safe to discard any new candidates. |
| 611 | bool send_signal = false; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 612 | for (std::vector<PortData>::iterator it = ports_.begin(); it != ports_.end(); |
| 613 | ++it) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 614 | if (it->inprogress()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 615 | // Updating port state to error, which didn't finish allocating candidates |
| 616 | // yet. |
| 617 | it->set_error(); |
| 618 | send_signal = true; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | // Did we stop any running sequences? |
| 623 | for (std::vector<AllocationSequence*>::iterator it = sequences_.begin(); |
| 624 | it != sequences_.end() && !send_signal; ++it) { |
| 625 | if ((*it)->state() == AllocationSequence::kStopped) { |
| 626 | send_signal = true; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // If we stopped anything that was running, send a done signal now. |
| 631 | if (send_signal) { |
| 632 | MaybeSignalCandidatesAllocationDone(); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | void BasicPortAllocatorSession::AllocatePorts() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 637 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 638 | network_thread_->Post(RTC_FROM_HERE, this, MSG_ALLOCATE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | void BasicPortAllocatorSession::OnAllocate() { |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 642 | if (network_manager_started_ && !IsStopped()) { |
| 643 | bool disable_equivalent_phases = true; |
| 644 | DoAllocate(disable_equivalent_phases); |
| 645 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 646 | |
| 647 | allocation_started_ = true; |
| 648 | } |
| 649 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 650 | std::vector<rtc::Network*> BasicPortAllocatorSession::GetNetworks() { |
| 651 | std::vector<rtc::Network*> networks; |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 652 | rtc::NetworkManager* network_manager = allocator_->network_manager(); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 653 | RTC_DCHECK(network_manager != nullptr); |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 654 | // If the network permission state is BLOCKED, we just act as if the flag has |
| 655 | // been passed in. |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 656 | if (network_manager->enumeration_permission() == |
guoweis | ea1012b | 2015-08-21 09:06:28 -0700 | [diff] [blame] | 657 | rtc::NetworkManager::ENUMERATION_BLOCKED) { |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 658 | set_flags(flags() | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION); |
| 659 | } |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 660 | // If the adapter enumeration is disabled, we'll just bind to any address |
| 661 | // instead of specific NIC. This is to ensure the same routing for http |
| 662 | // traffic by OS is also used here to avoid any local or public IP leakage |
| 663 | // during stun process. |
| 664 | if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 665 | network_manager->GetAnyAddressNetworks(&networks); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 666 | } else { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 667 | network_manager->GetNetworks(&networks); |
deadbeef | e97389c | 2016-12-23 01:43:45 -0800 | [diff] [blame] | 668 | // If network enumeration fails, use the ANY address as a fallback, so we |
| 669 | // can at least try gathering candidates using the default route chosen by |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 670 | // the OS. Or, if the PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS flag is |
| 671 | // set, we'll use ANY address candidates either way. |
| 672 | if (networks.empty() || flags() & PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS) { |
deadbeef | e97389c | 2016-12-23 01:43:45 -0800 | [diff] [blame] | 673 | network_manager->GetAnyAddressNetworks(&networks); |
| 674 | } |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 675 | } |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 676 | // Filter out link-local networks if needed. |
| 677 | if (flags() & PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS) { |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 678 | NetworkFilter link_local_filter( |
| 679 | [](rtc::Network* network) { return IPIsLinkLocal(network->prefix()); }, |
| 680 | "link-local"); |
| 681 | FilterNetworks(&networks, link_local_filter); |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 682 | } |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 683 | // Do some more filtering, depending on the network ignore mask and "disable |
| 684 | // costly networks" flag. |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 685 | NetworkFilter ignored_filter( |
| 686 | [this](rtc::Network* network) { |
| 687 | return allocator_->network_ignore_mask() & network->type(); |
| 688 | }, |
| 689 | "ignored"); |
| 690 | FilterNetworks(&networks, ignored_filter); |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 691 | if (flags() & PORTALLOCATOR_DISABLE_COSTLY_NETWORKS) { |
| 692 | uint16_t lowest_cost = rtc::kNetworkCostMax; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 693 | for (rtc::Network* network : networks) { |
Yuwei Huang | b181f71 | 2018-01-22 17:01:28 -0800 | [diff] [blame] | 694 | // Don't determine the lowest cost from a link-local network. |
| 695 | // On iOS, a device connected to the computer will get a link-local |
| 696 | // network for communicating with the computer, however this network can't |
| 697 | // be used to connect to a peer outside the network. |
| 698 | if (rtc::IPIsLinkLocal(network->GetBestIP())) { |
| 699 | continue; |
| 700 | } |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 701 | lowest_cost = std::min<uint16_t>(lowest_cost, network->GetCost()); |
| 702 | } |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 703 | NetworkFilter costly_filter( |
| 704 | [lowest_cost](rtc::Network* network) { |
| 705 | return network->GetCost() > lowest_cost + rtc::kNetworkCostLow; |
| 706 | }, |
| 707 | "costly"); |
| 708 | FilterNetworks(&networks, costly_filter); |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 709 | } |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 710 | // Lastly, if we have a limit for the number of IPv6 network interfaces (by |
| 711 | // default, it's 5), remove networks to ensure that limit is satisfied. |
| 712 | // |
| 713 | // TODO(deadbeef): Instead of just taking the first N arbitrary IPv6 |
| 714 | // networks, we could try to choose a set that's "most likely to work". It's |
| 715 | // hard to define what that means though; it's not just "lowest cost". |
| 716 | // Alternatively, we could just focus on making our ICE pinging logic smarter |
| 717 | // such that this filtering isn't necessary in the first place. |
| 718 | int ipv6_networks = 0; |
| 719 | for (auto it = networks.begin(); it != networks.end();) { |
| 720 | if ((*it)->prefix().family() == AF_INET6) { |
| 721 | if (ipv6_networks >= allocator_->max_ipv6_networks()) { |
| 722 | it = networks.erase(it); |
| 723 | continue; |
| 724 | } else { |
| 725 | ++ipv6_networks; |
| 726 | } |
| 727 | } |
| 728 | ++it; |
| 729 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 730 | return networks; |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | // For each network, see if we have a sequence that covers it already. If not, |
| 734 | // create a new sequence to create the appropriate ports. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 735 | void BasicPortAllocatorSession::DoAllocate(bool disable_equivalent) { |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 736 | bool done_signal_needed = false; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 737 | std::vector<rtc::Network*> networks = GetNetworks(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 738 | if (networks.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 739 | RTC_LOG(LS_WARNING) |
| 740 | << "Machine has no networks; no ports will be allocated"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 741 | done_signal_needed = true; |
| 742 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 743 | RTC_LOG(LS_INFO) << "Allocate ports on " << networks.size() << " networks"; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 744 | PortConfiguration* config = configs_.empty() ? nullptr : configs_.back(); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 745 | for (uint32_t i = 0; i < networks.size(); ++i) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 746 | uint32_t sequence_flags = flags(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 747 | if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { |
| 748 | // If all the ports are disabled we should just fire the allocation |
| 749 | // done event and return. |
| 750 | done_signal_needed = true; |
| 751 | break; |
| 752 | } |
| 753 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 754 | if (!config || config->relays.empty()) { |
| 755 | // No relay ports specified in this config. |
| 756 | sequence_flags |= PORTALLOCATOR_DISABLE_RELAY; |
| 757 | } |
| 758 | |
| 759 | if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6) && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 760 | networks[i]->GetBestIP().family() == AF_INET6) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 761 | // Skip IPv6 networks unless the flag's been set. |
| 762 | continue; |
| 763 | } |
| 764 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 765 | if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6_ON_WIFI) && |
| 766 | networks[i]->GetBestIP().family() == AF_INET6 && |
| 767 | networks[i]->type() == rtc::ADAPTER_TYPE_WIFI) { |
| 768 | // Skip IPv6 Wi-Fi networks unless the flag's been set. |
| 769 | continue; |
| 770 | } |
| 771 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 772 | if (disable_equivalent) { |
| 773 | // Disable phases that would only create ports equivalent to |
| 774 | // ones that we have already made. |
| 775 | DisableEquivalentPhases(networks[i], config, &sequence_flags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 776 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 777 | if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { |
| 778 | // New AllocationSequence would have nothing to do, so don't make it. |
| 779 | continue; |
| 780 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | AllocationSequence* sequence = |
| 784 | new AllocationSequence(this, networks[i], config, sequence_flags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 785 | sequence->SignalPortAllocationComplete.connect( |
| 786 | this, &BasicPortAllocatorSession::OnPortAllocationComplete); |
Honghai Zhang | 5048f57 | 2016-08-23 15:47:33 -0700 | [diff] [blame] | 787 | sequence->Init(); |
| 788 | sequence->Start(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 789 | sequences_.push_back(sequence); |
Honghai Zhang | 5048f57 | 2016-08-23 15:47:33 -0700 | [diff] [blame] | 790 | done_signal_needed = true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | if (done_signal_needed) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 794 | network_thread_->Post(RTC_FROM_HERE, this, MSG_SEQUENCEOBJECTS_CREATED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | void BasicPortAllocatorSession::OnNetworksChanged() { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 799 | std::vector<rtc::Network*> networks = GetNetworks(); |
| 800 | std::vector<rtc::Network*> failed_networks; |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 801 | for (AllocationSequence* sequence : sequences_) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 802 | // Mark the sequence as "network failed" if its network is not in |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 803 | // |networks|. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 804 | if (!sequence->network_failed() && |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 805 | std::find(networks.begin(), networks.end(), sequence->network()) == |
| 806 | networks.end()) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 807 | sequence->OnNetworkFailed(); |
| 808 | failed_networks.push_back(sequence->network()); |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 811 | std::vector<PortData*> ports_to_prune = GetUnprunedPorts(failed_networks); |
| 812 | if (!ports_to_prune.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 813 | RTC_LOG(LS_INFO) << "Prune " << ports_to_prune.size() |
| 814 | << " ports because their networks were gone"; |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 815 | PrunePortsAndRemoveCandidates(ports_to_prune); |
| 816 | } |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 817 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 818 | if (allocation_started_ && !IsStopped()) { |
| 819 | if (network_manager_started_) { |
| 820 | // If the network manager has started, it must be regathering. |
| 821 | SignalIceRegathering(this, IceRegatheringReason::NETWORK_CHANGE); |
| 822 | } |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 823 | bool disable_equivalent_phases = true; |
| 824 | DoAllocate(disable_equivalent_phases); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Honghai Zhang | 5048f57 | 2016-08-23 15:47:33 -0700 | [diff] [blame] | 827 | if (!network_manager_started_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 828 | RTC_LOG(LS_INFO) << "Network manager has started"; |
Honghai Zhang | 5048f57 | 2016-08-23 15:47:33 -0700 | [diff] [blame] | 829 | network_manager_started_ = true; |
| 830 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | void BasicPortAllocatorSession::DisableEquivalentPhases( |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 834 | rtc::Network* network, |
| 835 | PortConfiguration* config, |
| 836 | uint32_t* flags) { |
| 837 | for (uint32_t i = 0; i < sequences_.size() && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 838 | (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 839 | ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 840 | sequences_[i]->DisableEquivalentPhases(network, config, flags); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | void BasicPortAllocatorSession::AddAllocatedPort(Port* port, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 845 | AllocationSequence* seq, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 846 | bool prepare_address) { |
| 847 | if (!port) |
| 848 | return; |
| 849 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 850 | RTC_LOG(LS_INFO) << "Adding allocated port for " << content_name(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 851 | port->set_content_name(content_name()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 852 | port->set_component(component()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 853 | port->set_generation(generation()); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 854 | if (allocator_->proxy().type != rtc::PROXY_NONE) |
| 855 | port->set_proxy(allocator_->user_agent(), allocator_->proxy()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 856 | port->set_send_retransmit_count_attribute( |
| 857 | (flags() & PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE) != 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 858 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 859 | PortData data(port, seq); |
| 860 | ports_.push_back(data); |
| 861 | |
| 862 | port->SignalCandidateReady.connect( |
| 863 | this, &BasicPortAllocatorSession::OnCandidateReady); |
| 864 | port->SignalPortComplete.connect(this, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 865 | &BasicPortAllocatorSession::OnPortComplete); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 866 | port->SignalDestroyed.connect(this, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 867 | &BasicPortAllocatorSession::OnPortDestroyed); |
| 868 | port->SignalPortError.connect(this, &BasicPortAllocatorSession::OnPortError); |
| 869 | RTC_LOG(LS_INFO) << port->ToString() << ": Added port to allocator"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 870 | |
| 871 | if (prepare_address) |
| 872 | port->PrepareAddress(); |
| 873 | } |
| 874 | |
| 875 | void BasicPortAllocatorSession::OnAllocationSequenceObjectsCreated() { |
| 876 | allocation_sequences_created_ = true; |
| 877 | // Send candidate allocation complete signal if we have no sequences. |
| 878 | MaybeSignalCandidatesAllocationDone(); |
| 879 | } |
| 880 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 881 | void BasicPortAllocatorSession::OnCandidateReady(Port* port, |
| 882 | const Candidate& c) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 883 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 884 | PortData* data = FindPort(port); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 885 | RTC_DCHECK(data != NULL); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 886 | RTC_LOG(LS_INFO) << port->ToString() |
| 887 | << ": Gathered candidate: " << c.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 888 | // Discarding any candidate signal if port allocation status is |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 889 | // already done with gathering. |
| 890 | if (!data->inprogress()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 891 | RTC_LOG(LS_WARNING) |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame] | 892 | << "Discarding candidate because port is already done gathering."; |
danilchap | f4e8cf0 | 2016-06-30 01:55:03 -0700 | [diff] [blame] | 893 | return; |
Honghai Zhang | 17aac05 | 2016-06-29 21:41:53 -0700 | [diff] [blame] | 894 | } |
Honghai Zhang | 17aac05 | 2016-06-29 21:41:53 -0700 | [diff] [blame] | 895 | |
danilchap | f4e8cf0 | 2016-06-30 01:55:03 -0700 | [diff] [blame] | 896 | // Mark that the port has a pairable candidate, either because we have a |
| 897 | // usable candidate from the port, or simply because the port is bound to the |
| 898 | // any address and therefore has no host candidate. This will trigger the port |
| 899 | // to start creating candidate pairs (connections) and issue connectivity |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 900 | // checks. If port has already been marked as having a pairable candidate, |
| 901 | // do nothing here. |
| 902 | // Note: We should check whether any candidates may become ready after this |
| 903 | // because there we will check whether the candidate is generated by the ready |
| 904 | // ports, which may include this port. |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 905 | bool pruned = false; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 906 | if (CandidatePairable(c, port) && !data->has_pairable_candidate()) { |
danilchap | f4e8cf0 | 2016-06-30 01:55:03 -0700 | [diff] [blame] | 907 | data->set_has_pairable_candidate(true); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 908 | |
| 909 | if (prune_turn_ports_ && port->Type() == RELAY_PORT_TYPE) { |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 910 | pruned = PruneTurnPorts(port); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 911 | } |
| 912 | // If the current port is not pruned yet, SignalPortReady. |
| 913 | if (!data->pruned()) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 914 | RTC_LOG(LS_INFO) << port->ToString() << ": Port ready."; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 915 | SignalPortReady(this, port); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 916 | port->KeepAliveUntilPruned(); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 917 | } |
Honghai Zhang | 17aac05 | 2016-06-29 21:41:53 -0700 | [diff] [blame] | 918 | } |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 919 | |
deadbeef | 1c5e6d0 | 2017-09-15 17:46:56 -0700 | [diff] [blame] | 920 | if (data->ready() && CheckCandidateFilter(c)) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 921 | std::vector<Candidate> candidates; |
| 922 | candidates.push_back(SanitizeRelatedAddress(c)); |
| 923 | SignalCandidatesReady(this, candidates); |
deadbeef | a64edb8 | 2016-07-15 14:42:21 -0700 | [diff] [blame] | 924 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 925 | RTC_LOG(LS_INFO) << "Discarding candidate because it doesn't match filter."; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | // If we have pruned any port, maybe need to signal port allocation done. |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 929 | if (pruned) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 930 | MaybeSignalCandidatesAllocationDone(); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | Port* BasicPortAllocatorSession::GetBestTurnPortForNetwork( |
| 935 | const std::string& network_name) const { |
| 936 | Port* best_turn_port = nullptr; |
| 937 | for (const PortData& data : ports_) { |
| 938 | if (data.port()->Network()->name() == network_name && |
| 939 | data.port()->Type() == RELAY_PORT_TYPE && data.ready() && |
| 940 | (!best_turn_port || ComparePort(data.port(), best_turn_port) > 0)) { |
| 941 | best_turn_port = data.port(); |
| 942 | } |
| 943 | } |
| 944 | return best_turn_port; |
| 945 | } |
| 946 | |
| 947 | bool BasicPortAllocatorSession::PruneTurnPorts(Port* newly_pairable_turn_port) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 948 | // Note: We determine the same network based only on their network names. So |
| 949 | // if an IPv4 address and an IPv6 address have the same network name, they |
| 950 | // are considered the same network here. |
| 951 | const std::string& network_name = newly_pairable_turn_port->Network()->name(); |
| 952 | Port* best_turn_port = GetBestTurnPortForNetwork(network_name); |
| 953 | // |port| is already in the list of ports, so the best port cannot be nullptr. |
| 954 | RTC_CHECK(best_turn_port != nullptr); |
| 955 | |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 956 | bool pruned = false; |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 957 | std::vector<PortData*> ports_to_prune; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 958 | for (PortData& data : ports_) { |
| 959 | if (data.port()->Network()->name() == network_name && |
| 960 | data.port()->Type() == RELAY_PORT_TYPE && !data.pruned() && |
| 961 | ComparePort(data.port(), best_turn_port) < 0) { |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 962 | pruned = true; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 963 | if (data.port() != newly_pairable_turn_port) { |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 964 | // These ports will be pruned in PrunePortsAndRemoveCandidates. |
| 965 | ports_to_prune.push_back(&data); |
| 966 | } else { |
| 967 | data.Prune(); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | } |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 971 | |
| 972 | if (!ports_to_prune.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 973 | RTC_LOG(LS_INFO) << "Prune " << ports_to_prune.size() |
| 974 | << " low-priority TURN ports"; |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 975 | PrunePortsAndRemoveCandidates(ports_to_prune); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 976 | } |
| 977 | return pruned; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 980 | void BasicPortAllocatorSession::PruneAllPorts() { |
| 981 | for (PortData& data : ports_) { |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 982 | data.Prune(); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 986 | void BasicPortAllocatorSession::OnPortComplete(Port* port) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 987 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 988 | RTC_LOG(LS_INFO) << port->ToString() |
| 989 | << ": Port completed gathering candidates."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 990 | PortData* data = FindPort(port); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 991 | RTC_DCHECK(data != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 992 | |
| 993 | // Ignore any late signals. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 994 | if (!data->inprogress()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 995 | return; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 996 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 997 | |
| 998 | // Moving to COMPLETE state. |
| 999 | data->set_complete(); |
| 1000 | // Send candidate allocation complete signal if this was the last port. |
| 1001 | MaybeSignalCandidatesAllocationDone(); |
| 1002 | } |
| 1003 | |
| 1004 | void BasicPortAllocatorSession::OnPortError(Port* port) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1005 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1006 | RTC_LOG(LS_INFO) << port->ToString() |
| 1007 | << ": Port encountered error while gathering candidates."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1008 | PortData* data = FindPort(port); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1009 | RTC_DCHECK(data != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1010 | // We might have already given up on this port and stopped it. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1011 | if (!data->inprogress()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1012 | return; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1013 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1014 | |
| 1015 | // SignalAddressError is currently sent from StunPort/TurnPort. |
| 1016 | // But this signal itself is generic. |
| 1017 | data->set_error(); |
| 1018 | // Send candidate allocation complete signal if this was the last port. |
| 1019 | MaybeSignalCandidatesAllocationDone(); |
| 1020 | } |
| 1021 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1022 | bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) const { |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 1023 | uint32_t filter = candidate_filter_; |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 1024 | |
| 1025 | // When binding to any address, before sending packets out, the getsockname |
| 1026 | // returns all 0s, but after sending packets, it'll be the NIC used to |
| 1027 | // send. All 0s is not a valid ICE candidate address and should be filtered |
| 1028 | // out. |
| 1029 | if (c.address().IsAnyIP()) { |
| 1030 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 1033 | if (c.type() == RELAY_PORT_TYPE) { |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 1034 | return ((filter & CF_RELAY) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 1035 | } else if (c.type() == STUN_PORT_TYPE) { |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 1036 | return ((filter & CF_REFLEXIVE) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 1037 | } else if (c.type() == LOCAL_PORT_TYPE) { |
| 1038 | if ((filter & CF_REFLEXIVE) && !c.address().IsPrivateIP()) { |
| 1039 | // We allow host candidates if the filter allows server-reflexive |
| 1040 | // candidates and the candidate is a public IP. Because we don't generate |
| 1041 | // server-reflexive candidates if they have the same IP as the host |
| 1042 | // candidate (i.e. when the host candidate is a public IP), filtering to |
| 1043 | // only server-reflexive candidates won't work right when the host |
| 1044 | // candidates have public IPs. |
| 1045 | return true; |
| 1046 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1047 | |
guoweis@webrtc.org | 931e0cf | 2015-02-18 19:09:42 +0000 | [diff] [blame] | 1048 | return ((filter & CF_HOST) != 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 1049 | } |
| 1050 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 1053 | bool BasicPortAllocatorSession::CandidatePairable(const Candidate& c, |
| 1054 | const Port* port) const { |
| 1055 | bool candidate_signalable = CheckCandidateFilter(c); |
| 1056 | |
| 1057 | // When device enumeration is disabled (to prevent non-default IP addresses |
| 1058 | // from leaking), we ping from some local candidates even though we don't |
| 1059 | // signal them. However, if host candidates are also disabled (for example, to |
| 1060 | // prevent even default IP addresses from leaking), we still don't want to |
| 1061 | // ping from them, even if device enumeration is disabled. Thus, we check for |
| 1062 | // both device enumeration and host candidates being disabled. |
| 1063 | bool network_enumeration_disabled = c.address().IsAnyIP(); |
| 1064 | bool can_ping_from_candidate = |
| 1065 | (port->SharedSocket() || c.protocol() == TCP_PROTOCOL_NAME); |
| 1066 | bool host_candidates_disabled = !(candidate_filter_ & CF_HOST); |
| 1067 | |
| 1068 | return candidate_signalable || |
| 1069 | (network_enumeration_disabled && can_ping_from_candidate && |
| 1070 | !host_candidates_disabled); |
| 1071 | } |
| 1072 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1073 | void BasicPortAllocatorSession::OnPortAllocationComplete( |
| 1074 | AllocationSequence* seq) { |
| 1075 | // Send candidate allocation complete signal if all ports are done. |
| 1076 | MaybeSignalCandidatesAllocationDone(); |
| 1077 | } |
| 1078 | |
| 1079 | void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1080 | if (CandidatesAllocationDone()) { |
| 1081 | if (pooled()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1082 | RTC_LOG(LS_INFO) << "All candidates gathered for pooled session."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1083 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1084 | RTC_LOG(LS_INFO) << "All candidates gathered for " << content_name() |
| 1085 | << ":" << component() << ":" << generation(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1086 | } |
| 1087 | SignalCandidatesAllocationDone(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1088 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1091 | void BasicPortAllocatorSession::OnPortDestroyed(PortInterface* port) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1092 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1093 | for (std::vector<PortData>::iterator iter = ports_.begin(); |
| 1094 | iter != ports_.end(); ++iter) { |
| 1095 | if (port == iter->port()) { |
| 1096 | ports_.erase(iter); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1097 | RTC_LOG(LS_INFO) << port->ToString() << ": Removed port from allocator (" |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1098 | << static_cast<int>(ports_.size()) << " remaining)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1099 | return; |
| 1100 | } |
| 1101 | } |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 1102 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1105 | BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort( |
| 1106 | Port* port) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1107 | for (std::vector<PortData>::iterator it = ports_.begin(); it != ports_.end(); |
| 1108 | ++it) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1109 | if (it->port() == port) { |
| 1110 | return &*it; |
| 1111 | } |
| 1112 | } |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1116 | std::vector<BasicPortAllocatorSession::PortData*> |
| 1117 | BasicPortAllocatorSession::GetUnprunedPorts( |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1118 | const std::vector<rtc::Network*>& networks) { |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1119 | std::vector<PortData*> unpruned_ports; |
| 1120 | for (PortData& port : ports_) { |
| 1121 | if (!port.pruned() && |
| 1122 | std::find(networks.begin(), networks.end(), |
| 1123 | port.sequence()->network()) != networks.end()) { |
| 1124 | unpruned_ports.push_back(&port); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1125 | } |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1126 | } |
| 1127 | return unpruned_ports; |
| 1128 | } |
| 1129 | |
| 1130 | void BasicPortAllocatorSession::PrunePortsAndRemoveCandidates( |
| 1131 | const std::vector<PortData*>& port_data_list) { |
| 1132 | std::vector<PortInterface*> pruned_ports; |
| 1133 | std::vector<Candidate> removed_candidates; |
| 1134 | for (PortData* data : port_data_list) { |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 1135 | // Prune the port so that it may be destroyed. |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1136 | data->Prune(); |
| 1137 | pruned_ports.push_back(data->port()); |
| 1138 | if (data->has_pairable_candidate()) { |
| 1139 | GetCandidatesFromPort(*data, &removed_candidates); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1140 | // Mark the port as having no pairable candidates so that its candidates |
| 1141 | // won't be removed multiple times. |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1142 | data->set_has_pairable_candidate(false); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1143 | } |
| 1144 | } |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1145 | if (!pruned_ports.empty()) { |
| 1146 | SignalPortsPruned(this, pruned_ports); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1147 | } |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1148 | if (!removed_candidates.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1149 | RTC_LOG(LS_INFO) << "Removed " << removed_candidates.size() |
| 1150 | << " candidates"; |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1151 | SignalCandidatesRemoved(this, removed_candidates); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1155 | // AllocationSequence |
| 1156 | |
| 1157 | AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session, |
| 1158 | rtc::Network* network, |
| 1159 | PortConfiguration* config, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1160 | uint32_t flags) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1161 | : session_(session), |
| 1162 | network_(network), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1163 | config_(config), |
| 1164 | state_(kInit), |
| 1165 | flags_(flags), |
| 1166 | udp_socket_(), |
| 1167 | udp_port_(NULL), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1168 | phase_(0) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1169 | |
Honghai Zhang | 5048f57 | 2016-08-23 15:47:33 -0700 | [diff] [blame] | 1170 | void AllocationSequence::Init() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1171 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 1172 | udp_socket_.reset(session_->socket_factory()->CreateUdpSocket( |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1173 | rtc::SocketAddress(network_->GetBestIP(), 0), |
| 1174 | session_->allocator()->min_port(), session_->allocator()->max_port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1175 | if (udp_socket_) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1176 | udp_socket_->SignalReadPacket.connect(this, |
| 1177 | &AllocationSequence::OnReadPacket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1178 | } |
| 1179 | // Continuing if |udp_socket_| is NULL, as local TCP and RelayPort using TCP |
| 1180 | // are next available options to setup a communication channel. |
| 1181 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | void AllocationSequence::Clear() { |
| 1185 | udp_port_ = NULL; |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1186 | relay_ports_.clear(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1189 | void AllocationSequence::OnNetworkFailed() { |
| 1190 | RTC_DCHECK(!network_failed_); |
| 1191 | network_failed_ = true; |
| 1192 | // Stop the allocation sequence if its network failed. |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 1193 | Stop(); |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1196 | AllocationSequence::~AllocationSequence() { |
| 1197 | session_->network_thread()->Clear(this); |
| 1198 | } |
| 1199 | |
| 1200 | void AllocationSequence::DisableEquivalentPhases(rtc::Network* network, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1201 | PortConfiguration* config, |
| 1202 | uint32_t* flags) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1203 | if (network_failed_) { |
| 1204 | // If the network of this allocation sequence has ever become failed, |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 1205 | // it won't be equivalent to the new network. |
| 1206 | return; |
| 1207 | } |
| 1208 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1209 | if (!((network == network_) && (previous_best_ip_ == network->GetBestIP()))) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1210 | // Different network setup; nothing is equivalent. |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | // Else turn off the stuff that we've already got covered. |
| 1215 | |
deadbeef | 1c46a35 | 2017-09-27 11:24:05 -0700 | [diff] [blame] | 1216 | // Every config implicitly specifies local, so turn that off right away if we |
| 1217 | // already have a port of the corresponding type. Look for a port that |
| 1218 | // matches this AllocationSequence's network, is the right protocol, and |
| 1219 | // hasn't encountered an error. |
| 1220 | // TODO(deadbeef): This doesn't take into account that there may be another |
| 1221 | // AllocationSequence that's ABOUT to allocate a UDP port, but hasn't yet. |
| 1222 | // This can happen if, say, there's a network change event right before an |
| 1223 | // application-triggered ICE restart. Hopefully this problem will just go |
| 1224 | // away if we get rid of the gathering "phases" though, which is planned. |
| 1225 | if (std::any_of(session_->ports_.begin(), session_->ports_.end(), |
| 1226 | [this](const BasicPortAllocatorSession::PortData& p) { |
| 1227 | return p.port()->Network() == network_ && |
| 1228 | p.port()->GetProtocol() == PROTO_UDP && !p.error(); |
| 1229 | })) { |
| 1230 | *flags |= PORTALLOCATOR_DISABLE_UDP; |
| 1231 | } |
| 1232 | if (std::any_of(session_->ports_.begin(), session_->ports_.end(), |
| 1233 | [this](const BasicPortAllocatorSession::PortData& p) { |
| 1234 | return p.port()->Network() == network_ && |
| 1235 | p.port()->GetProtocol() == PROTO_TCP && !p.error(); |
| 1236 | })) { |
| 1237 | *flags |= PORTALLOCATOR_DISABLE_TCP; |
| 1238 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1239 | |
| 1240 | if (config_ && config) { |
| 1241 | if (config_->StunServers() == config->StunServers()) { |
| 1242 | // Already got this STUN servers covered. |
| 1243 | *flags |= PORTALLOCATOR_DISABLE_STUN; |
| 1244 | } |
| 1245 | if (!config_->relays.empty()) { |
| 1246 | // Already got relays covered. |
| 1247 | // NOTE: This will even skip a _different_ set of relay servers if we |
| 1248 | // were to be given one, but that never happens in our codebase. Should |
| 1249 | // probably get rid of the list in PortConfiguration and just keep a |
| 1250 | // single relay server in each one. |
| 1251 | *flags |= PORTALLOCATOR_DISABLE_RELAY; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | void AllocationSequence::Start() { |
| 1257 | state_ = kRunning; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1258 | session_->network_thread()->Post(RTC_FROM_HERE, this, MSG_ALLOCATION_PHASE); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1259 | // Take a snapshot of the best IP, so that when DisableEquivalentPhases is |
| 1260 | // called next time, we enable all phases if the best IP has since changed. |
| 1261 | previous_best_ip_ = network_->GetBestIP(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | void AllocationSequence::Stop() { |
| 1265 | // If the port is completed, don't set it to stopped. |
| 1266 | if (state_ == kRunning) { |
| 1267 | state_ = kStopped; |
| 1268 | session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | void AllocationSequence::OnMessage(rtc::Message* msg) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1273 | RTC_DCHECK(rtc::Thread::Current() == session_->network_thread()); |
| 1274 | RTC_DCHECK(msg->message_id == MSG_ALLOCATION_PHASE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1275 | |
deadbeef | 1c5e6d0 | 2017-09-15 17:46:56 -0700 | [diff] [blame] | 1276 | const char* const PHASE_NAMES[kNumPhases] = {"Udp", "Relay", "Tcp"}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1277 | |
| 1278 | // Perform all of the phases in the current step. |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1279 | RTC_LOG(LS_INFO) << network_->ToString() |
| 1280 | << ": Allocation Phase=" << PHASE_NAMES[phase_]; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1281 | |
| 1282 | switch (phase_) { |
| 1283 | case PHASE_UDP: |
| 1284 | CreateUDPPorts(); |
| 1285 | CreateStunPorts(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1286 | break; |
| 1287 | |
| 1288 | case PHASE_RELAY: |
| 1289 | CreateRelayPorts(); |
| 1290 | break; |
| 1291 | |
| 1292 | case PHASE_TCP: |
| 1293 | CreateTCPPorts(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1294 | state_ = kCompleted; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1295 | break; |
| 1296 | |
| 1297 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 1298 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | if (state() == kRunning) { |
| 1302 | ++phase_; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1303 | session_->network_thread()->PostDelayed(RTC_FROM_HERE, |
| 1304 | session_->allocator()->step_delay(), |
| 1305 | this, MSG_ALLOCATION_PHASE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1306 | } else { |
| 1307 | // If all phases in AllocationSequence are completed, no allocation |
| 1308 | // steps needed further. Canceling pending signal. |
| 1309 | session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE); |
| 1310 | SignalPortAllocationComplete(this); |
| 1311 | } |
| 1312 | } |
| 1313 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1314 | void AllocationSequence::CreateUDPPorts() { |
| 1315 | if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1316 | RTC_LOG(LS_VERBOSE) << "AllocationSequence: UDP ports disabled, skipping."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1317 | return; |
| 1318 | } |
| 1319 | |
| 1320 | // TODO(mallinath) - Remove UDPPort creating socket after shared socket |
| 1321 | // is enabled completely. |
| 1322 | UDPPort* port = NULL; |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 1323 | bool emit_local_candidate_for_anyaddress = |
| 1324 | !IsFlagSet(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1325 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) { |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 1326 | port = UDPPort::Create( |
| 1327 | session_->network_thread(), session_->socket_factory(), network_, |
| 1328 | udp_socket_.get(), session_->username(), session_->password(), |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 1329 | session_->allocator()->origin(), emit_local_candidate_for_anyaddress, |
| 1330 | session_->allocator()->stun_candidate_keepalive_interval()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1331 | } else { |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 1332 | port = UDPPort::Create( |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1333 | session_->network_thread(), session_->socket_factory(), network_, |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 1334 | session_->allocator()->min_port(), session_->allocator()->max_port(), |
| 1335 | session_->username(), session_->password(), |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 1336 | session_->allocator()->origin(), emit_local_candidate_for_anyaddress, |
| 1337 | session_->allocator()->stun_candidate_keepalive_interval()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | if (port) { |
| 1341 | // If shared socket is enabled, STUN candidate will be allocated by the |
| 1342 | // UDPPort. |
| 1343 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 1344 | udp_port_ = port; |
jiayl@webrtc.org | 7e5b380 | 2015-01-22 21:28:39 +0000 | [diff] [blame] | 1345 | port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1346 | |
| 1347 | // If STUN is not disabled, setting stun server address to port. |
| 1348 | if (!IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1349 | if (config_ && !config_->StunServers().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1350 | RTC_LOG(LS_INFO) |
| 1351 | << "AllocationSequence: UDPPort will be handling the " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1352 | "STUN candidate generation."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1353 | port->set_server_addresses(config_->StunServers()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | session_->AddAllocatedPort(port, this, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | void AllocationSequence::CreateTCPPorts() { |
| 1363 | if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1364 | RTC_LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1365 | return; |
| 1366 | } |
| 1367 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1368 | Port* port = TCPPort::Create( |
| 1369 | session_->network_thread(), session_->socket_factory(), network_, |
| 1370 | session_->allocator()->min_port(), session_->allocator()->max_port(), |
| 1371 | session_->username(), session_->password(), |
| 1372 | session_->allocator()->allow_tcp_listen()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1373 | if (port) { |
| 1374 | session_->AddAllocatedPort(port, this, true); |
| 1375 | // Since TCPPort is not created using shared socket, |port| will not be |
| 1376 | // added to the dequeue. |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | void AllocationSequence::CreateStunPorts() { |
| 1381 | if (IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1382 | RTC_LOG(LS_VERBOSE) << "AllocationSequence: STUN ports disabled, skipping."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1383 | return; |
| 1384 | } |
| 1385 | |
| 1386 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) { |
| 1387 | return; |
| 1388 | } |
| 1389 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1390 | if (!(config_ && !config_->StunServers().empty())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1391 | RTC_LOG(LS_WARNING) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1392 | << "AllocationSequence: No STUN server configured, skipping."; |
| 1393 | return; |
| 1394 | } |
| 1395 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1396 | StunPort* port = StunPort::Create( |
| 1397 | session_->network_thread(), session_->socket_factory(), network_, |
| 1398 | session_->allocator()->min_port(), session_->allocator()->max_port(), |
| 1399 | session_->username(), session_->password(), config_->StunServers(), |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 1400 | session_->allocator()->origin(), |
| 1401 | session_->allocator()->stun_candidate_keepalive_interval()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1402 | if (port) { |
| 1403 | session_->AddAllocatedPort(port, this, true); |
| 1404 | // Since StunPort is not created using shared socket, |port| will not be |
| 1405 | // added to the dequeue. |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | void AllocationSequence::CreateRelayPorts() { |
| 1410 | if (IsFlagSet(PORTALLOCATOR_DISABLE_RELAY)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1411 | RTC_LOG(LS_VERBOSE) |
| 1412 | << "AllocationSequence: Relay ports disabled, skipping."; |
| 1413 | return; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we |
| 1417 | // ought to have a relay list for them here. |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 1418 | RTC_DCHECK(config_); |
| 1419 | RTC_DCHECK(!config_->relays.empty()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1420 | if (!(config_ && !config_->relays.empty())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1421 | RTC_LOG(LS_WARNING) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1422 | << "AllocationSequence: No relay server configured, skipping."; |
| 1423 | return; |
| 1424 | } |
| 1425 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1426 | for (RelayServerConfig& relay : config_->relays) { |
| 1427 | if (relay.type == RELAY_GTURN) { |
| 1428 | CreateGturnPort(relay); |
| 1429 | } else if (relay.type == RELAY_TURN) { |
| 1430 | CreateTurnPort(relay); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1431 | } else { |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 1432 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) { |
| 1438 | // TODO(mallinath) - Rename RelayPort to GTurnPort. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1439 | RelayPort* port = RelayPort::Create( |
| 1440 | session_->network_thread(), session_->socket_factory(), network_, |
| 1441 | session_->allocator()->min_port(), session_->allocator()->max_port(), |
| 1442 | config_->username, config_->password); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1443 | if (port) { |
| 1444 | // Since RelayPort is not created using shared socket, |port| will not be |
| 1445 | // added to the dequeue. |
| 1446 | // Note: We must add the allocated port before we add addresses because |
| 1447 | // the latter will create candidates that need name and preference |
| 1448 | // settings. However, we also can't prepare the address (normally |
| 1449 | // done by AddAllocatedPort) until we have these addresses. So we |
| 1450 | // wait to do that until below. |
| 1451 | session_->AddAllocatedPort(port, this, false); |
| 1452 | |
| 1453 | // Add the addresses of this protocol. |
| 1454 | PortList::const_iterator relay_port; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1455 | for (relay_port = config.ports.begin(); relay_port != config.ports.end(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1456 | ++relay_port) { |
| 1457 | port->AddServerAddress(*relay_port); |
| 1458 | port->AddExternalAddress(*relay_port); |
| 1459 | } |
| 1460 | // Start fetching an address for this port. |
| 1461 | port->PrepareAddress(); |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | void AllocationSequence::CreateTurnPort(const RelayServerConfig& config) { |
| 1466 | PortList::const_iterator relay_port; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1467 | for (relay_port = config.ports.begin(); relay_port != config.ports.end(); |
| 1468 | ++relay_port) { |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 1469 | // Skip UDP connections to relay servers if it's disallowed. |
| 1470 | if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP_RELAY) && |
| 1471 | relay_port->proto == PROTO_UDP) { |
| 1472 | continue; |
| 1473 | } |
| 1474 | |
Honghai Zhang | 3d31bd6 | 2016-08-10 10:33:05 -0700 | [diff] [blame] | 1475 | // Do not create a port if the server address family is known and does |
| 1476 | // not match the local IP address family. |
| 1477 | int server_ip_family = relay_port->address.ipaddr().family(); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1478 | int local_ip_family = network_->GetBestIP().family(); |
Honghai Zhang | 3d31bd6 | 2016-08-10 10:33:05 -0700 | [diff] [blame] | 1479 | if (server_ip_family != AF_UNSPEC && server_ip_family != local_ip_family) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1480 | RTC_LOG(LS_INFO) |
| 1481 | << "Server and local address families are not compatible. " |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1482 | "Server address: " |
| 1483 | << relay_port->address.ipaddr().ToString() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1484 | << " Local address: " << network_->GetBestIP().ToString(); |
Honghai Zhang | 3d31bd6 | 2016-08-10 10:33:05 -0700 | [diff] [blame] | 1485 | continue; |
| 1486 | } |
| 1487 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1488 | CreateRelayPortArgs args; |
| 1489 | args.network_thread = session_->network_thread(); |
| 1490 | args.socket_factory = session_->socket_factory(); |
| 1491 | args.network = network_; |
| 1492 | args.username = session_->username(); |
| 1493 | args.password = session_->password(); |
| 1494 | args.server_address = &(*relay_port); |
| 1495 | args.config = &config; |
| 1496 | args.origin = session_->allocator()->origin(); |
| 1497 | args.turn_customizer = session_->allocator()->turn_customizer(); |
| 1498 | |
| 1499 | std::unique_ptr<cricket::Port> port; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1500 | // Shared socket mode must be enabled only for UDP based ports. Hence |
| 1501 | // don't pass shared socket for ports which will create TCP sockets. |
| 1502 | // TODO(mallinath) - Enable shared socket mode for TURN ports. Disabled |
| 1503 | // due to webrtc bug https://code.google.com/p/webrtc/issues/detail?id=3537 |
| 1504 | if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 1505 | relay_port->proto == PROTO_UDP && udp_socket_) { |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1506 | port = session_->allocator()->relay_port_factory()->Create( |
| 1507 | args, udp_socket_.get()); |
| 1508 | |
| 1509 | if (!port) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1510 | RTC_LOG(LS_WARNING) << "Failed to create relay port with " |
| 1511 | << args.server_address->address.ToString(); |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1512 | continue; |
| 1513 | } |
| 1514 | |
| 1515 | relay_ports_.push_back(port.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1516 | // Listen to the port destroyed signal, to allow AllocationSequence to |
| 1517 | // remove entrt from it's map. |
| 1518 | port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed); |
| 1519 | } else { |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1520 | port = session_->allocator()->relay_port_factory()->Create( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1521 | args, session_->allocator()->min_port(), |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1522 | session_->allocator()->max_port()); |
| 1523 | |
| 1524 | if (!port) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1525 | RTC_LOG(LS_WARNING) << "Failed to create relay port with " |
| 1526 | << args.server_address->address.ToString(); |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1527 | continue; |
| 1528 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1529 | } |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1530 | RTC_DCHECK(port != NULL); |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1531 | session_->AddAllocatedPort(port.release(), this, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1535 | void AllocationSequence::OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 1536 | const char* data, |
| 1537 | size_t size, |
| 1538 | const rtc::SocketAddress& remote_addr, |
| 1539 | const rtc::PacketTime& packet_time) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1540 | RTC_DCHECK(socket == udp_socket_.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1541 | |
| 1542 | bool turn_port_found = false; |
| 1543 | |
| 1544 | // Try to find the TurnPort that matches the remote address. Note that the |
| 1545 | // message could be a STUN binding response if the TURN server is also used as |
| 1546 | // a STUN server. We don't want to parse every message here to check if it is |
| 1547 | // a STUN binding response, so we pass the message to TurnPort regardless of |
| 1548 | // the message type. The TurnPort will just ignore the message since it will |
| 1549 | // not find any request by transaction ID. |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1550 | for (auto* port : relay_ports_) { |
| 1551 | if (port->CanHandleIncomingPacketsFrom(remote_addr)) { |
Sergey Ulanov | 17fa672 | 2016-05-10 10:20:47 -0700 | [diff] [blame] | 1552 | if (port->HandleIncomingPacket(socket, data, size, remote_addr, |
| 1553 | packet_time)) { |
| 1554 | return; |
| 1555 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1556 | turn_port_found = true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | if (udp_port_) { |
| 1561 | const ServerAddresses& stun_servers = udp_port_->server_addresses(); |
| 1562 | |
| 1563 | // Pass the packet to the UdpPort if there is no matching TurnPort, or if |
| 1564 | // the TURN server is also a STUN server. |
| 1565 | if (!turn_port_found || |
| 1566 | stun_servers.find(remote_addr) != stun_servers.end()) { |
Sergey Ulanov | 17fa672 | 2016-05-10 10:20:47 -0700 | [diff] [blame] | 1567 | RTC_DCHECK(udp_port_->SharedSocket()); |
| 1568 | udp_port_->HandleIncomingPacket(socket, data, size, remote_addr, |
| 1569 | packet_time); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | void AllocationSequence::OnPortDestroyed(PortInterface* port) { |
| 1575 | if (udp_port_ == port) { |
| 1576 | udp_port_ = NULL; |
| 1577 | return; |
| 1578 | } |
| 1579 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 1580 | auto it = std::find(relay_ports_.begin(), relay_ports_.end(), port); |
| 1581 | if (it != relay_ports_.end()) { |
| 1582 | relay_ports_.erase(it); |
jiayl@webrtc.org | 7e5b380 | 2015-01-22 21:28:39 +0000 | [diff] [blame] | 1583 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1584 | RTC_LOG(LS_ERROR) << "Unexpected OnPortDestroyed for nonexistent port."; |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 1585 | RTC_NOTREACHED(); |
jiayl@webrtc.org | 7e5b380 | 2015-01-22 21:28:39 +0000 | [diff] [blame] | 1586 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | // PortConfiguration |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1590 | PortConfiguration::PortConfiguration(const rtc::SocketAddress& stun_address, |
| 1591 | const std::string& username, |
| 1592 | const std::string& password) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1593 | : stun_address(stun_address), username(username), password(password) { |
| 1594 | if (!stun_address.IsNil()) |
| 1595 | stun_servers.insert(stun_address); |
| 1596 | } |
| 1597 | |
| 1598 | PortConfiguration::PortConfiguration(const ServerAddresses& stun_servers, |
| 1599 | const std::string& username, |
| 1600 | const std::string& password) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1601 | : stun_servers(stun_servers), username(username), password(password) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1602 | if (!stun_servers.empty()) |
| 1603 | stun_address = *(stun_servers.begin()); |
| 1604 | } |
| 1605 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 1606 | PortConfiguration::~PortConfiguration() = default; |
| 1607 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1608 | ServerAddresses PortConfiguration::StunServers() { |
| 1609 | if (!stun_address.IsNil() && |
| 1610 | stun_servers.find(stun_address) == stun_servers.end()) { |
| 1611 | stun_servers.insert(stun_address); |
| 1612 | } |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 1613 | // Every UDP TURN server should also be used as a STUN server. |
| 1614 | ServerAddresses turn_servers = GetRelayServerAddresses(RELAY_TURN, PROTO_UDP); |
| 1615 | for (const rtc::SocketAddress& turn_server : turn_servers) { |
| 1616 | if (stun_servers.find(turn_server) == stun_servers.end()) { |
| 1617 | stun_servers.insert(turn_server); |
| 1618 | } |
| 1619 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1620 | return stun_servers; |
| 1621 | } |
| 1622 | |
| 1623 | void PortConfiguration::AddRelay(const RelayServerConfig& config) { |
| 1624 | relays.push_back(config); |
| 1625 | } |
| 1626 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1627 | bool PortConfiguration::SupportsProtocol(const RelayServerConfig& relay, |
| 1628 | ProtocolType type) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1629 | PortList::const_iterator relay_port; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1630 | for (relay_port = relay.ports.begin(); relay_port != relay.ports.end(); |
| 1631 | ++relay_port) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1632 | if (relay_port->proto == type) |
| 1633 | return true; |
| 1634 | } |
| 1635 | return false; |
| 1636 | } |
| 1637 | |
| 1638 | bool PortConfiguration::SupportsProtocol(RelayType turn_type, |
| 1639 | ProtocolType type) const { |
| 1640 | for (size_t i = 0; i < relays.size(); ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1641 | if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1642 | return true; |
| 1643 | } |
| 1644 | return false; |
| 1645 | } |
| 1646 | |
| 1647 | ServerAddresses PortConfiguration::GetRelayServerAddresses( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 1648 | RelayType turn_type, |
| 1649 | ProtocolType type) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1650 | ServerAddresses servers; |
| 1651 | for (size_t i = 0; i < relays.size(); ++i) { |
| 1652 | if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) { |
| 1653 | servers.insert(relays[i].ports.front().address); |
| 1654 | } |
| 1655 | } |
| 1656 | return servers; |
| 1657 | } |
| 1658 | |
| 1659 | } // namespace cricket |