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

Add Log Expression #56

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions nftnl/src/expr/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use super::{Expression, Rule};
use nftnl_sys::{self as sys, libc::c_char};

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

impl Log {
pub fn new() -> Self {
Log { group: None }
}

pub fn new_with_group(group: u16) -> Self {
Log { group: Some(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_u16(expr, sys::NFTNL_EXPR_LOG_GROUP as u16, group as u16);
Copy link
Member

Choose a reason for hiding this comment

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

group as u16 <- group is already a u16, no need to cast.

};
expr
}
}
}

#[macro_export]
macro_rules! 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.

You also need to add this as a separate branch into nft_expr! macro also. So it's possible to create all types of expression with just nft_expr!(...)

(group $group:expr) => {
$crate::expr::Log::new_with_group($group)
};
() => {
$crate::expr::Log::new()
};
}
3 changes: 3 additions & 0 deletions nftnl/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub use self::ct::*;
mod immediate;
pub use self::immediate::*;

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

mod lookup;
pub use self::lookup::*;

Expand Down