From 38e481bfc443ea24b55d9a4664e71bf7cf2379e5 Mon Sep 17 00:00:00 2001 From: turekt <32360115+turekt@users.noreply.github.com> Date: Wed, 14 Aug 2024 08:10:09 +0200 Subject: [PATCH] Added GetNamedObjects and ResetNamedObjects (#267) --- nftables_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ obj.go | 12 ++++++++ 2 files changed, 89 insertions(+) diff --git a/nftables_test.go b/nftables_test.go index dca28a1..cc16504 100644 --- a/nftables_test.go +++ b/nftables_test.go @@ -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() diff --git a/obj.go b/obj.go index 9331481..84ce00f 100644 --- a/obj.go +++ b/obj.go @@ -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 @@ -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)