blob: 356b1f175894dfaf2506d02c32e01eccca18b836 [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"
Diogo Real4f085432018-09-11 16:00:22 -070023#include "rtc_base/ssladapter.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070024#include "rtc_base/sslcertificate.h"
Artem Titove41c4332018-07-25 15:04:28 +020025#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/thread.h"
Qingsi Wanga2d60672018-04-11 16:57:45 -070027#include "rtc_base/thread_checker.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028
Honghai Zhangd93f50c2016-10-05 11:47:22 -070029namespace webrtc {
Jonas Orelandbdcee282017-10-10 14:01:40 +020030class TurnCustomizer;
Yves Gerey665174f2018-06-19 15:03:05 +020031} // namespace webrtc
Honghai Zhangd93f50c2016-10-05 11:47:22 -070032
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000033namespace cricket {
34
35// PortAllocator is responsible for allocating Port types for a given
36// P2PSocket. It also handles port freeing.
37//
38// Clients can override this class to control port allocation, including
39// what kinds of ports are allocated.
40
41enum {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070042 // Disable local UDP ports. This doesn't impact how we connect to relay
43 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044 PORTALLOCATOR_DISABLE_UDP = 0x01,
45 PORTALLOCATOR_DISABLE_STUN = 0x02,
46 PORTALLOCATOR_DISABLE_RELAY = 0x04,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070047 // Disable local TCP ports. This doesn't impact how we connect to relay
48 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049 PORTALLOCATOR_DISABLE_TCP = 0x08,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
52 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080053 // When specified, we'll only allocate the STUN candidate for the public
54 // interface as seen by regular http traffic and the HOST candidate associated
55 // with the default local interface.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000056 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080057 // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the
58 // default local candidate mentioned above will not be allocated. Only the
59 // STUN candidate will be.
60 PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070061 // Disallow use of UDP when connecting to a relay server. Since proxy servers
62 // usually don't handle UDP, using UDP will leak the IP address.
63 PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000,
honghaiz60347052016-05-31 18:29:12 -070064
65 // When multiple networks exist, do not gather candidates on the ones with
66 // high cost. So if both Wi-Fi and cellular networks exist, gather only on the
67 // Wi-Fi network. If a network type is "unknown", it has a cost lower than
68 // cellular but higher than Wi-Fi/Ethernet. So if an unknown network exists,
69 // cellular networks will not be used to gather candidates and if a Wi-Fi
70 // network is present, "unknown" networks will not be usd to gather
71 // candidates. Doing so ensures that even if a cellular network type was not
72 // detected initially, it would not be used if a Wi-Fi network is present.
73 PORTALLOCATOR_DISABLE_COSTLY_NETWORKS = 0x2000,
zhihuangb09b3f92017-03-07 14:40:51 -080074
75 // When specified, do not collect IPv6 ICE candidates on Wi-Fi.
76 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI = 0x4000,
deadbeef1ee21252017-06-13 15:49:45 -070077
Mirko Bonadei5f4d47b2018-08-22 17:41:22 +000078 // When this flag is set, ports not bound to any specific network interface
79 // will be used, in addition to normal ports bound to the enumerated
80 // interfaces. Without this flag, these "any address" ports would only be
81 // used when network enumeration fails or is disabled. But under certain
82 // conditions, these ports may succeed where others fail, so they may allow
83 // the application to work in a wider variety of environments, at the expense
84 // of having to allocate additional candidates.
Qingsi Wangefbcb312018-08-21 23:23:26 +000085 PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS = 0x8000,
86
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +010087 // Exclude link-local network interfaces
88 // from considertaion after adapter enumeration.
Qingsi Wangefbcb312018-08-21 23:23:26 +000089 PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS = 0x10000,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090};
91
Honghai Zhangd93f50c2016-10-05 11:47:22 -070092// Defines various reasons that have caused ICE regathering.
Steve Anton300bf8e2017-07-14 10:13:10 -070093enum class IceRegatheringReason {
94 NETWORK_CHANGE, // Network interfaces on the device changed
95 NETWORK_FAILURE, // Regather only on networks that have failed
96 OCCASIONAL_REFRESH, // Periodic regather on all networks
97 MAX_VALUE
98};
Honghai Zhangd93f50c2016-10-05 11:47:22 -070099
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100const uint32_t kDefaultPortAllocatorFlags = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
104// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105const uint32_t kMinimumStepDelay = 50;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106
deadbeef3427f532017-07-26 16:09:33 -0700107// Turning on IPv6 could make many IPv6 interfaces available for connectivity
108// check and delay the call setup time. kDefaultMaxIPv6Networks is the default
109// upper limit of IPv6 networks but could be changed by
110// set_max_ipv6_networks().
111constexpr int kDefaultMaxIPv6Networks = 5;
112
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113// CF = CANDIDATE FILTER
Oleh Prypinfd7df982017-12-21 16:25:19 +0100114enum : uint32_t {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 CF_NONE = 0x0,
116 CF_HOST = 0x1,
117 CF_REFLEXIVE = 0x2,
118 CF_RELAY = 0x4,
119 CF_ALL = 0x7,
120};
121
deadbeef653b8e02015-11-11 12:55:10 -0800122// TODO(deadbeef): Rename to TurnCredentials (and username to ufrag).
123struct RelayCredentials {
124 RelayCredentials() {}
125 RelayCredentials(const std::string& username, const std::string& password)
126 : username(username), password(password) {}
127
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700128 bool operator==(const RelayCredentials& o) const {
129 return username == o.username && password == o.password;
130 }
131 bool operator!=(const RelayCredentials& o) const { return !(*this == o); }
132
deadbeef653b8e02015-11-11 12:55:10 -0800133 std::string username;
134 std::string password;
135};
136
Diogo Real4f085432018-09-11 16:00:22 -0700137// TLS certificate policy.
138enum class TlsCertPolicy {
139 // For TLS based protocols, ensure the connection is secure by not
140 // circumventing certificate validation.
141 TLS_CERT_POLICY_SECURE,
142 // For TLS based protocols, disregard security completely by skipping
143 // certificate validation. This is insecure and should never be used unless
144 // security is irrelevant in that particular context.
145 TLS_CERT_POLICY_INSECURE_NO_CHECK,
146};
147
deadbeef653b8e02015-11-11 12:55:10 -0800148typedef std::vector<ProtocolAddress> PortList;
149// TODO(deadbeef): Rename to TurnServerConfig.
150struct RelayServerConfig {
Steve Anton6c38cc72017-11-29 10:25:58 -0800151 explicit RelayServerConfig(RelayType type);
Emad Omaradab1d2d2017-06-16 15:43:11 -0700152 RelayServerConfig(const rtc::SocketAddress& address,
153 const std::string& username,
154 const std::string& password,
Steve Anton7995d8c2017-10-30 16:23:38 -0700155 ProtocolType proto);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800156 RelayServerConfig(const std::string& address,
157 int port,
158 const std::string& username,
159 const std::string& password,
Steve Anton7995d8c2017-10-30 16:23:38 -0700160 ProtocolType proto);
hnsl277b2502016-12-13 05:17:23 -0800161 // Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS.
162 RelayServerConfig(const std::string& address,
163 int port,
164 const std::string& username,
165 const std::string& password,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800166 ProtocolType proto,
Steve Anton7995d8c2017-10-30 16:23:38 -0700167 bool secure);
168 RelayServerConfig(const RelayServerConfig&);
169 ~RelayServerConfig();
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800170
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700171 bool operator==(const RelayServerConfig& o) const {
172 return type == o.type && ports == o.ports && credentials == o.credentials &&
173 priority == o.priority;
174 }
175 bool operator!=(const RelayServerConfig& o) const { return !(*this == o); }
176
deadbeef653b8e02015-11-11 12:55:10 -0800177 RelayType type;
178 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;
Diogo Real4f085432018-09-11 16:00:22 -0700184 rtc::SSLConfig ssl_config;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700185 rtc::SSLCertificateVerifier* tls_cert_verifier = nullptr;
deadbeef653b8e02015-11-11 12:55:10 -0800186};
187
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000188class PortAllocatorSession : public sigslot::has_slots<> {
189 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_; }
206 bool pooled() const { return ice_ufrag_.empty(); }
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(
248 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;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700275 // Candidates should be signaled to be removed when the port that generated
276 // the candidates is removed.
277 sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
278 SignalCandidatesRemoved;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000279 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
280
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700281 sigslot::signal2<PortAllocatorSession*, IceRegatheringReason>
282 SignalIceRegathering;
283
Steve Anton7995d8c2017-10-30 16:23:38 -0700284 virtual uint32_t generation();
285 virtual void set_generation(uint32_t generation);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000286 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
287
deadbeefc55fb302016-05-12 12:51:38 -0700288 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700289 // This method is called when a pooled session (which doesn't have these
290 // properties initially) is returned by PortAllocator::TakePooledSession,
291 // and the content name, component, and ICE ufrag/pwd are updated.
292 //
293 // A subclass may need to override this method to perform additional actions,
294 // such as applying the updated information to ports and candidates.
295 virtual void UpdateIceParametersInternal() {}
296
deadbeefcbecd352015-09-23 11:50:27 -0700297 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
298 // ice_pwd.
299 const std::string& username() const { return ice_ufrag_; }
300 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000301
deadbeefc55fb302016-05-12 12:51:38 -0700302 private:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700303 void SetIceParameters(const std::string& content_name,
304 int component,
305 const std::string& ice_ufrag,
306 const std::string& ice_pwd) {
307 content_name_ = content_name;
308 component_ = component;
309 ice_ufrag_ = ice_ufrag;
310 ice_pwd_ = ice_pwd;
311 UpdateIceParametersInternal();
312 }
313
deadbeefc55fb302016-05-12 12:51:38 -0700314 uint32_t flags_;
315 uint32_t generation_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700316 std::string content_name_;
317 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700318 std::string ice_ufrag_;
319 std::string ice_pwd_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700320
321 // SetIceParameters is an implementation detail which only PortAllocator
322 // should be able to call.
323 friend class PortAllocator;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000324};
325
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700326// Every method of PortAllocator (including the destructor) must be called on
Qingsi Wanga2d60672018-04-11 16:57:45 -0700327// the same thread after Initialize is called.
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700328//
Qingsi Wanga2d60672018-04-11 16:57:45 -0700329// This allows a PortAllocator subclass to be constructed and configured on one
330// thread, and passed into an object that uses it on a different thread.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000331class PortAllocator : public sigslot::has_slots<> {
332 public:
Steve Anton7995d8c2017-10-30 16:23:38 -0700333 PortAllocator();
334 ~PortAllocator() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000335
Qingsi Wanga2d60672018-04-11 16:57:45 -0700336 // This MUST be called on the PortAllocator's thread after finishing
337 // constructing and configuring the PortAllocator subclasses.
338 virtual void Initialize();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700339
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700340 // Set STUN and TURN servers to be used in future sessions, and set
341 // candidate pool size, as described in JSEP.
342 //
deadbeef42a42632017-03-10 15:18:00 -0800343 // If the servers are changing, and the candidate pool size is nonzero, and
344 // FreezeCandidatePool hasn't been called, existing pooled sessions will be
345 // destroyed and new ones created.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700346 //
deadbeef42a42632017-03-10 15:18:00 -0800347 // If the servers are not changing but the candidate pool size is, and
348 // FreezeCandidatePool hasn't been called, pooled sessions will be either
349 // created or destroyed as necessary.
deadbeef6de92f92016-12-12 18:49:32 -0800350 //
351 // Returns true if the configuration could successfully be changed.
352 bool SetConfiguration(const ServerAddresses& stun_servers,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700353 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700354 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200355 bool prune_turn_ports,
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800356 webrtc::TurnCustomizer* turn_customizer = nullptr,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200357 const absl::optional<int>&
358 stun_candidate_keepalive_interval = absl::nullopt);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700359
Qingsi Wanga2d60672018-04-11 16:57:45 -0700360 const ServerAddresses& stun_servers() const {
361 CheckRunOnValidThreadIfInitialized();
362 return stun_servers_;
363 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700364
365 const std::vector<RelayServerConfig>& turn_servers() const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700366 CheckRunOnValidThreadIfInitialized();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700367 return turn_servers_;
368 }
369
Qingsi Wanga2d60672018-04-11 16:57:45 -0700370 int candidate_pool_size() const {
371 CheckRunOnValidThreadIfInitialized();
372 return candidate_pool_size_;
373 }
374
Danil Chapovalov00c71832018-06-15 15:58:38 +0200375 const absl::optional<int>& stun_candidate_keepalive_interval() const {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700376 CheckRunOnValidThreadIfInitialized();
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800377 return stun_candidate_keepalive_interval_;
378 }
deadbeef653b8e02015-11-11 12:55:10 -0800379
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800380 // Sets the network types to ignore.
381 // Values are defined by the AdapterType enum.
382 // For instance, calling this with
383 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
384 // loopback interfaces.
385 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0;
386
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700387 std::unique_ptr<PortAllocatorSession> CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 const std::string& content_name,
389 int component,
390 const std::string& ice_ufrag,
391 const std::string& ice_pwd);
392
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700393 // Get an available pooled session and set the transport information on it.
394 //
395 // Caller takes ownership of the returned session.
396 //
397 // If no pooled sessions are available, returns null.
398 std::unique_ptr<PortAllocatorSession> TakePooledSession(
399 const std::string& content_name,
400 int component,
401 const std::string& ice_ufrag,
402 const std::string& ice_pwd);
403
404 // Returns the next session that would be returned by TakePooledSession.
405 const PortAllocatorSession* GetPooledSession() const;
406
deadbeef42a42632017-03-10 15:18:00 -0800407 // After FreezeCandidatePool is called, changing the candidate pool size will
408 // no longer be allowed, and changing ICE servers will not cause pooled
409 // sessions to be recreated.
410 //
411 // Expected to be called when SetLocalDescription is called on a
412 // PeerConnection. Can be called safely on any thread as long as not
413 // simultaneously with SetConfiguration.
414 void FreezeCandidatePool();
415
416 // Discard any remaining pooled sessions.
417 void DiscardCandidatePool();
418
Qingsi Wanga2d60672018-04-11 16:57:45 -0700419 uint32_t flags() const {
420 CheckRunOnValidThreadIfInitialized();
421 return flags_;
422 }
423
424 void set_flags(uint32_t flags) {
425 CheckRunOnValidThreadIfInitialized();
426 flags_ = flags;
427 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000428
deadbeeff137e972017-03-23 15:45:49 -0700429 // These three methods are deprecated. If connections need to go through a
430 // proxy, the application should create a BasicPortAllocator given a custom
431 // PacketSocketFactory that creates proxy sockets.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700432 const std::string& user_agent() const {
433 CheckRunOnValidThreadIfInitialized();
434 return agent_;
435 }
436
437 const rtc::ProxyInfo& proxy() const {
438 CheckRunOnValidThreadIfInitialized();
439 return proxy_;
440 }
441
deadbeeff137e972017-03-23 15:45:49 -0700442 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700443 CheckRunOnValidThreadIfInitialized();
deadbeeff137e972017-03-23 15:45:49 -0700444 agent_ = agent;
445 proxy_ = proxy;
446 }
447
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000448 // Gets/Sets the port range to use when choosing client ports.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700449 int min_port() const {
450 CheckRunOnValidThreadIfInitialized();
451 return min_port_;
452 }
453
454 int max_port() const {
455 CheckRunOnValidThreadIfInitialized();
456 return max_port_;
457 }
458
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000459 bool SetPortRange(int min_port, int max_port) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700460 CheckRunOnValidThreadIfInitialized();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000461 if (min_port > max_port) {
462 return false;
463 }
464
465 min_port_ = min_port;
466 max_port_ = max_port;
467 return true;
468 }
469
deadbeef3427f532017-07-26 16:09:33 -0700470 // Can be used to change the default numer of IPv6 network interfaces used
471 // (5). Can set to INT_MAX to effectively disable the limit.
472 //
473 // TODO(deadbeef): Applications shouldn't have to arbitrarily limit the
474 // number of available IPv6 network interfaces just because they could slow
475 // ICE down. We should work on making our ICE logic smarter (for example,
476 // prioritizing pinging connections that are most likely to work) so that
477 // every network interface can be used without impacting ICE's speed.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700478 void set_max_ipv6_networks(int networks) {
479 CheckRunOnValidThreadIfInitialized();
480 max_ipv6_networks_ = networks;
481 }
482
483 int max_ipv6_networks() {
484 CheckRunOnValidThreadIfInitialized();
485 return max_ipv6_networks_;
486 }
deadbeef3427f532017-07-26 16:09:33 -0700487
deadbeef1c5e6d02017-09-15 17:46:56 -0700488 // Delay between different candidate gathering phases (UDP, TURN, TCP).
489 // Defaults to 1 second, but PeerConnection sets it to 50ms.
490 // TODO(deadbeef): Get rid of this. Its purpose is to avoid sending too many
491 // STUN transactions at once, but that's already happening if you configure
492 // multiple STUN servers or have multiple network interfaces. We should
493 // implement some global pacing logic instead if that's our goal.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700494 uint32_t step_delay() const {
495 CheckRunOnValidThreadIfInitialized();
496 return step_delay_;
497 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000498
Qingsi Wanga2d60672018-04-11 16:57:45 -0700499 void set_step_delay(uint32_t delay) {
500 CheckRunOnValidThreadIfInitialized();
501 step_delay_ = delay;
502 }
503
504 bool allow_tcp_listen() const {
505 CheckRunOnValidThreadIfInitialized();
506 return allow_tcp_listen_;
507 }
508
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000509 void set_allow_tcp_listen(bool allow_tcp_listen) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700510 CheckRunOnValidThreadIfInitialized();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000511 allow_tcp_listen_ = allow_tcp_listen;
512 }
513
Qingsi Wanga2d60672018-04-11 16:57:45 -0700514 uint32_t candidate_filter() {
515 CheckRunOnValidThreadIfInitialized();
516 return candidate_filter_;
517 }
518
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700519 void set_candidate_filter(uint32_t filter) {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700520 CheckRunOnValidThreadIfInitialized();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000521 candidate_filter_ = filter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000522 }
523
Qingsi Wanga2d60672018-04-11 16:57:45 -0700524 bool prune_turn_ports() const {
525 CheckRunOnValidThreadIfInitialized();
526 return prune_turn_ports_;
527 }
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700528
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000529 // Gets/Sets the Origin value used for WebRTC STUN requests.
Qingsi Wanga2d60672018-04-11 16:57:45 -0700530 const std::string& origin() const {
531 CheckRunOnValidThreadIfInitialized();
532 return origin_;
533 }
534
535 void set_origin(const std::string& origin) {
536 CheckRunOnValidThreadIfInitialized();
537 origin_ = origin;
538 }
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000539
Jonas Orelandbdcee282017-10-10 14:01:40 +0200540 webrtc::TurnCustomizer* turn_customizer() {
Qingsi Wanga2d60672018-04-11 16:57:45 -0700541 CheckRunOnValidThreadIfInitialized();
Jonas Orelandbdcee282017-10-10 14:01:40 +0200542 return turn_customizer_;
543 }
544
Qingsi Wang72a43a12018-02-20 16:03:18 -0800545 // Collect candidate stats from pooled allocator sessions. This can be used to
546 // collect candidate stats without creating an offer/answer or setting local
547 // description. After the local description is set, the ownership of the
548 // pooled session is taken by P2PTransportChannel, and the
549 // candidate stats can be collected from P2PTransportChannel::GetStats.
550 virtual void GetCandidateStatsFromPooledSessions(
551 CandidateStatsList* candidate_stats_list);
552
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000553 protected:
554 virtual PortAllocatorSession* CreateSessionInternal(
555 const std::string& content_name,
556 int component,
557 const std::string& ice_ufrag,
558 const std::string& ice_pwd) = 0;
559
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700560 const std::deque<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() {
561 return pooled_sessions_;
562 }
563
Qingsi Wanga2d60672018-04-11 16:57:45 -0700564 // The following thread checks are only done in DCHECK for the consistency
565 // with the exsiting thread checks.
566 void CheckRunOnValidThreadIfInitialized() const {
567 RTC_DCHECK(!initialized_ || thread_checker_.CalledOnValidThread());
568 }
569
570 void CheckRunOnValidThreadAndInitialized() const {
571 RTC_DCHECK(initialized_ && thread_checker_.CalledOnValidThread());
572 }
573
574 bool initialized_ = false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200575 uint32_t flags_;
deadbeeff137e972017-03-23 15:45:49 -0700576 std::string agent_;
577 rtc::ProxyInfo proxy_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000578 int min_port_;
579 int max_port_;
deadbeef3427f532017-07-26 16:09:33 -0700580 int max_ipv6_networks_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200581 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000582 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200583 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000584 std::string origin_;
Qingsi Wanga2d60672018-04-11 16:57:45 -0700585 rtc::ThreadChecker thread_checker_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700586
587 private:
588 ServerAddresses stun_servers_;
589 std::vector<RelayServerConfig> turn_servers_;
deadbeef6de92f92016-12-12 18:49:32 -0800590 int candidate_pool_size_ = 0; // Last value passed into SetConfiguration.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700591 std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
deadbeef42a42632017-03-10 15:18:00 -0800592 bool candidate_pool_frozen_ = false;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700593 bool prune_turn_ports_ = false;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700594
Jonas Orelandbdcee282017-10-10 14:01:40 +0200595 // Customizer for TURN messages.
596 // The instance is owned by application and will be shared among
597 // all TurnPort(s) created.
598 webrtc::TurnCustomizer* turn_customizer_ = nullptr;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800599
Danil Chapovalov00c71832018-06-15 15:58:38 +0200600 absl::optional<int> stun_candidate_keepalive_interval_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000601};
602
603} // namespace cricket
604
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200605#endif // P2P_BASE_PORTALLOCATOR_H_