-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement AddGenerationalMonitor to deliver monitor events in batches (…
- Loading branch information
Showing
3 changed files
with
180 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package nftables | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"github.com/mdlayher/netlink" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
type GenMsg struct { | ||
ID uint32 | ||
ProcPID uint32 | ||
ProcComm string // [16]byte - max 16bytes - kernel TASK_COMM_LEN | ||
} | ||
|
||
var genHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWGEN) | ||
|
||
func genFromMsg(msg netlink.Message) (*GenMsg, error) { | ||
if got, want := msg.Header.Type, genHeaderType; got != want { | ||
return nil, fmt.Errorf("unexpected header type: got %v, want %v", got, want) | ||
} | ||
ad, err := netlink.NewAttributeDecoder(msg.Data[4:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ad.ByteOrder = binary.BigEndian | ||
|
||
msgOut := &GenMsg{} | ||
for ad.Next() { | ||
switch ad.Type() { | ||
case unix.NFTA_GEN_ID: | ||
msgOut.ID = ad.Uint32() | ||
case unix.NFTA_GEN_PROC_PID: | ||
msgOut.ProcPID = ad.Uint32() | ||
case unix.NFTA_GEN_PROC_NAME: | ||
msgOut.ProcComm = ad.String() | ||
default: | ||
return nil, fmt.Errorf("Unknown attribute: %d %v\n", ad.Type(), ad.Bytes()) | ||
} | ||
} | ||
if err := ad.Err(); err != nil { | ||
return nil, err | ||
} | ||
return msgOut, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters