netlink_packet_route/rtnl/neighbour/
header.rs

1// SPDX-License-Identifier: MIT
2
3use crate::{
4    traits::{Emitable, Parseable},
5    DecodeError,
6    NeighbourMessageBuffer,
7    NEIGHBOUR_HEADER_LEN,
8};
9
10/// Neighbour headers have the following structure:
11///
12/// ```no_rust
13/// 0                8                16              24               32
14/// +----------------+----------------+----------------+----------------+
15/// |     family     |                     padding                      |
16/// +----------------+----------------+----------------+----------------+
17/// |                             link index                            |
18/// +----------------+----------------+----------------+----------------+
19/// |              state              |     flags      |     ntype      |
20/// +----------------+----------------+----------------+----------------+
21/// ```
22///
23/// `NeighbourHeader` exposes all these fields.
24#[derive(Debug, PartialEq, Eq, Clone, Default)]
25pub struct NeighbourHeader {
26    pub family: u8,
27    pub ifindex: u32,
28    /// Neighbour cache entry state. It should be set to one of the
29    /// `NUD_*` constants
30    pub state: u16,
31    /// Neighbour cache entry flags. It should be set to a combination
32    /// of the `NTF_*` constants
33    pub flags: u8,
34    /// Neighbour cache entry type. It should be set to one of the
35    /// `NDA_*` constants.
36    pub ntype: u8,
37}
38
39impl<T: AsRef<[u8]>> Parseable<NeighbourMessageBuffer<T>> for NeighbourHeader {
40    fn parse(buf: &NeighbourMessageBuffer<T>) -> Result<Self, DecodeError> {
41        Ok(Self {
42            family: buf.family(),
43            ifindex: buf.ifindex(),
44            state: buf.state(),
45            flags: buf.flags(),
46            ntype: buf.ntype(),
47        })
48    }
49}
50
51impl Emitable for NeighbourHeader {
52    fn buffer_len(&self) -> usize {
53        NEIGHBOUR_HEADER_LEN
54    }
55
56    fn emit(&self, buffer: &mut [u8]) {
57        let mut packet = NeighbourMessageBuffer::new(buffer);
58        packet.set_family(self.family);
59        packet.set_ifindex(self.ifindex);
60        packet.set_state(self.state);
61        packet.set_flags(self.flags);
62        packet.set_ntype(self.ntype);
63    }
64}