mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 02:33:14 -04:00
Cleanup: put middleware helper functions in pkgs (#245)
Move all (almost all) Go files in middleware into their own packages. This makes for better naming and discoverability. Lot of changes elsewhere to make this change. The middleware.State was renamed to request.Request which is better, but still does not cover all use-cases. It was also moved out middleware because it is used by `dnsserver` as well. A pkg/dnsutil packages was added for shared, handy, dns util functions. All normalize functions are now put in normalize.go
This commit is contained in:
52
middleware/pkg/response/classify.go
Normal file
52
middleware/pkg/response/classify.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package response
|
||||
|
||||
import "github.com/miekg/dns"
|
||||
|
||||
type Type int
|
||||
|
||||
const (
|
||||
Success Type = iota
|
||||
NameError // NXDOMAIN in header, SOA in auth.
|
||||
NoData // NOERROR in header, SOA in auth.
|
||||
Delegation // NOERROR in header, NS in auth, optionally fluff in additional (not checked).
|
||||
OtherError // Don't cache these.
|
||||
)
|
||||
|
||||
// Classify classifies a message, it returns the Type.
|
||||
func Classify(m *dns.Msg) (Type, *dns.OPT) {
|
||||
opt := m.IsEdns0()
|
||||
|
||||
if len(m.Answer) > 0 && m.Rcode == dns.RcodeSuccess {
|
||||
return Success, opt
|
||||
}
|
||||
|
||||
soa := false
|
||||
ns := 0
|
||||
for _, r := range m.Ns {
|
||||
if r.Header().Rrtype == dns.TypeSOA {
|
||||
soa = true
|
||||
continue
|
||||
}
|
||||
if r.Header().Rrtype == dns.TypeNS {
|
||||
ns++
|
||||
}
|
||||
}
|
||||
|
||||
// Check length of different sections, and drop stuff that is just to large? TODO(miek).
|
||||
if soa && m.Rcode == dns.RcodeSuccess {
|
||||
return NoData, opt
|
||||
}
|
||||
if soa && m.Rcode == dns.RcodeNameError {
|
||||
return NameError, opt
|
||||
}
|
||||
|
||||
if ns > 0 && ns == len(m.Ns) && m.Rcode == dns.RcodeSuccess {
|
||||
return Delegation, opt
|
||||
}
|
||||
|
||||
if m.Rcode == dns.RcodeSuccess {
|
||||
return Success, opt
|
||||
}
|
||||
|
||||
return OtherError, opt
|
||||
}
|
||||
31
middleware/pkg/response/classify_test.go
Normal file
31
middleware/pkg/response/classify_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/coredns/middleware/test"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func TestClassifyDelegation(t *testing.T) {
|
||||
m := delegationMsg()
|
||||
mt, _ := Classify(m)
|
||||
if mt != Delegation {
|
||||
t.Errorf("message is wrongly classified, expected delegation, got %d", mt)
|
||||
}
|
||||
}
|
||||
|
||||
func delegationMsg() *dns.Msg {
|
||||
return &dns.Msg{
|
||||
Ns: []dns.RR{
|
||||
test.NS("miek.nl. 3600 IN NS linode.atoom.net."),
|
||||
test.NS("miek.nl. 3600 IN NS ns-ext.nlnetlabs.nl."),
|
||||
test.NS("miek.nl. 3600 IN NS omval.tednet.nl."),
|
||||
},
|
||||
Extra: []dns.RR{
|
||||
test.A("omval.tednet.nl. 3600 IN A 185.49.141.42"),
|
||||
test.AAAA("omval.tednet.nl. 3600 IN AAAA 2a04:b900:0:100::42"),
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user