Skip to main content

qualia_core_db/net/qdnf/
registries.rs

1//! Numeric registries. Unknown values fail closed; no inferred algorithm from length.
2
3use super::errors::QdnfError;
4
5/// QFrame type codes from the wire protocol.
6#[repr(u8)]
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum FrameType {
9    DiscoveryBeacon = 1,
10    LinkChallenge = 2,
11    LinkProof = 3,
12    LinkClose = 4,
13    LinkStateAdvertisement = 16,
14    RealmPathAdvertisement = 17,
15    RouteWithdraw = 18,
16    ResolveQuery = 32,
17    ResolveAnswer = 33,
18    SessionChallenge = 48,
19    SessionProof = 49,
20    CapabilityPresent = 50,
21    CapabilityDecision = 51,
22    ProtocolNegotiate = 52,
23    SessionDatagram = 64,
24    SessionStream = 65,
25    SessionAck = 66,
26    SessionReset = 67,
27    SyncOperation = 80,
28    ContentBlock = 81,
29    Error = 255,
30}
31
32impl FrameType {
33    /// Builds `FrameType` from `u8`.
34    pub const fn from_u8(v: u8) -> Result<Self, QdnfError> {
35        Ok(match v {
36            1 => Self::DiscoveryBeacon,
37            2 => Self::LinkChallenge,
38            3 => Self::LinkProof,
39            4 => Self::LinkClose,
40            16 => Self::LinkStateAdvertisement,
41            17 => Self::RealmPathAdvertisement,
42            18 => Self::RouteWithdraw,
43            32 => Self::ResolveQuery,
44            33 => Self::ResolveAnswer,
45            48 => Self::SessionChallenge,
46            49 => Self::SessionProof,
47            50 => Self::CapabilityPresent,
48            51 => Self::CapabilityDecision,
49            52 => Self::ProtocolNegotiate,
50            64 => Self::SessionDatagram,
51            65 => Self::SessionStream,
52            66 => Self::SessionAck,
53            67 => Self::SessionReset,
54            80 => Self::SyncOperation,
55            81 => Self::ContentBlock,
56            255 => Self::Error,
57            _ => return Err(QdnfError::Unsupported),
58        })
59    }
60
61    #[inline]
62    /// Encodes as u8.
63    pub const fn as_u8(self) -> u8 {
64        self as u8
65    }
66
67    /// Link-local frames must not be forwarded.
68    #[inline]
69    pub const fn forwarded(self) -> bool {
70        matches!(
71            self,
72            Self::LinkStateAdvertisement
73                | Self::RealmPathAdvertisement
74                | Self::RouteWithdraw
75                | Self::ResolveQuery
76                | Self::ResolveAnswer
77                | Self::SessionChallenge
78                | Self::SessionProof
79                | Self::CapabilityPresent
80                | Self::CapabilityDecision
81                | Self::ProtocolNegotiate
82                | Self::SessionDatagram
83                | Self::SessionStream
84                | Self::SessionAck
85                | Self::SessionReset
86                | Self::SyncOperation
87                | Self::ContentBlock
88        )
89    }
90}
91
92/// `next_protocol` byte.
93#[repr(u8)]
94#[derive(Clone, Copy, Debug, PartialEq, Eq)]
95pub enum NextProtocol {
96    QLink = 1,
97    QRoute = 2,
98    QResolve = 3,
99    QPolicy = 4,
100    QSession = 5,
101    QSync = 6,
102}
103
104impl NextProtocol {
105    /// Builds `NextProtocol` from `u8`.
106    pub const fn from_u8(v: u8) -> Result<Self, QdnfError> {
107        Ok(match v {
108            1 => Self::QLink,
109            2 => Self::QRoute,
110            3 => Self::QResolve,
111            4 => Self::QPolicy,
112            5 => Self::QSession,
113            6 => Self::QSync,
114            _ => return Err(QdnfError::Unsupported),
115        })
116    }
117}
118
119/// Algorithm identifiers. Length alone never selects an algorithm.
120#[repr(u16)]
121#[derive(Clone, Copy, Debug, PartialEq, Eq)]
122pub enum AlgorithmId {
123    Sha384 = 1,
124    HkdfSha384 = 2,
125    HmacSha384 = 3,
126    X25519 = 4,
127    MlKem768 = 5,
128    MlDsa65 = 6,
129    Ed25519 = 7,
130    ChaCha20Poly1305 = 8,
131    SlhDsaSha2_256s = 9,
132}
133
134impl AlgorithmId {
135    /// Builds `AlgorithmId` from `u16`.
136    pub const fn from_u16(v: u16) -> Result<Self, QdnfError> {
137        Ok(match v {
138            1 => Self::Sha384,
139            2 => Self::HkdfSha384,
140            3 => Self::HmacSha384,
141            4 => Self::X25519,
142            5 => Self::MlKem768,
143            6 => Self::MlDsa65,
144            7 => Self::Ed25519,
145            8 => Self::ChaCha20Poly1305,
146            9 => Self::SlhDsaSha2_256s,
147            _ => return Err(QdnfError::UnknownProfile),
148        })
149    }
150}
151
152/// Bearer profile identifiers.
153#[repr(u16)]
154#[derive(Clone, Copy, Debug, PartialEq, Eq)]
155pub enum BearerProfile {
156    LocalIpcV1 = 1,
157    RawEthernetV1 = 2,
158    UdpTransitionV1 = 3,
159    /// SocialWebNet userspace-WireGuard overlay. Labelled transition, not Native Independent.
160    WireGuardTransitionV1 = 4,
161    /// Local rustls WebSocket. Labelled transition, not Native Independent, not a public relay.
162    TlsWssTransitionV1 = 5,
163    /// HTTP/2 Extended CONNECT + RFC 9297 capsules. Labelled transition, not Native Independent, not MASQUE.
164    Http2CapsuleTransitionV1 = 6,
165    /// Nym decentralized mixnet overlay. Labelled transition, not Native Independent.
166    NymMixnetTransitionV1 = 7,
167}
168
169impl BearerProfile {
170    /// `BearerProfile::from_u16`.
171    pub const fn from_u16(v: u16) -> Result<Self, QdnfError> {
172        Ok(match v {
173            1 => Self::LocalIpcV1,
174            2 => Self::RawEthernetV1,
175            3 => Self::UdpTransitionV1,
176            4 => Self::WireGuardTransitionV1,
177            5 => Self::TlsWssTransitionV1,
178            6 => Self::Http2CapsuleTransitionV1,
179            7 => Self::NymMixnetTransitionV1,
180            _ => return Err(QdnfError::UnknownProfile),
181        })
182    }
183
184    /// Native Independent conformance excludes transition carriers.
185    #[inline]
186    pub const fn native_independent(self) -> bool {
187        matches!(self, Self::LocalIpcV1 | Self::RawEthernetV1)
188    }
189}
190
191/// QFrame flags (host-order interpretation of the 16-bit flags field).
192pub mod flags {
193    pub const CRITICAL: u16 = 1 << 0;
194    pub const FRAGMENT: u16 = 1 << 1;
195    pub const SESSION: u16 = 1 << 2;
196}
197
198#[cfg(test)]
199mod tests {
200    use super::*;
201
202    #[test]
203    fn unknown_frame_type_is_unsupported() {
204        assert_eq!(FrameType::from_u8(99), Err(QdnfError::Unsupported));
205    }
206
207    #[test]
208    fn discovery_is_not_forwarded() {
209        assert!(!FrameType::DiscoveryBeacon.forwarded());
210        assert!(FrameType::SessionStream.forwarded());
211    }
212
213    #[test]
214    fn udp_transition_is_not_native_independent() {
215        assert!(!BearerProfile::UdpTransitionV1.native_independent());
216        assert!(!BearerProfile::WireGuardTransitionV1.native_independent());
217        assert!(BearerProfile::LocalIpcV1.native_independent());
218        assert!(!BearerProfile::TlsWssTransitionV1.native_independent());
219        assert!(!BearerProfile::Http2CapsuleTransitionV1.native_independent());
220        assert!(!BearerProfile::NymMixnetTransitionV1.native_independent());
221        assert_eq!(
222            BearerProfile::from_u16(4).unwrap(),
223            BearerProfile::WireGuardTransitionV1
224        );
225        assert_eq!(
226            BearerProfile::from_u16(5).unwrap(),
227            BearerProfile::TlsWssTransitionV1
228        );
229        assert_eq!(
230            BearerProfile::from_u16(6).unwrap(),
231            BearerProfile::Http2CapsuleTransitionV1
232        );
233        assert_eq!(
234            BearerProfile::from_u16(7).unwrap(),
235            BearerProfile::NymMixnetTransitionV1
236        );
237        assert_eq!(BearerProfile::from_u16(8), Err(QdnfError::UnknownProfile));
238    }
239}