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