Skip to content

Commit

Permalink
Objects implementation refactor
Browse files Browse the repository at this point in the history
Refactored obj.go to a more generic approach
Added object support for already implemented expressions
Added test for limit object
Fixes #253
  • Loading branch information
turekt committed Jul 28, 2024
1 parent 912dee6 commit 955947b
Show file tree
Hide file tree
Showing 5 changed files with 715 additions and 129 deletions.
36 changes: 15 additions & 21 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
package nftables

import (
"github.com/google/nftables/binaryutil"
"github.com/google/nftables/expr"
"github.com/mdlayher/netlink"
"golang.org/x/sys/unix"
)

// CounterObj implements Obj.
type CounterObj struct {
Table *Table
Name string // e.g. “fwded”
Expand All @@ -41,29 +40,24 @@ func (c *CounterObj) unmarshal(ad *netlink.AttributeDecoder) error {
return ad.Err()
}

func (c *CounterObj) data() expr.Any {
return &expr.Counter{
Bytes: c.Bytes,
Packets: c.Packets,
}
}

func (c *CounterObj) name() string {
return c.Name
}
func (c *CounterObj) objType() ObjType {
return ObjTypeCounter
}

func (c *CounterObj) table() *Table {
return c.Table
}

func (c *CounterObj) family() TableFamily {
return c.Table.Family
}

func (c *CounterObj) marshal(data bool) ([]byte, error) {
obj, err := netlink.MarshalAttributes([]netlink.Attribute{
{Type: unix.NFTA_COUNTER_BYTES, Data: binaryutil.BigEndian.PutUint64(c.Bytes)},
{Type: unix.NFTA_COUNTER_PACKETS, Data: binaryutil.BigEndian.PutUint64(c.Packets)},
})
if err != nil {
return nil, err
}
attrs := []netlink.Attribute{
{Type: unix.NFTA_OBJ_TABLE, Data: []byte(c.Table.Name + "\x00")},
{Type: unix.NFTA_OBJ_NAME, Data: []byte(c.Name + "\x00")},
{Type: unix.NFTA_OBJ_TYPE, Data: binaryutil.BigEndian.PutUint32(unix.NFT_OBJECT_COUNTER)},
}
if data {
attrs = append(attrs, netlink.Attribute{Type: unix.NLA_F_NESTED | unix.NFTA_OBJ_DATA, Data: obj})
}
return netlink.MarshalAttributes(attrs)
}
2 changes: 1 addition & 1 deletion monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (monitor *Monitor) monitor() {
}
monitor.eventCh <- event
case unix.NFT_MSG_NEWOBJ, unix.NFT_MSG_DELOBJ:
obj, err := objFromMsg(msg)
obj, err := objFromMsg(msg, true)
event := &MonitorEvent{
Type: MonitorEventType(msgType),
Data: obj,
Expand Down
Loading

0 comments on commit 955947b

Please sign in to comment.