Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/engine/operations/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "::".');

Expand Down
11 changes: 10 additions & 1 deletion src/engine/operations/packets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');

Expand Down
6 changes: 6 additions & 0 deletions src/engine/tests/operations.verified.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/engine/tests/packets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Loading