dns

package module
v0.0.0-...-bdc7661 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeNone       Type = 0
	TypeA          Type = 1
	TypeNS         Type = 2
	TypeMD         Type = 3
	TypeMF         Type = 4
	TypeCNAME      Type = 5
	TypeSOA        Type = 6
	TypeMB         Type = 7
	TypeMG         Type = 8
	TypeMR         Type = 9
	TypeNULL       Type = 10
	TypePTR        Type = 12
	TypeHINFO      Type = 13
	TypeMINFO      Type = 14
	TypeMX         Type = 15
	TypeTXT        Type = 16
	TypeRP         Type = 17
	TypeAFSDB      Type = 18
	TypeX25        Type = 19
	TypeISDN       Type = 20
	TypeRT         Type = 21
	TypeNSAPPTR    Type = 23
	TypeSIG        Type = 24
	TypeKEY        Type = 25
	TypePX         Type = 26
	TypeGPOS       Type = 27
	TypeAAAA       Type = 28
	TypeLOC        Type = 29
	TypeNXT        Type = 30
	TypeEID        Type = 31
	TypeNIMLOC     Type = 32
	TypeSRV        Type = 33
	TypeATMA       Type = 34
	TypeNAPTR      Type = 35
	TypeKX         Type = 36
	TypeCERT       Type = 37
	TypeDNAME      Type = 39
	TypeOPT        Type = 41 // EDNS
	TypeAPL        Type = 42
	TypeDS         Type = 43
	TypeSSHFP      Type = 44
	TypeIPSECKEY   Type = 45
	TypeRRSIG      Type = 46
	TypeNSEC       Type = 47
	TypeDNSKEY     Type = 48
	TypeDHCID      Type = 49
	TypeNSEC3      Type = 50
	TypeNSEC3PARAM Type = 51
	TypeTLSA       Type = 52
	TypeSMIMEA     Type = 53
	TypeHIP        Type = 55
	TypeNINFO      Type = 56
	TypeRKEY       Type = 57
	TypeTALINK     Type = 58
	TypeCDS        Type = 59
	TypeCDNSKEY    Type = 60
	TypeOPENPGPKEY Type = 61
	TypeCSYNC      Type = 62
	TypeZONEMD     Type = 63
	TypeSVCB       Type = 64
	TypeHTTPS      Type = 65
	TypeSPF        Type = 99
	TypeUINFO      Type = 100
	TypeUID        Type = 101
	TypeGID        Type = 102
	TypeUNSPEC     Type = 103
	TypeNID        Type = 104
	TypeL32        Type = 105
	TypeL64        Type = 106
	TypeLP         Type = 107
	TypeEUI48      Type = 108
	TypeEUI64      Type = 109
	TypeURI        Type = 256
	TypeCAA        Type = 257
	TypeAVC        Type = 258
	TypeAMTRELAY   Type = 260

	TypeTKEY Type = 249
	TypeTSIG Type = 250

	// valid Question.Qtype only
	TypeIXFR  Type = 251
	TypeAXFR  Type = 252
	TypeMAILB Type = 253
	TypeMAILA Type = 254
	TypeANY   Type = 255

	TypeTA       Type = 32768
	TypeDLV      Type = 32769
	TypeReserved Type = 65535

	// valid Question.Qclass
	ClassINET   Class = 1
	ClassCSNET  Class = 2
	ClassCHAOS  Class = 3
	ClassHESIOD Class = 4
	ClassNONE   Class = 254
	ClassANY    Class = 255
)

Wire constants and supported types.

Variables

View Source
var (
	ErrBuf        error = errors.New("buffer size too small") // ErrBuf indicates that the buffer used is too small for the message.
	ErrLongDomain error = fmt.Errorf("domain name exceeded %d wire-format octets", maxDomainNameWireOctets)
	ErrRdata      error = errors.New("dns: invalid rdata in message")
)
View Source
var (
	// ErrInvalidHeader is returned when dns message does not have the expected header size.
	ErrInvalidHeader = errors.New("dns message does not have the expected header size")
	// ErrInvalidQuestion is returned when dns message does not have the expected question size.
	ErrInvalidQuestion = errors.New("dns message does not have the expected question size")
	// ErrInvalidAnswer is returned when dns message does not have the expected answer size.
	ErrInvalidAnswer = errors.New("dns message does not have the expected answer size")
	// ErrInvalidOPT is returned when dns message does not have the expected OPT size.
	ErrInvalidOPT = errors.New("dns message does not have the expected OPT size")
	//ErrInvalidRR is returned when dns message does not have the expected RR size.
	ErrInvalidRR = errors.New("dns message does not have the expected RR size")
)
View Source
var ClassToString = map[Class]string{
	ClassINET:   "IN",
	ClassCSNET:  "CS",
	ClassCHAOS:  "CH",
	ClassHESIOD: "HS",
	ClassNONE:   "NONE",
	ClassANY:    "ANY",
}

