netlink_packet_route/link/
prop_list.rs

1// SPDX-License-Identifier: MIT
2
3use netlink_packet_core::{
4    parse_string, DecodeError, DefaultNla, ErrorContext, Nla, NlaBuffer,
5    Parseable,
6};
7
8const IFLA_ALT_IFNAME: u16 = 53;
9
10#[derive(Debug, PartialEq, Eq, Clone)]
11#[non_exhaustive]
12pub enum Prop {
13    AltIfName(String),
14    Other(DefaultNla),
15}
16
17impl Nla for Prop {
18    #[rustfmt::skip]
19    fn value_len(&self) -> usize {
20        use self::Prop::*;
21        match self {
22            AltIfName(ref string) => string.len() + 1,
23            Other(nla) => nla.value_len()
24        }
25    }
26
27    #[rustfmt::skip]
28    fn emit_value(&self, buffer: &mut [u8]) {
29        use self::Prop::*;
30        match self {
31            AltIfName(ref string) => {
32                buffer[..string.len()].copy_from_slice(string.as_bytes());
33                buffer[string.len()] = 0;
34            },
35            Other(nla) => nla.emit_value(buffer)
36        }
37    }
38
39    fn kind(&self) -> u16 {
40        use self::Prop::*;
41        match self {
42            AltIfName(_) => IFLA_ALT_IFNAME,
43            Other(nla) => nla.kind(),
44        }
45    }
46}
47
48impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Prop {
49    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
50        let payload = buf.value();
51        Ok(match buf.kind() {
52            IFLA_ALT_IFNAME => Prop::AltIfName(
53                parse_string(payload)
54                    .context("invalid IFLA_ALT_IFNAME value")?,
55            ),
56            kind => Prop::Other(
57                DefaultNla::parse(buf)
58                    .context(format!("Unknown NLA type {kind}"))?,
59            ),
60        })
61    }
62}