Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

framer: includer user-agent in version packet. #17

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions lib/bcoin/protocol/framer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ var bcoin = require('../../bcoin');
var constants = require('./constants');
var utils = bcoin.utils;
var assert = utils.assert;
var version = require('../../../package.json').version;

var writeU32 = utils.writeU32;
var writeAscii = utils.writeAscii;

function Framer() {
function Framer(options) {
if (!(this instanceof Framer))
return new Framer();
return new Framer(options);

options = options || {};

this.options = options;
this.agent = utils.toArray(options.agent || '/bcoin:' + version + '/');
}
module.exports = Framer;

Expand Down Expand Up @@ -49,42 +55,52 @@ Framer.prototype._addr = function addr(buf, off) {
writeU32(buf, 0, off + 20);
buf[off + 24] = 0;
buf[off + 25] = 0;
return 26;
};

Framer.prototype.version = function version(packet) {
var p = new Array(86);
var off = 0;

if (!packet)
packet = {};

// Version
writeU32(p, constants.version, 0);
off += writeU32(p, constants.version, off);

// Services
writeU32(p, constants.services.network, 4);
writeU32(p, 0, 8);
off += writeU32(p, constants.services.network, off);
off += writeU32(p, 0, off);

// Timestamp
var ts = ((+new Date()) / 1000) | 0;
writeU32(p, ts, 12);
writeU32(p, 0, 16);
off += writeU32(p, ts, off);
off += writeU32(p, 0, off);

// Remote and local addresses
this._addr(p, 20);
this._addr(p, 46);
off += this._addr(p, off);
off += this._addr(p, off);

// Nonce, very dramatic
writeU32(p, (Math.random() * 0xffffffff) | 0, 72);
writeU32(p, (Math.random() * 0xffffffff) | 0, 76);
off += writeU32(p, (Math.random() * 0xffffffff) | 0, off);
off += writeU32(p, (Math.random() * 0xffffffff) | 0, off);

// No user-agent
p[80] = 0;
// User-agent
assert.equal(off, 80);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could just leave this one.

if (!this.agent) {
p[off++] = 0;
} else {
off += varint(p, this.agent.length, off);
for (var i = 0; i < this.agent.length; i++) {
p[off++] = this.agent[i];
}
}

// Start height
writeU32(p, packet.height, 81);
off += writeU32(p, packet.height, off);

// Relay
p[85] = packet.relay ? 1 : 0;
p[off++] = packet.relay ? 1 : 0;

return this.packet('version', p);
};
Expand Down