use byteorder::{ByteOrder, NetworkEndian};
use super::{Error, Result};
use crate::wire::icmpv6::{field, Message, Packet};
use crate::wire::Ipv6Address;
enum_with_unknown! {
pub enum RecordType(u8) {
ModeIsInclude = 0x01,
ModeIsExclude = 0x02,
ChangeToInclude = 0x03,
ChangeToExclude = 0x04,
AllowNewSources = 0x05,
BlockOldSources = 0x06
}
}
impl<T: AsRef<[u8]>> Packet<T> {
#[inline]
pub fn max_resp_code(&self) -> u16 {
let data = self.buffer.as_ref();
NetworkEndian::read_u16(&data[field::MAX_RESP_CODE])
}
#[inline]
pub fn mcast_addr(&self) -> Ipv6Address {
let data = self.buffer.as_ref();
Ipv6Address::from_bytes(&data[field::QUERY_MCAST_ADDR])
}
#[inline]
pub fn s_flag(&self) -> bool {
let data = self.buffer.as_ref();
(data[field::SQRV] & 0x08) != 0
}
#[inline]
pub fn qrv(&self) -> u8 {
let data = self.buffer.as_ref();
data[field::SQRV] & 0x7
}
#[inline]
pub fn qqic(&self) -> u8 {
let data = self.buffer.as_ref();
data[field::QQIC]
}
#[inline]
pub fn num_srcs(&self) -> u16 {
let data = self.buffer.as_ref();
NetworkEndian::read_u16(&data[field::QUERY_NUM_SRCS])
}
}
impl<T: AsRef<[u8]>> Packet<T> {
#[inline]
pub fn nr_mcast_addr_rcrds(&self) -> u16 {
let data = self.buffer.as_ref();
NetworkEndian::read_u16(&data[field::NR_MCAST_RCRDS])
}
}
impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
#[inline]
pub fn set_max_resp_code(&mut self, code: u16) {
let data = self.buffer.as_mut();
NetworkEndian::write_u16(&mut data[field::MAX_RESP_CODE], code);
}
#[inline]
pub fn set_mcast_addr(&mut self, addr: Ipv6Address) {
let data = self.buffer.as_mut();
data[field::QUERY_MCAST_ADDR].copy_from_slice(addr.as_bytes());
}
#[inline]
pub fn set_s_flag(&mut self) {
let data = self.buffer.as_mut();
let current = data[field::SQRV];
data[field::SQRV] = 0x8 | (current & 0x7);
}
#[inline]
pub fn clear_s_flag(&mut self) {
let data = self.buffer.as_mut();
data[field::SQRV] &= 0x7;
}
#[inline]
pub fn set_qrv(&mut self, value: u8) {
assert!(value < 8);
let data = self.buffer.as_mut();
data[field::SQRV] = (data[field::SQRV] & 0x8) | value & 0x7;
}
#[inline]
pub fn set_qqic(&mut self, value: u8) {
let data = self.buffer.as_mut();
data[field::QQIC] = value;
}
#[inline]
pub fn set_num_srcs(&mut self, value: u16) {
let data = self.buffer.as_mut();
NetworkEndian::write_u16(&mut data[field::QUERY_NUM_SRCS], value);
}
}
impl<T: AsRef<[u8]> + AsMut<[u8]>> Packet<T> {
#[inline]
pub fn set_nr_mcast_addr_rcrds(&mut self, value: u16) {
let data = self.buffer.as_mut();
NetworkEndian::write_u16(&mut data[field::NR_MCAST_RCRDS], value)
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AddressRecord<T: AsRef<[u8]>> {
buffer: T,
}
impl<T: AsRef<[u8]>> AddressRecord<T> {
pub const fn new_unchecked(buffer: T) -> Self {
Self { buffer }
}
pub fn new_checked(buffer: T) -> Result<Self> {
let packet = Self::new_unchecked(buffer);
packet.check_len()?;
Ok(packet)
}
pub fn check_len(&self) -> Result<()> {
let len = self.buffer.as_ref().len();
if len < field::RECORD_MCAST_ADDR.end {
Err(Error)
} else {
Ok(())
}
}
pub fn into_inner(self) -> T {
self.buffer
}
}
impl<T: AsRef<[u8]>> AddressRecord<T> {
#[inline]
pub fn record_type(&self) -> RecordType {
let data = self.buffer.as_ref();
RecordType::from(data[field::RECORD_TYPE])
}
#[inline]
pub fn aux_data_len(&self) -> u8 {
let data = self.buffer.as_ref();
data[field::AUX_DATA_LEN]
}
#[inline]
pub fn num_srcs(&self) -> u16 {
let data = self.buffer.as_ref();
NetworkEndian::read_u16(&data[field::RECORD_NUM_SRCS])
}
#[inline]
pub fn mcast_addr(&self) -> Ipv6Address {
let data = self.buffer.as_ref();
Ipv6Address::from_bytes(&data[field::RECORD_MCAST_ADDR])
}
}
impl<'a, T: AsRef<[u8]> + ?Sized> AddressRecord<&'a T> {
#[inline]
pub fn payload(&self) -> &'a [u8] {
let data = self.buffer.as_ref();
&data[field::RECORD_MCAST_ADDR.end..]
}
}
impl<T: AsMut<[u8]> + AsRef<[u8]>> AddressRecord<T> {
#[inline]
pub fn set_record_type(&mut self, rty: RecordType) {
let data = self.buffer.as_mut();
data[field::RECORD_TYPE] = rty.into();
}
#[inline]
pub fn set_aux_data_len(&mut self, len: u8) {
let data = self.buffer.as_mut();
data[field::AUX_DATA_LEN] = len;
}
#[inline]
pub fn set_num_srcs(&mut self, num_srcs: u16) {
let data = self.buffer.as_mut();
NetworkEndian::write_u16(&mut data[field::RECORD_NUM_SRCS], num_srcs);
}
#[inline]
pub fn set_mcast_addr(&mut self, addr: Ipv6Address) {
assert!(addr.is_multicast());
let data = self.buffer.as_mut();
data[field::RECORD_MCAST_ADDR].copy_from_slice(addr.as_bytes());
}
}
impl<T: AsRef<[u8]> + AsMut<[u8]>> AddressRecord<T> {
#[inline]
pub fn payload_mut(&mut self) -> &mut [u8] {
let data = self.buffer.as_mut();
&mut data[field::RECORD_MCAST_ADDR.end..]
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Repr<'a> {
Query {
max_resp_code: u16,
mcast_addr: Ipv6Address,
s_flag: bool,
qrv: u8,
qqic: u8,
num_srcs: u16,
data: &'a [u8],
},
Report {
nr_mcast_addr_rcrds: u16,
data: &'a [u8],
},
}
impl<'a> Repr<'a> {
pub fn parse<T>(packet: &Packet<&'a T>) -> Result<Repr<'a>>
where
T: AsRef<[u8]> + ?Sized,
{
match packet.msg_type() {
Message::MldQuery => Ok(Repr::Query {
max_resp_code: packet.max_resp_code(),
mcast_addr: packet.mcast_addr(),
s_flag: packet.s_flag(),
qrv: packet.qrv(),
qqic: packet.qqic(),
num_srcs: packet.num_srcs(),
data: packet.payload(),
}),
Message::MldReport => Ok(Repr::Report {
nr_mcast_addr_rcrds: packet.nr_mcast_addr_rcrds(),
data: packet.payload(),
}),
_ => Err(Error),
}
}
pub const fn buffer_len(&self) -> usize {
match self {
Repr::Query { data, .. } => field::QUERY_NUM_SRCS.end + data.len(),
Repr::Report { data, .. } => field::NR_MCAST_RCRDS.end + data.len(),
}
}
pub fn emit<T>(&self, packet: &mut Packet<&mut T>)
where
T: AsRef<[u8]> + AsMut<[u8]> + ?Sized,
{
match self {
Repr::Query {
max_resp_code,
mcast_addr,
s_flag,
qrv,
qqic,
num_srcs,
data,
} => {
packet.set_msg_type(Message::MldQuery);
packet.set_msg_code(0);
packet.clear_reserved();
packet.set_max_resp_code(*max_resp_code);
packet.set_mcast_addr(*mcast_addr);
if *s_flag {
packet.set_s_flag();
} else {
packet.clear_s_flag();
}
packet.set_qrv(*qrv);
packet.set_qqic(*qqic);
packet.set_num_srcs(*num_srcs);
packet.payload_mut().copy_from_slice(&data[..]);
}
Repr::Report {
nr_mcast_addr_rcrds,
data,
} => {
packet.set_msg_type(Message::MldReport);
packet.set_msg_code(0);
packet.clear_reserved();
packet.set_nr_mcast_addr_rcrds(*nr_mcast_addr_rcrds);
packet.payload_mut().copy_from_slice(&data[..]);
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::phy::ChecksumCapabilities;
use crate::wire::icmpv6::Message;
use crate::wire::Icmpv6Repr;
static QUERY_PACKET_BYTES: [u8; 44] = [
0x82, 0x00, 0x73, 0x74, 0x04, 0x00, 0x00, 0x00, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x12, 0x00, 0x01, 0xff, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
];
static QUERY_PACKET_PAYLOAD: [u8; 16] = [
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02,
];
static REPORT_PACKET_BYTES: [u8; 44] = [
0x8f, 0x00, 0x73, 0x85, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0xff, 0x02, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
];
static REPORT_PACKET_PAYLOAD: [u8; 36] = [
0x01, 0x00, 0x00, 0x01, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
];
fn create_repr<'a>(ty: Message) -> Icmpv6Repr<'a> {
match ty {
Message::MldQuery => Icmpv6Repr::Mld(Repr::Query {
max_resp_code: 0x400,
mcast_addr: Ipv6Address::LINK_LOCAL_ALL_NODES,
s_flag: true,
qrv: 0x02,
qqic: 0x12,
num_srcs: 0x01,
data: &QUERY_PACKET_PAYLOAD,
}),
Message::MldReport => Icmpv6Repr::Mld(Repr::Report {
nr_mcast_addr_rcrds: 1,
data: &REPORT_PACKET_PAYLOAD,
}),
_ => {
panic!("Message type must be a MLDv2 message type");
}
}
}
#[test]
fn test_query_deconstruct() {
let packet = Packet::new_unchecked(&QUERY_PACKET_BYTES[..]);
assert_eq!(packet.msg_type(), Message::MldQuery);
assert_eq!(packet.msg_code(), 0);
assert_eq!(packet.checksum(), 0x7374);
assert_eq!(packet.max_resp_code(), 0x0400);
assert_eq!(packet.mcast_addr(), Ipv6Address::LINK_LOCAL_ALL_NODES);
assert!(packet.s_flag());
assert_eq!(packet.qrv(), 0x02);
assert_eq!(packet.qqic(), 0x12);
assert_eq!(packet.num_srcs(), 0x01);
assert_eq!(
Ipv6Address::from_bytes(packet.payload()),
Ipv6Address::LINK_LOCAL_ALL_ROUTERS
);
}
#[test]
fn test_query_construct() {
let mut bytes = vec![0xff; 44];
let mut packet = Packet::new_unchecked(&mut bytes[..]);
packet.set_msg_type(Message::MldQuery);
packet.set_msg_code(0);
packet.set_max_resp_code(0x0400);
packet.set_mcast_addr(Ipv6Address::LINK_LOCAL_ALL_NODES);
packet.set_s_flag();
packet.set_qrv(0x02);
packet.set_qqic(0x12);
packet.set_num_srcs(0x01);
packet
.payload_mut()
.copy_from_slice(Ipv6Address::LINK_LOCAL_ALL_ROUTERS.as_bytes());
packet.clear_reserved();
packet.fill_checksum(
&Ipv6Address::LINK_LOCAL_ALL_NODES.into(),
&Ipv6Address::LINK_LOCAL_ALL_ROUTERS.into(),
);
assert_eq!(&*packet.into_inner(), &QUERY_PACKET_BYTES[..]);
}
#[test]
fn test_record_deconstruct() {
let packet = Packet::new_unchecked(&REPORT_PACKET_BYTES[..]);
assert_eq!(packet.msg_type(), Message::MldReport);
assert_eq!(packet.msg_code(), 0);
assert_eq!(packet.checksum(), 0x7385);
assert_eq!(packet.nr_mcast_addr_rcrds(), 0x01);
let addr_rcrd = AddressRecord::new_unchecked(packet.payload());
assert_eq!(addr_rcrd.record_type(), RecordType::ModeIsInclude);
assert_eq!(addr_rcrd.aux_data_len(), 0x00);
assert_eq!(addr_rcrd.num_srcs(), 0x01);
assert_eq!(addr_rcrd.mcast_addr(), Ipv6Address::LINK_LOCAL_ALL_NODES);
assert_eq!(
Ipv6Address::from_bytes(addr_rcrd.payload()),
Ipv6Address::LINK_LOCAL_ALL_ROUTERS
);
}
#[test]
fn test_record_construct() {
let mut bytes = vec![0xff; 44];
let mut packet = Packet::new_unchecked(&mut bytes[..]);
packet.set_msg_type(Message::MldReport);
packet.set_msg_code(0);
packet.clear_reserved();
packet.set_nr_mcast_addr_rcrds(1);
{
let mut addr_rcrd = AddressRecord::new_unchecked(packet.payload_mut());
addr_rcrd.set_record_type(RecordType::ModeIsInclude);
addr_rcrd.set_aux_data_len(0);
addr_rcrd.set_num_srcs(1);
addr_rcrd.set_mcast_addr(Ipv6Address::LINK_LOCAL_ALL_NODES);
addr_rcrd
.payload_mut()
.copy_from_slice(Ipv6Address::LINK_LOCAL_ALL_ROUTERS.as_bytes());
}
packet.fill_checksum(
&Ipv6Address::LINK_LOCAL_ALL_NODES.into(),
&Ipv6Address::LINK_LOCAL_ALL_ROUTERS.into(),
);
assert_eq!(&*packet.into_inner(), &REPORT_PACKET_BYTES[..]);
}
#[test]
fn test_query_repr_parse() {
let packet = Packet::new_unchecked(&QUERY_PACKET_BYTES[..]);
let repr = Icmpv6Repr::parse(
&Ipv6Address::LINK_LOCAL_ALL_NODES.into(),
&Ipv6Address::LINK_LOCAL_ALL_ROUTERS.into(),
&packet,
&ChecksumCapabilities::default(),
);
assert_eq!(repr, Ok(create_repr(Message::MldQuery)));
}
#[test]
fn test_report_repr_parse() {
let packet = Packet::new_unchecked(&REPORT_PACKET_BYTES[..]);
let repr = Icmpv6Repr::parse(
&Ipv6Address::LINK_LOCAL_ALL_NODES.into(),
&Ipv6Address::LINK_LOCAL_ALL_ROUTERS.into(),
&packet,
&ChecksumCapabilities::default(),
);
assert_eq!(repr, Ok(create_repr(Message::MldReport)));
}
#[test]
fn test_query_repr_emit() {
let mut bytes = [0x2a; 44];
let mut packet = Packet::new_unchecked(&mut bytes[..]);
let repr = create_repr(Message::MldQuery);
repr.emit(
&Ipv6Address::LINK_LOCAL_ALL_NODES.into(),
&Ipv6Address::LINK_LOCAL_ALL_ROUTERS.into(),
&mut packet,
&ChecksumCapabilities::default(),
);
assert_eq!(&*packet.into_inner(), &QUERY_PACKET_BYTES[..]);
}
#[test]
fn test_report_repr_emit() {
let mut bytes = [0x2a; 44];
let mut packet = Packet::new_unchecked(&mut bytes[..]);
let repr = create_repr(Message::MldReport);
repr.emit(
&Ipv6Address::LINK_LOCAL_ALL_NODES.into(),
&Ipv6Address::LINK_LOCAL_ALL_ROUTERS.into(),
&mut packet,
&ChecksumCapabilities::default(),
);
assert_eq!(&*packet.into_inner(), &REPORT_PACKET_BYTES[..]);
}
}