dns

package
v0.6.16 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 5 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

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

View Source
const (
	DigestSHA1   uint8 = 1
	DigestSHA256 uint8 = 2
	DigestSHA384 uint8 = 4
)

DNSSEC digest types (RFC 8624).

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

View Source
const (
	EDNSOptionCodeECS    uint16 = 8
	EDNSOptionCodeCookie uint16 = 10
	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.

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

Record types

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

View Source
const (
	OpcodeQuery  uint8 = 0
	OpcodeIQuery uint8 = 1
	OpcodeStatus uint8 = 2
)

Opcodes

View Source
const (
	ClassIN uint16 = 1
)

Classes

Variables

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

View Source
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",
	TypeANY: "ANY",
}

TypeToString maps type values to human-readable names.

Functions

func BuildPlainName

func BuildPlainName(name string) []byte

BuildPlainName encodes a domain name as uncompressed wire-format label sequence. Exported for use in tests and delegation handling.

func DecodeName

func DecodeName(msg []byte, offset int) (string, int, error)

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

func EncodeName(w *wireWriter, name string) error

EncodeName writes a domain name in wire format with optional compression.

func EncodeNameToBytes added in v0.6.15

func EncodeNameToBytes(name string) ([]byte, error)

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 Pack

func Pack(msg *Message, buf []byte) ([]byte, error)

Pack serializes a DNS message to wire format.

func ParseA

func ParseA(rdata []byte) (net.IP, error)

ParseA extracts an IPv4 address from A record RDATA.

func ParseAAAA

func ParseAAAA(rdata []byte) (net.IP, error)

ParseAAAA extracts an IPv6 address from AAAA record RDATA.

func ParseCNAME

func ParseCNAME(msg []byte, rdataOffset int) (string, error)

ParseCNAME extracts the canonical name from CNAME record RDATA.

func ParseCookieOption

func ParseCookieOption(data []byte) (clientCookie []byte, serverCookie []byte)

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

func ParseDNAME(msg []byte, rdataOffset int) (string, error)

ParseDNAME extracts the delegation name from DNAME record RDATA (RFC 6672).

func ParseEDEOption

func ParseEDEOption(data []byte) (infoCode uint16, extraText string, err error)

ParseEDEOption parses an Extended DNS Error option from EDNS0 option data. Returns the info code and extra text.

func ParseNS

func ParseNS(msg []byte, rdataOffset int) (string, error)

ParseNS extracts the nameserver hostname from NS record RDATA.

func ParsePTR

func ParsePTR(msg []byte, rdataOffset int) (string, error)

ParsePTR extracts the pointer domain name from PTR record RDATA.

func ParseTXT

func ParseTXT(rdata []byte) ([]string, error)

ParseTXT extracts character strings from TXT record RDATA.

func TruncateIP

func TruncateIP(ip net.IP, prefixLen uint8) net.IP

TruncateIP truncates an IP address to the given prefix length, zeroing out any bits beyond the prefix.

Types

type DNSKEYRecord

type DNSKEYRecord struct {
	Flags     uint16
	Protocol  uint8
	Algorithm uint8
	PublicKey []byte
}

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) KeyTag

func (k *DNSKEYRecord) KeyTag() uint16

KeyTag computes the key tag per RFC 4034 Appendix B.

type DSRecord

type DSRecord struct {
	KeyTag     uint16
	Algorithm  uint8
	DigestType uint8
	Digest     []byte
}

DSRecord holds parsed DS RDATA fields (RFC 4034 Section 5).

func ParseDS

func ParseDS(rdata []byte) (*DSRecord, error)

ParseDS extracts DS fields from RDATA.

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

func ExtractECSFromOPT(opt *EDNS0) (*ECSOption, error)

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 ParseECS

func ParseECS(data []byte) (*ECSOption, error)

ParseECS parses an EDNS Client Subnet option from EDNS0 option data.

func (*ECSOption) CacheKey added in v0.6.9

func (ecs *ECSOption) CacheKey() string

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

type EDNSOption struct {
	Code uint16
	Data []byte
}

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.

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 struct {
	ID      uint16
	Flags   uint16
	QDCount uint16
	ANCount uint16
	NSCount uint16
	ARCount uint16
}

Header represents the fixed 12-byte DNS message header.

func (*Header) AA

func (h *Header) AA() bool

func (*Header) AD

func (h *Header) AD() bool

func (*Header) CD

func (h *Header) CD() bool

func (*Header) Opcode

func (h *Header) Opcode() uint8

func (*Header) Pack

func (h *Header) Pack(w *wireWriter) error

Pack writes the header to a wireWriter.

func (*Header) QR

func (h *Header) QR() bool

func (*Header) RA

func (h *Header) RA() bool

func (*Header) RCODE

func (h *Header) RCODE() uint8

func (*Header) RD

func (h *Header) RD() bool

func (*Header) TC

func (h *Header) TC() bool

func (*Header) Unpack

func (h *Header) Unpack(r *wireReader) error

Unpack reads the header from a wireReader.

type MXRecord

type MXRecord struct {
	Preference uint16
	Exchange   string
}

MXRecord holds parsed MX RDATA fields.

func ParseMX

func ParseMX(msg []byte, rdataOffset int) (*MXRecord, error)

ParseMX extracts preference and exchange from MX record RDATA.

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.

func Unpack

func Unpack(buf []byte) (*Message, error)

Unpack parses a DNS message from wire format.

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

type NSECRecord struct {
	NextDomainName string
	TypeBitMaps    []uint16
}

NSECRecord holds parsed NSEC RDATA fields (RFC 4034 Section 4).

func ParseNSEC

func ParseNSEC(rdata []byte, offset int) (*NSECRecord, error)

ParseNSEC extracts NSEC fields from RDATA. The rdata slice must be self-contained (next domain name not compressed). The offset parameter is unused but kept for API consistency; pass 0.

type Question

type Question struct {
	Name  string
	Type  uint16
	Class uint16
}

Question represents a DNS question entry.

func UnpackQuestion

func UnpackQuestion(msg []byte, offset int) (Question, int, error)

UnpackQuestion decodes a question entry from the wire format.

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.

func UnpackRR

func UnpackRR(msg []byte, offset int) (ResourceRecord, int, error)

UnpackRR decodes a resource record from the wire format. For types with compressed domain names in RDATA (NS, CNAME, PTR, MX, SOA, SRV), the names are decompressed during unpack so stored RDATA is self-contained.

type SOARecord

type SOARecord struct {
	MName   string
	RName   string
	Serial  uint32
	Refresh uint32
	Retry   uint32
	Expire  uint32
	Minimum uint32
}

SOARecord holds parsed SOA RDATA fields.

func ParseSOA

func ParseSOA(msg []byte, rdataOffset int) (*SOARecord, error)

ParseSOA extracts all SOA fields from SOA record RDATA.

type SRVRecord

type SRVRecord struct {
	Priority uint16
	Weight   uint16
	Port     uint16
	Target   string
}

SRVRecord holds parsed SRV RDATA fields.

func ParseSRV

func ParseSRV(msg []byte, rdataOffset int) (*SRVRecord, error)

ParseSRV extracts all SRV fields from SRV record RDATA.

Jump to

Keyboard shortcuts

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