Rename the Label* functions

SplitLabels -> SplitDomainName
CompareLabels -> CompareDomainName
LenLabels -> CountLabel
          -> NextLabel was added as a simple iterator-like function
This commit is contained in:
Miek Gieben 2013-06-22 07:21:15 +00:00
parent 74a0da119d
commit 38ea608d79
7 changed files with 53 additions and 30 deletions

View File

@ -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. // IsSubDomain checks if child is indeed a child of the parent.
func IsSubDomain(parent, child string) bool { func IsSubDomain(parent, child string) bool {
// Entire child is contained in parent // 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. // IsFqdn checks if a domain name is fully qualified.

View File

@ -82,8 +82,8 @@ func TestBailiwick(t *testing.T) {
for parent, child := range yes { for parent, child := range yes {
if !IsSubDomain(parent, child) { if !IsSubDomain(parent, child) {
t.Logf("%s should be child of %s\n", child, parent) t.Logf("%s should be child of %s\n", child, parent)
t.Logf("comparelabels %d", CompareLabels(parent, child)) t.Logf("comparelabels %d", CompareDomainName(parent, child))
t.Logf("lenlabels %d %d", LenLabels(parent), LenLabels(child)) t.Logf("lenlabels %d %d", CountLabel(parent), CountLabel(child))
t.Fail() t.Fail()
} }
} }
@ -97,8 +97,8 @@ func TestBailiwick(t *testing.T) {
for parent, child := range no { for parent, child := range no {
if IsSubDomain(parent, child) { if IsSubDomain(parent, child) {
t.Logf("%s should not be child of %s\n", child, parent) t.Logf("%s should not be child of %s\n", child, parent)
t.Logf("comparelabels %d", CompareLabels(parent, child)) t.Logf("comparelabels %d", CompareDomainName(parent, child))
t.Logf("lenlabels %d %d", LenLabels(parent), LenLabels(child)) t.Logf("lenlabels %d %d", CountLabel(parent), CountLabel(child))
t.Fail() t.Fail()
} }
} }

View File

@ -644,7 +644,7 @@ func rawSignatureData(rrset []RR, s *RRSIG) (buf []byte) {
for i, r := range rrset { for i, r := range rrset {
r1 := r.copy() r1 := r.copy()
r1.Header().Ttl = s.OrigTtl r1.Header().Ttl = s.OrigTtl
labels := SplitLabels(r1.Header().Name) labels := SplitDomainName(r1.Header().Name)
// 6.2. Canonical RR Form. (4) - wildcards // 6.2. Canonical RR Form. (4) - wildcards
if len(labels) > int(s.Labels) { if len(labels) > int(s.Labels) {
// Wildcard // Wildcard

View File

@ -6,10 +6,10 @@ package dns
// Holds a bunch of helper functions for dealing with labels. // 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"} // www.miek.nl. returns []string{"www", "miek", "nl"}
// The root label (.) returns nil. // The root label (.) returns nil.
func SplitLabels(s string) []string { func SplitDomainName(s string) []string {
idx := Split(s) idx := Split(s)
switch len(idx) { switch len(idx) {
case 0: case 0:
@ -30,14 +30,14 @@ func SplitLabels(s string) []string {
panic("dns: not reached") panic("dns: not reached")
} }
// CompareLabels compares the names s1 and s2 and // CompareDomainName compares the names s1 and s2 and
// returns how many labels they have in common starting from the right. // returns how many labels they have in common starting from the *right*.
// The comparison stops at the first inequality. The labels are not downcased // The comparison stops at the first inequality. The names are not downcased
// before the comparison. // before the comparison.
// //
// www.miek.nl. and miek.nl. have two labels in common: miek and nl // 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 // 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) s1 = Fqdn(s1)
s2 = Fqdn(s2) s2 = Fqdn(s2)
l1 := Split(s1) l1 := Split(s1)
@ -76,8 +76,28 @@ func CompareLabels(s1, s2 string) (n int) {
return return
} }
// LenLabels returns the number of labels in the string s // SplitLabels splits a domainname string into its labels.
func LenLabels(s string) (labels int) { // 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 == "." { if s == "." {
return return
} }
@ -91,7 +111,12 @@ func LenLabels(s string) (labels int) {
return 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. // 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 // string s. The bool end is true when the end of the string has been
// reached. // reached.
func NextLabel(s string, offset int) (i int, end bool) { 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 quote := false
for i = offset; i < len(s)-1; i++ { for i = offset; i < len(s)-1; i++ {
switch s[i] { switch s[i] {

View File

@ -8,34 +8,34 @@ import (
"testing" "testing"
) )
func TestCompareLabels(t *testing.T) { func TestCompareDomainName(t *testing.T) {
s1 := "www.miek.nl." s1 := "www.miek.nl."
s2 := "miek.nl." s2 := "miek.nl."
s3 := "www.bla.nl." s3 := "www.bla.nl."
s4 := "nl.www.bla." s4 := "nl.www.bla."
s5 := "nl" s5 := "nl"
if CompareLabels(s1, s2) != 2 { if CompareDomainName(s1, s2) != 2 {
t.Logf("%s with %s should be %d", s1, s2, 2) t.Logf("%s with %s should be %d", s1, s2, 2)
t.Fail() t.Fail()
} }
if CompareLabels(s1, s3) != 1 { if CompareDomainName(s1, s3) != 1 {
t.Logf("%s with %s should be %d", s1, s3, 1) t.Logf("%s with %s should be %d", s1, s3, 1)
t.Fail() t.Fail()
} }
if CompareLabels(s3, s4) != 0 { if CompareDomainName(s3, s4) != 0 {
t.Logf("%s with %s should be %d", s3, s4, 0) t.Logf("%s with %s should be %d", s3, s4, 0)
t.Fail() t.Fail()
} }
if CompareLabels(s1, s5) != 1 { if CompareDomainName(s1, s5) != 1 {
t.Logf("%s with %s should be %d", s1, s5, 1) t.Logf("%s with %s should be %d", s1, s5, 1)
t.Fail() t.Fail()
} }
if CompareLabels(s1, ".") != 0 { if CompareDomainName(s1, ".") != 0 {
t.Logf("%s with %s should be %d", s1, s5, 0) t.Logf("%s with %s should be %d", s1, s5, 0)
t.Fail() t.Fail()
} }
if CompareLabels(".", ".") != 0 { if CompareDomainName(".", ".") != 0 {
t.Logf("%s with %s should be %d", ".", ".", 0) t.Logf("%s with %s should be %d", ".", ".", 0)
t.Fail() t.Fail()
} }
@ -63,7 +63,7 @@ func TestSplit(t *testing.T) {
} }
} }
func TestLenLabels(t *testing.T) { func TestCountLabel(t *testing.T) {
labels := map[string]int{ labels := map[string]int{
"miek.nl": 2, "miek.nl": 2,
".": 0, ".": 0,
@ -74,7 +74,7 @@ func TestLenLabels(t *testing.T) {
`www\\.miek.nl`: 3, `www\\.miek.nl`: 3,
} }
for owner, lab := range labels { 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.Logf("%s should have %d labels, got %d\n", owner, lab, l)
t.Fail() t.Fail()
} }

2
msg.go
View File

@ -1446,7 +1446,7 @@ func (dns *Msg) Len() int {
func compressionHelper(c map[string]int, s string) { func compressionHelper(c map[string]int, s string) {
pref := "" pref := ""
lbs := SplitLabels(s) lbs := SplitDomainName(s)
for j := len(lbs) - 1; j >= 0; j-- { for j := len(lbs) - 1; j >= 0; j-- {
c[lbs[j]+"."+pref] = 1 + len(pref) + len(lbs[j]) c[lbs[j]+"."+pref] = 1 + len(pref) + len(lbs[j])
pref = lbs[j] + "." + pref pref = lbs[j] + "." + pref

View File

@ -67,7 +67,7 @@ func (rr *NSEC3) HashNames(domain string) {
// Implement the Match method of Denialer // Implement the Match method of Denialer
func (rr *NSEC3) Match(domain string) bool { 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 // Implement the Match method of Denialer
@ -105,8 +105,8 @@ func (rr *NSEC) MatchType(rrtype uint16) bool {
func (rr *NSEC3) Cover(domain string) bool { func (rr *NSEC3) Cover(domain string) bool {
hashdom := strings.ToUpper(HashName(domain, rr.Hash, rr.Iterations, rr.Salt)) hashdom := strings.ToUpper(HashName(domain, rr.Hash, rr.Iterations, rr.Salt))
nextdom := strings.ToUpper(rr.NextDomain) nextdom := strings.ToUpper(rr.NextDomain)
owner := strings.ToUpper(SplitLabels(rr.Header().Name)[0]) // The hashed part owner := strings.ToUpper(SplitDomainName(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 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 nextdomain equals the apex, it is considered The End. So in that case hashdom is always less then nextdomain
if hashdom > owner && nextdom == apex { if hashdom > owner && nextdom == apex {
return true return true