Create IpPrefix in IaPrefixOption constructor
The constructor is invoked during Struct#parse() at which point we might
as well try to create the IpPrefix object which will be used later. If
this fails for some reason (UnknownHostException or ClassCastException),
the prefix is bogus and should not be used.
Test: TH
Change-Id: I7e0090735022d017ba11aa85266ba19e65807da7
diff --git a/staticlibs/device/com/android/net/module/util/structs/IaPrefixOption.java b/staticlibs/device/com/android/net/module/util/structs/IaPrefixOption.java
index 89ac79d..c11bd4e 100644
--- a/staticlibs/device/com/android/net/module/util/structs/IaPrefixOption.java
+++ b/staticlibs/device/com/android/net/module/util/structs/IaPrefixOption.java
@@ -18,12 +18,17 @@
import static com.android.net.module.util.NetworkStackConstants.DHCP6_OPTION_IAPREFIX;
+import android.net.IpPrefix;
import android.util.Log;
import com.android.net.module.util.Struct;
+import com.android.net.module.util.Struct.Computed;
import com.android.net.module.util.Struct.Field;
import com.android.net.module.util.Struct.Type;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@@ -70,6 +75,9 @@
@Field(order = 5, type = Type.ByteArray, arraysize = 16)
public final byte[] prefix;
+ @Computed
+ private final IpPrefix mIpPrefix;
+
// Constructor used by Struct.parse()
protected IaPrefixOption(final short code, final short length, final long preferred,
final long valid, final byte prefixLen, final byte[] prefix) {
@@ -79,6 +87,16 @@
this.valid = valid;
this.prefixLen = prefixLen;
this.prefix = prefix.clone();
+
+ try {
+ final Inet6Address addr = (Inet6Address) InetAddress.getByAddress(prefix);
+ mIpPrefix = new IpPrefix(addr, prefixLen);
+ } catch (UnknownHostException | ClassCastException e) {
+ // UnknownHostException should never happen unless prefix is null.
+ // ClassCastException can occur when prefix is an IPv6 mapped IPv4 address.
+ // Both scenarios should throw an exception in the context of Struct#parse().
+ throw new IllegalArgumentException(e);
+ }
}
public IaPrefixOption(final short length, final long preferred, final long valid,