Skip to content

Commit

Permalink
feat: add xt.Comment
Browse files Browse the repository at this point in the history
Signed-off-by: thediveo <thediveo@gmx.eu>
  • Loading branch information
thediveo committed Apr 17, 2024
1 parent 5e242ec commit 41a89e5
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
31 changes: 31 additions & 0 deletions xt/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package xt

import (
"bytes"
"fmt"
)

// CommentSize is the fixed size of a comment info xt blob.
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", CommentSize)
}
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("Comment takes exactly %d bytes", CommentSize)
}
*c = Comment(bytes.TrimRight(data, "\x00"))
return nil
}
64 changes: 64 additions & 0 deletions xt/comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package xt

import (
"fmt"
"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",
},
}

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 || err.Error() != fmt.Sprintf("Comment takes exactly %d bytes", CommentSize) {
t.Fatalf("unmarshal: expected error: %v", err)

}
}
3 changes: 3 additions & 0 deletions xt/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 41a89e5

Please sign in to comment.