Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Mock UDP socket based on pcap file data.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


Mock UDP socket based on pcap file data.

Build Status


'use strict';

var PcapDgram = require('../dgram');
var path = require('path');

module.exports.example = function(test) {

  var hello = new Buffer('Hello world!');
  var remoteAddr = '';
  var remotePort = 137;

  // Create a dgram socket using a netbios name service pcap file
  var file = path.join(__dirname, 'data', 'netbios-ns-b-register-winxp.pcap');
  var pdgram = new PcapDgram(file, '', {paused: true});

  // When we receive the netbios name service packet, validate it
  pdgram.on('message', function(msg, rinfo) {
    // assert values from inspecting pcap with tshark
    test.equal(68, msg.length);
    test.equal(msg.length, rinfo.size);
    test.equal(remoteAddr, rinfo.address);
    test.equal(remotePort, rinfo.port);

    // Simulate sending a response back
    pdgram.send(hello, 0, hello.length, rinfo.port, rinfo.address, function(error) {
      test.equal(null, error);

  // Validate the response was sent
  pdgram.on('output', function(msg, port, address) {
    test.equal(hello.toString(), msg.toString());
    test.equal(remotePort, port);
    test.equal(remoteAddr, address);

  pdgram.on('close', function() {

// because we constructor pdgram with {paused:true} we must explicitly start

Limitations / TODO

  • Currently only supports IPv4.

Class PcapDgram

The PcapDgram class implements the same API as the dgram.Socket class. Data is delivered to the 'message' event by reading data from a pcap file. Outgoing data is redirected to the 'output' event so that it can be validated for correctness.

var pdgram = new PcapDgram(pcapSource, address, opts)

  • pcapSource {String | Stream} If a String, pcapSource is interpreted as the name of a pcap file to read from. Otherwise pcapSource is treated as a stream providing pcap data.
  • address {String} An IPv4 address used in the pcap file. The dgram socket will act as that IP address. Packets sent to this address will be emitted via the 'message' event.
  • opts {Object | null} Optional parameters
    • port {Number} The UDP port associated with the address passed as the second argument. Packets sent to this port at the given address wil be emitted via the 'message' event. If not provided then the port will be automatically set to the port used on the first UDP packet sent to the address.
    • netmask {String} An IPv4 netmask to use when pretending to be the configured address. This mainly determines if packets sent to subnet specific broadcast addresses. For example, setting a netmask of '' will cause the socket to deliver packets addressed like this ''. Defaults to '' meaning only unicast and full broadcast will packets will be delivered.
    • paused {Boolean} If true, the PcapDgram will start in the paused state and will not emit any 'message' events until resume() is called.


Stop the flow of packets. Note, if the stream is paused for too long, the underlying buffer may not be able support the number of pcap packets and begin dropping data.


Start the flow of packets after pause() has been called or the if PcapDgram was created with the {paused: true} option. Note, if {paused: true} was used during construction, then the 'listening' event will not be emiited until the first time resume() is called.

Event 'output'

The 'output' event will fire whenever the send() fuction is called. Instead of writing the data out to the specified remote host, the data is provided to this event instead. This allows test code to validate that the correct output is being produced by the code under test.

  • msg {Buffer} The UDP payload to send to the remote host
  • port {Number} The UDP port of the remote host
  • address {Address} The IPv4 address of the remote host
Something went wrong with that request. Please try again.