diff --git a/defaults.go b/defaults.go index 87da6054..1b870808 100644 --- a/defaults.go +++ b/defaults.go @@ -236,7 +236,7 @@ func IsDomainName(s string) (uint8, uint8, bool) { // copied from net package. // IsSubDomain checks if child is indeed a child of the parent. func IsSubDomain(parent, child string) bool { // Entire child is contained in parent - return CompareLabels(strings.ToLower(parent), strings.ToLower(child)) == LenLabels(parent) + return CompareDomainName(strings.ToLower(parent), strings.ToLower(child)) == CountLabel(parent) } // IsFqdn checks if a domain name is fully qualified. diff --git a/dns_test.go b/dns_test.go index df3d48eb..dd11156e 100644 --- a/dns_test.go +++ b/dns_test.go @@ -82,8 +82,8 @@ func TestBailiwick(t *testing.T) { for parent, child := range yes { if !IsSubDomain(parent, child) { t.Logf("%s should be child of %s\n", child, parent) - t.Logf("comparelabels %d", CompareLabels(parent, child)) - t.Logf("lenlabels %d %d", LenLabels(parent), LenLabels(child)) + t.Logf("comparelabels %d", CompareDomainName(parent, child)) + t.Logf("lenlabels %d %d", CountLabel(parent), CountLabel(child)) t.Fail() } } @@ -97,8 +97,8 @@ func TestBailiwick(t *testing.T) { for parent, child := range no { if IsSubDomain(parent, child) { t.Logf("%s should not be child of %s\n", child, parent) - t.Logf("comparelabels %d", CompareLabels(parent, child)) - t.Logf("lenlabels %d %d", LenLabels(parent), LenLabels(child)) + t.Logf("comparelabels %d", CompareDomainName(parent, child)) + t.Logf("lenlabels %d %d", CountLabel(parent), CountLabel(child)) t.Fail() } } diff --git a/dnssec.go b/dnssec.go index ec27bf23..cde778a7 100644 --- a/dnssec.go +++ b/dnssec.go @@ -644,7 +644,7 @@ func rawSignatureData(rrset []RR, s *RRSIG) (buf []byte) { for i, r := range rrset { r1 := r.copy() r1.Header().Ttl = s.OrigTtl - labels := SplitLabels(r1.Header().Name) + labels := SplitDomainName(r1.Header().Name) // 6.2. Canonical RR Form. (4) - wildcards if len(labels) > int(s.Labels) { // Wildcard diff --git a/labels.go b/labels.go index 982217c0..9dfe6ff2 100644 --- a/labels.go +++ b/labels.go @@ -6,10 +6,10 @@ package dns // Holds a bunch of helper functions for dealing with labels. -// SplitLabels splits a domainname string into its labels. +// SplitDomainName splits a name string into it's labels. // www.miek.nl. returns []string{"www", "miek", "nl"} // The root label (.) returns nil. -func SplitLabels(s string) []string { +func SplitDomainName(s string) []string { idx := Split(s) switch len(idx) { case 0: @@ -30,14 +30,14 @@ func SplitLabels(s string) []string { panic("dns: not reached") } -// CompareLabels 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 labels are not downcased +// 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 not 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 -func CompareLabels(s1, s2 string) (n int) { +func CompareDomainName(s1, s2 string) (n int) { s1 = Fqdn(s1) s2 = Fqdn(s2) l1 := Split(s1) @@ -76,8 +76,28 @@ func CompareLabels(s1, s2 string) (n int) { return } -// LenLabels returns the number of labels in the string s -func LenLabels(s string) (labels int) { +// SplitLabels splits a domainname string into its labels. +// www.miek.nl. returns []string{"www", "miek", "nl"} +// The root label (.) returns nil. +func SplitLabels(s string) []string { + println("SplitLabels is to be removed in future versions, for the better named SplitDomainName") + return SplitDomainName(s) +} + +// CompareLabels 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 labels are not 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 +func CompareLabels(s1, s2 string) (n int) { + println("CompareLabels is to be removed in future versions, for better named CompareDomainName") + return CompareDomainName(s1, s2) +} + +// CountLabel counts the the number of labels in the string s. +func CountLabel(s string) (labels int) { if s == "." { return } @@ -91,7 +111,12 @@ func LenLabels(s string) (labels int) { return } } +} +// LenLabels returns the number of labels in the string s. +func LenLabels(s string) int { + println("LenLabels is to be removed in future versions, for the better named CountLabel") + return CountLabel(s) } // Split splits a name s into its label indexes. @@ -118,8 +143,6 @@ func Split(s string) []int { // 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 for i = offset; i < len(s)-1; i++ { switch s[i] { diff --git a/labels_test.go b/labels_test.go index 638de739..525ed420 100644 --- a/labels_test.go +++ b/labels_test.go @@ -8,34 +8,34 @@ import ( "testing" ) -func TestCompareLabels(t *testing.T) { +func TestCompareDomainName(t *testing.T) { s1 := "www.miek.nl." s2 := "miek.nl." s3 := "www.bla.nl." s4 := "nl.www.bla." s5 := "nl" - if CompareLabels(s1, s2) != 2 { + if CompareDomainName(s1, s2) != 2 { t.Logf("%s with %s should be %d", s1, s2, 2) t.Fail() } - if CompareLabels(s1, s3) != 1 { + if CompareDomainName(s1, s3) != 1 { t.Logf("%s with %s should be %d", s1, s3, 1) t.Fail() } - if CompareLabels(s3, s4) != 0 { + if CompareDomainName(s3, s4) != 0 { t.Logf("%s with %s should be %d", s3, s4, 0) t.Fail() } - if CompareLabels(s1, s5) != 1 { + if CompareDomainName(s1, s5) != 1 { t.Logf("%s with %s should be %d", s1, s5, 1) t.Fail() } - if CompareLabels(s1, ".") != 0 { + if CompareDomainName(s1, ".") != 0 { t.Logf("%s with %s should be %d", s1, s5, 0) t.Fail() } - if CompareLabels(".", ".") != 0 { + if CompareDomainName(".", ".") != 0 { t.Logf("%s with %s should be %d", ".", ".", 0) t.Fail() } @@ -49,7 +49,7 @@ func TestSplit(t *testing.T) { `www\\.miek.nl.`: 3, ".": 0, "nl.": 1, - "nl": 1, + "nl": 1, "com.": 1, ".com.": 2, } @@ -63,7 +63,7 @@ func TestSplit(t *testing.T) { } } -func TestLenLabels(t *testing.T) { +func TestCountLabel(t *testing.T) { labels := map[string]int{ "miek.nl": 2, ".": 0, @@ -74,7 +74,7 @@ func TestLenLabels(t *testing.T) { `www\\.miek.nl`: 3, } for owner, lab := range labels { - if l := LenLabels(owner); l != lab { + if l := CountLabel(owner); l != lab { t.Logf("%s should have %d labels, got %d\n", owner, lab, l) t.Fail() } diff --git a/msg.go b/msg.go index dcc7d267..b3a68f1b 100644 --- a/msg.go +++ b/msg.go @@ -1446,7 +1446,7 @@ func (dns *Msg) Len() int { func compressionHelper(c map[string]int, s string) { pref := "" - lbs := SplitLabels(s) + lbs := SplitDomainName(s) for j := len(lbs) - 1; j >= 0; j-- { c[lbs[j]+"."+pref] = 1 + len(pref) + len(lbs[j]) pref = lbs[j] + "." + pref diff --git a/nsecx.go b/nsecx.go index b832873b..c12545c1 100644 --- a/nsecx.go +++ b/nsecx.go @@ -67,7 +67,7 @@ func (rr *NSEC3) HashNames(domain string) { // Implement the Match method of Denialer func (rr *NSEC3) Match(domain string) bool { - return strings.ToUpper(SplitLabels(rr.Header().Name)[0]) == strings.ToUpper(HashName(domain, rr.Hash, rr.Iterations, rr.Salt)) + return strings.ToUpper(SplitDomainName(rr.Header().Name)[0]) == strings.ToUpper(HashName(domain, rr.Hash, rr.Iterations, rr.Salt)) } // Implement the Match method of Denialer @@ -105,8 +105,8 @@ func (rr *NSEC) MatchType(rrtype uint16) bool { func (rr *NSEC3) Cover(domain string) bool { hashdom := strings.ToUpper(HashName(domain, rr.Hash, rr.Iterations, rr.Salt)) nextdom := strings.ToUpper(rr.NextDomain) - owner := strings.ToUpper(SplitLabels(rr.Header().Name)[0]) // The hashed part - apex := strings.ToUpper(HashName(strings.Join(SplitLabels(rr.Header().Name)[1:], "."), rr.Hash, rr.Iterations, rr.Salt)) + "." // The name of the zone + owner := strings.ToUpper(SplitDomainName(rr.Header().Name)[0]) // The hashed part + apex := strings.ToUpper(HashName(strings.Join(SplitDomainName(rr.Header().Name)[1:], "."), rr.Hash, rr.Iterations, rr.Salt)) + "." // The name of the zone // if nextdomain equals the apex, it is considered The End. So in that case hashdom is always less then nextdomain if hashdom > owner && nextdom == apex { return true