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); |
| 185 | |
| 186 | bool CheckCandidateFilter(const Candidate& c); |
| 187 | |
| 188 | BasicPortAllocator* allocator_; |
| 189 | rtc::Thread* network_thread_; |
| 190 | rtc::scoped_ptr<rtc::PacketSocketFactory> owned_socket_factory_; |
| 191 | rtc::PacketSocketFactory* socket_factory_; |
| 192 | bool allocation_started_; |
| 193 | bool network_manager_started_; |
| 194 | bool running_; // set when StartGetAllPorts is called |
| 195 | bool allocation_sequences_created_; |
| 196 | std::vector<PortConfiguration*> configs_; |
| 197 | std::vector<AllocationSequence*> sequences_; |
| 198 | std::vector<PortData> ports_; |
| 199 | |
| 200 | friend class AllocationSequence; |
| 201 | }; |
| 202 | |
| 203 | // Records configuration information useful in creating ports. |
| 204 | struct PortConfiguration : public rtc::MessageData { |
| 205 | // TODO(jiayl): remove |stun_address| when Chrome is updated. |
| 206 | rtc::SocketAddress stun_address; |
| 207 | ServerAddresses stun_servers; |
| 208 | std::string username; |
| 209 | std::string password; |
| 210 | |
| 211 | typedef std::vector<RelayServerConfig> RelayList; |
| 212 | RelayList relays; |
| 213 | |
| 214 | // TODO(jiayl): remove this ctor when Chrome is updated. |
| 215 | PortConfiguration(const rtc::SocketAddress& stun_address, |
| 216 | const std::string& username, |
| 217 | const std::string& password); |
| 218 | |
| 219 | PortConfiguration(const ServerAddresses& stun_servers, |
| 220 | const std::string& username, |
| 221 | const std::string& password); |
| 222 | |
| 223 | // TODO(jiayl): remove when |stun_address| is removed. |
| 224 | ServerAddresses StunServers(); |
| 225 | |
| 226 | // Adds another relay server, with the given ports and modifier, to the list. |
| 227 | void AddRelay(const RelayServerConfig& config); |
| 228 | |
| 229 | // Determines whether the given relay server supports the given protocol. |
| 230 | bool SupportsProtocol(const RelayServerConfig& relay, |
| 231 | ProtocolType type) const; |
| 232 | bool SupportsProtocol(RelayType turn_type, ProtocolType type) const; |
| 233 | // Helper method returns the server addresses for the matching RelayType and |
| 234 | // Protocol type. |
| 235 | ServerAddresses GetRelayServerAddresses( |
| 236 | RelayType turn_type, ProtocolType type) const; |
| 237 | }; |
| 238 | |
| 239 | } // namespace cricket |
| 240 | |
| 241 | #endif // WEBRTC_P2P_CLIENT_BASICPORTALLOCATOR_H_ |