@nictool/dns-resource-record 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/rr.js CHANGED
@@ -1,3 +1,5 @@
1
+ import * as TINYDNS from './lib/tinydns.js'
2
+
1
3
  export default class RR extends Map {
2
4
  constructor(opts) {
3
5
  super()
@@ -184,6 +186,10 @@ export default class RR extends Map {
184
186
  return []
185
187
  }
186
188
 
189
+ getTags() {
190
+ return []
191
+ }
192
+
187
193
  getFields(arg) {
188
194
  const commonFields = ['owner', 'ttl', 'class', 'type']
189
195
  Object.freeze(commonFields)
@@ -352,6 +358,35 @@ export default class RR extends Map {
352
358
  return `${head}::${tail}`
353
359
  }
354
360
 
361
+ octalToBuffer(octalStr) {
362
+ return Buffer.from(TINYDNS.octalToChar(octalStr), 'binary')
363
+ }
364
+
365
+ wirePackDomain(fqdn) {
366
+ return packDomainNameWire(fqdn)
367
+ }
368
+
369
+ getWireRdata() {
370
+ const line = this.toTinydns()
371
+ if (!line.startsWith(':'))
372
+ throw new Error(`${this.get('type')}: override getWireRdata() — non-generic tinydns format`)
373
+ // line: :fqdn:typeId:rdata:ttl:ts:loc\n
374
+ const rdata = line.split(':')[3]
375
+ return this.octalToBuffer(rdata ?? '')
376
+ }
377
+
378
+ toWire() {
379
+ const rdata = this.getWireRdata()
380
+ const owner = this.wirePackDomain(this.get('owner'))
381
+ const classMap = { IN: 1, CS: 2, CH: 3, HS: 4, NONE: 254, ANY: 255 }
382
+ const meta = Buffer.alloc(10)
383
+ meta.writeUInt16BE(this.getTypeId(), 0)
384
+ meta.writeUInt16BE(classMap[this.get('class')] ?? 1, 2)
385
+ meta.writeUInt32BE(this.get('ttl'), 4)
386
+ meta.writeUInt16BE(rdata.length, 8)
387
+ return Buffer.concat([owner, meta, rdata])
388
+ }
389
+
355
390
  toBind(zone_opts) {
356
391
  return `${this.getPrefix(zone_opts)}\t${this.getRdataFields()
357
392
  .map((f) => this.getQuoted(f))
@@ -376,3 +411,25 @@ export default class RR extends Map {
376
411
  .join(' ')}' ~\n`
377
412
  }
378
413
  }
414
+
415
+ function packDomainNameWire(fqdn) {
416
+ if (fqdn === '.') return Buffer.from([0])
417
+ const parts = fqdn.split('.')
418
+ let len = 0
419
+ for (const part of parts) {
420
+ if (part.length > 0) len += part.length + 1
421
+ }
422
+ len += 1 // for the final \0
423
+
424
+ const buf = Buffer.allocUnsafe(len)
425
+ let offset = 0
426
+ for (const part of parts) {
427
+ if (part.length > 0) {
428
+ buf.writeUInt8(part.length, offset++)
429
+ buf.write(part, offset, 'ascii')
430
+ offset += part.length
431
+ }
432
+ }
433
+ buf.writeUInt8(0, offset)
434
+ return buf
435
+ }