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