Skip to content

Commit

Permalink
fix(stmt): fix named counter
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Use new enum variants of `Counter`: `Named` and `Anonymous`.
  • Loading branch information
jwhb committed Oct 6, 2024
1 parent 51ccf10 commit 9f109c5
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 7 deletions.
150 changes: 150 additions & 0 deletions resources/test/json/counter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"nftables": [
{
"metainfo": {
"version": "1.0.9",
"release_name": "Old Doc Yak #3",
"json_schema_version": 1
}
},
{
"table": {
"family": "inet",
"name": "named_counter_demo",
"handle": 1
}
},
{
"counter": {
"family": "inet",
"name": "cnt_http",
"table": "named_counter_demo",
"handle": 2,
"comment": "count both http and https packets",
"packets": 0,
"bytes": 0
}
},
{
"counter": {
"family": "inet",
"name": "cnt_smtp",
"table": "named_counter_demo",
"handle": 3,
"packets": 0,
"bytes": 0
}
},
{
"chain": {
"family": "inet",
"table": "named_counter_demo",
"name": "IN",
"handle": 1
}
},
{
"rule": {
"family": "inet",
"table": "named_counter_demo",
"chain": "IN",
"handle": 4,
"expr": [
{
"match": {
"op": "==",
"left": {
"payload": {
"protocol": "tcp",
"field": "dport"
}
},
"right": 21
}
},
{
"counter": {
"packets": 0,
"bytes": 0
}
}
]
}
},
{
"rule": {
"family": "inet",
"table": "named_counter_demo",
"chain": "IN",
"handle": 5,
"expr": [
{
"match": {
"op": "==",
"left": {
"payload": {
"protocol": "tcp",
"field": "dport"
}
},
"right": 25
}
},
{
"counter": "cnt_smtp"
}
]
}
},
{
"rule": {
"family": "inet",
"table": "named_counter_demo",
"chain": "IN",
"handle": 6,
"expr": [
{
"match": {
"op": "==",
"left": {
"payload": {
"protocol": "tcp",
"field": "dport"
}
},
"right": 80
}
},
{
"counter": "cnt_http"
}
]
}
},
{
"rule": {
"family": "inet",
"table": "named_counter_demo",
"chain": "IN",
"handle": 7,
"expr": [
{
"match": {
"op": "==",
"left": {
"payload": {
"protocol": "tcp",
"field": "dport"
}
},
"right": 443
}
},
{
"counter": "cnt_http"
}
]
}
}
]
}
17 changes: 17 additions & 0 deletions resources/test/nft/counter.nft
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
table inet named_counter_demo {
counter cnt_http {
comment "count both http and https packets"
packets 0 bytes 0
}

counter cnt_smtp {
packets 0 bytes 0
}

chain IN {
tcp dport 21 counter
tcp dport 25 counter name "cnt_smtp"
tcp dport 80 counter name "cnt_http"
tcp dport 443 counter name "cnt_http"
}
}
16 changes: 11 additions & 5 deletions src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ pub enum Statement {
Goto(JumpTarget),

Match(Match),
Counter(Option<Counter>),
#[serde(rename = "counter")]
/// reference to a named counter
CounterRef(String),
/// anonymous or named counter.
Counter(Counter),
Mangle(Mangle),
Quota(Quota),
#[serde(rename = "quota")]
Expand Down Expand Up @@ -115,11 +113,19 @@ pub struct Match {
pub op: Operator,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
/// Anonymous or named Counter.
pub enum Counter {
Named(String),
Anonymous(Option<AnonymousCounter>),
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
/// This object represents a byte/packet counter.
/// In input, no properties are required.
/// If given, they act as initial values for the counter.
pub struct Counter {
pub struct AnonymousCounter {
#[serde(skip_serializing_if = "Option::is_none")]
/// Packets counted.
pub packets: Option<usize>,
Expand Down
4 changes: 2 additions & 2 deletions tests/json_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use nftables::expr::{Expression, Meta, MetaKey, NamedExpression};
use nftables::stmt::{Match, Operator, Queue, Statement};
use nftables::stmt::{Counter, Match, Operator, Queue, Statement};
use nftables::{schema::*, types::*};
use serde_json::json;
use std::fs::{self, File};
Expand Down Expand Up @@ -77,7 +77,7 @@ fn test_insert() {
right: Expression::String("wg_exit".to_string()),
op: Operator::EQ,
}),
Statement::Counter(None),
Statement::Counter(Counter::Anonymous(None)),
Statement::Accept(None),
],
handle: None,
Expand Down

0 comments on commit 9f109c5

Please sign in to comment.