Commit 30f03014 authored by Ian Gudger's avatar Ian Gudger Committed by Brad Fitzpatrick

dns/dnsmessage: allocation free DNS message parsing and building

Parsing and building can now be done 100% allocation free for most
types. Non-name variable-length fields such as TXT records still
require allocations. Building with compression still causes allocations
due to the use of a map. Both parsing and building will perform
allocations in error paths.

Updates golang/go#16218

Change-Id: I644a7776bd0bf4ad20def521cee44eeadc5e9c06
Reviewed-on: https://go-review.googlesource.com/41145
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 057a25b0
...@@ -68,12 +68,12 @@ const ( ...@@ -68,12 +68,12 @@ const (
var ( var (
// ErrNotStarted indicates that the prerequisite information isn't // ErrNotStarted indicates that the prerequisite information isn't
// available yet because the previous records haven't been appropriately // available yet because the previous records haven't been appropriately
// parsed or skipped. // parsed, skipped or finished.
ErrNotStarted = errors.New("parsing of this type isn't available yet") ErrNotStarted = errors.New("parsing/packing of this type isn't available yet")
// ErrSectionDone indicated that all records in the section have been // ErrSectionDone indicated that all records in the section have been
// parsed. // parsed or finished.
ErrSectionDone = errors.New("parsing of this section has completed") ErrSectionDone = errors.New("parsing/packing of this section has completed")
errBaseLen = errors.New("insufficient data for base length type") errBaseLen = errors.New("insufficient data for base length type")
errCalcLen = errors.New("insufficient data for calculated length type") errCalcLen = errors.New("insufficient data for calculated length type")
...@@ -88,6 +88,28 @@ var ( ...@@ -88,6 +88,28 @@ var (
errTooManyAnswers = errors.New("too many Answers to pack (>65535)") errTooManyAnswers = errors.New("too many Answers to pack (>65535)")
errTooManyAuthorities = errors.New("too many Authorities to pack (>65535)") errTooManyAuthorities = errors.New("too many Authorities to pack (>65535)")
errTooManyAdditionals = errors.New("too many Additionals to pack (>65535)") errTooManyAdditionals = errors.New("too many Additionals to pack (>65535)")
errNonCanonicalName = errors.New("name is not in canonical format (it must end with a .)")
)
// Internal constants.
const (
// 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.
packStartingCap = 512
// uint16Len is the length (in bytes) of a uint16.
uint16Len = 2
// uint32Len is the length (in bytes) of a uint32.
uint32Len = 4
// headerLen is the length (in bytes) of a DNS header.
//
// A header is comprised of 6 uint16s and no padding.
headerLen = 6 * uint16Len
) )
type nestedError struct { type nestedError struct {
...@@ -148,7 +170,8 @@ type Message struct { ...@@ -148,7 +170,8 @@ type Message struct {
type section uint8 type section uint8
const ( const (
sectionHeader section = iota sectionNotStarted section = iota
sectionHeader
sectionQuestions sectionQuestions
sectionAnswers sectionAnswers
sectionAuthorities sectionAuthorities
...@@ -241,10 +264,13 @@ func (h *header) header() Header { ...@@ -241,10 +264,13 @@ func (h *header) header() Header {
} }
// A Resource is a DNS resource record. // A Resource is a DNS resource record.
type Resource interface { type Resource struct {
// Header return's the Resource's ResourceHeader. Header ResourceHeader
Header() *ResourceHeader Body ResourceBody
}
// A ResourceBody is a DNS resource record minus the header.
type ResourceBody interface {
// pack packs a Resource except for its header. // pack packs a Resource except for its header.
pack(msg []byte, compression map[string]int) ([]byte, error) pack(msg []byte, compression map[string]int) ([]byte, error)
...@@ -253,25 +279,21 @@ type Resource interface { ...@@ -253,25 +279,21 @@ type Resource interface {
realType() Type realType() Type
} }
func packResource(msg []byte, resource Resource, compression map[string]int) ([]byte, error) { func (r *Resource) pack(msg []byte, compression map[string]int) ([]byte, error) {
oldMsg := msg oldMsg := msg
resource.Header().Type = resource.realType() r.Header.Type = r.Body.realType()
msg, length, err := resource.Header().pack(msg, compression) msg, length, err := r.Header.pack(msg, compression)
if err != nil { if err != nil {
return msg, &nestedError{"ResourceHeader", err} return msg, &nestedError{"ResourceHeader", err}
} }
preLen := len(msg) preLen := len(msg)
msg, err = resource.pack(msg, compression) msg, err = r.Body.pack(msg, compression)
if err != nil { if err != nil {
return msg, &nestedError{"content", err} return msg, &nestedError{"content", err}
} }
conLen := len(msg) - preLen if err := r.Header.fixLen(msg, length, preLen); err != nil {
if conLen > int(^uint16(0)) { return oldMsg, err
return oldMsg, errResTooLong
} }
// Fill in the length now that we know how long the content is.
packUint16(length[:0], uint16(conLen))
resource.Header().Length = uint16(conLen)
return msg, nil return msg, nil
} }
...@@ -330,14 +352,15 @@ func (p *Parser) checkAdvance(sec section) error { ...@@ -330,14 +352,15 @@ func (p *Parser) checkAdvance(sec section) error {
func (p *Parser) resource(sec section) (Resource, error) { func (p *Parser) resource(sec section) (Resource, error) {
var r Resource var r Resource
hdr, err := p.resourceHeader(sec) var err error
r.Header, err = p.resourceHeader(sec)
if err != nil { if err != nil {
return r, err return r, err
} }
p.resHeaderValid = false p.resHeaderValid = false
r, p.off, err = unpackResource(p.msg, p.off, hdr) r.Body, p.off, err = unpackResourceBody(p.msg, p.off, r.Header)
if err != nil { if err != nil {
return nil, &nestedError{"unpacking " + sectionNames[sec], err} return Resource{}, &nestedError{"unpacking " + sectionNames[sec], err}
} }
p.index++ p.index++
return r, nil return r, nil
...@@ -389,7 +412,8 @@ func (p *Parser) Question() (Question, error) { ...@@ -389,7 +412,8 @@ func (p *Parser) Question() (Question, error) {
if err := p.checkAdvance(sectionQuestions); err != nil { if err := p.checkAdvance(sectionQuestions); err != nil {
return Question{}, err return Question{}, err
} }
name, off, err := unpackName(p.msg, p.off) var name Name
off, err := name.unpack(p.msg, p.off)
if err != nil { if err != nil {
return Question{}, &nestedError{"unpacking Question.Name", err} return Question{}, &nestedError{"unpacking Question.Name", err}
} }
...@@ -575,6 +599,168 @@ func (p *Parser) SkipAllAdditionals() error { ...@@ -575,6 +599,168 @@ func (p *Parser) SkipAllAdditionals() error {
} }
} }
// CNAMEResource parses a single CNAMEResource.
//
// One of the XXXHeader methods must have been called before calling this
// method.
func (p *Parser) CNAMEResource() (CNAMEResource, error) {
if !p.resHeaderValid || p.resHeader.Type != TypeCNAME {
return CNAMEResource{}, ErrNotStarted
}
r, err := unpackCNAMEResource(p.msg, p.off)
if err != nil {
return CNAMEResource{}, err
}
p.off += int(p.resHeader.Length)
p.resHeaderValid = false
p.index++
return r, nil
}
// MXResource parses a single MXResource.
//
// One of the XXXHeader methods must have been called before calling this
// method.
func (p *Parser) MXResource() (MXResource, error) {
if !p.resHeaderValid || p.resHeader.Type != TypeMX {
return MXResource{}, ErrNotStarted
}
r, err := unpackMXResource(p.msg, p.off)
if err != nil {
return MXResource{}, err
}
p.off += int(p.resHeader.Length)
p.resHeaderValid = false
p.index++
return r, nil
}
// NSResource parses a single NSResource.
//
// One of the XXXHeader methods must have been called before calling this
// method.
func (p *Parser) NSResource() (NSResource, error) {
if !p.resHeaderValid || p.resHeader.Type != TypeNS {
return NSResource{}, ErrNotStarted
}
r, err := unpackNSResource(p.msg, p.off)
if err != nil {
return NSResource{}, err
}
p.off += int(p.resHeader.Length)
p.resHeaderValid = false
p.index++
return r, nil
}
// PTRResource parses a single PTRResource.
//
// One of the XXXHeader methods must have been called before calling this
// method.
func (p *Parser) PTRResource() (PTRResource, error) {
if !p.resHeaderValid || p.resHeader.Type != TypePTR {
return PTRResource{}, ErrNotStarted
}
r, err := unpackPTRResource(p.msg, p.off)
if err != nil {
return PTRResource{}, err
}
p.off += int(p.resHeader.Length)
p.resHeaderValid = false
p.index++
return r, nil
}
// SOAResource parses a single SOAResource.
//
// One of the XXXHeader methods must have been called before calling this
// method.
func (p *Parser) SOAResource() (SOAResource, error) {
if !p.resHeaderValid || p.resHeader.Type != TypeSOA {
return SOAResource{}, ErrNotStarted
}
r, err := unpackSOAResource(p.msg, p.off)
if err != nil {
return SOAResource{}, err
}
p.off += int(p.resHeader.Length)
p.resHeaderValid = false
p.index++
return r, nil
}
// TXTResource parses a single TXTResource.
//
// One of the XXXHeader methods must have been called before calling this
// method.
func (p *Parser) TXTResource() (TXTResource, error) {
if !p.resHeaderValid || p.resHeader.Type != TypeTXT {
return TXTResource{}, ErrNotStarted
}
r, err := unpackTXTResource(p.msg, p.off, p.resHeader.Length)
if err != nil {
return TXTResource{}, err
}
p.off += int(p.resHeader.Length)
p.resHeaderValid = false
p.index++
return r, nil
}
// SRVResource parses a single SRVResource.
//
// One of the XXXHeader methods must have been called before calling this
// method.
func (p *Parser) SRVResource() (SRVResource, error) {
if !p.resHeaderValid || p.resHeader.Type != TypeSRV {
return SRVResource{}, ErrNotStarted
}
r, err := unpackSRVResource(p.msg, p.off)
if err != nil {
return SRVResource{}, err
}
p.off += int(p.resHeader.Length)
p.resHeaderValid = false
p.index++
return r, nil
}
// AResource parses a single AResource.
//
// One of the XXXHeader methods must have been called before calling this
// method.
func (p *Parser) AResource() (AResource, error) {
if !p.resHeaderValid || p.resHeader.Type != TypeA {
return AResource{}, ErrNotStarted
}
r, err := unpackAResource(p.msg, p.off)
if err != nil {
return AResource{}, err
}
p.off += int(p.resHeader.Length)
p.resHeaderValid = false
p.index++
return r, nil
}
// AAAAResource parses a single AAAAResource.
//
// One of the XXXHeader methods must have been called before calling this
// method.
func (p *Parser) AAAAResource() (AAAAResource, error) {
if !p.resHeaderValid || p.resHeader.Type != TypeAAAA {
return AAAAResource{}, ErrNotStarted
}
r, err := unpackAAAAResource(p.msg, p.off)
if err != nil {
return AAAAResource{}, err
}
p.off += int(p.resHeader.Length)
p.resHeaderValid = false
p.index++
return r, nil
}
// Unpack parses a full Message. // Unpack parses a full Message.
func (m *Message) Unpack(msg []byte) error { func (m *Message) Unpack(msg []byte) error {
var p Parser var p Parser
...@@ -623,9 +809,7 @@ func (m *Message) Pack() ([]byte, error) { ...@@ -623,9 +809,7 @@ func (m *Message) Pack() ([]byte, error) {
h.authorities = uint16(len(m.Authorities)) h.authorities = uint16(len(m.Authorities))
h.additionals = uint16(len(m.Additionals)) h.additionals = uint16(len(m.Additionals))
// The starting capacity doesn't matter too much, but most DNS responses msg := make([]byte, 0, packStartingCap)
// Will be <= 512 bytes as it is the limit for DNS over UDP.
msg := make([]byte, 0, 512)
msg = h.pack(msg) msg = h.pack(msg)
...@@ -639,31 +823,27 @@ func (m *Message) Pack() ([]byte, error) { ...@@ -639,31 +823,27 @@ func (m *Message) Pack() ([]byte, error) {
// compression will help ensure compliance. // compression will help ensure compliance.
compression := map[string]int{} compression := map[string]int{}
for _, q := range m.Questions { for i := range m.Questions {
var err error var err error
msg, err = q.pack(msg, compression) if msg, err = m.Questions[i].pack(msg, compression); err != nil {
if err != nil {
return nil, &nestedError{"packing Question", err} return nil, &nestedError{"packing Question", err}
} }
} }
for _, a := range m.Answers { for i := range m.Answers {
var err error var err error
msg, err = packResource(msg, a, compression) if msg, err = m.Answers[i].pack(msg, compression); err != nil {
if err != nil {
return nil, &nestedError{"packing Answer", err} return nil, &nestedError{"packing Answer", err}
} }
} }
for _, a := range m.Authorities { for i := range m.Authorities {
var err error var err error
msg, err = packResource(msg, a, compression) if msg, err = m.Authorities[i].pack(msg, compression); err != nil {
if err != nil {
return nil, &nestedError{"packing Authority", err} return nil, &nestedError{"packing Authority", err}
} }
} }
for _, a := range m.Additionals { for i := range m.Additionals {
var err error var err error
msg, err = packResource(msg, a, compression) if msg, err = m.Additionals[i].pack(msg, compression); err != nil {
if err != nil {
return nil, &nestedError{"packing Additional", err} return nil, &nestedError{"packing Additional", err}
} }
} }
...@@ -671,11 +851,369 @@ func (m *Message) Pack() ([]byte, error) { ...@@ -671,11 +851,369 @@ func (m *Message) Pack() ([]byte, error) {
return msg, nil return msg, nil
} }
// A Builder allows incrementally packing a DNS message.
type Builder struct {
msg []byte
header header
section section
compression map[string]int
}
// Start initializes the builder.
//
// buf is optional (nil is fine), but if provided, Start takes ownership of buf.
func (b *Builder) Start(buf []byte, h Header) {
b.StartWithoutCompression(buf, h)
b.compression = map[string]int{}
}
// StartWithoutCompression initializes the builder with compression disabled.
//
// This 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.
//
// buf is optional (nil is fine), but if provided, Start takes ownership of buf.
func (b *Builder) StartWithoutCompression(buf []byte, h Header) {
*b = Builder{msg: buf}
b.header.id, b.header.bits = h.pack()
if cap(b.msg) < headerLen {
b.msg = make([]byte, 0, packStartingCap)
}
b.msg = b.msg[:headerLen]
b.section = sectionHeader
}
func (b *Builder) startCheck(s section) error {
if b.section <= sectionNotStarted {
return ErrNotStarted
}
if b.section > s {
return ErrSectionDone
}
return nil
}
// StartQuestions prepares the builder for packing Questions.
func (b *Builder) StartQuestions() error {
if err := b.startCheck(sectionQuestions); err != nil {
return err
}
b.section = sectionQuestions
return nil
}
// StartAnswers prepares the builder for packing Answers.
func (b *Builder) StartAnswers() error {
if err := b.startCheck(sectionAnswers); err != nil {
return err
}
b.section = sectionAnswers
return nil
}
// StartAuthorities prepares the builder for packing Authorities.
func (b *Builder) StartAuthorities() error {
if err := b.startCheck(sectionAuthorities); err != nil {
return err
}
b.section = sectionAuthorities
return nil
}
// StartAdditionals prepares the builder for packing Additionals.
func (b *Builder) StartAdditionals() error {
if err := b.startCheck(sectionAdditionals); err != nil {
return err
}
b.section = sectionAdditionals
return nil
}
func (b *Builder) incrementSectionCount() error {
var count *uint16
var err error
switch b.section {
case sectionQuestions:
count = &b.header.questions
err = errTooManyQuestions
case sectionAnswers:
count = &b.header.answers
err = errTooManyAnswers
case sectionAuthorities:
count = &b.header.authorities
err = errTooManyAuthorities
case sectionAdditionals:
count = &b.header.additionals
err = errTooManyAdditionals
}
if *count == ^uint16(0) {
return err
}
*count++
return nil
}
// Question adds a single Question.
func (b *Builder) Question(q Question) error {
if b.section < sectionQuestions {
return ErrNotStarted
}
if b.section > sectionQuestions {
return ErrSectionDone
}
msg, err := q.pack(b.msg, b.compression)
if err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
func (b *Builder) checkResourceSection() error {
if b.section < sectionAnswers {
return ErrNotStarted
}
if b.section > sectionAdditionals {
return ErrSectionDone
}
return nil
}
// CNAMEResource adds a single CNAMEResource.
func (b *Builder) CNAMEResource(h ResourceHeader, r CNAMEResource) error {
if err := b.checkResourceSection(); err != nil {
return err
}
h.Type = r.realType()
msg, length, err := h.pack(b.msg, b.compression)
if err != nil {
return &nestedError{"ResourceHeader", err}
}
preLen := len(msg)
if msg, err = r.pack(msg, b.compression); err != nil {
return &nestedError{"CNAMEResource body", err}
}
if err := h.fixLen(msg, length, preLen); err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
// MXResource adds a single MXResource.
func (b *Builder) MXResource(h ResourceHeader, r MXResource) error {
if err := b.checkResourceSection(); err != nil {
return err
}
h.Type = r.realType()
msg, length, err := h.pack(b.msg, b.compression)
if err != nil {
return &nestedError{"ResourceHeader", err}
}
preLen := len(msg)
if msg, err = r.pack(msg, b.compression); err != nil {
return &nestedError{"MXResource body", err}
}
if err := h.fixLen(msg, length, preLen); err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
// NSResource adds a single NSResource.
func (b *Builder) NSResource(h ResourceHeader, r NSResource) error {
if err := b.checkResourceSection(); err != nil {
return err
}
h.Type = r.realType()
msg, length, err := h.pack(b.msg, b.compression)
if err != nil {
return &nestedError{"ResourceHeader", err}
}
preLen := len(msg)
if msg, err = r.pack(msg, b.compression); err != nil {
return &nestedError{"NSResource body", err}
}
if err := h.fixLen(msg, length, preLen); err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
// PTRResource adds a single PTRResource.
func (b *Builder) PTRResource(h ResourceHeader, r PTRResource) error {
if err := b.checkResourceSection(); err != nil {
return err
}
h.Type = r.realType()
msg, length, err := h.pack(b.msg, b.compression)
if err != nil {
return &nestedError{"ResourceHeader", err}
}
preLen := len(msg)
if msg, err = r.pack(msg, b.compression); err != nil {
return &nestedError{"PTRResource body", err}
}
if err := h.fixLen(msg, length, preLen); err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
// SOAResource adds a single SOAResource.
func (b *Builder) SOAResource(h ResourceHeader, r SOAResource) error {
if err := b.checkResourceSection(); err != nil {
return err
}
h.Type = r.realType()
msg, length, err := h.pack(b.msg, b.compression)
if err != nil {
return &nestedError{"ResourceHeader", err}
}
preLen := len(msg)
if msg, err = r.pack(msg, b.compression); err != nil {
return &nestedError{"SOAResource body", err}
}
if err := h.fixLen(msg, length, preLen); err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
// TXTResource adds a single TXTResource.
func (b *Builder) TXTResource(h ResourceHeader, r TXTResource) error {
if err := b.checkResourceSection(); err != nil {
return err
}
h.Type = r.realType()
msg, length, err := h.pack(b.msg, b.compression)
if err != nil {
return &nestedError{"ResourceHeader", err}
}
preLen := len(msg)
if msg, err = r.pack(msg, b.compression); err != nil {
return &nestedError{"TXTResource body", err}
}
if err := h.fixLen(msg, length, preLen); err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
// SRVResource adds a single SRVResource.
func (b *Builder) SRVResource(h ResourceHeader, r SRVResource) error {
if err := b.checkResourceSection(); err != nil {
return err
}
h.Type = r.realType()
msg, length, err := h.pack(b.msg, b.compression)
if err != nil {
return &nestedError{"ResourceHeader", err}
}
preLen := len(msg)
if msg, err = r.pack(msg, b.compression); err != nil {
return &nestedError{"SRVResource body", err}
}
if err := h.fixLen(msg, length, preLen); err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
// AResource adds a single AResource.
func (b *Builder) AResource(h ResourceHeader, r AResource) error {
if err := b.checkResourceSection(); err != nil {
return err
}
h.Type = r.realType()
msg, length, err := h.pack(b.msg, b.compression)
if err != nil {
return &nestedError{"ResourceHeader", err}
}
preLen := len(msg)
if msg, err = r.pack(msg, b.compression); err != nil {
return &nestedError{"AResource body", err}
}
if err := h.fixLen(msg, length, preLen); err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
// AAAAResource adds a single AAAAResource.
func (b *Builder) AAAAResource(h ResourceHeader, r AAAAResource) error {
if err := b.checkResourceSection(); err != nil {
return err
}
h.Type = r.realType()
msg, length, err := h.pack(b.msg, b.compression)
if err != nil {
return &nestedError{"ResourceHeader", err}
}
preLen := len(msg)
if msg, err = r.pack(msg, b.compression); err != nil {
return &nestedError{"AAAAResource body", err}
}
if err := h.fixLen(msg, length, preLen); err != nil {
return err
}
if err := b.incrementSectionCount(); err != nil {
return err
}
b.msg = msg
return nil
}
// Finish ends message building and generates a binary packet.
func (b *Builder) Finish() ([]byte, error) {
if b.section < sectionHeader {
return nil, ErrNotStarted
}
b.section = sectionDone
b.header.pack(b.msg[:0])
return b.msg, nil
}
// An ResourceHeader is the header of a DNS resource record. There are // An ResourceHeader is the header of a DNS resource record. There are
// many types of DNS resource records, but they all share the same header. // many types of DNS resource records, but they all share the same header.
type ResourceHeader struct { type ResourceHeader struct {
// Name is the domain name for which this resource record pertains. // Name is the domain name for which this resource record pertains.
Name string Name Name
// Type is the type of DNS resource record. // Type is the type of DNS resource record.
// //
...@@ -697,17 +1235,12 @@ type ResourceHeader struct { ...@@ -697,17 +1235,12 @@ type ResourceHeader struct {
Length uint16 Length uint16
} }
// Header implements Resource.Header.
func (h *ResourceHeader) Header() *ResourceHeader {
return h
}
// pack packs all of the fields in a ResourceHeader except for the length. The // pack packs all of the fields in a ResourceHeader except for the length. The
// length bytes are returned as a slice so they can be filled in after the rest // length bytes are returned as a slice so they can be filled in after the rest
// of the Resource has been packed. // of the Resource has been packed.
func (h *ResourceHeader) pack(oldMsg []byte, compression map[string]int) (msg []byte, length []byte, err error) { func (h *ResourceHeader) pack(oldMsg []byte, compression map[string]int) (msg []byte, length []byte, err error) {
msg = oldMsg msg = oldMsg
if msg, err = packName(msg, h.Name, compression); err != nil { if msg, err = h.Name.pack(msg, compression); err != nil {
return oldMsg, nil, &nestedError{"Name", err} return oldMsg, nil, &nestedError{"Name", err}
} }
msg = packType(msg, h.Type) msg = packType(msg, h.Type)
...@@ -715,13 +1248,13 @@ func (h *ResourceHeader) pack(oldMsg []byte, compression map[string]int) (msg [] ...@@ -715,13 +1248,13 @@ func (h *ResourceHeader) pack(oldMsg []byte, compression map[string]int) (msg []
msg = packUint32(msg, h.TTL) msg = packUint32(msg, h.TTL)
lenBegin := len(msg) lenBegin := len(msg)
msg = packUint16(msg, h.Length) msg = packUint16(msg, h.Length)
return msg, msg[lenBegin:], nil return msg, msg[lenBegin : lenBegin+uint16Len], nil
} }
func (h *ResourceHeader) unpack(msg []byte, off int) (int, error) { func (h *ResourceHeader) unpack(msg []byte, off int) (int, error) {
newOff := off newOff := off
var err error var err error
if h.Name, newOff, err = unpackName(msg, newOff); err != nil { if newOff, err = h.Name.unpack(msg, newOff); err != nil {
return off, &nestedError{"Name", err} return off, &nestedError{"Name", err}
} }
if h.Type, newOff, err = unpackType(msg, newOff); err != nil { if h.Type, newOff, err = unpackType(msg, newOff); err != nil {
...@@ -739,6 +1272,19 @@ func (h *ResourceHeader) unpack(msg []byte, off int) (int, error) { ...@@ -739,6 +1272,19 @@ func (h *ResourceHeader) unpack(msg []byte, off int) (int, error) {
return newOff, nil return newOff, nil
} }
func (h *ResourceHeader) fixLen(msg []byte, length []byte, preLen int) error {
conLen := len(msg) - preLen
if conLen > int(^uint16(0)) {
return errResTooLong
}
// Fill in the length now that we know how long the content is.
packUint16(length[:0], uint16(conLen))
h.Length = uint16(conLen)
return nil
}
func skipResource(msg []byte, off int) (int, error) { func skipResource(msg []byte, off int) (int, error) {
newOff, err := skipName(msg, off) newOff, err := skipName(msg, off)
if err != nil { if err != nil {
...@@ -768,17 +1314,17 @@ func packUint16(msg []byte, field uint16) []byte { ...@@ -768,17 +1314,17 @@ func packUint16(msg []byte, field uint16) []byte {
} }
func unpackUint16(msg []byte, off int) (uint16, int, error) { func unpackUint16(msg []byte, off int) (uint16, int, error) {
if off+2 > len(msg) { if off+uint16Len > len(msg) {
return 0, off, errBaseLen return 0, off, errBaseLen
} }
return uint16(msg[off])<<8 | uint16(msg[off+1]), off + 2, nil return uint16(msg[off])<<8 | uint16(msg[off+1]), off + uint16Len, nil
} }
func skipUint16(msg []byte, off int) (int, error) { func skipUint16(msg []byte, off int) (int, error) {
if off+2 > len(msg) { if off+uint16Len > len(msg) {
return off, errBaseLen return off, errBaseLen
} }
return off + 2, nil return off + uint16Len, nil
} }
func packType(msg []byte, field Type) []byte { func packType(msg []byte, field Type) []byte {
...@@ -818,18 +1364,18 @@ func packUint32(msg []byte, field uint32) []byte { ...@@ -818,18 +1364,18 @@ func packUint32(msg []byte, field uint32) []byte {
} }
func unpackUint32(msg []byte, off int) (uint32, int, error) { func unpackUint32(msg []byte, off int) (uint32, int, error) {
if off+4 > len(msg) { if off+uint32Len > len(msg) {
return 0, off, errBaseLen return 0, off, errBaseLen
} }
v := uint32(msg[off])<<24 | uint32(msg[off+1])<<16 | uint32(msg[off+2])<<8 | uint32(msg[off+3]) v := uint32(msg[off])<<24 | uint32(msg[off+1])<<16 | uint32(msg[off+2])<<8 | uint32(msg[off+3])
return v, off + 4, nil return v, off + uint32Len, nil
} }
func skipUint32(msg []byte, off int) (int, error) { func skipUint32(msg []byte, off int) (int, error) {
if off+4 > len(msg) { if off+uint32Len > len(msg) {
return off, errBaseLen return off, errBaseLen
} }
return off + 4, nil return off + uint32Len, nil
} }
func packText(msg []byte, field string) []byte { func packText(msg []byte, field string) []byte {
...@@ -889,30 +1435,53 @@ func skipBytes(msg []byte, off int, field []byte) (int, error) { ...@@ -889,30 +1435,53 @@ func skipBytes(msg []byte, off int, field []byte) (int, error) {
return newOff, nil return newOff, nil
} }
// packName packs a domain name. const nameLen = 255
// A Name is a non-encoded domain name. It is used instead of strings to avoid
// allocations.
type Name struct {
Data [nameLen]byte
Length uint8
}
// NewName creates a new Name from a string.
func NewName(name string) (Name, error) {
if len([]byte(name)) > nameLen {
return Name{}, errCalcLen
}
n := Name{Length: uint8(len(name))}
copy(n.Data[:], []byte(name))
return n, nil
}
func (n Name) String() string {
return string(n.Data[:n.Length])
}
// pack packs a domain name.
// //
// Domain names are a sequence of counted strings split at the dots. They end // 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. // 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 // The compression map will be updated with new domain suffixes. If compression
// is nil, compression will not be used. // is nil, compression will not be used.
func packName(msg []byte, name string, compression map[string]int) ([]byte, error) { func (n *Name) pack(msg []byte, compression map[string]int) ([]byte, error) {
oldMsg := msg oldMsg := msg
// Add a trailing dot to canonicalize name. // Add a trailing dot to canonicalize name.
if n := len(name); n == 0 || name[n-1] != '.' { if n.Length == 0 || n.Data[n.Length-1] != '.' {
name += "." return oldMsg, errNonCanonicalName
} }
// Allow root domain. // Allow root domain.
if name == "." { if n.Data[0] == '.' && n.Length == 1 {
return append(msg, 0), nil return append(msg, 0), nil
} }
// Emit sequence of counted strings, chopping at dots. // Emit sequence of counted strings, chopping at dots.
for i, begin := 0, 0; i < len(name); i++ { for i, begin := 0, 0; i < int(n.Length); i++ {
// Check for the end of the segment. // Check for the end of the segment.
if name[i] == '.' { if n.Data[i] == '.' {
// The two most significant bits have special meaning. // The two most significant bits have special meaning.
// It isn't allowed for segments to be long enough to // It isn't allowed for segments to be long enough to
// need them. // need them.
...@@ -928,7 +1497,7 @@ func packName(msg []byte, name string, compression map[string]int) ([]byte, erro ...@@ -928,7 +1497,7 @@ func packName(msg []byte, name string, compression map[string]int) ([]byte, erro
msg = append(msg, byte(i-begin)) msg = append(msg, byte(i-begin))
for j := begin; j < i; j++ { for j := begin; j < i; j++ {
msg = append(msg, name[j]) msg = append(msg, n.Data[j])
} }
begin = i + 1 begin = i + 1
...@@ -938,8 +1507,8 @@ func packName(msg []byte, name string, compression map[string]int) ([]byte, erro ...@@ -938,8 +1507,8 @@ func packName(msg []byte, name string, compression map[string]int) ([]byte, erro
// We can only compress domain suffixes starting with a new // We can only compress domain suffixes starting with a new
// segment. A pointer is two bytes with the two most significant // segment. A pointer is two bytes with the two most significant
// bits set to 1 to indicate that it is a pointer. // bits set to 1 to indicate that it is a pointer.
if (i == 0 || name[i-1] == '.') && compression != nil { if (i == 0 || n.Data[i-1] == '.') && compression != nil {
if ptr, ok := compression[name[i:]]; ok { if ptr, ok := compression[string(n.Data[i:])]; ok {
// Hit. Emit a pointer instead of the rest of // Hit. Emit a pointer instead of the rest of
// the domain. // the domain.
return append(msg, byte(ptr>>8|0xC0), byte(ptr)), nil return append(msg, byte(ptr>>8|0xC0), byte(ptr)), nil
...@@ -948,15 +1517,15 @@ func packName(msg []byte, name string, compression map[string]int) ([]byte, erro ...@@ -948,15 +1517,15 @@ func packName(msg []byte, name string, compression map[string]int) ([]byte, erro
// Miss. Add the suffix to the compression table if the // Miss. Add the suffix to the compression table if the
// offset can be stored in the available 14 bytes. // offset can be stored in the available 14 bytes.
if len(msg) <= int(^uint16(0)>>2) { if len(msg) <= int(^uint16(0)>>2) {
compression[name[i:]] = len(msg) compression[string(n.Data[i:])] = len(msg)
} }
} }
} }
return append(msg, 0), nil return append(msg, 0), nil
} }
// unpackName unpacks a domain name. // unpack unpacks a domain name.
func unpackName(msg []byte, off int) (string, int, error) { func (n *Name) unpack(msg []byte, off int) (int, error) {
// currOff is the current working offset. // currOff is the current working offset.
currOff := off currOff := off
...@@ -965,15 +1534,16 @@ func unpackName(msg []byte, off int) (string, int, error) { ...@@ -965,15 +1534,16 @@ func unpackName(msg []byte, off int) (string, int, error) {
// the usage of this name. // the usage of this name.
newOff := off newOff := off
// name is the domain name being unpacked.
name := make([]byte, 0, 255)
// ptr is the number of pointers followed. // ptr is the number of pointers followed.
var ptr int var ptr int
// Name is a slice representation of the name data.
name := n.Data[:0]
Loop: Loop:
for { for {
if currOff >= len(msg) { if currOff >= len(msg) {
return "", off, errBaseLen return off, errBaseLen
} }
c := int(msg[currOff]) c := int(msg[currOff])
currOff++ currOff++
...@@ -985,14 +1555,14 @@ Loop: ...@@ -985,14 +1555,14 @@ Loop:
} }
endOff := currOff + c endOff := currOff + c
if endOff > len(msg) { if endOff > len(msg) {
return "", off, errCalcLen return off, errCalcLen
} }
name = append(name, msg[currOff:endOff]...) name = append(name, msg[currOff:endOff]...)
name = append(name, '.') name = append(name, '.')
currOff = endOff currOff = endOff
case 0xC0: // Pointer case 0xC0: // Pointer
if currOff >= len(msg) { if currOff >= len(msg) {
return "", off, errInvalidPtr return off, errInvalidPtr
} }
c1 := msg[currOff] c1 := msg[currOff]
currOff++ currOff++
...@@ -1001,21 +1571,25 @@ Loop: ...@@ -1001,21 +1571,25 @@ Loop:
} }
// Don't follow too many pointers, maybe there's a loop. // Don't follow too many pointers, maybe there's a loop.
if ptr++; ptr > 10 { if ptr++; ptr > 10 {
return "", off, errTooManyPtr return off, errTooManyPtr
} }
currOff = (c^0xC0)<<8 | int(c1) currOff = (c^0xC0)<<8 | int(c1)
default: default:
// Prefixes 0x80 and 0x40 are reserved. // Prefixes 0x80 and 0x40 are reserved.
return "", off, errReserved return off, errReserved
} }
} }
if len(name) == 0 { if len(name) == 0 {
name = append(name, '.') name = append(name, '.')
} }
if len(name) > len(n.Data) {
return off, errCalcLen
}
n.Length = uint8(len(name))
if ptr == 0 { if ptr == 0 {
newOff = currOff newOff = currOff
} }
return string(name), newOff, nil return newOff, nil
} }
func skipName(msg []byte, off int) (int, error) { func skipName(msg []byte, off int) (int, error) {
...@@ -1061,13 +1635,13 @@ Loop: ...@@ -1061,13 +1635,13 @@ Loop:
// A Question is a DNS query. // A Question is a DNS query.
type Question struct { type Question struct {
Name string Name Name
Type Type Type Type
Class Class Class Class
} }
func (q *Question) pack(msg []byte, compression map[string]int) ([]byte, error) { func (q *Question) pack(msg []byte, compression map[string]int) ([]byte, error) {
msg, err := packName(msg, q.Name, compression) msg, err := q.Name.pack(msg, compression)
if err != nil { if err != nil {
return msg, &nestedError{"Name", err} return msg, &nestedError{"Name", err}
} }
...@@ -1075,55 +1649,71 @@ func (q *Question) pack(msg []byte, compression map[string]int) ([]byte, error) ...@@ -1075,55 +1649,71 @@ func (q *Question) pack(msg []byte, compression map[string]int) ([]byte, error)
return packClass(msg, q.Class), nil return packClass(msg, q.Class), nil
} }
func unpackResource(msg []byte, off int, hdr ResourceHeader) (Resource, int, error) { func unpackResourceBody(msg []byte, off int, hdr ResourceHeader) (ResourceBody, int, error) {
var ( var (
r Resource r ResourceBody
err error err error
name string name string
) )
switch hdr.Type { switch hdr.Type {
case TypeA: case TypeA:
r, err = unpackAResource(hdr, msg, off) var rb AResource
rb, err = unpackAResource(msg, off)
r = &rb
name = "A" name = "A"
case TypeNS: case TypeNS:
r, err = unpackNSResource(hdr, msg, off) var rb NSResource
rb, err = unpackNSResource(msg, off)
r = &rb
name = "NS" name = "NS"
case TypeCNAME: case TypeCNAME:
r, err = unpackCNAMEResource(hdr, msg, off) var rb CNAMEResource
rb, err = unpackCNAMEResource(msg, off)
r = &rb
name = "CNAME" name = "CNAME"
case TypeSOA: case TypeSOA:
r, err = unpackSOAResource(hdr, msg, off) var rb SOAResource
rb, err = unpackSOAResource(msg, off)
r = &rb
name = "SOA" name = "SOA"
case TypePTR: case TypePTR:
r, err = unpackPTRResource(hdr, msg, off) var rb PTRResource
rb, err = unpackPTRResource(msg, off)
r = &rb
name = "PTR" name = "PTR"
case TypeMX: case TypeMX:
r, err = unpackMXResource(hdr, msg, off) var rb MXResource
rb, err = unpackMXResource(msg, off)
r = &rb
name = "MX" name = "MX"
case TypeTXT: case TypeTXT:
r, err = unpackTXTResource(hdr, msg, off) var rb TXTResource
rb, err = unpackTXTResource(msg, off, hdr.Length)
r = &rb
name = "TXT" name = "TXT"
case TypeAAAA: case TypeAAAA:
r, err = unpackAAAAResource(hdr, msg, off) var rb AAAAResource
rb, err = unpackAAAAResource(msg, off)
r = &rb
name = "AAAA" name = "AAAA"
case TypeSRV: case TypeSRV:
r, err = unpackSRVResource(hdr, msg, off) var rb SRVResource
rb, err = unpackSRVResource(msg, off)
r = &rb
name = "SRV" name = "SRV"
} }
if err != nil { if err != nil {
return nil, off, &nestedError{name + " record", err} return nil, off, &nestedError{name + " record", err}
} }
if r != nil { if r == nil {
return r, off + int(hdr.Length), nil return nil, off, errors.New("invalid resource type: " + string(hdr.Type+'0'))
} }
return nil, off, errors.New("invalid resource type: " + string(hdr.Type+'0')) return r, off + int(hdr.Length), nil
} }
// A CNAMEResource is a CNAME Resource record. // A CNAMEResource is a CNAME Resource record.
type CNAMEResource struct { type CNAMEResource struct {
ResourceHeader CNAME Name
CNAME string
} }
func (r *CNAMEResource) realType() Type { func (r *CNAMEResource) realType() Type {
...@@ -1131,23 +1721,21 @@ func (r *CNAMEResource) realType() Type { ...@@ -1131,23 +1721,21 @@ func (r *CNAMEResource) realType() Type {
} }
func (r *CNAMEResource) pack(msg []byte, compression map[string]int) ([]byte, error) { func (r *CNAMEResource) pack(msg []byte, compression map[string]int) ([]byte, error) {
return packName(msg, r.CNAME, compression) return r.CNAME.pack(msg, compression)
} }
func unpackCNAMEResource(hdr ResourceHeader, msg []byte, off int) (*CNAMEResource, error) { func unpackCNAMEResource(msg []byte, off int) (CNAMEResource, error) {
cname, _, err := unpackName(msg, off) var cname Name
if err != nil { if _, err := cname.unpack(msg, off); err != nil {
return nil, err return CNAMEResource{}, err
} }
return &CNAMEResource{hdr, cname}, nil return CNAMEResource{cname}, nil
} }
// An MXResource is an MX Resource record. // An MXResource is an MX Resource record.
type MXResource struct { type MXResource struct {
ResourceHeader
Pref uint16 Pref uint16
MX string MX Name
} }
func (r *MXResource) realType() Type { func (r *MXResource) realType() Type {
...@@ -1157,30 +1745,28 @@ func (r *MXResource) realType() Type { ...@@ -1157,30 +1745,28 @@ func (r *MXResource) realType() Type {
func (r *MXResource) pack(msg []byte, compression map[string]int) ([]byte, error) { func (r *MXResource) pack(msg []byte, compression map[string]int) ([]byte, error) {
oldMsg := msg oldMsg := msg
msg = packUint16(msg, r.Pref) msg = packUint16(msg, r.Pref)
msg, err := packName(msg, r.MX, compression) msg, err := r.MX.pack(msg, compression)
if err != nil { if err != nil {
return oldMsg, &nestedError{"MXResource.MX", err} return oldMsg, &nestedError{"MXResource.MX", err}
} }
return msg, nil return msg, nil
} }
func unpackMXResource(hdr ResourceHeader, msg []byte, off int) (*MXResource, error) { func unpackMXResource(msg []byte, off int) (MXResource, error) {
pref, off, err := unpackUint16(msg, off) pref, off, err := unpackUint16(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"Pref", err} return MXResource{}, &nestedError{"Pref", err}
} }
mx, _, err := unpackName(msg, off) var mx Name
if err != nil { if _, err := mx.unpack(msg, off); err != nil {
return nil, &nestedError{"MX", err} return MXResource{}, &nestedError{"MX", err}
} }
return &MXResource{hdr, pref, mx}, nil return MXResource{pref, mx}, nil
} }
// An NSResource is an NS Resource record. // An NSResource is an NS Resource record.
type NSResource struct { type NSResource struct {
ResourceHeader NS Name
NS string
} }
func (r *NSResource) realType() Type { func (r *NSResource) realType() Type {
...@@ -1188,22 +1774,20 @@ func (r *NSResource) realType() Type { ...@@ -1188,22 +1774,20 @@ func (r *NSResource) realType() Type {
} }
func (r *NSResource) pack(msg []byte, compression map[string]int) ([]byte, error) { func (r *NSResource) pack(msg []byte, compression map[string]int) ([]byte, error) {
return packName(msg, r.NS, compression) return r.NS.pack(msg, compression)
} }
func unpackNSResource(hdr ResourceHeader, msg []byte, off int) (*NSResource, error) { func unpackNSResource(msg []byte, off int) (NSResource, error) {
ns, _, err := unpackName(msg, off) var ns Name
if err != nil { if _, err := ns.unpack(msg, off); err != nil {
return nil, err return NSResource{}, err
} }
return &NSResource{hdr, ns}, nil return NSResource{ns}, nil
} }
// A PTRResource is a PTR Resource record. // A PTRResource is a PTR Resource record.
type PTRResource struct { type PTRResource struct {
ResourceHeader PTR Name
PTR string
} }
func (r *PTRResource) realType() Type { func (r *PTRResource) realType() Type {
...@@ -1211,23 +1795,21 @@ func (r *PTRResource) realType() Type { ...@@ -1211,23 +1795,21 @@ func (r *PTRResource) realType() Type {
} }
func (r *PTRResource) pack(msg []byte, compression map[string]int) ([]byte, error) { func (r *PTRResource) pack(msg []byte, compression map[string]int) ([]byte, error) {
return packName(msg, r.PTR, compression) return r.PTR.pack(msg, compression)
} }
func unpackPTRResource(hdr ResourceHeader, msg []byte, off int) (*PTRResource, error) { func unpackPTRResource(msg []byte, off int) (PTRResource, error) {
ptr, _, err := unpackName(msg, off) var ptr Name
if err != nil { if _, err := ptr.unpack(msg, off); err != nil {
return nil, err return PTRResource{}, err
} }
return &PTRResource{hdr, ptr}, nil return PTRResource{ptr}, nil
} }
// An SOAResource is an SOA Resource record. // An SOAResource is an SOA Resource record.
type SOAResource struct { type SOAResource struct {
ResourceHeader NS Name
MBox Name
NS string
MBox string
Serial uint32 Serial uint32
Refresh uint32 Refresh uint32
Retry uint32 Retry uint32
...@@ -1245,11 +1827,11 @@ func (r *SOAResource) realType() Type { ...@@ -1245,11 +1827,11 @@ func (r *SOAResource) realType() Type {
func (r *SOAResource) pack(msg []byte, compression map[string]int) ([]byte, error) { func (r *SOAResource) pack(msg []byte, compression map[string]int) ([]byte, error) {
oldMsg := msg oldMsg := msg
msg, err := packName(msg, r.NS, compression) msg, err := r.NS.pack(msg, compression)
if err != nil { if err != nil {
return oldMsg, &nestedError{"SOAResource.NS", err} return oldMsg, &nestedError{"SOAResource.NS", err}
} }
msg, err = packName(msg, r.MBox, compression) msg, err = r.MBox.pack(msg, compression)
if err != nil { if err != nil {
return oldMsg, &nestedError{"SOAResource.MBox", err} return oldMsg, &nestedError{"SOAResource.MBox", err}
} }
...@@ -1260,42 +1842,41 @@ func (r *SOAResource) pack(msg []byte, compression map[string]int) ([]byte, erro ...@@ -1260,42 +1842,41 @@ func (r *SOAResource) pack(msg []byte, compression map[string]int) ([]byte, erro
return packUint32(msg, r.MinTTL), nil return packUint32(msg, r.MinTTL), nil
} }
func unpackSOAResource(hdr ResourceHeader, msg []byte, off int) (*SOAResource, error) { func unpackSOAResource(msg []byte, off int) (SOAResource, error) {
ns, off, err := unpackName(msg, off) var ns Name
off, err := ns.unpack(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"NS", err} return SOAResource{}, &nestedError{"NS", err}
} }
mbox, off, err := unpackName(msg, off) var mbox Name
if err != nil { if off, err = mbox.unpack(msg, off); err != nil {
return nil, &nestedError{"MBox", err} return SOAResource{}, &nestedError{"MBox", err}
} }
serial, off, err := unpackUint32(msg, off) serial, off, err := unpackUint32(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"Serial", err} return SOAResource{}, &nestedError{"Serial", err}
} }
refresh, off, err := unpackUint32(msg, off) refresh, off, err := unpackUint32(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"Refresh", err} return SOAResource{}, &nestedError{"Refresh", err}
} }
retry, off, err := unpackUint32(msg, off) retry, off, err := unpackUint32(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"Retry", err} return SOAResource{}, &nestedError{"Retry", err}
} }
expire, off, err := unpackUint32(msg, off) expire, off, err := unpackUint32(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"Expire", err} return SOAResource{}, &nestedError{"Expire", err}
} }
minTTL, _, err := unpackUint32(msg, off) minTTL, _, err := unpackUint32(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"MinTTL", err} return SOAResource{}, &nestedError{"MinTTL", err}
} }
return &SOAResource{hdr, ns, mbox, serial, refresh, retry, expire, minTTL}, nil return SOAResource{ns, mbox, serial, refresh, retry, expire, minTTL}, nil
} }
// A TXTResource is a TXT Resource record. // A TXTResource is a TXT Resource record.
type TXTResource struct { type TXTResource struct {
ResourceHeader
Txt string // Not a domain name. Txt string // Not a domain name.
} }
...@@ -1307,32 +1888,30 @@ func (r *TXTResource) pack(msg []byte, compression map[string]int) ([]byte, erro ...@@ -1307,32 +1888,30 @@ func (r *TXTResource) pack(msg []byte, compression map[string]int) ([]byte, erro
return packText(msg, r.Txt), nil return packText(msg, r.Txt), nil
} }
func unpackTXTResource(hdr ResourceHeader, msg []byte, off int) (*TXTResource, error) { func unpackTXTResource(msg []byte, off int, length uint16) (TXTResource, error) {
var txt string var txt string
for n := uint16(0); n < hdr.Length; { for n := uint16(0); n < length; {
var t string var t string
var err error var err error
if t, off, err = unpackText(msg, off); err != nil { if t, off, err = unpackText(msg, off); err != nil {
return nil, &nestedError{"text", err} return TXTResource{}, &nestedError{"text", err}
} }
// Check if we got too many bytes. // Check if we got too many bytes.
if hdr.Length-n < uint16(len(t))+1 { if length-n < uint16(len(t))+1 {
return nil, errCalcLen return TXTResource{}, errCalcLen
} }
n += uint16(len(t)) + 1 n += uint16(len(t)) + 1
txt += t txt += t
} }
return &TXTResource{hdr, txt}, nil return TXTResource{txt}, nil
} }
// An SRVResource is an SRV Resource record. // An SRVResource is an SRV Resource record.
type SRVResource struct { type SRVResource struct {
ResourceHeader
Priority uint16 Priority uint16
Weight uint16 Weight uint16
Port uint16 Port uint16
Target string // Not compressed as per RFC 2782. Target Name // Not compressed as per RFC 2782.
} }
func (r *SRVResource) realType() Type { func (r *SRVResource) realType() Type {
...@@ -1344,37 +1923,35 @@ func (r *SRVResource) pack(msg []byte, compression map[string]int) ([]byte, erro ...@@ -1344,37 +1923,35 @@ func (r *SRVResource) pack(msg []byte, compression map[string]int) ([]byte, erro
msg = packUint16(msg, r.Priority) msg = packUint16(msg, r.Priority)
msg = packUint16(msg, r.Weight) msg = packUint16(msg, r.Weight)
msg = packUint16(msg, r.Port) msg = packUint16(msg, r.Port)
msg, err := packName(msg, r.Target, nil) msg, err := r.Target.pack(msg, nil)
if err != nil { if err != nil {
return oldMsg, &nestedError{"SRVResource.Target", err} return oldMsg, &nestedError{"SRVResource.Target", err}
} }
return msg, nil return msg, nil
} }
func unpackSRVResource(hdr ResourceHeader, msg []byte, off int) (*SRVResource, error) { func unpackSRVResource(msg []byte, off int) (SRVResource, error) {
priority, off, err := unpackUint16(msg, off) priority, off, err := unpackUint16(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"Priority", err} return SRVResource{}, &nestedError{"Priority", err}
} }
weight, off, err := unpackUint16(msg, off) weight, off, err := unpackUint16(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"Weight", err} return SRVResource{}, &nestedError{"Weight", err}
} }
port, off, err := unpackUint16(msg, off) port, off, err := unpackUint16(msg, off)
if err != nil { if err != nil {
return nil, &nestedError{"Port", err} return SRVResource{}, &nestedError{"Port", err}
} }
target, _, err := unpackName(msg, off) var target Name
if err != nil { if _, err := target.unpack(msg, off); err != nil {
return nil, &nestedError{"Target", err} return SRVResource{}, &nestedError{"Target", err}
} }
return &SRVResource{hdr, priority, weight, port, target}, nil return SRVResource{priority, weight, port, target}, nil
} }
// An AResource is an A Resource record. // An AResource is an A Resource record.
type AResource struct { type AResource struct {
ResourceHeader
A [4]byte A [4]byte
} }
...@@ -1386,18 +1963,16 @@ func (r *AResource) pack(msg []byte, compression map[string]int) ([]byte, error) ...@@ -1386,18 +1963,16 @@ func (r *AResource) pack(msg []byte, compression map[string]int) ([]byte, error)
return packBytes(msg, r.A[:]), nil return packBytes(msg, r.A[:]), nil
} }
func unpackAResource(hdr ResourceHeader, msg []byte, off int) (*AResource, error) { func unpackAResource(msg []byte, off int) (AResource, error) {
var a [4]byte var a [4]byte
if _, err := unpackBytes(msg, off, a[:]); err != nil { if _, err := unpackBytes(msg, off, a[:]); err != nil {
return nil, err return AResource{}, err
} }
return &AResource{hdr, a}, nil return AResource{a}, nil
} }
// An AAAAResource is an AAAA Resource record. // An AAAAResource is an AAAA Resource record.
type AAAAResource struct { type AAAAResource struct {
ResourceHeader
AAAA [16]byte AAAA [16]byte
} }
...@@ -1409,10 +1984,10 @@ func (r *AAAAResource) pack(msg []byte, compression map[string]int) ([]byte, err ...@@ -1409,10 +1984,10 @@ func (r *AAAAResource) pack(msg []byte, compression map[string]int) ([]byte, err
return packBytes(msg, r.AAAA[:]), nil return packBytes(msg, r.AAAA[:]), nil
} }
func unpackAAAAResource(hdr ResourceHeader, msg []byte, off int) (*AAAAResource, error) { func unpackAAAAResource(msg []byte, off int) (AAAAResource, error) {
var aaaa [16]byte var aaaa [16]byte
if _, err := unpackBytes(msg, off, aaaa[:]); err != nil { if _, err := unpackBytes(msg, off, aaaa[:]); err != nil {
return nil, err return AAAAResource{}, err
} }
return &AAAAResource{hdr, aaaa}, nil return AAAAResource{aaaa}, nil
} }
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package dnsmessage package dnsmessage
import ( import (
"bytes"
"fmt" "fmt"
"net" "net"
"reflect" "reflect"
...@@ -12,6 +13,14 @@ import ( ...@@ -12,6 +13,14 @@ import (
"testing" "testing"
) )
func mustNewName(name string) Name {
n, err := NewName(name)
if err != nil {
panic(err)
}
return n
}
func (m *Message) String() string { func (m *Message) String() string {
s := fmt.Sprintf("Message: %#v\n", &m.Header) s := fmt.Sprintf("Message: %#v\n", &m.Header)
if len(m.Questions) > 0 { if len(m.Questions) > 0 {
...@@ -41,9 +50,17 @@ func (m *Message) String() string { ...@@ -41,9 +50,17 @@ func (m *Message) String() string {
return s return s
} }
func TestNameString(t *testing.T) {
want := "foo"
name := mustNewName(want)
if got := fmt.Sprint(name); got != want {
t.Errorf("got fmt.Sprint(%#v) = %s, want = %s", name, got, want)
}
}
func TestQuestionPackUnpack(t *testing.T) { func TestQuestionPackUnpack(t *testing.T) {
want := Question{ want := Question{
Name: ".", Name: mustNewName("."),
Type: TypeA, Type: TypeA,
Class: ClassINET, Class: ClassINET,
} }
...@@ -68,16 +85,42 @@ func TestQuestionPackUnpack(t *testing.T) { ...@@ -68,16 +85,42 @@ func TestQuestionPackUnpack(t *testing.T) {
} }
} }
func TestName(t *testing.T) {
tests := []string{
"",
".",
"google..com",
"google.com",
"google..com.",
"google.com.",
".google.com.",
"www..google.com.",
"www.google.com.",
}
for _, test := range tests {
n, err := NewName(test)
if err != nil {
t.Errorf("Creating name for %q: %v", test, err)
continue
}
if ns := n.String(); ns != test {
t.Errorf("Got %#v.String() = %q, want = %q", n, ns, test)
continue
}
}
}
func TestNamePackUnpack(t *testing.T) { func TestNamePackUnpack(t *testing.T) {
tests := []struct { tests := []struct {
in string in string
want string want string
err error err error
}{ }{
{"", ".", nil}, {"", "", errNonCanonicalName},
{".", ".", nil}, {".", ".", nil},
{"google..com", "", errZeroSegLen}, {"google..com", "", errNonCanonicalName},
{"google.com", "google.com.", nil}, {"google.com", "", errNonCanonicalName},
{"google..com.", "", errZeroSegLen}, {"google..com.", "", errZeroSegLen},
{"google.com.", "google.com.", nil}, {"google.com.", "google.com.", nil},
{".google.com.", "", errZeroSegLen}, {".google.com.", "", errZeroSegLen},
...@@ -86,29 +129,32 @@ func TestNamePackUnpack(t *testing.T) { ...@@ -86,29 +129,32 @@ func TestNamePackUnpack(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
buf, err := packName(make([]byte, 0, 30), test.in, map[string]int{}) in := mustNewName(test.in)
want := mustNewName(test.want)
buf, err := in.pack(make([]byte, 0, 30), map[string]int{})
if err != test.err { if err != test.err {
t.Errorf("Packing of %s: got err = %v, want err = %v", test.in, err, test.err) t.Errorf("Packing of %q: got err = %v, want err = %v", test.in, err, test.err)
continue continue
} }
if test.err != nil { if test.err != nil {
continue continue
} }
got, n, err := unpackName(buf, 0) var got Name
n, err := got.unpack(buf, 0)
if err != nil { if err != nil {
t.Errorf("Unpacking for %s failed: %v", test.in, err) t.Errorf("Unpacking for %q failed: %v", test.in, err)
continue continue
} }
if n != len(buf) { if n != len(buf) {
t.Errorf( t.Errorf(
"Unpacked different amount than packed for %s: got n = %d, want = %d", "Unpacked different amount than packed for %q: got n = %d, want = %d",
test.in, test.in,
n, n,
len(buf), len(buf),
) )
} }
if got != test.want { if got != want {
t.Errorf("Unpacking packing of %s: got = %s, want = %s", test.in, got, test.want) t.Errorf("Unpacking packing of %q: got = %#v, want = %#v", test.in, got, want)
} }
} }
} }
...@@ -118,7 +164,7 @@ func TestDNSPackUnpack(t *testing.T) { ...@@ -118,7 +164,7 @@ func TestDNSPackUnpack(t *testing.T) {
{ {
Questions: []Question{ Questions: []Question{
{ {
Name: ".", Name: mustNewName("."),
Type: TypeAAAA, Type: TypeAAAA,
Class: ClassINET, Class: ClassINET,
}, },
...@@ -238,32 +284,161 @@ func TestTooManyRecords(t *testing.T) { ...@@ -238,32 +284,161 @@ func TestTooManyRecords(t *testing.T) {
} }
func TestVeryLongTxt(t *testing.T) { func TestVeryLongTxt(t *testing.T) {
want := &TXTResource{ want := Resource{
ResourceHeader: ResourceHeader{ ResourceHeader{
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeTXT, Type: TypeTXT,
Class: ClassINET, Class: ClassINET,
}, },
Txt: loremIpsum, &TXTResource{loremIpsum},
} }
buf, err := packResource(make([]byte, 0, 8000), want, map[string]int{}) buf, err := want.pack(make([]byte, 0, 8000), map[string]int{})
if err != nil { if err != nil {
t.Fatal("Packing failed:", err) t.Fatal("Packing failed:", err)
} }
var hdr ResourceHeader var got Resource
off, err := hdr.unpack(buf, 0) off, err := got.Header.unpack(buf, 0)
if err != nil { if err != nil {
t.Fatal("Unpacking ResourceHeader failed:", err) t.Fatal("Unpacking ResourceHeader failed:", err)
} }
got, n, err := unpackResource(buf, off, hdr) body, n, err := unpackResourceBody(buf, off, got.Header)
if err != nil { if err != nil {
t.Fatal("Unpacking failed:", err) t.Fatal("Unpacking failed:", err)
} }
got.Body = body
if n != len(buf) { if n != len(buf) {
t.Errorf("Unpacked different amount than packed: got n = %d, want = %d", n, len(buf)) t.Errorf("Unpacked different amount than packed: got n = %d, want = %d", n, len(buf))
} }
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("Got = %+v, want = %+v", got, want) t.Errorf("Got = %#v, want = %#v", got, want)
}
}
func TestStartError(t *testing.T) {
tests := []struct {
name string
fn func(*Builder) error
}{
{"Questions", func(b *Builder) error { return b.StartQuestions() }},
{"Answers", func(b *Builder) error { return b.StartAnswers() }},
{"Authorities", func(b *Builder) error { return b.StartAuthorities() }},
{"Additionals", func(b *Builder) error { return b.StartAdditionals() }},
}
envs := []struct {
name string
fn func() *Builder
want error
}{
{"sectionNotStarted", func() *Builder { return &Builder{section: sectionNotStarted} }, ErrNotStarted},
{"sectionDone", func() *Builder { return &Builder{section: sectionDone} }, ErrSectionDone},
}
for _, env := range envs {
for _, test := range tests {
if got := test.fn(env.fn()); got != env.want {
t.Errorf("got Builder{%s}.Start%s = %v, want = %v", env.name, test.name, got, env.want)
}
}
}
}
func TestBuilderResourceError(t *testing.T) {
tests := []struct {
name string
fn func(*Builder) error
}{
{"CNAMEResource", func(b *Builder) error { return b.CNAMEResource(ResourceHeader{}, CNAMEResource{}) }},
{"MXResource", func(b *Builder) error { return b.MXResource(ResourceHeader{}, MXResource{}) }},
{"NSResource", func(b *Builder) error { return b.NSResource(ResourceHeader{}, NSResource{}) }},
{"PTRResource", func(b *Builder) error { return b.PTRResource(ResourceHeader{}, PTRResource{}) }},
{"SOAResource", func(b *Builder) error { return b.SOAResource(ResourceHeader{}, SOAResource{}) }},
{"TXTResource", func(b *Builder) error { return b.TXTResource(ResourceHeader{}, TXTResource{}) }},
{"SRVResource", func(b *Builder) error { return b.SRVResource(ResourceHeader{}, SRVResource{}) }},
{"AResource", func(b *Builder) error { return b.AResource(ResourceHeader{}, AResource{}) }},
{"AAAAResource", func(b *Builder) error { return b.AAAAResource(ResourceHeader{}, AAAAResource{}) }},
}
envs := []struct {
name string
fn func() *Builder
want error
}{
{"sectionNotStarted", func() *Builder { return &Builder{section: sectionNotStarted} }, ErrNotStarted},
{"sectionHeader", func() *Builder { return &Builder{section: sectionHeader} }, ErrNotStarted},
{"sectionQuestions", func() *Builder { return &Builder{section: sectionQuestions} }, ErrNotStarted},
{"sectionDone", func() *Builder { return &Builder{section: sectionDone} }, ErrSectionDone},
}
for _, env := range envs {
for _, test := range tests {
if got := test.fn(env.fn()); got != env.want {
t.Errorf("got Builder{%s}.%s = %v, want = %v", env.name, test.name, got, env.want)
}
}
}
}
func TestFinishError(t *testing.T) {
var b Builder
want := ErrNotStarted
if _, got := b.Finish(); got != want {
t.Errorf("got Builder{}.Finish() = %v, want = %v", got, want)
}
}
func TestBuilder(t *testing.T) {
msg := largeTestMsg()
want, err := msg.Pack()
if err != nil {
t.Fatal("Packing without builder:", err)
}
var b Builder
b.Start(nil, msg.Header)
if err := b.StartQuestions(); err != nil {
t.Fatal("b.StartQuestions():", err)
}
for _, q := range msg.Questions {
if err := b.Question(q); err != nil {
t.Fatalf("b.Question(%#v): %v", q, err)
}
}
if err := b.StartAnswers(); err != nil {
t.Fatal("b.StartAnswers():", err)
}
for _, a := range msg.Answers {
if err := b.AResource(a.Header, *a.Body.(*AResource)); err != nil {
t.Fatalf("b.AResource(%#v): %v", a, err)
}
}
if err := b.StartAuthorities(); err != nil {
t.Fatal("b.StartAuthorities():", err)
}
for _, a := range msg.Authorities {
if err := b.NSResource(a.Header, *a.Body.(*NSResource)); err != nil {
t.Fatalf("b.NSResource(%#v): %v", a, err)
}
}
if err := b.StartAdditionals(); err != nil {
t.Fatal("b.StartAdditionals():", err)
}
for _, a := range msg.Additionals {
if err := b.TXTResource(a.Header, *a.Body.(*TXTResource)); err != nil {
t.Fatalf("b.TXTResource(%#v): %v", a, err)
}
}
got, err := b.Finish()
if err != nil {
t.Fatal("b.Finish():", err)
}
if !bytes.Equal(got, want) {
t.Fatalf("Got from Builder: %#v\nwant = %#v", got, want)
} }
} }
...@@ -272,32 +447,32 @@ func ExampleHeaderSearch() { ...@@ -272,32 +447,32 @@ func ExampleHeaderSearch() {
Header: Header{Response: true, Authoritative: true}, Header: Header{Response: true, Authoritative: true},
Questions: []Question{ Questions: []Question{
{ {
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeA, Type: TypeA,
Class: ClassINET, Class: ClassINET,
}, },
{ {
Name: "bar.example.com.", Name: mustNewName("bar.example.com."),
Type: TypeA, Type: TypeA,
Class: ClassINET, Class: ClassINET,
}, },
}, },
Answers: []Resource{ Answers: []Resource{
&AResource{ Resource{
ResourceHeader: ResourceHeader{ ResourceHeader{
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeA, Type: TypeA,
Class: ClassINET, Class: ClassINET,
}, },
A: [4]byte{127, 0, 0, 1}, &AResource{[4]byte{127, 0, 0, 1}},
}, },
&AResource{ Resource{
ResourceHeader: ResourceHeader{ ResourceHeader{
Name: "bar.example.com.", Name: mustNewName("bar.example.com."),
Type: TypeA, Type: TypeA,
Class: ClassINET, Class: ClassINET,
}, },
A: [4]byte{127, 0, 0, 2}, &AResource{[4]byte{127, 0, 0, 2}},
}, },
}, },
} }
...@@ -323,7 +498,7 @@ func ExampleHeaderSearch() { ...@@ -323,7 +498,7 @@ func ExampleHeaderSearch() {
panic(err) panic(err)
} }
if q.Name != wantName { if q.Name.String() != wantName {
continue continue
} }
...@@ -348,23 +523,25 @@ func ExampleHeaderSearch() { ...@@ -348,23 +523,25 @@ func ExampleHeaderSearch() {
continue continue
} }
if !strings.EqualFold(h.Name, wantName) { if !strings.EqualFold(h.Name.String(), wantName) {
if err := p.SkipAnswer(); err != nil { if err := p.SkipAnswer(); err != nil {
panic(err) panic(err)
} }
continue continue
} }
a, err := p.Answer()
if err != nil {
panic(err)
}
switch r := a.(type) { switch h.Type {
default: case TypeA:
panic(fmt.Sprintf("unknown type: %T", r)) r, err := p.AResource()
case *AResource: if err != nil {
panic(err)
}
gotIPs = append(gotIPs, r.A[:]) gotIPs = append(gotIPs, r.A[:])
case *AAAAResource: case TypeAAAA:
r, err := p.AAAAResource()
if err != nil {
panic(err)
}
gotIPs = append(gotIPs, r.AAAA[:]) gotIPs = append(gotIPs, r.AAAA[:])
} }
} }
...@@ -376,68 +553,223 @@ func ExampleHeaderSearch() { ...@@ -376,68 +553,223 @@ func ExampleHeaderSearch() {
// Found A/AAAA records for name bar.example.com.: [127.0.0.2] // Found A/AAAA records for name bar.example.com.: [127.0.0.2]
} }
func BenchmarkParsing(b *testing.B) {
b.ReportAllocs()
name := mustNewName("foo.bar.example.com.")
msg := Message{
Header: Header{Response: true, Authoritative: true},
Questions: []Question{
{
Name: name,
Type: TypeA,
Class: ClassINET,
},
},
Answers: []Resource{
Resource{
ResourceHeader{
Name: name,
Class: ClassINET,
},
&AResource{[4]byte{}},
},
Resource{
ResourceHeader{
Name: name,
Class: ClassINET,
},
&AAAAResource{[16]byte{}},
},
Resource{
ResourceHeader{
Name: name,
Class: ClassINET,
},
&CNAMEResource{name},
},
Resource{
ResourceHeader{
Name: name,
Class: ClassINET,
},
&NSResource{name},
},
},
}
buf, err := msg.Pack()
if err != nil {
b.Fatal("msg.Pack():", err)
}
for i := 0; i < b.N; i++ {
var p Parser
if _, err := p.Start(buf); err != nil {
b.Fatal("p.Start(buf):", err)
}
for {
_, err := p.Question()
if err == ErrSectionDone {
break
}
if err != nil {
b.Fatal("p.Question():", err)
}
}
for {
h, err := p.AnswerHeader()
if err == ErrSectionDone {
break
}
if err != nil {
panic(err)
}
switch h.Type {
case TypeA:
if _, err := p.AResource(); err != nil {
b.Fatal("p.AResource():", err)
}
case TypeAAAA:
if _, err := p.AAAAResource(); err != nil {
b.Fatal("p.AAAAResource():", err)
}
case TypeCNAME:
if _, err := p.CNAMEResource(); err != nil {
b.Fatal("p.CNAMEResource():", err)
}
case TypeNS:
if _, err := p.NSResource(); err != nil {
b.Fatal("p.NSResource():", err)
}
default:
b.Fatalf("unknown type: %T", h)
}
}
}
}
func BenchmarkBuilding(b *testing.B) {
b.ReportAllocs()
name := mustNewName("foo.bar.example.com.")
buf := make([]byte, 0, packStartingCap)
for i := 0; i < b.N; i++ {
var bld Builder
bld.StartWithoutCompression(buf, Header{Response: true, Authoritative: true})
if err := bld.StartQuestions(); err != nil {
b.Fatal("bld.StartQuestions():", err)
}
q := Question{
Name: name,
Type: TypeA,
Class: ClassINET,
}
if err := bld.Question(q); err != nil {
b.Fatalf("bld.Question(%+v): %v", q, err)
}
hdr := ResourceHeader{
Name: name,
Class: ClassINET,
}
if err := bld.StartAnswers(); err != nil {
b.Fatal("bld.StartQuestions():", err)
}
ar := AResource{[4]byte{}}
if err := bld.AResource(hdr, ar); err != nil {
b.Fatalf("bld.AResource(%+v, %+v): %v", hdr, ar, err)
}
aaar := AAAAResource{[16]byte{}}
if err := bld.AAAAResource(hdr, aaar); err != nil {
b.Fatalf("bld.AAAAResource(%+v, %+v): %v", hdr, aaar, err)
}
cnr := CNAMEResource{name}
if err := bld.CNAMEResource(hdr, cnr); err != nil {
b.Fatalf("bld.CNAMEResource(%+v, %+v): %v", hdr, cnr, err)
}
nsr := NSResource{name}
if err := bld.NSResource(hdr, nsr); err != nil {
b.Fatalf("bld.NSResource(%+v, %+v): %v", hdr, nsr, err)
}
if _, err := bld.Finish(); err != nil {
b.Fatal("bld.Finish():", err)
}
}
}
func largeTestMsg() Message { func largeTestMsg() Message {
return Message{ return Message{
Header: Header{Response: true, Authoritative: true}, Header: Header{Response: true, Authoritative: true},
Questions: []Question{ Questions: []Question{
{ {
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeA, Type: TypeA,
Class: ClassINET, Class: ClassINET,
}, },
}, },
Answers: []Resource{ Answers: []Resource{
&AResource{ Resource{
ResourceHeader: ResourceHeader{ ResourceHeader{
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeA, Type: TypeA,
Class: ClassINET, Class: ClassINET,
}, },
A: [4]byte{127, 0, 0, 1}, &AResource{[4]byte{127, 0, 0, 1}},
}, },
&AResource{ Resource{
ResourceHeader: ResourceHeader{ ResourceHeader{
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeA, Type: TypeA,
Class: ClassINET, Class: ClassINET,
}, },
A: [4]byte{127, 0, 0, 2}, &AResource{[4]byte{127, 0, 0, 2}},
}, },
}, },
Authorities: []Resource{ Authorities: []Resource{
&NSResource{ Resource{
ResourceHeader: ResourceHeader{ ResourceHeader{
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeNS, Type: TypeNS,
Class: ClassINET, Class: ClassINET,
}, },
NS: "ns1.example.com.", &NSResource{mustNewName("ns1.example.com.")},
}, },
&NSResource{ Resource{
ResourceHeader: ResourceHeader{ ResourceHeader{
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeNS, Type: TypeNS,
Class: ClassINET, Class: ClassINET,
}, },
NS: "ns2.example.com.", &NSResource{mustNewName("ns2.example.com.")},
}, },
}, },
Additionals: []Resource{ Additionals: []Resource{
&TXTResource{ Resource{
ResourceHeader: ResourceHeader{ ResourceHeader{
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeTXT, Type: TypeTXT,
Class: ClassINET, Class: ClassINET,
}, },
Txt: "So Long, and Thanks for All the Fish", &TXTResource{"So Long, and Thanks for All the Fish"},
}, },
&TXTResource{ Resource{
ResourceHeader: ResourceHeader{ ResourceHeader{
Name: "foo.bar.example.com.", Name: mustNewName("foo.bar.example.com."),
Type: TypeTXT, Type: TypeTXT,
Class: ClassINET, Class: ClassINET,
}, },
Txt: "Hamster Huey and the Gooey Kablooie", &TXTResource{"Hamster Huey and the Gooey Kablooie"},
}, },
}, },
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment