diff options
-rw-r--r-- | rustables/src/expr/counter.rs | 23 | ||||
-rw-r--r-- | rustables/src/expr/mod.rs | 2 |
2 files changed, 22 insertions, 3 deletions
diff --git a/rustables/src/expr/counter.rs b/rustables/src/expr/counter.rs index c2a0b5d..d254543 100644 --- a/rustables/src/expr/counter.rs +++ b/rustables/src/expr/counter.rs @@ -4,10 +4,29 @@ use std::os::raw::c_char; /// A counter expression adds a counter to the rule that is incremented to count number of packets /// and number of bytes for all packets that has matched the rule. -pub struct Counter; +pub struct Counter { + pub nb_bytes: u64, + pub nb_packets: u64, +} + +impl Counter { + pub fn new() -> Self { + Self { + nb_bytes: 0, + nb_packets: 0, + } + } +} impl Expression for Counter { fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr { - try_alloc!(unsafe { sys::nftnl_expr_alloc(b"counter\0" as *const _ as *const c_char) }) + unsafe { + let expr = try_alloc!(sys::nftnl_expr_alloc( + b"counter\0" as *const _ as *const c_char + )); + sys::nftnl_expr_set_u64(expr, sys::NFTNL_EXPR_CTR_BYTES as u16, self.nb_bytes); + sys::nftnl_expr_set_u64(expr, sys::NFTNL_EXPR_CTR_PACKETS as u16, self.nb_packets); + expr + } } } diff --git a/rustables/src/expr/mod.rs b/rustables/src/expr/mod.rs index a86b44d..4493662 100644 --- a/rustables/src/expr/mod.rs +++ b/rustables/src/expr/mod.rs @@ -120,7 +120,7 @@ macro_rules! nft_expr { nft_expr_cmp!($op $data) }; (counter) => { - $crate::expr::Counter + $crate::expr::Counter { nb_bytes: 0, nb_packets: 0} }; (ct $key:ident set) => { nft_expr_ct!($key set) |