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