Compare commits

...

3 Commits

Author SHA1 Message Date
Paul Greenberg a4241cd73c
Merge dae73eaa9c into 0420ffbf57 2025-02-22 07:35:53 +01:00
Marten Seemann 0420ffbf57
fix unmarshalling of expr.Ct source register (#301) 2025-02-21 09:34:44 +01:00
Paul Greenberg dae73eaa9c rule: add String() method
Before this commit: the printing of a rule results in
a pointer address.

After this commit: the printing of a rules results in
a human-readable text.

Resolves: #104

Signed-off-by: Paul Greenberg <greenpau@outlook.com>
2020-08-03 10:59:40 -04:00
7 changed files with 81 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
nftables.test

View File

@ -21,4 +21,12 @@ the data types/API will be identified as more functionality is added.
Contributions are very welcome! Contributions are very welcome!
### Testing Changes
Run the following commands to test your changes:
```bash
go test ./...
go test -c github.com/google/nftables
sudo ./nftables.test -test.v -run_system_tests
```

View File

@ -186,6 +186,9 @@ func (e *Ct) unmarshal(fam byte, data []byte) error {
e.Register = ad.Uint32() e.Register = ad.Uint32()
case unix.NFTA_CT_DIRECTION: case unix.NFTA_CT_DIRECTION:
e.Direction = ad.Uint32() e.Direction = ad.Uint32()
case unix.NFTA_CT_SREG:
e.SourceRegister = true
e.Register = ad.Uint32()
} }
} }
return ad.Err() return ad.Err()

View File

@ -38,6 +38,14 @@ func TestCt(t *testing.T) {
Direction: 1, // direction: reply Direction: 1, // direction: reply
}, },
}, },
{
name: "Unmarshal Ct source register case",
ct: Ct{
Register: 1,
Key: CtKeySRC,
SourceRegister: true,
},
},
} }
for _, tt := range tests { for _, tt := range tests {

View File

@ -24,6 +24,15 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
const (
NFT_DROP = 0
NFT_ACCEPT = 1
NFT_STOLEN = 2
NFT_QUEUE = 3
NFT_REPEAT = 4
NFT_STOP = 5
)
// This code assembles the verdict structure, as expected by the // This code assembles the verdict structure, as expected by the
// nftables netlink API. // nftables netlink API.
// For further information, consult: // For further information, consult:
@ -129,3 +138,37 @@ func (e *Verdict) unmarshal(fam byte, data []byte) error {
} }
return ad.Err() return ad.Err()
} }
func (e *Verdict) String() string {
var v string
switch e.Kind {
case unix.NFT_RETURN:
v = "return" // -0x5
case unix.NFT_GOTO:
v = "goto" // -0x4
case unix.NFT_JUMP:
v = "jump" // NFT_JUMP = -0x3
case unix.NFT_BREAK:
v = "break" // NFT_BREAK = -0x2
case unix.NFT_CONTINUE:
v = "continue" // NFT_CONTINUE = -0x1
case NFT_DROP:
v = "drop"
case NFT_ACCEPT:
v = "accept"
case NFT_STOLEN:
v = "stolen"
case NFT_QUEUE:
v = "queue"
case NFT_REPEAT:
v = "repeat"
case NFT_STOP:
v = "stop"
default:
v = fmt.Sprintf("verdict %v", e.Kind)
}
if e.Chain != "" {
return v + " " + e.Chain
}
return v
}

View File

@ -222,12 +222,27 @@ func TestRuleOperations(t *testing.T) {
expr.VerdictDrop, expr.VerdictDrop,
} }
wantStrings := []string{
"queue",
"accept",
"queue",
"accept",
"drop",
"drop",
}
for i, r := range rules { for i, r := range rules {
rr, _ := r.Exprs[0].(*expr.Verdict) rr, _ := r.Exprs[0].(*expr.Verdict)
if rr.Kind != want[i] { if rr.Kind != want[i] {
t.Fatalf("bad verdict kind at %d", i) t.Fatalf("bad verdict kind at %d", i)
} }
if rr.String() != wantStrings[i] {
t.Fatalf("bad verdict string at %d: %s (received) vs. %s (expected)", i, rr.String(), wantStrings[i])
}
t.Logf("%s", rr)
} }
} }

3
nftables_test.sh Executable file
View File

@ -0,0 +1,3 @@
go test ./...
go test -c github.com/google/nftables
sudo ./nftables.test -test.v -run_system_tests