s2n_quic_core/
event.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{connection, endpoint};
5#[cfg(feature = "alloc")]
6use alloc::vec::Vec;
7use core::{fmt, ops::RangeInclusive, time::Duration};
8
9mod generated;
10pub mod metrics;
11pub use generated::*;
12
13#[cfg(any(test, feature = "testing"))]
14#[doc(hidden)]
15pub mod snapshot;
16
17/// All event types which can be emitted from this library.
18pub trait Event: core::fmt::Debug {
19    const NAME: &'static str;
20}
21
22pub trait IntoEvent<Target> {
23    fn into_event(self) -> Target;
24}
25
26macro_rules! ident_into_event {
27    ($($name:ty),* $(,)?) => {
28        $(
29            impl IntoEvent<$name> for $name {
30                #[inline]
31                fn into_event(self) -> Self {
32                    self
33                }
34            }
35        )*
36    };
37}
38
39macro_rules! borrowed_into_event {
40    ($($name:ty),* $(,)?) => {
41        $(
42            impl<'a> IntoEvent<&'a $name> for &'a $name {
43                #[inline]
44                fn into_event(self) -> Self {
45                    self
46                }
47            }
48        )*
49    };
50}
51
52ident_into_event!(
53    u8,
54    i8,
55    u16,
56    i16,
57    u32,
58    i32,
59    u64,
60    i64,
61    usize,
62    isize,
63    f32,
64    Duration,
65    bool,
66    connection::Error,
67    endpoint::Location,
68);
69borrowed_into_event!([u8; 4], [u8; 16], [u8], [u32], [&'a [u8]]);
70
71impl<T: IntoEvent<U>, U> IntoEvent<Option<U>> for Option<T> {
72    #[inline]
73    fn into_event(self) -> Option<U> {
74        self.map(IntoEvent::into_event)
75    }
76}
77
78impl<'a> IntoEvent<&'a str> for &'a str {
79    #[inline]
80    fn into_event(self) -> Self {
81        self
82    }
83}
84
85impl<'a> IntoEvent<&'a (dyn core::any::Any + Send + 'static)>
86    for &'a (dyn core::any::Any + Send + 'static)
87{
88    #[inline]
89    fn into_event(self) -> Self {
90        self
91    }
92}
93
94impl<T> IntoEvent<RangeInclusive<T>> for RangeInclusive<T> {
95    #[inline]
96    fn into_event(self) -> RangeInclusive<T> {
97        self
98    }
99}
100
101#[derive(Clone, Copy)]
102pub struct Timestamp(crate::time::Timestamp);
103
104impl fmt::Debug for Timestamp {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        self.0.fmt(f)
107    }
108}
109
110impl fmt::Display for Timestamp {
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        self.0.fmt(f)
113    }
114}
115
116impl Timestamp {
117    /// The duration since the start of the s2n-quic process.
118    ///
119    /// Record the start `SystemTime` at the start of the program
120    /// to derive the absolute time at which an event is emitted.
121    ///
122    /// ```rust
123    /// # use s2n_quic_core::{
124    /// #    endpoint,
125    /// #    event::{self, IntoEvent},
126    /// #    time::{Duration, Timestamp},
127    /// # };
128    ///
129    /// let start_time = std::time::SystemTime::now();
130    /// // `meta` is included as part of each event
131    /// # let meta: event::api::ConnectionMeta = event::builder::ConnectionMeta {
132    /// #     endpoint_type: endpoint::Type::Server,
133    /// #     id: 0,
134    /// #     timestamp: unsafe { Timestamp::from_duration(Duration::from_secs(1) )},
135    /// # }.into_event();
136    /// let event_time = start_time + meta.timestamp.duration_since_start();
137    /// ```
138    pub fn duration_since_start(&self) -> Duration {
139        // Safety: the duration is relative to start of program. This function along
140        // with it's documentation captures this intent.
141        unsafe { self.0.as_duration() }
142    }
143
144    /// Returns the `Duration` which elapsed since an earlier `Timestamp`.
145    /// If `earlier` is more recent, the method returns a `Duration` of 0.
146    #[inline]
147    pub fn saturating_duration_since(self, earlier: Self) -> Duration {
148        self.0.saturating_duration_since(earlier.0)
149    }
150}
151
152impl IntoEvent<Timestamp> for crate::time::Timestamp {
153    #[inline]
154    fn into_event(self) -> Timestamp {
155        Timestamp(self)
156    }
157}
158
159impl IntoEvent<Timestamp> for Timestamp {
160    #[inline]
161    fn into_event(self) -> Timestamp {
162        self
163    }
164}
165
166#[derive(Clone)]
167pub struct TlsSession<'a> {
168    session: &'a dyn crate::crypto::tls::TlsSession,
169}
170
171impl<'a> TlsSession<'a> {
172    #[doc(hidden)]
173    pub fn new(session: &'a dyn crate::crypto::tls::TlsSession) -> TlsSession<'a> {
174        TlsSession { session }
175    }
176
177    pub fn tls_exporter(
178        &self,
179        label: &[u8],
180        context: &[u8],
181        output: &mut [u8],
182    ) -> Result<(), crate::crypto::tls::TlsExportError> {
183        self.session.tls_exporter(label, context, output)
184    }
185
186    // Currently intended only for unstable usage
187    #[doc(hidden)]
188    #[cfg(feature = "alloc")]
189    pub fn peer_cert_chain_der(&self) -> Result<Vec<Vec<u8>>, crate::crypto::tls::ChainError> {
190        self.session.peer_cert_chain_der()
191    }
192
193    pub fn cipher_suite(&self) -> crate::event::api::CipherSuite {
194        self.session.cipher_suite().into_event()
195    }
196}
197
198impl<'a> crate::event::IntoEvent<TlsSession<'a>> for TlsSession<'a> {
199    #[inline]
200    fn into_event(self) -> Self {
201        self
202    }
203}
204
205impl core::fmt::Debug for TlsSession<'_> {
206    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
207        f.debug_struct("TlsSession").finish_non_exhaustive()
208    }
209}
210
211#[cfg(feature = "std")]
212impl<'a> IntoEvent<&'a std::io::Error> for &'a std::io::Error {
213    #[inline]
214    fn into_event(self) -> &'a std::io::Error {
215        self
216    }
217}
218
219/// Provides metadata related to an event
220pub trait Meta: core::fmt::Debug {
221    /// Returns whether the local endpoint is a Client or Server
222    fn endpoint_type(&self) -> &api::EndpointType;
223
224    /// A context from which the event is being emitted
225    ///
226    /// An event can occur in the context of an Endpoint or Connection
227    fn subject(&self) -> api::Subject;
228
229    /// The time the event occurred
230    fn timestamp(&self) -> &Timestamp;
231}
232
233impl Meta for api::ConnectionMeta {
234    fn endpoint_type(&self) -> &api::EndpointType {
235        &self.endpoint_type
236    }
237
238    fn subject(&self) -> api::Subject {
239        api::Subject::Connection { id: self.id }
240    }
241
242    fn timestamp(&self) -> &Timestamp {
243        &self.timestamp
244    }
245}
246
247impl Meta for api::EndpointMeta {
248    fn endpoint_type(&self) -> &api::EndpointType {
249        &self.endpoint_type
250    }
251
252    fn subject(&self) -> api::Subject {
253        api::Subject::Endpoint {}
254    }
255
256    fn timestamp(&self) -> &Timestamp {
257        &self.timestamp
258    }
259}