OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/protocol/port_allocator.h" | 5 #include "remoting/protocol/port_allocator.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 } | 34 } |
35 | 35 |
36 const int kNumRetries = 5; | 36 const int kNumRetries = 5; |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 namespace remoting { | 40 namespace remoting { |
41 namespace protocol { | 41 namespace protocol { |
42 | 42 |
43 PortAllocator::PortAllocator( | 43 PortAllocator::PortAllocator( |
44 scoped_ptr<rtc::NetworkManager> network_manager, | 44 std::unique_ptr<rtc::NetworkManager> network_manager, |
45 scoped_ptr<rtc::PacketSocketFactory> socket_factory, | 45 std::unique_ptr<rtc::PacketSocketFactory> socket_factory, |
46 scoped_refptr<TransportContext> transport_context) | 46 scoped_refptr<TransportContext> transport_context) |
47 : BasicPortAllocator(network_manager.get(), socket_factory.get()), | 47 : BasicPortAllocator(network_manager.get(), socket_factory.get()), |
48 network_manager_(std::move(network_manager)), | 48 network_manager_(std::move(network_manager)), |
49 socket_factory_(std::move(socket_factory)), | 49 socket_factory_(std::move(socket_factory)), |
50 transport_context_(transport_context) { | 50 transport_context_(transport_context) { |
51 // We always use PseudoTcp to provide a reliable channel. It provides poor | 51 // We always use PseudoTcp to provide a reliable channel. It provides poor |
52 // performance when combined with TCP-based transport, so we have to disable | 52 // performance when combined with TCP-based transport, so we have to disable |
53 // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username | 53 // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username |
54 // fragment is shared between all candidates. | 54 // fragment is shared between all candidates. |
55 int flags = cricket::PORTALLOCATOR_DISABLE_TCP | | 55 int flags = cricket::PORTALLOCATOR_DISABLE_TCP | |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 void PortAllocatorSession::OnIceConfig(const IceConfig& ice_config) { | 103 void PortAllocatorSession::OnIceConfig(const IceConfig& ice_config) { |
104 ice_config_ = ice_config; | 104 ice_config_ = ice_config; |
105 ConfigReady(GetPortConfiguration().release()); | 105 ConfigReady(GetPortConfiguration().release()); |
106 | 106 |
107 if (relay_enabled() && !ice_config_.relay_servers.empty() && | 107 if (relay_enabled() && !ice_config_.relay_servers.empty() && |
108 !ice_config_.relay_token.empty()) { | 108 !ice_config_.relay_token.empty()) { |
109 TryCreateRelaySession(); | 109 TryCreateRelaySession(); |
110 } | 110 } |
111 } | 111 } |
112 | 112 |
113 scoped_ptr<cricket::PortConfiguration> | 113 std::unique_ptr<cricket::PortConfiguration> |
114 PortAllocatorSession::GetPortConfiguration() { | 114 PortAllocatorSession::GetPortConfiguration() { |
115 cricket::ServerAddresses stun_servers; | 115 cricket::ServerAddresses stun_servers; |
116 for (const auto& host : ice_config_.stun_servers) { | 116 for (const auto& host : ice_config_.stun_servers) { |
117 stun_servers.insert(host); | 117 stun_servers.insert(host); |
118 } | 118 } |
119 | 119 |
120 scoped_ptr<cricket::PortConfiguration> config( | 120 std::unique_ptr<cricket::PortConfiguration> config( |
121 new cricket::PortConfiguration(stun_servers, username(), password())); | 121 new cricket::PortConfiguration(stun_servers, username(), password())); |
122 | 122 |
123 if (relay_enabled()) { | 123 if (relay_enabled()) { |
124 for (const auto& turn_server : ice_config_.turn_servers) { | 124 for (const auto& turn_server : ice_config_.turn_servers) { |
125 config->AddRelay(turn_server); | 125 config->AddRelay(turn_server); |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 return config; | 129 return config; |
130 } | 130 } |
(...skipping 12 matching lines...) Expand all Loading... |
143 std::string host = | 143 std::string host = |
144 ice_config_.relay_servers[attempts_ % ice_config_.relay_servers.size()]; | 144 ice_config_.relay_servers[attempts_ % ice_config_.relay_servers.size()]; |
145 attempts_++; | 145 attempts_++; |
146 | 146 |
147 DCHECK(!username().empty()); | 147 DCHECK(!username().empty()); |
148 DCHECK(!password().empty()); | 148 DCHECK(!password().empty()); |
149 std::string url = "https://" + host + "/create_session?username=" + | 149 std::string url = "https://" + host + "/create_session?username=" + |
150 net::EscapeUrlEncodedData(username(), false) + | 150 net::EscapeUrlEncodedData(username(), false) + |
151 "&password=" + | 151 "&password=" + |
152 net::EscapeUrlEncodedData(password(), false) + "&sn=1"; | 152 net::EscapeUrlEncodedData(password(), false) + "&sn=1"; |
153 scoped_ptr<UrlRequest> url_request = | 153 std::unique_ptr<UrlRequest> url_request = |
154 transport_context_->url_request_factory()->CreateUrlRequest( | 154 transport_context_->url_request_factory()->CreateUrlRequest( |
155 UrlRequest::Type::GET, url); | 155 UrlRequest::Type::GET, url); |
156 url_request->AddHeader("X-Talk-Google-Relay-Auth: " + | 156 url_request->AddHeader("X-Talk-Google-Relay-Auth: " + |
157 ice_config_.relay_token); | 157 ice_config_.relay_token); |
158 url_request->AddHeader("X-Google-Relay-Auth: " + ice_config_.relay_token); | 158 url_request->AddHeader("X-Google-Relay-Auth: " + ice_config_.relay_token); |
159 url_request->AddHeader("X-Stream-Type: chromoting"); | 159 url_request->AddHeader("X-Stream-Type: chromoting"); |
160 url_request->Start(base::Bind(&PortAllocatorSession::OnSessionRequestResult, | 160 url_request->Start(base::Bind(&PortAllocatorSession::OnSessionRequestResult, |
161 base::Unretained(this))); | 161 base::Unretained(this))); |
162 url_requests_.insert(std::move(url_request)); | 162 url_requests_.insert(std::move(url_request)); |
163 } | 163 } |
164 | 164 |
165 void PortAllocatorSession::OnSessionRequestResult( | 165 void PortAllocatorSession::OnSessionRequestResult( |
166 const UrlRequest::Result& result) { | 166 const UrlRequest::Result& result) { |
167 if (!result.success || result.status != net::HTTP_OK) { | 167 if (!result.success || result.status != net::HTTP_OK) { |
168 LOG(WARNING) << "Received error when allocating relay session: " | 168 LOG(WARNING) << "Received error when allocating relay session: " |
169 << result.status; | 169 << result.status; |
170 TryCreateRelaySession(); | 170 TryCreateRelaySession(); |
171 return; | 171 return; |
172 } | 172 } |
173 | 173 |
174 StringMap map = ParseMap(result.response_body); | 174 StringMap map = ParseMap(result.response_body); |
175 | 175 |
176 if (!username().empty() && map["username"] != username()) { | 176 if (!username().empty() && map["username"] != username()) { |
177 LOG(WARNING) << "Received unexpected username value from relay server."; | 177 LOG(WARNING) << "Received unexpected username value from relay server."; |
178 } | 178 } |
179 if (!password().empty() && map["password"] != password()) { | 179 if (!password().empty() && map["password"] != password()) { |
180 LOG(WARNING) << "Received unexpected password value from relay server."; | 180 LOG(WARNING) << "Received unexpected password value from relay server."; |
181 } | 181 } |
182 | 182 |
183 scoped_ptr<cricket::PortConfiguration> config = GetPortConfiguration(); | 183 std::unique_ptr<cricket::PortConfiguration> config = GetPortConfiguration(); |
184 | 184 |
185 std::string relay_ip = map["relay.ip"]; | 185 std::string relay_ip = map["relay.ip"]; |
186 std::string relay_port = map["relay.udp_port"]; | 186 std::string relay_port = map["relay.udp_port"]; |
187 unsigned relay_port_int; | 187 unsigned relay_port_int; |
188 | 188 |
189 if (!relay_ip.empty() && !relay_port.empty() && | 189 if (!relay_ip.empty() && !relay_port.empty() && |
190 base::StringToUint(relay_port, &relay_port_int)) { | 190 base::StringToUint(relay_port, &relay_port_int)) { |
191 cricket::RelayServerConfig relay_config(cricket::RELAY_GTURN); | 191 cricket::RelayServerConfig relay_config(cricket::RELAY_GTURN); |
192 rtc::SocketAddress address(relay_ip, relay_port_int); | 192 rtc::SocketAddress address(relay_ip, relay_port_int); |
193 relay_config.ports.push_back( | 193 relay_config.ports.push_back( |
194 cricket::ProtocolAddress(address, cricket::PROTO_UDP)); | 194 cricket::ProtocolAddress(address, cricket::PROTO_UDP)); |
195 config->AddRelay(relay_config); | 195 config->AddRelay(relay_config); |
196 } | 196 } |
197 | 197 |
198 ConfigReady(config.release()); | 198 ConfigReady(config.release()); |
199 } | 199 } |
200 | 200 |
201 } // namespace protocol | 201 } // namespace protocol |
202 } // namespace remoting | 202 } // namespace remoting |
OLD | NEW |