-
Notifications
You must be signed in to change notification settings - Fork 12
/
ttl.go
53 lines (46 loc) · 1.2 KB
/
ttl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package redis
import (
"time"
"github.com/coredns/coredns/plugin/pkg/response"
"github.com/miekg/dns"
)
func minMsgTTL(m *dns.Msg, mt response.Type) time.Duration {
if mt != response.NoError && mt != response.NameError && mt != response.NoData {
return 0
}
// No data to examine, return a short ttl as a fail safe.
if len(m.Answer)+len(m.Ns) == 0 {
return failSafeTTL
}
minTTL := maxTTL
for _, r := range append(append(m.Answer, m.Ns...), m.Extra...) {
if r.Header().Rrtype == dns.TypeOPT {
// OPT records use TTL field for extended rcode and flags
continue
}
switch mt {
case response.NameError, response.NoData:
if r.Header().Rrtype == dns.TypeSOA {
return 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
}
}
}
return minTTL
}
func msgTTL(m *dns.Msg, ttl int) {
for i := range m.Answer {
m.Answer[i].Header().Ttl = uint32(ttl)
}
for i := range m.Ns {
m.Ns[i].Header().Ttl = uint32(ttl)
}
for i := range m.Extra {
if m.Extra[i].Header().Rrtype != dns.TypeOPT {
m.Extra[i].Header().Ttl = uint32(ttl)
}
}
}