add FlushRuleset

This commit is contained in:
Michael Stapelberg 2018-06-14 08:26:52 +02:00
parent 4de6a4e001
commit 13709ef9a6
2 changed files with 16 additions and 0 deletions

View File

@ -231,6 +231,18 @@ type Conn struct {
err error
}
// FlushRuleset flushes the entire ruleset. See also
// https://wiki.nftables.org/wiki-nftables/index.php/Operations_at_ruleset_level
func (cc *Conn) FlushRuleset() {
cc.messages = append(cc.messages, netlink.Message{
Header: netlink.Header{
Type: netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_DELTABLE),
Flags: netlink.HeaderFlagsRequest | netlink.HeaderFlagsAcknowledge | netlink.HeaderFlagsCreate,
},
Data: extraHeader(0, 0),
})
}
// AddTable adds the specified Table. See also
// https://wiki.nftables.org/wiki-nftables/index.php/Configuring_tables
func (cc *Conn) AddTable(t *Table) *Table {

View File

@ -41,6 +41,8 @@ func TestConfigureNAT(t *testing.T) {
want := [][]byte{
// batch begin
[]byte("\x00\x00\x0a\x00"),
// nft flush ruleset
[]byte("\x00\x00\x00\x00"),
// nft add table ip nat
[]byte("\x02\x00\x00\x00\x08\x00\x01\x00\x6e\x61\x74\x00\x08\x00\x02\x00\x00\x00\x00\x00"),
// nft add chain nat prerouting '{' type nat hook prerouting priority 0 \; '}'
@ -79,6 +81,8 @@ func TestConfigureNAT(t *testing.T) {
},
}
c.FlushRuleset()
nat := c.AddTable(&nftables.Table{
Family: nftables.TableFamilyIPv4,
Name: "nat",