list tables and chains optionally by specific table family (#168)

This commit is contained in:
TheDiveO 2022-06-07 17:23:05 +02:00 committed by GitHub
parent a9775fb167
commit 1f0380f5c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -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)

View File

@ -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)