Documentation
¶
Index ¶
- Constants
- Variables
- func AddTCPKeepaliveToRawResponse(resp []byte, timeoutUnits100ms uint16) []byte
- func BuildPlainName(name string) []byte
- func DecodeName(msg []byte, offset int) (string, int, error)
- func EncodeName(w *wireWriter, name string) error
- func EncodeNameToBytes(name string) ([]byte, error)
- func HasPaddingOption(e *EDNS0) bool
- func HasTCPKeepaliveOption(e *EDNS0) bool
- func Pack(msg *Message, buf []byte) ([]byte, error)
- func PadRawResponse(resp []byte, block int) []byte
- func ParseA(rdata []byte) (net.IP, error)
- func ParseAAAA(rdata []byte) (net.IP, error)
- func ParseCNAME(msg []byte, rdataOffset int) (string, error)
- func ParseCookieOption(data []byte) (clientCookie []byte, serverCookie []byte)
- func ParseDNAME(msg []byte, rdataOffset int) (string, error)
- func ParseEDEOption(data []byte) (infoCode uint16, extraText string, err error)
- func ParseNS(msg []byte, rdataOffset int) (string, error)
- func ParsePTR(msg []byte, rdataOffset int) (string, error)
- func ParseTXT(rdata []byte) ([]string, error)
- func TruncateIP(ip net.IP, prefixLen uint8) net.IP
- type DNSKEYRecord
- type DSRecord
- type ECSOption
- type EDNS0
- type EDNSOption
- func BuildDAUOption(algorithms []uint8) EDNSOption
- func BuildDHUOption(digests []uint8) EDNSOption
- func BuildECS(ecs *ECSOption) EDNSOption
- func BuildEDEOption(infoCode uint16, extraText string) EDNSOption
- func BuildN3UOption(hashes []uint8) EDNSOption
- func BuildPaddingOption(n int) EDNSOption
- func BuildTCPKeepaliveOption(timeoutUnits100ms uint16) EDNSOption
- type FlagBuilder
- func (fb *FlagBuilder) Build() uint16
- func (fb *FlagBuilder) SetAA(v bool) *FlagBuilder
- func (fb *FlagBuilder) SetAD(v bool) *FlagBuilder
- func (fb *FlagBuilder) SetCD(v bool) *FlagBuilder
- func (fb *FlagBuilder) SetOpcode(v uint8) *FlagBuilder
- func (fb *FlagBuilder) SetQR(v bool) *FlagBuilder
- func (fb *FlagBuilder) SetRA(v bool) *FlagBuilder
- func (fb *FlagBuilder) SetRCODE(v uint8) *FlagBuilder
- func (fb *FlagBuilder) SetRD(v bool) *FlagBuilder
- func (fb *FlagBuilder) SetTC(v bool) *FlagBuilder
- type Header
- func (h *Header) AA() bool
- func (h *Header) AD() bool
- func (h *Header) CD() bool
- func (h *Header) Opcode() uint8
- func (h *Header) Pack(w *wireWriter) error
- func (h *Header) QR() bool
- func (h *Header) RA() bool
- func (h *Header) RCODE() uint8
- func (h *Header) RD() bool
- func (h *Header) TC() bool
- func (h *Header) Unpack(r *wireReader) error
- type MXRecord
- type Message
- type NSEC3Record
- type NSECRecord
- type Question
- type RRSIGRecord
- type ResourceRecord
- type SOARecord
- type SRVRecord
Constants ¶
const ( AlgRSASHA1 uint8 = 5 AlgRSASHA256 uint8 = 8 AlgRSASHA512 uint8 = 10 AlgECDSAP256 uint8 = 13 AlgECDSAP384 uint8 = 14 AlgED25519 uint8 = 15 AlgED448 uint8 = 16 )
DNSSEC algorithm numbers (RFC 8624).
const ( // DigestReserved (0) is the IANA-reserved DS digest type. RFC 4509 // §2.4 / RFC 8624 §3.3 say a DS RR carrying digest type 0 MUST NOT // be used to authenticate the corresponding DNSKEY. If the parent // publishes ONLY digest-type-0 DS records for a delegation, the // delegation has no usable DS — RFC 4035 §5.2 treats that the same // as no DS at all and the validator falls through to Insecure. DigestReserved uint8 = 0 DigestSHA1 uint8 = 1 DigestSHA256 uint8 = 2 DigestSHA384 uint8 = 4 )
DNSSEC digest types (RFC 8624).
const ( AlgRSAMD5 uint8 = 1 AlgDSA uint8 = 3 AlgDSANSEC3SHA1 uint8 = 6 AlgRSASHA1NSEC3 uint8 = 7 )
DNSSEC algorithm numbers that this resolver explicitly does NOT validate. The verify-switch in dnssec/verify.go falls through to "unsupported" for any algorithm not enumerated above, so listing these as named constants is for spec-completeness and pin-test readability — never for acceptance.
- RSAMD5 (1): MD5 is cryptographically broken (RFC 6151) and MUST NOT be used for new signatures (RFC 6944).
- DSA (3): DSA-SHA1 is deprecated and dropped from the Internet root signing key rollover (RFC 8624 §3.1).
- DSA-NSEC3 (6): Same DSA constraints, NSEC3 variant.
- RSASHA1-NSEC3 (7):RSA/SHA-1 in any form is deprecated; we honour the validator's allowSHA1 gate via algorithm 5 only when explicitly opted in. Algorithm 7 is legacy and we leave it strictly off.
const ( EDECodeOtherError uint16 = 0 EDECodeUnsupportedDNSKEYAlgo uint16 = 1 EDECodeUnsupportedDSDigestType uint16 = 2 EDECodeStaleAnswer uint16 = 3 EDECodeForgedAnswer uint16 = 4 EDECodeDNSSECIndeterminate uint16 = 5 EDECodeDNSSECBogus uint16 = 6 EDECodeSignatureExpired uint16 = 7 EDECodeSignatureNotYetValid uint16 = 8 EDECodeDNSKEYMissing uint16 = 9 EDECodeRRSIGsMissing uint16 = 10 EDECodeNoZoneKeyBitSet uint16 = 11 EDECodeNSECMissing uint16 = 12 EDECodeCachedError uint16 = 13 EDECodeNotReady uint16 = 14 EDECodeBlocked uint16 = 15 EDECodeCensored uint16 = 16 EDECodeFiltered uint16 = 17 EDECodeProhibited uint16 = 18 EDECodeStaleNXDOMAINAnswer uint16 = 19 EDECodeNotAuthoritative uint16 = 20 EDECodeNotSupported uint16 = 21 EDECodeNoReachableAuthority uint16 = 22 EDECodeNetworkError uint16 = 23 EDECodeInvalidData uint16 = 24 )
EDE info codes (RFC 8914).
const ( EDNSOptionCodeECS uint16 = 8 EDNSOptionCodeCookie uint16 = 10 EDNSOptionCodeTCPKeepalive uint16 = 11 EDNSOptionCodePadding uint16 = 12 EDNSOptionCodeEDE uint16 = 15 // EDNSOptionCodeDAU is the "DNSSEC Algorithm Understood" option // (RFC 6975 §3). The option data is the list of DNSKEY/RRSIG // algorithm numbers the resolver can validate. Multi-signed zones // (a zone published with multiple algorithms during a rollover) // use this to decide which signature to return; without DAU they // must send all signatures, wasting bandwidth and amplification. EDNSOptionCodeDAU uint16 = 5 // EDNSOptionCodeDHU is the "DS Hash Understood" option. Same idea // but for DS digest types (RFC 6975 §3, option code 6). EDNSOptionCodeDHU uint16 = 6 // EDNSOptionCodeN3U is the "NSEC3 Hash Understood" option. NSEC3 // hash algorithms — only SHA-1 (algorithm 1) is currently defined // (RFC 6975 §3, option code 7). EDNSOptionCodeN3U uint16 = 7 )
EDNS option codes.
const ( TypeA uint16 = 1 TypeNS uint16 = 2 TypeCNAME uint16 = 5 TypeSOA uint16 = 6 TypePTR uint16 = 12 TypeMX uint16 = 15 TypeTXT uint16 = 16 TypeAAAA uint16 = 28 TypeSRV uint16 = 33 TypeHINFO uint16 = 13 TypeDNAME uint16 = 39 TypeOPT uint16 = 41 TypeANY uint16 = 255 // DNSSEC types TypeDS uint16 = 43 TypeRRSIG uint16 = 46 TypeNSEC uint16 = 47 TypeDNSKEY uint16 = 48 TypeNSEC3 uint16 = 50 TypeNSEC3PARAM uint16 = 51 // RFC 7344 — child-published DS/DNSKEY records the parent zone uses // to update the child's delegation. CDS RDATA is byte-identical to // DS; CDNSKEY RDATA is byte-identical to DNSKEY. Distinct types // only at the owner-side semantics: present at the apex of the // CHILD zone, signed by the child's keys, instructing the parent. TypeCDS uint16 = 59 TypeCDNSKEY uint16 = 60 // Modern service-discovery types. We do not parse their RDATA into // rich Go structs (the wire layer treats them as opaque, which is // the right behaviour for a forwarding recursive resolver per RFC // 3597 — pass unknown RDATA through verbatim so signatures remain // verifiable downstream). But registering them in TypeToString so // logs, the queries UI, and DNSSEC trace events show "HTTPS" and // "SVCB" instead of "TYPE65"/"TYPE64" is important for operators // chasing real-world traffic on networks where these dominate // (Apple devices, HTTP/3 / QUIC clients, ECH-enabled browsers). TypeSVCB uint16 = 64 // RFC 9460 §2.1 — service binding TypeHTTPS uint16 = 65 // RFC 9460 §9 — HTTPS specialisation of SVCB // RFC 8659 (obsoletes 6844). DNS-based authorization for X.509 // issuance — every CA on the public PKI MUST honour CAA, so // resolvers that mangle this RDATA can silently break cert renewal. TypeCAA uint16 = 257 )
Record types
const ( RCodeNoError uint8 = 0 RCodeFormErr uint8 = 1 RCodeServFail uint8 = 2 RCodeNXDomain uint8 = 3 RCodeNotImp uint8 = 4 RCodeRefused uint8 = 5 // RCodeBadCookie is the extended RCODE 23 (RFC 7873 §5.2). It must be // transmitted using the EDNS0 ExtRCODE split: the low 4 bits (0x07) go // into the DNS header RCODE field, the high 8 bits (0x01) go into the // OPT pseudo-RR TTL byte 0. See buildBadCookieResponse. RCodeBadCookie uint8 = 23 )
Response codes
const ( OpcodeQuery uint8 = 0 OpcodeIQuery uint8 = 1 OpcodeStatus uint8 = 2 )
Opcodes
const (
ClassIN uint16 = 1
)
Classes
const PaddingBlockSize = 468
PaddingBlockSize is the recommended response padding block (RFC 8467 §4.1: "the server SHOULD pad the response packet to a multiple of 468 bytes"). The number is empirical: it avoids most common MTUs while still being a single power-of-two-friendly value that hides most short answers in the same length bucket as longer ones.
Variables ¶
var RCodeToString = map[uint8]string{ RCodeNoError: "NOERROR", RCodeFormErr: "FORMERR", RCodeServFail: "SERVFAIL", RCodeNXDomain: "NXDOMAIN", RCodeNotImp: "NOTIMP", RCodeRefused: "REFUSED", RCodeBadCookie: "BADCOOKIE", }
RCodeToString maps response codes to human-readable names.
var TypeToString = map[uint16]string{ TypeA: "A", TypeNS: "NS", TypeCNAME: "CNAME", TypeSOA: "SOA", TypeHINFO: "HINFO", TypePTR: "PTR", TypeMX: "MX", TypeTXT: "TXT", TypeAAAA: "AAAA", TypeSRV: "SRV", TypeDNAME: "DNAME", TypeOPT: "OPT", TypeDS: "DS", TypeRRSIG: "RRSIG", TypeNSEC: "NSEC", TypeDNSKEY: "DNSKEY", TypeNSEC3: "NSEC3", TypeNSEC3PARAM: "NSEC3PARAM", TypeCDS: "CDS", TypeCDNSKEY: "CDNSKEY", TypeANY: "ANY", TypeSVCB: "SVCB", TypeHTTPS: "HTTPS", TypeCAA: "CAA", }
TypeToString maps type values to human-readable names.
Functions ¶
func AddTCPKeepaliveToRawResponse ¶ added in v0.6.20
AddTCPKeepaliveToRawResponse parses a wire-format response and adds an edns-tcp-keepalive option (RFC 7828 §3.1) carrying the server's idle timeout in 100ms units. Used by TCP/DoT/DoH servers when the client signalled keepalive interest. Idempotent — if the response already carries a keepalive option, the original bytes are returned unchanged. Returns the original bytes on any parse/pack error.
func BuildPlainName ¶
BuildPlainName encodes a domain name as uncompressed wire-format label sequence. Exported for use in tests and delegation handling.
func DecodeName ¶
DecodeName reads a domain name from the wire format. It returns the decoded name and the number of bytes consumed from the ORIGINAL position (before any pointer jumps).
func EncodeName ¶
EncodeName writes a domain name in wire format with optional compression.
func EncodeNameToBytes ¶ added in v0.6.15
EncodeNameToBytes returns the uncompressed wire-format encoding of a domain name: a sequence of length-prefixed labels terminated by a zero byte (e.g. "foo.example.com" → 3 'f' 'o' 'o' 7 'e'...'m' 0). Used for callers that need to produce stand-alone RData bytes — DNAME→CNAME synthesis, hand-built test fixtures — without the message-level compression dictionary that EncodeName depends on. Returns an error on labels longer than 63 octets (RFC 1035 §2.3.4) or full names longer than 255 octets including the root terminator.
func HasPaddingOption ¶ added in v0.6.20
HasPaddingOption reports whether the parsed EDNS0 record carries a PADDING option (used as the client's signal that it wants the response padded — RFC 8467 §4).
func HasTCPKeepaliveOption ¶ added in v0.6.20
HasTCPKeepaliveOption reports whether the parsed EDNS0 record carries an edns-tcp-keepalive option — the client's signal that it wants to negotiate a TCP idle timeout (RFC 7828 §3.2). Per the RFC the client form has zero-length data; we accept any length and treat the option's presence alone as the signal.
func PadRawResponse ¶ added in v0.6.20
PadRawResponse parses a wire-format response and adds an EDNS PADDING option (RFC 7830 §3) sized so that the total wire length becomes the next multiple of `block` bytes (RFC 8467 §4.1 recommends 468). Used by DoT/DoH servers when the client signalled padding interest, so a passive observer cannot infer the queried name from response length alone. Returns the original bytes unchanged on any parse/pack error or when `block <= 0`.
Implementation note: we deliberately mutate the existing OPT RR's RData (rather than rebuilding via BuildOPTWithOptions) so OPT TTL bits — ExtRCODE, Version, DO-flag — are preserved verbatim. The caller's resolver may have set ExtRCODE for BADVERS/BADKEY signalling and we must not lose it.
func ParseCNAME ¶
ParseCNAME extracts the canonical name from CNAME record RDATA.
func ParseCookieOption ¶
ParseCookieOption parses a DNS Cookie option (RFC 7873, option code 10). Returns the client cookie (8 bytes) and optional server cookie (8-32 bytes).
func ParseDNAME ¶
ParseDNAME extracts the delegation name from DNAME record RDATA (RFC 6672).
func ParseEDEOption ¶
ParseEDEOption parses an Extended DNS Error option from EDNS0 option data. Returns the info code and extra text.
Types ¶
type DNSKEYRecord ¶
DNSKEYRecord holds parsed DNSKEY RDATA fields (RFC 4034 Section 2).
func ParseDNSKEY ¶
func ParseDNSKEY(rdata []byte) (*DNSKEYRecord, error)
ParseDNSKEY extracts DNSKEY fields from RDATA.
func (*DNSKEYRecord) IsKSK ¶
func (k *DNSKEYRecord) IsKSK() bool
IsKSK reports whether the key-signing-key flag (SEP bit) is set.
func (*DNSKEYRecord) IsRevoked ¶ added in v0.6.13
func (k *DNSKEYRecord) IsRevoked() bool
IsRevoked reports whether the REVOKE flag (RFC 5011 §3, bit 8) is set. A revoked DNSKEY MUST NOT be used to validate signatures or to match DS records — the operator has explicitly disowned that key and the chain must rebuild from the unrevoked siblings. Resolvers that ignore the bit keep accepting signatures from compromised or rolled-out keys.
func (*DNSKEYRecord) IsZoneKey ¶ added in v0.6.32
func (k *DNSKEYRecord) IsZoneKey() bool
IsZoneKey reports whether the Zone Key flag (bit 7, value 0x0100) is set. Per RFC 4034 §2.1.1, a DNSKEY whose Zone Key bit is clear holds some other kind of public key (e.g. SIG(0) host keys) and MUST NOT be used to verify RRSIGs that cover RRsets. The validator gates VerifyRRSIG on this.
func (*DNSKEYRecord) KeyTag ¶
func (k *DNSKEYRecord) KeyTag() uint16
KeyTag computes the key tag per RFC 4034 Appendix B.
type DSRecord ¶
DSRecord holds parsed DS RDATA fields (RFC 4034 Section 5).
type ECSOption ¶
type ECSOption struct {
Family uint16 // 1=IPv4, 2=IPv6
SourcePrefixLen uint8
ScopePrefixLen uint8
Address net.IP
}
ECSOption represents an EDNS Client Subnet option (RFC 7871, option code 8).
func ExtractECSFromOPT ¶ added in v0.6.9
ExtractECSFromOPT walks the OPT record options and returns the first EDNS Client Subnet option found, or nil if none is present. Parse errors are reported so callers can FORMERR-fail a malformed option per RFC 7871 §6 (unrecognized FAMILY → SERVFAIL). Missing-ECS is the common case and returns (nil, nil).
func (*ECSOption) CacheKey ¶ added in v0.6.9
CacheKey returns the canonical string used to key ECS-scoped cache entries: "<address>/<prefix>" where address is truncated to prefix. Returns "" when ecs is nil or SourcePrefixLen is 0 (global scope), so callers can distinguish global from ECS-scoped entries by emptiness.
type EDNS0 ¶
type EDNS0 struct {
UDPSize uint16
ExtRCODE uint8
Version uint8
DOFlag bool
Options []EDNSOption
}
EDNS0 represents parsed EDNS0 extension data.
func ParseOPT ¶
func ParseOPT(rr *ResourceRecord) (*EDNS0, error)
ParseOPT extracts EDNS0 information from an OPT pseudo-record.
type EDNSOption ¶
EDNSOption represents a single EDNS0 option.
func BuildDAUOption ¶ added in v0.6.14
func BuildDAUOption(algorithms []uint8) EDNSOption
BuildDAUOption constructs the DNSSEC Algorithm Understood option (RFC 6975) carrying the list of DNSKEY/RRSIG algorithm numbers this resolver can validate. Algorithms we deliberately refuse (RSASHA1 when allowSHA1=false, ED448 until we ship a verifier) are omitted — the spec is explicit that DAU advertises ONLY algorithms the resolver will actually accept.
func BuildDHUOption ¶ added in v0.6.14
func BuildDHUOption(digests []uint8) EDNSOption
BuildDHUOption is the DS-hash counterpart of BuildDAUOption.
func BuildECS ¶
func BuildECS(ecs *ECSOption) EDNSOption
BuildECS constructs an EDNS Client Subnet option (RFC 7871, option code 8).
func BuildEDEOption ¶
func BuildEDEOption(infoCode uint16, extraText string) EDNSOption
BuildEDEOption constructs an Extended DNS Error option (RFC 8914, option code 15). infoCode is the EDE info code, extraText is an optional UTF-8 string.
func BuildN3UOption ¶ added in v0.6.14
func BuildN3UOption(hashes []uint8) EDNSOption
BuildN3UOption is the NSEC3-hash counterpart of BuildDAUOption.
func BuildPaddingOption ¶ added in v0.6.20
func BuildPaddingOption(n int) EDNSOption
BuildPaddingOption constructs an EDNS(0) PADDING option (RFC 7830, option code 12) carrying `n` zero bytes. The option SHOULD be the last option in the OPT RR per RFC 7830 §3 so receivers can ignore trailing zeros without parsing the rest. Only meaningful on encrypted transports (DoT/DoH) — RFC 8467 §6 explicitly disallows padding on plain UDP/TCP since it gives no privacy benefit and wastes bandwidth.
func BuildTCPKeepaliveOption ¶ added in v0.6.20
func BuildTCPKeepaliveOption(timeoutUnits100ms uint16) EDNSOption
BuildTCPKeepaliveOption constructs an EDNS(0) edns-tcp-keepalive option (RFC 7828, option code 11). `timeoutUnits100ms` is the idle timeout the server is willing to keep the connection open, expressed in 100-millisecond units (RFC 7828 §3.1). A zero-length data field (length=0) is the client form ("I want to use keepalive"); the server response form MUST carry the 2-byte timeout. This builder produces the server form.
type FlagBuilder ¶
type FlagBuilder struct {
// contains filtered or unexported fields
}
FlagBuilder provides a builder pattern for constructing DNS header flags.
func NewFlagBuilder ¶
func NewFlagBuilder() *FlagBuilder
func (*FlagBuilder) Build ¶
func (fb *FlagBuilder) Build() uint16
func (*FlagBuilder) SetAA ¶
func (fb *FlagBuilder) SetAA(v bool) *FlagBuilder
func (*FlagBuilder) SetAD ¶
func (fb *FlagBuilder) SetAD(v bool) *FlagBuilder
func (*FlagBuilder) SetCD ¶
func (fb *FlagBuilder) SetCD(v bool) *FlagBuilder
func (*FlagBuilder) SetOpcode ¶
func (fb *FlagBuilder) SetOpcode(v uint8) *FlagBuilder
func (*FlagBuilder) SetQR ¶
func (fb *FlagBuilder) SetQR(v bool) *FlagBuilder
func (*FlagBuilder) SetRA ¶
func (fb *FlagBuilder) SetRA(v bool) *FlagBuilder
func (*FlagBuilder) SetRCODE ¶
func (fb *FlagBuilder) SetRCODE(v uint8) *FlagBuilder
func (*FlagBuilder) SetRD ¶
func (fb *FlagBuilder) SetRD(v bool) *FlagBuilder
func (*FlagBuilder) SetTC ¶
func (fb *FlagBuilder) SetTC(v bool) *FlagBuilder
type Header ¶
type Header struct {
ID uint16
Flags uint16
QDCount uint16
ANCount uint16
NSCount uint16
ARCount uint16
}
Header represents the fixed 12-byte DNS message header.
type Message ¶
type Message struct {
Header Header
Questions []Question
Answers []ResourceRecord
Authority []ResourceRecord
Additional []ResourceRecord
EDNS0 *EDNS0
// Raw holds the original wire-format bytes (needed for RDATA decompression)
Raw []byte
}
Message represents a complete DNS message.
type NSEC3Record ¶
type NSEC3Record struct {
HashAlgorithm uint8
Flags uint8
Iterations uint16
Salt []byte
NextHash []byte
TypeBitMaps []uint16
}
NSEC3Record holds parsed NSEC3 RDATA fields (RFC 5155).
func ParseNSEC3 ¶
func ParseNSEC3(rdata []byte) (*NSEC3Record, error)
ParseNSEC3 extracts NSEC3 fields from RDATA.
type NSECRecord ¶
NSECRecord holds parsed NSEC RDATA fields (RFC 4034 Section 4).
type RRSIGRecord ¶
type RRSIGRecord struct {
TypeCovered uint16
Algorithm uint8
Labels uint8
OrigTTL uint32
Expiration uint32
Inception uint32
KeyTag uint16
SignerName string
Signature []byte
}
RRSIGRecord holds parsed RRSIG RDATA fields (RFC 4034 Section 3).
func ParseRRSIG ¶
func ParseRRSIG(rdata []byte, offset int) (*RRSIGRecord, error)
ParseRRSIG extracts RRSIG fields from RDATA. The rdata slice must be self-contained (signer name not compressed). The offset parameter is unused but kept for API consistency with other name-bearing parsers; pass 0.
type ResourceRecord ¶
type ResourceRecord struct {
Name string
Type uint16
Class uint16
TTL uint32
RDLength uint16
RData []byte
// RDataOffset is the offset of this RR's RDATA in the original wire message.
// Used by type-specific parsers that need the full message for name decompression.
RDataOffset int
}
ResourceRecord represents a DNS resource record.
func BuildOPT ¶
func BuildOPT(udpSize uint16, doFlag bool) ResourceRecord
BuildOPT creates an OPT pseudo-record for outgoing queries.
func BuildOPTWithOptions ¶
func BuildOPTWithOptions(udpSize uint16, doFlag bool, options []EDNSOption) ResourceRecord
BuildOPTWithOptions creates an OPT pseudo-record with EDNS0 options.