Skip to main content

qualia_core_db/net/qdnf/frame/
header.rs

1//! Fixed 80-byte QFrame base header. Network byte order.
2
3use super::errors::FrameError;
4use crate::net::qdnf::registries::{flags, FrameType, NextProtocol};
5use crate::net::qdnf::types::{FlowId, LinkId, Sequence};
6
7pub const MAGIC: [u8; 4] = *b"QDNF";
8pub const VERSION: u8 = 1;
9pub const BASE_HEADER_LEN: usize = 80;
10pub const MAX_FRAME_LEN: usize = 65_535;
11
12#[repr(C)]
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub struct FrameHeader {
15    pub version: u8,
16    pub frame_type: FrameType,
17    pub flags: u16,
18    pub header_len: u16,
19    pub payload_len: u16,
20    pub hop_limit: u8,
21    pub next_protocol: NextProtocol,
22    pub source_link_id: LinkId,
23    pub destination_link_id: LinkId,
24    pub flow_id: FlowId,
25    pub sequence: Sequence,
26    pub header_tag: [u8; 16],
27}
28
29impl FrameHeader {
30    /// Creates a default `FrameHeader`.
31    pub fn new(frame_type: FrameType, next_protocol: NextProtocol) -> Self {
32        Self {
33            version: VERSION,
34            frame_type,
35            flags: 0,
36            header_len: BASE_HEADER_LEN as u16,
37            payload_len: 0,
38            hop_limit: if frame_type.forwarded() { 16 } else { 0 },
39            next_protocol,
40            source_link_id: LinkId::ZERO,
41            destination_link_id: LinkId::ZERO,
42            flow_id: FlowId::ZERO,
43            sequence: Sequence::ZERO,
44            header_tag: [0u8; 16],
45        }
46    }
47
48    #[inline]
49    /// `true` when critical.
50    pub const fn is_critical(self) -> bool {
51        self.flags & flags::CRITICAL != 0
52    }
53
54    #[inline]
55    /// `true` when fragment.
56    pub const fn is_fragment(self) -> bool {
57        self.flags & flags::FRAGMENT != 0
58    }
59
60    /// Complete on-wire length of this frame (header + payload).
61    pub fn wire_len(self) -> Result<usize, FrameError> {
62        let header = self.header_len as usize;
63        let payload = self.payload_len as usize;
64        if header < BASE_HEADER_LEN {
65            return Err(FrameError::Malformed);
66        }
67        header.checked_add(payload).ok_or(FrameError::Range)
68    }
69}