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" |
| 19 | #include "webrtc/base/messagequeue.h" |
| 20 | #include "webrtc/base/network.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 21 | #include "webrtc/base/thread.h" |
| 22 | |
| 23 | namespace cricket { |
| 24 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 25 | class BasicPortAllocator : public PortAllocator { |
| 26 | public: |
| 27 | BasicPortAllocator(rtc::NetworkManager* network_manager, |
| 28 | rtc::PacketSocketFactory* socket_factory); |
| 29 | explicit BasicPortAllocator(rtc::NetworkManager* network_manager); |
| 30 | BasicPortAllocator(rtc::NetworkManager* network_manager, |
| 31 | rtc::PacketSocketFactory* socket_factory, |
| 32 | const ServerAddresses& stun_servers); |
| 33 | BasicPortAllocator(rtc::NetworkManager* network_manager, |
| 34 | const ServerAddresses& stun_servers, |
| 35 | const rtc::SocketAddress& relay_server_udp, |
| 36 | const rtc::SocketAddress& relay_server_tcp, |
| 37 | const rtc::SocketAddress& relay_server_ssl); |
| 38 | virtual ~BasicPortAllocator(); |
| 39 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 14:14:52 -0800 | [diff] [blame] | 40 | // Set to kDefaultNetworkIgnoreMask by default. |
| 41 | void SetNetworkIgnoreMask(int network_ignore_mask) override { |
| 42 | // TODO(phoglund): implement support for other types than loopback. |
| 43 | // See https://code.google.com/p/webrtc/issues/detail?id=4288. |
| 44 | // Then remove set_network_ignore_list from NetworkManager. |
| 45 | network_ignore_mask_ = network_ignore_mask; |
| 46 | } |
| 47 | |
| 48 | int network_ignore_mask() const { return network_ignore_mask_; } |
| 49 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 50 | rtc::NetworkManager* network_manager() { return network_manager_; } |
| 51 | |
| 52 | // If socket_factory() is set to NULL each PortAllocatorSession |
| 53 | // creates its own socket factory. |
| 54 | rtc::PacketSocketFactory* socket_factory() { return socket_factory_; } |
| 55 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 56 | PortAllocatorSession* CreateSessionInternal( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 57 | const std::string& content_name, |
| 58 | int component, |
| 59 | const std::string& ice_ufrag, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 60 | const std::string& ice_pwd) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 61 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 62 | // Convenience method that adds a TURN server to the configuration. |
| 63 | void AddTurnServer(const RelayServerConfig& turn_server); |
| 64 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | private: |
| 66 | void Construct(); |
| 67 | |
| 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 | |
| 77 | class BasicPortAllocatorSession : public PortAllocatorSession, |
| 78 | public rtc::MessageHandler { |
| 79 | public: |
| 80 | BasicPortAllocatorSession(BasicPortAllocator* allocator, |
| 81 | const std::string& content_name, |
| 82 | int component, |
| 83 | const std::string& ice_ufrag, |
| 84 | const std::string& ice_pwd); |
| 85 | ~BasicPortAllocatorSession(); |
| 86 | |
| 87 | virtual BasicPortAllocator* allocator() { return allocator_; } |
| 88 | rtc::Thread* network_thread() { return network_thread_; } |
| 89 | rtc::PacketSocketFactory* socket_factory() { return socket_factory_; } |
| 90 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 91 | void StartGettingPorts() override; |
| 92 | void StopGettingPorts() override; |
| 93 | void ClearGettingPorts() override; |
| 94 | bool IsGettingPorts() override { return running_; } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 95 | // These will all be cricket::Ports. |
| 96 | std::vector<PortInterface*> ReadyPorts() const override; |
| 97 | std::vector<Candidate> ReadyCandidates() const override; |
| 98 | bool CandidatesAllocationDone() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 99 | |
| 100 | protected: |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 101 | void UpdateIceParametersInternal() override; |
| 102 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 103 | // Starts the process of getting the port configurations. |
| 104 | virtual void GetPortConfigurations(); |
| 105 | |
| 106 | // Adds a port configuration that is now ready. Once we have one for each |
| 107 | // network (or a timeout occurs), we will start allocating ports. |
| 108 | virtual void ConfigReady(PortConfiguration* config); |
| 109 | |
| 110 | // MessageHandler. Can be overriden if message IDs do not conflict. |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 111 | void OnMessage(rtc::Message* message) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | class PortData { |
| 115 | public: |
| 116 | PortData() : port_(NULL), sequence_(NULL), state_(STATE_INIT) {} |
| 117 | PortData(Port* port, AllocationSequence* seq) |
| 118 | : port_(port), sequence_(seq), state_(STATE_INIT) { |
| 119 | } |
| 120 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 121 | Port* port() const { return port_; } |
| 122 | AllocationSequence* sequence() const { return sequence_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 123 | bool ready() const { return state_ == STATE_READY; } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 124 | bool complete() const { return state_ == STATE_COMPLETE; } |
| 125 | bool error() const { return state_ == STATE_ERROR; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 126 | |
| 127 | void set_ready() { ASSERT(state_ == STATE_INIT); state_ = STATE_READY; } |
| 128 | void set_complete() { |
| 129 | state_ = STATE_COMPLETE; |
| 130 | } |
| 131 | void set_error() { |
| 132 | ASSERT(state_ == STATE_INIT || state_ == STATE_READY); |
| 133 | state_ = STATE_ERROR; |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | enum State { |
| 138 | STATE_INIT, // No candidates allocated yet. |
| 139 | STATE_READY, // At least one candidate is ready for process. |
| 140 | STATE_COMPLETE, // All candidates allocated and ready for process. |
| 141 | STATE_ERROR // Error in gathering candidates. |
| 142 | }; |
| 143 | Port* port_; |
| 144 | AllocationSequence* sequence_; |
| 145 | State state_; |
| 146 | }; |
| 147 | |
| 148 | void OnConfigReady(PortConfiguration* config); |
| 149 | void OnConfigStop(); |
| 150 | void AllocatePorts(); |
| 151 | void OnAllocate(); |
| 152 | void DoAllocate(); |
| 153 | void OnNetworksChanged(); |
| 154 | void OnAllocationSequenceObjectsCreated(); |
| 155 | void DisableEquivalentPhases(rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 156 | PortConfiguration* config, |
| 157 | uint32_t* flags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 158 | void AddAllocatedPort(Port* port, AllocationSequence* seq, |
| 159 | bool prepare_address); |
| 160 | void OnCandidateReady(Port* port, const Candidate& c); |
| 161 | void OnPortComplete(Port* port); |
| 162 | void OnPortError(Port* port); |
| 163 | void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto); |
| 164 | void OnPortDestroyed(PortInterface* port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 165 | void MaybeSignalCandidatesAllocationDone(); |
| 166 | void OnPortAllocationComplete(AllocationSequence* seq); |
| 167 | PortData* FindPort(Port* port); |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 168 | void GetNetworks(std::vector<rtc::Network*>* networks); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 169 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 170 | bool CheckCandidateFilter(const Candidate& c) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 171 | |
| 172 | BasicPortAllocator* allocator_; |
| 173 | rtc::Thread* network_thread_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 174 | std::unique_ptr<rtc::PacketSocketFactory> owned_socket_factory_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 175 | rtc::PacketSocketFactory* socket_factory_; |
| 176 | bool allocation_started_; |
| 177 | bool network_manager_started_; |
| 178 | bool running_; // set when StartGetAllPorts is called |
| 179 | bool allocation_sequences_created_; |
| 180 | std::vector<PortConfiguration*> configs_; |
| 181 | std::vector<AllocationSequence*> sequences_; |
| 182 | std::vector<PortData> ports_; |
| 183 | |
| 184 | friend class AllocationSequence; |
| 185 | }; |
| 186 | |
| 187 | // Records configuration information useful in creating ports. |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 188 | // TODO(deadbeef): Rename "relay" to "turn_server" in this struct. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 189 | struct PortConfiguration : public rtc::MessageData { |
| 190 | // TODO(jiayl): remove |stun_address| when Chrome is updated. |
| 191 | rtc::SocketAddress stun_address; |
| 192 | ServerAddresses stun_servers; |
| 193 | std::string username; |
| 194 | std::string password; |
| 195 | |
| 196 | typedef std::vector<RelayServerConfig> RelayList; |
| 197 | RelayList relays; |
| 198 | |
| 199 | // TODO(jiayl): remove this ctor when Chrome is updated. |
| 200 | PortConfiguration(const rtc::SocketAddress& stun_address, |
| 201 | const std::string& username, |
| 202 | const std::string& password); |
| 203 | |
| 204 | PortConfiguration(const ServerAddresses& stun_servers, |
| 205 | const std::string& username, |
| 206 | const std::string& password); |
| 207 | |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 208 | // Returns addresses of both the explicitly configured STUN servers, |
| 209 | // and TURN servers that should be used as STUN servers. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 210 | ServerAddresses StunServers(); |
| 211 | |
| 212 | // Adds another relay server, with the given ports and modifier, to the list. |
| 213 | void AddRelay(const RelayServerConfig& config); |
| 214 | |
| 215 | // Determines whether the given relay server supports the given protocol. |
| 216 | bool SupportsProtocol(const RelayServerConfig& relay, |
| 217 | ProtocolType type) const; |
| 218 | bool SupportsProtocol(RelayType turn_type, ProtocolType type) const; |
| 219 | // Helper method returns the server addresses for the matching RelayType and |
| 220 | // Protocol type. |
| 221 | ServerAddresses GetRelayServerAddresses( |
| 222 | RelayType turn_type, ProtocolType type) const; |
| 223 | }; |
| 224 | |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 225 | class UDPPort; |
| 226 | class TurnPort; |
| 227 | |
| 228 | // Performs the allocation of ports, in a sequenced (timed) manner, for a given |
| 229 | // network and IP address. |
| 230 | class AllocationSequence : public rtc::MessageHandler, |
| 231 | public sigslot::has_slots<> { |
| 232 | public: |
| 233 | enum State { |
| 234 | kInit, // Initial state. |
| 235 | kRunning, // Started allocating ports. |
| 236 | kStopped, // Stopped from running. |
| 237 | kCompleted, // All ports are allocated. |
| 238 | |
| 239 | // kInit --> kRunning --> {kCompleted|kStopped} |
| 240 | }; |
| 241 | AllocationSequence(BasicPortAllocatorSession* session, |
| 242 | rtc::Network* network, |
| 243 | PortConfiguration* config, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 244 | uint32_t flags); |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 245 | ~AllocationSequence(); |
| 246 | bool Init(); |
| 247 | void Clear(); |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 248 | void OnNetworkRemoved(); |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 249 | |
| 250 | State state() const { return state_; } |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 251 | const rtc::Network* network() const { return network_; } |
| 252 | bool network_removed() const { return network_removed_; } |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 253 | |
| 254 | // Disables the phases for a new sequence that this one already covers for an |
| 255 | // equivalent network setup. |
| 256 | void DisableEquivalentPhases(rtc::Network* network, |
| 257 | PortConfiguration* config, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 258 | uint32_t* flags); |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 259 | |
| 260 | // Starts and stops the sequence. When started, it will continue allocating |
| 261 | // new ports on its own timed schedule. |
| 262 | void Start(); |
| 263 | void Stop(); |
| 264 | |
| 265 | // MessageHandler |
| 266 | void OnMessage(rtc::Message* msg); |
| 267 | |
| 268 | void EnableProtocol(ProtocolType proto); |
| 269 | bool ProtocolEnabled(ProtocolType proto) const; |
| 270 | |
| 271 | // Signal from AllocationSequence, when it's done with allocating ports. |
| 272 | // This signal is useful, when port allocation fails which doesn't result |
| 273 | // in any candidates. Using this signal BasicPortAllocatorSession can send |
| 274 | // its candidate discovery conclusion signal. Without this signal, |
| 275 | // BasicPortAllocatorSession doesn't have any event to trigger signal. This |
| 276 | // can also be achieved by starting timer in BPAS. |
| 277 | sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete; |
| 278 | |
| 279 | protected: |
| 280 | // For testing. |
| 281 | void CreateTurnPort(const RelayServerConfig& config); |
| 282 | |
| 283 | private: |
| 284 | typedef std::vector<ProtocolType> ProtocolList; |
| 285 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 286 | bool IsFlagSet(uint32_t flag) { return ((flags_ & flag) != 0); } |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 287 | void CreateUDPPorts(); |
| 288 | void CreateTCPPorts(); |
| 289 | void CreateStunPorts(); |
| 290 | void CreateRelayPorts(); |
| 291 | void CreateGturnPort(const RelayServerConfig& config); |
| 292 | |
| 293 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 294 | const char* data, |
| 295 | size_t size, |
| 296 | const rtc::SocketAddress& remote_addr, |
| 297 | const rtc::PacketTime& packet_time); |
| 298 | |
| 299 | void OnPortDestroyed(PortInterface* port); |
| 300 | |
| 301 | BasicPortAllocatorSession* session_; |
honghaiz | 8c404fa | 2015-09-28 07:59:43 -0700 | [diff] [blame] | 302 | bool network_removed_ = false; |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 303 | rtc::Network* network_; |
| 304 | rtc::IPAddress ip_; |
| 305 | PortConfiguration* config_; |
| 306 | State state_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 307 | uint32_t flags_; |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 308 | ProtocolList protocols_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 309 | std::unique_ptr<rtc::AsyncPacketSocket> udp_socket_; |
honghaiz | f421bdc | 2015-07-17 16:21:55 -0700 | [diff] [blame] | 310 | // There will be only one udp port per AllocationSequence. |
| 311 | UDPPort* udp_port_; |
| 312 | std::vector<TurnPort*> turn_ports_; |
| 313 | int phase_; |
| 314 | }; |
| 315 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 316 | } // namespace cricket |
| 317 | |
| 318 | #endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_ |