util: handle single-address IPv4 prefix in reverse DNS generation

A /32 indexed past the 4-byte address and panicked at startup.
This commit is contained in:
Kristoffer Dalby
2026-06-05 14:59:10 +00:00
committed by Kristoffer Dalby
parent 08f186f22a
commit ba54349176
2 changed files with 40 additions and 0 deletions
+20
View File
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/netip"
"slices"
"strconv"
"strings"
"unicode"
@@ -103,6 +104,25 @@ func GenerateIPv4DNSRootDomain(ipPrefix netip.Prefix) []dnsname.FQDN {
// wildcardBits is the number of bits not under the mask in the lastOctet
wildcardBits := ByteSize - maskBits%ByteSize
// A mask covering the full address width (an IPv4 /32) leaves no wildcard
// octet, so lastOctet would index past the address. Emit the single
// reverse-DNS name for that exact address instead of panicking.
if lastOctet >= len(netRange.IP) {
rdnsSlice := make([]string, 0, len(netRange.IP)+1)
for _, v := range slices.Backward(netRange.IP) {
rdnsSlice = append(rdnsSlice, strconv.FormatUint(uint64(v), 10))
}
rdnsSlice = append(rdnsSlice, "in-addr.arpa.")
fqdn, err := dnsname.ToFQDN(strings.Join(rdnsSlice, "."))
if err != nil {
return nil
}
return []dnsname.FQDN{fqdn}
}
// minVal is the value in the lastOctet byte of the IP
// maxVal is basically 2^wildcardBits - i.e., the value when all the wildcardBits are set to 1
minVal := uint(netRange.IP[lastOctet])
+20
View File
@@ -0,0 +1,20 @@
package util
import (
"net/netip"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestGenerateIPv4DNSRootDomainSingleAddress ensures a single-address IPv4
// prefix (/32) does not index past the 4-byte address and panic. A full-width
// mask leaves no wildcard octet, so the function must emit the one reverse-DNS
// name for that address.
func TestGenerateIPv4DNSRootDomainSingleAddress(t *testing.T) {
require.NotPanics(t, func() {
fqdns := GenerateIPv4DNSRootDomain(netip.MustParsePrefix("100.64.0.1/32"))
assert.Len(t, fqdns, 1)
})
}