Fix IPv6 case for CIDR format reverse zones (#4652)

* fix ipv6 case for cidr.Class

Signed-off-by: Chris O'Haver <cohaver@infoblox.com>

* add check and test case for invalid ipv6 cidr

Signed-off-by: Chris O'Haver <cohaver@infoblox.com>

* net package is bad at detecting ipv6/ipv4

Signed-off-by: Chris O'Haver <cohaver@infoblox.com>

* rename Class -> Split

Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
This commit is contained in:
Chris O'Haver
2021-06-04 04:17:17 -04:00
committed by GitHub
parent a50c0f151f
commit 846ace3f51
4 changed files with 67 additions and 9 deletions

View File

@@ -4,19 +4,28 @@ package cidr
import (
"math"
"net"
"strings"
"github.com/apparentlymart/go-cidr/cidr"
"github.com/miekg/dns"
)
// Class return slice of "classful" (/8, /16, /24 or /32 only) CIDR's from the CIDR in net.
func Class(n *net.IPNet) []string {
// Split returns a slice of non-overlapping subnets that in union equal the subnet n,
// and where each subnet falls on a reverse name segment boundary.
// for ipv4 this is any multiple of 8 bits (/8, /16, /24 or /32)
// for ipv6 this is any multiple of 4 bits
func Split(n *net.IPNet) []string {
boundary := 8
nstr := n.String()
if strings.Contains(nstr,":") {
boundary = 4
}
ones, _ := n.Mask.Size()
if ones%8 == 0 {
if ones%boundary == 0 {
return []string{n.String()}
}
mask := int(math.Ceil(float64(ones)/8)) * 8
mask := int(math.Ceil(float64(ones)/float64(boundary))) * boundary
networks := nets(n, mask)
cidrs := make([]string, len(networks))
for i := range networks {