use crate::icmpv6::{NdpOptionHeader, NdpOptionReadError, NdpOptionType};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MtuOptionSlice<'a> {
slice: &'a [u8; MtuOptionSlice::LEN],
}
impl<'a> MtuOptionSlice<'a> {
pub const LEN: usize = 8;
const LENGTH_UNITS: u8 = 1;
pub fn from_slice(slice: &'a [u8]) -> Result<Self, NdpOptionReadError> {
let slice = <&[u8; MtuOptionSlice::LEN]>::try_from(slice).map_err(|_| {
NdpOptionReadError::UnexpectedSize {
option_id: NdpOptionType::MTU,
expected_size: MtuOptionSlice::LEN,
actual_size: slice.len(),
}
})?;
let header = NdpOptionHeader::from_bytes([slice[0], slice[1]]);
if header.option_type != NdpOptionType::MTU || header.length_units != Self::LENGTH_UNITS {
return Err(NdpOptionReadError::UnexpectedHeader {
expected_option_id: NdpOptionType::MTU,
actual_option_id: header.option_type,
expected_length_units: Self::LENGTH_UNITS,
actual_length_units: header.length_units,
});
}
Ok(Self { slice })
}
pub const fn option_type(&self) -> NdpOptionType {
NdpOptionType::MTU
}
pub fn as_bytes(&self) -> &'a [u8] {
self.slice
}
pub fn mtu(&self) -> u32 {
u32::from_be_bytes([self.slice[4], self.slice[5], self.slice[6], self.slice[7]])
}
}