Packet

Represents a network packet with protocol layer support Provides fluent interface for packet crafting and parsing

Constructor

new Packet(dataopt)

Parameters:
NameTypeAttributesDescription
dataObject<optional>
Packet data, options object, or existing packet
Properties
NameTypeAttributesDescription
bufferBuffer<optional>
Raw packet buffer for parsing existing packets
ifaceObject<optional>
Network interface configuration
Examples
// Create an ICMP echo request packet
const packet = new Packet({ iface: 'eth0' })
  .Ethernet({ dst: 'ff:ff:ff:ff:ff:ff', src: '00:11:22:33:44:55' })
  .IPv4({ src: '192.168.1.1', dst: '8.8.8.8', timeToLive: 64 })
  .ICMP({ type: 8, code: 0, id: 12345, sequence: 1 });
// Parse existing packet
const packet = new Packet({ buffer: rawBuffer });
console.log(packet.layers.IPv4.src);

Extends

  • LayersList

Members

buffer

Raw buffer containing the complete packet data Automatically builds packet if layers were added

Methods

ARP(data) → {Packet}

Adds ARP layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
hardwareTypenumberHardware type (HTYPE)
protocolTypenumberProtocol type (PTYPE)
hardwareLengthnumberHardware address length (HLEN)
protocolLengthnumberProtocol length (PLEN)
opcode'who-has' | 'is-at'Operation: 1 (request) for `who-has`, 2 (reply) for `is-at`.
hardwareSrcstringSender hardware address (SHA)
protocolSrcstringSender protocol address (SPA)
hardwareDststringTarget hardware address (THA)
protocolDststringTarget protocol address (TPA)
Returns:
the packet instance
Type: 
Packet

DHCP(data) → {Packet}

Adds DHCP layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
opCodenumberBootP opcode.
hardwareTypenumberHardware type (1 = Ethernet).
hardwareAddressLengthnumberHardware address length (6 = MAC).
hopsnumberHop count.
transactionIdnumberDHCP transaction ID.
clientIpAddressstringClient IPv4 address (string form).
yourIpAddressstringYour IPv4 address (string form).
serverIpAddressnumberServer IPv4 address as raw uint32.
gatewayIpAddressstringGateway IPv4 address (string form).
clientHardwareAddressstringClient hardware address (MAC, first 6 bytes of 16).
serverNamestringBootP server name (C-string).
bootFilenamestringBootP boot file name (C-string).
magicNumbernumberDHCP magic number (0x63825363).
Returns:
the packet instance
Type: 
Packet

DNS(data) → {Packet}

Adds DNS layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
idnumberTransaction identifier.
qrnumberQuery/response flag (0 = query, 1 = response).
opcodenumber4-bit query opcode.
aanumberAuthoritative answer flag.
tcnumberTruncation flag.
rdnumberRecursion desired flag.
ranumberRecursion available flag.
rcodenumber4-bit response code.
qdCountnumberNumber of questions.
anCountnumberNumber of answers.
nsCountnumberNumber of authority records.
arCountnumberNumber of additional records.
Returns:
the packet instance
Type: 
Packet

Ethernet(data) → {Packet}

Adds Ethernet layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
srcstringSource MAC address.
dststringDestination MAC address.
typenumberProtocol type.
Returns:
the packet instance
Type: 
Packet

GRE(data) → {Packet}

Adds GRE layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
versionnumberVersion number, must be 0 for standard GRE.
protocolnumberEtherType of the encapsulated protocol.
checksumFlagnumberIndicates that the checksum field is present.
keyFlagnumberIndicates that the key field is present.
sequenceFlagnumberIndicates that the sequence number field is present.
Returns:
the packet instance
Type: 
Packet

ICMP(data) → {Packet}

Adds ICMP layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
typenumberICMP message type (see ICMPTypes)
codenumberICMP message code
checksumnumberICMP checksum
typeNamestringHuman-readable name of the ICMP message type
isEchoRequestbooleanTrue for type=8 (EchoRequest).
isEchoReplybooleanTrue for type=0 (EchoReply).
isDestinationUnreachablebooleanTrue for type=3.
isTimeExceededbooleanTrue for type=11.
isParameterProblembooleanTrue for type=12.
isTimestampRequestbooleanTrue for type=13.
isTimestampReplybooleanTrue for type=14.
Returns:
the packet instance
Type: 
Packet

ICMPv6(data) → {Packet}

Adds ICMPv6 layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
typenumberICMPv6 message type (see ICMPv6Types).
codenumberICMPv6 message code.
checksumnumberICMPv6 checksum.
Returns:
the packet instance
Type: 
Packet

