noq_proto/
address_discovery.rs

1//! Address discovery types from
2//! <https://datatracker.ietf.org/doc/draft-seemann-quic-address-discovery/>
3
4use crate::VarInt;
5
6/// The role of each participant.
7///
8/// When enabled, this is reported as a transport parameter.
9#[derive(PartialEq, Eq, Clone, Copy, Debug, Default)]
10pub(crate) struct Role {
11    /// Whether this peer reports observed addresses to other peers.
12    pub(crate) send: bool,
13    /// Whether this peer wants to receive observed address reports from other peers.
14    pub(crate) receive: bool,
15}
16
17impl Role {
18    pub(crate) const fn send_only() -> Self {
19        Self {
20            send: true,
21            receive: false,
22        }
23    }
24
25    pub(crate) const fn receive_only() -> Self {
26        Self {
27            send: false,
28            receive: true,
29        }
30    }
31
32    pub(crate) const fn both() -> Self {
33        Self {
34            send: true,
35            receive: true,
36        }
37    }
38}
39
40impl TryFrom<VarInt> for Role {
41    type Error = crate::transport_parameters::Error;
42
43    fn try_from(value: VarInt) -> Result<Self, Self::Error> {
44        match value.0 {
45            0 => Ok(Self::send_only()),
46            1 => Ok(Self::receive_only()),
47            2 => Ok(Self::both()),
48            _ => Err(crate::transport_parameters::Error::IllegalValue),
49        }
50    }
51}
52
53impl Role {
54    /// Whether address discovery is disabled.
55    pub(crate) fn is_disabled(&self) -> bool {
56        !self.send && !self.receive
57    }
58
59    /// Whether this peer should report observed addresses to the other peer.
60    pub(crate) fn should_report(&self, other: &Self) -> bool {
61        self.send && other.receive
62    }
63
64    /// Gives the [`VarInt`] representing this [`Role`] as a transport parameter.
65    pub(crate) fn as_transport_parameter(&self) -> Option<VarInt> {
66        match (self.send, self.receive) {
67            (false, false) => None,
68            (true, false) => Some(VarInt(0)),
69            (false, true) => Some(VarInt(1)),
70            (true, true) => Some(VarInt(2)),
71        }
72    }
73}