henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2004--2011 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 30 | #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 31 | #include "webrtc/p2p/client/basicportallocator.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 32 | #include "webrtc/base/logging.h" |
| 33 | #include "webrtc/base/network.h" |
| 34 | #include "webrtc/base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | namespace webrtc { |
| 37 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 38 | using rtc::scoped_ptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 40 | rtc::scoped_refptr<PortAllocatorFactoryInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | PortAllocatorFactory::Create( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 42 | rtc::Thread* worker_thread) { |
| 43 | rtc::RefCountedObject<PortAllocatorFactory>* allocator = |
| 44 | new rtc::RefCountedObject<PortAllocatorFactory>(worker_thread); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | return allocator; |
| 46 | } |
| 47 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 48 | PortAllocatorFactory::PortAllocatorFactory(rtc::Thread* worker_thread) |
| 49 | : network_manager_(new rtc::BasicNetworkManager()), |
| 50 | socket_factory_(new rtc::BasicPacketSocketFactory(worker_thread)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | PortAllocatorFactory::~PortAllocatorFactory() {} |
| 54 | |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 55 | void PortAllocatorFactory::SetNetworkIgnoreMask(int network_ignore_mask) { |
| 56 | network_manager_->set_network_ignore_mask(network_ignore_mask); |
| 57 | } |
| 58 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | cricket::PortAllocator* PortAllocatorFactory::CreatePortAllocator( |
| 60 | const std::vector<StunConfiguration>& stun, |
| 61 | const std::vector<TurnConfiguration>& turn) { |
buildbot@webrtc.org | 51c5508 | 2014-07-28 22:26:15 +0000 | [diff] [blame] | 62 | cricket::ServerAddresses stun_hosts; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | typedef std::vector<StunConfiguration>::const_iterator StunIt; |
| 64 | for (StunIt stun_it = stun.begin(); stun_it != stun.end(); ++stun_it) { |
buildbot@webrtc.org | 51c5508 | 2014-07-28 22:26:15 +0000 | [diff] [blame] | 65 | stun_hosts.insert(stun_it->server); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | } |
| 67 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | scoped_ptr<cricket::BasicPortAllocator> allocator( |
| 69 | new cricket::BasicPortAllocator( |
buildbot@webrtc.org | 51c5508 | 2014-07-28 22:26:15 +0000 | [diff] [blame] | 70 | network_manager_.get(), socket_factory_.get(), stun_hosts)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 72 | for (size_t i = 0; i < turn.size(); ++i) { |
| 73 | cricket::RelayCredentials credentials(turn[i].username, turn[i].password); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 74 | cricket::RelayServerConfig turn_server(cricket::RELAY_TURN); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | cricket::ProtocolType protocol; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 76 | if (cricket::StringToProto(turn[i].transport_type.c_str(), &protocol)) { |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 77 | turn_server.ports.push_back( |
| 78 | cricket::ProtocolAddress(turn[i].server, protocol, turn[i].secure)); |
| 79 | turn_server.credentials = credentials; |
mallinath@webrtc.org | e5995aa | 2014-07-17 18:23:52 +0000 | [diff] [blame] | 80 | // First in the list gets highest priority. |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 81 | turn_server.priority = static_cast<int>(turn.size() - i - 1); |
| 82 | allocator->AddTurnServer(turn_server); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | } else { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 84 | LOG(LS_WARNING) << "Ignoring TURN server " << turn[i].server << ". " |
| 85 | << "Reason= Incorrect " << turn[i].transport_type |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | << " transport parameter."; |
| 87 | } |
| 88 | } |
| 89 | return allocator.release(); |
| 90 | } |
| 91 | |
| 92 | } // namespace webrtc |