update-ip/fqdn.go

40 lines
863 B
Go
Raw Permalink Normal View History

2024-07-24 15:38:56 +02:00
package updateip
import "strings"
// Canonicalize a DNS record.
func Canonicalize(domainName, recordName string) string {
if recordName == "@" {
return ToFqdn(domainName)
} else if strings.HasSuffix(recordName, ".@") {
return ToFqdn(strings.TrimSuffix(recordName, "@") + domainName)
}
return ToFqdn(recordName)
}
// IsFqdn checks if the label is a full-qualified name.
func IsFqdn(name string) bool {
return strings.HasSuffix(name, ".")
}
// ToFqdn converts the name into a fqdn appending a trailing dot.
func ToFqdn(name string) string {
if name == "@" {
return name
}
n := len(name)
if n == 0 || name[n-1] == '.' {
return name
}
return name + "."
}
// UnFqdn converts the fqdn into a name removing the trailing dot.
func UnFqdn(name string) string {
n := len(name)
if n != 0 && name[n-1] == '.' {
return name[:n-1]
}
return name
}