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