henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef P2P_BASE_PORT_ALLOCATOR_H_ |
| 12 | #define P2P_BASE_PORT_ALLOCATOR_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 13 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 14 | #include <deque> |
| 15 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Artem Titov | 06159aa | 2021-02-09 22:38:13 | [diff] [blame] | 19 | #include "api/sequence_checker.h" |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 20 | #include "api/transport/enums.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 21 | #include "p2p/base/port.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "p2p/base/port_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "rtc_base/helpers.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 24 | #include "rtc_base/proxy_info.h" |
| 25 | #include "rtc_base/ssl_certificate.h" |
Mirko Bonadei | 3b56ee7 | 2018-10-15 15:15:12 | [diff] [blame] | 26 | #include "rtc_base/system/rtc_export.h" |
Artem Titov | e41c433 | 2018-07-25 13:04:28 | [diff] [blame] | 27 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 28 | #include "rtc_base/thread.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 29 | |
Honghai Zhang | d93f50c | 2016-10-05 18:47:22 | [diff] [blame] | 30 | namespace webrtc { |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 31 | class TurnCustomizer; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 32 | } // namespace webrtc |
Honghai Zhang | d93f50c | 2016-10-05 18:47:22 | [diff] [blame] | 33 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 34 | namespace cricket { |
| 35 | |
| 36 | // PortAllocator is responsible for allocating Port types for a given |
| 37 | // P2PSocket. It also handles port freeing. |
| 38 | // |
| 39 | // Clients can override this class to control port allocation, including |
| 40 | // what kinds of ports are allocated. |
| 41 | |
| 42 | enum { |
Guo-wei Shieh | 13d35f6 | 2015-08-26 22:32:56 | [diff] [blame] | 43 | // Disable local UDP ports. This doesn't impact how we connect to relay |
| 44 | // servers. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 45 | PORTALLOCATOR_DISABLE_UDP = 0x01, |
| 46 | PORTALLOCATOR_DISABLE_STUN = 0x02, |
| 47 | PORTALLOCATOR_DISABLE_RELAY = 0x04, |
Guo-wei Shieh | 13d35f6 | 2015-08-26 22:32:56 | [diff] [blame] | 48 | // Disable local TCP ports. This doesn't impact how we connect to relay |
| 49 | // servers. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 50 | PORTALLOCATOR_DISABLE_TCP = 0x08, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 51 | PORTALLOCATOR_ENABLE_IPV6 = 0x40, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 52 | PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100, |
| 53 | PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 54 | // When specified, we'll only allocate the STUN candidate for the public |
| 55 | // interface as seen by regular http traffic and the HOST candidate associated |
| 56 | // with the default local interface. |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 | [diff] [blame] | 57 | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION = 0x400, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 58 | // When specified along with PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION, the |
| 59 | // default local candidate mentioned above will not be allocated. Only the |
| 60 | // STUN candidate will be. |
| 61 | PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE = 0x800, |
Guo-wei Shieh | 13d35f6 | 2015-08-26 22:32:56 | [diff] [blame] | 62 | // Disallow use of UDP when connecting to a relay server. Since proxy servers |
| 63 | // usually don't handle UDP, using UDP will leak the IP address. |
| 64 | PORTALLOCATOR_DISABLE_UDP_RELAY = 0x1000, |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 65 | |
| 66 | // When multiple networks exist, do not gather candidates on the ones with |
| 67 | // high cost. So if both Wi-Fi and cellular networks exist, gather only on the |
| 68 | // Wi-Fi network. If a network type is "unknown", it has a cost lower than |
| 69 | // cellular but higher than Wi-Fi/Ethernet. So if an unknown network exists, |
| 70 | // cellular networks will not be used to gather candidates and if a Wi-Fi |
| 71 | // network is present, "unknown" networks will not be usd to gather |
| 72 | // candidates. Doing so ensures that even if a cellular network type was not |
| 73 | // detected initially, it would not be used if a Wi-Fi network is present. |
| 74 | PORTALLOCATOR_DISABLE_COSTLY_NETWORKS = 0x2000, |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 75 | |
| 76 | // When specified, do not collect IPv6 ICE candidates on Wi-Fi. |
| 77 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI = 0x4000, |
deadbeef | 1ee2125 | 2017-06-13 22:49:45 | [diff] [blame] | 78 | |
Mirko Bonadei | 5f4d47b | 2018-08-22 17:41:22 | [diff] [blame] | 79 | // When this flag is set, ports not bound to any specific network interface |
| 80 | // will be used, in addition to normal ports bound to the enumerated |
| 81 | // interfaces. Without this flag, these "any address" ports would only be |
| 82 | // used when network enumeration fails or is disabled. But under certain |
| 83 | // conditions, these ports may succeed where others fail, so they may allow |
| 84 | // the application to work in a wider variety of environments, at the expense |
| 85 | // of having to allocate additional candidates. |
Qingsi Wang | efbcb31 | 2018-08-21 23:23:26 | [diff] [blame] | 86 | PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS = 0x8000, |
| 87 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 88 | // Exclude link-local network interfaces |
| 89 | // from considertaion after adapter enumeration. |
Qingsi Wang | efbcb31 | 2018-08-21 23:23:26 | [diff] [blame] | 90 | PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS = 0x10000, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 91 | }; |
| 92 | |
Honghai Zhang | d93f50c | 2016-10-05 18:47:22 | [diff] [blame] | 93 | // Defines various reasons that have caused ICE regathering. |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 94 | enum class IceRegatheringReason { |
| 95 | NETWORK_CHANGE, // Network interfaces on the device changed |
| 96 | NETWORK_FAILURE, // Regather only on networks that have failed |
| 97 | OCCASIONAL_REFRESH, // Periodic regather on all networks |
| 98 | MAX_VALUE |
| 99 | }; |
Honghai Zhang | d93f50c | 2016-10-05 18:47:22 | [diff] [blame] | 100 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 101 | const uint32_t kDefaultPortAllocatorFlags = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 102 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 103 | const uint32_t kDefaultStepDelay = 1000; // 1 sec step delay. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 104 | // As per RFC 5245 Appendix B.1, STUN transactions need to be paced at certain |
| 105 | // internal. Less than 20ms is not acceptable. We choose 50ms as our default. |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 106 | const uint32_t kMinimumStepDelay = 50; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 107 | |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 108 | // Turning on IPv6 could make many IPv6 interfaces available for connectivity |
| 109 | // check and delay the call setup time. kDefaultMaxIPv6Networks is the default |
| 110 | // upper limit of IPv6 networks but could be changed by |
| 111 | // set_max_ipv6_networks(). |
| 112 | constexpr int kDefaultMaxIPv6Networks = 5; |
| 113 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 114 | // CF = CANDIDATE FILTER |
Oleh Prypin | fd7df98 | 2017-12-21 15:25:19 | [diff] [blame] | 115 | enum : uint32_t { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 116 | CF_NONE = 0x0, |
| 117 | CF_HOST = 0x1, |
| 118 | CF_REFLEXIVE = 0x2, |
| 119 | CF_RELAY = 0x4, |
| 120 | CF_ALL = 0x7, |
| 121 | }; |
| 122 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 | [diff] [blame] | 123 | // TLS certificate policy. |
| 124 | enum class TlsCertPolicy { |
| 125 | // For TLS based protocols, ensure the connection is secure by not |
| 126 | // circumventing certificate validation. |
| 127 | TLS_CERT_POLICY_SECURE, |
| 128 | // For TLS based protocols, disregard security completely by skipping |
| 129 | // certificate validation. This is insecure and should never be used unless |
| 130 | // security is irrelevant in that particular context. |
| 131 | TLS_CERT_POLICY_INSECURE_NO_CHECK, |
| 132 | }; |
| 133 | |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 134 | // TODO(deadbeef): Rename to TurnCredentials (and username to ufrag). |
| 135 | struct RelayCredentials { |
| 136 | RelayCredentials() {} |
| 137 | RelayCredentials(const std::string& username, const std::string& password) |
| 138 | : username(username), password(password) {} |
| 139 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 140 | bool operator==(const RelayCredentials& o) const { |
| 141 | return username == o.username && password == o.password; |
| 142 | } |
| 143 | bool operator!=(const RelayCredentials& o) const { return !(*this == o); } |
| 144 | |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 145 | std::string username; |
| 146 | std::string password; |
| 147 | }; |
| 148 | |
| 149 | typedef std::vector<ProtocolAddress> PortList; |
| 150 | // TODO(deadbeef): Rename to TurnServerConfig. |
Mirko Bonadei | 3b56ee7 | 2018-10-15 15:15:12 | [diff] [blame] | 151 | struct RTC_EXPORT RelayServerConfig { |
Niels Möller | 191e38f | 2019-11-04 07:49:12 | [diff] [blame] | 152 | RelayServerConfig(); |
Emad Omara | dab1d2d | 2017-06-16 22:43:11 | [diff] [blame] | 153 | RelayServerConfig(const rtc::SocketAddress& address, |
| 154 | const std::string& username, |
| 155 | const std::string& password, |
Steve Anton | 7995d8c | 2017-10-30 23:23:38 | [diff] [blame] | 156 | ProtocolType proto); |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 157 | RelayServerConfig(const std::string& address, |
| 158 | int port, |
| 159 | const std::string& username, |
| 160 | const std::string& password, |
Steve Anton | 7995d8c | 2017-10-30 23:23:38 | [diff] [blame] | 161 | ProtocolType proto); |
hnsl | 277b250 | 2016-12-13 13:17:23 | [diff] [blame] | 162 | // Legacy constructor where "secure" and PROTO_TCP implies PROTO_TLS. |
| 163 | RelayServerConfig(const std::string& address, |
| 164 | int port, |
| 165 | const std::string& username, |
| 166 | const std::string& password, |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 167 | ProtocolType proto, |
Steve Anton | 7995d8c | 2017-10-30 23:23:38 | [diff] [blame] | 168 | bool secure); |
| 169 | RelayServerConfig(const RelayServerConfig&); |
| 170 | ~RelayServerConfig(); |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 171 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 172 | bool operator==(const RelayServerConfig& o) const { |
Niels Möller | 191e38f | 2019-11-04 07:49:12 | [diff] [blame] | 173 | return ports == o.ports && credentials == o.credentials && |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 174 | priority == o.priority; |
| 175 | } |
| 176 | bool operator!=(const RelayServerConfig& o) const { return !(*this == o); } |
| 177 | |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 178 | PortList ports; |
| 179 | RelayCredentials credentials; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 180 | int priority = 0; |
hnsl | 0483362 | 2017-01-09 16:35:45 | [diff] [blame] | 181 | TlsCertPolicy tls_cert_policy = TlsCertPolicy::TLS_CERT_POLICY_SECURE; |
Diogo Real | 1dca9d5 | 2017-08-29 19:18:32 | [diff] [blame] | 182 | std::vector<std::string> tls_alpn_protocols; |
Diogo Real | 7bd1f1b | 2017-09-08 19:50:41 | [diff] [blame] | 183 | std::vector<std::string> tls_elliptic_curves; |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 184 | rtc::SSLCertificateVerifier* tls_cert_verifier = nullptr; |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 185 | std::string turn_logging_id; |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 186 | }; |
| 187 | |
Mirko Bonadei | 3b56ee7 | 2018-10-15 15:15:12 | [diff] [blame] | 188 | class RTC_EXPORT PortAllocatorSession : public sigslot::has_slots<> { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 189 | public: |
| 190 | // Content name passed in mostly for logging and debugging. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 191 | PortAllocatorSession(const std::string& content_name, |
| 192 | int component, |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 193 | const std::string& ice_ufrag, |
| 194 | const std::string& ice_pwd, |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 195 | uint32_t flags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 196 | |
| 197 | // Subclasses should clean up any ports created. |
Steve Anton | 7995d8c | 2017-10-30 23:23:38 | [diff] [blame] | 198 | ~PortAllocatorSession() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 199 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 200 | uint32_t flags() const { return flags_; } |
| 201 | void set_flags(uint32_t flags) { flags_ = flags; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 202 | std::string content_name() const { return content_name_; } |
| 203 | int component() const { return component_; } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 204 | const std::string& ice_ufrag() const { return ice_ufrag_; } |
| 205 | const std::string& ice_pwd() const { return ice_pwd_; } |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 206 | bool pooled() const { return pooled_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 207 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 208 | // Setting this filter should affect not only candidates gathered in the |
| 209 | // future, but candidates already gathered and ports already "ready", |
| 210 | // which would be returned by ReadyCandidates() and ReadyPorts(). |
| 211 | // |
| 212 | // Default filter should be CF_ALL. |
| 213 | virtual void SetCandidateFilter(uint32_t filter) = 0; |
| 214 | |
deadbeef | b60a819 | 2016-08-24 22:15:00 | [diff] [blame] | 215 | // Starts gathering ports and ICE candidates. |
Honghai Zhang | d8f6fc4 | 2016-07-02 00:31:12 | [diff] [blame] | 216 | virtual void StartGettingPorts() = 0; |
deadbeef | b60a819 | 2016-08-24 22:15:00 | [diff] [blame] | 217 | // Completely stops gathering. Will not gather again unless StartGettingPorts |
| 218 | // is called again. |
Honghai Zhang | d8f6fc4 | 2016-07-02 00:31:12 | [diff] [blame] | 219 | virtual void StopGettingPorts() = 0; |
Honghai Zhang | 5622c5e | 2016-07-01 20:59:29 | [diff] [blame] | 220 | // Whether the session is actively getting ports. |
Honghai Zhang | d8f6fc4 | 2016-07-02 00:31:12 | [diff] [blame] | 221 | virtual bool IsGettingPorts() = 0; |
deadbeef | b60a819 | 2016-08-24 22:15:00 | [diff] [blame] | 222 | |
| 223 | // |
| 224 | // NOTE: The group of methods below is only used for continual gathering. |
| 225 | // |
| 226 | |
| 227 | // ClearGettingPorts should have the same immediate effect as |
| 228 | // StopGettingPorts, but if the implementation supports continual gathering, |
| 229 | // ClearGettingPorts allows additional ports/candidates to be gathered if the |
| 230 | // network conditions change. |
Honghai Zhang | d8f6fc4 | 2016-07-02 00:31:12 | [diff] [blame] | 231 | virtual void ClearGettingPorts() = 0; |
Honghai Zhang | 5622c5e | 2016-07-01 20:59:29 | [diff] [blame] | 232 | // Whether it is in the state where the existing gathering process is stopped, |
| 233 | // but new ones may be started (basically after calling ClearGettingPorts). |
Steve Anton | 7995d8c | 2017-10-30 23:23:38 | [diff] [blame] | 234 | virtual bool IsCleared() const; |
Honghai Zhang | 5622c5e | 2016-07-01 20:59:29 | [diff] [blame] | 235 | // Whether the session has completely stopped. |
Steve Anton | 7995d8c | 2017-10-30 23:23:38 | [diff] [blame] | 236 | virtual bool IsStopped() const; |
Honghai Zhang | 5622c5e | 2016-07-01 20:59:29 | [diff] [blame] | 237 | // Re-gathers candidates on networks that do not have any connections. More |
| 238 | // precisely, a network interface may have more than one IP addresses (e.g., |
| 239 | // IPv4 and IPv6 addresses). Each address subnet will be used to create a |
| 240 | // network. Only if all networks of an interface have no connection, the |
| 241 | // implementation should start re-gathering on all networks of that interface. |
| 242 | virtual void RegatherOnFailedNetworks() {} |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 243 | // Get candidate-level stats from all candidates on the ready ports and return |
| 244 | // the stats to the given list. |
| 245 | virtual void GetCandidateStatsFromReadyPorts( |
Qingsi Wang | 7627fdd | 2019-08-19 23:07:40 | [diff] [blame] | 246 | CandidateStatsList* candidate_stats_list) const {} |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [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( |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 252 | const absl::optional<int>& stun_keepalive_interval) {} |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [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-29 01:06:15 | [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 15:15:11 | [diff] [blame] | 263 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 264 | sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; |
Honghai Zhang | 8eeecab | 2016-07-28 20:20:15 | [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 20:59:29 | [diff] [blame] | 269 | sigslot::signal2<PortAllocatorSession*, const std::vector<PortInterface*>&> |
Honghai Zhang | 8eeecab | 2016-07-28 20:20:15 | [diff] [blame] | 270 | SignalPortsPruned; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 271 | sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&> |
| 272 | SignalCandidatesReady; |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 273 | sigslot::signal2<PortAllocatorSession*, const IceCandidateErrorEvent&> |
| 274 | SignalCandidateError; |
Honghai Zhang | 5622c5e | 2016-07-01 20:59:29 | [diff] [blame] | 275 | // Candidates should be signaled to be removed when the port that generated |
| 276 | // the candidates is removed. |
| 277 | sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&> |
| 278 | SignalCandidatesRemoved; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 279 | sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; |
| 280 | |
Honghai Zhang | d93f50c | 2016-10-05 18:47:22 | [diff] [blame] | 281 | sigslot::signal2<PortAllocatorSession*, IceRegatheringReason> |
| 282 | SignalIceRegathering; |
| 283 | |
Steve Anton | 7995d8c | 2017-10-30 23:23:38 | [diff] [blame] | 284 | virtual uint32_t generation(); |
| 285 | virtual void set_generation(uint32_t generation); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 286 | sigslot::signal1<PortAllocatorSession*> SignalDestroyed; |
| 287 | |
deadbeef | c55fb30 | 2016-05-12 19:51:38 | [diff] [blame] | 288 | protected: |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 289 | // This method is called when a pooled session (which doesn't have these |
| 290 | // properties initially) is returned by PortAllocator::TakePooledSession, |
| 291 | // and the content name, component, and ICE ufrag/pwd are updated. |
| 292 | // |
| 293 | // A subclass may need to override this method to perform additional actions, |
| 294 | // such as applying the updated information to ports and candidates. |
| 295 | virtual void UpdateIceParametersInternal() {} |
| 296 | |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 297 | // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and |
| 298 | // ice_pwd. |
| 299 | const std::string& username() const { return ice_ufrag_; } |
| 300 | const std::string& password() const { return ice_pwd_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 301 | |
deadbeef | c55fb30 | 2016-05-12 19:51:38 | [diff] [blame] | 302 | private: |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 303 | void SetIceParameters(const std::string& content_name, |
| 304 | int component, |
| 305 | const std::string& ice_ufrag, |
| 306 | const std::string& ice_pwd) { |
| 307 | content_name_ = content_name; |
| 308 | component_ = component; |
| 309 | ice_ufrag_ = ice_ufrag; |
| 310 | ice_pwd_ = ice_pwd; |
| 311 | UpdateIceParametersInternal(); |
| 312 | } |
| 313 | |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 314 | void set_pooled(bool value) { pooled_ = value; } |
| 315 | |
deadbeef | c55fb30 | 2016-05-12 19:51:38 | [diff] [blame] | 316 | uint32_t flags_; |
| 317 | uint32_t generation_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 318 | std::string content_name_; |
| 319 | int component_; |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 320 | std::string ice_ufrag_; |
| 321 | std::string ice_pwd_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 322 | |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 323 | bool pooled_ = false; |
| 324 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 325 | // SetIceParameters is an implementation detail which only PortAllocator |
| 326 | // should be able to call. |
| 327 | friend class PortAllocator; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 328 | }; |
| 329 | |
Taylor Brandstetter | f8e6577 | 2016-06-28 00:20:15 | [diff] [blame] | 330 | // Every method of PortAllocator (including the destructor) must be called on |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 331 | // the same thread after Initialize is called. |
Taylor Brandstetter | f8e6577 | 2016-06-28 00:20:15 | [diff] [blame] | 332 | // |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 333 | // This allows a PortAllocator subclass to be constructed and configured on one |
| 334 | // thread, and passed into an object that uses it on a different thread. |
Mirko Bonadei | 3b56ee7 | 2018-10-15 15:15:12 | [diff] [blame] | 335 | class RTC_EXPORT PortAllocator : public sigslot::has_slots<> { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 336 | public: |
Steve Anton | 7995d8c | 2017-10-30 23:23:38 | [diff] [blame] | 337 | PortAllocator(); |
| 338 | ~PortAllocator() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 339 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 340 | // This MUST be called on the PortAllocator's thread after finishing |
| 341 | // constructing and configuring the PortAllocator subclasses. |
| 342 | virtual void Initialize(); |
Taylor Brandstetter | f8e6577 | 2016-06-28 00:20:15 | [diff] [blame] | 343 | |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 344 | // Set to true if some Ports need to know the ICE credentials when they are |
| 345 | // created. This will ensure that the PortAllocator will only match pooled |
| 346 | // allocator sessions to the ICE transport with the same credentials. |
| 347 | virtual void set_restrict_ice_credentials_change(bool value); |
| 348 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 349 | // Set STUN and TURN servers to be used in future sessions, and set |
| 350 | // candidate pool size, as described in JSEP. |
| 351 | // |
deadbeef | 42a4263 | 2017-03-10 23:18:00 | [diff] [blame] | 352 | // If the servers are changing, and the candidate pool size is nonzero, and |
| 353 | // FreezeCandidatePool hasn't been called, existing pooled sessions will be |
| 354 | // destroyed and new ones created. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 355 | // |
deadbeef | 42a4263 | 2017-03-10 23:18:00 | [diff] [blame] | 356 | // If the servers are not changing but the candidate pool size is, and |
| 357 | // FreezeCandidatePool hasn't been called, pooled sessions will be either |
| 358 | // created or destroyed as necessary. |
deadbeef | 6de92f9 | 2016-12-13 02:49:32 | [diff] [blame] | 359 | // |
| 360 | // Returns true if the configuration could successfully be changed. |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 361 | // Deprecated |
deadbeef | 6de92f9 | 2016-12-13 02:49:32 | [diff] [blame] | 362 | bool SetConfiguration(const ServerAddresses& stun_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 363 | const std::vector<RelayServerConfig>& turn_servers, |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 364 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 365 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 366 | webrtc::TurnCustomizer* turn_customizer = nullptr, |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 367 | const absl::optional<int>& |
| 368 | stun_candidate_keepalive_interval = absl::nullopt); |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 369 | bool SetConfiguration(const ServerAddresses& stun_servers, |
| 370 | const std::vector<RelayServerConfig>& turn_servers, |
| 371 | int candidate_pool_size, |
| 372 | webrtc::PortPrunePolicy turn_port_prune_policy, |
| 373 | webrtc::TurnCustomizer* turn_customizer = nullptr, |
| 374 | const absl::optional<int>& |
| 375 | stun_candidate_keepalive_interval = absl::nullopt); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 376 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 377 | const ServerAddresses& stun_servers() const { |
| 378 | CheckRunOnValidThreadIfInitialized(); |
| 379 | return stun_servers_; |
| 380 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 381 | |
| 382 | const std::vector<RelayServerConfig>& turn_servers() const { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 383 | CheckRunOnValidThreadIfInitialized(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 384 | return turn_servers_; |
| 385 | } |
| 386 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 387 | int candidate_pool_size() const { |
| 388 | CheckRunOnValidThreadIfInitialized(); |
| 389 | return candidate_pool_size_; |
| 390 | } |
| 391 | |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 392 | const absl::optional<int>& stun_candidate_keepalive_interval() const { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 393 | CheckRunOnValidThreadIfInitialized(); |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 394 | return stun_candidate_keepalive_interval_; |
| 395 | } |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 396 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 397 | // Sets the network types to ignore. |
| 398 | // Values are defined by the AdapterType enum. |
| 399 | // For instance, calling this with |
| 400 | // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and |
| 401 | // loopback interfaces. |
| 402 | virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0; |
| 403 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 404 | std::unique_ptr<PortAllocatorSession> CreateSession( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 405 | const std::string& content_name, |
| 406 | int component, |
| 407 | const std::string& ice_ufrag, |
| 408 | const std::string& ice_pwd); |
| 409 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 410 | // Get an available pooled session and set the transport information on it. |
| 411 | // |
| 412 | // Caller takes ownership of the returned session. |
| 413 | // |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 414 | // If restrict_ice_credentials_change is TRUE, then it will only |
| 415 | // return a pooled session with matching ice credentials. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 416 | // If no pooled sessions are available, returns null. |
| 417 | std::unique_ptr<PortAllocatorSession> TakePooledSession( |
| 418 | const std::string& content_name, |
| 419 | int component, |
| 420 | const std::string& ice_ufrag, |
| 421 | const std::string& ice_pwd); |
| 422 | |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 423 | // Returns the next session that would be returned by TakePooledSession |
| 424 | // optionally restricting it to sessions with specified ice credentials. |
| 425 | const PortAllocatorSession* GetPooledSession( |
| 426 | const IceParameters* ice_credentials = nullptr) const; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 427 | |
deadbeef | 42a4263 | 2017-03-10 23:18:00 | [diff] [blame] | 428 | // After FreezeCandidatePool is called, changing the candidate pool size will |
| 429 | // no longer be allowed, and changing ICE servers will not cause pooled |
| 430 | // sessions to be recreated. |
| 431 | // |
| 432 | // Expected to be called when SetLocalDescription is called on a |
| 433 | // PeerConnection. Can be called safely on any thread as long as not |
| 434 | // simultaneously with SetConfiguration. |
| 435 | void FreezeCandidatePool(); |
| 436 | |
| 437 | // Discard any remaining pooled sessions. |
| 438 | void DiscardCandidatePool(); |
| 439 | |
Qingsi Wang | 7627fdd | 2019-08-19 23:07:40 | [diff] [blame] | 440 | // Clears the address and the related address fields of a local candidate to |
| 441 | // avoid IP leakage. This is applicable in several scenarios: |
| 442 | // 1. Sanitization is configured via the candidate filter. |
| 443 | // 2. Sanitization is configured via the port allocator flags. |
| 444 | // 3. mDNS concealment of private IPs is enabled. |
| 445 | Candidate SanitizeCandidate(const Candidate& c) const; |
| 446 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 447 | uint32_t flags() const { |
| 448 | CheckRunOnValidThreadIfInitialized(); |
| 449 | return flags_; |
| 450 | } |
| 451 | |
| 452 | void set_flags(uint32_t flags) { |
| 453 | CheckRunOnValidThreadIfInitialized(); |
| 454 | flags_ = flags; |
| 455 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 456 | |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 457 | // These three methods are deprecated. If connections need to go through a |
| 458 | // proxy, the application should create a BasicPortAllocator given a custom |
| 459 | // PacketSocketFactory that creates proxy sockets. |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 460 | const std::string& user_agent() const { |
| 461 | CheckRunOnValidThreadIfInitialized(); |
| 462 | return agent_; |
| 463 | } |
| 464 | |
| 465 | const rtc::ProxyInfo& proxy() const { |
| 466 | CheckRunOnValidThreadIfInitialized(); |
| 467 | return proxy_; |
| 468 | } |
| 469 | |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 470 | void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 471 | CheckRunOnValidThreadIfInitialized(); |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 472 | agent_ = agent; |
| 473 | proxy_ = proxy; |
| 474 | } |
| 475 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 476 | // Gets/Sets the port range to use when choosing client ports. |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 477 | int min_port() const { |
| 478 | CheckRunOnValidThreadIfInitialized(); |
| 479 | return min_port_; |
| 480 | } |
| 481 | |
| 482 | int max_port() const { |
| 483 | CheckRunOnValidThreadIfInitialized(); |
| 484 | return max_port_; |
| 485 | } |
| 486 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 487 | bool SetPortRange(int min_port, int max_port) { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 488 | CheckRunOnValidThreadIfInitialized(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 489 | if (min_port > max_port) { |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | min_port_ = min_port; |
| 494 | max_port_ = max_port; |
| 495 | return true; |
| 496 | } |
| 497 | |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 498 | // Can be used to change the default numer of IPv6 network interfaces used |
| 499 | // (5). Can set to INT_MAX to effectively disable the limit. |
| 500 | // |
| 501 | // TODO(deadbeef): Applications shouldn't have to arbitrarily limit the |
| 502 | // number of available IPv6 network interfaces just because they could slow |
| 503 | // ICE down. We should work on making our ICE logic smarter (for example, |
| 504 | // prioritizing pinging connections that are most likely to work) so that |
| 505 | // every network interface can be used without impacting ICE's speed. |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 506 | void set_max_ipv6_networks(int networks) { |
| 507 | CheckRunOnValidThreadIfInitialized(); |
| 508 | max_ipv6_networks_ = networks; |
| 509 | } |
| 510 | |
| 511 | int max_ipv6_networks() { |
| 512 | CheckRunOnValidThreadIfInitialized(); |
| 513 | return max_ipv6_networks_; |
| 514 | } |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 515 | |
deadbeef | 1c5e6d0 | 2017-09-16 00:46:56 | [diff] [blame] | 516 | // Delay between different candidate gathering phases (UDP, TURN, TCP). |
| 517 | // Defaults to 1 second, but PeerConnection sets it to 50ms. |
| 518 | // TODO(deadbeef): Get rid of this. Its purpose is to avoid sending too many |
| 519 | // STUN transactions at once, but that's already happening if you configure |
| 520 | // multiple STUN servers or have multiple network interfaces. We should |
| 521 | // implement some global pacing logic instead if that's our goal. |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 522 | uint32_t step_delay() const { |
| 523 | CheckRunOnValidThreadIfInitialized(); |
| 524 | return step_delay_; |
| 525 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 526 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 527 | void set_step_delay(uint32_t delay) { |
| 528 | CheckRunOnValidThreadIfInitialized(); |
| 529 | step_delay_ = delay; |
| 530 | } |
| 531 | |
| 532 | bool allow_tcp_listen() const { |
| 533 | CheckRunOnValidThreadIfInitialized(); |
| 534 | return allow_tcp_listen_; |
| 535 | } |
| 536 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 537 | void set_allow_tcp_listen(bool allow_tcp_listen) { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 538 | CheckRunOnValidThreadIfInitialized(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 539 | allow_tcp_listen_ = allow_tcp_listen; |
| 540 | } |
| 541 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 542 | uint32_t candidate_filter() { |
| 543 | CheckRunOnValidThreadIfInitialized(); |
| 544 | return candidate_filter_; |
| 545 | } |
| 546 | |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 547 | // The new filter value will be populated to future allocation sessions, when |
| 548 | // they are created via CreateSession, and also pooled sessions when one is |
| 549 | // taken via TakePooledSession. |
| 550 | // |
| 551 | // A change in the candidate filter also fires a signal |
| 552 | // |SignalCandidateFilterChanged|, so that objects subscribed to this signal |
| 553 | // can, for example, update the candidate filter for sessions created by this |
| 554 | // allocator and already taken by the object. |
| 555 | // |
| 556 | // Specifically for the session taken by the ICE transport, we currently do |
| 557 | // not support removing candidate pairs formed with local candidates from this |
| 558 | // session that are disabled by the new candidate filter. |
| 559 | void SetCandidateFilter(uint32_t filter); |
| 560 | // Deprecated. |
| 561 | // TODO(qingsi): Remove this after Chromium migrates to the new method. |
| 562 | void set_candidate_filter(uint32_t filter) { SetCandidateFilter(filter); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 563 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 564 | // Deprecated (by the next method). |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 565 | bool prune_turn_ports() const { |
| 566 | CheckRunOnValidThreadIfInitialized(); |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 567 | return turn_port_prune_policy_ == webrtc::PRUNE_BASED_ON_PRIORITY; |
| 568 | } |
| 569 | |
| 570 | webrtc::PortPrunePolicy turn_port_prune_policy() const { |
| 571 | CheckRunOnValidThreadIfInitialized(); |
| 572 | return turn_port_prune_policy_; |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 573 | } |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 574 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 | [diff] [blame] | 575 | // Gets/Sets the Origin value used for WebRTC STUN requests. |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 576 | const std::string& origin() const { |
| 577 | CheckRunOnValidThreadIfInitialized(); |
| 578 | return origin_; |
| 579 | } |
| 580 | |
| 581 | void set_origin(const std::string& origin) { |
| 582 | CheckRunOnValidThreadIfInitialized(); |
| 583 | origin_ = origin; |
| 584 | } |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 | [diff] [blame] | 585 | |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 586 | webrtc::TurnCustomizer* turn_customizer() { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 587 | CheckRunOnValidThreadIfInitialized(); |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 588 | return turn_customizer_; |
| 589 | } |
| 590 | |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 591 | // Collect candidate stats from pooled allocator sessions. This can be used to |
| 592 | // collect candidate stats without creating an offer/answer or setting local |
| 593 | // description. After the local description is set, the ownership of the |
| 594 | // pooled session is taken by P2PTransportChannel, and the |
| 595 | // candidate stats can be collected from P2PTransportChannel::GetStats. |
| 596 | virtual void GetCandidateStatsFromPooledSessions( |
| 597 | CandidateStatsList* candidate_stats_list); |
| 598 | |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 599 | // Return IceParameters of the pooled sessions. |
| 600 | std::vector<IceParameters> GetPooledIceCredentials(); |
| 601 | |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 602 | // Fired when |candidate_filter_| changes. |
| 603 | sigslot::signal2<uint32_t /* prev_filter */, uint32_t /* cur_filter */> |
| 604 | SignalCandidateFilterChanged; |
| 605 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 606 | protected: |
| 607 | virtual PortAllocatorSession* CreateSessionInternal( |
| 608 | const std::string& content_name, |
| 609 | int component, |
| 610 | const std::string& ice_ufrag, |
| 611 | const std::string& ice_pwd) = 0; |
| 612 | |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 613 | const std::vector<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() { |
Honghai Zhang | d93f50c | 2016-10-05 18:47:22 | [diff] [blame] | 614 | return pooled_sessions_; |
| 615 | } |
| 616 | |
Qingsi Wang | 7627fdd | 2019-08-19 23:07:40 | [diff] [blame] | 617 | // Returns true if there is an mDNS responder attached to the network manager. |
| 618 | virtual bool MdnsObfuscationEnabled() const { return false; } |
| 619 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 620 | // The following thread checks are only done in DCHECK for the consistency |
| 621 | // with the exsiting thread checks. |
| 622 | void CheckRunOnValidThreadIfInitialized() const { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 623 | RTC_DCHECK(!initialized_ || thread_checker_.IsCurrent()); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void CheckRunOnValidThreadAndInitialized() const { |
Sebastian Jansson | c01367d | 2019-04-08 13:20:44 | [diff] [blame] | 627 | RTC_DCHECK(initialized_ && thread_checker_.IsCurrent()); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | bool initialized_ = false; |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 631 | uint32_t flags_; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 632 | std::string agent_; |
| 633 | rtc::ProxyInfo proxy_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 634 | int min_port_; |
| 635 | int max_port_; |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 636 | int max_ipv6_networks_; |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 637 | uint32_t step_delay_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 638 | bool allow_tcp_listen_; |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 639 | uint32_t candidate_filter_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 | [diff] [blame] | 640 | std::string origin_; |
Artem Titov | 06159aa | 2021-02-09 22:38:13 | [diff] [blame] | 641 | webrtc::SequenceChecker thread_checker_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 642 | |
| 643 | private: |
| 644 | ServerAddresses stun_servers_; |
| 645 | std::vector<RelayServerConfig> turn_servers_; |
deadbeef | 6de92f9 | 2016-12-13 02:49:32 | [diff] [blame] | 646 | int candidate_pool_size_ = 0; // Last value passed into SetConfiguration. |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 647 | std::vector<std::unique_ptr<PortAllocatorSession>> pooled_sessions_; |
deadbeef | 42a4263 | 2017-03-10 23:18:00 | [diff] [blame] | 648 | bool candidate_pool_frozen_ = false; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 649 | webrtc::PortPrunePolicy turn_port_prune_policy_ = webrtc::NO_PRUNE; |
Honghai Zhang | d93f50c | 2016-10-05 18:47:22 | [diff] [blame] | 650 | |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 651 | // Customizer for TURN messages. |
| 652 | // The instance is owned by application and will be shared among |
| 653 | // all TurnPort(s) created. |
| 654 | webrtc::TurnCustomizer* turn_customizer_ = nullptr; |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 655 | |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 656 | absl::optional<int> stun_candidate_keepalive_interval_; |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 657 | |
| 658 | // If true, TakePooledSession() will only return sessions that has same ice |
| 659 | // credentials as requested. |
| 660 | bool restrict_ice_credentials_change_ = false; |
| 661 | |
| 662 | // Returns iterator to pooled session with specified ice_credentials or first |
| 663 | // if ice_credentials is nullptr. |
| 664 | std::vector<std::unique_ptr<PortAllocatorSession>>::const_iterator |
| 665 | FindPooledSession(const IceParameters* ice_credentials = nullptr) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 666 | }; |
| 667 | |
| 668 | } // namespace cricket |
| 669 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 670 | #endif // P2P_BASE_PORT_ALLOCATOR_H_ |