diff options
author | Simon THOBY <git@nightmared.fr> | 2021-10-23 18:14:47 +0200 |
---|---|---|
committer | Simon THOBY <git@nightmared.fr> | 2021-11-02 22:18:12 +0100 |
commit | 46dbd76149346ba978c69048a09b3657e3b86f0a (patch) | |
tree | 03405b74d8f58db986a406ed9dfa47ec3bfb9360 /rustables/src/expr/mod.rs | |
parent | 2f1261eae4d52165bc4abed0762c1b20a0f2b4bc (diff) |
reorganize rustables/src/expr/mod.rs
Diffstat (limited to 'rustables/src/expr/mod.rs')
-rw-r--r-- | rustables/src/expr/mod.rs | 132 |
1 files changed, 25 insertions, 107 deletions
diff --git a/rustables/src/expr/mod.rs b/rustables/src/expr/mod.rs index f2a4d5e..1993f15 100644 --- a/rustables/src/expr/mod.rs +++ b/rustables/src/expr/mod.rs @@ -3,116 +3,9 @@ //! //! [`Rule`]: struct.Rule.html -use std::ffi::CStr; -use std::ffi::CString; -use std::fmt::Debug; -use std::rc::Rc; - use super::rule::Rule; use rustables_sys::{self as sys, libc}; -pub struct ExpressionWrapper { - pub(crate) expr: *const sys::nftnl_expr, - // we also need the rule here to ensure that the rule lives as long as the `expr` pointer - #[allow(dead_code)] - pub(crate) rule: Rc<Rule>, -} - -impl Debug for ExpressionWrapper { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self.get_str()) - } -} - -impl ExpressionWrapper { - /// Retrieves a textual description of the expression. - pub fn get_str(&self) -> CString { - let mut descr_buf = vec![0i8; 4096]; - unsafe { - sys::nftnl_expr_snprintf( - descr_buf.as_mut_ptr(), - (descr_buf.len() - 1) as u64, - self.expr, - sys::NFTNL_OUTPUT_DEFAULT, - 0, - ); - CStr::from_ptr(descr_buf.as_ptr()).to_owned() - } - } - - /// Retrieves the type of expression ("log", "counter", ...). - pub fn get_kind(&self) -> Option<&CStr> { - unsafe { - let ptr = sys::nftnl_expr_get_str(self.expr, sys::NFTNL_EXPR_NAME as u16); - if !ptr.is_null() { - Some(CStr::from_ptr(ptr)) - } else { - None - } - } - } - - /// Attempt to decode the expression as the type T, returning None if such - /// conversion is not possible or failed. - pub fn decode_expr<T: Expression>(&self) -> Option<T> { - if let Some(kind) = self.get_kind() { - let raw_name = unsafe { CStr::from_ptr(T::get_raw_name()) }; - if kind == raw_name { - return T::from_expr(self.expr); - } - } - None - } -} - -/// Trait for every safe wrapper of an nftables expression. -pub trait Expression { - /// Returns the raw name used by nftables to identify the rule. - fn get_raw_name() -> *const libc::c_char; - - /// Try to parse the expression from a raw nftables expression, - /// returning None if the attempted parsing failed. - fn from_expr(_expr: *const sys::nftnl_expr) -> Option<Self> - where - Self: Sized, - { - None - } - - /// Allocates and returns the low level `nftnl_expr` representation of this expression. - /// The caller to this method is responsible for freeing the expression. - fn to_expr(&self, rule: &Rule) -> *mut sys::nftnl_expr; -} - -/// A netfilter data register. The expressions store and read data to and from these -/// when evaluating rule statements. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -#[repr(i32)] -pub enum Register { - Verdict = libc::NFT_REG_VERDICT, - Reg1 = libc::NFT_REG_1, - Reg2 = libc::NFT_REG_2, - Reg3 = libc::NFT_REG_3, - Reg4 = libc::NFT_REG_4, -} - -impl Register { - pub fn to_raw(self) -> u32 { - self as u32 - } - - pub fn from_raw(val: u32) -> Option<Self> { - match val as i32 { - libc::NFT_REG_VERDICT => Some(Self::Verdict), - libc::NFT_REG_1 => Some(Self::Reg1), - libc::NFT_REG_2 => Some(Self::Reg2), - libc::NFT_REG_3 => Some(Self::Reg3), - libc::NFT_REG_4 => Some(Self::Reg4), - _ => None, - } - } -} - mod bitwise; pub use self::bitwise::*; @@ -149,6 +42,31 @@ pub use self::payload::*; mod verdict; pub use self::verdict::*; +mod register; +pub use self::register::Register; + +mod wrapper; +pub use self::wrapper::ExpressionWrapper; + +/// Trait for every safe wrapper of an nftables expression. +pub trait Expression { + /// Returns the raw name used by nftables to identify the rule. + fn get_raw_name() -> *const libc::c_char; + + /// Try to parse the expression from a raw nftables expression, + /// returning None if the attempted parsing failed. + fn from_expr(_expr: *const sys::nftnl_expr) -> Option<Self> + where + Self: Sized, + { + None + } + + /// Allocates and returns the low level `nftnl_expr` representation of this expression. + /// The caller to this method is responsible for freeing the expression. + fn to_expr(&self, rule: &Rule) -> *mut sys::nftnl_expr; +} + #[macro_export(local_inner_macros)] macro_rules! nft_expr { (bitwise mask $mask:expr,xor $xor:expr) => { |