Reverse zones made easy: suggested, filled, in the right view, PTRs kept in step
Missing reverse zones are suggested for the private views' A and AAAA records (/24 for private IPv4, /32 for a public address, /48 for IPv6) and created filled with PTR records (jarl zone suggest-reverse, the Zones page). zone create --fill and zone fill-ptr add the PTRs a reverse zone lacks; a reverse zone created without a view goes to the private view using its addresses. Per zone, auto_ptr keeps the PTR records of A and AAAA records in step on every change (database version 12). Zone commands take a network. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a3b054cc86
commit
aa4b004a14
27 files changed
+1173
-38
No files matched your search
@@ -469,3 +469,47 @@ func BestZone(name string, zones []string) string {
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
// PrefixFromReverse returns the network a reverse zone answers for:
|
||||
// 10.0.10.in-addr.arpa. is 10.0.10.0/24, a /32 zone a single address, and
|
||||
// an ip6.arpa zone of 12 nibbles a /48. False for names that aren't whole
|
||||
// octets or nibbles (classless delegations such as 0-63.1.0.10.in-addr.arpa.).
|
||||
func PrefixFromReverse(zone string) (netip.Prefix, bool) {
|
||||
zone = strings.ToLower(Fqdn(zone))
|
||||
switch {
|
||||
case strings.HasSuffix(zone, ".in-addr.arpa."):
|
||||
labels := strings.Split(strings.TrimSuffix(zone, ".in-addr.arpa."), ".")
|
||||
if len(labels) > 4 {
|
||||
return netip.Prefix{}, false
|
||||
}
|
||||
var b [4]byte
|
||||
for i, l := range labels {
|
||||
v, err := strconv.Atoi(l)
|
||||
if err != nil || v < 0 || v > 255 || strconv.Itoa(v) != l {
|
||||
return netip.Prefix{}, false
|
||||
}
|
||||
b[len(labels)-1-i] = byte(v)
|
||||
}
|
||||
return netip.PrefixFrom(netip.AddrFrom4(b), 8*len(labels)), true
|
||||
case strings.HasSuffix(zone, ".ip6.arpa."):
|
||||
labels := strings.Split(strings.TrimSuffix(zone, ".ip6.arpa."), ".")
|
||||
if len(labels) > 32 {
|
||||
return netip.Prefix{}, false
|
||||
}
|
||||
var b [16]byte
|
||||
for i, l := range labels {
|
||||
v := strings.IndexByte("0123456789abcdef", l[0])
|
||||
if len(l) != 1 || v < 0 {
|
||||
return netip.Prefix{}, false
|
||||
}
|
||||
n := len(labels) - 1 - i // nibble position from the left
|
||||
if n%2 == 0 {
|
||||
b[n/2] |= byte(v) << 4
|
||||
} else {
|
||||
b[n/2] |= byte(v)
|
||||
}
|
||||
}
|
||||
return netip.PrefixFrom(netip.AddrFrom16(b), 4*len(labels)), true
|
||||
}
|
||||
return netip.Prefix{}, false
|
||||
}
|
||||
@@ -217,3 +217,35 @@ func TestReverseZones(t *testing.T) {
|
||||
t.Errorf("ReverseZone /22: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefixFromReverse(t *testing.T) {
|
||||
for zone, want := range map[string]string{
|
||||
"10.0.10.in-addr.arpa.": "10.0.10.0/24",
|
||||
"10.in-addr.arpa": "10.0.0.0/8",
|
||||
"154.144.144.139.in-addr.arpa.": "139.144.144.154/32",
|
||||
"d.3.f.7.3.1.0.8.2.0.a.2.ip6.arpa.": "2a02:8013:7f3d::/48",
|
||||
"b.a.b.a.b.a.b.a.b.a.d.f.ip6.arpa.": "fdab:abab:abab::/48",
|
||||
"0-63.1.0.10.in-addr.arpa.": "",
|
||||
"example.com.": "",
|
||||
"256.10.in-addr.arpa.": "",
|
||||
} {
|
||||
p, ok := PrefixFromReverse(zone)
|
||||
got := ""
|
||||
if ok {
|
||||
got = p.String()
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("PrefixFromReverse(%q) = %q, want %q", zone, got, want)
|
||||
}
|
||||
}
|
||||
// Round trip with ReverseZone.
|
||||
for _, s := range []string{"192.168.1.0/24", "2a02:8013:7f3d::/48", "fd00::/8", "10.0.0.5/32"} {
|
||||
z, err := ReverseZone(netip.MustParsePrefix(s))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p, ok := PrefixFromReverse(z); !ok || p.String() != s {
|
||||
t.Errorf("%s -> %s -> %v", s, z, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user