|
| 1 | +package com.thealgorithms.conversions; |
| 2 | + |
| 3 | +import java.net.InetAddress; |
| 4 | +import java.net.UnknownHostException; |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +/** |
| 8 | + * A utility class for converting between IPv6 and IPv4 addresses. |
| 9 | + * |
| 10 | + * - Converts IPv4 to IPv6-mapped IPv6 address. |
| 11 | + * - Extracts IPv4 address from IPv6-mapped IPv6. |
| 12 | + * - Handles exceptions for invalid inputs. |
| 13 | + * |
| 14 | + * @author Hardvan |
| 15 | + */ |
| 16 | +public final class IPv6Converter { |
| 17 | + private IPv6Converter() { |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Converts an IPv4 address (e.g., "192.0.2.128") to an IPv6-mapped IPv6 address. |
| 22 | + * Example: IPv4 "192.0.2.128" -> IPv6 "::ffff:192.0.2.128" |
| 23 | + * |
| 24 | + * @param ipv4Address The IPv4 address in string format. |
| 25 | + * @return The corresponding IPv6-mapped IPv6 address. |
| 26 | + * @throws UnknownHostException If the IPv4 address is invalid. |
| 27 | + * @throws IllegalArgumentException If the IPv6 address is not a mapped IPv4 address. |
| 28 | + */ |
| 29 | + public static String ipv4ToIpv6(String ipv4Address) throws UnknownHostException { |
| 30 | + if (ipv4Address == null || ipv4Address.isEmpty()) { |
| 31 | + throw new UnknownHostException("IPv4 address is empty."); |
| 32 | + } |
| 33 | + |
| 34 | + InetAddress ipv4 = InetAddress.getByName(ipv4Address); |
| 35 | + byte[] ipv4Bytes = ipv4.getAddress(); |
| 36 | + |
| 37 | + // Create IPv6-mapped IPv6 address (starts with ::ffff:) |
| 38 | + byte[] ipv6Bytes = new byte[16]; |
| 39 | + ipv6Bytes[10] = (byte) 0xff; |
| 40 | + ipv6Bytes[11] = (byte) 0xff; |
| 41 | + System.arraycopy(ipv4Bytes, 0, ipv6Bytes, 12, 4); |
| 42 | + |
| 43 | + // Manually format to "::ffff:x.x.x.x" format |
| 44 | + StringBuilder ipv6String = new StringBuilder("::ffff:"); |
| 45 | + for (int i = 12; i < 16; i++) { |
| 46 | + ipv6String.append(ipv6Bytes[i] & 0xFF); |
| 47 | + if (i < 15) { |
| 48 | + ipv6String.append('.'); |
| 49 | + } |
| 50 | + } |
| 51 | + return ipv6String.toString(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Extracts the IPv4 address from an IPv6-mapped IPv6 address. |
| 56 | + * Example: IPv6 "::ffff:192.0.2.128" -> IPv4 "192.0.2.128" |
| 57 | + * |
| 58 | + * @param ipv6Address The IPv6 address in string format. |
| 59 | + * @return The extracted IPv4 address. |
| 60 | + * @throws UnknownHostException If the IPv6 address is invalid or not a mapped IPv4 address. |
| 61 | + */ |
| 62 | + public static String ipv6ToIpv4(String ipv6Address) throws UnknownHostException { |
| 63 | + InetAddress ipv6 = InetAddress.getByName(ipv6Address); |
| 64 | + byte[] ipv6Bytes = ipv6.getAddress(); |
| 65 | + |
| 66 | + // Check if the address is an IPv6-mapped IPv4 address |
| 67 | + if (isValidIpv6MappedIpv4(ipv6Bytes)) { |
| 68 | + byte[] ipv4Bytes = Arrays.copyOfRange(ipv6Bytes, 12, 16); |
| 69 | + InetAddress ipv4 = InetAddress.getByAddress(ipv4Bytes); |
| 70 | + return ipv4.getHostAddress(); |
| 71 | + } else { |
| 72 | + throw new IllegalArgumentException("Not a valid IPv6-mapped IPv4 address."); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Helper function to check if the given byte array represents |
| 78 | + * an IPv6-mapped IPv4 address (prefix 0:0:0:0:0:ffff). |
| 79 | + * |
| 80 | + * @param ipv6Bytes Byte array representation of the IPv6 address. |
| 81 | + * @return True if the address is IPv6-mapped IPv4, otherwise false. |
| 82 | + */ |
| 83 | + private static boolean isValidIpv6MappedIpv4(byte[] ipv6Bytes) { |
| 84 | + // IPv6-mapped IPv4 addresses are 16 bytes long, with the first 10 bytes set to 0, |
| 85 | + // followed by 0xff, 0xff, and the last 4 bytes representing the IPv4 address. |
| 86 | + if (ipv6Bytes.length != 16) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + for (int i = 0; i < 10; i++) { |
| 91 | + if (ipv6Bytes[i] != 0) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return ipv6Bytes[10] == (byte) 0xff && ipv6Bytes[11] == (byte) 0xff; |
| 97 | + } |
| 98 | +} |
0 commit comments