blob: b754376d89af3ae6d4eb4af2d565dcb6f47db169 [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
58const uint32 kDefaultPortAllocatorFlags = 0;
59
60const uint32 kDefaultStepDelay = 1000; // 1 sec step delay.
61// 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.
63const uint32 kMinimumStepDelay = 50;
64
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.
77 // TODO(mallinath) - Change username and password to ice_ufrag and ice_pwd.
78 PortAllocatorSession(const std::string& content_name,
79 int component,
80 const std::string& username,
81 const std::string& password,
82 uint32 flags);
83
84 // Subclasses should clean up any ports created.
85 virtual ~PortAllocatorSession() {}
86
87 uint32 flags() const { return flags_; }
88 void set_flags(uint32 flags) { flags_ = flags; }
89 std::string content_name() const { return content_name_; }
90 int component() const { return component_; }
91
92 // Starts gathering STUN and Relay configurations.
93 virtual void StartGettingPorts() = 0;
94 virtual void StopGettingPorts() = 0;
95 virtual bool IsGettingPorts() = 0;
96
97 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
98 sigslot::signal2<PortAllocatorSession*,
99 const std::vector<Candidate>&> SignalCandidatesReady;
100 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
101
102 virtual uint32 generation() { return generation_; }
103 virtual void set_generation(uint32 generation) { generation_ = generation; }
104 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
105
106 protected:
107 const std::string& username() const { return username_; }
108 const std::string& password() const { return password_; }
109
110 std::string content_name_;
111 int component_;
112
113 private:
114 uint32 flags_;
115 uint32 generation_;
116 std::string username_;
117 std::string password_;
118};
119
120class PortAllocator : public sigslot::has_slots<> {
121 public:
122 PortAllocator() :
123 flags_(kDefaultPortAllocatorFlags),
124 min_port_(0),
125 max_port_(0),
126 step_delay_(kDefaultStepDelay),
127 allow_tcp_listen_(true),
128 candidate_filter_(CF_ALL) {
129 // This will allow us to have old behavior on non webrtc clients.
130 }
Peter Thatcher73ba7a62015-04-14 09:26:03 -0700131 virtual ~PortAllocator() {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132
133 PortAllocatorSession* CreateSession(
134 const std::string& sid,
135 const std::string& content_name,
136 int component,
137 const std::string& ice_ufrag,
138 const std::string& ice_pwd);
139
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 uint32 flags() const { return flags_; }
141 void set_flags(uint32 flags) { flags_ = flags; }
142
143 const std::string& user_agent() const { return agent_; }
144 const rtc::ProxyInfo& proxy() const { return proxy_; }
145 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
146 agent_ = agent;
147 proxy_ = proxy;
148 }
149
150 // Gets/Sets the port range to use when choosing client ports.
151 int min_port() const { return min_port_; }
152 int max_port() const { return max_port_; }
153 bool SetPortRange(int min_port, int max_port) {
154 if (min_port > max_port) {
155 return false;
156 }
157
158 min_port_ = min_port;
159 max_port_ = max_port;
160 return true;
161 }
162
163 uint32 step_delay() const { return step_delay_; }
164 void set_step_delay(uint32 delay) {
165 step_delay_ = delay;
166 }
167
168 bool allow_tcp_listen() const { return allow_tcp_listen_; }
169 void set_allow_tcp_listen(bool allow_tcp_listen) {
170 allow_tcp_listen_ = allow_tcp_listen;
171 }
172
173 uint32 candidate_filter() { return candidate_filter_; }
174 bool set_candidate_filter(uint32 filter) {
175 // TODO(mallinath) - Do transition check?
176 candidate_filter_ = filter;
177 return true;
178 }
179
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000180 // Gets/Sets the Origin value used for WebRTC STUN requests.
181 const std::string& origin() const { return origin_; }
182 void set_origin(const std::string& origin) { origin_ = origin; }
183
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184 protected:
185 virtual PortAllocatorSession* CreateSessionInternal(
186 const std::string& content_name,
187 int component,
188 const std::string& ice_ufrag,
189 const std::string& ice_pwd) = 0;
190
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 uint32 flags_;
192 std::string agent_;
193 rtc::ProxyInfo proxy_;
194 int min_port_;
195 int max_port_;
196 uint32 step_delay_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197 bool allow_tcp_listen_;
198 uint32 candidate_filter_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000199 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200};
201
202} // namespace cricket
203
204#endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_