Skip to content

Commit

Permalink
Added GetNamedObjects and ResetNamedObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
turekt committed Aug 13, 2024
1 parent 4d451ef commit cd0b6d6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
77 changes: 77 additions & 0 deletions nftables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,83 @@ func TestGetObjReset(t *testing.T) {
}
}

func TestGetResetNamedObj(t *testing.T) {
c, newNS := nftest.OpenSystemConn(t, *enableSysTests)
defer nftest.CleanupSystemConn(t, newNS)
c.FlushRuleset()
defer c.FlushRuleset()

table := c.AddTable(&nftables.Table{
Family: nftables.TableFamilyIPv4,
Name: "filter",
})

c.AddObj(&nftables.NamedObj{
Table: table,
Name: "fwded1",
Type: nftables.ObjTypeCounter,
Obj: &expr.Counter{
Bytes: 1,
Packets: 1,
},
})

c.AddObj(&nftables.NamedObj{
Table: table,
Name: "fwded2",
Type: nftables.ObjTypeQuota,
Obj: &expr.Quota{
Consumed: 1,
Over: true,
Bytes: 0x6400,
},
})

c.AddObj(&nftables.NamedObj{
Table: table,
Name: "fwded3",
Type: nftables.ObjTypeConnLimit,
Obj: &expr.Connlimit{
Count: 20,
Flags: 1,
},
})

if err := c.Flush(); err != nil {
t.Fatalf(err.Error())
}

objsNamed, err := c.GetNamedObjects(table)
if err != nil {
t.Errorf("c.GetNamedObjects(table) failed: %v failed", err)
}

if got := len(objsNamed); got != 3 {
t.Fatalf("unexpected number of objects: got %d, want %d", got, 3)
}

for _, o := range objsNamed {
switch v := o.(type) {
case *nftables.NamedObj:
default:
t.Fatalf("unexpected type in objsNamed: got %v, want *nftables.NamedObj", v)
}
}

objsReset, err := c.ResetNamedObjects(table)
if err != nil {
t.Errorf("c.ResetObjects(table) failed: %v failed", err)
}

for _, o := range objsReset {
switch v := o.(type) {
case *nftables.NamedObj:
default:
t.Fatalf("unexpected type in objsReset: got %v, want *nftables.NamedObj", v)
}
}
}

func TestObjAPI(t *testing.T) {
if os.Getenv("TRAVIS") == "true" {
t.SkipNow()
Expand Down
12 changes: 12 additions & 0 deletions obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ func (cc *Conn) GetObjects(t *Table) ([]Obj, error) {
return cc.getObj(nil, t, unix.NFT_MSG_GETOBJ)
}

// GetNamedObjects get all the Obj that belongs to the given table
// This function always return NamedObj types
func (cc *Conn) GetNamedObjects(t *Table) ([]Obj, error) {
return cc.getObjWithLegacyType(nil, t, unix.NFT_MSG_GETOBJ, false)
}

// ResetObject reset the given Obj
// This function returns the same concrete type as passed,
// e.g. QuotaObj, CounterObj or NamedObj. Prefer using the more
Expand All @@ -215,6 +221,12 @@ func (cc *Conn) ResetObjects(t *Table) ([]Obj, error) {
return cc.getObj(nil, t, unix.NFT_MSG_GETOBJ_RESET)
}

// ResetNamedObjects reset all the Obj that belongs to the given table
// This function always return NamedObj types
func (cc *Conn) ResetNamedObjects(t *Table) ([]Obj, error) {
return cc.getObjWithLegacyType(nil, t, unix.NFT_MSG_GETOBJ_RESET, false)
}

func objFromMsg(msg netlink.Message, returnLegacyType bool) (Obj, error) {
if got, want1, want2 := msg.Header.Type, newObjHeaderType, delObjHeaderType; got != want1 && got != want2 {
return nil, fmt.Errorf("unexpected header type: got %v, want %v or %v", got, want1, want2)
Expand Down

0 comments on commit cd0b6d6

Please sign in to comment.