Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/protocol/http_ice_config_request.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback_helpers.h" | |
| 9 #include "base/json/json_reader.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "base/values.h" | |
| 13 #include "net/base/url_util.h" | |
| 14 #include "remoting/protocol/ice_config.h" | |
| 15 | |
| 16 namespace remoting { | |
| 17 namespace protocol { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Ensure they are active at least one hour after session starts. | |
|
Jamie
2016/02/18 19:37:38
s/they/<whatever "they" refers to in this context>
Sergey Ulanov
2016/02/19 20:08:32
Done.
| |
| 22 const int kMinimumConfigLifetimeSeconds = 3600; | |
| 23 | |
| 24 // See draft-petithuguenin-behave-turn-uris-01. | |
| 25 const int kDefaultStunTurnPort = 3478; | |
| 26 const int kDefaultTurnsPort = 5349; | |
| 27 | |
| 28 base::TimeDelta ParseLifetime(const std::string& string) { | |
| 29 double seconds; | |
| 30 if (!base::EndsWith(string, "s", base::CompareCase::INSENSITIVE_ASCII) || | |
| 31 !base::StringToDouble(string.substr(0, string.size() - 1), &seconds)) { | |
| 32 LOG(ERROR) << "Received invalid lifetimeDuration value: " << string; | |
|
Jamie
2016/02/18 19:37:38
For consistency with the other error messages, I t
Sergey Ulanov
2016/02/19 20:08:32
Done.
| |
| 33 return base::TimeDelta(); | |
| 34 } | |
| 35 return base::TimeDelta::FromSecondsD(seconds); | |
| 36 } | |
| 37 | |
| 38 // Parses url in form of <stun|turn|turns>:<host>[:<port>][?transport=<udp|tcp>] | |
| 39 // and adds an entry to the |config|. | |
| 40 bool AddServerToConfig(std::string url, | |
| 41 const std::string& username, | |
| 42 const std::string& password, | |
| 43 IceConfig* config) { | |
| 44 cricket::ProtocolType turn_transport_type = cricket::PROTO_LAST; | |
| 45 | |
| 46 const char kTcpTransportSuffix[] = "?transport=tcp"; | |
| 47 const char kUdpTransportSuffix[] = "?transport=udp"; | |
| 48 if (base::EndsWith(url, kTcpTransportSuffix, | |
| 49 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 50 turn_transport_type = cricket::PROTO_TCP; | |
| 51 url.resize(url.size() - strlen(kTcpTransportSuffix)); | |
| 52 } else if (base::EndsWith(url, kUdpTransportSuffix, | |
| 53 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 54 turn_transport_type = cricket::PROTO_UDP; | |
| 55 url.resize(url.size() - strlen(kUdpTransportSuffix)); | |
| 56 } | |
| 57 | |
| 58 size_t colon_pos = url.find(':'); | |
| 59 if (colon_pos == std::string::npos) | |
| 60 return false; | |
| 61 | |
| 62 std::string protocol = url.substr(0, colon_pos); | |
| 63 | |
| 64 std::string host; | |
| 65 int port; | |
| 66 if (!net::ParseHostAndPort(url.substr(colon_pos + 1), &host, &port)) | |
| 67 return false; | |
|
Jamie
2016/02/18 19:37:37
Optional: This might be more readable using String
Sergey Ulanov
2016/02/19 20:08:32
StringSplit wouldn't work for IPv6 addresses. IPv6
| |
| 68 | |
| 69 if (protocol == "stun") { | |
| 70 if (port == -1) | |
| 71 port = kDefaultStunTurnPort; | |
| 72 config->stun_servers.push_back(rtc::SocketAddress(host, port)); | |
| 73 } else if (protocol == "turn") { | |
| 74 if (port == -1) | |
| 75 port = kDefaultStunTurnPort; | |
| 76 if (turn_transport_type == cricket::PROTO_LAST) | |
| 77 turn_transport_type = cricket::PROTO_UDP; | |
| 78 config->turn_servers.push_back(cricket::RelayServerConfig( | |
| 79 host, port, username, password, turn_transport_type, false)); | |
| 80 } else if (protocol == "turns") { | |
| 81 if (port == -1) | |
| 82 port = kDefaultTurnsPort; | |
| 83 if (turn_transport_type == cricket::PROTO_LAST) | |
| 84 turn_transport_type = cricket::PROTO_TCP; | |
| 85 config->turn_servers.push_back(cricket::RelayServerConfig( | |
| 86 host, port, username, password, turn_transport_type, true)); | |
| 87 } else { | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 94 } // namespace | |
| 95 | |
| 96 HttpIceConfigRequest::HttpIceConfigRequest( | |
| 97 UrlRequestFactory* url_request_factory, | |
| 98 const std::string& url) | |
| 99 : url_(url) { | |
| 100 url_request_ = | |
| 101 url_request_factory->CreateUrlRequest(UrlRequest::Type::POST, url_); | |
| 102 url_request_->SetPostData("application/json", ""); | |
| 103 } | |
| 104 | |
| 105 HttpIceConfigRequest::~HttpIceConfigRequest() {} | |
| 106 | |
| 107 void HttpIceConfigRequest::Send(const OnIceConfigCallback& callback) { | |
| 108 on_ice_config_callback_ = callback; | |
|
Jamie
2016/02/18 19:37:38
DCHECK it isn't already set (and that it is set in
Sergey Ulanov
2016/02/19 20:08:31
Done.
| |
| 109 url_request_->Start( | |
| 110 base::Bind(&HttpIceConfigRequest::OnResponse, base::Unretained(this))); | |
| 111 } | |
| 112 | |
| 113 void HttpIceConfigRequest::OnResponse(const UrlRequest::Result& result) { | |
| 114 IceConfig ice_config; | |
| 115 if (!result.success) { | |
| 116 LOG(ERROR) << "Failed to fetch " << url_; | |
| 117 base::ResetAndReturn(&on_ice_config_callback_).Run(ice_config); | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 if (result.status != 200) { | |
| 122 LOG(ERROR) << "Received status code " << result.status << " from " << url_ | |
| 123 << ": " << result.response_body; | |
| 124 base::ResetAndReturn(&on_ice_config_callback_).Run(ice_config); | |
|
Jamie
2016/02/18 19:37:38
In case of error, you seem to be returning as much
Sergey Ulanov
2016/02/19 20:08:32
In this particular case we are returning a null Ic
| |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 scoped_ptr<base::Value> json = base::JSONReader::Read(result.response_body); | |
| 129 base::DictionaryValue* dictionary; | |
| 130 base::ListValue* ice_servers_list; | |
| 131 std::string lifetime_str; | |
| 132 if (!json || !json->GetAsDictionary(&dictionary) || | |
| 133 !dictionary->GetList("iceServers", &ice_servers_list) || | |
| 134 !dictionary->GetString("lifetimeDuration", &lifetime_str)) { | |
| 135 LOG(ERROR) << "Received invalid response from " << url_ << ": " | |
| 136 << result.response_body; | |
| 137 base::ResetAndReturn(&on_ice_config_callback_).Run(ice_config); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 // Parse lifetimeDuration field. | |
| 142 base::TimeDelta lifetime = ParseLifetime(lifetime_str); | |
| 143 if (lifetime == base::TimeDelta()) { | |
| 144 base::ResetAndReturn(&on_ice_config_callback_).Run(ice_config); | |
| 145 return; | |
| 146 } | |
| 147 ice_config.expiration_time = | |
| 148 base::Time::Now() + lifetime - | |
| 149 base::TimeDelta::FromSeconds(kMinimumConfigLifetimeSeconds); | |
| 150 | |
| 151 // Parse iceServers list. | |
| 152 bool errors_found = false; | |
|
Jamie
2016/02/18 19:37:38
Why not just return early in case of error? Partic
Sergey Ulanov
2016/02/19 20:08:32
When we fail to fetch a config we will still want
| |
| 153 for (base::Value* server : *ice_servers_list) { | |
| 154 base::DictionaryValue* server_dict; | |
| 155 if (!server->GetAsDictionary(&server_dict)) { | |
| 156 errors_found = true; | |
| 157 continue; | |
| 158 } | |
| 159 | |
| 160 base::ListValue* urls_list; | |
| 161 if (!server_dict->GetList("urls", &urls_list)) { | |
| 162 errors_found = true; | |
| 163 continue; | |
| 164 } | |
| 165 | |
| 166 std::string username; | |
| 167 server_dict->GetString("username", &username); | |
| 168 | |
| 169 std::string password; | |
| 170 server_dict->GetString("credential", &password); | |
| 171 | |
| 172 for (base::Value* url : *urls_list) { | |
| 173 std::string url_str; | |
| 174 if (!url->GetAsString(&url_str)) { | |
| 175 errors_found = true; | |
| 176 continue; | |
| 177 } | |
| 178 if (!AddServerToConfig(url_str, username, password, &ice_config)) { | |
| 179 LOG(ERROR) << "Invalid ICE server URL: " << url_str; | |
|
Jamie
2016/02/18 19:37:38
errors_found = true?
Sergey Ulanov
2016/02/19 20:08:31
error_found is there only for structural errors, e
| |
| 180 } | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 if (errors_found) { | |
| 185 LOG(ERROR) << "Received ICE config from the server that contained errors: " | |
| 186 << result.response_body; | |
| 187 } | |
| 188 | |
| 189 base::ResetAndReturn(&on_ice_config_callback_).Run(ice_config); | |
| 190 } | |
| 191 | |
| 192 } // namespace protocol | |
| 193 } // namespace remoting | |
| OLD | NEW |