netlink_packet_route/route/
realm.rsuse super::RouteError;
use netlink_packet_utils::Emitable;
const RULE_REALM_LEN: usize = 4;
#[derive(Clone, Eq, PartialEq, Debug, Copy)]
pub struct RouteRealm {
pub source: u16,
pub destination: u16,
}
impl RouteRealm {
pub(crate) fn parse(buf: &[u8]) -> Result<Self, RouteError> {
let all = u32::from_ne_bytes([buf[0], buf[1], buf[2], buf[3]]);
if buf.len() == RULE_REALM_LEN {
Ok(Self { source: (all >> 16) as u16, destination: (all & 0xFFFF) as u16 })
} else {
Err(RouteError::InvalidRulePortRange { expected: RULE_REALM_LEN, got: buf.len() })
}
}
}
impl Emitable for RouteRealm {
fn buffer_len(&self) -> usize {
RULE_REALM_LEN
}
fn emit(&self, buffer: &mut [u8]) {
let all = (self.source as u32) << 16 | self.destination as u32;
buffer.copy_from_slice(&all.to_ne_bytes());
}
}