From 1f0380f5c76eb92b9bc92aa1d20585e6871ae4b3 Mon Sep 17 00:00:00 2001 From: TheDiveO <6920158+thediveo@users.noreply.github.com> Date: Tue, 7 Jun 2022 17:23:05 +0200 Subject: [PATCH] list tables and chains optionally by specific table family (#168) --- chain.go | 9 ++++++++- table.go | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/chain.go b/chain.go index f98fbd5..39b192c 100644 --- a/chain.go +++ b/chain.go @@ -173,6 +173,13 @@ func (cc *Conn) FlushChain(c *Chain) { // ListChains returns currently configured chains in the kernel func (cc *Conn) ListChains() ([]*Chain, error) { + return cc.ListChainsOfTableFamily(TableFamilyUnspecified) +} + +// ListChainsOfTableFamily returns currently configured chains for the specified +// family in the kernel. It lists all chains ins all tables if family is +// TableFamilyUnspecified. +func (cc *Conn) ListChainsOfTableFamily(family TableFamily) ([]*Chain, error) { conn, closer, err := cc.netlinkConn() if err != nil { return nil, err @@ -184,7 +191,7 @@ func (cc *Conn) ListChains() ([]*Chain, error) { Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_GETCHAIN), Flags: netlink.Request | netlink.Dump, }, - Data: extraHeader(uint8(TableFamilyUnspecified), 0), + Data: extraHeader(uint8(family), 0), } response, err := conn.Execute(msg) diff --git a/table.go b/table.go index 8019117..bf76df3 100644 --- a/table.go +++ b/table.go @@ -101,6 +101,12 @@ func (cc *Conn) FlushTable(t *Table) { // ListTables returns currently configured tables in the kernel func (cc *Conn) ListTables() ([]*Table, error) { + return cc.ListTablesOfFamily(TableFamilyUnspecified) +} + +// ListTables returns currently configured tables for the specified table family +// in the kernel. It lists all tables if family is TableFamilyUnspecified. +func (cc *Conn) ListTablesOfFamily(family TableFamily) ([]*Table, error) { conn, closer, err := cc.netlinkConn() if err != nil { return nil, err @@ -112,7 +118,7 @@ func (cc *Conn) ListTables() ([]*Table, error) { Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_GETTABLE), Flags: netlink.Request | netlink.Dump, }, - Data: extraHeader(uint8(TableFamilyUnspecified), 0), + Data: extraHeader(uint8(family), 0), } response, err := conn.Execute(msg)