aboutsummaryrefslogtreecommitdiff
path: root/src/expr/log.rs
blob: 82c201db37125f52f73308b7d398a5680d911338 (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
47
48
49
50
51
52
53
use super::{Expression, ExpressionError};
use crate::create_wrapper_type;
use crate::sys;

// A Log expression will log all packets that match the rule.
create_wrapper_type!(
    inline: Log,
    [
        (
            get_group,
            set_group,
            with_group,
            sys::NFTA_LOG_GROUP,
            group,
            u32
        ),
        (
            get_prefix,
            set_prefix,
            with_prefix,
            sys::NFTA_LOG_PREFIX,
            prefix,
            String
        )
    ]
);

impl Log {
    pub fn new(
        group: Option<u16>,
        prefix: Option<impl Into<String>>,
    ) -> Result<Log, ExpressionError> {
        let mut res = Log::default();
        if let Some(group) = group {
            res.set_group(group);
        }
        if let Some(prefix) = prefix {
            let prefix = prefix.into();

            if prefix.bytes().count() > 127 {
                return Err(ExpressionError::TooLongLogPrefix);
            }
            res.set_prefix(prefix);
        }
        Ok(res)
    }
}

impl Expression for Log {
    fn get_name() -> &'static str {
        "log"
    }
}