diff options
author | Simon THOBY <git@nightmared.fr> | 2021-11-05 06:23:45 +0000 |
---|---|---|
committer | Simon THOBY <git@nightmared.fr> | 2021-11-05 06:23:45 +0000 |
commit | 46b22d88c36863851e4b27efa767d28c8aeecfe0 (patch) | |
tree | ab1a638de7587e7b73fe64093428218e1c545004 /rustables/src/expr/log.rs | |
parent | 3f61ea42bd291c208d07006d8019c25d588f9183 (diff) | |
parent | 1bec5a5c30541e47e9c7cff839ac0e7dd3fb6215 (diff) |
Merge branch 'manipulate-exprs' into 'master'
Add functions to iterate over the expressions of existing rules
See merge request rustwall/rustables!3
Diffstat (limited to 'rustables/src/expr/log.rs')
-rw-r--r-- | rustables/src/expr/log.rs | 78 |
1 files changed, 54 insertions, 24 deletions
diff --git a/rustables/src/expr/log.rs b/rustables/src/expr/log.rs index aa7a8b7..db96ba9 100644 --- a/rustables/src/expr/log.rs +++ b/rustables/src/expr/log.rs @@ -1,34 +1,54 @@ -use super::{Expression, Rule}; +use super::{DeserializationError, Expression, Rule}; use rustables_sys as sys; +use std::ffi::{CStr, CString}; use std::os::raw::c_char; -use std::ffi::CString; use thiserror::Error; /// A Log expression will log all packets that match the rule. +#[derive(Debug, PartialEq)] pub struct Log { pub group: Option<LogGroup>, - pub prefix: Option<LogPrefix> + pub prefix: Option<LogPrefix>, } impl Expression for Log { - fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr { + fn get_raw_name() -> *const sys::libc::c_char { + b"log\0" as *const _ as *const c_char + } + + fn from_expr(expr: *const sys::nftnl_expr) -> Result<Self, DeserializationError> + where + Self: Sized, + { unsafe { - let expr = try_alloc!(sys::nftnl_expr_alloc( - b"log\0" as *const _ as *const c_char - )); - if let Some(log_group) = self.group { - sys::nftnl_expr_set_u32( + let mut group = None; + if sys::nftnl_expr_is_set(expr, sys::NFTNL_EXPR_LOG_GROUP as u16) { + group = Some(LogGroup(sys::nftnl_expr_get_u32( expr, sys::NFTNL_EXPR_LOG_GROUP as u16, - log_group.0 as u32, - ); + ) as u16)); + } + let mut prefix = None; + if sys::nftnl_expr_is_set(expr, sys::NFTNL_EXPR_LOG_PREFIX as u16) { + let raw_prefix = sys::nftnl_expr_get_str(expr, sys::NFTNL_EXPR_LOG_PREFIX as u16); + if raw_prefix.is_null() { + return Err(DeserializationError::NullPointer); + } else { + prefix = Some(LogPrefix(CStr::from_ptr(raw_prefix).to_owned())); + } + } + Ok(Log { group, prefix }) + } + } + + fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr { + unsafe { + let expr = try_alloc!(sys::nftnl_expr_alloc(b"log\0" as *const _ as *const c_char)); + if let Some(log_group) = self.group { + sys::nftnl_expr_set_u32(expr, sys::NFTNL_EXPR_LOG_GROUP as u16, log_group.0 as u32); }; if let Some(LogPrefix(prefix)) = &self.prefix { - sys::nftnl_expr_set_str( - expr, - sys::NFTNL_EXPR_LOG_PREFIX as u16, - prefix.as_ptr() - ); + sys::nftnl_expr_set_str(expr, sys::NFTNL_EXPR_LOG_PREFIX as u16, prefix.as_ptr()); }; expr @@ -41,8 +61,7 @@ pub enum LogPrefixError { #[error("The log prefix string is more than 128 characters long")] TooLongPrefix, #[error("The log prefix string contains an invalid Nul character.")] - PrefixContainsANul(#[from] std::ffi::NulError) - + PrefixContainsANul(#[from] std::ffi::NulError), } /// The NFLOG group that will be assigned to each log line. @@ -58,25 +77,36 @@ impl LogPrefix { /// that LogPrefix should not be more than 127 characters long. pub fn new(prefix: &str) -> Result<Self, LogPrefixError> { if prefix.chars().count() > 127 { - return Err(LogPrefixError::TooLongPrefix) + return Err(LogPrefixError::TooLongPrefix); } Ok(LogPrefix(CString::new(prefix)?)) } } - #[macro_export] macro_rules! nft_expr_log { (group $group:ident prefix $prefix:expr) => { - $crate::expr::Log { group: $group, prefix: $prefix } + $crate::expr::Log { + group: $group, + prefix: $prefix, + } }; (prefix $prefix:expr) => { - $crate::expr::Log { group: None, prefix: $prefix } + $crate::expr::Log { + group: None, + prefix: $prefix, + } }; (group $group:ident) => { - $crate::expr::Log { group: $group, prefix: None } + $crate::expr::Log { + group: $group, + prefix: None, + } }; () => { - $crate::expr::Log { group: None, prefix: None } + $crate::expr::Log { + group: None, + prefix: None, + } }; } |