Compare commits
No commits in common. "6baf2cbc05140271796e979ba974979103c31aa6" and "919abdc34fbc29b73b218046b653cc124c3cd08b" have entirely different histories.
6baf2cbc05
...
919abdc34f
|
@ -137,14 +137,14 @@ func TestTableCreateDestroy(t *testing.T) {
|
|||
Family: nftables.TableFamilyIPv4,
|
||||
Name: "filter",
|
||||
}
|
||||
c.DestroyTable(filter)
|
||||
c.DelTable(filter, true)
|
||||
c.AddTable(filter)
|
||||
err := c.Flush()
|
||||
if err != nil {
|
||||
t.Fatalf("on Flush: %q", err.Error())
|
||||
}
|
||||
|
||||
lookupMyTable := func() bool {
|
||||
LookupMyTable := func() bool {
|
||||
ts, err := c.ListTables()
|
||||
if err != nil {
|
||||
t.Fatalf("on ListTables: %q", err.Error())
|
||||
|
@ -153,20 +153,21 @@ func TestTableCreateDestroy(t *testing.T) {
|
|||
return t.Name == filter.Name && t.Family == filter.Family
|
||||
})
|
||||
}
|
||||
if !lookupMyTable() {
|
||||
if !LookupMyTable() {
|
||||
t.Fatal("AddTable doesn't create my table!")
|
||||
}
|
||||
|
||||
c.DestroyTable(filter)
|
||||
if err = c.Flush(); err != nil {
|
||||
c.DelTable(filter)
|
||||
err = c.Flush()
|
||||
if err != nil {
|
||||
t.Fatalf("on Flush: %q", err.Error())
|
||||
}
|
||||
|
||||
if lookupMyTable() {
|
||||
t.Fatal("DestroyTable doesn't delete my table!")
|
||||
if LookupMyTable() {
|
||||
t.Fatal("DelTable doesn't delete my table!")
|
||||
}
|
||||
|
||||
c.DestroyTable(filter) // just for test that 'destroy' ignore error 'not found'
|
||||
c.DelTable(filter, true) // just for test that 'force' ignore error 'not found'
|
||||
}
|
||||
|
||||
func TestRuleOperations(t *testing.T) {
|
||||
|
|
18
table.go
18
table.go
|
@ -16,6 +16,7 @@ package nftables
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"github.com/mdlayher/netlink"
|
||||
"golang.org/x/sys/unix"
|
||||
|
@ -54,16 +55,7 @@ type Table struct {
|
|||
}
|
||||
|
||||
// DelTable deletes a specific table, along with all chains/rules it contains.
|
||||
func (cc *Conn) DelTable(t *Table) {
|
||||
cc.delTable(t, delTableHeaderType)
|
||||
}
|
||||
|
||||
// DestroyTable is like DelTable, but not an error if table doesn't exists
|
||||
func (cc *Conn) DestroyTable(t *Table) {
|
||||
cc.delTable(t, destroyTableHeaderType)
|
||||
}
|
||||
|
||||
func (cc *Conn) delTable(t *Table, hdrType netlink.HeaderType) {
|
||||
func (cc *Conn) DelTable(t *Table, force ...bool) {
|
||||
cc.mu.Lock()
|
||||
defer cc.mu.Unlock()
|
||||
data := cc.marshalAttr([]netlink.Attribute{
|
||||
|
@ -71,6 +63,12 @@ func (cc *Conn) delTable(t *Table, hdrType netlink.HeaderType) {
|
|||
{Type: unix.NFTA_TABLE_FLAGS, Data: []byte{0, 0, 0, 0}},
|
||||
})
|
||||
|
||||
var hdrType netlink.HeaderType
|
||||
if slices.Contains(force, true) {
|
||||
hdrType = destroyTableHeaderType
|
||||
} else {
|
||||
hdrType = delTableHeaderType
|
||||
}
|
||||
cc.messages = append(cc.messages, netlinkMessage{
|
||||
Header: netlink.Header{
|
||||
Type: hdrType,
|
||||
|
|
Loading…
Reference in New Issue