Documentation
¶
Overview ¶
Package dnscache contains a minimal DNS cache that makes a bunch of assumptions that are only valid for us. Not recommended for general use.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCacheMiss = errors.New("cache miss")
ErrCacheMiss is a sentinel error returned by MessageCache.ReplyFromCache when the request can not be satisfied from cache.
var ErrNoFallback = errors.New("no fallback DNS for host")
ErrNoFallback is returned (possibly wrapped) by a Resolver.LookupIPFallback func to say that it doesn't handle the requested host, so the Resolver should return its original error without logging or counting a fallback attempt.
net/dnsfallback returns it for any host that isn't one of Tailscale's own, as part of slowly retiring the DERP-based fallback DNS entirely. See https://github.com/tailscale/tailscale/issues/21028.
HookHostVerified optionally points to the dnsresolvecache feature's function to record that a TLS connection to host, at the given remote IP, presented a certificate chain that is valid for host. That is checked independently of the connection's own TLS config, which may deliberately tolerate interception (as the control plane Noise connection does). The feature uses this to flush a pending resolution of host to disk only once one of its IPs has been cryptographically verified to be host.
HookLookupDiskCache optionally points to the dnsresolvecache feature's function to load previously persisted IPs for host from disk. It is consulted only after regular DNS resolution has failed, before falling back to the DERP-based bootstrap DNS.
HookPersistResolution optionally points to the dnsresolvecache feature's function to record a successful DNS resolution of host to disk. The resolver argument is one of the "forward", "cloud", or "fallback" resolver source names. The feature defers the actual disk write until HookHostVerified confirms one of the resolution's IPs.
HookSetCacheDir optionally points to the dnsresolvecache feature's function to configure the directory in which resolved IPs are persisted to disk. It is called by tailscaled at startup, if the feature is linked in.
Functions ¶
func Dialer ¶ added in v1.4.0
Dialer returns a wrapped DialContext func that uses the provided dnsCache.
func SetDebugLoggingEnabled ¶ added in v1.36.0
func SetDebugLoggingEnabled(v bool)
SetDebugLoggingEnabled controls whether debug logging is enabled for this package.
These logs are also printed when the TS_DEBUG_DNS_CACHE envknob is set, but we allow configuring this manually as well so that it can be changed at runtime.
Types ¶
type MessageCache ¶ added in v1.20.0
type MessageCache struct {
// Clock is a clock, for testing.
// If nil, time.Now is used.
Clock func() time.Time
// contains filtered or unexported fields
}
MessageCache is a cache that works at the DNS message layer, with its cache keyed on a DNS wire-level question, and capable of replying to DNS messages.
Its zero value is ready for use with a default cache size. Use SetMaxCacheSize to specify the cache size.
It's safe for concurrent use.
func (*MessageCache) AddCacheEntry ¶ added in v1.20.0
func (c *MessageCache) AddCacheEntry(qPacket, res []byte) error
AddCacheEntry adds a cache entry to the cache. It returns an error if the entry could not be cached.
func (*MessageCache) Flush ¶ added in v1.20.0
func (c *MessageCache) Flush()
Flush clears the cache.
func (*MessageCache) ReplyFromCache ¶ added in v1.20.0
func (c *MessageCache) ReplyFromCache(w io.Writer, dnsQueryMessage []byte) error
ReplyFromCache writes a DNS reply to w for the provided DNS query message, which must begin with the two ID bytes of a DNS message.
If there's a cache miss, the message is invalid or unexpected, ErrCacheMiss is returned. On cache hit, either nil or an error from a w.Write call is returned.
func (*MessageCache) SetMaxCacheSize ¶ added in v1.20.0
func (c *MessageCache) SetMaxCacheSize(n int)
SetMaxCacheSize sets the maximum number of DNS cache entries that can be stored.
type Resolver ¶
type Resolver struct {
// Forward is the resolver to use to populate the cache.
// If nil, net.DefaultResolver is used.
Forward *net.Resolver
// LookupIPForTest, if non-nil and in tests, handles requests instead
// of the usual mechanisms.
LookupIPForTest func(ctx context.Context, host string) ([]netip.Addr, error)
// LookupIPFallback optionally provides a backup DNS mechanism
// to use if Forward returns an error or no results.
//
// If it returns an error wrapping [ErrNoFallback], the Resolver
// behaves as if LookupIPFallback were nil for that host and
// returns the original error.
LookupIPFallback func(ctx context.Context, host string) ([]netip.Addr, error)
// TTL is how long to keep entries cached
//
// If zero, a default (currently 10 minutes) is used.
TTL time.Duration
// UseLastGood controls whether a cached entry older than TTL is used
// if a refresh fails.
UseLastGood bool
// SingleHostStaticResult, if non-nil, is the static result of IPs that is returned
// by Resolver.LookupIP for any hostname. When non-nil, SingleHost must also be
// set with the expected name.
SingleHostStaticResult []netip.Addr
// SingleHost is the hostname that SingleHostStaticResult is for.
// It is required when SingleHostStaticResult is present.
SingleHost string
// Logf optionally provides a log function to use for debug logs. If
// not present, log.Printf will be used. The prefix "dnscache: " will
// be added to all log messages printed with this logger.
Logf logger.Logf
// contains filtered or unexported fields
}
Resolver is a minimal DNS caching resolver.
The TTL is always fixed for now. It's not intended for general use. Cache entries are never cleaned up so it's intended that this is only used with a fixed set of hostnames.
func (*Resolver) LookupIP ¶
func (r *Resolver) LookupIP(ctx context.Context, host string) (ip, v6 netip.Addr, allIPs []netip.Addr, err error)
LookupIP returns the host's primary IP address (either IPv4 or IPv6, but preferring IPv4) and optionally its IPv6 address, if there is both IPv4 and IPv6.
If err is nil, ip will be non-nil. The v6 address may be nil even with a nil error.