blob: dc1961db73f2ab41c8f887a2b90ebbd3174bb20a [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"
21#include "webrtc/base/helpers.h"
22#include "webrtc/base/proxyinfo.h"
23#include "webrtc/base/sigslot.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070024#include "webrtc/base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
26namespace cricket {
27
28// PortAllocator is responsible for allocating Port types for a given
29// P2PSocket. It also handles port freeing.
30//
31// Clients can override this class to control port allocation, including
32// what kinds of ports are allocated.
33
34enum {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070035 // Disable local UDP ports. This doesn't impact how we connect to relay
36 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037 PORTALLOCATOR_DISABLE_UDP = 0x01,
38 PORTALLOCATOR_DISABLE_STUN = 0x02,
39 PORTALLOCATOR_DISABLE_RELAY = 0x04,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070040 // Disable local TCP ports. This doesn't impact how we connect to relay
41 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042 PORTALLOCATOR_DISABLE_TCP = 0x08,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
Peter Thatcher7cbd1882015-09-17 18:54:52 -070044 // TODO(pthatcher): Remove this once it's no longer used in:
45 // remoting/client/plugin/pepper_port_allocator.cc
46 // remoting/protocol/chromium_port_allocator.cc
47 // remoting/test/fake_port_allocator.cc
48 // It's a no-op and is no longer needed.
pthatcherfa301802015-08-11 04:12:56 -070049 PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
51 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080052 // When specified, we'll only allocate the STUN candidate for the public
53 // interface as seen by regular http traffic and the HOST candidate associated
54 // with the default local interface.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000055 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080056 // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the
57 // default local candidate mentioned above will not be allocated. Only the
58 // STUN candidate will be.
59 PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070060 // Disallow use of UDP when connecting to a relay server. Since proxy servers
61 // usually don't handle UDP, using UDP will leak the IP address.
62 PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000,
honghaiz60347052016-05-31 18:29:12 -070063
64 // When multiple networks exist, do not gather candidates on the ones with
65 // high cost. So if both Wi-Fi and cellular networks exist, gather only on the
66 // Wi-Fi network. If a network type is "unknown", it has a cost lower than
67 // cellular but higher than Wi-Fi/Ethernet. So if an unknown network exists,
68 // cellular networks will not be used to gather candidates and if a Wi-Fi
69 // network is present, "unknown" networks will not be usd to gather
70 // candidates. Doing so ensures that even if a cellular network type was not
71 // detected initially, it would not be used if a Wi-Fi network is present.
72 PORTALLOCATOR_DISABLE_COSTLY_NETWORKS = 0x2000,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073};
74
Peter Boström0c4e06b2015-10-07 12:23:21 +020075const uint32_t kDefaultPortAllocatorFlags = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076
Peter Boström0c4e06b2015-10-07 12:23:21 +020077const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
79// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
Peter Boström0c4e06b2015-10-07 12:23:21 +020080const uint32_t kMinimumStepDelay = 50;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000081
82// CF = CANDIDATE FILTER
83enum {
84 CF_NONE = 0x0,
85 CF_HOST = 0x1,
86 CF_REFLEXIVE = 0x2,
87 CF_RELAY = 0x4,
88 CF_ALL = 0x7,
89};
90
Honghai Zhang5622c5e2016-07-01 13:59:29 -070091enum class SessionState {
92 GATHERING, // Actively allocating ports and gathering candidates.
93 CLEARED, // Current allocation process has been stopped but may start
94 // new ones.
95 STOPPED // This session has completely stopped, no new allocation
96 // process will be started.
97};
98
deadbeef653b8e02015-11-11 12:55:10 -080099// TODO(deadbeef): Rename to TurnCredentials (and username to ufrag).
100struct RelayCredentials {
101 RelayCredentials() {}
102 RelayCredentials(const std::string& username, const std::string& password)
103 : username(username), password(password) {}
104
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700105 bool operator==(const RelayCredentials& o) const {
106 return username == o.username && password == o.password;
107 }
108 bool operator!=(const RelayCredentials& o) const { return !(*this == o); }
109
deadbeef653b8e02015-11-11 12:55:10 -0800110 std::string username;
111 std::string password;
112};
113
114typedef std::vector<ProtocolAddress> PortList;
115// TODO(deadbeef): Rename to TurnServerConfig.
116struct RelayServerConfig {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700117 RelayServerConfig(RelayType type) : type(type) {}
deadbeef653b8e02015-11-11 12:55:10 -0800118
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800119 RelayServerConfig(const std::string& address,
120 int port,
121 const std::string& username,
122 const std::string& password,
123 ProtocolType proto,
124 bool secure)
125 : type(RELAY_TURN), credentials(username, password) {
126 ports.push_back(
127 ProtocolAddress(rtc::SocketAddress(address, port), proto, secure));
128 }
129
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700130 bool operator==(const RelayServerConfig& o) const {
131 return type == o.type && ports == o.ports && credentials == o.credentials &&
132 priority == o.priority;
133 }
134 bool operator!=(const RelayServerConfig& o) const { return !(*this == o); }
135
deadbeef653b8e02015-11-11 12:55:10 -0800136 RelayType type;
137 PortList ports;
138 RelayCredentials credentials;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700139 int priority = 0;
deadbeef653b8e02015-11-11 12:55:10 -0800140};
141
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142class PortAllocatorSession : public sigslot::has_slots<> {
143 public:
144 // Content name passed in mostly for logging and debugging.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145 PortAllocatorSession(const std::string& content_name,
146 int component,
deadbeefcbecd352015-09-23 11:50:27 -0700147 const std::string& ice_ufrag,
148 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200149 uint32_t flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150
151 // Subclasses should clean up any ports created.
152 virtual ~PortAllocatorSession() {}
153
Peter Boström0c4e06b2015-10-07 12:23:21 +0200154 uint32_t flags() const { return flags_; }
155 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000156 std::string content_name() const { return content_name_; }
157 int component() const { return component_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700158 const std::string& ice_ufrag() const { return ice_ufrag_; }
159 const std::string& ice_pwd() const { return ice_pwd_; }
160 bool pooled() const { return ice_ufrag_.empty(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700162 // Setting this filter should affect not only candidates gathered in the
163 // future, but candidates already gathered and ports already "ready",
164 // which would be returned by ReadyCandidates() and ReadyPorts().
165 //
166 // Default filter should be CF_ALL.
167 virtual void SetCandidateFilter(uint32_t filter) = 0;
168
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000169 // Starts gathering STUN and Relay configurations.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700170 virtual void StartGettingPorts() { state_ = SessionState::GATHERING; }
171 // Completely stops the gathering process and will not start new ones.
172 virtual void StopGettingPorts() { state_ = SessionState::STOPPED; }
173 // Only stops the existing gathering process but may start new ones if needed.
174 virtual void ClearGettingPorts() { state_ = SessionState::CLEARED; }
175 // Whether the session is actively getting ports.
176 bool IsGettingPorts() { return state_ == SessionState::GATHERING; }
177 // Whether it is in the state where the existing gathering process is stopped,
178 // but new ones may be started (basically after calling ClearGettingPorts).
179 bool IsCleared() { return state_ == SessionState::CLEARED; }
180 // Whether the session has completely stopped.
181 bool IsStopped() { return state_ == SessionState::STOPPED; }
182 // Re-gathers candidates on networks that do not have any connections. More
183 // precisely, a network interface may have more than one IP addresses (e.g.,
184 // IPv4 and IPv6 addresses). Each address subnet will be used to create a
185 // network. Only if all networks of an interface have no connection, the
186 // implementation should start re-gathering on all networks of that interface.
187 virtual void RegatherOnFailedNetworks() {}
188 // Re-gathers candidates on all networks.
189 // TODO(honghaiz): Implement this in BasicPortAllocator.
190 virtual void RegatherOnAllNetworks() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700192 // Another way of getting the information provided by the signals below.
193 //
194 // Ports and candidates are not guaranteed to be in the same order as the
195 // signals were emitted in.
196 virtual std::vector<PortInterface*> ReadyPorts() const = 0;
197 virtual std::vector<Candidate> ReadyCandidates() const = 0;
198 virtual bool CandidatesAllocationDone() const = 0;
199
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700201 // Ports should be signaled to be removed when the networks of the ports
202 // failed (either because the interface is down, or because there is no
203 // connection on the interface).
204 sigslot::signal2<PortAllocatorSession*, const std::vector<PortInterface*>&>
205 SignalPortsRemoved;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206 sigslot::signal2<PortAllocatorSession*,
207 const std::vector<Candidate>&> SignalCandidatesReady;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700208 // Candidates should be signaled to be removed when the port that generated
209 // the candidates is removed.
210 sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
211 SignalCandidatesRemoved;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700213 // A TURN port is pruned if a higher-priority TURN port becomes ready
214 // (pairable). When it is pruned, it will not be used for creating
215 // connections and its candidates will not be sent to the remote side
216 // if they have not been sent.
217 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortPruned;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218
Peter Boström0c4e06b2015-10-07 12:23:21 +0200219 virtual uint32_t generation() { return generation_; }
220 virtual void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
222
deadbeefc55fb302016-05-12 12:51:38 -0700223 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700224 // This method is called when a pooled session (which doesn't have these
225 // properties initially) is returned by PortAllocator::TakePooledSession,
226 // and the content name, component, and ICE ufrag/pwd are updated.
227 //
228 // A subclass may need to override this method to perform additional actions,
229 // such as applying the updated information to ports and candidates.
230 virtual void UpdateIceParametersInternal() {}
231
deadbeefcbecd352015-09-23 11:50:27 -0700232 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
233 // ice_pwd.
234 const std::string& username() const { return ice_ufrag_; }
235 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236
deadbeefc55fb302016-05-12 12:51:38 -0700237 private:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700238 void SetIceParameters(const std::string& content_name,
239 int component,
240 const std::string& ice_ufrag,
241 const std::string& ice_pwd) {
242 content_name_ = content_name;
243 component_ = component;
244 ice_ufrag_ = ice_ufrag;
245 ice_pwd_ = ice_pwd;
246 UpdateIceParametersInternal();
247 }
248
deadbeefc55fb302016-05-12 12:51:38 -0700249 uint32_t flags_;
250 uint32_t generation_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700251 std::string content_name_;
252 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700253 std::string ice_ufrag_;
254 std::string ice_pwd_;
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700255 SessionState state_ = SessionState::CLEARED;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700256
257 // SetIceParameters is an implementation detail which only PortAllocator
258 // should be able to call.
259 friend class PortAllocator;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000260};
261
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700262// Every method of PortAllocator (including the destructor) must be called on
263// the same thread, except for the constructor which may be called on any
264// thread.
265//
266// This allows constructing a PortAllocator subclass on one thread and
267// passing it into an object that uses it on a different thread.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000268class PortAllocator : public sigslot::has_slots<> {
269 public:
270 PortAllocator() :
271 flags_(kDefaultPortAllocatorFlags),
272 min_port_(0),
273 max_port_(0),
274 step_delay_(kDefaultStepDelay),
275 allow_tcp_listen_(true),
276 candidate_filter_(CF_ALL) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000277 }
Peter Thatcher73ba7a62015-04-14 09:26:03 -0700278 virtual ~PortAllocator() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000279
Taylor Brandstetterf8e65772016-06-27 17:20:15 -0700280 // This should be called on the PortAllocator's thread before the
281 // PortAllocator is used. Subclasses may override this if necessary.
282 virtual void Initialize() {}
283
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700284 // Set STUN and TURN servers to be used in future sessions, and set
285 // candidate pool size, as described in JSEP.
286 //
287 // If the servers are changing and the candidate pool size is nonzero,
288 // existing pooled sessions will be destroyed and new ones created.
289 //
290 // If the servers are not changing but the candidate pool size is,
291 // pooled sessions will be either created or destroyed as necessary.
292 void SetConfiguration(const ServerAddresses& stun_servers,
293 const std::vector<RelayServerConfig>& turn_servers,
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700294 int candidate_pool_size,
295 bool prune_turn_ports);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700296
297 const ServerAddresses& stun_servers() const { return stun_servers_; }
298
299 const std::vector<RelayServerConfig>& turn_servers() const {
300 return turn_servers_;
301 }
302
303 int candidate_pool_size() const { return target_pooled_session_count_; }
deadbeef653b8e02015-11-11 12:55:10 -0800304
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800305 // Sets the network types to ignore.
306 // Values are defined by the AdapterType enum.
307 // For instance, calling this with
308 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
309 // loopback interfaces.
310 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0;
311
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700312 std::unique_ptr<PortAllocatorSession> CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000313 const std::string& sid,
314 const std::string& content_name,
315 int component,
316 const std::string& ice_ufrag,
317 const std::string& ice_pwd);
318
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700319 // Get an available pooled session and set the transport information on it.
320 //
321 // Caller takes ownership of the returned session.
322 //
323 // If no pooled sessions are available, returns null.
324 std::unique_ptr<PortAllocatorSession> TakePooledSession(
325 const std::string& content_name,
326 int component,
327 const std::string& ice_ufrag,
328 const std::string& ice_pwd);
329
330 // Returns the next session that would be returned by TakePooledSession.
331 const PortAllocatorSession* GetPooledSession() const;
332
Peter Boström0c4e06b2015-10-07 12:23:21 +0200333 uint32_t flags() const { return flags_; }
334 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000335
336 const std::string& user_agent() const { return agent_; }
337 const rtc::ProxyInfo& proxy() const { return proxy_; }
338 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
339 agent_ = agent;
340 proxy_ = proxy;
341 }
342
343 // Gets/Sets the port range to use when choosing client ports.
344 int min_port() const { return min_port_; }
345 int max_port() const { return max_port_; }
346 bool SetPortRange(int min_port, int max_port) {
347 if (min_port > max_port) {
348 return false;
349 }
350
351 min_port_ = min_port;
352 max_port_ = max_port;
353 return true;
354 }
355
Peter Boström0c4e06b2015-10-07 12:23:21 +0200356 uint32_t step_delay() const { return step_delay_; }
357 void set_step_delay(uint32_t delay) { step_delay_ = delay; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000358
359 bool allow_tcp_listen() const { return allow_tcp_listen_; }
360 void set_allow_tcp_listen(bool allow_tcp_listen) {
361 allow_tcp_listen_ = allow_tcp_listen;
362 }
363
Peter Boström0c4e06b2015-10-07 12:23:21 +0200364 uint32_t candidate_filter() { return candidate_filter_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700365 void set_candidate_filter(uint32_t filter) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000366 candidate_filter_ = filter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000367 }
368
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700369 bool prune_turn_ports() const { return prune_turn_ports_; }
370
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000371 // Gets/Sets the Origin value used for WebRTC STUN requests.
372 const std::string& origin() const { return origin_; }
373 void set_origin(const std::string& origin) { origin_ = origin; }
374
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000375 protected:
376 virtual PortAllocatorSession* CreateSessionInternal(
377 const std::string& content_name,
378 int component,
379 const std::string& ice_ufrag,
380 const std::string& ice_pwd) = 0;
381
Peter Boström0c4e06b2015-10-07 12:23:21 +0200382 uint32_t flags_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000383 std::string agent_;
384 rtc::ProxyInfo proxy_;
385 int min_port_;
386 int max_port_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200387 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200389 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000390 std::string origin_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700391
392 private:
393 ServerAddresses stun_servers_;
394 std::vector<RelayServerConfig> turn_servers_;
395 // The last size passed into SetConfiguration.
396 int target_pooled_session_count_ = 0;
397 // This variable represents the total number of pooled sessions
398 // both owned by this class and taken by TakePooledSession.
399 int allocated_pooled_session_count_ = 0;
400 std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700401 bool prune_turn_ports_ = false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000402};
403
404} // namespace cricket
405
406#endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_