From c375cbb4406210ac94c5f1794201673a7a0b4b48 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Wed, 24 Jan 2024 00:05:16 +0100 Subject: [PATCH] fix parsing of some IPv4-embedded IPv6 adresses fixes #171 --- lib/ipaddr.js | 6 +++++- test/ipaddr.test.js | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/ipaddr.js b/lib/ipaddr.js index 235c66b..d203f0e 100644 --- a/lib/ipaddr.js +++ b/lib/ipaddr.js @@ -874,7 +874,11 @@ } if ((match = string.match(ipv6Regexes.transitional))) { zoneId = match[6] || ''; - addr = expandIPv6(match[1].slice(0, -1) + zoneId, 6); + addr = match[1] + if (!match[1].endsWith('::')) { + addr = addr.slice(0, -1) + } + addr = expandIPv6(addr + zoneId, 6); if (addr.parts) { octets = [ parseInt(match[2]), diff --git a/test/ipaddr.test.js b/test/ipaddr.test.js index b5a29b2..eb405df 100644 --- a/test/ipaddr.test.js +++ b/test/ipaddr.test.js @@ -354,6 +354,8 @@ describe('ipaddr', () => { assert.deepEqual(ipaddr.IPv6.parse('2001:db8:F53A::').parts, [0x2001, 0xdb8, 0xf53a, 0, 0, 0, 0, 0]); assert.deepEqual(ipaddr.IPv6.parse('::1').parts, [0, 0, 0, 0, 0, 0, 0, 1]); assert.deepEqual(ipaddr.IPv6.parse('::8.8.8.8').parts, [0, 0, 0, 0, 0, 0xffff, 2056, 2056]); + assert.deepEqual(ipaddr.IPv6.parse('FFFF::255.255.255.255').parts, [0xffff, 0, 0, 0, 0, 0, 0xffff, 0xffff]); + assert.deepEqual(ipaddr.IPv6.parse('64:ff9a::0.0.0.0').parts, [0x64, 0xff9a, 0, 0, 0, 0, 0, 0]); assert.deepEqual(ipaddr.IPv6.parse('::').parts, [0, 0, 0, 0, 0, 0, 0, 0]); assert.deepEqual(ipaddr.IPv6.parse('::%z').parts, [0, 0, 0, 0, 0, 0, 0, 0]); assert.deepEqual(ipaddr.IPv6.parse('::%z').zoneId, 'z');