| 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 "content/renderer/p2p/port_allocator.h" | 5 #include "content/renderer/p2p/port_allocator.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 LOG(ERROR) << "Relay session request failed."; | 119 LOG(ERROR) << "Relay session request failed."; |
| 120 | 120 |
| 121 // Retry the request. | 121 // Retry the request. |
| 122 AllocateRelaySession(); | 122 AllocateRelaySession(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void P2PPortAllocatorSession::GetPortConfigurations() { | 125 void P2PPortAllocatorSession::GetPortConfigurations() { |
| 126 // Add an empty configuration synchronously, so a local connection | 126 // Add an empty configuration synchronously, so a local connection |
| 127 // can be started immediately. | 127 // can be started immediately. |
| 128 ConfigReady(new cricket::PortConfiguration( | 128 ConfigReady(new cricket::PortConfiguration( |
| 129 talk_base::SocketAddress(), "", "", "")); | 129 talk_base::SocketAddress(), username(), password(), "")); |
| 130 | 130 |
| 131 ResolveStunServerAddress(); | 131 ResolveStunServerAddress(); |
| 132 AllocateRelaySession(); | 132 AllocateRelaySession(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void P2PPortAllocatorSession::ResolveStunServerAddress() { | 135 void P2PPortAllocatorSession::ResolveStunServerAddress() { |
| 136 if (allocator_->config_.stun_server.empty()) | 136 if (allocator_->config_.stun_server.empty()) |
| 137 return; | 137 return; |
| 138 | 138 |
| 139 DCHECK(!stun_address_request_); | 139 DCHECK(!stun_address_request_); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; | 186 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; |
| 187 | 187 |
| 188 relay_session_request_.reset( | 188 relay_session_request_.reset( |
| 189 allocator_->web_frame_->createAssociatedURLLoader(options)); | 189 allocator_->web_frame_->createAssociatedURLLoader(options)); |
| 190 if (!relay_session_request_.get()) { | 190 if (!relay_session_request_.get()) { |
| 191 LOG(ERROR) << "Failed to create URL loader."; | 191 LOG(ERROR) << "Failed to create URL loader."; |
| 192 return; | 192 return; |
| 193 } | 193 } |
| 194 | 194 |
| 195 std::string url = "https://" + allocator_->config_.relay_server + | 195 std::string url = "https://" + allocator_->config_.relay_server + |
| 196 kCreateRelaySessionURL; | 196 kCreateRelaySessionURL + |
| 197 "?username=" + net::EscapeUrlEncodedData(username(), true) + |
| 198 "&password=" + net::EscapeUrlEncodedData(password(), true); |
| 197 | 199 |
| 198 // Use |relay_username| parameter to identify type of client for the | 200 // Use |relay_username| parameter to identify type of client for the |
| 199 // relay session. | 201 // relay session. |
| 200 // | 202 // |
| 201 // TODO(sergeyu): Username is not used for legacy non-TURN relay | 203 // TODO(sergeyu): Username is not used for legacy non-TURN relay |
| 202 // servers, so we reuse it here to identify relay client type. This | 204 // servers, so we reuse it here to identify relay client type. This |
| 203 // is currently used for Chromoting only. This code should be removed | 205 // is currently used for Chromoting only. This code should be removed |
| 204 // once Chromoting stops using Transport API and the API is removed. | 206 // once Chromoting stops using Transport API and the API is removed. |
| 205 if (!allocator_->config_.relay_username.empty()) { | 207 if (!allocator_->config_.relay_username.empty()) { |
| 206 url += "?sn=" + | 208 url += "&sn=" + |
| 207 net::EscapeUrlEncodedData(allocator_->config_.relay_username, true); | 209 net::EscapeUrlEncodedData(allocator_->config_.relay_username, true); |
| 208 } | 210 } |
| 209 | 211 |
| 210 WebURLRequest request; | 212 WebURLRequest request; |
| 211 request.initialize(); | 213 request.initialize(); |
| 212 request.setURL(WebURL(GURL(url))); | 214 request.setURL(WebURL(GURL(url))); |
| 213 request.setAllowStoredCredentials(false); | 215 request.setAllowStoredCredentials(false); |
| 214 request.setCachePolicy(WebURLRequest::ReloadIgnoringCacheData); | 216 request.setCachePolicy(WebURLRequest::ReloadIgnoringCacheData); |
| 215 request.setHTTPMethod("GET"); | 217 request.setHTTPMethod("GET"); |
| 216 request.addHTTPHeaderField( | 218 request.addHTTPHeaderField( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 228 } | 230 } |
| 229 | 231 |
| 230 void P2PPortAllocatorSession::ParseRelayResponse() { | 232 void P2PPortAllocatorSession::ParseRelayResponse() { |
| 231 std::vector<std::pair<std::string, std::string> > value_pairs; | 233 std::vector<std::pair<std::string, std::string> > value_pairs; |
| 232 if (!base::SplitStringIntoKeyValuePairs(relay_session_response_, '=', '\n', | 234 if (!base::SplitStringIntoKeyValuePairs(relay_session_response_, '=', '\n', |
| 233 &value_pairs)) { | 235 &value_pairs)) { |
| 234 LOG(ERROR) << "Received invalid response from relay server"; | 236 LOG(ERROR) << "Received invalid response from relay server"; |
| 235 return; | 237 return; |
| 236 } | 238 } |
| 237 | 239 |
| 238 relay_username_.clear(); | |
| 239 relay_password_.clear(); | |
| 240 relay_ip_.Clear(); | 240 relay_ip_.Clear(); |
| 241 relay_udp_port_ = 0; | 241 relay_udp_port_ = 0; |
| 242 relay_tcp_port_ = 0; | 242 relay_tcp_port_ = 0; |
| 243 relay_ssltcp_port_ = 0; | 243 relay_ssltcp_port_ = 0; |
| 244 | 244 |
| 245 for (std::vector<std::pair<std::string, std::string> >::iterator | 245 for (std::vector<std::pair<std::string, std::string> >::iterator |
| 246 it = value_pairs.begin(); | 246 it = value_pairs.begin(); |
| 247 it != value_pairs.end(); ++it) { | 247 it != value_pairs.end(); ++it) { |
| 248 std::string key; | 248 std::string key; |
| 249 std::string value; | 249 std::string value; |
| 250 TrimWhitespaceASCII(it->first, TRIM_ALL, &key); | 250 TrimWhitespaceASCII(it->first, TRIM_ALL, &key); |
| 251 TrimWhitespaceASCII(it->second, TRIM_ALL, &value); | 251 TrimWhitespaceASCII(it->second, TRIM_ALL, &value); |
| 252 | 252 |
| 253 if (key == "username") { | 253 if (key == "username") { |
| 254 relay_username_ = value; | 254 if (value != username()) { |
| 255 LOG(ERROR) << "When creating relay session received user name " |
| 256 " that was different from the value specified in the query."; |
| 257 return; |
| 258 } |
| 255 } else if (key == "password") { | 259 } else if (key == "password") { |
| 256 relay_password_ = value; | 260 if (value != password()) { |
| 261 LOG(ERROR) << "When creating relay session received password " |
| 262 "that was different from the value specified in the query."; |
| 263 return; |
| 264 } |
| 257 } else if (key == "relay.ip") { | 265 } else if (key == "relay.ip") { |
| 258 relay_ip_.SetIP(value); | 266 relay_ip_.SetIP(value); |
| 259 if (relay_ip_.ip() == 0) { | 267 if (relay_ip_.ip() == 0) { |
| 260 LOG(ERROR) << "Received unresolved relay server address: " << value; | 268 LOG(ERROR) << "Received unresolved relay server address: " << value; |
| 261 return; | 269 return; |
| 262 } | 270 } |
| 263 } else if (key == "relay.udp_port") { | 271 } else if (key == "relay.udp_port") { |
| 264 if (!ParsePortNumber(value, &relay_udp_port_)) | 272 if (!ParsePortNumber(value, &relay_udp_port_)) |
| 265 return; | 273 return; |
| 266 } else if (key == "relay.tcp_port") { | 274 } else if (key == "relay.tcp_port") { |
| 267 if (!ParsePortNumber(value, &relay_tcp_port_)) | 275 if (!ParsePortNumber(value, &relay_tcp_port_)) |
| 268 return; | 276 return; |
| 269 } else if (key == "relay.ssltcp_port") { | 277 } else if (key == "relay.ssltcp_port") { |
| 270 if (!ParsePortNumber(value, &relay_ssltcp_port_)) | 278 if (!ParsePortNumber(value, &relay_ssltcp_port_)) |
| 271 return; | 279 return; |
| 272 } | 280 } |
| 273 } | 281 } |
| 274 | 282 |
| 275 AddConfig(); | 283 AddConfig(); |
| 276 } | 284 } |
| 277 | 285 |
| 278 void P2PPortAllocatorSession::AddConfig() { | 286 void P2PPortAllocatorSession::AddConfig() { |
| 279 cricket::PortConfiguration* config = | 287 cricket::PortConfiguration* config = |
| 280 new cricket::PortConfiguration(stun_server_address_, | 288 new cricket::PortConfiguration(stun_server_address_, |
| 281 relay_username_, relay_password_, ""); | 289 username(), password(), ""); |
| 282 | 290 |
| 283 if (relay_ip_.ip() != 0) { | 291 if (relay_ip_.ip() != 0) { |
| 284 cricket::PortConfiguration::PortList ports; | 292 cricket::PortConfiguration::PortList ports; |
| 285 if (relay_udp_port_ > 0) { | 293 if (relay_udp_port_ > 0) { |
| 286 talk_base::SocketAddress address(relay_ip_.ip(), relay_udp_port_); | 294 talk_base::SocketAddress address(relay_ip_.ip(), relay_udp_port_); |
| 287 ports.push_back(cricket::ProtocolAddress(address, cricket::PROTO_UDP)); | 295 ports.push_back(cricket::ProtocolAddress(address, cricket::PROTO_UDP)); |
| 288 } | 296 } |
| 289 if (relay_tcp_port_ > 0 && !allocator_->config_.disable_tcp_transport) { | 297 if (relay_tcp_port_ > 0 && !allocator_->config_.disable_tcp_transport) { |
| 290 talk_base::SocketAddress address(relay_ip_.ip(), relay_tcp_port_); | 298 talk_base::SocketAddress address(relay_ip_.ip(), relay_tcp_port_); |
| 291 ports.push_back(cricket::ProtocolAddress(address, cricket::PROTO_TCP)); | 299 ports.push_back(cricket::ProtocolAddress(address, cricket::PROTO_TCP)); |
| 292 } | 300 } |
| 293 if (relay_ssltcp_port_ > 0 && !allocator_->config_.disable_tcp_transport) { | 301 if (relay_ssltcp_port_ > 0 && !allocator_->config_.disable_tcp_transport) { |
| 294 talk_base::SocketAddress address(relay_ip_.ip(), relay_ssltcp_port_); | 302 talk_base::SocketAddress address(relay_ip_.ip(), relay_ssltcp_port_); |
| 295 ports.push_back(cricket::ProtocolAddress(address, cricket::PROTO_SSLTCP)); | 303 ports.push_back(cricket::ProtocolAddress(address, cricket::PROTO_SSLTCP)); |
| 296 } | 304 } |
| 297 if (!ports.empty()) | 305 if (!ports.empty()) |
| 298 config->AddRelay(ports, 0.0f); | 306 config->AddRelay(ports, 0.0f); |
| 299 } | 307 } |
| 300 ConfigReady(config); | 308 ConfigReady(config); |
| 301 } | 309 } |
| 302 | 310 |
| 303 } // namespace content | 311 } // namespace content |
| OLD | NEW |