netlink_packet_route/neighbour/
message.rs

1// SPDX-License-Identifier: MIT
2
3use netlink_packet_core::{
4    DecodeError, Emitable, ErrorContext, Parseable, ParseableParametrized,
5};
6
7use super::{
8    super::AddressFamily, NeighbourAttribute, NeighbourHeader,
9    NeighbourMessageBuffer,
10};
11
12#[derive(Debug, PartialEq, Eq, Clone, Default)]
13#[non_exhaustive]
14pub struct NeighbourMessage {
15    pub header: NeighbourHeader,
16    pub attributes: Vec<NeighbourAttribute>,
17}
18
19impl Emitable for NeighbourMessage {
20    fn buffer_len(&self) -> usize {
21        self.header.buffer_len() + self.attributes.as_slice().buffer_len()
22    }
23
24    fn emit(&self, buffer: &mut [u8]) {
25        self.header.emit(buffer);
26        self.attributes
27            .as_slice()
28            .emit(&mut buffer[self.header.buffer_len()..]);
29    }
30}
31
32impl<'a, T: AsRef<[u8]> + 'a> Parseable<NeighbourMessageBuffer<&'a T>>
33    for NeighbourMessage
34{
35    fn parse(buf: &NeighbourMessageBuffer<&'a T>) -> Result<Self, DecodeError> {
36        let header = NeighbourHeader::parse(buf)
37            .context("failed to parse neighbour message header")?;
38        let address_family = header.family;
39        Ok(NeighbourMessage {
40            header,
41            attributes: Vec::<NeighbourAttribute>::parse_with_param(
42                buf,
43                address_family,
44            )
45            .context("failed to parse neighbour message NLAs")?,
46        })
47    }
48}
49
50impl<'a, T: AsRef<[u8]> + 'a>
51    ParseableParametrized<NeighbourMessageBuffer<&'a T>, AddressFamily>
52    for Vec<NeighbourAttribute>
53{
54    fn parse_with_param(
55        buf: &NeighbourMessageBuffer<&'a T>,
56        address_family: AddressFamily,
57    ) -> Result<Self, DecodeError> {
58        let mut attributes = vec![];
59        for nla_buf in buf.attributes() {
60            attributes.push(NeighbourAttribute::parse_with_param(
61                &nla_buf?,
62                address_family,
63            )?);
64        }
65        Ok(attributes)
66    }
67}