blob: a25a937ddb7307e2cdc9cafcf90ad194e7bac372 [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() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700189 CheckRunOnValidThreadIfInitialized();
deadbeef42a42632017-03-10 15:18:00 -0800190 // Our created port allocator sessions depend on us, so destroy our remaining
191 // pooled sessions before anything else.
192 DiscardCandidatePool();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193}
194
Steve Anton7995d8c2017-10-30 16:23:38 -0700195void BasicPortAllocator::SetNetworkIgnoreMask(int network_ignore_mask) {
196 // TODO(phoglund): implement support for other types than loopback.
197 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
198 // Then remove set_network_ignore_list from NetworkManager.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700199 CheckRunOnValidThreadIfInitialized();
Steve Anton7995d8c2017-10-30 16:23:38 -0700200 network_ignore_mask_ = network_ignore_mask;
201}
202
deadbeefc5d0d952015-07-16 10:22:21 -0700203PortAllocatorSession* BasicPortAllocator::CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204 const std::string& content_name, int component,
205 const std::string& ice_ufrag, const std::string& ice_pwd) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700206 CheckRunOnValidThreadAndInitialized();
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700207 PortAllocatorSession* session = new BasicPortAllocatorSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208 this, content_name, component, ice_ufrag, ice_pwd);
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700209 session->SignalIceRegathering.connect(this,
210 &BasicPortAllocator::OnIceRegathering);
211 return session;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212}
213
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700214void BasicPortAllocator::AddTurnServer(const RelayServerConfig& turn_server) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700215 CheckRunOnValidThreadAndInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700216 std::vector<RelayServerConfig> new_turn_servers = turn_servers();
217 new_turn_servers.push_back(turn_server);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700218 SetConfiguration(stun_servers(), new_turn_servers, candidate_pool_size(),
Jonas Orelandbdcee282017-10-10 14:01:40 +0200219 prune_turn_ports(), turn_customizer());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700220}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221
Jonas Oreland202994c2017-12-18 12:10:43 +0100222void BasicPortAllocator::InitRelayPortFactory(
223 RelayPortFactoryInterface* relay_port_factory) {
224 if (relay_port_factory != nullptr) {
225 relay_port_factory_ = relay_port_factory;
226 } else {
227 default_relay_port_factory_.reset(new TurnPortFactory());
228 relay_port_factory_ = default_relay_port_factory_.get();
229 }
230}
231
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000232// BasicPortAllocatorSession
233BasicPortAllocatorSession::BasicPortAllocatorSession(
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700234 BasicPortAllocator* allocator,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235 const std::string& content_name,
236 int component,
237 const std::string& ice_ufrag,
238 const std::string& ice_pwd)
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700239 : PortAllocatorSession(content_name,
240 component,
241 ice_ufrag,
242 ice_pwd,
243 allocator->flags()),
244 allocator_(allocator),
245 network_thread_(NULL),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000246 socket_factory_(allocator->socket_factory()),
247 allocation_started_(false),
248 network_manager_started_(false),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700249 allocation_sequences_created_(false),
250 prune_turn_ports_(allocator->prune_turn_ports()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 allocator_->network_manager()->SignalNetworksChanged.connect(
252 this, &BasicPortAllocatorSession::OnNetworksChanged);
253 allocator_->network_manager()->StartUpdating();
254}
255
256BasicPortAllocatorSession::~BasicPortAllocatorSession() {
257 allocator_->network_manager()->StopUpdating();
258 if (network_thread_ != NULL)
259 network_thread_->Clear(this);
260
Peter Boström0c4e06b2015-10-07 12:23:21 +0200261 for (uint32_t i = 0; i < sequences_.size(); ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 // AllocationSequence should clear it's map entry for turn ports before
263 // ports are destroyed.
264 sequences_[i]->Clear();
265 }
266
267 std::vector<PortData>::iterator it;
268 for (it = ports_.begin(); it != ports_.end(); it++)
269 delete it->port();
270
Peter Boström0c4e06b2015-10-07 12:23:21 +0200271 for (uint32_t i = 0; i < configs_.size(); ++i)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000272 delete configs_[i];
273
Peter Boström0c4e06b2015-10-07 12:23:21 +0200274 for (uint32_t i = 0; i < sequences_.size(); ++i)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275 delete sequences_[i];
276}
277
Steve Anton7995d8c2017-10-30 16:23:38 -0700278BasicPortAllocator* BasicPortAllocatorSession::allocator() {
279 return allocator_;
280}
281
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700282void BasicPortAllocatorSession::SetCandidateFilter(uint32_t filter) {
283 if (filter == candidate_filter_) {
284 return;
285 }
286 // We assume the filter will only change from "ALL" to something else.
287 RTC_DCHECK(candidate_filter_ == CF_ALL);
288 candidate_filter_ = filter;
289 for (PortData& port : ports_) {
290 if (!port.has_pairable_candidate()) {
291 continue;
292 }
293 const auto& candidates = port.port()->Candidates();
294 // Setting a filter may cause a ready port to become non-ready
295 // if it no longer has any pairable candidates.
296 if (!std::any_of(candidates.begin(), candidates.end(),
297 [this, &port](const Candidate& candidate) {
298 return CandidatePairable(candidate, port.port());
299 })) {
300 port.set_has_pairable_candidate(false);
301 }
302 }
303}
304
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000305void BasicPortAllocatorSession::StartGettingPorts() {
306 network_thread_ = rtc::Thread::Current();
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700307 state_ = SessionState::GATHERING;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000308 if (!socket_factory_) {
309 owned_socket_factory_.reset(
310 new rtc::BasicPacketSocketFactory(network_thread_));
311 socket_factory_ = owned_socket_factory_.get();
312 }
313
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700314 network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_START);
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700315
Mirko Bonadei675513b2017-11-09 11:09:25 +0100316 RTC_LOG(LS_INFO) << "Start getting ports with prune_turn_ports "
317 << (prune_turn_ports_ ? "enabled" : "disabled");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000318}
319
320void BasicPortAllocatorSession::StopGettingPorts() {
nisseede5da42017-01-12 05:15:36 -0800321 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
honghaiz98db68f2015-09-29 07:58:17 -0700322 ClearGettingPorts();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700323 // Note: this must be called after ClearGettingPorts because both may set the
324 // session state and we should set the state to STOPPED.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700325 state_ = SessionState::STOPPED;
honghaiz98db68f2015-09-29 07:58:17 -0700326}
327
328void BasicPortAllocatorSession::ClearGettingPorts() {
nisseede5da42017-01-12 05:15:36 -0800329 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000330 network_thread_->Clear(this, MSG_ALLOCATE);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700331 for (uint32_t i = 0; i < sequences_.size(); ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000332 sequences_[i]->Stop();
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700333 }
deadbeefb60a8192016-08-24 15:15:00 -0700334 network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_STOP);
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700335 state_ = SessionState::CLEARED;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700336}
337
Steve Anton7995d8c2017-10-30 16:23:38 -0700338bool BasicPortAllocatorSession::IsGettingPorts() {
339 return state_ == SessionState::GATHERING;
340}
341
342bool BasicPortAllocatorSession::IsCleared() const {
343 return state_ == SessionState::CLEARED;
344}
345
346bool BasicPortAllocatorSession::IsStopped() const {
347 return state_ == SessionState::STOPPED;
348}
349
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700350std::vector<rtc::Network*> BasicPortAllocatorSession::GetFailedNetworks() {
351 std::vector<rtc::Network*> networks = GetNetworks();
352
353 // A network interface may have both IPv4 and IPv6 networks. Only if
354 // neither of the networks has any connections, the network interface
355 // is considered failed and need to be regathered on.
356 std::set<std::string> networks_with_connection;
357 for (const PortData& data : ports_) {
358 Port* port = data.port();
359 if (!port->connections().empty()) {
360 networks_with_connection.insert(port->Network()->name());
361 }
362 }
363
364 networks.erase(
365 std::remove_if(networks.begin(), networks.end(),
366 [networks_with_connection](rtc::Network* network) {
367 // If a network does not have any connection, it is
368 // considered failed.
369 return networks_with_connection.find(network->name()) !=
370 networks_with_connection.end();
371 }),
372 networks.end());
373 return networks;
374}
375
376void BasicPortAllocatorSession::RegatherOnFailedNetworks() {
377 // Find the list of networks that have no connection.
378 std::vector<rtc::Network*> failed_networks = GetFailedNetworks();
379 if (failed_networks.empty()) {
380 return;
381 }
382
Mirko Bonadei675513b2017-11-09 11:09:25 +0100383 RTC_LOG(LS_INFO) << "Regather candidates on failed networks";
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700384
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700385 // Mark a sequence as "network failed" if its network is in the list of failed
386 // networks, so that it won't be considered as equivalent when the session
387 // regathers ports and candidates.
388 for (AllocationSequence* sequence : sequences_) {
389 if (!sequence->network_failed() &&
390 std::find(failed_networks.begin(), failed_networks.end(),
391 sequence->network()) != failed_networks.end()) {
392 sequence->set_network_failed();
393 }
394 }
Steve Anton300bf8e2017-07-14 10:13:10 -0700395
396 bool disable_equivalent_phases = true;
397 Regather(failed_networks, disable_equivalent_phases,
398 IceRegatheringReason::NETWORK_FAILURE);
399}
400
401void BasicPortAllocatorSession::RegatherOnAllNetworks() {
402 std::vector<rtc::Network*> networks = GetNetworks();
403 if (networks.empty()) {
404 return;
405 }
406
Mirko Bonadei675513b2017-11-09 11:09:25 +0100407 RTC_LOG(LS_INFO) << "Regather candidates on all networks";
Steve Anton300bf8e2017-07-14 10:13:10 -0700408
409 // We expect to generate candidates that are equivalent to what we have now.
410 // Force DoAllocate to generate them instead of skipping.
411 bool disable_equivalent_phases = false;
412 Regather(networks, disable_equivalent_phases,
413 IceRegatheringReason::OCCASIONAL_REFRESH);
414}
415
416void BasicPortAllocatorSession::Regather(
417 const std::vector<rtc::Network*>& networks,
418 bool disable_equivalent_phases,
419 IceRegatheringReason reason) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700420 // Remove ports from being used locally and send signaling to remove
421 // the candidates on the remote side.
Steve Anton300bf8e2017-07-14 10:13:10 -0700422 std::vector<PortData*> ports_to_prune = GetUnprunedPorts(networks);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700423 if (!ports_to_prune.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100424 RTC_LOG(LS_INFO) << "Prune " << ports_to_prune.size() << " ports";
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700425 PrunePortsAndRemoveCandidates(ports_to_prune);
426 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700427
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700428 if (allocation_started_ && network_manager_started_ && !IsStopped()) {
Steve Anton300bf8e2017-07-14 10:13:10 -0700429 SignalIceRegathering(this, reason);
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700430
Steve Anton300bf8e2017-07-14 10:13:10 -0700431 DoAllocate(disable_equivalent_phases);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700432 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000433}
434
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800435void BasicPortAllocatorSession::SetStunKeepaliveIntervalForReadyPorts(
436 const rtc::Optional<int>& stun_keepalive_interval) {
437 auto ports = ReadyPorts();
438 for (PortInterface* port : ports) {
Qingsi Wang4ff54432018-03-01 18:25:20 -0800439 // The port type and protocol can be used to identify different subclasses
440 // of Port in the current implementation. Note that a TCPPort has the type
441 // LOCAL_PORT_TYPE but uses the protocol PROTO_TCP.
442 if (port->Type() == STUN_PORT_TYPE ||
443 (port->Type() == LOCAL_PORT_TYPE && port->GetProtocol() == PROTO_UDP)) {
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800444 static_cast<UDPPort*>(port)->set_stun_keepalive_delay(
445 stun_keepalive_interval);
446 }
447 }
448}
449
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700450std::vector<PortInterface*> BasicPortAllocatorSession::ReadyPorts() const {
451 std::vector<PortInterface*> ret;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700452 for (const PortData& data : ports_) {
453 if (data.ready()) {
454 ret.push_back(data.port());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700455 }
456 }
457 return ret;
458}
459
460std::vector<Candidate> BasicPortAllocatorSession::ReadyCandidates() const {
461 std::vector<Candidate> candidates;
462 for (const PortData& data : ports_) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700463 if (!data.ready()) {
464 continue;
465 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700466 GetCandidatesFromPort(data, &candidates);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700467 }
468 return candidates;
469}
470
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700471void BasicPortAllocatorSession::GetCandidatesFromPort(
472 const PortData& data,
473 std::vector<Candidate>* candidates) const {
474 RTC_CHECK(candidates != nullptr);
475 for (const Candidate& candidate : data.port()->Candidates()) {
476 if (!CheckCandidateFilter(candidate)) {
477 continue;
478 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700479 candidates->push_back(SanitizeRelatedAddress(candidate));
480 }
481}
482
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700483Candidate BasicPortAllocatorSession::SanitizeRelatedAddress(
484 const Candidate& c) const {
485 Candidate copy = c;
486 // If adapter enumeration is disabled or host candidates are disabled,
487 // clear the raddr of STUN candidates to avoid local address leakage.
488 bool filter_stun_related_address =
489 ((flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) &&
490 (flags() & PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE)) ||
491 !(candidate_filter_ & CF_HOST);
492 // If the candidate filter doesn't allow reflexive addresses, empty TURN raddr
493 // to avoid reflexive address leakage.
494 bool filter_turn_related_address = !(candidate_filter_ & CF_REFLEXIVE);
495 if ((c.type() == STUN_PORT_TYPE && filter_stun_related_address) ||
496 (c.type() == RELAY_PORT_TYPE && filter_turn_related_address)) {
497 copy.set_related_address(
498 rtc::EmptySocketAddressWithFamily(copy.address().family()));
499 }
500 return copy;
501}
502
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700503bool BasicPortAllocatorSession::CandidatesAllocationDone() const {
504 // Done only if all required AllocationSequence objects
505 // are created.
506 if (!allocation_sequences_created_) {
507 return false;
508 }
509
510 // Check that all port allocation sequences are complete (not running).
511 if (std::any_of(sequences_.begin(), sequences_.end(),
512 [](const AllocationSequence* sequence) {
513 return sequence->state() == AllocationSequence::kRunning;
514 })) {
515 return false;
516 }
517
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700518 // If all allocated ports are no longer gathering, session must have got all
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700519 // expected candidates. Session will trigger candidates allocation complete
520 // signal.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700521 return std::none_of(ports_.begin(), ports_.end(),
522 [](const PortData& port) { return port.inprogress(); });
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700523}
524
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000525void BasicPortAllocatorSession::OnMessage(rtc::Message *message) {
526 switch (message->message_id) {
527 case MSG_CONFIG_START:
nisseede5da42017-01-12 05:15:36 -0800528 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000529 GetPortConfigurations();
530 break;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000531 case MSG_CONFIG_READY:
nisseede5da42017-01-12 05:15:36 -0800532 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000533 OnConfigReady(static_cast<PortConfiguration*>(message->pdata));
534 break;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000535 case MSG_ALLOCATE:
nisseede5da42017-01-12 05:15:36 -0800536 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000537 OnAllocate();
538 break;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000539 case MSG_SEQUENCEOBJECTS_CREATED:
nisseede5da42017-01-12 05:15:36 -0800540 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000541 OnAllocationSequenceObjectsCreated();
542 break;
543 case MSG_CONFIG_STOP:
nisseede5da42017-01-12 05:15:36 -0800544 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000545 OnConfigStop();
546 break;
547 default:
nissec80e7412017-01-11 05:56:46 -0800548 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000549 }
550}
551
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700552void BasicPortAllocatorSession::UpdateIceParametersInternal() {
553 for (PortData& port : ports_) {
554 port.port()->set_content_name(content_name());
555 port.port()->SetIceParameters(component(), ice_ufrag(), ice_pwd());
556 }
557}
558
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000559void BasicPortAllocatorSession::GetPortConfigurations() {
560 PortConfiguration* config = new PortConfiguration(allocator_->stun_servers(),
561 username(),
562 password());
563
deadbeef653b8e02015-11-11 12:55:10 -0800564 for (const RelayServerConfig& turn_server : allocator_->turn_servers()) {
565 config->AddRelay(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000566 }
567 ConfigReady(config);
568}
569
570void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700571 network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_READY, config);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000572}
573
574// Adds a configuration to the list.
575void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) {
deadbeef653b8e02015-11-11 12:55:10 -0800576 if (config) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000577 configs_.push_back(config);
deadbeef653b8e02015-11-11 12:55:10 -0800578 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000579
580 AllocatePorts();
581}
582
583void BasicPortAllocatorSession::OnConfigStop() {
nisseede5da42017-01-12 05:15:36 -0800584 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000585
586 // If any of the allocated ports have not completed the candidates allocation,
587 // mark those as error. Since session doesn't need any new candidates
588 // at this stage of the allocation, it's safe to discard any new candidates.
589 bool send_signal = false;
590 for (std::vector<PortData>::iterator it = ports_.begin();
591 it != ports_.end(); ++it) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700592 if (it->inprogress()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000593 // Updating port state to error, which didn't finish allocating candidates
594 // yet.
595 it->set_error();
596 send_signal = true;
597 }
598 }
599
600 // Did we stop any running sequences?
601 for (std::vector<AllocationSequence*>::iterator it = sequences_.begin();
602 it != sequences_.end() && !send_signal; ++it) {
603 if ((*it)->state() == AllocationSequence::kStopped) {
604 send_signal = true;
605 }
606 }
607
608 // If we stopped anything that was running, send a done signal now.
609 if (send_signal) {
610 MaybeSignalCandidatesAllocationDone();
611 }
612}
613
614void BasicPortAllocatorSession::AllocatePorts() {
nisseede5da42017-01-12 05:15:36 -0800615 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700616 network_thread_->Post(RTC_FROM_HERE, this, MSG_ALLOCATE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000617}
618
619void BasicPortAllocatorSession::OnAllocate() {
Steve Anton300bf8e2017-07-14 10:13:10 -0700620 if (network_manager_started_ && !IsStopped()) {
621 bool disable_equivalent_phases = true;
622 DoAllocate(disable_equivalent_phases);
623 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000624
625 allocation_started_ = true;
626}
627
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700628std::vector<rtc::Network*> BasicPortAllocatorSession::GetNetworks() {
629 std::vector<rtc::Network*> networks;
honghaiz8c404fa2015-09-28 07:59:43 -0700630 rtc::NetworkManager* network_manager = allocator_->network_manager();
nisseede5da42017-01-12 05:15:36 -0800631 RTC_DCHECK(network_manager != nullptr);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700632 // If the network permission state is BLOCKED, we just act as if the flag has
633 // been passed in.
honghaiz8c404fa2015-09-28 07:59:43 -0700634 if (network_manager->enumeration_permission() ==
guoweisea1012b2015-08-21 09:06:28 -0700635 rtc::NetworkManager::ENUMERATION_BLOCKED) {
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700636 set_flags(flags() | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
637 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000638 // If the adapter enumeration is disabled, we'll just bind to any address
639 // instead of specific NIC. This is to ensure the same routing for http
640 // traffic by OS is also used here to avoid any local or public IP leakage
641 // during stun process.
642 if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700643 network_manager->GetAnyAddressNetworks(&networks);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000644 } else {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700645 network_manager->GetNetworks(&networks);
deadbeefe97389c2016-12-23 01:43:45 -0800646 // If network enumeration fails, use the ANY address as a fallback, so we
647 // can at least try gathering candidates using the default route chosen by
deadbeef1ee21252017-06-13 15:49:45 -0700648 // the OS. Or, if the PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS flag is
649 // set, we'll use ANY address candidates either way.
650 if (networks.empty() || flags() & PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS) {
deadbeefe97389c2016-12-23 01:43:45 -0800651 network_manager->GetAnyAddressNetworks(&networks);
652 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000653 }
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100654 // Filter out link-local networks if needed.
655 if (flags() & PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS) {
656 networks.erase(std::remove_if(networks.begin(), networks.end(),
657 [](rtc::Network* network) {
658 return IPIsLinkLocal(network->prefix());
659 }),
660 networks.end());
661 }
deadbeef3427f532017-07-26 16:09:33 -0700662 // Do some more filtering, depending on the network ignore mask and "disable
663 // costly networks" flag.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700664 networks.erase(std::remove_if(networks.begin(), networks.end(),
665 [this](rtc::Network* network) {
666 return allocator_->network_ignore_mask() &
667 network->type();
668 }),
669 networks.end());
honghaiz60347052016-05-31 18:29:12 -0700670 if (flags() & PORTALLOCATOR_DISABLE_COSTLY_NETWORKS) {
671 uint16_t lowest_cost = rtc::kNetworkCostMax;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700672 for (rtc::Network* network : networks) {
Yuwei Huangb181f712018-01-22 17:01:28 -0800673 // Don't determine the lowest cost from a link-local network.
674 // On iOS, a device connected to the computer will get a link-local
675 // network for communicating with the computer, however this network can't
676 // be used to connect to a peer outside the network.
677 if (rtc::IPIsLinkLocal(network->GetBestIP())) {
678 continue;
679 }
honghaiz60347052016-05-31 18:29:12 -0700680 lowest_cost = std::min<uint16_t>(lowest_cost, network->GetCost());
681 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700682 networks.erase(std::remove_if(networks.begin(), networks.end(),
683 [lowest_cost](rtc::Network* network) {
684 return network->GetCost() >
685 lowest_cost + rtc::kNetworkCostLow;
686 }),
687 networks.end());
honghaiz60347052016-05-31 18:29:12 -0700688 }
deadbeef3427f532017-07-26 16:09:33 -0700689 // Lastly, if we have a limit for the number of IPv6 network interfaces (by
690 // default, it's 5), remove networks to ensure that limit is satisfied.
691 //
692 // TODO(deadbeef): Instead of just taking the first N arbitrary IPv6
693 // networks, we could try to choose a set that's "most likely to work". It's
694 // hard to define what that means though; it's not just "lowest cost".
695 // Alternatively, we could just focus on making our ICE pinging logic smarter
696 // such that this filtering isn't necessary in the first place.
697 int ipv6_networks = 0;
698 for (auto it = networks.begin(); it != networks.end();) {
699 if ((*it)->prefix().family() == AF_INET6) {
700 if (ipv6_networks >= allocator_->max_ipv6_networks()) {
701 it = networks.erase(it);
702 continue;
703 } else {
704 ++ipv6_networks;
705 }
706 }
707 ++it;
708 }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700709 return networks;
honghaiz8c404fa2015-09-28 07:59:43 -0700710}
711
712// For each network, see if we have a sequence that covers it already. If not,
713// create a new sequence to create the appropriate ports.
Steve Anton300bf8e2017-07-14 10:13:10 -0700714void BasicPortAllocatorSession::DoAllocate(bool disable_equivalent) {
honghaiz8c404fa2015-09-28 07:59:43 -0700715 bool done_signal_needed = false;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700716 std::vector<rtc::Network*> networks = GetNetworks();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000717 if (networks.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100718 RTC_LOG(LS_WARNING)
719 << "Machine has no networks; no ports will be allocated";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000720 done_signal_needed = true;
721 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100722 RTC_LOG(LS_INFO) << "Allocate ports on " << networks.size() << " networks";
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700723 PortConfiguration* config = configs_.empty() ? nullptr : configs_.back();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200724 for (uint32_t i = 0; i < networks.size(); ++i) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200725 uint32_t sequence_flags = flags();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000726 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
727 // If all the ports are disabled we should just fire the allocation
728 // done event and return.
729 done_signal_needed = true;
730 break;
731 }
732
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000733 if (!config || config->relays.empty()) {
734 // No relay ports specified in this config.
735 sequence_flags |= PORTALLOCATOR_DISABLE_RELAY;
736 }
737
738 if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6) &&
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000739 networks[i]->GetBestIP().family() == AF_INET6) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000740 // Skip IPv6 networks unless the flag's been set.
741 continue;
742 }
743
zhihuangb09b3f92017-03-07 14:40:51 -0800744 if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6_ON_WIFI) &&
745 networks[i]->GetBestIP().family() == AF_INET6 &&
746 networks[i]->type() == rtc::ADAPTER_TYPE_WIFI) {
747 // Skip IPv6 Wi-Fi networks unless the flag's been set.
748 continue;
749 }
750
Steve Anton300bf8e2017-07-14 10:13:10 -0700751 if (disable_equivalent) {
752 // Disable phases that would only create ports equivalent to
753 // ones that we have already made.
754 DisableEquivalentPhases(networks[i], config, &sequence_flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000755
Steve Anton300bf8e2017-07-14 10:13:10 -0700756 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
757 // New AllocationSequence would have nothing to do, so don't make it.
758 continue;
759 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000760 }
761
762 AllocationSequence* sequence =
763 new AllocationSequence(this, networks[i], config, sequence_flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000764 sequence->SignalPortAllocationComplete.connect(
765 this, &BasicPortAllocatorSession::OnPortAllocationComplete);
Honghai Zhang5048f572016-08-23 15:47:33 -0700766 sequence->Init();
767 sequence->Start();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000768 sequences_.push_back(sequence);
Honghai Zhang5048f572016-08-23 15:47:33 -0700769 done_signal_needed = true;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000770 }
771 }
772 if (done_signal_needed) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700773 network_thread_->Post(RTC_FROM_HERE, this, MSG_SEQUENCEOBJECTS_CREATED);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000774 }
775}
776
777void BasicPortAllocatorSession::OnNetworksChanged() {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700778 std::vector<rtc::Network*> networks = GetNetworks();
779 std::vector<rtc::Network*> failed_networks;
honghaiz8c404fa2015-09-28 07:59:43 -0700780 for (AllocationSequence* sequence : sequences_) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700781 // Mark the sequence as "network failed" if its network is not in
honghaiz8c404fa2015-09-28 07:59:43 -0700782 // |networks|.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700783 if (!sequence->network_failed() &&
honghaiz8c404fa2015-09-28 07:59:43 -0700784 std::find(networks.begin(), networks.end(), sequence->network()) ==
785 networks.end()) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700786 sequence->OnNetworkFailed();
787 failed_networks.push_back(sequence->network());
honghaiz8c404fa2015-09-28 07:59:43 -0700788 }
789 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700790 std::vector<PortData*> ports_to_prune = GetUnprunedPorts(failed_networks);
791 if (!ports_to_prune.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100792 RTC_LOG(LS_INFO) << "Prune " << ports_to_prune.size()
793 << " ports because their networks were gone";
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700794 PrunePortsAndRemoveCandidates(ports_to_prune);
795 }
honghaiz8c404fa2015-09-28 07:59:43 -0700796
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700797 if (allocation_started_ && !IsStopped()) {
798 if (network_manager_started_) {
799 // If the network manager has started, it must be regathering.
800 SignalIceRegathering(this, IceRegatheringReason::NETWORK_CHANGE);
801 }
Steve Anton300bf8e2017-07-14 10:13:10 -0700802 bool disable_equivalent_phases = true;
803 DoAllocate(disable_equivalent_phases);
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700804 }
805
Honghai Zhang5048f572016-08-23 15:47:33 -0700806 if (!network_manager_started_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100807 RTC_LOG(LS_INFO) << "Network manager has started";
Honghai Zhang5048f572016-08-23 15:47:33 -0700808 network_manager_started_ = true;
809 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000810}
811
812void BasicPortAllocatorSession::DisableEquivalentPhases(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200813 rtc::Network* network,
814 PortConfiguration* config,
815 uint32_t* flags) {
816 for (uint32_t i = 0; i < sequences_.size() &&
817 (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES;
818 ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000819 sequences_[i]->DisableEquivalentPhases(network, config, flags);
820 }
821}
822
823void BasicPortAllocatorSession::AddAllocatedPort(Port* port,
824 AllocationSequence * seq,
825 bool prepare_address) {
826 if (!port)
827 return;
828
Mirko Bonadei675513b2017-11-09 11:09:25 +0100829 RTC_LOG(LS_INFO) << "Adding allocated port for " << content_name();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000830 port->set_content_name(content_name());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700831 port->set_component(component());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000832 port->set_generation(generation());
deadbeeff137e972017-03-23 15:45:49 -0700833 if (allocator_->proxy().type != rtc::PROXY_NONE)
834 port->set_proxy(allocator_->user_agent(), allocator_->proxy());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700835 port->set_send_retransmit_count_attribute(
836 (flags() & PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE) != 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000837
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000838 PortData data(port, seq);
839 ports_.push_back(data);
840
841 port->SignalCandidateReady.connect(
842 this, &BasicPortAllocatorSession::OnCandidateReady);
843 port->SignalPortComplete.connect(this,
844 &BasicPortAllocatorSession::OnPortComplete);
845 port->SignalDestroyed.connect(this,
846 &BasicPortAllocatorSession::OnPortDestroyed);
847 port->SignalPortError.connect(
848 this, &BasicPortAllocatorSession::OnPortError);
Jonas Olssond7d762d2018-03-28 09:47:51 +0200849 RTC_LOG(LS_INFO) << port->ToString()
850 << ": Added port to allocator";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000851
852 if (prepare_address)
853 port->PrepareAddress();
854}
855
856void BasicPortAllocatorSession::OnAllocationSequenceObjectsCreated() {
857 allocation_sequences_created_ = true;
858 // Send candidate allocation complete signal if we have no sequences.
859 MaybeSignalCandidatesAllocationDone();
860}
861
862void BasicPortAllocatorSession::OnCandidateReady(
863 Port* port, const Candidate& c) {
nisseede5da42017-01-12 05:15:36 -0800864 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000865 PortData* data = FindPort(port);
nisseede5da42017-01-12 05:15:36 -0800866 RTC_DCHECK(data != NULL);
Jonas Olssond7d762d2018-03-28 09:47:51 +0200867 RTC_LOG(LS_INFO) << port->ToString()
868 << ": Gathered candidate: " << c.ToSensitiveString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000869 // Discarding any candidate signal if port allocation status is
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700870 // already done with gathering.
871 if (!data->inprogress()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100872 RTC_LOG(LS_WARNING)
deadbeefa64edb82016-07-15 14:42:21 -0700873 << "Discarding candidate because port is already done gathering.";
danilchapf4e8cf02016-06-30 01:55:03 -0700874 return;
Honghai Zhang17aac052016-06-29 21:41:53 -0700875 }
Honghai Zhang17aac052016-06-29 21:41:53 -0700876
danilchapf4e8cf02016-06-30 01:55:03 -0700877 // Mark that the port has a pairable candidate, either because we have a
878 // usable candidate from the port, or simply because the port is bound to the
879 // any address and therefore has no host candidate. This will trigger the port
880 // to start creating candidate pairs (connections) and issue connectivity
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700881 // checks. If port has already been marked as having a pairable candidate,
882 // do nothing here.
883 // Note: We should check whether any candidates may become ready after this
884 // because there we will check whether the candidate is generated by the ready
885 // ports, which may include this port.
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700886 bool pruned = false;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700887 if (CandidatePairable(c, port) && !data->has_pairable_candidate()) {
danilchapf4e8cf02016-06-30 01:55:03 -0700888 data->set_has_pairable_candidate(true);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700889
890 if (prune_turn_ports_ && port->Type() == RELAY_PORT_TYPE) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700891 pruned = PruneTurnPorts(port);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700892 }
893 // If the current port is not pruned yet, SignalPortReady.
894 if (!data->pruned()) {
Jonas Olssond7d762d2018-03-28 09:47:51 +0200895 RTC_LOG(LS_INFO) << port->ToString() << ": Port ready.";
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700896 SignalPortReady(this, port);
Honghai Zhanga74363c2016-07-28 18:06:15 -0700897 port->KeepAliveUntilPruned();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700898 }
Honghai Zhang17aac052016-06-29 21:41:53 -0700899 }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700900
deadbeef1c5e6d02017-09-15 17:46:56 -0700901 if (data->ready() && CheckCandidateFilter(c)) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700902 std::vector<Candidate> candidates;
903 candidates.push_back(SanitizeRelatedAddress(c));
904 SignalCandidatesReady(this, candidates);
deadbeefa64edb82016-07-15 14:42:21 -0700905 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100906 RTC_LOG(LS_INFO) << "Discarding candidate because it doesn't match filter.";
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700907 }
908
909 // If we have pruned any port, maybe need to signal port allocation done.
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700910 if (pruned) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700911 MaybeSignalCandidatesAllocationDone();
912 }
913}
914
915Port* BasicPortAllocatorSession::GetBestTurnPortForNetwork(
916 const std::string& network_name) const {
917 Port* best_turn_port = nullptr;
918 for (const PortData& data : ports_) {
919 if (data.port()->Network()->name() == network_name &&
920 data.port()->Type() == RELAY_PORT_TYPE && data.ready() &&
921 (!best_turn_port || ComparePort(data.port(), best_turn_port) > 0)) {
922 best_turn_port = data.port();
923 }
924 }
925 return best_turn_port;
926}
927
928bool BasicPortAllocatorSession::PruneTurnPorts(Port* newly_pairable_turn_port) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700929 // Note: We determine the same network based only on their network names. So
930 // if an IPv4 address and an IPv6 address have the same network name, they
931 // are considered the same network here.
932 const std::string& network_name = newly_pairable_turn_port->Network()->name();
933 Port* best_turn_port = GetBestTurnPortForNetwork(network_name);
934 // |port| is already in the list of ports, so the best port cannot be nullptr.
935 RTC_CHECK(best_turn_port != nullptr);
936
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700937 bool pruned = false;
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700938 std::vector<PortData*> ports_to_prune;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700939 for (PortData& data : ports_) {
940 if (data.port()->Network()->name() == network_name &&
941 data.port()->Type() == RELAY_PORT_TYPE && !data.pruned() &&
942 ComparePort(data.port(), best_turn_port) < 0) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700943 pruned = true;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700944 if (data.port() != newly_pairable_turn_port) {
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700945 // These ports will be pruned in PrunePortsAndRemoveCandidates.
946 ports_to_prune.push_back(&data);
947 } else {
948 data.Prune();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700949 }
950 }
951 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700952
953 if (!ports_to_prune.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100954 RTC_LOG(LS_INFO) << "Prune " << ports_to_prune.size()
955 << " low-priority TURN ports";
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700956 PrunePortsAndRemoveCandidates(ports_to_prune);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700957 }
958 return pruned;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000959}
960
Honghai Zhanga74363c2016-07-28 18:06:15 -0700961void BasicPortAllocatorSession::PruneAllPorts() {
962 for (PortData& data : ports_) {
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700963 data.Prune();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700964 }
965}
966
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000967void BasicPortAllocatorSession::OnPortComplete(Port* port) {
nisseede5da42017-01-12 05:15:36 -0800968 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
Jonas Olssond7d762d2018-03-28 09:47:51 +0200969 RTC_LOG(LS_INFO) << port->ToString()
970 << ": Port completed gathering candidates.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000971 PortData* data = FindPort(port);
nisseede5da42017-01-12 05:15:36 -0800972 RTC_DCHECK(data != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000973
974 // Ignore any late signals.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700975 if (!data->inprogress()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000976 return;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700977 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000978
979 // Moving to COMPLETE state.
980 data->set_complete();
981 // Send candidate allocation complete signal if this was the last port.
982 MaybeSignalCandidatesAllocationDone();
983}
984
985void BasicPortAllocatorSession::OnPortError(Port* port) {
nisseede5da42017-01-12 05:15:36 -0800986 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
Jonas Olssond7d762d2018-03-28 09:47:51 +0200987 RTC_LOG(LS_INFO) << port->ToString()
988 << ": Port encountered error while gathering candidates.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000989 PortData* data = FindPort(port);
nisseede5da42017-01-12 05:15:36 -0800990 RTC_DCHECK(data != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991 // We might have already given up on this port and stopped it.
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700992 if (!data->inprogress()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000993 return;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700994 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000995
996 // SignalAddressError is currently sent from StunPort/TurnPort.
997 // But this signal itself is generic.
998 data->set_error();
999 // Send candidate allocation complete signal if this was the last port.
1000 MaybeSignalCandidatesAllocationDone();
1001}
1002
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001003bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) const {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001004 uint32_t filter = candidate_filter_;
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001005
1006 // When binding to any address, before sending packets out, the getsockname
1007 // returns all 0s, but after sending packets, it'll be the NIC used to
1008 // send. All 0s is not a valid ICE candidate address and should be filtered
1009 // out.
1010 if (c.address().IsAnyIP()) {
1011 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001012 }
1013
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001014 if (c.type() == RELAY_PORT_TYPE) {
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +00001015 return ((filter & CF_RELAY) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001016 } else if (c.type() == STUN_PORT_TYPE) {
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +00001017 return ((filter & CF_REFLEXIVE) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001018 } else if (c.type() == LOCAL_PORT_TYPE) {
1019 if ((filter & CF_REFLEXIVE) && !c.address().IsPrivateIP()) {
1020 // We allow host candidates if the filter allows server-reflexive
1021 // candidates and the candidate is a public IP. Because we don't generate
1022 // server-reflexive candidates if they have the same IP as the host
1023 // candidate (i.e. when the host candidate is a public IP), filtering to
1024 // only server-reflexive candidates won't work right when the host
1025 // candidates have public IPs.
1026 return true;
1027 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001028
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +00001029 return ((filter & CF_HOST) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001030 }
1031 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001032}
1033
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001034bool BasicPortAllocatorSession::CandidatePairable(const Candidate& c,
1035 const Port* port) const {
1036 bool candidate_signalable = CheckCandidateFilter(c);
1037
1038 // When device enumeration is disabled (to prevent non-default IP addresses
1039 // from leaking), we ping from some local candidates even though we don't
1040 // signal them. However, if host candidates are also disabled (for example, to
1041 // prevent even default IP addresses from leaking), we still don't want to
1042 // ping from them, even if device enumeration is disabled. Thus, we check for
1043 // both device enumeration and host candidates being disabled.
1044 bool network_enumeration_disabled = c.address().IsAnyIP();
1045 bool can_ping_from_candidate =
1046 (port->SharedSocket() || c.protocol() == TCP_PROTOCOL_NAME);
1047 bool host_candidates_disabled = !(candidate_filter_ & CF_HOST);
1048
1049 return candidate_signalable ||
1050 (network_enumeration_disabled && can_ping_from_candidate &&
1051 !host_candidates_disabled);
1052}
1053
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001054void BasicPortAllocatorSession::OnPortAllocationComplete(
1055 AllocationSequence* seq) {
1056 // Send candidate allocation complete signal if all ports are done.
1057 MaybeSignalCandidatesAllocationDone();
1058}
1059
1060void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001061 if (CandidatesAllocationDone()) {
1062 if (pooled()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001063 RTC_LOG(LS_INFO) << "All candidates gathered for pooled session.";
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001064 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001065 RTC_LOG(LS_INFO) << "All candidates gathered for " << content_name()
1066 << ":" << component() << ":" << generation();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001067 }
1068 SignalCandidatesAllocationDone(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001069 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001070}
1071
1072void BasicPortAllocatorSession::OnPortDestroyed(
1073 PortInterface* port) {
nisseede5da42017-01-12 05:15:36 -08001074 RTC_DCHECK(rtc::Thread::Current() == network_thread_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001075 for (std::vector<PortData>::iterator iter = ports_.begin();
1076 iter != ports_.end(); ++iter) {
1077 if (port == iter->port()) {
1078 ports_.erase(iter);
Jonas Olssond7d762d2018-03-28 09:47:51 +02001079 RTC_LOG(LS_INFO) << port->ToString()
1080 << ": Removed port from allocator ("
1081 << static_cast<int>(ports_.size()) << " remaining)";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001082 return;
1083 }
1084 }
nissec80e7412017-01-11 05:56:46 -08001085 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001086}
1087
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001088BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort(
1089 Port* port) {
1090 for (std::vector<PortData>::iterator it = ports_.begin();
1091 it != ports_.end(); ++it) {
1092 if (it->port() == port) {
1093 return &*it;
1094 }
1095 }
1096 return NULL;
1097}
1098
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001099std::vector<BasicPortAllocatorSession::PortData*>
1100BasicPortAllocatorSession::GetUnprunedPorts(
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001101 const std::vector<rtc::Network*>& networks) {
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001102 std::vector<PortData*> unpruned_ports;
1103 for (PortData& port : ports_) {
1104 if (!port.pruned() &&
1105 std::find(networks.begin(), networks.end(),
1106 port.sequence()->network()) != networks.end()) {
1107 unpruned_ports.push_back(&port);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001108 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001109 }
1110 return unpruned_ports;
1111}
1112
1113void BasicPortAllocatorSession::PrunePortsAndRemoveCandidates(
1114 const std::vector<PortData*>& port_data_list) {
1115 std::vector<PortInterface*> pruned_ports;
1116 std::vector<Candidate> removed_candidates;
1117 for (PortData* data : port_data_list) {
Honghai Zhanga74363c2016-07-28 18:06:15 -07001118 // Prune the port so that it may be destroyed.
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001119 data->Prune();
1120 pruned_ports.push_back(data->port());
1121 if (data->has_pairable_candidate()) {
1122 GetCandidatesFromPort(*data, &removed_candidates);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001123 // Mark the port as having no pairable candidates so that its candidates
1124 // won't be removed multiple times.
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001125 data->set_has_pairable_candidate(false);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001126 }
1127 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001128 if (!pruned_ports.empty()) {
1129 SignalPortsPruned(this, pruned_ports);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001130 }
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001131 if (!removed_candidates.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001132 RTC_LOG(LS_INFO) << "Removed " << removed_candidates.size()
1133 << " candidates";
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001134 SignalCandidatesRemoved(this, removed_candidates);
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001135 }
1136}
1137
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001138// AllocationSequence
1139
1140AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session,
1141 rtc::Network* network,
1142 PortConfiguration* config,
Peter Boström0c4e06b2015-10-07 12:23:21 +02001143 uint32_t flags)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001144 : session_(session),
1145 network_(network),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001146 config_(config),
1147 state_(kInit),
1148 flags_(flags),
1149 udp_socket_(),
1150 udp_port_(NULL),
1151 phase_(0) {
1152}
1153
Honghai Zhang5048f572016-08-23 15:47:33 -07001154void AllocationSequence::Init() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001155 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
1156 udp_socket_.reset(session_->socket_factory()->CreateUdpSocket(
deadbeef5c3c1042017-08-04 15:01:57 -07001157 rtc::SocketAddress(network_->GetBestIP(), 0),
1158 session_->allocator()->min_port(), session_->allocator()->max_port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159 if (udp_socket_) {
1160 udp_socket_->SignalReadPacket.connect(
1161 this, &AllocationSequence::OnReadPacket);
1162 }
1163 // Continuing if |udp_socket_| is NULL, as local TCP and RelayPort using TCP
1164 // are next available options to setup a communication channel.
1165 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001166}
1167
1168void AllocationSequence::Clear() {
1169 udp_port_ = NULL;
Jonas Oreland202994c2017-12-18 12:10:43 +01001170 relay_ports_.clear();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001171}
1172
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001173void AllocationSequence::OnNetworkFailed() {
1174 RTC_DCHECK(!network_failed_);
1175 network_failed_ = true;
1176 // Stop the allocation sequence if its network failed.
honghaiz8c404fa2015-09-28 07:59:43 -07001177 Stop();
honghaiz8c404fa2015-09-28 07:59:43 -07001178}
1179
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001180AllocationSequence::~AllocationSequence() {
1181 session_->network_thread()->Clear(this);
1182}
1183
1184void AllocationSequence::DisableEquivalentPhases(rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +02001185 PortConfiguration* config, uint32_t* flags) {
Honghai Zhang5622c5e2016-07-01 13:59:29 -07001186 if (network_failed_) {
1187 // If the network of this allocation sequence has ever become failed,
honghaiz8c404fa2015-09-28 07:59:43 -07001188 // it won't be equivalent to the new network.
1189 return;
1190 }
1191
deadbeef5c3c1042017-08-04 15:01:57 -07001192 if (!((network == network_) && (previous_best_ip_ == network->GetBestIP()))) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001193 // Different network setup; nothing is equivalent.
1194 return;
1195 }
1196
1197 // Else turn off the stuff that we've already got covered.
1198
deadbeef1c46a352017-09-27 11:24:05 -07001199 // Every config implicitly specifies local, so turn that off right away if we
1200 // already have a port of the corresponding type. Look for a port that
1201 // matches this AllocationSequence's network, is the right protocol, and
1202 // hasn't encountered an error.
1203 // TODO(deadbeef): This doesn't take into account that there may be another
1204 // AllocationSequence that's ABOUT to allocate a UDP port, but hasn't yet.
1205 // This can happen if, say, there's a network change event right before an
1206 // application-triggered ICE restart. Hopefully this problem will just go
1207 // away if we get rid of the gathering "phases" though, which is planned.
1208 if (std::any_of(session_->ports_.begin(), session_->ports_.end(),
1209 [this](const BasicPortAllocatorSession::PortData& p) {
1210 return p.port()->Network() == network_ &&
1211 p.port()->GetProtocol() == PROTO_UDP && !p.error();
1212 })) {
1213 *flags |= PORTALLOCATOR_DISABLE_UDP;
1214 }
1215 if (std::any_of(session_->ports_.begin(), session_->ports_.end(),
1216 [this](const BasicPortAllocatorSession::PortData& p) {
1217 return p.port()->Network() == network_ &&
1218 p.port()->GetProtocol() == PROTO_TCP && !p.error();
1219 })) {
1220 *flags |= PORTALLOCATOR_DISABLE_TCP;
1221 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001222
1223 if (config_ && config) {
1224 if (config_->StunServers() == config->StunServers()) {
1225 // Already got this STUN servers covered.
1226 *flags |= PORTALLOCATOR_DISABLE_STUN;
1227 }
1228 if (!config_->relays.empty()) {
1229 // Already got relays covered.
1230 // NOTE: This will even skip a _different_ set of relay servers if we
1231 // were to be given one, but that never happens in our codebase. Should
1232 // probably get rid of the list in PortConfiguration and just keep a
1233 // single relay server in each one.
1234 *flags |= PORTALLOCATOR_DISABLE_RELAY;
1235 }
1236 }
1237}
1238
1239void AllocationSequence::Start() {
1240 state_ = kRunning;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001241 session_->network_thread()->Post(RTC_FROM_HERE, this, MSG_ALLOCATION_PHASE);
deadbeef5c3c1042017-08-04 15:01:57 -07001242 // Take a snapshot of the best IP, so that when DisableEquivalentPhases is
1243 // called next time, we enable all phases if the best IP has since changed.
1244 previous_best_ip_ = network_->GetBestIP();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001245}
1246
1247void AllocationSequence::Stop() {
1248 // If the port is completed, don't set it to stopped.
1249 if (state_ == kRunning) {
1250 state_ = kStopped;
1251 session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
1252 }
1253}
1254
1255void AllocationSequence::OnMessage(rtc::Message* msg) {
nisseede5da42017-01-12 05:15:36 -08001256 RTC_DCHECK(rtc::Thread::Current() == session_->network_thread());
1257 RTC_DCHECK(msg->message_id == MSG_ALLOCATION_PHASE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001258
deadbeef1c5e6d02017-09-15 17:46:56 -07001259 const char* const PHASE_NAMES[kNumPhases] = {"Udp", "Relay", "Tcp"};
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001260
1261 // Perform all of the phases in the current step.
Jonas Olssond7d762d2018-03-28 09:47:51 +02001262 RTC_LOG(LS_INFO) << network_->ToString()
1263 << ": Allocation Phase=" << PHASE_NAMES[phase_];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001264
1265 switch (phase_) {
1266 case PHASE_UDP:
1267 CreateUDPPorts();
1268 CreateStunPorts();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001269 break;
1270
1271 case PHASE_RELAY:
1272 CreateRelayPorts();
1273 break;
1274
1275 case PHASE_TCP:
1276 CreateTCPPorts();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001277 state_ = kCompleted;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001278 break;
1279
1280 default:
nissec80e7412017-01-11 05:56:46 -08001281 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001282 }
1283
1284 if (state() == kRunning) {
1285 ++phase_;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001286 session_->network_thread()->PostDelayed(RTC_FROM_HERE,
1287 session_->allocator()->step_delay(),
1288 this, MSG_ALLOCATION_PHASE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001289 } else {
1290 // If all phases in AllocationSequence are completed, no allocation
1291 // steps needed further. Canceling pending signal.
1292 session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
1293 SignalPortAllocationComplete(this);
1294 }
1295}
1296
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001297void AllocationSequence::CreateUDPPorts() {
1298 if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001299 RTC_LOG(LS_VERBOSE) << "AllocationSequence: UDP ports disabled, skipping.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001300 return;
1301 }
1302
1303 // TODO(mallinath) - Remove UDPPort creating socket after shared socket
1304 // is enabled completely.
1305 UDPPort* port = NULL;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001306 bool emit_local_candidate_for_anyaddress =
1307 !IsFlagSet(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001308 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001309 port = UDPPort::Create(
1310 session_->network_thread(), session_->socket_factory(), network_,
1311 udp_socket_.get(), session_->username(), session_->password(),
Qingsi Wang4ff54432018-03-01 18:25:20 -08001312 session_->allocator()->origin(), emit_local_candidate_for_anyaddress,
1313 session_->allocator()->stun_candidate_keepalive_interval());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001314 } else {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001315 port = UDPPort::Create(
deadbeef5c3c1042017-08-04 15:01:57 -07001316 session_->network_thread(), session_->socket_factory(), network_,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001317 session_->allocator()->min_port(), session_->allocator()->max_port(),
1318 session_->username(), session_->password(),
Qingsi Wang4ff54432018-03-01 18:25:20 -08001319 session_->allocator()->origin(), emit_local_candidate_for_anyaddress,
1320 session_->allocator()->stun_candidate_keepalive_interval());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001321 }
1322
1323 if (port) {
1324 // If shared socket is enabled, STUN candidate will be allocated by the
1325 // UDPPort.
1326 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
1327 udp_port_ = port;
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001328 port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001329
1330 // If STUN is not disabled, setting stun server address to port.
1331 if (!IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001332 if (config_ && !config_->StunServers().empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001333 RTC_LOG(LS_INFO)
1334 << "AllocationSequence: UDPPort will be handling the "
Jonas Olssond7d762d2018-03-28 09:47:51 +02001335 "STUN candidate generation.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001336 port->set_server_addresses(config_->StunServers());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001337 }
1338 }
1339 }
1340
1341 session_->AddAllocatedPort(port, this, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001342 }
1343}
1344
1345void AllocationSequence::CreateTCPPorts() {
1346 if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001347 RTC_LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001348 return;
1349 }
1350
deadbeef5c3c1042017-08-04 15:01:57 -07001351 Port* port = TCPPort::Create(
1352 session_->network_thread(), session_->socket_factory(), network_,
1353 session_->allocator()->min_port(), session_->allocator()->max_port(),
1354 session_->username(), session_->password(),
1355 session_->allocator()->allow_tcp_listen());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001356 if (port) {
1357 session_->AddAllocatedPort(port, this, true);
1358 // Since TCPPort is not created using shared socket, |port| will not be
1359 // added to the dequeue.
1360 }
1361}
1362
1363void AllocationSequence::CreateStunPorts() {
1364 if (IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001365 RTC_LOG(LS_VERBOSE) << "AllocationSequence: STUN ports disabled, skipping.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001366 return;
1367 }
1368
1369 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
1370 return;
1371 }
1372
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001373 if (!(config_ && !config_->StunServers().empty())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001374 RTC_LOG(LS_WARNING)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001375 << "AllocationSequence: No STUN server configured, skipping.";
1376 return;
1377 }
1378
deadbeef5c3c1042017-08-04 15:01:57 -07001379 StunPort* port = StunPort::Create(
1380 session_->network_thread(), session_->socket_factory(), network_,
1381 session_->allocator()->min_port(), session_->allocator()->max_port(),
1382 session_->username(), session_->password(), config_->StunServers(),
Qingsi Wang4ff54432018-03-01 18:25:20 -08001383 session_->allocator()->origin(),
1384 session_->allocator()->stun_candidate_keepalive_interval());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001385 if (port) {
1386 session_->AddAllocatedPort(port, this, true);
1387 // Since StunPort is not created using shared socket, |port| will not be
1388 // added to the dequeue.
1389 }
1390}
1391
1392void AllocationSequence::CreateRelayPorts() {
1393 if (IsFlagSet(PORTALLOCATOR_DISABLE_RELAY)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001394 RTC_LOG(LS_VERBOSE)
1395 << "AllocationSequence: Relay ports disabled, skipping.";
1396 return;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001397 }
1398
1399 // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we
1400 // ought to have a relay list for them here.
kwibergee89e782017-08-09 17:22:01 -07001401 RTC_DCHECK(config_);
1402 RTC_DCHECK(!config_->relays.empty());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001403 if (!(config_ && !config_->relays.empty())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001404 RTC_LOG(LS_WARNING)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001405 << "AllocationSequence: No relay server configured, skipping.";
1406 return;
1407 }
1408
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001409 for (RelayServerConfig& relay : config_->relays) {
1410 if (relay.type == RELAY_GTURN) {
1411 CreateGturnPort(relay);
1412 } else if (relay.type == RELAY_TURN) {
1413 CreateTurnPort(relay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001414 } else {
nissec80e7412017-01-11 05:56:46 -08001415 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001416 }
1417 }
1418}
1419
1420void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) {
1421 // TODO(mallinath) - Rename RelayPort to GTurnPort.
deadbeef5c3c1042017-08-04 15:01:57 -07001422 RelayPort* port = RelayPort::Create(
1423 session_->network_thread(), session_->socket_factory(), network_,
1424 session_->allocator()->min_port(), session_->allocator()->max_port(),
1425 config_->username, config_->password);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001426 if (port) {
1427 // Since RelayPort is not created using shared socket, |port| will not be
1428 // added to the dequeue.
1429 // Note: We must add the allocated port before we add addresses because
1430 // the latter will create candidates that need name and preference
1431 // settings. However, we also can't prepare the address (normally
1432 // done by AddAllocatedPort) until we have these addresses. So we
1433 // wait to do that until below.
1434 session_->AddAllocatedPort(port, this, false);
1435
1436 // Add the addresses of this protocol.
1437 PortList::const_iterator relay_port;
1438 for (relay_port = config.ports.begin();
1439 relay_port != config.ports.end();
1440 ++relay_port) {
1441 port->AddServerAddress(*relay_port);
1442 port->AddExternalAddress(*relay_port);
1443 }
1444 // Start fetching an address for this port.
1445 port->PrepareAddress();
1446 }
1447}
1448
1449void AllocationSequence::CreateTurnPort(const RelayServerConfig& config) {
1450 PortList::const_iterator relay_port;
1451 for (relay_port = config.ports.begin();
1452 relay_port != config.ports.end(); ++relay_port) {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001453 // Skip UDP connections to relay servers if it's disallowed.
1454 if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP_RELAY) &&
1455 relay_port->proto == PROTO_UDP) {
1456 continue;
1457 }
1458
Honghai Zhang3d31bd62016-08-10 10:33:05 -07001459 // Do not create a port if the server address family is known and does
1460 // not match the local IP address family.
1461 int server_ip_family = relay_port->address.ipaddr().family();
deadbeef5c3c1042017-08-04 15:01:57 -07001462 int local_ip_family = network_->GetBestIP().family();
Honghai Zhang3d31bd62016-08-10 10:33:05 -07001463 if (server_ip_family != AF_UNSPEC && server_ip_family != local_ip_family) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001464 RTC_LOG(LS_INFO)
1465 << "Server and local address families are not compatible. "
Jonas Olssond7d762d2018-03-28 09:47:51 +02001466 "Server address: " << relay_port->address.ipaddr().ToString()
Mirko Bonadei675513b2017-11-09 11:09:25 +01001467 << " Local address: " << network_->GetBestIP().ToString();
Honghai Zhang3d31bd62016-08-10 10:33:05 -07001468 continue;
1469 }
1470
Jonas Oreland202994c2017-12-18 12:10:43 +01001471 CreateRelayPortArgs args;
1472 args.network_thread = session_->network_thread();
1473 args.socket_factory = session_->socket_factory();
1474 args.network = network_;
1475 args.username = session_->username();
1476 args.password = session_->password();
1477 args.server_address = &(*relay_port);
1478 args.config = &config;
1479 args.origin = session_->allocator()->origin();
1480 args.turn_customizer = session_->allocator()->turn_customizer();
1481
1482 std::unique_ptr<cricket::Port> port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001483 // Shared socket mode must be enabled only for UDP based ports. Hence
1484 // don't pass shared socket for ports which will create TCP sockets.
1485 // TODO(mallinath) - Enable shared socket mode for TURN ports. Disabled
1486 // due to webrtc bug https://code.google.com/p/webrtc/issues/detail?id=3537
1487 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) &&
honghaizf421bdc2015-07-17 16:21:55 -07001488 relay_port->proto == PROTO_UDP && udp_socket_) {
Jonas Oreland202994c2017-12-18 12:10:43 +01001489 port = session_->allocator()->relay_port_factory()->Create(
1490 args, udp_socket_.get());
1491
1492 if (!port) {
1493 RTC_LOG(LS_WARNING)
1494 << "Failed to create relay port with "
1495 << args.server_address->address.ToString();
1496 continue;
1497 }
1498
1499 relay_ports_.push_back(port.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001500 // Listen to the port destroyed signal, to allow AllocationSequence to
1501 // remove entrt from it's map.
1502 port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed);
1503 } else {
Jonas Oreland202994c2017-12-18 12:10:43 +01001504 port = session_->allocator()->relay_port_factory()->Create(
1505 args,
1506 session_->allocator()->min_port(),
1507 session_->allocator()->max_port());
1508
1509 if (!port) {
1510 RTC_LOG(LS_WARNING)
1511 << "Failed to create relay port with "
1512 << args.server_address->address.ToString();
1513 continue;
1514 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001515 }
nisseede5da42017-01-12 05:15:36 -08001516 RTC_DCHECK(port != NULL);
Jonas Oreland202994c2017-12-18 12:10:43 +01001517 session_->AddAllocatedPort(port.release(), this, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001518 }
1519}
1520
1521void AllocationSequence::OnReadPacket(
1522 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
1523 const rtc::SocketAddress& remote_addr,
1524 const rtc::PacketTime& packet_time) {
nisseede5da42017-01-12 05:15:36 -08001525 RTC_DCHECK(socket == udp_socket_.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001526
1527 bool turn_port_found = false;
1528
1529 // Try to find the TurnPort that matches the remote address. Note that the
1530 // message could be a STUN binding response if the TURN server is also used as
1531 // a STUN server. We don't want to parse every message here to check if it is
1532 // a STUN binding response, so we pass the message to TurnPort regardless of
1533 // the message type. The TurnPort will just ignore the message since it will
1534 // not find any request by transaction ID.
Jonas Oreland202994c2017-12-18 12:10:43 +01001535 for (auto* port : relay_ports_) {
1536 if (port->CanHandleIncomingPacketsFrom(remote_addr)) {
Sergey Ulanov17fa6722016-05-10 10:20:47 -07001537 if (port->HandleIncomingPacket(socket, data, size, remote_addr,
1538 packet_time)) {
1539 return;
1540 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001541 turn_port_found = true;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001542 }
1543 }
1544
1545 if (udp_port_) {
1546 const ServerAddresses& stun_servers = udp_port_->server_addresses();
1547
1548 // Pass the packet to the UdpPort if there is no matching TurnPort, or if
1549 // the TURN server is also a STUN server.
1550 if (!turn_port_found ||
1551 stun_servers.find(remote_addr) != stun_servers.end()) {
Sergey Ulanov17fa6722016-05-10 10:20:47 -07001552 RTC_DCHECK(udp_port_->SharedSocket());
1553 udp_port_->HandleIncomingPacket(socket, data, size, remote_addr,
1554 packet_time);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001555 }
1556 }
1557}
1558
1559void AllocationSequence::OnPortDestroyed(PortInterface* port) {
1560 if (udp_port_ == port) {
1561 udp_port_ = NULL;
1562 return;
1563 }
1564
Jonas Oreland202994c2017-12-18 12:10:43 +01001565 auto it = std::find(relay_ports_.begin(), relay_ports_.end(), port);
1566 if (it != relay_ports_.end()) {
1567 relay_ports_.erase(it);
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001568 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001569 RTC_LOG(LS_ERROR) << "Unexpected OnPortDestroyed for nonexistent port.";
nissec80e7412017-01-11 05:56:46 -08001570 RTC_NOTREACHED();
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001571 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001572}
1573
1574// PortConfiguration
1575PortConfiguration::PortConfiguration(
1576 const rtc::SocketAddress& stun_address,
1577 const std::string& username,
1578 const std::string& password)
1579 : stun_address(stun_address), username(username), password(password) {
1580 if (!stun_address.IsNil())
1581 stun_servers.insert(stun_address);
1582}
1583
1584PortConfiguration::PortConfiguration(const ServerAddresses& stun_servers,
1585 const std::string& username,
1586 const std::string& password)
1587 : stun_servers(stun_servers),
1588 username(username),
1589 password(password) {
1590 if (!stun_servers.empty())
1591 stun_address = *(stun_servers.begin());
1592}
1593
Steve Anton7995d8c2017-10-30 16:23:38 -07001594PortConfiguration::~PortConfiguration() = default;
1595
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001596ServerAddresses PortConfiguration::StunServers() {
1597 if (!stun_address.IsNil() &&
1598 stun_servers.find(stun_address) == stun_servers.end()) {
1599 stun_servers.insert(stun_address);
1600 }
deadbeefc5d0d952015-07-16 10:22:21 -07001601 // Every UDP TURN server should also be used as a STUN server.
1602 ServerAddresses turn_servers = GetRelayServerAddresses(RELAY_TURN, PROTO_UDP);
1603 for (const rtc::SocketAddress& turn_server : turn_servers) {
1604 if (stun_servers.find(turn_server) == stun_servers.end()) {
1605 stun_servers.insert(turn_server);
1606 }
1607 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001608 return stun_servers;
1609}
1610
1611void PortConfiguration::AddRelay(const RelayServerConfig& config) {
1612 relays.push_back(config);
1613}
1614
1615bool PortConfiguration::SupportsProtocol(
1616 const RelayServerConfig& relay, ProtocolType type) const {
1617 PortList::const_iterator relay_port;
1618 for (relay_port = relay.ports.begin();
1619 relay_port != relay.ports.end();
1620 ++relay_port) {
1621 if (relay_port->proto == type)
1622 return true;
1623 }
1624 return false;
1625}
1626
1627bool PortConfiguration::SupportsProtocol(RelayType turn_type,
1628 ProtocolType type) const {
1629 for (size_t i = 0; i < relays.size(); ++i) {
1630 if (relays[i].type == turn_type &&
1631 SupportsProtocol(relays[i], type))
1632 return true;
1633 }
1634 return false;
1635}
1636
1637ServerAddresses PortConfiguration::GetRelayServerAddresses(
1638 RelayType turn_type, ProtocolType type) const {
1639 ServerAddresses servers;
1640 for (size_t i = 0; i < relays.size(); ++i) {
1641 if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) {
1642 servers.insert(relays[i].ports.front().address);
1643 }
1644 }
1645 return servers;
1646}
1647
1648} // namespace cricket