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