-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffered.go
238 lines (220 loc) · 5.82 KB
/
buffered.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
package subnetmath
import (
"bytes"
"math"
"math/big"
"net"
"sync"
)
type Buffer struct {
mtx *sync.Mutex
bigIntAlpha *big.Int
bigIntBravo *big.Int
bigIntCharlie *big.Int
bigIntDelta *big.Int
bigIntEcho *big.Int
ipSubZero [16]byte
}
func NewBuffer() *Buffer {
return &Buffer{
mtx: &sync.Mutex{},
bigIntAlpha: new(big.Int),
bigIntBravo: new(big.Int),
bigIntCharlie: new(big.Int),
bigIntDelta: new(big.Int),
bigIntEcho: new(big.Int),
ipSubZero: [16]byte{},
}
}
var memoizedBigExp2 [128]*big.Int
func init() {
for i := range memoizedBigExp2 {
memoizedBigExp2[i] = new(big.Int).Exp(bigTwo, big.NewInt(int64(i)), nil)
}
}
// NetworkComesBefore returns a bool with regards to numerical network order.
// Note that IPv4 networks come before IPv6 networks.
func (b *Buffer) NetworkComesBefore(first, second *net.IPNet) bool {
if first != nil && second != nil {
if first.IP.Equal(second.IP) {
firstMask, _ := first.Mask.Size()
secondMask, _ := second.Mask.Size()
if firstMask < secondMask {
return true
}
return false
}
return b.AddressComesBefore(first.IP, second.IP)
}
return false
}
// AddressComesBefore returns a bool with regards to numerical address order.
// Note that IPv4 addresses come before IPv6 addresses.
func (b *Buffer) AddressComesBefore(firstIP, secondIP net.IP) bool {
if firstIP.To4() == nil && secondIP.To4() != nil {
return false
} else if firstIP.To4() != nil && secondIP.To4() == nil {
return true
}
b.mtx.Lock()
defer b.mtx.Unlock()
if b.addrToIntAlpha(firstIP).Cmp(b.addrToIntBravo(secondIP)) < 0 {
return true
}
return false
}
// NetworkContainsSubnet validates that the network is a valid supernet
func (b *Buffer) NetworkContainsSubnet(network *net.IPNet, subnet *net.IPNet) bool {
if network != nil && subnet != nil {
b.mtx.Lock()
defer b.mtx.Unlock()
supernetInt := b.addrToIntAlpha(network.IP)
subnetInt := b.addrToIntBravo(subnet.IP)
if supernetInt.Cmp(subnetInt) <= 0 {
supernetInt.Add(supernetInt, b.addressCountCharlieDelta(network))
subnetInt.Add(subnetInt, b.addressCountCharlieDelta(subnet))
if supernetInt.Cmp(subnetInt) >= 0 {
return true
}
}
}
return false
}
func (b *Buffer) addrToIntAlpha(address net.IP) *big.Int {
v4addr := address.To4()
if v4addr != nil {
b.bigIntAlpha.SetBytes(v4addr)
} else {
b.bigIntAlpha.SetBytes(address.To16())
}
return b.bigIntAlpha
}
func (b *Buffer) addrToIntBravo(address net.IP) *big.Int {
v4addr := address.To4()
if v4addr != nil {
b.bigIntBravo.SetBytes(v4addr)
} else {
b.bigIntBravo.SetBytes(address.To16())
}
return b.bigIntBravo
}
func (b *Buffer) addressCountCharlieDelta(network *net.IPNet) *big.Int {
if network != nil {
ones, bits := network.Mask.Size()
if bits <= 32 {
return b.bigIntCharlie.SetInt64(int64(math.Exp2(float64(bits - ones))))
}
return memoizedBigExp2[bits-ones]
}
return nil
}
func (b *Buffer) nextNetworkEcho(network *net.IPNet) *net.IPNet {
if network != nil {
nextNetwork := DuplicateNetwork(network)
v4addr := network.IP.To4()
if v4addr != nil {
b.bigIntEcho.SetBytes(v4addr)
} else {
b.bigIntEcho.SetBytes(network.IP.To16())
}
b.bigIntEcho.Add(b.bigIntEcho, b.addressCountCharlieDelta(network))
nextNetwork.IP = IntToAddr(b.bigIntEcho)
return nextNetwork
}
return nil
}
// FindInbetweenSubnets returns a slice of subnets given a range of IP addresses.
// Note that the delimiter 'stop' is inclusive. In other words, it will be included in the result.
func (b *Buffer) FindInbetweenSubnets(start, stop net.IP) []*net.IPNet {
if sameAddrType(start, stop) && b.AddressComesBefore(start, stop) {
var subnets []*net.IPNet
maskBits := maskBitLength(start)
current := DuplicateAddr(start)
stopInt := b.addrToIntAlpha(stop)
for {
currentSubnet := &net.IPNet{
IP: current,
Mask: make(net.IPMask, maskBits/8),
}
for ones := 1; ones <= maskBits; ones++ {
currentSubnet.Mask = recreateMask(currentSubnet.Mask, ones, maskBits)
increment := b.addressCountCharlieDelta(currentSubnet)
addressInt := b.addrToIntBravo(currentSubnet.IP)
addressInt.Add(addressInt, increment)
addressInt.Sub(addressInt, bigOne)
if addressInt.Cmp(stopInt) > 0 {
continue
}
if b.SubnetZeroAddr(currentSubnet.IP, currentSubnet).Equal(currentSubnet.IP) {
break
}
}
subnets = append(subnets, currentSubnet)
current = b.nextNetworkEcho(currentSubnet).IP
if b.AddressComesBefore(current, start) {
break
}
if b.AddressComesBefore(stop, current) && !current.Equal(stop) {
break
}
}
return subnets
}
return nil
}
func allFF(b []byte) bool {
for _, c := range b {
if c != 0xff {
return false
}
}
return true
}
var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
func applyMaskDirectly(ip net.IP, mask net.IPMask) net.IP {
if len(mask) == net.IPv6len && len(ip) == net.IPv4len && allFF(mask[:12]) {
mask = mask[12:]
}
if len(mask) == net.IPv4len && len(ip) == net.IPv6len && bytes.Equal(ip[:12], v4InV6Prefix) {
ip = ip[12:]
}
if len(ip) == len(mask) {
for i := 0; i < len(ip); i++ {
ip[i] = ip[i] & mask[i]
}
return ip
}
return nil
}
// SubnetZeroAddr returns the subnet zero address
func (b *Buffer) SubnetZeroAddr(address net.IP, network *net.IPNet) net.IP {
if network != nil {
b.mtx.Lock()
defer b.mtx.Unlock()
for i := 0; i < len(address); i++ {
b.ipSubZero[i] = address[i]
}
return applyMaskDirectly(b.ipSubZero[:len(address)], network.Mask)
}
return nil
}
func recreateMask(mask net.IPMask, ones, bits int) net.IPMask {
if bits != 8*net.IPv4len && bits != 8*net.IPv6len {
return nil
}
if ones < 0 || ones > bits {
return nil
}
l := bits / 8
n := uint(ones)
for i := 0; i < l; i++ {
if n >= 8 {
mask[i] = 0xff
n -= 8
continue
}
mask[i] = ^byte(0xff >> n)
n = 0
}
return mask
}