Skip to content

Commit

Permalink
fix: unknown empty service IP (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov authored Jul 19, 2024
1 parent 02a97cc commit 20d2a99
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 0 additions & 2 deletions lib/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ module.exports = {
PORT: 2,
},
IP_ADDRESS_SIZE: 16,
EMPTY_IPV6_ADDRESS: '[0:0:0:0:0:0:0:0]:0',
EMPTY_IPV4_ADDRESS: '0.0.0.0:0',
CURRENT_PROTOCOL_VERSION: 70211,
SML_ENTRY_VERSION_1_SIZE: 151,
SML_ENTRY_TYPE_2_ADDITION_SIZE: 22,
Expand Down
18 changes: 14 additions & 4 deletions lib/util/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ var serviceRegex =
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]):[0-9]+$/;
var ipV6prefix = Buffer.from('00000000000000000000ffff', 'hex');
var emptyAddress = Buffer.alloc(18);
var EMPTY_IPV6_ADDRESS = constants.EMPTY_IPV6_ADDRESS;
var EMPTY_IPV4_ADDRESS = constants.EMPTY_IPV4_ADDRESS;
var EMPTY_FULL_IPV6_ADDRESS = '[0:0:0:0:0:0:0:0]:0';
var EMPTY_SHORT_IPV6_ADDRESS = '[::]:0';
var EMPTY_SHORT_ZERO_IPV6_ADDRESS = '[::0]:0';
var EMPTY_FULL_IPV4_ADDRESS = '0.0.0.0:0';
var EMPTY_SHORT_IPV4_ADDRESS = '0:0';
var EMPTY_ADDRESSES = [
EMPTY_FULL_IPV6_ADDRESS,
EMPTY_SHORT_IPV6_ADDRESS,
EMPTY_SHORT_ZERO_IPV6_ADDRESS,
EMPTY_FULL_IPV4_ADDRESS,
EMPTY_SHORT_IPV4_ADDRESS,
];

/**
* Maps ipv4:port to ipv6 buffer and port
Expand Down Expand Up @@ -56,7 +66,7 @@ function bufferToIPAndPort(buffer) {
var serviceString = ipV4string + ':' + String(port);
// This is a hack to match core implementation, which in case of an empty address returns ipv6 string
serviceString = isZeroAddress(serviceString)
? EMPTY_IPV6_ADDRESS
? EMPTY_FULL_IPV6_ADDRESS
: serviceString;
return serviceString;
}
Expand All @@ -75,7 +85,7 @@ function isIpV4(ipAndPortString) {
* @return {boolean}
*/
function isZeroAddress(address) {
return address === EMPTY_IPV6_ADDRESS || address === EMPTY_IPV4_ADDRESS;
return EMPTY_ADDRESSES.includes(address);
}

var ip = {
Expand Down
4 changes: 2 additions & 2 deletions test/util/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('ip', function () {
});
it('Should accept only zero ipv6, if ipv6 is passed as an arg, as implemented in dashcore', function () {
var zeroaddressBuffer = ip.ipAndPortToBuffer(
constants.EMPTY_IPV6_ADDRESS
'[::]:0'
);
expect(zeroaddressBuffer.length).to.be.equal(18);
expect(zeroaddressBuffer).to.be.deep.equal(Buffer.alloc(18));
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('ip', function () {
it('Should return zero ipv6 if hex is zero, as it works in dashcore', function () {
var zeroBuffer = Buffer.alloc(18);
var ipAndPort = ip.bufferToIPAndPort(zeroBuffer);
expect(ipAndPort).to.be.equal(constants.EMPTY_IPV6_ADDRESS);
expect(ipAndPort).to.be.equal('[0:0:0:0:0:0:0:0]:0');
});
it('Should throw if buffer size is different from ip and port size', function () {
expect(ip.bufferToIPAndPort.bind(this, Buffer.alloc(19))).to.throw(
Expand Down

0 comments on commit 20d2a99

Please sign in to comment.