Compare commits

..

2 Commits

Author SHA1 Message Date
Antonio Ojea c43b47f4ec
Merge 1e48c1007e into 4195a123ff 2025-09-19 16:38:36 -07:00
Nikita Vorontsov 4195a123ff
add DataInterval flag for maps, fix comments (#327) 2025-09-19 16:36:24 +02:00
2 changed files with 39 additions and 13 deletions

17
set.go
View File

@ -253,6 +253,7 @@ type Set struct {
Anonymous bool
Constant bool
Interval bool
DataInterval bool
AutoMerge bool
IsMap bool
HasTimeout bool
@ -674,6 +675,10 @@ func (cc *Conn) AddSet(s *Set, vals []SetElement) error {
userData = userdata.AppendUint32(userData, userdata.NFTNL_UDATA_SET_MERGE_ELEMENTS, 1)
}
if s.DataInterval {
userData = userdata.AppendUint32(userData, userdata.NFTNL_UDATA_SET_DATA_INTERVAL, 1)
}
if len(s.Comment) != 0 {
userData = userdata.AppendString(userData, userdata.NFTNL_UDATA_SET_COMMENT, s.Comment)
}
@ -797,8 +802,16 @@ func setsFromMsg(msg netlink.Message) (*Set, error) {
set.DataType.Bytes = binary.BigEndian.Uint32(ad.Bytes())
case unix.NFTA_SET_USERDATA:
data := ad.Bytes()
value, ok := userdata.GetUint32(data, userdata.NFTNL_UDATA_SET_MERGE_ELEMENTS)
set.AutoMerge = ok && value == 1
if val, ok := userdata.GetString(data, userdata.NFTNL_UDATA_SET_COMMENT); ok {
set.Comment = val
}
if val, ok := userdata.GetUint32(data, userdata.NFTNL_UDATA_SET_MERGE_ELEMENTS); ok {
set.AutoMerge = val == 1
}
if val, ok := userdata.GetUint32(data, userdata.NFTNL_UDATA_SET_DATA_INTERVAL); ok {
set.DataInterval = val == 1
}
case unix.NFTA_SET_DESC:
nestedAD, err := netlink.NewAttributeDecoder(ad.Bytes())
if err != nil {

View File

@ -257,13 +257,26 @@ func TestMarshalSet(t *testing.T) {
name: "Vedict map",
set: Set{
Name: "test-map",
ID: uint32(3),
ID: uint32(4),
Table: tbl,
KeyType: TypeIPAddr,
DataType: TypeVerdict,
IsMap: true,
},
},
{
name: "Map ip-ip", // generic case
set: Set{
Name: "test-map",
ID: uint32(5),
Table: tbl,
KeyType: TypeIPAddr,
DataType: TypeIPAddr,
DataInterval: true,
IsMap: true,
Comment: "test-comment",
},
},
}
for i, tt := range tests {