-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
216 lines (198 loc) · 4.61 KB
/
table.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// +build linux
package nftlib
import (
"errors"
"github.com/google/nftables"
"net"
)
const (
TableFamilyInet tableFamily = "inet"
TableFamilyIpv4 tableFamily = "ipv4"
TableFamilyIpv6 tableFamily = "ipv6"
TableFamilyBridge tableFamily = "bridge"
)
type tableFamily string
type Table struct {
conn *Conn
Name string `json:"name"`
Family tableFamily `json:"family"`
}
func (d *Table) AddSet(name, dtype string, drange bool, elems ...string) (*Set, error) {
nset, err := d.conn.GetSetByName(d.toNTable(), name)
if err == nil && nset != nil {
return nil, errors.New("named set already exist")
}
var nsrv = nftables.TypeInvalid
if v, ok := dtypeList[dtype]; ok {
nsrv = v
}
if nsrv == nftables.TypeInvalid {
return nil, errors.New("invalid datatype")
}
set := &Set{Name: name, conn: d.conn, Table: d, DType: dtype, Elements: elems, ElemRange: drange}
nset, nelems, err := set.toNSet()
if err != nil {
return nil, err
}
if len(nelems) > 0 && drange {
switch dtype {
case SetDtypeIpv4:
nelems = append([]nftables.SetElement{{Key: make([]byte, net.IPv4len), IntervalEnd: true}}, nelems...)
case SetDtypeIpv6:
nelems = append([]nftables.SetElement{{Key: make([]byte, net.IPv6len), IntervalEnd: true}}, nelems...)
case SetDtypePort:
nelems = append([]nftables.SetElement{{Key: make([]byte, 2), IntervalEnd: true}}, nelems...)
}
}
err = d.conn.AddSet(nset, nelems)
if err != nil {
return nil, err
}
return set, nil
}
func (d *Table) GetSetByName(name string) (*Set, error) {
nset, err := d.conn.GetSetByName(d.toNTable(), name)
if err != nil {
return nil, err
}
nelems, err := d.conn.GetSetElements(nset)
if err != nil {
return nil, err
}
set := &Set{conn: d.conn, Table: d}
err = set.toSet(*nset, nelems...)
if err != nil {
return nil, err
}
return set, nil
}
func (d *Table) DelSet(set *Set) error {
nset, _, err := set.toNSet()
if err != nil {
return err
}
d.conn.DelSet(nset)
return nil
}
func (d *Table) ListSet() ([]*Set, error) {
var setList []*Set
nsets, err := d.conn.GetSets(d.toNTable())
if err != nil {
return nil, err
}
for _, nset := range nsets {
nelems, err := d.conn.GetSetElements(nset)
if err != nil {
return nil, err
}
set := &Set{conn: d.conn, Table: d}
err = set.toSet(*nset, nelems...)
if err != nil {
return nil, err
}
setList = append(setList, set)
}
return setList, nil
}
// ClearSet 清除所有没被使用的集合
func (d *Table) ClearSet() error {
sets, err := d.ListSet()
if err != nil {
return err
}
for _, set := range sets {
nset, _, err := set.toNSet()
if err != nil {
return err
}
d.conn.DelSet(nset)
}
return nil
}
func (d *Table) GetChainByName(name string) (*Chain, error) {
nch, err := d.conn.ListChains()
if err != nil {
return nil, err
}
for _, nc := range nch {
if nc.Name == name && nc.Table.Name == d.Name {
ch := &Chain{Conn: d.conn, Table: d}
ch.toCh(*nc)
return ch, nil
}
}
return nil, errors.New("not found")
}
func (d *Table) ListChain() ([]*Chain, error) {
var chs []*Chain
nchs, err := d.conn.ListChains()
if err != nil {
return nil, err
}
for _, nch := range nchs {
if nch.Table.Name == d.Name {
ch := &Chain{Table: d, Conn: d.conn}
ch.toCh(*nch)
chs = append(chs, ch)
}
}
return chs, nil
}
func (d *Table) AddBaseChain(chain *Chain) *Chain {
chain.Conn = d.conn
chain.Table = d
nch := chain.toNch()
d.conn.AddChain(nch)
return chain
}
func (d *Table) AddRegularChain(name string) (*Chain, error) {
ch := &Chain{
Conn: d.conn,
Table: d,
Name: name,
}
nch := ch.toNch()
d.conn.AddChain(nch)
return ch, nil
}
func (d *Table) Commit() error {
return d.conn.Commit()
}
func (d *Table) toTable(nTable nftables.Table) error {
switch nTable.Family {
case nftables.TableFamilyINet:
d.Name = nTable.Name
d.Family = TableFamilyInet
case nftables.TableFamilyIPv4:
d.Name = nTable.Name
d.Family = TableFamilyIpv4
case nftables.TableFamilyIPv6:
d.Name = nTable.Name
d.Family = TableFamilyIpv6
case nftables.TableFamilyBridge:
d.Name = nTable.Name
d.Family = TableFamilyBridge
}
if d.Name == "" {
return errors.New("toTable failed: nil table name")
}
if d.Family == "" {
return errors.New("toTable failed: nil table family")
}
return nil
}
func (d *Table) toNTable() *nftables.Table {
var ntbl = new(nftables.Table)
ntbl.Name = d.Name
switch d.Family {
case TableFamilyInet:
ntbl.Family = nftables.TableFamilyINet
case TableFamilyIpv4:
ntbl.Family = nftables.TableFamilyIPv4
case TableFamilyIpv6:
ntbl.Family = nftables.TableFamilyIPv6
case TableFamilyBridge:
ntbl.Family = nftables.TableFamilyBridge
}
return ntbl
}