aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/response
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2017-09-14 09:36:06 +0100
committerGravatar GitHub <noreply@github.com> 2017-09-14 09:36:06 +0100
commitd8714e64e400ef873c2adc4d929a07d7890727b9 (patch)
treec9fa4c157e6af12eb1517654f8d23ca5d5619513 /plugin/pkg/response
parentb984aa45595dc95253b91191afe7d3ee29e71b48 (diff)
downloadcoredns-d8714e64e400ef873c2adc4d929a07d7890727b9.tar.gz
coredns-d8714e64e400ef873c2adc4d929a07d7890727b9.tar.zst
coredns-d8714e64e400ef873c2adc4d929a07d7890727b9.zip
Remove the word middleware (#1067)
* Rename middleware to plugin first pass; mostly used 'sed', few spots where I manually changed text. This still builds a coredns binary. * fmt error * Rename AddMiddleware to AddPlugin * Readd AddMiddleware to remain backwards compat
Diffstat (limited to 'plugin/pkg/response')
-rw-r--r--plugin/pkg/response/classify.go61
-rw-r--r--plugin/pkg/response/typify.go146
-rw-r--r--plugin/pkg/response/typify_test.go84
3 files changed, 291 insertions, 0 deletions
diff --git a/plugin/pkg/response/classify.go b/plugin/pkg/response/classify.go
new file mode 100644
index 000000000..2e705cb0b
--- /dev/null
+++ b/plugin/pkg/response/classify.go
@@ -0,0 +1,61 @@
+package response
+
+import "fmt"
+
+// Class holds sets of Types
+type Class int
+
+const (
+ // All is a meta class encompassing all the classes.
+ All Class = iota
+ // Success is a class for a successful response.
+ Success
+ // Denial is a class for denying existence (NXDOMAIN, or a nodata: type does not exist)
+ Denial
+ // Error is a class for errors, right now defined as not Success and not Denial
+ Error
+)
+
+func (c Class) String() string {
+ switch c {
+ case All:
+ return "all"
+ case Success:
+ return "success"
+ case Denial:
+ return "denial"
+ case Error:
+ return "error"
+ }
+ return ""
+}
+
+// ClassFromString returns the class from the string s. If not class matches
+// the All class and an error are returned
+func ClassFromString(s string) (Class, error) {
+ switch s {
+ case "all":
+ return All, nil
+ case "success":
+ return Success, nil
+ case "denial":
+ return Denial, nil
+ case "error":
+ return Error, nil
+ }
+ return All, fmt.Errorf("invalid Class: %s", s)
+}
+
+// Classify classifies the Type t, it returns its Class.
+func Classify(t Type) Class {
+ switch t {
+ case NoError, Delegation:
+ return Success
+ case NameError, NoData:
+ return Denial
+ case OtherError:
+ fallthrough
+ default:
+ return Error
+ }
+}
diff --git a/plugin/pkg/response/typify.go b/plugin/pkg/response/typify.go
new file mode 100644
index 000000000..7cfaab497
--- /dev/null
+++ b/plugin/pkg/response/typify.go
@@ -0,0 +1,146 @@
+package response
+
+import (
+ "fmt"
+ "time"
+
+ "github.com/miekg/dns"
+)
+
+// Type is the type of the message.
+type Type int
+
+const (
+ // NoError indicates a positive reply
+ NoError Type = iota
+ // NameError is a NXDOMAIN in header, SOA in auth.
+ NameError
+ // NoData indicates name found, but not the type: NOERROR in header, SOA in auth.
+ NoData
+ // Delegation is a msg with a pointer to another nameserver: NOERROR in header, NS in auth, optionally fluff in additional (not checked).
+ Delegation
+ // Meta indicates a meta message, NOTIFY, or a transfer: qType is IXFR or AXFR.
+ Meta
+ // Update is an dynamic update message.
+ Update
+ // OtherError indicates any other error: don't cache these.
+ OtherError
+)
+
+var toString = map[Type]string{
+ NoError: "NOERROR",
+ NameError: "NXDOMAIN",
+ NoData: "NODATA",
+ Delegation: "DELEGATION",
+ Meta: "META",
+ Update: "UPDATE",
+ OtherError: "OTHERERROR",
+}
+
+func (t Type) String() string { return toString[t] }
+
+// TypeFromString returns the type from the string s. If not type matches
+// the OtherError type and an error are returned.
+func TypeFromString(s string) (Type, error) {
+ for t, str := range toString {
+ if s == str {
+ return t, nil
+ }
+ }
+ return NoError, fmt.Errorf("invalid Type: %s", s)
+}
+
+// Typify classifies a message, it returns the Type.
+func Typify(m *dns.Msg, t time.Time) (Type, *dns.OPT) {
+ if m == nil {
+ return OtherError, nil
+ }
+ opt := m.IsEdns0()
+ do := false
+ if opt != nil {
+ do = opt.Do()
+ }
+
+ if m.Opcode == dns.OpcodeUpdate {
+ return Update, opt
+ }
+
+ // Check transfer and update first
+ if m.Opcode == dns.OpcodeNotify {
+ return Meta, opt
+ }
+
+ if len(m.Question) > 0 {
+ if m.Question[0].Qtype == dns.TypeAXFR || m.Question[0].Qtype == dns.TypeIXFR {
+ return Meta, opt
+ }
+ }
+
+ // If our message contains any expired sigs and we care about that, we should return expired
+ if do {
+ if expired := typifyExpired(m, t); expired {
+ return OtherError, opt
+ }
+ }
+
+ if len(m.Answer) > 0 && m.Rcode == dns.RcodeSuccess {
+ return NoError, 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 && m.Rcode == dns.RcodeSuccess {
+ return Delegation, opt
+ }
+
+ if m.Rcode == dns.RcodeSuccess {
+ return NoError, opt
+ }
+
+ return OtherError, opt
+}
+
+func typifyExpired(m *dns.Msg, t time.Time) bool {
+ if expired := typifyExpiredRRSIG(m.Answer, t); expired {
+ return true
+ }
+ if expired := typifyExpiredRRSIG(m.Ns, t); expired {
+ return true
+ }
+ if expired := typifyExpiredRRSIG(m.Extra, t); expired {
+ return true
+ }
+ return false
+}
+
+func typifyExpiredRRSIG(rrs []dns.RR, t time.Time) bool {
+ for _, r := range rrs {
+ if r.Header().Rrtype != dns.TypeRRSIG {
+ continue
+ }
+ ok := r.(*dns.RRSIG).ValidityPeriod(t)
+ if !ok {
+ return true
+ }
+ }
+ return false
+}
diff --git a/plugin/pkg/response/typify_test.go b/plugin/pkg/response/typify_test.go
new file mode 100644
index 000000000..faeaf3579
--- /dev/null
+++ b/plugin/pkg/response/typify_test.go
@@ -0,0 +1,84 @@
+package response
+
+import (
+ "testing"
+ "time"
+
+ "github.com/coredns/coredns/plugin/test"
+
+ "github.com/miekg/dns"
+)
+
+func TestTypifyNilMsg(t *testing.T) {
+ var m *dns.Msg
+
+ ty, _ := Typify(m, time.Now().UTC())
+ if ty != OtherError {
+ t.Errorf("message wrongly typified, expected OtherError, got %s", ty)
+ }
+}
+
+func TestTypifyDelegation(t *testing.T) {
+ m := delegationMsg()
+ mt, _ := Typify(m, time.Now().UTC())
+ if mt != Delegation {
+ t.Errorf("message is wrongly typified, expected Delegation, got %s", mt)
+ }
+}
+
+func TestTypifyRRSIG(t *testing.T) {
+ now, _ := time.Parse(time.UnixDate, "Fri Apr 21 10:51:21 BST 2017")
+ utc := now.UTC()
+
+ m := delegationMsgRRSIGOK()
+ if mt, _ := Typify(m, utc); mt != Delegation {
+ t.Errorf("message is wrongly typified, expected Delegation, got %s", mt)
+ }
+
+ // Still a Delegation because EDNS0 OPT DO bool is not set, so we won't check the sigs.
+ m = delegationMsgRRSIGFail()
+ if mt, _ := Typify(m, utc); mt != Delegation {
+ t.Errorf("message is wrongly typified, expected Delegation, got %s", mt)
+ }
+
+ m = delegationMsgRRSIGFail()
+ m = addOpt(m)
+ if mt, _ := Typify(m, utc); mt != OtherError {
+ t.Errorf("message is wrongly typified, expected OtherError, got %s", 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"),
+ },
+ }
+}
+
+func delegationMsgRRSIGOK() *dns.Msg {
+ del := delegationMsg()
+ del.Ns = append(del.Ns,
+ test.RRSIG("miek.nl. 1800 IN RRSIG NS 8 2 1800 20170521031301 20170421031301 12051 miek.nl. PIUu3TKX/sB/N1n1E1yWxHHIcPnc2q6Wq9InShk+5ptRqChqKdZNMLDm gCq+1bQAZ7jGvn2PbwTwE65JzES7T+hEiqR5PU23DsidvZyClbZ9l0xG JtKwgzGXLtUHxp4xv/Plq+rq/7pOG61bNCxRyS7WS7i7QcCCWT1BCcv+ wZ0="),
+ )
+ return del
+}
+
+func delegationMsgRRSIGFail() *dns.Msg {
+ del := delegationMsg()
+ del.Ns = append(del.Ns,
+ test.RRSIG("miek.nl. 1800 IN RRSIG NS 8 2 1800 20160521031301 20160421031301 12051 miek.nl. PIUu3TKX/sB/N1n1E1yWxHHIcPnc2q6Wq9InShk+5ptRqChqKdZNMLDm gCq+1bQAZ7jGvn2PbwTwE65JzES7T+hEiqR5PU23DsidvZyClbZ9l0xG JtKwgzGXLtUHxp4xv/Plq+rq/7pOG61bNCxRyS7WS7i7QcCCWT1BCcv+ wZ0="),
+ )
+ return del
+}
+
+func addOpt(m *dns.Msg) *dns.Msg {
+ m.Extra = append(m.Extra, test.OPT(4096, true))
+ return m
+}