Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
|
Taylor Brandstetter
2017/03/17 04:30:37
This code is all unchanged.
| |
| 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/pc/iceserverparsing.h" | |
| 12 | |
| 13 #include <string> | |
| 14 | |
| 15 #include "webrtc/base/arraysize.h" | |
| 16 | |
| 17 namespace webrtc { | |
| 18 | |
| 19 // The min number of tokens must present in Turn host uri. | |
| 20 // e.g. user@turn.example.org | |
| 21 static const size_t kTurnHostTokensNum = 2; | |
| 22 // Number of tokens must be preset when TURN uri has transport param. | |
| 23 static const size_t kTurnTransportTokensNum = 2; | |
| 24 // The default stun port. | |
| 25 static const int kDefaultStunPort = 3478; | |
| 26 static const int kDefaultStunTlsPort = 5349; | |
| 27 static const char kTransport[] = "transport"; | |
| 28 | |
| 29 // NOTE: Must be in the same order as the ServiceType enum. | |
| 30 static const char* kValidIceServiceTypes[] = {"stun", "stuns", "turn", "turns"}; | |
| 31 | |
| 32 // NOTE: A loop below assumes that the first value of this enum is 0 and all | |
| 33 // other values are incremental. | |
| 34 enum ServiceType { | |
| 35 STUN = 0, // Indicates a STUN server. | |
| 36 STUNS, // Indicates a STUN server used with a TLS session. | |
| 37 TURN, // Indicates a TURN server | |
| 38 TURNS, // Indicates a TURN server used with a TLS session. | |
| 39 INVALID, // Unknown. | |
| 40 }; | |
| 41 static_assert(INVALID == arraysize(kValidIceServiceTypes), | |
| 42 "kValidIceServiceTypes must have as many strings as ServiceType " | |
| 43 "has values."); | |
| 44 | |
| 45 // |in_str| should be of format | |
| 46 // stunURI = scheme ":" stun-host [ ":" stun-port ] | |
| 47 // scheme = "stun" / "stuns" | |
| 48 // stun-host = IP-literal / IPv4address / reg-name | |
| 49 // stun-port = *DIGIT | |
| 50 // | |
| 51 // draft-petithuguenin-behave-turn-uris-01 | |
| 52 // turnURI = scheme ":" turn-host [ ":" turn-port ] | |
| 53 // turn-host = username@IP-literal / IPv4address / reg-name | |
| 54 static bool GetServiceTypeAndHostnameFromUri(const std::string& in_str, | |
| 55 ServiceType* service_type, | |
| 56 std::string* hostname) { | |
| 57 const std::string::size_type colonpos = in_str.find(':'); | |
| 58 if (colonpos == std::string::npos) { | |
| 59 LOG(LS_WARNING) << "Missing ':' in ICE URI: " << in_str; | |
| 60 return false; | |
| 61 } | |
| 62 if ((colonpos + 1) == in_str.length()) { | |
| 63 LOG(LS_WARNING) << "Empty hostname in ICE URI: " << in_str; | |
| 64 return false; | |
| 65 } | |
| 66 *service_type = INVALID; | |
| 67 for (size_t i = 0; i < arraysize(kValidIceServiceTypes); ++i) { | |
| 68 if (in_str.compare(0, colonpos, kValidIceServiceTypes[i]) == 0) { | |
| 69 *service_type = static_cast<ServiceType>(i); | |
| 70 break; | |
| 71 } | |
| 72 } | |
| 73 if (*service_type == INVALID) { | |
| 74 return false; | |
| 75 } | |
| 76 *hostname = in_str.substr(colonpos + 1, std::string::npos); | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 static bool ParsePort(const std::string& in_str, int* port) { | |
| 81 // Make sure port only contains digits. FromString doesn't check this. | |
| 82 for (const char& c : in_str) { | |
| 83 if (!std::isdigit(c)) { | |
| 84 return false; | |
| 85 } | |
| 86 } | |
| 87 return rtc::FromString(in_str, port); | |
| 88 } | |
| 89 | |
| 90 // This method parses IPv6 and IPv4 literal strings, along with hostnames in | |
| 91 // standard hostname:port format. | |
| 92 // Consider following formats as correct. | |
| 93 // |hostname:port|, |[IPV6 address]:port|, |IPv4 address|:port, | |
| 94 // |hostname|, |[IPv6 address]|, |IPv4 address|. | |
| 95 static bool ParseHostnameAndPortFromString(const std::string& in_str, | |
| 96 std::string* host, | |
| 97 int* port) { | |
| 98 RTC_DCHECK(host->empty()); | |
| 99 if (in_str.at(0) == '[') { | |
| 100 std::string::size_type closebracket = in_str.rfind(']'); | |
| 101 if (closebracket != std::string::npos) { | |
| 102 std::string::size_type colonpos = in_str.find(':', closebracket); | |
| 103 if (std::string::npos != colonpos) { | |
| 104 if (!ParsePort(in_str.substr(closebracket + 2, std::string::npos), | |
| 105 port)) { | |
| 106 return false; | |
| 107 } | |
| 108 } | |
| 109 *host = in_str.substr(1, closebracket - 1); | |
| 110 } else { | |
| 111 return false; | |
| 112 } | |
| 113 } else { | |
| 114 std::string::size_type colonpos = in_str.find(':'); | |
| 115 if (std::string::npos != colonpos) { | |
| 116 if (!ParsePort(in_str.substr(colonpos + 1, std::string::npos), port)) { | |
| 117 return false; | |
| 118 } | |
| 119 *host = in_str.substr(0, colonpos); | |
| 120 } else { | |
| 121 *host = in_str; | |
| 122 } | |
| 123 } | |
| 124 return !host->empty(); | |
| 125 } | |
| 126 | |
| 127 // Adds a STUN or TURN server to the appropriate list, | |
| 128 // by parsing |url| and using the username/password in |server|. | |
| 129 static RTCErrorType ParseIceServerUrl( | |
| 130 const PeerConnectionInterface::IceServer& server, | |
| 131 const std::string& url, | |
| 132 cricket::ServerAddresses* stun_servers, | |
| 133 std::vector<cricket::RelayServerConfig>* turn_servers) { | |
| 134 // draft-nandakumar-rtcweb-stun-uri-01 | |
| 135 // stunURI = scheme ":" stun-host [ ":" stun-port ] | |
| 136 // scheme = "stun" / "stuns" | |
| 137 // stun-host = IP-literal / IPv4address / reg-name | |
| 138 // stun-port = *DIGIT | |
| 139 | |
| 140 // draft-petithuguenin-behave-turn-uris-01 | |
| 141 // turnURI = scheme ":" turn-host [ ":" turn-port ] | |
| 142 // [ "?transport=" transport ] | |
| 143 // scheme = "turn" / "turns" | |
| 144 // transport = "udp" / "tcp" / transport-ext | |
| 145 // transport-ext = 1*unreserved | |
| 146 // turn-host = IP-literal / IPv4address / reg-name | |
| 147 // turn-port = *DIGIT | |
| 148 RTC_DCHECK(stun_servers != nullptr); | |
| 149 RTC_DCHECK(turn_servers != nullptr); | |
| 150 std::vector<std::string> tokens; | |
| 151 cricket::ProtocolType turn_transport_type = cricket::PROTO_UDP; | |
| 152 RTC_DCHECK(!url.empty()); | |
| 153 rtc::tokenize_with_empty_tokens(url, '?', &tokens); | |
| 154 std::string uri_without_transport = tokens[0]; | |
| 155 // Let's look into transport= param, if it exists. | |
| 156 if (tokens.size() == kTurnTransportTokensNum) { // ?transport= is present. | |
| 157 std::string uri_transport_param = tokens[1]; | |
| 158 rtc::tokenize_with_empty_tokens(uri_transport_param, '=', &tokens); | |
| 159 if (tokens[0] != kTransport) { | |
| 160 LOG(LS_WARNING) << "Invalid transport parameter key."; | |
| 161 return RTCErrorType::SYNTAX_ERROR; | |
| 162 } | |
| 163 if (tokens.size() < 2) { | |
| 164 LOG(LS_WARNING) << "Transport parameter missing value."; | |
| 165 return RTCErrorType::SYNTAX_ERROR; | |
| 166 } | |
| 167 if (!cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) || | |
| 168 (turn_transport_type != cricket::PROTO_UDP && | |
| 169 turn_transport_type != cricket::PROTO_TCP)) { | |
| 170 LOG(LS_WARNING) << "Transport parameter should always be udp or tcp."; | |
| 171 return RTCErrorType::SYNTAX_ERROR; | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 std::string hoststring; | |
| 176 ServiceType service_type; | |
| 177 if (!GetServiceTypeAndHostnameFromUri(uri_without_transport, &service_type, | |
| 178 &hoststring)) { | |
| 179 LOG(LS_WARNING) << "Invalid transport parameter in ICE URI: " << url; | |
| 180 return RTCErrorType::SYNTAX_ERROR; | |
| 181 } | |
| 182 | |
| 183 // GetServiceTypeAndHostnameFromUri should never give an empty hoststring | |
| 184 RTC_DCHECK(!hoststring.empty()); | |
| 185 | |
| 186 // Let's break hostname. | |
| 187 tokens.clear(); | |
| 188 rtc::tokenize_with_empty_tokens(hoststring, '@', &tokens); | |
| 189 | |
| 190 std::string username(server.username); | |
| 191 if (tokens.size() > kTurnHostTokensNum) { | |
| 192 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring; | |
| 193 return RTCErrorType::SYNTAX_ERROR; | |
| 194 } | |
| 195 if (tokens.size() == kTurnHostTokensNum) { | |
| 196 if (tokens[0].empty() || tokens[1].empty()) { | |
| 197 LOG(LS_WARNING) << "Invalid user@hostname format: " << hoststring; | |
| 198 return RTCErrorType::SYNTAX_ERROR; | |
| 199 } | |
| 200 username.assign(rtc::s_url_decode(tokens[0])); | |
| 201 hoststring = tokens[1]; | |
| 202 } else { | |
| 203 hoststring = tokens[0]; | |
| 204 } | |
| 205 | |
| 206 int port = kDefaultStunPort; | |
| 207 if (service_type == TURNS) { | |
| 208 port = kDefaultStunTlsPort; | |
| 209 turn_transport_type = cricket::PROTO_TLS; | |
| 210 } | |
| 211 | |
| 212 std::string address; | |
| 213 if (!ParseHostnameAndPortFromString(hoststring, &address, &port)) { | |
| 214 LOG(WARNING) << "Invalid hostname format: " << uri_without_transport; | |
| 215 return RTCErrorType::SYNTAX_ERROR; | |
| 216 } | |
| 217 | |
| 218 if (port <= 0 || port > 0xffff) { | |
| 219 LOG(WARNING) << "Invalid port: " << port; | |
| 220 return RTCErrorType::SYNTAX_ERROR; | |
| 221 } | |
| 222 | |
| 223 switch (service_type) { | |
| 224 case STUN: | |
| 225 case STUNS: | |
| 226 stun_servers->insert(rtc::SocketAddress(address, port)); | |
| 227 break; | |
| 228 case TURN: | |
| 229 case TURNS: { | |
| 230 if (username.empty() || server.password.empty()) { | |
| 231 // The WebRTC spec requires throwing an InvalidAccessError when username | |
| 232 // or credential are ommitted; this is the native equivalent. | |
| 233 return RTCErrorType::INVALID_PARAMETER; | |
| 234 } | |
| 235 cricket::RelayServerConfig config = cricket::RelayServerConfig( | |
| 236 address, port, username, server.password, turn_transport_type); | |
| 237 if (server.tls_cert_policy == | |
| 238 PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck) { | |
| 239 config.tls_cert_policy = | |
| 240 cricket::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK; | |
| 241 } | |
| 242 turn_servers->push_back(config); | |
| 243 break; | |
| 244 } | |
| 245 default: | |
| 246 // We shouldn't get to this point with an invalid service_type, we should | |
| 247 // have returned an error already. | |
| 248 RTC_NOTREACHED() << "Unexpected service type"; | |
| 249 return RTCErrorType::INTERNAL_ERROR; | |
| 250 } | |
| 251 return RTCErrorType::NONE; | |
| 252 } | |
| 253 | |
| 254 RTCErrorType ParseIceServers( | |
| 255 const PeerConnectionInterface::IceServers& servers, | |
| 256 cricket::ServerAddresses* stun_servers, | |
| 257 std::vector<cricket::RelayServerConfig>* turn_servers) { | |
| 258 for (const PeerConnectionInterface::IceServer& server : servers) { | |
| 259 if (!server.urls.empty()) { | |
| 260 for (const std::string& url : server.urls) { | |
| 261 if (url.empty()) { | |
| 262 LOG(LS_ERROR) << "Empty uri."; | |
| 263 return RTCErrorType::SYNTAX_ERROR; | |
| 264 } | |
| 265 RTCErrorType err = | |
| 266 ParseIceServerUrl(server, url, stun_servers, turn_servers); | |
| 267 if (err != RTCErrorType::NONE) { | |
| 268 return err; | |
| 269 } | |
| 270 } | |
| 271 } else if (!server.uri.empty()) { | |
| 272 // Fallback to old .uri if new .urls isn't present. | |
| 273 RTCErrorType err = | |
| 274 ParseIceServerUrl(server, server.uri, stun_servers, turn_servers); | |
| 275 if (err != RTCErrorType::NONE) { | |
| 276 return err; | |
| 277 } | |
| 278 } else { | |
| 279 LOG(LS_ERROR) << "Empty uri."; | |
| 280 return RTCErrorType::SYNTAX_ERROR; | |
| 281 } | |
| 282 } | |
| 283 // Candidates must have unique priorities, so that connectivity checks | |
| 284 // are performed in a well-defined order. | |
| 285 int priority = static_cast<int>(turn_servers->size() - 1); | |
| 286 for (cricket::RelayServerConfig& turn_server : *turn_servers) { | |
| 287 // First in the list gets highest priority. | |
| 288 turn_server.priority = priority--; | |
| 289 } | |
| 290 return RTCErrorType::NONE; | |
| 291 } | |
| 292 | |
| 293 } // namespace webrtc | |
| OLD | NEW |