Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

working readable interface

commit 8be863546cf407729712f89431e53ab1ba02d87d 1 parent 4bab476
@joeferner joeferner authored
Showing with 62 additions and 14 deletions.
  1. +28 −14 lib/pcap-parser.js
  2. +34 −0 test/pcap-parser-test.js
View
42 lib/pcap-parser.js
@@ -9,10 +9,10 @@ var GLOBAL_HEADER_LENGTH = 24; //bytes
var PACKET_HEADER_LENGTH = 16; //bytes
function onReadable() {
- if (this.errored) {
+ if (this.error) {
return;
}
- while (this.state.call(this)) {}
+ while (this.state.call(this)) { }
}
function onEnd() {
@@ -34,14 +34,14 @@ function parseGlobalHeader() {
} else if (magicNumber == "d4c3b2a1") {
this.endianness = "LE";
} else {
- this.errored = true;
msg = util.format('unknown magic number: 0x%s', magicNumber.toString());
- this.emit('error', new Error(msg));
+ this.error = new Error(msg);
+ this.emit('error', this.error);
this.emit('end');
return false;
}
- var header = {
+ this.globalHeader = {
magicNumber: buffer['readUInt32' + this.endianness](0, true),
majorVersion: buffer['readUInt16' + this.endianness](4, true),
minorVersion: buffer['readUInt16' + this.endianness](6, true),
@@ -51,13 +51,13 @@ function parseGlobalHeader() {
linkLayerType: buffer['readUInt32' + this.endianness](20, true)
};
- if (header.majorVersion != 2 && header.minorVersion != 4) {
- this.errored = true;
- msg = util.format('unsupported version %d.%d. pcap-parser only parses libpcap file format 2.4', header.majorVersion, header.minorVersion);
- this.emit('error', new Error(msg));
+ if (this.globalHeader.majorVersion != 2 && this.globalHeader.minorVersion != 4) {
+ msg = util.format('unsupported version %d.%d. pcap-parser only parses libpcap file format 2.4', this.globalHeader.majorVersion, this.globalHeader.minorVersion);
+ this.error = new Error(msg);
+ this.emit('error', this.error);
this.emit('end');
} else {
- this.emit('globalHeader', header);
+ this.emit('globalHeader', this.globalHeader);
this.buffer = buffer.slice(GLOBAL_HEADER_LENGTH);
this.state = parsePacketHeader;
return true;
@@ -89,13 +89,23 @@ function parsePacketBody() {
return false;
}
- this.emit('packetData', buffer);
- this.emit('packet', {
+ if (!this._readCallback) {
+ this.error = new Error('No read method called expecting data');
+ this.emit('error', this.error);
+ this.emit('end');
+ return false;
+ }
+
+ var packet = {
header: this.currentPacketHeader,
data: buffer
- });
+ };
+
+ this.emit('packetData', buffer);
+ this.emit('packet', packet);
this.state = parsePacketHeader;
+ this._readCallback(null, packet);
return true;
}
@@ -115,7 +125,11 @@ function Parser(stream) {
util.inherits(Parser, Readable);
Parser.prototype._read = function(size, callback) {
- onReadable.call(this);
+ if (this.error) {
+ return callback(this.error);
+ }
+ this._readCallback = callback;
+ return this.state.call(this);
};
exports.createParser = function(stream) {
View
34 test/pcap-parser-test.js
@@ -124,6 +124,40 @@ module.exports = {
test.done();
});
parser.resume();
+ },
+
+ 'little-endian pcap file using readable interface': function(test) {
+ var parser = pcapp.createParser(fs.createReadStream(path.join(__dirname, 'smtp.pcap')));
+ var packets = [];
+
+ parser.on('readable', function() {
+ if (parser.globalHeader) {
+ test.equals(parser.globalHeader.magicNumber, 2712847316);
+ test.equals(parser.globalHeader.majorVersion, 2);
+ test.equals(parser.globalHeader.minorVersion, 4);
+ test.equals(parser.globalHeader.gmtOffset, 0);
+ test.equals(parser.globalHeader.timestampAccuracy, 0);
+ test.equals(parser.globalHeader.snapshotLength, 65535);
+ test.equals(parser.globalHeader.linkLayerType, 1);
+ }
+
+ var packet;
+ while (packet = parser.read(1)) {
+ packets.push(packet);
+ }
+ });
+ parser.on('end', function() {
+ test.equals(packets.length, 60);
+ test.ok(packets[0].header);
+ test.ok(packets[0].data);
+ test.equals(packets[0].data.length, 76);
+ test.equals(packets[0].header.timestampSeconds, 1254722767);
+ test.equals(packets[0].header.timestampMicroseconds, 492060);
+ test.equals(packets[0].header.capturedLength, 76);
+ test.equals(packets[0].header.originalLength, 76);
+ test.done();
+ });
+ parser.resume();
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.