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 // A Transport manages a set of named channels of the same type. | 11 #ifndef WEBRTC_P2P_BASE_JSEPTRANSPORT_H_ |
12 // | 12 #define WEBRTC_P2P_BASE_JSEPTRANSPORT_H_ |
13 // Subclasses choose the appropriate class to instantiate for each channel; | |
14 // however, this base class keeps track of the channels by name, watches their | |
15 // state changes (in order to update the manager's state), and forwards | |
16 // requests to begin connecting or to reset to each of the channels. | |
17 // | |
18 // On Threading: Transport performs work solely on the network thread, and so | |
19 // its methods should only be called on the network thread. | |
20 // | |
21 // Note: Subclasses must call DestroyChannels() in their own destructors. | |
22 // It is not possible to do so here because the subclass destructor will | |
23 // already have run. | |
24 | |
25 #ifndef WEBRTC_P2P_BASE_TRANSPORT_H_ | |
26 #define WEBRTC_P2P_BASE_TRANSPORT_H_ | |
27 | 13 |
28 #include <map> | 14 #include <map> |
29 #include <memory> | 15 #include <memory> |
30 #include <string> | 16 #include <string> |
31 #include <vector> | 17 #include <vector> |
32 | 18 |
33 #include "webrtc/base/constructormagic.h" | 19 #include "webrtc/base/constructormagic.h" |
34 #include "webrtc/base/optional.h" | 20 #include "webrtc/base/optional.h" |
35 #include "webrtc/p2p/base/candidate.h" | 21 #include "webrtc/p2p/base/candidate.h" |
36 #include "webrtc/p2p/base/p2pconstants.h" | 22 #include "webrtc/p2p/base/p2pconstants.h" |
37 #include "webrtc/p2p/base/sessiondescription.h" | 23 #include "webrtc/p2p/base/sessiondescription.h" |
38 #include "webrtc/p2p/base/transportinfo.h" | 24 #include "webrtc/p2p/base/transportinfo.h" |
39 #include "webrtc/base/messagequeue.h" | 25 #include "webrtc/base/messagequeue.h" |
40 #include "webrtc/base/rtccertificate.h" | 26 #include "webrtc/base/rtccertificate.h" |
41 #include "webrtc/base/sigslot.h" | 27 #include "webrtc/base/sigslot.h" |
42 #include "webrtc/base/sslstreamadapter.h" | 28 #include "webrtc/base/sslstreamadapter.h" |
43 | 29 |
44 namespace cricket { | 30 namespace cricket { |
45 | 31 |
46 class PortAllocator; | 32 class TransportChannelImpl; |
47 class TransportChannel; | |
48 class TransportChannelImpl; | 33 class TransportChannelImpl; |
49 | 34 |
50 typedef std::vector<Candidate> Candidates; | 35 typedef std::vector<Candidate> Candidates; |
51 | 36 |
| 37 // TODO(deadbeef): Move all of these enums, POD types and utility methods to |
| 38 // another header file. |
| 39 |
52 // TODO(deadbeef): Unify with PeerConnectionInterface::IceConnectionState | 40 // TODO(deadbeef): Unify with PeerConnectionInterface::IceConnectionState |
53 // once /talk/ and /webrtc/ are combined, and also switch to ENUM_NAME naming | 41 // once /talk/ and /webrtc/ are combined, and also switch to ENUM_NAME naming |
54 // style. | 42 // style. |
55 enum IceConnectionState { | 43 enum IceConnectionState { |
56 kIceConnectionConnecting = 0, | 44 kIceConnectionConnecting = 0, |
57 kIceConnectionFailed, | 45 kIceConnectionFailed, |
58 kIceConnectionConnected, // Writable, but still checking one or more | 46 kIceConnectionConnected, // Writable, but still checking one or more |
59 // connections | 47 // connections |
60 kIceConnectionCompleted, | 48 kIceConnectionCompleted, |
61 }; | 49 }; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 sent_total_packets(0), | 96 sent_total_packets(0), |
109 sent_ping_requests_total(0), | 97 sent_ping_requests_total(0), |
110 sent_ping_requests_before_first_response(0), | 98 sent_ping_requests_before_first_response(0), |
111 sent_ping_responses(0), | 99 sent_ping_responses(0), |
112 recv_total_bytes(0), | 100 recv_total_bytes(0), |
113 recv_bytes_second(0), | 101 recv_bytes_second(0), |
114 recv_ping_requests(0), | 102 recv_ping_requests(0), |
115 recv_ping_responses(0), | 103 recv_ping_responses(0), |
116 key(NULL) {} | 104 key(NULL) {} |
117 | 105 |
118 bool best_connection; // Is this the best connection we have? | 106 bool best_connection; // Is this the best connection we have? |
119 bool writable; // Has this connection received a STUN response? | 107 bool writable; // Has this connection received a STUN response? |
120 bool receiving; // Has this connection received anything? | 108 bool receiving; // Has this connection received anything? |
121 bool timeout; // Has this connection timed out? | 109 bool timeout; // Has this connection timed out? |
122 bool new_connection; // Is this a newly created connection? | 110 bool new_connection; // Is this a newly created connection? |
123 size_t rtt; // The STUN RTT for this connection. | 111 size_t rtt; // The STUN RTT for this connection. |
124 size_t sent_total_bytes; // Total bytes sent on this connection. | 112 size_t sent_total_bytes; // Total bytes sent on this connection. |
125 size_t sent_bytes_second; // Bps over the last measurement interval. | 113 size_t sent_bytes_second; // Bps over the last measurement interval. |
126 size_t sent_discarded_packets; // Number of outgoing packets discarded due to | 114 size_t sent_discarded_packets; // Number of outgoing packets discarded due to |
127 // socket errors. | 115 // socket errors. |
128 size_t sent_total_packets; // Number of total outgoing packets attempted for | 116 size_t sent_total_packets; // Number of total outgoing packets attempted for |
129 // sending. | 117 // sending. |
130 size_t sent_ping_requests_total; // Number of STUN ping request sent. | 118 size_t sent_ping_requests_total; // Number of STUN ping request sent. |
131 size_t sent_ping_requests_before_first_response; // Number of STUN ping | 119 size_t sent_ping_requests_before_first_response; // Number of STUN ping |
132 // sent before receiving the first response. | 120 // sent before receiving the first response. |
133 size_t sent_ping_responses; // Number of STUN ping response sent. | 121 size_t sent_ping_responses; // Number of STUN ping response sent. |
134 | 122 |
135 size_t recv_total_bytes; // Total bytes received on this connection. | 123 size_t recv_total_bytes; // Total bytes received on this connection. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 receiving_switching_delay(receiving_switching_delay_ms) {} | 223 receiving_switching_delay(receiving_switching_delay_ms) {} |
236 }; | 224 }; |
237 | 225 |
238 bool BadTransportDescription(const std::string& desc, std::string* err_desc); | 226 bool BadTransportDescription(const std::string& desc, std::string* err_desc); |
239 | 227 |
240 bool IceCredentialsChanged(const std::string& old_ufrag, | 228 bool IceCredentialsChanged(const std::string& old_ufrag, |
241 const std::string& old_pwd, | 229 const std::string& old_pwd, |
242 const std::string& new_ufrag, | 230 const std::string& new_ufrag, |
243 const std::string& new_pwd); | 231 const std::string& new_pwd); |
244 | 232 |
245 class Transport : public sigslot::has_slots<> { | 233 // If a candidate is not acceptable, returns false and sets error. |
| 234 bool VerifyCandidate(const Candidate& candidate, std::string* error); |
| 235 bool VerifyCandidates(const Candidates& candidates, std::string* error); |
| 236 |
| 237 // Helper class used by TransportController that processes |
| 238 // TransportDescriptions. A TransportDescription represents the |
| 239 // transport-specific properties of an SDP m= section, processed according to |
| 240 // JSEP. Each transport consists of DTLS and ICE transport channels for RTP |
| 241 // (and possibly RTCP, if rtcp-mux isn't used). |
| 242 // |
| 243 // On Threading: Transport performs work solely on the network thread, and so |
| 244 // its methods should only be called on the network thread. |
| 245 // |
| 246 // TODO(deadbeef): Move this into /pc/ and out of /p2p/base/, since it's |
| 247 // PeerConnection-specific. |
| 248 class JsepTransport : public sigslot::has_slots<> { |
246 public: | 249 public: |
247 Transport(const std::string& name, PortAllocator* allocator); | 250 // |mid| is just used for log statements in order to identify the Transport. |
248 virtual ~Transport(); | 251 // Note that |certificate| is allowed to be null since a remote description |
| 252 // may be set before a local certificate is generated. |
| 253 JsepTransport(const std::string& mid, |
| 254 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
249 | 255 |
250 // Returns the name of this transport. | 256 // Returns the MID of this transport. |
251 const std::string& name() const { return name_; } | 257 const std::string& mid() const { return mid_; } |
252 | 258 |
253 // Returns the port allocator object for this transport. | 259 // Add or remove channel that is affected when a local/remote transport |
254 PortAllocator* port_allocator() { return allocator_; } | 260 // description is set on this transport. Need to add all channels before |
| 261 // setting a transport description. |
| 262 bool AddChannel(TransportChannelImpl* dtls, int component); |
| 263 bool RemoveChannel(int component); |
| 264 bool HasChannels() const; |
255 | 265 |
256 bool ready_for_remote_candidates() const { | 266 bool ready_for_remote_candidates() const { |
257 return local_description_set_ && remote_description_set_; | 267 return local_description_set_ && remote_description_set_; |
258 } | 268 } |
259 | 269 |
260 void SetIceRole(IceRole role); | |
261 IceRole ice_role() const { return ice_role_; } | |
262 | |
263 void SetIceTiebreaker(uint64_t IceTiebreaker) { tiebreaker_ = IceTiebreaker; } | |
264 uint64_t IceTiebreaker() { return tiebreaker_; } | |
265 | |
266 void SetIceConfig(const IceConfig& config); | |
267 | |
268 // Must be called before applying local session description. | 270 // Must be called before applying local session description. |
269 virtual void SetLocalCertificate( | 271 // Needed in order to verify the local fingerprint. |
270 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {} | 272 void SetLocalCertificate( |
| 273 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
271 | 274 |
272 // Get a copy of the local certificate provided by SetLocalCertificate. | 275 // Get a copy of the local certificate provided by SetLocalCertificate. |
273 virtual bool GetLocalCertificate( | 276 bool GetLocalCertificate( |
274 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { | 277 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const; |
275 return false; | |
276 } | |
277 | 278 |
278 // Get a copy of the remote certificate in use by the specified channel. | 279 // Set the local TransportDescription to be used by DTLS and ICE channels |
279 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(); | 280 // that are part of this Transport. |
280 | |
281 // Create, destroy, and lookup the channels of this type by their components. | |
282 TransportChannelImpl* CreateChannel(int component); | |
283 | |
284 TransportChannelImpl* GetChannel(int component); | |
285 | |
286 bool HasChannel(int component) { | |
287 return (NULL != GetChannel(component)); | |
288 } | |
289 bool HasChannels(); | |
290 | |
291 void DestroyChannel(int component); | |
292 | |
293 // Set the local TransportDescription to be used by TransportChannels. | |
294 bool SetLocalTransportDescription(const TransportDescription& description, | 281 bool SetLocalTransportDescription(const TransportDescription& description, |
295 ContentAction action, | 282 ContentAction action, |
296 std::string* error_desc); | 283 std::string* error_desc); |
297 | 284 |
298 // Set the remote TransportDescription to be used by TransportChannels. | 285 // Set the remote TransportDescription to be used by DTLS and ICE channels |
| 286 // that are part of this Transport. |
299 bool SetRemoteTransportDescription(const TransportDescription& description, | 287 bool SetRemoteTransportDescription(const TransportDescription& description, |
300 ContentAction action, | 288 ContentAction action, |
301 std::string* error_desc); | 289 std::string* error_desc); |
302 | 290 |
303 // Tells channels to start gathering candidates if necessary. | 291 void GetSslRole(rtc::SSLRole* ssl_role) const; |
304 // Should be called after ConnectChannels() has been called at least once, | |
305 // which will happen in SetLocalTransportDescription. | |
306 void MaybeStartGathering(); | |
307 | 292 |
308 // Resets all of the channels back to their initial state. They are no | 293 // TODO(deadbeef): Make this const. See comment in transportcontroller.h. |
309 // longer connecting. | |
310 void ResetChannels(); | |
311 | |
312 // Destroys every channel created so far. | |
313 void DestroyAllChannels(); | |
314 | |
315 bool GetStats(TransportStats* stats); | 294 bool GetStats(TransportStats* stats); |
316 | 295 |
317 // Called when one or more candidates are ready from the remote peer. | 296 // The current local transport description, possibly used |
318 bool AddRemoteCandidates(const std::vector<Candidate>& candidates, | |
319 std::string* error); | |
320 bool RemoveRemoteCandidates(const std::vector<Candidate>& candidates, | |
321 std::string* error); | |
322 | |
323 virtual bool GetSslRole(rtc::SSLRole* ssl_role) const { return false; } | |
324 | |
325 // Must be called before channel is starting to connect. | |
326 virtual bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) { | |
327 return false; | |
328 } | |
329 | |
330 // The current local transport description, for use by derived classes | |
331 // when performing transport description negotiation, and possibly used | |
332 // by the transport controller. | 297 // by the transport controller. |
333 const TransportDescription* local_description() const { | 298 const TransportDescription* local_description() const { |
334 return local_description_.get(); | 299 return local_description_.get(); |
335 } | 300 } |
336 | 301 |
337 // The current remote transport description, for use by derived classes | 302 // The current remote transport description, possibly used |
338 // when performing transport description negotiation, and possibly used | |
339 // by the transport controller. | 303 // by the transport controller. |
340 const TransportDescription* remote_description() const { | 304 const TransportDescription* remote_description() const { |
341 return remote_description_.get(); | 305 return remote_description_.get(); |
342 } | 306 } |
343 | 307 |
344 protected: | 308 // TODO(deadbeef): The methods below are only public for testing. Should make |
345 // These are called by Create/DestroyChannel above in order to create or | 309 // them utility functions or objects so they can be tested independently from |
346 // destroy the appropriate type of channel. | 310 // this class. |
347 virtual TransportChannelImpl* CreateTransportChannel(int component) = 0; | |
348 virtual void DestroyTransportChannel(TransportChannelImpl* channel) = 0; | |
349 | 311 |
350 // Pushes down the transport parameters from the local description, such | 312 // Returns false if the certificate's identity does not match the fingerprint, |
351 // as the ICE ufrag and pwd. | 313 // or either is NULL. |
352 // Derived classes can override, but must call the base as well. | 314 bool VerifyCertificateFingerprint(const rtc::RTCCertificate* certificate, |
353 virtual bool ApplyLocalTransportDescription(TransportChannelImpl* channel, | 315 const rtc::SSLFingerprint* fingerprint, |
354 std::string* error_desc); | 316 std::string* error_desc) const; |
355 | 317 |
356 // Pushes down remote ice credentials from the remote description to the | 318 // Negotiates the SSL role based off the offer and answer as specified by |
357 // transport channel. | 319 // RFC 4145, section-4.1. Returns false if the SSL role cannot be determined |
358 virtual bool ApplyRemoteTransportDescription(TransportChannelImpl* ch, | 320 // from the local description and remote description. |
359 std::string* error_desc); | 321 bool NegotiateRole(ContentAction local_role, |
| 322 rtc::SSLRole* ssl_role, |
| 323 std::string* error_desc) const; |
| 324 |
| 325 private: |
| 326 TransportChannelImpl* GetChannel(int component); |
360 | 327 |
361 // Negotiates the transport parameters based on the current local and remote | 328 // Negotiates the transport parameters based on the current local and remote |
362 // transport description, such as the ICE role to use, and whether DTLS | 329 // transport description, such as the ICE role to use, and whether DTLS |
363 // should be activated. | 330 // should be activated. |
364 // Derived classes can negotiate their specific parameters here, but must call | 331 // |
365 // the base as well. | 332 // Called when an answer TransportDescription is applied. |
366 virtual bool NegotiateTransportDescription(ContentAction local_role, | 333 bool NegotiateTransportDescription(ContentAction local_role, |
367 std::string* error_desc); | 334 std::string* error_desc); |
| 335 |
| 336 // Pushes down the transport parameters from the local description, such |
| 337 // as the ICE ufrag and pwd. |
| 338 bool ApplyLocalTransportDescription(TransportChannelImpl* channel, |
| 339 std::string* error_desc); |
| 340 |
| 341 // Pushes down the transport parameters from the remote description to the |
| 342 // transport channel. |
| 343 bool ApplyRemoteTransportDescription(TransportChannelImpl* channel, |
| 344 std::string* error_desc); |
368 | 345 |
369 // Pushes down the transport parameters obtained via negotiation. | 346 // Pushes down the transport parameters obtained via negotiation. |
370 // Derived classes can set their specific parameters here, but must call the | 347 bool ApplyNegotiatedTransportDescription(TransportChannelImpl* channel, |
371 // base as well. | 348 std::string* error_desc); |
372 virtual bool ApplyNegotiatedTransportDescription( | |
373 TransportChannelImpl* channel, | |
374 std::string* error_desc); | |
375 | 349 |
376 // Returns false if the certificate's identity does not match the fingerprint, | 350 const std::string mid_; |
377 // or either is NULL. | 351 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
378 virtual bool VerifyCertificateFingerprint( | 352 rtc::SSLRole secure_role_ = rtc::SSL_CLIENT; |
379 const rtc::RTCCertificate* certificate, | 353 std::unique_ptr<rtc::SSLFingerprint> remote_fingerprint_; |
380 const rtc::SSLFingerprint* fingerprint, | |
381 std::string* error_desc) const; | |
382 | |
383 // Negotiates the SSL role based off the offer and answer as specified by | |
384 // RFC 4145, section-4.1. Returns false if the SSL role cannot be determined | |
385 // from the local description and remote description. | |
386 virtual bool NegotiateRole(ContentAction local_role, | |
387 rtc::SSLRole* ssl_role, | |
388 std::string* error_desc) const; | |
389 | |
390 private: | |
391 // If a candidate is not acceptable, returns false and sets error. | |
392 // Call this before calling OnRemoteCandidates. | |
393 bool VerifyCandidate(const Candidate& candidate, std::string* error); | |
394 bool VerifyCandidates(const Candidates& candidates, std::string* error); | |
395 | |
396 // Candidate component => TransportChannelImpl* | |
397 typedef std::map<int, TransportChannelImpl*> ChannelMap; | |
398 | |
399 // Helper function that invokes the given function on every channel. | |
400 typedef void (TransportChannelImpl::* TransportChannelFunc)(); | |
401 void CallChannels(TransportChannelFunc func); | |
402 | |
403 const std::string name_; | |
404 PortAllocator* const allocator_; | |
405 bool channels_destroyed_ = false; | |
406 IceRole ice_role_ = ICEROLE_UNKNOWN; | |
407 uint64_t tiebreaker_ = 0; | |
408 IceMode remote_ice_mode_ = ICEMODE_FULL; | |
409 IceConfig ice_config_; | |
410 std::unique_ptr<TransportDescription> local_description_; | 354 std::unique_ptr<TransportDescription> local_description_; |
411 std::unique_ptr<TransportDescription> remote_description_; | 355 std::unique_ptr<TransportDescription> remote_description_; |
412 bool local_description_set_ = false; | 356 bool local_description_set_ = false; |
413 bool remote_description_set_ = false; | 357 bool remote_description_set_ = false; |
414 | 358 |
415 ChannelMap channels_; | 359 // Candidate component => DTLS channel |
| 360 std::map<int, TransportChannelImpl*> channels_; |
416 | 361 |
417 RTC_DISALLOW_COPY_AND_ASSIGN(Transport); | 362 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransport); |
418 }; | 363 }; |
419 | 364 |
420 | |
421 } // namespace cricket | 365 } // namespace cricket |
422 | 366 |
423 #endif // WEBRTC_P2P_BASE_TRANSPORT_H_ | 367 #endif // WEBRTC_P2P_BASE_JSEPTRANSPORT_H_ |
OLD | NEW |