IPv4(data) → {Packet}

Adds IPv4 layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
headerLengthnumberIP header length, has the value of 5 for IPv4.
versionnumberIP version number, has the value of 4 for IPv4.
typeOfServicenumberType of service / DSCP.
totalLengthnumberEntire packet (fragment) size, including header and data, in bytes.
idnumberIdentification field used for fragmentation.
fragmentOffsetRawnumberFragment offset field (raw, includes flags) @LE.
timeToLivenumberHop count.
protocolnumberEncapsulated protocol number (one of IPProtocolTypes).
checksumnumberHeader checksum.
srcstringIPv4 address of the sender.
dststringIPv4 address of the receiver.
Returns:
the packet instance
Type: 
Packet

IPv6(data) → {Packet}

Adds IPv6 layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
versionnumberIP version, has the value of 6 for IPv6.
trafficClassnumberTraffic class (DSCP + ECN), 8 bits.
flowLabelnumberFlow label, 20 bits.
payloadLengthnumberLength of payload following the header in bytes.
nextHeadernumberNext header type. Must be one of ::IPProtocolTypes.
hopLimitnumberHop limit, decremented by each forwarder.
srcstringIPv6 source address in human-readable form.
dststringIPv6 destination address in human-readable form.
Returns:
the packet instance
Type: 
Packet

NTP(data) → {Packet}

Adds NTP layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
linumberLeap indicator (0..3).
vnnumberVersion number (typically 3 or 4).
modenumberMode (1=symmetric active, 3=client, 4=server, 5=broadcast).
stratumnumberStratum level (0=unspecified, 1=primary, 2-15=secondary).
pollnumberPoll interval as signed log2 seconds.
precisionnumberPrecision as signed log2 seconds.
rootDelaynumber32-bit fixed-point root delay.
rootDispersionnumber32-bit fixed-point root dispersion.
referenceIdnumberReference identifier (4 bytes; IPv4 or 4-char ASCII).
referenceTimestampDate | nullTime at which the local clock was last set.
originTimestampDate | nullTime at the client when the request was sent.
receiveTimestampDate | nullTime at the server when the request was received.
transmitTimestampDate | nullTime at the server when the response was sent.
Returns:
the packet instance
Type: 
Packet

Payload(data) → {Packet}

Adds Payload layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Returns:
the packet instance
Type: 
Packet

TCP(data) → {Packet}

Adds TCP layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
srcnumberSource TCP port.
dstnumberDestination TCP port.
seqnumberSequence number.
acknumberAcknowledgment number.
windowSizenumberReceive window size.
checksumnumberHeader + data checksum.
urgentPointernumberUrgent data byte offset (when URG is set).
flagsTCPFlagsTCP flags.
Returns:
the packet instance
Type: 
Packet

UDP(data) → {Packet}

Adds UDP layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
srcnumberSource UDP port.
dstnumberDestination UDP port.
totalLengthnumberLength in bytes of the UDP datagram (header + data).
checksumnumber16-bit checksum field for error-checking.
Returns:
the packet instance
Type: 
Packet

Vlan(data) → {Packet}

Adds Vlan layer to the packet stack
Parameters:
NameTypeDescription
dataObjectThe layer data
Properties
NameTypeDescription
vidnumber12-bit VLAN identifier (0..4095).
deinumberDrop Eligible Indicator (0 or 1).
pcpnumberPriority Code Point (0..7).
etherTypenumberEtherType of the encapsulated payload.
Returns:
the packet instance
Type: 
Packet

clone() → {Packet}

Creates a deep clone of the packet Preserves all layers, timestamps, and interface settings
Returns:
New packet instance identical to this one
Type: 
Packet

equals(pkt) → {boolean}

Compares this packet with another packet for equality
Parameters:
NameTypeDescription
pktPacketThe packet to compare with
Returns:
True if packets are identical (same interface, timestamp, and buffer content)
Type: 
boolean
Example
const packet1 = new Packet({ buffer: data });
const packet2 = new Packet({ buffer: data });
if (packet1.equals(packet2)) {
  console.log('Packets are identical');
}

toObject() → {Object|Object|Object}

Converts the packet to a plain JavaScript object
Returns:
  • Plain object representation
    Type: 
    Object
  • returns.iface - Interface configuration
    Type: 
    Object
  • returns.layers - Layer objects with their fields
    Type: 
    Object
Example
const obj = packet.toObject();
console.log(obj.layers.IPv4.src);