ClassToString is a maps Classes to strings for each CLASS wire type.

View Source
var OpcodeToString = map[Opcode]string{
	OpcodeQuery:  "QUERY",
	OpcodeIQuery: "IQUERY",
	OpcodeStatus: "STATUS",
	OpcodeNotify: "NOTIFY",
	OpcodeUpdate: "UPDATE",
}

OpcodeToString maps Opcodes to strings.

View Source
var RcodeToString = map[Rcode]string{
	RcodeSuccess:        "NOERROR",
	RcodeFormatError:    "FORMERR",
	RcodeServerFailure:  "SERVFAIL",
	RcodeNameError:      "NXDOMAIN",
	RcodeNotImplemented: "NOTIMP",
	RcodeRefused:        "REFUSED",
	RcodeYXDomain:       "YXDOMAIN",
	RcodeYXRrset:        "YXRRSET",
	RcodeNXRrset:        "NXRRSET",
	RcodeNotAuth:        "NOTAUTH",
	RcodeNotZone:        "NOTZONE",
	RcodeBadSig:         "BADSIG",

	RcodeBadKey:    "BADKEY",
	RcodeBadTime:   "BADTIME",
	RcodeBadMode:   "BADMODE",
	RcodeBadName:   "BADNAME",
	RcodeBadAlg:    "BADALG",
	RcodeBadTrunc:  "BADTRUNC",
	RcodeBadCookie: "BADCOOKIE",
}

RcodeToString maps Rcodes to strings.

View Source
var TypeToRR = map[Type]func() RR{
	TypeA:     func() RR { return new(A) },
	TypeNS:    func() RR { return new(NS) },
	TypeCNAME: func() RR { return new(CNAME) },
	TypeSOA:   func() RR { return new(SOA) },
	TypePTR:   func() RR { return new(PTR) },
	TypeMX:    func() RR { return new(MX) },
	TypeTXT:   func() RR { return new(TXT) },
	TypeSRV:   func() RR { return new(SRV) },
	TypeAAAA:  func() RR { return new(AAAA) },
	TypeOPT:   func() RR { return new(OPT) },
}

TypeToRR is a map of constructors for each RR type.

Functions

func AcquireBuffer

func AcquireBuffer() *[]byte

AcquireBuffer gets a buffer from the pool

func DecodeDomain

func DecodeDomain(domain []byte) []byte

func EncodeDomain

func EncodeDomain(dst []byte, domain string) []byte

EncodeDomain encodes domain to dst. If dst has enough capacity, this will be zero-allocation.

func ReleaseBuffer

func ReleaseBuffer(b *[]byte)

ReleaseBuffer returns a buffer to the pool

func ReleaseRequest

func ReleaseRequest(msg *Request)

ReleaseRequest returnes the dns request to the pool.

func ReleaseResponse

func ReleaseResponse(msg *Response)

ReleaseResponse returnes the dns response to the pool.

func UnpackDomainName

func UnpackDomainName(msg []byte, off int) ([]byte, int, error)

Types

type A

type A struct {
	A   [4]byte // Fixed-size array to avoid net.IP allocations
	Hdr RR_Header
}

func (*A) Header

func (rr *A) Header() *RR_Header

func (*A) String

func (rr *A) String() string

type AAAA

type AAAA struct {
	Hdr  RR_Header
	AAAA net.IP
}

AAAA record

func (*AAAA) Header

func (rr *AAAA) Header() *RR_Header

func (*AAAA) String

func (rr *AAAA) String() string

type CNAME

type CNAME struct {
	Hdr   RR_Header
	CNAME string
}

CNAME record

func (*CNAME) Header

func (rr *CNAME) Header() *RR_Header

func (*CNAME) String

func (rr *CNAME) String() string

type Class

type Class uint16

Class is a DNS class.

func (Class) String

func (c Class) String() string

type Error

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

Error represents a DNS error.

