From 8643db8411bc62ef8e6ff25a33efd239cc6800d1 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Fri, 19 Jul 2024 19:40:10 +0700 Subject: [PATCH] fix: unknown empty service IP --- lib/constants/index.js | 2 -- lib/util/ip.js | 18 ++++++++++++++---- test/util/ip.js | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/constants/index.js b/lib/constants/index.js index 1de610f1a..bac664c53 100644 --- a/lib/constants/index.js +++ b/lib/constants/index.js @@ -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, diff --git a/lib/util/ip.js b/lib/util/ip.js index f203fedd5..4890909f9 100644 --- a/lib/util/ip.js +++ b/lib/util/ip.js @@ -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 @@ -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; } @@ -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 = { diff --git a/test/util/ip.js b/test/util/ip.js index c31b51044..352f6de55 100644 --- a/test/util/ip.js +++ b/test/util/ip.js @@ -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)); @@ -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(