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> | |
16 #include <string> | 14 #include <string> |
17 #include <vector> | 15 #include <vector> |
18 | 16 |
19 #include "webrtc/p2p/base/port.h" | 17 #include "webrtc/p2p/base/port.h" |
20 #include "webrtc/p2p/base/portinterface.h" | 18 #include "webrtc/p2p/base/portinterface.h" |
21 #include "webrtc/base/helpers.h" | 19 #include "webrtc/base/helpers.h" |
22 #include "webrtc/base/proxyinfo.h" | 20 #include "webrtc/base/proxyinfo.h" |
23 #include "webrtc/base/sigslot.h" | 21 #include "webrtc/base/sigslot.h" |
24 #include "webrtc/base/thread.h" | |
25 | 22 |
26 namespace cricket { | 23 namespace cricket { |
27 | 24 |
28 // PortAllocator is responsible for allocating Port types for a given | 25 // PortAllocator is responsible for allocating Port types for a given |
29 // P2PSocket. It also handles port freeing. | 26 // P2PSocket. It also handles port freeing. |
30 // | 27 // |
31 // Clients can override this class to control port allocation, including | 28 // Clients can override this class to control port allocation, including |
32 // what kinds of ports are allocated. | 29 // what kinds of ports are allocated. |
33 | 30 |
34 enum { | 31 enum { |
(...skipping 43 matching lines...) Loading... |
78 CF_RELAY = 0x4, | 75 CF_RELAY = 0x4, |
79 CF_ALL = 0x7, | 76 CF_ALL = 0x7, |
80 }; | 77 }; |
81 | 78 |
82 // TODO(deadbeef): Rename to TurnCredentials (and username to ufrag). | 79 // TODO(deadbeef): Rename to TurnCredentials (and username to ufrag). |
83 struct RelayCredentials { | 80 struct RelayCredentials { |
84 RelayCredentials() {} | 81 RelayCredentials() {} |
85 RelayCredentials(const std::string& username, const std::string& password) | 82 RelayCredentials(const std::string& username, const std::string& password) |
86 : username(username), password(password) {} | 83 : username(username), password(password) {} |
87 | 84 |
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 | |
93 std::string username; | 85 std::string username; |
94 std::string password; | 86 std::string password; |
95 }; | 87 }; |
96 | 88 |
97 typedef std::vector<ProtocolAddress> PortList; | 89 typedef std::vector<ProtocolAddress> PortList; |
98 // TODO(deadbeef): Rename to TurnServerConfig. | 90 // TODO(deadbeef): Rename to TurnServerConfig. |
99 struct RelayServerConfig { | 91 struct RelayServerConfig { |
100 RelayServerConfig(RelayType type) : type(type) {} | 92 RelayServerConfig(RelayType type) : type(type), priority(0) {} |
101 | 93 |
102 RelayServerConfig(const std::string& address, | 94 RelayServerConfig(const std::string& address, |
103 int port, | 95 int port, |
104 const std::string& username, | 96 const std::string& username, |
105 const std::string& password, | 97 const std::string& password, |
106 ProtocolType proto, | 98 ProtocolType proto, |
107 bool secure) | 99 bool secure) |
108 : type(RELAY_TURN), credentials(username, password) { | 100 : type(RELAY_TURN), credentials(username, password) { |
109 ports.push_back( | 101 ports.push_back( |
110 ProtocolAddress(rtc::SocketAddress(address, port), proto, secure)); | 102 ProtocolAddress(rtc::SocketAddress(address, port), proto, secure)); |
111 } | 103 } |
112 | 104 |
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 | |
119 RelayType type; | 105 RelayType type; |
120 PortList ports; | 106 PortList ports; |
121 RelayCredentials credentials; | 107 RelayCredentials credentials; |
122 int priority = 0; | 108 int priority; |
123 }; | 109 }; |
124 | 110 |
125 class PortAllocatorSession : public sigslot::has_slots<> { | 111 class PortAllocatorSession : public sigslot::has_slots<> { |
126 public: | 112 public: |
127 // Content name passed in mostly for logging and debugging. | 113 // Content name passed in mostly for logging and debugging. |
128 PortAllocatorSession(const std::string& content_name, | 114 PortAllocatorSession(const std::string& content_name, |
129 int component, | 115 int component, |
130 const std::string& ice_ufrag, | 116 const std::string& ice_ufrag, |
131 const std::string& ice_pwd, | 117 const std::string& ice_pwd, |
132 uint32_t flags); | 118 uint32_t flags); |
133 | 119 |
134 // Subclasses should clean up any ports created. | 120 // Subclasses should clean up any ports created. |
135 virtual ~PortAllocatorSession() {} | 121 virtual ~PortAllocatorSession() {} |
136 | 122 |
137 uint32_t flags() const { return flags_; } | 123 uint32_t flags() const { return flags_; } |
138 void set_flags(uint32_t flags) { flags_ = flags; } | 124 void set_flags(uint32_t flags) { flags_ = flags; } |
139 std::string content_name() const { return content_name_; } | 125 std::string content_name() const { return content_name_; } |
140 int component() const { return component_; } | 126 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(); } | |
144 | 127 |
145 // Starts gathering STUN and Relay configurations. | 128 // Starts gathering STUN and Relay configurations. |
146 virtual void StartGettingPorts() = 0; | 129 virtual void StartGettingPorts() = 0; |
147 virtual void StopGettingPorts() = 0; | 130 virtual void StopGettingPorts() = 0; |
148 // Only stop the existing gathering process but may start new ones if needed. | 131 // Only stop the existing gathering process but may start new ones if needed. |
149 virtual void ClearGettingPorts() = 0; | 132 virtual void ClearGettingPorts() = 0; |
150 // Whether the process of getting ports has been stopped. | 133 // Whether the process of getting ports has been stopped. |
151 virtual bool IsGettingPorts() = 0; | 134 virtual bool IsGettingPorts() = 0; |
152 | 135 |
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 | |
161 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; | 136 sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady; |
162 sigslot::signal2<PortAllocatorSession*, | 137 sigslot::signal2<PortAllocatorSession*, |
163 const std::vector<Candidate>&> SignalCandidatesReady; | 138 const std::vector<Candidate>&> SignalCandidatesReady; |
164 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; | 139 sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone; |
165 | 140 |
166 virtual uint32_t generation() { return generation_; } | 141 virtual uint32_t generation() { return generation_; } |
167 virtual void set_generation(uint32_t generation) { generation_ = generation; } | 142 virtual void set_generation(uint32_t generation) { generation_ = generation; } |
168 sigslot::signal1<PortAllocatorSession*> SignalDestroyed; | 143 sigslot::signal1<PortAllocatorSession*> SignalDestroyed; |
169 | 144 |
| 145 const std::string& ice_ufrag() const { return ice_ufrag_; } |
| 146 const std::string& ice_pwd() const { return ice_pwd_; } |
| 147 |
170 protected: | 148 protected: |
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() {} | |
178 | |
179 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and | 149 // TODO(deadbeef): Get rid of these when everyone switches to ice_ufrag and |
180 // ice_pwd. | 150 // ice_pwd. |
181 const std::string& username() const { return ice_ufrag_; } | 151 const std::string& username() const { return ice_ufrag_; } |
182 const std::string& password() const { return ice_pwd_; } | 152 const std::string& password() const { return ice_pwd_; } |
183 | 153 |
| 154 std::string content_name_; |
| 155 int component_; |
| 156 |
184 private: | 157 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_; | 158 uint32_t flags_; |
197 uint32_t generation_; | 159 uint32_t generation_; |
198 std::string content_name_; | |
199 int component_; | |
200 std::string ice_ufrag_; | 160 std::string ice_ufrag_; |
201 std::string ice_pwd_; | 161 std::string ice_pwd_; |
202 | |
203 // SetIceParameters is an implementation detail which only PortAllocator | |
204 // should be able to call. | |
205 friend class PortAllocator; | |
206 }; | 162 }; |
207 | 163 |
208 // Note that this class should only be used on one thread. | |
209 // This includes calling the destructor. | |
210 class PortAllocator : public sigslot::has_slots<> { | 164 class PortAllocator : public sigslot::has_slots<> { |
211 public: | 165 public: |
212 PortAllocator() : | 166 PortAllocator() : |
213 flags_(kDefaultPortAllocatorFlags), | 167 flags_(kDefaultPortAllocatorFlags), |
214 min_port_(0), | 168 min_port_(0), |
215 max_port_(0), | 169 max_port_(0), |
216 step_delay_(kDefaultStepDelay), | 170 step_delay_(kDefaultStepDelay), |
217 allow_tcp_listen_(true), | 171 allow_tcp_listen_(true), |
218 candidate_filter_(CF_ALL) { | 172 candidate_filter_(CF_ALL) { |
219 // This will allow us to have old behavior on non webrtc clients. | 173 // This will allow us to have old behavior on non webrtc clients. |
220 } | 174 } |
221 virtual ~PortAllocator() {} | 175 virtual ~PortAllocator() {} |
222 | 176 |
223 // Set STUN and TURN servers to be used in future sessions, and set | 177 // Set STUN and TURN servers to be used in future sessions. |
224 // candidate pool size, as described in JSEP. | 178 virtual void SetIceServers( |
225 // | 179 const ServerAddresses& stun_servers, |
226 // If the servers are changing and the candidate pool size is nonzero, | 180 const std::vector<RelayServerConfig>& turn_servers) = 0; |
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_; } | |
242 | 181 |
243 // Sets the network types to ignore. | 182 // Sets the network types to ignore. |
244 // Values are defined by the AdapterType enum. | 183 // Values are defined by the AdapterType enum. |
245 // For instance, calling this with | 184 // For instance, calling this with |
246 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and | 185 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and |
247 // loopback interfaces. | 186 // loopback interfaces. |
248 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0; | 187 virtual void SetNetworkIgnoreMask(int network_ignore_mask) = 0; |
249 | 188 |
250 std::unique_ptr<PortAllocatorSession> CreateSession( | 189 PortAllocatorSession* CreateSession( |
251 const std::string& sid, | 190 const std::string& sid, |
252 const std::string& content_name, | 191 const std::string& content_name, |
253 int component, | 192 int component, |
254 const std::string& ice_ufrag, | 193 const std::string& ice_ufrag, |
255 const std::string& ice_pwd); | 194 const std::string& ice_pwd); |
256 | 195 |
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 | |
271 uint32_t flags() const { return flags_; } | 196 uint32_t flags() const { return flags_; } |
272 void set_flags(uint32_t flags) { flags_ = flags; } | 197 void set_flags(uint32_t flags) { flags_ = flags; } |
273 | 198 |
274 const std::string& user_agent() const { return agent_; } | 199 const std::string& user_agent() const { return agent_; } |
275 const rtc::ProxyInfo& proxy() const { return proxy_; } | 200 const rtc::ProxyInfo& proxy() const { return proxy_; } |
276 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { | 201 void set_proxy(const std::string& agent, const rtc::ProxyInfo& proxy) { |
277 agent_ = agent; | 202 agent_ = agent; |
278 proxy_ = proxy; | 203 proxy_ = proxy; |
279 } | 204 } |
280 | 205 |
(...skipping 12 matching lines...) Loading... |
293 | 218 |
294 uint32_t step_delay() const { return step_delay_; } | 219 uint32_t step_delay() const { return step_delay_; } |
295 void set_step_delay(uint32_t delay) { step_delay_ = delay; } | 220 void set_step_delay(uint32_t delay) { step_delay_ = delay; } |
296 | 221 |
297 bool allow_tcp_listen() const { return allow_tcp_listen_; } | 222 bool allow_tcp_listen() const { return allow_tcp_listen_; } |
298 void set_allow_tcp_listen(bool allow_tcp_listen) { | 223 void set_allow_tcp_listen(bool allow_tcp_listen) { |
299 allow_tcp_listen_ = allow_tcp_listen; | 224 allow_tcp_listen_ = allow_tcp_listen; |
300 } | 225 } |
301 | 226 |
302 uint32_t candidate_filter() { return candidate_filter_; } | 227 uint32_t candidate_filter() { return candidate_filter_; } |
303 void set_candidate_filter(uint32_t filter) { | 228 bool set_candidate_filter(uint32_t filter) { |
304 // TODO(mallinath) - Do transition check? | 229 // TODO(mallinath) - Do transition check? |
305 candidate_filter_ = filter; | 230 candidate_filter_ = filter; |
| 231 return true; |
306 } | 232 } |
307 | 233 |
308 // Gets/Sets the Origin value used for WebRTC STUN requests. | 234 // Gets/Sets the Origin value used for WebRTC STUN requests. |
309 const std::string& origin() const { return origin_; } | 235 const std::string& origin() const { return origin_; } |
310 void set_origin(const std::string& origin) { origin_ = origin; } | 236 void set_origin(const std::string& origin) { origin_ = origin; } |
311 | 237 |
312 protected: | 238 protected: |
313 virtual PortAllocatorSession* CreateSessionInternal( | 239 virtual PortAllocatorSession* CreateSessionInternal( |
314 const std::string& content_name, | 240 const std::string& content_name, |
315 int component, | 241 int component, |
316 const std::string& ice_ufrag, | 242 const std::string& ice_ufrag, |
317 const std::string& ice_pwd) = 0; | 243 const std::string& ice_pwd) = 0; |
318 | 244 |
319 uint32_t flags_; | 245 uint32_t flags_; |
320 std::string agent_; | 246 std::string agent_; |
321 rtc::ProxyInfo proxy_; | 247 rtc::ProxyInfo proxy_; |
322 int min_port_; | 248 int min_port_; |
323 int max_port_; | 249 int max_port_; |
324 uint32_t step_delay_; | 250 uint32_t step_delay_; |
325 bool allow_tcp_listen_; | 251 bool allow_tcp_listen_; |
326 uint32_t candidate_filter_; | 252 uint32_t candidate_filter_; |
327 std::string origin_; | 253 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_; | |
338 }; | 254 }; |
339 | 255 |
340 } // namespace cricket | 256 } // namespace cricket |
341 | 257 |
342 #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ | 258 #endif // WEBRTC_P2P_BASE_PORTALLOCATOR_H_ |
OLD | NEW |