netlink_packet_route/neighbour/
address.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// SPDX-License-Identifier: MIT

use std::net::{Ipv4Addr, Ipv6Addr};

use netlink_packet_utils::{DecodeError, Emitable};

use crate::ip::{
    emit_ip_to_buffer, parse_ipv4_addr, parse_ipv6_addr, IPV4_ADDR_LEN, IPV6_ADDR_LEN,
};
use crate::AddressFamily;

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum NeighbourAddress {
    Inet(Ipv4Addr),
    Inet6(Ipv6Addr),
    Other(Vec<u8>),
}

impl NeighbourAddress {
    pub(crate) fn parse_with_param(
        address_family: AddressFamily,
        payload: &[u8],
    ) -> Result<Self, DecodeError> {
        Ok(match address_family {
            AddressFamily::Inet => Self::Inet(parse_ipv4_addr(payload)?),
            AddressFamily::Inet6 => Self::Inet6(parse_ipv6_addr(payload)?),
            _ => Self::Other(payload.to_vec()),
        })
    }
}

impl Emitable for NeighbourAddress {
    fn buffer_len(&self) -> usize {
        match self {
            Self::Inet(_) => IPV4_ADDR_LEN,
            Self::Inet6(_) => IPV6_ADDR_LEN,
            Self::Other(v) => v.len(),
        }
    }

    fn emit(&self, buffer: &mut [u8]) {
        match self {
            Self::Inet(v) => emit_ip_to_buffer(&((*v).into()), buffer),
            Self::Inet6(v) => emit_ip_to_buffer(&((*v).into()), buffer),
            Self::Other(v) => buffer.copy_from_slice(v.as_slice()),
        }
    }
}

impl From<Ipv4Addr> for NeighbourAddress {
    fn from(v: Ipv4Addr) -> Self {
        Self::Inet(v)
    }
}

impl From<Ipv6Addr> for NeighbourAddress {
    fn from(v: Ipv6Addr) -> Self {
        Self::Inet6(v)
    }
}