| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | // Package cache implements a cache. | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | package cache | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	"hash/fnv" | 
					
						
							| 
									
										
										
										
											2018-06-19 20:50:08 +02:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/cache" | 
					
						
							| 
									
										
										
										
											2018-06-27 21:12:27 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnsutil" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/response" | 
					
						
							| 
									
										
										
										
											2018-03-25 17:11:10 +01:00
										 |  |  | 	"github.com/coredns/coredns/request" | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 20:19:45 +08:00
										 |  |  | // Cache is a plugin that looks up responses in a cache and caches replies. | 
					
						
							| 
									
										
										
										
											2016-10-08 15:12:28 +01:00
										 |  |  | // It has a success and a denial of existence cache. | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | type Cache struct { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	Next  plugin.Handler | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	Zones []string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 12:10:30 -05:00
										 |  |  | 	zonesMetricLabel string | 
					
						
							| 
									
										
										
										
											2022-09-08 14:56:27 -04:00
										 |  |  | 	viewMetricLabel  string | 
					
						
							| 
									
										
										
										
											2022-02-14 12:10:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 14:26:02 -05:00
										 |  |  | 	ncache  *cache.Cache | 
					
						
							|  |  |  | 	ncap    int | 
					
						
							|  |  |  | 	nttl    time.Duration | 
					
						
							|  |  |  | 	minnttl time.Duration | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 14:26:02 -05:00
										 |  |  | 	pcache  *cache.Cache | 
					
						
							|  |  |  | 	pcap    int | 
					
						
							|  |  |  | 	pttl    time.Duration | 
					
						
							|  |  |  | 	minpttl time.Duration | 
					
						
							| 
									
										
										
										
											2022-06-17 15:48:57 -04:00
										 |  |  | 	failttl time.Duration // TTL for caching SERVFAIL responses | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Prefetch. | 
					
						
							|  |  |  | 	prefetch   int | 
					
						
							|  |  |  | 	duration   time.Duration | 
					
						
							|  |  |  | 	percentage int | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:16:33 +02:00
										 |  |  | 	// Stale serve | 
					
						
							|  |  |  | 	staleUpTo   time.Duration | 
					
						
							|  |  |  | 	verifyStale bool | 
					
						
							| 
									
										
										
										
											2019-11-29 11:17:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-28 10:51:08 -04:00
										 |  |  | 	// Positive/negative zone exceptions | 
					
						
							|  |  |  | 	pexcept []string | 
					
						
							|  |  |  | 	nexcept []string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 17:35:24 +01:00
										 |  |  | 	// Keep ttl option | 
					
						
							|  |  |  | 	keepttl bool | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 	// Testing. | 
					
						
							|  |  |  | 	now func() time.Time | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // New returns an initialized Cache with default settings. It's up to the | 
					
						
							|  |  |  | // caller to set the Next handler. | 
					
						
							|  |  |  | func New() *Cache { | 
					
						
							|  |  |  | 	return &Cache{ | 
					
						
							|  |  |  | 		Zones:      []string{"."}, | 
					
						
							|  |  |  | 		pcap:       defaultCap, | 
					
						
							|  |  |  | 		pcache:     cache.New(defaultCap), | 
					
						
							|  |  |  | 		pttl:       maxTTL, | 
					
						
							| 
									
										
										
										
											2018-09-03 14:26:02 -05:00
										 |  |  | 		minpttl:    minTTL, | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 		ncap:       defaultCap, | 
					
						
							|  |  |  | 		ncache:     cache.New(defaultCap), | 
					
						
							|  |  |  | 		nttl:       maxNTTL, | 
					
						
							| 
									
										
										
										
											2018-09-03 14:26:02 -05:00
										 |  |  | 		minnttl:    minNTTL, | 
					
						
							| 
									
										
										
										
											2022-06-17 15:48:57 -04:00
										 |  |  | 		failttl:    minNTTL, | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 		prefetch:   0, | 
					
						
							|  |  |  | 		duration:   1 * time.Minute, | 
					
						
							|  |  |  | 		percentage: 10, | 
					
						
							|  |  |  | 		now:        time.Now, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-29 15:13:39 +00:00
										 |  |  | // key returns key under which we store the item, -1 will be returned if we don't store the message. | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | // Currently we do not cache Truncated, errors zone transfers or dynamic update messages. | 
					
						
							| 
									
										
										
										
											2018-10-29 15:13:39 +00:00
										 |  |  | // qname holds the already lowercased qname. | 
					
						
							| 
									
										
										
										
											2023-11-10 07:00:47 -08:00
										 |  |  | func key(qname string, m *dns.Msg, t response.Type, do, cd bool) (bool, uint64) { | 
					
						
							| 
									
										
										
										
											2016-11-07 16:27:50 +00:00
										 |  |  | 	// We don't store truncated responses. | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	if m.Truncated { | 
					
						
							| 
									
										
										
										
											2018-08-31 17:26:43 -04:00
										 |  |  | 		return false, 0 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-17 16:28:43 +02:00
										 |  |  | 	// Nor errors or Meta or Update. | 
					
						
							| 
									
										
										
										
											2016-11-07 16:27:50 +00:00
										 |  |  | 	if t == response.OtherError || t == response.Meta || t == response.Update { | 
					
						
							| 
									
										
										
										
											2018-08-31 17:26:43 -04:00
										 |  |  | 		return false, 0 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-10 07:00:47 -08:00
										 |  |  | 	return true, hash(qname, m.Question[0].Qtype, do, cd) | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-21 09:29:04 -06:00
										 |  |  | var one = []byte("1") | 
					
						
							|  |  |  | var zero = []byte("0") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-10 07:00:47 -08:00
										 |  |  | func hash(qname string, qtype uint16, do, cd bool) uint64 { | 
					
						
							| 
									
										
										
										
											2018-08-31 17:26:43 -04:00
										 |  |  | 	h := fnv.New64() | 
					
						
							| 
									
										
										
										
											2022-10-21 09:29:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if do { | 
					
						
							|  |  |  | 		h.Write(one) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		h.Write(zero) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-10 07:00:47 -08:00
										 |  |  | 	if cd { | 
					
						
							|  |  |  | 		h.Write(one) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		h.Write(zero) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-29 15:13:39 +00:00
										 |  |  | 	h.Write([]byte{byte(qtype >> 8)}) | 
					
						
							|  |  |  | 	h.Write([]byte{byte(qtype)}) | 
					
						
							|  |  |  | 	h.Write([]byte(qname)) | 
					
						
							| 
									
										
										
										
											2018-08-31 17:26:43 -04:00
										 |  |  | 	return h.Sum64() | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 14:26:02 -05:00
										 |  |  | func computeTTL(msgTTL, minTTL, maxTTL time.Duration) time.Duration { | 
					
						
							|  |  |  | 	ttl := msgTTL | 
					
						
							|  |  |  | 	if ttl < minTTL { | 
					
						
							|  |  |  | 		ttl = minTTL | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ttl > maxTTL { | 
					
						
							|  |  |  | 		ttl = maxTTL | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ttl | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // ResponseWriter is a response writer that caches the reply message. | 
					
						
							|  |  |  | type ResponseWriter struct { | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	dns.ResponseWriter | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	*Cache | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	state  request.Request | 
					
						
							|  |  |  | 	server string // Server handling the request. | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-14 04:11:22 +02:00
										 |  |  | 	do         bool // When true the original request had the DO bit set. | 
					
						
							| 
									
										
										
										
											2023-11-10 07:00:47 -08:00
										 |  |  | 	cd         bool // When true the original request had the CD bit set. | 
					
						
							| 
									
										
										
										
											2022-06-17 15:47:35 -04:00
										 |  |  | 	ad         bool // When true the original request had the AD bit set. | 
					
						
							| 
									
										
										
										
											2018-06-19 20:50:08 +02:00
										 |  |  | 	prefetch   bool // When true write nothing back to the client. | 
					
						
							|  |  |  | 	remoteAddr net.Addr | 
					
						
							| 
									
										
										
										
											2022-07-07 17:07:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	wildcardFunc func() string // function to retrieve wildcard name that synthesized the result. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-28 10:51:08 -04:00
										 |  |  | 	pexcept []string // positive zone exceptions | 
					
						
							|  |  |  | 	nexcept []string // negative zone exceptions | 
					
						
							| 
									
										
										
										
											2018-06-19 20:50:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // newPrefetchResponseWriter returns a Cache ResponseWriter to be used in | 
					
						
							|  |  |  | // prefetch requests. It ensures RemoteAddr() can be called even after the | 
					
						
							| 
									
										
										
										
											2019-02-17 15:31:12 +07:00
										 |  |  | // original connection has already been closed. | 
					
						
							| 
									
										
										
										
											2018-06-19 20:50:08 +02:00
										 |  |  | func newPrefetchResponseWriter(server string, state request.Request, c *Cache) *ResponseWriter { | 
					
						
							|  |  |  | 	// Resolve the address now, the connection might be already closed when the | 
					
						
							|  |  |  | 	// actual prefetch request is made. | 
					
						
							|  |  |  | 	addr := state.W.RemoteAddr() | 
					
						
							|  |  |  | 	// The protocol of the client triggering a cache prefetch doesn't matter. | 
					
						
							|  |  |  | 	// The address type is used by request.Proto to determine the response size, | 
					
						
							|  |  |  | 	// and using TCP ensures the message isn't unnecessarily truncated. | 
					
						
							|  |  |  | 	if u, ok := addr.(*net.UDPAddr); ok { | 
					
						
							|  |  |  | 		addr = &net.TCPAddr{IP: u.IP, Port: u.Port, Zone: u.Zone} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &ResponseWriter{ | 
					
						
							|  |  |  | 		ResponseWriter: state.W, | 
					
						
							|  |  |  | 		Cache:          c, | 
					
						
							|  |  |  | 		state:          state, | 
					
						
							|  |  |  | 		server:         server, | 
					
						
							| 
									
										
										
										
											2022-10-21 09:29:04 -06:00
										 |  |  | 		do:             state.Do(), | 
					
						
							| 
									
										
										
										
											2023-11-10 07:00:47 -08:00
										 |  |  | 		cd:             state.Req.CheckingDisabled, | 
					
						
							| 
									
										
										
										
											2018-06-19 20:50:08 +02:00
										 |  |  | 		prefetch:       true, | 
					
						
							|  |  |  | 		remoteAddr:     addr, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RemoteAddr implements the dns.ResponseWriter interface. | 
					
						
							|  |  |  | func (w *ResponseWriter) RemoteAddr() net.Addr { | 
					
						
							|  |  |  | 	if w.remoteAddr != nil { | 
					
						
							|  |  |  | 		return w.remoteAddr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return w.ResponseWriter.RemoteAddr() | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // WriteMsg implements the dns.ResponseWriter interface. | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | func (w *ResponseWriter) WriteMsg(res *dns.Msg) error { | 
					
						
							| 
									
										
										
										
											2020-10-15 16:47:07 +02:00
										 |  |  | 	mt, _ := response.Typify(res, w.now().UTC()) | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 16:27:50 +00:00
										 |  |  | 	// key returns empty string for anything we don't want to cache. | 
					
						
							| 
									
										
										
										
											2023-11-10 07:00:47 -08:00
										 |  |  | 	hasKey, key := key(w.state.Name(), res, mt, w.do, w.cd) | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 16:47:07 +02:00
										 |  |  | 	msgTTL := dnsutil.MinimalTTL(res, mt) | 
					
						
							| 
									
										
										
										
											2018-09-03 14:26:02 -05:00
										 |  |  | 	var duration time.Duration | 
					
						
							|  |  |  | 	if mt == response.NameError || mt == response.NoData { | 
					
						
							|  |  |  | 		duration = computeTTL(msgTTL, w.minnttl, w.nttl) | 
					
						
							| 
									
										
										
										
											2019-04-08 18:15:05 +08:00
										 |  |  | 	} else if mt == response.ServerError { | 
					
						
							| 
									
										
										
										
											2022-06-17 15:48:57 -04:00
										 |  |  | 		duration = w.failttl | 
					
						
							| 
									
										
										
										
											2018-09-03 14:26:02 -05:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		duration = computeTTL(msgTTL, w.minpttl, w.pttl) | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-31 17:26:43 -04:00
										 |  |  | 	if hasKey && duration > 0 { | 
					
						
							| 
									
										
										
										
											2020-10-15 16:47:07 +02:00
										 |  |  | 		if w.state.Match(res) { | 
					
						
							|  |  |  | 			w.set(res, key, mt, duration) | 
					
						
							| 
									
										
										
										
											2022-09-08 14:56:27 -04:00
										 |  |  | 			cacheSize.WithLabelValues(w.server, Success, w.zonesMetricLabel, w.viewMetricLabel).Set(float64(w.pcache.Len())) | 
					
						
							|  |  |  | 			cacheSize.WithLabelValues(w.server, Denial, w.zonesMetricLabel, w.viewMetricLabel).Set(float64(w.ncache.Len())) | 
					
						
							| 
									
										
										
										
											2018-03-25 17:11:10 +01:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			// Don't log it, but increment counter | 
					
						
							| 
									
										
										
										
											2022-09-08 14:56:27 -04:00
										 |  |  | 			cacheDrops.WithLabelValues(w.server, w.zonesMetricLabel, w.viewMetricLabel).Inc() | 
					
						
							| 
									
										
										
										
											2018-03-25 17:11:10 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	if w.prefetch { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-20 11:36:41 +01:00
										 |  |  | 	// Apply capped TTL to this reply to avoid jarring TTL experience 1799 -> 8 (e.g.) | 
					
						
							|  |  |  | 	ttl := uint32(duration.Seconds()) | 
					
						
							| 
									
										
										
										
											2022-10-21 09:29:04 -06:00
										 |  |  | 	res.Answer = filterRRSlice(res.Answer, ttl, false) | 
					
						
							|  |  |  | 	res.Ns = filterRRSlice(res.Ns, ttl, false) | 
					
						
							|  |  |  | 	res.Extra = filterRRSlice(res.Extra, ttl, false) | 
					
						
							| 
									
										
										
										
											2020-09-28 16:53:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 15:47:35 -04:00
										 |  |  | 	if !w.do && !w.ad { | 
					
						
							|  |  |  | 		// unset AD bit if requester is not OK with DNSSEC | 
					
						
							|  |  |  | 		// But retain AD bit if requester set the AD bit in the request, per RFC6840 5.7-5.8 | 
					
						
							|  |  |  | 		res.AuthenticatedData = false | 
					
						
							| 
									
										
										
										
											2021-07-09 04:53:50 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 16:47:07 +02:00
										 |  |  | 	return w.ResponseWriter.WriteMsg(res) | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-31 17:26:43 -04:00
										 |  |  | func (w *ResponseWriter) set(m *dns.Msg, key uint64, mt response.Type, duration time.Duration) { | 
					
						
							|  |  |  | 	// duration is expected > 0 | 
					
						
							|  |  |  | 	// and key is valid | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	switch mt { | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	case response.NoError, response.Delegation: | 
					
						
							| 
									
										
										
										
											2022-07-28 10:51:08 -04:00
										 |  |  | 		if plugin.Zones(w.pexcept).Matches(m.Question[0].Name) != "" { | 
					
						
							|  |  |  | 			// zone is in exception list, do not cache | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 		i := newItem(m, w.now(), duration) | 
					
						
							| 
									
										
										
										
											2022-07-07 17:07:04 -04:00
										 |  |  | 		if w.wildcardFunc != nil { | 
					
						
							|  |  |  | 			i.wildcard = w.wildcardFunc() | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-03-21 08:58:16 -07:00
										 |  |  | 		if w.pcache.Add(key, i) { | 
					
						
							| 
									
										
										
										
											2022-09-08 14:56:27 -04:00
										 |  |  | 			evictions.WithLabelValues(w.server, Success, w.zonesMetricLabel, w.viewMetricLabel).Inc() | 
					
						
							| 
									
										
										
										
											2021-03-21 08:58:16 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-03-20 05:05:09 -04:00
										 |  |  | 		// when pre-fetching, remove the negative cache entry if it exists | 
					
						
							|  |  |  | 		if w.prefetch { | 
					
						
							|  |  |  | 			w.ncache.Remove(key) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-08 18:15:05 +08:00
										 |  |  | 	case response.NameError, response.NoData, response.ServerError: | 
					
						
							| 
									
										
										
										
											2022-07-28 10:51:08 -04:00
										 |  |  | 		if plugin.Zones(w.nexcept).Matches(m.Question[0].Name) != "" { | 
					
						
							|  |  |  | 			// zone is in exception list, do not cache | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 		i := newItem(m, w.now(), duration) | 
					
						
							| 
									
										
										
										
											2022-07-07 17:07:04 -04:00
										 |  |  | 		if w.wildcardFunc != nil { | 
					
						
							|  |  |  | 			i.wildcard = w.wildcardFunc() | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-03-21 08:58:16 -07:00
										 |  |  | 		if w.ncache.Add(key, i) { | 
					
						
							| 
									
										
										
										
											2022-09-08 14:56:27 -04:00
										 |  |  | 			evictions.WithLabelValues(w.server, Denial, w.zonesMetricLabel, w.viewMetricLabel).Inc() | 
					
						
							| 
									
										
										
										
											2021-03-21 08:58:16 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case response.OtherError: | 
					
						
							| 
									
										
										
										
											2016-05-22 19:43:58 +01:00
										 |  |  | 		// don't cache these | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2018-04-19 07:41:56 +01:00
										 |  |  | 		log.Warningf("Caching called with unknown classification: %d", mt) | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // Write implements the dns.ResponseWriter interface. | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | func (w *ResponseWriter) Write(buf []byte) (int, error) { | 
					
						
							| 
									
										
										
										
											2018-04-19 07:41:56 +01:00
										 |  |  | 	log.Warning("Caching called with Write: not caching reply") | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	if w.prefetch { | 
					
						
							|  |  |  | 		return 0, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	n, err := w.ResponseWriter.Write(buf) | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	return n, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 19:16:33 +02:00
										 |  |  | // verifyStaleResponseWriter is a response writer that only writes messages if they should replace a | 
					
						
							|  |  |  | // stale cache entry, and otherwise discards them. | 
					
						
							|  |  |  | type verifyStaleResponseWriter struct { | 
					
						
							|  |  |  | 	*ResponseWriter | 
					
						
							|  |  |  | 	refreshed bool // set to true if the last WriteMsg wrote to ResponseWriter, false otherwise. | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // newVerifyStaleResponseWriter returns a ResponseWriter to be used when verifying stale cache | 
					
						
							|  |  |  | // entries. It only forward writes if an entry was successfully refreshed according to RFC8767, | 
					
						
							|  |  |  | // section 4 (response is NoError or NXDomain), and ignores any other response. | 
					
						
							|  |  |  | func newVerifyStaleResponseWriter(w *ResponseWriter) *verifyStaleResponseWriter { | 
					
						
							|  |  |  | 	return &verifyStaleResponseWriter{ | 
					
						
							|  |  |  | 		w, | 
					
						
							|  |  |  | 		false, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteMsg implements the dns.ResponseWriter interface. | 
					
						
							|  |  |  | func (w *verifyStaleResponseWriter) WriteMsg(res *dns.Msg) error { | 
					
						
							|  |  |  | 	w.refreshed = false | 
					
						
							|  |  |  | 	if res.Rcode == dns.RcodeSuccess || res.Rcode == dns.RcodeNameError { | 
					
						
							|  |  |  | 		w.refreshed = true | 
					
						
							|  |  |  | 		return w.ResponseWriter.WriteMsg(res) // stores to the cache and send to client | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil // else discard | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | const ( | 
					
						
							| 
									
										
										
										
											2018-06-27 21:12:27 +01:00
										 |  |  | 	maxTTL  = dnsutil.MaximumDefaulTTL | 
					
						
							| 
									
										
										
										
											2018-10-22 16:59:12 -04:00
										 |  |  | 	minTTL  = dnsutil.MinimalDefaultTTL | 
					
						
							| 
									
										
										
										
											2018-06-27 21:12:27 +01:00
										 |  |  | 	maxNTTL = dnsutil.MaximumDefaulTTL / 2 | 
					
						
							| 
									
										
										
										
											2018-10-22 16:59:12 -04:00
										 |  |  | 	minNTTL = dnsutil.MinimalDefaultTTL | 
					
						
							| 
									
										
										
										
											2016-11-09 10:01:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	defaultCap = 10000 // default capacity of the cache. | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-13 14:03:12 +00:00
										 |  |  | 	// Success is the class for caching positive caching. | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	Success = "success" | 
					
						
							|  |  |  | 	// Denial is the class defined for negative caching. | 
					
						
							|  |  |  | 	Denial = "denial" | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | ) |