The idea is very simple: look inside the IPv6 address whether part of the IPv4 address appears...
Code is:
function is6RD(addr4, addr6) { var bytes = addr4.split('.') ; var i, addr4Bits, addr6Bits, chunk, index ;This code can be tried at http://test-ipv6.vyncke.org
// Now process the IPv4 address by generating a string of 32 '0' or '1' based on the host address addr4Bits = '' ; for (i = 0 ; i < 4 ; i++) { chunk = parseInt(bytes[i], 10).toString(2) ; while (chunk.length < 8) chunk = '0' + chunk ; addr4Bits = addr4Bits + chunk ; }
// Now process the IPv6 address by generating a string of 64 '0' or '1' based on the /64 prefix bytes = addr6.split(':') ; addr6Bits = '' ; for (i = 0 ; i < 4 ; i++) { chunk = parseInt(bytes[i], 16).toString(2) ; while (chunk.length < 16) chunk = '0' + chunk ; addr6Bits = addr6Bits + chunk ; }
// Now check whether full or at least 16 bits of the IPv4 is embedded into the IPv6 prefix while (addr4Bits.length > 16) { index = addr6Bits.indexOf(addr4Bits) ; // Is part of the IPv4 address embedded in the /64 of IPv6? if (index >= 0) return addr4Bits.length + " bits of the IPv4 address are embedded in the IPv6 address starting at " + index ; // Else, remove one leading bit of addr4
addr4Bits = addr4Bits.substr(1, addr4Bits.length-1) ; } return 0
}
Aucun commentaire:
Enregistrer un commentaire