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