func (*Error) Error

func (e *Error) Error() string
type Header struct {
	ID uint16
	// Flags is an arbitrary 16bit represents QR, Opcode, AA, TC, RD, RA, Z and RCODE.
	//
	//   0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
	// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	// |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
	// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	Bits uint16
	// Qdcount specifies the number of entries in the question section
	// Ancount specifies the number of resource records in the answer section
	// Nscount specifies the number of name server resource records in the authority records section
	// Arcount specifies the number of resource records in the additional records section
	Qdcount, Ancount, Nscount, Arcount uint16
}

Header is the wire format for the DNS packet header.

func (*Header) AuthenticatedData

func (h *Header) AuthenticatedData() bool

func (*Header) Authoritative

func (h *Header) Authoritative() bool

func (*Header) CheckingDisabled

func (h *Header) CheckingDisabled() bool

func (*Header) OpCode

func (h *Header) OpCode() Opcode

func (*Header) Pack

func (h *Header) Pack() [headerSize]byte

Pack returns the wire format of the header.

func (*Header) Rcode

func (h *Header) Rcode() Rcode

func (*Header) RecursionAvailable

func (h *Header) RecursionAvailable() bool

func (*Header) RecursionDesired

func (h *Header) RecursionDesired() bool

func (*Header) Response

func (h *Header) Response() bool

func (*Header) SetAuthenticatedData

func (h *Header) SetAuthenticatedData()

func (*Header) SetAuthoritative

func (h *Header) SetAuthoritative()

func (*Header) SetCheckingDisabled

func (h *Header) SetCheckingDisabled()

func (*Header) SetOpCode

func (h *Header) SetOpCode(op Opcode)

func (*Header) SetRcode

func (h *Header) SetRcode(rcode Rcode)

func (*Header) SetRecursionAvailable

func (h *Header) SetRecursionAvailable()

func (*Header) SetRecursionDesired

func (h *Header) SetRecursionDesired()

func (*Header) SetResponse

func (h *Header) SetResponse()

func (*Header) SetTruncated

func (h *Header) SetTruncated()

func (*Header) SetZero

func (h *Header) SetZero()

func (*Header) String

func (h *Header) String() string

func (*Header) Truncated

func (h *Header) Truncated() bool

func (*Header) Unpack

func (h *Header) Unpack(b []byte) error

func (*Header) Zero

func (h *Header) Zero() bool

type MX

type MX struct {
	Hdr        RR_Header
	Preference uint16
	MX         string
}

MX record

func (*MX) Header

func (rr *MX) Header() *RR_Header

func (*MX) String

func (rr *MX) String() string

type NS

type NS struct {
	Hdr RR_Header
	NS  string
}

NS 记录

func (*NS) Header

func (rr *NS) Header() *RR_Header

func (*NS) String

func (rr *NS) String() string

type Name

type Name struct {
	Data   [255]byte
	Length uint8
}

A Name is a non-encoded and non-escaped domain name. It is used instead of strings to avoid allocations.

type OPT

type OPT struct {
	// Name     string
	// Type     Type
	// MaxSize  uint16
	// TTL      uint32
	// RDLength uint16
	Options []Option
	Hdr     RR_Header
}

func (*OPT) AddOption

func (r *OPT) AddOption(code OptionCode, data []byte)

func (*OPT) Header

func (rr *OPT) Header() *RR_Header

func (*OPT) Pack

func (r *OPT) Pack() []byte

Pack returns the wire format of the OPT RR (not including the name field which is always root). This returns just the RDATA part (options), not the full RR.

func (*OPT) String

func (r *OPT) String() string

func (*OPT) Unpack

func (r *OPT) Unpack(data []byte) error

type Opcode

type Opcode byte

Opcode denotes a 4bit field that specified the query type.

const (
	OpcodeQuery  Opcode = 0
	OpcodeIQuery Opcode = 1
	OpcodeStatus Opcode = 2
	OpcodeNotify Opcode = 4
	OpcodeUpdate Opcode = 5
)

Wire constants and supported types.

func (Opcode) String

func (c Opcode) String() string

type Option

type Option struct {
	Data   []byte
	Code   OptionCode
	Length uint16
}

func (*Option) String

func (o *Option) String() string

type OptionCode

type OptionCode uint16

OptionCode represents the code of a DNS Option, see RFC6891, section 6.1.2

