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