File: ice_config.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (261 lines) | stat: -rw-r--r-- 8,370 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif

#include "remoting/protocol/ice_config.h"

#include <algorithm>

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "net/base/url_util.h"
#include "remoting/proto/remoting/v1/network_traversal_messages.pb.h"

namespace remoting::protocol {

namespace {

// See draft-petithuguenin-behave-turn-uris-01.
const int kDefaultStunTurnPort = 3478;
const int kDefaultTurnsPort = 5349;

bool ParseLifetime(const std::string& string, base::TimeDelta* result) {
  double seconds = 0;
  if (!base::EndsWith(string, "s", base::CompareCase::INSENSITIVE_ASCII) ||
      !base::StringToDouble(string.substr(0, string.size() - 1), &seconds)) {
    return false;
  }
  *result = base::Seconds(seconds);
  return true;
}

// Returns the smallest specified value, or 0 if neither is specified.
// A value is "specified" if it is greater than 0.
int MinimumSpecified(int value1, int value2) {
  if (value1 <= 0) {
    // value1 is not specified, so return value2 (or 0).
    return std::max(0, value2);
  }
  if (value2 <= 0) {
    // value1 is specified, so return it directly.
    return value1;
  }
  // Both values are specified, so return the minimum.
  return std::min(value1, value2);
}

}  // namespace

IceConfig::IceConfig() = default;
IceConfig::IceConfig(const IceConfig& other) = default;
IceConfig::~IceConfig() = default;

// static
IceConfig IceConfig::Parse(const base::Value::Dict& dictionary) {
  const base::Value::Dict* data = dictionary.FindDict("data");
  if (data) {
    return Parse(*data);
  }

  const base::Value::List* ice_servers_list = dictionary.FindList("iceServers");
  if (!ice_servers_list) {
    return IceConfig();
  }

  IceConfig ice_config;

  // Parse lifetimeDuration field.
  const std::string* lifetime_str = dictionary.FindString("lifetimeDuration");
  base::TimeDelta lifetime;
  if (!lifetime_str || !ParseLifetime(*lifetime_str, &lifetime)) {
    LOG(ERROR) << "Received invalid lifetimeDuration value: " << lifetime_str;

    // If the |lifetimeDuration| field is missing or cannot be parsed then mark
    // the config as expired so it will refreshed for the next session.
    ice_config.expiration_time = base::Time::Now();
  } else {
    ice_config.expiration_time = base::Time::Now() + lifetime;
  }

  // Parse iceServers list and store them in |ice_config|.
  bool errors_found = false;
  ice_config.max_bitrate_kbps = 0;
  for (const auto& server : *ice_servers_list) {
    const base::Value::Dict* server_dict = server.GetIfDict();
    if (!server_dict) {
      errors_found = true;
      continue;
    }

    const base::Value::List* urls_list = server_dict->FindList("urls");
    if (!urls_list) {
      errors_found = true;
      continue;
    }

    std::string username;
    const std::string* maybe_username = server_dict->FindString("username");
    if (maybe_username) {
      username = *maybe_username;
    }

    std::string password;
    const std::string* maybe_password = server_dict->FindString("credential");
    if (maybe_password) {
      password = *maybe_password;
    }

    // Compute the lowest specified bitrate of all the ICE servers.
    // Ideally the bitrate would be stored per ICE server, but it is not
    // possible (at the application level) to look up which particular
    // ICE server was used for the P2P connection.
    auto new_bitrate_double = server_dict->FindDouble("maxRateKbps");
    if (new_bitrate_double.has_value()) {
      ice_config.max_bitrate_kbps =
          MinimumSpecified(ice_config.max_bitrate_kbps,
                           static_cast<int>(new_bitrate_double.value()));
    }

    for (const auto& url : *urls_list) {
      const std::string* url_str = url.GetIfString();
      if (!url_str) {
        errors_found = true;
        continue;
      }
      if (!ice_config.AddServer(*url_str, username, password)) {
        LOG(ERROR) << "Invalid ICE server URL: " << *url_str;
      }
    }
  }

  if (errors_found) {
    std::string json;
    if (!base::JSONWriter::WriteWithOptions(
            dictionary, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json)) {
      NOTREACHED();
    }
    LOG(ERROR) << "Received ICE config with errors: " << json;
  }

  // If there are no STUN or no TURN servers then mark the config as expired so
  // it will refreshed for the next session.
  if (errors_found || ice_config.stun_servers.empty() ||
      ice_config.turn_servers.empty()) {
    ice_config.expiration_time = base::Time::Now();
  }

  return ice_config;
}

// static
IceConfig IceConfig::Parse(const apis::v1::GetIceConfigResponse& config) {
  IceConfig ice_config;

  // Parse lifetimeDuration field.
  base::TimeDelta lifetime =
      base::Seconds(config.lifetime_duration().seconds()) +
      base::Nanoseconds(config.lifetime_duration().nanos());
  ice_config.expiration_time = base::Time::Now() + lifetime;

  // Parse iceServers list and store them in |ice_config|.
  ice_config.max_bitrate_kbps = 0;
  for (const auto& server : config.servers()) {
    // Compute the lowest specified bitrate of all the ICE servers.
    // Ideally the bitrate would be stored per ICE server, but it is not
    // possible (at the application level) to look up which particular
    // ICE server was used for the P2P connection.
    ice_config.max_bitrate_kbps =
        MinimumSpecified(ice_config.max_bitrate_kbps, server.max_rate_kbps());

    for (const auto& url : server.urls()) {
      if (!ice_config.AddServer(url, server.username(), server.credential())) {
        LOG(ERROR) << "Invalid ICE server URL: " << url;
      }
    }
  }

  // If there are no STUN or no TURN servers then mark the config as expired so
  // it will be refreshed for the next session.
  if (ice_config.stun_servers.empty() || ice_config.turn_servers.empty()) {
    ice_config.expiration_time = base::Time::Now();
  }

  return ice_config;
}

bool IceConfig::AddStunServer(std::string_view url) {
  CHECK(url.starts_with("stun:"));
  return AddServer(url, /*username=*/"", /*password=*/"");
}

bool IceConfig::AddServer(std::string_view url,
                          const std::string& username,
                          const std::string& password) {
  webrtc::ProtocolType turn_transport_type = webrtc::PROTO_LAST;

  const char kTcpTransportSuffix[] = "?transport=tcp";
  const char kUdpTransportSuffix[] = "?transport=udp";
  if (base::EndsWith(url, kTcpTransportSuffix,
                     base::CompareCase::INSENSITIVE_ASCII)) {
    turn_transport_type = webrtc::PROTO_TCP;
    url.remove_suffix(strlen(kTcpTransportSuffix));
  } else if (base::EndsWith(url, kUdpTransportSuffix,
                            base::CompareCase::INSENSITIVE_ASCII)) {
    turn_transport_type = webrtc::PROTO_UDP;
    url.remove_suffix(strlen(kUdpTransportSuffix));
  }

  auto parts = base::SplitStringOnce(url, ':');
  if (!parts) {
    return false;
  }

  auto [protocol, host_and_port] = *parts;
  std::string host;
  int port;
  if (!net::ParseHostAndPort(host_and_port, &host, &port)) {
    return false;
  }

  if (protocol == "stun") {
    if (port == -1) {
      port = kDefaultStunTurnPort;
    }
    stun_servers.emplace_back(host, port);
  } else if (protocol == "turn") {
    if (port == -1) {
      port = kDefaultStunTurnPort;
    }
    if (turn_transport_type == webrtc::PROTO_LAST) {
      turn_transport_type = webrtc::PROTO_UDP;
    }
    turn_servers.emplace_back(host, port, username, password,
                              turn_transport_type, false);
  } else if (protocol == "turns") {
    if (port == -1) {
      port = kDefaultTurnsPort;
    }
    if (turn_transport_type == webrtc::PROTO_LAST) {
      turn_transport_type = webrtc::PROTO_TCP;
    }
    turn_servers.emplace_back(host, port, username, password,
                              turn_transport_type, true);
  } else {
    return false;
  }

  return true;
}

}  // namespace remoting::protocol