net: classify.

This commit is contained in:
Christopher Jeffrey 2017-11-16 19:37:09 -08:00
parent 8be995bd78
commit 4ebfb5d9ff
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 9518 additions and 9467 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -12,57 +12,60 @@ const Network = require('../protocol/network');
const hash256 = require('bcrypto/lib/hash256');
/**
* Protocol packet framer
* Protocol Message Framer
* @alias module:net.Framer
* @constructor
* @param {Network} network
*/
function Framer(network) {
if (!(this instanceof Framer))
return new Framer(network);
class Framer {
/**
* Create a framer.
* @constructor
* @param {Network} network
*/
this.network = Network.get(network);
constructor(network) {
this.network = Network.get(network);
}
/**
* Frame a payload with a header.
* @param {String} cmd - Packet type.
* @param {Buffer} payload
* @param {Buffer?} checksum - Precomputed checksum.
* @returns {Buffer} Payload with header prepended.
*/
packet(cmd, payload, checksum) {
assert(payload, 'No payload.');
assert(cmd.length < 12);
assert(payload.length <= 0xffffffff);
const msg = Buffer.allocUnsafe(24 + payload.length);
// Magic value
msg.writeUInt32LE(this.network.magic, 0, true);
// Command
msg.write(cmd, 4, 'ascii');
for (let i = 4 + cmd.length; i < 16; i++)
msg[i] = 0;
// Payload length
msg.writeUInt32LE(payload.length, 16, true);
if (!checksum)
checksum = hash256.digest(payload);
// Checksum
checksum.copy(msg, 20, 0, 4);
payload.copy(msg, 24);
return msg;
}
}
/**
* Frame a payload with a header.
* @param {String} cmd - Packet type.
* @param {Buffer} payload
* @param {Buffer?} checksum - Precomputed checksum.
* @returns {Buffer} Payload with header prepended.
*/
Framer.prototype.packet = function packet(cmd, payload, checksum) {
assert(payload, 'No payload.');
assert(cmd.length < 12);
assert(payload.length <= 0xffffffff);
const msg = Buffer.allocUnsafe(24 + payload.length);
// Magic value
msg.writeUInt32LE(this.network.magic, 0, true);
// Command
msg.write(cmd, 4, 'ascii');
for (let i = 4 + cmd.length; i < 16; i++)
msg[i] = 0;
// Payload length
msg.writeUInt32LE(payload.length, 16, true);
if (!checksum)
checksum = hash256.digest(payload);
// Checksum
checksum.copy(msg, 20, 0, 4);
payload.copy(msg, 24);
return msg;
};
/*
* Expose
*/

File diff suppressed because it is too large Load Diff

View File

@ -18,170 +18,178 @@ const common = require('./common');
const packets = require('./packets');
/**
* Protocol packet parser
* Protocol Message Parser
* @alias module:net.Parser
* @constructor
* @param {Network} network
* @extends EventEmitter
* @emits Parser#error
* @emits Parser#packet
*/
function Parser(network) {
if (!(this instanceof Parser))
return new Parser(network);
class Parser extends EventEmitter {
/**
* Create a parser.
* @constructor
* @param {Network} network
*/
EventEmitter.call(this);
constructor(network) {
super();
this.network = Network.get(network);
this.network = Network.get(network);
this.pending = [];
this.total = 0;
this.waiting = 24;
this.header = null;
}
this.pending = [];
this.total = 0;
this.waiting = 24;
this.header = null;
}
Object.setPrototypeOf(Parser.prototype, EventEmitter.prototype);
/**
* Emit an error.
* @private
* @param {...String} msg
*/
/**
* Emit an error.
* @private
* @param {...String} msg
*/
error() {
const msg = format.apply(null, arguments);
this.emit('error', new Error(msg));
}
Parser.prototype.error = function error() {
const msg = format.apply(null, arguments);
this.emit('error', new Error(msg));
};
/**
* Feed data to the parser.
* @param {Buffer} data
*/
/**
* Feed data to the parser.
* @param {Buffer} data
*/
feed(data) {
this.total += data.length;
this.pending.push(data);
Parser.prototype.feed = function feed(data) {
this.total += data.length;
this.pending.push(data);
while (this.total >= this.waiting) {
const chunk = Buffer.allocUnsafe(this.waiting);
let off = 0;
while (this.total >= this.waiting) {
const chunk = Buffer.allocUnsafe(this.waiting);
let off = 0;
while (off < chunk.length) {
const len = this.pending[0].copy(chunk, off);
if (len === this.pending[0].length)
this.pending.shift();
else
this.pending[0] = this.pending[0].slice(len);
off += len;
}
while (off < chunk.length) {
const len = this.pending[0].copy(chunk, off);
if (len === this.pending[0].length)
this.pending.shift();
else
this.pending[0] = this.pending[0].slice(len);
off += len;
assert.strictEqual(off, chunk.length);
this.total -= chunk.length;
this.parse(chunk);
}
}
/**
* Parse a fully-buffered chunk.
* @param {Buffer} chunk
*/
parse(data) {
assert(data.length <= common.MAX_MESSAGE);
if (!this.header) {
this.header = this.parseHeader(data);
return;
}
assert.strictEqual(off, chunk.length);
const hash = hash256.digest(data);
const checksum = hash.readUInt32LE(0, true);
this.total -= chunk.length;
this.parse(chunk);
}
};
if (checksum !== this.header.checksum) {
this.waiting = 24;
this.header = null;
this.error('Invalid checksum: %s.', checksum.toString(16));
return;
}
/**
* Parse a fully-buffered chunk.
* @param {Buffer} chunk
*/
let payload;
try {
payload = this.parsePayload(this.header.cmd, data);
} catch (e) {
this.waiting = 24;
this.header = null;
this.emit('error', e);
return;
}
Parser.prototype.parse = function parse(data) {
assert(data.length <= common.MAX_MESSAGE);
if (!this.header) {
this.header = this.parseHeader(data);
return;
}
const hash = hash256.digest(data);
const checksum = hash.readUInt32LE(0, true);
if (checksum !== this.header.checksum) {
this.waiting = 24;
this.header = null;
this.error('Invalid checksum: %s.', checksum.toString(16));
return;
this.emit('packet', payload);
}
let payload;
try {
payload = this.parsePayload(this.header.cmd, data);
} catch (e) {
this.waiting = 24;
this.header = null;
this.emit('error', e);
return;
/**
* Parse buffered packet header.
* @param {Buffer} data - Header.
* @returns {Header}
*/
parseHeader(data) {
const magic = data.readUInt32LE(0, true);
if (magic !== this.network.magic) {
this.error('Invalid magic value: %s.', magic.toString(16));
return null;
}
// Count length of the cmd.
let i = 0;
for (; data[i + 4] !== 0 && i < 12; i++);
if (i === 12) {
this.error('Non NULL-terminated command.');
return null;
}
const cmd = data.toString('ascii', 4, 4 + i);
const size = data.readUInt32LE(16, true);
if (size > common.MAX_MESSAGE) {
this.waiting = 24;
this.error('Packet length too large: %d.', size);
return null;
}
this.waiting = size;
const checksum = data.readUInt32LE(20, true);
return new Header(cmd, size, checksum);
}
this.waiting = 24;
this.header = null;
/**
* Parse a payload.
* @param {String} cmd - Packet type.
* @param {Buffer} data - Payload.
* @returns {Object}
*/
this.emit('packet', payload);
};
/**
* Parse buffered packet header.
* @param {Buffer} data - Header.
* @returns {Header}
*/
Parser.prototype.parseHeader = function parseHeader(data) {
const magic = data.readUInt32LE(0, true);
if (magic !== this.network.magic) {
this.error('Invalid magic value: %s.', magic.toString(16));
return null;
parsePayload(cmd, data) {
return packets.fromRaw(cmd, data);
}
// Count length of the cmd.
let i = 0;
for (; data[i + 4] !== 0 && i < 12; i++);
if (i === 12) {
this.error('Non NULL-terminated command.');
return null;
}
const cmd = data.toString('ascii', 4, 4 + i);
const size = data.readUInt32LE(16, true);
if (size > common.MAX_MESSAGE) {
this.waiting = 24;
this.error('Packet length too large: %d.', size);
return null;
}
this.waiting = size;
const checksum = data.readUInt32LE(20, true);
return new Header(cmd, size, checksum);
};
/**
* Parse a payload.
* @param {String} cmd - Packet type.
* @param {Buffer} data - Payload.
* @returns {Object}
*/
Parser.prototype.parsePayload = function parsePayload(cmd, data) {
return packets.fromRaw(cmd, data);
};
}
/**
* Packet Header
* @constructor
* @ignore
*/
function Header(cmd, size, checksum) {
this.cmd = cmd;
this.size = size;
this.checksum = checksum;
class Header {
/**
* Create a header.
* @constructor
*/
constructor(cmd, size, checksum) {
this.cmd = cmd;
this.size = size;
this.checksum = checksum;
}
}
/*

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff