aboutsummaryrefslogtreecommitdiff
path: root/rustables/src/expr/counter.rs
diff options
context:
space:
mode:
authorSimon THOBY <git@nightmared.fr>2021-11-05 06:23:45 +0000
committerSimon THOBY <git@nightmared.fr>2021-11-05 06:23:45 +0000
commit46b22d88c36863851e4b27efa767d28c8aeecfe0 (patch)
treeab1a638de7587e7b73fe64093428218e1c545004 /rustables/src/expr/counter.rs
parent3f61ea42bd291c208d07006d8019c25d588f9183 (diff)
parent1bec5a5c30541e47e9c7cff839ac0e7dd3fb6215 (diff)
Merge branch 'manipulate-exprs' into 'master'
Add functions to iterate over the expressions of existing rules See merge request rustwall/rustables!3
Diffstat (limited to 'rustables/src/expr/counter.rs')
-rw-r--r--rustables/src/expr/counter.rs39
1 files changed, 36 insertions, 3 deletions
diff --git a/rustables/src/expr/counter.rs b/rustables/src/expr/counter.rs
index c2a0b5d..099e7fa 100644
--- a/rustables/src/expr/counter.rs
+++ b/rustables/src/expr/counter.rs
@@ -1,13 +1,46 @@
-use super::{Expression, Rule};
+use super::{DeserializationError, Expression, Rule};
use rustables_sys as sys;
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;
+#[derive(Debug, PartialEq)]
+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 get_raw_name() -> *const c_char {
+ b"counter\0" as *const _ as *const c_char
+ }
+
+ fn from_expr(expr: *const sys::nftnl_expr) -> Result<Self, DeserializationError> {
+ unsafe {
+ let nb_bytes = sys::nftnl_expr_get_u64(expr, sys::NFTNL_EXPR_CTR_BYTES as u16);
+ let nb_packets = sys::nftnl_expr_get_u64(expr, sys::NFTNL_EXPR_CTR_PACKETS as u16);
+ Ok(Counter {
+ nb_bytes,
+ nb_packets,
+ })
+ }
+ }
+
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(Self::get_raw_name()));
+ 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
+ }
}
}