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, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame^] | 49 | // When specified, we'll only allocate the STUN candidate for the public |
| 50 | // interface as seen by regular http traffic and the HOST candidate associated |
| 51 | // with the default local interface. |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 52 | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame^] | 53 | // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the |
| 54 | // default local candidate mentioned above will not be allocated. Only the |
| 55 | // STUN candidate will be. |
| 56 | PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800, |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 57 | // Disallow use of UDP when connecting to a relay server. Since proxy servers |
| 58 | // usually don't handle UDP, using UDP will leak the IP address. |
| 59 | PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 62 | const uint32_t kDefaultPortAllocatorFlags = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 63 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 64 | const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | // As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain |
| 66 | // internal. Less than 20ms is not acceptable. We choose 50ms as our default. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 67 | const uint32_t kMinimumStepDelay = 50; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 68 | |
| 69 | // CF = CANDIDATE FILTER |
| 70 | enum { |
| 71 | CF_NONE = 0x0, |
| 72 | CF_HOST = 0x1, |
| 73 | CF_REFLEXIVE = 0x2, |
| 74 | CF_RELAY = 0x4, |
| 75 | CF_ALL = 0x7, |
| 76 | }; |
| 77 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 78 | class PortAllocatorSession : public sigslot::has_slots<> { |
| 79 | public: |
| 80 | // Content name passed in mostly for logging and debugging. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 81 | PortAllocatorSession(const std::string& content_name, |
| 82 | int component, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 83 | const std::string& ice_ufrag, |
| 84 | const std::string& ice_pwd, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 85 | uint32_t flags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 86 | |
| 87 | // Subclasses should clean up any ports created. |
| 88 | virtual ~PortAllocatorSession() {} |
| 89 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 90 | uint32_t flags() const { return flags_; } |
| 91 | void set_flags(uint32_t flags) { flags_ = flags; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 92 | std::string content_name() const { return content_name_; } |
| 93 | int component() const { return component_; } |
| 94 | |
| 95 | // Starts gathering STUN and Relay configurations. |
| 96 | virtual void StartGettingPorts() = 0; |
| 97 | virtual void StopGettingPorts() = 0; |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 98 | // Only stop the existing gathering process but may start new ones if needed. |
| 99 | virtual void ClearGettingPorts() = 0; |
| 100 | // Whether the process of getting ports has been stopped. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 101 | virtual bool IsGettingPorts() = 0; |
| 102 | |
| 103 | sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; |
| 104 | sigslot::signal2<PortAllocatorSession*, |
| 105 | const std::vector<Candidate>&> SignalCandidatesReady; |
| 106 | sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; |
| 107 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 108 | virtual uint32_t generation() { return generation_; } |
| 109 | virtual void set_generation(uint32_t generation) { generation_ = generation; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 110 | sigslot::signal1<PortAllocatorSession*> SignalDestroyed; |
| 111 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 112 | const std::string& ice_ufrag() const { return ice_ufrag_; } |
| 113 | const std::string& ice_pwd() const { return ice_pwd_; } |
| 114 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 115 | protected: |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 116 | // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and |
| 117 | // ice_pwd. |
| 118 | const std::string& username() const { return ice_ufrag_; } |
| 119 | const std::string& password() const { return ice_pwd_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 120 | |
| 121 | std::string content_name_; |
| 122 | int component_; |
| 123 | |
| 124 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 125 | uint32_t flags_; |
| 126 | uint32_t generation_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 127 | std::string ice_ufrag_; |
| 128 | std::string ice_pwd_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | class PortAllocator : public sigslot::has_slots<> { |
| 132 | public: |
| 133 | PortAllocator() : |
| 134 | flags_(kDefaultPortAllocatorFlags), |
| 135 | min_port_(0), |
| 136 | max_port_(0), |
| 137 | step_delay_(kDefaultStepDelay), |
| 138 | allow_tcp_listen_(true), |
| 139 | candidate_filter_(CF_ALL) { |
| 140 | // This will allow us to have old behavior on non webrtc clients. |
| 141 | } |
Peter Thatcher | 73ba7a6 | 2015-04-14 09:26:03 -0700 | [diff] [blame] | 142 | virtual ~PortAllocator() {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 143 | |
| 144 | PortAllocatorSession* CreateSession( |
| 145 | const std::string& sid, |
| 146 | const std::string& content_name, |
| 147 | int component, |
| 148 | const std::string& ice_ufrag, |
| 149 | const std::string& ice_pwd); |
| 150 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 151 | uint32_t flags() const { return flags_; } |
| 152 | void set_flags(uint32_t flags) { flags_ = flags; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 153 | |
| 154 | const std::string& user_agent() const { return agent_; } |
| 155 | const rtc::ProxyInfo& proxy() const { return proxy_; } |
| 156 | void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { |
| 157 | agent_ = agent; |
| 158 | proxy_ = proxy; |
| 159 | } |
| 160 | |
| 161 | // Gets/Sets the port range to use when choosing client ports. |
| 162 | int min_port() const { return min_port_; } |
| 163 | int max_port() const { return max_port_; } |
| 164 | bool SetPortRange(int min_port, int max_port) { |
| 165 | if (min_port > max_port) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | min_port_ = min_port; |
| 170 | max_port_ = max_port; |
| 171 | return true; |
| 172 | } |
| 173 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 174 | uint32_t step_delay() const { return step_delay_; } |
| 175 | void set_step_delay(uint32_t delay) { step_delay_ = delay; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 176 | |
| 177 | bool allow_tcp_listen() const { return allow_tcp_listen_; } |
| 178 | void set_allow_tcp_listen(bool allow_tcp_listen) { |
| 179 | allow_tcp_listen_ = allow_tcp_listen; |
| 180 | } |
| 181 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 182 | uint32_t candidate_filter() { return candidate_filter_; } |
| 183 | bool set_candidate_filter(uint32_t filter) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 184 | // TODO(mallinath) - Do transition check? |
| 185 | candidate_filter_ = filter; |
| 186 | return true; |
| 187 | } |
| 188 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 189 | // Gets/Sets the Origin value used for WebRTC STUN requests. |
| 190 | const std::string& origin() const { return origin_; } |
| 191 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 192 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 193 | protected: |
| 194 | virtual PortAllocatorSession* CreateSessionInternal( |
| 195 | const std::string& content_name, |
| 196 | int component, |
| 197 | const std::string& ice_ufrag, |
| 198 | const std::string& ice_pwd) = 0; |
| 199 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 200 | uint32_t flags_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 201 | std::string agent_; |
| 202 | rtc::ProxyInfo proxy_; |
| 203 | int min_port_; |
| 204 | int max_port_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 205 | uint32_t step_delay_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 206 | bool allow_tcp_listen_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 207 | uint32_t candidate_filter_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 208 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | } // namespace cricket |
| 212 | |
| 213 | #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |