diff options
author | Simon THOBY <git@nightmared.fr> | 2022-08-26 21:43:04 +0200 |
---|---|---|
committer | Simon THOBY <git@nightmared.fr> | 2022-08-26 21:52:00 +0200 |
commit | 5d207e951f39531a97619a3bdf900f7aa16efb9e (patch) | |
tree | 3ffcdf67d64d91937df4323cbde7053e60a2389c | |
parent | 3e48e7efa516183d623f80d2e4e393cecc2acde9 (diff) |
anonymous set support
-rw-r--r-- | src/query.rs | 2 | ||||
-rw-r--r-- | src/set.rs | 38 | ||||
-rw-r--r-- | tests/expr.rs | 2 | ||||
-rw-r--r-- | tests/lib.rs | 1 |
4 files changed, 22 insertions, 21 deletions
diff --git a/src/query.rs b/src/query.rs index bc1d02e..d7574e8 100644 --- a/src/query.rs +++ b/src/query.rs @@ -48,7 +48,7 @@ mod inner { ProcessNetlinkError(#[source] std::io::Error), #[error("Custom error when customizing the query")] - InitError(#[from] Box<dyn std::error::Error + 'static>), + InitError(#[from] Box<dyn std::error::Error + Send + 'static>), #[error("Couldn't allocate a netlink object, out of memory ?")] NetlinkAllocationFailed, @@ -1,5 +1,5 @@ use crate::sys::{self, libc}; -use crate::{table::Table, MsgType, ProtoFamily}; +use crate::{table::Table, MsgType}; use std::{ cell::Cell, ffi::{c_void, CStr, CString}, @@ -11,14 +11,14 @@ use std::{ #[macro_export] macro_rules! nft_set { - ($name:expr, $id:expr, $table:expr, $family:expr) => { - $crate::set::Set::new($name, $id, $table, $family) + ($name:expr, $id:expr, $table:expr) => { + $crate::set::Set::new(Some($name), $id, $table, $family) }; - ($name:expr, $id:expr, $table:expr, $family:expr; [ ]) => { - nft_set!($name, $id, $table, $family) + ($name:expr, $id:expr, $table:expr; [ ]) => { + nft_set!(Some($name), $id, $table) }; - ($name:expr, $id:expr, $table:expr, $family:expr; [ $($value:expr,)* ]) => {{ - let mut set = nft_set!($name, $id, $table, $family).expect("Set allocation failed"); + ($name:expr, $id:expr, $table:expr; [ $($value:expr,)* ]) => {{ + let mut set = nft_set!(Some($name), $id, $table).expect("Set allocation failed"); $( set.add($value).expect(stringify!(Unable to add $value to set $name)); )* @@ -29,19 +29,18 @@ macro_rules! nft_set { pub struct Set<K> { pub(crate) set: *mut sys::nftnl_set, pub(crate) table: Rc<Table>, - pub(crate) family: ProtoFamily, _marker: ::std::marker::PhantomData<K>, } impl<K> Set<K> { - pub fn new(name: &CStr, id: u32, table: Rc<Table>, family: ProtoFamily) -> Self + pub fn new(name: &CStr, id: u32, table: Rc<Table>) -> Self where K: SetKey, { unsafe { let set = try_alloc!(sys::nftnl_set_alloc()); - sys::nftnl_set_set_u32(set, sys::NFTNL_SET_FAMILY as u16, family as u32); + sys::nftnl_set_set_u32(set, sys::NFTNL_SET_FAMILY as u16, table.get_family() as u32); sys::nftnl_set_set_str(set, sys::NFTNL_SET_TABLE as u16, table.get_name().as_ptr()); sys::nftnl_set_set_str(set, sys::NFTNL_SET_NAME as u16, name.as_ptr()); sys::nftnl_set_set_u32(set, sys::NFTNL_SET_ID as u16, id); @@ -57,20 +56,18 @@ impl<K> Set<K> { Set { set, table, - family, _marker: ::std::marker::PhantomData, } } } - pub unsafe fn from_raw(set: *mut sys::nftnl_set, table: Rc<Table>, family: ProtoFamily) -> Self + pub unsafe fn from_raw(set: *mut sys::nftnl_set, table: Rc<Table>) -> Self where K: SetKey, { Set { set, table, - family, _marker: ::std::marker::PhantomData, } } @@ -111,10 +108,6 @@ impl<K> Set<K> { self.set } - pub fn get_family(&self) -> ProtoFamily { - self.family - } - /// Returns a textual description of the set. pub fn get_str(&self) -> CString { let mut descr_buf = vec![0i8; 4096]; @@ -237,7 +230,7 @@ unsafe impl<'a, K> crate::NlMsg for SetElemsMsg<'a, K> { let header = sys::nftnl_nlmsg_build_hdr( buf as *mut c_char, type_ as u16, - self.set.get_family() as u16, + self.set.table.get_family() as u16, flags as u16, seq, ); @@ -271,3 +264,12 @@ impl SetKey for Ipv6Addr { self.octets().to_vec().into_boxed_slice() } } + +impl<const N: usize> SetKey for [u8; N] { + const TYPE: u32 = 5; + const LEN: u32 = N as u32; + + fn data(&self) -> Box<[u8]> { + Box::new(*self) + } +} diff --git a/tests/expr.rs b/tests/expr.rs index 4af18f2..7950df3 100644 --- a/tests/expr.rs +++ b/tests/expr.rs @@ -281,7 +281,7 @@ fn lookup_expr_is_valid() { let set_name = &CStr::from_bytes_with_nul(b"mockset\0").unwrap(); let mut rule = get_test_rule(); let table = rule.get_chain().get_table(); - let mut set = Set::new(set_name, 0, table, ProtoFamily::Inet); + let mut set = Set::new(set_name, 0, table); let address: Ipv4Addr = [8, 8, 8, 8].into(); set.add(&address); let lookup = Lookup::new(&set).unwrap(); diff --git a/tests/lib.rs b/tests/lib.rs index 0d7132c..29c61b3 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -120,7 +120,6 @@ pub fn get_test_set<T: SetKey>() -> Set<T> { CStr::from_bytes_with_nul(SET_NAME).unwrap(), SET_ID, Rc::new(get_test_table()), - ProtoFamily::Ipv4, ) } |