dtls_transport_interface.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2018 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. #ifndef API_DTLS_TRANSPORT_INTERFACE_H_
  11. #define API_DTLS_TRANSPORT_INTERFACE_H_
  12. #include <memory>
  13. #include <utility>
  14. #include "absl/types/optional.h"
  15. #include "api/ice_transport_interface.h"
  16. #include "api/rtc_error.h"
  17. #include "api/scoped_refptr.h"
  18. #include "rtc_base/ref_count.h"
  19. #include "rtc_base/ssl_certificate.h"
  20. #include "rtc_base/system/rtc_export.h"
  21. namespace webrtc {
  22. // States of a DTLS transport, corresponding to the JS API specification.
  23. // http://w3c.github.io/webrtc-pc/#dom-rtcdtlstransportstate
  24. enum class DtlsTransportState {
  25. kNew, // Has not started negotiating yet.
  26. kConnecting, // In the process of negotiating a secure connection.
  27. kConnected, // Completed negotiation and verified fingerprints.
  28. kClosed, // Intentionally closed.
  29. kFailed, // Failure due to an error or failing to verify a remote
  30. // fingerprint.
  31. kNumValues
  32. };
  33. // This object gives snapshot information about the changeable state of a
  34. // DTLSTransport.
  35. class RTC_EXPORT DtlsTransportInformation {
  36. public:
  37. DtlsTransportInformation();
  38. explicit DtlsTransportInformation(DtlsTransportState state);
  39. DtlsTransportInformation(
  40. DtlsTransportState state,
  41. absl::optional<int> tls_version,
  42. absl::optional<int> ssl_cipher_suite,
  43. absl::optional<int> srtp_cipher_suite,
  44. std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates);
  45. // Copy and assign
  46. DtlsTransportInformation(const DtlsTransportInformation& c);
  47. DtlsTransportInformation& operator=(const DtlsTransportInformation& c);
  48. // Move
  49. DtlsTransportInformation(DtlsTransportInformation&& other) = default;
  50. DtlsTransportInformation& operator=(DtlsTransportInformation&& other) =
  51. default;
  52. DtlsTransportState state() const { return state_; }
  53. absl::optional<int> tls_version() const { return tls_version_; }
  54. absl::optional<int> ssl_cipher_suite() const { return ssl_cipher_suite_; }
  55. absl::optional<int> srtp_cipher_suite() const { return srtp_cipher_suite_; }
  56. // The accessor returns a temporary pointer, it does not release ownership.
  57. const rtc::SSLCertChain* remote_ssl_certificates() const {
  58. return remote_ssl_certificates_.get();
  59. }
  60. private:
  61. DtlsTransportState state_;
  62. absl::optional<int> tls_version_;
  63. absl::optional<int> ssl_cipher_suite_;
  64. absl::optional<int> srtp_cipher_suite_;
  65. std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates_;
  66. };
  67. class DtlsTransportObserverInterface {
  68. public:
  69. // This callback carries information about the state of the transport.
  70. // The argument is a pass-by-value snapshot of the state.
  71. virtual void OnStateChange(DtlsTransportInformation info) = 0;
  72. // This callback is called when an error occurs, causing the transport
  73. // to go to the kFailed state.
  74. virtual void OnError(RTCError error) = 0;
  75. protected:
  76. virtual ~DtlsTransportObserverInterface() = default;
  77. };
  78. // A DTLS transport, as represented to the outside world.
  79. // This object is created on the network thread, and can only be
  80. // accessed on that thread, except for functions explicitly marked otherwise.
  81. // References can be held by other threads, and destruction can therefore
  82. // be initiated by other threads.
  83. class DtlsTransportInterface : public rtc::RefCountInterface {
  84. public:
  85. // Returns a pointer to the ICE transport that is owned by the DTLS transport.
  86. virtual rtc::scoped_refptr<IceTransportInterface> ice_transport() = 0;
  87. // Returns information on the state of the DtlsTransport.
  88. // This function can be called from other threads.
  89. virtual DtlsTransportInformation Information() = 0;
  90. // Observer management.
  91. virtual void RegisterObserver(DtlsTransportObserverInterface* observer) = 0;
  92. virtual void UnregisterObserver() = 0;
  93. };
  94. } // namespace webrtc
  95. #endif // API_DTLS_TRANSPORT_INTERFACE_H_