netlink_packet_route/neighbour/
message.rs

1// SPDX-License-Identifier: MIT
2
3use super::super::AddressFamily;
4use super::{NeighbourAttribute, NeighbourError, NeighbourHeader, NeighbourMessageBuffer};
5use netlink_packet_utils::traits::{Emitable, Parseable, ParseableParametrized};
6
7#[derive(Debug, PartialEq, Eq, Clone, Default)]
8#[non_exhaustive]
9pub struct NeighbourMessage {
10    pub header: NeighbourHeader,
11    pub attributes: Vec<NeighbourAttribute>,
12}
13
14impl Emitable for NeighbourMessage {
15    fn buffer_len(&self) -> usize {
16        self.header.buffer_len() + self.attributes.as_slice().buffer_len()
17    }
18
19    fn emit(&self, buffer: &mut [u8]) {
20        self.header.emit(buffer);
21        self.attributes.as_slice().emit(&mut buffer[self.header.buffer_len()..]);
22    }
23}
24
25impl<'a, T: AsRef<[u8]> + 'a> Parseable<NeighbourMessageBuffer<&'a T>> for NeighbourMessage {
26    type Error = NeighbourError;
27    fn parse(buf: &NeighbourMessageBuffer<&'a T>) -> Result<Self, NeighbourError> {
28        // unwrap: parsing the header is always ok.
29        let header = NeighbourHeader::parse(buf).unwrap();
30        let address_family = header.family;
31        Ok(NeighbourMessage {
32            header,
33            attributes: Vec::<NeighbourAttribute>::parse_with_param(buf, address_family)?,
34        })
35    }
36}
37
38impl<'a, T: AsRef<[u8]> + 'a> ParseableParametrized<NeighbourMessageBuffer<&'a T>, AddressFamily>
39    for Vec<NeighbourAttribute>
40{
41    type Error = NeighbourError;
42    fn parse_with_param(
43        buf: &NeighbourMessageBuffer<&'a T>,
44        address_family: AddressFamily,
45    ) -> Result<Self, NeighbourError> {
46        let mut attributes = vec![];
47        for nla_buf in buf.attributes() {
48            attributes.push(NeighbourAttribute::parse_with_param(&nla_buf?, address_family)?);
49        }
50        Ok(attributes)
51    }
52}