aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg
diff options
context:
space:
mode:
authorGravatar Ruslan Drozhdzh <30860269+rdrozhdzh@users.noreply.github.com> 2018-10-19 21:10:08 +0300
committerGravatar Chris O'Haver <cohaver@infoblox.com> 2018-10-19 14:10:08 -0400
commitdbc2efc49a0b4e8c0ba27d314328a0cf53332b02 (patch)
tree01b7498f9c78d36a0871e02f4202a799d8e8e5b5 /plugin/pkg
parent54df160aa4c0084f00ea95bf632f551df35770bb (diff)
downloadcoredns-dbc2efc49a0b4e8c0ba27d314328a0cf53332b02.tar.gz
coredns-dbc2efc49a0b4e8c0ba27d314328a0cf53332b02.tar.zst
coredns-dbc2efc49a0b4e8c0ba27d314328a0cf53332b02.zip
plugin/cache: fix TTL for negative DNS responses (#2197)
Diffstat (limited to 'plugin/pkg')
-rw-r--r--plugin/pkg/dnsutil/ttl.go38
-rw-r--r--plugin/pkg/dnsutil/ttl_test.go8
2 files changed, 13 insertions, 33 deletions
diff --git a/plugin/pkg/dnsutil/ttl.go b/plugin/pkg/dnsutil/ttl.go
index e969fa8a6..e2b26526d 100644
--- a/plugin/pkg/dnsutil/ttl.go
+++ b/plugin/pkg/dnsutil/ttl.go
@@ -14,34 +14,21 @@ func MinimalTTL(m *dns.Msg, mt response.Type) time.Duration {
return MinimalDefaultTTL
}
- // No data to examine, return a short ttl as a fail safe.
- if len(m.Answer)+len(m.Ns)+len(m.Extra) == 0 {
+ // No records or OPT is the only record, return a short ttl as a fail safe.
+ if len(m.Answer)+len(m.Ns) == 0 &&
+ (len(m.Extra) == 0 || (len(m.Extra) == 1 && m.Extra[0].Header().Rrtype == dns.TypeOPT)) {
return MinimalDefaultTTL
}
minTTL := MaximumDefaulTTL
for _, r := range m.Answer {
- switch mt {
- case response.NameError, response.NoData:
- if r.Header().Rrtype == dns.TypeSOA {
- minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
- }
- case response.NoError, response.Delegation:
- if r.Header().Ttl < uint32(minTTL.Seconds()) {
- minTTL = time.Duration(r.Header().Ttl) * time.Second
- }
+ if r.Header().Ttl < uint32(minTTL.Seconds()) {
+ minTTL = time.Duration(r.Header().Ttl) * time.Second
}
}
for _, r := range m.Ns {
- switch mt {
- case response.NameError, response.NoData:
- if r.Header().Rrtype == dns.TypeSOA {
- minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
- }
- case response.NoError, response.Delegation:
- if r.Header().Ttl < uint32(minTTL.Seconds()) {
- minTTL = time.Duration(r.Header().Ttl) * time.Second
- }
+ if r.Header().Ttl < uint32(minTTL.Seconds()) {
+ minTTL = time.Duration(r.Header().Ttl) * time.Second
}
}
@@ -50,15 +37,8 @@ func MinimalTTL(m *dns.Msg, mt response.Type) time.Duration {
// OPT records use TTL field for extended rcode and flags
continue
}
- switch mt {
- case response.NameError, response.NoData:
- if r.Header().Rrtype == dns.TypeSOA {
- minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
- }
- case response.NoError, response.Delegation:
- if r.Header().Ttl < uint32(minTTL.Seconds()) {
- minTTL = time.Duration(r.Header().Ttl) * time.Second
- }
+ if r.Header().Ttl < uint32(minTTL.Seconds()) {
+ minTTL = time.Duration(r.Header().Ttl) * time.Second
}
}
return minTTL
diff --git a/plugin/pkg/dnsutil/ttl_test.go b/plugin/pkg/dnsutil/ttl_test.go
index ee11d06f3..7dab65cb7 100644
--- a/plugin/pkg/dnsutil/ttl_test.go
+++ b/plugin/pkg/dnsutil/ttl_test.go
@@ -26,8 +26,8 @@ func TestMinimalTTL(t *testing.T) {
t.Fatalf("Expected type to be response.NoData, got %s", mt)
}
dur := MinimalTTL(m, mt) // minTTL on msg is 3600 (neg. ttl on SOA)
- if dur != time.Duration(3600*time.Second) {
- t.Fatalf("Expected minttl duration to be %d, got %d", 3600, dur)
+ if dur != time.Duration(1800*time.Second) {
+ t.Fatalf("Expected minttl duration to be %d, got %d", 1800, dur)
}
m.Rcode = dns.RcodeNameError
@@ -36,8 +36,8 @@ func TestMinimalTTL(t *testing.T) {
t.Fatalf("Expected type to be response.NameError, got %s", mt)
}
dur = MinimalTTL(m, mt) // minTTL on msg is 3600 (neg. ttl on SOA)
- if dur != time.Duration(3600*time.Second) {
- t.Fatalf("Expected minttl duration to be %d, got %d", 3600, dur)
+ if dur != time.Duration(1800*time.Second) {
+ t.Fatalf("Expected minttl duration to be %d, got %d", 1800, dur)
}
}