vendor: go fetch github.com/apparentlymart/go-cidr/cidr
This is to get an upstream fix which will in turn address #15321.
This commit is contained in:
parent
5963ea6f26
commit
96d08e8aac
|
@ -71,8 +71,13 @@ func Host(base *net.IPNet, num int) (net.IP, error) {
|
||||||
if numUint64 > maxHostNum {
|
if numUint64 > maxHostNum {
|
||||||
return nil, fmt.Errorf("prefix of %d does not accommodate a host numbered %d", parentLen, num)
|
return nil, fmt.Errorf("prefix of %d does not accommodate a host numbered %d", parentLen, num)
|
||||||
}
|
}
|
||||||
|
var bitlength int
|
||||||
return insertNumIntoIP(ip, num, 32), nil
|
if ip.To4() != nil {
|
||||||
|
bitlength = 32
|
||||||
|
} else {
|
||||||
|
bitlength = 128
|
||||||
|
}
|
||||||
|
return insertNumIntoIP(ip, num, bitlength), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddressRange returns the first and last addresses in the given CIDR range.
|
// AddressRange returns the first and last addresses in the given CIDR range.
|
||||||
|
@ -110,3 +115,96 @@ func AddressCount(network *net.IPNet) uint64 {
|
||||||
prefixLen, bits := network.Mask.Size()
|
prefixLen, bits := network.Mask.Size()
|
||||||
return 1 << (uint64(bits) - uint64(prefixLen))
|
return 1 << (uint64(bits) - uint64(prefixLen))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//VerifyNoOverlap takes a list subnets and supernet (CIDRBlock) and verifies
|
||||||
|
//none of the subnets overlap and all subnets are in the supernet
|
||||||
|
//it returns an error if any of those conditions are not satisfied
|
||||||
|
func VerifyNoOverlap(subnets []*net.IPNet, CIDRBlock *net.IPNet) error {
|
||||||
|
firstLastIP := make([][]net.IP, len(subnets))
|
||||||
|
for i, s := range subnets {
|
||||||
|
first, last := AddressRange(s)
|
||||||
|
firstLastIP[i] = []net.IP{first, last}
|
||||||
|
}
|
||||||
|
for i, s := range subnets {
|
||||||
|
if !CIDRBlock.Contains(firstLastIP[i][0]) || !CIDRBlock.Contains(firstLastIP[i][1]) {
|
||||||
|
return fmt.Errorf("%s does not fully contain %s", CIDRBlock.String(), s.String())
|
||||||
|
}
|
||||||
|
for j := i + 1; j < len(subnets); j++ {
|
||||||
|
first := firstLastIP[j][0]
|
||||||
|
last := firstLastIP[j][1]
|
||||||
|
if s.Contains(first) || s.Contains(last) {
|
||||||
|
return fmt.Errorf("%s overlaps with %s", subnets[j].String(), s.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PreviousSubnet returns the subnet of the desired mask in the IP space
|
||||||
|
// just lower than the start of IPNet provided. If the IP space rolls over
|
||||||
|
// then the second return value is true
|
||||||
|
func PreviousSubnet(network *net.IPNet, prefixLen int) (*net.IPNet, bool) {
|
||||||
|
startIP := checkIPv4(network.IP)
|
||||||
|
previousIP := make(net.IP, len(startIP))
|
||||||
|
copy(previousIP, startIP)
|
||||||
|
cMask := net.CIDRMask(prefixLen, 8*len(previousIP))
|
||||||
|
previousIP = Dec(previousIP)
|
||||||
|
previous := &net.IPNet{IP: previousIP.Mask(cMask), Mask: cMask}
|
||||||
|
if startIP.Equal(net.IPv4zero) || startIP.Equal(net.IPv6zero) {
|
||||||
|
return previous, true
|
||||||
|
}
|
||||||
|
return previous, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// NextSubnet returns the next available subnet of the desired mask size
|
||||||
|
// starting for the maximum IP of the offset subnet
|
||||||
|
// If the IP exceeds the maxium IP then the second return value is true
|
||||||
|
func NextSubnet(network *net.IPNet, prefixLen int) (*net.IPNet, bool) {
|
||||||
|
_, currentLast := AddressRange(network)
|
||||||
|
mask := net.CIDRMask(prefixLen, 8*len(currentLast))
|
||||||
|
currentSubnet := &net.IPNet{IP: currentLast.Mask(mask), Mask: mask}
|
||||||
|
_, last := AddressRange(currentSubnet)
|
||||||
|
last = Inc(last)
|
||||||
|
next := &net.IPNet{IP: last.Mask(mask), Mask: mask}
|
||||||
|
if last.Equal(net.IPv4zero) || last.Equal(net.IPv6zero) {
|
||||||
|
return next, true
|
||||||
|
}
|
||||||
|
return next, false
|
||||||
|
}
|
||||||
|
|
||||||
|
//Inc increases the IP by one this returns a new []byte for the IP
|
||||||
|
func Inc(IP net.IP) net.IP {
|
||||||
|
IP = checkIPv4(IP)
|
||||||
|
incIP := make([]byte, len(IP))
|
||||||
|
copy(incIP, IP)
|
||||||
|
for j := len(incIP) - 1; j >= 0; j-- {
|
||||||
|
incIP[j]++
|
||||||
|
if incIP[j] > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return incIP
|
||||||
|
}
|
||||||
|
|
||||||
|
//Dec decreases the IP by one this returns a new []byte for the IP
|
||||||
|
func Dec(IP net.IP) net.IP {
|
||||||
|
IP = checkIPv4(IP)
|
||||||
|
decIP := make([]byte, len(IP))
|
||||||
|
copy(decIP, IP)
|
||||||
|
decIP = checkIPv4(decIP)
|
||||||
|
for j := len(decIP) - 1; j >= 0; j-- {
|
||||||
|
decIP[j]--
|
||||||
|
if decIP[j] < 255 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return decIP
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkIPv4(ip net.IP) net.IP {
|
||||||
|
// Go for some reason allocs IPv6len for IPv4 so we have to correct it
|
||||||
|
if v4 := ip.To4(); v4 != nil {
|
||||||
|
return v4
|
||||||
|
}
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
|
@ -94,10 +94,10 @@
|
||||||
"revisionTime": "2015-08-30T18:26:16Z"
|
"revisionTime": "2015-08-30T18:26:16Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "FIL83loX9V9APvGQIjJpbxq53F0=",
|
"checksumSHA1": "7eAIWei337IlBYIfzA3HyOEV9WE=",
|
||||||
"path": "github.com/apparentlymart/go-cidr/cidr",
|
"path": "github.com/apparentlymart/go-cidr/cidr",
|
||||||
"revision": "7e4b007599d4e2076d9a81be723b3912852dda2c",
|
"revision": "2bd8b58cf4275aeb086ade613de226773e29e853",
|
||||||
"revisionTime": "2017-04-18T07:21:50Z"
|
"revisionTime": "2017-06-16T19:18:03Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "l0iFqayYAaEip6Olaq3/LCOa/Sg=",
|
"checksumSHA1": "l0iFqayYAaEip6Olaq3/LCOa/Sg=",
|
||||||
|
|
Loading…
Reference in New Issue