dnscache

package
v1.106.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: BSD-3-Clause Imports: 29 Imported by: 6 modules, 26 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
var ErrCacheMiss = errors.New("cache miss")

ErrCacheMiss is a sentinel error returned by MessageCache.ReplyFromCache when the request can not be satisfied from cache.

View Source
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.

View Source
var HookHostVerified feature.Hook[func(host string, ip netip.Addr)]

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.

View Source
var HookLookupDiskCache feature.Hook[func(host string) ([]netip.Addr, bool)]

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.

View Source
var HookPersistResolution feature.Hook[func(host, resolver string, ips []netip.Addr)]

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.

View Source
var HookSetCacheDir feature.Hook[func(dir string, logf logger.Logf)]

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

func Dialer(fwd netx.DialFunc, dnsCache *Resolver) netx.DialFunc

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.

func TLSDialer added in v1.6.0

func TLSDialer(fwd netx.DialFunc, dnsCache *Resolver, tlsConfigBase *tls.Config) netx.DialFunc

TLSDialer is like Dialer but returns a func suitable for using with net/http.Transport.DialTLSContext. It returns a *tls.Conn type on success. On TLS cert validation failure, it can invoke a backup DNS resolution strategy.

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 Get

func Get() *Resolver

Get returns a caching Resolver singleton.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL