forked from IBM/netaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iptree.go
315 lines (278 loc) · 6.75 KB
/
iptree.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package netaddr
import (
"bytes"
"errors"
"math/big"
"net"
)
type ipTree struct {
net *net.IPNet
left, right, up *ipTree
}
// setLeft helps maintain the bidirectional relationships in the tree. Always
// use it to set the left child of a node.
func (t *ipTree) setLeft(child *ipTree) {
if t.left != nil && t == t.left.up {
t.left.up = nil
}
t.left = child
if child != nil {
child.up = t
}
}
// setRight helps maintain the bidirectional relationships in the tree. Always
// use it to set the right child of a node.
func (t *ipTree) setRight(child *ipTree) {
if t.right != nil && t == t.right.up {
t.right.up = nil
}
t.right = child
if child != nil {
child.up = t
}
}
// trimLeft trims CIDRs that overlap top from the left child
func (t *ipTree) trimLeft(top *ipTree) *ipTree {
if t == nil {
return nil
}
if ContainsNet(top.net, t.net) {
return t.left.trimLeft(top)
}
t.setRight(t.right.trimLeft(top))
return t
}
// trimRight trims CIDRs that overlap top from the right child
func (t *ipTree) trimRight(top *ipTree) *ipTree {
if t == nil {
return nil
}
if ContainsNet(top.net, t.net) {
return t.right.trimRight(top)
}
t.setLeft(t.left.trimRight(top))
return t
}
// insert adds the given node to the tree if its CIDR is not already in the
// set. The new node's CIDR is added in the correct spot and any existing
// subsets are removed from the tree. This method does not optimize the tree by
// adding CIDRs that can be combined.
func (t *ipTree) insert(newNode *ipTree) *ipTree {
if t == nil {
return newNode
}
if ContainsNet(t.net, newNode.net) {
return t
}
if ContainsNet(newNode.net, t.net) {
// Replace the current top node and trim the tree
newNode.setLeft(t.left.trimLeft(newNode))
newNode.setRight(t.right.trimRight(newNode))
// Check the left-most leaf to see if it can be combined with this one
return newNode
}
if bytes.Compare(newNode.net.IP, t.net.IP) < 0 {
t.setLeft(t.left.insert(newNode))
} else {
t.setRight(t.right.insert(newNode))
}
return t
}
// contains returns true if the given IP is in the set.
func (t *ipTree) contains(newNode *ipTree) bool {
if t == nil || newNode == nil {
return false
}
if ContainsNet(t.net, newNode.net) {
return true
}
if ContainsNet(newNode.net, t.net) {
return false
}
if bytes.Compare(newNode.net.IP, t.net.IP) < 0 {
return t.left.contains(newNode)
}
return t.right.contains(newNode)
}
// remove takes out the node and adjusts the tree recursively
func (t *ipTree) remove() *ipTree {
replaceMe := func(newChild *ipTree) *ipTree {
if t.up != nil {
if t == t.up.left {
t.up.setLeft(newChild)
} else {
t.up.setRight(newChild)
}
} else if newChild != nil {
newChild.up = t.up
}
return newChild
}
if t.left != nil && t.right != nil {
next := t.next()
t.net = next.net
next.remove()
return t
}
if t.left != nil {
return replaceMe(t.left)
}
if t.right != nil {
return replaceMe(t.right)
}
return replaceMe(nil)
}
// removeNet removes all of the IPs in the given net from the set
func (t *ipTree) removeNet(net *net.IPNet) (top *ipTree) {
if t == nil {
return
}
// If net starts before me.net, recursively remove net from the left
if bytes.Compare(net.IP, t.net.IP) < 0 {
t.left = t.left.removeNet(net)
}
// If any CIDRs in `net - me.net` come after me.net, remove net from
// the right
diff := netDifference(net, t.net)
for _, n := range diff {
if bytes.Compare(t.net.IP, n.IP) < 0 {
t.right = t.right.removeNet(net)
break
}
}
top = t
if ContainsNet(net, t.net) {
// Remove the current node
top = t.remove()
} else if ContainsNet(t.net, net) {
diff = netDifference(t.net, net)
t.net = diff[0]
for _, n := range diff[1:] {
top = top.insert(&ipTree{net: n})
}
}
return
}
// first returns the first node in the tree or nil if there are none. It is
// always the left-most node.
func (t *ipTree) first() *ipTree {
if t == nil {
return nil
}
if t.left == nil {
return t
}
return t.left.first()
}
// next returns the node following the given one in order or nil if it is the last.
func (t *ipTree) next() *ipTree {
if t.right != nil {
next := t.right
for next.left != nil {
next = next.left
}
return next
}
next := t
for next.up != nil {
if next.up.left == next {
return next.up
}
next = next.up
}
return nil
}
// prev returns the node preceding the given one in order or nil if it is the first.
func (t *ipTree) prev() *ipTree {
if t.left != nil {
prev := t.left
for prev.right != nil {
prev = prev.right
}
return prev
}
prev := t
for prev.up != nil {
if prev.up.right == prev {
return prev.up
}
prev = prev.up
}
return nil
}
// walk visits all of the nodes in order by passing each node, in turn, to the
// given visit function.
func (t *ipTree) walk(visit func(*ipTree)) {
if t == nil {
return
}
t.left.walk(visit)
visit(t)
t.right.walk(visit)
}
// size returns the number of IPs in the set.
// It isn't efficient and only meant for testing.
func (t *ipTree) size() *big.Int {
s := big.NewInt(0)
if t != nil {
ones, bits := t.net.Mask.Size()
s.Lsh(big.NewInt(1), uint(bits-ones))
s.Add(s, t.left.size())
s.Add(s, t.right.size())
}
return s
}
// height returns the length of the maximum path from top node to leaf
// It isn't efficient and only meant for testing.
func (t *ipTree) height() uint {
if t == nil {
return 0
}
s := t.left.height()
if s < t.right.height() {
s = t.right.height()
}
return s + 1
}
// numNodes Return the number of nodes in the underlying tree It isn't
// efficient and only meant for testing.
func (t *ipTree) numNodes() int {
if t == nil {
return 0
}
return 1 + t.left.numNodes() + t.right.numNodes()
}
func (t *ipTree) validate() []error {
errs := []error{}
// if tree is nil, then it is valid
if t == nil {
return errs
}
// assert root's up is nil
if t.up != nil {
errs = append(errs, errors.New("root up must be nil"))
}
// validate each node
var lastNode *ipTree
t.walk(func(n *ipTree) {
// assert that the node's are linked properly
if n.left != nil && n.left.up != n {
errs = append(errs, errors.New("linkage error: left.up node must equal node"))
}
if n.right != nil && n.right.up != n {
errs = append(errs, errors.New("linkage error: right.up node must equal node"))
}
if n.net == nil {
errs = append(errs, errors.New("each node in tree must have a network"))
} else if !n.net.IP.Mask(n.net.Mask).Equal(n.net.IP) {
// verify that the network is valid
errs = append(errs, errors.New("cidr invalid: "+n.net.String()))
}
// assert order is correct
if lastNode != nil && bytes.Compare(lastNode.net.IP, n.net.IP) >= 0 {
errs = append(errs, errors.New("nodes must be in order: "+lastNode.net.IP.String()+" !< "+n.net.IP.String()))
}
lastNode = n
})
return errs
}