blob: 75354bcad3d381304e94111c958768099e59733f [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,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063};
64
Peter Boström0c4e06b2015-10-07 12:23:21 +020065const uint32_t kDefaultPortAllocatorFlags = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066
Peter Boström0c4e06b2015-10-07 12:23:21 +020067const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
69// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
Peter Boström0c4e06b2015-10-07 12:23:21 +020070const uint32_t kMinimumStepDelay = 50;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071
72// CF = CANDIDATE FILTER
73enum {
74 CF_NONE = 0x0,
75 CF_HOST = 0x1,
76 CF_REFLEXIVE = 0x2,
77 CF_RELAY = 0x4,
78 CF_ALL = 0x7,
79};
80
deadbeef653b8e02015-11-11 12:55:10 -080081// TODO(deadbeef): Rename to TurnCredentials (and username to ufrag).
82struct RelayCredentials {
83 RelayCredentials() {}
84 RelayCredentials(const std::string& username, const std::string& password)
85 : username(username), password(password) {}
86
Taylor Brandstettera1c30352016-05-13 08:15:11 -070087 bool operator==(const RelayCredentials& o) const {
88 return username == o.username && password == o.password;
89 }
90 bool operator!=(const RelayCredentials& o) const { return !(*this == o); }
91
deadbeef653b8e02015-11-11 12:55:10 -080092 std::string username;
93 std::string password;
94};
95
96typedef std::vector<ProtocolAddress> PortList;
97// TODO(deadbeef): Rename to TurnServerConfig.
98struct RelayServerConfig {
Taylor Brandstettera1c30352016-05-13 08:15:11 -070099 RelayServerConfig(RelayType type) : type(type) {}
deadbeef653b8e02015-11-11 12:55:10 -0800100
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800101 RelayServerConfig(const std::string& address,
102 int port,
103 const std::string& username,
104 const std::string& password,
105 ProtocolType proto,
106 bool secure)
107 : type(RELAY_TURN), credentials(username, password) {
108 ports.push_back(
109 ProtocolAddress(rtc::SocketAddress(address, port), proto, secure));
110 }
111
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700112 bool operator==(const RelayServerConfig& o) const {
113 return type == o.type && ports == o.ports && credentials == o.credentials &&
114 priority == o.priority;
115 }
116 bool operator!=(const RelayServerConfig& o) const { return !(*this == o); }
117
deadbeef653b8e02015-11-11 12:55:10 -0800118 RelayType type;
119 PortList ports;
120 RelayCredentials credentials;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700121 int priority = 0;
deadbeef653b8e02015-11-11 12:55:10 -0800122};
123
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124class PortAllocatorSession : public sigslot::has_slots<> {
125 public:
126 // Content name passed in mostly for logging and debugging.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000127 PortAllocatorSession(const std::string& content_name,
128 int component,
deadbeefcbecd352015-09-23 11:50:27 -0700129 const std::string& ice_ufrag,
130 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200131 uint32_t flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132
133 // Subclasses should clean up any ports created.
134 virtual ~PortAllocatorSession() {}
135
Peter Boström0c4e06b2015-10-07 12:23:21 +0200136 uint32_t flags() const { return flags_; }
137 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138 std::string content_name() const { return content_name_; }
139 int component() const { return component_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700140 const std::string& ice_ufrag() const { return ice_ufrag_; }
141 const std::string& ice_pwd() const { return ice_pwd_; }
142 bool pooled() const { return ice_ufrag_.empty(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700144 // Setting this filter should affect not only candidates gathered in the
145 // future, but candidates already gathered and ports already "ready",
146 // which would be returned by ReadyCandidates() and ReadyPorts().
147 //
148 // Default filter should be CF_ALL.
149 virtual void SetCandidateFilter(uint32_t filter) = 0;
150
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151 // Starts gathering STUN and Relay configurations.
152 virtual void StartGettingPorts() = 0;
153 virtual void StopGettingPorts() = 0;
honghaiz98db68f2015-09-29 07:58:17 -0700154 // Only stop the existing gathering process but may start new ones if needed.
155 virtual void ClearGettingPorts() = 0;
156 // Whether the process of getting ports has been stopped.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 virtual bool IsGettingPorts() = 0;
158
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700159 // Another way of getting the information provided by the signals below.
160 //
161 // Ports and candidates are not guaranteed to be in the same order as the
162 // signals were emitted in.
163 virtual std::vector<PortInterface*> ReadyPorts() const = 0;
164 virtual std::vector<Candidate> ReadyCandidates() const = 0;
165 virtual bool CandidatesAllocationDone() const = 0;
166
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
168 sigslot::signal2<PortAllocatorSession*,
169 const std::vector<Candidate>&> SignalCandidatesReady;
170 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
171
Peter Boström0c4e06b2015-10-07 12:23:21 +0200172 virtual uint32_t generation() { return generation_; }
173 virtual void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000174 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
175
deadbeefc55fb302016-05-12 12:51:38 -0700176 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700177 // This method is called when a pooled session (which doesn't have these
178 // properties initially) is returned by PortAllocator::TakePooledSession,
179 // and the content name, component, and ICE ufrag/pwd are updated.
180 //
181 // A subclass may need to override this method to perform additional actions,
182 // such as applying the updated information to ports and candidates.
183 virtual void UpdateIceParametersInternal() {}
184
deadbeefcbecd352015-09-23 11:50:27 -0700185 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
186 // ice_pwd.
187 const std::string& username() const { return ice_ufrag_; }
188 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189
deadbeefc55fb302016-05-12 12:51:38 -0700190 private:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700191 void SetIceParameters(const std::string& content_name,
192 int component,
193 const std::string& ice_ufrag,
194 const std::string& ice_pwd) {
195 content_name_ = content_name;
196 component_ = component;
197 ice_ufrag_ = ice_ufrag;
198 ice_pwd_ = ice_pwd;
199 UpdateIceParametersInternal();
200 }
201
deadbeefc55fb302016-05-12 12:51:38 -0700202 uint32_t flags_;
203 uint32_t generation_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700204 std::string content_name_;
205 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700206 std::string ice_ufrag_;
207 std::string ice_pwd_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700208
209 // SetIceParameters is an implementation detail which only PortAllocator
210 // should be able to call.
211 friend class PortAllocator;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212};
213
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700214// Note that this class should only be used on one thread.
215// This includes calling the destructor.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216class PortAllocator : public sigslot::has_slots<> {
217 public:
218 PortAllocator() :
219 flags_(kDefaultPortAllocatorFlags),
220 min_port_(0),
221 max_port_(0),
222 step_delay_(kDefaultStepDelay),
223 allow_tcp_listen_(true),
224 candidate_filter_(CF_ALL) {
225 // This will allow us to have old behavior on non webrtc clients.
226 }
Peter Thatcher73ba7a62015-04-14 09:26:03 -0700227 virtual ~PortAllocator() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700229 // Set STUN and TURN servers to be used in future sessions, and set
230 // candidate pool size, as described in JSEP.
231 //
232 // If the servers are changing and the candidate pool size is nonzero,
233 // existing pooled sessions will be destroyed and new ones created.
234 //
235 // If the servers are not changing but the candidate pool size is,
236 // pooled sessions will be either created or destroyed as necessary.
237 void SetConfiguration(const ServerAddresses& stun_servers,
238 const std::vector<RelayServerConfig>& turn_servers,
239 int candidate_pool_size);
240
241 const ServerAddresses& stun_servers() const { return stun_servers_; }
242
243 const std::vector<RelayServerConfig>& turn_servers() const {
244 return turn_servers_;
245 }
246
247 int candidate_pool_size() const { return target_pooled_session_count_; }
deadbeef653b8e02015-11-11 12:55:10 -0800248
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800249 // Sets the network types to ignore.
250 // Values are defined by the AdapterType enum.
251 // For instance, calling this with
252 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
253 // loopback interfaces.
254 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0;
255
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700256 std::unique_ptr<PortAllocatorSession> CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257 const std::string& sid,
258 const std::string& content_name,
259 int component,
260 const std::string& ice_ufrag,
261 const std::string& ice_pwd);
262
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700263 // Get an available pooled session and set the transport information on it.
264 //
265 // Caller takes ownership of the returned session.
266 //
267 // If no pooled sessions are available, returns null.
268 std::unique_ptr<PortAllocatorSession> TakePooledSession(
269 const std::string& content_name,
270 int component,
271 const std::string& ice_ufrag,
272 const std::string& ice_pwd);
273
274 // Returns the next session that would be returned by TakePooledSession.
275 const PortAllocatorSession* GetPooledSession() const;
276
Peter Boström0c4e06b2015-10-07 12:23:21 +0200277 uint32_t flags() const { return flags_; }
278 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000279
280 const std::string& user_agent() const { return agent_; }
281 const rtc::ProxyInfo& proxy() const { return proxy_; }
282 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
283 agent_ = agent;
284 proxy_ = proxy;
285 }
286
287 // Gets/Sets the port range to use when choosing client ports.
288 int min_port() const { return min_port_; }
289 int max_port() const { return max_port_; }
290 bool SetPortRange(int min_port, int max_port) {
291 if (min_port > max_port) {
292 return false;
293 }
294
295 min_port_ = min_port;
296 max_port_ = max_port;
297 return true;
298 }
299
Peter Boström0c4e06b2015-10-07 12:23:21 +0200300 uint32_t step_delay() const { return step_delay_; }
301 void set_step_delay(uint32_t delay) { step_delay_ = delay; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000302
303 bool allow_tcp_listen() const { return allow_tcp_listen_; }
304 void set_allow_tcp_listen(bool allow_tcp_listen) {
305 allow_tcp_listen_ = allow_tcp_listen;
306 }
307
Peter Boström0c4e06b2015-10-07 12:23:21 +0200308 uint32_t candidate_filter() { return candidate_filter_; }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700309 void set_candidate_filter(uint32_t filter) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000310 candidate_filter_ = filter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000311 }
312
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000313 // Gets/Sets the Origin value used for WebRTC STUN requests.
314 const std::string& origin() const { return origin_; }
315 void set_origin(const std::string& origin) { origin_ = origin; }
316
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000317 protected:
318 virtual PortAllocatorSession* CreateSessionInternal(
319 const std::string& content_name,
320 int component,
321 const std::string& ice_ufrag,
322 const std::string& ice_pwd) = 0;
323
Peter Boström0c4e06b2015-10-07 12:23:21 +0200324 uint32_t flags_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000325 std::string agent_;
326 rtc::ProxyInfo proxy_;
327 int min_port_;
328 int max_port_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200329 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000330 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200331 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000332 std::string origin_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700333
334 private:
335 ServerAddresses stun_servers_;
336 std::vector<RelayServerConfig> turn_servers_;
337 // The last size passed into SetConfiguration.
338 int target_pooled_session_count_ = 0;
339 // This variable represents the total number of pooled sessions
340 // both owned by this class and taken by TakePooledSession.
341 int allocated_pooled_session_count_ = 0;
342 std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000343};
344
345} // namespace cricket
346
347#endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_