Compare commits
3 Commits
fb79e6f97f
...
1646f72f2d
Author | SHA1 | Date |
---|---|---|
|
1646f72f2d | |
|
aa8348f790 | |
|
20edd38e22 |
2
go.mod
2
go.mod
|
@ -12,6 +12,6 @@ require (
|
|||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/josharian/native v1.1.0 // indirect
|
||||
github.com/mdlayher/socket v0.5.0 // indirect
|
||||
golang.org/x/net v0.22.0 // indirect
|
||||
golang.org/x/net v0.23.0 // indirect
|
||||
golang.org/x/sync v0.6.0 // indirect
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -8,8 +8,8 @@ github.com/mdlayher/socket v0.5.0 h1:ilICZmJcQz70vrWVes1MFera4jGiWNocSkykwwoy3XI
|
|||
github.com/mdlayher/socket v0.5.0/go.mod h1:WkcBFfvyG8QENs5+hfQPl1X6Jpd2yeLIYgrGFmJiJxI=
|
||||
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc h1:R83G5ikgLMxrBvLh22JhdfI8K6YXEPHx5P03Uu3DRs4=
|
||||
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI=
|
||||
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
||||
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
|
||||
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package xt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// CommentSize is the fixed size of a comment info xt blob, see:
|
||||
// https://elixir.bootlin.com/linux/v6.8.7/source/include/uapi/linux/netfilter/xt_comment.h#L5
|
||||
const CommentSize = 256
|
||||
|
||||
// Comment gets marshalled and unmarshalled as a fixed-sized char array, filled
|
||||
// with zeros as necessary, see:
|
||||
// https://elixir.bootlin.com/linux/v6.8.7/source/include/uapi/linux/netfilter/xt_comment.h#L7
|
||||
type Comment string
|
||||
|
||||
func (c *Comment) marshal(fam TableFamily, rev uint32) ([]byte, error) {
|
||||
if len(*c) >= CommentSize {
|
||||
return nil, fmt.Errorf("comment must be less than %d bytes, got %d bytes",
|
||||
CommentSize, len(*c))
|
||||
}
|
||||
data := make([]byte, CommentSize)
|
||||
copy(data, []byte(*c))
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (c *Comment) unmarshal(fam TableFamily, rev uint32, data []byte) error {
|
||||
if len(data) != CommentSize {
|
||||
return fmt.Errorf("malformed comment: got %d bytes, expected exactly %d bytes",
|
||||
len(data), CommentSize)
|
||||
}
|
||||
*c = Comment(bytes.TrimRight(data, "\x00"))
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package xt
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestComment(t *testing.T) {
|
||||
t.Parallel()
|
||||
payload := Comment("The quick brown fox jumps over the lazy dog.")
|
||||
oversized := Comment(strings.Repeat("foobar", 100))
|
||||
tests := []struct {
|
||||
name string
|
||||
info InfoAny
|
||||
errmsg string
|
||||
}{
|
||||
{
|
||||
name: "un/marshal Comment round-trip",
|
||||
info: &payload,
|
||||
},
|
||||
{
|
||||
name: "marshal oversized Comment",
|
||||
info: &oversized,
|
||||
errmsg: "comment must be less than 256 bytes, got 600 bytes",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
data, err := tt.info.marshal(0, 0)
|
||||
if err != nil {
|
||||
if tt.errmsg != "" && err.Error() == tt.errmsg {
|
||||
return
|
||||
}
|
||||
t.Fatalf("marshal error: %+v", err)
|
||||
|
||||
}
|
||||
if len(data) != CommentSize {
|
||||
t.Fatalf("marshal error: invalid size %d", len(data))
|
||||
}
|
||||
if data[len(data)-1] != 0 {
|
||||
t.Fatalf("marshal error: invalid termination")
|
||||
}
|
||||
var comment Comment
|
||||
var recoveredInfo InfoAny = &comment
|
||||
err = recoveredInfo.unmarshal(0, 0, data)
|
||||
if err != nil {
|
||||
t.Fatalf("unmarshal error: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(tt.info, recoveredInfo) {
|
||||
t.Fatalf("original %+v and recovered %+v are different", tt.info, recoveredInfo)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
oversizeddata := []byte(oversized)
|
||||
var comment Comment
|
||||
if err := (&comment).unmarshal(0, 0, oversizeddata); err == nil {
|
||||
t.Fatalf("unmarshal: expected error, but got nil")
|
||||
}
|
||||
}
|
|
@ -34,6 +34,9 @@ func Unmarshal(name string, fam TableFamily, rev uint32, data []byte) (InfoAny,
|
|||
case 1:
|
||||
i = &AddrTypeV1{}
|
||||
}
|
||||
case "comment":
|
||||
var c Comment
|
||||
i = &c
|
||||
case "conntrack":
|
||||
switch rev {
|
||||
case 1:
|
||||
|
|
Loading…
Reference in New Issue