blob: 0270043cbcea97bf22844f2fb34afab31fd6d47e [file] [log] [blame]
Harald Alvestrandad88c882018-11-28 16:47:46 +01001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef API_DTLS_TRANSPORT_INTERFACE_H_
12#define API_DTLS_TRANSPORT_INTERFACE_H_
Harald Alvestrandad88c882018-11-28 16:47:46 +010013
Harald Alvestrand7061e512019-04-10 17:20:42 +020014#include <memory>
15#include <utility>
16
Harald Alvestrand98462622019-01-30 14:57:03 +010017#include "api/ice_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "api/rtc_error.h"
Harald Alvestrand98462622019-01-30 14:57:03 +010019#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/ref_count.h"
Harald Alvestrand7061e512019-04-10 17:20:42 +020021#include "rtc_base/ssl_certificate.h"
Harald Alvestrandad88c882018-11-28 16:47:46 +010022
23namespace webrtc {
24
Harald Alvestrandd02541e2019-01-03 12:43:28 +010025// States of a DTLS transport, corresponding to the JS API specification.
26// http://w3c.github.io/webrtc-pc/#dom-rtcdtlstransportstate
27enum class DtlsTransportState {
28 kNew, // Has not started negotiating yet.
29 kConnecting, // In the process of negotiating a secure connection.
30 kConnected, // Completed negotiation and verified fingerprints.
31 kClosed, // Intentionally closed.
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +010032 kFailed, // Failure due to an error or failing to verify a remote
33 // fingerprint.
34 kNumValues
Harald Alvestrandd02541e2019-01-03 12:43:28 +010035};
36
37// This object gives snapshot information about the changeable state of a
38// DTLSTransport.
39class DtlsTransportInformation {
40 public:
Harald Alvestrand7061e512019-04-10 17:20:42 +020041 DtlsTransportInformation();
42 explicit DtlsTransportInformation(DtlsTransportState state);
43 DtlsTransportInformation(
44 DtlsTransportState state,
45 std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates);
46 // Copy and assign
47 DtlsTransportInformation(const DtlsTransportInformation& c);
48 DtlsTransportInformation& operator=(const DtlsTransportInformation& c);
49 // Move
50 DtlsTransportInformation(DtlsTransportInformation&& other) = default;
51 DtlsTransportInformation& operator=(DtlsTransportInformation&& other) =
52 default;
53
Harald Alvestrandd02541e2019-01-03 12:43:28 +010054 DtlsTransportState state() const { return state_; }
Harald Alvestrand7061e512019-04-10 17:20:42 +020055 // The accessor returns a temporary pointer, it does not release ownership.
56 const rtc::SSLCertChain* remote_ssl_certificates() const {
57 return remote_ssl_certificates_.get();
58 }
59
Harald Alvestrandd02541e2019-01-03 12:43:28 +010060 private:
61 DtlsTransportState state_;
Harald Alvestrand7061e512019-04-10 17:20:42 +020062 std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates_;
Harald Alvestrandd02541e2019-01-03 12:43:28 +010063};
64
65class DtlsTransportObserverInterface {
66 public:
67 // This callback carries information about the state of the transport.
68 // The argument is a pass-by-value snapshot of the state.
69 virtual void OnStateChange(DtlsTransportInformation info) = 0;
70 // This callback is called when an error occurs, causing the transport
71 // to go to the kFailed state.
72 virtual void OnError(RTCError error) = 0;
73
74 protected:
75 virtual ~DtlsTransportObserverInterface() = default;
76};
77
Harald Alvestrandad88c882018-11-28 16:47:46 +010078// A DTLS transport, as represented to the outside world.
Harald Alvestrand69fb6c82019-02-13 19:40:11 +010079// This object is created on the network thread, and can only be
80// accessed on that thread, except for functions explicitly marked otherwise.
Harald Alvestrandd02541e2019-01-03 12:43:28 +010081// References can be held by other threads, and destruction can therefore
82// be initiated by other threads.
Harald Alvestrandad88c882018-11-28 16:47:46 +010083class DtlsTransportInterface : public rtc::RefCountInterface {
84 public:
Harald Alvestrand98462622019-01-30 14:57:03 +010085 // Returns a pointer to the ICE transport that is owned by the DTLS transport.
86 virtual rtc::scoped_refptr<IceTransportInterface> ice_transport() = 0;
Harald Alvestrand69fb6c82019-02-13 19:40:11 +010087 // Returns information on the state of the DtlsTransport.
88 // This function can be called from other threads.
Harald Alvestrandd02541e2019-01-03 12:43:28 +010089 virtual DtlsTransportInformation Information() = 0;
90 // Observer management.
91 virtual void RegisterObserver(DtlsTransportObserverInterface* observer) = 0;
92 virtual void UnregisterObserver() = 0;
Harald Alvestrandad88c882018-11-28 16:47:46 +010093};
94
95} // namespace webrtc
96
Steve Anton10542f22019-01-11 09:11:00 -080097#endif // API_DTLS_TRANSPORT_INTERFACE_H_