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