diff options
author | Simon THOBY <git@nightmared.fr> | 2021-11-02 21:04:20 +0000 |
---|---|---|
committer | Simon THOBY <git@nightmared.fr> | 2021-11-02 21:04:20 +0000 |
commit | 3f61ea42bd291c208d07006d8019c25d588f9183 (patch) | |
tree | 6a73e12c3a98c39d7580dd2d4f5ababe86532799 | |
parent | 7402a2b5729f61d8e700bbb21636e35cf6dd44b1 (diff) | |
parent | 82432a1aede0f72484b7b1453e2f563817ea1f06 (diff) |
Merge branch 'add-log-arguments' into 'master'
add LogGroup and LogPrefix arguments to Log target
See merge request rustwall/rustables!5
-rw-r--r-- | rustables/src/expr/log.rs | 74 | ||||
-rw-r--r-- | rustables/src/expr/mod.rs | 9 |
2 files changed, 81 insertions, 2 deletions
diff --git a/rustables/src/expr/log.rs b/rustables/src/expr/log.rs index d6e0089..aa7a8b7 100644 --- a/rustables/src/expr/log.rs +++ b/rustables/src/expr/log.rs @@ -1,12 +1,82 @@ use super::{Expression, Rule}; use rustables_sys as sys; use std::os::raw::c_char; +use std::ffi::CString; +use thiserror::Error; /// A Log expression will log all packets that match the rule. -pub struct Log; +pub struct Log { + pub group: Option<LogGroup>, + pub prefix: Option<LogPrefix> +} impl Expression for Log { fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr { - try_alloc!(unsafe { sys::nftnl_expr_alloc(b"log\0" as *const _ as *const c_char) }) + 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() + ); + }; + + expr + } } } + +#[derive(Error, Debug)] +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) + +} + +/// The NFLOG group that will be assigned to each log line. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +pub struct LogGroup(pub u16); + +/// A prefix that will get prepended to each log line. +#[derive(Debug, Clone, PartialEq)] +pub struct LogPrefix(CString); + +impl LogPrefix { + /// Create a new LogPrefix from a String. Converts it to CString as needed by nftnl. Note + /// 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) + } + 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 } + }; + (prefix $prefix:expr) => { + $crate::expr::Log { group: None, prefix: $prefix } + }; + (group $group:ident) => { + $crate::expr::Log { group: $group, prefix: None } + }; + () => { + $crate::expr::Log { group: None, prefix: None } + }; +} diff --git a/rustables/src/expr/mod.rs b/rustables/src/expr/mod.rs index 1364904..99ea44b 100644 --- a/rustables/src/expr/mod.rs +++ b/rustables/src/expr/mod.rs @@ -86,6 +86,15 @@ macro_rules! nft_expr { (immediate $expr:ident $value:expr) => { nft_expr_immediate!($expr $value) }; + (log group $group:ident prefix $prefix:expr) => { + nft_expr_log!(group $group prefix $prefix) + }; + (log group $group:ident) => { + nft_expr_log!(group $group) + }; + (log prefix $prefix:expr) => { + nft_expr_log!(prefix $prefix) + }; (log) => { nft_expr_log!() }; |