Better trie implementations
This commit is contained in:
62
dataset/dnstrie/name.go
Normal file
62
dataset/dnstrie/name.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package dnstrie
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// isValidDomainName validates if the given string is a valid DNS hostname.
|
||||
// A valid hostname consists of a series of labels separated by dots.
|
||||
// Each label must:
|
||||
// - Be between 1 and 63 characters long.
|
||||
// - Consist only of ASCII letters ('a'-'z', 'A'-'Z'), digits ('0'-'9'), and hyphens ('-').
|
||||
// - Not start or end with a hyphen.
|
||||
// The total length of the hostname (including dots) must not exceed 253 characters.
|
||||
func isValidDomainName(host string) bool {
|
||||
// 1. Check total length. The maximum length of a full hostname is 253 characters.
|
||||
if len(host) > 253 {
|
||||
return false
|
||||
}
|
||||
|
||||
// An empty string is not a valid hostname.
|
||||
if host == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// 2. Handle optional trailing dot for FQDNs.
|
||||
// If the hostname ends with a dot, we remove it for validation purposes.
|
||||
if strings.HasSuffix(host, ".") {
|
||||
host = host[:len(host)-1]
|
||||
}
|
||||
|
||||
// After removing a potential trailing dot, the string might be empty.
|
||||
if host == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// 3. Split the hostname into labels.
|
||||
labels := strings.Split(host, ".")
|
||||
|
||||
// 4. Validate each label.
|
||||
for _, label := range labels {
|
||||
// a. Check label length (1 to 63 characters).
|
||||
if len(label) < 1 || len(label) > 63 {
|
||||
return false
|
||||
}
|
||||
|
||||
// b. Check if label starts or ends with a hyphen.
|
||||
if strings.HasPrefix(label, "-") || strings.HasSuffix(label, "-") {
|
||||
return false
|
||||
}
|
||||
|
||||
// c. Check for allowed characters in the label.
|
||||
for _, char := range label {
|
||||
if !unicode.IsLetter(char) && !unicode.IsDigit(char) && char != '-' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If all checks pass, the hostname is valid.
|
||||
return true
|
||||
}
|
Reference in New Issue
Block a user