resolver

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 19 Imported by: 1 module, 0 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

Index

Constants

This section is empty.

Variables

View Source
var DefaultDNS64Prefix = net.IPNet{
	IP:   net.ParseIP("64:ff9b::"),
	Mask: net.CIDRMask(96, 128),
}

DefaultDNS64Prefix is the well-known prefix for DNS64 (RFC 6052).

View Source
var RootServers = []NameServer{
	{Name: "a.root-servers.net", IPv4: "198.41.0.4", IPv6: "2001:503:ba3e::2:30"},
	{Name: "b.root-servers.net", IPv4: "170.247.170.2", IPv6: "2801:1b8:10::b"},
	{Name: "c.root-servers.net", IPv4: "192.33.4.12", IPv6: "2001:500:2::c"},
	{Name: "d.root-servers.net", IPv4: "199.7.91.13", IPv6: "2001:500:2d::d"},
	{Name: "e.root-servers.net", IPv4: "192.203.230.10", IPv6: "2001:500:a8::e"},
	{Name: "f.root-servers.net", IPv4: "192.5.5.241", IPv6: "2001:500:2f::f"},
	{Name: "g.root-servers.net", IPv4: "192.112.36.4", IPv6: "2001:500:12::d0d"},
	{Name: "h.root-servers.net", IPv4: "198.97.190.53", IPv6: "2001:500:1::53"},
	{Name: "i.root-servers.net", IPv4: "192.36.148.17", IPv6: "2001:7fe::53"},
	{Name: "j.root-servers.net", IPv4: "192.58.128.30", IPv6: "2001:503:c27::2:30"},
	{Name: "k.root-servers.net", IPv4: "193.0.14.129", IPv6: "2001:7fd::1"},
	{Name: "l.root-servers.net", IPv4: "199.7.83.42", IPv6: "2001:500:9f::42"},
	{Name: "m.root-servers.net", IPv4: "202.12.27.33", IPv6: "2001:dc3::35"},
}

RootServers contains the 13 root DNS servers with IPv4 and IPv6 addresses.

Functions

func ParseDNS64Prefix

func ParseDNS64Prefix(cidr string) (net.IPNet, error)

ParseDNS64Prefix parses a CIDR string into a net.IPNet suitable for DNS64. It returns an error if the prefix is not a valid IPv6 /96 network.

func SynthesizeAAAA

func SynthesizeAAAA(ipv4 net.IP, prefix net.IPNet) net.IP

SynthesizeAAAA embeds an IPv4 address into a DNS64 prefix to produce a synthesized IPv6 address. The prefix must be a /96 network. For the well-known prefix 64:ff9b::/96, the result is 64:ff9b::<ipv4 bytes>.

Types

type DelegationNS

type DelegationNS struct {
	Hostname string
	IPv4     string
	IPv6     string
	IPv4TTL  uint32
	IPv6TTL  uint32
}

DelegationNS represents a delegated nameserver with optional glue.

type ForwardTable

type ForwardTable struct {
	// contains filtered or unexported fields
}

ForwardTable stores forward/stub zones and provides longest-suffix matching.

func NewForwardTable

func NewForwardTable(zones []ForwardZone) *ForwardTable

NewForwardTable creates a ForwardTable from the given zone list. Zone names are normalised to lowercase with no trailing dot.

func (*ForwardTable) Match

func (ft *ForwardTable) Match(qname string) *ForwardZone

Match finds the ForwardZone whose name is the longest suffix of qname. Returns nil if no zone matches.

type ForwardZone

type ForwardZone struct {
	Name   string   // zone name (lowercase, no trailing dot)
	Addrs  []string // upstream IP addresses
	IsStub bool     // false = forward (RD=1), true = stub (RD=0, iterative from addrs)
}

ForwardZone represents a configured forwarding or stub zone.

type InfraCache

type InfraCache struct {
	// contains filtered or unexported fields
}

InfraCache tracks nameserver performance (RTT, failures, lameness) to enable intelligent NS selection.

func NewInfraCache

func NewInfraCache() *InfraCache

NewInfraCache creates a new infrastructure cache.

func (*InfraCache) CleanStale

