2016-04-15 05:28:23 -07:00
|
|
|
/*!
|
2015-12-18 22:53:31 -08:00
|
|
|
* parser.js - packet parser for bcoin
|
|
|
|
|
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
|
2017-02-03 22:47:26 -08:00
|
|
|
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
|
2016-06-09 16:18:50 -07:00
|
|
|
* https://github.com/bcoin-org/bcoin
|
2015-12-18 22:53:31 -08:00
|
|
|
*/
|
|
|
|
|
|
2017-07-31 00:34:42 -07:00
|
|
|
/* eslint nonblock-statement-body-position: "off" */
|
|
|
|
|
|
2016-06-13 01:06:01 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-06-29 20:54:07 -07:00
|
|
|
const assert = require('assert');
|
2017-07-01 03:41:57 -07:00
|
|
|
const EventEmitter = require('events');
|
2017-10-26 04:07:36 -07:00
|
|
|
const {format} = require('util');
|
2017-06-29 20:54:07 -07:00
|
|
|
const Network = require('../protocol/network');
|
2017-11-01 15:41:32 -07:00
|
|
|
const hash256 = require('bcrypto/lib/hash256');
|
2017-06-29 20:54:07 -07:00
|
|
|
const common = require('./common');
|
|
|
|
|
const packets = require('./packets');
|
2014-04-28 17:12:26 +04:00
|
|
|
|
2015-12-18 22:53:31 -08:00
|
|
|
/**
|
2017-11-16 19:37:09 -08:00
|
|
|
* Protocol Message Parser
|
2017-02-03 22:47:26 -08:00
|
|
|
* @alias module:net.Parser
|
2017-11-16 19:37:09 -08:00
|
|
|
* @extends EventEmitter
|
2016-04-15 05:28:23 -07:00
|
|
|
* @emits Parser#error
|
|
|
|
|
* @emits Parser#packet
|
2015-12-18 22:53:31 -08:00
|
|
|
*/
|
|
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
class Parser extends EventEmitter {
|
|
|
|
|
/**
|
|
|
|
|
* Create a parser.
|
|
|
|
|
* @constructor
|
|
|
|
|
* @param {Network} network
|
|
|
|
|
*/
|
2014-04-28 17:12:26 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
constructor(network) {
|
|
|
|
|
super();
|
2014-04-28 17:12:26 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
this.network = Network.get(network);
|
2016-07-26 12:23:40 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
this.pending = [];
|
|
|
|
|
this.total = 0;
|
|
|
|
|
this.waiting = 24;
|
|
|
|
|
this.header = null;
|
|
|
|
|
}
|
2015-12-18 22:37:02 -08:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
/**
|
|
|
|
|
* Emit an error.
|
|
|
|
|
* @private
|
|
|
|
|
* @param {...String} msg
|
|
|
|
|
*/
|
2014-04-28 17:12:26 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
error() {
|
|
|
|
|
const msg = format.apply(null, arguments);
|
|
|
|
|
this.emit('error', new Error(msg));
|
|
|
|
|
}
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
/**
|
|
|
|
|
* Feed data to the parser.
|
|
|
|
|
* @param {Buffer} data
|
|
|
|
|
*/
|
2014-05-05 16:52:17 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
feed(data) {
|
|
|
|
|
this.total += data.length;
|
|
|
|
|
this.pending.push(data);
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
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;
|
|
|
|
|
}
|
2015-12-18 22:37:02 -08:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
assert.strictEqual(off, chunk.length);
|
2014-04-28 17:12:26 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
this.total -= chunk.length;
|
|
|
|
|
this.parse(chunk);
|
|
|
|
|
}
|
2014-04-28 17:12:26 +04:00
|
|
|
}
|
|
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
/**
|
|
|
|
|
* Parse a fully-buffered chunk.
|
|
|
|
|
* @param {Buffer} chunk
|
|
|
|
|
*/
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
parse(data) {
|
|
|
|
|
assert(data.length <= common.MAX_MESSAGE);
|
2016-03-04 14:51:37 -08:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
if (!this.header) {
|
|
|
|
|
this.header = this.parseHeader(data);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-12-18 22:37:02 -08:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
const hash = hash256.digest(data);
|
|
|
|
|
const checksum = hash.readUInt32LE(0, true);
|
2015-12-18 22:37:02 -08:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
if (checksum !== this.header.checksum) {
|
|
|
|
|
this.waiting = 24;
|
|
|
|
|
this.header = null;
|
|
|
|
|
this.error('Invalid checksum: %s.', checksum.toString(16));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let payload;
|
|
|
|
|
try {
|
|
|
|
|
payload = this.parsePayload(this.header.cmd, data);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.waiting = 24;
|
|
|
|
|
this.header = null;
|
|
|
|
|
this.emit('error', e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-12-18 22:37:02 -08:00
|
|
|
|
2016-05-18 17:32:35 -07:00
|
|
|
this.waiting = 24;
|
2016-08-26 16:49:38 -07:00
|
|
|
this.header = null;
|
2016-09-16 19:35:04 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
this.emit('packet', payload);
|
|
|
|
|
}
|
2014-04-28 17:12:26 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
/**
|
|
|
|
|
* Parse buffered packet header.
|
|
|
|
|
* @param {Buffer} data - Header.
|
|
|
|
|
* @returns {Header}
|
|
|
|
|
*/
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
parseHeader(data) {
|
|
|
|
|
const magic = data.readUInt32LE(0, true);
|
2015-12-18 22:37:02 -08:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
if (magic !== this.network.magic) {
|
|
|
|
|
this.error('Invalid magic value: %s.', magic.toString(16));
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-04-28 17:12:26 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
// Count length of the cmd.
|
|
|
|
|
let i = 0;
|
|
|
|
|
for (; data[i + 4] !== 0 && i < 12; i++);
|
2015-12-18 22:37:02 -08:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
if (i === 12) {
|
|
|
|
|
this.error('Non NULL-terminated command.');
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-04-28 17:12:26 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
const cmd = data.toString('ascii', 4, 4 + i);
|
2016-05-18 18:54:46 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
const size = data.readUInt32LE(16, true);
|
2014-04-28 17:12:26 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
if (size > common.MAX_MESSAGE) {
|
|
|
|
|
this.waiting = 24;
|
|
|
|
|
this.error('Packet length too large: %d.', size);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-03-04 14:51:37 -08:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
this.waiting = size;
|
2016-09-16 19:35:04 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
const checksum = data.readUInt32LE(20, true);
|
2016-06-22 11:44:43 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
return new Header(cmd, size, checksum);
|
|
|
|
|
}
|
2014-04-28 17:43:13 +04:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
/**
|
|
|
|
|
* Parse a payload.
|
|
|
|
|
* @param {String} cmd - Packet type.
|
|
|
|
|
* @param {Buffer} data - Payload.
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
*/
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
parsePayload(cmd, data) {
|
|
|
|
|
return packets.fromRaw(cmd, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-18 22:53:31 -08:00
|
|
|
|
2016-06-22 11:44:43 -07:00
|
|
|
/**
|
2016-09-16 17:34:34 -07:00
|
|
|
* Packet Header
|
2017-02-03 22:47:26 -08:00
|
|
|
* @ignore
|
2016-06-22 11:44:43 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-11-16 19:37:09 -08:00
|
|
|
class Header {
|
|
|
|
|
/**
|
|
|
|
|
* Create a header.
|
|
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
constructor(cmd, size, checksum) {
|
|
|
|
|
this.cmd = cmd;
|
|
|
|
|
this.size = size;
|
|
|
|
|
this.checksum = checksum;
|
|
|
|
|
}
|
2016-06-22 11:44:43 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-15 18:07:06 -07:00
|
|
|
/*
|
|
|
|
|
* Expose
|
|
|
|
|
*/
|
|
|
|
|
|
2016-05-13 09:23:57 -07:00
|
|
|
module.exports = Parser;
|