Imports #
"errors"
"errors"
Question.Class
const ClassANY Class = 255const ClassCHAOS Class = 3const ClassCSNET Class = 2const ClassHESIOD Class = 4ResourceHeader.Class and Question.Class
const ClassINET Class = 1ErrNotStarted indicates that the prerequisite information isn't available yet because the previous records haven't been appropriately parsed, skipped or finished.
var ErrNotStarted = *ast.CallExprErrSectionDone indicated that all records in the section have been parsed or finished.
var ErrSectionDone = *ast.CallExprHeader.RCode values.
const RCodeFormatError RCode = 1Header.RCode values.
const RCodeNameError RCode = 3Header.RCode values.
const RCodeNotImplemented RCode = 4Header.RCode values.
const RCodeRefused RCode = 5Header.RCode values.
const RCodeServerFailure RCode = 2Header.RCode values.
const RCodeSuccess RCode = 0ResourceHeader.Type and Question.Type
const TypeA Type = 1const TypeAAAA Type = 28const TypeALL Type = 255const TypeAXFR Type = 252const TypeCNAME Type = 5const TypeHINFO Type = 13const TypeMINFO Type = 14const TypeMX Type = 15const TypeNS Type = 2const TypeOPT Type = 41const TypePTR Type = 12const TypeSOA Type = 6const TypeSRV Type = 33const TypeTXT Type = 16Question.Type
const TypeWKS Type = 11var classNames = map[Class]string{...}EDNS(0) wire constants.
const edns0DNSSECOK = 0x00008000EDNS(0) wire constants.
const edns0DNSSECOKMask = 0x00ff8000EDNS(0) wire constants.
const edns0Version = 0EDNS(0) wire constants.
const ednsVersionMask = 0x00ff0000var errBaseLen = *ast.CallExprvar errCalcLen = *ast.CallExprvar errInvalidName = *ast.CallExprvar errInvalidPtr = *ast.CallExprvar errNameTooLong = *ast.CallExprvar errNilResouceBody = *ast.CallExprvar errNonCanonicalName = *ast.CallExprvar errResTooLong = *ast.CallExprvar errReserved = *ast.CallExprvar errResourceLen = *ast.CallExprvar errSegTooLong = *ast.CallExprvar errStringTooLong = *ast.CallExprvar errTooManyAdditionals = *ast.CallExprvar errTooManyAnswers = *ast.CallExprvar errTooManyAuthorities = *ast.CallExprvar errTooManyPtr = *ast.CallExprvar errTooManyQuestions = *ast.CallExprvar errZeroSegLen = *ast.CallExprconst headerBitAA = *ast.BinaryExprconst headerBitAD = *ast.BinaryExprconst headerBitCD = *ast.BinaryExprconst headerBitQR = *ast.BinaryExprconst headerBitRA = *ast.BinaryExprconst headerBitRD = *ast.BinaryExprconst headerBitTC = *ast.BinaryExprheaderLen is the length (in bytes) of a DNS header. A header is comprised of 6 uint16s and no padding.
const headerLen = *ast.BinaryExprconst hexDigits = "0123456789abcdef"const nonEncodedNameMax = 254packStartingCap is the default initial buffer size allocated during packing. The starting capacity doesn't matter too much, but most DNS responses Will be <= 512 bytes as it is the limit for DNS over UDP.
const packStartingCap = 512var rCodeNames = map[RCode]string{...}const sectionAdditionalsconst sectionAnswersconst sectionAuthoritiesconst sectionDoneconst sectionHeadervar sectionNames = map[section]string{...}const sectionNotStarted section = iotaconst sectionQuestionsvar typeNames = map[Type]string{...}uint16Len is the length (in bytes) of a uint16.
const uint16Len = 2uint32Len is the length (in bytes) of a uint32.
const uint32Len = 4A Class is a type of network.
type Class uint16An OpCode is a DNS operation code.
type OpCode uint16An RCode is a DNS response status code.
type RCode uint16A Type is a type of DNS request and response.
type Type uint16type section uint8A ResourceBody is a DNS resource record minus the header.
type ResourceBody interface {
pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)
realType() Type
GoString() string
}An AAAAResource is an AAAA Resource record.
type AAAAResource struct {
AAAA [16]byte
}An AResource is an A Resource record.
type AResource struct {
A [4]byte
}A Builder allows incrementally packing a DNS message. Example usage: buf := make([]byte, 2, 514) b := NewBuilder(buf, Header{...}) b.EnableCompression() // Optionally start a section and add things to that section. // Repeat adding sections as necessary. buf, err := b.Finish() // If err is nil, buf[2:] will contain the built bytes.
type Builder struct {
msg []byte
section section
header header
start int
compression map[string]uint16
}A CNAMEResource is a CNAME Resource record.
type CNAMEResource struct {
CNAME Name
}Header is a representation of a DNS message header.
type Header struct {
ID uint16
Response bool
OpCode OpCode
Authoritative bool
Truncated bool
RecursionDesired bool
RecursionAvailable bool
AuthenticData bool
CheckingDisabled bool
RCode RCode
}An MXResource is an MX Resource record.
type MXResource struct {
Pref uint16
MX Name
}Message is a representation of a DNS message.
type Message struct {
Header
Questions []Question
Answers []Resource
Authorities []Resource
Additionals []Resource
}An NSResource is an NS Resource record.
type NSResource struct {
NS Name
}A Name is a non-encoded and non-escaped domain name. It is used instead of strings to avoid allocations.
type Name struct {
Data [255]byte
Length uint8
}An OPTResource is an OPT pseudo Resource record. The pseudo resource record is part of the extension mechanisms for DNS as defined in RFC 6891.
type OPTResource struct {
Options []Option
}An Option represents a DNS message option within OPTResource. The message option is part of the extension mechanisms for DNS as defined in RFC 6891.
type Option struct {
Code uint16
Data []byte
}A PTRResource is a PTR Resource record.
type PTRResource struct {
PTR Name
}A Parser allows incrementally parsing a DNS message. When parsing is started, the Header is parsed. Next, each Question can be either parsed or skipped. Alternatively, all Questions can be skipped at once. When all Questions have been parsed, attempting to parse Questions will return the [ErrSectionDone] error. After all Questions have been either parsed or skipped, all Answers, Authorities and Additionals can be either parsed or skipped in the same way, and each type of Resource must be fully parsed or skipped before proceeding to the next type of Resource. Parser is safe to copy to preserve the parsing state. Note that there is no requirement to fully skip or parse the message.
type Parser struct {
msg []byte
header header
section section
off int
index int
resHeaderValid bool
resHeaderOffset int
resHeaderType Type
resHeaderLength uint16
}A Question is a DNS query.
type Question struct {
Name Name
Type Type
Class Class
}A Resource is a DNS resource record.
type Resource struct {
Header ResourceHeader
Body ResourceBody
}A ResourceHeader is the header of a DNS resource record. There are many types of DNS resource records, but they all share the same header.
type ResourceHeader struct {
Name Name
Type Type
Class Class
TTL uint32
Length uint16
}An SOAResource is an SOA Resource record.
type SOAResource struct {
NS Name
MBox Name
Serial uint32
Refresh uint32
Retry uint32
Expire uint32
MinTTL uint32
}An SRVResource is an SRV Resource record.
type SRVResource struct {
Priority uint16
Weight uint16
Port uint16
Target Name
}A TXTResource is a TXT Resource record.
type TXTResource struct {
TXT []string
}An UnknownResource is a catch-all container for unknown record types.
type UnknownResource struct {
Type Type
Data []byte
}header is the wire format for a DNS message header.
type header struct {
id uint16
bits uint16
questions uint16
answers uint16
authorities uint16
additionals uint16
}type nestedError struct {
s string
err error
}AAAAResource parses a single AAAAResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) AAAAResource() (AAAAResource, error)AAAAResource adds a single AAAAResource.
func (b *Builder) AAAAResource(h ResourceHeader, r AAAAResource) errorAResource parses a single AResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) AResource() (AResource, error)AResource adds a single AResource.
func (b *Builder) AResource(h ResourceHeader, r AResource) errorAdditional parses a single Additional Resource.
func (p *Parser) Additional() (Resource, error)AdditionalHeader parses a single Additional ResourceHeader.
func (p *Parser) AdditionalHeader() (ResourceHeader, error)AllAdditionals parses all Additional Resources.
func (p *Parser) AllAdditionals() ([]Resource, error)AllAnswers parses all Answer Resources.
func (p *Parser) AllAnswers() ([]Resource, error)AllAuthorities parses all Authority Resources.
func (p *Parser) AllAuthorities() ([]Resource, error)AllQuestions parses all Questions.
func (p *Parser) AllQuestions() ([]Question, error)Answer parses a single Answer Resource.
func (p *Parser) Answer() (Resource, error)AnswerHeader parses a single Answer ResourceHeader.
func (p *Parser) AnswerHeader() (ResourceHeader, error)AppendPack is like Pack but appends the full Message to b and returns the extended buffer.
func (m *Message) AppendPack(b []byte) ([]byte, error)Authority parses a single Authority Resource.
func (p *Parser) Authority() (Resource, error)AuthorityHeader parses a single Authority ResourceHeader.
func (p *Parser) AuthorityHeader() (ResourceHeader, error)CNAMEResource adds a single CNAMEResource.
func (b *Builder) CNAMEResource(h ResourceHeader, r CNAMEResource) errorCNAMEResource parses a single CNAMEResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) CNAMEResource() (CNAMEResource, error)DNSSECAllowed reports whether the DNSSEC OK bit is set.
func (h *ResourceHeader) DNSSECAllowed() boolEnableCompression enables compression in the Builder. Leaving compression disabled avoids compression related allocations, but can result in larger message sizes. Be careful with this mode as it can cause messages to exceed the UDP size limit. According to RFC 1035, section 4.1.4, the use of compression is optional, but all implementations must accept both compressed and uncompressed DNS messages. Compression should be enabled before any sections are added for best results.
func (b *Builder) EnableCompression()nestedError implements error.Error.
func (e *nestedError) Error() stringExtendedRCode returns an extended RCode. The provided rcode must be the RCode in DNS message header.
func (h *ResourceHeader) ExtendedRCode(rcode RCode) RCodeFinish ends message building and generates a binary message.
func (b *Builder) Finish() ([]byte, error)GoString implements fmt.GoStringer.GoString.
func (m *Header) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *AAAAResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (t Type) GoString() stringfunc (r *Resource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *UnknownResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (h *ResourceHeader) GoString() stringGoString implements fmt.GoStringer.GoString.
func (m *Message) GoString() stringGoString implements fmt.GoStringer.GoString.
func (n *Name) GoString() stringGoString implements fmt.GoStringer.GoString.
func (q *Question) GoString() stringGoString implements fmt.GoStringer.GoString.
func (c Class) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *CNAMEResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *MXResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (o OpCode) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *NSResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *OPTResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *PTRResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *SOAResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *TXTResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *SRVResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r RCode) GoString() stringGoString implements fmt.GoStringer.GoString.
func (r *AResource) GoString() stringGoString implements fmt.GoStringer.GoString.
func (o *Option) GoString() stringMXResource adds a single MXResource.
func (b *Builder) MXResource(h ResourceHeader, r MXResource) errorMXResource parses a single MXResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) MXResource() (MXResource, error)MustNewName creates a new Name from a string and panics on error.
func MustNewName(name string) NameNSResource parses a single NSResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) NSResource() (NSResource, error)NSResource adds a single NSResource.
func (b *Builder) NSResource(h ResourceHeader, r NSResource) errorNewBuilder creates a new builder with compression disabled. Note: Most users will want to immediately enable compression with the EnableCompression method. See that method's comment for why you may or may not want to enable compression. The DNS message is appended to the provided initial buffer buf (which may be nil) as it is built. The final message is returned by the (*Builder).Finish method, which includes buf[:len(buf)] and may return the same underlying array if there was sufficient capacity in the slice.
func NewBuilder(buf []byte, h Header) BuilderNewName creates a new Name from a string.
func NewName(name string) (Name, error)OPTResource parses a single OPTResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) OPTResource() (OPTResource, error)OPTResource adds a single OPTResource.
func (b *Builder) OPTResource(h ResourceHeader, r OPTResource) errorPTRResource parses a single PTRResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) PTRResource() (PTRResource, error)PTRResource adds a single PTRResource.
func (b *Builder) PTRResource(h ResourceHeader, r PTRResource) errorPack packs a full Message.
func (m *Message) Pack() ([]byte, error)Question adds a single Question.
func (b *Builder) Question(q Question) errorQuestion parses a single Question.
func (p *Parser) Question() (Question, error)SOAResource adds a single SOAResource.
func (b *Builder) SOAResource(h ResourceHeader, r SOAResource) errorSOAResource parses a single SOAResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) SOAResource() (SOAResource, error)SRVResource parses a single SRVResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) SRVResource() (SRVResource, error)SRVResource adds a single SRVResource.
func (b *Builder) SRVResource(h ResourceHeader, r SRVResource) errorSetEDNS0 configures h for EDNS(0). The provided extRCode must be an extended RCode.
func (h *ResourceHeader) SetEDNS0(udpPayloadLen int, extRCode RCode, dnssecOK bool) errorSkipAdditional skips a single Additional Resource. It does not perform a complete validation of the resource header, which means it may return a nil error when the [AdditionalHeader] would actually return an error.
func (p *Parser) SkipAdditional() errorSkipAllAdditionals skips all Additional Resources.
func (p *Parser) SkipAllAdditionals() errorSkipAllAnswers skips all Answer Resources.
func (p *Parser) SkipAllAnswers() errorSkipAllAuthorities skips all Authority Resources.
func (p *Parser) SkipAllAuthorities() errorSkipAllQuestions skips all Questions.
func (p *Parser) SkipAllQuestions() errorSkipAnswer skips a single Answer Resource. It does not perform a complete validation of the resource header, which means it may return a nil error when the [AnswerHeader] would actually return an error.
func (p *Parser) SkipAnswer() errorSkipAuthority skips a single Authority Resource. It does not perform a complete validation of the resource header, which means it may return a nil error when the [AuthorityHeader] would actually return an error.
func (p *Parser) SkipAuthority() errorSkipQuestion skips a single Question.
func (p *Parser) SkipQuestion() errorStart parses the header and enables the parsing of Questions.
func (p *Parser) Start(msg []byte) (Header, error)StartAdditionals prepares the builder for packing Additionals.
func (b *Builder) StartAdditionals() errorStartAnswers prepares the builder for packing Answers.
func (b *Builder) StartAnswers() errorStartAuthorities prepares the builder for packing Authorities.
func (b *Builder) StartAuthorities() errorStartQuestions prepares the builder for packing Questions.
func (b *Builder) StartQuestions() errorString implements fmt.Stringer.String.
func (c Class) String() stringString implements fmt.Stringer.String.
func (r RCode) String() stringString implements fmt.Stringer.String.
func (t Type) String() stringString implements fmt.Stringer.String. Note: characters inside the labels are not escaped in any way.
func (n Name) String() stringTXTResource parses a single TXTResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) TXTResource() (TXTResource, error)TXTResource adds a single TXTResource.
func (b *Builder) TXTResource(h ResourceHeader, r TXTResource) errorUnknownResource parses a single UnknownResource. One of the XXXHeader methods must have been called before calling this method.
func (p *Parser) UnknownResource() (UnknownResource, error)UnknownResource adds a single UnknownResource.
func (b *Builder) UnknownResource(h ResourceHeader, r UnknownResource) errorUnpack parses a full Message.
func (m *Message) Unpack(msg []byte) errorfunc (p *Parser) checkAdvance(sec section) errorfunc (b *Builder) checkResourceSection() errorfunc (h *header) count(sec section) uint16fixLen updates a packed ResourceHeader to include the length of the ResourceBody. lenOff is the offset of the ResourceHeader.Length field in msg. preLen is the length that msg was before the ResourceBody was packed.
func (h *ResourceHeader) fixLen(msg []byte, lenOff int, preLen int) errorfunc (h *header) header() Headerfunc (b *Builder) incrementSectionCount() errorpack appends the wire format of the AAAAResource to msg.
func (r *AAAAResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the TXTResource to msg.
func (r *TXTResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the header to msg.
func (h *header) pack(msg []byte) []bytepack appends the wire format of the AResource to msg.
func (r *AResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the MXResource to msg.
func (r *MXResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the Question to msg.
func (q *Question) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the SRVResource to msg.
func (r *SRVResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the UnknownResource to msg.
func (r *UnknownResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the CNAMEResource to msg.
func (r *CNAMEResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the Resource to msg.
func (r *Resource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the NSResource to msg.
func (r *NSResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the SOAResource to msg.
func (r *SOAResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the Name to msg. Domain names are a sequence of counted strings split at the dots. They end with a zero-length string. Compression can be used to reuse domain suffixes. The compression map will be updated with new domain suffixes. If compression is nil, compression will not be used.
func (n *Name) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)pack appends the wire format of the ResourceHeader to oldMsg. lenOff is the offset in msg where the Length field was packed.
func (h *ResourceHeader) pack(oldMsg []byte, compression map[string]uint16, compressionOff int) (msg []byte, lenOff int, err error)pack appends the wire format of the PTRResource to msg.
func (r *PTRResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)func (r *OPTResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)func (m *Header) pack() (id uint16, bits uint16)packBytes appends the wire format of field to msg.
func packBytes(msg []byte, field []byte) []bytepackClass appends the wire format of field to msg.
func packClass(msg []byte, field Class) []bytepackText appends the wire format of field to msg.
func packText(msg []byte, field string) ([]byte, error)packType appends the wire format of field to msg.
func packType(msg []byte, field Type) []bytepackUint16 appends the wire format of field to msg.
func packUint16(msg []byte, field uint16) []bytepackUint32 appends the wire format of field to msg.
func packUint32(msg []byte, field uint32) []bytefunc printBool(b bool) stringfunc printByteSlice(b []byte) stringfunc printPaddedUint8(i uint8) stringfunc printString(str []byte) stringfunc printUint16(i uint16) stringfunc printUint32(i uint32) stringfunc printUint8Bytes(buf []byte, i uint8) []bytefunc (r *CNAMEResource) realType() Typefunc (r *AResource) realType() Typefunc (r *OPTResource) realType() Typefunc (r *TXTResource) realType() Typefunc (r *SOAResource) realType() Typefunc (r *NSResource) realType() Typefunc (r *SRVResource) realType() Typefunc (r *UnknownResource) realType() Typefunc (r *AAAAResource) realType() Typefunc (r *PTRResource) realType() Typefunc (r *MXResource) realType() Typefunc (p *Parser) resource(sec section) (Resource, error)func (p *Parser) resourceHeader(sec section) (ResourceHeader, error)func skipClass(msg []byte, off int) (int, error)func skipName(msg []byte, off int) (int, error)func skipResource(msg []byte, off int) (int, error)func (p *Parser) skipResource(sec section) errorfunc skipType(msg []byte, off int) (int, error)func skipUint16(msg []byte, off int) (int, error)func skipUint32(msg []byte, off int) (int, error)func (b *Builder) startCheck(s section) errorfunc (h *header) unpack(msg []byte, off int) (int, error)unpack unpacks a domain name.
func (n *Name) unpack(msg []byte, off int) (int, error)func (h *ResourceHeader) unpack(msg []byte, off int) (int, error)func unpackAAAAResource(msg []byte, off int) (AAAAResource, error)func unpackAResource(msg []byte, off int) (AResource, error)func unpackBytes(msg []byte, off int, field []byte) (int, error)func unpackCNAMEResource(msg []byte, off int) (CNAMEResource, error)func unpackClass(msg []byte, off int) (Class, int, error)func unpackMXResource(msg []byte, off int) (MXResource, error)func unpackNSResource(msg []byte, off int) (NSResource, error)func unpackOPTResource(msg []byte, off int, length uint16) (OPTResource, error)func unpackPTRResource(msg []byte, off int) (PTRResource, error)func unpackResourceBody(msg []byte, off int, hdr ResourceHeader) (ResourceBody, int, error)func unpackSOAResource(msg []byte, off int) (SOAResource, error)func unpackSRVResource(msg []byte, off int) (SRVResource, error)func unpackTXTResource(msg []byte, off int, length uint16) (TXTResource, error)func unpackText(msg []byte, off int) (string, int, error)func unpackType(msg []byte, off int) (Type, int, error)func unpackUint16(msg []byte, off int) (uint16, int, error)func unpackUint32(msg []byte, off int) (uint32, int, error)func unpackUnknownResource(recordType Type, msg []byte, off int, length uint16) (UnknownResource, error)Generated with Arrow