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