iroh_relay/protos/
common.rs

1//! Common types between the [`super::handshake`] and [`super::relay`] protocols.
2//!
3//! Hosts the [`FrameType`] enum to make sure we're not accidentally reusing frame type
4//! integers for different frames.
5
6use bytes::{Buf, BufMut};
7use n0_error::{e, stack_error};
8use quinn_proto::{
9    VarInt,
10    coding::{Codec, UnexpectedEnd},
11};
12
13/// Possible frame types during handshaking
14#[repr(u32)]
15#[derive(
16    Copy, Clone, PartialEq, Eq, Debug, num_enum::IntoPrimitive, num_enum::TryFromPrimitive,
17)]
18// needs to be pub due to being exposed in error types
19pub enum FrameType {
20    /// The server frame type for the challenge response
21    ServerChallenge = 0,
22    /// The client frame type for the authentication frame
23    ClientAuth = 1,
24    /// The server frame type for authentication confirmation
25    ServerConfirmsAuth = 2,
26    /// The server frame type for authentication denial
27    ServerDeniesAuth = 3,
28    /// 32B dest pub key + ECN bytes + one datagram's content
29    ClientToRelayDatagram = 4,
30    /// 32B dest pub key + ECN byte + segment size u16 + datagrams contents
31    ClientToRelayDatagramBatch = 5,
32    /// 32B src pub key + ECN bytes + one datagram's content
33    RelayToClientDatagram = 6,
34    /// 32B src pub key + ECN byte + segment size u16 + datagrams contents
35    RelayToClientDatagramBatch = 7,
36    /// Sent from server to client to signal that a previous sender is no longer connected.
37    ///
38    /// That is, if A sent to B, and then if A disconnects, the server sends `FrameType::PeerGone`
39    /// to B so B can forget that a reverse path exists on that connection to get back to A
40    ///
41    /// 32B pub key of peer that's gone
42    EndpointGone = 8,
43    /// Messages with these frames will be ignored.
44    /// 8 byte ping payload, to be echoed back in FrameType::Pong
45    Ping = 9,
46    /// 8 byte payload, the contents of ping being replied to
47    Pong = 10,
48    /// Sent from server to client to tell the client if their connection is unhealthy somehow.
49    /// Contains only UTF-8 bytes.
50    Health = 11,
51
52    /// Sent from server to client for the server to declare that it's restarting.
53    /// Payload is two big endian u32 durations in milliseconds: when to reconnect,
54    /// and how long to try total.
55    Restarting = 12,
56}
57
58#[stack_error(derive, add_meta)]
59#[allow(missing_docs)]
60#[non_exhaustive]
61pub enum FrameTypeError {
62    #[error("not enough bytes to parse frame type")]
63    UnexpectedEnd {
64        #[error(std_err)]
65        source: UnexpectedEnd,
66    },
67    #[error("frame type unknown")]
68    UnknownFrameType { tag: VarInt },
69}
70
71impl FrameType {
72    /// Writes the frame type to the buffer (as a QUIC-encoded varint).
73    pub(crate) fn write_to<O: BufMut>(&self, mut dst: O) -> O {
74        VarInt::from(*self).encode(&mut dst);
75        dst
76    }
77
78    /// Returns the amount of bytes that [`Self::write_to`] would write.
79    pub(crate) fn encoded_len(&self) -> usize {
80        // Copied implementation from `VarInt::size`
81        let x: u32 = (*self).into();
82        if x < 2u32.pow(6) {
83            1 // this will pretty much always be the case
84        } else if x < 2u32.pow(14) {
85            2
86        } else if x < 2u32.pow(30) {
87            4
88        } else {
89            unreachable!("Impossible FrameType primitive representation")
90        }
91    }
92
93    /// Parses the frame type (as a QUIC-encoded varint) from the first couple of bytes given
94    /// and returns the frame type and the rest.
95    pub(crate) fn from_bytes(buf: &mut impl Buf) -> Result<Self, FrameTypeError> {
96        let tag = VarInt::decode(buf).map_err(|err| e!(FrameTypeError::UnexpectedEnd, err))?;
97        let tag_u32 = u32::try_from(u64::from(tag))
98            .map_err(|_| e!(FrameTypeError::UnknownFrameType { tag }))?;
99        let frame_type = FrameType::try_from(tag_u32)
100            .map_err(|_| e!(FrameTypeError::UnknownFrameType { tag }))?;
101        Ok(frame_type)
102    }
103}
104
105impl From<FrameType> for VarInt {
106    fn from(value: FrameType) -> Self {
107        (value as u32).into()
108    }
109}