OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/host_port_allocator.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" |
| 9 #include "base/string_number_conversions.h" |
| 10 #include "googleurl/src/gurl.h" |
| 11 #include "net/http/http_status_code.h" |
| 12 #include "net/url_request/url_request_context_getter.h" |
| 13 #include "remoting/host/network_settings.h" |
| 14 #include "third_party/libjingle/source/talk/base/basicpacketsocketfactory.h" |
| 15 |
| 16 namespace remoting { |
| 17 |
| 18 namespace { |
| 19 |
| 20 class HostPortAllocatorSession |
| 21 : public cricket::HttpPortAllocatorSessionBase { |
| 22 public: |
| 23 HostPortAllocatorSession( |
| 24 cricket::HttpPortAllocatorBase* allocator, |
| 25 const std::string& channel_name, |
| 26 int component, |
| 27 const std::vector<talk_base::SocketAddress>& stun_hosts, |
| 28 const std::vector<std::string>& relay_hosts, |
| 29 const std::string& relay, |
| 30 const scoped_refptr<net::URLRequestContextGetter>& url_context); |
| 31 virtual ~HostPortAllocatorSession(); |
| 32 |
| 33 // cricket::HttpPortAllocatorBase overrides. |
| 34 virtual void ConfigReady(cricket::PortConfiguration* config) OVERRIDE; |
| 35 virtual void SendSessionRequest(const std::string& host, int port) OVERRIDE; |
| 36 |
| 37 private: |
| 38 void OnSessionRequestDone(UrlFetcher* url_fetcher, |
| 39 const net::URLRequestStatus& status, |
| 40 int response_code, |
| 41 const std::string& response); |
| 42 |
| 43 scoped_refptr<net::URLRequestContextGetter> url_context_; |
| 44 std::set<UrlFetcher*> url_fetchers_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(HostPortAllocatorSession); |
| 47 }; |
| 48 |
| 49 HostPortAllocatorSession::HostPortAllocatorSession( |
| 50 cricket::HttpPortAllocatorBase* allocator, |
| 51 const std::string& channel_name, |
| 52 int component, |
| 53 const std::vector<talk_base::SocketAddress>& stun_hosts, |
| 54 const std::vector<std::string>& relay_hosts, |
| 55 const std::string& relay, |
| 56 const scoped_refptr<net::URLRequestContextGetter>& url_context) |
| 57 : HttpPortAllocatorSessionBase( |
| 58 allocator, channel_name, component, stun_hosts, relay_hosts, relay, ""), |
| 59 url_context_(url_context) { |
| 60 } |
| 61 |
| 62 HostPortAllocatorSession::~HostPortAllocatorSession() { |
| 63 STLDeleteElements(&url_fetchers_); |
| 64 } |
| 65 |
| 66 void HostPortAllocatorSession::ConfigReady(cricket::PortConfiguration* config) { |
| 67 // Filter out non-UDP relay ports, so that we don't try using TCP. |
| 68 for (cricket::PortConfiguration::RelayList::iterator relay = |
| 69 config->relays.begin(); relay != config->relays.end(); ++relay) { |
| 70 cricket::PortConfiguration::PortList filtered_ports; |
| 71 for (cricket::PortConfiguration::PortList::iterator port = |
| 72 relay->ports.begin(); port != relay->ports.end(); ++port) { |
| 73 if (port->proto == cricket::PROTO_UDP) { |
| 74 filtered_ports.push_back(*port); |
| 75 } |
| 76 } |
| 77 relay->ports = filtered_ports; |
| 78 } |
| 79 cricket::BasicPortAllocatorSession::ConfigReady(config); |
| 80 } |
| 81 |
| 82 void HostPortAllocatorSession::SendSessionRequest(const std::string& host, |
| 83 int port) { |
| 84 GURL url("https://" + host + ":" + base::IntToString(port) + |
| 85 GetSessionRequestUrl() + "&sn=1"); |
| 86 scoped_ptr<UrlFetcher> url_fetcher(new UrlFetcher(url, UrlFetcher::GET)); |
| 87 url_fetcher->SetRequestContext(url_context_); |
| 88 url_fetcher->SetHeader("X-Talk-Google-Relay-Auth", relay_token()); |
| 89 url_fetcher->SetHeader("X-Google-Relay-Auth", relay_token()); |
| 90 url_fetcher->SetHeader("X-Stream-Type", channel_name()); |
| 91 url_fetcher->Start(base::Bind(&HostPortAllocatorSession::OnSessionRequestDone, |
| 92 base::Unretained(this), url_fetcher.get())); |
| 93 url_fetchers_.insert(url_fetcher.release()); |
| 94 } |
| 95 |
| 96 void HostPortAllocatorSession::OnSessionRequestDone( |
| 97 UrlFetcher* url_fetcher, |
| 98 const net::URLRequestStatus& status, |
| 99 int response_code, |
| 100 const std::string& response) { |
| 101 url_fetchers_.erase(url_fetcher); |
| 102 delete url_fetcher; |
| 103 |
| 104 if (response_code != net::HTTP_OK) { |
| 105 LOG(WARNING) << "Received error when allocating relay session: " |
| 106 << response_code; |
| 107 TryCreateRelaySession(); |
| 108 return; |
| 109 } |
| 110 |
| 111 ReceiveSessionResponse(response); |
| 112 } |
| 113 |
| 114 } // namespace |
| 115 |
| 116 // static |
| 117 scoped_ptr<HostPortAllocator> HostPortAllocator::Create( |
| 118 const scoped_refptr<net::URLRequestContextGetter>& url_context, |
| 119 const NetworkSettings& network_settings) { |
| 120 scoped_ptr<talk_base::NetworkManager> network_manager( |
| 121 new talk_base::BasicNetworkManager()); |
| 122 scoped_ptr<talk_base::PacketSocketFactory> socket_factory( |
| 123 new talk_base::BasicPacketSocketFactory()); |
| 124 scoped_ptr<HostPortAllocator> result( |
| 125 new HostPortAllocator(url_context, network_manager.Pass(), |
| 126 socket_factory.Pass())); |
| 127 |
| 128 // We always use PseudoTcp to provide a reliable channel. It |
| 129 // provides poor performance when combined with TCP-based transport, |
| 130 // so we have to disable TCP ports. |
| 131 int flags = cricket::PORTALLOCATOR_DISABLE_TCP; |
| 132 if (network_settings.nat_traversal_mode != |
| 133 NetworkSettings::NAT_TRAVERSAL_ENABLED) { |
| 134 flags |= cricket::PORTALLOCATOR_DISABLE_STUN | |
| 135 cricket::PORTALLOCATOR_DISABLE_RELAY; |
| 136 } |
| 137 result->set_flags(flags); |
| 138 result->SetPortRange(network_settings.min_port, |
| 139 network_settings.max_port); |
| 140 |
| 141 return result.Pass(); |
| 142 } |
| 143 |
| 144 HostPortAllocator::HostPortAllocator( |
| 145 const scoped_refptr<net::URLRequestContextGetter>& url_context, |
| 146 scoped_ptr<talk_base::NetworkManager> network_manager, |
| 147 scoped_ptr<talk_base::PacketSocketFactory> socket_factory) |
| 148 : HttpPortAllocatorBase(network_manager.get(), socket_factory.get(), ""), |
| 149 url_context_(url_context), |
| 150 network_manager_(network_manager.Pass()), |
| 151 socket_factory_(socket_factory.Pass()) { |
| 152 } |
| 153 |
| 154 HostPortAllocator::~HostPortAllocator() { |
| 155 } |
| 156 |
| 157 cricket::PortAllocatorSession* HostPortAllocator::CreateSession( |
| 158 const std::string& channel_name, |
| 159 int component) { |
| 160 return new HostPortAllocatorSession( |
| 161 this, channel_name, component, stun_hosts(), |
| 162 relay_hosts(), relay_token(), url_context_); |
| 163 } |
| 164 |
| 165 } // namespace remoting |
OLD | NEW |