简体   繁体   中英

Decimal formatted IPV6 ip address or extract IPV4 from IPV6

Is there any JAVA or LINUX way to extract decimal formatted IPV6 ip address. Or Is there any JAVA or LINUX way to covert IPV6 ip to IPV4 format.

Thanks. Dnyanesh.

Is there any JAVA or LINUX way to extract decimal formatted IPV6 ip address.

No. IPv6 addresses are always presented in hexadecimal.

Or Is there any JAVA or LINUX way to covert IPV6 ip to IPV4 format.

No. They're completely different address types.

Well, IPv6 addresses are just 16 bytes (and a netmask), in the same way IPv4 addresses are 4 bytes. So, printing them in decimal is certainly possible. The question is, though, why you'd want to do that, given that everybody writes these addresses in hex.

While there is a specific address range within IPv6 for embedding IPv4 addresses (and you're free to create new ones in your own infrastructure), this is obviously not generically possible.

If your real question is "how do I talk to an IPv6 host if I only have IPv4", then the answer is more complicated and involves tunneling IPv6 traffic within IPv4. http://www.networkworld.com/news/2010/050610-ipv6-tunnel-basics.html has a comprehensive overview about the options available. The TLDR summary: you probably should use Teredo for ad-hoc access to IPv6 (Linux client: http://www.remlab.net/miredo/ ). If you want to connect a server and can't get IPv6 service from your provider, the best answer used to be 6to4 (in my experience), but these days it's more useful to tell your provider to get their act together. Or change providers.

Depends on what you mean by extract.

The IPAddress Java library will do some or all of what you ask. The javadoc is available at the link. Disclaimer: I am the project manager.

The library parses IPv4 and IPv6. It allows you to produce various strings of different formats, and you could call the toNormalizedString(IPStringOptions) method to produce an IPv6 string with a decimal format, even though IPv6 is generally hexadecimal.

The library allows you to extract an IPv4 address from an IPv6 address using IPAddress.getEmbeddedIPv4Address(), as shown:

    String str = "1::1";//ipv6 address
    IPAddressString addrString = new IPAddressString(str);
    try {
         IPAddress addr = addrString.toAddress();//parse the ipv6 address
         IPAddress addr2 = addr.getEmbeddedIPv4Address()//extract the ipv4 address 0.0.0.1 from the terminating bytes
         ...
    } catch(AddressStringException e) {
        //e.getMessage provides validation issue
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM