-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
87 lines (77 loc) · 1.43 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// +build linux
package nftlib
import (
"errors"
"github.com/google/nftables"
"github.com/vishvananda/netns"
)
func New(namespace ...string) (*Conn, error) {
var (
nsname string
ns netns.NsHandle
err error
)
if len(namespace) > 0 {
nsname = namespace[0]
}
if nsname != "" {
if ns, err = netns.GetFromName(nsname); err != nil {
return nil, err
}
} else {
ns, err = netns.Get()
}
return &Conn{Conn: &nftables.Conn{NetNS: int(ns)}}, nil
}
type Conn struct {
*nftables.Conn
}
func (d *Conn) ADDTable(table *Table) *Table {
table.conn = d
ntbl := table.toNTable()
d.AddTable(ntbl)
return table
}
func (d *Conn) ShowTables() ([]*Table, error) {
var tbl []*Table
ntbl, err := d.ListTables()
if err != nil {
return nil, err
}
for _, ntb := range ntbl {
t := &Table{conn: d}
err = t.toTable(*ntb)
if err != nil {
return nil, err
}
tbl = append(tbl, t)
}
return tbl, nil
}
func (d *Conn) GetTableByName(tableName string) (*Table, error) {
ntbl, err := d.Conn.ListTables()
if err != nil {
return nil, err
}
for _, ntb := range ntbl {
if ntb.Name == tableName {
t := &Table{conn: d}
err = t.toTable(*ntb)
if err != nil {
return nil, err
}
return t, nil
}
}
return nil, errors.New("not found")
}
func (d *Conn) ClearAll() {
d.FlushRuleset()
}
func (d *Conn) Commit() error {
err := d.Flush()
return err
}
func (d *Conn) Discard() {
d.Conn = &nftables.Conn{NetNS: d.NetNS}
}