scan_rr.go 49 KB

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