Validator checking IPv4 mapped IPv6 addresses
I'm trying to use the validator to know whether an IP address is IPv4 or IPv6.
const [isIPv4] = Validator.isValidIPv4String(host);
const [isIPv6] = Validator.isValidIPv6String(host);
if (isIPv4) {
return [host as Host, 'udp4'];
} else if (isIPv6) {
return [host as Host, 'udp6'];
} else {
// THIS IS A HOSTNAME not an IP address!
}
Well the issue is that if I pass it an "IPv4 mapped IPv6 address" like ::ffff:127.0.0.1, it ends up in the else block.
This means in my code, it thinks its a hostname, and not a host.
I think the validator should accept that IPv4 mapped IPv6 addresses are ipv6 addresses. Because they are legitimate ipv6 address that can be used later.
Thanks for reporting this. A bit busy at the moment but I'll try and look into this over the weekend
Cool! If you do fix it, please release as soon as you can as I'm trying to implement a QUIC library that relies on this.
I noticed that new IPv6('::ffff:127.0.0.1'); also fails with Given string is already longer than given final length after padding: 4.
So I have found out that this library prefers this format:
::ffff:7f00:1
Even though this is the equivalent:
::ffff:127.0.0.1
So nodejs dgram socket actually accepts both formats and both work.
I had a work around like this:
function isIPv4MappedIPv6(host: string) {
if (host.startsWith('::ffff:')) {
const ipv4 = host.slice('::ffff:'.length);
// so it turns out ::ffff:7f00:1 is also the same as ::ffff:127.0.0.1
// return true;
if (isIPv4(ipv4)) {
return true;
}
}
return false;
}
But that ended up failing on ::ffff:7f00:1. So I actually need to parse the hex digits too...