blob: c89684727b72c475df46f770fe3845934d70dbcb [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 {
31 PORTALLOCATOR_DISABLE_UDP = 0x01,
32 PORTALLOCATOR_DISABLE_STUN = 0x02,
33 PORTALLOCATOR_DISABLE_RELAY = 0x04,
34 PORTALLOCATOR_DISABLE_TCP = 0x08,
35 PORTALLOCATOR_ENABLE_SHAKER = 0x10,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
Peter Thatcher081f34b2015-08-19 20:37:54 -070037 // TODO(pthatcher): Remove this once it's no longer used in:
38 // remoting/client/plugin/pepper_port_allocator.cc
39 // remoting/protocol/chromium_port_allocator.cc
40 // remoting/test/fake_port_allocator.cc
41 // It's a no-op and is no longer needed.
pthatcherfa301802015-08-11 04:12:56 -070042 PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
44 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000045 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070046 // When specified, a loopback candidate will be generated if
47 // PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified.
48 PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE = 0x800,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049};
50
51const uint32 kDefaultPortAllocatorFlags = 0;
52
53const uint32 kDefaultStepDelay = 1000; // 1 sec step delay.
54// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
55// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
56const uint32 kMinimumStepDelay = 50;
57
58// CF = CANDIDATE FILTER
59enum {
60 CF_NONE = 0x0,
61 CF_HOST = 0x1,
62 CF_REFLEXIVE = 0x2,
63 CF_RELAY = 0x4,
64 CF_ALL = 0x7,
65};
66
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000067class PortAllocatorSession : public sigslot::has_slots<> {
68 public:
69 // Content name passed in mostly for logging and debugging.
70 // TODO(mallinath) - Change username and password to ice_ufrag and ice_pwd.
71 PortAllocatorSession(const std::string& content_name,
72 int component,
73 const std::string& username,
74 const std::string& password,
75 uint32 flags);
76
77 // Subclasses should clean up any ports created.
78 virtual ~PortAllocatorSession() {}
79
80 uint32 flags() const { return flags_; }
81 void set_flags(uint32 flags) { flags_ = flags; }
82 std::string content_name() const { return content_name_; }
83 int component() const { return component_; }
84
85 // Starts gathering STUN and Relay configurations.
86 virtual void StartGettingPorts() = 0;
87 virtual void StopGettingPorts() = 0;
88 virtual bool IsGettingPorts() = 0;
89
90 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
91 sigslot::signal2<PortAllocatorSession*,
92 const std::vector<Candidate>&> SignalCandidatesReady;
93 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
94
95 virtual uint32 generation() { return generation_; }
96 virtual void set_generation(uint32 generation) { generation_ = generation; }
97 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
98
99 protected:
100 const std::string& username() const { return username_; }
101 const std::string& password() const { return password_; }
102
103 std::string content_name_;
104 int component_;
105
106 private:
107 uint32 flags_;
108 uint32 generation_;
109 std::string username_;
110 std::string password_;
111};
112
113class PortAllocator : public sigslot::has_slots<> {
114 public:
115 PortAllocator() :
116 flags_(kDefaultPortAllocatorFlags),
117 min_port_(0),
118 max_port_(0),
119 step_delay_(kDefaultStepDelay),
120 allow_tcp_listen_(true),
121 candidate_filter_(CF_ALL) {
122 // This will allow us to have old behavior on non webrtc clients.
123 }
Peter Thatcher73ba7a62015-04-14 09:26:03 -0700124 virtual ~PortAllocator() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125
126 PortAllocatorSession* CreateSession(
127 const std::string& sid,
128 const std::string& content_name,
129 int component,
130 const std::string& ice_ufrag,
131 const std::string& ice_pwd);
132
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133 uint32 flags() const { return flags_; }
134 void set_flags(uint32 flags) { flags_ = flags; }
135
136 const std::string& user_agent() const { return agent_; }
137 const rtc::ProxyInfo& proxy() const { return proxy_; }
138 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
139 agent_ = agent;
140 proxy_ = proxy;
141 }
142
143 // Gets/Sets the port range to use when choosing client ports.
144 int min_port() const { return min_port_; }
145 int max_port() const { return max_port_; }
146 bool SetPortRange(int min_port, int max_port) {
147 if (min_port > max_port) {
148 return false;
149 }
150
151 min_port_ = min_port;
152 max_port_ = max_port;
153 return true;
154 }
155
156 uint32 step_delay() const { return step_delay_; }
157 void set_step_delay(uint32 delay) {
158 step_delay_ = delay;
159 }
160
161 bool allow_tcp_listen() const { return allow_tcp_listen_; }
162 void set_allow_tcp_listen(bool allow_tcp_listen) {
163 allow_tcp_listen_ = allow_tcp_listen;
164 }
165
166 uint32 candidate_filter() { return candidate_filter_; }
167 bool set_candidate_filter(uint32 filter) {
168 // TODO(mallinath) - Do transition check?
169 candidate_filter_ = filter;
170 return true;
171 }
172
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000173 // Gets/Sets the Origin value used for WebRTC STUN requests.
174 const std::string& origin() const { return origin_; }
175 void set_origin(const std::string& origin) { origin_ = origin; }
176
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 protected:
178 virtual PortAllocatorSession* CreateSessionInternal(
179 const std::string& content_name,
180 int component,
181 const std::string& ice_ufrag,
182 const std::string& ice_pwd) = 0;
183
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184 uint32 flags_;
185 std::string agent_;
186 rtc::ProxyInfo proxy_;
187 int min_port_;
188 int max_port_;
189 uint32 step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 bool allow_tcp_listen_;
191 uint32 candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000192 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193};
194
195} // namespace cricket
196
197#endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_