Skip to content

Commit 20d2a99

Browse files
authored
fix: unknown empty service IP (#299)
1 parent 02a97cc commit 20d2a99

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

lib/constants/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ module.exports = {
3636
PORT: 2,
3737
},
3838
IP_ADDRESS_SIZE: 16,
39-
EMPTY_IPV6_ADDRESS: '[0:0:0:0:0:0:0:0]:0',
40-
EMPTY_IPV4_ADDRESS: '0.0.0.0:0',
4139
CURRENT_PROTOCOL_VERSION: 70211,
4240
SML_ENTRY_VERSION_1_SIZE: 151,
4341
SML_ENTRY_TYPE_2_ADDITION_SIZE: 22,

lib/util/ip.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ var serviceRegex =
88
/^(([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]+$/;
99
var ipV6prefix = Buffer.from('00000000000000000000ffff', 'hex');
1010
var emptyAddress = Buffer.alloc(18);
11-
var EMPTY_IPV6_ADDRESS = constants.EMPTY_IPV6_ADDRESS;
12-
var EMPTY_IPV4_ADDRESS = constants.EMPTY_IPV4_ADDRESS;
11+
var EMPTY_FULL_IPV6_ADDRESS = '[0:0:0:0:0:0:0:0]:0';
12+
var EMPTY_SHORT_IPV6_ADDRESS = '[::]:0';
13+
var EMPTY_SHORT_ZERO_IPV6_ADDRESS = '[::0]:0';
14+
var EMPTY_FULL_IPV4_ADDRESS = '0.0.0.0:0';
15+
var EMPTY_SHORT_IPV4_ADDRESS = '0:0';
16+
var EMPTY_ADDRESSES = [
17+
EMPTY_FULL_IPV6_ADDRESS,
18+
EMPTY_SHORT_IPV6_ADDRESS,
19+
EMPTY_SHORT_ZERO_IPV6_ADDRESS,
20+
EMPTY_FULL_IPV4_ADDRESS,
21+
EMPTY_SHORT_IPV4_ADDRESS,
22+
];
1323

1424
/**
1525
* Maps ipv4:port to ipv6 buffer and port
@@ -56,7 +66,7 @@ function bufferToIPAndPort(buffer) {
5666
var serviceString = ipV4string + ':' + String(port);
5767
// This is a hack to match core implementation, which in case of an empty address returns ipv6 string
5868
serviceString = isZeroAddress(serviceString)
59-
? EMPTY_IPV6_ADDRESS
69+
? EMPTY_FULL_IPV6_ADDRESS
6070
: serviceString;
6171
return serviceString;
6272
}
@@ -75,7 +85,7 @@ function isIpV4(ipAndPortString) {
7585
* @return {boolean}
7686
*/
7787
function isZeroAddress(address) {
78-
return address === EMPTY_IPV6_ADDRESS || address === EMPTY_IPV4_ADDRESS;
88+
return EMPTY_ADDRESSES.includes(address);
7989
}
8090

8191
var ip = {

test/util/ip.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('ip', function () {
2222
});
2323
it('Should accept only zero ipv6, if ipv6 is passed as an arg, as implemented in dashcore', function () {
2424
var zeroaddressBuffer = ip.ipAndPortToBuffer(
25-
constants.EMPTY_IPV6_ADDRESS
25+
'[::]:0'
2626
);
2727
expect(zeroaddressBuffer.length).to.be.equal(18);
2828
expect(zeroaddressBuffer).to.be.deep.equal(Buffer.alloc(18));
@@ -61,7 +61,7 @@ describe('ip', function () {
6161
it('Should return zero ipv6 if hex is zero, as it works in dashcore', function () {
6262
var zeroBuffer = Buffer.alloc(18);
6363
var ipAndPort = ip.bufferToIPAndPort(zeroBuffer);
64-
expect(ipAndPort).to.be.equal(constants.EMPTY_IPV6_ADDRESS);
64+
expect(ipAndPort).to.be.equal('[0:0:0:0:0:0:0:0]:0');
6565
});
6666
it('Should throw if buffer size is different from ip and port size', function () {
6767
expect(ip.bufferToIPAndPort.bind(this, Buffer.alloc(19))).to.throw(

0 commit comments

Comments
 (0)