message.go 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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.
  4. // Package dnsmessage provides a mostly RFC 1035 compliant implementation of
  5. // DNS message packing and unpacking.
  6. //
  7. // This implementation is designed to minimize heap allocations and avoid
  8. // unnecessary packing and unpacking as much as possible.
  9. package dnsmessage
  10. import (
  11. "errors"
  12. )
  13. // Message formats
  14. // A Type is a type of DNS request and response.
  15. type Type uint16
  16. // A Class is a type of network.
  17. type Class uint16
  18. // An OpCode is a DNS operation code.
  19. type OpCode uint16
  20. // An RCode is a DNS response status code.
  21. type RCode uint16
  22. // Wire constants.
  23. const (
  24. // ResourceHeader.Type and Question.Type
  25. TypeA Type = 1
  26. TypeNS Type = 2
  27. TypeCNAME Type = 5
  28. TypeSOA Type = 6
  29. TypePTR Type = 12
  30. TypeMX Type = 15
  31. TypeTXT Type = 16
  32. TypeAAAA Type = 28
  33. TypeSRV Type = 33
  34. // Question.Type
  35. TypeWKS Type = 11
  36. TypeHINFO Type = 13
  37. TypeMINFO Type = 14
  38. TypeAXFR Type = 252
  39. TypeALL Type = 255
  40. // ResourceHeader.Class and Question.Class
  41. ClassINET Class = 1
  42. ClassCSNET Class = 2
  43. ClassCHAOS Class = 3
  44. ClassHESIOD Class = 4
  45. // Question.Class
  46. ClassANY Class = 255
  47. // Message.Rcode
  48. RCodeSuccess RCode = 0
  49. RCodeFormatError RCode = 1
  50. RCodeServerFailure RCode = 2
  51. RCodeNameError RCode = 3
  52. RCodeNotImplemented RCode = 4
  53. RCodeRefused RCode = 5
  54. )
  55. var (
  56. // ErrNotStarted indicates that the prerequisite information isn't
  57. // available yet because the previous records haven't been appropriately
  58. // parsed, skipped or finished.
  59. ErrNotStarted = errors.New("parsing/packing of this type isn't available yet")
  60. // ErrSectionDone indicated that all records in the section have been
  61. // parsed or finished.
  62. ErrSectionDone = errors.New("parsing/packing of this section has completed")
  63. errBaseLen = errors.New("insufficient data for base length type")
  64. errCalcLen = errors.New("insufficient data for calculated length type")
  65. errReserved = errors.New("segment prefix is reserved")
  66. errTooManyPtr = errors.New("too many pointers (>10)")
  67. errInvalidPtr = errors.New("invalid pointer")
  68. errNilResouceBody = errors.New("nil resource body")
  69. errResourceLen = errors.New("insufficient data for resource body length")
  70. errSegTooLong = errors.New("segment length too long")
  71. errZeroSegLen = errors.New("zero length segment")
  72. errResTooLong = errors.New("resource length too long")
  73. errTooManyQuestions = errors.New("too many Questions to pack (>65535)")
  74. errTooManyAnswers = errors.New("too many Answers to pack (>65535)")
  75. errTooManyAuthorities = errors.New("too many Authorities to pack (>65535)")
  76. errTooManyAdditionals = errors.New("too many Additionals to pack (>65535)")
  77. errNonCanonicalName = errors.New("name is not in canonical format (it must end with a .)")
  78. )
  79. // Internal constants.
  80. const (
  81. // packStartingCap is the default initial buffer size allocated during
  82. // packing.
  83. //
  84. // The starting capacity doesn't matter too much, but most DNS responses
  85. // Will be <= 512 bytes as it is the limit for DNS over UDP.
  86. packStartingCap = 512
  87. // uint16Len is the length (in bytes) of a uint16.
  88. uint16Len = 2
  89. // uint32Len is the length (in bytes) of a uint32.
  90. uint32Len = 4
  91. // headerLen is the length (in bytes) of a DNS header.
  92. //
  93. // A header is comprised of 6 uint16s and no padding.
  94. headerLen = 6 * uint16Len
  95. )
  96. type nestedError struct {
  97. // s is the current level's error message.
  98. s string
  99. // err is the nested error.
  100. err error
  101. }
  102. // nestedError implements error.Error.
  103. func (e *nestedError) Error() string {
  104. return e.s + ": " + e.err.Error()
  105. }
  106. // Header is a representation of a DNS message header.
  107. type Header struct {
  108. ID uint16
  109. Response bool
  110. OpCode OpCode
  111. Authoritative bool
  112. Truncated bool
  113. RecursionDesired bool
  114. RecursionAvailable bool
  115. RCode RCode
  116. }
  117. func (m *Header) pack() (id uint16, bits uint16) {
  118. id = m.ID
  119. bits = uint16(m.OpCode)<<11 | uint16(m.RCode)
  120. if m.RecursionAvailable {
  121. bits |= headerBitRA
  122. }
  123. if m.RecursionDesired {
  124. bits |= headerBitRD
  125. }
  126. if m.Truncated {
  127. bits |= headerBitTC
  128. }
  129. if m.Authoritative {
  130. bits |= headerBitAA
  131. }
  132. if m.Response {
  133. bits |= headerBitQR
  134. }
  135. return
  136. }
  137. // Message is a representation of a DNS message.
  138. type Message struct {
  139. Header
  140. Questions []Question
  141. Answers []Resource
  142. Authorities []Resource
  143. Additionals []Resource
  144. }
  145. type section uint8
  146. const (
  147. sectionNotStarted section = iota
  148. sectionHeader
  149. sectionQuestions
  150. sectionAnswers
  151. sectionAuthorities
  152. sectionAdditionals
  153. sectionDone
  154. headerBitQR = 1 << 15 // query/response (response=1)
  155. headerBitAA = 1 << 10 // authoritative
  156. headerBitTC = 1 << 9 // truncated
  157. headerBitRD = 1 << 8 // recursion desired
  158. headerBitRA = 1 << 7 // recursion available
  159. )
  160. var sectionNames = map[section]string{
  161. sectionHeader: "header",
  162. sectionQuestions: "Question",
  163. sectionAnswers: "Answer",
  164. sectionAuthorities: "Authority",
  165. sectionAdditionals: "Additional",
  166. }
  167. // header is the wire format for a DNS message header.
  168. type header struct {
  169. id uint16
  170. bits uint16
  171. questions uint16
  172. answers uint16
  173. authorities uint16
  174. additionals uint16
  175. }
  176. func (h *header) count(sec section) uint16 {
  177. switch sec {
  178. case sectionQuestions:
  179. return h.questions
  180. case sectionAnswers:
  181. return h.answers
  182. case sectionAuthorities:
  183. return h.authorities
  184. case sectionAdditionals:
  185. return h.additionals
  186. }
  187. return 0
  188. }
  189. func (h *header) pack(msg []byte) []byte {
  190. msg = packUint16(msg, h.id)
  191. msg = packUint16(msg, h.bits)
  192. msg = packUint16(msg, h.questions)
  193. msg = packUint16(msg, h.answers)
  194. msg = packUint16(msg, h.authorities)
  195. return packUint16(msg, h.additionals)
  196. }
  197. func (h *header) unpack(msg []byte, off int) (int, error) {
  198. newOff := off
  199. var err error
  200. if h.id, newOff, err = unpackUint16(msg, newOff); err != nil {
  201. return off, &nestedError{"id", err}
  202. }
  203. if h.bits, newOff, err = unpackUint16(msg, newOff); err != nil {
  204. return off, &nestedError{"bits", err}
  205. }
  206. if h.questions, newOff, err = unpackUint16(msg, newOff); err != nil {
  207. return off, &nestedError{"questions", err}
  208. }
  209. if h.answers, newOff, err = unpackUint16(msg, newOff); err != nil {
  210. return off, &nestedError{"answers", err}
  211. }
  212. if h.authorities, newOff, err = unpackUint16(msg, newOff); err != nil {
  213. return off, &nestedError{"authorities", err}
  214. }
  215. if h.additionals, newOff, err = unpackUint16(msg, newOff); err != nil {
  216. return off, &nestedError{"additionals", err}
  217. }
  218. return newOff, nil
  219. }
  220. func (h *header) header() Header {
  221. return Header{
  222. ID: h.id,
  223. Response: (h.bits & headerBitQR) != 0,
  224. OpCode: OpCode(h.bits>>11) & 0xF,
  225. Authoritative: (h.bits & headerBitAA) != 0,
  226. Truncated: (h.bits & headerBitTC) != 0,
  227. RecursionDesired: (h.bits & headerBitRD) != 0,
  228. RecursionAvailable: (h.bits & headerBitRA) != 0,
  229. RCode: RCode(h.bits & 0xF),
  230. }
  231. }
  232. // A Resource is a DNS resource record.
  233. type Resource struct {
  234. Header ResourceHeader
  235. Body ResourceBody
  236. }
  237. // A ResourceBody is a DNS resource record minus the header.
  238. type ResourceBody interface {
  239. // pack packs a Resource except for its header.
  240. pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error)
  241. // realType returns the actual type of the Resource. This is used to
  242. // fill in the header Type field.
  243. realType() Type
  244. }
  245. func (r *Resource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  246. if r.Body == nil {
  247. return msg, errNilResouceBody
  248. }
  249. oldMsg := msg
  250. r.Header.Type = r.Body.realType()
  251. msg, length, err := r.Header.pack(msg, compression, compressionOff)
  252. if err != nil {
  253. return msg, &nestedError{"ResourceHeader", err}
  254. }
  255. preLen := len(msg)
  256. msg, err = r.Body.pack(msg, compression, compressionOff)
  257. if err != nil {
  258. return msg, &nestedError{"content", err}
  259. }
  260. if err := r.Header.fixLen(msg, length, preLen); err != nil {
  261. return oldMsg, err
  262. }
  263. return msg, nil
  264. }
  265. // A Parser allows incrementally parsing a DNS message.
  266. //
  267. // When parsing is started, the Header is parsed. Next, each Question can be
  268. // either parsed or skipped. Alternatively, all Questions can be skipped at
  269. // once. When all Questions have been parsed, attempting to parse Questions
  270. // will return (nil, nil) and attempting to skip Questions will return
  271. // (true, nil). After all Questions have been either parsed or skipped, all
  272. // Answers, Authorities and Additionals can be either parsed or skipped in the
  273. // same way, and each type of Resource must be fully parsed or skipped before
  274. // proceeding to the next type of Resource.
  275. //
  276. // Note that there is no requirement to fully skip or parse the message.
  277. type Parser struct {
  278. msg []byte
  279. header header
  280. section section
  281. off int
  282. index int
  283. resHeaderValid bool
  284. resHeader ResourceHeader
  285. }
  286. // Start parses the header and enables the parsing of Questions.
  287. func (p *Parser) Start(msg []byte) (Header, error) {
  288. if p.msg != nil {
  289. *p = Parser{}
  290. }
  291. p.msg = msg
  292. var err error
  293. if p.off, err = p.header.unpack(msg, 0); err != nil {
  294. return Header{}, &nestedError{"unpacking header", err}
  295. }
  296. p.section = sectionQuestions
  297. return p.header.header(), nil
  298. }
  299. func (p *Parser) checkAdvance(sec section) error {
  300. if p.section < sec {
  301. return ErrNotStarted
  302. }
  303. if p.section > sec {
  304. return ErrSectionDone
  305. }
  306. p.resHeaderValid = false
  307. if p.index == int(p.header.count(sec)) {
  308. p.index = 0
  309. p.section++
  310. return ErrSectionDone
  311. }
  312. return nil
  313. }
  314. func (p *Parser) resource(sec section) (Resource, error) {
  315. var r Resource
  316. var err error
  317. r.Header, err = p.resourceHeader(sec)
  318. if err != nil {
  319. return r, err
  320. }
  321. p.resHeaderValid = false
  322. r.Body, p.off, err = unpackResourceBody(p.msg, p.off, r.Header)
  323. if err != nil {
  324. return Resource{}, &nestedError{"unpacking " + sectionNames[sec], err}
  325. }
  326. p.index++
  327. return r, nil
  328. }
  329. func (p *Parser) resourceHeader(sec section) (ResourceHeader, error) {
  330. if p.resHeaderValid {
  331. return p.resHeader, nil
  332. }
  333. if err := p.checkAdvance(sec); err != nil {
  334. return ResourceHeader{}, err
  335. }
  336. var hdr ResourceHeader
  337. off, err := hdr.unpack(p.msg, p.off)
  338. if err != nil {
  339. return ResourceHeader{}, err
  340. }
  341. p.resHeaderValid = true
  342. p.resHeader = hdr
  343. p.off = off
  344. return hdr, nil
  345. }
  346. func (p *Parser) skipResource(sec section) error {
  347. if p.resHeaderValid {
  348. newOff := p.off + int(p.resHeader.Length)
  349. if newOff > len(p.msg) {
  350. return errResourceLen
  351. }
  352. p.off = newOff
  353. p.resHeaderValid = false
  354. p.index++
  355. return nil
  356. }
  357. if err := p.checkAdvance(sec); err != nil {
  358. return err
  359. }
  360. var err error
  361. p.off, err = skipResource(p.msg, p.off)
  362. if err != nil {
  363. return &nestedError{"skipping: " + sectionNames[sec], err}
  364. }
  365. p.index++
  366. return nil
  367. }
  368. // Question parses a single Question.
  369. func (p *Parser) Question() (Question, error) {
  370. if err := p.checkAdvance(sectionQuestions); err != nil {
  371. return Question{}, err
  372. }
  373. var name Name
  374. off, err := name.unpack(p.msg, p.off)
  375. if err != nil {
  376. return Question{}, &nestedError{"unpacking Question.Name", err}
  377. }
  378. typ, off, err := unpackType(p.msg, off)
  379. if err != nil {
  380. return Question{}, &nestedError{"unpacking Question.Type", err}
  381. }
  382. class, off, err := unpackClass(p.msg, off)
  383. if err != nil {
  384. return Question{}, &nestedError{"unpacking Question.Class", err}
  385. }
  386. p.off = off
  387. p.index++
  388. return Question{name, typ, class}, nil
  389. }
  390. // AllQuestions parses all Questions.
  391. func (p *Parser) AllQuestions() ([]Question, error) {
  392. // Multiple questions are valid according to the spec,
  393. // but servers don't actually support them. There will
  394. // be at most one question here.
  395. //
  396. // Do not pre-allocate based on info in p.header, since
  397. // the data is untrusted.
  398. qs := []Question{}
  399. for {
  400. q, err := p.Question()
  401. if err == ErrSectionDone {
  402. return qs, nil
  403. }
  404. if err != nil {
  405. return nil, err
  406. }
  407. qs = append(qs, q)
  408. }
  409. }
  410. // SkipQuestion skips a single Question.
  411. func (p *Parser) SkipQuestion() error {
  412. if err := p.checkAdvance(sectionQuestions); err != nil {
  413. return err
  414. }
  415. off, err := skipName(p.msg, p.off)
  416. if err != nil {
  417. return &nestedError{"skipping Question Name", err}
  418. }
  419. if off, err = skipType(p.msg, off); err != nil {
  420. return &nestedError{"skipping Question Type", err}
  421. }
  422. if off, err = skipClass(p.msg, off); err != nil {
  423. return &nestedError{"skipping Question Class", err}
  424. }
  425. p.off = off
  426. p.index++
  427. return nil
  428. }
  429. // SkipAllQuestions skips all Questions.
  430. func (p *Parser) SkipAllQuestions() error {
  431. for {
  432. if err := p.SkipQuestion(); err == ErrSectionDone {
  433. return nil
  434. } else if err != nil {
  435. return err
  436. }
  437. }
  438. }
  439. // AnswerHeader parses a single Answer ResourceHeader.
  440. func (p *Parser) AnswerHeader() (ResourceHeader, error) {
  441. return p.resourceHeader(sectionAnswers)
  442. }
  443. // Answer parses a single Answer Resource.
  444. func (p *Parser) Answer() (Resource, error) {
  445. return p.resource(sectionAnswers)
  446. }
  447. // AllAnswers parses all Answer Resources.
  448. func (p *Parser) AllAnswers() ([]Resource, error) {
  449. // The most common query is for A/AAAA, which usually returns
  450. // a handful of IPs.
  451. //
  452. // Pre-allocate up to a certain limit, since p.header is
  453. // untrusted data.
  454. n := int(p.header.answers)
  455. if n > 20 {
  456. n = 20
  457. }
  458. as := make([]Resource, 0, n)
  459. for {
  460. a, err := p.Answer()
  461. if err == ErrSectionDone {
  462. return as, nil
  463. }
  464. if err != nil {
  465. return nil, err
  466. }
  467. as = append(as, a)
  468. }
  469. }
  470. // SkipAnswer skips a single Answer Resource.
  471. func (p *Parser) SkipAnswer() error {
  472. return p.skipResource(sectionAnswers)
  473. }
  474. // SkipAllAnswers skips all Answer Resources.
  475. func (p *Parser) SkipAllAnswers() error {
  476. for {
  477. if err := p.SkipAnswer(); err == ErrSectionDone {
  478. return nil
  479. } else if err != nil {
  480. return err
  481. }
  482. }
  483. }
  484. // AuthorityHeader parses a single Authority ResourceHeader.
  485. func (p *Parser) AuthorityHeader() (ResourceHeader, error) {
  486. return p.resourceHeader(sectionAuthorities)
  487. }
  488. // Authority parses a single Authority Resource.
  489. func (p *Parser) Authority() (Resource, error) {
  490. return p.resource(sectionAuthorities)
  491. }
  492. // AllAuthorities parses all Authority Resources.
  493. func (p *Parser) AllAuthorities() ([]Resource, error) {
  494. // Authorities contains SOA in case of NXDOMAIN and friends,
  495. // otherwise it is empty.
  496. //
  497. // Pre-allocate up to a certain limit, since p.header is
  498. // untrusted data.
  499. n := int(p.header.authorities)
  500. if n > 10 {
  501. n = 10
  502. }
  503. as := make([]Resource, 0, n)
  504. for {
  505. a, err := p.Authority()
  506. if err == ErrSectionDone {
  507. return as, nil
  508. }
  509. if err != nil {
  510. return nil, err
  511. }
  512. as = append(as, a)
  513. }
  514. }
  515. // SkipAuthority skips a single Authority Resource.
  516. func (p *Parser) SkipAuthority() error {
  517. return p.skipResource(sectionAuthorities)
  518. }
  519. // SkipAllAuthorities skips all Authority Resources.
  520. func (p *Parser) SkipAllAuthorities() error {
  521. for {
  522. if err := p.SkipAuthority(); err == ErrSectionDone {
  523. return nil
  524. } else if err != nil {
  525. return err
  526. }
  527. }
  528. }
  529. // AdditionalHeader parses a single Additional ResourceHeader.
  530. func (p *Parser) AdditionalHeader() (ResourceHeader, error) {
  531. return p.resourceHeader(sectionAdditionals)
  532. }
  533. // Additional parses a single Additional Resource.
  534. func (p *Parser) Additional() (Resource, error) {
  535. return p.resource(sectionAdditionals)
  536. }
  537. // AllAdditionals parses all Additional Resources.
  538. func (p *Parser) AllAdditionals() ([]Resource, error) {
  539. // Additionals usually contain OPT, and sometimes A/AAAA
  540. // glue records.
  541. //
  542. // Pre-allocate up to a certain limit, since p.header is
  543. // untrusted data.
  544. n := int(p.header.additionals)
  545. if n > 10 {
  546. n = 10
  547. }
  548. as := make([]Resource, 0, n)
  549. for {
  550. a, err := p.Additional()
  551. if err == ErrSectionDone {
  552. return as, nil
  553. }
  554. if err != nil {
  555. return nil, err
  556. }
  557. as = append(as, a)
  558. }
  559. }
  560. // SkipAdditional skips a single Additional Resource.
  561. func (p *Parser) SkipAdditional() error {
  562. return p.skipResource(sectionAdditionals)
  563. }
  564. // SkipAllAdditionals skips all Additional Resources.
  565. func (p *Parser) SkipAllAdditionals() error {
  566. for {
  567. if err := p.SkipAdditional(); err == ErrSectionDone {
  568. return nil
  569. } else if err != nil {
  570. return err
  571. }
  572. }
  573. }
  574. // CNAMEResource parses a single CNAMEResource.
  575. //
  576. // One of the XXXHeader methods must have been called before calling this
  577. // method.
  578. func (p *Parser) CNAMEResource() (CNAMEResource, error) {
  579. if !p.resHeaderValid || p.resHeader.Type != TypeCNAME {
  580. return CNAMEResource{}, ErrNotStarted
  581. }
  582. r, err := unpackCNAMEResource(p.msg, p.off)
  583. if err != nil {
  584. return CNAMEResource{}, err
  585. }
  586. p.off += int(p.resHeader.Length)
  587. p.resHeaderValid = false
  588. p.index++
  589. return r, nil
  590. }
  591. // MXResource parses a single MXResource.
  592. //
  593. // One of the XXXHeader methods must have been called before calling this
  594. // method.
  595. func (p *Parser) MXResource() (MXResource, error) {
  596. if !p.resHeaderValid || p.resHeader.Type != TypeMX {
  597. return MXResource{}, ErrNotStarted
  598. }
  599. r, err := unpackMXResource(p.msg, p.off)
  600. if err != nil {
  601. return MXResource{}, err
  602. }
  603. p.off += int(p.resHeader.Length)
  604. p.resHeaderValid = false
  605. p.index++
  606. return r, nil
  607. }
  608. // NSResource parses a single NSResource.
  609. //
  610. // One of the XXXHeader methods must have been called before calling this
  611. // method.
  612. func (p *Parser) NSResource() (NSResource, error) {
  613. if !p.resHeaderValid || p.resHeader.Type != TypeNS {
  614. return NSResource{}, ErrNotStarted
  615. }
  616. r, err := unpackNSResource(p.msg, p.off)
  617. if err != nil {
  618. return NSResource{}, err
  619. }
  620. p.off += int(p.resHeader.Length)
  621. p.resHeaderValid = false
  622. p.index++
  623. return r, nil
  624. }
  625. // PTRResource parses a single PTRResource.
  626. //
  627. // One of the XXXHeader methods must have been called before calling this
  628. // method.
  629. func (p *Parser) PTRResource() (PTRResource, error) {
  630. if !p.resHeaderValid || p.resHeader.Type != TypePTR {
  631. return PTRResource{}, ErrNotStarted
  632. }
  633. r, err := unpackPTRResource(p.msg, p.off)
  634. if err != nil {
  635. return PTRResource{}, err
  636. }
  637. p.off += int(p.resHeader.Length)
  638. p.resHeaderValid = false
  639. p.index++
  640. return r, nil
  641. }
  642. // SOAResource parses a single SOAResource.
  643. //
  644. // One of the XXXHeader methods must have been called before calling this
  645. // method.
  646. func (p *Parser) SOAResource() (SOAResource, error) {
  647. if !p.resHeaderValid || p.resHeader.Type != TypeSOA {
  648. return SOAResource{}, ErrNotStarted
  649. }
  650. r, err := unpackSOAResource(p.msg, p.off)
  651. if err != nil {
  652. return SOAResource{}, err
  653. }
  654. p.off += int(p.resHeader.Length)
  655. p.resHeaderValid = false
  656. p.index++
  657. return r, nil
  658. }
  659. // TXTResource parses a single TXTResource.
  660. //
  661. // One of the XXXHeader methods must have been called before calling this
  662. // method.
  663. func (p *Parser) TXTResource() (TXTResource, error) {
  664. if !p.resHeaderValid || p.resHeader.Type != TypeTXT {
  665. return TXTResource{}, ErrNotStarted
  666. }
  667. r, err := unpackTXTResource(p.msg, p.off, p.resHeader.Length)
  668. if err != nil {
  669. return TXTResource{}, err
  670. }
  671. p.off += int(p.resHeader.Length)
  672. p.resHeaderValid = false
  673. p.index++
  674. return r, nil
  675. }
  676. // SRVResource parses a single SRVResource.
  677. //
  678. // One of the XXXHeader methods must have been called before calling this
  679. // method.
  680. func (p *Parser) SRVResource() (SRVResource, error) {
  681. if !p.resHeaderValid || p.resHeader.Type != TypeSRV {
  682. return SRVResource{}, ErrNotStarted
  683. }
  684. r, err := unpackSRVResource(p.msg, p.off)
  685. if err != nil {
  686. return SRVResource{}, err
  687. }
  688. p.off += int(p.resHeader.Length)
  689. p.resHeaderValid = false
  690. p.index++
  691. return r, nil
  692. }
  693. // AResource parses a single AResource.
  694. //
  695. // One of the XXXHeader methods must have been called before calling this
  696. // method.
  697. func (p *Parser) AResource() (AResource, error) {
  698. if !p.resHeaderValid || p.resHeader.Type != TypeA {
  699. return AResource{}, ErrNotStarted
  700. }
  701. r, err := unpackAResource(p.msg, p.off)
  702. if err != nil {
  703. return AResource{}, err
  704. }
  705. p.off += int(p.resHeader.Length)
  706. p.resHeaderValid = false
  707. p.index++
  708. return r, nil
  709. }
  710. // AAAAResource parses a single AAAAResource.
  711. //
  712. // One of the XXXHeader methods must have been called before calling this
  713. // method.
  714. func (p *Parser) AAAAResource() (AAAAResource, error) {
  715. if !p.resHeaderValid || p.resHeader.Type != TypeAAAA {
  716. return AAAAResource{}, ErrNotStarted
  717. }
  718. r, err := unpackAAAAResource(p.msg, p.off)
  719. if err != nil {
  720. return AAAAResource{}, err
  721. }
  722. p.off += int(p.resHeader.Length)
  723. p.resHeaderValid = false
  724. p.index++
  725. return r, nil
  726. }
  727. // Unpack parses a full Message.
  728. func (m *Message) Unpack(msg []byte) error {
  729. var p Parser
  730. var err error
  731. if m.Header, err = p.Start(msg); err != nil {
  732. return err
  733. }
  734. if m.Questions, err = p.AllQuestions(); err != nil {
  735. return err
  736. }
  737. if m.Answers, err = p.AllAnswers(); err != nil {
  738. return err
  739. }
  740. if m.Authorities, err = p.AllAuthorities(); err != nil {
  741. return err
  742. }
  743. if m.Additionals, err = p.AllAdditionals(); err != nil {
  744. return err
  745. }
  746. return nil
  747. }
  748. // Pack packs a full Message.
  749. func (m *Message) Pack() ([]byte, error) {
  750. return m.AppendPack(make([]byte, 0, packStartingCap))
  751. }
  752. // AppendPack is like Pack but appends the full Message to b and returns the
  753. // extended buffer.
  754. func (m *Message) AppendPack(b []byte) ([]byte, error) {
  755. // Validate the lengths. It is very unlikely that anyone will try to
  756. // pack more than 65535 of any particular type, but it is possible and
  757. // we should fail gracefully.
  758. if len(m.Questions) > int(^uint16(0)) {
  759. return nil, errTooManyQuestions
  760. }
  761. if len(m.Answers) > int(^uint16(0)) {
  762. return nil, errTooManyAnswers
  763. }
  764. if len(m.Authorities) > int(^uint16(0)) {
  765. return nil, errTooManyAuthorities
  766. }
  767. if len(m.Additionals) > int(^uint16(0)) {
  768. return nil, errTooManyAdditionals
  769. }
  770. var h header
  771. h.id, h.bits = m.Header.pack()
  772. h.questions = uint16(len(m.Questions))
  773. h.answers = uint16(len(m.Answers))
  774. h.authorities = uint16(len(m.Authorities))
  775. h.additionals = uint16(len(m.Additionals))
  776. compressionOff := len(b)
  777. msg := h.pack(b)
  778. // RFC 1035 allows (but does not require) compression for packing. RFC
  779. // 1035 requires unpacking implementations to support compression, so
  780. // unconditionally enabling it is fine.
  781. //
  782. // DNS lookups are typically done over UDP, and RFC 1035 states that UDP
  783. // DNS messages can be a maximum of 512 bytes long. Without compression,
  784. // many DNS response messages are over this limit, so enabling
  785. // compression will help ensure compliance.
  786. compression := map[string]int{}
  787. for i := range m.Questions {
  788. var err error
  789. if msg, err = m.Questions[i].pack(msg, compression, compressionOff); err != nil {
  790. return nil, &nestedError{"packing Question", err}
  791. }
  792. }
  793. for i := range m.Answers {
  794. var err error
  795. if msg, err = m.Answers[i].pack(msg, compression, compressionOff); err != nil {
  796. return nil, &nestedError{"packing Answer", err}
  797. }
  798. }
  799. for i := range m.Authorities {
  800. var err error
  801. if msg, err = m.Authorities[i].pack(msg, compression, compressionOff); err != nil {
  802. return nil, &nestedError{"packing Authority", err}
  803. }
  804. }
  805. for i := range m.Additionals {
  806. var err error
  807. if msg, err = m.Additionals[i].pack(msg, compression, compressionOff); err != nil {
  808. return nil, &nestedError{"packing Additional", err}
  809. }
  810. }
  811. return msg, nil
  812. }
  813. // A Builder allows incrementally packing a DNS message.
  814. //
  815. // Example usage:
  816. // buf := make([]byte, 2, 514)
  817. // b := NewBuilder(buf, Header{...})
  818. // b.EnableCompression()
  819. // // Optionally start a section and add things to that section.
  820. // // Repeat adding sections as necessary.
  821. // buf, err := b.Finish()
  822. // // If err is nil, buf[2:] will contain the built bytes.
  823. type Builder struct {
  824. // msg is the storage for the message being built.
  825. msg []byte
  826. // section keeps track of the current section being built.
  827. section section
  828. // header keeps track of what should go in the header when Finish is
  829. // called.
  830. header header
  831. // start is the starting index of the bytes allocated in msg for header.
  832. start int
  833. // compression is a mapping from name suffixes to their starting index
  834. // in msg.
  835. compression map[string]int
  836. }
  837. // NewBuilder creates a new builder with compression disabled.
  838. //
  839. // Note: Most users will want to immediately enable compression with the
  840. // EnableCompression method. See that method's comment for why you may or may
  841. // not want to enable compression.
  842. //
  843. // The DNS message is appended to the provided initial buffer buf (which may be
  844. // nil) as it is built. The final message is returned by the (*Builder).Finish
  845. // method, which may return the same underlying array if there was sufficient
  846. // capacity in the slice.
  847. func NewBuilder(buf []byte, h Header) Builder {
  848. if buf == nil {
  849. buf = make([]byte, 0, packStartingCap)
  850. }
  851. b := Builder{msg: buf, start: len(buf)}
  852. b.header.id, b.header.bits = h.pack()
  853. var hb [headerLen]byte
  854. b.msg = append(b.msg, hb[:]...)
  855. b.section = sectionHeader
  856. return b
  857. }
  858. // EnableCompression enables compression in the Builder.
  859. //
  860. // Leaving compression disabled avoids compression related allocations, but can
  861. // result in larger message sizes. Be careful with this mode as it can cause
  862. // messages to exceed the UDP size limit.
  863. //
  864. // According to RFC 1035, section 4.1.4, the use of compression is optional, but
  865. // all implementations must accept both compressed and uncompressed DNS
  866. // messages.
  867. //
  868. // Compression should be enabled before any sections are added for best results.
  869. func (b *Builder) EnableCompression() {
  870. b.compression = map[string]int{}
  871. }
  872. func (b *Builder) startCheck(s section) error {
  873. if b.section <= sectionNotStarted {
  874. return ErrNotStarted
  875. }
  876. if b.section > s {
  877. return ErrSectionDone
  878. }
  879. return nil
  880. }
  881. // StartQuestions prepares the builder for packing Questions.
  882. func (b *Builder) StartQuestions() error {
  883. if err := b.startCheck(sectionQuestions); err != nil {
  884. return err
  885. }
  886. b.section = sectionQuestions
  887. return nil
  888. }
  889. // StartAnswers prepares the builder for packing Answers.
  890. func (b *Builder) StartAnswers() error {
  891. if err := b.startCheck(sectionAnswers); err != nil {
  892. return err
  893. }
  894. b.section = sectionAnswers
  895. return nil
  896. }
  897. // StartAuthorities prepares the builder for packing Authorities.
  898. func (b *Builder) StartAuthorities() error {
  899. if err := b.startCheck(sectionAuthorities); err != nil {
  900. return err
  901. }
  902. b.section = sectionAuthorities
  903. return nil
  904. }
  905. // StartAdditionals prepares the builder for packing Additionals.
  906. func (b *Builder) StartAdditionals() error {
  907. if err := b.startCheck(sectionAdditionals); err != nil {
  908. return err
  909. }
  910. b.section = sectionAdditionals
  911. return nil
  912. }
  913. func (b *Builder) incrementSectionCount() error {
  914. var count *uint16
  915. var err error
  916. switch b.section {
  917. case sectionQuestions:
  918. count = &b.header.questions
  919. err = errTooManyQuestions
  920. case sectionAnswers:
  921. count = &b.header.answers
  922. err = errTooManyAnswers
  923. case sectionAuthorities:
  924. count = &b.header.authorities
  925. err = errTooManyAuthorities
  926. case sectionAdditionals:
  927. count = &b.header.additionals
  928. err = errTooManyAdditionals
  929. }
  930. if *count == ^uint16(0) {
  931. return err
  932. }
  933. *count++
  934. return nil
  935. }
  936. // Question adds a single Question.
  937. func (b *Builder) Question(q Question) error {
  938. if b.section < sectionQuestions {
  939. return ErrNotStarted
  940. }
  941. if b.section > sectionQuestions {
  942. return ErrSectionDone
  943. }
  944. msg, err := q.pack(b.msg, b.compression, b.start)
  945. if err != nil {
  946. return err
  947. }
  948. if err := b.incrementSectionCount(); err != nil {
  949. return err
  950. }
  951. b.msg = msg
  952. return nil
  953. }
  954. func (b *Builder) checkResourceSection() error {
  955. if b.section < sectionAnswers {
  956. return ErrNotStarted
  957. }
  958. if b.section > sectionAdditionals {
  959. return ErrSectionDone
  960. }
  961. return nil
  962. }
  963. // CNAMEResource adds a single CNAMEResource.
  964. func (b *Builder) CNAMEResource(h ResourceHeader, r CNAMEResource) error {
  965. if err := b.checkResourceSection(); err != nil {
  966. return err
  967. }
  968. h.Type = r.realType()
  969. msg, length, err := h.pack(b.msg, b.compression, b.start)
  970. if err != nil {
  971. return &nestedError{"ResourceHeader", err}
  972. }
  973. preLen := len(msg)
  974. if msg, err = r.pack(msg, b.compression, b.start); err != nil {
  975. return &nestedError{"CNAMEResource body", err}
  976. }
  977. if err := h.fixLen(msg, length, preLen); err != nil {
  978. return err
  979. }
  980. if err := b.incrementSectionCount(); err != nil {
  981. return err
  982. }
  983. b.msg = msg
  984. return nil
  985. }
  986. // MXResource adds a single MXResource.
  987. func (b *Builder) MXResource(h ResourceHeader, r MXResource) error {
  988. if err := b.checkResourceSection(); err != nil {
  989. return err
  990. }
  991. h.Type = r.realType()
  992. msg, length, err := h.pack(b.msg, b.compression, b.start)
  993. if err != nil {
  994. return &nestedError{"ResourceHeader", err}
  995. }
  996. preLen := len(msg)
  997. if msg, err = r.pack(msg, b.compression, b.start); err != nil {
  998. return &nestedError{"MXResource body", err}
  999. }
  1000. if err := h.fixLen(msg, length, preLen); err != nil {
  1001. return err
  1002. }
  1003. if err := b.incrementSectionCount(); err != nil {
  1004. return err
  1005. }
  1006. b.msg = msg
  1007. return nil
  1008. }
  1009. // NSResource adds a single NSResource.
  1010. func (b *Builder) NSResource(h ResourceHeader, r NSResource) error {
  1011. if err := b.checkResourceSection(); err != nil {
  1012. return err
  1013. }
  1014. h.Type = r.realType()
  1015. msg, length, err := h.pack(b.msg, b.compression, b.start)
  1016. if err != nil {
  1017. return &nestedError{"ResourceHeader", err}
  1018. }
  1019. preLen := len(msg)
  1020. if msg, err = r.pack(msg, b.compression, b.start); err != nil {
  1021. return &nestedError{"NSResource body", err}
  1022. }
  1023. if err := h.fixLen(msg, length, preLen); err != nil {
  1024. return err
  1025. }
  1026. if err := b.incrementSectionCount(); err != nil {
  1027. return err
  1028. }
  1029. b.msg = msg
  1030. return nil
  1031. }
  1032. // PTRResource adds a single PTRResource.
  1033. func (b *Builder) PTRResource(h ResourceHeader, r PTRResource) error {
  1034. if err := b.checkResourceSection(); err != nil {
  1035. return err
  1036. }
  1037. h.Type = r.realType()
  1038. msg, length, err := h.pack(b.msg, b.compression, b.start)
  1039. if err != nil {
  1040. return &nestedError{"ResourceHeader", err}
  1041. }
  1042. preLen := len(msg)
  1043. if msg, err = r.pack(msg, b.compression, b.start); err != nil {
  1044. return &nestedError{"PTRResource body", err}
  1045. }
  1046. if err := h.fixLen(msg, length, preLen); err != nil {
  1047. return err
  1048. }
  1049. if err := b.incrementSectionCount(); err != nil {
  1050. return err
  1051. }
  1052. b.msg = msg
  1053. return nil
  1054. }
  1055. // SOAResource adds a single SOAResource.
  1056. func (b *Builder) SOAResource(h ResourceHeader, r SOAResource) error {
  1057. if err := b.checkResourceSection(); err != nil {
  1058. return err
  1059. }
  1060. h.Type = r.realType()
  1061. msg, length, err := h.pack(b.msg, b.compression, b.start)
  1062. if err != nil {
  1063. return &nestedError{"ResourceHeader", err}
  1064. }
  1065. preLen := len(msg)
  1066. if msg, err = r.pack(msg, b.compression, b.start); err != nil {
  1067. return &nestedError{"SOAResource body", err}
  1068. }
  1069. if err := h.fixLen(msg, length, preLen); err != nil {
  1070. return err
  1071. }
  1072. if err := b.incrementSectionCount(); err != nil {
  1073. return err
  1074. }
  1075. b.msg = msg
  1076. return nil
  1077. }
  1078. // TXTResource adds a single TXTResource.
  1079. func (b *Builder) TXTResource(h ResourceHeader, r TXTResource) error {
  1080. if err := b.checkResourceSection(); err != nil {
  1081. return err
  1082. }
  1083. h.Type = r.realType()
  1084. msg, length, err := h.pack(b.msg, b.compression, b.start)
  1085. if err != nil {
  1086. return &nestedError{"ResourceHeader", err}
  1087. }
  1088. preLen := len(msg)
  1089. if msg, err = r.pack(msg, b.compression, b.start); err != nil {
  1090. return &nestedError{"TXTResource body", err}
  1091. }
  1092. if err := h.fixLen(msg, length, preLen); err != nil {
  1093. return err
  1094. }
  1095. if err := b.incrementSectionCount(); err != nil {
  1096. return err
  1097. }
  1098. b.msg = msg
  1099. return nil
  1100. }
  1101. // SRVResource adds a single SRVResource.
  1102. func (b *Builder) SRVResource(h ResourceHeader, r SRVResource) error {
  1103. if err := b.checkResourceSection(); err != nil {
  1104. return err
  1105. }
  1106. h.Type = r.realType()
  1107. msg, length, err := h.pack(b.msg, b.compression, b.start)
  1108. if err != nil {
  1109. return &nestedError{"ResourceHeader", err}
  1110. }
  1111. preLen := len(msg)
  1112. if msg, err = r.pack(msg, b.compression, b.start); err != nil {
  1113. return &nestedError{"SRVResource body", err}
  1114. }
  1115. if err := h.fixLen(msg, length, preLen); err != nil {
  1116. return err
  1117. }
  1118. if err := b.incrementSectionCount(); err != nil {
  1119. return err
  1120. }
  1121. b.msg = msg
  1122. return nil
  1123. }
  1124. // AResource adds a single AResource.
  1125. func (b *Builder) AResource(h ResourceHeader, r AResource) error {
  1126. if err := b.checkResourceSection(); err != nil {
  1127. return err
  1128. }
  1129. h.Type = r.realType()
  1130. msg, length, err := h.pack(b.msg, b.compression, b.start)
  1131. if err != nil {
  1132. return &nestedError{"ResourceHeader", err}
  1133. }
  1134. preLen := len(msg)
  1135. if msg, err = r.pack(msg, b.compression, b.start); err != nil {
  1136. return &nestedError{"AResource body", err}
  1137. }
  1138. if err := h.fixLen(msg, length, preLen); err != nil {
  1139. return err
  1140. }
  1141. if err := b.incrementSectionCount(); err != nil {
  1142. return err
  1143. }
  1144. b.msg = msg
  1145. return nil
  1146. }
  1147. // AAAAResource adds a single AAAAResource.
  1148. func (b *Builder) AAAAResource(h ResourceHeader, r AAAAResource) error {
  1149. if err := b.checkResourceSection(); err != nil {
  1150. return err
  1151. }
  1152. h.Type = r.realType()
  1153. msg, length, err := h.pack(b.msg, b.compression, b.start)
  1154. if err != nil {
  1155. return &nestedError{"ResourceHeader", err}
  1156. }
  1157. preLen := len(msg)
  1158. if msg, err = r.pack(msg, b.compression, b.start); err != nil {
  1159. return &nestedError{"AAAAResource body", err}
  1160. }
  1161. if err := h.fixLen(msg, length, preLen); err != nil {
  1162. return err
  1163. }
  1164. if err := b.incrementSectionCount(); err != nil {
  1165. return err
  1166. }
  1167. b.msg = msg
  1168. return nil
  1169. }
  1170. // Finish ends message building and generates a binary message.
  1171. func (b *Builder) Finish() ([]byte, error) {
  1172. if b.section < sectionHeader {
  1173. return nil, ErrNotStarted
  1174. }
  1175. b.section = sectionDone
  1176. // Space for the header was allocated in NewBuilder.
  1177. b.header.pack(b.msg[b.start:b.start])
  1178. return b.msg, nil
  1179. }
  1180. // A ResourceHeader is the header of a DNS resource record. There are
  1181. // many types of DNS resource records, but they all share the same header.
  1182. type ResourceHeader struct {
  1183. // Name is the domain name for which this resource record pertains.
  1184. Name Name
  1185. // Type is the type of DNS resource record.
  1186. //
  1187. // This field will be set automatically during packing.
  1188. Type Type
  1189. // Class is the class of network to which this DNS resource record
  1190. // pertains.
  1191. Class Class
  1192. // TTL is the length of time (measured in seconds) which this resource
  1193. // record is valid for (time to live). All Resources in a set should
  1194. // have the same TTL (RFC 2181 Section 5.2).
  1195. TTL uint32
  1196. // Length is the length of data in the resource record after the header.
  1197. //
  1198. // This field will be set automatically during packing.
  1199. Length uint16
  1200. }
  1201. // pack packs all of the fields in a ResourceHeader except for the length. The
  1202. // length bytes are returned as a slice so they can be filled in after the rest
  1203. // of the Resource has been packed.
  1204. func (h *ResourceHeader) pack(oldMsg []byte, compression map[string]int, compressionOff int) (msg []byte, length []byte, err error) {
  1205. msg = oldMsg
  1206. if msg, err = h.Name.pack(msg, compression, compressionOff); err != nil {
  1207. return oldMsg, nil, &nestedError{"Name", err}
  1208. }
  1209. msg = packType(msg, h.Type)
  1210. msg = packClass(msg, h.Class)
  1211. msg = packUint32(msg, h.TTL)
  1212. lenBegin := len(msg)
  1213. msg = packUint16(msg, h.Length)
  1214. return msg, msg[lenBegin : lenBegin+uint16Len], nil
  1215. }
  1216. func (h *ResourceHeader) unpack(msg []byte, off int) (int, error) {
  1217. newOff := off
  1218. var err error
  1219. if newOff, err = h.Name.unpack(msg, newOff); err != nil {
  1220. return off, &nestedError{"Name", err}
  1221. }
  1222. if h.Type, newOff, err = unpackType(msg, newOff); err != nil {
  1223. return off, &nestedError{"Type", err}
  1224. }
  1225. if h.Class, newOff, err = unpackClass(msg, newOff); err != nil {
  1226. return off, &nestedError{"Class", err}
  1227. }
  1228. if h.TTL, newOff, err = unpackUint32(msg, newOff); err != nil {
  1229. return off, &nestedError{"TTL", err}
  1230. }
  1231. if h.Length, newOff, err = unpackUint16(msg, newOff); err != nil {
  1232. return off, &nestedError{"Length", err}
  1233. }
  1234. return newOff, nil
  1235. }
  1236. func (h *ResourceHeader) fixLen(msg []byte, length []byte, preLen int) error {
  1237. conLen := len(msg) - preLen
  1238. if conLen > int(^uint16(0)) {
  1239. return errResTooLong
  1240. }
  1241. // Fill in the length now that we know how long the content is.
  1242. packUint16(length[:0], uint16(conLen))
  1243. h.Length = uint16(conLen)
  1244. return nil
  1245. }
  1246. func skipResource(msg []byte, off int) (int, error) {
  1247. newOff, err := skipName(msg, off)
  1248. if err != nil {
  1249. return off, &nestedError{"Name", err}
  1250. }
  1251. if newOff, err = skipType(msg, newOff); err != nil {
  1252. return off, &nestedError{"Type", err}
  1253. }
  1254. if newOff, err = skipClass(msg, newOff); err != nil {
  1255. return off, &nestedError{"Class", err}
  1256. }
  1257. if newOff, err = skipUint32(msg, newOff); err != nil {
  1258. return off, &nestedError{"TTL", err}
  1259. }
  1260. length, newOff, err := unpackUint16(msg, newOff)
  1261. if err != nil {
  1262. return off, &nestedError{"Length", err}
  1263. }
  1264. if newOff += int(length); newOff > len(msg) {
  1265. return off, errResourceLen
  1266. }
  1267. return newOff, nil
  1268. }
  1269. func packUint16(msg []byte, field uint16) []byte {
  1270. return append(msg, byte(field>>8), byte(field))
  1271. }
  1272. func unpackUint16(msg []byte, off int) (uint16, int, error) {
  1273. if off+uint16Len > len(msg) {
  1274. return 0, off, errBaseLen
  1275. }
  1276. return uint16(msg[off])<<8 | uint16(msg[off+1]), off + uint16Len, nil
  1277. }
  1278. func skipUint16(msg []byte, off int) (int, error) {
  1279. if off+uint16Len > len(msg) {
  1280. return off, errBaseLen
  1281. }
  1282. return off + uint16Len, nil
  1283. }
  1284. func packType(msg []byte, field Type) []byte {
  1285. return packUint16(msg, uint16(field))
  1286. }
  1287. func unpackType(msg []byte, off int) (Type, int, error) {
  1288. t, o, err := unpackUint16(msg, off)
  1289. return Type(t), o, err
  1290. }
  1291. func skipType(msg []byte, off int) (int, error) {
  1292. return skipUint16(msg, off)
  1293. }
  1294. func packClass(msg []byte, field Class) []byte {
  1295. return packUint16(msg, uint16(field))
  1296. }
  1297. func unpackClass(msg []byte, off int) (Class, int, error) {
  1298. c, o, err := unpackUint16(msg, off)
  1299. return Class(c), o, err
  1300. }
  1301. func skipClass(msg []byte, off int) (int, error) {
  1302. return skipUint16(msg, off)
  1303. }
  1304. func packUint32(msg []byte, field uint32) []byte {
  1305. return append(
  1306. msg,
  1307. byte(field>>24),
  1308. byte(field>>16),
  1309. byte(field>>8),
  1310. byte(field),
  1311. )
  1312. }
  1313. func unpackUint32(msg []byte, off int) (uint32, int, error) {
  1314. if off+uint32Len > len(msg) {
  1315. return 0, off, errBaseLen
  1316. }
  1317. v := uint32(msg[off])<<24 | uint32(msg[off+1])<<16 | uint32(msg[off+2])<<8 | uint32(msg[off+3])
  1318. return v, off + uint32Len, nil
  1319. }
  1320. func skipUint32(msg []byte, off int) (int, error) {
  1321. if off+uint32Len > len(msg) {
  1322. return off, errBaseLen
  1323. }
  1324. return off + uint32Len, nil
  1325. }
  1326. func packText(msg []byte, field string) []byte {
  1327. for len(field) > 0 {
  1328. l := len(field)
  1329. if l > 255 {
  1330. l = 255
  1331. }
  1332. msg = append(msg, byte(l))
  1333. msg = append(msg, field[:l]...)
  1334. field = field[l:]
  1335. }
  1336. return msg
  1337. }
  1338. func unpackText(msg []byte, off int) (string, int, error) {
  1339. if off >= len(msg) {
  1340. return "", off, errBaseLen
  1341. }
  1342. beginOff := off + 1
  1343. endOff := beginOff + int(msg[off])
  1344. if endOff > len(msg) {
  1345. return "", off, errCalcLen
  1346. }
  1347. return string(msg[beginOff:endOff]), endOff, nil
  1348. }
  1349. func skipText(msg []byte, off int) (int, error) {
  1350. if off >= len(msg) {
  1351. return off, errBaseLen
  1352. }
  1353. endOff := off + 1 + int(msg[off])
  1354. if endOff > len(msg) {
  1355. return off, errCalcLen
  1356. }
  1357. return endOff, nil
  1358. }
  1359. func packBytes(msg []byte, field []byte) []byte {
  1360. return append(msg, field...)
  1361. }
  1362. func unpackBytes(msg []byte, off int, field []byte) (int, error) {
  1363. newOff := off + len(field)
  1364. if newOff > len(msg) {
  1365. return off, errBaseLen
  1366. }
  1367. copy(field, msg[off:newOff])
  1368. return newOff, nil
  1369. }
  1370. func skipBytes(msg []byte, off int, field []byte) (int, error) {
  1371. newOff := off + len(field)
  1372. if newOff > len(msg) {
  1373. return off, errBaseLen
  1374. }
  1375. return newOff, nil
  1376. }
  1377. const nameLen = 255
  1378. // A Name is a non-encoded domain name. It is used instead of strings to avoid
  1379. // allocations.
  1380. type Name struct {
  1381. Data [nameLen]byte
  1382. Length uint8
  1383. }
  1384. // NewName creates a new Name from a string.
  1385. func NewName(name string) (Name, error) {
  1386. if len([]byte(name)) > nameLen {
  1387. return Name{}, errCalcLen
  1388. }
  1389. n := Name{Length: uint8(len(name))}
  1390. copy(n.Data[:], []byte(name))
  1391. return n, nil
  1392. }
  1393. func (n Name) String() string {
  1394. return string(n.Data[:n.Length])
  1395. }
  1396. // pack packs a domain name.
  1397. //
  1398. // Domain names are a sequence of counted strings split at the dots. They end
  1399. // with a zero-length string. Compression can be used to reuse domain suffixes.
  1400. //
  1401. // The compression map will be updated with new domain suffixes. If compression
  1402. // is nil, compression will not be used.
  1403. func (n *Name) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1404. oldMsg := msg
  1405. // Add a trailing dot to canonicalize name.
  1406. if n.Length == 0 || n.Data[n.Length-1] != '.' {
  1407. return oldMsg, errNonCanonicalName
  1408. }
  1409. // Allow root domain.
  1410. if n.Data[0] == '.' && n.Length == 1 {
  1411. return append(msg, 0), nil
  1412. }
  1413. // Emit sequence of counted strings, chopping at dots.
  1414. for i, begin := 0, 0; i < int(n.Length); i++ {
  1415. // Check for the end of the segment.
  1416. if n.Data[i] == '.' {
  1417. // The two most significant bits have special meaning.
  1418. // It isn't allowed for segments to be long enough to
  1419. // need them.
  1420. if i-begin >= 1<<6 {
  1421. return oldMsg, errSegTooLong
  1422. }
  1423. // Segments must have a non-zero length.
  1424. if i-begin == 0 {
  1425. return oldMsg, errZeroSegLen
  1426. }
  1427. msg = append(msg, byte(i-begin))
  1428. for j := begin; j < i; j++ {
  1429. msg = append(msg, n.Data[j])
  1430. }
  1431. begin = i + 1
  1432. continue
  1433. }
  1434. // We can only compress domain suffixes starting with a new
  1435. // segment. A pointer is two bytes with the two most significant
  1436. // bits set to 1 to indicate that it is a pointer.
  1437. if (i == 0 || n.Data[i-1] == '.') && compression != nil {
  1438. if ptr, ok := compression[string(n.Data[i:])]; ok {
  1439. // Hit. Emit a pointer instead of the rest of
  1440. // the domain.
  1441. return append(msg, byte(ptr>>8|0xC0), byte(ptr)), nil
  1442. }
  1443. // Miss. Add the suffix to the compression table if the
  1444. // offset can be stored in the available 14 bytes.
  1445. if len(msg) <= int(^uint16(0)>>2) {
  1446. compression[string(n.Data[i:])] = len(msg) - compressionOff
  1447. }
  1448. }
  1449. }
  1450. return append(msg, 0), nil
  1451. }
  1452. // unpack unpacks a domain name.
  1453. func (n *Name) unpack(msg []byte, off int) (int, error) {
  1454. // currOff is the current working offset.
  1455. currOff := off
  1456. // newOff is the offset where the next record will start. Pointers lead
  1457. // to data that belongs to other names and thus doesn't count towards to
  1458. // the usage of this name.
  1459. newOff := off
  1460. // ptr is the number of pointers followed.
  1461. var ptr int
  1462. // Name is a slice representation of the name data.
  1463. name := n.Data[:0]
  1464. Loop:
  1465. for {
  1466. if currOff >= len(msg) {
  1467. return off, errBaseLen
  1468. }
  1469. c := int(msg[currOff])
  1470. currOff++
  1471. switch c & 0xC0 {
  1472. case 0x00: // String segment
  1473. if c == 0x00 {
  1474. // A zero length signals the end of the name.
  1475. break Loop
  1476. }
  1477. endOff := currOff + c
  1478. if endOff > len(msg) {
  1479. return off, errCalcLen
  1480. }
  1481. name = append(name, msg[currOff:endOff]...)
  1482. name = append(name, '.')
  1483. currOff = endOff
  1484. case 0xC0: // Pointer
  1485. if currOff >= len(msg) {
  1486. return off, errInvalidPtr
  1487. }
  1488. c1 := msg[currOff]
  1489. currOff++
  1490. if ptr == 0 {
  1491. newOff = currOff
  1492. }
  1493. // Don't follow too many pointers, maybe there's a loop.
  1494. if ptr++; ptr > 10 {
  1495. return off, errTooManyPtr
  1496. }
  1497. currOff = (c^0xC0)<<8 | int(c1)
  1498. default:
  1499. // Prefixes 0x80 and 0x40 are reserved.
  1500. return off, errReserved
  1501. }
  1502. }
  1503. if len(name) == 0 {
  1504. name = append(name, '.')
  1505. }
  1506. if len(name) > len(n.Data) {
  1507. return off, errCalcLen
  1508. }
  1509. n.Length = uint8(len(name))
  1510. if ptr == 0 {
  1511. newOff = currOff
  1512. }
  1513. return newOff, nil
  1514. }
  1515. func skipName(msg []byte, off int) (int, error) {
  1516. // newOff is the offset where the next record will start. Pointers lead
  1517. // to data that belongs to other names and thus doesn't count towards to
  1518. // the usage of this name.
  1519. newOff := off
  1520. Loop:
  1521. for {
  1522. if newOff >= len(msg) {
  1523. return off, errBaseLen
  1524. }
  1525. c := int(msg[newOff])
  1526. newOff++
  1527. switch c & 0xC0 {
  1528. case 0x00:
  1529. if c == 0x00 {
  1530. // A zero length signals the end of the name.
  1531. break Loop
  1532. }
  1533. // literal string
  1534. newOff += c
  1535. if newOff > len(msg) {
  1536. return off, errCalcLen
  1537. }
  1538. case 0xC0:
  1539. // Pointer to somewhere else in msg.
  1540. // Pointers are two bytes.
  1541. newOff++
  1542. // Don't follow the pointer as the data here has ended.
  1543. break Loop
  1544. default:
  1545. // Prefixes 0x80 and 0x40 are reserved.
  1546. return off, errReserved
  1547. }
  1548. }
  1549. return newOff, nil
  1550. }
  1551. // A Question is a DNS query.
  1552. type Question struct {
  1553. Name Name
  1554. Type Type
  1555. Class Class
  1556. }
  1557. func (q *Question) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1558. msg, err := q.Name.pack(msg, compression, compressionOff)
  1559. if err != nil {
  1560. return msg, &nestedError{"Name", err}
  1561. }
  1562. msg = packType(msg, q.Type)
  1563. return packClass(msg, q.Class), nil
  1564. }
  1565. func unpackResourceBody(msg []byte, off int, hdr ResourceHeader) (ResourceBody, int, error) {
  1566. var (
  1567. r ResourceBody
  1568. err error
  1569. name string
  1570. )
  1571. switch hdr.Type {
  1572. case TypeA:
  1573. var rb AResource
  1574. rb, err = unpackAResource(msg, off)
  1575. r = &rb
  1576. name = "A"
  1577. case TypeNS:
  1578. var rb NSResource
  1579. rb, err = unpackNSResource(msg, off)
  1580. r = &rb
  1581. name = "NS"
  1582. case TypeCNAME:
  1583. var rb CNAMEResource
  1584. rb, err = unpackCNAMEResource(msg, off)
  1585. r = &rb
  1586. name = "CNAME"
  1587. case TypeSOA:
  1588. var rb SOAResource
  1589. rb, err = unpackSOAResource(msg, off)
  1590. r = &rb
  1591. name = "SOA"
  1592. case TypePTR:
  1593. var rb PTRResource
  1594. rb, err = unpackPTRResource(msg, off)
  1595. r = &rb
  1596. name = "PTR"
  1597. case TypeMX:
  1598. var rb MXResource
  1599. rb, err = unpackMXResource(msg, off)
  1600. r = &rb
  1601. name = "MX"
  1602. case TypeTXT:
  1603. var rb TXTResource
  1604. rb, err = unpackTXTResource(msg, off, hdr.Length)
  1605. r = &rb
  1606. name = "TXT"
  1607. case TypeAAAA:
  1608. var rb AAAAResource
  1609. rb, err = unpackAAAAResource(msg, off)
  1610. r = &rb
  1611. name = "AAAA"
  1612. case TypeSRV:
  1613. var rb SRVResource
  1614. rb, err = unpackSRVResource(msg, off)
  1615. r = &rb
  1616. name = "SRV"
  1617. }
  1618. if err != nil {
  1619. return nil, off, &nestedError{name + " record", err}
  1620. }
  1621. if r == nil {
  1622. return nil, off, errors.New("invalid resource type: " + string(hdr.Type+'0'))
  1623. }
  1624. return r, off + int(hdr.Length), nil
  1625. }
  1626. // A CNAMEResource is a CNAME Resource record.
  1627. type CNAMEResource struct {
  1628. CNAME Name
  1629. }
  1630. func (r *CNAMEResource) realType() Type {
  1631. return TypeCNAME
  1632. }
  1633. func (r *CNAMEResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1634. return r.CNAME.pack(msg, compression, compressionOff)
  1635. }
  1636. func unpackCNAMEResource(msg []byte, off int) (CNAMEResource, error) {
  1637. var cname Name
  1638. if _, err := cname.unpack(msg, off); err != nil {
  1639. return CNAMEResource{}, err
  1640. }
  1641. return CNAMEResource{cname}, nil
  1642. }
  1643. // An MXResource is an MX Resource record.
  1644. type MXResource struct {
  1645. Pref uint16
  1646. MX Name
  1647. }
  1648. func (r *MXResource) realType() Type {
  1649. return TypeMX
  1650. }
  1651. func (r *MXResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1652. oldMsg := msg
  1653. msg = packUint16(msg, r.Pref)
  1654. msg, err := r.MX.pack(msg, compression, compressionOff)
  1655. if err != nil {
  1656. return oldMsg, &nestedError{"MXResource.MX", err}
  1657. }
  1658. return msg, nil
  1659. }
  1660. func unpackMXResource(msg []byte, off int) (MXResource, error) {
  1661. pref, off, err := unpackUint16(msg, off)
  1662. if err != nil {
  1663. return MXResource{}, &nestedError{"Pref", err}
  1664. }
  1665. var mx Name
  1666. if _, err := mx.unpack(msg, off); err != nil {
  1667. return MXResource{}, &nestedError{"MX", err}
  1668. }
  1669. return MXResource{pref, mx}, nil
  1670. }
  1671. // An NSResource is an NS Resource record.
  1672. type NSResource struct {
  1673. NS Name
  1674. }
  1675. func (r *NSResource) realType() Type {
  1676. return TypeNS
  1677. }
  1678. func (r *NSResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1679. return r.NS.pack(msg, compression, compressionOff)
  1680. }
  1681. func unpackNSResource(msg []byte, off int) (NSResource, error) {
  1682. var ns Name
  1683. if _, err := ns.unpack(msg, off); err != nil {
  1684. return NSResource{}, err
  1685. }
  1686. return NSResource{ns}, nil
  1687. }
  1688. // A PTRResource is a PTR Resource record.
  1689. type PTRResource struct {
  1690. PTR Name
  1691. }
  1692. func (r *PTRResource) realType() Type {
  1693. return TypePTR
  1694. }
  1695. func (r *PTRResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1696. return r.PTR.pack(msg, compression, compressionOff)
  1697. }
  1698. func unpackPTRResource(msg []byte, off int) (PTRResource, error) {
  1699. var ptr Name
  1700. if _, err := ptr.unpack(msg, off); err != nil {
  1701. return PTRResource{}, err
  1702. }
  1703. return PTRResource{ptr}, nil
  1704. }
  1705. // An SOAResource is an SOA Resource record.
  1706. type SOAResource struct {
  1707. NS Name
  1708. MBox Name
  1709. Serial uint32
  1710. Refresh uint32
  1711. Retry uint32
  1712. Expire uint32
  1713. // MinTTL the is the default TTL of Resources records which did not
  1714. // contain a TTL value and the TTL of negative responses. (RFC 2308
  1715. // Section 4)
  1716. MinTTL uint32
  1717. }
  1718. func (r *SOAResource) realType() Type {
  1719. return TypeSOA
  1720. }
  1721. func (r *SOAResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1722. oldMsg := msg
  1723. msg, err := r.NS.pack(msg, compression, compressionOff)
  1724. if err != nil {
  1725. return oldMsg, &nestedError{"SOAResource.NS", err}
  1726. }
  1727. msg, err = r.MBox.pack(msg, compression, compressionOff)
  1728. if err != nil {
  1729. return oldMsg, &nestedError{"SOAResource.MBox", err}
  1730. }
  1731. msg = packUint32(msg, r.Serial)
  1732. msg = packUint32(msg, r.Refresh)
  1733. msg = packUint32(msg, r.Retry)
  1734. msg = packUint32(msg, r.Expire)
  1735. return packUint32(msg, r.MinTTL), nil
  1736. }
  1737. func unpackSOAResource(msg []byte, off int) (SOAResource, error) {
  1738. var ns Name
  1739. off, err := ns.unpack(msg, off)
  1740. if err != nil {
  1741. return SOAResource{}, &nestedError{"NS", err}
  1742. }
  1743. var mbox Name
  1744. if off, err = mbox.unpack(msg, off); err != nil {
  1745. return SOAResource{}, &nestedError{"MBox", err}
  1746. }
  1747. serial, off, err := unpackUint32(msg, off)
  1748. if err != nil {
  1749. return SOAResource{}, &nestedError{"Serial", err}
  1750. }
  1751. refresh, off, err := unpackUint32(msg, off)
  1752. if err != nil {
  1753. return SOAResource{}, &nestedError{"Refresh", err}
  1754. }
  1755. retry, off, err := unpackUint32(msg, off)
  1756. if err != nil {
  1757. return SOAResource{}, &nestedError{"Retry", err}
  1758. }
  1759. expire, off, err := unpackUint32(msg, off)
  1760. if err != nil {
  1761. return SOAResource{}, &nestedError{"Expire", err}
  1762. }
  1763. minTTL, _, err := unpackUint32(msg, off)
  1764. if err != nil {
  1765. return SOAResource{}, &nestedError{"MinTTL", err}
  1766. }
  1767. return SOAResource{ns, mbox, serial, refresh, retry, expire, minTTL}, nil
  1768. }
  1769. // A TXTResource is a TXT Resource record.
  1770. type TXTResource struct {
  1771. Txt string // Not a domain name.
  1772. }
  1773. func (r *TXTResource) realType() Type {
  1774. return TypeTXT
  1775. }
  1776. func (r *TXTResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1777. return packText(msg, r.Txt), nil
  1778. }
  1779. func unpackTXTResource(msg []byte, off int, length uint16) (TXTResource, error) {
  1780. var txt string
  1781. for n := uint16(0); n < length; {
  1782. var t string
  1783. var err error
  1784. if t, off, err = unpackText(msg, off); err != nil {
  1785. return TXTResource{}, &nestedError{"text", err}
  1786. }
  1787. // Check if we got too many bytes.
  1788. if length-n < uint16(len(t))+1 {
  1789. return TXTResource{}, errCalcLen
  1790. }
  1791. n += uint16(len(t)) + 1
  1792. txt += t
  1793. }
  1794. return TXTResource{txt}, nil
  1795. }
  1796. // An SRVResource is an SRV Resource record.
  1797. type SRVResource struct {
  1798. Priority uint16
  1799. Weight uint16
  1800. Port uint16
  1801. Target Name // Not compressed as per RFC 2782.
  1802. }
  1803. func (r *SRVResource) realType() Type {
  1804. return TypeSRV
  1805. }
  1806. func (r *SRVResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1807. oldMsg := msg
  1808. msg = packUint16(msg, r.Priority)
  1809. msg = packUint16(msg, r.Weight)
  1810. msg = packUint16(msg, r.Port)
  1811. msg, err := r.Target.pack(msg, nil, compressionOff)
  1812. if err != nil {
  1813. return oldMsg, &nestedError{"SRVResource.Target", err}
  1814. }
  1815. return msg, nil
  1816. }
  1817. func unpackSRVResource(msg []byte, off int) (SRVResource, error) {
  1818. priority, off, err := unpackUint16(msg, off)
  1819. if err != nil {
  1820. return SRVResource{}, &nestedError{"Priority", err}
  1821. }
  1822. weight, off, err := unpackUint16(msg, off)
  1823. if err != nil {
  1824. return SRVResource{}, &nestedError{"Weight", err}
  1825. }
  1826. port, off, err := unpackUint16(msg, off)
  1827. if err != nil {
  1828. return SRVResource{}, &nestedError{"Port", err}
  1829. }
  1830. var target Name
  1831. if _, err := target.unpack(msg, off); err != nil {
  1832. return SRVResource{}, &nestedError{"Target", err}
  1833. }
  1834. return SRVResource{priority, weight, port, target}, nil
  1835. }
  1836. // An AResource is an A Resource record.
  1837. type AResource struct {
  1838. A [4]byte
  1839. }
  1840. func (r *AResource) realType() Type {
  1841. return TypeA
  1842. }
  1843. func (r *AResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1844. return packBytes(msg, r.A[:]), nil
  1845. }
  1846. func unpackAResource(msg []byte, off int) (AResource, error) {
  1847. var a [4]byte
  1848. if _, err := unpackBytes(msg, off, a[:]); err != nil {
  1849. return AResource{}, err
  1850. }
  1851. return AResource{a}, nil
  1852. }
  1853. // An AAAAResource is an AAAA Resource record.
  1854. type AAAAResource struct {
  1855. AAAA [16]byte
  1856. }
  1857. func (r *AAAAResource) realType() Type {
  1858. return TypeAAAA
  1859. }
  1860. func (r *AAAAResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
  1861. return packBytes(msg, r.AAAA[:]), nil
  1862. }
  1863. func unpackAAAAResource(msg []byte, off int) (AAAAResource, error) {
  1864. var aaaa [16]byte
  1865. if _, err := unpackBytes(msg, off, aaaa[:]); err != nil {
  1866. return AAAAResource{}, err
  1867. }
  1868. return AAAAResource{aaaa}, nil
  1869. }