An abstract-encoding compliant module for encoding and decoding DNS packets with support for all major DNS record types and transport protocols
npx @tessl/cli install tessl/npm-dns-packet@5.6.0DNS Packet is an abstract-encoding compliant module for encoding and decoding DNS packets. It provides comprehensive support for all major DNS record types and transport protocols, making it ideal for building DNS servers, DNS-over-HTTPS implementations, multicast DNS systems, and any networking tool requiring low-level DNS packet manipulation.
npm install dns-packetconst dnsPacket = require('dns-packet');For specific imports:
const { encode, decode, streamEncode, streamDecode } = require('dns-packet');const dnsPacket = require('dns-packet');
const dgram = require('dgram');
// Encode a DNS query
const buf = dnsPacket.encode({
type: 'query',
id: 1,
flags: dnsPacket.RECURSION_DESIRED,
questions: [{
type: 'A',
name: 'google.com'
}]
});
// Decode a DNS response
const socket = dgram.createSocket('udp4');
socket.on('message', message => {
const packet = dnsPacket.decode(message);
console.log(packet); // Full DNS packet object
});
socket.send(buf, 0, buf.length, 53, '8.8.8.8');DNS Packet is built around several key components:
Core packet encoding and decoding functionality for DNS messages. Supports both UDP and TCP transport protocols with proper length prefixing for stream protocols.
function encode(packet, buf, offset);
function decode(buf, offset);
function streamEncode(packet);
function streamDecode(buf);
function encodingLength(packet);Comprehensive support for all major DNS record types including basic records (A, AAAA, CNAME), mail records (MX), service discovery (SRV), security records (DNSSEC), and EDNS0 extensions.
// Record type objects with encode/decode/encodingLength methods
const {
a, aaaa, cname, ptr, dname,
mx, srv, txt, ns, soa, null: nullRecord, hinfo,
caa, naptr, tlsa, sshfp,
dnskey, rrsig, ds, nsec, nsec3, rp,
opt, option, unknown
} = require('dns-packet');
// Generic record handler
const recordHandler = require('dns-packet').record(type);DNS packet structure handling for questions, answers, authorities, and additional sections with proper flag management and metadata.
// Packet structure
{
type: 'query' | 'response',
id: number,
flags: number,
questions: Question[],
answers: Answer[],
authorities: Answer[],
additionals: Answer[]
}
// Flag constants
const {
AUTHORITATIVE_ANSWER,
TRUNCATED_RESPONSE,
RECURSION_DESIRED,
RECURSION_AVAILABLE,
AUTHENTIC_DATA,
CHECKING_DISABLED,
DNSSEC_OK
} = require('dns-packet');Low-level packet components for advanced usage and custom implementations.
// Direct access to internal components
const { name, question, answer, header } = require('dns-packet');
// Name encoding/decoding with compression support
name.encode(str, buf, offset, options);
name.decode(buf, offset, options);
name.encodingLength(str);
// Question section handling
question.encode(q, buf, offset);
question.decode(buf, offset);
question.encodingLength(q);
// Answer/Authority/Additional section handling
answer.encode(a, buf, offset);
answer.decode(buf, offset);
answer.encodingLength(a);
// Header handling (12 bytes)
header.encode(h, buf, offset);
header.decode(buf, offset);
header.encodingLength(); // Always returns 12Type conversion utilities for DNS record types, response codes, operation codes, DNS classes, and EDNS0 option codes.
const types = require('dns-packet/types');
const rcodes = require('dns-packet/rcodes');
const opcodes = require('dns-packet/opcodes');
const classes = require('dns-packet/classes');
const optioncodes = require('dns-packet/optioncodes');