blob: 6fb79b065e62f1ee1f8f33d5023569bca5e93108 [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
14#include <string>
15#include <vector>
16
deadbeef653b8e02015-11-11 12:55:10 -080017#include "webrtc/p2p/base/port.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000018#include "webrtc/p2p/base/portinterface.h"
19#include "webrtc/base/helpers.h"
20#include "webrtc/base/proxyinfo.h"
21#include "webrtc/base/sigslot.h"
22
23namespace cricket {
24
25// PortAllocator is responsible for allocating Port types for a given
26// P2PSocket. It also handles port freeing.
27//
28// Clients can override this class to control port allocation, including
29// what kinds of ports are allocated.
30
31enum {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070032 // Disable local UDP ports. This doesn't impact how we connect to relay
33 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000034 PORTALLOCATOR_DISABLE_UDP = 0x01,
35 PORTALLOCATOR_DISABLE_STUN = 0x02,
36 PORTALLOCATOR_DISABLE_RELAY = 0x04,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070037 // Disable local TCP ports. This doesn't impact how we connect to relay
38 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039 PORTALLOCATOR_DISABLE_TCP = 0x08,
40 PORTALLOCATOR_ENABLE_SHAKER = 0x10,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000041 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
Peter Thatcher7cbd1882015-09-17 18:54:52 -070042 // TODO(pthatcher): Remove this once it's no longer used in:
43 // remoting/client/plugin/pepper_port_allocator.cc
44 // remoting/protocol/chromium_port_allocator.cc
45 // remoting/test/fake_port_allocator.cc
46 // It's a no-op and is no longer needed.
pthatcherfa301802015-08-11 04:12:56 -070047 PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
49 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080050 // When specified, we'll only allocate the STUN candidate for the public
51 // interface as seen by regular http traffic and the HOST candidate associated
52 // with the default local interface.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000053 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080054 // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the
55 // default local candidate mentioned above will not be allocated. Only the
56 // STUN candidate will be.
57 PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070058 // Disallow use of UDP when connecting to a relay server. Since proxy servers
59 // usually don't handle UDP, using UDP will leak the IP address.
60 PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061};
62
Peter Boström0c4e06b2015-10-07 12:23:21 +020063const uint32_t kDefaultPortAllocatorFlags = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064
Peter Boström0c4e06b2015-10-07 12:23:21 +020065const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
67// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
Peter Boström0c4e06b2015-10-07 12:23:21 +020068const uint32_t kMinimumStepDelay = 50;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069
70// CF = CANDIDATE FILTER
71enum {
72 CF_NONE = 0x0,
73 CF_HOST = 0x1,
74 CF_REFLEXIVE = 0x2,
75 CF_RELAY = 0x4,
76 CF_ALL = 0x7,
77};
78
deadbeef653b8e02015-11-11 12:55:10 -080079// TODO(deadbeef): Rename to TurnCredentials (and username to ufrag).
80struct RelayCredentials {
81 RelayCredentials() {}
82 RelayCredentials(const std::string& username, const std::string& password)
83 : username(username), password(password) {}
84
85 std::string username;
86 std::string password;
87};
88
89typedef std::vector<ProtocolAddress> PortList;
90// TODO(deadbeef): Rename to TurnServerConfig.
91struct RelayServerConfig {
deadbeefc55fb302016-05-12 12:51:38 -070092 RelayServerConfig(RelayType type) : type(type), priority(0) {}
deadbeef653b8e02015-11-11 12:55:10 -080093
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080094 RelayServerConfig(const std::string& address,
95 int port,
96 const std::string& username,
97 const std::string& password,
98 ProtocolType proto,
99 bool secure)
100 : type(RELAY_TURN), credentials(username, password) {
101 ports.push_back(
102 ProtocolAddress(rtc::SocketAddress(address, port), proto, secure));
103 }
104
deadbeef653b8e02015-11-11 12:55:10 -0800105 RelayType type;
106 PortList ports;
107 RelayCredentials credentials;
deadbeefc55fb302016-05-12 12:51:38 -0700108 int priority;
deadbeef653b8e02015-11-11 12:55:10 -0800109};
110
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111class PortAllocatorSession : public sigslot::has_slots<> {
112 public:
113 // Content name passed in mostly for logging and debugging.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 PortAllocatorSession(const std::string& content_name,
115 int component,
deadbeefcbecd352015-09-23 11:50:27 -0700116 const std::string& ice_ufrag,
117 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200118 uint32_t flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119
120 // Subclasses should clean up any ports created.
121 virtual ~PortAllocatorSession() {}
122
Peter Boström0c4e06b2015-10-07 12:23:21 +0200123 uint32_t flags() const { return flags_; }
124 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125 std::string content_name() const { return content_name_; }
126 int component() const { return component_; }
127
128 // Starts gathering STUN and Relay configurations.
129 virtual void StartGettingPorts() = 0;
130 virtual void StopGettingPorts() = 0;
honghaiz98db68f2015-09-29 07:58:17 -0700131 // Only stop the existing gathering process but may start new ones if needed.
132 virtual void ClearGettingPorts() = 0;
133 // Whether the process of getting ports has been stopped.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134 virtual bool IsGettingPorts() = 0;
135
136 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
137 sigslot::signal2<PortAllocatorSession*,
138 const std::vector<Candidate>&> SignalCandidatesReady;
139 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
140
Peter Boström0c4e06b2015-10-07 12:23:21 +0200141 virtual uint32_t generation() { return generation_; }
142 virtual void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
144
deadbeefc55fb302016-05-12 12:51:38 -0700145 const std::string& ice_ufrag() const { return ice_ufrag_; }
146 const std::string& ice_pwd() const { return ice_pwd_; }
Taylor Brandstetter48e9d052016-05-12 10:19:38 -0700147
deadbeefc55fb302016-05-12 12:51:38 -0700148 protected:
deadbeefcbecd352015-09-23 11:50:27 -0700149 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
150 // ice_pwd.
151 const std::string& username() const { return ice_ufrag_; }
152 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153
Taylor Brandstetter48e9d052016-05-12 10:19:38 -0700154 std::string content_name_;
155 int component_;
deadbeefc55fb302016-05-12 12:51:38 -0700156
157 private:
158 uint32_t flags_;
159 uint32_t generation_;
deadbeefcbecd352015-09-23 11:50:27 -0700160 std::string ice_ufrag_;
161 std::string ice_pwd_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000162};
163
164class PortAllocator : public sigslot::has_slots<> {
165 public:
166 PortAllocator() :
167 flags_(kDefaultPortAllocatorFlags),
168 min_port_(0),
169 max_port_(0),
170 step_delay_(kDefaultStepDelay),
171 allow_tcp_listen_(true),
172 candidate_filter_(CF_ALL) {
173 // This will allow us to have old behavior on non webrtc clients.
174 }
Peter Thatcher73ba7a62015-04-14 09:26:03 -0700175 virtual ~PortAllocator() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176
deadbeefc55fb302016-05-12 12:51:38 -0700177 // Set STUN and TURN servers to be used in future sessions.
178 virtual void SetIceServers(
179 const ServerAddresses& stun_servers,
180 const std::vector<RelayServerConfig>& turn_servers) = 0;
deadbeef653b8e02015-11-11 12:55:10 -0800181
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800182 // Sets the network types to ignore.
183 // Values are defined by the AdapterType enum.
184 // For instance, calling this with
185 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
186 // loopback interfaces.
187 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0;
188
deadbeefc55fb302016-05-12 12:51:38 -0700189 PortAllocatorSession* CreateSession(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 const std::string& sid,
191 const std::string& content_name,
192 int component,
193 const std::string& ice_ufrag,
194 const std::string& ice_pwd);
195
Peter Boström0c4e06b2015-10-07 12:23:21 +0200196 uint32_t flags() const { return flags_; }
197 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198
199 const std::string& user_agent() const { return agent_; }
200 const rtc::ProxyInfo& proxy() const { return proxy_; }
201 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
202 agent_ = agent;
203 proxy_ = proxy;
204 }
205
206 // Gets/Sets the port range to use when choosing client ports.
207 int min_port() const { return min_port_; }
208 int max_port() const { return max_port_; }
209 bool SetPortRange(int min_port, int max_port) {
210 if (min_port > max_port) {
211 return false;
212 }
213
214 min_port_ = min_port;
215 max_port_ = max_port;
216 return true;
217 }
218
Peter Boström0c4e06b2015-10-07 12:23:21 +0200219 uint32_t step_delay() const { return step_delay_; }
220 void set_step_delay(uint32_t delay) { step_delay_ = delay; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221
222 bool allow_tcp_listen() const { return allow_tcp_listen_; }
223 void set_allow_tcp_listen(bool allow_tcp_listen) {
224 allow_tcp_listen_ = allow_tcp_listen;
225 }
226
Peter Boström0c4e06b2015-10-07 12:23:21 +0200227 uint32_t candidate_filter() { return candidate_filter_; }
deadbeefc55fb302016-05-12 12:51:38 -0700228 bool set_candidate_filter(uint32_t filter) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 // TODO(mallinath) - Do transition check?
230 candidate_filter_ = filter;
deadbeefc55fb302016-05-12 12:51:38 -0700231 return true;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000232 }
233
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000234 // Gets/Sets the Origin value used for WebRTC STUN requests.
235 const std::string& origin() const { return origin_; }
236 void set_origin(const std::string& origin) { origin_ = origin; }
237
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000238 protected:
239 virtual PortAllocatorSession* CreateSessionInternal(
240 const std::string& content_name,
241 int component,
242 const std::string& ice_ufrag,
243 const std::string& ice_pwd) = 0;
244
Peter Boström0c4e06b2015-10-07 12:23:21 +0200245 uint32_t flags_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000246 std::string agent_;
247 rtc::ProxyInfo proxy_;
248 int min_port_;
249 int max_port_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200250 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200252 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000253 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000254};
255
256} // namespace cricket
257
258#endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_