s2n_quic_core/frame/
path_challenge.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::frame::Tag;
5use core::convert::TryInto;
6use s2n_codec::{decoder_parameterized_value, Encoder, EncoderValue};
7
8//= https://www.rfc-editor.org/rfc/rfc9000#section-19.17
9//# Endpoints can use PATH_CHALLENGE frames (type=0x1a) to check
10//# reachability to the peer and for path validation during connection
11//# migration.
12
13macro_rules! path_challenge_tag {
14    () => {
15        0x1au8
16    };
17}
18
19//= https://www.rfc-editor.org/rfc/rfc9000#section-19.17
20//# PATH_CHALLENGE Frame {
21//#   Type (i) = 0x1a,
22//#   Data (64),
23//# }
24
25//= https://www.rfc-editor.org/rfc/rfc9000#section-19.17
26//# PATH_CHALLENGE frames contain the following fields:
27//#
28//# Data:  This 8-byte field contains arbitrary data.
29
30pub const DATA_LEN: usize = 8;
31
32#[derive(Debug, PartialEq, Eq)]
33pub struct PathChallenge<'a> {
34    /// This 8-byte field contains arbitrary data.
35    pub data: &'a [u8; DATA_LEN],
36}
37
38impl PathChallenge<'_> {
39    pub const fn tag(&self) -> u8 {
40        path_challenge_tag!()
41    }
42}
43
44decoder_parameterized_value!(
45    impl<'a> PathChallenge<'a> {
46        fn decode(_tag: Tag, buffer: Buffer) -> Result<Self> {
47            let (data, buffer) = buffer.decode_slice(DATA_LEN)?;
48            let data: &[u8] = data.into_less_safe_slice();
49
50            let data = data.try_into().expect("Length has been already verified");
51
52            let frame = PathChallenge { data };
53
54            Ok((frame, buffer))
55        }
56    }
57);
58
59impl EncoderValue for PathChallenge<'_> {
60    fn encode<E: Encoder>(&self, buffer: &mut E) {
61        buffer.encode(&self.tag());
62        buffer.encode(&self.data.as_ref());
63    }
64}