| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | package cache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware/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
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newItem(m *dns.Msg, d time.Duration) *item {
 | 
					
						
							|  |  |  | 	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))
 | 
					
						
							|  |  |  | 	// Don't copy OPT record as these are hop-by-hop.
 | 
					
						
							|  |  |  | 	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())
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	i.stored = time.Now().UTC()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return i
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | // toMsg turns i into a message, it tailers 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.
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | func (i *item) toMsg(m *dns.Msg) *dns.Msg {
 | 
					
						
							|  |  |  | 	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
										 |  |  | 	m1.Compress = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m1.Answer = i.Answer
 | 
					
						
							|  |  |  | 	m1.Ns = i.Ns
 | 
					
						
							|  |  |  | 	m1.Extra = i.Extra
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	ttl := int(i.origTTL) - int(time.Now().UTC().Sub(i.stored).Seconds())
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	setMsgTTL(m1, uint32(ttl))
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	return m1
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | func (i *item) expired(now time.Time) bool {
 | 
					
						
							|  |  |  | 	ttl := int(i.origTTL) - int(now.UTC().Sub(i.stored).Seconds())
 | 
					
						
							|  |  |  | 	return ttl < 0
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-09 10:01:26 +00:00
										 |  |  | // setMsgTTL sets the ttl on all RRs in all sections. If ttl is smaller than minTTL
 | 
					
						
							|  |  |  | // that value is used.
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | func setMsgTTL(m *dns.Msg, ttl uint32) {
 | 
					
						
							| 
									
										
										
										
											2016-11-09 10:01:26 +00:00
										 |  |  | 	if ttl < minTTL {
 | 
					
						
							|  |  |  | 		ttl = minTTL
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	for _, r := range m.Answer {
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		r.Header().Ttl = ttl
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, r := range m.Ns {
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		r.Header().Ttl = ttl
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, r := range m.Extra {
 | 
					
						
							| 
									
										
										
										
											2016-05-22 19:43:58 +01:00
										 |  |  | 		if r.Header().Rrtype == dns.TypeOPT {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		r.Header().Ttl = 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
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	minTTL := maxTTL
 | 
					
						
							|  |  |  | 	for _, r := range append(m.Answer, m.Ns...) {
 | 
					
						
							|  |  |  | 		switch mt {
 | 
					
						
							|  |  |  | 		case response.NameError, response.NoData:
 | 
					
						
							|  |  |  | 			if r.Header().Rrtype == dns.TypeSOA {
 | 
					
						
							|  |  |  | 				return time.Duration(r.(*dns.SOA).Minttl) * time.Second
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | }
 |