blob: 45a941a9b748e7a77b3c600d0f70925abcf923a3 [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
11#ifndef WEBRTC_P2P_BASE_PORTALLOCATOR_H_
12#define WEBRTC_P2P_BASE_PORTALLOCATOR_H_
13
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
deadbeef653b8e02015-11-11 12:55:10 -080019#include "webrtc/p2p/base/port.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020#include "webrtc/p2p/base/portinterface.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020021#include "webrtc/rtc_base/helpers.h"
22#include "webrtc/rtc_base/proxyinfo.h"
23#include "webrtc/rtc_base/sigslot.h"
24#include "webrtc/rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
Honghai Zhangd93f50c2016-10-05 11:47:22 -070026namespace webrtc {
27class MetricsObserverInterface;
28}
29
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030namespace cricket {
31
32// PortAllocator is responsible for allocating Port types for a given
33// P2PSocket. It also handles port freeing.
34//
35// Clients can override this class to control port allocation, including
36// what kinds of ports are allocated.
37
38enum {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070039 // Disable local UDP ports. This doesn't impact how we connect to relay
40 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000041 PORTALLOCATOR_DISABLE_UDP = 0x01,
42 PORTALLOCATOR_DISABLE_STUN = 0x02,
43 PORTALLOCATOR_DISABLE_RELAY = 0x04,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070044 // Disable local TCP ports. This doesn't impact how we connect to relay
45 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046 PORTALLOCATOR_DISABLE_TCP = 0x08,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
Peter Thatcher7cbd1882015-09-17 18:54:52 -070048 // TODO(pthatcher): Remove this once it's no longer used in:
49 // remoting/client/plugin/pepper_port_allocator.cc
50 // remoting/protocol/chromium_port_allocator.cc
51 // remoting/test/fake_port_allocator.cc
52 // It's a no-op and is no longer needed.
pthatcherfa301802015-08-11 04:12:56 -070053 PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
55 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080056 // When specified, we'll only allocate the STUN candidate for the public
57 // interface as seen by regular http traffic and the HOST candidate associated
58 // with the default local interface.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000059 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080060 // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the
61 // default local candidate mentioned above will not be allocated. Only the
62 // STUN candidate will be.
63 PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070064 // Disallow use of UDP when connecting to a relay server. Since proxy servers
65 // usually don't handle UDP, using UDP will leak the IP address.
66 PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000,
honghaiz60347052016-05-31 18:29:12 -070067
68 // When multiple networks exist, do not gather candidates on the ones with
69 // high cost. So if both Wi-Fi and cellular networks exist, gather only on the
70 // Wi-Fi network. If a network type is "unknown", it has a cost lower than
71 // cellular but higher than Wi-Fi/Ethernet. So if an unknown network exists,
72 // cellular networks will not be used to gather candidates and if a Wi-Fi
73 // network is present, "unknown" networks will not be usd to gather
74 // candidates. Doing so ensures that even if a cellular network type was not
75 // detected initially, it would not be used if a Wi-Fi network is present.
76 PORTALLOCATOR_DISABLE_COSTLY_NETWORKS = 0x2000,
zhihuangb09b3f92017-03-07 14:40:51 -080077
78 // When specified, do not collect IPv6 ICE candidates on Wi-Fi.
79 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI = 0x4000,
deadbeef1ee21252017-06-13 15:49:45 -070080
81 // When this flag is set, ports not bound to any specific network interface
82 // will be used, in addition to normal ports bound to the enumerated
83 // interfaces. Without this flag, these "any address" ports would only be
84 // used when network enumeration fails or is disabled. But under certain
85 // conditions, these ports may succeed where others fail, so they may allow
86 // the application to work in a wider variety of environments, at the expense
87 // of having to allocate additional candidates.
88 PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS = 0x8000,
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
113enum {
114 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 {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700150 RelayServerConfig(RelayType type) : type(type) {}
deadbeef653b8e02015-11-11 12:55:10 -0800151
Emad Omaradab1d2d2017-06-16 15:43:11 -0700152 RelayServerConfig(const rtc::SocketAddress& address,
153 const std::string& username,
154 const std::string& password,
155 ProtocolType proto)
156 : type(RELAY_TURN), credentials(username, password) {
157 ports.push_back(ProtocolAddress(address, proto));
158 }
159
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800160 RelayServerConfig(const std::string& address,
161 int port,
162 const std::string& username,
163 const std::string& password,
hnsl277b2502016-12-13 05:17:23 -0800164 ProtocolType proto)
Emad Omaradab1d2d2017-06-16 15:43:11 -0700165 : RelayServerConfig(rtc::SocketAddress(address, port),
166 username,
167 password,
168 proto) {}
hnsl277b2502016-12-13 05:17:23 -0800169
170 // Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS.
171 RelayServerConfig(const std::string& address,
172 int port,
173 const std::string& username,
174 const std::string& password,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800175 ProtocolType proto,
176 bool secure)
hnsl277b2502016-12-13 05:17:23 -0800177 : RelayServerConfig(address,
178 port,
179 username,
180 password,
181 (proto == PROTO_TCP && secure ? PROTO_TLS : proto)) {}
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800182
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700183 bool operator==(const RelayServerConfig& o) const {
184 return type == o.type && ports == o.ports && credentials == o.credentials &&
185 priority == o.priority;
186 }
187 bool operator!=(const RelayServerConfig& o) const { return !(*this == o); }
188
deadbeef653b8e02015-11-11 12:55:10 -0800189 RelayType type;
190 PortList ports;
191 RelayCredentials credentials;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700192 int priority = 0;
hnsl04833622017-01-09 08:35:45 -0800193 TlsCertPolicy tls_cert_policy = TlsCertPolicy::TLS_CERT_POLICY_SECURE;
deadbeef653b8e02015-11-11 12:55:10 -0800194};
195
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196class PortAllocatorSession : public sigslot::has_slots<> {
197 public:
198 // Content name passed in mostly for logging and debugging.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199 PortAllocatorSession(const std::string& content_name,
200 int component,
deadbeefcbecd352015-09-23 11:50:27 -0700201 const std::string& ice_ufrag,
202 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200203 uint32_t flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204
205 // Subclasses should clean up any ports created.
206 virtual ~PortAllocatorSession() {}
207
Peter Boström0c4e06b2015-10-07 12:23:21 +0200208 uint32_t flags() const { return flags_; }
209 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000210 std::string content_name() const { return content_name_; }
211 int component() const { return component_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700212 const std::string& ice_ufrag() const { return ice_ufrag_; }
213 const std::string& ice_pwd() const { return ice_pwd_; }
214 bool pooled() const { return ice_ufrag_.empty(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700216 // Setting this filter should affect not only candidates gathered in the
217 // future, but candidates already gathered and ports already "ready",
218 // which would be returned by ReadyCandidates() and ReadyPorts().
219 //
220 // Default filter should be CF_ALL.
221 virtual void SetCandidateFilter(uint32_t filter) = 0;
222
deadbeefb60a8192016-08-24 15:15:00 -0700223 // Starts gathering ports and ICE candidates.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700224 virtual void StartGettingPorts() = 0;
deadbeefb60a8192016-08-24 15:15:00 -0700225 // Completely stops gathering. Will not gather again unless StartGettingPorts
226 // is called again.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700227 virtual void StopGettingPorts() = 0;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700228 // Whether the session is actively getting ports.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700229 virtual bool IsGettingPorts() = 0;
deadbeefb60a8192016-08-24 15:15:00 -0700230
231 //
232 // NOTE: The group of methods below is only used for continual gathering.
233 //
234
235 // ClearGettingPorts should have the same immediate effect as
236 // StopGettingPorts, but if the implementation supports continual gathering,
237 // ClearGettingPorts allows additional ports/candidates to be gathered if the
238 // network conditions change.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700239 virtual void ClearGettingPorts() = 0;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700240 // Whether it is in the state where the existing gathering process is stopped,
241 // but new ones may be started (basically after calling ClearGettingPorts).
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700242 virtual bool IsCleared() const { return false; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700243 // Whether the session has completely stopped.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700244 virtual bool IsStopped() const { return false; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700245 // Re-gathers candidates on networks that do not have any connections. More
246 // precisely, a network interface may have more than one IP addresses (e.g.,
247 // IPv4 and IPv6 addresses). Each address subnet will be used to create a
248 // network. Only if all networks of an interface have no connection, the
249 // implementation should start re-gathering on all networks of that interface.
250 virtual void RegatherOnFailedNetworks() {}
251 // Re-gathers candidates on all networks.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700252 virtual void RegatherOnAllNetworks() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700254 // Another way of getting the information provided by the signals below.
255 //
256 // Ports and candidates are not guaranteed to be in the same order as the
257 // signals were emitted in.
258 virtual std::vector<PortInterface*> ReadyPorts() const = 0;
259 virtual std::vector<Candidate> ReadyCandidates() const = 0;
260 virtual bool CandidatesAllocationDone() const = 0;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700261 // Marks all ports in the current session as "pruned" so that they may be
262 // destroyed if no connection is using them.
263 virtual void PruneAllPorts() {}
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700264
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000265 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700266 // Fires this signal when the network of the ports failed (either because the
267 // interface is down, or because there is no connection on the interface),
268 // or when TURN ports are pruned because a higher-priority TURN port becomes
269 // ready(pairable).
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700270 sigslot::signal2<PortAllocatorSession*, const std::vector<PortInterface*>&>
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700271 SignalPortsPruned;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000272 sigslot::signal2<PortAllocatorSession*,
273 const std::vector<Candidate>&> SignalCandidatesReady;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700274 // Candidates should be signaled to be removed when the port that generated
275 // the candidates is removed.
276 sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
277 SignalCandidatesRemoved;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000278 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
279
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700280 sigslot::signal2<PortAllocatorSession*, IceRegatheringReason>
281 SignalIceRegathering;
282
Peter Boström0c4e06b2015-10-07 12:23:21 +0200283 virtual uint32_t generation() { return generation_; }
284 virtual void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000285 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
286
deadbeefc55fb302016-05-12 12:51:38 -0700287 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700288 // This method is called when a pooled session (which doesn't have these
289 // properties initially) is returned by PortAllocator::TakePooledSession,
290 // and the content name, component, and ICE ufrag/pwd are updated.
291 //
292 // A subclass may need to override this method to perform additional actions,
293 // such as applying the updated information to ports and candidates.
294 virtual void UpdateIceParametersInternal() {}
295
deadbeefcbecd352015-09-23 11:50:27 -0700296 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
297 // ice_pwd.
298 const std::string& username() const { return ice_ufrag_; }
299 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000300
deadbeefc55fb302016-05-12 12:51:38 -0700301 private:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700302 void SetIceParameters(const std::string& content_name,
303 int component,
304 const std::string& ice_ufrag,
305 const std::string& ice_pwd) {
306 content_name_ = content_name;
307 component_ = component;
308 ice_ufrag_ = ice_ufrag;
309 ice_pwd_ = ice_pwd;
310 UpdateIceParametersInternal();
311 }
312
deadbeefc55fb302016-05-12 12:51:38 -0700313 uint32_t flags_;
314 uint32_t generation_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700315 std::string content_name_;
316 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700317 std::string ice_ufrag_;
318 std::string ice_pwd_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700319
320 // SetIceParameters is an implementation detail which only PortAllocator
321 // should be able to call.
322 friend class PortAllocator;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000323};
324
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700325// Every method of PortAllocator (including the destructor) must be called on
326// the same thread, except for the constructor which may be called on any
327// thread.
328//
329// This allows constructing a PortAllocator subclass on one thread and
330// passing it 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:
deadbeef3427f532017-07-26 16:09:33 -0700333 PortAllocator()
334 : flags_(kDefaultPortAllocatorFlags),
335 min_port_(0),
336 max_port_(0),
337 max_ipv6_networks_(kDefaultMaxIPv6Networks),
338 step_delay_(kDefaultStepDelay),
339 allow_tcp_listen_(true),
340 candidate_filter_(CF_ALL) {}
deadbeef42a42632017-03-10 15:18:00 -0800341
Peter Thatcher73ba7a62015-04-14 09:26:03 -0700342 virtual ~PortAllocator() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000343
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700344 // This should be called on the PortAllocator's thread before the
345 // PortAllocator is used. Subclasses may override this if necessary.
346 virtual void Initialize() {}
347
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700348 // Set STUN and TURN servers to be used in future sessions, and set
349 // candidate pool size, as described in JSEP.
350 //
deadbeef42a42632017-03-10 15:18:00 -0800351 // If the servers are changing, and the candidate pool size is nonzero, and
352 // FreezeCandidatePool hasn't been called, existing pooled sessions will be
353 // destroyed and new ones created.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700354 //
deadbeef42a42632017-03-10 15:18:00 -0800355 // If the servers are not changing but the candidate pool size is, and
356 // FreezeCandidatePool hasn't been called, pooled sessions will be either
357 // created or destroyed as necessary.
deadbeef6de92f92016-12-12 18:49:32 -0800358 //
359 // Returns true if the configuration could successfully be changed.
360 bool SetConfiguration(const ServerAddresses& stun_servers,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700361 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700362 int candidate_pool_size,
363 bool prune_turn_ports);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700364
365 const ServerAddresses& stun_servers() const { return stun_servers_; }
366
367 const std::vector<RelayServerConfig>& turn_servers() const {
368 return turn_servers_;
369 }
370
deadbeef6de92f92016-12-12 18:49:32 -0800371 int candidate_pool_size() const { return candidate_pool_size_; }
deadbeef653b8e02015-11-11 12:55:10 -0800372
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800373 // Sets the network types to ignore.
374 // Values are defined by the AdapterType enum.
375 // For instance, calling this with
376 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
377 // loopback interfaces.
378 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0;
379
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700380 std::unique_ptr<PortAllocatorSession> CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381 const std::string& content_name,
382 int component,
383 const std::string& ice_ufrag,
384 const std::string& ice_pwd);
385
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700386 // Get an available pooled session and set the transport information on it.
387 //
388 // Caller takes ownership of the returned session.
389 //
390 // If no pooled sessions are available, returns null.
391 std::unique_ptr<PortAllocatorSession> TakePooledSession(
392 const std::string& content_name,
393 int component,
394 const std::string& ice_ufrag,
395 const std::string& ice_pwd);
396
397 // Returns the next session that would be returned by TakePooledSession.
398 const PortAllocatorSession* GetPooledSession() const;
399
deadbeef42a42632017-03-10 15:18:00 -0800400 // After FreezeCandidatePool is called, changing the candidate pool size will
401 // no longer be allowed, and changing ICE servers will not cause pooled
402 // sessions to be recreated.
403 //
404 // Expected to be called when SetLocalDescription is called on a
405 // PeerConnection. Can be called safely on any thread as long as not
406 // simultaneously with SetConfiguration.
407 void FreezeCandidatePool();
408
409 // Discard any remaining pooled sessions.
410 void DiscardCandidatePool();
411
Peter Boström0c4e06b2015-10-07 12:23:21 +0200412 uint32_t flags() const { return flags_; }
413 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000414
deadbeeff137e972017-03-23 15:45:49 -0700415 // These three methods are deprecated. If connections need to go through a
416 // proxy, the application should create a BasicPortAllocator given a custom
417 // PacketSocketFactory that creates proxy sockets.
418 const std::string& user_agent() const { return agent_; }
419 const rtc::ProxyInfo& proxy() const { return proxy_; }
420 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
421 agent_ = agent;
422 proxy_ = proxy;
423 }
424
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000425 // Gets/Sets the port range to use when choosing client ports.
426 int min_port() const { return min_port_; }
427 int max_port() const { return max_port_; }
428 bool SetPortRange(int min_port, int max_port) {
429 if (min_port > max_port) {
430 return false;
431 }
432
433 min_port_ = min_port;
434 max_port_ = max_port;
435 return true;
436 }
437
deadbeef3427f532017-07-26 16:09:33 -0700438 // Can be used to change the default numer of IPv6 network interfaces used
439 // (5). Can set to INT_MAX to effectively disable the limit.
440 //
441 // TODO(deadbeef): Applications shouldn't have to arbitrarily limit the
442 // number of available IPv6 network interfaces just because they could slow
443 // ICE down. We should work on making our ICE logic smarter (for example,
444 // prioritizing pinging connections that are most likely to work) so that
445 // every network interface can be used without impacting ICE's speed.
446 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; }
447 int max_ipv6_networks() { return max_ipv6_networks_; }
448
Peter Boström0c4e06b2015-10-07 12:23:21 +0200449 uint32_t step_delay() const { return step_delay_; }
450 void set_step_delay(uint32_t delay) { step_delay_ = delay; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000451
452 bool allow_tcp_listen() const { return allow_tcp_listen_; }
453 void set_allow_tcp_listen(bool allow_tcp_listen) {
454 allow_tcp_listen_ = allow_tcp_listen;
455 }
456
Peter Boström0c4e06b2015-10-07 12:23:21 +0200457 uint32_t candidate_filter() { return candidate_filter_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700458 void set_candidate_filter(uint32_t filter) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000459 candidate_filter_ = filter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000460 }
461
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700462 bool prune_turn_ports() const { return prune_turn_ports_; }
463
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000464 // Gets/Sets the Origin value used for WebRTC STUN requests.
465 const std::string& origin() const { return origin_; }
466 void set_origin(const std::string& origin) { origin_ = origin; }
467
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700468 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) {
469 metrics_observer_ = observer;
470 }
471
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000472 protected:
473 virtual PortAllocatorSession* CreateSessionInternal(
474 const std::string& content_name,
475 int component,
476 const std::string& ice_ufrag,
477 const std::string& ice_pwd) = 0;
478
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700479 webrtc::MetricsObserverInterface* metrics_observer() {
480 return metrics_observer_;
481 }
482
483 const std::deque<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() {
484 return pooled_sessions_;
485 }
486
Peter Boström0c4e06b2015-10-07 12:23:21 +0200487 uint32_t flags_;
deadbeeff137e972017-03-23 15:45:49 -0700488 std::string agent_;
489 rtc::ProxyInfo proxy_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000490 int min_port_;
491 int max_port_;
deadbeef3427f532017-07-26 16:09:33 -0700492 int max_ipv6_networks_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200493 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000494 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200495 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000496 std::string origin_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700497
498 private:
499 ServerAddresses stun_servers_;
500 std::vector<RelayServerConfig> turn_servers_;
deadbeef6de92f92016-12-12 18:49:32 -0800501 int candidate_pool_size_ = 0; // Last value passed into SetConfiguration.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700502 std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
deadbeef42a42632017-03-10 15:18:00 -0800503 bool candidate_pool_frozen_ = false;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700504 bool prune_turn_ports_ = false;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700505
506 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000507};
508
509} // namespace cricket
510
511#endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_