neli/consts/
genl.rs

1use std::{io::Cursor, mem::size_of};
2
3use neli_proc_macros::neli_enum;
4
5use crate::{
6    self as neli, FromBytes, Size, ToBytes, TypeSize,
7    consts::{
8        netfilter::{NfLogAttr, NfLogCfg},
9        nl::NlmsgerrAttr,
10    },
11    err::{DeError, SerError},
12};
13
14impl_trait!(
15    /// Trait marking constants valid for use in
16    /// [`Genlmsghdr`][crate::genl::Genlmsghdr] field, `cmd`.
17    pub Cmd,
18    u8,
19    /// Wrapper valid for use with all values in the [`Genlmsghdr`]
20    /// field, `cmd`
21    CmdConsts,
22    CtrlCmd
23);
24
25/// Values for `cmd` in [`Genlmsghdr`][crate::genl::Genlmsghdr].
26#[neli_enum(serialized_type = "u8")]
27pub enum CtrlCmd {
28    Unspec = libc::CTRL_CMD_UNSPEC as u8,
29    Newfamily = libc::CTRL_CMD_NEWFAMILY as u8,
30    Delfamily = libc::CTRL_CMD_DELFAMILY as u8,
31    Getfamily = libc::CTRL_CMD_GETFAMILY as u8,
32    Newops = libc::CTRL_CMD_NEWOPS as u8,
33    Delops = libc::CTRL_CMD_DELOPS as u8,
34    Getops = libc::CTRL_CMD_GETOPS as u8,
35    NewmcastGrp = libc::CTRL_CMD_NEWMCAST_GRP as u8,
36    DelmcastGrp = libc::CTRL_CMD_DELMCAST_GRP as u8,
37    GetmcastGrp = libc::CTRL_CMD_GETMCAST_GRP as u8,
38}
39
40impl_trait!(
41    /// Marker trait for types usable in the
42    /// [`Nlattr`][crate::genl::Nlattr] field, `nla_type`
43    pub NlAttrType,
44    u16,
45    /// Wrapper that is usable with all values in the
46    /// [`Nlattr`][crate::genl::Nlattr] field, `nla_type`.
47    pub NlAttrTypeWrapper,
48    CtrlAttr,
49    CtrlAttrMcastGrp,
50    NfLogAttr,
51    NfLogCfg,
52    Index,
53    NlmsgerrAttr,
54);
55
56/// Values for `nla_type` in [`Nlattr`][crate::genl::Nlattr]
57#[neli_enum(serialized_type = "u16")]
58pub enum CtrlAttr {
59    Unspec = libc::CTRL_ATTR_UNSPEC as u16,
60    FamilyId = libc::CTRL_ATTR_FAMILY_ID as u16,
61    FamilyName = libc::CTRL_ATTR_FAMILY_NAME as u16,
62    Version = libc::CTRL_ATTR_VERSION as u16,
63    Hdrsize = libc::CTRL_ATTR_HDRSIZE as u16,
64    Maxattr = libc::CTRL_ATTR_MAXATTR as u16,
65    Ops = libc::CTRL_ATTR_OPS as u16,
66    McastGroups = libc::CTRL_ATTR_MCAST_GROUPS as u16,
67}
68
69/// Values for `nla_type` in [`Nlattr`][crate::genl::Nlattr]
70#[neli_enum(serialized_type = "u16")]
71pub enum CtrlAttrMcastGrp {
72    Unspec = libc::CTRL_ATTR_MCAST_GRP_UNSPEC as u16,
73    Name = libc::CTRL_ATTR_MCAST_GRP_NAME as u16,
74    Id = libc::CTRL_ATTR_MCAST_GRP_ID as u16,
75}
76
77/// Type representing attribute list types as indices
78#[derive(Debug, PartialEq, Eq, Clone, Copy, Size)]
79pub struct Index(u16);
80
81impl Index {
82    fn is_unrecognized(self) -> bool {
83        false
84    }
85}
86
87impl TypeSize for Index {
88    fn type_size() -> usize {
89        size_of::<u16>()
90    }
91}
92
93impl ToBytes for Index {
94    fn to_bytes(&self, buffer: &mut Cursor<Vec<u8>>) -> Result<(), SerError> {
95        self.0.to_bytes(buffer)
96    }
97}
98
99impl FromBytes for Index {
100    fn from_bytes(buffer: &mut Cursor<impl AsRef<[u8]>>) -> Result<Self, DeError> {
101        Ok(Index(u16::from_bytes(buffer)?))
102    }
103}
104
105impl From<Index> for u16 {
106    fn from(i: Index) -> Self {
107        i.0
108    }
109}
110
111impl From<&Index> for u16 {
112    fn from(i: &Index) -> Self {
113        i.0
114    }
115}
116
117impl From<u16> for Index {
118    fn from(v: u16) -> Self {
119        Index(v)
120    }
121}