sergeyu | 58e801a | 2016-02-13 02:07:16 | [diff] [blame] | 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/ice_config.h" |
| 6 | |
Lambros Lambrou | f46bf54 | 2018-09-07 00:04:09 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 9 | #include "base/json/json_reader.h" |
Sergey Ulanov | b5da5970c | 2017-06-08 18:55:28 | [diff] [blame] | 10 | #include "base/json/json_writer.h" |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
| 12 | #include "base/strings/string_util.h" |
| 13 | #include "base/values.h" |
| 14 | #include "net/base/url_util.h" |
| 15 | |
sergeyu | 58e801a | 2016-02-13 02:07:16 | [diff] [blame] | 16 | namespace remoting { |
| 17 | namespace protocol { |
| 18 | |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | // See draft-petithuguenin-behave-turn-uris-01. |
| 22 | const int kDefaultStunTurnPort = 3478; |
| 23 | const int kDefaultTurnsPort = 5349; |
| 24 | |
| 25 | bool ParseLifetime(const std::string& string, base::TimeDelta* result) { |
| 26 | double seconds = 0; |
| 27 | if (!base::EndsWith(string, "s", base::CompareCase::INSENSITIVE_ASCII) || |
| 28 | !base::StringToDouble(string.substr(0, string.size() - 1), &seconds)) { |
| 29 | return false; |
| 30 | } |
| 31 | *result = base::TimeDelta::FromSecondsD(seconds); |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | // Parses url in form of <stun|turn|turns>:<host>[:<port>][?transport=<udp|tcp>] |
| 36 | // and adds an entry to the |config|. |
| 37 | bool AddServerToConfig(std::string url, |
| 38 | const std::string& username, |
| 39 | const std::string& password, |
| 40 | IceConfig* config) { |
| 41 | cricket::ProtocolType turn_transport_type = cricket::PROTO_LAST; |
| 42 | |
| 43 | const char kTcpTransportSuffix[] = "?transport=tcp"; |
| 44 | const char kUdpTransportSuffix[] = "?transport=udp"; |
| 45 | if (base::EndsWith(url, kTcpTransportSuffix, |
| 46 | base::CompareCase::INSENSITIVE_ASCII)) { |
| 47 | turn_transport_type = cricket::PROTO_TCP; |
| 48 | url.resize(url.size() - strlen(kTcpTransportSuffix)); |
| 49 | } else if (base::EndsWith(url, kUdpTransportSuffix, |
| 50 | base::CompareCase::INSENSITIVE_ASCII)) { |
| 51 | turn_transport_type = cricket::PROTO_UDP; |
| 52 | url.resize(url.size() - strlen(kUdpTransportSuffix)); |
| 53 | } |
| 54 | |
| 55 | size_t colon_pos = url.find(':'); |
| 56 | if (colon_pos == std::string::npos) |
| 57 | return false; |
| 58 | |
| 59 | std::string protocol = url.substr(0, colon_pos); |
| 60 | |
| 61 | std::string host; |
| 62 | int port; |
| 63 | if (!net::ParseHostAndPort(url.substr(colon_pos + 1), &host, &port)) |
| 64 | return false; |
| 65 | |
| 66 | if (protocol == "stun") { |
| 67 | if (port == -1) |
| 68 | port = kDefaultStunTurnPort; |
| 69 | config->stun_servers.push_back(rtc::SocketAddress(host, port)); |
| 70 | } else if (protocol == "turn") { |
| 71 | if (port == -1) |
| 72 | port = kDefaultStunTurnPort; |
| 73 | if (turn_transport_type == cricket::PROTO_LAST) |
| 74 | turn_transport_type = cricket::PROTO_UDP; |
| 75 | config->turn_servers.push_back(cricket::RelayServerConfig( |
| 76 | host, port, username, password, turn_transport_type, false)); |
| 77 | } else if (protocol == "turns") { |
| 78 | if (port == -1) |
| 79 | port = kDefaultTurnsPort; |
| 80 | if (turn_transport_type == cricket::PROTO_LAST) |
| 81 | turn_transport_type = cricket::PROTO_TCP; |
| 82 | config->turn_servers.push_back(cricket::RelayServerConfig( |
| 83 | host, port, username, password, turn_transport_type, true)); |
| 84 | } else { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
Lambros Lambrou | f46bf54 | 2018-09-07 00:04:09 | [diff] [blame] | 91 | // Returns the smallest specified value, or 0 if neither is specified. |
| 92 | // A value is "specified" if it is greater than 0. |
| 93 | int MinimumSpecified(int value1, int value2) { |
| 94 | if (value1 <= 0) { |
| 95 | // value1 is not specified, so return value2 (or 0). |
| 96 | return std::max(0, value2); |
| 97 | } |
| 98 | if (value2 <= 0) { |
| 99 | // value1 is specified, so return it directly. |
| 100 | return value1; |
| 101 | } |
| 102 | // Both values are specified, so return the minimum. |
| 103 | return std::min(value1, value2); |
| 104 | } |
| 105 | |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 106 | } // namespace |
| 107 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 108 | IceConfig::IceConfig() = default; |
vmpstr | bf0d713a | 2016-03-24 20:22:54 | [diff] [blame] | 109 | IceConfig::IceConfig(const IceConfig& other) = default; |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 110 | IceConfig::~IceConfig() = default; |
sergeyu | 58e801a | 2016-02-13 02:07:16 | [diff] [blame] | 111 | |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 112 | // static |
Sergey Ulanov | b5da5970c | 2017-06-08 18:55:28 | [diff] [blame] | 113 | IceConfig IceConfig::Parse(const base::DictionaryValue& dictionary) { |
| 114 | const base::ListValue* ice_servers_list = nullptr; |
| 115 | if (!dictionary.GetList("iceServers", &ice_servers_list)) { |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 116 | return IceConfig(); |
| 117 | } |
| 118 | |
| 119 | IceConfig ice_config; |
| 120 | |
| 121 | // Parse lifetimeDuration field. |
| 122 | std::string lifetime_str; |
| 123 | base::TimeDelta lifetime; |
Sergey Ulanov | b5da5970c | 2017-06-08 18:55:28 | [diff] [blame] | 124 | if (!dictionary.GetString("lifetimeDuration", &lifetime_str) || |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 125 | !ParseLifetime(lifetime_str, &lifetime)) { |
| 126 | LOG(ERROR) << "Received invalid lifetimeDuration value: " << lifetime_str; |
| 127 | |
| 128 | // If the |lifetimeDuration| field is missing or cannot be parsed then mark |
| 129 | // the config as expired so it will refreshed for the next session. |
| 130 | ice_config.expiration_time = base::Time::Now(); |
| 131 | } else { |
| 132 | ice_config.expiration_time = base::Time::Now() + lifetime; |
| 133 | } |
| 134 | |
| 135 | // Parse iceServers list and store them in |ice_config|. |
| 136 | bool errors_found = false; |
Lambros Lambrou | f46bf54 | 2018-09-07 00:04:09 | [diff] [blame] | 137 | ice_config.max_bitrate_kbps = 0; |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 138 | for (const auto& server : *ice_servers_list) { |
| 139 | const base::DictionaryValue* server_dict; |
| 140 | if (!server.GetAsDictionary(&server_dict)) { |
| 141 | errors_found = true; |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | const base::ListValue* urls_list = nullptr; |
| 146 | if (!server_dict->GetList("urls", &urls_list)) { |
| 147 | errors_found = true; |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | std::string username; |
| 152 | server_dict->GetString("username", &username); |
| 153 | |
| 154 | std::string password; |
| 155 | server_dict->GetString("credential", &password); |
| 156 | |
Lambros Lambrou | f46bf54 | 2018-09-07 00:04:09 | [diff] [blame] | 157 | // Compute the lowest specified bitrate of all the ICE servers. |
| 158 | // Ideally the bitrate would be stored per ICE server, but it is not |
| 159 | // possible (at the application level) to look up which particular |
| 160 | // ICE server was used for the P2P connection. |
| 161 | double new_bitrate_double; |
| 162 | if (server_dict->GetDouble("maxRateKbps", &new_bitrate_double)) { |
| 163 | ice_config.max_bitrate_kbps = MinimumSpecified( |
| 164 | ice_config.max_bitrate_kbps, static_cast<int>(new_bitrate_double)); |
| 165 | } |
| 166 | |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 167 | for (const auto& url : *urls_list) { |
| 168 | std::string url_str; |
| 169 | if (!url.GetAsString(&url_str)) { |
| 170 | errors_found = true; |
| 171 | continue; |
| 172 | } |
| 173 | if (!AddServerToConfig(url_str, username, password, &ice_config)) { |
| 174 | LOG(ERROR) << "Invalid ICE server URL: " << url_str; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (errors_found) { |
Sergey Ulanov | b5da5970c | 2017-06-08 18:55:28 | [diff] [blame] | 180 | std::string json; |
| 181 | if (!base::JSONWriter::WriteWithOptions( |
| 182 | dictionary, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json)) { |
| 183 | NOTREACHED(); |
| 184 | } |
| 185 | LOG(ERROR) << "Received ICE config with errors: " << json; |
Sergey Ulanov | d1b9d02 | 2017-06-06 01:15:28 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // If there are no STUN or no TURN servers then mark the config as expired so |
| 189 | // it will refreshed for the next session. |
| 190 | if (errors_found || ice_config.stun_servers.empty() || |
| 191 | ice_config.turn_servers.empty()) { |
| 192 | ice_config.expiration_time = base::Time::Now(); |
| 193 | } |
| 194 | |
| 195 | return ice_config; |
| 196 | } |
| 197 | |
Sergey Ulanov | b5da5970c | 2017-06-08 18:55:28 | [diff] [blame] | 198 | // static |
| 199 | IceConfig IceConfig::Parse(const std::string& config_json) { |
| 200 | std::unique_ptr<base::Value> json = base::JSONReader::Read(config_json); |
| 201 | if (!json) { |
| 202 | return IceConfig(); |
| 203 | } |
| 204 | |
| 205 | base::DictionaryValue* dictionary = nullptr; |
| 206 | if (!json->GetAsDictionary(&dictionary)) { |
| 207 | return IceConfig(); |
| 208 | } |
| 209 | |
| 210 | // Handle the case when the config is wrapped in 'data', i.e. as {'data': { |
| 211 | // 'iceServers': {...} }}. |
| 212 | base::DictionaryValue* data_dictionary = nullptr; |
| 213 | if (!dictionary->HasKey("iceServers") && |
| 214 | dictionary->GetDictionary("data", &data_dictionary)) { |
| 215 | return Parse(*data_dictionary); |
| 216 | } |
| 217 | |
| 218 | return Parse(*dictionary); |
| 219 | } |
| 220 | |
sergeyu | 58e801a | 2016-02-13 02:07:16 | [diff] [blame] | 221 | } // namespace protocol |
| 222 | } // namespace remoting |