blob: 1b3947b6cfc3ca644768b9ba19e1c06c77e5827b [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "p2p/client/basicportallocator.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000012
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080013#include <algorithm>
Steve Anton6c38cc72017-11-29 10:25:58 -080014#include <set>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000015#include <string>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/umametrics.h"
19#include "p2p/base/basicpacketsocketfactory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "p2p/base/port.h"
21#include "p2p/base/relayport.h"
22#include "p2p/base/stunport.h"
23#include "p2p/base/tcpport.h"
24#include "p2p/base/turnport.h"
25#include "p2p/base/udpport.h"
26#include "rtc_base/checks.h"
27#include "rtc_base/helpers.h"
28#include "rtc_base/logging.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000029
30using rtc::CreateRandomId;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000031
32namespace {
33
34enum {
35 MSG_CONFIG_START,
36 MSG_CONFIG_READY,
37 MSG_ALLOCATE,
38 MSG_ALLOCATION_PHASE,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039 MSG_SEQUENCEOBJECTS_CREATED,
40 MSG_CONFIG_STOP,
41};
42
43const int PHASE_UDP = 0;
44const int PHASE_RELAY = 1;
45const int PHASE_TCP = 2;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046
deadbeef1c5e6d02017-09-15 17:46:56 -070047const int kNumPhases = 3;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048
zhihuang696f8ca2017-06-27 15:11:24 -070049// Gets protocol priority: UDP > TCP > SSLTCP == TLS.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070050int GetProtocolPriority(cricket::ProtocolType protocol) {
51 switch (protocol) {
52 case cricket::PROTO_UDP:
53 return 2;
54 case cricket::PROTO_TCP:
55 return 1;
56 case cricket::PROTO_SSLTCP:
zhihuang696f8ca2017-06-27 15:11:24 -070057 case cricket::PROTO_TLS:
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070058 return 0;
59 default:
nisseeb4ca4e2017-01-12 02:24:27 -080060 RTC_NOTREACHED();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070061 return 0;
62 }
63}
64// Gets address family priority: IPv6 > IPv4 > Unspecified.
65int GetAddressFamilyPriority(int ip_family) {
66 switch (ip_family) {
67 case AF_INET6:
68 return 2;
69 case AF_INET:
70 return 1;
71 default:
nisseeb4ca4e2017-01-12 02:24:27 -080072 RTC_NOTREACHED();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070073 return 0;
74 }
75}
76
77// Returns positive if a is better, negative if b is better, and 0 otherwise.
78int ComparePort(const cricket::Port* a, const cricket::Port* b) {
79 int a_protocol = GetProtocolPriority(a->GetProtocol());
80 int b_protocol = GetProtocolPriority(b->GetProtocol());
81 int cmp_protocol = a_protocol - b_protocol;
82 if (cmp_protocol != 0) {
83 return cmp_protocol;
84 }
85
86 int a_family = GetAddressFamilyPriority(a->Network()->GetBestIP().family());
87 int b_family = GetAddressFamilyPriority(b->Network()->GetBestIP().family());
88 return a_family - b_family;
89}
90
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091} // namespace
92
93namespace cricket {
Peter Boström0c4e06b2015-10-07 12:23:21 +020094const uint32_t DISABLE_ALL_PHASES =
honghaizf421bdc2015-07-17 16:21:55 -070095 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_TCP |
96 PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097
98// BasicPortAllocator
Jonas Orelandbdcee282017-10-10 14:01:40 +020099BasicPortAllocator::BasicPortAllocator(
100 rtc::NetworkManager* network_manager,
101 rtc::PacketSocketFactory* socket_factory,
Jonas Oreland202994c2017-12-18 12:10:43 +0100102 webrtc::TurnCustomizer* customizer,
103 RelayPortFactoryInterface* relay_port_factory)
maxmorine9ef9072017-08-29 04:49:00 -0700104 : network_manager_(network_manager), socket_factory_(socket_factory) {
Jonas Oreland202994c2017-12-18 12:10:43 +0100105 InitRelayPortFactory(relay_port_factory);
106 RTC_DCHECK(relay_port_factory_ != nullptr);
nisseede5da42017-01-12 05:15:36 -0800107 RTC_DCHECK(network_manager_ != nullptr);
108 RTC_DCHECK(socket_factory_ != nullptr);
Jonas Orelandbdcee282017-10-10 14:01:40 +0200109 SetConfiguration(ServerAddresses(), std::vector<RelayServerConfig>(),
110 0, false, customizer);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111 Construct();
112}
113
Jonas Oreland202994c2017-12-18 12:10:43 +0100114BasicPortAllocator::BasicPortAllocator(
115 rtc::NetworkManager* network_manager)
maxmorine9ef9072017-08-29 04:49:00 -0700116 : network_manager_(network_manager), socket_factory_(nullptr) {
Jonas Oreland202994c2017-12-18 12:10:43 +0100117 InitRelayPortFactory(nullptr);
118 RTC_DCHECK(relay_port_factory_ != nullptr);
nisseede5da42017-01-12 05:15:36 -0800119 RTC_DCHECK(network_manager_ != nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120 Construct();
121}
122
Jonas Oreland202994c2017-12-18 12:10:43 +0100123BasicPortAllocator::BasicPortAllocator(
124 rtc::NetworkManager* network_manager,
125 rtc::PacketSocketFactory* socket_factory,
126 const ServerAddresses& stun_servers)
maxmorine9ef9072017-08-29 04:49:00 -0700127 : network_manager_(network_manager), socket_factory_(socket_factory) {
Jonas Oreland202994c2017-12-18 12:10:43 +0100128 InitRelayPortFactory(nullptr);
129 RTC_DCHECK(relay_port_factory_ != nullptr);
nisseede5da42017-01-12 05:15:36 -0800130 RTC_DCHECK(socket_factory_ != NULL);
Jonas Orelandbdcee282017-10-10 14:01:40 +0200131 SetConfiguration(stun_servers, std::vector<RelayServerConfig>(), 0, false,
132 nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133 Construct();
134}
135
136BasicPortAllocator::BasicPortAllocator(
137 rtc::NetworkManager* network_manager,
138 const ServerAddresses& stun_servers,
139 const rtc::SocketAddress& relay_address_udp,
140 const rtc::SocketAddress& relay_address_tcp,
141 const rtc::SocketAddress& relay_address_ssl)
maxmorine9ef9072017-08-29 04:49:00 -0700142 : network_manager_(network_manager), socket_factory_(NULL) {
Jonas Oreland202994c2017-12-18 12:10:43 +0100143 InitRelayPortFactory(nullptr);
144 RTC_DCHECK(relay_port_factory_ != nullptr);
145 RTC_DCHECK(network_manager_ != nullptr);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700146 std::vector<RelayServerConfig> turn_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147 RelayServerConfig config(RELAY_GTURN);
deadbeef653b8e02015-11-11 12:55:10 -0800148 if (!relay_address_udp.IsNil()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000149 config.ports.push_back(ProtocolAddress(relay_address_udp, PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -0800150 }
151 if (!relay_address_tcp.IsNil()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000152 config.ports.push_back(ProtocolAddress(relay_address_tcp, PROTO_TCP));
deadbeef653b8e02015-11-11 12:55:10 -0800153 }
154 if (!relay_address_ssl.IsNil()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000155 config.ports.push_back(ProtocolAddress(relay_address_ssl, PROTO_SSLTCP));
deadbeef653b8e02015-11-11 12:55:10 -0800156 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157
deadbeef653b8e02015-11-11 12:55:10 -0800158 if (!config.ports.empty()) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700159 turn_servers.push_back(config);
deadbeef653b8e02015-11-11 12:55:10 -0800160 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161
Jonas Orelandbdcee282017-10-10 14:01:40 +0200162 SetConfiguration(stun_servers, turn_servers, 0, false, nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000163 Construct();
164}
165
166void BasicPortAllocator::Construct() {
167 allow_tcp_listen_ = true;
168}
169
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700170void BasicPortAllocator::OnIceRegathering(PortAllocatorSession* session,
171 IceRegatheringReason reason) {
172 if (!metrics_observer()) {
173 return;
174 }
175 // If the session has not been taken by an active channel, do not report the
176 // metric.
177 for (auto& allocator_session : pooled_sessions()) {
178 if (allocator_session.get() == session) {
179 return;
180 }
181 }
182
183 metrics_observer()->IncrementEnumCounter(
184 webrtc::kEnumCounterIceRegathering, static_cast<int>(reason),
185 static_cast<int>(IceRegatheringReason::MAX_VALUE));
186}
187
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000188BasicPortAllocator::~BasicPortAllocator() {
deadbeef42a42632017-03-10 15:18:00 -0800189 // Our created port allocator sessions depend on us, so destroy our remaining
190 // pooled sessions before anything else.
191 DiscardCandidatePool();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192}
193
Steve Anton7995d8c2017-10-30 16:23:38 -0700194void BasicPortAllocator::SetNetworkIgnoreMask(int network_ignore_mask) {
195 // TODO(phoglund): implement support for other types than loopback.
196 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
197 // Then remove set_network_ignore_list from NetworkManager.
198 network_ignore_mask_ = network_ignore_mask;
199}
200
deadbeefc5d0d952015-07-16 10:22:21 -0700201PortAllocatorSession* BasicPortAllocator::CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 const std::string& content_name, int component,
203 const std::string& ice_ufrag, const std::string& ice_pwd) {
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700204 PortAllocatorSession* session = new BasicPortAllocatorSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205 this, content_name, component, ice_ufrag, ice_pwd);
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700206 session->SignalIceRegathering.connect(this,
207 &BasicPortAllocator::OnIceRegathering);
208 return session;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209}
210
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700211void BasicPortAllocator::AddTurnServer(const RelayServerConfig& turn_server) {
212 std::vector<RelayServerConfig> new_turn_servers = turn_servers();
213 new_turn_servers.push_back(turn_server);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700214 SetConfiguration(stun_servers(), new_turn_servers, candidate_pool_size(),
Jonas Orelandbdcee282017-10-10 14:01:40 +0200215 prune_turn_ports(), turn_customizer());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700216}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000217
Jonas Oreland202994c2017-12-18 12:10:43 +0100218void BasicPortAllocator::InitRelayPortFactory(
219 RelayPortFactoryInterface* relay_port_factory) {
220 if (relay_port_factory != nullptr) {
221 relay_port_factory_ = relay_port_factory;
222 } else {
223 default_relay_port_factory_.reset(new TurnPortFactory());
224 relay_port_factory_ = default_relay_port_factory_.get();
225 }
226}
227
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228// BasicPortAllocatorSession
229BasicPortAllocatorSession::BasicPortAllocatorSession(
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700230 BasicPortAllocator* allocator,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000231 const std::string& content_name,
232 int component,
233 const std::string& ice_ufrag,
234 const std::string& ice_pwd)
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700235 : PortAllocatorSession(content_name,
236 component,
237 ice_ufrag,
238 ice_pwd,
239 allocator->flags()),
240 allocator_(allocator),
241 network_thread_(NULL),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000242 socket_factory_(allocator->socket_factory()),
243 allocation_started_(false),
244 network_manager_started_(false),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700245 allocation_sequences_created_(false),
246 prune_turn_ports_(allocator->prune_turn_ports()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247 allocator_->network_manager()->SignalNetworksChanged.connect(
248 this, &BasicPortAllocatorSession::OnNetworksChanged);
249 allocator_->network_manager()->StartUpdating();
250}
251
252BasicPortAllocatorSession::~BasicPortAllocatorSession() {
253 allocator_->network_manager()->StopUpdating();
254 if (network_thread_ != NULL)
255 network_thread_->Clear(this);
256
Peter Boström0c4e06b2015-10-07 12:23:21 +0200257 for (uint32_t i = 0; i < sequences_.size(); ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000258 // AllocationSequence should clear it's map entry for turn ports before
259 // ports are destroyed.
260 sequences_[i]->Clear();
261 }
262
263 std::vector<PortData>::iterator it;
264 for (it = ports_.begin(); it != ports_.end(); it++)
265 delete it->port();
266
Peter Boström0c4e06b2015-10-07 12:23:21 +0200267 for (uint32_t i = 0; i < configs_.size(); ++i)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000268 delete configs_[i];
269
Peter Boström0c4e06b2015-10-07 12:23:21 +0200270 for (uint32_t i = 0; i < sequences_.size(); ++i)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000271 delete sequences_[i];
272}
273
Steve Anton7995d8c2017-10-30 16:23:38 -0700274BasicPortAllocator* BasicPortAllocatorSession::allocator() {
275 return allocator_;
276}
277
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700278void BasicPortAllocatorSession::SetCandidateFilter(uint32_t filter) {
279 if (filter == candidate_filter_) {
280 return;
281 }
282 // We assume the filter will only change from "ALL" to something else.
283 RTC_DCHECK(candidate_filter_ == CF_ALL);
284 candidate_filter_ = filter;
285 for (PortData& port : ports_) {
286 if (!port.has_pairable_candidate()) {
287 continue;
288 }
289 const auto& candidates = port.port()->Candidates();
290 // Setting a filter may cause a ready port to become non-ready
291 // if it no longer has any pairable candidates.
292 if (!std::any_of(candidates.begin(), candidates.end(),
293 [this, &port](const Candidate& candidate) {
294 return CandidatePairable(candidate, port.port());
295 })) {
296 port.set_has_pairable_candidate(false);
297 }
298 }
299}
300
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000301void BasicPortAllocatorSession::StartGettingPorts() {
302 network_thread_ = rtc::Thread::Current();
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700303 state_ = SessionState::GATHERING;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000304 if (!socket_factory_) {
305 owned_socket_factory_.reset(
306 new rtc::BasicPacketSocketFactory(network_thread_));
307 socket_factory_ = owned_socket_factory_.get();
308 }
309
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700310 network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_START);
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700311
Mirko Bonadei675513b2017-11-09 11:09:25 +0100312 RTC_LOG(LS_INFO) << "Start getting ports with prune_turn_ports "
313 << (prune_turn_ports_ ? "enabled" : "disabled");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000314}
315
316void BasicPortAllocatorSession::StopGettingPorts() {
nisseede5da42017-01-12 05:15:36 -0800317 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
honghaiz98db68f2015-09-29 07:58:17 -0700318 ClearGettingPorts();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700319 // Note: this must be called after ClearGettingPorts because both may set the
320 // session state and we should set the state to STOPPED.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700321 state_ = SessionState::STOPPED;
honghaiz98db68f2015-09-29 07:58:17 -0700322}
323
324void BasicPortAllocatorSession::ClearGettingPorts() {
nisseede5da42017-01-12 05:15:36 -0800325 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000326 network_thread_->Clear(this, MSG_ALLOCATE);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700327 for (uint32_t i = 0; i < sequences_.size(); ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000328 sequences_[i]->Stop();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700329 }
deadbeefb60a8192016-08-24 15:15:00 -0700330 network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_STOP);
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700331 state_ = SessionState::CLEARED;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700332}
333
Steve Anton7995d8c2017-10-30 16:23:38 -0700334bool BasicPortAllocatorSession::IsGettingPorts() {
335 return state_ == SessionState::GATHERING;
336}
337
338bool BasicPortAllocatorSession::IsCleared() const {
339 return state_ == SessionState::CLEARED;
340}
341
342bool BasicPortAllocatorSession::IsStopped() const {
343 return state_ == SessionState::STOPPED;
344}
345
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700346std::vector<rtc::Network*> BasicPortAllocatorSession::GetFailedNetworks() {
347 std::vector<rtc::Network*> networks = GetNetworks();
348
349 // A network interface may have both IPv4 and IPv6 networks. Only if
350 // neither of the networks has any connections, the network interface
351 // is considered failed and need to be regathered on.
352 std::set<std::string> networks_with_connection;
353 for (const PortData& data : ports_) {
354 Port* port = data.port();
355 if (!port->connections().empty()) {
356 networks_with_connection.insert(port->Network()->name());
357 }
358 }
359
360 networks.erase(
361 std::remove_if(networks.begin(), networks.end(),
362 [networks_with_connection](rtc::Network* network) {
363 // If a network does not have any connection, it is
364 // considered failed.
365 return networks_with_connection.find(network->name()) !=
366 networks_with_connection.end();
367 }),
368 networks.end());
369 return networks;
370}
371
372void BasicPortAllocatorSession::RegatherOnFailedNetworks() {
373 // Find the list of networks that have no connection.
374 std::vector<rtc::Network*> failed_networks = GetFailedNetworks();
375 if (failed_networks.empty()) {
376 return;
377 }
378
Mirko Bonadei675513b2017-11-09 11:09:25 +0100379 RTC_LOG(LS_INFO) << "Regather candidates on failed networks";
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700380
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700381 // Mark a sequence as "network failed" if its network is in the list of failed
382 // networks, so that it won't be considered as equivalent when the session
383 // regathers ports and candidates.
384 for (AllocationSequence* sequence : sequences_) {
385 if (!sequence->network_failed() &&
386 std::find(failed_networks.begin(), failed_networks.end(),
387 sequence->network()) != failed_networks.end()) {
388 sequence->set_network_failed();
389 }
390 }
Steve Anton300bf8e2017-07-14 10:13:10 -0700391
392 bool disable_equivalent_phases = true;
393 Regather(failed_networks, disable_equivalent_phases,
394 IceRegatheringReason::NETWORK_FAILURE);
395}
396
397void BasicPortAllocatorSession::RegatherOnAllNetworks() {
398 std::vector<rtc::Network*> networks = GetNetworks();
399 if (networks.empty()) {
400 return;
401 }
402
Mirko Bonadei675513b2017-11-09 11:09:25 +0100403 RTC_LOG(LS_INFO) << "Regather candidates on all networks";
Steve Anton300bf8e2017-07-14 10:13:10 -0700404
405 // We expect to generate candidates that are equivalent to what we have now.
406 // Force DoAllocate to generate them instead of skipping.
407 bool disable_equivalent_phases = false;
408 Regather(networks, disable_equivalent_phases,
409 IceRegatheringReason::OCCASIONAL_REFRESH);
410}
411
412void BasicPortAllocatorSession::Regather(
413 const std::vector<rtc::Network*>& networks,
414 bool disable_equivalent_phases,
415 IceRegatheringReason reason) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700416 // Remove ports from being used locally and send signaling to remove
417 // the candidates on the remote side.
Steve Anton300bf8e2017-07-14 10:13:10 -0700418 std::vector<PortData*> ports_to_prune = GetUnprunedPorts(networks);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700419 if (!ports_to_prune.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100420 RTC_LOG(LS_INFO) << "Prune " << ports_to_prune.size() << " ports";
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700421 PrunePortsAndRemoveCandidates(ports_to_prune);
422 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700423
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700424 if (allocation_started_ && network_manager_started_ && !IsStopped()) {
Steve Anton300bf8e2017-07-14 10:13:10 -0700425 SignalIceRegathering(this, reason);
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700426
Steve Anton300bf8e2017-07-14 10:13:10 -0700427 DoAllocate(disable_equivalent_phases);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700428 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000429}
430
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800431void BasicPortAllocatorSession::SetStunKeepaliveIntervalForReadyPorts(
432 const rtc::Optional<int>& stun_keepalive_interval) {
433 auto ports = ReadyPorts();
434 for (PortInterface* port : ports) {
Qingsi Wang4ff54432018-03-01 18:25:20 -0800435 // The port type and protocol can be used to identify different subclasses
436 // of Port in the current implementation. Note that a TCPPort has the type
437 // LOCAL_PORT_TYPE but uses the protocol PROTO_TCP.
438 if (port->Type() == STUN_PORT_TYPE ||
439 (port->Type() == LOCAL_PORT_TYPE && port->GetProtocol() == PROTO_UDP)) {
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800440 static_cast<UDPPort*>(port)->set_stun_keepalive_delay(
441 stun_keepalive_interval);
442 }
443 }
444}
445
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700446std::vector<PortInterface*> BasicPortAllocatorSession::ReadyPorts() const {
447 std::vector<PortInterface*> ret;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700448 for (const PortData& data : ports_) {
449 if (data.ready()) {
450 ret.push_back(data.port());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700451 }
452 }
453 return ret;
454}
455
456std::vector<Candidate> BasicPortAllocatorSession::ReadyCandidates() const {
457 std::vector<Candidate> candidates;
458 for (const PortData& data : ports_) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700459 if (!data.ready()) {
460 continue;
461 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700462 GetCandidatesFromPort(data, &candidates);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700463 }
464 return candidates;
465}
466
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700467void BasicPortAllocatorSession::GetCandidatesFromPort(
468 const PortData& data,
469 std::vector<Candidate>* candidates) const {
470 RTC_CHECK(candidates != nullptr);
471 for (const Candidate& candidate : data.port()->Candidates()) {
472 if (!CheckCandidateFilter(candidate)) {
473 continue;
474 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700475 candidates->push_back(SanitizeRelatedAddress(candidate));
476 }
477}
478
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700479Candidate BasicPortAllocatorSession::SanitizeRelatedAddress(
480 const Candidate& c) const {
481 Candidate copy = c;
482 // If adapter enumeration is disabled or host candidates are disabled,
483 // clear the raddr of STUN candidates to avoid local address leakage.
484 bool filter_stun_related_address =
485 ((flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) &&
486 (flags() & PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE)) ||
487 !(candidate_filter_ & CF_HOST);
488 // If the candidate filter doesn't allow reflexive addresses, empty TURN raddr
489 // to avoid reflexive address leakage.
490 bool filter_turn_related_address = !(candidate_filter_ & CF_REFLEXIVE);
491 if ((c.type() == STUN_PORT_TYPE && filter_stun_related_address) ||
492 (c.type() == RELAY_PORT_TYPE && filter_turn_related_address)) {
493 copy.set_related_address(
494 rtc::EmptySocketAddressWithFamily(copy.address().family()));
495 }
496 return copy;
497}
498
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700499bool BasicPortAllocatorSession::CandidatesAllocationDone() const {
500 // Done only if all required AllocationSequence objects
501 // are created.
502 if (!allocation_sequences_created_) {
503 return false;
504 }
505
506 // Check that all port allocation sequences are complete (not running).
507 if (std::any_of(sequences_.begin(), sequences_.end(),
508 [](const AllocationSequence* sequence) {
509 return sequence->state() == AllocationSequence::kRunning;
510 })) {
511 return false;
512 }
513
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700514 // If all allocated ports are no longer gathering, session must have got all
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700515 // expected candidates. Session will trigger candidates allocation complete
516 // signal.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700517 return std::none_of(ports_.begin(), ports_.end(),
518 [](const PortData& port) { return port.inprogress(); });
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700519}
520
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000521void BasicPortAllocatorSession::OnMessage(rtc::Message *message) {
522 switch (message->message_id) {
523 case MSG_CONFIG_START:
nisseede5da42017-01-12 05:15:36 -0800524 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000525 GetPortConfigurations();
526 break;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000527 case MSG_CONFIG_READY:
nisseede5da42017-01-12 05:15:36 -0800528 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000529 OnConfigReady(static_cast<PortConfiguration*>(message->pdata));
530 break;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000531 case MSG_ALLOCATE:
nisseede5da42017-01-12 05:15:36 -0800532 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000533 OnAllocate();
534 break;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000535 case MSG_SEQUENCEOBJECTS_CREATED:
nisseede5da42017-01-12 05:15:36 -0800536 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000537 OnAllocationSequenceObjectsCreated();
538 break;
539 case MSG_CONFIG_STOP:
nisseede5da42017-01-12 05:15:36 -0800540 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000541 OnConfigStop();
542 break;
543 default:
nissec80e7412017-01-11 05:56:46 -0800544 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000545 }
546}
547
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700548void BasicPortAllocatorSession::UpdateIceParametersInternal() {
549 for (PortData& port : ports_) {
550 port.port()->set_content_name(content_name());
551 port.port()->SetIceParameters(component(), ice_ufrag(), ice_pwd());
552 }
553}
554
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000555void BasicPortAllocatorSession::GetPortConfigurations() {
556 PortConfiguration* config = new PortConfiguration(allocator_->stun_servers(),
557 username(),
558 password());
559
deadbeef653b8e02015-11-11 12:55:10 -0800560 for (const RelayServerConfig& turn_server : allocator_->turn_servers()) {
561 config->AddRelay(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000562 }
563 ConfigReady(config);
564}
565
566void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700567 network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_READY, config);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000568}
569
570// Adds a configuration to the list.
571void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) {
deadbeef653b8e02015-11-11 12:55:10 -0800572 if (config) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000573 configs_.push_back(config);
deadbeef653b8e02015-11-11 12:55:10 -0800574 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000575
576 AllocatePorts();
577}
578
579void BasicPortAllocatorSession::OnConfigStop() {
nisseede5da42017-01-12 05:15:36 -0800580 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000581
582 // If any of the allocated ports have not completed the candidates allocation,
583 // mark those as error. Since session doesn't need any new candidates
584 // at this stage of the allocation, it's safe to discard any new candidates.
585 bool send_signal = false;
586 for (std::vector<PortData>::iterator it = ports_.begin();
587 it != ports_.end(); ++it) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700588 if (it->inprogress()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000589 // Updating port state to error, which didn't finish allocating candidates
590 // yet.
591 it->set_error();
592 send_signal = true;
593 }
594 }
595
596 // Did we stop any running sequences?
597 for (std::vector<AllocationSequence*>::iterator it = sequences_.begin();
598 it != sequences_.end() && !send_signal; ++it) {
599 if ((*it)->state() == AllocationSequence::kStopped) {
600 send_signal = true;
601 }
602 }
603
604 // If we stopped anything that was running, send a done signal now.
605 if (send_signal) {
606 MaybeSignalCandidatesAllocationDone();
607 }
608}
609
610void BasicPortAllocatorSession::AllocatePorts() {
nisseede5da42017-01-12 05:15:36 -0800611 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700612 network_thread_->Post(RTC_FROM_HERE, this, MSG_ALLOCATE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000613}
614
615void BasicPortAllocatorSession::OnAllocate() {
Steve Anton300bf8e2017-07-14 10:13:10 -0700616 if (network_manager_started_ && !IsStopped()) {
617 bool disable_equivalent_phases = true;
618 DoAllocate(disable_equivalent_phases);
619 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000620
621 allocation_started_ = true;
622}
623
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700624std::vector<rtc::Network*> BasicPortAllocatorSession::GetNetworks() {
625 std::vector<rtc::Network*> networks;
honghaiz8c404fa2015-09-28 07:59:43 -0700626 rtc::NetworkManager* network_manager = allocator_->network_manager();
nisseede5da42017-01-12 05:15:36 -0800627 RTC_DCHECK(network_manager != nullptr);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700628 // If the network permission state is BLOCKED, we just act as if the flag has
629 // been passed in.
honghaiz8c404fa2015-09-28 07:59:43 -0700630 if (network_manager->enumeration_permission() ==
guoweisea1012b2015-08-21 09:06:28 -0700631 rtc::NetworkManager::ENUMERATION_BLOCKED) {
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700632 set_flags(flags() | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
633 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000634 // If the adapter enumeration is disabled, we'll just bind to any address
635 // instead of specific NIC. This is to ensure the same routing for http
636 // traffic by OS is also used here to avoid any local or public IP leakage
637 // during stun process.
638 if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700639 network_manager->GetAnyAddressNetworks(&networks);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000640 } else {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700641 network_manager->GetNetworks(&networks);
deadbeefe97389c2016-12-23 01:43:45 -0800642 // If network enumeration fails, use the ANY address as a fallback, so we
643 // can at least try gathering candidates using the default route chosen by
deadbeef1ee21252017-06-13 15:49:45 -0700644 // the OS. Or, if the PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS flag is
645 // set, we'll use ANY address candidates either way.
646 if (networks.empty() || flags() & PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS) {
deadbeefe97389c2016-12-23 01:43:45 -0800647 network_manager->GetAnyAddressNetworks(&networks);
648 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000649 }
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100650 // Filter out link-local networks if needed.
651 if (flags() & PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS) {
652 networks.erase(std::remove_if(networks.begin(), networks.end(),
653 [](rtc::Network* network) {
654 return IPIsLinkLocal(network->prefix());
655 }),
656 networks.end());
657 }
deadbeef3427f532017-07-26 16:09:33 -0700658 // Do some more filtering, depending on the network ignore mask and "disable
659 // costly networks" flag.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700660 networks.erase(std::remove_if(networks.begin(), networks.end(),
661 [this](rtc::Network* network) {
662 return allocator_->network_ignore_mask() &
663 network->type();
664 }),
665 networks.end());
honghaiz60347052016-05-31 18:29:12 -0700666 if (flags() & PORTALLOCATOR_DISABLE_COSTLY_NETWORKS) {
667 uint16_t lowest_cost = rtc::kNetworkCostMax;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700668 for (rtc::Network* network : networks) {
Yuwei Huangb181f712018-01-22 17:01:28 -0800669 // Don't determine the lowest cost from a link-local network.
670 // On iOS, a device connected to the computer will get a link-local
671 // network for communicating with the computer, however this network can't
672 // be used to connect to a peer outside the network.
673 if (rtc::IPIsLinkLocal(network->GetBestIP())) {
674 continue;
675 }
honghaiz60347052016-05-31 18:29:12 -0700676 lowest_cost = std::min<uint16_t>(lowest_cost, network->GetCost());
677 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700678 networks.erase(std::remove_if(networks.begin(), networks.end(),
679 [lowest_cost](rtc::Network* network) {
680 return network->GetCost() >
681 lowest_cost + rtc::kNetworkCostLow;
682 }),
683 networks.end());
honghaiz60347052016-05-31 18:29:12 -0700684 }
deadbeef3427f532017-07-26 16:09:33 -0700685 // Lastly, if we have a limit for the number of IPv6 network interfaces (by
686 // default, it's 5), remove networks to ensure that limit is satisfied.
687 //
688 // TODO(deadbeef): Instead of just taking the first N arbitrary IPv6
689 // networks, we could try to choose a set that's "most likely to work". It's
690 // hard to define what that means though; it's not just "lowest cost".
691 // Alternatively, we could just focus on making our ICE pinging logic smarter
692 // such that this filtering isn't necessary in the first place.
693 int ipv6_networks = 0;
694 for (auto it = networks.begin(); it != networks.end();) {
695 if ((*it)->prefix().family() == AF_INET6) {
696 if (ipv6_networks >= allocator_->max_ipv6_networks()) {
697 it = networks.erase(it);
698 continue;
699 } else {
700 ++ipv6_networks;
701 }
702 }
703 ++it;
704 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700705 return networks;
honghaiz8c404fa2015-09-28 07:59:43 -0700706}
707
708// For each network, see if we have a sequence that covers it already. If not,
709// create a new sequence to create the appropriate ports.
Steve Anton300bf8e2017-07-14 10:13:10 -0700710void BasicPortAllocatorSession::DoAllocate(bool disable_equivalent) {
honghaiz8c404fa2015-09-28 07:59:43 -0700711 bool done_signal_needed = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700712 std::vector<rtc::Network*> networks = GetNetworks();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000713 if (networks.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100714 RTC_LOG(LS_WARNING)
715 << "Machine has no networks; no ports will be allocated";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000716 done_signal_needed = true;
717 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100718 RTC_LOG(LS_INFO) << "Allocate ports on " << networks.size() << " networks";
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700719 PortConfiguration* config = configs_.empty() ? nullptr : configs_.back();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200720 for (uint32_t i = 0; i < networks.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200721 uint32_t sequence_flags = flags();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000722 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
723 // If all the ports are disabled we should just fire the allocation
724 // done event and return.
725 done_signal_needed = true;
726 break;
727 }
728
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000729 if (!config || config->relays.empty()) {
730 // No relay ports specified in this config.
731 sequence_flags |= PORTALLOCATOR_DISABLE_RELAY;
732 }
733
734 if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6) &&
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000735 networks[i]->GetBestIP().family() == AF_INET6) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000736 // Skip IPv6 networks unless the flag's been set.
737 continue;
738 }
739
zhihuangb09b3f92017-03-07 14:40:51 -0800740 if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6_ON_WIFI) &&
741 networks[i]->GetBestIP().family() == AF_INET6 &&
742 networks[i]->type() == rtc::ADAPTER_TYPE_WIFI) {
743 // Skip IPv6 Wi-Fi networks unless the flag's been set.
744 continue;
745 }
746
Steve Anton300bf8e2017-07-14 10:13:10 -0700747 if (disable_equivalent) {
748 // Disable phases that would only create ports equivalent to
749 // ones that we have already made.
750 DisableEquivalentPhases(networks[i], config, &sequence_flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000751
Steve Anton300bf8e2017-07-14 10:13:10 -0700752 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
753 // New AllocationSequence would have nothing to do, so don't make it.
754 continue;
755 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000756 }
757
758 AllocationSequence* sequence =
759 new AllocationSequence(this, networks[i], config, sequence_flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000760 sequence->SignalPortAllocationComplete.connect(
761 this, &BasicPortAllocatorSession::OnPortAllocationComplete);
Honghai Zhang5048f572016-08-23 15:47:33 -0700762 sequence->Init();
763 sequence->Start();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000764 sequences_.push_back(sequence);
Honghai Zhang5048f572016-08-23 15:47:33 -0700765 done_signal_needed = true;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000766 }
767 }
768 if (done_signal_needed) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700769 network_thread_->Post(RTC_FROM_HERE, this, MSG_SEQUENCEOBJECTS_CREATED);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000770 }
771}
772
773void BasicPortAllocatorSession::OnNetworksChanged() {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700774 std::vector<rtc::Network*> networks = GetNetworks();
775 std::vector<rtc::Network*> failed_networks;
honghaiz8c404fa2015-09-28 07:59:43 -0700776 for (AllocationSequence* sequence : sequences_) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700777 // Mark the sequence as "network failed" if its network is not in
honghaiz8c404fa2015-09-28 07:59:43 -0700778 // |networks|.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700779 if (!sequence->network_failed() &&
honghaiz8c404fa2015-09-28 07:59:43 -0700780 std::find(networks.begin(), networks.end(), sequence->network()) ==
781 networks.end()) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700782 sequence->OnNetworkFailed();
783 failed_networks.push_back(sequence->network());
honghaiz8c404fa2015-09-28 07:59:43 -0700784 }
785 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700786 std::vector<PortData*> ports_to_prune = GetUnprunedPorts(failed_networks);
787 if (!ports_to_prune.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100788 RTC_LOG(LS_INFO) << "Prune " << ports_to_prune.size()
789 << " ports because their networks were gone";
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700790 PrunePortsAndRemoveCandidates(ports_to_prune);
791 }
honghaiz8c404fa2015-09-28 07:59:43 -0700792
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700793 if (allocation_started_ && !IsStopped()) {
794 if (network_manager_started_) {
795 // If the network manager has started, it must be regathering.
796 SignalIceRegathering(this, IceRegatheringReason::NETWORK_CHANGE);
797 }
Steve Anton300bf8e2017-07-14 10:13:10 -0700798 bool disable_equivalent_phases = true;
799 DoAllocate(disable_equivalent_phases);
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700800 }
801
Honghai Zhang5048f572016-08-23 15:47:33 -0700802 if (!network_manager_started_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100803 RTC_LOG(LS_INFO) << "Network manager has started";
Honghai Zhang5048f572016-08-23 15:47:33 -0700804 network_manager_started_ = true;
805 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000806}
807
808void BasicPortAllocatorSession::DisableEquivalentPhases(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200809 rtc::Network* network,
810 PortConfiguration* config,
811 uint32_t* flags) {
812 for (uint32_t i = 0; i < sequences_.size() &&
813 (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES;
814 ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000815 sequences_[i]->DisableEquivalentPhases(network, config, flags);
816 }
817}
818
819void BasicPortAllocatorSession::AddAllocatedPort(Port* port,
820 AllocationSequence * seq,
821 bool prepare_address) {
822 if (!port)
823 return;
824
Mirko Bonadei675513b2017-11-09 11:09:25 +0100825 RTC_LOG(LS_INFO) << "Adding allocated port for " << content_name();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000826 port->set_content_name(content_name());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700827 port->set_component(component());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000828 port->set_generation(generation());
deadbeeff137e972017-03-23 15:45:49 -0700829 if (allocator_->proxy().type != rtc::PROXY_NONE)
830 port->set_proxy(allocator_->user_agent(), allocator_->proxy());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700831 port->set_send_retransmit_count_attribute(
832 (flags() & PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE) != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000833
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000834 PortData data(port, seq);
835 ports_.push_back(data);
836
837 port->SignalCandidateReady.connect(
838 this, &BasicPortAllocatorSession::OnCandidateReady);
839 port->SignalPortComplete.connect(this,
840 &BasicPortAllocatorSession::OnPortComplete);
841 port->SignalDestroyed.connect(this,
842 &BasicPortAllocatorSession::OnPortDestroyed);
843 port->SignalPortError.connect(
844 this, &BasicPortAllocatorSession::OnPortError);
Jonas Olssond7d762d2018-03-28 09:47:51 +0200845 RTC_LOG(LS_INFO) << port->ToString()
846 << ": Added port to allocator";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000847
848 if (prepare_address)
849 port->PrepareAddress();
850}
851
852void BasicPortAllocatorSession::OnAllocationSequenceObjectsCreated() {
853 allocation_sequences_created_ = true;
854 // Send candidate allocation complete signal if we have no sequences.
855 MaybeSignalCandidatesAllocationDone();
856}
857
858void BasicPortAllocatorSession::OnCandidateReady(
859 Port* port, const Candidate& c) {
nisseede5da42017-01-12 05:15:36 -0800860 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000861 PortData* data = FindPort(port);
nisseede5da42017-01-12 05:15:36 -0800862 RTC_DCHECK(data != NULL);
Jonas Olssond7d762d2018-03-28 09:47:51 +0200863 RTC_LOG(LS_INFO) << port->ToString()
864 << ": Gathered candidate: " << c.ToSensitiveString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000865 // Discarding any candidate signal if port allocation status is
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700866 // already done with gathering.
867 if (!data->inprogress()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100868 RTC_LOG(LS_WARNING)
deadbeefa64edb82016-07-15 14:42:21 -0700869 << "Discarding candidate because port is already done gathering.";
danilchapf4e8cf02016-06-30 01:55:03 -0700870 return;
Honghai Zhang17aac052016-06-29 21:41:53 -0700871 }
Honghai Zhang17aac052016-06-29 21:41:53 -0700872
danilchapf4e8cf02016-06-30 01:55:03 -0700873 // Mark that the port has a pairable candidate, either because we have a
874 // usable candidate from the port, or simply because the port is bound to the
875 // any address and therefore has no host candidate. This will trigger the port
876 // to start creating candidate pairs (connections) and issue connectivity
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700877 // checks. If port has already been marked as having a pairable candidate,
878 // do nothing here.
879 // Note: We should check whether any candidates may become ready after this
880 // because there we will check whether the candidate is generated by the ready
881 // ports, which may include this port.
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700882 bool pruned = false;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700883 if (CandidatePairable(c, port) && !data->has_pairable_candidate()) {
danilchapf4e8cf02016-06-30 01:55:03 -0700884 data->set_has_pairable_candidate(true);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700885
886 if (prune_turn_ports_ && port->Type() == RELAY_PORT_TYPE) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700887 pruned = PruneTurnPorts(port);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700888 }
889 // If the current port is not pruned yet, SignalPortReady.
890 if (!data->pruned()) {
Jonas Olssond7d762d2018-03-28 09:47:51 +0200891 RTC_LOG(LS_INFO) << port->ToString() << ": Port ready.";
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700892 SignalPortReady(this, port);
Honghai Zhanga74363c2016-07-28 18:06:15 -0700893 port->KeepAliveUntilPruned();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700894 }
Honghai Zhang17aac052016-06-29 21:41:53 -0700895 }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700896
deadbeef1c5e6d02017-09-15 17:46:56 -0700897 if (data->ready() && CheckCandidateFilter(c)) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700898 std::vector<Candidate> candidates;
899 candidates.push_back(SanitizeRelatedAddress(c));
900 SignalCandidatesReady(this, candidates);
deadbeefa64edb82016-07-15 14:42:21 -0700901 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100902 RTC_LOG(LS_INFO) << "Discarding candidate because it doesn't match filter.";
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700903 }
904
905 // If we have pruned any port, maybe need to signal port allocation done.
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700906 if (pruned) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700907 MaybeSignalCandidatesAllocationDone();
908 }
909}
910
911Port* BasicPortAllocatorSession::GetBestTurnPortForNetwork(
912 const std::string& network_name) const {
913 Port* best_turn_port = nullptr;
914 for (const PortData& data : ports_) {
915 if (data.port()->Network()->name() == network_name &&
916 data.port()->Type() == RELAY_PORT_TYPE && data.ready() &&
917 (!best_turn_port || ComparePort(data.port(), best_turn_port) > 0)) {
918 best_turn_port = data.port();
919 }
920 }
921 return best_turn_port;
922}
923
924bool BasicPortAllocatorSession::PruneTurnPorts(Port* newly_pairable_turn_port) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700925 // Note: We determine the same network based only on their network names. So
926 // if an IPv4 address and an IPv6 address have the same network name, they
927 // are considered the same network here.
928 const std::string& network_name = newly_pairable_turn_port->Network()->name();
929 Port* best_turn_port = GetBestTurnPortForNetwork(network_name);
930 // |port| is already in the list of ports, so the best port cannot be nullptr.
931 RTC_CHECK(best_turn_port != nullptr);
932
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700933 bool pruned = false;
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700934 std::vector<PortData*> ports_to_prune;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700935 for (PortData& data : ports_) {
936 if (data.port()->Network()->name() == network_name &&
937 data.port()->Type() == RELAY_PORT_TYPE && !data.pruned() &&
938 ComparePort(data.port(), best_turn_port) < 0) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700939 pruned = true;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700940 if (data.port() != newly_pairable_turn_port) {
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700941 // These ports will be pruned in PrunePortsAndRemoveCandidates.
942 ports_to_prune.push_back(&data);
943 } else {
944 data.Prune();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700945 }
946 }
947 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700948
949 if (!ports_to_prune.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100950 RTC_LOG(LS_INFO) << "Prune " << ports_to_prune.size()
951 << " low-priority TURN ports";
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700952 PrunePortsAndRemoveCandidates(ports_to_prune);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700953 }
954 return pruned;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000955}
956
Honghai Zhanga74363c2016-07-28 18:06:15 -0700957void BasicPortAllocatorSession::PruneAllPorts() {
958 for (PortData& data : ports_) {
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700959 data.Prune();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700960 }
961}
962
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000963void BasicPortAllocatorSession::OnPortComplete(Port* port) {
nisseede5da42017-01-12 05:15:36 -0800964 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
Jonas Olssond7d762d2018-03-28 09:47:51 +0200965 RTC_LOG(LS_INFO) << port->ToString()
966 << ": Port completed gathering candidates.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000967 PortData* data = FindPort(port);
nisseede5da42017-01-12 05:15:36 -0800968 RTC_DCHECK(data != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000969
970 // Ignore any late signals.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700971 if (!data->inprogress()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000972 return;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700973 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000974
975 // Moving to COMPLETE state.
976 data->set_complete();
977 // Send candidate allocation complete signal if this was the last port.
978 MaybeSignalCandidatesAllocationDone();
979}
980
981void BasicPortAllocatorSession::OnPortError(Port* port) {
nisseede5da42017-01-12 05:15:36 -0800982 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
Jonas Olssond7d762d2018-03-28 09:47:51 +0200983 RTC_LOG(LS_INFO) << port->ToString()
984 << ": Port encountered error while gathering candidates.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000985 PortData* data = FindPort(port);
nisseede5da42017-01-12 05:15:36 -0800986 RTC_DCHECK(data != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000987 // We might have already given up on this port and stopped it.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700988 if (!data->inprogress()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000989 return;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700990 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991
992 // SignalAddressError is currently sent from StunPort/TurnPort.
993 // But this signal itself is generic.
994 data->set_error();
995 // Send candidate allocation complete signal if this was the last port.
996 MaybeSignalCandidatesAllocationDone();
997}
998
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700999bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) const {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001000 uint32_t filter = candidate_filter_;
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001001
1002 // When binding to any address, before sending packets out, the getsockname
1003 // returns all 0s, but after sending packets, it'll be the NIC used to
1004 // send. All 0s is not a valid ICE candidate address and should be filtered
1005 // out.
1006 if (c.address().IsAnyIP()) {
1007 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001008 }
1009
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001010 if (c.type() == RELAY_PORT_TYPE) {
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +00001011 return ((filter & CF_RELAY) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001012 } else if (c.type() == STUN_PORT_TYPE) {
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +00001013 return ((filter & CF_REFLEXIVE) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001014 } else if (c.type() == LOCAL_PORT_TYPE) {
1015 if ((filter & CF_REFLEXIVE) && !c.address().IsPrivateIP()) {
1016 // We allow host candidates if the filter allows server-reflexive
1017 // candidates and the candidate is a public IP. Because we don't generate
1018 // server-reflexive candidates if they have the same IP as the host
1019 // candidate (i.e. when the host candidate is a public IP), filtering to
1020 // only server-reflexive candidates won't work right when the host
1021 // candidates have public IPs.
1022 return true;
1023 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001024
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +00001025 return ((filter & CF_HOST) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001026 }
1027 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001028}
1029
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001030bool BasicPortAllocatorSession::CandidatePairable(const Candidate& c,
1031 const Port* port) const {
1032 bool candidate_signalable = CheckCandidateFilter(c);
1033
1034 // When device enumeration is disabled (to prevent non-default IP addresses
1035 // from leaking), we ping from some local candidates even though we don't
1036 // signal them. However, if host candidates are also disabled (for example, to
1037 // prevent even default IP addresses from leaking), we still don't want to
1038 // ping from them, even if device enumeration is disabled. Thus, we check for
1039 // both device enumeration and host candidates being disabled.
1040 bool network_enumeration_disabled = c.address().IsAnyIP();
1041 bool can_ping_from_candidate =
1042 (port->SharedSocket() || c.protocol() == TCP_PROTOCOL_NAME);
1043 bool host_candidates_disabled = !(candidate_filter_ & CF_HOST);
1044
1045 return candidate_signalable ||
1046 (network_enumeration_disabled && can_ping_from_candidate &&
1047 !host_candidates_disabled);
1048}
1049
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001050void BasicPortAllocatorSession::OnPortAllocationComplete(
1051 AllocationSequence* seq) {
1052 // Send candidate allocation complete signal if all ports are done.
1053 MaybeSignalCandidatesAllocationDone();
1054}
1055
1056void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001057 if (CandidatesAllocationDone()) {
1058 if (pooled()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001059 RTC_LOG(LS_INFO) << "All candidates gathered for pooled session.";
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001060 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001061 RTC_LOG(LS_INFO) << "All candidates gathered for " << content_name()
1062 << ":" << component() << ":" << generation();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001063 }
1064 SignalCandidatesAllocationDone(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001065 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001066}
1067
1068void BasicPortAllocatorSession::OnPortDestroyed(
1069 PortInterface* port) {
nisseede5da42017-01-12 05:15:36 -08001070 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001071 for (std::vector<PortData>::iterator iter = ports_.begin();
1072 iter != ports_.end(); ++iter) {
1073 if (port == iter->port()) {
1074 ports_.erase(iter);
Jonas Olssond7d762d2018-03-28 09:47:51 +02001075 RTC_LOG(LS_INFO) << port->ToString()
1076 << ": Removed port from allocator ("
1077 << static_cast<int>(ports_.size()) << " remaining)";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001078 return;
1079 }
1080 }
nissec80e7412017-01-11 05:56:46 -08001081 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001082}
1083
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001084BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort(
1085 Port* port) {
1086 for (std::vector<PortData>::iterator it = ports_.begin();
1087 it != ports_.end(); ++it) {
1088 if (it->port() == port) {
1089 return &*it;
1090 }
1091 }
1092 return NULL;
1093}
1094
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001095std::vector<BasicPortAllocatorSession::PortData*>
1096BasicPortAllocatorSession::GetUnprunedPorts(
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001097 const std::vector<rtc::Network*>& networks) {
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001098 std::vector<PortData*> unpruned_ports;
1099 for (PortData& port : ports_) {
1100 if (!port.pruned() &&
1101 std::find(networks.begin(), networks.end(),
1102 port.sequence()->network()) != networks.end()) {
1103 unpruned_ports.push_back(&port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001104 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001105 }
1106 return unpruned_ports;
1107}
1108
1109void BasicPortAllocatorSession::PrunePortsAndRemoveCandidates(
1110 const std::vector<PortData*>& port_data_list) {
1111 std::vector<PortInterface*> pruned_ports;
1112 std::vector<Candidate> removed_candidates;
1113 for (PortData* data : port_data_list) {
Honghai Zhanga74363c2016-07-28 18:06:15 -07001114 // Prune the port so that it may be destroyed.
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001115 data->Prune();
1116 pruned_ports.push_back(data->port());
1117 if (data->has_pairable_candidate()) {
1118 GetCandidatesFromPort(*data, &removed_candidates);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001119 // Mark the port as having no pairable candidates so that its candidates
1120 // won't be removed multiple times.
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001121 data->set_has_pairable_candidate(false);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001122 }
1123 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001124 if (!pruned_ports.empty()) {
1125 SignalPortsPruned(this, pruned_ports);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001126 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001127 if (!removed_candidates.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001128 RTC_LOG(LS_INFO) << "Removed " << removed_candidates.size()
1129 << " candidates";
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001130 SignalCandidatesRemoved(this, removed_candidates);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001131 }
1132}
1133
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001134// AllocationSequence
1135
1136AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session,
1137 rtc::Network* network,
1138 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +02001139 uint32_t flags)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140 : session_(session),
1141 network_(network),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001142 config_(config),
1143 state_(kInit),
1144 flags_(flags),
1145 udp_socket_(),
1146 udp_port_(NULL),
1147 phase_(0) {
1148}
1149
Honghai Zhang5048f572016-08-23 15:47:33 -07001150void AllocationSequence::Init() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001151 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
1152 udp_socket_.reset(session_->socket_factory()->CreateUdpSocket(
deadbeef5c3c1042017-08-04 15:01:57 -07001153 rtc::SocketAddress(network_->GetBestIP(), 0),
1154 session_->allocator()->min_port(), session_->allocator()->max_port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001155 if (udp_socket_) {
1156 udp_socket_->SignalReadPacket.connect(
1157 this, &AllocationSequence::OnReadPacket);
1158 }
1159 // Continuing if |udp_socket_| is NULL, as local TCP and RelayPort using TCP
1160 // are next available options to setup a communication channel.
1161 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001162}
1163
1164void AllocationSequence::Clear() {
1165 udp_port_ = NULL;
Jonas Oreland202994c2017-12-18 12:10:43 +01001166 relay_ports_.clear();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001167}
1168
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001169void AllocationSequence::OnNetworkFailed() {
1170 RTC_DCHECK(!network_failed_);
1171 network_failed_ = true;
1172 // Stop the allocation sequence if its network failed.
honghaiz8c404fa2015-09-28 07:59:43 -07001173 Stop();
honghaiz8c404fa2015-09-28 07:59:43 -07001174}
1175
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001176AllocationSequence::~AllocationSequence() {
1177 session_->network_thread()->Clear(this);
1178}
1179
1180void AllocationSequence::DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +02001181 PortConfiguration* config, uint32_t* flags) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001182 if (network_failed_) {
1183 // If the network of this allocation sequence has ever become failed,
honghaiz8c404fa2015-09-28 07:59:43 -07001184 // it won't be equivalent to the new network.
1185 return;
1186 }
1187
deadbeef5c3c1042017-08-04 15:01:57 -07001188 if (!((network == network_) && (previous_best_ip_ == network->GetBestIP()))) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001189 // Different network setup; nothing is equivalent.
1190 return;
1191 }
1192
1193 // Else turn off the stuff that we've already got covered.
1194
deadbeef1c46a352017-09-27 11:24:05 -07001195 // Every config implicitly specifies local, so turn that off right away if we
1196 // already have a port of the corresponding type. Look for a port that
1197 // matches this AllocationSequence's network, is the right protocol, and
1198 // hasn't encountered an error.
1199 // TODO(deadbeef): This doesn't take into account that there may be another
1200 // AllocationSequence that's ABOUT to allocate a UDP port, but hasn't yet.
1201 // This can happen if, say, there's a network change event right before an
1202 // application-triggered ICE restart. Hopefully this problem will just go
1203 // away if we get rid of the gathering "phases" though, which is planned.
1204 if (std::any_of(session_->ports_.begin(), session_->ports_.end(),
1205 [this](const BasicPortAllocatorSession::PortData& p) {
1206 return p.port()->Network() == network_ &&
1207 p.port()->GetProtocol() == PROTO_UDP && !p.error();
1208 })) {
1209 *flags |= PORTALLOCATOR_DISABLE_UDP;
1210 }
1211 if (std::any_of(session_->ports_.begin(), session_->ports_.end(),
1212 [this](const BasicPortAllocatorSession::PortData& p) {
1213 return p.port()->Network() == network_ &&
1214 p.port()->GetProtocol() == PROTO_TCP && !p.error();
1215 })) {
1216 *flags |= PORTALLOCATOR_DISABLE_TCP;
1217 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001218
1219 if (config_ && config) {
1220 if (config_->StunServers() == config->StunServers()) {
1221 // Already got this STUN servers covered.
1222 *flags |= PORTALLOCATOR_DISABLE_STUN;
1223 }
1224 if (!config_->relays.empty()) {
1225 // Already got relays covered.
1226 // NOTE: This will even skip a _different_ set of relay servers if we
1227 // were to be given one, but that never happens in our codebase. Should
1228 // probably get rid of the list in PortConfiguration and just keep a
1229 // single relay server in each one.
1230 *flags |= PORTALLOCATOR_DISABLE_RELAY;
1231 }
1232 }
1233}
1234
1235void AllocationSequence::Start() {
1236 state_ = kRunning;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001237 session_->network_thread()->Post(RTC_FROM_HERE, this, MSG_ALLOCATION_PHASE);
deadbeef5c3c1042017-08-04 15:01:57 -07001238 // Take a snapshot of the best IP, so that when DisableEquivalentPhases is
1239 // called next time, we enable all phases if the best IP has since changed.
1240 previous_best_ip_ = network_->GetBestIP();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001241}
1242
1243void AllocationSequence::Stop() {
1244 // If the port is completed, don't set it to stopped.
1245 if (state_ == kRunning) {
1246 state_ = kStopped;
1247 session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
1248 }
1249}
1250
1251void AllocationSequence::OnMessage(rtc::Message* msg) {
nisseede5da42017-01-12 05:15:36 -08001252 RTC_DCHECK(rtc::Thread::Current() == session_->network_thread());
1253 RTC_DCHECK(msg->message_id == MSG_ALLOCATION_PHASE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001254
deadbeef1c5e6d02017-09-15 17:46:56 -07001255 const char* const PHASE_NAMES[kNumPhases] = {"Udp", "Relay", "Tcp"};
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001256
1257 // Perform all of the phases in the current step.
Jonas Olssond7d762d2018-03-28 09:47:51 +02001258 RTC_LOG(LS_INFO) << network_->ToString()
1259 << ": Allocation Phase=" << PHASE_NAMES[phase_];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001260
1261 switch (phase_) {
1262 case PHASE_UDP:
1263 CreateUDPPorts();
1264 CreateStunPorts();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001265 break;
1266
1267 case PHASE_RELAY:
1268 CreateRelayPorts();
1269 break;
1270
1271 case PHASE_TCP:
1272 CreateTCPPorts();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001273 state_ = kCompleted;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001274 break;
1275
1276 default:
nissec80e7412017-01-11 05:56:46 -08001277 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001278 }
1279
1280 if (state() == kRunning) {
1281 ++phase_;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001282 session_->network_thread()->PostDelayed(RTC_FROM_HERE,
1283 session_->allocator()->step_delay(),
1284 this, MSG_ALLOCATION_PHASE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001285 } else {
1286 // If all phases in AllocationSequence are completed, no allocation
1287 // steps needed further. Canceling pending signal.
1288 session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
1289 SignalPortAllocationComplete(this);
1290 }
1291}
1292
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001293void AllocationSequence::CreateUDPPorts() {
1294 if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001295 RTC_LOG(LS_VERBOSE) << "AllocationSequence: UDP ports disabled, skipping.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001296 return;
1297 }
1298
1299 // TODO(mallinath) - Remove UDPPort creating socket after shared socket
1300 // is enabled completely.
1301 UDPPort* port = NULL;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001302 bool emit_local_candidate_for_anyaddress =
1303 !IsFlagSet(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001304 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001305 port = UDPPort::Create(
1306 session_->network_thread(), session_->socket_factory(), network_,
1307 udp_socket_.get(), session_->username(), session_->password(),
Qingsi Wang4ff54432018-03-01 18:25:20 -08001308 session_->allocator()->origin(), emit_local_candidate_for_anyaddress,
1309 session_->allocator()->stun_candidate_keepalive_interval());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001310 } else {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001311 port = UDPPort::Create(
deadbeef5c3c1042017-08-04 15:01:57 -07001312 session_->network_thread(), session_->socket_factory(), network_,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001313 session_->allocator()->min_port(), session_->allocator()->max_port(),
1314 session_->username(), session_->password(),
Qingsi Wang4ff54432018-03-01 18:25:20 -08001315 session_->allocator()->origin(), emit_local_candidate_for_anyaddress,
1316 session_->allocator()->stun_candidate_keepalive_interval());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001317 }
1318
1319 if (port) {
1320 // If shared socket is enabled, STUN candidate will be allocated by the
1321 // UDPPort.
1322 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
1323 udp_port_ = port;
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001324 port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001325
1326 // If STUN is not disabled, setting stun server address to port.
1327 if (!IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001328 if (config_ && !config_->StunServers().empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001329 RTC_LOG(LS_INFO)
1330 << "AllocationSequence: UDPPort will be handling the "
Jonas Olssond7d762d2018-03-28 09:47:51 +02001331 "STUN candidate generation.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001332 port->set_server_addresses(config_->StunServers());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001333 }
1334 }
1335 }
1336
1337 session_->AddAllocatedPort(port, this, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001338 }
1339}
1340
1341void AllocationSequence::CreateTCPPorts() {
1342 if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001343 RTC_LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001344 return;
1345 }
1346
deadbeef5c3c1042017-08-04 15:01:57 -07001347 Port* port = TCPPort::Create(
1348 session_->network_thread(), session_->socket_factory(), network_,
1349 session_->allocator()->min_port(), session_->allocator()->max_port(),
1350 session_->username(), session_->password(),
1351 session_->allocator()->allow_tcp_listen());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001352 if (port) {
1353 session_->AddAllocatedPort(port, this, true);
1354 // Since TCPPort is not created using shared socket, |port| will not be
1355 // added to the dequeue.
1356 }
1357}
1358
1359void AllocationSequence::CreateStunPorts() {
1360 if (IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001361 RTC_LOG(LS_VERBOSE) << "AllocationSequence: STUN ports disabled, skipping.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001362 return;
1363 }
1364
1365 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
1366 return;
1367 }
1368
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001369 if (!(config_ && !config_->StunServers().empty())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001370 RTC_LOG(LS_WARNING)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001371 << "AllocationSequence: No STUN server configured, skipping.";
1372 return;
1373 }
1374
deadbeef5c3c1042017-08-04 15:01:57 -07001375 StunPort* port = StunPort::Create(
1376 session_->network_thread(), session_->socket_factory(), network_,
1377 session_->allocator()->min_port(), session_->allocator()->max_port(),
1378 session_->username(), session_->password(), config_->StunServers(),
Qingsi Wang4ff54432018-03-01 18:25:20 -08001379 session_->allocator()->origin(),
1380 session_->allocator()->stun_candidate_keepalive_interval());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001381 if (port) {
1382 session_->AddAllocatedPort(port, this, true);
1383 // Since StunPort is not created using shared socket, |port| will not be
1384 // added to the dequeue.
1385 }
1386}
1387
1388void AllocationSequence::CreateRelayPorts() {
1389 if (IsFlagSet(PORTALLOCATOR_DISABLE_RELAY)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001390 RTC_LOG(LS_VERBOSE)
1391 << "AllocationSequence: Relay ports disabled, skipping.";
1392 return;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001393 }
1394
1395 // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we
1396 // ought to have a relay list for them here.
kwibergee89e782017-08-09 17:22:01 -07001397 RTC_DCHECK(config_);
1398 RTC_DCHECK(!config_->relays.empty());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001399 if (!(config_ && !config_->relays.empty())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001400 RTC_LOG(LS_WARNING)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001401 << "AllocationSequence: No relay server configured, skipping.";
1402 return;
1403 }
1404
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001405 for (RelayServerConfig& relay : config_->relays) {
1406 if (relay.type == RELAY_GTURN) {
1407 CreateGturnPort(relay);
1408 } else if (relay.type == RELAY_TURN) {
1409 CreateTurnPort(relay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001410 } else {
nissec80e7412017-01-11 05:56:46 -08001411 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001412 }
1413 }
1414}
1415
1416void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) {
1417 // TODO(mallinath) - Rename RelayPort to GTurnPort.
deadbeef5c3c1042017-08-04 15:01:57 -07001418 RelayPort* port = RelayPort::Create(
1419 session_->network_thread(), session_->socket_factory(), network_,
1420 session_->allocator()->min_port(), session_->allocator()->max_port(),
1421 config_->username, config_->password);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001422 if (port) {
1423 // Since RelayPort is not created using shared socket, |port| will not be
1424 // added to the dequeue.
1425 // Note: We must add the allocated port before we add addresses because
1426 // the latter will create candidates that need name and preference
1427 // settings. However, we also can't prepare the address (normally
1428 // done by AddAllocatedPort) until we have these addresses. So we
1429 // wait to do that until below.
1430 session_->AddAllocatedPort(port, this, false);
1431
1432 // Add the addresses of this protocol.
1433 PortList::const_iterator relay_port;
1434 for (relay_port = config.ports.begin();
1435 relay_port != config.ports.end();
1436 ++relay_port) {
1437 port->AddServerAddress(*relay_port);
1438 port->AddExternalAddress(*relay_port);
1439 }
1440 // Start fetching an address for this port.
1441 port->PrepareAddress();
1442 }
1443}
1444
1445void AllocationSequence::CreateTurnPort(const RelayServerConfig& config) {
1446 PortList::const_iterator relay_port;
1447 for (relay_port = config.ports.begin();
1448 relay_port != config.ports.end(); ++relay_port) {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001449 // Skip UDP connections to relay servers if it's disallowed.
1450 if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP_RELAY) &&
1451 relay_port->proto == PROTO_UDP) {
1452 continue;
1453 }
1454
Honghai Zhang3d31bd62016-08-10 10:33:05 -07001455 // Do not create a port if the server address family is known and does
1456 // not match the local IP address family.
1457 int server_ip_family = relay_port->address.ipaddr().family();
deadbeef5c3c1042017-08-04 15:01:57 -07001458 int local_ip_family = network_->GetBestIP().family();
Honghai Zhang3d31bd62016-08-10 10:33:05 -07001459 if (server_ip_family != AF_UNSPEC && server_ip_family != local_ip_family) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001460 RTC_LOG(LS_INFO)
1461 << "Server and local address families are not compatible. "
Jonas Olssond7d762d2018-03-28 09:47:51 +02001462 "Server address: " << relay_port->address.ipaddr().ToString()
Mirko Bonadei675513b2017-11-09 11:09:25 +01001463 << " Local address: " << network_->GetBestIP().ToString();
Honghai Zhang3d31bd62016-08-10 10:33:05 -07001464 continue;
1465 }
1466
Jonas Oreland202994c2017-12-18 12:10:43 +01001467 CreateRelayPortArgs args;
1468 args.network_thread = session_->network_thread();
1469 args.socket_factory = session_->socket_factory();
1470 args.network = network_;
1471 args.username = session_->username();
1472 args.password = session_->password();
1473 args.server_address = &(*relay_port);
1474 args.config = &config;
1475 args.origin = session_->allocator()->origin();
1476 args.turn_customizer = session_->allocator()->turn_customizer();
1477
1478 std::unique_ptr<cricket::Port> port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001479 // Shared socket mode must be enabled only for UDP based ports. Hence
1480 // don't pass shared socket for ports which will create TCP sockets.
1481 // TODO(mallinath) - Enable shared socket mode for TURN ports. Disabled
1482 // due to webrtc bug https://code.google.com/p/webrtc/issues/detail?id=3537
1483 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) &&
honghaizf421bdc2015-07-17 16:21:55 -07001484 relay_port->proto == PROTO_UDP && udp_socket_) {
Jonas Oreland202994c2017-12-18 12:10:43 +01001485 port = session_->allocator()->relay_port_factory()->Create(
1486 args, udp_socket_.get());
1487
1488 if (!port) {
1489 RTC_LOG(LS_WARNING)
1490 << "Failed to create relay port with "
1491 << args.server_address->address.ToString();
1492 continue;
1493 }
1494
1495 relay_ports_.push_back(port.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001496 // Listen to the port destroyed signal, to allow AllocationSequence to
1497 // remove entrt from it's map.
1498 port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed);
1499 } else {
Jonas Oreland202994c2017-12-18 12:10:43 +01001500 port = session_->allocator()->relay_port_factory()->Create(
1501 args,
1502 session_->allocator()->min_port(),
1503 session_->allocator()->max_port());
1504
1505 if (!port) {
1506 RTC_LOG(LS_WARNING)
1507 << "Failed to create relay port with "
1508 << args.server_address->address.ToString();
1509 continue;
1510 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001511 }
nisseede5da42017-01-12 05:15:36 -08001512 RTC_DCHECK(port != NULL);
Jonas Oreland202994c2017-12-18 12:10:43 +01001513 session_->AddAllocatedPort(port.release(), this, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001514 }
1515}
1516
1517void AllocationSequence::OnReadPacket(
1518 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
1519 const rtc::SocketAddress& remote_addr,
1520 const rtc::PacketTime& packet_time) {
nisseede5da42017-01-12 05:15:36 -08001521 RTC_DCHECK(socket == udp_socket_.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001522
1523 bool turn_port_found = false;
1524
1525 // Try to find the TurnPort that matches the remote address. Note that the
1526 // message could be a STUN binding response if the TURN server is also used as
1527 // a STUN server. We don't want to parse every message here to check if it is
1528 // a STUN binding response, so we pass the message to TurnPort regardless of
1529 // the message type. The TurnPort will just ignore the message since it will
1530 // not find any request by transaction ID.
Jonas Oreland202994c2017-12-18 12:10:43 +01001531 for (auto* port : relay_ports_) {
1532 if (port->CanHandleIncomingPacketsFrom(remote_addr)) {
Sergey Ulanov17fa6722016-05-10 10:20:47 -07001533 if (port->HandleIncomingPacket(socket, data, size, remote_addr,
1534 packet_time)) {
1535 return;
1536 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001537 turn_port_found = true;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001538 }
1539 }
1540
1541 if (udp_port_) {
1542 const ServerAddresses& stun_servers = udp_port_->server_addresses();
1543
1544 // Pass the packet to the UdpPort if there is no matching TurnPort, or if
1545 // the TURN server is also a STUN server.
1546 if (!turn_port_found ||
1547 stun_servers.find(remote_addr) != stun_servers.end()) {
Sergey Ulanov17fa6722016-05-10 10:20:47 -07001548 RTC_DCHECK(udp_port_->SharedSocket());
1549 udp_port_->HandleIncomingPacket(socket, data, size, remote_addr,
1550 packet_time);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001551 }
1552 }
1553}
1554
1555void AllocationSequence::OnPortDestroyed(PortInterface* port) {
1556 if (udp_port_ == port) {
1557 udp_port_ = NULL;
1558 return;
1559 }
1560
Jonas Oreland202994c2017-12-18 12:10:43 +01001561 auto it = std::find(relay_ports_.begin(), relay_ports_.end(), port);
1562 if (it != relay_ports_.end()) {
1563 relay_ports_.erase(it);
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001564 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001565 RTC_LOG(LS_ERROR) << "Unexpected OnPortDestroyed for nonexistent port.";
nissec80e7412017-01-11 05:56:46 -08001566 RTC_NOTREACHED();
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001567 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001568}
1569
1570// PortConfiguration
1571PortConfiguration::PortConfiguration(
1572 const rtc::SocketAddress& stun_address,
1573 const std::string& username,
1574 const std::string& password)
1575 : stun_address(stun_address), username(username), password(password) {
1576 if (!stun_address.IsNil())
1577 stun_servers.insert(stun_address);
1578}
1579
1580PortConfiguration::PortConfiguration(const ServerAddresses& stun_servers,
1581 const std::string& username,
1582 const std::string& password)
1583 : stun_servers(stun_servers),
1584 username(username),
1585 password(password) {
1586 if (!stun_servers.empty())
1587 stun_address = *(stun_servers.begin());
1588}
1589
Steve Anton7995d8c2017-10-30 16:23:38 -07001590PortConfiguration::~PortConfiguration() = default;
1591
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001592ServerAddresses PortConfiguration::StunServers() {
1593 if (!stun_address.IsNil() &&
1594 stun_servers.find(stun_address) == stun_servers.end()) {
1595 stun_servers.insert(stun_address);
1596 }
deadbeefc5d0d952015-07-16 10:22:21 -07001597 // Every UDP TURN server should also be used as a STUN server.
1598 ServerAddresses turn_servers = GetRelayServerAddresses(RELAY_TURN, PROTO_UDP);
1599 for (const rtc::SocketAddress& turn_server : turn_servers) {
1600 if (stun_servers.find(turn_server) == stun_servers.end()) {
1601 stun_servers.insert(turn_server);
1602 }
1603 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001604 return stun_servers;
1605}
1606
1607void PortConfiguration::AddRelay(const RelayServerConfig& config) {
1608 relays.push_back(config);
1609}
1610
1611bool PortConfiguration::SupportsProtocol(
1612 const RelayServerConfig& relay, ProtocolType type) const {
1613 PortList::const_iterator relay_port;
1614 for (relay_port = relay.ports.begin();
1615 relay_port != relay.ports.end();
1616 ++relay_port) {
1617 if (relay_port->proto == type)
1618 return true;
1619 }
1620 return false;
1621}
1622
1623bool PortConfiguration::SupportsProtocol(RelayType turn_type,
1624 ProtocolType type) const {
1625 for (size_t i = 0; i < relays.size(); ++i) {
1626 if (relays[i].type == turn_type &&
1627 SupportsProtocol(relays[i], type))
1628 return true;
1629 }
1630 return false;
1631}
1632
1633ServerAddresses PortConfiguration::GetRelayServerAddresses(
1634 RelayType turn_type, ProtocolType type) const {
1635 ServerAddresses servers;
1636 for (size_t i = 0; i < relays.size(); ++i) {
1637 if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) {
1638 servers.insert(relays[i].ports.front().address);
1639 }
1640 }
1641 return servers;
1642}
1643
1644} // namespace cricket