Skip to content

Commit

Permalink
counters may be absent
Browse files Browse the repository at this point in the history
  • Loading branch information
MEschenbacher committed Mar 29, 2024
1 parent 275b7c9 commit e7d3fc0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
17 changes: 11 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func init() {
}

var (
regDefault *regexp.Regexp = regexp.MustCompile(`^\s*(\S+)\s+(\S+)\s+(\[\d*\:\d*\])\s*$`)
regDefault *regexp.Regexp = regexp.MustCompile(`^\s*(\S+)\s+(\S+)(?:\s+(\[\d*\:\d*\]))?\s*$`)
regCounter *regexp.Regexp = regexp.MustCompile(`^\[(\d*)\:(\d*)\]$`)
)

Expand All @@ -422,12 +422,17 @@ func (p *Parser) parseDefault(lit string) (Line, error) {
a := regDefault.ReplaceAll([]byte(lit), []byte("$2"))
r.Action = string(a)
cs := regDefault.ReplaceAll([]byte(lit), []byte("$3"))
c, err := parseCounter(cs)
if err != nil {
return nil, err
if string(cs) == "" {
// nothing has changed
// iptables-restore allows the counter to not exist
r.Counter = &Counter{}
} else {
c, err := parseCounter(cs)
if err != nil {
return nil, err
}
r.Counter = &c
}

r.Counter = &c
return r, nil
}

Expand Down
13 changes: 13 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,19 @@ func TestParser_Parse(t *testing.T) {
},
err: nil,
},
{
name: "parse default rule without counter",
s: ":hello-chain DROP",
r: Policy{
Chain: "hello-chain",
Action: "DROP",
Counter: &Counter{
packets: 0,
bytes: 0,
},
},
err: nil,
},
{
name: "parse policy",
s: "-P hello-chain DROP",
Expand Down

0 comments on commit e7d3fc0

Please sign in to comment.