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