zscan_rr.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  1. package dns
  2. import (
  3. "encoding/base64"
  4. "net"
  5. "strconv"
  6. "strings"
  7. )
  8. type parserFunc struct {
  9. // Func defines the function that parses the tokens and returns the RR
  10. // or an error. The last string contains any comments in the line as
  11. // they returned by the lexer as well.
  12. Func func(h RR_Header, c chan lex, origin string, file string) (RR, *ParseError, string)
  13. // Signals if the RR ending is of variable length, like TXT or records
  14. // that have Hexadecimal or Base64 as their last element in the Rdata. Records
  15. // that have a fixed ending or for instance A, AAAA, SOA and etc.
  16. Variable bool
  17. }
  18. // Parse the rdata of each rrtype.
  19. // All data from the channel c is either zString or zBlank.
  20. // After the rdata there may come a zBlank and then a zNewline
  21. // or immediately a zNewline. If this is not the case we flag
  22. // an *ParseError: garbage after rdata.
  23. func setRR(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  24. parserfunc, ok := typeToparserFunc[h.Rrtype]
  25. if ok {
  26. r, e, cm := parserfunc.Func(h, c, o, f)
  27. if parserfunc.Variable {
  28. return r, e, cm
  29. }
  30. if e != nil {
  31. return nil, e, ""
  32. }
  33. e, cm = slurpRemainder(c, f)
  34. if e != nil {
  35. return nil, e, ""
  36. }
  37. return r, nil, cm
  38. }
  39. // RFC3957 RR (Unknown RR handling)
  40. return setRFC3597(h, c, o, f)
  41. }
  42. // A remainder of the rdata with embedded spaces, return the parsed string (sans the spaces)
  43. // or an error
  44. func endingToString(c chan lex, errstr, f string) (string, *ParseError, string) {
  45. s := ""
  46. l := <-c // zString
  47. for l.value != zNewline && l.value != zEOF {
  48. if l.err {
  49. return s, &ParseError{f, errstr, l}, ""
  50. }
  51. switch l.value {
  52. case zString:
  53. s += l.token
  54. case zBlank: // Ok
  55. default:
  56. return "", &ParseError{f, errstr, l}, ""
  57. }
  58. l = <-c
  59. }
  60. return s, nil, l.comment
  61. }
  62. // A remainder of the rdata with embedded spaces, return the parsed string slice (sans the spaces)
  63. // or an error
  64. func endingToTxtSlice(c chan lex, errstr, f string) ([]string, *ParseError, string) {
  65. // Get the remaining data until we see a zNewline
  66. quote := false
  67. l := <-c
  68. var s []string
  69. if l.err {
  70. return s, &ParseError{f, errstr, l}, ""
  71. }
  72. switch l.value == zQuote {
  73. case true: // A number of quoted string
  74. s = make([]string, 0)
  75. empty := true
  76. for l.value != zNewline && l.value != zEOF {
  77. if l.err {
  78. return nil, &ParseError{f, errstr, l}, ""
  79. }
  80. switch l.value {
  81. case zString:
  82. empty = false
  83. if len(l.token) > 255 {
  84. // split up tokens that are larger than 255 into 255-chunks
  85. sx := []string{}
  86. p, i := 0, 255
  87. for {
  88. if i <= len(l.token) {
  89. sx = append(sx, l.token[p:i])
  90. } else {
  91. sx = append(sx, l.token[p:])
  92. break
  93. }
  94. p, i = p+255, i+255
  95. }
  96. s = append(s, sx...)
  97. break
  98. }
  99. s = append(s, l.token)
  100. case zBlank:
  101. if quote {
  102. // zBlank can only be seen in between txt parts.
  103. return nil, &ParseError{f, errstr, l}, ""
  104. }
  105. case zQuote:
  106. if empty && quote {
  107. s = append(s, "")
  108. }
  109. quote = !quote
  110. empty = true
  111. default:
  112. return nil, &ParseError{f, errstr, l}, ""
  113. }
  114. l = <-c
  115. }
  116. if quote {
  117. return nil, &ParseError{f, errstr, l}, ""
  118. }
  119. case false: // Unquoted text record
  120. s = make([]string, 1)
  121. for l.value != zNewline && l.value != zEOF {
  122. if l.err {
  123. return s, &ParseError{f, errstr, l}, ""
  124. }
  125. s[0] += l.token
  126. l = <-c
  127. }
  128. }
  129. return s, nil, l.comment
  130. }
  131. func setA(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  132. rr := new(A)
  133. rr.Hdr = h
  134. l := <-c
  135. if l.length == 0 { // Dynamic updates.
  136. return rr, nil, ""
  137. }
  138. rr.A = net.ParseIP(l.token)
  139. if rr.A == nil || l.err {
  140. return nil, &ParseError{f, "bad A A", l}, ""
  141. }
  142. return rr, nil, ""
  143. }
  144. func setAAAA(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  145. rr := new(AAAA)
  146. rr.Hdr = h
  147. l := <-c
  148. if l.length == 0 {
  149. return rr, nil, ""
  150. }
  151. rr.AAAA = net.ParseIP(l.token)
  152. if rr.AAAA == nil || l.err {
  153. return nil, &ParseError{f, "bad AAAA AAAA", l}, ""
  154. }
  155. return rr, nil, ""
  156. }
  157. func setNS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  158. rr := new(NS)
  159. rr.Hdr = h
  160. l := <-c
  161. rr.Ns = l.token
  162. if l.length == 0 {
  163. return rr, nil, ""
  164. }
  165. if l.token == "@" {
  166. rr.Ns = o
  167. return rr, nil, ""
  168. }
  169. _, ok := IsDomainName(l.token)
  170. if !ok || l.length == 0 || l.err {
  171. return nil, &ParseError{f, "bad NS Ns", l}, ""
  172. }
  173. if rr.Ns[l.length-1] != '.' {
  174. rr.Ns = appendOrigin(rr.Ns, o)
  175. }
  176. return rr, nil, ""
  177. }
  178. func setPTR(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  179. rr := new(PTR)
  180. rr.Hdr = h
  181. l := <-c
  182. rr.Ptr = l.token
  183. if l.length == 0 { // dynamic update rr.
  184. return rr, nil, ""
  185. }
  186. if l.token == "@" {
  187. rr.Ptr = o
  188. return rr, nil, ""
  189. }
  190. _, ok := IsDomainName(l.token)
  191. if !ok || l.length == 0 || l.err {
  192. return nil, &ParseError{f, "bad PTR Ptr", l}, ""
  193. }
  194. if rr.Ptr[l.length-1] != '.' {
  195. rr.Ptr = appendOrigin(rr.Ptr, o)
  196. }
  197. return rr, nil, ""
  198. }
  199. func setNSAPPTR(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  200. rr := new(NSAPPTR)
  201. rr.Hdr = h
  202. l := <-c
  203. rr.Ptr = l.token
  204. if l.length == 0 {
  205. return rr, nil, ""
  206. }
  207. if l.token == "@" {
  208. rr.Ptr = o
  209. return rr, nil, ""
  210. }
  211. _, ok := IsDomainName(l.token)
  212. if !ok || l.length == 0 || l.err {
  213. return nil, &ParseError{f, "bad NSAP-PTR Ptr", l}, ""
  214. }
  215. if rr.Ptr[l.length-1] != '.' {
  216. rr.Ptr = appendOrigin(rr.Ptr, o)
  217. }
  218. return rr, nil, ""
  219. }
  220. func setRP(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  221. rr := new(RP)
  222. rr.Hdr = h
  223. l := <-c
  224. rr.Mbox = l.token
  225. if l.length == 0 {
  226. return rr, nil, ""
  227. }
  228. if l.token == "@" {
  229. rr.Mbox = o
  230. } else {
  231. _, ok := IsDomainName(l.token)
  232. if !ok || l.length == 0 || l.err {
  233. return nil, &ParseError{f, "bad RP Mbox", l}, ""
  234. }
  235. if rr.Mbox[l.length-1] != '.' {
  236. rr.Mbox = appendOrigin(rr.Mbox, o)
  237. }
  238. }
  239. <-c // zBlank
  240. l = <-c
  241. rr.Txt = l.token
  242. if l.token == "@" {
  243. rr.Txt = o
  244. return rr, nil, ""
  245. }
  246. _, ok := IsDomainName(l.token)
  247. if !ok || l.length == 0 || l.err {
  248. return nil, &ParseError{f, "bad RP Txt", l}, ""
  249. }
  250. if rr.Txt[l.length-1] != '.' {
  251. rr.Txt = appendOrigin(rr.Txt, o)
  252. }
  253. return rr, nil, ""
  254. }
  255. func setMR(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  256. rr := new(MR)
  257. rr.Hdr = h
  258. l := <-c
  259. rr.Mr = l.token
  260. if l.length == 0 {
  261. return rr, nil, ""
  262. }
  263. if l.token == "@" {
  264. rr.Mr = o
  265. return rr, nil, ""
  266. }
  267. _, ok := IsDomainName(l.token)
  268. if !ok || l.length == 0 || l.err {
  269. return nil, &ParseError{f, "bad MR Mr", l}, ""
  270. }
  271. if rr.Mr[l.length-1] != '.' {
  272. rr.Mr = appendOrigin(rr.Mr, o)
  273. }
  274. return rr, nil, ""
  275. }
  276. func setMB(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  277. rr := new(MB)
  278. rr.Hdr = h
  279. l := <-c
  280. rr.Mb = l.token
  281. if l.length == 0 {
  282. return rr, nil, ""
  283. }
  284. if l.token == "@" {
  285. rr.Mb = o
  286. return rr, nil, ""
  287. }
  288. _, ok := IsDomainName(l.token)
  289. if !ok || l.length == 0 || l.err {
  290. return nil, &ParseError{f, "bad MB Mb", l}, ""
  291. }
  292. if rr.Mb[l.length-1] != '.' {
  293. rr.Mb = appendOrigin(rr.Mb, o)
  294. }
  295. return rr, nil, ""
  296. }
  297. func setMG(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  298. rr := new(MG)
  299. rr.Hdr = h
  300. l := <-c
  301. rr.Mg = l.token
  302. if l.length == 0 {
  303. return rr, nil, ""
  304. }
  305. if l.token == "@" {
  306. rr.Mg = o
  307. return rr, nil, ""
  308. }
  309. _, ok := IsDomainName(l.token)
  310. if !ok || l.length == 0 || l.err {
  311. return nil, &ParseError{f, "bad MG Mg", l}, ""
  312. }
  313. if rr.Mg[l.length-1] != '.' {
  314. rr.Mg = appendOrigin(rr.Mg, o)
  315. }
  316. return rr, nil, ""
  317. }
  318. func setHINFO(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  319. rr := new(HINFO)
  320. rr.Hdr = h
  321. chunks, e, c1 := endingToTxtSlice(c, "bad HINFO Fields", f)
  322. if e != nil {
  323. return nil, e, c1
  324. }
  325. if ln := len(chunks); ln == 0 {
  326. return rr, nil, ""
  327. } else if ln == 1 {
  328. // Can we split it?
  329. if out := strings.Fields(chunks[0]); len(out) > 1 {
  330. chunks = out
  331. } else {
  332. chunks = append(chunks, "")
  333. }
  334. }
  335. rr.Cpu = chunks[0]
  336. rr.Os = strings.Join(chunks[1:], " ")
  337. return rr, nil, ""
  338. }
  339. func setMINFO(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  340. rr := new(MINFO)
  341. rr.Hdr = h
  342. l := <-c
  343. rr.Rmail = l.token
  344. if l.length == 0 {
  345. return rr, nil, ""
  346. }
  347. if l.token == "@" {
  348. rr.Rmail = o
  349. } else {
  350. _, ok := IsDomainName(l.token)
  351. if !ok || l.length == 0 || l.err {
  352. return nil, &ParseError{f, "bad MINFO Rmail", l}, ""
  353. }
  354. if rr.Rmail[l.length-1] != '.' {
  355. rr.Rmail = appendOrigin(rr.Rmail, o)
  356. }
  357. }
  358. <-c // zBlank
  359. l = <-c
  360. rr.Email = l.token
  361. if l.token == "@" {
  362. rr.Email = o
  363. return rr, nil, ""
  364. }
  365. _, ok := IsDomainName(l.token)
  366. if !ok || l.length == 0 || l.err {
  367. return nil, &ParseError{f, "bad MINFO Email", l}, ""
  368. }
  369. if rr.Email[l.length-1] != '.' {
  370. rr.Email = appendOrigin(rr.Email, o)
  371. }
  372. return rr, nil, ""
  373. }
  374. func setMF(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  375. rr := new(MF)
  376. rr.Hdr = h
  377. l := <-c
  378. rr.Mf = l.token
  379. if l.length == 0 {
  380. return rr, nil, ""
  381. }
  382. if l.token == "@" {
  383. rr.Mf = o
  384. return rr, nil, ""
  385. }
  386. _, ok := IsDomainName(l.token)
  387. if !ok || l.length == 0 || l.err {
  388. return nil, &ParseError{f, "bad MF Mf", l}, ""
  389. }
  390. if rr.Mf[l.length-1] != '.' {
  391. rr.Mf = appendOrigin(rr.Mf, o)
  392. }
  393. return rr, nil, ""
  394. }
  395. func setMD(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  396. rr := new(MD)
  397. rr.Hdr = h
  398. l := <-c
  399. rr.Md = l.token
  400. if l.length == 0 {
  401. return rr, nil, ""
  402. }
  403. if l.token == "@" {
  404. rr.Md = o
  405. return rr, nil, ""
  406. }
  407. _, ok := IsDomainName(l.token)
  408. if !ok || l.length == 0 || l.err {
  409. return nil, &ParseError{f, "bad MD Md", l}, ""
  410. }
  411. if rr.Md[l.length-1] != '.' {
  412. rr.Md = appendOrigin(rr.Md, o)
  413. }
  414. return rr, nil, ""
  415. }
  416. func setMX(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  417. rr := new(MX)
  418. rr.Hdr = h
  419. l := <-c
  420. if l.length == 0 {
  421. return rr, nil, ""
  422. }
  423. i, e := strconv.Atoi(l.token)
  424. if e != nil || l.err {
  425. return nil, &ParseError{f, "bad MX Pref", l}, ""
  426. }
  427. rr.Preference = uint16(i)
  428. <-c // zBlank
  429. l = <-c // zString
  430. rr.Mx = l.token
  431. if l.token == "@" {
  432. rr.Mx = o
  433. return rr, nil, ""
  434. }
  435. _, ok := IsDomainName(l.token)
  436. if !ok || l.length == 0 || l.err {
  437. return nil, &ParseError{f, "bad MX Mx", l}, ""
  438. }
  439. if rr.Mx[l.length-1] != '.' {
  440. rr.Mx = appendOrigin(rr.Mx, o)
  441. }
  442. return rr, nil, ""
  443. }
  444. func setRT(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  445. rr := new(RT)
  446. rr.Hdr = h
  447. l := <-c
  448. if l.length == 0 {
  449. return rr, nil, ""
  450. }
  451. i, e := strconv.Atoi(l.token)
  452. if e != nil {
  453. return nil, &ParseError{f, "bad RT Preference", l}, ""
  454. }
  455. rr.Preference = uint16(i)
  456. <-c // zBlank
  457. l = <-c // zString
  458. rr.Host = l.token
  459. if l.token == "@" {
  460. rr.Host = o
  461. return rr, nil, ""
  462. }
  463. _, ok := IsDomainName(l.token)
  464. if !ok || l.length == 0 || l.err {
  465. return nil, &ParseError{f, "bad RT Host", l}, ""
  466. }
  467. if rr.Host[l.length-1] != '.' {
  468. rr.Host = appendOrigin(rr.Host, o)
  469. }
  470. return rr, nil, ""
  471. }
  472. func setAFSDB(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  473. rr := new(AFSDB)
  474. rr.Hdr = h
  475. l := <-c
  476. if l.length == 0 {
  477. return rr, nil, ""
  478. }
  479. i, e := strconv.Atoi(l.token)
  480. if e != nil || l.err {
  481. return nil, &ParseError{f, "bad AFSDB Subtype", l}, ""
  482. }
  483. rr.Subtype = uint16(i)
  484. <-c // zBlank
  485. l = <-c // zString
  486. rr.Hostname = l.token
  487. if l.token == "@" {
  488. rr.Hostname = o
  489. return rr, nil, ""
  490. }
  491. _, ok := IsDomainName(l.token)
  492. if !ok || l.length == 0 || l.err {
  493. return nil, &ParseError{f, "bad AFSDB Hostname", l}, ""
  494. }
  495. if rr.Hostname[l.length-1] != '.' {
  496. rr.Hostname = appendOrigin(rr.Hostname, o)
  497. }
  498. return rr, nil, ""
  499. }
  500. func setX25(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  501. rr := new(X25)
  502. rr.Hdr = h
  503. l := <-c
  504. if l.length == 0 {
  505. return rr, nil, ""
  506. }
  507. if l.err {
  508. return nil, &ParseError{f, "bad X25 PSDNAddress", l}, ""
  509. }
  510. rr.PSDNAddress = l.token
  511. return rr, nil, ""
  512. }
  513. func setKX(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  514. rr := new(KX)
  515. rr.Hdr = h
  516. l := <-c
  517. if l.length == 0 {
  518. return rr, nil, ""
  519. }
  520. i, e := strconv.Atoi(l.token)
  521. if e != nil || l.err {
  522. return nil, &ParseError{f, "bad KX Pref", l}, ""
  523. }
  524. rr.Preference = uint16(i)
  525. <-c // zBlank
  526. l = <-c // zString
  527. rr.Exchanger = l.token
  528. if l.token == "@" {
  529. rr.Exchanger = o
  530. return rr, nil, ""
  531. }
  532. _, ok := IsDomainName(l.token)
  533. if !ok || l.length == 0 || l.err {
  534. return nil, &ParseError{f, "bad KX Exchanger", l}, ""
  535. }
  536. if rr.Exchanger[l.length-1] != '.' {
  537. rr.Exchanger = appendOrigin(rr.Exchanger, o)
  538. }
  539. return rr, nil, ""
  540. }
  541. func setCNAME(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  542. rr := new(CNAME)
  543. rr.Hdr = h
  544. l := <-c
  545. rr.Target = l.token
  546. if l.length == 0 {
  547. return rr, nil, ""
  548. }
  549. if l.token == "@" {
  550. rr.Target = o
  551. return rr, nil, ""
  552. }
  553. _, ok := IsDomainName(l.token)
  554. if !ok || l.length == 0 || l.err {
  555. return nil, &ParseError{f, "bad CNAME Target", l}, ""
  556. }
  557. if rr.Target[l.length-1] != '.' {
  558. rr.Target = appendOrigin(rr.Target, o)
  559. }
  560. return rr, nil, ""
  561. }
  562. func setDNAME(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  563. rr := new(DNAME)
  564. rr.Hdr = h
  565. l := <-c
  566. rr.Target = l.token
  567. if l.length == 0 {
  568. return rr, nil, ""
  569. }
  570. if l.token == "@" {
  571. rr.Target = o
  572. return rr, nil, ""
  573. }
  574. _, ok := IsDomainName(l.token)
  575. if !ok || l.length == 0 || l.err {
  576. return nil, &ParseError{f, "bad CNAME Target", l}, ""
  577. }
  578. if rr.Target[l.length-1] != '.' {
  579. rr.Target = appendOrigin(rr.Target, o)
  580. }
  581. return rr, nil, ""
  582. }
  583. func setSOA(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  584. rr := new(SOA)
  585. rr.Hdr = h
  586. l := <-c
  587. rr.Ns = l.token
  588. if l.length == 0 {
  589. return rr, nil, ""
  590. }
  591. <-c // zBlank
  592. if l.token == "@" {
  593. rr.Ns = o
  594. } else {
  595. _, ok := IsDomainName(l.token)
  596. if !ok || l.length == 0 || l.err {
  597. return nil, &ParseError{f, "bad SOA Ns", l}, ""
  598. }
  599. if rr.Ns[l.length-1] != '.' {
  600. rr.Ns = appendOrigin(rr.Ns, o)
  601. }
  602. }
  603. l = <-c
  604. rr.Mbox = l.token
  605. if l.token == "@" {
  606. rr.Mbox = o
  607. } else {
  608. _, ok := IsDomainName(l.token)
  609. if !ok || l.length == 0 || l.err {
  610. return nil, &ParseError{f, "bad SOA Mbox", l}, ""
  611. }
  612. if rr.Mbox[l.length-1] != '.' {
  613. rr.Mbox = appendOrigin(rr.Mbox, o)
  614. }
  615. }
  616. <-c // zBlank
  617. var (
  618. v uint32
  619. ok bool
  620. )
  621. for i := 0; i < 5; i++ {
  622. l = <-c
  623. if l.err {
  624. return nil, &ParseError{f, "bad SOA zone parameter", l}, ""
  625. }
  626. if j, e := strconv.Atoi(l.token); e != nil {
  627. if i == 0 {
  628. // Serial should be a number
  629. return nil, &ParseError{f, "bad SOA zone parameter", l}, ""
  630. }
  631. if v, ok = stringToTtl(l.token); !ok {
  632. return nil, &ParseError{f, "bad SOA zone parameter", l}, ""
  633. }
  634. } else {
  635. v = uint32(j)
  636. }
  637. switch i {
  638. case 0:
  639. rr.Serial = v
  640. <-c // zBlank
  641. case 1:
  642. rr.Refresh = v
  643. <-c // zBlank
  644. case 2:
  645. rr.Retry = v
  646. <-c // zBlank
  647. case 3:
  648. rr.Expire = v
  649. <-c // zBlank
  650. case 4:
  651. rr.Minttl = v
  652. }
  653. }
  654. return rr, nil, ""
  655. }
  656. func setSRV(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  657. rr := new(SRV)
  658. rr.Hdr = h
  659. l := <-c
  660. if l.length == 0 {
  661. return rr, nil, ""
  662. }
  663. i, e := strconv.Atoi(l.token)
  664. if e != nil || l.err {
  665. return nil, &ParseError{f, "bad SRV Priority", l}, ""
  666. }
  667. rr.Priority = uint16(i)
  668. <-c // zBlank
  669. l = <-c // zString
  670. i, e = strconv.Atoi(l.token)
  671. if e != nil || l.err {
  672. return nil, &ParseError{f, "bad SRV Weight", l}, ""
  673. }
  674. rr.Weight = uint16(i)
  675. <-c // zBlank
  676. l = <-c // zString
  677. i, e = strconv.Atoi(l.token)
  678. if e != nil || l.err {
  679. return nil, &ParseError{f, "bad SRV Port", l}, ""
  680. }
  681. rr.Port = uint16(i)
  682. <-c // zBlank
  683. l = <-c // zString
  684. rr.Target = l.token
  685. if l.token == "@" {
  686. rr.Target = o
  687. return rr, nil, ""
  688. }
  689. _, ok := IsDomainName(l.token)
  690. if !ok || l.length == 0 || l.err {
  691. return nil, &ParseError{f, "bad SRV Target", l}, ""
  692. }
  693. if rr.Target[l.length-1] != '.' {
  694. rr.Target = appendOrigin(rr.Target, o)
  695. }
  696. return rr, nil, ""
  697. }
  698. func setNAPTR(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  699. rr := new(NAPTR)
  700. rr.Hdr = h
  701. l := <-c
  702. if l.length == 0 {
  703. return rr, nil, ""
  704. }
  705. i, e := strconv.Atoi(l.token)
  706. if e != nil || l.err {
  707. return nil, &ParseError{f, "bad NAPTR Order", l}, ""
  708. }
  709. rr.Order = uint16(i)
  710. <-c // zBlank
  711. l = <-c // zString
  712. i, e = strconv.Atoi(l.token)
  713. if e != nil || l.err {
  714. return nil, &ParseError{f, "bad NAPTR Preference", l}, ""
  715. }
  716. rr.Preference = uint16(i)
  717. // Flags
  718. <-c // zBlank
  719. l = <-c // _QUOTE
  720. if l.value != zQuote {
  721. return nil, &ParseError{f, "bad NAPTR Flags", l}, ""
  722. }
  723. l = <-c // Either String or Quote
  724. if l.value == zString {
  725. rr.Flags = l.token
  726. l = <-c // _QUOTE
  727. if l.value != zQuote {
  728. return nil, &ParseError{f, "bad NAPTR Flags", l}, ""
  729. }
  730. } else if l.value == zQuote {
  731. rr.Flags = ""
  732. } else {
  733. return nil, &ParseError{f, "bad NAPTR Flags", l}, ""
  734. }
  735. // Service
  736. <-c // zBlank
  737. l = <-c // _QUOTE
  738. if l.value != zQuote {
  739. return nil, &ParseError{f, "bad NAPTR Service", l}, ""
  740. }
  741. l = <-c // Either String or Quote
  742. if l.value == zString {
  743. rr.Service = l.token
  744. l = <-c // _QUOTE
  745. if l.value != zQuote {
  746. return nil, &ParseError{f, "bad NAPTR Service", l}, ""
  747. }
  748. } else if l.value == zQuote {
  749. rr.Service = ""
  750. } else {
  751. return nil, &ParseError{f, "bad NAPTR Service", l}, ""
  752. }
  753. // Regexp
  754. <-c // zBlank
  755. l = <-c // _QUOTE
  756. if l.value != zQuote {
  757. return nil, &ParseError{f, "bad NAPTR Regexp", l}, ""
  758. }
  759. l = <-c // Either String or Quote
  760. if l.value == zString {
  761. rr.Regexp = l.token
  762. l = <-c // _QUOTE
  763. if l.value != zQuote {
  764. return nil, &ParseError{f, "bad NAPTR Regexp", l}, ""
  765. }
  766. } else if l.value == zQuote {
  767. rr.Regexp = ""
  768. } else {
  769. return nil, &ParseError{f, "bad NAPTR Regexp", l}, ""
  770. }
  771. // After quote no space??
  772. <-c // zBlank
  773. l = <-c // zString
  774. rr.Replacement = l.token
  775. if l.token == "@" {
  776. rr.Replacement = o
  777. return rr, nil, ""
  778. }
  779. _, ok := IsDomainName(l.token)
  780. if !ok || l.length == 0 || l.err {
  781. return nil, &ParseError{f, "bad NAPTR Replacement", l}, ""
  782. }
  783. if rr.Replacement[l.length-1] != '.' {
  784. rr.Replacement = appendOrigin(rr.Replacement, o)
  785. }
  786. return rr, nil, ""
  787. }
  788. func setTALINK(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  789. rr := new(TALINK)
  790. rr.Hdr = h
  791. l := <-c
  792. rr.PreviousName = l.token
  793. if l.length == 0 {
  794. return rr, nil, ""
  795. }
  796. if l.token == "@" {
  797. rr.PreviousName = o
  798. } else {
  799. _, ok := IsDomainName(l.token)
  800. if !ok || l.length == 0 || l.err {
  801. return nil, &ParseError{f, "bad TALINK PreviousName", l}, ""
  802. }
  803. if rr.PreviousName[l.length-1] != '.' {
  804. rr.PreviousName = appendOrigin(rr.PreviousName, o)
  805. }
  806. }
  807. <-c // zBlank
  808. l = <-c
  809. rr.NextName = l.token
  810. if l.token == "@" {
  811. rr.NextName = o
  812. return rr, nil, ""
  813. }
  814. _, ok := IsDomainName(l.token)
  815. if !ok || l.length == 0 || l.err {
  816. return nil, &ParseError{f, "bad TALINK NextName", l}, ""
  817. }
  818. if rr.NextName[l.length-1] != '.' {
  819. rr.NextName = appendOrigin(rr.NextName, o)
  820. }
  821. return rr, nil, ""
  822. }
  823. func setLOC(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  824. rr := new(LOC)
  825. rr.Hdr = h
  826. // Non zero defaults for LOC record, see RFC 1876, Section 3.
  827. rr.HorizPre = 165 // 10000
  828. rr.VertPre = 162 // 10
  829. rr.Size = 18 // 1
  830. ok := false
  831. // North
  832. l := <-c
  833. if l.length == 0 {
  834. return rr, nil, ""
  835. }
  836. i, e := strconv.Atoi(l.token)
  837. if e != nil || l.err {
  838. return nil, &ParseError{f, "bad LOC Latitude", l}, ""
  839. }
  840. rr.Latitude = 1000 * 60 * 60 * uint32(i)
  841. <-c // zBlank
  842. // Either number, 'N' or 'S'
  843. l = <-c
  844. if rr.Latitude, ok = locCheckNorth(l.token, rr.Latitude); ok {
  845. goto East
  846. }
  847. i, e = strconv.Atoi(l.token)
  848. if e != nil || l.err {
  849. return nil, &ParseError{f, "bad LOC Latitude minutes", l}, ""
  850. }
  851. rr.Latitude += 1000 * 60 * uint32(i)
  852. <-c // zBlank
  853. l = <-c
  854. if i, e := strconv.ParseFloat(l.token, 32); e != nil || l.err {
  855. return nil, &ParseError{f, "bad LOC Latitude seconds", l}, ""
  856. } else {
  857. rr.Latitude += uint32(1000 * i)
  858. }
  859. <-c // zBlank
  860. // Either number, 'N' or 'S'
  861. l = <-c
  862. if rr.Latitude, ok = locCheckNorth(l.token, rr.Latitude); ok {
  863. goto East
  864. }
  865. // If still alive, flag an error
  866. return nil, &ParseError{f, "bad LOC Latitude North/South", l}, ""
  867. East:
  868. // East
  869. <-c // zBlank
  870. l = <-c
  871. if i, e := strconv.Atoi(l.token); e != nil || l.err {
  872. return nil, &ParseError{f, "bad LOC Longitude", l}, ""
  873. } else {
  874. rr.Longitude = 1000 * 60 * 60 * uint32(i)
  875. }
  876. <-c // zBlank
  877. // Either number, 'E' or 'W'
  878. l = <-c
  879. if rr.Longitude, ok = locCheckEast(l.token, rr.Longitude); ok {
  880. goto Altitude
  881. }
  882. if i, e := strconv.Atoi(l.token); e != nil || l.err {
  883. return nil, &ParseError{f, "bad LOC Longitude minutes", l}, ""
  884. } else {
  885. rr.Longitude += 1000 * 60 * uint32(i)
  886. }
  887. <-c // zBlank
  888. l = <-c
  889. if i, e := strconv.ParseFloat(l.token, 32); e != nil || l.err {
  890. return nil, &ParseError{f, "bad LOC Longitude seconds", l}, ""
  891. } else {
  892. rr.Longitude += uint32(1000 * i)
  893. }
  894. <-c // zBlank
  895. // Either number, 'E' or 'W'
  896. l = <-c
  897. if rr.Longitude, ok = locCheckEast(l.token, rr.Longitude); ok {
  898. goto Altitude
  899. }
  900. // If still alive, flag an error
  901. return nil, &ParseError{f, "bad LOC Longitude East/West", l}, ""
  902. Altitude:
  903. <-c // zBlank
  904. l = <-c
  905. if l.length == 0 || l.err {
  906. return nil, &ParseError{f, "bad LOC Altitude", l}, ""
  907. }
  908. if l.token[len(l.token)-1] == 'M' || l.token[len(l.token)-1] == 'm' {
  909. l.token = l.token[0 : len(l.token)-1]
  910. }
  911. if i, e := strconv.ParseFloat(l.token, 32); e != nil {
  912. return nil, &ParseError{f, "bad LOC Altitude", l}, ""
  913. } else {
  914. rr.Altitude = uint32(i*100.0 + 10000000.0 + 0.5)
  915. }
  916. // And now optionally the other values
  917. l = <-c
  918. count := 0
  919. for l.value != zNewline && l.value != zEOF {
  920. switch l.value {
  921. case zString:
  922. switch count {
  923. case 0: // Size
  924. e, m, ok := stringToCm(l.token)
  925. if !ok {
  926. return nil, &ParseError{f, "bad LOC Size", l}, ""
  927. }
  928. rr.Size = (e & 0x0f) | (m << 4 & 0xf0)
  929. case 1: // HorizPre
  930. e, m, ok := stringToCm(l.token)
  931. if !ok {
  932. return nil, &ParseError{f, "bad LOC HorizPre", l}, ""
  933. }
  934. rr.HorizPre = (e & 0x0f) | (m << 4 & 0xf0)
  935. case 2: // VertPre
  936. e, m, ok := stringToCm(l.token)
  937. if !ok {
  938. return nil, &ParseError{f, "bad LOC VertPre", l}, ""
  939. }
  940. rr.VertPre = (e & 0x0f) | (m << 4 & 0xf0)
  941. }
  942. count++
  943. case zBlank:
  944. // Ok
  945. default:
  946. return nil, &ParseError{f, "bad LOC Size, HorizPre or VertPre", l}, ""
  947. }
  948. l = <-c
  949. }
  950. return rr, nil, ""
  951. }
  952. func setHIP(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  953. rr := new(HIP)
  954. rr.Hdr = h
  955. // HitLength is not represented
  956. l := <-c
  957. if l.length == 0 {
  958. return rr, nil, l.comment
  959. }
  960. i, e := strconv.Atoi(l.token)
  961. if e != nil || l.err {
  962. return nil, &ParseError{f, "bad HIP PublicKeyAlgorithm", l}, ""
  963. }
  964. rr.PublicKeyAlgorithm = uint8(i)
  965. <-c // zBlank
  966. l = <-c // zString
  967. if l.length == 0 || l.err {
  968. return nil, &ParseError{f, "bad HIP Hit", l}, ""
  969. }
  970. rr.Hit = l.token // This can not contain spaces, see RFC 5205 Section 6.
  971. rr.HitLength = uint8(len(rr.Hit)) / 2
  972. <-c // zBlank
  973. l = <-c // zString
  974. if l.length == 0 || l.err {
  975. return nil, &ParseError{f, "bad HIP PublicKey", l}, ""
  976. }
  977. rr.PublicKey = l.token // This cannot contain spaces
  978. rr.PublicKeyLength = uint16(base64.StdEncoding.DecodedLen(len(rr.PublicKey)))
  979. // RendezvousServers (if any)
  980. l = <-c
  981. var xs []string
  982. for l.value != zNewline && l.value != zEOF {
  983. switch l.value {
  984. case zString:
  985. if l.token == "@" {
  986. xs = append(xs, o)
  987. l = <-c
  988. continue
  989. }
  990. _, ok := IsDomainName(l.token)
  991. if !ok || l.length == 0 || l.err {
  992. return nil, &ParseError{f, "bad HIP RendezvousServers", l}, ""
  993. }
  994. if l.token[l.length-1] != '.' {
  995. l.token = appendOrigin(l.token, o)
  996. }
  997. xs = append(xs, l.token)
  998. case zBlank:
  999. // Ok
  1000. default:
  1001. return nil, &ParseError{f, "bad HIP RendezvousServers", l}, ""
  1002. }
  1003. l = <-c
  1004. }
  1005. rr.RendezvousServers = xs
  1006. return rr, nil, l.comment
  1007. }
  1008. func setCERT(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1009. rr := new(CERT)
  1010. rr.Hdr = h
  1011. l := <-c
  1012. if l.length == 0 {
  1013. return rr, nil, l.comment
  1014. }
  1015. if v, ok := StringToCertType[l.token]; ok {
  1016. rr.Type = v
  1017. } else if i, e := strconv.Atoi(l.token); e != nil {
  1018. return nil, &ParseError{f, "bad CERT Type", l}, ""
  1019. } else {
  1020. rr.Type = uint16(i)
  1021. }
  1022. <-c // zBlank
  1023. l = <-c // zString
  1024. i, e := strconv.Atoi(l.token)
  1025. if e != nil || l.err {
  1026. return nil, &ParseError{f, "bad CERT KeyTag", l}, ""
  1027. }
  1028. rr.KeyTag = uint16(i)
  1029. <-c // zBlank
  1030. l = <-c // zString
  1031. if v, ok := StringToAlgorithm[l.token]; ok {
  1032. rr.Algorithm = v
  1033. } else if i, e := strconv.Atoi(l.token); e != nil {
  1034. return nil, &ParseError{f, "bad CERT Algorithm", l}, ""
  1035. } else {
  1036. rr.Algorithm = uint8(i)
  1037. }
  1038. s, e1, c1 := endingToString(c, "bad CERT Certificate", f)
  1039. if e1 != nil {
  1040. return nil, e1, c1
  1041. }
  1042. rr.Certificate = s
  1043. return rr, nil, c1
  1044. }
  1045. func setOPENPGPKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1046. rr := new(OPENPGPKEY)
  1047. rr.Hdr = h
  1048. s, e, c1 := endingToString(c, "bad OPENPGPKEY PublicKey", f)
  1049. if e != nil {
  1050. return nil, e, c1
  1051. }
  1052. rr.PublicKey = s
  1053. return rr, nil, c1
  1054. }
  1055. func setSIG(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1056. r, e, s := setRRSIG(h, c, o, f)
  1057. if r != nil {
  1058. return &SIG{*r.(*RRSIG)}, e, s
  1059. }
  1060. return nil, e, s
  1061. }
  1062. func setRRSIG(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1063. rr := new(RRSIG)
  1064. rr.Hdr = h
  1065. l := <-c
  1066. if l.length == 0 {
  1067. return rr, nil, l.comment
  1068. }
  1069. if t, ok := StringToType[l.tokenUpper]; !ok {
  1070. if strings.HasPrefix(l.tokenUpper, "TYPE") {
  1071. t, ok = typeToInt(l.tokenUpper)
  1072. if !ok {
  1073. return nil, &ParseError{f, "bad RRSIG Typecovered", l}, ""
  1074. }
  1075. rr.TypeCovered = t
  1076. } else {
  1077. return nil, &ParseError{f, "bad RRSIG Typecovered", l}, ""
  1078. }
  1079. } else {
  1080. rr.TypeCovered = t
  1081. }
  1082. <-c // zBlank
  1083. l = <-c
  1084. i, err := strconv.Atoi(l.token)
  1085. if err != nil || l.err {
  1086. return nil, &ParseError{f, "bad RRSIG Algorithm", l}, ""
  1087. }
  1088. rr.Algorithm = uint8(i)
  1089. <-c // zBlank
  1090. l = <-c
  1091. i, err = strconv.Atoi(l.token)
  1092. if err != nil || l.err {
  1093. return nil, &ParseError{f, "bad RRSIG Labels", l}, ""
  1094. }
  1095. rr.Labels = uint8(i)
  1096. <-c // zBlank
  1097. l = <-c
  1098. i, err = strconv.Atoi(l.token)
  1099. if err != nil || l.err {
  1100. return nil, &ParseError{f, "bad RRSIG OrigTtl", l}, ""
  1101. }
  1102. rr.OrigTtl = uint32(i)
  1103. <-c // zBlank
  1104. l = <-c
  1105. if i, err := StringToTime(l.token); err != nil {
  1106. // Try to see if all numeric and use it as epoch
  1107. if i, err := strconv.ParseInt(l.token, 10, 64); err == nil {
  1108. // TODO(miek): error out on > MAX_UINT32, same below
  1109. rr.Expiration = uint32(i)
  1110. } else {
  1111. return nil, &ParseError{f, "bad RRSIG Expiration", l}, ""
  1112. }
  1113. } else {
  1114. rr.Expiration = i
  1115. }
  1116. <-c // zBlank
  1117. l = <-c
  1118. if i, err := StringToTime(l.token); err != nil {
  1119. if i, err := strconv.ParseInt(l.token, 10, 64); err == nil {
  1120. rr.Inception = uint32(i)
  1121. } else {
  1122. return nil, &ParseError{f, "bad RRSIG Inception", l}, ""
  1123. }
  1124. } else {
  1125. rr.Inception = i
  1126. }
  1127. <-c // zBlank
  1128. l = <-c
  1129. i, err = strconv.Atoi(l.token)
  1130. if err != nil || l.err {
  1131. return nil, &ParseError{f, "bad RRSIG KeyTag", l}, ""
  1132. }
  1133. rr.KeyTag = uint16(i)
  1134. <-c // zBlank
  1135. l = <-c
  1136. rr.SignerName = l.token
  1137. if l.token == "@" {
  1138. rr.SignerName = o
  1139. } else {
  1140. _, ok := IsDomainName(l.token)
  1141. if !ok || l.length == 0 || l.err {
  1142. return nil, &ParseError{f, "bad RRSIG SignerName", l}, ""
  1143. }
  1144. if rr.SignerName[l.length-1] != '.' {
  1145. rr.SignerName = appendOrigin(rr.SignerName, o)
  1146. }
  1147. }
  1148. s, e, c1 := endingToString(c, "bad RRSIG Signature", f)
  1149. if e != nil {
  1150. return nil, e, c1
  1151. }
  1152. rr.Signature = s
  1153. return rr, nil, c1
  1154. }
  1155. func setNSEC(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1156. rr := new(NSEC)
  1157. rr.Hdr = h
  1158. l := <-c
  1159. rr.NextDomain = l.token
  1160. if l.length == 0 {
  1161. return rr, nil, l.comment
  1162. }
  1163. if l.token == "@" {
  1164. rr.NextDomain = o
  1165. } else {
  1166. _, ok := IsDomainName(l.token)
  1167. if !ok || l.length == 0 || l.err {
  1168. return nil, &ParseError{f, "bad NSEC NextDomain", l}, ""
  1169. }
  1170. if rr.NextDomain[l.length-1] != '.' {
  1171. rr.NextDomain = appendOrigin(rr.NextDomain, o)
  1172. }
  1173. }
  1174. rr.TypeBitMap = make([]uint16, 0)
  1175. var (
  1176. k uint16
  1177. ok bool
  1178. )
  1179. l = <-c
  1180. for l.value != zNewline && l.value != zEOF {
  1181. switch l.value {
  1182. case zBlank:
  1183. // Ok
  1184. case zString:
  1185. if k, ok = StringToType[l.tokenUpper]; !ok {
  1186. if k, ok = typeToInt(l.tokenUpper); !ok {
  1187. return nil, &ParseError{f, "bad NSEC TypeBitMap", l}, ""
  1188. }
  1189. }
  1190. rr.TypeBitMap = append(rr.TypeBitMap, k)
  1191. default:
  1192. return nil, &ParseError{f, "bad NSEC TypeBitMap", l}, ""
  1193. }
  1194. l = <-c
  1195. }
  1196. return rr, nil, l.comment
  1197. }
  1198. func setNSEC3(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1199. rr := new(NSEC3)
  1200. rr.Hdr = h
  1201. l := <-c
  1202. if l.length == 0 {
  1203. return rr, nil, l.comment
  1204. }
  1205. i, e := strconv.Atoi(l.token)
  1206. if e != nil || l.err {
  1207. return nil, &ParseError{f, "bad NSEC3 Hash", l}, ""
  1208. }
  1209. rr.Hash = uint8(i)
  1210. <-c // zBlank
  1211. l = <-c
  1212. i, e = strconv.Atoi(l.token)
  1213. if e != nil || l.err {
  1214. return nil, &ParseError{f, "bad NSEC3 Flags", l}, ""
  1215. }
  1216. rr.Flags = uint8(i)
  1217. <-c // zBlank
  1218. l = <-c
  1219. i, e = strconv.Atoi(l.token)
  1220. if e != nil || l.err {
  1221. return nil, &ParseError{f, "bad NSEC3 Iterations", l}, ""
  1222. }
  1223. rr.Iterations = uint16(i)
  1224. <-c
  1225. l = <-c
  1226. if len(l.token) == 0 || l.err {
  1227. return nil, &ParseError{f, "bad NSEC3 Salt", l}, ""
  1228. }
  1229. rr.SaltLength = uint8(len(l.token)) / 2
  1230. rr.Salt = l.token
  1231. <-c
  1232. l = <-c
  1233. if len(l.token) == 0 || l.err {
  1234. return nil, &ParseError{f, "bad NSEC3 NextDomain", l}, ""
  1235. }
  1236. rr.HashLength = 20 // Fix for NSEC3 (sha1 160 bits)
  1237. rr.NextDomain = l.token
  1238. rr.TypeBitMap = make([]uint16, 0)
  1239. var (
  1240. k uint16
  1241. ok bool
  1242. )
  1243. l = <-c
  1244. for l.value != zNewline && l.value != zEOF {
  1245. switch l.value {
  1246. case zBlank:
  1247. // Ok
  1248. case zString:
  1249. if k, ok = StringToType[l.tokenUpper]; !ok {
  1250. if k, ok = typeToInt(l.tokenUpper); !ok {
  1251. return nil, &ParseError{f, "bad NSEC3 TypeBitMap", l}, ""
  1252. }
  1253. }
  1254. rr.TypeBitMap = append(rr.TypeBitMap, k)
  1255. default:
  1256. return nil, &ParseError{f, "bad NSEC3 TypeBitMap", l}, ""
  1257. }
  1258. l = <-c
  1259. }
  1260. return rr, nil, l.comment
  1261. }
  1262. func setNSEC3PARAM(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1263. rr := new(NSEC3PARAM)
  1264. rr.Hdr = h
  1265. l := <-c
  1266. if l.length == 0 {
  1267. return rr, nil, ""
  1268. }
  1269. i, e := strconv.Atoi(l.token)
  1270. if e != nil || l.err {
  1271. return nil, &ParseError{f, "bad NSEC3PARAM Hash", l}, ""
  1272. }
  1273. rr.Hash = uint8(i)
  1274. <-c // zBlank
  1275. l = <-c
  1276. i, e = strconv.Atoi(l.token)
  1277. if e != nil || l.err {
  1278. return nil, &ParseError{f, "bad NSEC3PARAM Flags", l}, ""
  1279. }
  1280. rr.Flags = uint8(i)
  1281. <-c // zBlank
  1282. l = <-c
  1283. i, e = strconv.Atoi(l.token)
  1284. if e != nil || l.err {
  1285. return nil, &ParseError{f, "bad NSEC3PARAM Iterations", l}, ""
  1286. }
  1287. rr.Iterations = uint16(i)
  1288. <-c
  1289. l = <-c
  1290. rr.SaltLength = uint8(len(l.token))
  1291. rr.Salt = l.token
  1292. return rr, nil, ""
  1293. }
  1294. func setEUI48(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1295. rr := new(EUI48)
  1296. rr.Hdr = h
  1297. l := <-c
  1298. if l.length == 0 {
  1299. return rr, nil, ""
  1300. }
  1301. if l.length != 17 || l.err {
  1302. return nil, &ParseError{f, "bad EUI48 Address", l}, ""
  1303. }
  1304. addr := make([]byte, 12)
  1305. dash := 0
  1306. for i := 0; i < 10; i += 2 {
  1307. addr[i] = l.token[i+dash]
  1308. addr[i+1] = l.token[i+1+dash]
  1309. dash++
  1310. if l.token[i+1+dash] != '-' {
  1311. return nil, &ParseError{f, "bad EUI48 Address", l}, ""
  1312. }
  1313. }
  1314. addr[10] = l.token[15]
  1315. addr[11] = l.token[16]
  1316. i, e := strconv.ParseUint(string(addr), 16, 48)
  1317. if e != nil {
  1318. return nil, &ParseError{f, "bad EUI48 Address", l}, ""
  1319. }
  1320. rr.Address = i
  1321. return rr, nil, ""
  1322. }
  1323. func setEUI64(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1324. rr := new(EUI64)
  1325. rr.Hdr = h
  1326. l := <-c
  1327. if l.length == 0 {
  1328. return rr, nil, ""
  1329. }
  1330. if l.length != 23 || l.err {
  1331. return nil, &ParseError{f, "bad EUI64 Address", l}, ""
  1332. }
  1333. addr := make([]byte, 16)
  1334. dash := 0
  1335. for i := 0; i < 14; i += 2 {
  1336. addr[i] = l.token[i+dash]
  1337. addr[i+1] = l.token[i+1+dash]
  1338. dash++
  1339. if l.token[i+1+dash] != '-' {
  1340. return nil, &ParseError{f, "bad EUI64 Address", l}, ""
  1341. }
  1342. }
  1343. addr[14] = l.token[21]
  1344. addr[15] = l.token[22]
  1345. i, e := strconv.ParseUint(string(addr), 16, 64)
  1346. if e != nil {
  1347. return nil, &ParseError{f, "bad EUI68 Address", l}, ""
  1348. }
  1349. rr.Address = uint64(i)
  1350. return rr, nil, ""
  1351. }
  1352. func setWKS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1353. rr := new(WKS)
  1354. rr.Hdr = h
  1355. l := <-c
  1356. if l.length == 0 {
  1357. return rr, nil, l.comment
  1358. }
  1359. rr.Address = net.ParseIP(l.token)
  1360. if rr.Address == nil || l.err {
  1361. return nil, &ParseError{f, "bad WKS Address", l}, ""
  1362. }
  1363. <-c // zBlank
  1364. l = <-c
  1365. proto := "tcp"
  1366. i, e := strconv.Atoi(l.token)
  1367. if e != nil || l.err {
  1368. return nil, &ParseError{f, "bad WKS Protocol", l}, ""
  1369. }
  1370. rr.Protocol = uint8(i)
  1371. switch rr.Protocol {
  1372. case 17:
  1373. proto = "udp"
  1374. case 6:
  1375. proto = "tcp"
  1376. default:
  1377. return nil, &ParseError{f, "bad WKS Protocol", l}, ""
  1378. }
  1379. <-c
  1380. l = <-c
  1381. rr.BitMap = make([]uint16, 0)
  1382. var (
  1383. k int
  1384. err error
  1385. )
  1386. for l.value != zNewline && l.value != zEOF {
  1387. switch l.value {
  1388. case zBlank:
  1389. // Ok
  1390. case zString:
  1391. if k, err = net.LookupPort(proto, l.token); err != nil {
  1392. i, e := strconv.Atoi(l.token) // If a number use that
  1393. if e != nil {
  1394. return nil, &ParseError{f, "bad WKS BitMap", l}, ""
  1395. }
  1396. rr.BitMap = append(rr.BitMap, uint16(i))
  1397. }
  1398. rr.BitMap = append(rr.BitMap, uint16(k))
  1399. default:
  1400. return nil, &ParseError{f, "bad WKS BitMap", l}, ""
  1401. }
  1402. l = <-c
  1403. }
  1404. return rr, nil, l.comment
  1405. }
  1406. func setSSHFP(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1407. rr := new(SSHFP)
  1408. rr.Hdr = h
  1409. l := <-c
  1410. if l.length == 0 {
  1411. return rr, nil, ""
  1412. }
  1413. i, e := strconv.Atoi(l.token)
  1414. if e != nil || l.err {
  1415. return nil, &ParseError{f, "bad SSHFP Algorithm", l}, ""
  1416. }
  1417. rr.Algorithm = uint8(i)
  1418. <-c // zBlank
  1419. l = <-c
  1420. i, e = strconv.Atoi(l.token)
  1421. if e != nil || l.err {
  1422. return nil, &ParseError{f, "bad SSHFP Type", l}, ""
  1423. }
  1424. rr.Type = uint8(i)
  1425. <-c // zBlank
  1426. s, e1, c1 := endingToString(c, "bad SSHFP Fingerprint", f)
  1427. if e1 != nil {
  1428. return nil, e1, c1
  1429. }
  1430. rr.FingerPrint = s
  1431. return rr, nil, ""
  1432. }
  1433. func setDNSKEYs(h RR_Header, c chan lex, o, f, typ string) (RR, *ParseError, string) {
  1434. rr := new(DNSKEY)
  1435. rr.Hdr = h
  1436. l := <-c
  1437. if l.length == 0 {
  1438. return rr, nil, l.comment
  1439. }
  1440. i, e := strconv.Atoi(l.token)
  1441. if e != nil || l.err {
  1442. return nil, &ParseError{f, "bad " + typ + " Flags", l}, ""
  1443. }
  1444. rr.Flags = uint16(i)
  1445. <-c // zBlank
  1446. l = <-c // zString
  1447. i, e = strconv.Atoi(l.token)
  1448. if e != nil || l.err {
  1449. return nil, &ParseError{f, "bad " + typ + " Protocol", l}, ""
  1450. }
  1451. rr.Protocol = uint8(i)
  1452. <-c // zBlank
  1453. l = <-c // zString
  1454. i, e = strconv.Atoi(l.token)
  1455. if e != nil || l.err {
  1456. return nil, &ParseError{f, "bad " + typ + " Algorithm", l}, ""
  1457. }
  1458. rr.Algorithm = uint8(i)
  1459. s, e1, c1 := endingToString(c, "bad "+typ+" PublicKey", f)
  1460. if e1 != nil {
  1461. return nil, e1, c1
  1462. }
  1463. rr.PublicKey = s
  1464. return rr, nil, c1
  1465. }
  1466. func setKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1467. r, e, s := setDNSKEYs(h, c, o, f, "KEY")
  1468. if r != nil {
  1469. return &KEY{*r.(*DNSKEY)}, e, s
  1470. }
  1471. return nil, e, s
  1472. }
  1473. func setDNSKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1474. r, e, s := setDNSKEYs(h, c, o, f, "DNSKEY")
  1475. return r, e, s
  1476. }
  1477. func setCDNSKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1478. r, e, s := setDNSKEYs(h, c, o, f, "CDNSKEY")
  1479. if r != nil {
  1480. return &CDNSKEY{*r.(*DNSKEY)}, e, s
  1481. }
  1482. return nil, e, s
  1483. }
  1484. func setRKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1485. rr := new(RKEY)
  1486. rr.Hdr = h
  1487. l := <-c
  1488. if l.length == 0 {
  1489. return rr, nil, l.comment
  1490. }
  1491. i, e := strconv.Atoi(l.token)
  1492. if e != nil || l.err {
  1493. return nil, &ParseError{f, "bad RKEY Flags", l}, ""
  1494. }
  1495. rr.Flags = uint16(i)
  1496. <-c // zBlank
  1497. l = <-c // zString
  1498. i, e = strconv.Atoi(l.token)
  1499. if e != nil || l.err {
  1500. return nil, &ParseError{f, "bad RKEY Protocol", l}, ""
  1501. }
  1502. rr.Protocol = uint8(i)
  1503. <-c // zBlank
  1504. l = <-c // zString
  1505. i, e = strconv.Atoi(l.token)
  1506. if e != nil || l.err {
  1507. return nil, &ParseError{f, "bad RKEY Algorithm", l}, ""
  1508. }
  1509. rr.Algorithm = uint8(i)
  1510. s, e1, c1 := endingToString(c, "bad RKEY PublicKey", f)
  1511. if e1 != nil {
  1512. return nil, e1, c1
  1513. }
  1514. rr.PublicKey = s
  1515. return rr, nil, c1
  1516. }
  1517. func setEID(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1518. rr := new(EID)
  1519. rr.Hdr = h
  1520. s, e, c1 := endingToString(c, "bad EID Endpoint", f)
  1521. if e != nil {
  1522. return nil, e, c1
  1523. }
  1524. rr.Endpoint = s
  1525. return rr, nil, c1
  1526. }
  1527. func setNIMLOC(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1528. rr := new(NIMLOC)
  1529. rr.Hdr = h
  1530. s, e, c1 := endingToString(c, "bad NIMLOC Locator", f)
  1531. if e != nil {
  1532. return nil, e, c1
  1533. }
  1534. rr.Locator = s
  1535. return rr, nil, c1
  1536. }
  1537. func setGPOS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1538. rr := new(GPOS)
  1539. rr.Hdr = h
  1540. l := <-c
  1541. if l.length == 0 {
  1542. return rr, nil, ""
  1543. }
  1544. _, e := strconv.ParseFloat(l.token, 64)
  1545. if e != nil || l.err {
  1546. return nil, &ParseError{f, "bad GPOS Longitude", l}, ""
  1547. }
  1548. rr.Longitude = l.token
  1549. <-c // zBlank
  1550. l = <-c
  1551. _, e = strconv.ParseFloat(l.token, 64)
  1552. if e != nil || l.err {
  1553. return nil, &ParseError{f, "bad GPOS Latitude", l}, ""
  1554. }
  1555. rr.Latitude = l.token
  1556. <-c // zBlank
  1557. l = <-c
  1558. _, e = strconv.ParseFloat(l.token, 64)
  1559. if e != nil || l.err {
  1560. return nil, &ParseError{f, "bad GPOS Altitude", l}, ""
  1561. }
  1562. rr.Altitude = l.token
  1563. return rr, nil, ""
  1564. }
  1565. func setDSs(h RR_Header, c chan lex, o, f, typ string) (RR, *ParseError, string) {
  1566. rr := new(DS)
  1567. rr.Hdr = h
  1568. l := <-c
  1569. if l.length == 0 {
  1570. return rr, nil, l.comment
  1571. }
  1572. i, e := strconv.Atoi(l.token)
  1573. if e != nil || l.err {
  1574. return nil, &ParseError{f, "bad " + typ + " KeyTag", l}, ""
  1575. }
  1576. rr.KeyTag = uint16(i)
  1577. <-c // zBlank
  1578. l = <-c
  1579. if i, e := strconv.Atoi(l.token); e != nil {
  1580. i, ok := StringToAlgorithm[l.tokenUpper]
  1581. if !ok || l.err {
  1582. return nil, &ParseError{f, "bad " + typ + " Algorithm", l}, ""
  1583. }
  1584. rr.Algorithm = i
  1585. } else {
  1586. rr.Algorithm = uint8(i)
  1587. }
  1588. <-c // zBlank
  1589. l = <-c
  1590. i, e = strconv.Atoi(l.token)
  1591. if e != nil || l.err {
  1592. return nil, &ParseError{f, "bad " + typ + " DigestType", l}, ""
  1593. }
  1594. rr.DigestType = uint8(i)
  1595. s, e1, c1 := endingToString(c, "bad "+typ+" Digest", f)
  1596. if e1 != nil {
  1597. return nil, e1, c1
  1598. }
  1599. rr.Digest = s
  1600. return rr, nil, c1
  1601. }
  1602. func setDS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1603. r, e, s := setDSs(h, c, o, f, "DS")
  1604. return r, e, s
  1605. }
  1606. func setDLV(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1607. r, e, s := setDSs(h, c, o, f, "DLV")
  1608. if r != nil {
  1609. return &DLV{*r.(*DS)}, e, s
  1610. }
  1611. return nil, e, s
  1612. }
  1613. func setCDS(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1614. r, e, s := setDSs(h, c, o, f, "CDS")
  1615. if r != nil {
  1616. return &CDS{*r.(*DS)}, e, s
  1617. }
  1618. return nil, e, s
  1619. }
  1620. func setTA(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1621. rr := new(TA)
  1622. rr.Hdr = h
  1623. l := <-c
  1624. if l.length == 0 {
  1625. return rr, nil, l.comment
  1626. }
  1627. i, e := strconv.Atoi(l.token)
  1628. if e != nil || l.err {
  1629. return nil, &ParseError{f, "bad TA KeyTag", l}, ""
  1630. }
  1631. rr.KeyTag = uint16(i)
  1632. <-c // zBlank
  1633. l = <-c
  1634. if i, e := strconv.Atoi(l.token); e != nil {
  1635. i, ok := StringToAlgorithm[l.tokenUpper]
  1636. if !ok || l.err {
  1637. return nil, &ParseError{f, "bad TA Algorithm", l}, ""
  1638. }
  1639. rr.Algorithm = i
  1640. } else {
  1641. rr.Algorithm = uint8(i)
  1642. }
  1643. <-c // zBlank
  1644. l = <-c
  1645. i, e = strconv.Atoi(l.token)
  1646. if e != nil || l.err {
  1647. return nil, &ParseError{f, "bad TA DigestType", l}, ""
  1648. }
  1649. rr.DigestType = uint8(i)
  1650. s, e, c1 := endingToString(c, "bad TA Digest", f)
  1651. if e != nil {
  1652. return nil, e.(*ParseError), c1
  1653. }
  1654. rr.Digest = s
  1655. return rr, nil, c1
  1656. }
  1657. func setTLSA(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1658. rr := new(TLSA)
  1659. rr.Hdr = h
  1660. l := <-c
  1661. if l.length == 0 {
  1662. return rr, nil, l.comment
  1663. }
  1664. i, e := strconv.Atoi(l.token)
  1665. if e != nil || l.err {
  1666. return nil, &ParseError{f, "bad TLSA Usage", l}, ""
  1667. }
  1668. rr.Usage = uint8(i)
  1669. <-c // zBlank
  1670. l = <-c
  1671. i, e = strconv.Atoi(l.token)
  1672. if e != nil || l.err {
  1673. return nil, &ParseError{f, "bad TLSA Selector", l}, ""
  1674. }
  1675. rr.Selector = uint8(i)
  1676. <-c // zBlank
  1677. l = <-c
  1678. i, e = strconv.Atoi(l.token)
  1679. if e != nil || l.err {
  1680. return nil, &ParseError{f, "bad TLSA MatchingType", l}, ""
  1681. }
  1682. rr.MatchingType = uint8(i)
  1683. // So this needs be e2 (i.e. different than e), because...??t
  1684. s, e2, c1 := endingToString(c, "bad TLSA Certificate", f)
  1685. if e2 != nil {
  1686. return nil, e2, c1
  1687. }
  1688. rr.Certificate = s
  1689. return rr, nil, c1
  1690. }
  1691. func setRFC3597(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1692. rr := new(RFC3597)
  1693. rr.Hdr = h
  1694. l := <-c
  1695. if l.token != "\\#" {
  1696. return nil, &ParseError{f, "bad RFC3597 Rdata", l}, ""
  1697. }
  1698. <-c // zBlank
  1699. l = <-c
  1700. rdlength, e := strconv.Atoi(l.token)
  1701. if e != nil || l.err {
  1702. return nil, &ParseError{f, "bad RFC3597 Rdata ", l}, ""
  1703. }
  1704. s, e1, c1 := endingToString(c, "bad RFC3597 Rdata", f)
  1705. if e1 != nil {
  1706. return nil, e1, c1
  1707. }
  1708. if rdlength*2 != len(s) {
  1709. return nil, &ParseError{f, "bad RFC3597 Rdata", l}, ""
  1710. }
  1711. rr.Rdata = s
  1712. return rr, nil, c1
  1713. }
  1714. func setSPF(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1715. rr := new(SPF)
  1716. rr.Hdr = h
  1717. s, e, c1 := endingToTxtSlice(c, "bad SPF Txt", f)
  1718. if e != nil {
  1719. return nil, e, ""
  1720. }
  1721. rr.Txt = s
  1722. return rr, nil, c1
  1723. }
  1724. func setTXT(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1725. rr := new(TXT)
  1726. rr.Hdr = h
  1727. // no zBlank reading here, because all this rdata is TXT
  1728. s, e, c1 := endingToTxtSlice(c, "bad TXT Txt", f)
  1729. if e != nil {
  1730. return nil, e, ""
  1731. }
  1732. rr.Txt = s
  1733. return rr, nil, c1
  1734. }
  1735. // identical to setTXT
  1736. func setNINFO(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1737. rr := new(NINFO)
  1738. rr.Hdr = h
  1739. s, e, c1 := endingToTxtSlice(c, "bad NINFO ZSData", f)
  1740. if e != nil {
  1741. return nil, e, ""
  1742. }
  1743. rr.ZSData = s
  1744. return rr, nil, c1
  1745. }
  1746. func setURI(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1747. rr := new(URI)
  1748. rr.Hdr = h
  1749. l := <-c
  1750. if l.length == 0 { // Dynamic updates.
  1751. return rr, nil, ""
  1752. }
  1753. i, e := strconv.Atoi(l.token)
  1754. if e != nil || l.err {
  1755. return nil, &ParseError{f, "bad URI Priority", l}, ""
  1756. }
  1757. rr.Priority = uint16(i)
  1758. <-c // zBlank
  1759. l = <-c
  1760. i, e = strconv.Atoi(l.token)
  1761. if e != nil || l.err {
  1762. return nil, &ParseError{f, "bad URI Weight", l}, ""
  1763. }
  1764. rr.Weight = uint16(i)
  1765. <-c // zBlank
  1766. s, err, c1 := endingToTxtSlice(c, "bad URI Target", f)
  1767. if err != nil {
  1768. return nil, err, ""
  1769. }
  1770. if len(s) > 1 {
  1771. return nil, &ParseError{f, "bad URI Target", l}, ""
  1772. }
  1773. rr.Target = s[0]
  1774. return rr, nil, c1
  1775. }
  1776. func setDHCID(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1777. // awesome record to parse!
  1778. rr := new(DHCID)
  1779. rr.Hdr = h
  1780. s, e, c1 := endingToString(c, "bad DHCID Digest", f)
  1781. if e != nil {
  1782. return nil, e, c1
  1783. }
  1784. rr.Digest = s
  1785. return rr, nil, c1
  1786. }
  1787. func setNID(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1788. rr := new(NID)
  1789. rr.Hdr = h
  1790. l := <-c
  1791. if l.length == 0 {
  1792. return rr, nil, ""
  1793. }
  1794. i, e := strconv.Atoi(l.token)
  1795. if e != nil || l.err {
  1796. return nil, &ParseError{f, "bad NID Preference", l}, ""
  1797. }
  1798. rr.Preference = uint16(i)
  1799. <-c // zBlank
  1800. l = <-c // zString
  1801. u, err := stringToNodeID(l)
  1802. if err != nil || l.err {
  1803. return nil, err, ""
  1804. }
  1805. rr.NodeID = u
  1806. return rr, nil, ""
  1807. }
  1808. func setL32(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1809. rr := new(L32)
  1810. rr.Hdr = h
  1811. l := <-c
  1812. if l.length == 0 {
  1813. return rr, nil, ""
  1814. }
  1815. i, e := strconv.Atoi(l.token)
  1816. if e != nil || l.err {
  1817. return nil, &ParseError{f, "bad L32 Preference", l}, ""
  1818. }
  1819. rr.Preference = uint16(i)
  1820. <-c // zBlank
  1821. l = <-c // zString
  1822. rr.Locator32 = net.ParseIP(l.token)
  1823. if rr.Locator32 == nil || l.err {
  1824. return nil, &ParseError{f, "bad L32 Locator", l}, ""
  1825. }
  1826. return rr, nil, ""
  1827. }
  1828. func setLP(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1829. rr := new(LP)
  1830. rr.Hdr = h
  1831. l := <-c
  1832. if l.length == 0 {
  1833. return rr, nil, ""
  1834. }
  1835. i, e := strconv.Atoi(l.token)
  1836. if e != nil || l.err {
  1837. return nil, &ParseError{f, "bad LP Preference", l}, ""
  1838. }
  1839. rr.Preference = uint16(i)
  1840. <-c // zBlank
  1841. l = <-c // zString
  1842. rr.Fqdn = l.token
  1843. if l.length == 0 {
  1844. return rr, nil, ""
  1845. }
  1846. if l.token == "@" {
  1847. rr.Fqdn = o
  1848. return rr, nil, ""
  1849. }
  1850. _, ok := IsDomainName(l.token)
  1851. if !ok || l.length == 0 || l.err {
  1852. return nil, &ParseError{f, "bad LP Fqdn", l}, ""
  1853. }
  1854. if rr.Fqdn[l.length-1] != '.' {
  1855. rr.Fqdn = appendOrigin(rr.Fqdn, o)
  1856. }
  1857. return rr, nil, ""
  1858. }
  1859. func setL64(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1860. rr := new(L64)
  1861. rr.Hdr = h
  1862. l := <-c
  1863. if l.length == 0 {
  1864. return rr, nil, ""
  1865. }
  1866. i, e := strconv.Atoi(l.token)
  1867. if e != nil || l.err {
  1868. return nil, &ParseError{f, "bad L64 Preference", l}, ""
  1869. }
  1870. rr.Preference = uint16(i)
  1871. <-c // zBlank
  1872. l = <-c // zString
  1873. u, err := stringToNodeID(l)
  1874. if err != nil || l.err {
  1875. return nil, err, ""
  1876. }
  1877. rr.Locator64 = u
  1878. return rr, nil, ""
  1879. }
  1880. func setUID(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1881. rr := new(UID)
  1882. rr.Hdr = h
  1883. l := <-c
  1884. if l.length == 0 {
  1885. return rr, nil, ""
  1886. }
  1887. i, e := strconv.Atoi(l.token)
  1888. if e != nil || l.err {
  1889. return nil, &ParseError{f, "bad UID Uid", l}, ""
  1890. }
  1891. rr.Uid = uint32(i)
  1892. return rr, nil, ""
  1893. }
  1894. func setGID(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1895. rr := new(GID)
  1896. rr.Hdr = h
  1897. l := <-c
  1898. if l.length == 0 {
  1899. return rr, nil, ""
  1900. }
  1901. i, e := strconv.Atoi(l.token)
  1902. if e != nil || l.err {
  1903. return nil, &ParseError{f, "bad GID Gid", l}, ""
  1904. }
  1905. rr.Gid = uint32(i)
  1906. return rr, nil, ""
  1907. }
  1908. func setUINFO(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1909. rr := new(UINFO)
  1910. rr.Hdr = h
  1911. s, e, c1 := endingToTxtSlice(c, "bad UINFO Uinfo", f)
  1912. if e != nil {
  1913. return nil, e, ""
  1914. }
  1915. rr.Uinfo = s[0] // silently discard anything above
  1916. return rr, nil, c1
  1917. }
  1918. func setPX(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1919. rr := new(PX)
  1920. rr.Hdr = h
  1921. l := <-c
  1922. if l.length == 0 {
  1923. return rr, nil, ""
  1924. }
  1925. i, e := strconv.Atoi(l.token)
  1926. if e != nil || l.err {
  1927. return nil, &ParseError{f, "bad PX Preference", l}, ""
  1928. }
  1929. rr.Preference = uint16(i)
  1930. <-c // zBlank
  1931. l = <-c // zString
  1932. rr.Map822 = l.token
  1933. if l.length == 0 {
  1934. return rr, nil, ""
  1935. }
  1936. if l.token == "@" {
  1937. rr.Map822 = o
  1938. return rr, nil, ""
  1939. }
  1940. _, ok := IsDomainName(l.token)
  1941. if !ok || l.length == 0 || l.err {
  1942. return nil, &ParseError{f, "bad PX Map822", l}, ""
  1943. }
  1944. if rr.Map822[l.length-1] != '.' {
  1945. rr.Map822 = appendOrigin(rr.Map822, o)
  1946. }
  1947. <-c // zBlank
  1948. l = <-c // zString
  1949. rr.Mapx400 = l.token
  1950. if l.token == "@" {
  1951. rr.Mapx400 = o
  1952. return rr, nil, ""
  1953. }
  1954. _, ok = IsDomainName(l.token)
  1955. if !ok || l.length == 0 || l.err {
  1956. return nil, &ParseError{f, "bad PX Mapx400", l}, ""
  1957. }
  1958. if rr.Mapx400[l.length-1] != '.' {
  1959. rr.Mapx400 = appendOrigin(rr.Mapx400, o)
  1960. }
  1961. return rr, nil, ""
  1962. }
  1963. func setIPSECKEY(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  1964. rr := new(IPSECKEY)
  1965. rr.Hdr = h
  1966. l := <-c
  1967. if l.length == 0 {
  1968. return rr, nil, l.comment
  1969. }
  1970. i, err := strconv.Atoi(l.token)
  1971. if err != nil || l.err {
  1972. return nil, &ParseError{f, "bad IPSECKEY Precedence", l}, ""
  1973. }
  1974. rr.Precedence = uint8(i)
  1975. <-c // zBlank
  1976. l = <-c
  1977. i, err = strconv.Atoi(l.token)
  1978. if err != nil || l.err {
  1979. return nil, &ParseError{f, "bad IPSECKEY GatewayType", l}, ""
  1980. }
  1981. rr.GatewayType = uint8(i)
  1982. <-c // zBlank
  1983. l = <-c
  1984. i, err = strconv.Atoi(l.token)
  1985. if err != nil || l.err {
  1986. return nil, &ParseError{f, "bad IPSECKEY Algorithm", l}, ""
  1987. }
  1988. rr.Algorithm = uint8(i)
  1989. // Now according to GatewayType we can have different elements here
  1990. <-c // zBlank
  1991. l = <-c
  1992. switch rr.GatewayType {
  1993. case 0:
  1994. fallthrough
  1995. case 3:
  1996. rr.GatewayName = l.token
  1997. if l.token == "@" {
  1998. rr.GatewayName = o
  1999. }
  2000. _, ok := IsDomainName(l.token)
  2001. if !ok || l.length == 0 || l.err {
  2002. return nil, &ParseError{f, "bad IPSECKEY GatewayName", l}, ""
  2003. }
  2004. if rr.GatewayName[l.length-1] != '.' {
  2005. rr.GatewayName = appendOrigin(rr.GatewayName, o)
  2006. }
  2007. case 1:
  2008. rr.GatewayA = net.ParseIP(l.token)
  2009. if rr.GatewayA == nil {
  2010. return nil, &ParseError{f, "bad IPSECKEY GatewayA", l}, ""
  2011. }
  2012. case 2:
  2013. rr.GatewayAAAA = net.ParseIP(l.token)
  2014. if rr.GatewayAAAA == nil {
  2015. return nil, &ParseError{f, "bad IPSECKEY GatewayAAAA", l}, ""
  2016. }
  2017. default:
  2018. return nil, &ParseError{f, "bad IPSECKEY GatewayType", l}, ""
  2019. }
  2020. s, e, c1 := endingToString(c, "bad IPSECKEY PublicKey", f)
  2021. if e != nil {
  2022. return nil, e, c1
  2023. }
  2024. rr.PublicKey = s
  2025. return rr, nil, c1
  2026. }
  2027. func setCAA(h RR_Header, c chan lex, o, f string) (RR, *ParseError, string) {
  2028. rr := new(CAA)
  2029. rr.Hdr = h
  2030. l := <-c
  2031. if l.length == 0 {
  2032. return rr, nil, l.comment
  2033. }
  2034. i, err := strconv.Atoi(l.token)
  2035. if err != nil || l.err {
  2036. return nil, &ParseError{f, "bad CAA Flag", l}, ""
  2037. }
  2038. rr.Flag = uint8(i)
  2039. <-c // zBlank
  2040. l = <-c // zString
  2041. if l.value != zString {
  2042. return nil, &ParseError{f, "bad CAA Tag", l}, ""
  2043. }
  2044. rr.Tag = l.token
  2045. <-c // zBlank
  2046. s, e, c1 := endingToTxtSlice(c, "bad CAA Value", f)
  2047. if e != nil {
  2048. return nil, e, ""
  2049. }
  2050. if len(s) > 1 {
  2051. return nil, &ParseError{f, "bad CAA Value", l}, ""
  2052. }
  2053. rr.Value = s[0]
  2054. return rr, nil, c1
  2055. }
  2056. var typeToparserFunc = map[uint16]parserFunc{
  2057. TypeAAAA: {setAAAA, false},
  2058. TypeAFSDB: {setAFSDB, false},
  2059. TypeA: {setA, false},
  2060. TypeCAA: {setCAA, true},
  2061. TypeCDS: {setCDS, true},
  2062. TypeCDNSKEY: {setCDNSKEY, true},
  2063. TypeCERT: {setCERT, true},
  2064. TypeCNAME: {setCNAME, false},
  2065. TypeDHCID: {setDHCID, true},
  2066. TypeDLV: {setDLV, true},
  2067. TypeDNAME: {setDNAME, false},
  2068. TypeKEY: {setKEY, true},
  2069. TypeDNSKEY: {setDNSKEY, true},
  2070. TypeDS: {setDS, true},
  2071. TypeEID: {setEID, true},
  2072. TypeEUI48: {setEUI48, false},
  2073. TypeEUI64: {setEUI64, false},
  2074. TypeGID: {setGID, false},
  2075. TypeGPOS: {setGPOS, false},
  2076. TypeHINFO: {setHINFO, true},
  2077. TypeHIP: {setHIP, true},
  2078. TypeIPSECKEY: {setIPSECKEY, true},
  2079. TypeKX: {setKX, false},
  2080. TypeL32: {setL32, false},
  2081. TypeL64: {setL64, false},
  2082. TypeLOC: {setLOC, true},
  2083. TypeLP: {setLP, false},
  2084. TypeMB: {setMB, false},
  2085. TypeMD: {setMD, false},
  2086. TypeMF: {setMF, false},
  2087. TypeMG: {setMG, false},
  2088. TypeMINFO: {setMINFO, false},
  2089. TypeMR: {setMR, false},
  2090. TypeMX: {setMX, false},
  2091. TypeNAPTR: {setNAPTR, false},
  2092. TypeNID: {setNID, false},
  2093. TypeNIMLOC: {setNIMLOC, true},
  2094. TypeNINFO: {setNINFO, true},
  2095. TypeNSAPPTR: {setNSAPPTR, false},
  2096. TypeNSEC3PARAM: {setNSEC3PARAM, false},
  2097. TypeNSEC3: {setNSEC3, true},
  2098. TypeNSEC: {setNSEC, true},
  2099. TypeNS: {setNS, false},
  2100. TypeOPENPGPKEY: {setOPENPGPKEY, true},
  2101. TypePTR: {setPTR, false},
  2102. TypePX: {setPX, false},
  2103. TypeSIG: {setSIG, true},
  2104. TypeRKEY: {setRKEY, true},
  2105. TypeRP: {setRP, false},
  2106. TypeRRSIG: {setRRSIG, true},
  2107. TypeRT: {setRT, false},
  2108. TypeSOA: {setSOA, false},
  2109. TypeSPF: {setSPF, true},
  2110. TypeSRV: {setSRV, false},
  2111. TypeSSHFP: {setSSHFP, true},
  2112. TypeTALINK: {setTALINK, false},
  2113. TypeTA: {setTA, true},
  2114. TypeTLSA: {setTLSA, true},
  2115. TypeTXT: {setTXT, true},
  2116. TypeUID: {setUID, false},
  2117. TypeUINFO: {setUINFO, true},
  2118. TypeURI: {setURI, true},
  2119. TypeWKS: {setWKS, true},
  2120. TypeX25: {setX25, false},
  2121. }