blob: 5c0d3035020d6fd0cc22b7486bdd85461b4e271d [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 {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700151 RelayServerConfig(RelayType type) : type(type) {}
deadbeef653b8e02015-11-11 12:55:10 -0800152
Emad Omaradab1d2d2017-06-16 15:43:11 -0700153 RelayServerConfig(const rtc::SocketAddress& address,
154 const std::string& username,
155 const std::string& password,
156 ProtocolType proto)
157 : type(RELAY_TURN), credentials(username, password) {
158 ports.push_back(ProtocolAddress(address, proto));
159 }
160
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800161 RelayServerConfig(const std::string& address,
162 int port,
163 const std::string& username,
164 const std::string& password,
hnsl277b2502016-12-13 05:17:23 -0800165 ProtocolType proto)
Emad Omaradab1d2d2017-06-16 15:43:11 -0700166 : RelayServerConfig(rtc::SocketAddress(address, port),
167 username,
168 password,
169 proto) {}
hnsl277b2502016-12-13 05:17:23 -0800170
171 // Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS.
172 RelayServerConfig(const std::string& address,
173 int port,
174 const std::string& username,
175 const std::string& password,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800176 ProtocolType proto,
177 bool secure)
hnsl277b2502016-12-13 05:17:23 -0800178 : RelayServerConfig(address,
179 port,
180 username,
181 password,
182 (proto == PROTO_TCP && secure ? PROTO_TLS : proto)) {}
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800183
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700184 bool operator==(const RelayServerConfig& o) const {
185 return type == o.type && ports == o.ports && credentials == o.credentials &&
186 priority == o.priority;
187 }
188 bool operator!=(const RelayServerConfig& o) const { return !(*this == o); }
189
deadbeef653b8e02015-11-11 12:55:10 -0800190 RelayType type;
191 PortList ports;
192 RelayCredentials credentials;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700193 int priority = 0;
hnsl04833622017-01-09 08:35:45 -0800194 TlsCertPolicy tls_cert_policy = TlsCertPolicy::TLS_CERT_POLICY_SECURE;
Diogo Real1dca9d52017-08-29 12:18:32 -0700195 std::vector<std::string> tls_alpn_protocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700196 std::vector<std::string> tls_elliptic_curves;
deadbeef653b8e02015-11-11 12:55:10 -0800197};
198
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199class PortAllocatorSession : public sigslot::has_slots<> {
200 public:
201 // Content name passed in mostly for logging and debugging.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 PortAllocatorSession(const std::string& content_name,
203 int component,
deadbeefcbecd352015-09-23 11:50:27 -0700204 const std::string& ice_ufrag,
205 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200206 uint32_t flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207
208 // Subclasses should clean up any ports created.
209 virtual ~PortAllocatorSession() {}
210
Peter Boström0c4e06b2015-10-07 12:23:21 +0200211 uint32_t flags() const { return flags_; }
212 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 std::string content_name() const { return content_name_; }
214 int component() const { return component_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700215 const std::string& ice_ufrag() const { return ice_ufrag_; }
216 const std::string& ice_pwd() const { return ice_pwd_; }
217 bool pooled() const { return ice_ufrag_.empty(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700219 // Setting this filter should affect not only candidates gathered in the
220 // future, but candidates already gathered and ports already "ready",
221 // which would be returned by ReadyCandidates() and ReadyPorts().
222 //
223 // Default filter should be CF_ALL.
224 virtual void SetCandidateFilter(uint32_t filter) = 0;
225
deadbeefb60a8192016-08-24 15:15:00 -0700226 // Starts gathering ports and ICE candidates.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700227 virtual void StartGettingPorts() = 0;
deadbeefb60a8192016-08-24 15:15:00 -0700228 // Completely stops gathering. Will not gather again unless StartGettingPorts
229 // is called again.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700230 virtual void StopGettingPorts() = 0;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700231 // Whether the session is actively getting ports.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700232 virtual bool IsGettingPorts() = 0;
deadbeefb60a8192016-08-24 15:15:00 -0700233
234 //
235 // NOTE: The group of methods below is only used for continual gathering.
236 //
237
238 // ClearGettingPorts should have the same immediate effect as
239 // StopGettingPorts, but if the implementation supports continual gathering,
240 // ClearGettingPorts allows additional ports/candidates to be gathered if the
241 // network conditions change.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700242 virtual void ClearGettingPorts() = 0;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700243 // Whether it is in the state where the existing gathering process is stopped,
244 // but new ones may be started (basically after calling ClearGettingPorts).
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700245 virtual bool IsCleared() const { return false; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700246 // Whether the session has completely stopped.
Honghai Zhangd8f6fc42016-07-01 17:31:12 -0700247 virtual bool IsStopped() const { return false; }
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700248 // Re-gathers candidates on networks that do not have any connections. More
249 // precisely, a network interface may have more than one IP addresses (e.g.,
250 // IPv4 and IPv6 addresses). Each address subnet will be used to create a
251 // network. Only if all networks of an interface have no connection, the
252 // implementation should start re-gathering on all networks of that interface.
253 virtual void RegatherOnFailedNetworks() {}
254 // Re-gathers candidates on all networks.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700255 virtual void RegatherOnAllNetworks() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000256
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700257 // Another way of getting the information provided by the signals below.
258 //
259 // Ports and candidates are not guaranteed to be in the same order as the
260 // signals were emitted in.
261 virtual std::vector<PortInterface*> ReadyPorts() const = 0;
262 virtual std::vector<Candidate> ReadyCandidates() const = 0;
263 virtual bool CandidatesAllocationDone() const = 0;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700264 // Marks all ports in the current session as "pruned" so that they may be
265 // destroyed if no connection is using them.
266 virtual void PruneAllPorts() {}
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700267
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000268 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700269 // Fires this signal when the network of the ports failed (either because the
270 // interface is down, or because there is no connection on the interface),
271 // or when TURN ports are pruned because a higher-priority TURN port becomes
272 // ready(pairable).
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700273 sigslot::signal2<PortAllocatorSession*, const std::vector<PortInterface*>&>
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700274 SignalPortsPruned;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275 sigslot::signal2<PortAllocatorSession*,
276 const std::vector<Candidate>&> SignalCandidatesReady;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700277 // Candidates should be signaled to be removed when the port that generated
278 // the candidates is removed.
279 sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
280 SignalCandidatesRemoved;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000281 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
282
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700283 sigslot::signal2<PortAllocatorSession*, IceRegatheringReason>
284 SignalIceRegathering;
285
Peter Boström0c4e06b2015-10-07 12:23:21 +0200286 virtual uint32_t generation() { return generation_; }
287 virtual void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000288 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
289
deadbeefc55fb302016-05-12 12:51:38 -0700290 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700291 // This method is called when a pooled session (which doesn't have these
292 // properties initially) is returned by PortAllocator::TakePooledSession,
293 // and the content name, component, and ICE ufrag/pwd are updated.
294 //
295 // A subclass may need to override this method to perform additional actions,
296 // such as applying the updated information to ports and candidates.
297 virtual void UpdateIceParametersInternal() {}
298
deadbeefcbecd352015-09-23 11:50:27 -0700299 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
300 // ice_pwd.
301 const std::string& username() const { return ice_ufrag_; }
302 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000303
deadbeefc55fb302016-05-12 12:51:38 -0700304 private:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700305 void SetIceParameters(const std::string& content_name,
306 int component,
307 const std::string& ice_ufrag,
308 const std::string& ice_pwd) {
309 content_name_ = content_name;
310 component_ = component;
311 ice_ufrag_ = ice_ufrag;
312 ice_pwd_ = ice_pwd;
313 UpdateIceParametersInternal();
314 }
315
deadbeefc55fb302016-05-12 12:51:38 -0700316 uint32_t flags_;
317 uint32_t generation_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700318 std::string content_name_;
319 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700320 std::string ice_ufrag_;
321 std::string ice_pwd_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700322
323 // SetIceParameters is an implementation detail which only PortAllocator
324 // should be able to call.
325 friend class PortAllocator;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000326};
327
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700328// Every method of PortAllocator (including the destructor) must be called on
329// the same thread, except for the constructor which may be called on any
330// thread.
331//
332// This allows constructing a PortAllocator subclass on one thread and
333// passing it into an object that uses it on a different thread.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000334class PortAllocator : public sigslot::has_slots<> {
335 public:
deadbeef3427f532017-07-26 16:09:33 -0700336 PortAllocator()
337 : flags_(kDefaultPortAllocatorFlags),
338 min_port_(0),
339 max_port_(0),
340 max_ipv6_networks_(kDefaultMaxIPv6Networks),
341 step_delay_(kDefaultStepDelay),
342 allow_tcp_listen_(true),
343 candidate_filter_(CF_ALL) {}
deadbeef42a42632017-03-10 15:18:00 -0800344
Peter Thatcher73ba7a62015-04-14 09:26:03 -0700345 virtual ~PortAllocator() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000346
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700347 // This should be called on the PortAllocator's thread before the
348 // PortAllocator is used. Subclasses may override this if necessary.
349 virtual void Initialize() {}
350
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700351 // Set STUN and TURN servers to be used in future sessions, and set
352 // candidate pool size, as described in JSEP.
353 //
deadbeef42a42632017-03-10 15:18:00 -0800354 // If the servers are changing, and the candidate pool size is nonzero, and
355 // FreezeCandidatePool hasn't been called, existing pooled sessions will be
356 // destroyed and new ones created.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700357 //
deadbeef42a42632017-03-10 15:18:00 -0800358 // If the servers are not changing but the candidate pool size is, and
359 // FreezeCandidatePool hasn't been called, pooled sessions will be either
360 // created or destroyed as necessary.
deadbeef6de92f92016-12-12 18:49:32 -0800361 //
362 // Returns true if the configuration could successfully be changed.
363 bool SetConfiguration(const ServerAddresses& stun_servers,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700364 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700365 int candidate_pool_size,
Jonas Orelandbdcee282017-10-10 14:01:40 +0200366 bool prune_turn_ports,
367 webrtc::TurnCustomizer* turn_customizer = nullptr);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700368
369 const ServerAddresses& stun_servers() const { return stun_servers_; }
370
371 const std::vector<RelayServerConfig>& turn_servers() const {
372 return turn_servers_;
373 }
374
deadbeef6de92f92016-12-12 18:49:32 -0800375 int candidate_pool_size() const { return candidate_pool_size_; }
deadbeef653b8e02015-11-11 12:55:10 -0800376
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800377 // Sets the network types to ignore.
378 // Values are defined by the AdapterType enum.
379 // For instance, calling this with
380 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
381 // loopback interfaces.
382 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0;
383
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700384 std::unique_ptr<PortAllocatorSession> CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000385 const std::string& content_name,
386 int component,
387 const std::string& ice_ufrag,
388 const std::string& ice_pwd);
389
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700390 // Get an available pooled session and set the transport information on it.
391 //
392 // Caller takes ownership of the returned session.
393 //
394 // If no pooled sessions are available, returns null.
395 std::unique_ptr<PortAllocatorSession> TakePooledSession(
396 const std::string& content_name,
397 int component,
398 const std::string& ice_ufrag,
399 const std::string& ice_pwd);
400
401 // Returns the next session that would be returned by TakePooledSession.
402 const PortAllocatorSession* GetPooledSession() const;
403
deadbeef42a42632017-03-10 15:18:00 -0800404 // After FreezeCandidatePool is called, changing the candidate pool size will
405 // no longer be allowed, and changing ICE servers will not cause pooled
406 // sessions to be recreated.
407 //
408 // Expected to be called when SetLocalDescription is called on a
409 // PeerConnection. Can be called safely on any thread as long as not
410 // simultaneously with SetConfiguration.
411 void FreezeCandidatePool();
412
413 // Discard any remaining pooled sessions.
414 void DiscardCandidatePool();
415
Peter Boström0c4e06b2015-10-07 12:23:21 +0200416 uint32_t flags() const { return flags_; }
417 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418
deadbeeff137e972017-03-23 15:45:49 -0700419 // These three methods are deprecated. If connections need to go through a
420 // proxy, the application should create a BasicPortAllocator given a custom
421 // PacketSocketFactory that creates proxy sockets.
422 const std::string& user_agent() const { return agent_; }
423 const rtc::ProxyInfo& proxy() const { return proxy_; }
424 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
425 agent_ = agent;
426 proxy_ = proxy;
427 }
428
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000429 // Gets/Sets the port range to use when choosing client ports.
430 int min_port() const { return min_port_; }
431 int max_port() const { return max_port_; }
432 bool SetPortRange(int min_port, int max_port) {
433 if (min_port > max_port) {
434 return false;
435 }
436
437 min_port_ = min_port;
438 max_port_ = max_port;
439 return true;
440 }
441
deadbeef3427f532017-07-26 16:09:33 -0700442 // Can be used to change the default numer of IPv6 network interfaces used
443 // (5). Can set to INT_MAX to effectively disable the limit.
444 //
445 // TODO(deadbeef): Applications shouldn't have to arbitrarily limit the
446 // number of available IPv6 network interfaces just because they could slow
447 // ICE down. We should work on making our ICE logic smarter (for example,
448 // prioritizing pinging connections that are most likely to work) so that
449 // every network interface can be used without impacting ICE's speed.
450 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; }
451 int max_ipv6_networks() { return max_ipv6_networks_; }
452
deadbeef1c5e6d02017-09-15 17:46:56 -0700453 // Delay between different candidate gathering phases (UDP, TURN, TCP).
454 // Defaults to 1 second, but PeerConnection sets it to 50ms.
455 // TODO(deadbeef): Get rid of this. Its purpose is to avoid sending too many
456 // STUN transactions at once, but that's already happening if you configure
457 // multiple STUN servers or have multiple network interfaces. We should
458 // implement some global pacing logic instead if that's our goal.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200459 uint32_t step_delay() const { return step_delay_; }
460 void set_step_delay(uint32_t delay) { step_delay_ = delay; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000461
462 bool allow_tcp_listen() const { return allow_tcp_listen_; }
463 void set_allow_tcp_listen(bool allow_tcp_listen) {
464 allow_tcp_listen_ = allow_tcp_listen;
465 }
466
Peter Boström0c4e06b2015-10-07 12:23:21 +0200467 uint32_t candidate_filter() { return candidate_filter_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700468 void set_candidate_filter(uint32_t filter) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000469 candidate_filter_ = filter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000470 }
471
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700472 bool prune_turn_ports() const { return prune_turn_ports_; }
473
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000474 // Gets/Sets the Origin value used for WebRTC STUN requests.
475 const std::string& origin() const { return origin_; }
476 void set_origin(const std::string& origin) { origin_ = origin; }
477
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700478 void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) {
479 metrics_observer_ = observer;
480 }
481
Jonas Orelandbdcee282017-10-10 14:01:40 +0200482 webrtc::TurnCustomizer* turn_customizer() {
483 return turn_customizer_;
484 }
485
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000486 protected:
487 virtual PortAllocatorSession* CreateSessionInternal(
488 const std::string& content_name,
489 int component,
490 const std::string& ice_ufrag,
491 const std::string& ice_pwd) = 0;
492
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700493 webrtc::MetricsObserverInterface* metrics_observer() {
494 return metrics_observer_;
495 }
496
497 const std::deque<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() {
498 return pooled_sessions_;
499 }
500
Peter Boström0c4e06b2015-10-07 12:23:21 +0200501 uint32_t flags_;
deadbeeff137e972017-03-23 15:45:49 -0700502 std::string agent_;
503 rtc::ProxyInfo proxy_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000504 int min_port_;
505 int max_port_;
deadbeef3427f532017-07-26 16:09:33 -0700506 int max_ipv6_networks_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200507 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000508 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200509 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000510 std::string origin_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700511
512 private:
513 ServerAddresses stun_servers_;
514 std::vector<RelayServerConfig> turn_servers_;
deadbeef6de92f92016-12-12 18:49:32 -0800515 int candidate_pool_size_ = 0; // Last value passed into SetConfiguration.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700516 std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
deadbeef42a42632017-03-10 15:18:00 -0800517 bool candidate_pool_frozen_ = false;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700518 bool prune_turn_ports_ = false;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700519
520 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
Jonas Orelandbdcee282017-10-10 14:01:40 +0200521
522 // Customizer for TURN messages.
523 // The instance is owned by application and will be shared among
524 // all TurnPort(s) created.
525 webrtc::TurnCustomizer* turn_customizer_ = nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000526};
527
528} // namespace cricket
529
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200530#endif // P2P_BASE_PORTALLOCATOR_H_