blob: 8bd709642c8aec3846581be8d6c6b06e78bce020 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_BASE_PORTALLOCATOR_H_
12#define P2P_BASE_PORTALLOCATOR_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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "p2p/base/port.h"
20#include "p2p/base/portinterface.h"
21#include "rtc_base/helpers.h"
22#include "rtc_base/proxyinfo.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070023#include "rtc_base/sslcertificate.h"
Artem Titove41c4332018-07-25 15:04:28 +020024#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/thread.h"
Qingsi Wanga2d60672018-04-11 16:57:45 -070026#include "rtc_base/thread_checker.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027
Honghai Zhangd93f50c2016-10-05 11:47:22 -070028namespace webrtc {
Jonas Orelandbdcee282017-10-10 14:01:40 +020029class TurnCustomizer;
Yves Gerey665174f2018-06-19 15:03:05 +020030} // namespace webrtc
Honghai Zhangd93f50c2016-10-05 11:47:22 -070031
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032namespace cricket {
33
34// PortAllocator is responsible for allocating Port types for a given
35// P2PSocket. It also handles port freeing.
36//
37// Clients can override this class to control port allocation, including
38// what kinds of ports are allocated.
39
40enum {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070041 // Disable local UDP ports. This doesn't impact how we connect to relay
42 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043 PORTALLOCATOR_DISABLE_UDP = 0x01,
44 PORTALLOCATOR_DISABLE_STUN = 0x02,
45 PORTALLOCATOR_DISABLE_RELAY = 0x04,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070046 // Disable local TCP ports. This doesn't impact how we connect to relay
47 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048 PORTALLOCATOR_DISABLE_TCP = 0x08,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
51 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080052 // When specified, we'll only allocate the STUN candidate for the public
53 // interface as seen by regular http traffic and the HOST candidate associated
54 // with the default local interface.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000055 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080056 // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the
57 // default local candidate mentioned above will not be allocated. Only the
58 // STUN candidate will be.
59 PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070060 // Disallow use of UDP when connecting to a relay server. Since proxy servers
61 // usually don't handle UDP, using UDP will leak the IP address.
62 PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000,
honghaiz60347052016-05-31 18:29:12 -070063
64 // When multiple networks exist, do not gather candidates on the ones with
65 // high cost. So if both Wi-Fi and cellular networks exist, gather only on the
66 // Wi-Fi network. If a network type is "unknown", it has a cost lower than
67 // cellular but higher than Wi-Fi/Ethernet. So if an unknown network exists,
68 // cellular networks will not be used to gather candidates and if a Wi-Fi
69 // network is present, "unknown" networks will not be usd to gather
70 // candidates. Doing so ensures that even if a cellular network type was not
71 // detected initially, it would not be used if a Wi-Fi network is present.
72 PORTALLOCATOR_DISABLE_COSTLY_NETWORKS = 0x2000,
zhihuangb09b3f92017-03-07 14:40:51 -080073
74 // When specified, do not collect IPv6 ICE candidates on Wi-Fi.
75 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI = 0x4000,
deadbeef1ee21252017-06-13 15:49:45 -070076
Qingsi Wang11659492018-08-21 23:20:47 +000077 // When this flag is set, ports not bound to any specific network interface
78 // will be used, in addition to normal ports bound to the enumerated
79 // interfaces. Without this flag, these "any address" ports would only be
80 // used when network enumeration fails or is disabled. But under certain
81 // conditions, these ports may succeed where others fail, so they may allow
82 // the application to work in a wider variety of environments, at the expense
83 // of having to allocate additional candidates.
Qingsi Wangefbcb312018-08-21 23:23:26 +000084 PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS = 0x8000,
85
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +010086 // Exclude link-local network interfaces
87 // from considertaion after adapter enumeration.
Qingsi Wangefbcb312018-08-21 23:23:26 +000088 PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS = 0x10000,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000089};
90
Honghai Zhangd93f50c2016-10-05 11:47:22 -070091// Defines various reasons that have caused ICE regathering.
Steve Anton300bf8e2017-07-14 10:13:10 -070092enum class IceRegatheringReason {
93 NETWORK_CHANGE, // Network interfaces on the device changed
94 NETWORK_FAILURE, // Regather only on networks that have failed
95 OCCASIONAL_REFRESH, // Periodic regather on all networks
96 MAX_VALUE
97};
Honghai Zhangd93f50c2016-10-05 11:47:22 -070098
Peter Boström0c4e06b2015-10-07 12:23:21 +020099const uint32_t kDefaultPortAllocatorFlags = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
103// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104const uint32_t kMinimumStepDelay = 50;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105
deadbeef3427f532017-07-26 16:09:33 -0700106// Turning on IPv6 could make many IPv6 interfaces available for connectivity
107// check and delay the call setup time. kDefaultMaxIPv6Networks is the default
108// upper limit of IPv6 networks but could be changed by
109// set_max_ipv6_networks().
110constexpr int kDefaultMaxIPv6Networks = 5;
111
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112// CF = CANDIDATE FILTER
Oleh Prypinfd7df982017-12-21 16:25:19 +0100113enum : uint32_t {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 CF_NONE = 0x0,
115 CF_HOST = 0x1,
116 CF_REFLEXIVE = 0x2,
117 CF_RELAY = 0x4,
118 CF_ALL = 0x7,
119};
120
hnsl04833622017-01-09 08:35:45 -0800121// TLS certificate policy.
122enum class TlsCertPolicy {
123 // For TLS based protocols, ensure the connection is secure by not
124 // circumventing certificate validation.
125 TLS_CERT_POLICY_SECURE,
126 // For TLS based protocols, disregard security completely by skipping
127 // certificate validation. This is insecure and should never be used unless
128 // security is irrelevant in that particular context.
129 TLS_CERT_POLICY_INSECURE_NO_CHECK,
130};
131
deadbeef653b8e02015-11-11 12:55:10 -0800132// TODO(deadbeef): Rename to TurnCredentials (and username to ufrag).
133struct RelayCredentials {
134 RelayCredentials() {}
135 RelayCredentials(const std::string& username, const std::string& password)
136 : username(username), password(password) {}
137
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700138 bool operator==(const RelayCredentials& o) const {
139 return username == o.username && password == o.password;
140 }
141 bool operator!=(const RelayCredentials& o) const { return !(*this == o); }
142
deadbeef653b8e02015-11-11 12:55:10 -0800143 std::string username;
144 std::string password;
145};
146
147typedef std::vector<ProtocolAddress> PortList;
148// TODO(deadbeef): Rename to TurnServerConfig.
149struct RelayServerConfig {
Steve Anton6c38cc72017-11-29 10:25:58 -0800150 explicit RelayServerConfig(RelayType type);
Emad Omaradab1d2d2017-06-16 15:43:11 -0700151 RelayServerConfig(const rtc::SocketAddress& address,
152 const std::string& username,
153 const std::string& password,
Steve Anton7995d8c2017-10-30 16:23:38 -0700154 ProtocolType proto);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800155 RelayServerConfig(const std::string& address,
156 int port,
157 const std::string& username,
158 const std::string& password,
Steve Anton7995d8c2017-10-30 16:23:38 -0700159 ProtocolType proto);
hnsl277b2502016-12-13 05:17:23 -0800160 // Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS.
161 RelayServerConfig(const std::string& address,
162 int port,
163 const std::string& username,
164 const std::string& password,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800165 ProtocolType proto,
Steve Anton7995d8c2017-10-30 16:23:38 -0700166 bool secure);
167 RelayServerConfig(const RelayServerConfig&);
168 ~RelayServerConfig();
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800169
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700170 bool operator==(const RelayServerConfig& o) const {
171 return type == o.type && ports == o.ports && credentials == o.credentials &&
172 priority == o.priority;
173 }
174 bool operator!=(const RelayServerConfig& o) const { return !(*this == o); }
175
deadbeef653b8e02015-11-11 12:55:10 -0800176 RelayType type;
177 PortList ports;
178 RelayCredentials credentials;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700179 int priority = 0;
hnsl04833622017-01-09 08:35:45 -0800180 TlsCertPolicy tls_cert_policy = TlsCertPolicy::TLS_CERT_POLICY_SECURE;
Diogo Real1dca9d52017-08-29 12:18:32 -0700181 std::vector<std::string> tls_alpn_protocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700182 std::vector<std::string> tls_elliptic_curves;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700183 rtc::SSLCertificateVerifier* tls_cert_verifier = nullptr;
deadbeef653b8e02015-11-11 12:55:10 -0800184};
185
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186class PortAllocatorSession : public sigslot::has_slots<> {
187 public:
188 // Content name passed in mostly for logging and debugging.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189 PortAllocatorSession(const std::string& content_name,
190 int component,
deadbeefcbecd352015-09-23 11:50:27 -0700191 const std::string& ice_ufrag,
192 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200193 uint32_t flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194
195 // Subclasses should clean up any ports created.
Steve Anton7995d8c2017-10-30 16:23:38 -0700196 ~PortAllocatorSession() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197
Peter Boström0c4e06b2015-10-07 12:23:21 +0200198 uint32_t flags() const { return flags_; }
199 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 std::string content_name() const { return content_name_; }
201 int component() const { return component_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700202 const std::string& ice_ufrag() const { return ice_ufrag_; }
203 const std::string& ice_pwd() const { return ice_pwd_; }
204 bool pooled() const { return ice_ufrag_.empty(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700206 // Setting this filter should affect not only candidates gathered in the
207 // future, but candidates already gathered and ports already "ready",
208 // which would be returned by ReadyCandidates() and ReadyPorts().
209 //
210 // Default filter should be CF_ALL.
211 virtual void SetCandidateFilter(uint32_t filter) = 0;
212
deadbeefb60a8192016-08-24 15:15:00 -0700213 // Starts gathering ports and ICE candidates.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700214 virtual void StartGettingPorts() = 0;
deadbeefb60a8192016-08-24 15:15:00 -0700215 // Completely stops gathering. Will not gather again unless StartGettingPorts
216 // is called again.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700217 virtual void StopGettingPorts() = 0;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700218 // Whether the session is actively getting ports.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700219 virtual bool IsGettingPorts() = 0;
deadbeefb60a8192016-08-24 15:15:00 -0700220
221 //
222 // NOTE: The group of methods below is only used for continual gathering.
223 //
224
225 // ClearGettingPorts should have the same immediate effect as
226 // StopGettingPorts, but if the implementation supports continual gathering,
227 // ClearGettingPorts allows additional ports/candidates to be gathered if the
228 // network conditions change.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700229 virtual void ClearGettingPorts() = 0;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700230 // Whether it is in the state where the existing gathering process is stopped,
231 // but new ones may be started (basically after calling ClearGettingPorts).
Steve Anton7995d8c2017-10-30 16:23:38 -0700232 virtual bool IsCleared() const;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700233 // Whether the session has completely stopped.
Steve Anton7995d8c2017-10-30 16:23:38 -0700234 virtual bool IsStopped() const;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700235 // Re-gathers candidates on networks that do not have any connections. More
236 // precisely, a network interface may have more than one IP addresses (e.g.,
237 // IPv4 and IPv6 addresses). Each address subnet will be used to create a
238 // network. Only if all networks of an interface have no connection, the
239 // implementation should start re-gathering on all networks of that interface.
240 virtual void RegatherOnFailedNetworks() {}
241 // Re-gathers candidates on all networks.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700242 virtual void RegatherOnAllNetworks() {}
Qingsi Wang72a43a12018-02-20 16:03:18 -0800243 // Get candidate-level stats from all candidates on the ready ports and return
244 // the stats to the given list.
245 virtual void GetCandidateStatsFromReadyPorts(
246 CandidateStatsList* candidate_stats_list) const;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800247 // Set the interval at which STUN candidates will resend STUN binding requests
248 // on the underlying ports to keep NAT bindings open.
249 // The default value of the interval in implementation is restored if a null
250 // optional value is passed.
251 virtual void SetStunKeepaliveIntervalForReadyPorts(
Danil Chapovalov00c71832018-06-15 15:58:38 +0200252 const absl::optional<int>& stun_keepalive_interval) {}
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700253 // Another way of getting the information provided by the signals below.
254 //
255 // Ports and candidates are not guaranteed to be in the same order as the
256 // signals were emitted in.
257 virtual std::vector<PortInterface*> ReadyPorts() const = 0;
258 virtual std::vector<Candidate> ReadyCandidates() const = 0;
259 virtual bool CandidatesAllocationDone() const = 0;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700260 // Marks all ports in the current session as "pruned" so that they may be
261 // destroyed if no connection is using them.
262 virtual void PruneAllPorts() {}
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700263
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000264 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700265 // Fires this signal when the network of the ports failed (either because the
266 // interface is down, or because there is no connection on the interface),
267 // or when TURN ports are pruned because a higher-priority TURN port becomes
268 // ready(pairable).
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700269 sigslot::signal2<PortAllocatorSession*, const std::vector<PortInterface*>&>
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700270 SignalPortsPruned;
Yves Gerey665174f2018-06-19 15:03:05 +0200271 sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
272 SignalCandidatesReady;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700273 // Candidates should be signaled to be removed when the port that generated
274 // the candidates is removed.
275 sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
276 SignalCandidatesRemoved;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000277 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
278
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700279 sigslot::signal2<PortAllocatorSession*, IceRegatheringReason>
280 SignalIceRegathering;
281
Steve Anton7995d8c2017-10-30 16:23:38 -0700282 virtual uint32_t generation();
283 virtual void set_generation(uint32_t generation);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000284 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
285
deadbeefc55fb302016-05-12 12:51:38 -0700286 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700287 // This method is called when a pooled session (which doesn't have these
288 // properties initially) is returned by PortAllocator::TakePooledSession,
289 // and the content name, component, and ICE ufrag/pwd are updated.
290 //
291 // A subclass may need to override this method to perform additional actions,
292 // such as applying the updated information to ports and candidates.
293 virtual void UpdateIceParametersInternal() {}
294
deadbeefcbecd352015-09-23 11:50:27 -0700295 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
296 // ice_pwd.
297 const std::string& username() const { return ice_ufrag_; }
298 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000299
deadbeefc55fb302016-05-12 12:51:38 -0700300 private:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700301 void SetIceParameters(const std::string& content_name,
302 int component,
303 const std::string& ice_ufrag,
304 const std::string& ice_pwd) {
305 content_name_ = content_name;
306 component_ = component;
307 ice_ufrag_ = ice_ufrag;
308 ice_pwd_ = ice_pwd;
309 UpdateIceParametersInternal();
310 }
311
deadbeefc55fb302016-05-12 12:51:38 -0700312 uint32_t flags_;
313 uint32_t generation_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700314 std::string content_name_;
315 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700316 std::string ice_ufrag_;
317 std::string ice_pwd_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700318
319 // SetIceParameters is an implementation detail which only PortAllocator
320 // should be able to call.
321 friend class PortAllocator;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000322};
323
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700324// Every method of PortAllocator (including the destructor) must be called on
Qingsi Wanga2d60672018-04-11 16:57:45 -0700325// the same thread after Initialize is called.
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700326//
Qingsi Wanga2d60672018-04-11 16:57:45 -0700327// This allows a PortAllocator subclass to be constructed and configured on one
328// thread, and passed into an object that uses it on a different thread.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000329class PortAllocator : public sigslot::has_slots<> {
330 public:
Steve Anton7995d8c2017-10-30 16:23:38 -0700331 PortAllocator();
332 ~PortAllocator() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000333
Qingsi Wanga2d60672018-04-11 16:57:45 -0700334 // This MUST be called on the PortAllocator's thread after finishing
335 // constructing and configuring the PortAllocator subclasses.
336 virtual void Initialize();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700337
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700338 // Set STUN and TURN servers to be used in future sessions, and set
339 // candidate pool size, as described in JSEP.
340 //
deadbeef42a42632017-03-10 15:18:00 -0800341 // If the servers are changing, and the candidate pool size is nonzero, and
342 // FreezeCandidatePool hasn't been called, existing pooled sessions will be
343 // destroyed and new ones created.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700344 //
deadbeef42a42632017-03-10 15:18:00 -0800345 // If the servers are not changing but the candidate pool size is, and
346 // FreezeCandidatePool hasn't been called, pooled sessions will be either
347 // created or destroyed as necessary.
deadbeef6de92f92016-12-12 18:49:32 -0800348 //
349 // Returns true if the configuration could successfully be changed.
350 bool SetConfiguration(const ServerAddresses& stun_servers,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700351 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700352 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200353 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800354 webrtc::TurnCustomizer* turn_customizer = nullptr,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200355 const absl::optional<int>&
356 stun_candidate_keepalive_interval = absl::nullopt);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700357
Qingsi Wanga2d60672018-04-11 16:57:45 -0700358 const ServerAddresses& stun_servers() const {
359 CheckRunOnValidThreadIfInitialized();
360 return stun_servers_;
361 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700362
363 const std::vector<RelayServerConfig>& turn_servers() const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700364 CheckRunOnValidThreadIfInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700365 return turn_servers_;
366 }
367
Qingsi Wanga2d60672018-04-11 16:57:45 -0700368 int candidate_pool_size() const {
369 CheckRunOnValidThreadIfInitialized();
370 return candidate_pool_size_;
371 }
372
Danil Chapovalov00c71832018-06-15 15:58:38 +0200373 const absl::optional<int>& stun_candidate_keepalive_interval() const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700374 CheckRunOnValidThreadIfInitialized();
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800375 return stun_candidate_keepalive_interval_;
376 }
deadbeef653b8e02015-11-11 12:55:10 -0800377
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800378 // Sets the network types to ignore.
379 // Values are defined by the AdapterType enum.
380 // For instance, calling this with
381 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
382 // loopback interfaces.
383 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0;
384
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700385 std::unique_ptr<PortAllocatorSession> CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386 const std::string& content_name,
387 int component,
388 const std::string& ice_ufrag,
389 const std::string& ice_pwd);
390
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700391 // Get an available pooled session and set the transport information on it.
392 //
393 // Caller takes ownership of the returned session.
394 //
395 // If no pooled sessions are available, returns null.
396 std::unique_ptr<PortAllocatorSession> TakePooledSession(
397 const std::string& content_name,
398 int component,
399 const std::string& ice_ufrag,
400 const std::string& ice_pwd);
401
402 // Returns the next session that would be returned by TakePooledSession.
403 const PortAllocatorSession* GetPooledSession() const;
404
deadbeef42a42632017-03-10 15:18:00 -0800405 // After FreezeCandidatePool is called, changing the candidate pool size will
406 // no longer be allowed, and changing ICE servers will not cause pooled
407 // sessions to be recreated.
408 //
409 // Expected to be called when SetLocalDescription is called on a
410 // PeerConnection. Can be called safely on any thread as long as not
411 // simultaneously with SetConfiguration.
412 void FreezeCandidatePool();
413
414 // Discard any remaining pooled sessions.
415 void DiscardCandidatePool();
416
Qingsi Wanga2d60672018-04-11 16:57:45 -0700417 uint32_t flags() const {
418 CheckRunOnValidThreadIfInitialized();
419 return flags_;
420 }
421
422 void set_flags(uint32_t flags) {
423 CheckRunOnValidThreadIfInitialized();
424 flags_ = flags;
425 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000426
deadbeeff137e972017-03-23 15:45:49 -0700427 // These three methods are deprecated. If connections need to go through a
428 // proxy, the application should create a BasicPortAllocator given a custom
429 // PacketSocketFactory that creates proxy sockets.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700430 const std::string& user_agent() const {
431 CheckRunOnValidThreadIfInitialized();
432 return agent_;
433 }
434
435 const rtc::ProxyInfo& proxy() const {
436 CheckRunOnValidThreadIfInitialized();
437 return proxy_;
438 }
439
deadbeeff137e972017-03-23 15:45:49 -0700440 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700441 CheckRunOnValidThreadIfInitialized();
deadbeeff137e972017-03-23 15:45:49 -0700442 agent_ = agent;
443 proxy_ = proxy;
444 }
445
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000446 // Gets/Sets the port range to use when choosing client ports.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700447 int min_port() const {
448 CheckRunOnValidThreadIfInitialized();
449 return min_port_;
450 }
451
452 int max_port() const {
453 CheckRunOnValidThreadIfInitialized();
454 return max_port_;
455 }
456
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000457 bool SetPortRange(int min_port, int max_port) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700458 CheckRunOnValidThreadIfInitialized();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000459 if (min_port > max_port) {
460 return false;
461 }
462
463 min_port_ = min_port;
464 max_port_ = max_port;
465 return true;
466 }
467
deadbeef3427f532017-07-26 16:09:33 -0700468 // Can be used to change the default numer of IPv6 network interfaces used
469 // (5). Can set to INT_MAX to effectively disable the limit.
470 //
471 // TODO(deadbeef): Applications shouldn't have to arbitrarily limit the
472 // number of available IPv6 network interfaces just because they could slow
473 // ICE down. We should work on making our ICE logic smarter (for example,
474 // prioritizing pinging connections that are most likely to work) so that
475 // every network interface can be used without impacting ICE's speed.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700476 void set_max_ipv6_networks(int networks) {
477 CheckRunOnValidThreadIfInitialized();
478 max_ipv6_networks_ = networks;
479 }
480
481 int max_ipv6_networks() {
482 CheckRunOnValidThreadIfInitialized();
483 return max_ipv6_networks_;
484 }
deadbeef3427f532017-07-26 16:09:33 -0700485
deadbeef1c5e6d02017-09-15 17:46:56 -0700486 // Delay between different candidate gathering phases (UDP, TURN, TCP).
487 // Defaults to 1 second, but PeerConnection sets it to 50ms.
488 // TODO(deadbeef): Get rid of this. Its purpose is to avoid sending too many
489 // STUN transactions at once, but that's already happening if you configure
490 // multiple STUN servers or have multiple network interfaces. We should
491 // implement some global pacing logic instead if that's our goal.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700492 uint32_t step_delay() const {
493 CheckRunOnValidThreadIfInitialized();
494 return step_delay_;
495 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000496
Qingsi Wanga2d60672018-04-11 16:57:45 -0700497 void set_step_delay(uint32_t delay) {
498 CheckRunOnValidThreadIfInitialized();
499 step_delay_ = delay;
500 }
501
502 bool allow_tcp_listen() const {
503 CheckRunOnValidThreadIfInitialized();
504 return allow_tcp_listen_;
505 }
506
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000507 void set_allow_tcp_listen(bool allow_tcp_listen) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700508 CheckRunOnValidThreadIfInitialized();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000509 allow_tcp_listen_ = allow_tcp_listen;
510 }
511
Qingsi Wanga2d60672018-04-11 16:57:45 -0700512 uint32_t candidate_filter() {
513 CheckRunOnValidThreadIfInitialized();
514 return candidate_filter_;
515 }
516
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700517 void set_candidate_filter(uint32_t filter) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700518 CheckRunOnValidThreadIfInitialized();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000519 candidate_filter_ = filter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000520 }
521
Qingsi Wanga2d60672018-04-11 16:57:45 -0700522 bool prune_turn_ports() const {
523 CheckRunOnValidThreadIfInitialized();
524 return prune_turn_ports_;
525 }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700526
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000527 // Gets/Sets the Origin value used for WebRTC STUN requests.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700528 const std::string& origin() const {
529 CheckRunOnValidThreadIfInitialized();
530 return origin_;
531 }
532
533 void set_origin(const std::string& origin) {
534 CheckRunOnValidThreadIfInitialized();
535 origin_ = origin;
536 }
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000537
Jonas Orelandbdcee282017-10-10 14:01:40 +0200538 webrtc::TurnCustomizer* turn_customizer() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700539 CheckRunOnValidThreadIfInitialized();
Jonas Orelandbdcee282017-10-10 14:01:40 +0200540 return turn_customizer_;
541 }
542
Qingsi Wang72a43a12018-02-20 16:03:18 -0800543 // Collect candidate stats from pooled allocator sessions. This can be used to
544 // collect candidate stats without creating an offer/answer or setting local
545 // description. After the local description is set, the ownership of the
546 // pooled session is taken by P2PTransportChannel, and the
547 // candidate stats can be collected from P2PTransportChannel::GetStats.
548 virtual void GetCandidateStatsFromPooledSessions(
549 CandidateStatsList* candidate_stats_list);
550
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000551 protected:
552 virtual PortAllocatorSession* CreateSessionInternal(
553 const std::string& content_name,
554 int component,
555 const std::string& ice_ufrag,
556 const std::string& ice_pwd) = 0;
557
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700558 const std::deque<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() {
559 return pooled_sessions_;
560 }
561
Qingsi Wanga2d60672018-04-11 16:57:45 -0700562 // The following thread checks are only done in DCHECK for the consistency
563 // with the exsiting thread checks.
564 void CheckRunOnValidThreadIfInitialized() const {
565 RTC_DCHECK(!initialized_ || thread_checker_.CalledOnValidThread());
566 }
567
568 void CheckRunOnValidThreadAndInitialized() const {
569 RTC_DCHECK(initialized_ && thread_checker_.CalledOnValidThread());
570 }
571
572 bool initialized_ = false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200573 uint32_t flags_;
deadbeeff137e972017-03-23 15:45:49 -0700574 std::string agent_;
575 rtc::ProxyInfo proxy_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000576 int min_port_;
577 int max_port_;
deadbeef3427f532017-07-26 16:09:33 -0700578 int max_ipv6_networks_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200579 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000580 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200581 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000582 std::string origin_;
Qingsi Wanga2d60672018-04-11 16:57:45 -0700583 rtc::ThreadChecker thread_checker_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700584
585 private:
586 ServerAddresses stun_servers_;
587 std::vector<RelayServerConfig> turn_servers_;
deadbeef6de92f92016-12-12 18:49:32 -0800588 int candidate_pool_size_ = 0; // Last value passed into SetConfiguration.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700589 std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
deadbeef42a42632017-03-10 15:18:00 -0800590 bool candidate_pool_frozen_ = false;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700591 bool prune_turn_ports_ = false;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700592
Jonas Orelandbdcee282017-10-10 14:01:40 +0200593 // Customizer for TURN messages.
594 // The instance is owned by application and will be shared among
595 // all TurnPort(s) created.
596 webrtc::TurnCustomizer* turn_customizer_ = nullptr;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800597
Danil Chapovalov00c71832018-06-15 15:58:38 +0200598 absl::optional<int> stun_candidate_keepalive_interval_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000599};
600
601} // namespace cricket
602
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200603#endif // P2P_BASE_PORTALLOCATOR_H_