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