s2n_quic_core/dc/
traits.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{crypto::tls::TlsSession, dc, stateless_reset, transport};
5use alloc::vec::Vec;
6
7/// The `dc::Endpoint` trait provides a way to support dc functionality
8pub trait Endpoint: 'static + Send {
9    /// If enabled, a dc version will attempt to be negotiated and dc-specific frames
10    /// will be processed. Otherwise, no dc version will be negotiated and dc-specific
11    /// frames received will result in a connection error.
12    const ENABLED: bool = true;
13
14    type Path: Path;
15
16    /// Called when a dc version has been negotiated for the given `ConnectionInfo`
17    ///
18    /// Return `None` if dc should not be used for this path
19    fn new_path(&mut self, connection_info: &dc::ConnectionInfo) -> Option<Self::Path>;
20
21    /// Called when a datagram arrives that cannot be decoded as a non-DC QUIC packet, and
22    /// thus may contain a secret control packet
23    ///
24    /// Return `true` if a secret control packet was decoded from the datagram, `false` otherwise
25    fn on_possible_secret_control_packet(
26        &mut self,
27        datagram_info: &dc::DatagramInfo,
28        payload: &mut [u8],
29    ) -> bool;
30
31    /// Returns whether mtu_probing_complete_support is enabled.
32    /// This value is default to true if dc is enabled.
33    fn mtu_probing_complete_support(&self) -> bool {
34        true
35    }
36}
37
38/// A dc path
39pub trait Path: 'static + Send {
40    /// Called when path secrets are ready to be derived from the given `TlsSession`
41    ///
42    /// Returns the stateless reset tokens to include in a `DC_STATELESS_RESET_TOKENS`
43    /// frame sent to the peer.
44    fn on_path_secrets_ready(
45        &mut self,
46        session: &impl TlsSession,
47    ) -> Result<Vec<stateless_reset::Token>, transport::Error>;
48
49    /// Called when a `DC_STATELESS_RESET_TOKENS` frame has been received from the peer
50    fn on_peer_stateless_reset_tokens<'a>(
51        &mut self,
52        stateless_reset_tokens: impl Iterator<Item = &'a stateless_reset::Token>,
53    );
54
55    /// Called when the peer has confirmed receipt of `DC_STATELESS_RESET_TOKENS`, either
56    /// by the server sending back its own `DC_STATELESS_RESET_TOKENS` or by the client
57    /// acknowledging the `DC_STATELESS_RESET_TOKENS` frame was received.
58    fn on_dc_handshake_complete(&mut self);
59
60    /// Called when the MTU has been updated for the path
61    fn on_mtu_updated(&mut self, mtu: u16);
62}
63
64impl<P: Path> Path for Option<P> {
65    #[inline]
66    fn on_path_secrets_ready(
67        &mut self,
68        session: &impl TlsSession,
69    ) -> Result<Vec<stateless_reset::Token>, transport::Error> {
70        if let Some(path) = self {
71            path.on_path_secrets_ready(session)
72        } else {
73            Ok(Vec::default())
74        }
75    }
76
77    #[inline]
78    fn on_peer_stateless_reset_tokens<'a>(
79        &mut self,
80        stateless_reset_tokens: impl Iterator<Item = &'a stateless_reset::Token>,
81    ) {
82        if let Some(path) = self {
83            path.on_peer_stateless_reset_tokens(stateless_reset_tokens)
84        }
85    }
86
87    #[inline]
88    fn on_dc_handshake_complete(&mut self) {
89        if let Some(path) = self {
90            path.on_dc_handshake_complete()
91        }
92    }
93
94    #[inline]
95    fn on_mtu_updated(&mut self, max_datagram_size: u16) {
96        if let Some(path) = self {
97            path.on_mtu_updated(max_datagram_size)
98        }
99    }
100}