or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdpacket-operations.mdpacket-structure.mdrecord-types.mdutilities.md
tile.json

tessl/npm-dns-packet

An abstract-encoding compliant module for encoding and decoding DNS packets with support for all major DNS record types and transport protocols

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/dns-packet@5.6.x

To install, run

npx @tessl/cli install tessl/npm-dns-packet@5.6.0

index.mddocs/

DNS Packet

DNS 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.

Package Information

  • Package Name: dns-packet
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install dns-packet

Core Imports

const dnsPacket = require('dns-packet');

For specific imports:

const { encode, decode, streamEncode, streamDecode } = require('dns-packet');

Basic Usage

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');

Architecture

DNS Packet is built around several key components:

  • Packet Encoding/Decoding: Core functions for UDP and TCP transport protocols
  • Record Type Handlers: Specialized encoders/decoders for each DNS record type (A, AAAA, MX, TXT, etc.)
  • Abstract Encoding Interface: Consistent encode/decode/encodingLength API for all components
  • Transport Abstraction: Unified interface supporting both UDP (direct) and TCP/TLS/HTTPS (stream) protocols
  • Utility Modules: Type conversion, response codes, operation codes, and class mappings

Capabilities

Packet Operations

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);

Packet Operations

DNS Record Types

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 Record Types

Packet Structure

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');

Packet Structure

Internal Components

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 12

Utility Functions

Type 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');

Utility Functions