const (
	OptionCodeNSID             OptionCode = 3
	OptionCodeDAU              OptionCode = 5
	OptionCodeDHU              OptionCode = 6
	OptionCodeN3U              OptionCode = 7
	OptionCodeEDNSClientSubnet OptionCode = 8
	OptionCodeEDNSExpire       OptionCode = 9
	OptionCodeCookie           OptionCode = 10
	OptionCodeEDNSKeepAlive    OptionCode = 11
	OptionCodePadding          OptionCode = 12
	OptionCodeChain            OptionCode = 13
	OptionCodeEDNSKeyTag       OptionCode = 14
	OptionCodeEDNSClientTag    OptionCode = 16
	OptionCodeEDNSServerTag    OptionCode = 17
	OptionCodeDeviceID         OptionCode = 26946
)

OptionCode known values. See IANA

func (OptionCode) String

func (c OptionCode) String() string

type PTR

type PTR struct {
	Hdr RR_Header
	Ptr string
}

PTR record (Pointer) RFC 1035, section 3.3.12

func (*PTR) Header

func (rr *PTR) Header() *RR_Header

func (*PTR) String

func (rr *PTR) String() string

type Question

type Question struct {
	// Name refers to the raw query name to be resolved in the query.
	//
	// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	// |                                               |
	// /                     QNAME                     /
	// /                                               /
	// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	Name []byte

	// Type specifies the type of the query to perform.
	//
	// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	// |                     QTYPE                     |
	// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	Type Type

	// Class specifies the class of the query to perform.
	//
	// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	// |                     QCLASS                    |
	// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	Class Class
}

type RFC3597

type RFC3597 struct {
	Hdr   RR_Header
	Rdata string `dns:"hex"`
}

RFC3597 represents an unknown/generic RR. See RFC 3597.

func (*RFC3597) Header

func (rr *RFC3597) Header() *RR_Header

func (*RFC3597) String

func (rr *RFC3597) String() string

type RR

type RR interface {
	Header() *RR_Header

	String() string
	// contains filtered or unexported methods
}

func UnpackRR

func UnpackRR(msg []byte, off int) (rr RR, off1 int, err error)

UnpackRR unpacks msg[off:] into an RR.

func UnpackRRWithHeader

func UnpackRRWithHeader(h RR_Header, msg []byte, off int) (rr RR, off1 int, err error)

UnpackRRWithHeader unpacks the record type specific payload given an existing RR_Header.

type RR_Header

type RR_Header struct {
	Name     string `dns:"cdomain-name"`
	Rrtype   Type
	Class    Class
	Ttl      uint32
	Rdlength uint16 // Length of data after header.
}

func (*RR_Header) Header

func (h *RR_Header) Header() *RR_Header

func (*RR_Header) String

func (h *RR_Header) String() string

type Rcode

type Rcode uint16
const (
	// Message Response Codes, see https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml
	RcodeSuccess        Rcode = 0  // NoError   - No Error                          [DNS]
	RcodeFormatError    Rcode = 1  // FormErr   - Format Error                      [DNS]
	RcodeServerFailure  Rcode = 2  // ServFail  - Server Failure                    [DNS]
	RcodeNameError      Rcode = 3  // NXDomain  - Non-Existent Domain               [DNS]
	RcodeNotImplemented Rcode = 4  // NotImp    - Not Implemented                   [DNS]
	RcodeRefused        Rcode = 5  // Refused   - Query Refused                     [DNS]
	RcodeYXDomain       Rcode = 6  // YXDomain  - Name Exists when it should not    [DNS Update]
	RcodeYXRrset        Rcode = 7  // YXRRSet   - RR Set Exists when it should not  [DNS Update]
	RcodeNXRrset        Rcode = 8  // NXRRSet   - RR Set that should exist does not [DNS Update]
	RcodeNotAuth        Rcode = 9  // NotAuth   - Server Not Authoritative for zone [DNS Update]
	RcodeNotZone        Rcode = 10 // NotZone   - Name not contained in zone        [DNS Update/TSIG]
	RcodeBadSig         Rcode = 16 // BADSIG    - TSIG Signature Failure            [TSIG]  https://www.rfc-editor.org/rfc/rfc6895.html#section-2.3
	RcodeBadVers        Rcode = 16 // BADVERS   - Bad OPT Version                   [EDNS0] https://www.rfc-editor.org/rfc/rfc6895.html#section-2.3
	RcodeBadKey         Rcode = 17 // BADKEY    - Key not recognized                [TSIG]
	RcodeBadTime        Rcode = 18 // BADTIME   - Signature out of time window      [TSIG]
	RcodeBadMode        Rcode = 19 // BADMODE   - Bad TKEY Mode                     [TKEY]
	RcodeBadName        Rcode = 20 // BADNAME   - Duplicate key name                [TKEY]
	RcodeBadAlg         Rcode = 21 // BADALG    - Algorithm not supported           [TKEY]
	RcodeBadTrunc       Rcode = 22 // BADTRUNC  - Bad Truncation                    [TSIG]
	RcodeBadCookie      Rcode = 23 // BADCOOKIE - Bad/missing Server Cookie         [DNS Cookies]
)

