use winnow::{binary, combinator, Parser as _};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DecodedIcmpMsg<'a> {
msg_type: u8,
msg_code: u8,
checksum: u16,
body: &'a [u8],
}
impl<'a> DecodedIcmpMsg<'a> {
pub fn decode(input: &'a [u8]) -> Result<Self, InvalidIcmpMsg> {
(
binary::u8::<_, winnow::error::ContextError>,
binary::u8,
binary::be_u16,
combinator::rest,
)
.map(|(msg_type, msg_code, checksum, contents)| Self {
msg_type,
msg_code,
checksum,
body: contents,
})
.parse(input)
.map_err(|_| InvalidIcmpMsg)
}
pub fn msg_type(&self) -> u8 {
self.msg_type
}
pub fn msg_code(&self) -> u8 {
self.msg_code
}
pub fn checksum(&self) -> u16 {
self.checksum
}
pub fn body(&self) -> &[u8] {
self.body
}
}
#[derive(Debug, thiserror::Error)]
#[error("Input is not a valid ICMP message")]
pub struct InvalidIcmpMsg;