blob: 38650a52a14341e1af6fd65969b052d37bac8447 [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
17#include "webrtc/p2p/base/portinterface.h"
18#include "webrtc/base/helpers.h"
19#include "webrtc/base/proxyinfo.h"
20#include "webrtc/base/sigslot.h"
21
22namespace cricket {
23
24// PortAllocator is responsible for allocating Port types for a given
25// P2PSocket. It also handles port freeing.
26//
27// Clients can override this class to control port allocation, including
28// what kinds of ports are allocated.
29
30enum {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070031 // Disable local UDP ports. This doesn't impact how we connect to relay
32 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000033 PORTALLOCATOR_DISABLE_UDP = 0x01,
34 PORTALLOCATOR_DISABLE_STUN = 0x02,
35 PORTALLOCATOR_DISABLE_RELAY = 0x04,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070036 // Disable local TCP ports. This doesn't impact how we connect to relay
37 // servers.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038 PORTALLOCATOR_DISABLE_TCP = 0x08,
39 PORTALLOCATOR_ENABLE_SHAKER = 0x10,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000040 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
Peter Thatcher7cbd1882015-09-17 18:54:52 -070041 // TODO(pthatcher): Remove this once it's no longer used in:
42 // remoting/client/plugin/pepper_port_allocator.cc
43 // remoting/protocol/chromium_port_allocator.cc
44 // remoting/test/fake_port_allocator.cc
45 // It's a no-op and is no longer needed.
pthatcherfa301802015-08-11 04:12:56 -070046 PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
48 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080049 // When specified, we'll only allocate the STUN candidate for the public
50 // interface as seen by regular http traffic and the HOST candidate associated
51 // with the default local interface.
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000052 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080053 // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the
54 // default local candidate mentioned above will not be allocated. Only the
55 // STUN candidate will be.
56 PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070057 // Disallow use of UDP when connecting to a relay server. Since proxy servers
58 // usually don't handle UDP, using UDP will leak the IP address.
59 PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060};
61
Peter Boström0c4e06b2015-10-07 12:23:21 +020062const uint32_t kDefaultPortAllocatorFlags = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063
Peter Boström0c4e06b2015-10-07 12:23:21 +020064const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
66// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
Peter Boström0c4e06b2015-10-07 12:23:21 +020067const uint32_t kMinimumStepDelay = 50;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068
69// CF = CANDIDATE FILTER
70enum {
71 CF_NONE = 0x0,
72 CF_HOST = 0x1,
73 CF_REFLEXIVE = 0x2,
74 CF_RELAY = 0x4,
75 CF_ALL = 0x7,
76};
77
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078class PortAllocatorSession : public sigslot::has_slots<> {
79 public:
80 // Content name passed in mostly for logging and debugging.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000081 PortAllocatorSession(const std::string& content_name,
82 int component,
deadbeefcbecd352015-09-23 11:50:27 -070083 const std::string& ice_ufrag,
84 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 uint32_t flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000086
87 // Subclasses should clean up any ports created.
88 virtual ~PortAllocatorSession() {}
89
Peter Boström0c4e06b2015-10-07 12:23:21 +020090 uint32_t flags() const { return flags_; }
91 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092 std::string content_name() const { return content_name_; }
93 int component() const { return component_; }
94
95 // Starts gathering STUN and Relay configurations.
96 virtual void StartGettingPorts() = 0;
97 virtual void StopGettingPorts() = 0;
honghaiz98db68f2015-09-29 07:58:17 -070098 // Only stop the existing gathering process but may start new ones if needed.
99 virtual void ClearGettingPorts() = 0;
100 // Whether the process of getting ports has been stopped.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101 virtual bool IsGettingPorts() = 0;
102
103 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
104 sigslot::signal2<PortAllocatorSession*,
105 const std::vector<Candidate>&> SignalCandidatesReady;
106 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
107
Peter Boström0c4e06b2015-10-07 12:23:21 +0200108 virtual uint32_t generation() { return generation_; }
109 virtual void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000110 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
111
deadbeefcbecd352015-09-23 11:50:27 -0700112 const std::string& ice_ufrag() const { return ice_ufrag_; }
113 const std::string& ice_pwd() const { return ice_pwd_; }
114
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 protected:
deadbeefcbecd352015-09-23 11:50:27 -0700116 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
117 // ice_pwd.
118 const std::string& username() const { return ice_ufrag_; }
119 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120
121 std::string content_name_;
122 int component_;
123
124 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200125 uint32_t flags_;
126 uint32_t generation_;
deadbeefcbecd352015-09-23 11:50:27 -0700127 std::string ice_ufrag_;
128 std::string ice_pwd_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129};
130
131class PortAllocator : public sigslot::has_slots<> {
132 public:
133 PortAllocator() :
134 flags_(kDefaultPortAllocatorFlags),
135 min_port_(0),
136 max_port_(0),
137 step_delay_(kDefaultStepDelay),
138 allow_tcp_listen_(true),
139 candidate_filter_(CF_ALL) {
140 // This will allow us to have old behavior on non webrtc clients.
141 }
Peter Thatcher73ba7a62015-04-14 09:26:03 -0700142 virtual ~PortAllocator() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143
144 PortAllocatorSession* CreateSession(
145 const std::string& sid,
146 const std::string& content_name,
147 int component,
148 const std::string& ice_ufrag,
149 const std::string& ice_pwd);
150
Peter Boström0c4e06b2015-10-07 12:23:21 +0200151 uint32_t flags() const { return flags_; }
152 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153
154 const std::string& user_agent() const { return agent_; }
155 const rtc::ProxyInfo& proxy() const { return proxy_; }
156 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
157 agent_ = agent;
158 proxy_ = proxy;
159 }
160
161 // Gets/Sets the port range to use when choosing client ports.
162 int min_port() const { return min_port_; }
163 int max_port() const { return max_port_; }
164 bool SetPortRange(int min_port, int max_port) {
165 if (min_port > max_port) {
166 return false;
167 }
168
169 min_port_ = min_port;
170 max_port_ = max_port;
171 return true;
172 }
173
Peter Boström0c4e06b2015-10-07 12:23:21 +0200174 uint32_t step_delay() const { return step_delay_; }
175 void set_step_delay(uint32_t delay) { step_delay_ = delay; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176
177 bool allow_tcp_listen() const { return allow_tcp_listen_; }
178 void set_allow_tcp_listen(bool allow_tcp_listen) {
179 allow_tcp_listen_ = allow_tcp_listen;
180 }
181
Peter Boström0c4e06b2015-10-07 12:23:21 +0200182 uint32_t candidate_filter() { return candidate_filter_; }
183 bool set_candidate_filter(uint32_t filter) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184 // TODO(mallinath) - Do transition check?
185 candidate_filter_ = filter;
186 return true;
187 }
188
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000189 // Gets/Sets the Origin value used for WebRTC STUN requests.
190 const std::string& origin() const { return origin_; }
191 void set_origin(const std::string& origin) { origin_ = origin; }
192
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193 protected:
194 virtual PortAllocatorSession* CreateSessionInternal(
195 const std::string& content_name,
196 int component,
197 const std::string& ice_ufrag,
198 const std::string& ice_pwd) = 0;
199
Peter Boström0c4e06b2015-10-07 12:23:21 +0200200 uint32_t flags_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 std::string agent_;
202 rtc::ProxyInfo proxy_;
203 int min_port_;
204 int max_port_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200205 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200207 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000208 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209};
210
211} // namespace cricket
212
213#endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_