diff options
author | Simon THOBY <git@nightmared.fr> | 2021-10-20 21:33:20 +0200 |
---|---|---|
committer | Simon THOBY <git@nightmared.fr> | 2021-11-02 22:17:44 +0100 |
commit | 180c4d5c8ff86836e0f440d7d0540c02c168c4bf (patch) | |
tree | 889d82104a18cb7fbbcb0774a52bdf2af80b40a8 /rustables/src | |
parent | 2036da41552f5e19d3de08e9bbe4d3bdfea761f0 (diff) |
counter: add the possibility to start from a non-zero value
Diffstat (limited to 'rustables/src')
-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) |