mirror of
https://github.com/coredns/coredns.git
synced 2025-11-02 02:03:13 -05:00
reverse zone: fix Normalize (#4621)
Make normalize return multiple "hosts" (= reverse zones) when a non-octet boundary cidr is given. Added pkg/cidr package that holds the cidr calculation routines; felt they didn't really fit dnsutil. This change means the IPNet return parameter isn't needed, the hosts are all correct. The tests that tests this is also removed: TestSplitHostPortReverse The fallout was that zoneAddr _also_ doesn't need the IPNet member, that in turn make it visible that zoneAddr in address.go duplicated a bunch of stuff from register.go; removed/refactored that too. Created a plugin.OriginsFromArgsOrServerBlock to help plugins do the right things, by consuming ZONE arguments; this now expands reverse zones correctly. This is mostly mechanical. Remove the reverse test in plugin/kubernetes which is a copy-paste from a core test (which has since been fixed). Remove MustNormalize as it has no plugin users. This change is not backwards compatible to plugins that have a ZONE argument that they parse in the setup util. All in-tree plugins have been updated. Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
74
plugin/pkg/cidr/cidr.go
Normal file
74
plugin/pkg/cidr/cidr.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Package cidr contains functions that deal with classless reverse zones in the DNS.
|
||||
package cidr
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net"
|
||||
|
||||
"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 {
|
||||
ones, _ := n.Mask.Size()
|
||||
if ones%8 == 0 {
|
||||
return []string{n.String()}
|
||||
}
|
||||
|
||||
mask := int(math.Ceil(float64(ones)/8)) * 8
|
||||
networks := nets(n, mask)
|
||||
cidrs := make([]string, len(networks))
|
||||
for i := range networks {
|
||||
cidrs[i] = networks[i].String()
|
||||
}
|
||||
return cidrs
|
||||
}
|
||||
|
||||
// nets return a slice of prefixes with the desired mask subnetted from original network.
|
||||
func nets(network *net.IPNet, newPrefixLen int) []*net.IPNet {
|
||||
prefixLen, _ := network.Mask.Size()
|
||||
maxSubnets := int(math.Exp2(float64(newPrefixLen)) / math.Exp2(float64(prefixLen)))
|
||||
nets := []*net.IPNet{{network.IP, net.CIDRMask(newPrefixLen, 8*len(network.IP))}}
|
||||
|
||||
for i := 1; i < maxSubnets; i++ {
|
||||
next, exceeds := cidr.NextSubnet(nets[len(nets)-1], newPrefixLen)
|
||||
nets = append(nets, next)
|
||||
if exceeds {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nets
|
||||
}
|
||||
|
||||
// Reverse return the reverse zones that are authoritative for each net in ns.
|
||||
func Reverse(nets []string) []string {
|
||||
rev := make([]string, len(nets))
|
||||
for i := range nets {
|
||||
ip, n, _ := net.ParseCIDR(nets[i])
|
||||
r, err1 := dns.ReverseAddr(ip.String())
|
||||
if err1 != nil {
|
||||
continue
|
||||
}
|
||||
ones, bits := n.Mask.Size()
|
||||
// get the size, in bits, of each portion of hostname defined in the reverse address. (8 for IPv4, 4 for IPv6)
|
||||
sizeDigit := 8
|
||||
if len(n.IP) == net.IPv6len {
|
||||
sizeDigit = 4
|
||||
}
|
||||
// Get the first lower octet boundary to see what encompassing zone we should be authoritative for.
|
||||
mod := (bits - ones) % sizeDigit
|
||||
nearest := (bits - ones) + mod
|
||||
offset := 0
|
||||
var end bool
|
||||
for i := 0; i < nearest/sizeDigit; i++ {
|
||||
offset, end = dns.NextLabel(r, offset)
|
||||
if end {
|
||||
break
|
||||
}
|
||||
}
|
||||
rev[i] = r[offset:]
|
||||
}
|
||||
return rev
|
||||
}
|
||||
46
plugin/pkg/cidr/cidr_test.go
Normal file
46
plugin/pkg/cidr/cidr_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package cidr
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var tests = []struct {
|
||||
in string
|
||||
expected []string
|
||||
zones []string
|
||||
}{
|
||||
{"10.0.0.0/15", []string{"10.0.0.0/16", "10.1.0.0/16"}, []string{"0.10.in-addr.arpa.", "1.10.in-addr.arpa."}},
|
||||
{"10.0.0.0/16", []string{"10.0.0.0/16"}, []string{"0.10.in-addr.arpa."}},
|
||||
{"192.168.1.1/23", []string{"192.168.0.0/24", "192.168.1.0/24"}, []string{"0.168.192.in-addr.arpa.", "1.168.192.in-addr.arpa."}},
|
||||
{"10.129.60.0/22", []string{"10.129.60.0/24", "10.129.61.0/24", "10.129.62.0/24", "10.129.63.0/24"}, []string{"60.129.10.in-addr.arpa.", "61.129.10.in-addr.arpa.", "62.129.10.in-addr.arpa.", "63.129.10.in-addr.arpa."}},
|
||||
}
|
||||
|
||||
func TestClass(t *testing.T) {
|
||||
for i, tc := range tests {
|
||||
_, n, _ := net.ParseCIDR(tc.in)
|
||||
nets := Class(n)
|
||||
if len(nets) != len(tc.expected) {
|
||||
t.Errorf("Test %d, expected %d subnets, got %d", i, len(tc.expected), len(nets))
|
||||
continue
|
||||
}
|
||||
for j := range nets {
|
||||
if nets[j] != tc.expected[j] {
|
||||
t.Errorf("Test %d, expected %s, got %s", i, tc.expected[j], nets[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReverse(t *testing.T) {
|
||||
for i, tc := range tests {
|
||||
_, n, _ := net.ParseCIDR(tc.in)
|
||||
nets := Class(n)
|
||||
reverse := Reverse(nets)
|
||||
for j := range reverse {
|
||||
if reverse[j] != tc.zones[j] {
|
||||
t.Errorf("Test %d, expected %s, got %s", i, tc.zones[j], reverse[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,10 +31,11 @@ func (f F) Through(qname string) bool {
|
||||
|
||||
// setZones will set zones in f.
|
||||
func (f *F) setZones(zones []string) {
|
||||
z := []string{}
|
||||
for i := range zones {
|
||||
zones[i] = plugin.Host(zones[i]).Normalize()
|
||||
z = append(z, plugin.Host(zones[i]).Normalize()...)
|
||||
}
|
||||
f.Zones = zones
|
||||
f.Zones = z
|
||||
}
|
||||
|
||||
// SetZonesFromArgs sets zones in f to the passed value or to "." if the slice is empty.
|
||||
@@ -47,7 +48,7 @@ func (f *F) SetZonesFromArgs(zones []string) {
|
||||
}
|
||||
|
||||
// Equal returns true if f and g are equal.
|
||||
func (f F) Equal(g F) bool {
|
||||
func (f *F) Equal(g F) bool {
|
||||
if len(f.Zones) != len(g.Zones) {
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user