util

package
v1.6.6 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package util provides DNS protocol utilities for SDNS.

Package util provides DNS protocol utilities for SDNS.

Package util provides DNS protocol utilities for SDNS.

Package util provides DNS protocol utilities for SDNS.

Index

Constants

View Source
const (
	// MinCacheTTL is the minimum time to cache any response.
	MinCacheTTL = 5 * time.Second
	// MaxCacheTTL is the maximum time to cache any response.
	MaxCacheTTL = 24 * time.Hour
)
View Source
const (
	// ReverseDomainV4 is the reverse DNS domain for IPv4 addresses.
	ReverseDomainV4 = ".in-addr.arpa."
	// ReverseDomainV6 is the reverse DNS domain for IPv6 addresses.
	ReverseDomainV6 = ".ip6.arpa."
)
View Source
const (
	// DefaultMsgSize EDNS0 message size.
	DefaultMsgSize = 1232
)

Variables

This section is empty.

Functions

func CalculateCacheTTL

func CalculateCacheTTL(msg *dns.Msg, respType ResponseType) time.Duration

CalculateCacheTTL determines the appropriate cache duration for a DNS response. It scans all resource records and returns the minimum TTL found, with bounds checking. For DNSSEC-signed responses, it also considers RRSIG expiration times.

func CheckReverseName

func CheckReverseName(name string) int

CheckReverseName checks if a domain name is in a reverse DNS zone. Returns: - 0: not a reverse domain - 1: IPv4 reverse domain (.in-addr.arpa.) - 2: IPv6 reverse domain (.ip6.arpa.)

func ClearDNSSEC

func ClearDNSSEC(msg *dns.Msg) *dns.Msg

ClearDNSSEC removes RRSIG, NSEC and NSEC3 records from Answer and Ns sections in place. Short-circuits when the sections already hold nothing to strip (typical for non-DNSSEC responses), and reuses the slice backing array when a filter is actually needed.

func ClearOPT

func ClearOPT(msg *dns.Msg) *dns.Msg

ClearOPT removes every OPT record from msg.Extra in place. No copy is made when the Extra section has no OPT (the common case for responses synthesised without EDNS).

func DnameTarget added in v1.6.6

func DnameTarget(msg *dns.Msg) string

DnameTarget returns the synthesized CNAME target for the question given a DNAME in msg.Answer, or "" if no redirect applies. Per RFC 6672 §2.3 the DNAME owner itself is *not* redirected, and only names strictly below the owner are substituted.

dns.CompareDomainName counts matching trailing labels regardless of whether one name is actually an ancestor of the other, so a DNAME at sub.example.com. and a query for other.example.com. both share the two-label suffix example.com. — but sub.example.com. is a *sibling* of other.example.com., not an ancestor, and must not rewrite the query. Require the shared count to exactly equal the DNAME owner's label count (i.e. owner is a proper suffix sequence of qname), and that qname has strictly more labels (rules out exact match), before synthesizing.

func ErrorToEDE

func ErrorToEDE(err error) (uint16, string)

ErrorToEDE maps errors to Extended DNS Error codes efficiently.

func Exchange

func Exchange(ctx context.Context, req *dns.Msg, addr string, net string, client *dns.Client) (*dns.Msg, error)

Exchange exchange dns request with TCP fallback.

func ExtractRRSet added in v1.6.6

func ExtractRRSet(in []dns.RR, name string, t ...uint16) []dns.RR

ExtractRRSet returns every RR in `in` whose owner equals `name` (case-insensitive; pass "" to skip the name match) and whose Rrtype is one of `t`. The returned slice is a fresh copy and shares no storage with the input.

func FilterRRsToZone added in v1.6.6

func FilterRRsToZone(rrs []dns.RR, zone string) []dns.RR

FilterRRsToZone returns the subset of rrs whose owner is in zone. For NSEC records the NextDomain field is also checked: a legitimate NSEC's NextDomain is always another owner in the same zone (the last NSEC wraps back to the zone apex), so an in-zone owner paired with an out-of-zone NextDomain is either a broken zone or an attacker-crafted record and must be discarded. Without this the structural coverage helpers would accept a forged NSEC whose NextDomain is picked to canonically straddle the qname.

