Involved Source Files
Package dnsmessage provides a mostly RFC 1035 compliant implementation of
DNS message packing and unpacking.
The package also supports messages with Extension Mechanisms for DNS
(EDNS(0)) as defined in RFC 6891.
This implementation is designed to minimize heap allocations and avoid
unnecessary packing and unpacking as much as possible.
Package-Level Type Names (total 27, in which 24 are exported)
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.
compression is a mapping from name suffixes to their starting index
in msg.
header keeps track of what should go in the header when Finish is
called.
msg is the storage for the message being built.
section keeps track of the current section being built.
start is the starting index of the bytes allocated in msg for header.
AAAAResource adds a single AAAAResource.
AResource adds a single AResource.
CNAMEResource adds a single CNAMEResource.
EnableCompression 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.
Finish ends message building and generates a binary message.
MXResource adds a single MXResource.
NSResource adds a single NSResource.
OPTResource adds a single OPTResource.
PTRResource adds a single PTRResource.
Question adds a single Question.
SOAResource adds a single SOAResource.
SRVResource adds a single SRVResource.
StartAdditionals prepares the builder for packing Additionals.
StartAnswers prepares the builder for packing Answers.
StartAuthorities prepares the builder for packing Authorities.
StartQuestions prepares the builder for packing Questions.
TXTResource adds a single TXTResource.
(*T) checkResourceSection() error(*T) incrementSectionCount() error(*T) startCheck(s section) error
func NewBuilder(buf []byte, h Header) Builder
A Name is a non-encoded domain name. It is used instead of strings to avoid
allocations.
Data[255]byteLengthuint8
GoString implements fmt.GoStringer.GoString.
String implements fmt.Stringer.String.
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.
unpack unpacks a domain name.
(*T) unpackCompressed(msg []byte, off int, allowCompression bool) (int, error)
*T : fmt.GoStringer
T : fmt.Stringer
T : context.stringer
T : runtime.stringer
func MustNewName(name string) Name
func NewName(name string) (Name, error)
func net.(*Resolver).goLookupIPCNAMEOrder(ctx context.Context, name string, order net.hostLookupOrder) (addrs []net.IPAddr, cname Name, err error)
func net.equalASCIIName(x, y Name) bool
An OpCode is a DNS operation code.
GoString implements fmt.GoStringer.GoString.
T : fmt.GoStringer
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.
// option code
Data[]byte
GoString implements fmt.GoStringer.GoString.
*T : fmt.GoStringer
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 (nil, nil) and attempting to skip Questions will return
(true, nil). 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.
Note that there is no requirement to fully skip or parse the message.
headerheaderindexintmsg[]byteoffintresHeaderResourceHeaderresHeaderValidboolsectionsection
AAAAResource parses a single AAAAResource.
One of the XXXHeader methods must have been called before calling this
method.
AResource parses a single AResource.
One of the XXXHeader methods must have been called before calling this
method.
Additional parses a single Additional Resource.
AdditionalHeader parses a single Additional ResourceHeader.
AllAdditionals parses all Additional Resources.
AllAnswers parses all Answer Resources.
AllAuthorities parses all Authority Resources.
AllQuestions parses all Questions.
Answer parses a single Answer Resource.
AnswerHeader parses a single Answer ResourceHeader.
Authority parses a single Authority Resource.
AuthorityHeader parses a single Authority ResourceHeader.
CNAMEResource parses a single CNAMEResource.
One of the XXXHeader methods must have been called before calling this
method.
MXResource parses a single MXResource.
One of the XXXHeader methods must have been called before calling this
method.
NSResource parses a single NSResource.
One of the XXXHeader methods must have been called before calling this
method.
OPTResource parses a single OPTResource.
One of the XXXHeader methods must have been called before calling this
method.
PTRResource parses a single PTRResource.
One of the XXXHeader methods must have been called before calling this
method.
Question parses a single Question.
SOAResource parses a single SOAResource.
One of the XXXHeader methods must have been called before calling this
method.
SRVResource parses a single SRVResource.
One of the XXXHeader methods must have been called before calling this
method.
SkipAdditional skips a single Additional Resource.
SkipAllAdditionals skips all Additional Resources.
SkipAllAnswers skips all Answer Resources.
SkipAllAuthorities skips all Authority Resources.
SkipAllQuestions skips all Questions.
SkipAnswer skips a single Answer Resource.
SkipAuthority skips a single Authority Resource.
SkipQuestion skips a single Question.
Start parses the header and enables the parsing of Questions.
TXTResource parses a single TXTResource.
One of the XXXHeader methods must have been called before calling this
method.
(*T) checkAdvance(sec section) error(*T) resource(sec section) (Resource, error)(*T) resourceHeader(sec section) (ResourceHeader, error)(*T) skipResource(sec section) error
func net.dnsPacketRoundTrip(c net.Conn, id uint16, query Question, b []byte) (Parser, Header, error)
func net.dnsStreamRoundTrip(c net.Conn, id uint16, query Question, b []byte) (Parser, Header, error)
func net.(*Resolver).exchange(ctx context.Context, server string, q Question, timeout time.Duration, useTCP bool) (Parser, Header, error)
func net.(*Resolver).lookup(ctx context.Context, name string, qtype Type) (Parser, string, error)
func net.(*Resolver).tryOneName(ctx context.Context, cfg *net.dnsConfig, name string, qtype Type) (Parser, string, error)
func net.checkHeader(p *Parser, h Header) error
func net.skipToAnswer(p *Parser, qtype Type) error
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.
Class is the class of network to which this DNS resource record
pertains.
Length is the length of data in the resource record after the header.
This field will be set automatically during packing.
Name is the domain name for which this resource record pertains.
TTL is the length of time (measured in seconds) which this resource
record is valid for (time to live). All Resources in a set should
have the same TTL (RFC 2181 Section 5.2).
Type is the type of DNS resource record.
This field will be set automatically during packing.
DNSSECAllowed reports whether the DNSSEC OK bit is set.
ExtendedRCode returns an extended RCode.
The provided rcode must be the RCode in DNS message header.
GoString implements fmt.GoStringer.GoString.
SetEDNS0 configures h for EDNS(0).
The provided extRCode must be an extedned RCode.
fixLen 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.
pack appends the wire format of the ResourceHeader to oldMsg.
lenOff is the offset in msg where the Length field was packed.
(*T) unpack(msg []byte, off int) (int, error)
*T : fmt.GoStringer
func (*Parser).AdditionalHeader() (ResourceHeader, error)
func (*Parser).AnswerHeader() (ResourceHeader, error)
func (*Parser).AuthorityHeader() (ResourceHeader, error)
func (*Parser).resourceHeader(sec section) (ResourceHeader, error)
func (*Builder).AAAAResource(h ResourceHeader, r AAAAResource) error
func (*Builder).AResource(h ResourceHeader, r AResource) error
func (*Builder).CNAMEResource(h ResourceHeader, r CNAMEResource) error
func (*Builder).MXResource(h ResourceHeader, r MXResource) error
func (*Builder).NSResource(h ResourceHeader, r NSResource) error
func (*Builder).OPTResource(h ResourceHeader, r OPTResource) error
func (*Builder).PTRResource(h ResourceHeader, r PTRResource) error
func (*Builder).SOAResource(h ResourceHeader, r SOAResource) error
func (*Builder).SRVResource(h ResourceHeader, r SRVResource) error
func (*Builder).TXTResource(h ResourceHeader, r TXTResource) error
func unpackResourceBody(msg []byte, off int, hdr ResourceHeader) (ResourceBody, int, error)
Package-Level Functions (total 39, in which 3 are exported)
MustNewName creates a new Name from a string and panics on error.
NewBuilder 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 may return the same underlying array if there was sufficient
capacity in the slice.
NewName creates a new Name from a string.
packBytes appends the wire format of field to msg.
packClass appends the wire format of field to msg.
packText appends the wire format of field to msg.
packType appends the wire format of field to msg.
packUint16 appends the wire format of field to msg.
packUint32 appends the wire format of field to msg.
Package-Level Variables (total 23, in which 2 are exported)
ErrNotStarted indicates that the prerequisite information isn't
available yet because the previous records haven't been appropriately
parsed, skipped or finished.
ErrSectionDone indicated that all records in the section have been
parsed or finished.
packStartingCap 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.
The pages are generated with Goldsv0.4.2. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.