netlink_packet_route/route/
realm.rs

1// SPDX-License-Identifier: MIT
2
3use super::RouteError;
4use netlink_packet_utils::Emitable;
5
6const RULE_REALM_LEN: usize = 4;
7
8#[derive(Clone, Eq, PartialEq, Debug, Copy)]
9pub struct RouteRealm {
10    pub source: u16,
11    pub destination: u16,
12}
13
14impl RouteRealm {
15    pub(crate) fn parse(buf: &[u8]) -> Result<Self, RouteError> {
16        let all = u32::from_ne_bytes([buf[0], buf[1], buf[2], buf[3]]);
17        if buf.len() == RULE_REALM_LEN {
18            Ok(Self { source: (all >> 16) as u16, destination: (all & 0xFFFF) as u16 })
19        } else {
20            Err(RouteError::InvalidRulePortRange { expected: RULE_REALM_LEN, got: buf.len() })
21        }
22    }
23}
24
25impl Emitable for RouteRealm {
26    fn buffer_len(&self) -> usize {
27        RULE_REALM_LEN
28    }
29
30    fn emit(&self, buffer: &mut [u8]) {
31        let all = (self.source as u32) << 16 | self.destination as u32;
32        buffer.copy_from_slice(&all.to_ne_bytes());
33    }
34}