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