henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | namespace 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 | |
| 30 | enum { |
| 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.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 36 | PORTALLOCATOR_ENABLE_IPV6 = 0x40, |
Peter Thatcher | 081f34b | 2015-08-19 20:37:54 -0700 | [diff] [blame] | 37 | // 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. |
pthatcher | fa30180 | 2015-08-11 04:12:56 -0700 | [diff] [blame] | 42 | PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 43 | PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100, |
| 44 | PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200, |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 45 | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | const uint32 kDefaultPortAllocatorFlags = 0; |
| 49 | |
| 50 | const uint32 kDefaultStepDelay = 1000; // 1 sec step delay. |
| 51 | // As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain |
| 52 | // internal. Less than 20ms is not acceptable. We choose 50ms as our default. |
| 53 | const uint32 kMinimumStepDelay = 50; |
| 54 | |
| 55 | // CF = CANDIDATE FILTER |
| 56 | enum { |
| 57 | CF_NONE = 0x0, |
| 58 | CF_HOST = 0x1, |
| 59 | CF_REFLEXIVE = 0x2, |
| 60 | CF_RELAY = 0x4, |
| 61 | CF_ALL = 0x7, |
| 62 | }; |
| 63 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 64 | class PortAllocatorSession : public sigslot::has_slots<> { |
| 65 | public: |
| 66 | // Content name passed in mostly for logging and debugging. |
| 67 | // TODO(mallinath) - Change username and password to ice_ufrag and ice_pwd. |
| 68 | PortAllocatorSession(const std::string& content_name, |
| 69 | int component, |
| 70 | const std::string& username, |
| 71 | const std::string& password, |
| 72 | uint32 flags); |
| 73 | |
| 74 | // Subclasses should clean up any ports created. |
| 75 | virtual ~PortAllocatorSession() {} |
| 76 | |
| 77 | uint32 flags() const { return flags_; } |
| 78 | void set_flags(uint32 flags) { flags_ = flags; } |
| 79 | std::string content_name() const { return content_name_; } |
| 80 | int component() const { return component_; } |
| 81 | |
| 82 | // Starts gathering STUN and Relay configurations. |
| 83 | virtual void StartGettingPorts() = 0; |
| 84 | virtual void StopGettingPorts() = 0; |
| 85 | virtual bool IsGettingPorts() = 0; |
| 86 | |
| 87 | sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; |
| 88 | sigslot::signal2<PortAllocatorSession*, |
| 89 | const std::vector<Candidate>&> SignalCandidatesReady; |
| 90 | sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; |
| 91 | |
| 92 | virtual uint32 generation() { return generation_; } |
| 93 | virtual void set_generation(uint32 generation) { generation_ = generation; } |
| 94 | sigslot::signal1<PortAllocatorSession*> SignalDestroyed; |
| 95 | |
| 96 | protected: |
| 97 | const std::string& username() const { return username_; } |
| 98 | const std::string& password() const { return password_; } |
| 99 | |
| 100 | std::string content_name_; |
| 101 | int component_; |
| 102 | |
| 103 | private: |
| 104 | uint32 flags_; |
| 105 | uint32 generation_; |
| 106 | std::string username_; |
| 107 | std::string password_; |
| 108 | }; |
| 109 | |
| 110 | class PortAllocator : public sigslot::has_slots<> { |
| 111 | public: |
| 112 | PortAllocator() : |
| 113 | flags_(kDefaultPortAllocatorFlags), |
| 114 | min_port_(0), |
| 115 | max_port_(0), |
| 116 | step_delay_(kDefaultStepDelay), |
| 117 | allow_tcp_listen_(true), |
| 118 | candidate_filter_(CF_ALL) { |
| 119 | // This will allow us to have old behavior on non webrtc clients. |
| 120 | } |
Peter Thatcher | 73ba7a6 | 2015-04-14 09:26:03 -0700 | [diff] [blame] | 121 | virtual ~PortAllocator() {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 122 | |
| 123 | PortAllocatorSession* CreateSession( |
| 124 | const std::string& sid, |
| 125 | const std::string& content_name, |
| 126 | int component, |
| 127 | const std::string& ice_ufrag, |
| 128 | const std::string& ice_pwd); |
| 129 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 170 | // Gets/Sets the Origin value used for WebRTC STUN requests. |
| 171 | const std::string& origin() const { return origin_; } |
| 172 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 173 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 174 | protected: |
| 175 | virtual PortAllocatorSession* CreateSessionInternal( |
| 176 | const std::string& content_name, |
| 177 | int component, |
| 178 | const std::string& ice_ufrag, |
| 179 | const std::string& ice_pwd) = 0; |
| 180 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 181 | uint32 flags_; |
| 182 | std::string agent_; |
| 183 | rtc::ProxyInfo proxy_; |
| 184 | int min_port_; |
| 185 | int max_port_; |
| 186 | uint32 step_delay_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 187 | bool allow_tcp_listen_; |
| 188 | uint32 candidate_filter_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 189 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | } // namespace cricket |
| 193 | |
| 194 | #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |