Supporting multiple pcap sessions #27

Open
wants to merge 6 commits into
from
View
46 pcap.js
@@ -5,6 +5,7 @@ var util, IOWatcher,
Buffer = require('buffer').Buffer,
events = require('events'),
binding = require('./build/default/pcap_binding'),
+ Pcap = binding.Pcap,
HTTPParser = process.binding('http_parser').HTTPParser,
url = require('url');
@@ -16,20 +17,8 @@ if (process.versions && process.versions.node && process.versions.node.split('.'
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();
-Pcap.prototype.findalldevs = function () {
- return binding.findalldevs();
-};
-
Pcap.prototype.open = function (live, device, filter, buffer_size) {
var me = this;
@@ -40,14 +29,16 @@ Pcap.prototype.open = function (live, device, filter, buffer_size) {
}
if (live) {
- this.device_name = device || binding.default_device();
- this.link_type = binding.open_live(this.device_name, filter || "", this.buffer_size);
+ this.device_name = device || this.default_device();
+ this.open_live(this.device_name, filter || "", this.buffer_size);
} else {
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.readWatcher = new IOWatcher();
this.empty_reads = 0;
@@ -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.
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) {
// TODO - figure out what is causing this, and if it is bad.
me.empty_reads += 1;
+ me.emit('empty_read', me.buf);
}
};
this.readWatcher.set(this.fd, true, false);
this.readWatcher.start();
};
Pcap.prototype.close = function () {
- this.opened = false;
- binding.close();
+ this._close();
+ this.readWatcher.stop();
// TODO - remove listeners so program will exit I guess?
};
-Pcap.prototype.stats = function () {
- return binding.stats();
-};
-
exports.Pcap = Pcap;
exports.createSession = function (device, filter, buffer_size) {
@@ -578,7 +566,17 @@ decode.udp = function (raw_packet, offset) {
if (ret.sport === 53 || ret.dport === 53) {
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;
};
Oops, something went wrong.