Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #45 - Add a log expression with group argument #46

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions nftnl/src/expr/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use super::{Expression, Rule};
use nftnl_sys::{
self as sys,
libc::c_char,
};

/// A log expression.
pub struct Log {
group: Option<LogGroup>,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum LogGroup {
Copy link
Member

@dlon dlon Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any reason for limiting this to 8. u16 seems to be the actual type, so I think it would make sense to use that.

LogGroupZero,
LogGroupOne,
LogGroupTwo,
LogGroupThree,
LogGroupFour,
LogGroupFive,
LogGroupSix,
LogGroupSeven,
}


impl Log {
pub fn new(group: Option<LogGroup>) -> Self {
Log { group }
}
}

impl Expression for Log {
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(group) = self.group {
sys::nftnl_expr_set_u32(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be nftnl_expr_set_u16.

expr,
sys::NFTNL_EXPR_LOG_GROUP as u16,
group as u32,
);
};
expr
}
}
}

#[macro_export]
macro_rules! nft_expr_log {
(group $group:ident) => {
$crate::expr::Log::new($group)
};
() => {
$crate::expr::Log::new(None)
};
}
9 changes: 9 additions & 0 deletions nftnl/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub use self::nat::*;
mod payload;
pub use self::payload::*;

mod log;
pub use self::log::*;

mod verdict;
pub use self::verdict::*;

Expand All @@ -71,6 +74,12 @@ macro_rules! nft_expr {
(cmp $op:tt $data:expr) => {
nft_expr_cmp!($op $data)
};
(log group $group:ident) => {
nft_expr_log!(group $group)
};
(log) => {
nft_expr_log!()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you call crate::expr::Log::new(None) here directly instead?

};
(counter) => {
$crate::expr::Counter
};
Expand Down