| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_P2P_BASE_PORTALLOCATOR_H_ | 11 #ifndef WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |
| 12 #define WEBRTC_P2P_BASE_PORTALLOCATOR_H_ | 12 #define WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |
| 13 | 13 |
| 14 #include <deque> |
| 15 #include <memory> |
| 14 #include <string> | 16 #include <string> |
| 15 #include <vector> | 17 #include <vector> |
| 16 | 18 |
| 17 #include "webrtc/p2p/base/port.h" | 19 #include "webrtc/p2p/base/port.h" |
| 18 #include "webrtc/p2p/base/portinterface.h" | 20 #include "webrtc/p2p/base/portinterface.h" |
| 19 #include "webrtc/base/helpers.h" | 21 #include "webrtc/base/helpers.h" |
| 20 #include "webrtc/base/proxyinfo.h" | 22 #include "webrtc/base/proxyinfo.h" |
| 21 #include "webrtc/base/sigslot.h" | 23 #include "webrtc/base/sigslot.h" |
| 24 #include "webrtc/base/thread.h" |
| 22 | 25 |
| 23 namespace cricket { | 26 namespace cricket { |
| 24 | 27 |
| 25 // PortAllocator is responsible for allocating Port types for a given | 28 // PortAllocator is responsible for allocating Port types for a given |
| 26 // P2PSocket. It also handles port freeing. | 29 // P2PSocket. It also handles port freeing. |
| 27 // | 30 // |
| 28 // Clients can override this class to control port allocation, including | 31 // Clients can override this class to control port allocation, including |
| 29 // what kinds of ports are allocated. | 32 // what kinds of ports are allocated. |
| 30 | 33 |
| 31 enum { | 34 enum { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 CF_RELAY = 0x4, | 78 CF_RELAY = 0x4, |
| 76 CF_ALL = 0x7, | 79 CF_ALL = 0x7, |
| 77 }; | 80 }; |
| 78 | 81 |
| 79 // TODO(deadbeef): Rename to TurnCredentials (and username to ufrag). | 82 // TODO(deadbeef): Rename to TurnCredentials (and username to ufrag). |
| 80 struct RelayCredentials { | 83 struct RelayCredentials { |
| 81 RelayCredentials() {} | 84 RelayCredentials() {} |
| 82 RelayCredentials(const std::string& username, const std::string& password) | 85 RelayCredentials(const std::string& username, const std::string& password) |
| 83 : username(username), password(password) {} | 86 : username(username), password(password) {} |
| 84 | 87 |
| 88 bool operator==(const RelayCredentials& o) const { |
| 89 return username == o.username && password == o.password; |
| 90 } |
| 91 bool operator!=(const RelayCredentials& o) const { return !(*this == o); } |
| 92 |
| 85 std::string username; | 93 std::string username; |
| 86 std::string password; | 94 std::string password; |
| 87 }; | 95 }; |
| 88 | 96 |
| 89 typedef std::vector<ProtocolAddress> PortList; | 97 typedef std::vector<ProtocolAddress> PortList; |
| 90 // TODO(deadbeef): Rename to TurnServerConfig. | 98 // TODO(deadbeef): Rename to TurnServerConfig. |
| 91 struct RelayServerConfig { | 99 struct RelayServerConfig { |
| 92 RelayServerConfig(RelayType type) : type(type), priority(0) {} | 100 RelayServerConfig(RelayType type) : type(type) {} |
| 93 | 101 |
| 94 RelayServerConfig(const std::string& address, | 102 RelayServerConfig(const std::string& address, |
| 95 int port, | 103 int port, |
| 96 const std::string& username, | 104 const std::string& username, |
| 97 const std::string& password, | 105 const std::string& password, |
| 98 ProtocolType proto, | 106 ProtocolType proto, |
| 99 bool secure) | 107 bool secure) |
| 100 : type(RELAY_TURN), credentials(username, password) { | 108 : type(RELAY_TURN), credentials(username, password) { |
| 101 ports.push_back( | 109 ports.push_back( |
| 102 ProtocolAddress(rtc::SocketAddress(address, port), proto, secure)); | 110 ProtocolAddress(rtc::SocketAddress(address, port), proto, secure)); |
| 103 } | 111 } |
| 104 | 112 |
| 113 bool operator==(const RelayServerConfig& o) const { |
| 114 return type == o.type && ports == o.ports && credentials == o.credentials && |
| 115 priority == o.priority; |
| 116 } |
| 117 bool operator!=(const RelayServerConfig& o) const { return !(*this == o); } |
| 118 |
| 105 RelayType type; | 119 RelayType type; |
| 106 PortList ports; | 120 PortList ports; |
| 107 RelayCredentials credentials; | 121 RelayCredentials credentials; |
| 108 int priority; | 122 int priority = 0; |
| 109 }; | 123 }; |
| 110 | 124 |
| 111 class PortAllocatorSession : public sigslot::has_slots<> { | 125 class PortAllocatorSession : public sigslot::has_slots<> { |
| 112 public: | 126 public: |
| 113 // Content name passed in mostly for logging and debugging. | 127 // Content name passed in mostly for logging and debugging. |
| 114 PortAllocatorSession(const std::string& content_name, | 128 PortAllocatorSession(const std::string& content_name, |
| 115 int component, | 129 int component, |
| 116 const std::string& ice_ufrag, | 130 const std::string& ice_ufrag, |
| 117 const std::string& ice_pwd, | 131 const std::string& ice_pwd, |
| 118 uint32_t flags); | 132 uint32_t flags); |
| 119 | 133 |
| 120 // Subclasses should clean up any ports created. | 134 // Subclasses should clean up any ports created. |
| 121 virtual ~PortAllocatorSession() {} | 135 virtual ~PortAllocatorSession() {} |
| 122 | 136 |
| 123 uint32_t flags() const { return flags_; } | 137 uint32_t flags() const { return flags_; } |
| 124 void set_flags(uint32_t flags) { flags_ = flags; } | 138 void set_flags(uint32_t flags) { flags_ = flags; } |
| 125 std::string content_name() const { return content_name_; } | 139 std::string content_name() const { return content_name_; } |
| 126 int component() const { return component_; } | 140 int component() const { return component_; } |
| 141 const std::string& ice_ufrag() const { return ice_ufrag_; } |
| 142 const std::string& ice_pwd() const { return ice_pwd_; } |
| 143 bool pooled() const { return ice_ufrag_.empty(); } |
| 127 | 144 |
| 128 // Starts gathering STUN and Relay configurations. | 145 // Starts gathering STUN and Relay configurations. |
| 129 virtual void StartGettingPorts() = 0; | 146 virtual void StartGettingPorts() = 0; |
| 130 virtual void StopGettingPorts() = 0; | 147 virtual void StopGettingPorts() = 0; |
| 131 // Only stop the existing gathering process but may start new ones if needed. | 148 // Only stop the existing gathering process but may start new ones if needed. |
| 132 virtual void ClearGettingPorts() = 0; | 149 virtual void ClearGettingPorts() = 0; |
| 133 // Whether the process of getting ports has been stopped. | 150 // Whether the process of getting ports has been stopped. |
| 134 virtual bool IsGettingPorts() = 0; | 151 virtual bool IsGettingPorts() = 0; |
| 135 | 152 |
| 153 // Another way of getting the information provided by the signals below. |
| 154 // |
| 155 // Ports and candidates are not guaranteed to be in the same order as the |
| 156 // signals were emitted in. |
| 157 virtual std::vector<PortInterface*> ReadyPorts() const = 0; |
| 158 virtual std::vector<Candidate> ReadyCandidates() const = 0; |
| 159 virtual bool CandidatesAllocationDone() const = 0; |
| 160 |
| 136 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; | 161 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; |
| 137 sigslot::signal2<PortAllocatorSession*, | 162 sigslot::signal2<PortAllocatorSession*, |
| 138 const std::vector<Candidate>&> SignalCandidatesReady; | 163 const std::vector<Candidate>&> SignalCandidatesReady; |
| 139 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; | 164 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; |
| 140 | 165 |
| 141 virtual uint32_t generation() { return generation_; } | 166 virtual uint32_t generation() { return generation_; } |
| 142 virtual void set_generation(uint32_t generation) { generation_ = generation; } | 167 virtual void set_generation(uint32_t generation) { generation_ = generation; } |
| 143 sigslot::signal1<PortAllocatorSession*> SignalDestroyed; | 168 sigslot::signal1<PortAllocatorSession*> SignalDestroyed; |
| 144 | 169 |
| 145 const std::string& ice_ufrag() const { return ice_ufrag_; } | 170 protected: |
| 146 const std::string& ice_pwd() const { return ice_pwd_; } | 171 // This method is called when a pooled session (which doesn't have these |
| 172 // properties initially) is returned by PortAllocator::TakePooledSession, |
| 173 // and the content name, component, and ICE ufrag/pwd are updated. |
| 174 // |
| 175 // A subclass may need to override this method to perform additional actions, |
| 176 // such as applying the updated information to ports and candidates. |
| 177 virtual void UpdateIceParametersInternal() {} |
| 147 | 178 |
| 148 protected: | |
| 149 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and | 179 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and |
| 150 // ice_pwd. | 180 // ice_pwd. |
| 151 const std::string& username() const { return ice_ufrag_; } | 181 const std::string& username() const { return ice_ufrag_; } |
| 152 const std::string& password() const { return ice_pwd_; } | 182 const std::string& password() const { return ice_pwd_; } |
| 153 | 183 |
| 184 private: |
| 185 void SetIceParameters(const std::string& content_name, |
| 186 int component, |
| 187 const std::string& ice_ufrag, |
| 188 const std::string& ice_pwd) { |
| 189 content_name_ = content_name; |
| 190 component_ = component; |
| 191 ice_ufrag_ = ice_ufrag; |
| 192 ice_pwd_ = ice_pwd; |
| 193 UpdateIceParametersInternal(); |
| 194 } |
| 195 |
| 196 uint32_t flags_; |
| 197 uint32_t generation_; |
| 154 std::string content_name_; | 198 std::string content_name_; |
| 155 int component_; | 199 int component_; |
| 156 | |
| 157 private: | |
| 158 uint32_t flags_; | |
| 159 uint32_t generation_; | |
| 160 std::string ice_ufrag_; | 200 std::string ice_ufrag_; |
| 161 std::string ice_pwd_; | 201 std::string ice_pwd_; |
| 202 |
| 203 // SetIceParameters is an implementation detail which only PortAllocator |
| 204 // should be able to call. |
| 205 friend class PortAllocator; |
| 162 }; | 206 }; |
| 163 | 207 |
| 208 // Note that this class should only be used on one thread. |
| 209 // This includes calling the destructor. |
| 164 class PortAllocator : public sigslot::has_slots<> { | 210 class PortAllocator : public sigslot::has_slots<> { |
| 165 public: | 211 public: |
| 166 PortAllocator() : | 212 PortAllocator() : |
| 167 flags_(kDefaultPortAllocatorFlags), | 213 flags_(kDefaultPortAllocatorFlags), |
| 168 min_port_(0), | 214 min_port_(0), |
| 169 max_port_(0), | 215 max_port_(0), |
| 170 step_delay_(kDefaultStepDelay), | 216 step_delay_(kDefaultStepDelay), |
| 171 allow_tcp_listen_(true), | 217 allow_tcp_listen_(true), |
| 172 candidate_filter_(CF_ALL) { | 218 candidate_filter_(CF_ALL) { |
| 173 // This will allow us to have old behavior on non webrtc clients. | 219 // This will allow us to have old behavior on non webrtc clients. |
| 174 } | 220 } |
| 175 virtual ~PortAllocator() {} | 221 virtual ~PortAllocator() {} |
| 176 | 222 |
| 177 // Set STUN and TURN servers to be used in future sessions. | 223 // Set STUN and TURN servers to be used in future sessions, and set |
| 178 virtual void SetIceServers( | 224 // candidate pool size, as described in JSEP. |
| 179 const ServerAddresses& stun_servers, | 225 // |
| 180 const std::vector<RelayServerConfig>& turn_servers) = 0; | 226 // If the servers are changing and the candidate pool size is nonzero, |
| 227 // existing pooled sessions will be destroyed and new ones created. |
| 228 // |
| 229 // If the servers are not changing but the candidate pool size is, |
| 230 // pooled sessions will be either created or destroyed as necessary. |
| 231 void SetConfiguration(const ServerAddresses& stun_servers, |
| 232 const std::vector<RelayServerConfig>& turn_servers, |
| 233 int candidate_pool_size); |
| 234 |
| 235 const ServerAddresses& stun_servers() const { return stun_servers_; } |
| 236 |
| 237 const std::vector<RelayServerConfig>& turn_servers() const { |
| 238 return turn_servers_; |
| 239 } |
| 240 |
| 241 int candidate_pool_size() const { return target_pooled_session_count_; } |
| 181 | 242 |
| 182 // Sets the network types to ignore. | 243 // Sets the network types to ignore. |
| 183 // Values are defined by the AdapterType enum. | 244 // Values are defined by the AdapterType enum. |
| 184 // For instance, calling this with | 245 // For instance, calling this with |
| 185 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and | 246 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and |
| 186 // loopback interfaces. | 247 // loopback interfaces. |
| 187 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0; | 248 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0; |
| 188 | 249 |
| 189 PortAllocatorSession* CreateSession( | 250 std::unique_ptr<PortAllocatorSession> CreateSession( |
| 190 const std::string& sid, | 251 const std::string& sid, |
| 191 const std::string& content_name, | 252 const std::string& content_name, |
| 192 int component, | 253 int component, |
| 193 const std::string& ice_ufrag, | 254 const std::string& ice_ufrag, |
| 194 const std::string& ice_pwd); | 255 const std::string& ice_pwd); |
| 195 | 256 |
| 257 // Get an available pooled session and set the transport information on it. |
| 258 // |
| 259 // Caller takes ownership of the returned session. |
| 260 // |
| 261 // If no pooled sessions are available, returns null. |
| 262 std::unique_ptr<PortAllocatorSession> TakePooledSession( |
| 263 const std::string& content_name, |
| 264 int component, |
| 265 const std::string& ice_ufrag, |
| 266 const std::string& ice_pwd); |
| 267 |
| 268 // Returns the next session that would be returned by TakePooledSession. |
| 269 const PortAllocatorSession* GetPooledSession() const; |
| 270 |
| 196 uint32_t flags() const { return flags_; } | 271 uint32_t flags() const { return flags_; } |
| 197 void set_flags(uint32_t flags) { flags_ = flags; } | 272 void set_flags(uint32_t flags) { flags_ = flags; } |
| 198 | 273 |
| 199 const std::string& user_agent() const { return agent_; } | 274 const std::string& user_agent() const { return agent_; } |
| 200 const rtc::ProxyInfo& proxy() const { return proxy_; } | 275 const rtc::ProxyInfo& proxy() const { return proxy_; } |
| 201 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { | 276 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { |
| 202 agent_ = agent; | 277 agent_ = agent; |
| 203 proxy_ = proxy; | 278 proxy_ = proxy; |
| 204 } | 279 } |
| 205 | 280 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 218 | 293 |
| 219 uint32_t step_delay() const { return step_delay_; } | 294 uint32_t step_delay() const { return step_delay_; } |
| 220 void set_step_delay(uint32_t delay) { step_delay_ = delay; } | 295 void set_step_delay(uint32_t delay) { step_delay_ = delay; } |
| 221 | 296 |
| 222 bool allow_tcp_listen() const { return allow_tcp_listen_; } | 297 bool allow_tcp_listen() const { return allow_tcp_listen_; } |
| 223 void set_allow_tcp_listen(bool allow_tcp_listen) { | 298 void set_allow_tcp_listen(bool allow_tcp_listen) { |
| 224 allow_tcp_listen_ = allow_tcp_listen; | 299 allow_tcp_listen_ = allow_tcp_listen; |
| 225 } | 300 } |
| 226 | 301 |
| 227 uint32_t candidate_filter() { return candidate_filter_; } | 302 uint32_t candidate_filter() { return candidate_filter_; } |
| 228 bool set_candidate_filter(uint32_t filter) { | 303 void set_candidate_filter(uint32_t filter) { |
| 229 // TODO(mallinath) - Do transition check? | 304 // TODO(mallinath) - Do transition check? |
| 230 candidate_filter_ = filter; | 305 candidate_filter_ = filter; |
| 231 return true; | |
| 232 } | 306 } |
| 233 | 307 |
| 234 // Gets/Sets the Origin value used for WebRTC STUN requests. | 308 // Gets/Sets the Origin value used for WebRTC STUN requests. |
| 235 const std::string& origin() const { return origin_; } | 309 const std::string& origin() const { return origin_; } |
| 236 void set_origin(const std::string& origin) { origin_ = origin; } | 310 void set_origin(const std::string& origin) { origin_ = origin; } |
| 237 | 311 |
| 238 protected: | 312 protected: |
| 239 virtual PortAllocatorSession* CreateSessionInternal( | 313 virtual PortAllocatorSession* CreateSessionInternal( |
| 240 const std::string& content_name, | 314 const std::string& content_name, |
| 241 int component, | 315 int component, |
| 242 const std::string& ice_ufrag, | 316 const std::string& ice_ufrag, |
| 243 const std::string& ice_pwd) = 0; | 317 const std::string& ice_pwd) = 0; |
| 244 | 318 |
| 245 uint32_t flags_; | 319 uint32_t flags_; |
| 246 std::string agent_; | 320 std::string agent_; |
| 247 rtc::ProxyInfo proxy_; | 321 rtc::ProxyInfo proxy_; |
| 248 int min_port_; | 322 int min_port_; |
| 249 int max_port_; | 323 int max_port_; |
| 250 uint32_t step_delay_; | 324 uint32_t step_delay_; |
| 251 bool allow_tcp_listen_; | 325 bool allow_tcp_listen_; |
| 252 uint32_t candidate_filter_; | 326 uint32_t candidate_filter_; |
| 253 std::string origin_; | 327 std::string origin_; |
| 328 |
| 329 private: |
| 330 ServerAddresses stun_servers_; |
| 331 std::vector<RelayServerConfig> turn_servers_; |
| 332 // The last size passed into SetConfiguration. |
| 333 int target_pooled_session_count_ = 0; |
| 334 // This variable represents the total number of pooled sessions |
| 335 // both owned by this class and taken by TakePooledSession. |
| 336 int allocated_pooled_session_count_ = 0; |
| 337 std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_; |
| 254 }; | 338 }; |
| 255 | 339 |
| 256 } // namespace cricket | 340 } // namespace cricket |
| 257 | 341 |
| 258 #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ | 342 #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |
| OLD | NEW |