blob: 1092910a4938628a27077569f8e81538715ff2b0 [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"
23#include "rtc_base/sigslot.h"
24#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
Honghai Zhangd93f50c2016-10-05 11:47:22 -070026namespace webrtc {
27class MetricsObserverInterface;
Jonas Orelandbdcee282017-10-10 14:01:40 +020028class TurnCustomizer;
Honghai Zhangd93f50c2016-10-05 11:47:22 -070029}
30
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000031namespace cricket {
32
33// PortAllocator is responsible for allocating Port types for a given
34// P2PSocket. It also handles port freeing.
35//
36// Clients can override this class to control port allocation, including
37// what kinds of ports are allocated.
38
39enum {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070040 // Disable local UDP ports. This doesn't impact how we connect to relay
41 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042 PORTALLOCATOR_DISABLE_UDP = 0x01,
43 PORTALLOCATOR_DISABLE_STUN = 0x02,
44 PORTALLOCATOR_DISABLE_RELAY = 0x04,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070045 // Disable local TCP ports. This doesn't impact how we connect to relay
46 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047 PORTALLOCATOR_DISABLE_TCP = 0x08,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
Peter Thatcher7cbd1882015-09-17 18:54:52 -070049 // TODO(pthatcher): Remove this once it's no longer used in:
50 // remoting/client/plugin/pepper_port_allocator.cc
51 // remoting/protocol/chromium_port_allocator.cc
52 // remoting/test/fake_port_allocator.cc
53 // It's a no-op and is no longer needed.
pthatcherfa301802015-08-11 04:12:56 -070054 PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
56 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080057 // When specified, we'll only allocate the STUN candidate for the public
58 // interface as seen by regular http traffic and the HOST candidate associated
59 // with the default local interface.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000060 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080061 // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the
62 // default local candidate mentioned above will not be allocated. Only the
63 // STUN candidate will be.
64 PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070065 // Disallow use of UDP when connecting to a relay server. Since proxy servers
66 // usually don't handle UDP, using UDP will leak the IP address.
67 PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000,
honghaiz60347052016-05-31 18:29:12 -070068
69 // When multiple networks exist, do not gather candidates on the ones with
70 // high cost. So if both Wi-Fi and cellular networks exist, gather only on the
71 // Wi-Fi network. If a network type is "unknown", it has a cost lower than
72 // cellular but higher than Wi-Fi/Ethernet. So if an unknown network exists,
73 // cellular networks will not be used to gather candidates and if a Wi-Fi
74 // network is present, "unknown" networks will not be usd to gather
75 // candidates. Doing so ensures that even if a cellular network type was not
76 // detected initially, it would not be used if a Wi-Fi network is present.
77 PORTALLOCATOR_DISABLE_COSTLY_NETWORKS = 0x2000,
zhihuangb09b3f92017-03-07 14:40:51 -080078
79 // When specified, do not collect IPv6 ICE candidates on Wi-Fi.
80 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI = 0x4000,
deadbeef1ee21252017-06-13 15:49:45 -070081
82 // When this flag is set, ports not bound to any specific network interface
83 // will be used, in addition to normal ports bound to the enumerated
84 // interfaces. Without this flag, these "any address" ports would only be
85 // used when network enumeration fails or is disabled. But under certain
86 // conditions, these ports may succeed where others fail, so they may allow
87 // the application to work in a wider variety of environments, at the expense
88 // of having to allocate additional candidates.
89 PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS = 0x8000,
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
114enum {
115 CF_NONE = 0x0,
116 CF_HOST = 0x1,
117 CF_REFLEXIVE = 0x2,
118 CF_RELAY = 0x4,
119 CF_ALL = 0x7,
120};
121
hnsl04833622017-01-09 08:35:45 -0800122// TLS certificate policy.
123enum class TlsCertPolicy {
124 // For TLS based protocols, ensure the connection is secure by not
125 // circumventing certificate validation.
126 TLS_CERT_POLICY_SECURE,
127 // For TLS based protocols, disregard security completely by skipping
128 // certificate validation. This is insecure and should never be used unless
129 // security is irrelevant in that particular context.
130 TLS_CERT_POLICY_INSECURE_NO_CHECK,
131};
132
deadbeef653b8e02015-11-11 12:55:10 -0800133// TODO(deadbeef): Rename to TurnCredentials (and username to ufrag).
134struct RelayCredentials {
135 RelayCredentials() {}
136 RelayCredentials(const std::string& username, const std::string& password)
137 : username(username), password(password) {}
138
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700139 bool operator==(const RelayCredentials& o) const {
140 return username == o.username && password == o.password;
141 }
142 bool operator!=(const RelayCredentials& o) const { return !(*this == o); }
143
deadbeef653b8e02015-11-11 12:55:10 -0800144 std::string username;
145 std::string password;
146};
147
148typedef std::vector<ProtocolAddress> PortList;
149// TODO(deadbeef): Rename to TurnServerConfig.
150struct RelayServerConfig {
Steve Anton7995d8c2017-10-30 16:23:38 -0700151 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;
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() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000243
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700244 // Another way of getting the information provided by the signals below.
245 //
246 // Ports and candidates are not guaranteed to be in the same order as the
247 // signals were emitted in.
248 virtual std::vector<PortInterface*> ReadyPorts() const = 0;
249 virtual std::vector<Candidate> ReadyCandidates() const = 0;
250 virtual bool CandidatesAllocationDone() const = 0;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700251 // Marks all ports in the current session as "pruned" so that they may be
252 // destroyed if no connection is using them.
253 virtual void PruneAllPorts() {}
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700254
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000255 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700256 // Fires this signal when the network of the ports failed (either because the
257 // interface is down, or because there is no connection on the interface),
258 // or when TURN ports are pruned because a higher-priority TURN port becomes
259 // ready(pairable).
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700260 sigslot::signal2<PortAllocatorSession*, const std::vector<PortInterface*>&>
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700261 SignalPortsPruned;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 sigslot::signal2<PortAllocatorSession*,
263 const std::vector<Candidate>&> SignalCandidatesReady;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700264 // Candidates should be signaled to be removed when the port that generated
265 // the candidates is removed.
266 sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
267 SignalCandidatesRemoved;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000268 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
269
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700270 sigslot::signal2<PortAllocatorSession*, IceRegatheringReason>
271 SignalIceRegathering;
272
Steve Anton7995d8c2017-10-30 16:23:38 -0700273 virtual uint32_t generation();
274 virtual void set_generation(uint32_t generation);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
276
deadbeefc55fb302016-05-12 12:51:38 -0700277 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700278 // This method is called when a pooled session (which doesn't have these
279 // properties initially) is returned by PortAllocator::TakePooledSession,
280 // and the content name, component, and ICE ufrag/pwd are updated.
281 //
282 // A subclass may need to override this method to perform additional actions,
283 // such as applying the updated information to ports and candidates.
284 virtual void UpdateIceParametersInternal() {}
285
deadbeefcbecd352015-09-23 11:50:27 -0700286 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
287 // ice_pwd.
288 const std::string& username() const { return ice_ufrag_; }
289 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000290
deadbeefc55fb302016-05-12 12:51:38 -0700291 private:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700292 void SetIceParameters(const std::string& content_name,
293 int component,
294 const std::string& ice_ufrag,
295 const std::string& ice_pwd) {
296 content_name_ = content_name;
297 component_ = component;
298 ice_ufrag_ = ice_ufrag;
299 ice_pwd_ = ice_pwd;
300 UpdateIceParametersInternal();
301 }
302
deadbeefc55fb302016-05-12 12:51:38 -0700303 uint32_t flags_;
304 uint32_t generation_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700305 std::string content_name_;
306 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700307 std::string ice_ufrag_;
308 std::string ice_pwd_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700309
310 // SetIceParameters is an implementation detail which only PortAllocator
311 // should be able to call.
312 friend class PortAllocator;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000313};
314
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700315// Every method of PortAllocator (including the destructor) must be called on
316// the same thread, except for the constructor which may be called on any
317// thread.
318//
319// This allows constructing a PortAllocator subclass on one thread and
320// passing it into an object that uses it on a different thread.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000321class PortAllocator : public sigslot::has_slots<> {
322 public:
Steve Anton7995d8c2017-10-30 16:23:38 -0700323 PortAllocator();
324 ~PortAllocator() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000325
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700326 // This should be called on the PortAllocator's thread before the
327 // PortAllocator is used. Subclasses may override this if necessary.
328 virtual void Initialize() {}
329
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700330 // Set STUN and TURN servers to be used in future sessions, and set
331 // candidate pool size, as described in JSEP.
332 //
deadbeef42a42632017-03-10 15:18:00 -0800333 // If the servers are changing, and the candidate pool size is nonzero, and
334 // FreezeCandidatePool hasn't been called, existing pooled sessions will be
335 // destroyed and new ones created.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700336 //
deadbeef42a42632017-03-10 15:18:00 -0800337 // If the servers are not changing but the candidate pool size is, and
338 // FreezeCandidatePool hasn't been called, pooled sessions will be either
339 // created or destroyed as necessary.
deadbeef6de92f92016-12-12 18:49:32 -0800340 //
341 // Returns true if the configuration could successfully be changed.
342 bool SetConfiguration(const ServerAddresses& stun_servers,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700343 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700344 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200345 bool prune_turn_ports,
346 webrtc::TurnCustomizer* turn_customizer = nullptr);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700347
348 const ServerAddresses& stun_servers() const { return stun_servers_; }
349
350 const std::vector<RelayServerConfig>& turn_servers() const {
351 return turn_servers_;
352 }
353
deadbeef6de92f92016-12-12 18:49:32 -0800354 int candidate_pool_size() const { return candidate_pool_size_; }
deadbeef653b8e02015-11-11 12:55:10 -0800355
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800356 // Sets the network types to ignore.
357 // Values are defined by the AdapterType enum.
358 // For instance, calling this with
359 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
360 // loopback interfaces.
361 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0;
362
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700363 std::unique_ptr<PortAllocatorSession> CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364 const std::string& content_name,
365 int component,
366 const std::string& ice_ufrag,
367 const std::string& ice_pwd);
368
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700369 // Get an available pooled session and set the transport information on it.
370 //
371 // Caller takes ownership of the returned session.
372 //
373 // If no pooled sessions are available, returns null.
374 std::unique_ptr<PortAllocatorSession> TakePooledSession(
375 const std::string& content_name,
376 int component,
377 const std::string& ice_ufrag,
378 const std::string& ice_pwd);
379
380 // Returns the next session that would be returned by TakePooledSession.
381 const PortAllocatorSession* GetPooledSession() const;
382
deadbeef42a42632017-03-10 15:18:00 -0800383 // After FreezeCandidatePool is called, changing the candidate pool size will
384 // no longer be allowed, and changing ICE servers will not cause pooled
385 // sessions to be recreated.
386 //
387 // Expected to be called when SetLocalDescription is called on a
388 // PeerConnection. Can be called safely on any thread as long as not
389 // simultaneously with SetConfiguration.
390 void FreezeCandidatePool();
391
392 // Discard any remaining pooled sessions.
393 void DiscardCandidatePool();
394
Peter Boström0c4e06b2015-10-07 12:23:21 +0200395 uint32_t flags() const { return flags_; }
396 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000397
deadbeeff137e972017-03-23 15:45:49 -0700398 // These three methods are deprecated. If connections need to go through a
399 // proxy, the application should create a BasicPortAllocator given a custom
400 // PacketSocketFactory that creates proxy sockets.
401 const std::string& user_agent() const { return agent_; }
402 const rtc::ProxyInfo& proxy() const { return proxy_; }
403 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
404 agent_ = agent;
405 proxy_ = proxy;
406 }
407
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000408 // Gets/Sets the port range to use when choosing client ports.
409 int min_port() const { return min_port_; }
410 int max_port() const { return max_port_; }
411 bool SetPortRange(int min_port, int max_port) {
412 if (min_port > max_port) {
413 return false;
414 }
415
416 min_port_ = min_port;
417 max_port_ = max_port;
418 return true;
419 }
420
deadbeef3427f532017-07-26 16:09:33 -0700421 // Can be used to change the default numer of IPv6 network interfaces used
422 // (5). Can set to INT_MAX to effectively disable the limit.
423 //
424 // TODO(deadbeef): Applications shouldn't have to arbitrarily limit the
425 // number of available IPv6 network interfaces just because they could slow
426 // ICE down. We should work on making our ICE logic smarter (for example,
427 // prioritizing pinging connections that are most likely to work) so that
428 // every network interface can be used without impacting ICE's speed.
429 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; }
430 int max_ipv6_networks() { return max_ipv6_networks_; }
431
deadbeef1c5e6d02017-09-15 17:46:56 -0700432 // Delay between different candidate gathering phases (UDP, TURN, TCP).
433 // Defaults to 1 second, but PeerConnection sets it to 50ms.
434 // TODO(deadbeef): Get rid of this. Its purpose is to avoid sending too many
435 // STUN transactions at once, but that's already happening if you configure
436 // multiple STUN servers or have multiple network interfaces. We should
437 // implement some global pacing logic instead if that's our goal.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200438 uint32_t step_delay() const { return step_delay_; }
439 void set_step_delay(uint32_t delay) { step_delay_ = delay; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000440
441 bool allow_tcp_listen() const { return allow_tcp_listen_; }
442 void set_allow_tcp_listen(bool allow_tcp_listen) {
443 allow_tcp_listen_ = allow_tcp_listen;
444 }
445
Peter Boström0c4e06b2015-10-07 12:23:21 +0200446 uint32_t candidate_filter() { return candidate_filter_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700447 void set_candidate_filter(uint32_t filter) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000448 candidate_filter_ = filter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000449 }
450
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700451 bool prune_turn_ports() const { return prune_turn_ports_; }
452
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000453 // Gets/Sets the Origin value used for WebRTC STUN requests.
454 const std::string& origin() const { return origin_; }
455 void set_origin(const std::string& origin) { origin_ = origin; }
456
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700457 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) {
458 metrics_observer_ = observer;
459 }
460
Jonas Orelandbdcee282017-10-10 14:01:40 +0200461 webrtc::TurnCustomizer* turn_customizer() {
462 return turn_customizer_;
463 }
464
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000465 protected:
466 virtual PortAllocatorSession* CreateSessionInternal(
467 const std::string& content_name,
468 int component,
469 const std::string& ice_ufrag,
470 const std::string& ice_pwd) = 0;
471
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700472 webrtc::MetricsObserverInterface* metrics_observer() {
473 return metrics_observer_;
474 }
475
476 const std::deque<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() {
477 return pooled_sessions_;
478 }
479
Peter Boström0c4e06b2015-10-07 12:23:21 +0200480 uint32_t flags_;
deadbeeff137e972017-03-23 15:45:49 -0700481 std::string agent_;
482 rtc::ProxyInfo proxy_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000483 int min_port_;
484 int max_port_;
deadbeef3427f532017-07-26 16:09:33 -0700485 int max_ipv6_networks_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200486 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000487 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200488 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000489 std::string origin_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700490
491 private:
492 ServerAddresses stun_servers_;
493 std::vector<RelayServerConfig> turn_servers_;
deadbeef6de92f92016-12-12 18:49:32 -0800494 int candidate_pool_size_ = 0; // Last value passed into SetConfiguration.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700495 std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
deadbeef42a42632017-03-10 15:18:00 -0800496 bool candidate_pool_frozen_ = false;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700497 bool prune_turn_ports_ = false;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700498
499 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
Jonas Orelandbdcee282017-10-10 14:01:40 +0200500
501 // Customizer for TURN messages.
502 // The instance is owned by application and will be shared among
503 // all TurnPort(s) created.
504 webrtc::TurnCustomizer* turn_customizer_ = nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000505};
506
507} // namespace cricket
508
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200509#endif // P2P_BASE_PORTALLOCATOR_H_