From e3e34bdf3a7ab31d36aad9b3010f9d8de1ec53a0 Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Mon, 30 Oct 2023 19:42:52 +0900 Subject: [PATCH] Handle v4-mapped v6 address in Struct parsing testV4MappedV6Address fails without change in Struct.java Bug: 295800201 Test: atest ConnectivityCoverageTests Change-Id: I4a40bc47b051860c6420f211491e2ecd34c1d732 --- .../device/com/android/net/module/util/Struct.java | 9 ++++++++- .../unit/src/com/android/net/module/util/StructTest.java | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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;