func (Rcode) String

func (r Rcode) String() string

type Request

type Request struct {
	OPT      OPT
	Raw      []byte
	Domain   []byte
	Question Question
	Header   Header
}

func AcquireRequest

func AcquireRequest() *Request

AcquireRequest returns new dns request.

func (*Request) Reset

func (r *Request) Reset()

func (*Request) SetEDNS0

func (r *Request) SetEDNS0(maxSize uint16, do bool)

func (*Request) SetEDNS0Chain

func (r *Request) SetEDNS0Chain(chain []byte)

func (*Request) SetEDNS0ClientSubnet

func (r *Request) SetEDNS0ClientSubnet(clientSubnet netip.Prefix) error

// e.Code = dns.EDNS0SUBNET // by default this is filled in through unpacking OPT packets (unpackDataOpt) // e.Family = 1 // 1 for IPv4 source address, 2 for IPv6 // e.SourceNetmask = 32 // 32 for IPV4, 128 for IPv6 // e.SourceScope = 0 // e.Address = net.ParseIP("127.0.0.1").To4() // for IPv4 // // e.Address = net.ParseIP("2001:7b8:32a::2") // for IPV6

func (*Request) SetEDNS0Cookie

func (r *Request) SetEDNS0Cookie(cookie []byte)

func (*Request) SetEDNS0Keepalive

func (r *Request) SetEDNS0Keepalive(timeout uint16)

func (*Request) SetEDNS0NSID

func (r *Request) SetEDNS0NSID(nsid string)

func (*Request) SetEDNS0Padding

func (r *Request) SetEDNS0Padding(size int)

func (*Request) SetQuestion

func (r *Request) SetQuestion(domain string, typ Type, class Class)

func (*Request) Unpack

func (r *Request) Unpack(payload []byte) error

type Response

type Response struct {
	Answer []RR
	Ns     []RR
	Extra  []RR
	// Question holds the question section of the response message.
	Question Question
	// Header is the wire format for the DNS packet header.
	Header Header
}

func AcquireResponse

func AcquireResponse() *Response

AcquireResponse returns new dns response.

func (*Response) Pack

func (r *Response) Pack() []byte

func (*Response) Reset

func (r *Response) Reset()

func (*Response) SetQuestion

func (r *Response) SetQuestion(name string, typ Type, class Class)

func (*Response) Unpack

func (r *Response) Unpack(payload []byte) error

type SOA

type SOA struct {
	Hdr     RR_Header
	Ns      string // Primary name server
	Mbox    string // Responsible mailbox
	Serial  uint32 // Serial number
	Refresh uint32 // Refresh interval
	Retry   uint32 // Retry interval
	Expire  uint32 // Expire limit
	Minttl  uint32 // Minimum TTL
}

SOA record (Start of Authority) RFC 1035, section 3.3.13

func (*SOA) Header

func (rr *SOA) Header() *RR_Header

func (*SOA) String

func (rr *SOA) String() string

type SRV

type SRV struct {
	Hdr      RR_Header
	Priority uint16 // Priority
	Weight   uint16 // Weight
	Port     uint16 // Port
	Target   string // Target domain name
}

SRV record (Service) RFC 2782

func (*SRV) Header

func (rr *SRV) Header() *RR_Header

func (*SRV) String

func (rr *SRV) String() string

type TXT

type TXT struct {
	Hdr RR_Header
	TXT []string
}

TXT record

func (*TXT) Header

func (rr *TXT) Header() *RR_Header

func (*TXT) String

func (rr *TXT) String() string

type Type

type Type uint16

Type is a DNS type.

func ParseType

func ParseType(s string) (t Type)

ParseType converts a question type string into a question type value.

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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