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