Skip to content

Commit f33c079

Browse files
committed
fix incorrect mapping of IPv4 addresses in IPv6
1 parent c7124b2 commit f33c079

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/IPv6.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,14 @@ export class IPv6 extends IPAddress {
8181
* @internal
8282
*/
8383
private static parseHextet(hextet: string): number | number[] {
84-
return IPv4.regex.test(hextet)
85-
? Array.from(new Uint16Array(IPv4.fromString(hextet).binary().buffer))
86-
: Number.parseInt(hextet, 16);
84+
if (IPv4.regex.test(hextet)) {
85+
const ip = IPv4.fromString(hextet).binary();
86+
return [
87+
(ip[0]! << 8) | ip[1]!,
88+
(ip[2]! << 8) | ip[3]!
89+
]
90+
}
91+
return Number.parseInt(hextet, 16);
8792
}
8893

8994
/**
@@ -116,7 +121,13 @@ export class IPv6 extends IPAddress {
116121
* @see {@link IPv6#hasMappedIPv4}
117122
*/
118123
public getMappedIPv4(): IPv4 {
119-
return IPv4.fromBinary(new Uint8Array(this.binary().buffer).slice(-4));
124+
const bin = this.binary().slice(-2);
125+
return IPv4.fromBinary(new Uint8Array([
126+
(bin[0]! >> 8) & 0xFF,
127+
bin[0]! & 0xFF,
128+
(bin[1]! >> 8) & 0xFF,
129+
bin[1]! & 0xFF
130+
]));
120131
}
121132

122133
public override toString(): string {

0 commit comments

Comments
 (0)