aboutsummaryrefslogtreecommitdiff
path: root/middleware/pkg/response/classify.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2016-09-07 11:10:16 +0100
committerGravatar GitHub <noreply@github.com> 2016-09-07 11:10:16 +0100
commitd1f17fa7e061d91aa0af7e1fb959a68db899c812 (patch)
tree0f828a0e7bd8ca86051a721dae48e8c19a0a2f0e /middleware/pkg/response/classify.go
parent684330fd28f65a7a8d1ad01fb4c6c5db78240f29 (diff)
downloadcoredns-d1f17fa7e061d91aa0af7e1fb959a68db899c812.tar.gz
coredns-d1f17fa7e061d91aa0af7e1fb959a68db899c812.tar.zst
coredns-d1f17fa7e061d91aa0af7e1fb959a68db899c812.zip
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
Diffstat (limited to 'middleware/pkg/response/classify.go')
-rw-r--r--middleware/pkg/response/classify.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/middleware/pkg/response/classify.go b/middleware/pkg/response/classify.go
new file mode 100644
index 000000000..adbaa6526
--- /dev/null
+++ b/middleware/pkg/response/classify.go
@@ -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
+}