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