* Extract the embedded IPv4 address from an IPv4-mapped IPv6 address * (0:0:0:0:0:ffff:X:Y) in any valid representation — compressed, expanded, * hex groups, or trailing dotted-decimal. Returns null if the address is * not an IPv4-mapped IPv6 address.
(addr: string)
| 185 | * not an IPv4-mapped IPv6 address. |
| 186 | */ |
| 187 | function extractMappedIPv4(addr: string): string | null { |
| 188 | const g = expandIPv6Groups(addr) |
| 189 | if (!g) return null |
| 190 | // IPv4-mapped: first 80 bits zero, next 16 bits ffff, last 32 bits = IPv4 |
| 191 | if ( |
| 192 | g[0] === 0 && |
| 193 | g[1] === 0 && |
| 194 | g[2] === 0 && |
| 195 | g[3] === 0 && |
| 196 | g[4] === 0 && |
| 197 | g[5] === 0xffff |
| 198 | ) { |
| 199 | const hi = g[6]! |
| 200 | const lo = g[7]! |
| 201 | return `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}` |
| 202 | } |
| 203 | return null |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * A dns.lookup-compatible function that resolves a hostname and rejects |
no test coverage detected