blob: 33e0e63102051ab539e032e14e2472cca4194fd2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2004--2011 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/portallocatorfactory.h"
29
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030#include "webrtc/p2p/base/basicpacketsocketfactory.h"
31#include "webrtc/p2p/client/basicportallocator.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/logging.h"
33#include "webrtc/base/network.h"
34#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036namespace webrtc {
37
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000038using rtc::scoped_ptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000040rtc::scoped_refptr<PortAllocatorFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041PortAllocatorFactory::Create(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000042 rtc::Thread* worker_thread) {
43 rtc::RefCountedObject<PortAllocatorFactory>* allocator =
44 new rtc::RefCountedObject<PortAllocatorFactory>(worker_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 return allocator;
46}
47
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000048PortAllocatorFactory::PortAllocatorFactory(rtc::Thread* worker_thread)
49 : network_manager_(new rtc::BasicNetworkManager()),
50 socket_factory_(new rtc::BasicPacketSocketFactory(worker_thread)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051}
52
53PortAllocatorFactory::~PortAllocatorFactory() {}
54
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000055void PortAllocatorFactory::SetNetworkIgnoreMask(int network_ignore_mask) {
56 network_manager_->set_network_ignore_mask(network_ignore_mask);
57}
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059cricket::PortAllocator* PortAllocatorFactory::CreatePortAllocator(
60 const std::vector<StunConfiguration>& stun,
61 const std::vector<TurnConfiguration>& turn) {
buildbot@webrtc.org51c55082014-07-28 22:26:15 +000062 cricket::ServerAddresses stun_hosts;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 typedef std::vector<StunConfiguration>::const_iterator StunIt;
64 for (StunIt stun_it = stun.begin(); stun_it != stun.end(); ++stun_it) {
buildbot@webrtc.org51c55082014-07-28 22:26:15 +000065 stun_hosts.insert(stun_it->server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 }
67
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 scoped_ptr<cricket::BasicPortAllocator> allocator(
69 new cricket::BasicPortAllocator(
buildbot@webrtc.org51c55082014-07-28 22:26:15 +000070 network_manager_.get(), socket_factory_.get(), stun_hosts));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000072 for (size_t i = 0; i < turn.size(); ++i) {
73 cricket::RelayCredentials credentials(turn[i].username, turn[i].password);
deadbeef653b8e02015-11-11 12:55:10 -080074 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 cricket::ProtocolType protocol;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000076 if (cricket::StringToProto(turn[i].transport_type.c_str(), &protocol)) {
deadbeef653b8e02015-11-11 12:55:10 -080077 turn_server.ports.push_back(
78 cricket::ProtocolAddress(turn[i].server, protocol, turn[i].secure));
79 turn_server.credentials = credentials;
mallinath@webrtc.orge5995aa2014-07-17 18:23:52 +000080 // First in the list gets highest priority.
deadbeef653b8e02015-11-11 12:55:10 -080081 turn_server.priority = static_cast<int>(turn.size() - i - 1);
82 allocator->AddTurnServer(turn_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 } else {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000084 LOG(LS_WARNING) << "Ignoring TURN server " << turn[i].server << ". "
85 << "Reason= Incorrect " << turn[i].transport_type
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 << " transport parameter.";
87 }
88 }
89 return allocator.release();
90}
91
92} // namespace webrtc