UNPKG

dns-packet-typescript

Version:

An abstract-encoding compliant module for encoding / decoding DNS packets

60 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.encodingLength = exports.decode = exports.encode = void 0; var opcodes = require("./opcodes"); var rcodes = require("./rcodes"); var QUERY_FLAG = 0; var RESPONSE_FLAG = 1 << 15; function encode(h, buf, offset) { var _a, _b; if (offset === void 0) { offset = 0; } if (!buf) buf = Buffer.allocUnsafe(encodingLength()); if (!offset) offset = 0; var flags = ((_a = h.flags) !== null && _a !== void 0 ? _a : 0) & 32767; var type = h.type === 'response' ? RESPONSE_FLAG : QUERY_FLAG; buf.writeUInt16BE((_b = h.id) !== null && _b !== void 0 ? _b : 0, offset); buf.writeUInt16BE(flags | type, offset + 2); buf.writeUInt16BE(h.questions.length, offset + 4); buf.writeUInt16BE(h.answers.length, offset + 6); buf.writeUInt16BE(h.authorities.length, offset + 8); buf.writeUInt16BE(h.additionals.length, offset + 10); return buf; } exports.encode = encode; encode.bytes = 12; function decode(buf, offset) { if (offset === void 0) { offset = 0; } if (!offset) offset = 0; if (buf.length < 12) throw new Error('Header must be 12 bytes'); var flags = buf.readUInt16BE(offset + 2); return { id: buf.readUInt16BE(offset), type: flags & RESPONSE_FLAG ? 'response' : 'query', flags: flags & 32767, flag_qr: ((flags >> 15) & 0x1) === 1, opcode: opcodes.toString((flags >> 11) & 0xf), flag_aa: ((flags >> 10) & 0x1) === 1, flag_tc: ((flags >> 9) & 0x1) === 1, flag_rd: ((flags >> 8) & 0x1) === 1, flag_ra: ((flags >> 7) & 0x1) === 1, flag_z: ((flags >> 6) & 0x1) === 1, flag_ad: ((flags >> 5) & 0x1) === 1, flag_cd: ((flags >> 4) & 0x1) === 1, rcode: rcodes.toString(flags & 0xf), questions: new Array(buf.readUInt16BE(offset + 4)), answers: new Array(buf.readUInt16BE(offset + 6)), authorities: new Array(buf.readUInt16BE(offset + 8)), additionals: new Array(buf.readUInt16BE(offset + 10)) }; } exports.decode = decode; decode.bytes = 12; function encodingLength() { return 12; } exports.encodingLength = encodingLength; //# sourceMappingURL=header.js.map