Allow to pass services as buffer to net_addr

This commit is contained in:
Kagami Hiiragi 2015-02-28 22:25:28 +03:00
parent d68e5adf0b
commit 56083e4751
2 changed files with 17 additions and 2 deletions

View File

@ -761,7 +761,7 @@ exports.net_addr = {
* included in short mode (current time by default) * included in short mode (current time by default)
* @param {number=} opts.stream - Stream number of the node, not * @param {number=} opts.stream - Stream number of the node, not
* included in short mode (1 by default) * included in short mode (1 by default)
* @param {Object=} opts.services - * @param {(Object|Buffer)=} opts.services -
* [Services]{@link module:bitmessage/structs.ServicesBitfield} * [Services]{@link module:bitmessage/structs.ServicesBitfield}
* provided by the node (`NODE_NETWORK` by default) * provided by the node (`NODE_NETWORK` by default)
* @param {string} opts.host - IPv4/IPv6 address of the node * @param {string} opts.host - IPv4/IPv6 address of the node
@ -788,7 +788,12 @@ exports.net_addr = {
} }
var services = opts.services || var services = opts.services ||
ServicesBitfield().set(ServicesBitfield.NODE_NETWORK); ServicesBitfield().set(ServicesBitfield.NODE_NETWORK);
services.buffer.copy(buf, shift); if (Buffer.isBuffer(services)) {
assert(services.length === 8, "Bad services buffer length");
} else {
services = services.buffer;
}
services.copy(buf, shift);
inet_pton(opts.host).copy(buf, shift + 8); inet_pton(opts.host).copy(buf, shift + 8);
buf.writeUInt16BE(opts.port, shift + 24); buf.writeUInt16BE(opts.port, shift + 24);
return buf; return buf;

10
test.js
View File

@ -414,6 +414,16 @@ describe("Common structures", function() {
var opts = {host: " 127.0.0.1", port: 1234}; var opts = {host: " 127.0.0.1", port: 1234};
expect(net_addr.encode.bind(null, opts)).to.throw(/bad octet/i); expect(net_addr.encode.bind(null, opts)).to.throw(/bad octet/i);
}); });
it("should allow to pass services as Buffer", function() {
var services = Buffer(8);
var res = net_addr.decode(net_addr.encode({
host: "1.2.3.4",
port: 1234,
services: services,
}));
expect(bufferEqual(res.services.buffer, services)).to.be.true;
});
}); });
describe("inv_vect", function() { describe("inv_vect", function() {