add closest encloser stuff
This commit is contained in:
parent
3838eabda4
commit
bf6d90600b
6 changed files with 62 additions and 18 deletions
|
@ -17,8 +17,11 @@ func Less(a, b string) int {
|
||||||
aj := len(a)
|
aj := len(a)
|
||||||
bj := len(b)
|
bj := len(b)
|
||||||
for {
|
for {
|
||||||
ai, _ := dns.PrevLabel(a, i)
|
ai, oka := dns.PrevLabel(a, i)
|
||||||
bi, _ := dns.PrevLabel(b, i)
|
bi, okb := dns.PrevLabel(b, i)
|
||||||
|
if oka && okb {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
// sadly this []byte will allocate...
|
// sadly this []byte will allocate...
|
||||||
ab := []byte(a[ai:aj])
|
ab := []byte(a[ai:aj])
|
||||||
toLowerAndDDD(ab)
|
toLowerAndDDD(ab)
|
||||||
|
|
16
middleware/file/closest.go
Normal file
16
middleware/file/closest.go
Normal 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 ""
|
||||||
|
}
|
34
middleware/file/closest_test.go
Normal file
34
middleware/file/closest_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,
|
Rcode: dns.RcodeNameError,
|
||||||
Ns: []dns.RR{
|
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"),
|
coretest.SOA("miek.nl. 1800 IN SOA linode.atoom.net. miek.miek.nl. 1282630057 14400 3600 604800 14400"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -2,6 +2,7 @@ package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/miekg/coredns/middleware/file/tree"
|
"github.com/miekg/coredns/middleware/file/tree"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,7 @@ package tree
|
||||||
// TODO(miek): fix docs
|
// TODO(miek): fix docs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"github.com/miekg/coredns/middleware"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -112,21 +111,11 @@ func (e *Elem) Delete(rr dns.RR) (empty bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(miek): need case ignore compare that is more efficient.
|
|
||||||
func Less(a *Elem, rr dns.RR) int {
|
func Less(a *Elem, rr dns.RR) int {
|
||||||
aname := ""
|
for _, ar := range a.m { // Get first element in a
|
||||||
for _, ar := range a.m {
|
return middleware.Less(ar[0].Header().Name, rr.Header().Name)
|
||||||
aname = strings.ToLower(ar[0].Header().Name)
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
rname := strings.ToLower(rr.Header().Name)
|
|
||||||
if aname == rname {
|
|
||||||
return 0
|
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.
|
// Assuming the same type and name this will check if the rdata is equal as well.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue