2018-05-01 23:45:06 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
// Holds a bunch of helper functions for dealing with labels.
|
|
|
|
|
|
|
|
// SplitDomainName splits a name string into it's labels.
|
|
|
|
// www.miek.nl. returns []string{"www", "miek", "nl"}
|
|
|
|
// .www.miek.nl. returns []string{"", "www", "miek", "nl"},
|
|
|
|
// The root label (.) returns nil. Note that using
|
|
|
|
// strings.Split(s) will work in most cases, but does not handle
|
|
|
|
// escaped dots (\.) for instance.
|
|
|
|
// s must be a syntactically valid domain name, see IsDomainName.
|
|
|
|
func SplitDomainName(s string) (labels []string) {
|
2022-01-25 13:15:24 +00:00
|
|
|
if s == "" {
|
2018-05-01 23:45:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fqdnEnd := 0 // offset of the final '.' or the length of the name
|
|
|
|
idx := Split(s)
|
|
|
|
begin := 0
|
2019-04-17 17:15:55 +00:00
|
|
|
if IsFqdn(s) {
|
2018-05-01 23:45:06 +00:00
|
|
|
fqdnEnd = len(s) - 1
|
|
|
|
} else {
|
|
|
|
fqdnEnd = len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(idx) {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
// no-op
|
|
|
|
default:
|
2019-04-17 17:15:55 +00:00
|
|
|
for _, end := range idx[1:] {
|
2018-05-01 23:45:06 +00:00
|
|
|
labels = append(labels, s[begin:end-1])
|
|
|
|
begin = end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
return append(labels, s[begin:fqdnEnd])
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompareDomainName compares the names s1 and s2 and
|
|
|
|
// returns how many labels they have in common starting from the *right*.
|
|
|
|
// The comparison stops at the first inequality. The names are downcased
|
|
|
|
// before the comparison.
|
|
|
|
//
|
|
|
|
// www.miek.nl. and miek.nl. have two labels in common: miek and nl
|
|
|
|
// www.miek.nl. and www.bla.nl. have one label in common: nl
|
|
|
|
//
|
|
|
|
// s1 and s2 must be syntactically valid domain names.
|
|
|
|
func CompareDomainName(s1, s2 string) (n int) {
|
|
|
|
// the first check: root label
|
|
|
|
if s1 == "." || s2 == "." {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
l1 := Split(s1)
|
|
|
|
l2 := Split(s2)
|
|
|
|
|
|
|
|
j1 := len(l1) - 1 // end
|
|
|
|
i1 := len(l1) - 2 // start
|
|
|
|
j2 := len(l2) - 1
|
|
|
|
i2 := len(l2) - 2
|
|
|
|
// the second check can be done here: last/only label
|
|
|
|
// before we fall through into the for-loop below
|
|
|
|
if equal(s1[l1[j1]:], s2[l2[j2]:]) {
|
|
|
|
n++
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
if i1 < 0 || i2 < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if equal(s1[l1[i1]:l1[j1]], s2[l2[i2]:l2[j2]]) {
|
|
|
|
n++
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
j1--
|
|
|
|
i1--
|
|
|
|
j2--
|
|
|
|
i2--
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
// CountLabel counts the number of labels in the string s.
|
2018-05-01 23:45:06 +00:00
|
|
|
// s must be a syntactically valid domain name.
|
|
|
|
func CountLabel(s string) (labels int) {
|
|
|
|
if s == "." {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
off := 0
|
|
|
|
end := false
|
|
|
|
for {
|
|
|
|
off, end = NextLabel(s, off)
|
|
|
|
labels++
|
|
|
|
if end {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split splits a name s into its label indexes.
|
|
|
|
// www.miek.nl. returns []int{0, 4, 9}, www.miek.nl also returns []int{0, 4, 9}.
|
|
|
|
// The root name (.) returns nil. Also see SplitDomainName.
|
|
|
|
// s must be a syntactically valid domain name.
|
|
|
|
func Split(s string) []int {
|
|
|
|
if s == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
idx := make([]int, 1, 3)
|
|
|
|
off := 0
|
|
|
|
end := false
|
|
|
|
|
|
|
|
for {
|
|
|
|
off, end = NextLabel(s, off)
|
|
|
|
if end {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
idx = append(idx, off)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextLabel returns the index of the start of the next label in the
|
2024-10-17 20:09:39 +00:00
|
|
|
// string s starting at offset. A negative offset will cause a panic.
|
2018-05-01 23:45:06 +00:00
|
|
|
// The bool end is true when the end of the string has been reached.
|
|
|
|
// Also see PrevLabel.
|
|
|
|
func NextLabel(s string, offset int) (i int, end bool) {
|
2020-02-21 15:53:11 +00:00
|
|
|
if s == "" {
|
|
|
|
return 0, true
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
for i = offset; i < len(s)-1; i++ {
|
2020-02-21 15:53:11 +00:00
|
|
|
if s[i] != '.' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
j := i - 1
|
|
|
|
for j >= 0 && s[j] == '\\' {
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j-i)%2 == 0 {
|
|
|
|
continue
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2020-02-21 15:53:11 +00:00
|
|
|
|
|
|
|
return i + 1, false
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
return i + 1, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrevLabel returns the index of the label when starting from the right and
|
|
|
|
// jumping n labels to the left.
|
|
|
|
// The bool start is true when the start of the string has been overshot.
|
|
|
|
// Also see NextLabel.
|
|
|
|
func PrevLabel(s string, n int) (i int, start bool) {
|
2020-02-21 15:53:11 +00:00
|
|
|
if s == "" {
|
|
|
|
return 0, true
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
if n == 0 {
|
|
|
|
return len(s), false
|
|
|
|
}
|
2020-02-21 15:53:11 +00:00
|
|
|
|
|
|
|
l := len(s) - 1
|
|
|
|
if s[l] == '.' {
|
|
|
|
l--
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2020-02-21 15:53:11 +00:00
|
|
|
|
|
|
|
for ; l >= 0 && n > 0; l-- {
|
|
|
|
if s[l] != '.' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
j := l - 1
|
|
|
|
for j >= 0 && s[j] == '\\' {
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j-l)%2 == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
n--
|
|
|
|
if n == 0 {
|
|
|
|
return l + 1, false
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2020-02-21 15:53:11 +00:00
|
|
|
|
|
|
|
return 0, n > 1
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// equal compares a and b while ignoring case. It returns true when equal otherwise false.
|
|
|
|
func equal(a, b string) bool {
|
|
|
|
// might be lifted into API function.
|
|
|
|
la := len(a)
|
|
|
|
lb := len(b)
|
|
|
|
if la != lb {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := la - 1; i >= 0; i-- {
|
|
|
|
ai := a[i]
|
|
|
|
bi := b[i]
|
|
|
|
if ai >= 'A' && ai <= 'Z' {
|
2019-04-17 17:15:55 +00:00
|
|
|
ai |= 'a' - 'A'
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
if bi >= 'A' && bi <= 'Z' {
|
2019-04-17 17:15:55 +00:00
|
|
|
bi |= 'a' - 'A'
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
if ai != bi {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|