| 1 |
package dnsreverse |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"net" |
| 6 |
"net/netip" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"codeberg.org/miekg/dns/dnsutil" |
| 10 |
"github.com/apparentlymart/go-cidr/cidr" |
| 11 |
) |
| 12 |
|
| 13 |
// Zones return the reverse zones that are authoritative for each net in n. |
| 14 |
func Zones(n *net.IPNet) []string { |
| 15 |
nets := split(n) |
| 16 |
rev := make([]string, len(nets)) |
| 17 |
for i := range nets { |
| 18 |
ip, n, _ := net.ParseCIDR(nets[i]) |
| 19 |
|
| 20 |
addr, _ := netip.AddrFromSlice(ip) |
| 21 |
r := dnsutil.ReverseAddr(addr) |
| 22 |
if len(n.IP) != net.IPv6len { |
| 23 |
addr, _ := netip.AddrFromSlice(ip.To4()) |
| 24 |
r = dnsutil.ReverseAddr(addr) |
| 25 |
} |
| 26 |
if r == "" { |
| 27 |
continue |
| 28 |
} |
| 29 |
|
| 30 |
ones, bits := n.Mask.Size() |
| 31 |
// get the size, in bits, of each portion of hostname defined in the reverse address. (8 for IPv4, 4 for IPv6) |
| 32 |
sizeDigit := 8 |
| 33 |
if len(n.IP) == net.IPv6len { |
| 34 |
sizeDigit = 4 |
| 35 |
} |
| 36 |
// Get the first lower octet boundary to see what encompassing zone we should be authoritative for. |
| 37 |
mod := (bits - ones) % sizeDigit |
| 38 |
nearest := (bits - ones) + mod |
| 39 |
offset := 0 |
| 40 |
var end bool |
| 41 |
for i := 0; i < nearest/sizeDigit; i++ { |
| 42 |
offset, end = dnsutil.Next(r, offset) |
| 43 |
if end { |
| 44 |
break |
| 45 |
} |
| 46 |
} |
| 47 |
rev[i] = r[offset:] |
| 48 |
} |
| 49 |
return rev |
| 50 |
} |
| 51 |
|
| 52 |
// split returns a slice of non-overlapping subnets that in union equal the subnet n, |
| 53 |
// and where each subnet falls on a reverse name segment boundary. |
| 54 |
// For ipv4 this is any multiple of 8 bits (/8, /16, /24 or /32). |
| 55 |
// For ipv6 this is any multiple of 4 bits. |
| 56 |
func split(n *net.IPNet) []string { |
| 57 |
boundary := 8 |
| 58 |
nstr := n.String() |
| 59 |
if strings.Contains(nstr, ":") { |
| 60 |
boundary = 4 |
| 61 |
} |
| 62 |
ones, _ := n.Mask.Size() |
| 63 |
if ones%boundary == 0 { |
| 64 |
return []string{n.String()} |
| 65 |
} |
| 66 |
|
| 67 |
mask := int(math.Ceil(float64(ones)/float64(boundary))) * boundary |
| 68 |
networks := nets(n, mask) |
| 69 |
cidrs := make([]string, len(networks)) |
| 70 |
for i := range networks { |
| 71 |
cidrs[i] = networks[i].String() |
| 72 |
} |
| 73 |
return cidrs |
| 74 |
} |
| 75 |
|
| 76 |
// nets return a slice of prefixes with the desired mask subnetted from original network. |
| 77 |
func nets(network *net.IPNet, newPrefixLen int) []*net.IPNet { |
| 78 |
prefixLen, _ := network.Mask.Size() |
| 79 |
max := int(math.Exp2(float64(newPrefixLen)) / math.Exp2(float64(prefixLen))) |
| 80 |
nets := []*net.IPNet{{IP: network.IP, Mask: net.CIDRMask(newPrefixLen, 8*len(network.IP))}} |
| 81 |
|
| 82 |
for i := 1; i < max; i++ { |
| 83 |
next, exceeds := cidr.NextSubnet(nets[len(nets)-1], newPrefixLen) |
| 84 |
nets = append(nets, next) |
| 85 |
if exceeds { |
| 86 |
break |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return nets |
| 91 |
} |