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 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 14 | #include <deque> |
| 15 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 19 | #include "webrtc/p2p/base/port.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 20 | #include "webrtc/p2p/base/portinterface.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 21 | #include "webrtc/rtc_base/helpers.h" |
| 22 | #include "webrtc/rtc_base/proxyinfo.h" |
| 23 | #include "webrtc/rtc_base/sigslot.h" |
| 24 | #include "webrtc/rtc_base/thread.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 25 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 26 | namespace webrtc { |
| 27 | class MetricsObserverInterface; |
| 28 | } |
| 29 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 30 | namespace cricket { |
| 31 | |
| 32 | // PortAllocator is responsible for allocating Port types for a given |
| 33 | // P2PSocket. It also handles port freeing. |
| 34 | // |
| 35 | // Clients can override this class to control port allocation, including |
| 36 | // what kinds of ports are allocated. |
| 37 | |
| 38 | enum { |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 39 | // Disable local UDP ports. This doesn't impact how we connect to relay |
| 40 | // servers. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 41 | PORTALLOCATOR_DISABLE_UDP = 0x01, |
| 42 | PORTALLOCATOR_DISABLE_STUN = 0x02, |
| 43 | PORTALLOCATOR_DISABLE_RELAY = 0x04, |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 44 | // Disable local TCP ports. This doesn't impact how we connect to relay |
| 45 | // servers. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 46 | PORTALLOCATOR_DISABLE_TCP = 0x08, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 47 | PORTALLOCATOR_ENABLE_IPV6 = 0x40, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 48 | // TODO(pthatcher): Remove this once it's no longer used in: |
| 49 | // remoting/client/plugin/pepper_port_allocator.cc |
| 50 | // remoting/protocol/chromium_port_allocator.cc |
| 51 | // remoting/test/fake_port_allocator.cc |
| 52 | // It's a no-op and is no longer needed. |
pthatcher | fa30180 | 2015-08-11 04:12:56 -0700 | [diff] [blame] | 53 | PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 54 | PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100, |
| 55 | PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 56 | // When specified, we'll only allocate the STUN candidate for the public |
| 57 | // interface as seen by regular http traffic and the HOST candidate associated |
| 58 | // with the default local interface. |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 59 | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 60 | // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the |
| 61 | // default local candidate mentioned above will not be allocated. Only the |
| 62 | // STUN candidate will be. |
| 63 | PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800, |
Guo-wei Shieh | 13d35f6 | 2015-08-26 15:32:56 -0700 | [diff] [blame] | 64 | // Disallow use of UDP when connecting to a relay server. Since proxy servers |
| 65 | // usually don't handle UDP, using UDP will leak the IP address. |
| 66 | PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000, |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 67 | |
| 68 | // When multiple networks exist, do not gather candidates on the ones with |
| 69 | // high cost. So if both Wi-Fi and cellular networks exist, gather only on the |
| 70 | // Wi-Fi network. If a network type is "unknown", it has a cost lower than |
| 71 | // cellular but higher than Wi-Fi/Ethernet. So if an unknown network exists, |
| 72 | // cellular networks will not be used to gather candidates and if a Wi-Fi |
| 73 | // network is present, "unknown" networks will not be usd to gather |
| 74 | // candidates. Doing so ensures that even if a cellular network type was not |
| 75 | // detected initially, it would not be used if a Wi-Fi network is present. |
| 76 | PORTALLOCATOR_DISABLE_COSTLY_NETWORKS = 0x2000, |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 77 | |
| 78 | // When specified, do not collect IPv6 ICE candidates on Wi-Fi. |
| 79 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI = 0x4000, |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 80 | |
| 81 | // When this flag is set, ports not bound to any specific network interface |
| 82 | // will be used, in addition to normal ports bound to the enumerated |
| 83 | // interfaces. Without this flag, these "any address" ports would only be |
| 84 | // used when network enumeration fails or is disabled. But under certain |
| 85 | // conditions, these ports may succeed where others fail, so they may allow |
| 86 | // the application to work in a wider variety of environments, at the expense |
| 87 | // of having to allocate additional candidates. |
| 88 | PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS = 0x8000, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 91 | // Defines various reasons that have caused ICE regathering. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 92 | enum class IceRegatheringReason { |
| 93 | NETWORK_CHANGE, // Network interfaces on the device changed |
| 94 | NETWORK_FAILURE, // Regather only on networks that have failed |
| 95 | OCCASIONAL_REFRESH, // Periodic regather on all networks |
| 96 | MAX_VALUE |
| 97 | }; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 98 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 99 | const uint32_t kDefaultPortAllocatorFlags = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 100 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 101 | const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 102 | // As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain |
| 103 | // 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] | 104 | const uint32_t kMinimumStepDelay = 50; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 105 | |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 106 | // Turning on IPv6 could make many IPv6 interfaces available for connectivity |
| 107 | // check and delay the call setup time. kDefaultMaxIPv6Networks is the default |
| 108 | // upper limit of IPv6 networks but could be changed by |
| 109 | // set_max_ipv6_networks(). |
| 110 | constexpr int kDefaultMaxIPv6Networks = 5; |
| 111 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 112 | // CF = CANDIDATE FILTER |
| 113 | enum { |
| 114 | CF_NONE = 0x0, |
| 115 | CF_HOST = 0x1, |
| 116 | CF_REFLEXIVE = 0x2, |
| 117 | CF_RELAY = 0x4, |
| 118 | CF_ALL = 0x7, |
| 119 | }; |
| 120 | |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 121 | // TLS certificate policy. |
| 122 | enum class TlsCertPolicy { |
| 123 | // For TLS based protocols, ensure the connection is secure by not |
| 124 | // circumventing certificate validation. |
| 125 | TLS_CERT_POLICY_SECURE, |
| 126 | // For TLS based protocols, disregard security completely by skipping |
| 127 | // certificate validation. This is insecure and should never be used unless |
| 128 | // security is irrelevant in that particular context. |
| 129 | TLS_CERT_POLICY_INSECURE_NO_CHECK, |
| 130 | }; |
| 131 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 132 | // TODO(deadbeef): Rename to TurnCredentials (and username to ufrag). |
| 133 | struct RelayCredentials { |
| 134 | RelayCredentials() {} |
| 135 | RelayCredentials(const std::string& username, const std::string& password) |
| 136 | : username(username), password(password) {} |
| 137 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 138 | bool operator==(const RelayCredentials& o) const { |
| 139 | return username == o.username && password == o.password; |
| 140 | } |
| 141 | bool operator!=(const RelayCredentials& o) const { return !(*this == o); } |
| 142 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 143 | std::string username; |
| 144 | std::string password; |
| 145 | }; |
| 146 | |
| 147 | typedef std::vector<ProtocolAddress> PortList; |
| 148 | // TODO(deadbeef): Rename to TurnServerConfig. |
| 149 | struct RelayServerConfig { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 150 | RelayServerConfig(RelayType type) : type(type) {} |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 151 | |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 152 | RelayServerConfig(const rtc::SocketAddress& address, |
| 153 | const std::string& username, |
| 154 | const std::string& password, |
| 155 | ProtocolType proto) |
| 156 | : type(RELAY_TURN), credentials(username, password) { |
| 157 | ports.push_back(ProtocolAddress(address, proto)); |
| 158 | } |
| 159 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 160 | RelayServerConfig(const std::string& address, |
| 161 | int port, |
| 162 | const std::string& username, |
| 163 | const std::string& password, |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 164 | ProtocolType proto) |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 165 | : RelayServerConfig(rtc::SocketAddress(address, port), |
| 166 | username, |
| 167 | password, |
| 168 | proto) {} |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 169 | |
| 170 | // Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS. |
| 171 | RelayServerConfig(const std::string& address, |
| 172 | int port, |
| 173 | const std::string& username, |
| 174 | const std::string& password, |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 175 | ProtocolType proto, |
| 176 | bool secure) |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 177 | : RelayServerConfig(address, |
| 178 | port, |
| 179 | username, |
| 180 | password, |
| 181 | (proto == PROTO_TCP && secure ? PROTO_TLS : proto)) {} |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 182 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 183 | bool operator==(const RelayServerConfig& o) const { |
| 184 | return type == o.type && ports == o.ports && credentials == o.credentials && |
| 185 | priority == o.priority; |
| 186 | } |
| 187 | bool operator!=(const RelayServerConfig& o) const { return !(*this == o); } |
| 188 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 189 | RelayType type; |
| 190 | PortList ports; |
| 191 | RelayCredentials credentials; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 192 | int priority = 0; |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 193 | TlsCertPolicy tls_cert_policy = TlsCertPolicy::TLS_CERT_POLICY_SECURE; |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 194 | std::vector<std::string> tls_alpn_protocols; |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 195 | std::vector<std::string> tls_elliptic_curves; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 198 | class PortAllocatorSession : public sigslot::has_slots<> { |
| 199 | public: |
| 200 | // Content name passed in mostly for logging and debugging. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 201 | PortAllocatorSession(const std::string& content_name, |
| 202 | int component, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 203 | const std::string& ice_ufrag, |
| 204 | const std::string& ice_pwd, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 205 | uint32_t flags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 206 | |
| 207 | // Subclasses should clean up any ports created. |
| 208 | virtual ~PortAllocatorSession() {} |
| 209 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 210 | uint32_t flags() const { return flags_; } |
| 211 | void set_flags(uint32_t flags) { flags_ = flags; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 212 | std::string content_name() const { return content_name_; } |
| 213 | int component() const { return component_; } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 214 | const std::string& ice_ufrag() const { return ice_ufrag_; } |
| 215 | const std::string& ice_pwd() const { return ice_pwd_; } |
| 216 | bool pooled() const { return ice_ufrag_.empty(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 217 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 218 | // Setting this filter should affect not only candidates gathered in the |
| 219 | // future, but candidates already gathered and ports already "ready", |
| 220 | // which would be returned by ReadyCandidates() and ReadyPorts(). |
| 221 | // |
| 222 | // Default filter should be CF_ALL. |
| 223 | virtual void SetCandidateFilter(uint32_t filter) = 0; |
| 224 | |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 225 | // Starts gathering ports and ICE candidates. |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 226 | virtual void StartGettingPorts() = 0; |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 227 | // Completely stops gathering. Will not gather again unless StartGettingPorts |
| 228 | // is called again. |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 229 | virtual void StopGettingPorts() = 0; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 230 | // Whether the session is actively getting ports. |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 231 | virtual bool IsGettingPorts() = 0; |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 232 | |
| 233 | // |
| 234 | // NOTE: The group of methods below is only used for continual gathering. |
| 235 | // |
| 236 | |
| 237 | // ClearGettingPorts should have the same immediate effect as |
| 238 | // StopGettingPorts, but if the implementation supports continual gathering, |
| 239 | // ClearGettingPorts allows additional ports/candidates to be gathered if the |
| 240 | // network conditions change. |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 241 | virtual void ClearGettingPorts() = 0; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 242 | // Whether it is in the state where the existing gathering process is stopped, |
| 243 | // but new ones may be started (basically after calling ClearGettingPorts). |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 244 | virtual bool IsCleared() const { return false; } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 245 | // Whether the session has completely stopped. |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 246 | virtual bool IsStopped() const { return false; } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 247 | // Re-gathers candidates on networks that do not have any connections. More |
| 248 | // precisely, a network interface may have more than one IP addresses (e.g., |
| 249 | // IPv4 and IPv6 addresses). Each address subnet will be used to create a |
| 250 | // network. Only if all networks of an interface have no connection, the |
| 251 | // implementation should start re-gathering on all networks of that interface. |
| 252 | virtual void RegatherOnFailedNetworks() {} |
| 253 | // Re-gathers candidates on all networks. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 254 | virtual void RegatherOnAllNetworks() {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 255 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 256 | // Another way of getting the information provided by the signals below. |
| 257 | // |
| 258 | // Ports and candidates are not guaranteed to be in the same order as the |
| 259 | // signals were emitted in. |
| 260 | virtual std::vector<PortInterface*> ReadyPorts() const = 0; |
| 261 | virtual std::vector<Candidate> ReadyCandidates() const = 0; |
| 262 | virtual bool CandidatesAllocationDone() const = 0; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 263 | // Marks all ports in the current session as "pruned" so that they may be |
| 264 | // destroyed if no connection is using them. |
| 265 | virtual void PruneAllPorts() {} |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 266 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 267 | sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 268 | // Fires this signal when the network of the ports failed (either because the |
| 269 | // interface is down, or because there is no connection on the interface), |
| 270 | // or when TURN ports are pruned because a higher-priority TURN port becomes |
| 271 | // ready(pairable). |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 272 | sigslot::signal2<PortAllocatorSession*, const std::vector<PortInterface*>&> |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 273 | SignalPortsPruned; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 274 | sigslot::signal2<PortAllocatorSession*, |
| 275 | const std::vector<Candidate>&> SignalCandidatesReady; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 276 | // Candidates should be signaled to be removed when the port that generated |
| 277 | // the candidates is removed. |
| 278 | sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&> |
| 279 | SignalCandidatesRemoved; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 280 | sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; |
| 281 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 282 | sigslot::signal2<PortAllocatorSession*, IceRegatheringReason> |
| 283 | SignalIceRegathering; |
| 284 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 285 | virtual uint32_t generation() { return generation_; } |
| 286 | virtual void set_generation(uint32_t generation) { generation_ = generation; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 287 | sigslot::signal1<PortAllocatorSession*> SignalDestroyed; |
| 288 | |
deadbeef | c55fb30 | 2016-05-12 12:51:38 -0700 | [diff] [blame] | 289 | protected: |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 290 | // This method is called when a pooled session (which doesn't have these |
| 291 | // properties initially) is returned by PortAllocator::TakePooledSession, |
| 292 | // and the content name, component, and ICE ufrag/pwd are updated. |
| 293 | // |
| 294 | // A subclass may need to override this method to perform additional actions, |
| 295 | // such as applying the updated information to ports and candidates. |
| 296 | virtual void UpdateIceParametersInternal() {} |
| 297 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 298 | // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and |
| 299 | // ice_pwd. |
| 300 | const std::string& username() const { return ice_ufrag_; } |
| 301 | const std::string& password() const { return ice_pwd_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 302 | |
deadbeef | c55fb30 | 2016-05-12 12:51:38 -0700 | [diff] [blame] | 303 | private: |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 304 | void SetIceParameters(const std::string& content_name, |
| 305 | int component, |
| 306 | const std::string& ice_ufrag, |
| 307 | const std::string& ice_pwd) { |
| 308 | content_name_ = content_name; |
| 309 | component_ = component; |
| 310 | ice_ufrag_ = ice_ufrag; |
| 311 | ice_pwd_ = ice_pwd; |
| 312 | UpdateIceParametersInternal(); |
| 313 | } |
| 314 | |
deadbeef | c55fb30 | 2016-05-12 12:51:38 -0700 | [diff] [blame] | 315 | uint32_t flags_; |
| 316 | uint32_t generation_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 317 | std::string content_name_; |
| 318 | int component_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 319 | std::string ice_ufrag_; |
| 320 | std::string ice_pwd_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 321 | |
| 322 | // SetIceParameters is an implementation detail which only PortAllocator |
| 323 | // should be able to call. |
| 324 | friend class PortAllocator; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 327 | // Every method of PortAllocator (including the destructor) must be called on |
| 328 | // the same thread, except for the constructor which may be called on any |
| 329 | // thread. |
| 330 | // |
| 331 | // This allows constructing a PortAllocator subclass on one thread and |
| 332 | // passing it into an object that uses it on a different thread. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 333 | class PortAllocator : public sigslot::has_slots<> { |
| 334 | public: |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 335 | PortAllocator() |
| 336 | : flags_(kDefaultPortAllocatorFlags), |
| 337 | min_port_(0), |
| 338 | max_port_(0), |
| 339 | max_ipv6_networks_(kDefaultMaxIPv6Networks), |
| 340 | step_delay_(kDefaultStepDelay), |
| 341 | allow_tcp_listen_(true), |
| 342 | candidate_filter_(CF_ALL) {} |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 343 | |
Peter Thatcher | 73ba7a6 | 2015-04-14 09:26:03 -0700 | [diff] [blame] | 344 | virtual ~PortAllocator() {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 345 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 346 | // This should be called on the PortAllocator's thread before the |
| 347 | // PortAllocator is used. Subclasses may override this if necessary. |
| 348 | virtual void Initialize() {} |
| 349 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 350 | // Set STUN and TURN servers to be used in future sessions, and set |
| 351 | // candidate pool size, as described in JSEP. |
| 352 | // |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 353 | // If the servers are changing, and the candidate pool size is nonzero, and |
| 354 | // FreezeCandidatePool hasn't been called, existing pooled sessions will be |
| 355 | // destroyed and new ones created. |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 356 | // |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 357 | // If the servers are not changing but the candidate pool size is, and |
| 358 | // FreezeCandidatePool hasn't been called, pooled sessions will be either |
| 359 | // created or destroyed as necessary. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 360 | // |
| 361 | // Returns true if the configuration could successfully be changed. |
| 362 | bool SetConfiguration(const ServerAddresses& stun_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 363 | const std::vector<RelayServerConfig>& turn_servers, |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 364 | int candidate_pool_size, |
| 365 | bool prune_turn_ports); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 366 | |
| 367 | const ServerAddresses& stun_servers() const { return stun_servers_; } |
| 368 | |
| 369 | const std::vector<RelayServerConfig>& turn_servers() const { |
| 370 | return turn_servers_; |
| 371 | } |
| 372 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 373 | int candidate_pool_size() const { return candidate_pool_size_; } |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 374 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 375 | // Sets the network types to ignore. |
| 376 | // Values are defined by the AdapterType enum. |
| 377 | // For instance, calling this with |
| 378 | // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and |
| 379 | // loopback interfaces. |
| 380 | virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0; |
| 381 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 382 | std::unique_ptr<PortAllocatorSession> CreateSession( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 383 | const std::string& content_name, |
| 384 | int component, |
| 385 | const std::string& ice_ufrag, |
| 386 | const std::string& ice_pwd); |
| 387 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 388 | // Get an available pooled session and set the transport information on it. |
| 389 | // |
| 390 | // Caller takes ownership of the returned session. |
| 391 | // |
| 392 | // If no pooled sessions are available, returns null. |
| 393 | std::unique_ptr<PortAllocatorSession> TakePooledSession( |
| 394 | const std::string& content_name, |
| 395 | int component, |
| 396 | const std::string& ice_ufrag, |
| 397 | const std::string& ice_pwd); |
| 398 | |
| 399 | // Returns the next session that would be returned by TakePooledSession. |
| 400 | const PortAllocatorSession* GetPooledSession() const; |
| 401 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 402 | // After FreezeCandidatePool is called, changing the candidate pool size will |
| 403 | // no longer be allowed, and changing ICE servers will not cause pooled |
| 404 | // sessions to be recreated. |
| 405 | // |
| 406 | // Expected to be called when SetLocalDescription is called on a |
| 407 | // PeerConnection. Can be called safely on any thread as long as not |
| 408 | // simultaneously with SetConfiguration. |
| 409 | void FreezeCandidatePool(); |
| 410 | |
| 411 | // Discard any remaining pooled sessions. |
| 412 | void DiscardCandidatePool(); |
| 413 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 414 | uint32_t flags() const { return flags_; } |
| 415 | void set_flags(uint32_t flags) { flags_ = flags; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 416 | |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 417 | // These three methods are deprecated. If connections need to go through a |
| 418 | // proxy, the application should create a BasicPortAllocator given a custom |
| 419 | // PacketSocketFactory that creates proxy sockets. |
| 420 | const std::string& user_agent() const { return agent_; } |
| 421 | const rtc::ProxyInfo& proxy() const { return proxy_; } |
| 422 | void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { |
| 423 | agent_ = agent; |
| 424 | proxy_ = proxy; |
| 425 | } |
| 426 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 427 | // Gets/Sets the port range to use when choosing client ports. |
| 428 | int min_port() const { return min_port_; } |
| 429 | int max_port() const { return max_port_; } |
| 430 | bool SetPortRange(int min_port, int max_port) { |
| 431 | if (min_port > max_port) { |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | min_port_ = min_port; |
| 436 | max_port_ = max_port; |
| 437 | return true; |
| 438 | } |
| 439 | |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 440 | // Can be used to change the default numer of IPv6 network interfaces used |
| 441 | // (5). Can set to INT_MAX to effectively disable the limit. |
| 442 | // |
| 443 | // TODO(deadbeef): Applications shouldn't have to arbitrarily limit the |
| 444 | // number of available IPv6 network interfaces just because they could slow |
| 445 | // ICE down. We should work on making our ICE logic smarter (for example, |
| 446 | // prioritizing pinging connections that are most likely to work) so that |
| 447 | // every network interface can be used without impacting ICE's speed. |
| 448 | void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; } |
| 449 | int max_ipv6_networks() { return max_ipv6_networks_; } |
| 450 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 451 | uint32_t step_delay() const { return step_delay_; } |
| 452 | void set_step_delay(uint32_t delay) { step_delay_ = delay; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 453 | |
| 454 | bool allow_tcp_listen() const { return allow_tcp_listen_; } |
| 455 | void set_allow_tcp_listen(bool allow_tcp_listen) { |
| 456 | allow_tcp_listen_ = allow_tcp_listen; |
| 457 | } |
| 458 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 459 | uint32_t candidate_filter() { return candidate_filter_; } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 460 | void set_candidate_filter(uint32_t filter) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 461 | candidate_filter_ = filter; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 464 | bool prune_turn_ports() const { return prune_turn_ports_; } |
| 465 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 466 | // Gets/Sets the Origin value used for WebRTC STUN requests. |
| 467 | const std::string& origin() const { return origin_; } |
| 468 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 469 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 470 | void SetMetricsObserver(webrtc::MetricsObserverInterface* observer) { |
| 471 | metrics_observer_ = observer; |
| 472 | } |
| 473 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 474 | protected: |
| 475 | virtual PortAllocatorSession* CreateSessionInternal( |
| 476 | const std::string& content_name, |
| 477 | int component, |
| 478 | const std::string& ice_ufrag, |
| 479 | const std::string& ice_pwd) = 0; |
| 480 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 481 | webrtc::MetricsObserverInterface* metrics_observer() { |
| 482 | return metrics_observer_; |
| 483 | } |
| 484 | |
| 485 | const std::deque<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() { |
| 486 | return pooled_sessions_; |
| 487 | } |
| 488 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 489 | uint32_t flags_; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 490 | std::string agent_; |
| 491 | rtc::ProxyInfo proxy_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 492 | int min_port_; |
| 493 | int max_port_; |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 494 | int max_ipv6_networks_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 495 | uint32_t step_delay_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 496 | bool allow_tcp_listen_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 497 | uint32_t candidate_filter_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 498 | std::string origin_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 499 | |
| 500 | private: |
| 501 | ServerAddresses stun_servers_; |
| 502 | std::vector<RelayServerConfig> turn_servers_; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 503 | int candidate_pool_size_ = 0; // Last value passed into SetConfiguration. |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 504 | std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_; |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 505 | bool candidate_pool_frozen_ = false; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 506 | bool prune_turn_ports_ = false; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 507 | |
| 508 | webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | } // namespace cricket |
| 512 | |
| 513 | #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |