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