blob: 3c77b4feabaa4a63664dc76cdccf119ca77f91e3 [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;
honghaiz98db68f2015-09-29 07:58:17 -0700195 network_thread_->Post(this, MSG_CONFIG_STOP);
196 ClearGettingPorts();
197}
198
199void BasicPortAllocatorSession::ClearGettingPorts() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 network_thread_->Clear(this, MSG_ALLOCATE);
201 for (uint32 i = 0; i < sequences_.size(); ++i)
202 sequences_[i]->Stop();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203}
204
205void BasicPortAllocatorSession::OnMessage(rtc::Message *message) {
206 switch (message->message_id) {
207 case MSG_CONFIG_START:
208 ASSERT(rtc::Thread::Current() == network_thread_);
209 GetPortConfigurations();
210 break;
211
212 case MSG_CONFIG_READY:
213 ASSERT(rtc::Thread::Current() == network_thread_);
214 OnConfigReady(static_cast<PortConfiguration*>(message->pdata));
215 break;
216
217 case MSG_ALLOCATE:
218 ASSERT(rtc::Thread::Current() == network_thread_);
219 OnAllocate();
220 break;
221
222 case MSG_SHAKE:
223 ASSERT(rtc::Thread::Current() == network_thread_);
224 OnShake();
225 break;
226 case MSG_SEQUENCEOBJECTS_CREATED:
227 ASSERT(rtc::Thread::Current() == network_thread_);
228 OnAllocationSequenceObjectsCreated();
229 break;
230 case MSG_CONFIG_STOP:
231 ASSERT(rtc::Thread::Current() == network_thread_);
232 OnConfigStop();
233 break;
234 default:
235 ASSERT(false);
236 }
237}
238
239void BasicPortAllocatorSession::GetPortConfigurations() {
240 PortConfiguration* config = new PortConfiguration(allocator_->stun_servers(),
241 username(),
242 password());
243
244 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
245 config->AddRelay(allocator_->relays()[i]);
246 }
247 ConfigReady(config);
248}
249
250void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) {
251 network_thread_->Post(this, MSG_CONFIG_READY, config);
252}
253
254// Adds a configuration to the list.
255void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) {
256 if (config)
257 configs_.push_back(config);
258
259 AllocatePorts();
260}
261
262void BasicPortAllocatorSession::OnConfigStop() {
263 ASSERT(rtc::Thread::Current() == network_thread_);
264
265 // If any of the allocated ports have not completed the candidates allocation,
266 // mark those as error. Since session doesn't need any new candidates
267 // at this stage of the allocation, it's safe to discard any new candidates.
268 bool send_signal = false;
269 for (std::vector<PortData>::iterator it = ports_.begin();
270 it != ports_.end(); ++it) {
271 if (!it->complete()) {
272 // Updating port state to error, which didn't finish allocating candidates
273 // yet.
274 it->set_error();
275 send_signal = true;
276 }
277 }
278
279 // Did we stop any running sequences?
280 for (std::vector<AllocationSequence*>::iterator it = sequences_.begin();
281 it != sequences_.end() && !send_signal; ++it) {
282 if ((*it)->state() == AllocationSequence::kStopped) {
283 send_signal = true;
284 }
285 }
286
287 // If we stopped anything that was running, send a done signal now.
288 if (send_signal) {
289 MaybeSignalCandidatesAllocationDone();
290 }
291}
292
293void BasicPortAllocatorSession::AllocatePorts() {
294 ASSERT(rtc::Thread::Current() == network_thread_);
295 network_thread_->Post(this, MSG_ALLOCATE);
296}
297
298void BasicPortAllocatorSession::OnAllocate() {
299 if (network_manager_started_)
300 DoAllocate();
301
302 allocation_started_ = true;
303}
304
honghaiz8c404fa2015-09-28 07:59:43 -0700305void BasicPortAllocatorSession::GetNetworks(
306 std::vector<rtc::Network*>* networks) {
307 networks->clear();
308 rtc::NetworkManager* network_manager = allocator_->network_manager();
309 ASSERT(network_manager != nullptr);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700310 // If the network permission state is BLOCKED, we just act as if the flag has
311 // been passed in.
honghaiz8c404fa2015-09-28 07:59:43 -0700312 if (network_manager->enumeration_permission() ==
guoweisea1012b2015-08-21 09:06:28 -0700313 rtc::NetworkManager::ENUMERATION_BLOCKED) {
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700314 set_flags(flags() | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
315 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000316 // If the adapter enumeration is disabled, we'll just bind to any address
317 // instead of specific NIC. This is to ensure the same routing for http
318 // traffic by OS is also used here to avoid any local or public IP leakage
319 // during stun process.
320 if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) {
honghaiz8c404fa2015-09-28 07:59:43 -0700321 network_manager->GetAnyAddressNetworks(networks);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000322 } else {
honghaiz8c404fa2015-09-28 07:59:43 -0700323 network_manager->GetNetworks(networks);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000324 }
honghaiz8c404fa2015-09-28 07:59:43 -0700325}
326
327// For each network, see if we have a sequence that covers it already. If not,
328// create a new sequence to create the appropriate ports.
329void BasicPortAllocatorSession::DoAllocate() {
330 bool done_signal_needed = false;
331 std::vector<rtc::Network*> networks;
332 GetNetworks(&networks);
333
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000334 if (networks.empty()) {
335 LOG(LS_WARNING) << "Machine has no networks; no ports will be allocated";
336 done_signal_needed = true;
337 } else {
338 for (uint32 i = 0; i < networks.size(); ++i) {
339 PortConfiguration* config = NULL;
340 if (configs_.size() > 0)
341 config = configs_.back();
342
343 uint32 sequence_flags = flags();
344 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
345 // If all the ports are disabled we should just fire the allocation
346 // done event and return.
347 done_signal_needed = true;
348 break;
349 }
350
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000351 if (!config || config->relays.empty()) {
352 // No relay ports specified in this config.
353 sequence_flags |= PORTALLOCATOR_DISABLE_RELAY;
354 }
355
356 if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6) &&
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000357 networks[i]->GetBestIP().family() == AF_INET6) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000358 // Skip IPv6 networks unless the flag's been set.
359 continue;
360 }
361
362 // Disable phases that would only create ports equivalent to
363 // ones that we have already made.
364 DisableEquivalentPhases(networks[i], config, &sequence_flags);
365
366 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
367 // New AllocationSequence would have nothing to do, so don't make it.
368 continue;
369 }
370
371 AllocationSequence* sequence =
372 new AllocationSequence(this, networks[i], config, sequence_flags);
373 if (!sequence->Init()) {
374 delete sequence;
375 continue;
376 }
377 done_signal_needed = true;
378 sequence->SignalPortAllocationComplete.connect(
379 this, &BasicPortAllocatorSession::OnPortAllocationComplete);
380 if (running_)
381 sequence->Start();
382 sequences_.push_back(sequence);
383 }
384 }
385 if (done_signal_needed) {
386 network_thread_->Post(this, MSG_SEQUENCEOBJECTS_CREATED);
387 }
388}
389
390void BasicPortAllocatorSession::OnNetworksChanged() {
honghaiz8c404fa2015-09-28 07:59:43 -0700391 std::vector<rtc::Network*> networks;
392 GetNetworks(&networks);
393 for (AllocationSequence* sequence : sequences_) {
394 // Remove the network from the allocation sequence if it is not in
395 // |networks|.
396 if (!sequence->network_removed() &&
397 std::find(networks.begin(), networks.end(), sequence->network()) ==
398 networks.end()) {
399 sequence->OnNetworkRemoved();
400 }
401 }
402
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000403 network_manager_started_ = true;
404 if (allocation_started_)
405 DoAllocate();
406}
407
408void BasicPortAllocatorSession::DisableEquivalentPhases(
409 rtc::Network* network, PortConfiguration* config, uint32* flags) {
410 for (uint32 i = 0; i < sequences_.size() &&
411 (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES; ++i) {
412 sequences_[i]->DisableEquivalentPhases(network, config, flags);
413 }
414}
415
416void BasicPortAllocatorSession::AddAllocatedPort(Port* port,
417 AllocationSequence * seq,
418 bool prepare_address) {
419 if (!port)
420 return;
421
422 LOG(LS_INFO) << "Adding allocated port for " << content_name();
423 port->set_content_name(content_name());
424 port->set_component(component_);
425 port->set_generation(generation());
426 if (allocator_->proxy().type != rtc::PROXY_NONE)
427 port->set_proxy(allocator_->user_agent(), allocator_->proxy());
428 port->set_send_retransmit_count_attribute((allocator_->flags() &
429 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE) != 0);
430
431 // Push down the candidate_filter to individual port.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000432 uint32 candidate_filter = allocator_->candidate_filter();
433
434 // When adapter enumeration is disabled, disable CF_HOST at port level so
435 // local address is not leaked by stunport in the candidate's related address.
436 if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) {
437 candidate_filter &= ~CF_HOST;
438 }
439 port->set_candidate_filter(candidate_filter);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000440
441 PortData data(port, seq);
442 ports_.push_back(data);
443
444 port->SignalCandidateReady.connect(
445 this, &BasicPortAllocatorSession::OnCandidateReady);
446 port->SignalPortComplete.connect(this,
447 &BasicPortAllocatorSession::OnPortComplete);
448 port->SignalDestroyed.connect(this,
449 &BasicPortAllocatorSession::OnPortDestroyed);
450 port->SignalPortError.connect(
451 this, &BasicPortAllocatorSession::OnPortError);
452 LOG_J(LS_INFO, port) << "Added port to allocator";
453
454 if (prepare_address)
455 port->PrepareAddress();
456}
457
458void BasicPortAllocatorSession::OnAllocationSequenceObjectsCreated() {
459 allocation_sequences_created_ = true;
460 // Send candidate allocation complete signal if we have no sequences.
461 MaybeSignalCandidatesAllocationDone();
462}
463
464void BasicPortAllocatorSession::OnCandidateReady(
465 Port* port, const Candidate& c) {
466 ASSERT(rtc::Thread::Current() == network_thread_);
467 PortData* data = FindPort(port);
468 ASSERT(data != NULL);
469 // Discarding any candidate signal if port allocation status is
470 // already in completed state.
471 if (data->complete())
472 return;
473
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000474 ProtocolType pvalue;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700475 bool candidate_signalable = CheckCandidateFilter(c);
476 bool candidate_pairable =
477 candidate_signalable ||
478 (c.address().IsAnyIP() &&
479 (port->SharedSocket() || c.protocol() == TCP_PROTOCOL_NAME));
480 bool candidate_protocol_enabled =
481 StringToProto(c.protocol().c_str(), &pvalue) &&
482 data->sequence()->ProtocolEnabled(pvalue);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000483
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700484 if (candidate_signalable && candidate_protocol_enabled) {
485 std::vector<Candidate> candidates;
486 candidates.push_back(c);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000487 SignalCandidatesReady(this, candidates);
488 }
489
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700490 // Port has been made ready. Nothing to do here.
491 if (data->ready()) {
492 return;
493 }
494
495 // Move the port to the READY state, either because we have a usable candidate
496 // from the port, or simply because the port is bound to the any address and
497 // therefore has no host candidate. This will trigger the port to start
498 // creating candidate pairs (connections) and issue connectivity checks.
499 if (candidate_pairable) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000500 data->set_ready();
501 SignalPortReady(this, port);
502 }
503}
504
505void BasicPortAllocatorSession::OnPortComplete(Port* port) {
506 ASSERT(rtc::Thread::Current() == network_thread_);
507 PortData* data = FindPort(port);
508 ASSERT(data != NULL);
509
510 // Ignore any late signals.
511 if (data->complete())
512 return;
513
514 // Moving to COMPLETE state.
515 data->set_complete();
516 // Send candidate allocation complete signal if this was the last port.
517 MaybeSignalCandidatesAllocationDone();
518}
519
520void BasicPortAllocatorSession::OnPortError(Port* port) {
521 ASSERT(rtc::Thread::Current() == network_thread_);
522 PortData* data = FindPort(port);
523 ASSERT(data != NULL);
524 // We might have already given up on this port and stopped it.
525 if (data->complete())
526 return;
527
528 // SignalAddressError is currently sent from StunPort/TurnPort.
529 // But this signal itself is generic.
530 data->set_error();
531 // Send candidate allocation complete signal if this was the last port.
532 MaybeSignalCandidatesAllocationDone();
533}
534
535void BasicPortAllocatorSession::OnProtocolEnabled(AllocationSequence* seq,
536 ProtocolType proto) {
537 std::vector<Candidate> candidates;
538 for (std::vector<PortData>::iterator it = ports_.begin();
539 it != ports_.end(); ++it) {
540 if (it->sequence() != seq)
541 continue;
542
543 const std::vector<Candidate>& potentials = it->port()->Candidates();
544 for (size_t i = 0; i < potentials.size(); ++i) {
545 if (!CheckCandidateFilter(potentials[i]))
546 continue;
547 ProtocolType pvalue;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700548 bool candidate_protocol_enabled =
549 StringToProto(potentials[i].protocol().c_str(), &pvalue) &&
550 pvalue == proto;
551 if (candidate_protocol_enabled) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000552 candidates.push_back(potentials[i]);
553 }
554 }
555 }
556
557 if (!candidates.empty()) {
558 SignalCandidatesReady(this, candidates);
559 }
560}
561
562bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) {
563 uint32 filter = allocator_->candidate_filter();
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000564
565 // When binding to any address, before sending packets out, the getsockname
566 // returns all 0s, but after sending packets, it'll be the NIC used to
567 // send. All 0s is not a valid ICE candidate address and should be filtered
568 // out.
569 if (c.address().IsAnyIP()) {
570 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000571 }
572
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000573 if (c.type() == RELAY_PORT_TYPE) {
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +0000574 return ((filter & CF_RELAY) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000575 } else if (c.type() == STUN_PORT_TYPE) {
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +0000576 return ((filter & CF_REFLEXIVE) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000577 } else if (c.type() == LOCAL_PORT_TYPE) {
578 if ((filter & CF_REFLEXIVE) && !c.address().IsPrivateIP()) {
579 // We allow host candidates if the filter allows server-reflexive
580 // candidates and the candidate is a public IP. Because we don't generate
581 // server-reflexive candidates if they have the same IP as the host
582 // candidate (i.e. when the host candidate is a public IP), filtering to
583 // only server-reflexive candidates won't work right when the host
584 // candidates have public IPs.
585 return true;
586 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000587
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700588 // If PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE is specified and it's
589 // loopback address, we should allow it as it's for demo page connectivity
590 // when no TURN/STUN specified.
591 if (c.address().IsLoopbackIP() &&
592 (flags() & PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE) != 0) {
593 return true;
594 }
595
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000596 // This is just to prevent the case when binding to any address (all 0s), if
597 // somehow the host candidate address is not all 0s. Either because local
598 // installed proxy changes the address or a packet has been sent for any
599 // reason before getsockname is called.
600 if (flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) {
601 LOG(LS_WARNING) << "Received non-0 host address: "
602 << c.address().ToString()
603 << " when adapter enumeration is disabled";
604 return false;
605 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000606
guoweis@webrtc.org931e0cf2015-02-18 19:09:42 +0000607 return ((filter & CF_HOST) != 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000608 }
609 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000610}
611
612void BasicPortAllocatorSession::OnPortAllocationComplete(
613 AllocationSequence* seq) {
614 // Send candidate allocation complete signal if all ports are done.
615 MaybeSignalCandidatesAllocationDone();
616}
617
618void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() {
619 // Send signal only if all required AllocationSequence objects
620 // are created.
621 if (!allocation_sequences_created_)
622 return;
623
624 // Check that all port allocation sequences are complete.
625 for (std::vector<AllocationSequence*>::iterator it = sequences_.begin();
626 it != sequences_.end(); ++it) {
627 if ((*it)->state() == AllocationSequence::kRunning)
628 return;
629 }
630
631 // If all allocated ports are in complete state, session must have got all
632 // expected candidates. Session will trigger candidates allocation complete
633 // signal.
634 for (std::vector<PortData>::iterator it = ports_.begin();
635 it != ports_.end(); ++it) {
636 if (!it->complete())
637 return;
638 }
639 LOG(LS_INFO) << "All candidates gathered for " << content_name_ << ":"
640 << component_ << ":" << generation();
641 SignalCandidatesAllocationDone(this);
642}
643
644void BasicPortAllocatorSession::OnPortDestroyed(
645 PortInterface* port) {
646 ASSERT(rtc::Thread::Current() == network_thread_);
647 for (std::vector<PortData>::iterator iter = ports_.begin();
648 iter != ports_.end(); ++iter) {
649 if (port == iter->port()) {
650 ports_.erase(iter);
651 LOG_J(LS_INFO, port) << "Removed port from allocator ("
652 << static_cast<int>(ports_.size()) << " remaining)";
653 return;
654 }
655 }
656 ASSERT(false);
657}
658
659void BasicPortAllocatorSession::OnShake() {
660 LOG(INFO) << ">>>>> SHAKE <<<<< >>>>> SHAKE <<<<< >>>>> SHAKE <<<<<";
661
662 std::vector<Port*> ports;
663 std::vector<Connection*> connections;
664
665 for (size_t i = 0; i < ports_.size(); ++i) {
666 if (ports_[i].ready())
667 ports.push_back(ports_[i].port());
668 }
669
670 for (size_t i = 0; i < ports.size(); ++i) {
671 Port::AddressMap::const_iterator iter;
672 for (iter = ports[i]->connections().begin();
673 iter != ports[i]->connections().end();
674 ++iter) {
675 connections.push_back(iter->second);
676 }
677 }
678
679 LOG(INFO) << ">>>>> Destroying " << ports.size() << " ports and "
680 << connections.size() << " connections";
681
682 for (size_t i = 0; i < connections.size(); ++i)
683 connections[i]->Destroy();
684
685 if (running_ || (ports.size() > 0) || (connections.size() > 0))
686 network_thread_->PostDelayed(ShakeDelay(), this, MSG_SHAKE);
687}
688
689BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort(
690 Port* port) {
691 for (std::vector<PortData>::iterator it = ports_.begin();
692 it != ports_.end(); ++it) {
693 if (it->port() == port) {
694 return &*it;
695 }
696 }
697 return NULL;
698}
699
700// AllocationSequence
701
702AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session,
703 rtc::Network* network,
704 PortConfiguration* config,
705 uint32 flags)
706 : session_(session),
707 network_(network),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000708 ip_(network->GetBestIP()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000709 config_(config),
710 state_(kInit),
711 flags_(flags),
712 udp_socket_(),
713 udp_port_(NULL),
714 phase_(0) {
715}
716
717bool AllocationSequence::Init() {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000718 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
719 udp_socket_.reset(session_->socket_factory()->CreateUdpSocket(
720 rtc::SocketAddress(ip_, 0), session_->allocator()->min_port(),
721 session_->allocator()->max_port()));
722 if (udp_socket_) {
723 udp_socket_->SignalReadPacket.connect(
724 this, &AllocationSequence::OnReadPacket);
725 }
726 // Continuing if |udp_socket_| is NULL, as local TCP and RelayPort using TCP
727 // are next available options to setup a communication channel.
728 }
729 return true;
730}
731
732void AllocationSequence::Clear() {
733 udp_port_ = NULL;
734 turn_ports_.clear();
735}
736
honghaiz8c404fa2015-09-28 07:59:43 -0700737void AllocationSequence::OnNetworkRemoved() {
738 // Stop the allocation sequence if its network is gone.
739 Stop();
740 network_removed_ = true;
741}
742
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000743AllocationSequence::~AllocationSequence() {
744 session_->network_thread()->Clear(this);
745}
746
747void AllocationSequence::DisableEquivalentPhases(rtc::Network* network,
748 PortConfiguration* config, uint32* flags) {
honghaiz8c404fa2015-09-28 07:59:43 -0700749 if (network_removed_) {
750 // If the network of this allocation sequence has ever gone away,
751 // it won't be equivalent to the new network.
752 return;
753 }
754
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000755 if (!((network == network_) && (ip_ == network->GetBestIP()))) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000756 // Different network setup; nothing is equivalent.
757 return;
758 }
759
760 // Else turn off the stuff that we've already got covered.
761
762 // Every config implicitly specifies local, so turn that off right away.
763 *flags |= PORTALLOCATOR_DISABLE_UDP;
764 *flags |= PORTALLOCATOR_DISABLE_TCP;
765
766 if (config_ && config) {
767 if (config_->StunServers() == config->StunServers()) {
768 // Already got this STUN servers covered.
769 *flags |= PORTALLOCATOR_DISABLE_STUN;
770 }
771 if (!config_->relays.empty()) {
772 // Already got relays covered.
773 // NOTE: This will even skip a _different_ set of relay servers if we
774 // were to be given one, but that never happens in our codebase. Should
775 // probably get rid of the list in PortConfiguration and just keep a
776 // single relay server in each one.
777 *flags |= PORTALLOCATOR_DISABLE_RELAY;
778 }
779 }
780}
781
782void AllocationSequence::Start() {
783 state_ = kRunning;
784 session_->network_thread()->Post(this, MSG_ALLOCATION_PHASE);
785}
786
787void AllocationSequence::Stop() {
788 // If the port is completed, don't set it to stopped.
789 if (state_ == kRunning) {
790 state_ = kStopped;
791 session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
792 }
793}
794
795void AllocationSequence::OnMessage(rtc::Message* msg) {
796 ASSERT(rtc::Thread::Current() == session_->network_thread());
797 ASSERT(msg->message_id == MSG_ALLOCATION_PHASE);
798
799 const char* const PHASE_NAMES[kNumPhases] = {
800 "Udp", "Relay", "Tcp", "SslTcp"
801 };
802
803 // Perform all of the phases in the current step.
804 LOG_J(LS_INFO, network_) << "Allocation Phase="
805 << PHASE_NAMES[phase_];
806
807 switch (phase_) {
808 case PHASE_UDP:
809 CreateUDPPorts();
810 CreateStunPorts();
811 EnableProtocol(PROTO_UDP);
812 break;
813
814 case PHASE_RELAY:
815 CreateRelayPorts();
816 break;
817
818 case PHASE_TCP:
819 CreateTCPPorts();
820 EnableProtocol(PROTO_TCP);
821 break;
822
823 case PHASE_SSLTCP:
824 state_ = kCompleted;
825 EnableProtocol(PROTO_SSLTCP);
826 break;
827
828 default:
829 ASSERT(false);
830 }
831
832 if (state() == kRunning) {
833 ++phase_;
834 session_->network_thread()->PostDelayed(
835 session_->allocator()->step_delay(),
836 this, MSG_ALLOCATION_PHASE);
837 } else {
838 // If all phases in AllocationSequence are completed, no allocation
839 // steps needed further. Canceling pending signal.
840 session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
841 SignalPortAllocationComplete(this);
842 }
843}
844
845void AllocationSequence::EnableProtocol(ProtocolType proto) {
846 if (!ProtocolEnabled(proto)) {
847 protocols_.push_back(proto);
848 session_->OnProtocolEnabled(this, proto);
849 }
850}
851
852bool AllocationSequence::ProtocolEnabled(ProtocolType proto) const {
853 for (ProtocolList::const_iterator it = protocols_.begin();
854 it != protocols_.end(); ++it) {
855 if (*it == proto)
856 return true;
857 }
858 return false;
859}
860
861void AllocationSequence::CreateUDPPorts() {
862 if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP)) {
863 LOG(LS_VERBOSE) << "AllocationSequence: UDP ports disabled, skipping.";
864 return;
865 }
866
867 // TODO(mallinath) - Remove UDPPort creating socket after shared socket
868 // is enabled completely.
869 UDPPort* port = NULL;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700870 bool emit_localhost_for_anyaddress =
871 IsFlagSet(PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000872 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700873 port = UDPPort::Create(
874 session_->network_thread(), session_->socket_factory(), network_,
875 udp_socket_.get(), session_->username(), session_->password(),
876 session_->allocator()->origin(), emit_localhost_for_anyaddress);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000877 } else {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700878 port = UDPPort::Create(
879 session_->network_thread(), session_->socket_factory(), network_, ip_,
880 session_->allocator()->min_port(), session_->allocator()->max_port(),
881 session_->username(), session_->password(),
882 session_->allocator()->origin(), emit_localhost_for_anyaddress);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000883 }
884
885 if (port) {
886 // If shared socket is enabled, STUN candidate will be allocated by the
887 // UDPPort.
888 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
889 udp_port_ = port;
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +0000890 port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000891
892 // If STUN is not disabled, setting stun server address to port.
893 if (!IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000894 if (config_ && !config_->StunServers().empty()) {
895 LOG(LS_INFO) << "AllocationSequence: UDPPort will be handling the "
896 << "STUN candidate generation.";
897 port->set_server_addresses(config_->StunServers());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000898 }
899 }
900 }
901
902 session_->AddAllocatedPort(port, this, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000903 }
904}
905
906void AllocationSequence::CreateTCPPorts() {
907 if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) {
908 LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping.";
909 return;
910 }
911
912 Port* port = TCPPort::Create(session_->network_thread(),
913 session_->socket_factory(),
914 network_, ip_,
915 session_->allocator()->min_port(),
916 session_->allocator()->max_port(),
917 session_->username(), session_->password(),
918 session_->allocator()->allow_tcp_listen());
919 if (port) {
920 session_->AddAllocatedPort(port, this, true);
921 // Since TCPPort is not created using shared socket, |port| will not be
922 // added to the dequeue.
923 }
924}
925
926void AllocationSequence::CreateStunPorts() {
927 if (IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
928 LOG(LS_VERBOSE) << "AllocationSequence: STUN ports disabled, skipping.";
929 return;
930 }
931
932 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
933 return;
934 }
935
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000936 if (!(config_ && !config_->StunServers().empty())) {
937 LOG(LS_WARNING)
938 << "AllocationSequence: No STUN server configured, skipping.";
939 return;
940 }
941
942 StunPort* port = StunPort::Create(session_->network_thread(),
943 session_->socket_factory(),
944 network_, ip_,
945 session_->allocator()->min_port(),
946 session_->allocator()->max_port(),
947 session_->username(), session_->password(),
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000948 config_->StunServers(),
949 session_->allocator()->origin());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000950 if (port) {
951 session_->AddAllocatedPort(port, this, true);
952 // Since StunPort is not created using shared socket, |port| will not be
953 // added to the dequeue.
954 }
955}
956
957void AllocationSequence::CreateRelayPorts() {
958 if (IsFlagSet(PORTALLOCATOR_DISABLE_RELAY)) {
959 LOG(LS_VERBOSE) << "AllocationSequence: Relay ports disabled, skipping.";
960 return;
961 }
962
963 // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we
964 // ought to have a relay list for them here.
965 ASSERT(config_ && !config_->relays.empty());
966 if (!(config_ && !config_->relays.empty())) {
967 LOG(LS_WARNING)
968 << "AllocationSequence: No relay server configured, skipping.";
969 return;
970 }
971
972 PortConfiguration::RelayList::const_iterator relay;
973 for (relay = config_->relays.begin();
974 relay != config_->relays.end(); ++relay) {
975 if (relay->type == RELAY_GTURN) {
976 CreateGturnPort(*relay);
977 } else if (relay->type == RELAY_TURN) {
978 CreateTurnPort(*relay);
979 } else {
980 ASSERT(false);
981 }
982 }
983}
984
985void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) {
986 // TODO(mallinath) - Rename RelayPort to GTurnPort.
987 RelayPort* port = RelayPort::Create(session_->network_thread(),
988 session_->socket_factory(),
989 network_, ip_,
990 session_->allocator()->min_port(),
991 session_->allocator()->max_port(),
992 config_->username, config_->password);
993 if (port) {
994 // Since RelayPort is not created using shared socket, |port| will not be
995 // added to the dequeue.
996 // Note: We must add the allocated port before we add addresses because
997 // the latter will create candidates that need name and preference
998 // settings. However, we also can't prepare the address (normally
999 // done by AddAllocatedPort) until we have these addresses. So we
1000 // wait to do that until below.
1001 session_->AddAllocatedPort(port, this, false);
1002
1003 // Add the addresses of this protocol.
1004 PortList::const_iterator relay_port;
1005 for (relay_port = config.ports.begin();
1006 relay_port != config.ports.end();
1007 ++relay_port) {
1008 port->AddServerAddress(*relay_port);
1009 port->AddExternalAddress(*relay_port);
1010 }
1011 // Start fetching an address for this port.
1012 port->PrepareAddress();
1013 }
1014}
1015
1016void AllocationSequence::CreateTurnPort(const RelayServerConfig& config) {
1017 PortList::const_iterator relay_port;
1018 for (relay_port = config.ports.begin();
1019 relay_port != config.ports.end(); ++relay_port) {
1020 TurnPort* port = NULL;
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001021
1022 // Skip UDP connections to relay servers if it's disallowed.
1023 if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP_RELAY) &&
1024 relay_port->proto == PROTO_UDP) {
1025 continue;
1026 }
1027
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001028 // Shared socket mode must be enabled only for UDP based ports. Hence
1029 // don't pass shared socket for ports which will create TCP sockets.
1030 // TODO(mallinath) - Enable shared socket mode for TURN ports. Disabled
1031 // due to webrtc bug https://code.google.com/p/webrtc/issues/detail?id=3537
1032 if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) &&
honghaizf421bdc2015-07-17 16:21:55 -07001033 relay_port->proto == PROTO_UDP && udp_socket_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001034 port = TurnPort::Create(session_->network_thread(),
1035 session_->socket_factory(),
1036 network_, udp_socket_.get(),
1037 session_->username(), session_->password(),
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +00001038 *relay_port, config.credentials, config.priority,
1039 session_->allocator()->origin());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001040 turn_ports_.push_back(port);
1041 // Listen to the port destroyed signal, to allow AllocationSequence to
1042 // remove entrt from it's map.
1043 port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed);
1044 } else {
1045 port = TurnPort::Create(session_->network_thread(),
1046 session_->socket_factory(),
1047 network_, ip_,
1048 session_->allocator()->min_port(),
1049 session_->allocator()->max_port(),
1050 session_->username(),
1051 session_->password(),
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +00001052 *relay_port, config.credentials, config.priority,
1053 session_->allocator()->origin());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001054 }
1055 ASSERT(port != NULL);
1056 session_->AddAllocatedPort(port, this, true);
1057 }
1058}
1059
1060void AllocationSequence::OnReadPacket(
1061 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
1062 const rtc::SocketAddress& remote_addr,
1063 const rtc::PacketTime& packet_time) {
1064 ASSERT(socket == udp_socket_.get());
1065
1066 bool turn_port_found = false;
1067
1068 // Try to find the TurnPort that matches the remote address. Note that the
1069 // message could be a STUN binding response if the TURN server is also used as
1070 // a STUN server. We don't want to parse every message here to check if it is
1071 // a STUN binding response, so we pass the message to TurnPort regardless of
1072 // the message type. The TurnPort will just ignore the message since it will
1073 // not find any request by transaction ID.
1074 for (std::vector<TurnPort*>::const_iterator it = turn_ports_.begin();
1075 it != turn_ports_.end(); ++it) {
1076 TurnPort* port = *it;
1077 if (port->server_address().address == remote_addr) {
1078 port->HandleIncomingPacket(socket, data, size, remote_addr, packet_time);
1079 turn_port_found = true;
1080 break;
1081 }
1082 }
1083
1084 if (udp_port_) {
1085 const ServerAddresses& stun_servers = udp_port_->server_addresses();
1086
1087 // Pass the packet to the UdpPort if there is no matching TurnPort, or if
1088 // the TURN server is also a STUN server.
1089 if (!turn_port_found ||
1090 stun_servers.find(remote_addr) != stun_servers.end()) {
1091 udp_port_->HandleIncomingPacket(
1092 socket, data, size, remote_addr, packet_time);
1093 }
1094 }
1095}
1096
1097void AllocationSequence::OnPortDestroyed(PortInterface* port) {
1098 if (udp_port_ == port) {
1099 udp_port_ = NULL;
1100 return;
1101 }
1102
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001103 auto it = std::find(turn_ports_.begin(), turn_ports_.end(), port);
1104 if (it != turn_ports_.end()) {
1105 turn_ports_.erase(it);
1106 } else {
1107 LOG(LS_ERROR) << "Unexpected OnPortDestroyed for nonexistent port.";
1108 ASSERT(false);
1109 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001110}
1111
1112// PortConfiguration
1113PortConfiguration::PortConfiguration(
1114 const rtc::SocketAddress& stun_address,
1115 const std::string& username,
1116 const std::string& password)
1117 : stun_address(stun_address), username(username), password(password) {
1118 if (!stun_address.IsNil())
1119 stun_servers.insert(stun_address);
1120}
1121
1122PortConfiguration::PortConfiguration(const ServerAddresses& stun_servers,
1123 const std::string& username,
1124 const std::string& password)
1125 : stun_servers(stun_servers),
1126 username(username),
1127 password(password) {
1128 if (!stun_servers.empty())
1129 stun_address = *(stun_servers.begin());
1130}
1131
1132ServerAddresses PortConfiguration::StunServers() {
1133 if (!stun_address.IsNil() &&
1134 stun_servers.find(stun_address) == stun_servers.end()) {
1135 stun_servers.insert(stun_address);
1136 }
deadbeefc5d0d952015-07-16 10:22:21 -07001137 // Every UDP TURN server should also be used as a STUN server.
1138 ServerAddresses turn_servers = GetRelayServerAddresses(RELAY_TURN, PROTO_UDP);
1139 for (const rtc::SocketAddress& turn_server : turn_servers) {
1140 if (stun_servers.find(turn_server) == stun_servers.end()) {
1141 stun_servers.insert(turn_server);
1142 }
1143 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001144 return stun_servers;
1145}
1146
1147void PortConfiguration::AddRelay(const RelayServerConfig& config) {
1148 relays.push_back(config);
1149}
1150
1151bool PortConfiguration::SupportsProtocol(
1152 const RelayServerConfig& relay, ProtocolType type) const {
1153 PortList::const_iterator relay_port;
1154 for (relay_port = relay.ports.begin();
1155 relay_port != relay.ports.end();
1156 ++relay_port) {
1157 if (relay_port->proto == type)
1158 return true;
1159 }
1160 return false;
1161}
1162
1163bool PortConfiguration::SupportsProtocol(RelayType turn_type,
1164 ProtocolType type) const {
1165 for (size_t i = 0; i < relays.size(); ++i) {
1166 if (relays[i].type == turn_type &&
1167 SupportsProtocol(relays[i], type))
1168 return true;
1169 }
1170 return false;
1171}
1172
1173ServerAddresses PortConfiguration::GetRelayServerAddresses(
1174 RelayType turn_type, ProtocolType type) const {
1175 ServerAddresses servers;
1176 for (size_t i = 0; i < relays.size(); ++i) {
1177 if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) {
1178 servers.insert(relays[i].ports.front().address);
1179 }
1180 }
1181 return servers;
1182}
1183
1184} // namespace cricket