diff --git a/staticlibs/device/com/android/net/module/util/Struct.java b/staticlibs/device/com/android/net/module/util/Struct.java index dc0d19b206..ff7a711899 100644 --- a/staticlibs/device/com/android/net/module/util/Struct.java +++ b/staticlibs/device/com/android/net/module/util/Struct.java @@ -422,7 +422,14 @@ public class Struct { final byte[] address = new byte[isIpv6 ? 16 : 4]; buf.get(address); try { - value = InetAddress.getByAddress(address); + if (isIpv6) { + // Using Inet6Address.getByAddress since InetAddress.getByAddress converts + // v4-mapped v6 address to v4 address internally and returns Inet4Address. + value = Inet6Address.getByAddress( + null /* host */, address, -1 /* scope_id */); + } else { + value = InetAddress.getByAddress(address); + } } catch (UnknownHostException e) { throw new IllegalArgumentException("illegal length of IP address", e); } diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/StructTest.java b/staticlibs/tests/unit/src/com/android/net/module/util/StructTest.java index b4da043953..a39b7a3263 100644 --- a/staticlibs/tests/unit/src/com/android/net/module/util/StructTest.java +++ b/staticlibs/tests/unit/src/com/android/net/module/util/StructTest.java @@ -765,6 +765,14 @@ public class StructTest { msg.writeToBytes(ByteOrder.BIG_ENDIAN)); } + @Test + public void testV4MappedV6Address() { + final IpAddressMessage msg = doParsingMessageTest("c0a86401" + + "00000000000000000000ffffc0a86401", IpAddressMessage.class, ByteOrder.BIG_ENDIAN); + assertEquals(TEST_IPV4_ADDRESS, msg.ipv4Address); + assertEquals(InetAddressUtils.v4MappedV6Address(TEST_IPV4_ADDRESS), msg.ipv6Address); + } + public static class WrongIpAddressType extends Struct { @Field(order = 0, type = Type.Ipv4Address) public byte[] ipv4Address; @Field(order = 1, type = Type.Ipv6Address) public byte[] ipv6Address;