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