blob: 11d0cef84741309a660f6aee10e7143016a20470 [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
11#include "webrtc/p2p/client/basicportallocator.h"
12
13#include <string>
14#include <vector>
15
16#include "webrtc/p2p/base/basicpacketsocketfactory.h"
17#include "webrtc/p2p/base/common.h"
18#include "webrtc/p2p/base/port.h"
19#include "webrtc/p2p/base/relayport.h"
20#include "webrtc/p2p/base/stunport.h"
21#include "webrtc/p2p/base/tcpport.h"
22#include "webrtc/p2p/base/turnport.h"
23#include "webrtc/p2p/base/udpport.h"
Guo-wei Shieh38f88932015-08-13 22:24:02 -070024#include "webrtc/base/checks.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025#include "webrtc/base/common.h"
26#include "webrtc/base/helpers.h"
27#include "webrtc/base/logging.h"
28
29using rtc::CreateRandomId;
30using rtc::CreateRandomString;
31
32namespace {
33
34enum {
35 MSG_CONFIG_START,
36 MSG_CONFIG_READY,
37 MSG_ALLOCATE,
38 MSG_ALLOCATION_PHASE,
39 MSG_SHAKE,
40 MSG_SEQUENCEOBJECTS_CREATED,
41 MSG_CONFIG_STOP,
42};
43
44const int PHASE_UDP = 0;
45const int PHASE_RELAY = 1;
46const int PHASE_TCP = 2;
47const int PHASE_SSLTCP = 3;
48
49const int kNumPhases = 4;
50
51const int SHAKE_MIN_DELAY = 45 * 1000; // 45 seconds
52const int SHAKE_MAX_DELAY = 90 * 1000; // 90 seconds
53
54int ShakeDelay() {
55 int range = SHAKE_MAX_DELAY - SHAKE_MIN_DELAY + 1;
56 return SHAKE_MIN_DELAY + CreateRandomId() % range;
57}
58
59} // namespace
60
61namespace cricket {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000062const uint32 DISABLE_ALL_PHASES =
honghaizf421bdc2015-07-17 16:21:55 -070063 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_TCP |
64 PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065
66// BasicPortAllocator
67BasicPortAllocator::BasicPortAllocator(
68 rtc::NetworkManager* network_manager,
69 rtc::PacketSocketFactory* socket_factory)
70 : network_manager_(network_manager),
eblima894ad942015-07-03 08:34:33 -070071 socket_factory_(socket_factory),
72 stun_servers_() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073 ASSERT(socket_factory_ != NULL);
74 Construct();
75}
76
77BasicPortAllocator::BasicPortAllocator(
78 rtc::NetworkManager* network_manager)
79 : network_manager_(network_manager),
eblima894ad942015-07-03 08:34:33 -070080 socket_factory_(NULL),
81 stun_servers_() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082 Construct();
83}
84
85BasicPortAllocator::BasicPortAllocator(
86 rtc::NetworkManager* network_manager,
87 rtc::PacketSocketFactory* socket_factory,
88 const ServerAddresses& stun_servers)
89 : network_manager_(network_manager),
90 socket_factory_(socket_factory),
91 stun_servers_(stun_servers) {
92 ASSERT(socket_factory_ != NULL);
93 Construct();
94}
95
96BasicPortAllocator::BasicPortAllocator(
97 rtc::NetworkManager* network_manager,
98 const ServerAddresses& stun_servers,
99 const rtc::SocketAddress& relay_address_udp,
100 const rtc::SocketAddress& relay_address_tcp,
101 const rtc::SocketAddress& relay_address_ssl)
102 : network_manager_(network_manager),
103 socket_factory_(NULL),
104 stun_servers_(stun_servers) {
105
106 RelayServerConfig config(RELAY_GTURN);
107 if (!relay_address_udp.IsNil())
108 config.ports.push_back(ProtocolAddress(relay_address_udp, PROTO_UDP));
109 if (!relay_address_tcp.IsNil())
110 config.ports.push_back(ProtocolAddress(relay_address_tcp, PROTO_TCP));
111 if (!relay_address_ssl.IsNil())
112 config.ports.push_back(ProtocolAddress(relay_address_ssl, PROTO_SSLTCP));
113
114 if (!config.ports.empty())
115 AddRelay(config);
116
117 Construct();
118}
119
120void BasicPortAllocator::Construct() {
121 allow_tcp_listen_ = true;
122}
123
124BasicPortAllocator::~BasicPortAllocator() {
125}
126
deadbeefc5d0d952015-07-16 10:22:21 -0700127PortAllocatorSession* BasicPortAllocator::CreateSessionInternal(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128 const std::string& content_name, int component,
129 const std::string& ice_ufrag, const std::string& ice_pwd) {
130 return new BasicPortAllocatorSession(
131 this, content_name, component, ice_ufrag, ice_pwd);
132}
133
134
135// BasicPortAllocatorSession
136BasicPortAllocatorSession::BasicPortAllocatorSession(
137 BasicPortAllocator *allocator,
138 const std::string& content_name,
139 int component,
140 const std::string& ice_ufrag,
141 const std::string& ice_pwd)
142 : PortAllocatorSession(content_name, component,
143 ice_ufrag, ice_pwd, allocator->flags()),
144 allocator_(allocator), network_thread_(NULL),
145 socket_factory_(allocator->socket_factory()),
146 allocation_started_(false),
147 network_manager_started_(false),
148 running_(false),
149 allocation_sequences_created_(false) {
150 allocator_->network_manager()->SignalNetworksChanged.connect(
151 this, &BasicPortAllocatorSession::OnNetworksChanged);
152 allocator_->network_manager()->StartUpdating();
153}
154
155BasicPortAllocatorSession::~BasicPortAllocatorSession() {
156 allocator_->network_manager()->StopUpdating();
157 if (network_thread_ != NULL)
158 network_thread_->Clear(this);
159
160 for (uint32 i = 0; i < sequences_.size(); ++i) {
161 // AllocationSequence should clear it's map entry for turn ports before
162 // ports are destroyed.
163 sequences_[i]->Clear();
164 }
165
166 std::vector<PortData>::iterator it;
167 for (it = ports_.begin(); it != ports_.end(); it++)
168 delete it->port();
169
170 for (uint32 i = 0; i < configs_.size(); ++i)
171 delete configs_[i];
172
173 for (uint32 i = 0; i < sequences_.size(); ++i)
174 delete sequences_[i];
175}
176
177void BasicPortAllocatorSession::StartGettingPorts() {
178 network_thread_ = rtc::Thread::Current();
179 if (!socket_factory_) {
180 owned_socket_factory_.reset(
181 new rtc::BasicPacketSocketFactory(network_thread_));
182 socket_factory_ = owned_socket_factory_.get();
183 }
184
185 running_ = true;
186 network_thread_->Post(this, MSG_CONFIG_START);
187
188 if (flags() & PORTALLOCATOR_ENABLE_SHAKER)
189 network_thread_->PostDelayed(ShakeDelay(), this, MSG_SHAKE);
190}
191
192void BasicPortAllocatorSession::StopGettingPorts() {
193 ASSERT(rtc::Thread::Current() == network_thread_);
194 running_ = false;
195 network_thread_->Clear(this, MSG_ALLOCATE);
196 for (uint32 i = 0; i < sequences_.size(); ++i)
197 sequences_[i]->Stop();
198 network_thread_->Post(this, MSG_CONFIG_STOP);
199}
200
201void BasicPortAllocatorSession::OnMessage(rtc::Message *message) {
202 switch (message->message_id) {
203 case MSG_CONFIG_START:
204 ASSERT(rtc::Thread::Current() == network_thread_);
205 GetPortConfigurations();
206 break;
207
208 case MSG_CONFIG_READY:
209 ASSERT(rtc::Thread::Current() == network_thread_);
210 OnConfigReady(static_cast<PortConfiguration*>(message->pdata));
211 break;
212
213 case MSG_ALLOCATE:
214 ASSERT(rtc::Thread::Current() == network_thread_);
215 OnAllocate();
216 break;
217
218 case MSG_SHAKE:
219 ASSERT(rtc::Thread::Current() == network_thread_);
220 OnShake();
221 break;
222 case MSG_SEQUENCEOBJECTS_CREATED:
223 ASSERT(rtc::Thread::Current() == network_thread_);
224 OnAllocationSequenceObjectsCreated();
225 break;
226 case MSG_CONFIG_STOP:
227 ASSERT(rtc::Thread::Current() == network_thread_);
228 OnConfigStop();
229 break;
230 default:
231 ASSERT(false);
232 }
233}
234
235void BasicPortAllocatorSession::GetPortConfigurations() {
236 PortConfiguration* config = new PortConfiguration(allocator_->stun_servers(),
237 username(),
238 password());
239
240 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
241 config->AddRelay(allocator_->relays()[i]);
242 }
243 ConfigReady(config);
244}
245
246void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) {
247 network_thread_->Post(this, MSG_CONFIG_READY, config);
248}
249
250// Adds a configuration to the list.
251void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) {
252 if (config)
253 configs_.push_back(config);
254
255 AllocatePorts();
256}
257
258void BasicPortAllocatorSession::OnConfigStop() {
259 ASSERT(rtc::Thread::Current() == network_thread_);
260
261 // If any of the allocated ports have not completed the candidates allocation,
262 // mark those as error. Since session doesn't need any new candidates
263 // at this stage of the allocation, it's safe to discard any new candidates.
264 bool send_signal = false;
265 for (std::vector<PortData>::iterator it = ports_.begin();
266 it != ports_.end(); ++it) {
267 if (!it->complete()) {
268 // Updating port state to error, which didn't finish allocating candidates
269 // yet.
270 it->set_error();
271 send_signal = true;
272 }
273 }
274
275 // Did we stop any running sequences?
276 for (std::vector<AllocationSequence*>::iterator it = sequences_.begin();
277 it != sequences_.end() && !send_signal; ++it) {
278 if ((*it)->state() == AllocationSequence::kStopped) {
279 send_signal = true;
280 }
281 }
282
283 // If we stopped anything that was running, send a done signal now.
284 if (send_signal) {
285 MaybeSignalCandidatesAllocationDone();
286 }
287}
288
289void BasicPortAllocatorSession::AllocatePorts() {
290 ASSERT(rtc::Thread::Current() == network_thread_);
291 network_thread_->Post(this, MSG_ALLOCATE);
292}
293
294void BasicPortAllocatorSession::OnAllocate() {
295 if (network_manager_started_)
296 DoAllocate();
297
298 allocation_started_ = true;
299}
300
honghaiz8c404fa2015-09-28 07:59:43 -0700301void BasicPortAllocatorSession::GetNetworks(
302 std::vector<rtc::Network*>* networks) {
303 networks->clear();
304 rtc::NetworkManager* network_manager = allocator_->network_manager();
305 ASSERT(network_manager != nullptr);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700306 // If the network permission state is BLOCKED, we just act as if the flag has
307 // been passed in.
honghaiz8c404fa2015-09-28 07:59:43 -0700308 if (network_manager->enumeration_permission() ==
guoweisea1012b2015-08-21 09:06:28 -0700309 rtc::NetworkManager::ENUMERATION_BLOCKED) {
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700310 set_flags(flags() | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
311 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000312 // If the adapter enumeration is disabled, we'll just bind to any address
313 // instead of specific NIC. This is to ensure the same routing for http
314 // traffic by OS is also used here to avoid any local or public IP leakage
315 // during stun process.
316 if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) {
honghaiz8c404fa2015-09-28 07:59:43 -0700317 network_manager->GetAnyAddressNetworks(networks);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000318 } else {
honghaiz8c404fa2015-09-28 07:59:43 -0700319 network_manager->GetNetworks(networks);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000320 }
honghaiz8c404fa2015-09-28 07:59:43 -0700321}
322
323// For each network, see if we have a sequence that covers it already. If not,
324// create a new sequence to create the appropriate ports.
325void BasicPortAllocatorSession::DoAllocate() {
326 bool done_signal_needed = false;
327 std::vector<rtc::Network*> networks;
328 GetNetworks(&networks);
329
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000330 if (networks.empty()) {
331 LOG(LS_WARNING) << "Machine has no networks; no ports will be allocated";
332 done_signal_needed = true;
333 } else {
334 for (uint32 i = 0; i < networks.size(); ++i) {
335 PortConfiguration* config = NULL;
336 if (configs_.size() > 0)
337 config = configs_.back();
338
339 uint32 sequence_flags = flags();
340 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
341 // If all the ports are disabled we should just fire the allocation
342 // done event and return.
343 done_signal_needed = true;
344 break;
345 }
346
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000347 if (!config || config->relays.empty()) {
348 // No relay ports specified in this config.
349 sequence_flags |= PORTALLOCATOR_DISABLE_RELAY;
350 }
351
352 if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6) &&
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000353 networks[i]->GetBestIP().family() == AF_INET6) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000354 // Skip IPv6 networks unless the flag's been set.
355 continue;
356 }
357
358 // Disable phases that would only create ports equivalent to
359 // ones that we have already made.
360 DisableEquivalentPhases(networks[i], config, &sequence_flags);
361
362 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
363 // New AllocationSequence would have nothing to do, so don't make it.
364 continue;
365 }
366
367 AllocationSequence* sequence =
368 new AllocationSequence(this, networks[i], config, sequence_flags);
369 if (!sequence->Init()) {
370 delete sequence;
371 continue;
372 }
373 done_signal_needed = true;
374 sequence->SignalPortAllocationComplete.connect(
375 this, &BasicPortAllocatorSession::OnPortAllocationComplete);
376 if (running_)
377 sequence->Start();
378 sequences_.push_back(sequence);
379 }
380 }
381 if (done_signal_needed) {
382 network_thread_->Post(this, MSG_SEQUENCEOBJECTS_CREATED);
383 }
384}
385
386void BasicPortAllocatorSession::OnNetworksChanged() {
honghaiz8c404fa2015-09-28 07:59:43 -0700387 std::vector<rtc::Network*> networks;
388 GetNetworks(&networks);
389 for (AllocationSequence* sequence : sequences_) {
390 // Remove the network from the allocation sequence if it is not in
391 // |networks|.
392 if (!sequence->network_removed() &&
393 std::find(networks.begin(), networks.end(), sequence->network()) ==
394 networks.end()) {
395 sequence->OnNetworkRemoved();
396 }
397 }
398
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000399 network_manager_started_ = true;
400 if (allocation_started_)
401 DoAllocate();
402}
403
404void BasicPortAllocatorSession::DisableEquivalentPhases(
405 rtc::Network* network, PortConfiguration* config, uint32* flags) {
406 for (uint32 i = 0; i < sequences_.size() &&
407 (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES; ++i) {
408 sequences_[i]->DisableEquivalentPhases(network, config, flags);
409 }
410}
411
412void BasicPortAllocatorSession::AddAllocatedPort(Port* port,
413 AllocationSequence * seq,
414 bool prepare_address) {
415 if (!port)
416 return;
417
418 LOG(LS_INFO) << "Adding allocated port for " << content_name();
419 port->set_content_name(content_name());
420 port->set_component(component_);
421 port->set_generation(generation());
422 if (allocator_->proxy().type != rtc::PROXY_NONE)
423 port->set_proxy(allocator_->user_agent(), allocator_->proxy());
424 port->set_send_retransmit_count_attribute((allocator_->flags() &
425 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE) != 0);
426
427 // Push down the candidate_filter to individual port.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000428 uint32 candidate_filter = allocator_->candidate_filter();
429
430 // When adapter enumeration is disabled, disable CF_HOST at port level so
431 // local address is not leaked by stunport in the candidate's related address.
432 if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) {
433 candidate_filter &= ~CF_HOST;
434 }
435 port->set_candidate_filter(candidate_filter);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000436
437 PortData data(port, seq);
438 ports_.push_back(data);
439
440 port->SignalCandidateReady.connect(
441 this, &BasicPortAllocatorSession::OnCandidateReady);
442 port->SignalPortComplete.connect(this,
443 &BasicPortAllocatorSession::OnPortComplete);
444 port->SignalDestroyed.connect(this,
445 &BasicPortAllocatorSession::OnPortDestroyed);
446 port->SignalPortError.connect(
447 this, &BasicPortAllocatorSession::OnPortError);
448 LOG_J(LS_INFO, port) << "Added port to allocator";
449
450 if (prepare_address)
451 port->PrepareAddress();
452}
453
454void BasicPortAllocatorSession::OnAllocationSequenceObjectsCreated() {
455 allocation_sequences_created_ = true;
456 // Send candidate allocation complete signal if we have no sequences.
457 MaybeSignalCandidatesAllocationDone();
458}
459
460void BasicPortAllocatorSession::OnCandidateReady(
461 Port* port, const Candidate& c) {
462 ASSERT(rtc::Thread::Current() == network_thread_);
463 PortData* data = FindPort(port);
464 ASSERT(data != NULL);
465 // Discarding any candidate signal if port allocation status is
466 // already in completed state.
467 if (data->complete())
468 return;
469
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000470 ProtocolType pvalue;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700471 bool candidate_signalable = CheckCandidateFilter(c);
472 bool candidate_pairable =
473 candidate_signalable ||
474 (c.address().IsAnyIP() &&
475 (port->SharedSocket() || c.protocol() == TCP_PROTOCOL_NAME));
476 bool candidate_protocol_enabled =
477 StringToProto(c.protocol().c_str(), &pvalue) &&
478 data->sequence()->ProtocolEnabled(pvalue);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000479
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700480 if (candidate_signalable && candidate_protocol_enabled) {
481 std::vector<Candidate> candidates;
482 candidates.push_back(c);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000483 SignalCandidatesReady(this, candidates);
484 }
485
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700486 // Port has been made ready. Nothing to do here.
487 if (data->ready()) {
488 return;
489 }
490
491 // Move the port to the READY state, either because we have a usable candidate
492 // from the port, or simply because the port is bound to the any address and
493 // therefore has no host candidate. This will trigger the port to start
494 // creating candidate pairs (connections) and issue connectivity checks.
495 if (candidate_pairable) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000496 data->set_ready();
497 SignalPortReady(this, port);
498 }
499}
500
501void BasicPortAllocatorSession::OnPortComplete(Port* port) {
502 ASSERT(rtc::Thread::Current() == network_thread_);
503 PortData* data = FindPort(port);
504 ASSERT(data != NULL);
505
506 // Ignore any late signals.
507 if (data->complete())
508 return;
509
510 // Moving to COMPLETE state.
511 data->set_complete();
512 // Send candidate allocation complete signal if this was the last port.
513 MaybeSignalCandidatesAllocationDone();
514}
515
516void BasicPortAllocatorSession::OnPortError(Port* port) {
517 ASSERT(rtc::Thread::Current() == network_thread_);
518 PortData* data = FindPort(port);
519 ASSERT(data != NULL);
520 // We might have already given up on this port and stopped it.
521 if (data->complete())
522 return;
523
524 // SignalAddressError is currently sent from StunPort/TurnPort.
525 // But this signal itself is generic.
526 data->set_error();
527 // Send candidate allocation complete signal if this was the last port.
528 MaybeSignalCandidatesAllocationDone();
529}
530
531void BasicPortAllocatorSession::OnProtocolEnabled(AllocationSequence* seq,
532 ProtocolType proto) {
533 std::vector<Candidate> candidates;
534 for (std::vector<PortData>::iterator it = ports_.begin();
535 it != ports_.end(); ++it) {
536 if (it->sequence() != seq)
537 continue;
538
539 const std::vector<Candidate>& potentials = it->port()->Candidates();
540 for (size_t i = 0; i < potentials.size(); ++i) {
541 if (!CheckCandidateFilter(potentials[i]))
542 continue;
543 ProtocolType pvalue;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700544 bool candidate_protocol_enabled =
545 StringToProto(potentials[i].protocol().c_str(), &pvalue) &&
546 pvalue == proto;
547 if (candidate_protocol_enabled) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000548 candidates.push_back(potentials[i]);
549 }
550 }
551 }
552
553 if (!candidates.empty()) {
554 SignalCandidatesReady(this, candidates);
555 }
556}
557
558bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) {
559 uint32 filter = allocator_->candidate_filter();
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000560
561 // When binding to any address, before sending packets out, the getsockname
562 // returns all 0s, but after sending packets, it'll be the NIC used to
563 // send. All 0s is not a valid ICE candidate address and should be filtered
564 // out.
565 if (c.address().IsAnyIP()) {
566 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000567 }
568
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000569 if (c.type() == RELAY_PORT_TYPE) {
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +0000570 return ((filter & CF_RELAY) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000571 } else if (c.type() == STUN_PORT_TYPE) {
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +0000572 return ((filter & CF_REFLEXIVE) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000573 } else if (c.type() == LOCAL_PORT_TYPE) {
574 if ((filter & CF_REFLEXIVE) && !c.address().IsPrivateIP()) {
575 // We allow host candidates if the filter allows server-reflexive
576 // candidates and the candidate is a public IP. Because we don't generate
577 // server-reflexive candidates if they have the same IP as the host
578 // candidate (i.e. when the host candidate is a public IP), filtering to
579 // only server-reflexive candidates won't work right when the host
580 // candidates have public IPs.
581 return true;
582 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000583
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700584 // If PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE is specified and it's
585 // loopback address, we should allow it as it's for demo page connectivity
586 // when no TURN/STUN specified.
587 if (c.address().IsLoopbackIP() &&
588 (flags() & PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE) != 0) {
589 return true;
590 }
591
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000592 // This is just to prevent the case when binding to any address (all 0s), if
593 // somehow the host candidate address is not all 0s. Either because local
594 // installed proxy changes the address or a packet has been sent for any
595 // reason before getsockname is called.
596 if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) {
597 LOG(LS_WARNING) << "Received non-0 host address: "
598 << c.address().ToString()
599 << " when adapter enumeration is disabled";
600 return false;
601 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000602
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +0000603 return ((filter & CF_HOST) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000604 }
605 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000606}
607
608void BasicPortAllocatorSession::OnPortAllocationComplete(
609 AllocationSequence* seq) {
610 // Send candidate allocation complete signal if all ports are done.
611 MaybeSignalCandidatesAllocationDone();
612}
613
614void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() {
615 // Send signal only if all required AllocationSequence objects
616 // are created.
617 if (!allocation_sequences_created_)
618 return;
619
620 // Check that all port allocation sequences are complete.
621 for (std::vector<AllocationSequence*>::iterator it = sequences_.begin();
622 it != sequences_.end(); ++it) {
623 if ((*it)->state() == AllocationSequence::kRunning)
624 return;
625 }
626
627 // If all allocated ports are in complete state, session must have got all
628 // expected candidates. Session will trigger candidates allocation complete
629 // signal.
630 for (std::vector<PortData>::iterator it = ports_.begin();
631 it != ports_.end(); ++it) {
632 if (!it->complete())
633 return;
634 }
635 LOG(LS_INFO) << "All candidates gathered for " << content_name_ << ":"
636 << component_ << ":" << generation();
637 SignalCandidatesAllocationDone(this);
638}
639
640void BasicPortAllocatorSession::OnPortDestroyed(
641 PortInterface* port) {
642 ASSERT(rtc::Thread::Current() == network_thread_);
643 for (std::vector<PortData>::iterator iter = ports_.begin();
644 iter != ports_.end(); ++iter) {
645 if (port == iter->port()) {
646 ports_.erase(iter);
647 LOG_J(LS_INFO, port) << "Removed port from allocator ("
648 << static_cast<int>(ports_.size()) << " remaining)";
649 return;
650 }
651 }
652 ASSERT(false);
653}
654
655void BasicPortAllocatorSession::OnShake() {
656 LOG(INFO) << ">>>>> SHAKE <<<<< >>>>> SHAKE <<<<< >>>>> SHAKE <<<<<";
657
658 std::vector<Port*> ports;
659 std::vector<Connection*> connections;
660
661 for (size_t i = 0; i < ports_.size(); ++i) {
662 if (ports_[i].ready())
663 ports.push_back(ports_[i].port());
664 }
665
666 for (size_t i = 0; i < ports.size(); ++i) {
667 Port::AddressMap::const_iterator iter;
668 for (iter = ports[i]->connections().begin();
669 iter != ports[i]->connections().end();
670 ++iter) {
671 connections.push_back(iter->second);
672 }
673 }
674
675 LOG(INFO) << ">>>>> Destroying " << ports.size() << " ports and "
676 << connections.size() << " connections";
677
678 for (size_t i = 0; i < connections.size(); ++i)
679 connections[i]->Destroy();
680
681 if (running_ || (ports.size() > 0) || (connections.size() > 0))
682 network_thread_->PostDelayed(ShakeDelay(), this, MSG_SHAKE);
683}
684
685BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort(
686 Port* port) {
687 for (std::vector<PortData>::iterator it = ports_.begin();
688 it != ports_.end(); ++it) {
689 if (it->port() == port) {
690 return &*it;
691 }
692 }
693 return NULL;
694}
695
696// AllocationSequence
697
698AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session,
699 rtc::Network* network,
700 PortConfiguration* config,
701 uint32 flags)
702 : session_(session),
703 network_(network),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000704 ip_(network->GetBestIP()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000705 config_(config),
706 state_(kInit),
707 flags_(flags),
708 udp_socket_(),
709 udp_port_(NULL),
710 phase_(0) {
711}
712
713bool AllocationSequence::Init() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000714 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
715 udp_socket_.reset(session_->socket_factory()->CreateUdpSocket(
716 rtc::SocketAddress(ip_, 0), session_->allocator()->min_port(),
717 session_->allocator()->max_port()));
718 if (udp_socket_) {
719 udp_socket_->SignalReadPacket.connect(
720 this, &AllocationSequence::OnReadPacket);
721 }
722 // Continuing if |udp_socket_| is NULL, as local TCP and RelayPort using TCP
723 // are next available options to setup a communication channel.
724 }
725 return true;
726}
727
728void AllocationSequence::Clear() {
729 udp_port_ = NULL;
730 turn_ports_.clear();
731}
732
honghaiz8c404fa2015-09-28 07:59:43 -0700733void AllocationSequence::OnNetworkRemoved() {
734 // Stop the allocation sequence if its network is gone.
735 Stop();
736 network_removed_ = true;
737}
738
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000739AllocationSequence::~AllocationSequence() {
740 session_->network_thread()->Clear(this);
741}
742
743void AllocationSequence::DisableEquivalentPhases(rtc::Network* network,
744 PortConfiguration* config, uint32* flags) {
honghaiz8c404fa2015-09-28 07:59:43 -0700745 if (network_removed_) {
746 // If the network of this allocation sequence has ever gone away,
747 // it won't be equivalent to the new network.
748 return;
749 }
750
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000751 if (!((network == network_) && (ip_ == network->GetBestIP()))) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000752 // Different network setup; nothing is equivalent.
753 return;
754 }
755
756 // Else turn off the stuff that we've already got covered.
757
758 // Every config implicitly specifies local, so turn that off right away.
759 *flags |= PORTALLOCATOR_DISABLE_UDP;
760 *flags |= PORTALLOCATOR_DISABLE_TCP;
761
762 if (config_ && config) {
763 if (config_->StunServers() == config->StunServers()) {
764 // Already got this STUN servers covered.
765 *flags |= PORTALLOCATOR_DISABLE_STUN;
766 }
767 if (!config_->relays.empty()) {
768 // Already got relays covered.
769 // NOTE: This will even skip a _different_ set of relay servers if we
770 // were to be given one, but that never happens in our codebase. Should
771 // probably get rid of the list in PortConfiguration and just keep a
772 // single relay server in each one.
773 *flags |= PORTALLOCATOR_DISABLE_RELAY;
774 }
775 }
776}
777
778void AllocationSequence::Start() {
779 state_ = kRunning;
780 session_->network_thread()->Post(this, MSG_ALLOCATION_PHASE);
781}
782
783void AllocationSequence::Stop() {
784 // If the port is completed, don't set it to stopped.
785 if (state_ == kRunning) {
786 state_ = kStopped;
787 session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
788 }
789}
790
791void AllocationSequence::OnMessage(rtc::Message* msg) {
792 ASSERT(rtc::Thread::Current() == session_->network_thread());
793 ASSERT(msg->message_id == MSG_ALLOCATION_PHASE);
794
795 const char* const PHASE_NAMES[kNumPhases] = {
796 "Udp", "Relay", "Tcp", "SslTcp"
797 };
798
799 // Perform all of the phases in the current step.
800 LOG_J(LS_INFO, network_) << "Allocation Phase="
801 << PHASE_NAMES[phase_];
802
803 switch (phase_) {
804 case PHASE_UDP:
805 CreateUDPPorts();
806 CreateStunPorts();
807 EnableProtocol(PROTO_UDP);
808 break;
809
810 case PHASE_RELAY:
811 CreateRelayPorts();
812 break;
813
814 case PHASE_TCP:
815 CreateTCPPorts();
816 EnableProtocol(PROTO_TCP);
817 break;
818
819 case PHASE_SSLTCP:
820 state_ = kCompleted;
821 EnableProtocol(PROTO_SSLTCP);
822 break;
823
824 default:
825 ASSERT(false);
826 }
827
828 if (state() == kRunning) {
829 ++phase_;
830 session_->network_thread()->PostDelayed(
831 session_->allocator()->step_delay(),
832 this, MSG_ALLOCATION_PHASE);
833 } else {
834 // If all phases in AllocationSequence are completed, no allocation
835 // steps needed further. Canceling pending signal.
836 session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
837 SignalPortAllocationComplete(this);
838 }
839}
840
841void AllocationSequence::EnableProtocol(ProtocolType proto) {
842 if (!ProtocolEnabled(proto)) {
843 protocols_.push_back(proto);
844 session_->OnProtocolEnabled(this, proto);
845 }
846}
847
848bool AllocationSequence::ProtocolEnabled(ProtocolType proto) const {
849 for (ProtocolList::const_iterator it = protocols_.begin();
850 it != protocols_.end(); ++it) {
851 if (*it == proto)
852 return true;
853 }
854 return false;
855}
856
857void AllocationSequence::CreateUDPPorts() {
858 if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP)) {
859 LOG(LS_VERBOSE) << "AllocationSequence: UDP ports disabled, skipping.";
860 return;
861 }
862
863 // TODO(mallinath) - Remove UDPPort creating socket after shared socket
864 // is enabled completely.
865 UDPPort* port = NULL;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700866 bool emit_localhost_for_anyaddress =
867 IsFlagSet(PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000868 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700869 port = UDPPort::Create(
870 session_->network_thread(), session_->socket_factory(), network_,
871 udp_socket_.get(), session_->username(), session_->password(),
872 session_->allocator()->origin(), emit_localhost_for_anyaddress);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873 } else {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700874 port = UDPPort::Create(
875 session_->network_thread(), session_->socket_factory(), network_, ip_,
876 session_->allocator()->min_port(), session_->allocator()->max_port(),
877 session_->username(), session_->password(),
878 session_->allocator()->origin(), emit_localhost_for_anyaddress);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000879 }
880
881 if (port) {
882 // If shared socket is enabled, STUN candidate will be allocated by the
883 // UDPPort.
884 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
885 udp_port_ = port;
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +0000886 port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000887
888 // If STUN is not disabled, setting stun server address to port.
889 if (!IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000890 if (config_ && !config_->StunServers().empty()) {
891 LOG(LS_INFO) << "AllocationSequence: UDPPort will be handling the "
892 << "STUN candidate generation.";
893 port->set_server_addresses(config_->StunServers());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000894 }
895 }
896 }
897
898 session_->AddAllocatedPort(port, this, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000899 }
900}
901
902void AllocationSequence::CreateTCPPorts() {
903 if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) {
904 LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping.";
905 return;
906 }
907
908 Port* port = TCPPort::Create(session_->network_thread(),
909 session_->socket_factory(),
910 network_, ip_,
911 session_->allocator()->min_port(),
912 session_->allocator()->max_port(),
913 session_->username(), session_->password(),
914 session_->allocator()->allow_tcp_listen());
915 if (port) {
916 session_->AddAllocatedPort(port, this, true);
917 // Since TCPPort is not created using shared socket, |port| will not be
918 // added to the dequeue.
919 }
920}
921
922void AllocationSequence::CreateStunPorts() {
923 if (IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
924 LOG(LS_VERBOSE) << "AllocationSequence: STUN ports disabled, skipping.";
925 return;
926 }
927
928 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
929 return;
930 }
931
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000932 if (!(config_ && !config_->StunServers().empty())) {
933 LOG(LS_WARNING)
934 << "AllocationSequence: No STUN server configured, skipping.";
935 return;
936 }
937
938 StunPort* port = StunPort::Create(session_->network_thread(),
939 session_->socket_factory(),
940 network_, ip_,
941 session_->allocator()->min_port(),
942 session_->allocator()->max_port(),
943 session_->username(), session_->password(),
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000944 config_->StunServers(),
945 session_->allocator()->origin());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000946 if (port) {
947 session_->AddAllocatedPort(port, this, true);
948 // Since StunPort is not created using shared socket, |port| will not be
949 // added to the dequeue.
950 }
951}
952
953void AllocationSequence::CreateRelayPorts() {
954 if (IsFlagSet(PORTALLOCATOR_DISABLE_RELAY)) {
955 LOG(LS_VERBOSE) << "AllocationSequence: Relay ports disabled, skipping.";
956 return;
957 }
958
959 // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we
960 // ought to have a relay list for them here.
961 ASSERT(config_ && !config_->relays.empty());
962 if (!(config_ && !config_->relays.empty())) {
963 LOG(LS_WARNING)
964 << "AllocationSequence: No relay server configured, skipping.";
965 return;
966 }
967
968 PortConfiguration::RelayList::const_iterator relay;
969 for (relay = config_->relays.begin();
970 relay != config_->relays.end(); ++relay) {
971 if (relay->type == RELAY_GTURN) {
972 CreateGturnPort(*relay);
973 } else if (relay->type == RELAY_TURN) {
974 CreateTurnPort(*relay);
975 } else {
976 ASSERT(false);
977 }
978 }
979}
980
981void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) {
982 // TODO(mallinath) - Rename RelayPort to GTurnPort.
983 RelayPort* port = RelayPort::Create(session_->network_thread(),
984 session_->socket_factory(),
985 network_, ip_,
986 session_->allocator()->min_port(),
987 session_->allocator()->max_port(),
988 config_->username, config_->password);
989 if (port) {
990 // Since RelayPort is not created using shared socket, |port| will not be
991 // added to the dequeue.
992 // Note: We must add the allocated port before we add addresses because
993 // the latter will create candidates that need name and preference
994 // settings. However, we also can't prepare the address (normally
995 // done by AddAllocatedPort) until we have these addresses. So we
996 // wait to do that until below.
997 session_->AddAllocatedPort(port, this, false);
998
999 // Add the addresses of this protocol.
1000 PortList::const_iterator relay_port;
1001 for (relay_port = config.ports.begin();
1002 relay_port != config.ports.end();
1003 ++relay_port) {
1004 port->AddServerAddress(*relay_port);
1005 port->AddExternalAddress(*relay_port);
1006 }
1007 // Start fetching an address for this port.
1008 port->PrepareAddress();
1009 }
1010}
1011
1012void AllocationSequence::CreateTurnPort(const RelayServerConfig& config) {
1013 PortList::const_iterator relay_port;
1014 for (relay_port = config.ports.begin();
1015 relay_port != config.ports.end(); ++relay_port) {
1016 TurnPort* port = NULL;
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001017
1018 // Skip UDP connections to relay servers if it's disallowed.
1019 if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP_RELAY) &&
1020 relay_port->proto == PROTO_UDP) {
1021 continue;
1022 }
1023
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001024 // Shared socket mode must be enabled only for UDP based ports. Hence
1025 // don't pass shared socket for ports which will create TCP sockets.
1026 // TODO(mallinath) - Enable shared socket mode for TURN ports. Disabled
1027 // due to webrtc bug https://code.google.com/p/webrtc/issues/detail?id=3537
1028 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) &&
honghaizf421bdc2015-07-17 16:21:55 -07001029 relay_port->proto == PROTO_UDP && udp_socket_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001030 port = TurnPort::Create(session_->network_thread(),
1031 session_->socket_factory(),
1032 network_, udp_socket_.get(),
1033 session_->username(), session_->password(),
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +00001034 *relay_port, config.credentials, config.priority,
1035 session_->allocator()->origin());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001036 turn_ports_.push_back(port);
1037 // Listen to the port destroyed signal, to allow AllocationSequence to
1038 // remove entrt from it's map.
1039 port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed);
1040 } else {
1041 port = TurnPort::Create(session_->network_thread(),
1042 session_->socket_factory(),
1043 network_, ip_,
1044 session_->allocator()->min_port(),
1045 session_->allocator()->max_port(),
1046 session_->username(),
1047 session_->password(),
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +00001048 *relay_port, config.credentials, config.priority,
1049 session_->allocator()->origin());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001050 }
1051 ASSERT(port != NULL);
1052 session_->AddAllocatedPort(port, this, true);
1053 }
1054}
1055
1056void AllocationSequence::OnReadPacket(
1057 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
1058 const rtc::SocketAddress& remote_addr,
1059 const rtc::PacketTime& packet_time) {
1060 ASSERT(socket == udp_socket_.get());
1061
1062 bool turn_port_found = false;
1063
1064 // Try to find the TurnPort that matches the remote address. Note that the
1065 // message could be a STUN binding response if the TURN server is also used as
1066 // a STUN server. We don't want to parse every message here to check if it is
1067 // a STUN binding response, so we pass the message to TurnPort regardless of
1068 // the message type. The TurnPort will just ignore the message since it will
1069 // not find any request by transaction ID.
1070 for (std::vector<TurnPort*>::const_iterator it = turn_ports_.begin();
1071 it != turn_ports_.end(); ++it) {
1072 TurnPort* port = *it;
1073 if (port->server_address().address == remote_addr) {
1074 port->HandleIncomingPacket(socket, data, size, remote_addr, packet_time);
1075 turn_port_found = true;
1076 break;
1077 }
1078 }
1079
1080 if (udp_port_) {
1081 const ServerAddresses& stun_servers = udp_port_->server_addresses();
1082
1083 // Pass the packet to the UdpPort if there is no matching TurnPort, or if
1084 // the TURN server is also a STUN server.
1085 if (!turn_port_found ||
1086 stun_servers.find(remote_addr) != stun_servers.end()) {
1087 udp_port_->HandleIncomingPacket(
1088 socket, data, size, remote_addr, packet_time);
1089 }
1090 }
1091}
1092
1093void AllocationSequence::OnPortDestroyed(PortInterface* port) {
1094 if (udp_port_ == port) {
1095 udp_port_ = NULL;
1096 return;
1097 }
1098
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001099 auto it = std::find(turn_ports_.begin(), turn_ports_.end(), port);
1100 if (it != turn_ports_.end()) {
1101 turn_ports_.erase(it);
1102 } else {
1103 LOG(LS_ERROR) << "Unexpected OnPortDestroyed for nonexistent port.";
1104 ASSERT(false);
1105 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001106}
1107
1108// PortConfiguration
1109PortConfiguration::PortConfiguration(
1110 const rtc::SocketAddress& stun_address,
1111 const std::string& username,
1112 const std::string& password)
1113 : stun_address(stun_address), username(username), password(password) {
1114 if (!stun_address.IsNil())
1115 stun_servers.insert(stun_address);
1116}
1117
1118PortConfiguration::PortConfiguration(const ServerAddresses& stun_servers,
1119 const std::string& username,
1120 const std::string& password)
1121 : stun_servers(stun_servers),
1122 username(username),
1123 password(password) {
1124 if (!stun_servers.empty())
1125 stun_address = *(stun_servers.begin());
1126}
1127
1128ServerAddresses PortConfiguration::StunServers() {
1129 if (!stun_address.IsNil() &&
1130 stun_servers.find(stun_address) == stun_servers.end()) {
1131 stun_servers.insert(stun_address);
1132 }
deadbeefc5d0d952015-07-16 10:22:21 -07001133 // Every UDP TURN server should also be used as a STUN server.
1134 ServerAddresses turn_servers = GetRelayServerAddresses(RELAY_TURN, PROTO_UDP);
1135 for (const rtc::SocketAddress& turn_server : turn_servers) {
1136 if (stun_servers.find(turn_server) == stun_servers.end()) {
1137 stun_servers.insert(turn_server);
1138 }
1139 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140 return stun_servers;
1141}
1142
1143void PortConfiguration::AddRelay(const RelayServerConfig& config) {
1144 relays.push_back(config);
1145}
1146
1147bool PortConfiguration::SupportsProtocol(
1148 const RelayServerConfig& relay, ProtocolType type) const {
1149 PortList::const_iterator relay_port;
1150 for (relay_port = relay.ports.begin();
1151 relay_port != relay.ports.end();
1152 ++relay_port) {
1153 if (relay_port->proto == type)
1154 return true;
1155 }
1156 return false;
1157}
1158
1159bool PortConfiguration::SupportsProtocol(RelayType turn_type,
1160 ProtocolType type) const {
1161 for (size_t i = 0; i < relays.size(); ++i) {
1162 if (relays[i].type == turn_type &&
1163 SupportsProtocol(relays[i], type))
1164 return true;
1165 }
1166 return false;
1167}
1168
1169ServerAddresses PortConfiguration::GetRelayServerAddresses(
1170 RelayType turn_type, ProtocolType type) const {
1171 ServerAddresses servers;
1172 for (size_t i = 0; i < relays.size(); ++i) {
1173 if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) {
1174 servers.insert(relays[i].ports.front().address);
1175 }
1176 }
1177 return servers;
1178}
1179
1180} // namespace cricket