aboutsummaryrefslogtreecommitdiff
path: root/src/expr/counter.rs
blob: 4732e857c98ed3791fd7e1f54c15c6453c5f2786 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use super::{DeserializationError, Expression, Rule};
use crate::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 have matched the rule.
#[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 {
        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
        }
    }
}