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 { |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 31 | // Disable local UDP ports. This doesn't impact how we connect to relay |
| 32 | // servers. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 33 | PORTALLOCATOR_DISABLE_UDP = 0x01, |
| 34 | PORTALLOCATOR_DISABLE_STUN = 0x02, |
| 35 | PORTALLOCATOR_DISABLE_RELAY = 0x04, |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 36 | // Disable local TCP ports. This doesn't impact how we connect to relay |
| 37 | // servers. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 38 | PORTALLOCATOR_DISABLE_TCP = 0x08, |
| 39 | PORTALLOCATOR_ENABLE_SHAKER = 0x10, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 40 | PORTALLOCATOR_ENABLE_IPV6 = 0x40, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 41 | // 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. |
pthatcher | fa30180 | 2015-08-11 04:12:56 -0700 | [diff] [blame] | 46 | PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 47 | PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100, |
| 48 | PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200, |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 49 | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400, |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 50 | // When specified, a loopback candidate will be generated if |
| 51 | // PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified. |
| 52 | PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE = 0x800, |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 53 | // 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.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | const uint32 kDefaultPortAllocatorFlags = 0; |
| 59 | |
| 60 | const 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. |
| 63 | const uint32 kMinimumStepDelay = 50; |
| 64 | |
| 65 | // CF = CANDIDATE FILTER |
| 66 | enum { |
| 67 | CF_NONE = 0x0, |
| 68 | CF_HOST = 0x1, |
| 69 | CF_REFLEXIVE = 0x2, |
| 70 | CF_RELAY = 0x4, |
| 71 | CF_ALL = 0x7, |
| 72 | }; |
| 73 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 74 | class PortAllocatorSession : public sigslot::has_slots<> { |
| 75 | public: |
| 76 | // Content name passed in mostly for logging and debugging. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 77 | PortAllocatorSession(const std::string& content_name, |
| 78 | int component, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 79 | const std::string& ice_ufrag, |
| 80 | const std::string& ice_pwd, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 81 | uint32 flags); |
| 82 | |
| 83 | // Subclasses should clean up any ports created. |
| 84 | virtual ~PortAllocatorSession() {} |
| 85 | |
| 86 | uint32 flags() const { return flags_; } |
| 87 | void set_flags(uint32 flags) { flags_ = flags; } |
| 88 | std::string content_name() const { return content_name_; } |
| 89 | int component() const { return component_; } |
| 90 | |
| 91 | // Starts gathering STUN and Relay configurations. |
| 92 | virtual void StartGettingPorts() = 0; |
| 93 | virtual void StopGettingPorts() = 0; |
| 94 | virtual bool IsGettingPorts() = 0; |
| 95 | |
| 96 | sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; |
| 97 | sigslot::signal2<PortAllocatorSession*, |
| 98 | const std::vector<Candidate>&> SignalCandidatesReady; |
| 99 | sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; |
| 100 | |
| 101 | virtual uint32 generation() { return generation_; } |
| 102 | virtual void set_generation(uint32 generation) { generation_ = generation; } |
| 103 | sigslot::signal1<PortAllocatorSession*> SignalDestroyed; |
| 104 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 105 | const std::string& ice_ufrag() const { return ice_ufrag_; } |
| 106 | const std::string& ice_pwd() const { return ice_pwd_; } |
| 107 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 108 | protected: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 109 | // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and |
| 110 | // ice_pwd. |
| 111 | const std::string& username() const { return ice_ufrag_; } |
| 112 | const std::string& password() const { return ice_pwd_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 113 | |
| 114 | std::string content_name_; |
| 115 | int component_; |
| 116 | |
| 117 | private: |
| 118 | uint32 flags_; |
| 119 | uint32 generation_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 120 | std::string ice_ufrag_; |
| 121 | std::string ice_pwd_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | class PortAllocator : public sigslot::has_slots<> { |
| 125 | public: |
| 126 | PortAllocator() : |
| 127 | flags_(kDefaultPortAllocatorFlags), |
| 128 | min_port_(0), |
| 129 | max_port_(0), |
| 130 | step_delay_(kDefaultStepDelay), |
| 131 | allow_tcp_listen_(true), |
| 132 | candidate_filter_(CF_ALL) { |
| 133 | // This will allow us to have old behavior on non webrtc clients. |
| 134 | } |
Peter Thatcher | 73ba7a6 | 2015-04-14 09:26:03 -0700 | [diff] [blame] | 135 | virtual ~PortAllocator() {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 136 | |
| 137 | PortAllocatorSession* CreateSession( |
| 138 | const std::string& sid, |
| 139 | const std::string& content_name, |
| 140 | int component, |
| 141 | const std::string& ice_ufrag, |
| 142 | const std::string& ice_pwd); |
| 143 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 144 | uint32 flags() const { return flags_; } |
| 145 | void set_flags(uint32 flags) { flags_ = flags; } |
| 146 | |
| 147 | const std::string& user_agent() const { return agent_; } |
| 148 | const rtc::ProxyInfo& proxy() const { return proxy_; } |
| 149 | void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { |
| 150 | agent_ = agent; |
| 151 | proxy_ = proxy; |
| 152 | } |
| 153 | |
| 154 | // Gets/Sets the port range to use when choosing client ports. |
| 155 | int min_port() const { return min_port_; } |
| 156 | int max_port() const { return max_port_; } |
| 157 | bool SetPortRange(int min_port, int max_port) { |
| 158 | if (min_port > max_port) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | min_port_ = min_port; |
| 163 | max_port_ = max_port; |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | uint32 step_delay() const { return step_delay_; } |
| 168 | void set_step_delay(uint32 delay) { |
| 169 | step_delay_ = delay; |
| 170 | } |
| 171 | |
| 172 | bool allow_tcp_listen() const { return allow_tcp_listen_; } |
| 173 | void set_allow_tcp_listen(bool allow_tcp_listen) { |
| 174 | allow_tcp_listen_ = allow_tcp_listen; |
| 175 | } |
| 176 | |
| 177 | uint32 candidate_filter() { return candidate_filter_; } |
| 178 | bool set_candidate_filter(uint32 filter) { |
| 179 | // TODO(mallinath) - Do transition check? |
| 180 | candidate_filter_ = filter; |
| 181 | return true; |
| 182 | } |
| 183 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 184 | // Gets/Sets the Origin value used for WebRTC STUN requests. |
| 185 | const std::string& origin() const { return origin_; } |
| 186 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 187 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 188 | protected: |
| 189 | virtual PortAllocatorSession* CreateSessionInternal( |
| 190 | const std::string& content_name, |
| 191 | int component, |
| 192 | const std::string& ice_ufrag, |
| 193 | const std::string& ice_pwd) = 0; |
| 194 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 195 | uint32 flags_; |
| 196 | std::string agent_; |
| 197 | rtc::ProxyInfo proxy_; |
| 198 | int min_port_; |
| 199 | int max_port_; |
| 200 | uint32 step_delay_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 201 | bool allow_tcp_listen_; |
| 202 | uint32 candidate_filter_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 203 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | } // namespace cricket |
| 207 | |
| 208 | #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |