nftnl_rs/netlink/linux/
netfilter.rs

1use std::net::IpAddr;
2
3use crate::{error::NftnlError, int_error_code};
4
5
6#[allow(non_camel_case_types)]
7#[repr(u8)]
8#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
9pub enum Nfproto 
10{
11	NFPROTO_UNSPEC =  0,
12	NFPROTO_INET   =  1,
13	NFPROTO_IPV4   =  2,
14	NFPROTO_ARP    =  3,
15	NFPROTO_NETDEV =  5,
16	NFPROTO_BRIDGE =  7,
17	NFPROTO_IPV6   = 10,
18	NFPROTO_DECNET = 12,
19	NFPROTO_NUMPROTO,
20}
21
22impl From<Nfproto> for u8
23{
24    fn from(value: Nfproto) -> Self 
25	{
26        return value as u8;
27    }
28}
29
30impl TryFrom<u8> for Nfproto
31{
32    type Error = NftnlError;
33
34    fn try_from(value: u8) -> Result<Self, Self::Error> 
35	{
36        match value
37		{
38			x if x == Self::NFPROTO_UNSPEC.into() =>  
39				return Ok(Self::NFPROTO_UNSPEC),
40			x if x == Self::NFPROTO_INET.into() =>  
41				return Ok(Self::NFPROTO_INET),
42			x if x == Self::NFPROTO_IPV4.into() =>  
43				return Ok(Self::NFPROTO_IPV4),
44			x if x == Self::NFPROTO_ARP.into() =>  
45				return Ok(Self::NFPROTO_ARP),
46			x if x == Self::NFPROTO_NETDEV.into() =>  
47				return Ok(Self::NFPROTO_NETDEV),
48			x if x == Self::NFPROTO_BRIDGE.into() =>  
49				return Ok(Self::NFPROTO_BRIDGE),
50			x if x == Self::NFPROTO_IPV6.into() =>  
51				return Ok(Self::NFPROTO_IPV6),
52			x if x == Self::NFPROTO_DECNET.into() =>  
53				return Ok(Self::NFPROTO_DECNET),
54			_ => 
55				int_error_code!(libc::EINVAL, "unknown Nfproto discriminant: '{}'", value),
56		}
57    }
58}
59
60impl From<&IpAddr> for Nfproto
61{
62    fn from(value: &IpAddr) -> Self 
63	{
64        match value
65		{
66			IpAddr::V4(_) => return Self::NFPROTO_IPV4,
67			IpAddr::V6(_) => return Self::NFPROTO_IPV6,
68		}
69    }
70}