blob: 65aab44c62a5ac5d7b39879b2a0e7aecfb374ed0 [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,
36 PORTALLOCATOR_ENABLE_BUNDLE = 0x20,
37 PORTALLOCATOR_ENABLE_IPV6 = 0x40,
38 PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80,
39 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100,
40 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200,
41};
42
43const uint32 kDefaultPortAllocatorFlags = 0;
44
45const uint32 kDefaultStepDelay = 1000; // 1 sec step delay.
46// As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain
47// internal. Less than 20ms is not acceptable. We choose 50ms as our default.
48const uint32 kMinimumStepDelay = 50;
49
50// CF = CANDIDATE FILTER
51enum {
52 CF_NONE = 0x0,
53 CF_HOST = 0x1,
54 CF_REFLEXIVE = 0x2,
55 CF_RELAY = 0x4,
56 CF_ALL = 0x7,
57};
58
59class PortAllocatorSessionMuxer;
60
61class PortAllocatorSession : public sigslot::has_slots<> {
62 public:
63 // Content name passed in mostly for logging and debugging.
64 // TODO(mallinath) - Change username and password to ice_ufrag and ice_pwd.
65 PortAllocatorSession(const std::string& content_name,
66 int component,
67 const std::string& username,
68 const std::string& password,
69 uint32 flags);
70
71 // Subclasses should clean up any ports created.
72 virtual ~PortAllocatorSession() {}
73
74 uint32 flags() const { return flags_; }
75 void set_flags(uint32 flags) { flags_ = flags; }
76 std::string content_name() const { return content_name_; }
77 int component() const { return component_; }
78
79 // Starts gathering STUN and Relay configurations.
80 virtual void StartGettingPorts() = 0;
81 virtual void StopGettingPorts() = 0;
82 virtual bool IsGettingPorts() = 0;
83
84 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
85 sigslot::signal2<PortAllocatorSession*,
86 const std::vector<Candidate>&> SignalCandidatesReady;
87 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
88
89 virtual uint32 generation() { return generation_; }
90 virtual void set_generation(uint32 generation) { generation_ = generation; }
91 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
92
93 protected:
94 const std::string& username() const { return username_; }
95 const std::string& password() const { return password_; }
96
97 std::string content_name_;
98 int component_;
99
100 private:
101 uint32 flags_;
102 uint32 generation_;
103 std::string username_;
104 std::string password_;
105};
106
107class PortAllocator : public sigslot::has_slots<> {
108 public:
109 PortAllocator() :
110 flags_(kDefaultPortAllocatorFlags),
111 min_port_(0),
112 max_port_(0),
113 step_delay_(kDefaultStepDelay),
114 allow_tcp_listen_(true),
115 candidate_filter_(CF_ALL) {
116 // This will allow us to have old behavior on non webrtc clients.
117 }
118 virtual ~PortAllocator();
119
120 PortAllocatorSession* CreateSession(
121 const std::string& sid,
122 const std::string& content_name,
123 int component,
124 const std::string& ice_ufrag,
125 const std::string& ice_pwd);
126
127 PortAllocatorSessionMuxer* GetSessionMuxer(const std::string& key) const;
128 void OnSessionMuxerDestroyed(PortAllocatorSessionMuxer* session);
129
130 uint32 flags() const { return flags_; }
131 void set_flags(uint32 flags) { flags_ = flags; }
132
133 const std::string& user_agent() const { return agent_; }
134 const rtc::ProxyInfo& proxy() const { return proxy_; }
135 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) {
136 agent_ = agent;
137 proxy_ = proxy;
138 }
139
140 // Gets/Sets the port range to use when choosing client ports.
141 int min_port() const { return min_port_; }
142 int max_port() const { return max_port_; }
143 bool SetPortRange(int min_port, int max_port) {
144 if (min_port > max_port) {
145 return false;
146 }
147
148 min_port_ = min_port;
149 max_port_ = max_port;
150 return true;
151 }
152
153 uint32 step_delay() const { return step_delay_; }
154 void set_step_delay(uint32 delay) {
155 step_delay_ = delay;
156 }
157
158 bool allow_tcp_listen() const { return allow_tcp_listen_; }
159 void set_allow_tcp_listen(bool allow_tcp_listen) {
160 allow_tcp_listen_ = allow_tcp_listen;
161 }
162
163 uint32 candidate_filter() { return candidate_filter_; }
164 bool set_candidate_filter(uint32 filter) {
165 // TODO(mallinath) - Do transition check?
166 candidate_filter_ = filter;
167 return true;
168 }
169
170 protected:
171 virtual PortAllocatorSession* CreateSessionInternal(
172 const std::string& content_name,
173 int component,
174 const std::string& ice_ufrag,
175 const std::string& ice_pwd) = 0;
176
177 typedef std::map<std::string, PortAllocatorSessionMuxer*> SessionMuxerMap;
178
179 uint32 flags_;
180 std::string agent_;
181 rtc::ProxyInfo proxy_;
182 int min_port_;
183 int max_port_;
184 uint32 step_delay_;
185 SessionMuxerMap muxers_;
186 bool allow_tcp_listen_;
187 uint32 candidate_filter_;
188};
189
190} // namespace cricket
191
192#endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_