-
Notifications
You must be signed in to change notification settings - Fork 4
/
collapse.go
213 lines (177 loc) · 4.16 KB
/
collapse.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
package ipx
import (
"net"
"sort"
)
// Collapse combines subnets into their closest available parent.
func Collapse(toMerge []*net.IPNet) []*net.IPNet {
var (
four []ip4Net
six []ip6Net
)
for _, ipN := range toMerge {
if ipN.IP.To4() != nil {
four = append(four, newIP4Net(ipN))
continue
}
six = append(six, newIP6Net(ipN))
}
return append(collapse4(four), collapse6(six)...)
}
func collapse4(nets []ip4Net) []*net.IPNet {
if len(nets) == 0 {
return nil
}
supers := make(map[ip4Net]ip4Net)
for len(nets) > 0 {
n := nets[len(nets)-1]
nets = nets[:len(nets)-1]
s := n.super()
other, ok := supers[s]
if !ok {
supers[s] = n
continue
}
if other == n {
continue
}
// we have found two nets with same immediate parent -- merge 'em
delete(supers, s)
nets = append(nets, s)
}
merged := make(ip4Nets, 0, len(supers))
for _, v := range supers {
merged = append(merged, v)
}
sort.Sort(merged)
result := []*net.IPNet{merged[0].asNet()}
lastMask := merged[0].mask()
lastAddr := merged[0].addr
for _, m := range merged[1:] {
if lastAddr == m.addr&lastMask {
continue
}
result = append(result, m.asNet())
lastMask, lastAddr = m.mask(), m.addr
}
return result
}
func collapse6(nets []ip6Net) []*net.IPNet {
if len(nets) == 0 {
return nil
}
supers := make(map[ip6Net]ip6Net)
for len(nets) > 0 {
n := nets[len(nets)-1]
nets = nets[:len(nets)-1]
s := n.super()
other, ok := supers[s]
if !ok {
supers[s] = n
continue
}
if other == n {
continue
}
// we have found two nets with same immediate parent -- merge 'em
delete(supers, s)
nets = append(nets, s)
}
merged := make(ip6Nets, 0, len(supers))
for _, v := range supers {
merged = append(merged, v)
}
sort.Sort(merged)
result := []*net.IPNet{merged[0].asNet()}
lastMask := merged[0].mask()
lastAddr := merged[0].addr
for _, m := range merged[1:] {
if lastAddr == m.addr.And(lastMask) {
continue
}
result = append(result, m.asNet())
lastMask, lastAddr = m.mask(), m.addr
}
return result
}
type ip4Net struct {
addr uint32
prefix uint8
}
func newIP4Net(ipN *net.IPNet) ip4Net {
ones, _ := ipN.Mask.Size()
return ip4Net{to32(ipN.IP), uint8(ones)}
}
func (n ip4Net) super() ip4Net {
n.addr &^= 1 << (32 - n.prefix) // unset last bit of net mask to find supernet address
n.prefix--
return n
}
func (n ip4Net) asNet() *net.IPNet {
r := &net.IPNet{IP: make(net.IP, 4), Mask: make(net.IPMask, 4)}
from32(n.addr, r.IP)
from32(n.mask(), r.Mask) // set first eight bits
return r
}
func (n ip4Net) subnets() (ip4Net, ip4Net) {
a := ip4Net{addr: n.addr, prefix: n.prefix + 1}
b := ip4Net{addr: n.addr | 1<<(31-n.prefix), prefix: n.prefix + 1}
return a, b
}
func (n ip4Net) subnetOf(o ip4Net) bool {
return n.addr&o.mask() == o.addr
}
func (n ip4Net) mask() uint32 {
return ^(1<<(32-n.prefix) - 1)
}
type ip4Nets []ip4Net
func (n ip4Nets) Len() int {
return len(n)
}
func (n ip4Nets) Less(i, j int) bool {
return n[i].addr < n[j].addr
}
func (n ip4Nets) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
type ip6Net struct {
addr Uint128
prefix uint8
}
func newIP6Net(ipN *net.IPNet) ip6Net {
ones, _ := ipN.Mask.Size()
return ip6Net{To128(ipN.IP), uint8(ones)}
}
func (n ip6Net) super() ip6Net {
// unset last bit of net mask to find supernet address
n.addr = n.addr.And(Uint128{0, 1}.Lsh(128 - uint(n.prefix)).Not())
n.prefix--
return n
}
func (n ip6Net) subnets() (ip6Net, ip6Net) {
a := ip6Net{addr: n.addr, prefix: n.prefix + 1}
b := ip6Net{addr: n.addr.Or(Uint128{0, 1}.Lsh(uint(127 - n.prefix))), prefix: n.prefix + 1}
return a, b
}
func (n ip6Net) asNet() *net.IPNet {
r := &net.IPNet{IP: make(net.IP, 16), Mask: make(net.IPMask, 16)}
From128(n.addr, r.IP)
From128(n.mask(), r.Mask) // set prefix bits
return r
}
func (n ip6Net) mask() Uint128 {
return Uint128{0, 1}.Lsh(128 - uint(n.prefix)).Minus(Uint128{0, 1}).Not()
}
func (n ip6Net) subnetOf(o ip6Net) bool {
return n.addr.And(o.mask()).Cmp(o.addr) == 0
}
type ip6Nets []ip6Net
func (n ip6Nets) Len() int {
return len(n)
}
func (n ip6Nets) Less(i, j int) bool {
return n[i].addr.Cmp(n[j].addr) == -1
}
func (n ip6Nets) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}