1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package dnsmessage provides a mostly RFC 1035 compliant implementation of
6 // DNS message packing and unpacking.
8 // The package also supports messages with Extension Mechanisms for DNS
9 // (EDNS(0)) as defined in RFC 6891.
11 // This implementation is designed to minimize heap allocations and avoid
12 // unnecessary packing and unpacking as much as possible.
21 // A Type is a type of DNS request and response.
25 // ResourceHeader.Type and Question.Type
45 var typeNames = map[Type]string{
48 TypeCNAME: "TypeCNAME",
57 TypeHINFO: "TypeHINFO",
58 TypeMINFO: "TypeMINFO",
63 // String implements fmt.Stringer.String.
64 func (t Type) String() string {
65 if n, ok := typeNames[t]; ok {
68 return printUint16(uint16(t))
71 // GoString implements fmt.GoStringer.GoString.
72 func (t Type) GoString() string {
73 if n, ok := typeNames[t]; ok {
74 return "dnsmessage." + n
76 return printUint16(uint16(t))
79 // A Class is a type of network.
83 // ResourceHeader.Class and Question.Class
93 var classNames = map[Class]string{
94 ClassINET: "ClassINET",
95 ClassCSNET: "ClassCSNET",
96 ClassCHAOS: "ClassCHAOS",
97 ClassHESIOD: "ClassHESIOD",
101 // String implements fmt.Stringer.String.
102 func (c Class) String() string {
103 if n, ok := classNames[c]; ok {
106 return printUint16(uint16(c))
109 // GoString implements fmt.GoStringer.GoString.
110 func (c Class) GoString() string {
111 if n, ok := classNames[c]; ok {
112 return "dnsmessage." + n
114 return printUint16(uint16(c))
117 // An OpCode is a DNS operation code.
120 // GoString implements fmt.GoStringer.GoString.
121 func (o OpCode) GoString() string {
122 return printUint16(uint16(o))
125 // An RCode is a DNS response status code.
130 RCodeSuccess RCode = 0
131 RCodeFormatError RCode = 1
132 RCodeServerFailure RCode = 2
133 RCodeNameError RCode = 3
134 RCodeNotImplemented RCode = 4
135 RCodeRefused RCode = 5
138 var rCodeNames = map[RCode]string{
139 RCodeSuccess: "RCodeSuccess",
140 RCodeFormatError: "RCodeFormatError",
141 RCodeServerFailure: "RCodeServerFailure",
142 RCodeNameError: "RCodeNameError",
143 RCodeNotImplemented: "RCodeNotImplemented",
144 RCodeRefused: "RCodeRefused",
147 // String implements fmt.Stringer.String.
148 func (r RCode) String() string {
149 if n, ok := rCodeNames[r]; ok {
152 return printUint16(uint16(r))
155 // GoString implements fmt.GoStringer.GoString.
156 func (r RCode) GoString() string {
157 if n, ok := rCodeNames[r]; ok {
158 return "dnsmessage." + n
160 return printUint16(uint16(r))
163 func printPaddedUint8(i uint8) string {
165 return string([]byte{
172 func printUint8Bytes(buf []byte, i uint8) []byte {
175 buf = append(buf, b/100+'0')
178 buf = append(buf, b/10%10+'0')
180 return append(buf, b%10+'0')
183 func printByteSlice(b []byte) string {
187 buf := make([]byte, 0, 5*len(b))
188 buf = printUint8Bytes(buf, uint8(b[0]))
189 for _, n := range b[1:] {
190 buf = append(buf, ',', ' ')
191 buf = printUint8Bytes(buf, uint8(n))
196 const hexDigits = "0123456789abcdef"
198 func printString(str []byte) string {
199 buf := make([]byte, 0, len(str))
200 for i := 0; i < len(str); i++ {
202 if c == '.' || c == '-' || c == ' ' ||
203 'A' <= c && c <= 'Z' ||
204 'a' <= c && c <= 'z' ||
205 '0' <= c && c <= '9' {
211 lower := (c << 4) >> 4
223 func printUint16(i uint16) string {
224 return printUint32(uint32(i))
227 func printUint32(i uint32) string {
228 // Max value is 4294967295.
229 buf := make([]byte, 10)
230 for b, d := buf, uint32(1000000000); d > 0; d /= 10 {
231 b[0] = byte(i/d%10 + '0')
232 if b[0] == '0' && len(b) == len(buf) && len(buf) > 1 {
241 func printBool(b bool) string {
249 // ErrNotStarted indicates that the prerequisite information isn't
250 // available yet because the previous records haven't been appropriately
251 // parsed, skipped or finished.
252 ErrNotStarted = errors.New("parsing/packing of this type isn't available yet")
254 // ErrSectionDone indicated that all records in the section have been
255 // parsed or finished.
256 ErrSectionDone = errors.New("parsing/packing of this section has completed")
258 errBaseLen = errors.New("insufficient data for base length type")
259 errCalcLen = errors.New("insufficient data for calculated length type")
260 errReserved = errors.New("segment prefix is reserved")
261 errTooManyPtr = errors.New("too many pointers (>10)")
262 errInvalidPtr = errors.New("invalid pointer")
263 errNilResouceBody = errors.New("nil resource body")
264 errResourceLen = errors.New("insufficient data for resource body length")
265 errSegTooLong = errors.New("segment length too long")
266 errZeroSegLen = errors.New("zero length segment")
267 errResTooLong = errors.New("resource length too long")
268 errTooManyQuestions = errors.New("too many Questions to pack (>65535)")
269 errTooManyAnswers = errors.New("too many Answers to pack (>65535)")
270 errTooManyAuthorities = errors.New("too many Authorities to pack (>65535)")
271 errTooManyAdditionals = errors.New("too many Additionals to pack (>65535)")
272 errNonCanonicalName = errors.New("name is not in canonical format (it must end with a .)")
273 errStringTooLong = errors.New("character string exceeds maximum length (255)")
274 errCompressedSRV = errors.New("compressed name in SRV resource data")
277 // Internal constants.
279 // packStartingCap is the default initial buffer size allocated during
282 // The starting capacity doesn't matter too much, but most DNS responses
283 // Will be <= 512 bytes as it is the limit for DNS over UDP.
284 packStartingCap = 512
286 // uint16Len is the length (in bytes) of a uint16.
289 // uint32Len is the length (in bytes) of a uint32.
292 // headerLen is the length (in bytes) of a DNS header.
294 // A header is comprised of 6 uint16s and no padding.
295 headerLen = 6 * uint16Len
298 type nestedError struct {
299 // s is the current level's error message.
302 // err is the nested error.
306 // nestedError implements error.Error.
307 func (e *nestedError) Error() string {
308 return e.s + ": " + e.err.Error()
311 // Header is a representation of a DNS message header.
318 RecursionDesired bool
319 RecursionAvailable bool
323 func (m *Header) pack() (id uint16, bits uint16) {
325 bits = uint16(m.OpCode)<<11 | uint16(m.RCode)
326 if m.RecursionAvailable {
329 if m.RecursionDesired {
344 // GoString implements fmt.GoStringer.GoString.
345 func (m *Header) GoString() string {
346 return "dnsmessage.Header{" +
347 "ID: " + printUint16(m.ID) + ", " +
348 "Response: " + printBool(m.Response) + ", " +
349 "OpCode: " + m.OpCode.GoString() + ", " +
350 "Authoritative: " + printBool(m.Authoritative) + ", " +
351 "Truncated: " + printBool(m.Truncated) + ", " +
352 "RecursionDesired: " + printBool(m.RecursionDesired) + ", " +
353 "RecursionAvailable: " + printBool(m.RecursionAvailable) + ", " +
354 "RCode: " + m.RCode.GoString() + "}"
357 // Message is a representation of a DNS message.
358 type Message struct {
362 Authorities []Resource
363 Additionals []Resource
369 sectionNotStarted section = iota
377 headerBitQR = 1 << 15 // query/response (response=1)
378 headerBitAA = 1 << 10 // authoritative
379 headerBitTC = 1 << 9 // truncated
380 headerBitRD = 1 << 8 // recursion desired
381 headerBitRA = 1 << 7 // recursion available
384 var sectionNames = map[section]string{
385 sectionHeader: "header",
386 sectionQuestions: "Question",
387 sectionAnswers: "Answer",
388 sectionAuthorities: "Authority",
389 sectionAdditionals: "Additional",
392 // header is the wire format for a DNS message header.
402 func (h *header) count(sec section) uint16 {
404 case sectionQuestions:
408 case sectionAuthorities:
410 case sectionAdditionals:
416 // pack appends the wire format of the header to msg.
417 func (h *header) pack(msg []byte) []byte {
418 msg = packUint16(msg, h.id)
419 msg = packUint16(msg, h.bits)
420 msg = packUint16(msg, h.questions)
421 msg = packUint16(msg, h.answers)
422 msg = packUint16(msg, h.authorities)
423 return packUint16(msg, h.additionals)
426 func (h *header) unpack(msg []byte, off int) (int, error) {
429 if h.id, newOff, err = unpackUint16(msg, newOff); err != nil {
430 return off, &nestedError{"id", err}
432 if h.bits, newOff, err = unpackUint16(msg, newOff); err != nil {
433 return off, &nestedError{"bits", err}
435 if h.questions, newOff, err = unpackUint16(msg, newOff); err != nil {
436 return off, &nestedError{"questions", err}
438 if h.answers, newOff, err = unpackUint16(msg, newOff); err != nil {
439 return off, &nestedError{"answers", err}
441 if h.authorities, newOff, err = unpackUint16(msg, newOff); err != nil {
442 return off, &nestedError{"authorities", err}
444 if h.additionals, newOff, err = unpackUint16(msg, newOff); err != nil {
445 return off, &nestedError{"additionals", err}
450 func (h *header) header() Header {
453 Response: (h.bits & headerBitQR) != 0,
454 OpCode: OpCode(h.bits>>11) & 0xF,
455 Authoritative: (h.bits & headerBitAA) != 0,
456 Truncated: (h.bits & headerBitTC) != 0,
457 RecursionDesired: (h.bits & headerBitRD) != 0,
458 RecursionAvailable: (h.bits & headerBitRA) != 0,
459 RCode: RCode(h.bits & 0xF),
463 // A Resource is a DNS resource record.
464 type Resource struct {
465 Header ResourceHeader
469 func (r *Resource) GoString() string {
470 return "dnsmessage.Resource{" +
471 "Header: " + r.Header.GoString() +
472 ", Body: &" + r.Body.GoString() +
476 // A ResourceBody is a DNS resource record minus the header.
477 type ResourceBody interface {
478 // pack packs a Resource except for its header.
479 pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error)
481 // realType returns the actual type of the Resource. This is used to
482 // fill in the header Type field.
485 // GoString implements fmt.GoStringer.GoString.
489 // pack appends the wire format of the Resource to msg.
490 func (r *Resource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
492 return msg, errNilResouceBody
495 r.Header.Type = r.Body.realType()
496 msg, lenOff, err := r.Header.pack(msg, compression, compressionOff)
498 return msg, &nestedError{"ResourceHeader", err}
501 msg, err = r.Body.pack(msg, compression, compressionOff)
503 return msg, &nestedError{"content", err}
505 if err := r.Header.fixLen(msg, lenOff, preLen); err != nil {
511 // A Parser allows incrementally parsing a DNS message.
513 // When parsing is started, the Header is parsed. Next, each Question can be
514 // either parsed or skipped. Alternatively, all Questions can be skipped at
515 // once. When all Questions have been parsed, attempting to parse Questions
516 // will return (nil, nil) and attempting to skip Questions will return
517 // (true, nil). After all Questions have been either parsed or skipped, all
518 // Answers, Authorities and Additionals can be either parsed or skipped in the
519 // same way, and each type of Resource must be fully parsed or skipped before
520 // proceeding to the next type of Resource.
522 // Note that there is no requirement to fully skip or parse the message.
531 resHeader ResourceHeader
534 // Start parses the header and enables the parsing of Questions.
535 func (p *Parser) Start(msg []byte) (Header, error) {
541 if p.off, err = p.header.unpack(msg, 0); err != nil {
542 return Header{}, &nestedError{"unpacking header", err}
544 p.section = sectionQuestions
545 return p.header.header(), nil
548 func (p *Parser) checkAdvance(sec section) error {
553 return ErrSectionDone
555 p.resHeaderValid = false
556 if p.index == int(p.header.count(sec)) {
559 return ErrSectionDone
564 func (p *Parser) resource(sec section) (Resource, error) {
567 r.Header, err = p.resourceHeader(sec)
571 p.resHeaderValid = false
572 r.Body, p.off, err = unpackResourceBody(p.msg, p.off, r.Header)
574 return Resource{}, &nestedError{"unpacking " + sectionNames[sec], err}
580 func (p *Parser) resourceHeader(sec section) (ResourceHeader, error) {
581 if p.resHeaderValid {
582 return p.resHeader, nil
584 if err := p.checkAdvance(sec); err != nil {
585 return ResourceHeader{}, err
587 var hdr ResourceHeader
588 off, err := hdr.unpack(p.msg, p.off)
590 return ResourceHeader{}, err
592 p.resHeaderValid = true
598 func (p *Parser) skipResource(sec section) error {
599 if p.resHeaderValid {
600 newOff := p.off + int(p.resHeader.Length)
601 if newOff > len(p.msg) {
602 return errResourceLen
605 p.resHeaderValid = false
609 if err := p.checkAdvance(sec); err != nil {
613 p.off, err = skipResource(p.msg, p.off)
615 return &nestedError{"skipping: " + sectionNames[sec], err}
621 // Question parses a single Question.
622 func (p *Parser) Question() (Question, error) {
623 if err := p.checkAdvance(sectionQuestions); err != nil {
624 return Question{}, err
627 off, err := name.unpack(p.msg, p.off)
629 return Question{}, &nestedError{"unpacking Question.Name", err}
631 typ, off, err := unpackType(p.msg, off)
633 return Question{}, &nestedError{"unpacking Question.Type", err}
635 class, off, err := unpackClass(p.msg, off)
637 return Question{}, &nestedError{"unpacking Question.Class", err}
641 return Question{name, typ, class}, nil
644 // AllQuestions parses all Questions.
645 func (p *Parser) AllQuestions() ([]Question, error) {
646 // Multiple questions are valid according to the spec,
647 // but servers don't actually support them. There will
648 // be at most one question here.
650 // Do not pre-allocate based on info in p.header, since
651 // the data is untrusted.
654 q, err := p.Question()
655 if err == ErrSectionDone {
665 // SkipQuestion skips a single Question.
666 func (p *Parser) SkipQuestion() error {
667 if err := p.checkAdvance(sectionQuestions); err != nil {
670 off, err := skipName(p.msg, p.off)
672 return &nestedError{"skipping Question Name", err}
674 if off, err = skipType(p.msg, off); err != nil {
675 return &nestedError{"skipping Question Type", err}
677 if off, err = skipClass(p.msg, off); err != nil {
678 return &nestedError{"skipping Question Class", err}
685 // SkipAllQuestions skips all Questions.
686 func (p *Parser) SkipAllQuestions() error {
688 if err := p.SkipQuestion(); err == ErrSectionDone {
690 } else if err != nil {
696 // AnswerHeader parses a single Answer ResourceHeader.
697 func (p *Parser) AnswerHeader() (ResourceHeader, error) {
698 return p.resourceHeader(sectionAnswers)
701 // Answer parses a single Answer Resource.
702 func (p *Parser) Answer() (Resource, error) {
703 return p.resource(sectionAnswers)
706 // AllAnswers parses all Answer Resources.
707 func (p *Parser) AllAnswers() ([]Resource, error) {
708 // The most common query is for A/AAAA, which usually returns
711 // Pre-allocate up to a certain limit, since p.header is
713 n := int(p.header.answers)
717 as := make([]Resource, 0, n)
720 if err == ErrSectionDone {
730 // SkipAnswer skips a single Answer Resource.
731 func (p *Parser) SkipAnswer() error {
732 return p.skipResource(sectionAnswers)
735 // SkipAllAnswers skips all Answer Resources.
736 func (p *Parser) SkipAllAnswers() error {
738 if err := p.SkipAnswer(); err == ErrSectionDone {
740 } else if err != nil {
746 // AuthorityHeader parses a single Authority ResourceHeader.
747 func (p *Parser) AuthorityHeader() (ResourceHeader, error) {
748 return p.resourceHeader(sectionAuthorities)
751 // Authority parses a single Authority Resource.
752 func (p *Parser) Authority() (Resource, error) {
753 return p.resource(sectionAuthorities)
756 // AllAuthorities parses all Authority Resources.
757 func (p *Parser) AllAuthorities() ([]Resource, error) {
758 // Authorities contains SOA in case of NXDOMAIN and friends,
759 // otherwise it is empty.
761 // Pre-allocate up to a certain limit, since p.header is
763 n := int(p.header.authorities)
767 as := make([]Resource, 0, n)
769 a, err := p.Authority()
770 if err == ErrSectionDone {
780 // SkipAuthority skips a single Authority Resource.
781 func (p *Parser) SkipAuthority() error {
782 return p.skipResource(sectionAuthorities)
785 // SkipAllAuthorities skips all Authority Resources.
786 func (p *Parser) SkipAllAuthorities() error {
788 if err := p.SkipAuthority(); err == ErrSectionDone {
790 } else if err != nil {
796 // AdditionalHeader parses a single Additional ResourceHeader.
797 func (p *Parser) AdditionalHeader() (ResourceHeader, error) {
798 return p.resourceHeader(sectionAdditionals)
801 // Additional parses a single Additional Resource.
802 func (p *Parser) Additional() (Resource, error) {
803 return p.resource(sectionAdditionals)
806 // AllAdditionals parses all Additional Resources.
807 func (p *Parser) AllAdditionals() ([]Resource, error) {
808 // Additionals usually contain OPT, and sometimes A/AAAA
811 // Pre-allocate up to a certain limit, since p.header is
813 n := int(p.header.additionals)
817 as := make([]Resource, 0, n)
819 a, err := p.Additional()
820 if err == ErrSectionDone {
830 // SkipAdditional skips a single Additional Resource.
831 func (p *Parser) SkipAdditional() error {
832 return p.skipResource(sectionAdditionals)
835 // SkipAllAdditionals skips all Additional Resources.
836 func (p *Parser) SkipAllAdditionals() error {
838 if err := p.SkipAdditional(); err == ErrSectionDone {
840 } else if err != nil {
846 // CNAMEResource parses a single CNAMEResource.
848 // One of the XXXHeader methods must have been called before calling this
850 func (p *Parser) CNAMEResource() (CNAMEResource, error) {
851 if !p.resHeaderValid || p.resHeader.Type != TypeCNAME {
852 return CNAMEResource{}, ErrNotStarted
854 r, err := unpackCNAMEResource(p.msg, p.off)
856 return CNAMEResource{}, err
858 p.off += int(p.resHeader.Length)
859 p.resHeaderValid = false
864 // MXResource parses a single MXResource.
866 // One of the XXXHeader methods must have been called before calling this
868 func (p *Parser) MXResource() (MXResource, error) {
869 if !p.resHeaderValid || p.resHeader.Type != TypeMX {
870 return MXResource{}, ErrNotStarted
872 r, err := unpackMXResource(p.msg, p.off)
874 return MXResource{}, err
876 p.off += int(p.resHeader.Length)
877 p.resHeaderValid = false
882 // NSResource parses a single NSResource.
884 // One of the XXXHeader methods must have been called before calling this
886 func (p *Parser) NSResource() (NSResource, error) {
887 if !p.resHeaderValid || p.resHeader.Type != TypeNS {
888 return NSResource{}, ErrNotStarted
890 r, err := unpackNSResource(p.msg, p.off)
892 return NSResource{}, err
894 p.off += int(p.resHeader.Length)
895 p.resHeaderValid = false
900 // PTRResource parses a single PTRResource.
902 // One of the XXXHeader methods must have been called before calling this
904 func (p *Parser) PTRResource() (PTRResource, error) {
905 if !p.resHeaderValid || p.resHeader.Type != TypePTR {
906 return PTRResource{}, ErrNotStarted
908 r, err := unpackPTRResource(p.msg, p.off)
910 return PTRResource{}, err
912 p.off += int(p.resHeader.Length)
913 p.resHeaderValid = false
918 // SOAResource parses a single SOAResource.
920 // One of the XXXHeader methods must have been called before calling this
922 func (p *Parser) SOAResource() (SOAResource, error) {
923 if !p.resHeaderValid || p.resHeader.Type != TypeSOA {
924 return SOAResource{}, ErrNotStarted
926 r, err := unpackSOAResource(p.msg, p.off)
928 return SOAResource{}, err
930 p.off += int(p.resHeader.Length)
931 p.resHeaderValid = false
936 // TXTResource parses a single TXTResource.
938 // One of the XXXHeader methods must have been called before calling this
940 func (p *Parser) TXTResource() (TXTResource, error) {
941 if !p.resHeaderValid || p.resHeader.Type != TypeTXT {
942 return TXTResource{}, ErrNotStarted
944 r, err := unpackTXTResource(p.msg, p.off, p.resHeader.Length)
946 return TXTResource{}, err
948 p.off += int(p.resHeader.Length)
949 p.resHeaderValid = false
954 // SRVResource parses a single SRVResource.
956 // One of the XXXHeader methods must have been called before calling this
958 func (p *Parser) SRVResource() (SRVResource, error) {
959 if !p.resHeaderValid || p.resHeader.Type != TypeSRV {
960 return SRVResource{}, ErrNotStarted
962 r, err := unpackSRVResource(p.msg, p.off)
964 return SRVResource{}, err
966 p.off += int(p.resHeader.Length)
967 p.resHeaderValid = false
972 // AResource parses a single AResource.
974 // One of the XXXHeader methods must have been called before calling this
976 func (p *Parser) AResource() (AResource, error) {
977 if !p.resHeaderValid || p.resHeader.Type != TypeA {
978 return AResource{}, ErrNotStarted
980 r, err := unpackAResource(p.msg, p.off)
982 return AResource{}, err
984 p.off += int(p.resHeader.Length)
985 p.resHeaderValid = false
990 // AAAAResource parses a single AAAAResource.
992 // One of the XXXHeader methods must have been called before calling this
994 func (p *Parser) AAAAResource() (AAAAResource, error) {
995 if !p.resHeaderValid || p.resHeader.Type != TypeAAAA {
996 return AAAAResource{}, ErrNotStarted
998 r, err := unpackAAAAResource(p.msg, p.off)
1000 return AAAAResource{}, err
1002 p.off += int(p.resHeader.Length)
1003 p.resHeaderValid = false
1008 // OPTResource parses a single OPTResource.
1010 // One of the XXXHeader methods must have been called before calling this
1012 func (p *Parser) OPTResource() (OPTResource, error) {
1013 if !p.resHeaderValid || p.resHeader.Type != TypeOPT {
1014 return OPTResource{}, ErrNotStarted
1016 r, err := unpackOPTResource(p.msg, p.off, p.resHeader.Length)
1018 return OPTResource{}, err
1020 p.off += int(p.resHeader.Length)
1021 p.resHeaderValid = false
1026 // Unpack parses a full Message.
1027 func (m *Message) Unpack(msg []byte) error {
1030 if m.Header, err = p.Start(msg); err != nil {
1033 if m.Questions, err = p.AllQuestions(); err != nil {
1036 if m.Answers, err = p.AllAnswers(); err != nil {
1039 if m.Authorities, err = p.AllAuthorities(); err != nil {
1042 if m.Additionals, err = p.AllAdditionals(); err != nil {
1048 // Pack packs a full Message.
1049 func (m *Message) Pack() ([]byte, error) {
1050 return m.AppendPack(make([]byte, 0, packStartingCap))
1053 // AppendPack is like Pack but appends the full Message to b and returns the
1055 func (m *Message) AppendPack(b []byte) ([]byte, error) {
1056 // Validate the lengths. It is very unlikely that anyone will try to
1057 // pack more than 65535 of any particular type, but it is possible and
1058 // we should fail gracefully.
1059 if len(m.Questions) > int(^uint16(0)) {
1060 return nil, errTooManyQuestions
1062 if len(m.Answers) > int(^uint16(0)) {
1063 return nil, errTooManyAnswers
1065 if len(m.Authorities) > int(^uint16(0)) {
1066 return nil, errTooManyAuthorities
1068 if len(m.Additionals) > int(^uint16(0)) {
1069 return nil, errTooManyAdditionals
1073 h.id, h.bits = m.Header.pack()
1075 h.questions = uint16(len(m.Questions))
1076 h.answers = uint16(len(m.Answers))
1077 h.authorities = uint16(len(m.Authorities))
1078 h.additionals = uint16(len(m.Additionals))
1080 compressionOff := len(b)
1083 // RFC 1035 allows (but does not require) compression for packing. RFC
1084 // 1035 requires unpacking implementations to support compression, so
1085 // unconditionally enabling it is fine.
1087 // DNS lookups are typically done over UDP, and RFC 1035 states that UDP
1088 // DNS messages can be a maximum of 512 bytes long. Without compression,
1089 // many DNS response messages are over this limit, so enabling
1090 // compression will help ensure compliance.
1091 compression := map[string]int{}
1093 for i := range m.Questions {
1095 if msg, err = m.Questions[i].pack(msg, compression, compressionOff); err != nil {
1096 return nil, &nestedError{"packing Question", err}
1099 for i := range m.Answers {
1101 if msg, err = m.Answers[i].pack(msg, compression, compressionOff); err != nil {
1102 return nil, &nestedError{"packing Answer", err}
1105 for i := range m.Authorities {
1107 if msg, err = m.Authorities[i].pack(msg, compression, compressionOff); err != nil {
1108 return nil, &nestedError{"packing Authority", err}
1111 for i := range m.Additionals {
1113 if msg, err = m.Additionals[i].pack(msg, compression, compressionOff); err != nil {
1114 return nil, &nestedError{"packing Additional", err}
1121 // GoString implements fmt.GoStringer.GoString.
1122 func (m *Message) GoString() string {
1123 s := "dnsmessage.Message{Header: " + m.Header.GoString() + ", " +
1124 "Questions: []dnsmessage.Question{"
1125 if len(m.Questions) > 0 {
1126 s += m.Questions[0].GoString()
1127 for _, q := range m.Questions[1:] {
1128 s += ", " + q.GoString()
1131 s += "}, Answers: []dnsmessage.Resource{"
1132 if len(m.Answers) > 0 {
1133 s += m.Answers[0].GoString()
1134 for _, a := range m.Answers[1:] {
1135 s += ", " + a.GoString()
1138 s += "}, Authorities: []dnsmessage.Resource{"
1139 if len(m.Authorities) > 0 {
1140 s += m.Authorities[0].GoString()
1141 for _, a := range m.Authorities[1:] {
1142 s += ", " + a.GoString()
1145 s += "}, Additionals: []dnsmessage.Resource{"
1146 if len(m.Additionals) > 0 {
1147 s += m.Additionals[0].GoString()
1148 for _, a := range m.Additionals[1:] {
1149 s += ", " + a.GoString()
1155 // A Builder allows incrementally packing a DNS message.
1158 // buf := make([]byte, 2, 514)
1159 // b := NewBuilder(buf, Header{...})
1160 // b.EnableCompression()
1161 // // Optionally start a section and add things to that section.
1162 // // Repeat adding sections as necessary.
1163 // buf, err := b.Finish()
1164 // // If err is nil, buf[2:] will contain the built bytes.
1165 type Builder struct {
1166 // msg is the storage for the message being built.
1169 // section keeps track of the current section being built.
1172 // header keeps track of what should go in the header when Finish is
1176 // start is the starting index of the bytes allocated in msg for header.
1179 // compression is a mapping from name suffixes to their starting index
1181 compression map[string]int
1184 // NewBuilder creates a new builder with compression disabled.
1186 // Note: Most users will want to immediately enable compression with the
1187 // EnableCompression method. See that method's comment for why you may or may
1188 // not want to enable compression.
1190 // The DNS message is appended to the provided initial buffer buf (which may be
1191 // nil) as it is built. The final message is returned by the (*Builder).Finish
1192 // method, which may return the same underlying array if there was sufficient
1193 // capacity in the slice.
1194 func NewBuilder(buf []byte, h Header) Builder {
1196 buf = make([]byte, 0, packStartingCap)
1198 b := Builder{msg: buf, start: len(buf)}
1199 b.header.id, b.header.bits = h.pack()
1200 var hb [headerLen]byte
1201 b.msg = append(b.msg, hb[:]...)
1202 b.section = sectionHeader
1206 // EnableCompression enables compression in the Builder.
1208 // Leaving compression disabled avoids compression related allocations, but can
1209 // result in larger message sizes. Be careful with this mode as it can cause
1210 // messages to exceed the UDP size limit.
1212 // According to RFC 1035, section 4.1.4, the use of compression is optional, but
1213 // all implementations must accept both compressed and uncompressed DNS
1216 // Compression should be enabled before any sections are added for best results.
1217 func (b *Builder) EnableCompression() {
1218 b.compression = map[string]int{}
1221 func (b *Builder) startCheck(s section) error {
1222 if b.section <= sectionNotStarted {
1223 return ErrNotStarted
1226 return ErrSectionDone
1231 // StartQuestions prepares the builder for packing Questions.
1232 func (b *Builder) StartQuestions() error {
1233 if err := b.startCheck(sectionQuestions); err != nil {
1236 b.section = sectionQuestions
1240 // StartAnswers prepares the builder for packing Answers.
1241 func (b *Builder) StartAnswers() error {
1242 if err := b.startCheck(sectionAnswers); err != nil {
1245 b.section = sectionAnswers
1249 // StartAuthorities prepares the builder for packing Authorities.
1250 func (b *Builder) StartAuthorities() error {
1251 if err := b.startCheck(sectionAuthorities); err != nil {
1254 b.section = sectionAuthorities
1258 // StartAdditionals prepares the builder for packing Additionals.
1259 func (b *Builder) StartAdditionals() error {
1260 if err := b.startCheck(sectionAdditionals); err != nil {
1263 b.section = sectionAdditionals
1267 func (b *Builder) incrementSectionCount() error {
1271 case sectionQuestions:
1272 count = &b.header.questions
1273 err = errTooManyQuestions
1274 case sectionAnswers:
1275 count = &b.header.answers
1276 err = errTooManyAnswers
1277 case sectionAuthorities:
1278 count = &b.header.authorities
1279 err = errTooManyAuthorities
1280 case sectionAdditionals:
1281 count = &b.header.additionals
1282 err = errTooManyAdditionals
1284 if *count == ^uint16(0) {
1291 // Question adds a single Question.
1292 func (b *Builder) Question(q Question) error {
1293 if b.section < sectionQuestions {
1294 return ErrNotStarted
1296 if b.section > sectionQuestions {
1297 return ErrSectionDone
1299 msg, err := q.pack(b.msg, b.compression, b.start)
1303 if err := b.incrementSectionCount(); err != nil {
1310 func (b *Builder) checkResourceSection() error {
1311 if b.section < sectionAnswers {
1312 return ErrNotStarted
1314 if b.section > sectionAdditionals {
1315 return ErrSectionDone
1320 // CNAMEResource adds a single CNAMEResource.
1321 func (b *Builder) CNAMEResource(h ResourceHeader, r CNAMEResource) error {
1322 if err := b.checkResourceSection(); err != nil {
1325 h.Type = r.realType()
1326 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1328 return &nestedError{"ResourceHeader", err}
1331 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1332 return &nestedError{"CNAMEResource body", err}
1334 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1337 if err := b.incrementSectionCount(); err != nil {
1344 // MXResource adds a single MXResource.
1345 func (b *Builder) MXResource(h ResourceHeader, r MXResource) error {
1346 if err := b.checkResourceSection(); err != nil {
1349 h.Type = r.realType()
1350 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1352 return &nestedError{"ResourceHeader", err}
1355 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1356 return &nestedError{"MXResource body", err}
1358 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1361 if err := b.incrementSectionCount(); err != nil {
1368 // NSResource adds a single NSResource.
1369 func (b *Builder) NSResource(h ResourceHeader, r NSResource) error {
1370 if err := b.checkResourceSection(); err != nil {
1373 h.Type = r.realType()
1374 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1376 return &nestedError{"ResourceHeader", err}
1379 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1380 return &nestedError{"NSResource body", err}
1382 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1385 if err := b.incrementSectionCount(); err != nil {
1392 // PTRResource adds a single PTRResource.
1393 func (b *Builder) PTRResource(h ResourceHeader, r PTRResource) error {
1394 if err := b.checkResourceSection(); err != nil {
1397 h.Type = r.realType()
1398 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1400 return &nestedError{"ResourceHeader", err}
1403 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1404 return &nestedError{"PTRResource body", err}
1406 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1409 if err := b.incrementSectionCount(); err != nil {
1416 // SOAResource adds a single SOAResource.
1417 func (b *Builder) SOAResource(h ResourceHeader, r SOAResource) error {
1418 if err := b.checkResourceSection(); err != nil {
1421 h.Type = r.realType()
1422 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1424 return &nestedError{"ResourceHeader", err}
1427 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1428 return &nestedError{"SOAResource body", err}
1430 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1433 if err := b.incrementSectionCount(); err != nil {
1440 // TXTResource adds a single TXTResource.
1441 func (b *Builder) TXTResource(h ResourceHeader, r TXTResource) error {
1442 if err := b.checkResourceSection(); err != nil {
1445 h.Type = r.realType()
1446 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1448 return &nestedError{"ResourceHeader", err}
1451 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1452 return &nestedError{"TXTResource body", err}
1454 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1457 if err := b.incrementSectionCount(); err != nil {
1464 // SRVResource adds a single SRVResource.
1465 func (b *Builder) SRVResource(h ResourceHeader, r SRVResource) error {
1466 if err := b.checkResourceSection(); err != nil {
1469 h.Type = r.realType()
1470 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1472 return &nestedError{"ResourceHeader", err}
1475 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1476 return &nestedError{"SRVResource body", err}
1478 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1481 if err := b.incrementSectionCount(); err != nil {
1488 // AResource adds a single AResource.
1489 func (b *Builder) AResource(h ResourceHeader, r AResource) error {
1490 if err := b.checkResourceSection(); err != nil {
1493 h.Type = r.realType()
1494 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1496 return &nestedError{"ResourceHeader", err}
1499 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1500 return &nestedError{"AResource body", err}
1502 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1505 if err := b.incrementSectionCount(); err != nil {
1512 // AAAAResource adds a single AAAAResource.
1513 func (b *Builder) AAAAResource(h ResourceHeader, r AAAAResource) error {
1514 if err := b.checkResourceSection(); err != nil {
1517 h.Type = r.realType()
1518 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1520 return &nestedError{"ResourceHeader", err}
1523 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1524 return &nestedError{"AAAAResource body", err}
1526 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1529 if err := b.incrementSectionCount(); err != nil {
1536 // OPTResource adds a single OPTResource.
1537 func (b *Builder) OPTResource(h ResourceHeader, r OPTResource) error {
1538 if err := b.checkResourceSection(); err != nil {
1541 h.Type = r.realType()
1542 msg, lenOff, err := h.pack(b.msg, b.compression, b.start)
1544 return &nestedError{"ResourceHeader", err}
1547 if msg, err = r.pack(msg, b.compression, b.start); err != nil {
1548 return &nestedError{"OPTResource body", err}
1550 if err := h.fixLen(msg, lenOff, preLen); err != nil {
1553 if err := b.incrementSectionCount(); err != nil {
1560 // Finish ends message building and generates a binary message.
1561 func (b *Builder) Finish() ([]byte, error) {
1562 if b.section < sectionHeader {
1563 return nil, ErrNotStarted
1565 b.section = sectionDone
1566 // Space for the header was allocated in NewBuilder.
1567 b.header.pack(b.msg[b.start:b.start])
1571 // A ResourceHeader is the header of a DNS resource record. There are
1572 // many types of DNS resource records, but they all share the same header.
1573 type ResourceHeader struct {
1574 // Name is the domain name for which this resource record pertains.
1577 // Type is the type of DNS resource record.
1579 // This field will be set automatically during packing.
1582 // Class is the class of network to which this DNS resource record
1586 // TTL is the length of time (measured in seconds) which this resource
1587 // record is valid for (time to live). All Resources in a set should
1588 // have the same TTL (RFC 2181 Section 5.2).
1591 // Length is the length of data in the resource record after the header.
1593 // This field will be set automatically during packing.
1597 // GoString implements fmt.GoStringer.GoString.
1598 func (h *ResourceHeader) GoString() string {
1599 return "dnsmessage.ResourceHeader{" +
1600 "Name: " + h.Name.GoString() + ", " +
1601 "Type: " + h.Type.GoString() + ", " +
1602 "Class: " + h.Class.GoString() + ", " +
1603 "TTL: " + printUint32(h.TTL) + ", " +
1604 "Length: " + printUint16(h.Length) + "}"
1607 // pack appends the wire format of the ResourceHeader to oldMsg.
1609 // lenOff is the offset in msg where the Length field was packed.
1610 func (h *ResourceHeader) pack(oldMsg []byte, compression map[string]int, compressionOff int) (msg []byte, lenOff int, err error) {
1612 if msg, err = h.Name.pack(msg, compression, compressionOff); err != nil {
1613 return oldMsg, 0, &nestedError{"Name", err}
1615 msg = packType(msg, h.Type)
1616 msg = packClass(msg, h.Class)
1617 msg = packUint32(msg, h.TTL)
1619 msg = packUint16(msg, h.Length)
1620 return msg, lenOff, nil
1623 func (h *ResourceHeader) unpack(msg []byte, off int) (int, error) {
1626 if newOff, err = h.Name.unpack(msg, newOff); err != nil {
1627 return off, &nestedError{"Name", err}
1629 if h.Type, newOff, err = unpackType(msg, newOff); err != nil {
1630 return off, &nestedError{"Type", err}
1632 if h.Class, newOff, err = unpackClass(msg, newOff); err != nil {
1633 return off, &nestedError{"Class", err}
1635 if h.TTL, newOff, err = unpackUint32(msg, newOff); err != nil {
1636 return off, &nestedError{"TTL", err}
1638 if h.Length, newOff, err = unpackUint16(msg, newOff); err != nil {
1639 return off, &nestedError{"Length", err}
1644 // fixLen updates a packed ResourceHeader to include the length of the
1647 // lenOff is the offset of the ResourceHeader.Length field in msg.
1649 // preLen is the length that msg was before the ResourceBody was packed.
1650 func (h *ResourceHeader) fixLen(msg []byte, lenOff int, preLen int) error {
1651 conLen := len(msg) - preLen
1652 if conLen > int(^uint16(0)) {
1653 return errResTooLong
1656 // Fill in the length now that we know how long the content is.
1657 packUint16(msg[lenOff:lenOff], uint16(conLen))
1658 h.Length = uint16(conLen)
1663 // EDNS(0) wire costants.
1667 edns0DNSSECOK = 0x00008000
1668 ednsVersionMask = 0x00ff0000
1669 edns0DNSSECOKMask = 0x00ff8000
1672 // SetEDNS0 configures h for EDNS(0).
1674 // The provided extRCode must be an extedned RCode.
1675 func (h *ResourceHeader) SetEDNS0(udpPayloadLen int, extRCode RCode, dnssecOK bool) error {
1676 h.Name = Name{Data: [nameLen]byte{'.'}, Length: 1} // RFC 6891 section 6.1.2
1678 h.Class = Class(udpPayloadLen)
1679 h.TTL = uint32(extRCode) >> 4 << 24
1681 h.TTL |= edns0DNSSECOK
1686 // DNSSECAllowed reports whether the DNSSEC OK bit is set.
1687 func (h *ResourceHeader) DNSSECAllowed() bool {
1688 return h.TTL&edns0DNSSECOKMask == edns0DNSSECOK // RFC 6891 section 6.1.3
1691 // ExtendedRCode returns an extended RCode.
1693 // The provided rcode must be the RCode in DNS message header.
1694 func (h *ResourceHeader) ExtendedRCode(rcode RCode) RCode {
1695 if h.TTL&ednsVersionMask == edns0Version { // RFC 6891 section 6.1.3
1696 return RCode(h.TTL>>24<<4) | rcode
1701 func skipResource(msg []byte, off int) (int, error) {
1702 newOff, err := skipName(msg, off)
1704 return off, &nestedError{"Name", err}
1706 if newOff, err = skipType(msg, newOff); err != nil {
1707 return off, &nestedError{"Type", err}
1709 if newOff, err = skipClass(msg, newOff); err != nil {
1710 return off, &nestedError{"Class", err}
1712 if newOff, err = skipUint32(msg, newOff); err != nil {
1713 return off, &nestedError{"TTL", err}
1715 length, newOff, err := unpackUint16(msg, newOff)
1717 return off, &nestedError{"Length", err}
1719 if newOff += int(length); newOff > len(msg) {
1720 return off, errResourceLen
1725 // packUint16 appends the wire format of field to msg.
1726 func packUint16(msg []byte, field uint16) []byte {
1727 return append(msg, byte(field>>8), byte(field))
1730 func unpackUint16(msg []byte, off int) (uint16, int, error) {
1731 if off+uint16Len > len(msg) {
1732 return 0, off, errBaseLen
1734 return uint16(msg[off])<<8 | uint16(msg[off+1]), off + uint16Len, nil
1737 func skipUint16(msg []byte, off int) (int, error) {
1738 if off+uint16Len > len(msg) {
1739 return off, errBaseLen
1741 return off + uint16Len, nil
1744 // packType appends the wire format of field to msg.
1745 func packType(msg []byte, field Type) []byte {
1746 return packUint16(msg, uint16(field))
1749 func unpackType(msg []byte, off int) (Type, int, error) {
1750 t, o, err := unpackUint16(msg, off)
1751 return Type(t), o, err
1754 func skipType(msg []byte, off int) (int, error) {
1755 return skipUint16(msg, off)
1758 // packClass appends the wire format of field to msg.
1759 func packClass(msg []byte, field Class) []byte {
1760 return packUint16(msg, uint16(field))
1763 func unpackClass(msg []byte, off int) (Class, int, error) {
1764 c, o, err := unpackUint16(msg, off)
1765 return Class(c), o, err
1768 func skipClass(msg []byte, off int) (int, error) {
1769 return skipUint16(msg, off)
1772 // packUint32 appends the wire format of field to msg.
1773 func packUint32(msg []byte, field uint32) []byte {
1783 func unpackUint32(msg []byte, off int) (uint32, int, error) {
1784 if off+uint32Len > len(msg) {
1785 return 0, off, errBaseLen
1787 v := uint32(msg[off])<<24 | uint32(msg[off+1])<<16 | uint32(msg[off+2])<<8 | uint32(msg[off+3])
1788 return v, off + uint32Len, nil
1791 func skipUint32(msg []byte, off int) (int, error) {
1792 if off+uint32Len > len(msg) {
1793 return off, errBaseLen
1795 return off + uint32Len, nil
1798 // packText appends the wire format of field to msg.
1799 func packText(msg []byte, field string) ([]byte, error) {
1802 return nil, errStringTooLong
1804 msg = append(msg, byte(l))
1805 msg = append(msg, field...)
1810 func unpackText(msg []byte, off int) (string, int, error) {
1811 if off >= len(msg) {
1812 return "", off, errBaseLen
1815 endOff := beginOff + int(msg[off])
1816 if endOff > len(msg) {
1817 return "", off, errCalcLen
1819 return string(msg[beginOff:endOff]), endOff, nil
1822 func skipText(msg []byte, off int) (int, error) {
1823 if off >= len(msg) {
1824 return off, errBaseLen
1826 endOff := off + 1 + int(msg[off])
1827 if endOff > len(msg) {
1828 return off, errCalcLen
1833 // packBytes appends the wire format of field to msg.
1834 func packBytes(msg []byte, field []byte) []byte {
1835 return append(msg, field...)
1838 func unpackBytes(msg []byte, off int, field []byte) (int, error) {
1839 newOff := off + len(field)
1840 if newOff > len(msg) {
1841 return off, errBaseLen
1843 copy(field, msg[off:newOff])
1847 func skipBytes(msg []byte, off int, field []byte) (int, error) {
1848 newOff := off + len(field)
1849 if newOff > len(msg) {
1850 return off, errBaseLen
1857 // A Name is a non-encoded domain name. It is used instead of strings to avoid
1864 // NewName creates a new Name from a string.
1865 func NewName(name string) (Name, error) {
1866 if len([]byte(name)) > nameLen {
1867 return Name{}, errCalcLen
1869 n := Name{Length: uint8(len(name))}
1870 copy(n.Data[:], []byte(name))
1874 // MustNewName creates a new Name from a string and panics on error.
1875 func MustNewName(name string) Name {
1876 n, err := NewName(name)
1878 panic("creating name: " + err.Error())
1883 // String implements fmt.Stringer.String.
1884 func (n Name) String() string {
1885 return string(n.Data[:n.Length])
1888 // GoString implements fmt.GoStringer.GoString.
1889 func (n *Name) GoString() string {
1890 return `dnsmessage.MustNewName("` + printString(n.Data[:n.Length]) + `")`
1893 // pack appends the wire format of the Name to msg.
1895 // Domain names are a sequence of counted strings split at the dots. They end
1896 // with a zero-length string. Compression can be used to reuse domain suffixes.
1898 // The compression map will be updated with new domain suffixes. If compression
1899 // is nil, compression will not be used.
1900 func (n *Name) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
1903 // Add a trailing dot to canonicalize name.
1904 if n.Length == 0 || n.Data[n.Length-1] != '.' {
1905 return oldMsg, errNonCanonicalName
1908 // Allow root domain.
1909 if n.Data[0] == '.' && n.Length == 1 {
1910 return append(msg, 0), nil
1913 // Emit sequence of counted strings, chopping at dots.
1914 for i, begin := 0, 0; i < int(n.Length); i++ {
1915 // Check for the end of the segment.
1916 if n.Data[i] == '.' {
1917 // The two most significant bits have special meaning.
1918 // It isn't allowed for segments to be long enough to
1920 if i-begin >= 1<<6 {
1921 return oldMsg, errSegTooLong
1924 // Segments must have a non-zero length.
1926 return oldMsg, errZeroSegLen
1929 msg = append(msg, byte(i-begin))
1931 for j := begin; j < i; j++ {
1932 msg = append(msg, n.Data[j])
1939 // We can only compress domain suffixes starting with a new
1940 // segment. A pointer is two bytes with the two most significant
1941 // bits set to 1 to indicate that it is a pointer.
1942 if (i == 0 || n.Data[i-1] == '.') && compression != nil {
1943 if ptr, ok := compression[string(n.Data[i:])]; ok {
1944 // Hit. Emit a pointer instead of the rest of
1946 return append(msg, byte(ptr>>8|0xC0), byte(ptr)), nil
1949 // Miss. Add the suffix to the compression table if the
1950 // offset can be stored in the available 14 bytes.
1951 if len(msg) <= int(^uint16(0)>>2) {
1952 compression[string(n.Data[i:])] = len(msg) - compressionOff
1956 return append(msg, 0), nil
1959 // unpack unpacks a domain name.
1960 func (n *Name) unpack(msg []byte, off int) (int, error) {
1961 return n.unpackCompressed(msg, off, true /* allowCompression */)
1964 func (n *Name) unpackCompressed(msg []byte, off int, allowCompression bool) (int, error) {
1965 // currOff is the current working offset.
1968 // newOff is the offset where the next record will start. Pointers lead
1969 // to data that belongs to other names and thus doesn't count towards to
1970 // the usage of this name.
1973 // ptr is the number of pointers followed.
1976 // Name is a slice representation of the name data.
1981 if currOff >= len(msg) {
1982 return off, errBaseLen
1984 c := int(msg[currOff])
1987 case 0x00: // String segment
1989 // A zero length signals the end of the name.
1992 endOff := currOff + c
1993 if endOff > len(msg) {
1994 return off, errCalcLen
1996 name = append(name, msg[currOff:endOff]...)
1997 name = append(name, '.')
1999 case 0xC0: // Pointer
2000 if !allowCompression {
2001 return off, errCompressedSRV
2003 if currOff >= len(msg) {
2004 return off, errInvalidPtr
2011 // Don't follow too many pointers, maybe there's a loop.
2012 if ptr++; ptr > 10 {
2013 return off, errTooManyPtr
2015 currOff = (c^0xC0)<<8 | int(c1)
2017 // Prefixes 0x80 and 0x40 are reserved.
2018 return off, errReserved
2022 name = append(name, '.')
2024 if len(name) > len(n.Data) {
2025 return off, errCalcLen
2027 n.Length = uint8(len(name))
2034 func skipName(msg []byte, off int) (int, error) {
2035 // newOff is the offset where the next record will start. Pointers lead
2036 // to data that belongs to other names and thus doesn't count towards to
2037 // the usage of this name.
2042 if newOff >= len(msg) {
2043 return off, errBaseLen
2045 c := int(msg[newOff])
2050 // A zero length signals the end of the name.
2055 if newOff > len(msg) {
2056 return off, errCalcLen
2059 // Pointer to somewhere else in msg.
2061 // Pointers are two bytes.
2064 // Don't follow the pointer as the data here has ended.
2067 // Prefixes 0x80 and 0x40 are reserved.
2068 return off, errReserved
2075 // A Question is a DNS query.
2076 type Question struct {
2082 // pack appends the wire format of the Question to msg.
2083 func (q *Question) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2084 msg, err := q.Name.pack(msg, compression, compressionOff)
2086 return msg, &nestedError{"Name", err}
2088 msg = packType(msg, q.Type)
2089 return packClass(msg, q.Class), nil
2092 // GoString implements fmt.GoStringer.GoString.
2093 func (q *Question) GoString() string {
2094 return "dnsmessage.Question{" +
2095 "Name: " + q.Name.GoString() + ", " +
2096 "Type: " + q.Type.GoString() + ", " +
2097 "Class: " + q.Class.GoString() + "}"
2100 func unpackResourceBody(msg []byte, off int, hdr ResourceHeader) (ResourceBody, int, error) {
2109 rb, err = unpackAResource(msg, off)
2114 rb, err = unpackNSResource(msg, off)
2118 var rb CNAMEResource
2119 rb, err = unpackCNAMEResource(msg, off)
2124 rb, err = unpackSOAResource(msg, off)
2129 rb, err = unpackPTRResource(msg, off)
2134 rb, err = unpackMXResource(msg, off)
2139 rb, err = unpackTXTResource(msg, off, hdr.Length)
2144 rb, err = unpackAAAAResource(msg, off)
2149 rb, err = unpackSRVResource(msg, off)
2154 rb, err = unpackOPTResource(msg, off, hdr.Length)
2159 return nil, off, &nestedError{name + " record", err}
2162 return nil, off, errors.New("invalid resource type: " + string(hdr.Type+'0'))
2164 return r, off + int(hdr.Length), nil
2167 // A CNAMEResource is a CNAME Resource record.
2168 type CNAMEResource struct {
2172 func (r *CNAMEResource) realType() Type {
2176 // pack appends the wire format of the CNAMEResource to msg.
2177 func (r *CNAMEResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2178 return r.CNAME.pack(msg, compression, compressionOff)
2181 // GoString implements fmt.GoStringer.GoString.
2182 func (r *CNAMEResource) GoString() string {
2183 return "dnsmessage.CNAMEResource{CNAME: " + r.CNAME.GoString() + "}"
2186 func unpackCNAMEResource(msg []byte, off int) (CNAMEResource, error) {
2188 if _, err := cname.unpack(msg, off); err != nil {
2189 return CNAMEResource{}, err
2191 return CNAMEResource{cname}, nil
2194 // An MXResource is an MX Resource record.
2195 type MXResource struct {
2200 func (r *MXResource) realType() Type {
2204 // pack appends the wire format of the MXResource to msg.
2205 func (r *MXResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2207 msg = packUint16(msg, r.Pref)
2208 msg, err := r.MX.pack(msg, compression, compressionOff)
2210 return oldMsg, &nestedError{"MXResource.MX", err}
2215 // GoString implements fmt.GoStringer.GoString.
2216 func (r *MXResource) GoString() string {
2217 return "dnsmessage.MXResource{" +
2218 "Pref: " + printUint16(r.Pref) + ", " +
2219 "MX: " + r.MX.GoString() + "}"
2222 func unpackMXResource(msg []byte, off int) (MXResource, error) {
2223 pref, off, err := unpackUint16(msg, off)
2225 return MXResource{}, &nestedError{"Pref", err}
2228 if _, err := mx.unpack(msg, off); err != nil {
2229 return MXResource{}, &nestedError{"MX", err}
2231 return MXResource{pref, mx}, nil
2234 // An NSResource is an NS Resource record.
2235 type NSResource struct {
2239 func (r *NSResource) realType() Type {
2243 // pack appends the wire format of the NSResource to msg.
2244 func (r *NSResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2245 return r.NS.pack(msg, compression, compressionOff)
2248 // GoString implements fmt.GoStringer.GoString.
2249 func (r *NSResource) GoString() string {
2250 return "dnsmessage.NSResource{NS: " + r.NS.GoString() + "}"
2253 func unpackNSResource(msg []byte, off int) (NSResource, error) {
2255 if _, err := ns.unpack(msg, off); err != nil {
2256 return NSResource{}, err
2258 return NSResource{ns}, nil
2261 // A PTRResource is a PTR Resource record.
2262 type PTRResource struct {
2266 func (r *PTRResource) realType() Type {
2270 // pack appends the wire format of the PTRResource to msg.
2271 func (r *PTRResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2272 return r.PTR.pack(msg, compression, compressionOff)
2275 // GoString implements fmt.GoStringer.GoString.
2276 func (r *PTRResource) GoString() string {
2277 return "dnsmessage.PTRResource{PTR: " + r.PTR.GoString() + "}"
2280 func unpackPTRResource(msg []byte, off int) (PTRResource, error) {
2282 if _, err := ptr.unpack(msg, off); err != nil {
2283 return PTRResource{}, err
2285 return PTRResource{ptr}, nil
2288 // An SOAResource is an SOA Resource record.
2289 type SOAResource struct {
2297 // MinTTL the is the default TTL of Resources records which did not
2298 // contain a TTL value and the TTL of negative responses. (RFC 2308
2303 func (r *SOAResource) realType() Type {
2307 // pack appends the wire format of the SOAResource to msg.
2308 func (r *SOAResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2310 msg, err := r.NS.pack(msg, compression, compressionOff)
2312 return oldMsg, &nestedError{"SOAResource.NS", err}
2314 msg, err = r.MBox.pack(msg, compression, compressionOff)
2316 return oldMsg, &nestedError{"SOAResource.MBox", err}
2318 msg = packUint32(msg, r.Serial)
2319 msg = packUint32(msg, r.Refresh)
2320 msg = packUint32(msg, r.Retry)
2321 msg = packUint32(msg, r.Expire)
2322 return packUint32(msg, r.MinTTL), nil
2325 // GoString implements fmt.GoStringer.GoString.
2326 func (r *SOAResource) GoString() string {
2327 return "dnsmessage.SOAResource{" +
2328 "NS: " + r.NS.GoString() + ", " +
2329 "MBox: " + r.MBox.GoString() + ", " +
2330 "Serial: " + printUint32(r.Serial) + ", " +
2331 "Refresh: " + printUint32(r.Refresh) + ", " +
2332 "Retry: " + printUint32(r.Retry) + ", " +
2333 "Expire: " + printUint32(r.Expire) + ", " +
2334 "MinTTL: " + printUint32(r.MinTTL) + "}"
2337 func unpackSOAResource(msg []byte, off int) (SOAResource, error) {
2339 off, err := ns.unpack(msg, off)
2341 return SOAResource{}, &nestedError{"NS", err}
2344 if off, err = mbox.unpack(msg, off); err != nil {
2345 return SOAResource{}, &nestedError{"MBox", err}
2347 serial, off, err := unpackUint32(msg, off)
2349 return SOAResource{}, &nestedError{"Serial", err}
2351 refresh, off, err := unpackUint32(msg, off)
2353 return SOAResource{}, &nestedError{"Refresh", err}
2355 retry, off, err := unpackUint32(msg, off)
2357 return SOAResource{}, &nestedError{"Retry", err}
2359 expire, off, err := unpackUint32(msg, off)
2361 return SOAResource{}, &nestedError{"Expire", err}
2363 minTTL, _, err := unpackUint32(msg, off)
2365 return SOAResource{}, &nestedError{"MinTTL", err}
2367 return SOAResource{ns, mbox, serial, refresh, retry, expire, minTTL}, nil
2370 // A TXTResource is a TXT Resource record.
2371 type TXTResource struct {
2375 func (r *TXTResource) realType() Type {
2379 // pack appends the wire format of the TXTResource to msg.
2380 func (r *TXTResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2382 for _, s := range r.TXT {
2384 msg, err = packText(msg, s)
2392 // GoString implements fmt.GoStringer.GoString.
2393 func (r *TXTResource) GoString() string {
2394 s := "dnsmessage.TXTResource{TXT: []string{"
2395 if len(r.TXT) == 0 {
2398 s += `"` + printString([]byte(r.TXT[0]))
2399 for _, t := range r.TXT[1:] {
2400 s += `", "` + printString([]byte(t))
2405 func unpackTXTResource(msg []byte, off int, length uint16) (TXTResource, error) {
2406 txts := make([]string, 0, 1)
2407 for n := uint16(0); n < length; {
2410 if t, off, err = unpackText(msg, off); err != nil {
2411 return TXTResource{}, &nestedError{"text", err}
2413 // Check if we got too many bytes.
2414 if length-n < uint16(len(t))+1 {
2415 return TXTResource{}, errCalcLen
2417 n += uint16(len(t)) + 1
2418 txts = append(txts, t)
2420 return TXTResource{txts}, nil
2423 // An SRVResource is an SRV Resource record.
2424 type SRVResource struct {
2428 Target Name // Not compressed as per RFC 2782.
2431 func (r *SRVResource) realType() Type {
2435 // pack appends the wire format of the SRVResource to msg.
2436 func (r *SRVResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2438 msg = packUint16(msg, r.Priority)
2439 msg = packUint16(msg, r.Weight)
2440 msg = packUint16(msg, r.Port)
2441 msg, err := r.Target.pack(msg, nil, compressionOff)
2443 return oldMsg, &nestedError{"SRVResource.Target", err}
2448 // GoString implements fmt.GoStringer.GoString.
2449 func (r *SRVResource) GoString() string {
2450 return "dnsmessage.SRVResource{" +
2451 "Priority: " + printUint16(r.Priority) + ", " +
2452 "Weight: " + printUint16(r.Weight) + ", " +
2453 "Port: " + printUint16(r.Port) + ", " +
2454 "Target: " + r.Target.GoString() + "}"
2457 func unpackSRVResource(msg []byte, off int) (SRVResource, error) {
2458 priority, off, err := unpackUint16(msg, off)
2460 return SRVResource{}, &nestedError{"Priority", err}
2462 weight, off, err := unpackUint16(msg, off)
2464 return SRVResource{}, &nestedError{"Weight", err}
2466 port, off, err := unpackUint16(msg, off)
2468 return SRVResource{}, &nestedError{"Port", err}
2471 if _, err := target.unpackCompressed(msg, off, false /* allowCompression */); err != nil {
2472 return SRVResource{}, &nestedError{"Target", err}
2474 return SRVResource{priority, weight, port, target}, nil
2477 // An AResource is an A Resource record.
2478 type AResource struct {
2482 func (r *AResource) realType() Type {
2486 // pack appends the wire format of the AResource to msg.
2487 func (r *AResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2488 return packBytes(msg, r.A[:]), nil
2491 // GoString implements fmt.GoStringer.GoString.
2492 func (r *AResource) GoString() string {
2493 return "dnsmessage.AResource{" +
2494 "A: [4]byte{" + printByteSlice(r.A[:]) + "}}"
2497 func unpackAResource(msg []byte, off int) (AResource, error) {
2499 if _, err := unpackBytes(msg, off, a[:]); err != nil {
2500 return AResource{}, err
2502 return AResource{a}, nil
2505 // An AAAAResource is an AAAA Resource record.
2506 type AAAAResource struct {
2510 func (r *AAAAResource) realType() Type {
2514 // GoString implements fmt.GoStringer.GoString.
2515 func (r *AAAAResource) GoString() string {
2516 return "dnsmessage.AAAAResource{" +
2517 "AAAA: [16]byte{" + printByteSlice(r.AAAA[:]) + "}}"
2520 // pack appends the wire format of the AAAAResource to msg.
2521 func (r *AAAAResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2522 return packBytes(msg, r.AAAA[:]), nil
2525 func unpackAAAAResource(msg []byte, off int) (AAAAResource, error) {
2527 if _, err := unpackBytes(msg, off, aaaa[:]); err != nil {
2528 return AAAAResource{}, err
2530 return AAAAResource{aaaa}, nil
2533 // An OPTResource is an OPT pseudo Resource record.
2535 // The pseudo resource record is part of the extension mechanisms for DNS
2536 // as defined in RFC 6891.
2537 type OPTResource struct {
2541 // An Option represents a DNS message option within OPTResource.
2543 // The message option is part of the extension mechanisms for DNS as
2544 // defined in RFC 6891.
2545 type Option struct {
2546 Code uint16 // option code
2550 // GoString implements fmt.GoStringer.GoString.
2551 func (o *Option) GoString() string {
2552 return "dnsmessage.Option{" +
2553 "Code: " + printUint16(o.Code) + ", " +
2554 "Data: []byte{" + printByteSlice(o.Data) + "}}"
2557 func (r *OPTResource) realType() Type {
2561 func (r *OPTResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2562 for _, opt := range r.Options {
2563 msg = packUint16(msg, opt.Code)
2564 l := uint16(len(opt.Data))
2565 msg = packUint16(msg, l)
2566 msg = packBytes(msg, opt.Data)
2571 // GoString implements fmt.GoStringer.GoString.
2572 func (r *OPTResource) GoString() string {
2573 s := "dnsmessage.OPTResource{Options: []dnsmessage.Option{"
2574 if len(r.Options) == 0 {
2577 s += r.Options[0].GoString()
2578 for _, o := range r.Options[1:] {
2579 s += ", " + o.GoString()
2584 func unpackOPTResource(msg []byte, off int, length uint16) (OPTResource, error) {
2586 for oldOff := off; off < oldOff+int(length); {
2589 o.Code, off, err = unpackUint16(msg, off)
2591 return OPTResource{}, &nestedError{"Code", err}
2594 l, off, err = unpackUint16(msg, off)
2596 return OPTResource{}, &nestedError{"Data", err}
2598 o.Data = make([]byte, l)
2599 if copy(o.Data, msg[off:]) != int(l) {
2600 return OPTResource{}, &nestedError{"Data", errCalcLen}
2603 opts = append(opts, o)
2605 return OPTResource{opts}, nil