From ba26f47d5c65295342402d2c9996eb74884935e0 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 28 Oct 2016 07:50:16 +0100 Subject: [PATCH] middleware/caching (#360) Add the rcode to the cached item and use this when we synthesize the answer again. We could also infer the rcode from the reassembled message, but this seems easier and is only an integer. Also set the autoritative bit to 0 for all from-cache answers. Fixes 357 --- middleware/cache/cache_test.go | 22 ++++++++++++++++++++++ middleware/cache/item.go | 7 ++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/middleware/cache/cache_test.go b/middleware/cache/cache_test.go index 7e80c66e0..31aeaa8ac 100644 --- a/middleware/cache/cache_test.go +++ b/middleware/cache/cache_test.go @@ -1,6 +1,8 @@ package cache import ( + "io/ioutil" + "log" "testing" "time" @@ -64,12 +66,30 @@ var cacheTestCases = []cacheTestCase{ }, in: test.Case{}, }, + { + RecursionAvailable: true, Authoritative: true, + Case: test.Case{ + Rcode: dns.RcodeNameError, + Qname: "example.org.", Qtype: dns.TypeA, + Ns: []dns.RR{ + test.SOA("example.org. 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. 2016082540 7200 3600 1209600 3600"), + }, + }, + in: test.Case{ + Rcode: dns.RcodeNameError, + Qname: "example.org.", Qtype: dns.TypeA, + Ns: []dns.RR{ + test.SOA("example.org. 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. 2016082540 7200 3600 1209600 3600"), + }, + }, + }, } func cacheMsg(m *dns.Msg, tc cacheTestCase) *dns.Msg { m.RecursionAvailable = tc.RecursionAvailable m.AuthenticatedData = tc.AuthenticatedData m.Authoritative = tc.Authoritative + m.Rcode = tc.Rcode m.Truncated = tc.Truncated m.Answer = tc.in.Answer m.Ns = tc.in.Ns @@ -89,6 +109,8 @@ func newTestCache(ttl time.Duration) (*Cache, *ResponseWriter) { func TestCache(t *testing.T) { c, crr := newTestCache(maxTTL) + log.SetOutput(ioutil.Discard) + for _, tc := range cacheTestCases { m := tc.in.Msg() m = cacheMsg(m, tc) diff --git a/middleware/cache/item.go b/middleware/cache/item.go index d2cb79520..f2d05f0a0 100644 --- a/middleware/cache/item.go +++ b/middleware/cache/item.go @@ -8,6 +8,7 @@ import ( ) type item struct { + Rcode int Authoritative bool AuthenticatedData bool RecursionAvailable bool @@ -21,6 +22,7 @@ type item struct { func newItem(m *dns.Msg, d time.Duration) *item { i := new(item) + i.Rcode = m.Rcode i.Authoritative = m.Authoritative i.AuthenticatedData = m.AuthenticatedData i.RecursionAvailable = m.RecursionAvailable @@ -45,12 +47,15 @@ func newItem(m *dns.Msg, d time.Duration) *item { } // toMsg turns i into a message, it tailers the reply to m. +// The Autoritative bit is always set to 0, because the answer is from the cache. func (i *item) toMsg(m *dns.Msg) *dns.Msg { m1 := new(dns.Msg) m1.SetReply(m) - m1.Authoritative = i.Authoritative + + m1.Authoritative = false m1.AuthenticatedData = i.AuthenticatedData m1.RecursionAvailable = i.RecursionAvailable + m1.Rcode = i.Rcode m1.Compress = true m1.Answer = i.Answer