From eaddafc10cb0eb4693dd35d890012ffaefa2884b Mon Sep 17 00:00:00 2001 From: lafleur Date: Thu, 21 Oct 2021 15:46:32 +0200 Subject: add LogGroup and LogPrefix arguments to Log target --- rustables/src/expr/log.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) (limited to 'rustables/src/expr/log.rs') diff --git a/rustables/src/expr/log.rs b/rustables/src/expr/log.rs index d6e0089..4345f41 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; /// A Log expression will log all packets that match the rule. -pub struct Log; +pub struct Log { + pub group: Option, + pub prefix: Option +} 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(group) = self.group { + sys::nftnl_expr_set_u32( + expr, + sys::NFTNL_EXPR_LOG_GROUP as u16, + group 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 + } + } +} + + +/// Enumeration of possible NFLOG groups. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +pub enum LogGroup { + LogGroupZero, + LogGroupOne, + LogGroupTwo, + LogGroupThree, + LogGroupFour, + LogGroupFive, + LogGroupSix, + LogGroupSeven, +} + +/// A prefix that will get prepended to each log line. +#[derive(Clone)] +pub struct LogPrefix(pub CString); + +impl LogPrefix { + /// Create a new LogPrefix from a String. Converts it to CString as needed by nftables. + pub fn new(prefix: &str) -> Result { + // TODO check for prefix size constraints. + match CString::new(prefix) { + Ok(string) => Ok(LogPrefix(string)), + Err(error)=> Err(error) + } } } + + +#[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 } + }; +} -- cgit v1.2.3 From efe4317e95c9f4095bddd7c0185805dac9d9af25 Mon Sep 17 00:00:00 2001 From: lafleur Date: Fri, 22 Oct 2021 22:34:14 +0200 Subject: Turn LogGroup in a struct holding an u16 --- rustables/src/expr/log.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'rustables/src/expr/log.rs') diff --git a/rustables/src/expr/log.rs b/rustables/src/expr/log.rs index 4345f41..f5b961f 100644 --- a/rustables/src/expr/log.rs +++ b/rustables/src/expr/log.rs @@ -15,11 +15,11 @@ impl Expression for Log { let expr = try_alloc!(sys::nftnl_expr_alloc( b"log\0" as *const _ as *const c_char )); - if let Some(group) = self.group { + if let Some(log_group) = self.group { sys::nftnl_expr_set_u32( expr, sys::NFTNL_EXPR_LOG_GROUP as u16, - group as u32, + log_group.0 as u32, ); }; if let Some(LogPrefix(prefix)) = &self.prefix { @@ -36,19 +36,12 @@ impl Expression for Log { } -/// Enumeration of possible NFLOG groups. -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -pub enum LogGroup { - LogGroupZero, - LogGroupOne, - LogGroupTwo, - LogGroupThree, - LogGroupFour, - LogGroupFive, - LogGroupSix, - LogGroupSeven, } +/// 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(Clone)] pub struct LogPrefix(pub CString); -- cgit v1.2.3 From b70868d53ed1729d4fa45ee1ee12e3b82e59ae15 Mon Sep 17 00:00:00 2001 From: lafleur Date: Fri, 22 Oct 2021 22:35:02 +0200 Subject: check that LogPrefix is a 128 chars CString at most --- rustables/src/expr/log.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'rustables/src/expr/log.rs') diff --git a/rustables/src/expr/log.rs b/rustables/src/expr/log.rs index f5b961f..614e340 100644 --- a/rustables/src/expr/log.rs +++ b/rustables/src/expr/log.rs @@ -2,6 +2,7 @@ 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 { @@ -35,6 +36,12 @@ impl Expression for Log { } } +#[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) } @@ -47,13 +54,13 @@ pub struct LogGroup(pub u16); pub struct LogPrefix(pub CString); impl LogPrefix { - /// Create a new LogPrefix from a String. Converts it to CString as needed by nftables. - pub fn new(prefix: &str) -> Result { - // TODO check for prefix size constraints. - match CString::new(prefix) { - Ok(string) => Ok(LogPrefix(string)), - Err(error)=> Err(error) + /// 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 { + if prefix.chars().count() > 127 { + return Err(LogPrefixError::TooLongPrefix) } + Ok(LogPrefix(CString::new(prefix)?)) } } -- cgit v1.2.3 From 82432a1aede0f72484b7b1453e2f563817ea1f06 Mon Sep 17 00:00:00 2001 From: lafleur Date: Tue, 2 Nov 2021 21:34:21 +0100 Subject: forbid instanciating LogPrefix directly --- rustables/src/expr/log.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'rustables/src/expr/log.rs') diff --git a/rustables/src/expr/log.rs b/rustables/src/expr/log.rs index 614e340..aa7a8b7 100644 --- a/rustables/src/expr/log.rs +++ b/rustables/src/expr/log.rs @@ -50,8 +50,8 @@ pub enum LogPrefixError { pub struct LogGroup(pub u16); /// A prefix that will get prepended to each log line. -#[derive(Clone)] -pub struct LogPrefix(pub CString); +#[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 -- cgit v1.2.3