1 /* 2 * libjingle 3 * Copyright 2004--2011, Google Inc. 4 * 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 30 #include "talk/p2p/base/basicpacketsocketfactory.h" 31 #include "talk/p2p/client/basicportallocator.h" 32 #include "webrtc/base/logging.h" 33 #include "webrtc/base/network.h" 34 #include "webrtc/base/thread.h" 35 36 namespace webrtc { 37 38 using rtc::scoped_ptr; 39 40 rtc::scoped_refptr<PortAllocatorFactoryInterface> 41 PortAllocatorFactory::Create( 42 rtc::Thread* worker_thread) { 43 rtc::RefCountedObject<PortAllocatorFactory>* allocator = 44 new rtc::RefCountedObject<PortAllocatorFactory>(worker_thread); 45 return allocator; 46 } 47 48 PortAllocatorFactory::PortAllocatorFactory(rtc::Thread* worker_thread) 49 : network_manager_(new rtc::BasicNetworkManager()), 50 socket_factory_(new rtc::BasicPacketSocketFactory(worker_thread)) { 51 } 52 53 PortAllocatorFactory::~PortAllocatorFactory() {} 54 55 cricket::PortAllocator* PortAllocatorFactory::CreatePortAllocator( 56 const std::vector<StunConfiguration>& stun, 57 const std::vector<TurnConfiguration>& turn) { 58 cricket::ServerAddresses stun_hosts; 59 typedef std::vector<StunConfiguration>::const_iterator StunIt; 60 for (StunIt stun_it = stun.begin(); stun_it != stun.end(); ++stun_it) { 61 stun_hosts.insert(stun_it->server); 62 } 63 64 scoped_ptr<cricket::BasicPortAllocator> allocator( 65 new cricket::BasicPortAllocator( 66 network_manager_.get(), socket_factory_.get(), stun_hosts)); 67 68 for (size_t i = 0; i < turn.size(); ++i) { 69 cricket::RelayCredentials credentials(turn[i].username, turn[i].password); 70 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN); 71 cricket::ProtocolType protocol; 72 if (cricket::StringToProto(turn[i].transport_type.c_str(), &protocol)) { 73 relay_server.ports.push_back(cricket::ProtocolAddress( 74 turn[i].server, protocol, turn[i].secure)); 75 relay_server.credentials = credentials; 76 // First in the list gets highest priority. 77 relay_server.priority = static_cast<int>(turn.size() - i - 1); 78 allocator->AddRelay(relay_server); 79 } else { 80 LOG(LS_WARNING) << "Ignoring TURN server " << turn[i].server << ". " 81 << "Reason= Incorrect " << turn[i].transport_type 82 << " transport parameter."; 83 } 84 } 85 return allocator.release(); 86 } 87 88 } // namespace webrtc 89