Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting multiple pcap sessions #27

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions pcap.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var util, IOWatcher,
Buffer = require('buffer').Buffer, Buffer = require('buffer').Buffer,
events = require('events'), events = require('events'),
binding = require('./build/default/pcap_binding'), binding = require('./build/default/pcap_binding'),
Pcap = binding.Pcap,
HTTPParser = process.binding('http_parser').HTTPParser, HTTPParser = process.binding('http_parser').HTTPParser,
url = require('url'); url = require('url');


Expand All @@ -16,20 +17,8 @@ if (process.versions && process.versions.node && process.versions.node.split('.'
IOWatcher = process.IOWatcher; IOWatcher = process.IOWatcher;
} }


function Pcap() {
this.opened = false;
this.fd = null;

events.EventEmitter.call(this);
}
util.inherits(Pcap, events.EventEmitter);

exports.lib_version = binding.lib_version(); exports.lib_version = binding.lib_version();


Pcap.prototype.findalldevs = function () {
return binding.findalldevs();
};

Pcap.prototype.open = function (live, device, filter, buffer_size) { Pcap.prototype.open = function (live, device, filter, buffer_size) {
var me = this; var me = this;


Expand All @@ -40,14 +29,16 @@ Pcap.prototype.open = function (live, device, filter, buffer_size) {
} }


if (live) { if (live) {
this.device_name = device || binding.default_device(); this.device_name = device || this.default_device();
this.link_type = binding.open_live(this.device_name, filter || "", this.buffer_size); this.open_live(this.device_name, filter || "", this.buffer_size);
} else { } else {
this.device_name = device; this.device_name = device;
this.link_type = binding.open_offline(this.device_name, filter || "", this.buffer_size); this.open_offline(this.device_name, filter || "", this.buffer_size);
} }


this.fd = binding.fileno(); this.link_type = this.link_type();

this.fd = this.fileno();
this.opened = true; this.opened = true;
this.readWatcher = new IOWatcher(); this.readWatcher = new IOWatcher();
this.empty_reads = 0; this.empty_reads = 0;
Expand All @@ -63,26 +54,23 @@ Pcap.prototype.open = function (live, device, filter, buffer_size) {


// readWatcher gets a callback when pcap has data to read. multiple packets may be readable. // readWatcher gets a callback when pcap has data to read. multiple packets may be readable.
this.readWatcher.callback = function pcap_read_callback() { this.readWatcher.callback = function pcap_read_callback() {
var packets_read = binding.dispatch(me.buf, packet_ready); var packets_read = me.dispatch(me.buf, packet_ready);
if (packets_read < 1) { if (packets_read < 1) {
// TODO - figure out what is causing this, and if it is bad. // TODO - figure out what is causing this, and if it is bad.
me.empty_reads += 1; me.empty_reads += 1;
me.emit('empty_read', me.buf);
} }
}; };
this.readWatcher.set(this.fd, true, false); this.readWatcher.set(this.fd, true, false);
this.readWatcher.start(); this.readWatcher.start();
}; };


Pcap.prototype.close = function () { Pcap.prototype.close = function () {
this.opened = false; this._close();
binding.close(); this.readWatcher.stop();
// TODO - remove listeners so program will exit I guess? // TODO - remove listeners so program will exit I guess?
}; };


Pcap.prototype.stats = function () {
return binding.stats();
};

exports.Pcap = Pcap; exports.Pcap = Pcap;


exports.createSession = function (device, filter, buffer_size) { exports.createSession = function (device, filter, buffer_size) {
Expand Down Expand Up @@ -578,7 +566,17 @@ decode.udp = function (raw_packet, offset) {
if (ret.sport === 53 || ret.dport === 53) { if (ret.sport === 53 || ret.dport === 53) {
ret.dns = decode.dns(raw_packet, offset + 8); ret.dns = decode.dns(raw_packet, offset + 8);
} }


ret.data_offset = offset + 8;
ret.data_end = offset + 8 + ret.length;
ret.data_bytes = ret.data_end - ret.data_offset;
if (ret.data_bytes > 0) {
// add a buffer slice pointing to the data area of this UDP packet.
// Note that this does not make a copy
ret.data = raw_packet.slice(ret.data_offset, ret.data_end);
ret.data.length = ret.data_bytes;
}

return ret; return ret;
}; };


Expand Down
Loading