blob: 2fe8db2c97e1ee44806a0b6b23bd5e723758391b [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2004 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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef P2P_BASE_PORT_ALLOCATOR_H_
12#define P2P_BASE_PORT_ALLOCATOR_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
Taylor Brandstettera1c30352016-05-13 08:15:11 -070014#include <deque>
15#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include <string>
17#include <vector>
18
Honghai Zhangf8998cf2019-10-14 11:27:50 -070019#include "api/transport/enums.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "p2p/base/port_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/helpers.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/proxy_info.h"
24#include "rtc_base/ssl_certificate.h"
Mirko Bonadei3b56ee72018-10-15 17:15:12 +020025#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020026#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/thread.h"
Qingsi Wanga2d60672018-04-11 16:57:45 -070028#include "rtc_base/thread_checker.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000029
Honghai Zhangd93f50c2016-10-05 11:47:22 -070030namespace webrtc {
Jonas Orelandbdcee282017-10-10 14:01:40 +020031class TurnCustomizer;
Yves Gerey665174f2018-06-19 15:03:05 +020032} // namespace webrtc
Honghai Zhangd93f50c2016-10-05 11:47:22 -070033
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000034namespace cricket {
35
36// PortAllocator is responsible for allocating Port types for a given
37// P2PSocket. It also handles port freeing.
38//
39// Clients can override this class to control port allocation, including
40// what kinds of ports are allocated.
41
42enum {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070043 // Disable local UDP ports. This doesn't impact how we connect to relay
44 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045 PORTALLOCATOR_DISABLE_UDP = 0x01,
46 PORTALLOCATOR_DISABLE_STUN = 0x02,
47 PORTALLOCATOR_DISABLE_RELAY = 0x04,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070048 // Disable local TCP ports. This doesn't impact how we connect to relay
49 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050 PORTALLOCATOR_DISABLE_TCP = 0x08,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
53 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080054 // When specified, we'll only allocate the STUN candidate for the public
55 // interface as seen by regular http traffic and the HOST candidate associated
56 // with the default local interface.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000057 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080058 // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the
59 // default local candidate mentioned above will not be allocated. Only the
60 // STUN candidate will be.
61 PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070062 // Disallow use of UDP when connecting to a relay server. Since proxy servers
63 // usually don't handle UDP, using UDP will leak the IP address.
64 PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000,
honghaiz60347052016-05-31 18:29:12 -070065
66 // When multiple networks exist, do not gather candidates on the ones with
67 // high cost. So if both Wi-Fi and cellular networks exist, gather only on the
68 // Wi-Fi network. If a network type is "unknown", it has a cost lower than
69 // cellular but higher than Wi-Fi/Ethernet. So if an unknown network exists,
70 // cellular networks will not be used to gather candidates and if a Wi-Fi
71 // network is present, "unknown" networks will not be usd to gather
72 // candidates. Doing so ensures that even if a cellular network type was not
73 // detected initially, it would not be used if a Wi-Fi network is present.
74 PORTALLOCATOR_DISABLE_COSTLY_NETWORKS = 0x2000,
zhihuangb09b3f92017-03-07 14:40:51 -080075
76 // When specified, do not collect IPv6 ICE candidates on Wi-Fi.
77 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI = 0x4000,
deadbeef1ee21252017-06-13 15:49:45 -070078
Mirko Bonadei5f4d47b2018-08-22 17:41:22 +000079 // When this flag is set, ports not bound to any specific network interface
80 // will be used, in addition to normal ports bound to the enumerated
81 // interfaces. Without this flag, these "any address" ports would only be
82 // used when network enumeration fails or is disabled. But under certain
83 // conditions, these ports may succeed where others fail, so they may allow
84 // the application to work in a wider variety of environments, at the expense
85 // of having to allocate additional candidates.
Qingsi Wangefbcb312018-08-21 23:23:26 +000086 PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS = 0x8000,
87
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +010088 // Exclude link-local network interfaces
89 // from considertaion after adapter enumeration.
Qingsi Wangefbcb312018-08-21 23:23:26 +000090 PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS = 0x10000,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091};
92
Honghai Zhangd93f50c2016-10-05 11:47:22 -070093// Defines various reasons that have caused ICE regathering.
Steve Anton300bf8e2017-07-14 10:13:10 -070094enum class IceRegatheringReason {
95 NETWORK_CHANGE, // Network interfaces on the device changed
96 NETWORK_FAILURE, // Regather only on networks that have failed
97 OCCASIONAL_REFRESH, // Periodic regather on all networks
98 MAX_VALUE
99};
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700100
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101const uint32_t kDefaultPortAllocatorFlags = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000104// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
105// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200106const uint32_t kMinimumStepDelay = 50;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107
deadbeef3427f532017-07-26 16:09:33 -0700108// Turning on IPv6 could make many IPv6 interfaces available for connectivity
109// check and delay the call setup time. kDefaultMaxIPv6Networks is the default
110// upper limit of IPv6 networks but could be changed by
111// set_max_ipv6_networks().
112constexpr int kDefaultMaxIPv6Networks = 5;
113
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114// CF = CANDIDATE FILTER
Oleh Prypinfd7df982017-12-21 16:25:19 +0100115enum : uint32_t {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116 CF_NONE = 0x0,
117 CF_HOST = 0x1,
118 CF_REFLEXIVE = 0x2,
119 CF_RELAY = 0x4,
120 CF_ALL = 0x7,
121};
122
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000123// TLS certificate policy.
124enum class TlsCertPolicy {
125 // For TLS based protocols, ensure the connection is secure by not
126 // circumventing certificate validation.
127 TLS_CERT_POLICY_SECURE,
128 // For TLS based protocols, disregard security completely by skipping
129 // certificate validation. This is insecure and should never be used unless
130 // security is irrelevant in that particular context.
131 TLS_CERT_POLICY_INSECURE_NO_CHECK,
132};
133
deadbeef653b8e02015-11-11 12:55:10 -0800134// TODO(deadbeef): Rename to TurnCredentials (and username to ufrag).
135struct RelayCredentials {
136 RelayCredentials() {}
137 RelayCredentials(const std::string& username, const std::string& password)
138 : username(username), password(password) {}
139
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700140 bool operator==(const RelayCredentials& o) const {
141 return username == o.username && password == o.password;
142 }
143 bool operator!=(const RelayCredentials& o) const { return !(*this == o); }
144
deadbeef653b8e02015-11-11 12:55:10 -0800145 std::string username;
146 std::string password;
147};
148
149typedef std::vector<ProtocolAddress> PortList;
150// TODO(deadbeef): Rename to TurnServerConfig.
Mirko Bonadei3b56ee72018-10-15 17:15:12 +0200151struct RTC_EXPORT RelayServerConfig {
Niels Möller191e38f2019-11-04 08:49:12 +0100152 RelayServerConfig();
Emad Omaradab1d2d2017-06-16 15:43:11 -0700153 RelayServerConfig(const rtc::SocketAddress& address,
154 const std::string& username,
155 const std::string& password,
Steve Anton7995d8c2017-10-30 16:23:38 -0700156 ProtocolType proto);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800157 RelayServerConfig(const std::string& address,
158 int port,
159 const std::string& username,
160 const std::string& password,
Steve Anton7995d8c2017-10-30 16:23:38 -0700161 ProtocolType proto);
hnsl277b2502016-12-13 05:17:23 -0800162 // Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS.
163 RelayServerConfig(const std::string& address,
164 int port,
165 const std::string& username,
166 const std::string& password,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800167 ProtocolType proto,
Steve Anton7995d8c2017-10-30 16:23:38 -0700168 bool secure);
169 RelayServerConfig(const RelayServerConfig&);
170 ~RelayServerConfig();
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800171
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700172 bool operator==(const RelayServerConfig& o) const {
Niels Möller191e38f2019-11-04 08:49:12 +0100173 return ports == o.ports && credentials == o.credentials &&
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700174 priority == o.priority;
175 }
176 bool operator!=(const RelayServerConfig& o) const { return !(*this == o); }
177
deadbeef653b8e02015-11-11 12:55:10 -0800178 PortList ports;
179 RelayCredentials credentials;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700180 int priority = 0;
hnsl04833622017-01-09 08:35:45 -0800181 TlsCertPolicy tls_cert_policy = TlsCertPolicy::TLS_CERT_POLICY_SECURE;
Diogo Real1dca9d52017-08-29 12:18:32 -0700182 std::vector<std::string> tls_alpn_protocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700183 std::vector<std::string> tls_elliptic_curves;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700184 rtc::SSLCertificateVerifier* tls_cert_verifier = nullptr;
Jonas Oreland3c028422019-08-22 16:16:35 +0200185 std::string turn_logging_id;
deadbeef653b8e02015-11-11 12:55:10 -0800186};
187
Mirko Bonadei3b56ee72018-10-15 17:15:12 +0200188class RTC_EXPORT PortAllocatorSession : public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189 public:
190 // Content name passed in mostly for logging and debugging.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 PortAllocatorSession(const std::string& content_name,
192 int component,
deadbeefcbecd352015-09-23 11:50:27 -0700193 const std::string& ice_ufrag,
194 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200195 uint32_t flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196
197 // Subclasses should clean up any ports created.
Steve Anton7995d8c2017-10-30 16:23:38 -0700198 ~PortAllocatorSession() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199
Peter Boström0c4e06b2015-10-07 12:23:21 +0200200 uint32_t flags() const { return flags_; }
201 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 std::string content_name() const { return content_name_; }
203 int component() const { return component_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700204 const std::string& ice_ufrag() const { return ice_ufrag_; }
205 const std::string& ice_pwd() const { return ice_pwd_; }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200206 bool pooled() const { return pooled_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700208 // Setting this filter should affect not only candidates gathered in the
209 // future, but candidates already gathered and ports already "ready",
210 // which would be returned by ReadyCandidates() and ReadyPorts().
211 //
212 // Default filter should be CF_ALL.
213 virtual void SetCandidateFilter(uint32_t filter) = 0;
214
deadbeefb60a8192016-08-24 15:15:00 -0700215 // Starts gathering ports and ICE candidates.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700216 virtual void StartGettingPorts() = 0;
deadbeefb60a8192016-08-24 15:15:00 -0700217 // Completely stops gathering. Will not gather again unless StartGettingPorts
218 // is called again.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700219 virtual void StopGettingPorts() = 0;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700220 // Whether the session is actively getting ports.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700221 virtual bool IsGettingPorts() = 0;
deadbeefb60a8192016-08-24 15:15:00 -0700222
223 //
224 // NOTE: The group of methods below is only used for continual gathering.
225 //
226
227 // ClearGettingPorts should have the same immediate effect as
228 // StopGettingPorts, but if the implementation supports continual gathering,
229 // ClearGettingPorts allows additional ports/candidates to be gathered if the
230 // network conditions change.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700231 virtual void ClearGettingPorts() = 0;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700232 // Whether it is in the state where the existing gathering process is stopped,
233 // but new ones may be started (basically after calling ClearGettingPorts).
Steve Anton7995d8c2017-10-30 16:23:38 -0700234 virtual bool IsCleared() const;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700235 // Whether the session has completely stopped.
Steve Anton7995d8c2017-10-30 16:23:38 -0700236 virtual bool IsStopped() const;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700237 // Re-gathers candidates on networks that do not have any connections. More
238 // precisely, a network interface may have more than one IP addresses (e.g.,
239 // IPv4 and IPv6 addresses). Each address subnet will be used to create a
240 // network. Only if all networks of an interface have no connection, the
241 // implementation should start re-gathering on all networks of that interface.
242 virtual void RegatherOnFailedNetworks() {}
243 // Re-gathers candidates on all networks.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700244 virtual void RegatherOnAllNetworks() {}
Qingsi Wang72a43a12018-02-20 16:03:18 -0800245 // Get candidate-level stats from all candidates on the ready ports and return
246 // the stats to the given list.
247 virtual void GetCandidateStatsFromReadyPorts(
Qingsi Wang7627fdd2019-08-19 16:07:40 -0700248 CandidateStatsList* candidate_stats_list) const {}
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800249 // Set the interval at which STUN candidates will resend STUN binding requests
250 // on the underlying ports to keep NAT bindings open.
251 // The default value of the interval in implementation is restored if a null
252 // optional value is passed.
253 virtual void SetStunKeepaliveIntervalForReadyPorts(
Danil Chapovalov00c71832018-06-15 15:58:38 +0200254 const absl::optional<int>& stun_keepalive_interval) {}
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700255 // Another way of getting the information provided by the signals below.
256 //
257 // Ports and candidates are not guaranteed to be in the same order as the
258 // signals were emitted in.
259 virtual std::vector<PortInterface*> ReadyPorts() const = 0;
260 virtual std::vector<Candidate> ReadyCandidates() const = 0;
261 virtual bool CandidatesAllocationDone() const = 0;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700262 // Marks all ports in the current session as "pruned" so that they may be
263 // destroyed if no connection is using them.
264 virtual void PruneAllPorts() {}
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700265
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000266 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700267 // Fires this signal when the network of the ports failed (either because the
268 // interface is down, or because there is no connection on the interface),
269 // or when TURN ports are pruned because a higher-priority TURN port becomes
270 // ready(pairable).
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700271 sigslot::signal2<PortAllocatorSession*, const std::vector<PortInterface*>&>
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700272 SignalPortsPruned;
Yves Gerey665174f2018-06-19 15:03:05 +0200273 sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
274 SignalCandidatesReady;
Eldar Relloda13ea22019-06-01 12:23:43 +0300275 sigslot::signal2<PortAllocatorSession*, const IceCandidateErrorEvent&>
276 SignalCandidateError;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700277 // Candidates should be signaled to be removed when the port that generated
278 // the candidates is removed.
279 sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
280 SignalCandidatesRemoved;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000281 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
282
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700283 sigslot::signal2<PortAllocatorSession*, IceRegatheringReason>
284 SignalIceRegathering;
285
Steve Anton7995d8c2017-10-30 16:23:38 -0700286 virtual uint32_t generation();
287 virtual void set_generation(uint32_t generation);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000288 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
289
deadbeefc55fb302016-05-12 12:51:38 -0700290 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700291 // This method is called when a pooled session (which doesn't have these
292 // properties initially) is returned by PortAllocator::TakePooledSession,
293 // and the content name, component, and ICE ufrag/pwd are updated.
294 //
295 // A subclass may need to override this method to perform additional actions,
296 // such as applying the updated information to ports and candidates.
297 virtual void UpdateIceParametersInternal() {}
298
deadbeefcbecd352015-09-23 11:50:27 -0700299 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
300 // ice_pwd.
301 const std::string& username() const { return ice_ufrag_; }
302 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000303
deadbeefc55fb302016-05-12 12:51:38 -0700304 private:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700305 void SetIceParameters(const std::string& content_name,
306 int component,
307 const std::string& ice_ufrag,
308 const std::string& ice_pwd) {
309 content_name_ = content_name;
310 component_ = component;
311 ice_ufrag_ = ice_ufrag;
312 ice_pwd_ = ice_pwd;
313 UpdateIceParametersInternal();
314 }
315
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200316 void set_pooled(bool value) { pooled_ = value; }
317
deadbeefc55fb302016-05-12 12:51:38 -0700318 uint32_t flags_;
319 uint32_t generation_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700320 std::string content_name_;
321 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700322 std::string ice_ufrag_;
323 std::string ice_pwd_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700324
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200325 bool pooled_ = false;
326
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700327 // SetIceParameters is an implementation detail which only PortAllocator
328 // should be able to call.
329 friend class PortAllocator;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000330};
331
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700332// Every method of PortAllocator (including the destructor) must be called on
Qingsi Wanga2d60672018-04-11 16:57:45 -0700333// the same thread after Initialize is called.
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700334//
Qingsi Wanga2d60672018-04-11 16:57:45 -0700335// This allows a PortAllocator subclass to be constructed and configured on one
336// thread, and passed into an object that uses it on a different thread.
Mirko Bonadei3b56ee72018-10-15 17:15:12 +0200337class RTC_EXPORT PortAllocator : public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000338 public:
Steve Anton7995d8c2017-10-30 16:23:38 -0700339 PortAllocator();
340 ~PortAllocator() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341
Qingsi Wanga2d60672018-04-11 16:57:45 -0700342 // This MUST be called on the PortAllocator's thread after finishing
343 // constructing and configuring the PortAllocator subclasses.
344 virtual void Initialize();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700345
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200346 // Set to true if some Ports need to know the ICE credentials when they are
347 // created. This will ensure that the PortAllocator will only match pooled
348 // allocator sessions to the ICE transport with the same credentials.
349 virtual void set_restrict_ice_credentials_change(bool value);
350
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700351 // Set STUN and TURN servers to be used in future sessions, and set
352 // candidate pool size, as described in JSEP.
353 //
deadbeef42a42632017-03-10 15:18:00 -0800354 // If the servers are changing, and the candidate pool size is nonzero, and
355 // FreezeCandidatePool hasn't been called, existing pooled sessions will be
356 // destroyed and new ones created.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700357 //
deadbeef42a42632017-03-10 15:18:00 -0800358 // If the servers are not changing but the candidate pool size is, and
359 // FreezeCandidatePool hasn't been called, pooled sessions will be either
360 // created or destroyed as necessary.
deadbeef6de92f92016-12-12 18:49:32 -0800361 //
362 // Returns true if the configuration could successfully be changed.
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700363 // Deprecated
deadbeef6de92f92016-12-12 18:49:32 -0800364 bool SetConfiguration(const ServerAddresses& stun_servers,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700365 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700366 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200367 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800368 webrtc::TurnCustomizer* turn_customizer = nullptr,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200369 const absl::optional<int>&
370 stun_candidate_keepalive_interval = absl::nullopt);
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700371 bool SetConfiguration(const ServerAddresses& stun_servers,
372 const std::vector<RelayServerConfig>& turn_servers,
373 int candidate_pool_size,
374 webrtc::PortPrunePolicy turn_port_prune_policy,
375 webrtc::TurnCustomizer* turn_customizer = nullptr,
376 const absl::optional<int>&
377 stun_candidate_keepalive_interval = absl::nullopt);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700378
Qingsi Wanga2d60672018-04-11 16:57:45 -0700379 const ServerAddresses& stun_servers() const {
380 CheckRunOnValidThreadIfInitialized();
381 return stun_servers_;
382 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700383
384 const std::vector<RelayServerConfig>& turn_servers() const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700385 CheckRunOnValidThreadIfInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700386 return turn_servers_;
387 }
388
Qingsi Wanga2d60672018-04-11 16:57:45 -0700389 int candidate_pool_size() const {
390 CheckRunOnValidThreadIfInitialized();
391 return candidate_pool_size_;
392 }
393
Danil Chapovalov00c71832018-06-15 15:58:38 +0200394 const absl::optional<int>& stun_candidate_keepalive_interval() const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700395 CheckRunOnValidThreadIfInitialized();
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800396 return stun_candidate_keepalive_interval_;
397 }
deadbeef653b8e02015-11-11 12:55:10 -0800398
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800399 // Sets the network types to ignore.
400 // Values are defined by the AdapterType enum.
401 // For instance, calling this with
402 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
403 // loopback interfaces.
404 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0;
405
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700406 std::unique_ptr<PortAllocatorSession> CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000407 const std::string& content_name,
408 int component,
409 const std::string& ice_ufrag,
410 const std::string& ice_pwd);
411
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700412 // Get an available pooled session and set the transport information on it.
413 //
414 // Caller takes ownership of the returned session.
415 //
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200416 // If restrict_ice_credentials_change is TRUE, then it will only
417 // return a pooled session with matching ice credentials.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700418 // If no pooled sessions are available, returns null.
419 std::unique_ptr<PortAllocatorSession> TakePooledSession(
420 const std::string& content_name,
421 int component,
422 const std::string& ice_ufrag,
423 const std::string& ice_pwd);
424
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200425 // Returns the next session that would be returned by TakePooledSession
426 // optionally restricting it to sessions with specified ice credentials.
427 const PortAllocatorSession* GetPooledSession(
428 const IceParameters* ice_credentials = nullptr) const;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700429
deadbeef42a42632017-03-10 15:18:00 -0800430 // After FreezeCandidatePool is called, changing the candidate pool size will
431 // no longer be allowed, and changing ICE servers will not cause pooled
432 // sessions to be recreated.
433 //
434 // Expected to be called when SetLocalDescription is called on a
435 // PeerConnection. Can be called safely on any thread as long as not
436 // simultaneously with SetConfiguration.
437 void FreezeCandidatePool();
438
439 // Discard any remaining pooled sessions.
440 void DiscardCandidatePool();
441
Qingsi Wang7627fdd2019-08-19 16:07:40 -0700442 // Clears the address and the related address fields of a local candidate to
443 // avoid IP leakage. This is applicable in several scenarios:
444 // 1. Sanitization is configured via the candidate filter.
445 // 2. Sanitization is configured via the port allocator flags.
446 // 3. mDNS concealment of private IPs is enabled.
447 Candidate SanitizeCandidate(const Candidate& c) const;
448
Qingsi Wanga2d60672018-04-11 16:57:45 -0700449 uint32_t flags() const {
450 CheckRunOnValidThreadIfInitialized();
451 return flags_;
452 }
453
454 void set_flags(uint32_t flags) {
455 CheckRunOnValidThreadIfInitialized();
456 flags_ = flags;
457 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000458
deadbeeff137e972017-03-23 15:45:49 -0700459 // These three methods are deprecated. If connections need to go through a
460 // proxy, the application should create a BasicPortAllocator given a custom
461 // PacketSocketFactory that creates proxy sockets.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700462 const std::string& user_agent() const {
463 CheckRunOnValidThreadIfInitialized();
464 return agent_;
465 }
466
467 const rtc::ProxyInfo& proxy() const {
468 CheckRunOnValidThreadIfInitialized();
469 return proxy_;
470 }
471
deadbeeff137e972017-03-23 15:45:49 -0700472 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700473 CheckRunOnValidThreadIfInitialized();
deadbeeff137e972017-03-23 15:45:49 -0700474 agent_ = agent;
475 proxy_ = proxy;
476 }
477
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000478 // Gets/Sets the port range to use when choosing client ports.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700479 int min_port() const {
480 CheckRunOnValidThreadIfInitialized();
481 return min_port_;
482 }
483
484 int max_port() const {
485 CheckRunOnValidThreadIfInitialized();
486 return max_port_;
487 }
488
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000489 bool SetPortRange(int min_port, int max_port) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700490 CheckRunOnValidThreadIfInitialized();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000491 if (min_port > max_port) {
492 return false;
493 }
494
495 min_port_ = min_port;
496 max_port_ = max_port;
497 return true;
498 }
499
deadbeef3427f532017-07-26 16:09:33 -0700500 // Can be used to change the default numer of IPv6 network interfaces used
501 // (5). Can set to INT_MAX to effectively disable the limit.
502 //
503 // TODO(deadbeef): Applications shouldn't have to arbitrarily limit the
504 // number of available IPv6 network interfaces just because they could slow
505 // ICE down. We should work on making our ICE logic smarter (for example,
506 // prioritizing pinging connections that are most likely to work) so that
507 // every network interface can be used without impacting ICE's speed.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700508 void set_max_ipv6_networks(int networks) {
509 CheckRunOnValidThreadIfInitialized();
510 max_ipv6_networks_ = networks;
511 }
512
513 int max_ipv6_networks() {
514 CheckRunOnValidThreadIfInitialized();
515 return max_ipv6_networks_;
516 }
deadbeef3427f532017-07-26 16:09:33 -0700517
deadbeef1c5e6d02017-09-15 17:46:56 -0700518 // Delay between different candidate gathering phases (UDP, TURN, TCP).
519 // Defaults to 1 second, but PeerConnection sets it to 50ms.
520 // TODO(deadbeef): Get rid of this. Its purpose is to avoid sending too many
521 // STUN transactions at once, but that's already happening if you configure
522 // multiple STUN servers or have multiple network interfaces. We should
523 // implement some global pacing logic instead if that's our goal.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700524 uint32_t step_delay() const {
525 CheckRunOnValidThreadIfInitialized();
526 return step_delay_;
527 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000528
Qingsi Wanga2d60672018-04-11 16:57:45 -0700529 void set_step_delay(uint32_t delay) {
530 CheckRunOnValidThreadIfInitialized();
531 step_delay_ = delay;
532 }
533
534 bool allow_tcp_listen() const {
535 CheckRunOnValidThreadIfInitialized();
536 return allow_tcp_listen_;
537 }
538
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000539 void set_allow_tcp_listen(bool allow_tcp_listen) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700540 CheckRunOnValidThreadIfInitialized();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000541 allow_tcp_listen_ = allow_tcp_listen;
542 }
543
Qingsi Wanga2d60672018-04-11 16:57:45 -0700544 uint32_t candidate_filter() {
545 CheckRunOnValidThreadIfInitialized();
546 return candidate_filter_;
547 }
548
Qingsi Wangc129c352019-04-18 10:41:58 -0700549 // The new filter value will be populated to future allocation sessions, when
550 // they are created via CreateSession, and also pooled sessions when one is
551 // taken via TakePooledSession.
552 //
553 // A change in the candidate filter also fires a signal
554 // |SignalCandidateFilterChanged|, so that objects subscribed to this signal
555 // can, for example, update the candidate filter for sessions created by this
556 // allocator and already taken by the object.
557 //
558 // Specifically for the session taken by the ICE transport, we currently do
559 // not support removing candidate pairs formed with local candidates from this
560 // session that are disabled by the new candidate filter.
561 void SetCandidateFilter(uint32_t filter);
562 // Deprecated.
563 // TODO(qingsi): Remove this after Chromium migrates to the new method.
564 void set_candidate_filter(uint32_t filter) { SetCandidateFilter(filter); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000565
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700566 // Deprecated (by the next method).
Qingsi Wanga2d60672018-04-11 16:57:45 -0700567 bool prune_turn_ports() const {
568 CheckRunOnValidThreadIfInitialized();
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700569 return turn_port_prune_policy_ == webrtc::PRUNE_BASED_ON_PRIORITY;
570 }
571
572 webrtc::PortPrunePolicy turn_port_prune_policy() const {
573 CheckRunOnValidThreadIfInitialized();
574 return turn_port_prune_policy_;
Qingsi Wanga2d60672018-04-11 16:57:45 -0700575 }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700576
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000577 // Gets/Sets the Origin value used for WebRTC STUN requests.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700578 const std::string& origin() const {
579 CheckRunOnValidThreadIfInitialized();
580 return origin_;
581 }
582
583 void set_origin(const std::string& origin) {
584 CheckRunOnValidThreadIfInitialized();
585 origin_ = origin;
586 }
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000587
Jonas Orelandbdcee282017-10-10 14:01:40 +0200588 webrtc::TurnCustomizer* turn_customizer() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700589 CheckRunOnValidThreadIfInitialized();
Jonas Orelandbdcee282017-10-10 14:01:40 +0200590 return turn_customizer_;
591 }
592
Qingsi Wang72a43a12018-02-20 16:03:18 -0800593 // Collect candidate stats from pooled allocator sessions. This can be used to
594 // collect candidate stats without creating an offer/answer or setting local
595 // description. After the local description is set, the ownership of the
596 // pooled session is taken by P2PTransportChannel, and the
597 // candidate stats can be collected from P2PTransportChannel::GetStats.
598 virtual void GetCandidateStatsFromPooledSessions(
599 CandidateStatsList* candidate_stats_list);
600
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200601 // Return IceParameters of the pooled sessions.
602 std::vector<IceParameters> GetPooledIceCredentials();
603
Qingsi Wangc129c352019-04-18 10:41:58 -0700604 // Fired when |candidate_filter_| changes.
605 sigslot::signal2<uint32_t /* prev_filter */, uint32_t /* cur_filter */>
606 SignalCandidateFilterChanged;
607
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000608 protected:
609 virtual PortAllocatorSession* CreateSessionInternal(
610 const std::string& content_name,
611 int component,
612 const std::string& ice_ufrag,
613 const std::string& ice_pwd) = 0;
614
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200615 const std::vector<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() {
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700616 return pooled_sessions_;
617 }
618
Qingsi Wang7627fdd2019-08-19 16:07:40 -0700619 // Returns true if there is an mDNS responder attached to the network manager.
620 virtual bool MdnsObfuscationEnabled() const { return false; }
621
Qingsi Wanga2d60672018-04-11 16:57:45 -0700622 // The following thread checks are only done in DCHECK for the consistency
623 // with the exsiting thread checks.
624 void CheckRunOnValidThreadIfInitialized() const {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200625 RTC_DCHECK(!initialized_ || thread_checker_.IsCurrent());
Qingsi Wanga2d60672018-04-11 16:57:45 -0700626 }
627
628 void CheckRunOnValidThreadAndInitialized() const {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200629 RTC_DCHECK(initialized_ && thread_checker_.IsCurrent());
Qingsi Wanga2d60672018-04-11 16:57:45 -0700630 }
631
632 bool initialized_ = false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200633 uint32_t flags_;
deadbeeff137e972017-03-23 15:45:49 -0700634 std::string agent_;
635 rtc::ProxyInfo proxy_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000636 int min_port_;
637 int max_port_;
deadbeef3427f532017-07-26 16:09:33 -0700638 int max_ipv6_networks_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200639 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000640 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200641 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000642 std::string origin_;
Qingsi Wanga2d60672018-04-11 16:57:45 -0700643 rtc::ThreadChecker thread_checker_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700644
645 private:
646 ServerAddresses stun_servers_;
647 std::vector<RelayServerConfig> turn_servers_;
deadbeef6de92f92016-12-12 18:49:32 -0800648 int candidate_pool_size_ = 0; // Last value passed into SetConfiguration.
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200649 std::vector<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
deadbeef42a42632017-03-10 15:18:00 -0800650 bool candidate_pool_frozen_ = false;
Honghai Zhangf8998cf2019-10-14 11:27:50 -0700651 webrtc::PortPrunePolicy turn_port_prune_policy_ = webrtc::NO_PRUNE;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700652
Jonas Orelandbdcee282017-10-10 14:01:40 +0200653 // Customizer for TURN messages.
654 // The instance is owned by application and will be shared among
655 // all TurnPort(s) created.
656 webrtc::TurnCustomizer* turn_customizer_ = nullptr;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800657
Danil Chapovalov00c71832018-06-15 15:58:38 +0200658 absl::optional<int> stun_candidate_keepalive_interval_;
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200659
660 // If true, TakePooledSession() will only return sessions that has same ice
661 // credentials as requested.
662 bool restrict_ice_credentials_change_ = false;
663
664 // Returns iterator to pooled session with specified ice_credentials or first
665 // if ice_credentials is nullptr.
666 std::vector<std::unique_ptr<PortAllocatorSession>>::const_iterator
667 FindPooledSession(const IceParameters* ice_credentials = nullptr) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000668};
669
670} // namespace cricket
671
Steve Anton10542f22019-01-11 09:11:00 -0800672#endif // P2P_BASE_PORT_ALLOCATOR_H_