-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit.go
36 lines (28 loc) · 981 Bytes
/
split.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
package netadv
import "net"
// SplitNetworks split network in a
func SplitNetworks(network *net.IPNet, newsize uint) ([]*net.IPNet, error) {
var ret []*net.IPNet
oldsize, _ := network.Mask.Size()
// use a stack to avoid a recurvive function
ret = append(ret, network)
for i := oldsize; i < int(newsize); i++ {
var tmp []*net.IPNet
// parse network array to split all of them in two
for _, n := range ret {
one, two := SplitNetworkInTwo(n)
tmp = append(tmp, one)
tmp = append(tmp, two)
}
ret = tmp
}
return ret, nil
}
// SplitNetworkInTwo split a network in two. return two sub network
func SplitNetworkInTwo(network *net.IPNet) (*net.IPNet, *net.IPNet) {
size, _ := network.Mask.Size()
newMask := net.CIDRMask(size+1, 8*len(network.IP))
ip2 := SliceSetOne(net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, uint(size))
ip2 = SliceOr(network.IP, ip2)
return &net.IPNet{IP: network.IP, Mask: newMask}, &net.IPNet{IP: ip2, Mask: newMask}
}