func (ic *InfraCache) CleanStale(maxIdle time.Duration)

CleanStale removes entries that have not been used for more than maxIdle.

func (*InfraCache) GetRTT

func (ic *InfraCache) GetRTT(nsIP string) time.Duration

GetRTT returns the recorded RTT for a nameserver (for testing).

func (*InfraCache) IsLame

func (ic *InfraCache) IsLame(nsIP string, zone string) bool

IsLame returns true if the nameserver is known to be lame for the given zone.

func (*InfraCache) Len

func (ic *InfraCache) Len() int

Len returns the number of entries in the infra cache (for testing).

func (*InfraCache) RecordFailure

func (ic *InfraCache) RecordFailure(nsIP string)

RecordFailure increments the fail count for a nameserver.

func (*InfraCache) RecordLame

func (ic *InfraCache) RecordLame(nsIP string, zone string)

RecordLame marks a nameserver as lame for a specific zone.

func (*InfraCache) RecordRTT

func (ic *InfraCache) RecordRTT(nsIP string, rtt time.Duration)

RecordRTT records a successful query RTT using EWMA (0.7*old + 0.3*sample).

func (*InfraCache) SortByRTT

func (ic *InfraCache) SortByRTT(entries []nsEntry) []nsEntry

SortByRTT sorts nameserver entries by their effective RTT (fastest first). Entries with failures are penalised. Unknown servers get moderate priority.

func (*InfraCache) StartCleanup

func (ic *InfraCache) StartCleanup(ctx context.Context, interval, maxIdle time.Duration)

StartCleanup runs periodic cleanup of stale infra cache entries.

type LocalRecord

type LocalRecord struct {
	Name  string
	Type  uint16
	RData []byte
	TTL   uint32
}

LocalRecord holds a single record within a local zone.

func ParseLocalRecord

func ParseLocalRecord(s string) (*LocalRecord, error)

ParseLocalRecord parses a string in "name TYPE rdata" format into a LocalRecord. Examples:

"localhost. A 127.0.0.1"
"localhost. AAAA ::1"
"example.com. CNAME www.example.com."
"example.com. TXT \"hello world\""
"example.com. MX 10 mail.example.com."
"1.0.0.127.in-addr.arpa. PTR localhost."

type LocalZone

type LocalZone struct {
	Name    string
	Type    LocalZoneType
	Records []LocalRecord
}

LocalZone represents a locally-served DNS zone.

type LocalZoneTable

type LocalZoneTable struct {
	// contains filtered or unexported fields
}

LocalZoneTable stores local zones sorted by specificity (longest name first) for efficient longest-suffix matching.

func NewLocalZoneTable

func NewLocalZoneTable(zones []LocalZone) *LocalZoneTable

NewLocalZoneTable creates a LocalZoneTable from the given zones, sorting them so the most specific (longest name) zone comes first.

func (*LocalZoneTable) Lookup

func (t *LocalZoneTable) Lookup(name string, qtype uint16, qclass uint16) *ResolveResult

Lookup checks whether the query matches a local zone and returns the appropriate result. Returns nil if no local zone matches or if the zone type allows fall-through (e.g. LocalTransparent with no matching record).

type LocalZoneType

type LocalZoneType int

LocalZoneType controls how a local zone behaves.

const (
	// LocalStatic returns configured records or NXDOMAIN.
	LocalStatic LocalZoneType = iota
	// LocalDeny silently drops the query (returns nil response).
	LocalDeny
	// LocalRefuse returns REFUSED rcode.
	LocalRefuse
	// LocalRedirect returns configured records for any name in the zone.
	LocalRedirect
	// LocalTransparent returns configured records if found, otherwise
	// falls through to recursive resolution.
	LocalTransparent
)

func ParseLocalZoneType

func ParseLocalZoneType(s string) (LocalZoneType, bool)

ParseLocalZoneType converts a string to a LocalZoneType.

type NSInfo

type NSInfo struct {
	RTT       time.Duration // EWMA of round-trip times
	FailCount int
	LameZones map[string]struct{}
	LastUsed  time.Time
}

NSInfo holds performance data for a single nameserver IP.

