Skip to content

Commit

Permalink
fix table monitor definition and bump go version to 1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
singchia committed Dec 10, 2023
1 parent 352d701 commit 71d4913
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/google/nftables

go 1.17
go 1.18

require (
github.com/mdlayher/netlink v1.7.1
Expand Down
24 changes: 20 additions & 4 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package nftables

import (
"math"
"strings"
"sync"

"github.com/mdlayher/netlink"
Expand Down Expand Up @@ -49,7 +51,7 @@ var (
monitorFlags = map[MonitorAction]map[MonitorObject]uint32{
MonitorActionAny: {
MonitorObjectAny: 0xffffffff,
MonitorObjectTables: 1<<unix.NFT_MSG_NEWTABLE | 1<<unix.NFT_MSG_DELCHAIN,
MonitorObjectTables: 1<<unix.NFT_MSG_NEWTABLE | 1<<unix.NFT_MSG_DELTABLE,
MonitorObjectChains: 1<<unix.NFT_MSG_NEWCHAIN | 1<<unix.NFT_MSG_DELCHAIN,
MonitorObjectRules: 1<<unix.NFT_MSG_NEWRULE | 1<<unix.NFT_MSG_DELRULE,
MonitorObjectSets: 1<<unix.NFT_MSG_NEWSET | 1<<unix.NFT_MSG_DELSET,
Expand Down Expand Up @@ -105,11 +107,12 @@ const (
EventTypeDelSetElem EventType = unix.NFT_MSG_DELSETELEM
EventTypeNewObj EventType = unix.NFT_MSG_NEWOBJ
EventTypeDelObj EventType = unix.NFT_MSG_DELOBJ
EventTypeOOB EventType = math.MaxInt
)

type Event struct {
Type EventType
Data interface{}
Data any
Error error
}

Expand Down Expand Up @@ -182,7 +185,19 @@ func (monitor *Monitor) monitor() {
for {
msgs, err := monitor.conn.Receive()
if err != nil {
break
if strings.Contains(err.Error(), "use of closed file") {
// ignore the error that be closed
break
} else {
// any other errors will be send to user, and then to close eventCh
event := &Event{
Type: EventTypeOOB,
Data: nil,
Error: err,
}
monitor.eventCh <- event
break
}
}
for _, msg := range msgs {
if msg.Header.Type&0xff00>>8 != netlink.HeaderType(unix.NFNL_SUBSYS_NFTABLES) {
Expand Down Expand Up @@ -256,12 +271,13 @@ func (monitor *Monitor) monitor() {

func (monitor *Monitor) Close() {
monitor.mu.Lock()
defer monitor.mu.Unlock()

if monitor.status != monitorClosed {
monitor.status = monitorClosed
monitor.closer()
close(monitor.eventCh)
}
monitor.mu.Unlock()
}

// AddMonitor to perform the monitor immediately. The channel will be closed after
Expand Down

0 comments on commit 71d4913

Please sign in to comment.