2013-05-13 00:15:52 +10:00
|
|
|
// Copyright 2011 Miek Gieben. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-01-11 01:55:52 +11:00
|
|
|
package dns
|
|
|
|
|
|
|
|
// Holds a bunch of helper functions for dealing with labels.
|
|
|
|
|
|
|
|
// SplitLabels splits a domainname string into its labels.
|
2012-02-12 21:34:28 +11:00
|
|
|
// www.miek.nl. returns []string{"www", "miek", "nl"}
|
2012-02-15 08:43:04 +11:00
|
|
|
// The root label (.) returns nil.
|
2012-01-11 01:55:52 +11:00
|
|
|
func SplitLabels(s string) []string {
|
2012-02-24 05:37:08 +11:00
|
|
|
if s == "." {
|
|
|
|
return nil
|
|
|
|
}
|
2012-02-15 08:26:18 +11:00
|
|
|
|
2012-01-11 01:55:52 +11:00
|
|
|
k := 0
|
2012-01-11 02:34:14 +11:00
|
|
|
labels := make([]string, 0)
|
2012-01-28 10:35:37 +11:00
|
|
|
last := byte('.')
|
|
|
|
lastlast := byte('.')
|
2012-01-11 02:34:14 +11:00
|
|
|
s = Fqdn(s) // Make fully qualified
|
2012-01-11 01:55:52 +11:00
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] == '.' {
|
2012-01-28 10:35:37 +11:00
|
|
|
if last == '\\' {
|
|
|
|
if lastlast != '\\' {
|
|
|
|
// do nothing
|
|
|
|
continue
|
|
|
|
}
|
2012-01-11 01:55:52 +11:00
|
|
|
}
|
2012-01-11 02:34:14 +11:00
|
|
|
labels = append(labels, s[k:i])
|
2012-01-11 01:55:52 +11:00
|
|
|
k = i + 1 // + dot
|
|
|
|
}
|
2012-01-28 10:35:37 +11:00
|
|
|
lastlast = last
|
|
|
|
last = s[i]
|
2012-01-11 01:55:52 +11:00
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompareLabels compares the strings s1 and s2 and
|
2012-01-11 02:04:32 +11:00
|
|
|
// returns how many labels they have in common starting from the right.
|
2012-09-05 04:08:55 +10:00
|
|
|
// The comparison stops at the first inequality. The labels are not downcased
|
|
|
|
// before the comparison.
|
2012-01-11 02:04:32 +11:00
|
|
|
//
|
2012-02-12 21:34:28 +11:00
|
|
|
// 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
|
2012-01-11 01:55:52 +11:00
|
|
|
func CompareLabels(s1, s2 string) (n int) {
|
|
|
|
l1 := SplitLabels(s1)
|
|
|
|
l2 := SplitLabels(s2)
|
|
|
|
|
2012-01-11 02:34:14 +11:00
|
|
|
x1 := len(l1) - 1
|
|
|
|
x2 := len(l2) - 1
|
2012-01-11 01:55:52 +11:00
|
|
|
for {
|
2012-01-11 02:34:14 +11:00
|
|
|
if x1 < 0 || x2 < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if l1[x1] == l2[x2] {
|
|
|
|
n++
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
x1--
|
|
|
|
x2--
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2012-07-16 16:51:39 +10:00
|
|
|
|
2013-06-21 00:24:14 +10:00
|
|
|
// lenLabels returns the number of labels in a domain name.
|
|
|
|
func lenLabels(s string) (labels int) {
|
2012-07-16 16:51:39 +10:00
|
|
|
if s == "." {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
last := byte('.')
|
|
|
|
lastlast := byte('.')
|
2013-06-21 00:24:14 +10:00
|
|
|
s = Fqdn(s)
|
2012-07-16 16:51:39 +10:00
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] == '.' {
|
|
|
|
if last == '\\' {
|
|
|
|
if lastlast != '\\' {
|
|
|
|
// do nothing
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
labels++
|
|
|
|
}
|
|
|
|
lastlast = last
|
|
|
|
last = s[i]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2013-06-20 16:49:18 +10:00
|
|
|
|
2013-06-21 00:24:14 +10:00
|
|
|
// LenLabels returns the number of labels in the string s
|
|
|
|
func LenLabels(s string) (labels int) {
|
|
|
|
if s == "." {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s = Fqdn(s)
|
|
|
|
off := 0
|
|
|
|
end := false
|
|
|
|
for {
|
|
|
|
off, end = nextLabel(s, off+1)
|
|
|
|
if end {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
labels++
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 16:49:18 +10:00
|
|
|
// NextLabel returns the index of the start of the next label in the
|
|
|
|
// string s. The bool end is true when the end of the string has been
|
|
|
|
// reached.
|
|
|
|
func nextLabel(s string, offset int) (i int, end bool) {
|
|
|
|
// The other label function are quite generous with memory,
|
|
|
|
// this one does not allocate.
|
|
|
|
quote := false
|
2013-06-21 00:24:14 +10:00
|
|
|
for i = offset; i < len(s); i++ {
|
2013-06-20 16:49:18 +10:00
|
|
|
switch s[i] {
|
|
|
|
case '\\':
|
2013-06-21 00:24:14 +10:00
|
|
|
quote = !quote
|
2013-06-20 16:49:18 +10:00
|
|
|
default:
|
|
|
|
quote = false
|
|
|
|
case '.':
|
|
|
|
if quote {
|
|
|
|
quote = !quote
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return i, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i, true
|
|
|
|
}
|