Core Java

Java IPv4 to IPv6 Conversion

With the growing demand for IP addresses, IPv6 was introduced as a successor to IPv4. While IPv4 uses 32-bit addresses, IPv6 uses 128-bit addresses, allowing for a vastly larger address space. In modern applications, developers may need to convert IPv4 addresses into IPv6 format for compatibility, especially when working with dual-stack networks or transitioning systems.

1. What Does Converting Mean?

Converting an IPv4 address to IPv6 does not mean altering or replacing the original address; instead, it means representing the same IPv4 address in a format that is compatible with IPv6 systems. This is important in modern networks where both IPv4 and IPv6 coexist (dual-stack environments). The most widely used technique for this is the IPv4-mapped IPv6 address, which embeds the IPv4 address within an IPv6 structure so that IPv6-enabled applications and systems can process it without losing the original information. The standard format for an IPv4-mapped IPv6 address is:

::ffff:IPv4-address

Here, :: represents a sequence of leading zeros (the first 80 bits), and ffff (16 bits set to 1) acts as a marker indicating that the remaining portion contains an IPv4 address. The last 32 bits store the actual IPv4 address. For example:

IPv4: 192.168.1.1
IPv6: ::ffff:192.168.1.1

Internally, the IPv4 part (192.168.1.1) is converted into hexadecimal and placed in the last two segments of the IPv6 address (e.g., c0a8:0101). Some systems, like Java, may display this hexadecimal form instead of the dotted-decimal notation. This mapping mechanism is especially useful for networking libraries, sockets, and servers that are designed to work with IPv6 but still need to handle IPv4 traffic. It ensures backward compatibility, simplifies network stack implementations, and allows seamless communication between IPv4 and IPv6 systems without requiring separate handling logic for each protocol.

2. Code Example

Below is a detailed Java example demonstrating how to convert an IPv4 address into an IPv6-mapped address.

// IPv4ToIPv6Converter.java
import java.net.InetAddress;
import java.net.UnknownHostException;

public class IPv4ToIPv6Converter {

    public static String convertToIPv6(String ipv4) throws UnknownHostException {
        // Convert IPv4 string to InetAddress
        InetAddress ipv4Address = InetAddress.getByName(ipv4);

        // Get byte representation of IPv4
        byte[] ipv4Bytes = ipv4Address.getAddress();

        // Create a 16-byte array for IPv6
        byte[] ipv6Bytes = new byte[16];

        // First 10 bytes are 0
        for (int i = 0; i < 10; i++) {
            ipv6Bytes[i] = 0;
        }

        // Next 2 bytes are 0xFF
        ipv6Bytes[10] = (byte) 0xFF;
        ipv6Bytes[11] = (byte) 0xFF;

        // Last 4 bytes are IPv4 bytes
        System.arraycopy(ipv4Bytes, 0, ipv6Bytes, 12, 4);

        // Convert byte array to InetAddress
        InetAddress ipv6Address = InetAddress.getByAddress(ipv6Bytes);

        return ipv6Address.getHostAddress();
    }

    public static void main(String[] args) {
        try {
            String ipv4 = "192.168.1.1";
            String ipv6 = convertToIPv6(ipv4);

            System.out.println("IPv4 Address: " + ipv4);
            System.out.println("Converted IPv6 Address: " + ipv6);

        } catch (UnknownHostException e) {
            System.out.println("Invalid IP Address: " + e.getMessage());
        }
    }
}

2.1 Code Explanation

The provided Java program defines a class IPv4ToIPv6Converter that converts an IPv4 address into an IPv4-mapped IPv6 address using Java’s networking APIs. The convertToIPv6 method begins by converting the input IPv4 string into an InetAddress object using InetAddress.getByName(), which also validates the address. It then retrieves the raw 4-byte representation of the IPv4 address via getAddress(). Since IPv6 addresses are 16 bytes long, a new byte array of size 16 is created. The first 10 bytes are initialized to 0 to follow the IPv6 format, and the next two bytes are explicitly set to 0xFF to indicate that this is an IPv4-mapped IPv6 address. The final 4 bytes of the IPv6 array are populated with the original IPv4 bytes using System.arraycopy(), effectively embedding the IPv4 address into the IPv6 structure. This 16-byte array is then converted back into an InetAddress object using InetAddress.getByAddress(), and its human-readable representation is returned using getHostAddress(). In the main method, a sample IPv4 address (192.168.1.1) is passed to the conversion method, and both the original and converted addresses are printed. The code also includes exception handling for UnknownHostException to gracefully handle invalid input addresses.

2.2 Code Output

IPv4 Address: 192.168.1.1
Converted IPv6 Address: 0:0:0:0:0:ffff:c0a8:101

The output shows the original IPv4 address 192.168.1.1 and its corresponding IPv4-mapped IPv6 representation. The converted value 0:0:0:0:0:ffff:c0a8:101 follows the standard IPv4-mapped IPv6 format, where the first five segments are zeros, the sixth segment ffff indicates that the address is an IPv4-mapped IPv6 address, and the last two segments represent the original IPv4 address in hexadecimal form. Specifically, the IPv4 bytes (192, 168, 1, 1) are converted into hexadecimal: 192 becomes c0, 168 becomes a8, 1 becomes 01, and 1 becomes 01, which are combined as c0a8:0101 (displayed as c0a8:101 after removing leading zeros). This demonstrates how the IPv4 address is embedded within the IPv6 structure while maintaining compatibility with IPv6 systems.

Note: Java may display the IPv6 address in hexadecimal format instead of the familiar dotted notation.

3. Conclusion

Converting IPv4 to IPv6 in Java is straightforward when using IPv4-mapped IPv6 addresses. This approach ensures compatibility between legacy systems and modern networking environments. Understanding the byte-level structure of IP addresses helps in building robust networking applications. As systems continue transitioning to IPv6, such conversions will become increasingly relevant, especially for developers working on networking, cloud, and distributed systems.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button