From ed6de8faa6d969c443218aba00e7ae11f00f8598 Mon Sep 17 00:00:00 2001 From: KVR Vishnu <46535652+VISHNU0906@users.noreply.github.com> Date: Tue, 1 Dec 2026 20:24:17 +0530 Subject: [PATCH] fix(network): read IPv6 addresses that end in an IPv4 address --- src/engine/operations/network.ts | 11 ++++++++++- src/engine/operations/packets.ts | 11 ++++++++++- src/engine/tests/operations.verified.test.ts | 6 ++++++ src/engine/tests/packets.test.ts | 6 ++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/engine/operations/network.ts b/src/engine/operations/network.ts index 880354a..cdab8ac 100644 --- a/src/engine/operations/network.ts +++ b/src/engine/operations/network.ts @@ -46,8 +46,17 @@ function formatIPv4(value: number): string { return [24, 16, 8, 0].map((shift) => (value >>> shift) & 0xff).join('.'); } +// RFC 4291 2.2 lets the last 32 bits be written as an IPv4 address +// (::ffff:192.0.2.1); turn that into the two hex groups it stands for. +function embeddedIPv4ToHex(text: string): string { + const mixed = /^(.*:)([^:]*\.[^:]*)$/.exec(text); + if (!mixed) return text; + const value = parseIPv4(mixed[2]!); + return `${mixed[1]}${(value >>> 16).toString(16)}:${(value & 0xffff).toString(16)}`; +} + function expandIPv6(text: string): string { - const trimmed = text.trim(); + const trimmed = embeddedIPv4ToHex(text.trim()); const halves = trimmed.split('::'); if (halves.length > 2) throw new OperationError('An IPv6 address may contain only one "::".'); diff --git a/src/engine/operations/packets.ts b/src/engine/operations/packets.ts index 585da6c..427bf18 100644 --- a/src/engine/operations/packets.ts +++ b/src/engine/operations/packets.ts @@ -102,7 +102,16 @@ function checksumLine(given: number, header: Uint8Array, zeroAt: number): string function expandIpv6(address: string): number[] { - const trimmed = address.trim().replace(/^\[|\]$/g, '').split('%')[0] as string; + let trimmed = address.trim().replace(/^\[|\]$/g, '').split('%')[0] as string; + // RFC 4291 2.2: the last 32 bits may be written as an IPv4 address (::ffff:192.0.2.1). + const mixed = /^(.*:)(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(trimmed); + if (mixed) { + const [a, b, c, d] = mixed.slice(2).map(Number) as [number, number, number, number]; + if ([a, b, c, d].some((octet) => octet > 255)) { + throw new OperationError(`'${mixed.slice(2).join('.')}' is not an IPv4 address.`); + } + trimmed = `${mixed[1]}${((a << 8) | b).toString(16)}:${((c << 8) | d).toString(16)}`; + } const halves = trimmed.split('::'); if (halves.length > 2) throw new OperationError('An address can hold only one :: gap.'); diff --git a/src/engine/tests/operations.verified.test.ts b/src/engine/tests/operations.verified.test.ts index 4bb28ff..ae87ea7 100644 --- a/src/engine/tests/operations.verified.test.ts +++ b/src/engine/tests/operations.verified.test.ts @@ -179,6 +179,12 @@ describe('arithmetic and set operations compute the right numbers', () => { expect(await text('2001:db8::1', 'expand-ipv6')).toBe('2001:0db8:0000:0000:0000:0000:0000:0001'); expect(await text('2001:0db8:0000:0000:0000:0000:0000:0001', 'compress-ipv6')).toBe('2001:db8::1'); }); + + it('IPv6 addresses that end in an IPv4 address expand and compress', async () => { + expect(await text('::ffff:192.168.1.1', 'expand-ipv6')).toBe('0000:0000:0000:0000:0000:ffff:c0a8:0101'); + expect(await text('64:ff9b:0:0:0:0:192.0.2.33', 'compress-ipv6')).toBe('64:ff9b::c000:221'); + expect(await refusal('::ffff:192.168.1.256', 'expand-ipv6')).toMatch(/outside 0-255/); + }); }); describe('bitwise operations change exactly the bits they should', () => { diff --git a/src/engine/tests/packets.test.ts b/src/engine/tests/packets.test.ts index 27e841e..ced5216 100644 --- a/src/engine/tests/packets.test.ts +++ b/src/engine/tests/packets.test.ts @@ -175,6 +175,12 @@ describe('addresses', () => { expect( await run('::ffff:c0a8:1', ['ipv6-transition-addresses', { Direction: 'IPv6 to IPv4' }]), ).toBe('IPv4-mapped address of 192.168.0.1'); + expect( + await run('::ffff:192.168.0.1', ['ipv6-transition-addresses', { Direction: 'IPv6 to IPv4' }]), + ).toBe('IPv4-mapped address of 192.168.0.1'); + expect(await run('::ffff:10.0.0.1', 'parse-ipv6-address')).toContain( + 'Full: 0000:0000:0000:0000:0000:ffff:0a00:0001', + ); }); it('rewrites MAC addresses in every notation asked for', async () => {