add closest encloser stuff

This commit is contained in:
Miek Gieben 2016-03-30 16:45:02 +00:00
parent 3838eabda4
commit bf6d90600b
6 changed files with 62 additions and 18 deletions

View file

@ -17,8 +17,11 @@ func Less(a, b string) int {
aj := len(a)
bj := len(b)
for {
ai, _ := dns.PrevLabel(a, i)
bi, _ := dns.PrevLabel(b, i)
ai, oka := dns.PrevLabel(a, i)
bi, okb := dns.PrevLabel(b, i)
if oka && okb {
return 0
}
// sadly this []byte will allocate...
ab := []byte(a[ai:aj])
toLowerAndDDD(ab)

View file

@ -0,0 +1,16 @@
package file
import "github.com/miekg/dns"
// ClosestEncloser returns the closest encloser for rr.
func (z *Zone) ClosestEncloser(rr dns.RR) string {
elem := z.Tree.Prev(rr)
if elem == nil {
// SOA?
return ""
}
for _, r := range elem.All() {
return r.Header().Name
}
return ""
}

View file

@ -0,0 +1,34 @@
package file
import (
"strings"
"testing"
"github.com/miekg/dns"
)
func TestClosestEncloser(t *testing.T) {
z, err := Parse(strings.NewReader(dbMiekNL), testzone, "stdin")
if err != nil {
t.Fatalf("expect no error when reading zone, got %q", err)
}
tests := []struct {
in, out string
}{
{"miek.nl.", "miek.nl."},
{"blaat.miek.nl.", "miek.nl."},
{"blaat.blaat.miek.nl.", "miek.nl."},
{"blaat.a.miek.nl.", "archive.miek.nl."},
}
mk, _ := dns.TypeToRR[dns.TypeA]
rr := mk()
for _, tc := range tests {
rr.Header().Name = tc.in
ce := z.ClosestEncloser(rr)
if ce != tc.out {
t.Errorf("expected ce to be %s for %s, got %s", tc.out, tc.in, ce)
}
}
}

View file

@ -61,9 +61,10 @@ var dnssecTestCases = []coretest.Case{
},
},
{
Qname: "b.miek.nl.", Qtype: dns.TypeA, // Do: true, // need sorting first
Qname: "b.miek.nl.", Qtype: dns.TypeA, Do: true,
Rcode: dns.RcodeNameError,
Ns: []dns.RR{
coretest.RRSIG("miek.nl. 1800 IN RRSIG SOA 8 2 1800 20160426031301 20160327031301 12051 miek.nl. FIrzy07acBbtyQczy1dc="),
coretest.SOA("miek.nl. 1800 IN SOA linode.atoom.net. miek.miek.nl. 1282630057 14400 3600 604800 14400"),
},
},

View file

@ -2,6 +2,7 @@ package file
import (
"github.com/miekg/coredns/middleware/file/tree"
"github.com/miekg/dns"
)

View file

@ -17,8 +17,7 @@ package tree
// TODO(miek): fix docs
import (
"strings"
"github.com/miekg/coredns/middleware"
"github.com/miekg/dns"
)
@ -112,21 +111,11 @@ func (e *Elem) Delete(rr dns.RR) (empty bool) {
return
}
// TODO(miek): need case ignore compare that is more efficient.
func Less(a *Elem, rr dns.RR) int {
aname := ""
for _, ar := range a.m {
aname = strings.ToLower(ar[0].Header().Name)
break
for _, ar := range a.m { // Get first element in a
return middleware.Less(ar[0].Header().Name, rr.Header().Name)
}
rname := strings.ToLower(rr.Header().Name)
if aname == rname {
return 0
}
if aname < rname {
return -1
}
return 1
}
// Assuming the same type and name this will check if the rdata is equal as well.