Java 如何将地址从 IPv4 转换为 IPv6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1555591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:04:07  来源:igfitidea点击:

How to convert an address from IPv4 to IPv6

javaipv6

提问by Chris

Is this possible? How can you convert an ipv4 to an ipv6 address?

这可能吗?如何将 ipv4 转换为 ipv6 地址?

a few example from here:

来自这里的几个例子:

0.0.0.0   -> ::
127.0.0.1 -> ::1

I'm searching a solution in Java.

我正在寻找一个 Java 解决方案。

Thanks,

谢谢,

采纳答案by Yishai

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.

没有任何有意义的 IPv4 到 IPv6 映射。诸如 0.0.0.0 和 127.0.0.1 之类的东西是规范中的特殊情况,因此它们具有相同的含义。但是给定一个 IPv4 地址,它不会告诉您它的特定 IPv6 地址是什么。您可以使用 DNS 查找来查看给定的 IP 地址是否解析为主机,而主机又会解析为 IPv6 地址和 IPv4 地址,但必须将 DNS 服务器配置为支持特定机器的该地址。

回答by Zed

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 中曾经有一个为 IPv4 地址保留的地址空间,您只需在 IPv4 地址前面加上 96 个 0 位即可。例如192.168.10.13 -> ::C0A8:0A0D。据我所知,这已被弃用,并且不再提供直接转换。

回答by Deep

Hybrid dual-stack IPv6/IPv4 implementations typically support a special class of addresses, the IPv4-mapped addresses. For more check the following link:

混合双栈 IPv6/IPv4 实现通常支持一类特殊的地址,即 IPv4 映射地址。有关更多信息,请查看以下链接:

http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses

http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses

For converting IPv4 to mapped IPv6, you can use the following:

要将 IPv4 转换为映射的 IPv6,您可以使用以下命令:

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

也检查这个

回答by MaxV

IPv6 is IPv4 compatible.

IPv6 与 IPv4 兼容。

An IPv4 address : example 192.168.99.1

IPv4 地址:例如 192.168.99.1

Step1 >

步骤 1 >

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

将第一个八位字节 (192) 除以 16(因为 Hex 是 Base-16) IE:192/16 = 12 次,剩下 0 – 十六进制中的 12 表示为 C – 十六进制中的 0(零),你猜对了it, 0 因此十六进制中的 192 是C0

Step2 >

步骤 2 >

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

对第二个八位位组 (168) 重复步骤 1,即:168/16 = 10 次,因为 10*6 = 160,因此剩下 8,– HEX 中的 10 是 A – HEX 中的 8 是 8 因此 HEX 中的 168 是A8

Step3 >

步骤 3 >

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

重复规则!!!第三个八位字节 (99) IE : 99/16 = 6 次,剩下 3 个 – HEX 中的 6 是 6 – HEX 中的 3 是 3 因此 HEX 中的 99 是63

Step4 >

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

最后一个八位字节 IE : 1/16 = 0 次,剩下 1 – 0 在十六进制中是,是的,它是 0 – 1 在十六进制中是 1 因此十六进制中的 1 是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:

因此,在 IPv6 地址部分中表示的 IPv4 地址 192.168.99.1 将是C0A8:6301。但是,您必须使用小写并添加所有缺少的字节 IPv6,因此正确的代码是:

::c0a8:6301

::c0a8:6301

or you can use a syntax now always accepted:

或者您可以使用现在始终接受的语法:

::192.168.99.1

::192.168.99.1

So in the end a IPv6 address can be the old address with the ::chars before the old address.

所以最后一个 IPv6 地址可以是旧地址,旧地址前有::字符。

回答by Sean F

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.

有多种方法可以将 IPv4 映射到 IPv6。对于大多数此类方法,转换后的 IPv4 地址放置在 16 字节 IPv6 地址的低 4 字节中。

The IPAddress Java libraryhas methods to assist with many of the most common ways of converting IPv4 to IPv6. Disclaimer: I am the project manager of that library.

IPAddress Java 库提供了一些方法来协助将 IPv4 转换为 IPv6 的许多最常见的方法。免责声明:我是那个图书馆的项目经理。

For instance, given an IPv4 address you can convert to IPv6 as shown, using IPv6-mapping conversion:

例如,给定一个 IPv4 地址,您可以使用 IPv6 映射转换,如图所示转换为 IPv6:

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.

使用 IPv6Address 实例,您可以检查地址是否是 IPv4 映射的、IPv4 兼容的、IPv4 转换的等等(这些是 IPv6 表示 IPv4 地址的多种可能方式中的一些)。之后,您可以转换回 IPv4。

if(addr.isIPv4Compatible() || addr.isIPv4Mapped()) {
    IPv4Address derivedIpv4Address = ipv6Address.getEmbeddedIPv4Address();
    byte ipv4Bytes[] = ipv4Address.getBytes();
    ...
 }

回答by VIJAYABAL DHANAPAL

Here is the conversion code in Javascript

这是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"));