net: classify.
This commit is contained in:
parent
8be995bd78
commit
4ebfb5d9ff
1306
lib/net/bip150.js
1306
lib/net/bip150.js
File diff suppressed because it is too large
Load Diff
1293
lib/net/bip151.js
1293
lib/net/bip151.js
File diff suppressed because it is too large
Load Diff
@ -12,57 +12,60 @@ const Network = require('../protocol/network');
|
|||||||
const hash256 = require('bcrypto/lib/hash256');
|
const hash256 = require('bcrypto/lib/hash256');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Protocol packet framer
|
* Protocol Message Framer
|
||||||
* @alias module:net.Framer
|
* @alias module:net.Framer
|
||||||
* @constructor
|
|
||||||
* @param {Network} network
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function Framer(network) {
|
class Framer {
|
||||||
if (!(this instanceof Framer))
|
/**
|
||||||
return new Framer(network);
|
* 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
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
2943
lib/net/hostlist.js
2943
lib/net/hostlist.js
File diff suppressed because it is too large
Load Diff
@ -18,170 +18,178 @@ const common = require('./common');
|
|||||||
const packets = require('./packets');
|
const packets = require('./packets');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Protocol packet parser
|
* Protocol Message Parser
|
||||||
* @alias module:net.Parser
|
* @alias module:net.Parser
|
||||||
* @constructor
|
* @extends EventEmitter
|
||||||
* @param {Network} network
|
|
||||||
* @emits Parser#error
|
* @emits Parser#error
|
||||||
* @emits Parser#packet
|
* @emits Parser#packet
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function Parser(network) {
|
class Parser extends EventEmitter {
|
||||||
if (!(this instanceof Parser))
|
/**
|
||||||
return new Parser(network);
|
* 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.pending = [];
|
||||||
this.total = 0;
|
this.total = 0;
|
||||||
this.waiting = 24;
|
this.waiting = 24;
|
||||||
this.header = null;
|
this.header = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.setPrototypeOf(Parser.prototype, EventEmitter.prototype);
|
/**
|
||||||
|
* Emit an error.
|
||||||
|
* @private
|
||||||
|
* @param {...String} msg
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
error() {
|
||||||
* Emit an error.
|
const msg = format.apply(null, arguments);
|
||||||
* @private
|
this.emit('error', new Error(msg));
|
||||||
* @param {...String} msg
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
Parser.prototype.error = function error() {
|
/**
|
||||||
const msg = format.apply(null, arguments);
|
* Feed data to the parser.
|
||||||
this.emit('error', new Error(msg));
|
* @param {Buffer} data
|
||||||
};
|
*/
|
||||||
|
|
||||||
/**
|
feed(data) {
|
||||||
* Feed data to the parser.
|
this.total += data.length;
|
||||||
* @param {Buffer} data
|
this.pending.push(data);
|
||||||
*/
|
|
||||||
|
|
||||||
Parser.prototype.feed = function feed(data) {
|
while (this.total >= this.waiting) {
|
||||||
this.total += data.length;
|
const chunk = Buffer.allocUnsafe(this.waiting);
|
||||||
this.pending.push(data);
|
let off = 0;
|
||||||
|
|
||||||
while (this.total >= this.waiting) {
|
while (off < chunk.length) {
|
||||||
const chunk = Buffer.allocUnsafe(this.waiting);
|
const len = this.pending[0].copy(chunk, off);
|
||||||
let off = 0;
|
if (len === this.pending[0].length)
|
||||||
|
this.pending.shift();
|
||||||
|
else
|
||||||
|
this.pending[0] = this.pending[0].slice(len);
|
||||||
|
off += len;
|
||||||
|
}
|
||||||
|
|
||||||
while (off < chunk.length) {
|
assert.strictEqual(off, chunk.length);
|
||||||
const len = this.pending[0].copy(chunk, off);
|
|
||||||
if (len === this.pending[0].length)
|
this.total -= chunk.length;
|
||||||
this.pending.shift();
|
this.parse(chunk);
|
||||||
else
|
}
|
||||||
this.pending[0] = this.pending[0].slice(len);
|
}
|
||||||
off += len;
|
|
||||||
|
/**
|
||||||
|
* 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;
|
if (checksum !== this.header.checksum) {
|
||||||
this.parse(chunk);
|
this.waiting = 24;
|
||||||
}
|
this.header = null;
|
||||||
};
|
this.error('Invalid checksum: %s.', checksum.toString(16));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
let payload;
|
||||||
* Parse a fully-buffered chunk.
|
try {
|
||||||
* @param {Buffer} chunk
|
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.waiting = 24;
|
||||||
this.header = null;
|
this.header = null;
|
||||||
this.error('Invalid checksum: %s.', checksum.toString(16));
|
|
||||||
return;
|
this.emit('packet', payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
let payload;
|
/**
|
||||||
try {
|
* Parse buffered packet header.
|
||||||
payload = this.parsePayload(this.header.cmd, data);
|
* @param {Buffer} data - Header.
|
||||||
} catch (e) {
|
* @returns {Header}
|
||||||
this.waiting = 24;
|
*/
|
||||||
this.header = null;
|
|
||||||
this.emit('error', e);
|
parseHeader(data) {
|
||||||
return;
|
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);
|
parsePayload(cmd, data) {
|
||||||
};
|
return packets.fromRaw(cmd, data);
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 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
|
* Packet Header
|
||||||
* @constructor
|
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function Header(cmd, size, checksum) {
|
class Header {
|
||||||
this.cmd = cmd;
|
/**
|
||||||
this.size = size;
|
* Create a header.
|
||||||
this.checksum = checksum;
|
* @constructor
|
||||||
|
*/
|
||||||
|
|
||||||
|
constructor(cmd, size, checksum) {
|
||||||
|
this.cmd = cmd;
|
||||||
|
this.size = size;
|
||||||
|
this.checksum = checksum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
4601
lib/net/peer.js
4601
lib/net/peer.js
File diff suppressed because it is too large
Load Diff
8481
lib/net/pool.js
8481
lib/net/pool.js
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user