blob: 4f8ec2fbe69c2d7a88594df48fe9873cbdaf440a [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,
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000049 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070050 // When specified, a loopback candidate will be generated if
51 // PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified.
52 PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE = 0x800,
Guo-wei Shieh13d35f62015-08-26 15:32:56 -070053 // Disallow use of UDP when connecting to a relay server. Since proxy servers
54 // usually don't handle UDP, using UDP will leak the IP address.
55 PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000056};
57
Peter Boström0c4e06b2015-10-07 12:23:21 +020058const uint32_t kDefaultPortAllocatorFlags = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059
Peter Boström0c4e06b2015-10-07 12:23:21 +020060const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
62// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
Peter Boström0c4e06b2015-10-07 12:23:21 +020063const uint32_t kMinimumStepDelay = 50;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064
65// CF = CANDIDATE FILTER
66enum {
67 CF_NONE = 0x0,
68 CF_HOST = 0x1,
69 CF_REFLEXIVE = 0x2,
70 CF_RELAY = 0x4,
71 CF_ALL = 0x7,
72};
73
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074class PortAllocatorSession : public sigslot::has_slots<> {
75 public:
76 // Content name passed in mostly for logging and debugging.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077 PortAllocatorSession(const std::string& content_name,
78 int component,
deadbeefcbecd352015-09-23 11:50:27 -070079 const std::string& ice_ufrag,
80 const std::string& ice_pwd,
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 uint32_t flags);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082
83 // Subclasses should clean up any ports created.
84 virtual ~PortAllocatorSession() {}
85
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 uint32_t flags() const { return flags_; }
87 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000088 std::string content_name() const { return content_name_; }
89 int component() const { return component_; }
90
91 // Starts gathering STUN and Relay configurations.
92 virtual void StartGettingPorts() = 0;
93 virtual void StopGettingPorts() = 0;
honghaiz98db68f2015-09-29 07:58:17 -070094 // Only stop the existing gathering process but may start new ones if needed.
95 virtual void ClearGettingPorts() = 0;
96 // Whether the process of getting ports has been stopped.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097 virtual bool IsGettingPorts() = 0;
98
99 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
100 sigslot::signal2<PortAllocatorSession*,
101 const std::vector<Candidate>&> SignalCandidatesReady;
102 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
103
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104 virtual uint32_t generation() { return generation_; }
105 virtual void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
107
deadbeefcbecd352015-09-23 11:50:27 -0700108 const std::string& ice_ufrag() const { return ice_ufrag_; }
109 const std::string& ice_pwd() const { return ice_pwd_; }
110
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111 protected:
deadbeefcbecd352015-09-23 11:50:27 -0700112 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and
113 // ice_pwd.
114 const std::string& username() const { return ice_ufrag_; }
115 const std::string& password() const { return ice_pwd_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116
117 std::string content_name_;
118 int component_;
119
120 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200121 uint32_t flags_;
122 uint32_t generation_;
deadbeefcbecd352015-09-23 11:50:27 -0700123 std::string ice_ufrag_;
124 std::string ice_pwd_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125};
126
127class PortAllocator : public sigslot::has_slots<> {
128 public:
129 PortAllocator() :
130 flags_(kDefaultPortAllocatorFlags),
131 min_port_(0),
132 max_port_(0),
133 step_delay_(kDefaultStepDelay),
134 allow_tcp_listen_(true),
135 candidate_filter_(CF_ALL) {
136 // This will allow us to have old behavior on non webrtc clients.
137 }
Peter Thatcher73ba7a62015-04-14 09:26:03 -0700138 virtual ~PortAllocator() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139
140 PortAllocatorSession* CreateSession(
141 const std::string& sid,
142 const std::string& content_name,
143 int component,
144 const std::string& ice_ufrag,
145 const std::string& ice_pwd);
146
Peter Boström0c4e06b2015-10-07 12:23:21 +0200147 uint32_t flags() const { return flags_; }
148 void set_flags(uint32_t flags) { flags_ = flags; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000149
150 const std::string& user_agent() const { return agent_; }
151 const rtc::ProxyInfo& proxy() const { return proxy_; }
152 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
153 agent_ = agent;
154 proxy_ = proxy;
155 }
156
157 // Gets/Sets the port range to use when choosing client ports.
158 int min_port() const { return min_port_; }
159 int max_port() const { return max_port_; }
160 bool SetPortRange(int min_port, int max_port) {
161 if (min_port > max_port) {
162 return false;
163 }
164
165 min_port_ = min_port;
166 max_port_ = max_port;
167 return true;
168 }
169
Peter Boström0c4e06b2015-10-07 12:23:21 +0200170 uint32_t step_delay() const { return step_delay_; }
171 void set_step_delay(uint32_t delay) { step_delay_ = delay; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172
173 bool allow_tcp_listen() const { return allow_tcp_listen_; }
174 void set_allow_tcp_listen(bool allow_tcp_listen) {
175 allow_tcp_listen_ = allow_tcp_listen;
176 }
177
Peter Boström0c4e06b2015-10-07 12:23:21 +0200178 uint32_t candidate_filter() { return candidate_filter_; }
179 bool set_candidate_filter(uint32_t filter) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180 // TODO(mallinath) - Do transition check?
181 candidate_filter_ = filter;
182 return true;
183 }
184
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000185 // Gets/Sets the Origin value used for WebRTC STUN requests.
186 const std::string& origin() const { return origin_; }
187 void set_origin(const std::string& origin) { origin_ = origin; }
188
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189 protected:
190 virtual PortAllocatorSession* CreateSessionInternal(
191 const std::string& content_name,
192 int component,
193 const std::string& ice_ufrag,
194 const std::string& ice_pwd) = 0;
195
Peter Boström0c4e06b2015-10-07 12:23:21 +0200196 uint32_t flags_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197 std::string agent_;
198 rtc::ProxyInfo proxy_;
199 int min_port_;
200 int max_port_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200201 uint32_t step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 bool allow_tcp_listen_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200203 uint32_t candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000204 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205};
206
207} // namespace cricket
208
209#endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_