-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipset.go
242 lines (208 loc) · 3.95 KB
/
ipset.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package iputils
import (
"errors"
"net"
"unsafe"
)
// Radix tree node
type node struct {
left *node
right *node
parent *node
value interface{}
}
type IPSet struct {
root *node
free *node
pool []node
}
const (
START_BYTE = byte(0x80)
)
var (
ErrNodeBusy = errors.New("node is busy")
ErrNotFound = errors.New("no such node")
ErrBadIP = errors.New("bad IP address or mask")
MASK_32 = net.CIDRMask(8*net.IPv4len, 8*net.IPv4len)
MASK_128 = net.CIDRMask(8*net.IPv6len, 8*net.IPv6len)
ALLOC_LEN = 4096 / unsafe.Sizeof(node{})
)
func NewSet() *IPSet {
set := &IPSet{}
set.root = set.newNode()
return set
}
func (s *IPSet) insert(key net.IP, mask net.IPMask, value interface{}, overwrite bool) error {
if len(key) != len(mask) {
return ErrBadIP
}
i := 0
bitmap := START_BYTE
node := s.root
next := s.root
for bitmap&mask[i] != 0 {
if key[i]&bitmap != 0 {
next = node.right
} else {
next = node.left
}
if next == nil {
break
}
node = next
if bitmap >>= 1; bitmap == 0 {
if i++; i == len(key) {
break
}
bitmap = START_BYTE
}
}
if next != nil {
if node.value != nil && !overwrite {
return ErrNodeBusy
}
node.value = value
return nil
}
for bitmap&mask[i] != 0 {
next = s.newNode()
next.parent = node
if key[i]&bitmap != 0 {
node.right = next
} else {
node.left = next
}
node = next
if bitmap >>= 1; bitmap == 0 {
if i++; i == len(key) {
break
}
bitmap = START_BYTE
}
}
node.value = value
return nil
}
func (s *IPSet) delete(key net.IP, mask net.IPMask, sub bool) error {
if len(key) != len(mask) {
return ErrBadIP
}
i := 0
bitmap := START_BYTE
node := s.root
for node != nil && bitmap&mask[i] != 0 {
if key[i]&bitmap != 0 {
node = node.right
} else {
node = node.left
}
if bitmap >>= 1; bitmap == 0 {
if i++; i == len(key) {
break
}
bitmap = START_BYTE
}
}
if node == nil {
return ErrNotFound
}
if !sub && (node.right != nil || node.left != nil) {
// trim value
if node.value != nil {
node.value = nil
return nil
}
return ErrNotFound
}
// trim leaf
for {
if node.parent.right == node {
node.parent.right = nil
} else {
node.parent.left = nil
}
// free
node.right = s.free
s.free = node
// move to parent
node = node.parent
if node.right != nil || node.left != nil || node.value != nil {
break
}
if node.parent == nil {
break
}
}
return nil
}
func (s *IPSet) find(key net.IP, mask net.IPMask) (value interface{}, err error) {
if len(key) != len(mask) {
return nil, ErrBadIP
}
i := 0
bitmap := START_BYTE
node := s.root
for node != nil {
if node.value != nil {
value = node.value
}
if key[i]&bitmap != 0 {
node = node.right
} else {
node = node.left
}
if mask[i]&bitmap == 0 {
break
}
if bitmap >>= 1; bitmap == 0 {
i, bitmap = i+1, START_BYTE
if i >= len(key) {
if node != nil {
value = node.value
}
break
}
}
}
return value, nil
}
func (s *IPSet) newNode() (p *node) {
if s.free != nil {
p = s.free
s.free = s.free.right
p.right = nil
p.parent = nil
p.left = nil
p.value = nil
return p
}
size := len(s.pool)
if size == cap(s.pool) {
s.pool = make([]node, ALLOC_LEN)[:1]
size = 0
} else {
s.pool = s.pool[:size+1]
}
return &(s.pool[size])
}
func (s *IPSet) Add(cidr *net.IPNet, val interface{}) error {
return s.insert(cidr.IP, cidr.Mask, val, false)
}
func (s *IPSet) Set(cidr *net.IPNet, val interface{}) error {
return s.insert(cidr.IP, cidr.Mask, val, true)
}
func (s *IPSet) Sub(cidr *net.IPNet) error {
return s.delete(cidr.IP, cidr.Mask, true)
}
func (s *IPSet) Remove(cidr *net.IPNet) error {
return s.delete(cidr.IP, cidr.Mask, false)
}
func (s *IPSet) Get(cidr *net.IPNet) (interface{}, error) {
return s.find(cidr.IP, cidr.Mask)
}
func (s *IPSet) GetByIP(ip net.IP) (interface{}, error) {
if len(ip) == net.IPv4len {
return s.find(ip, MASK_32)
}
return s.find(ip, MASK_128)
}