You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
11 KiB

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