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
This commit is contained in:
Miek Gieben 2016-10-28 07:50:16 +01:00 committed by GitHub
parent 039596f319
commit ba26f47d5c
2 changed files with 28 additions and 1 deletions

View file

@ -1,6 +1,8 @@
package cache package cache
import ( import (
"io/ioutil"
"log"
"testing" "testing"
"time" "time"
@ -64,12 +66,30 @@ var cacheTestCases = []cacheTestCase{
}, },
in: test.Case{}, 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 { func cacheMsg(m *dns.Msg, tc cacheTestCase) *dns.Msg {
m.RecursionAvailable = tc.RecursionAvailable m.RecursionAvailable = tc.RecursionAvailable
m.AuthenticatedData = tc.AuthenticatedData m.AuthenticatedData = tc.AuthenticatedData
m.Authoritative = tc.Authoritative m.Authoritative = tc.Authoritative
m.Rcode = tc.Rcode
m.Truncated = tc.Truncated m.Truncated = tc.Truncated
m.Answer = tc.in.Answer m.Answer = tc.in.Answer
m.Ns = tc.in.Ns m.Ns = tc.in.Ns
@ -89,6 +109,8 @@ func newTestCache(ttl time.Duration) (*Cache, *ResponseWriter) {
func TestCache(t *testing.T) { func TestCache(t *testing.T) {
c, crr := newTestCache(maxTTL) c, crr := newTestCache(maxTTL)
log.SetOutput(ioutil.Discard)
for _, tc := range cacheTestCases { for _, tc := range cacheTestCases {
m := tc.in.Msg() m := tc.in.Msg()
m = cacheMsg(m, tc) m = cacheMsg(m, tc)

View file

@ -8,6 +8,7 @@ import (
) )
type item struct { type item struct {
Rcode int
Authoritative bool Authoritative bool
AuthenticatedData bool AuthenticatedData bool
RecursionAvailable bool RecursionAvailable bool
@ -21,6 +22,7 @@ type item struct {
func newItem(m *dns.Msg, d time.Duration) *item { func newItem(m *dns.Msg, d time.Duration) *item {
i := new(item) i := new(item)
i.Rcode = m.Rcode
i.Authoritative = m.Authoritative i.Authoritative = m.Authoritative
i.AuthenticatedData = m.AuthenticatedData i.AuthenticatedData = m.AuthenticatedData
i.RecursionAvailable = m.RecursionAvailable 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. // 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 { func (i *item) toMsg(m *dns.Msg) *dns.Msg {
m1 := new(dns.Msg) m1 := new(dns.Msg)
m1.SetReply(m) m1.SetReply(m)
m1.Authoritative = i.Authoritative
m1.Authoritative = false
m1.AuthenticatedData = i.AuthenticatedData m1.AuthenticatedData = i.AuthenticatedData
m1.RecursionAvailable = i.RecursionAvailable m1.RecursionAvailable = i.RecursionAvailable
m1.Rcode = i.Rcode
m1.Compress = true m1.Compress = true
m1.Answer = i.Answer m1.Answer = i.Answer