| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | package cache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/cache/freq"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/response"
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type item struct {
 | 
					
						
							| 
									
										
										
										
											2016-10-28 07:50:16 +01:00
										 |  |  | 	Rcode              int
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	Authoritative      bool
 | 
					
						
							|  |  |  | 	AuthenticatedData  bool
 | 
					
						
							|  |  |  | 	RecursionAvailable bool
 | 
					
						
							|  |  |  | 	Answer             []dns.RR
 | 
					
						
							|  |  |  | 	Ns                 []dns.RR
 | 
					
						
							|  |  |  | 	Extra              []dns.RR
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	origTTL uint32
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	stored  time.Time
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	*freq.Freq
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | func newItem(m *dns.Msg, now time.Time, d time.Duration) *item {
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	i := new(item)
 | 
					
						
							| 
									
										
										
										
											2016-10-28 07:50:16 +01:00
										 |  |  | 	i.Rcode = m.Rcode
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	i.Authoritative = m.Authoritative
 | 
					
						
							|  |  |  | 	i.AuthenticatedData = m.AuthenticatedData
 | 
					
						
							|  |  |  | 	i.RecursionAvailable = m.RecursionAvailable
 | 
					
						
							|  |  |  | 	i.Answer = m.Answer
 | 
					
						
							|  |  |  | 	i.Ns = m.Ns
 | 
					
						
							|  |  |  | 	i.Extra = make([]dns.RR, len(m.Extra))
 | 
					
						
							| 
									
										
										
										
											2018-05-08 18:36:08 +01:00
										 |  |  | 	// Don't copy OPT records as these are hop-by-hop.
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	j := 0
 | 
					
						
							|  |  |  | 	for _, e := range m.Extra {
 | 
					
						
							|  |  |  | 		if e.Header().Rrtype == dns.TypeOPT {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		i.Extra[j] = e
 | 
					
						
							|  |  |  | 		j++
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	i.Extra = i.Extra[:j]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	i.origTTL = uint32(d.Seconds())
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 	i.stored = now.UTC()
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	i.Freq = new(freq.Freq)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	return i
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | // toMsg turns i into a message, it tailors the reply to m.
 | 
					
						
							| 
									
										
										
										
											2016-11-13 14:03:12 +00:00
										 |  |  | // The Authoritative bit is always set to 0, because the answer is from the cache.
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | func (i *item) toMsg(m *dns.Msg, now time.Time) *dns.Msg {
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	m1 := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m1.SetReply(m)
 | 
					
						
							| 
									
										
										
										
											2016-10-28 07:50:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	m1.Authoritative = false
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	m1.AuthenticatedData = i.AuthenticatedData
 | 
					
						
							|  |  |  | 	m1.RecursionAvailable = i.RecursionAvailable
 | 
					
						
							| 
									
										
										
										
											2016-10-28 07:50:16 +01:00
										 |  |  | 	m1.Rcode = i.Rcode
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-26 07:44:25 -07:00
										 |  |  | 	m1.Answer = make([]dns.RR, len(i.Answer))
 | 
					
						
							|  |  |  | 	m1.Ns = make([]dns.RR, len(i.Ns))
 | 
					
						
							|  |  |  | 	m1.Extra = make([]dns.RR, len(i.Extra))
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 	ttl := uint32(i.ttl(now))
 | 
					
						
							| 
									
										
										
										
											2017-06-26 07:44:25 -07:00
										 |  |  | 	for j, r := range i.Answer {
 | 
					
						
							|  |  |  | 		m1.Answer[j] = dns.Copy(r)
 | 
					
						
							|  |  |  | 		m1.Answer[j].Header().Ttl = ttl
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-06-26 07:44:25 -07:00
										 |  |  | 	for j, r := range i.Ns {
 | 
					
						
							|  |  |  | 		m1.Ns[j] = dns.Copy(r)
 | 
					
						
							|  |  |  | 		m1.Ns[j].Header().Ttl = ttl
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-05-08 18:36:08 +01:00
										 |  |  | 	// newItem skips OPT records, so we can just use i.Extra as is.
 | 
					
						
							| 
									
										
										
										
											2017-06-26 07:44:25 -07:00
										 |  |  | 	for j, r := range i.Extra {
 | 
					
						
							|  |  |  | 		m1.Extra[j] = dns.Copy(r)
 | 
					
						
							| 
									
										
										
										
											2018-05-08 18:36:08 +01:00
										 |  |  | 		m1.Extra[j].Header().Ttl = ttl
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-06-26 07:44:25 -07:00
										 |  |  | 	return m1
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (i *item) ttl(now time.Time) int {
 | 
					
						
							|  |  |  | 	ttl := int(i.origTTL) - int(now.UTC().Sub(i.stored).Seconds())
 | 
					
						
							|  |  |  | 	return ttl
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | func minMsgTTL(m *dns.Msg, mt response.Type) time.Duration {
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	if mt != response.NoError && mt != response.NameError && mt != response.NoData {
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		return 0
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-17 19:45:52 +01:00
										 |  |  | 	// No data to examine, return a short ttl as a fail safe.
 | 
					
						
							| 
									
										
										
										
											2018-02-21 16:02:49 -05:00
										 |  |  | 	if len(m.Answer)+len(m.Ns)+len(m.Extra) == 0 {
 | 
					
						
							| 
									
										
										
										
											2018-02-17 19:45:52 +01:00
										 |  |  | 		return failSafeTTL
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	minTTL := maxTTL
 | 
					
						
							| 
									
										
										
										
											2018-05-08 18:36:29 +01:00
										 |  |  | 	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
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	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
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, r := range m.Extra {
 | 
					
						
							| 
									
										
										
										
											2018-02-21 16:02:49 -05:00
										 |  |  | 		if r.Header().Rrtype == dns.TypeOPT {
 | 
					
						
							|  |  |  | 			// OPT records use TTL field for extended rcode and flags
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		switch mt {
 | 
					
						
							|  |  |  | 		case response.NameError, response.NoData:
 | 
					
						
							|  |  |  | 			if r.Header().Rrtype == dns.TypeSOA {
 | 
					
						
							| 
									
										
										
										
											2018-05-08 18:36:29 +01:00
										 |  |  | 				minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		case response.NoError, response.Delegation:
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 			if r.Header().Ttl < uint32(minTTL.Seconds()) {
 | 
					
						
							|  |  |  | 				minTTL = time.Duration(r.Header().Ttl) * time.Second
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	return minTTL
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | }
 |