Is this possible? How can you convert an ipv4 to an ipv6 address?
a few example from here:
0.0.0.0 -&g开发者_运维百科t; ::
127.0.0.1 -> ::1
I'm searching a solution in Java.
Thanks,
There is no IPv4 to IPv6 mapping that is meaningful. things like 0.0.0.0 and 127.0.0.1 are special cases in the spec, so they have equivalent meaning. But given an IPv4 address it tells you nothing about what its specific IPv6 address would be. You can use a DNS lookup to see if a given IP address resolves to a host which in turn resolves to an IPv6 address in addition to an IPv4 address, but the DNS server would have to be configured to support that for the specific machine.
Hybrid dual-stack IPv6/IPv4 implementations typically support a special class of addresses, the IPv4-mapped addresses. For more check the following link:
http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses
For converting IPv4 to mapped IPv6, you can use the following:
String ip = "127.0.0.1";
String[] octets = ip.split("\\.");
byte[] octetBytes = new byte[4];
for (int i = 0; i < 4; ++i) {
octetBytes[i] = (byte) Integer.parseInt(octets[i]);
}
byte ipv4asIpV6addr[] = new byte[16];
ipv4asIpV6addr[10] = (byte)0xff;
ipv4asIpV6addr[11] = (byte)0xff;
ipv4asIpV6addr[12] = octetBytes[0];
ipv4asIpV6addr[13] = octetBytes[1];
ipv4asIpV6addr[14] = octetBytes[2];
ipv4asIpV6addr[15] = octetBytes[3];
Also check this
There used to be a reserved address space in IPv6 for IPv4 addresses, where you simply prefixed the IPv4 address with 96 0-bits. E.g. 192.168.10.13 -> ::C0A8:0A0D
. As I know this has been deprecated, and there's no direct conversion available anymore.
IPv6 is IPv4 compatible.
An IPv4 address : example 192.168.99.1
Step1 >
Divide the first octet (192) by 16 (since Hex is a Base-16) IE : 192/16 = 12 times exactly with 0 left over – 12 in Hex is represented as C – 0 (zero) in Hex is, you guessed it, 0 Thus 192 in HEX is C0
Step2 >
Repeat step 1 with the second octet (168), IE : 168/16 = 10 times with 8 left over because 10*6 = 160, – 10 in HEX is A – 8 in HEX is 8 Thus 168 in HEX is A8
Step3 >
Repetition rules!!! Third octet (99) IE : 99/16 = 6 times with 3 left over – 6 in HEX is 6 – 3 in HEX is 3 Thus 99 in HEX is 63
Step4 >
Last octet IE : 1/16 = 0 times with 1 left over – 0 in HEX is, yeah it is 0 – 1 in HEX is 1 Thus 1 in HEX is 01
So the IPv4 address of 192.168.99.1, represented in the IPv6 address portion would be C0A8:6301. However you have to use lower case and add all the missing bytes IPv6, so the correct code is:
::c0a8:6301
or you can use a syntax now always accepted:
::192.168.99.1
So in the end a IPv6 address can be the old address with the :: chars before the old address.
There are numerous methods to map IPv4 to IPv6. For most such methods, the converted IPv4 address is placed in the lower 4 bytes of the 16 byte IPv6 address.
The IPAddress Java library has methods to assist with many of the most common ways of converting IPv4 to IPv6. Disclaimer: I am the project manager of that library.
For instance, given an IPv4 address you can convert to IPv6 as shown, using IPv6-mapping conversion:
IPv6Address ipv6Address = new IPAddressString("1.2.3.4").getAddress().toIPv4().
getIPv4MappedAddress();
System.out.println(ipv6Address); // ::ffff:102:304
System.out.println(ipv6Address.toMixedString()); // ::ffff:1.2.3.4
With an IPv6Address instance you can check if the address is IPv4 mapped, IPv4 compatible, IPv4 translated, and so on (those are some of the many possible ways IPv6 represents IPv4 addresses). Afterwards, you can convert back to to IPv4.
if(addr.isIPv4Compatible() || addr.isIPv4Mapped()) {
IPv4Address derivedIpv4Address = ipv6Address.getEmbeddedIPv4Address();
byte ipv4Bytes[] = ipv4Address.getBytes();
...
}
Here is the conversion code in Javascript
/** IPV4 CIDR to IPV6 CIDR conversion **/
function covertIPv6(x){
let ipV4 = x;
let address = ipV4.split('/');
let classValues = [];
if(address.length){
classValues = address[0].split('.');
}
if(classValues.length){
let str = classValues.reduce((acc, val, ind)=>{
let mod = +val >= 16 ? +val%16 : +val;
let divider = +val >= 16 ? (val-mod)/16 : 0;
const hexaCode = (hexaVal)=>{
switch(hexaVal){
case 10:
hexaVal = 'A';
break;
case 11:
hexaVal = 'B';
break;
case 12:
hexaVal = 'C';
break;
case 13:
hexaVal = 'D';
break;
case 14:
hexaVal = 'E';
break;
case 15:
hexaVal = 'F';
break;
default:
hexaVal = hexaVal;
break;
}
return hexaVal;
}
mod = hexaCode(mod);
divider = hexaCode(divider);
return ind === 1 ? `${acc}${divider}${mod}:`:`${acc}${divider}${mod}`
},'')
return `2002:${str}::/${address[1]}`;
}
return "Invalid Address";
}
// Run the function
console.log(covertIPv6("0.0.0.0/12"));
精彩评论