2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
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.
11 #ifndef WEBRTC_P2P_BASE_PORTALLOCATOR_H_
12 #define WEBRTC_P2P_BASE_PORTALLOCATOR_H_
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"
24 // PortAllocator is responsible for allocating Port types for a given
25 // P2PSocket. It also handles port freeing.
27 // Clients can override this class to control port allocation, including
28 // what kinds of ports are allocated.
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,
43 const uint32 kDefaultPortAllocatorFlags = 0;
45 const 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.
48 const uint32 kMinimumStepDelay = 50;
50 // CF = CANDIDATE FILTER
59 class PortAllocatorSessionMuxer;
61 class PortAllocatorSession : public sigslot::has_slots<> {
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,
67 const std::string& username,
68 const std::string& password,
71 // Subclasses should clean up any ports created.
72 virtual ~PortAllocatorSession() {}
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_; }
79 // Starts gathering STUN and Relay configurations.
80 virtual void StartGettingPorts() = 0;
81 virtual void StopGettingPorts() = 0;
82 virtual bool IsGettingPorts() = 0;
84 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
85 sigslot::signal2<PortAllocatorSession*,
86 const std::vector<Candidate>&> SignalCandidatesReady;
87 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
89 virtual uint32 generation() { return generation_; }
90 virtual void set_generation(uint32 generation) { generation_ = generation; }
91 sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
94 const std::string& username() const { return username_; }
95 const std::string& password() const { return password_; }
97 std::string content_name_;
103 std::string username_;
104 std::string password_;
107 class PortAllocator : public sigslot::has_slots<> {
110 flags_(kDefaultPortAllocatorFlags),
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.
118 virtual ~PortAllocator();
120 PortAllocatorSession* CreateSession(
121 const std::string& sid,
122 const std::string& content_name,
124 const std::string& ice_ufrag,
125 const std::string& ice_pwd);
127 PortAllocatorSessionMuxer* GetSessionMuxer(const std::string& key) const;
128 void OnSessionMuxerDestroyed(PortAllocatorSessionMuxer* session);
130 uint32 flags() const { return flags_; }
131 void set_flags(uint32 flags) { flags_ = flags; }
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) {
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) {
148 min_port_ = min_port;
149 max_port_ = max_port;
153 uint32 step_delay() const { return step_delay_; }
154 void set_step_delay(uint32 delay) {
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;
163 uint32 candidate_filter() { return candidate_filter_; }
164 bool set_candidate_filter(uint32 filter) {
165 // TODO(mallinath) - Do transition check?
166 candidate_filter_ = filter;
171 virtual PortAllocatorSession* CreateSessionInternal(
172 const std::string& content_name,
174 const std::string& ice_ufrag,
175 const std::string& ice_pwd) = 0;
177 typedef std::map<std::string, PortAllocatorSessionMuxer*> SessionMuxerMap;
181 rtc::ProxyInfo proxy_;
185 SessionMuxerMap muxers_;
186 bool allow_tcp_listen_;
187 uint32 candidate_filter_;
190 } // namespace cricket
192 #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_