diff --git a/service.go b/service.go index 2ffb6fd..92cf8da 100644 --- a/service.go +++ b/service.go @@ -1040,6 +1040,14 @@ func normalizeIP(ip string) string { return parsed.String() } +// NormalizeIP normalizes a raw IP string into its canonical textual form. +// +// normalized := dns.NormalizeIP("010.000.000.001") +// // normalized == "10.0.0.1" +func NormalizeIP(ip string) string { + return normalizeIP(ip) +} + func computeTreeRoot(records map[string]NameRecords) string { names := make([]string, 0, len(records)) for name := range records { @@ -1183,6 +1191,14 @@ func normalizeName(name string) string { return trimmed } +// NormalizeName normalizes a raw DNS name for cache lookups and action handling. +// +// normalized := dns.NormalizeName("Gateway.Charon.lthn.") +// // normalized == "gateway.charon.lthn" +func NormalizeName(name string) string { + return normalizeName(name) +} + // String returns a compact debug representation of the service. // // fmt.Println(service) diff --git a/service_test.go b/service_test.go index c76dc54..525d5be 100644 --- a/service_test.go +++ b/service_test.go @@ -3016,6 +3016,22 @@ func TestStringActionValueRejectsWhitespaceOnlyArgument(t *testing.T) { } } +func TestNormalizeNameAndNormalizeIPArePublicWrappers(t *testing.T) { + normalizedName := NormalizeName(" Gateway.Charon.lthn. ") + if normalizedName != "gateway.charon.lthn" { + t.Fatalf("expected normalized DNS name, got %q", normalizedName) + } + + normalizedIP := NormalizeIP(" 2001:0DB8::0001 ") + if normalizedIP != "2001:db8::1" { + t.Fatalf("expected normalized IP, got %q", normalizedIP) + } + + if NormalizeIP("invalid") != "" { + t.Fatal("expected invalid IP to normalize to empty string") + } +} + func TestIntActionValueRejectsNonIntegerFloat(t *testing.T) { _, err := intActionValue(map[string]any{ actionArgPort: 53.9,