Compare commits
1 Commits
0978693277
...
1e26887507
Author | SHA1 | Date |
---|---|---|
|
1e26887507 |
|
@ -137,14 +137,14 @@ func TestTableCreateDestroy(t *testing.T) {
|
||||||
Family: nftables.TableFamilyIPv4,
|
Family: nftables.TableFamilyIPv4,
|
||||||
Name: "filter",
|
Name: "filter",
|
||||||
}
|
}
|
||||||
c.DestroyTable(filter)
|
c.DelTable(filter, true)
|
||||||
c.AddTable(filter)
|
c.AddTable(filter)
|
||||||
err := c.Flush()
|
err := c.Flush()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("on Flush: %q", err.Error())
|
t.Fatalf("on Flush: %q", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
lookupMyTable := func() bool {
|
LookupMyTable := func() bool {
|
||||||
ts, err := c.ListTables()
|
ts, err := c.ListTables()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("on ListTables: %q", err.Error())
|
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
|
return t.Name == filter.Name && t.Family == filter.Family
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if !lookupMyTable() {
|
if !LookupMyTable() {
|
||||||
t.Fatal("AddTable doesn't create my table!")
|
t.Fatal("AddTable doesn't create my table!")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.DestroyTable(filter)
|
c.DelTable(filter)
|
||||||
if err = c.Flush(); err != nil {
|
err = c.Flush()
|
||||||
|
if err != nil {
|
||||||
t.Fatalf("on Flush: %q", err.Error())
|
t.Fatalf("on Flush: %q", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if lookupMyTable() {
|
if LookupMyTable() {
|
||||||
t.Fatal("DestroyTable doesn't delete my table!")
|
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) {
|
func TestRuleOperations(t *testing.T) {
|
||||||
|
|
18
table.go
18
table.go
|
@ -16,6 +16,7 @@ package nftables
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/mdlayher/netlink"
|
"github.com/mdlayher/netlink"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
@ -54,16 +55,7 @@ type Table struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelTable deletes a specific table, along with all chains/rules it contains.
|
// DelTable deletes a specific table, along with all chains/rules it contains.
|
||||||
func (cc *Conn) DelTable(t *Table) {
|
func (cc *Conn) DelTable(t *Table, force ...bool) {
|
||||||
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) {
|
|
||||||
cc.mu.Lock()
|
cc.mu.Lock()
|
||||||
defer cc.mu.Unlock()
|
defer cc.mu.Unlock()
|
||||||
data := cc.marshalAttr([]netlink.Attribute{
|
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}},
|
{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{
|
cc.messages = append(cc.messages, netlinkMessage{
|
||||||
Header: netlink.Header{
|
Header: netlink.Header{
|
||||||
Type: hdrType,
|
Type: hdrType,
|
||||||
|
|
Loading…
Reference in New Issue