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_CLIENT_BASICPORTALLOCATOR_H_ |
| 12 | #define P2P_CLIENT_BASICPORTALLOCATOR_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 14 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 18 | #include "api/turncustomizer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "p2p/base/portallocator.h" |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 20 | #include "p2p/client/relayportfactoryinterface.h" |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 21 | #include "p2p/client/turnportfactory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/messagequeue.h" |
| 24 | #include "rtc_base/network.h" |
Mirko Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 25 | #include "rtc_base/system/rtc_export.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/thread.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 27 | |
| 28 | namespace cricket { |
| 29 | |
Mirko Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 30 | class RTC_EXPORT BasicPortAllocator : public PortAllocator { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 31 | public: |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 32 | // note: The (optional) relay_port_factory is owned by caller |
| 33 | // and must have a life time that exceeds that of BasicPortAllocator. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 34 | BasicPortAllocator(rtc::NetworkManager* network_manager, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 35 | rtc::PacketSocketFactory* socket_factory, |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 36 | webrtc::TurnCustomizer* customizer = nullptr, |
| 37 | RelayPortFactoryInterface* relay_port_factory = nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 38 | explicit BasicPortAllocator(rtc::NetworkManager* network_manager); |
| 39 | BasicPortAllocator(rtc::NetworkManager* network_manager, |
| 40 | rtc::PacketSocketFactory* socket_factory, |
| 41 | const ServerAddresses& stun_servers); |
| 42 | BasicPortAllocator(rtc::NetworkManager* network_manager, |
| 43 | const ServerAddresses& stun_servers, |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 44 | const rtc::SocketAddress& relay_address_udp, |
| 45 | const rtc::SocketAddress& relay_address_tcp, |
| 46 | const rtc::SocketAddress& relay_address_ssl); |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 47 | ~BasicPortAllocator() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 48 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 49 | // Set to kDefaultNetworkIgnoreMask by default. |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 50 | void SetNetworkIgnoreMask(int network_ignore_mask) override; |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 51 | int network_ignore_mask() const { |
| 52 | CheckRunOnValidThreadIfInitialized(); |
| 53 | return network_ignore_mask_; |
| 54 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 55 | |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 56 | rtc::NetworkManager* network_manager() const { |
| 57 | CheckRunOnValidThreadIfInitialized(); |
| 58 | return network_manager_; |
| 59 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 60 | |
| 61 | // If socket_factory() is set to NULL each PortAllocatorSession |
| 62 | // creates its own socket factory. |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 63 | rtc::PacketSocketFactory* socket_factory() { |
| 64 | CheckRunOnValidThreadIfInitialized(); |
| 65 | return socket_factory_; |
| 66 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 67 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 68 | PortAllocatorSession* CreateSessionInternal( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | const std::string& content_name, |
| 70 | int component, |
| 71 | const std::string& ice_ufrag, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 72 | const std::string& ice_pwd) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 73 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 74 | // Convenience method that adds a TURN server to the configuration. |
| 75 | void AddTurnServer(const RelayServerConfig& turn_server); |
| 76 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 77 | RelayPortFactoryInterface* relay_port_factory() { |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 78 | CheckRunOnValidThreadIfInitialized(); |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 79 | return relay_port_factory_; |
| 80 | } |
| 81 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | private: |
| 83 | void Construct(); |
| 84 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 85 | void OnIceRegathering(PortAllocatorSession* session, |
| 86 | IceRegatheringReason reason); |
| 87 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 88 | // This function makes sure that relay_port_factory_ is set properly. |
| 89 | void InitRelayPortFactory(RelayPortFactoryInterface* relay_port_factory); |
| 90 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 91 | rtc::NetworkManager* network_manager_; |
| 92 | rtc::PacketSocketFactory* socket_factory_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 93 | bool allow_tcp_listen_; |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 94 | int network_ignore_mask_ = rtc::kDefaultNetworkIgnoreMask; |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 95 | |
| 96 | // This is the factory being used. |
| 97 | RelayPortFactoryInterface* relay_port_factory_; |
| 98 | |
| 99 | // This instance is created if caller does pass a factory. |
| 100 | std::unique_ptr<RelayPortFactoryInterface> default_relay_port_factory_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | struct PortConfiguration; |
| 104 | class AllocationSequence; |
| 105 | |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 106 | enum class SessionState { |
| 107 | GATHERING, // Actively allocating ports and gathering candidates. |
| 108 | CLEARED, // Current allocation process has been stopped but may start |
| 109 | // new ones. |
| 110 | STOPPED // This session has completely stopped, no new allocation |
| 111 | // process will be started. |
| 112 | }; |
| 113 | |
Mirko Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 114 | class RTC_EXPORT BasicPortAllocatorSession : public PortAllocatorSession, |
| 115 | public rtc::MessageHandler { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 116 | public: |
| 117 | BasicPortAllocatorSession(BasicPortAllocator* allocator, |
| 118 | const std::string& content_name, |
| 119 | int component, |
| 120 | const std::string& ice_ufrag, |
| 121 | const std::string& ice_pwd); |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 122 | ~BasicPortAllocatorSession() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 123 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 124 | virtual BasicPortAllocator* allocator(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 125 | rtc::Thread* network_thread() { return network_thread_; } |
| 126 | rtc::PacketSocketFactory* socket_factory() { return socket_factory_; } |
| 127 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 128 | void SetCandidateFilter(uint32_t filter) override; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 129 | void StartGettingPorts() override; |
| 130 | void StopGettingPorts() override; |
| 131 | void ClearGettingPorts() override; |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 132 | bool IsGettingPorts() override; |
| 133 | bool IsCleared() const override; |
| 134 | bool IsStopped() const override; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 135 | // These will all be cricket::Ports. |
| 136 | std::vector<PortInterface*> ReadyPorts() const override; |
| 137 | std::vector<Candidate> ReadyCandidates() const override; |
| 138 | bool CandidatesAllocationDone() const override; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 139 | void RegatherOnFailedNetworks() override; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 140 | void RegatherOnAllNetworks() override; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 141 | void SetStunKeepaliveIntervalForReadyPorts( |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 142 | const absl::optional<int>& stun_keepalive_interval) override; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 143 | void PruneAllPorts() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 144 | |
| 145 | protected: |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 146 | void UpdateIceParametersInternal() override; |
| 147 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 148 | // Starts the process of getting the port configurations. |
| 149 | virtual void GetPortConfigurations(); |
| 150 | |
| 151 | // Adds a port configuration that is now ready. Once we have one for each |
| 152 | // network (or a timeout occurs), we will start allocating ports. |
| 153 | virtual void ConfigReady(PortConfiguration* config); |
| 154 | |
| 155 | // MessageHandler. Can be overriden if message IDs do not conflict. |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 156 | void OnMessage(rtc::Message* message) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 157 | |
| 158 | private: |
| 159 | class PortData { |
| 160 | public: |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 161 | PortData() {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 162 | PortData(Port* port, AllocationSequence* seq) |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 163 | : port_(port), sequence_(seq) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 164 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 165 | Port* port() const { return port_; } |
| 166 | AllocationSequence* sequence() const { return sequence_; } |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 167 | bool has_pairable_candidate() const { return has_pairable_candidate_; } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 168 | bool complete() const { return state_ == STATE_COMPLETE; } |
| 169 | bool error() const { return state_ == STATE_ERROR; } |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 170 | bool pruned() const { return state_ == STATE_PRUNED; } |
| 171 | bool inprogress() const { return state_ == STATE_INPROGRESS; } |
| 172 | // Returns true if this port is ready to be used. |
| 173 | bool ready() const { |
| 174 | return has_pairable_candidate_ && state_ != STATE_ERROR && |
| 175 | state_ != STATE_PRUNED; |
| 176 | } |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 177 | // Sets the state to "PRUNED" and prunes the Port. |
| 178 | void Prune() { |
| 179 | state_ = STATE_PRUNED; |
| 180 | if (port()) { |
| 181 | port()->Prune(); |
| 182 | } |
| 183 | } |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 184 | void set_has_pairable_candidate(bool has_pairable_candidate) { |
| 185 | if (has_pairable_candidate) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 186 | RTC_DCHECK(state_ == STATE_INPROGRESS); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 187 | } |
| 188 | has_pairable_candidate_ = has_pairable_candidate; |
| 189 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 190 | void set_complete() { state_ = STATE_COMPLETE; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 191 | void set_error() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 192 | RTC_DCHECK(state_ == STATE_INPROGRESS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 193 | state_ = STATE_ERROR; |
| 194 | } |
| 195 | |
| 196 | private: |
| 197 | enum State { |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 198 | STATE_INPROGRESS, // Still gathering candidates. |
| 199 | STATE_COMPLETE, // All candidates allocated and ready for process. |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 200 | STATE_ERROR, // Error in gathering candidates. |
| 201 | STATE_PRUNED // Pruned by higher priority ports on the same network |
| 202 | // interface. Only TURN ports may be pruned. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 203 | }; |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 204 | Port* port_ = nullptr; |
| 205 | AllocationSequence* sequence_ = nullptr; |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 206 | bool has_pairable_candidate_ = false; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 207 | State state_ = STATE_INPROGRESS; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | void OnConfigReady(PortConfiguration* config); |
| 211 | void OnConfigStop(); |
| 212 | void AllocatePorts(); |
| 213 | void OnAllocate(); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 214 | void DoAllocate(bool disable_equivalent_phases); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 215 | void OnNetworksChanged(); |
| 216 | void OnAllocationSequenceObjectsCreated(); |
| 217 | void DisableEquivalentPhases(rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 218 | PortConfiguration* config, |
| 219 | uint32_t* flags); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 220 | void AddAllocatedPort(Port* port, |
| 221 | AllocationSequence* seq, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 222 | bool prepare_address); |
| 223 | void OnCandidateReady(Port* port, const Candidate& c); |
| 224 | void OnPortComplete(Port* port); |
| 225 | void OnPortError(Port* port); |
| 226 | void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto); |
| 227 | void OnPortDestroyed(PortInterface* port); |
Mirko Bonadei | 5f4d47b | 2018-08-22 17:41:22 +0000 | [diff] [blame] | 228 | void MaybeSignalCandidatesAllocationDone(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 229 | void OnPortAllocationComplete(AllocationSequence* seq); |
| 230 | PortData* FindPort(Port* port); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 231 | std::vector<rtc::Network*> GetNetworks(); |
| 232 | std::vector<rtc::Network*> GetFailedNetworks(); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 233 | void Regather(const std::vector<rtc::Network*>& networks, |
| 234 | bool disable_equivalent_phases, |
| 235 | IceRegatheringReason reason); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 236 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 237 | bool CheckCandidateFilter(const Candidate& c) const; |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 238 | bool CandidatePairable(const Candidate& c, const Port* port) const; |
Jeroen de Borst | 72d2ddd | 2018-11-27 13:20:39 -0800 | [diff] [blame] | 239 | |
| 240 | // Returns true if there is an mDNS responder attached to the network manager |
| 241 | bool MdnsObfuscationEnabled() const; |
| 242 | |
Qingsi Wang | b49b8f1 | 2018-09-16 17:48:10 -0700 | [diff] [blame] | 243 | // Clears 1) the address if the candidate is supposedly a hostname candidate; |
| 244 | // 2) the related address according to the flags and candidate filter in order |
| 245 | // to avoid leaking any information. |
| 246 | Candidate SanitizeCandidate(const Candidate& c) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 247 | |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 248 | std::vector<PortData*> GetUnprunedPorts( |
| 249 | const std::vector<rtc::Network*>& networks); |
| 250 | // Prunes ports and signal the remote side to remove the candidates that |
| 251 | // were previously signaled from these ports. |
Mirko Bonadei | 5f4d47b | 2018-08-22 17:41:22 +0000 | [diff] [blame] | 252 | void PrunePortsAndRemoveCandidates( |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 253 | const std::vector<PortData*>& port_data_list); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 254 | // Gets filtered and sanitized candidates generated from a port and |
| 255 | // append to |candidates|. |
| 256 | void GetCandidatesFromPort(const PortData& data, |
| 257 | std::vector<Candidate>* candidates) const; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 258 | Port* GetBestTurnPortForNetwork(const std::string& network_name) const; |
| 259 | // Returns true if at least one TURN port is pruned. |
| 260 | bool PruneTurnPorts(Port* newly_pairable_turn_port); |
| 261 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 262 | BasicPortAllocator* allocator_; |
| 263 | rtc::Thread* network_thread_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 264 | std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 265 | rtc::PacketSocketFactory* socket_factory_; |
| 266 | bool allocation_started_; |
| 267 | bool network_manager_started_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 268 | bool allocation_sequences_created_; |
| 269 | std::vector<PortConfiguration*> configs_; |
| 270 | std::vector<AllocationSequence*> sequences_; |
| 271 | std::vector<PortData> ports_; |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 272 | uint32_t candidate_filter_ = CF_ALL; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 273 | // Whether to prune low-priority ports, taken from the port allocator. |
| 274 | bool prune_turn_ports_; |
Honghai Zhang | d8f6fc4 | 2016-07-01 17:31:12 -0700 | [diff] [blame] | 275 | SessionState state_ = SessionState::CLEARED; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 276 | |
| 277 | friend class AllocationSequence; |
| 278 | }; |
| 279 | |
| 280 | // Records configuration information useful in creating ports. |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 281 | // TODO(deadbeef): Rename "relay" to "turn_server" in this struct. |
Mirko Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 282 | struct RTC_EXPORT PortConfiguration : public rtc::MessageData { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 283 | // TODO(jiayl): remove |stun_address| when Chrome is updated. |
| 284 | rtc::SocketAddress stun_address; |
| 285 | ServerAddresses stun_servers; |
| 286 | std::string username; |
| 287 | std::string password; |
| 288 | |
| 289 | typedef std::vector<RelayServerConfig> RelayList; |
| 290 | RelayList relays; |
| 291 | |
| 292 | // TODO(jiayl): remove this ctor when Chrome is updated. |
| 293 | PortConfiguration(const rtc::SocketAddress& stun_address, |
| 294 | const std::string& username, |
| 295 | const std::string& password); |
| 296 | |
| 297 | PortConfiguration(const ServerAddresses& stun_servers, |
| 298 | const std::string& username, |
| 299 | const std::string& password); |
| 300 | |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 301 | ~PortConfiguration() override; |
| 302 | |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 303 | // Returns addresses of both the explicitly configured STUN servers, |
| 304 | // and TURN servers that should be used as STUN servers. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 305 | ServerAddresses StunServers(); |
| 306 | |
| 307 | // Adds another relay server, with the given ports and modifier, to the list. |
| 308 | void AddRelay(const RelayServerConfig& config); |
| 309 | |
| 310 | // Determines whether the given relay server supports the given protocol. |
| 311 | bool SupportsProtocol(const RelayServerConfig& relay, |
| 312 | ProtocolType type) const; |
| 313 | bool SupportsProtocol(RelayType turn_type, ProtocolType type) const; |
| 314 | // Helper method returns the server addresses for the matching RelayType and |
| 315 | // Protocol type. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 316 | ServerAddresses GetRelayServerAddresses(RelayType turn_type, |
| 317 | ProtocolType type) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 320 | class UDPPort; |
| 321 | class TurnPort; |
| 322 | |
| 323 | // Performs the allocation of ports, in a sequenced (timed) manner, for a given |
| 324 | // network and IP address. |
| 325 | class AllocationSequence : public rtc::MessageHandler, |
| 326 | public sigslot::has_slots<> { |
| 327 | public: |
| 328 | enum State { |
| 329 | kInit, // Initial state. |
| 330 | kRunning, // Started allocating ports. |
| 331 | kStopped, // Stopped from running. |
| 332 | kCompleted, // All ports are allocated. |
| 333 | |
| 334 | // kInit --> kRunning --> {kCompleted|kStopped} |
| 335 | }; |
| 336 | AllocationSequence(BasicPortAllocatorSession* session, |
| 337 | rtc::Network* network, |
| 338 | PortConfiguration* config, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 339 | uint32_t flags); |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 340 | ~AllocationSequence() override; |
Honghai Zhang | 5048f57 | 2016-08-23 15:47:33 -0700 | [diff] [blame] | 341 | void Init(); |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 342 | void Clear(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 343 | void OnNetworkFailed(); |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 344 | |
| 345 | State state() const { return state_; } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 346 | rtc::Network* network() const { return network_; } |
| 347 | |
| 348 | bool network_failed() const { return network_failed_; } |
| 349 | void set_network_failed() { network_failed_ = true; } |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 350 | |
| 351 | // Disables the phases for a new sequence that this one already covers for an |
| 352 | // equivalent network setup. |
| 353 | void DisableEquivalentPhases(rtc::Network* network, |
| 354 | PortConfiguration* config, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 355 | uint32_t* flags); |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 356 | |
| 357 | // Starts and stops the sequence. When started, it will continue allocating |
| 358 | // new ports on its own timed schedule. |
| 359 | void Start(); |
| 360 | void Stop(); |
| 361 | |
| 362 | // MessageHandler |
Steve Anton | 7995d8c | 2017-10-30 16:23:38 -0700 | [diff] [blame] | 363 | void OnMessage(rtc::Message* msg) override; |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 364 | |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 365 | // Signal from AllocationSequence, when it's done with allocating ports. |
| 366 | // This signal is useful, when port allocation fails which doesn't result |
| 367 | // in any candidates. Using this signal BasicPortAllocatorSession can send |
| 368 | // its candidate discovery conclusion signal. Without this signal, |
| 369 | // BasicPortAllocatorSession doesn't have any event to trigger signal. This |
| 370 | // can also be achieved by starting timer in BPAS. |
| 371 | sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete; |
| 372 | |
| 373 | protected: |
| 374 | // For testing. |
| 375 | void CreateTurnPort(const RelayServerConfig& config); |
| 376 | |
| 377 | private: |
| 378 | typedef std::vector<ProtocolType> ProtocolList; |
| 379 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 380 | bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); } |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 381 | void CreateUDPPorts(); |
| 382 | void CreateTCPPorts(); |
| 383 | void CreateStunPorts(); |
| 384 | void CreateRelayPorts(); |
| 385 | void CreateGturnPort(const RelayServerConfig& config); |
| 386 | |
| 387 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 388 | const char* data, |
| 389 | size_t size, |
| 390 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 391 | const int64_t& packet_time_us); |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 392 | |
| 393 | void OnPortDestroyed(PortInterface* port); |
| 394 | |
| 395 | BasicPortAllocatorSession* session_; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 396 | bool network_failed_ = false; |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 397 | rtc::Network* network_; |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 398 | // Compared with the new best IP in DisableEquivalentPhases. |
| 399 | rtc::IPAddress previous_best_ip_; |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 400 | PortConfiguration* config_; |
| 401 | State state_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 402 | uint32_t flags_; |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 403 | ProtocolList protocols_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 404 | std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_; |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 405 | // There will be only one udp port per AllocationSequence. |
| 406 | UDPPort* udp_port_; |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 407 | std::vector<Port*> relay_ports_; |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 408 | int phase_; |
| 409 | }; |
| 410 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 411 | } // namespace cricket |
| 412 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 413 | #endif // P2P_CLIENT_BASICPORTALLOCATOR_H_ |