func GenerateServerCookie

func GenerateServerCookie(secret, remoteip, cookie string) string

GenerateServerCookie return generated edns server cookie.

func GetEDE

func GetEDE(msg *dns.Msg) *dns.EDNS0_EDE

GetEDE extracts Extended DNS Error from a message if present.

func IPFromReverseName

func IPFromReverseName(name string) string

IPFromReverseName extracts an IP address from a PTR record name. For example: - "54.119.58.176.in-addr.arpa." returns "176.58.119.54" - "b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa." returns "2001:db8::567:89ab" Returns empty string if the name is not a valid PTR record.

func NameInZone added in v1.6.6

func NameInZone(name, zone string) bool

NameInZone reports whether name is the zone apex or a descendant of zone. Both arguments are expected to be lowercase FQDNs.

func NotSupported

func NotSupported(w dns.ResponseWriter, req *dns.Msg) error

NotSupported response to writer an empty notimplemented message.

func SetEDE

func SetEDE(msg *dns.Msg, code uint16, extraText string)

SetEDE adds an Extended DNS Error to the response.

func SetEdns0

func SetEdns0(req *dns.Msg) (*dns.OPT, int, string, bool, bool)

SetEdns0 returns replaced or new opt rr and if request has do.

The function inspects the client's OPT record to harvest NSID / COOKIE signalling, strips every option before forwarding (ECS in particular, per RFC 7871 privacy guidance), and normalises the UDP size. Inspection uses typed pointer assertions so we avoid the allocating option.String() path, and the stripping reuses opt.Option's backing storage via opt.Option = nil rather than allocating an empty slice.

func SetRcode

func SetRcode(req *dns.Msg, rcode int, do bool) *dns.Msg

SetRcode returns message specified with rcode.

func SetRcodeWithEDE

func SetRcodeWithEDE(req *dns.Msg, rcode int, do bool, edeCode uint16, extraText string) *dns.Msg

SetRcodeWithEDE returns message with specified rcode and Extended DNS Error.

Types

type EDEError added in v1.6.6

type EDEError struct {
	Code    uint16
	Message string
	Err     error
}

EDEError is an error that carries an Extended DNS Error code (RFC 8914). Network and DNSSEC validation paths return EDEError values so that the response builder can copy the code straight into the EDE option without re-classifying string messages.

func (*EDEError) EDECode added in v1.6.6

func (e *EDEError) EDECode() uint16

EDECode returns the Extended DNS Error code for this error.

func (*EDEError) Error added in v1.6.6

func (e *EDEError) Error() string

func (*EDEError) Unwrap added in v1.6.6

func (e *EDEError) Unwrap() error

func (*EDEError) WithContext added in v1.6.6

func (e *EDEError) WithContext(format string, args ...any) *EDEError

WithContext returns a new EDEError whose Message is e.Message followed by the formatted context. Useful for tagging a generic sentinel with the zone or qname that triggered it without losing the code.

type ResponseType

type ResponseType int

ResponseType represents the classification of a DNS response.

const (
	// TypeSuccess indicates a positive response with answers.
	TypeSuccess ResponseType = iota
	// TypeNXDomain indicates the queried domain does not exist (NXDOMAIN).
	TypeNXDomain
	// TypeNoRecords indicates the domain exists but has no records of the requested type (NODATA).
	TypeNoRecords
	// TypeReferral indicates a delegation to another nameserver.
	TypeReferral
	// TypeMetaQuery indicates zone transfer or notification queries.
	TypeMetaQuery
	// TypeDynamicUpdate indicates a dynamic DNS update message.
	TypeDynamicUpdate
	// TypeServerFailure indicates a server error occurred.
	TypeServerFailure
	// TypeNotCacheable indicates responses that should not be cached.
	TypeNotCacheable
	// TypeExpiredSignature indicates DNSSEC signatures have expired.
	TypeExpiredSignature
)

func ClassifyResponse

func ClassifyResponse(msg *dns.Msg, now time.Time) (ResponseType, *dns.OPT)

ClassifyResponse analyzes a DNS message and determines its type. It also returns the OPT record if present for EDNS0 processing. The time parameter is used for checking DNSSEC signature expiration.

Jump to

Keyboard shortcuts

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