type NameServer

type NameServer struct {
	Name string
	IPv4 string
	IPv6 string
}

NameServer represents a DNS nameserver with its IP addresses.

type ResolveResult

type ResolveResult struct {
	Answers      []dns.ResourceRecord
	Authority    []dns.ResourceRecord
	Additional   []dns.ResourceRecord
	RCODE        uint8
	DNSSECStatus string // "secure", "insecure", "bogus", ""
}

ResolveResult holds the outcome of a recursive resolution.

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver implements recursive DNS resolution.

func NewResolver

func NewResolver(c *cache.Cache, cfg ResolverConfig, m *metrics.Metrics, logger *slog.Logger) *Resolver

NewResolver creates a new recursive resolver.

func (*Resolver) EnableDNSSEC

func (r *Resolver) EnableDNSSEC(logger *slog.Logger)

EnableDNSSEC creates the DNSSEC validator, allowing the resolver to validate signed responses. Call this after PrimeRootHints.

func (*Resolver) InfraCache

func (r *Resolver) InfraCache() *InfraCache

InfraCache returns the resolver's infrastructure cache for external use.

func (*Resolver) IsReady

func (r *Resolver) IsReady() bool

IsReady returns whether the resolver has completed root hint priming.

func (*Resolver) PrimeRootHints

func (r *Resolver) PrimeRootHints() error

PrimeRootHints queries a root server for . NS to refresh root data.

func (*Resolver) QueryDNSSEC

func (r *Resolver) QueryDNSSEC(name string, qtype uint16, qclass uint16) (*dns.Message, error)

QueryDNSSEC sends a DNS query with the DO bit set. It satisfies the dnssec.Querier interface so the validator can fetch DNSKEY/DS records.

func (*Resolver) Resolve

func (r *Resolver) Resolve(name string, qtype uint16, qclass uint16) (*ResolveResult, error)

Resolve performs recursive resolution for the given query. Concurrent requests for the same name+type are coalesced.

func (*Resolver) SetActiveECS

func (r *Resolver) SetActiveECS(ecs *dns.ECSOption)

SetActiveECS sets the EDNS Client Subnet option for the next query. Pass nil to clear.

func (*Resolver) SetForwardTable

func (r *Resolver) SetForwardTable(ft *ForwardTable)

SetForwardTable configures forward and stub zones for the resolver.

func (*Resolver) SetLocalZones

func (r *Resolver) SetLocalZones(lz *LocalZoneTable)

SetLocalZones configures the resolver's local zone table. Queries matching a local zone are answered immediately without recursion.

func (*Resolver) StartRootRefresh

func (r *Resolver) StartRootRefresh(ctx context.Context, interval time.Duration)

StartRootRefresh runs a background goroutine that re-primes root hints at the given interval (RFC 8109). Call this after the initial PrimeRootHints.

type ResolverConfig

type ResolverConfig struct {
	MaxDepth        int
	MaxCNAMEDepth   int
	UpstreamTimeout time.Duration
	UpstreamRetries int
	QMinEnabled     bool
	Caps0x20Enabled bool // DNS 0x20 case randomization (RFC 5452)
	PreferIPv4      bool
	DNSSECEnabled   bool
	// UpstreamPort overrides the DNS port for upstream queries (default "53").
	// Used for testing with mock DNS servers.
	UpstreamPort string
	// DNS64Enabled enables DNS64 synthesis (RFC 6147).
	DNS64Enabled bool
	// DNS64Prefix is the IPv6 prefix used for DNS64 synthesis (must be /96).
	DNS64Prefix net.IPNet
	// ECSEnabled enables forwarding of EDNS Client Subnet options.
	ECSEnabled bool
	// ECSMaxPrefix is the maximum source prefix length for ECS (default 24).
	ECSMaxPrefix int
	// FallbackResolvers is a list of backup recursive DNS servers (e.g. 8.8.8.8, 1.1.1.1).
	// When primary resolution returns SERVFAIL, one randomly-picked fallback is tried once.
	FallbackResolvers []string
}

ResolverConfig holds configuration for the recursive resolver.

Jump to

Keyboard shortcuts

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