port_allocator.h 26 KB

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