Skip to content
Snippets Groups Projects
Commit 49967eab authored by Patrick Rohr's avatar Patrick Rohr Committed by Gerrit Code Review
Browse files

Merge changes Idce2024f,I7e009073,I29fe506c,I37b2a0b8,Iccb4e3ec into main

* changes:
  Add toString for more consistent logging to IaPrefixOption
  Create IpPrefix in IaPrefixOption constructor
  Add @Computed annotation to Struct
  Remove check that preferred lifetime >= t2
  Remove IA prefix option option-code from public constructor
parents 0cfaea8d 8f77f016
No related branches found
No related tags found
No related merge requests found
......@@ -146,6 +146,14 @@ public class Struct {
int arraysize() default 0;
}
/**
* Indicates that this field contains a computed value and is ignored for the purposes of Struct
* parsing.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Computed {}
private static class FieldInfo {
@NonNull
public final Field annotation;
......@@ -533,6 +541,7 @@ public class Struct {
final FieldInfo[] annotationFields = new FieldInfo[getAnnotationFieldCount(clazz)];
for (java.lang.reflect.Field field : clazz.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())) continue;
if (field.getAnnotation(Computed.class) != null) continue;
final Field annotation = field.getAnnotation(Field.class);
if (annotation == null) {
......
......@@ -18,12 +18,17 @@ package com.android.net.module.util.structs;
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,7 +75,11 @@ public class IaPrefixOption extends Struct {
@Field(order = 5, type = Type.ByteArray, arraysize = 16)
public final byte[] prefix;
public IaPrefixOption(final short code, final short length, final long preferred,
@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) {
this.code = code;
this.length = length;
......@@ -78,30 +87,43 @@ public class IaPrefixOption extends Struct {
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,
final byte prefixLen, final byte[] prefix) {
this((byte) DHCP6_OPTION_IAPREFIX, length, preferred, valid, prefixLen, prefix);
}
/**
* Check whether or not IA Prefix option in IA_PD option is valid per RFC8415#section-21.22.
*
* Note: an expired prefix can still be valid.
*/
public boolean isValid(int t2) {
if (preferred < 0 || valid < 0) {
Log.w(TAG, "IA_PD option with invalid lifetime, preferred lifetime " + preferred
+ ", valid lifetime " + valid);
public boolean isValid() {
if (preferred < 0) {
Log.w(TAG, "Invalid preferred lifetime: " + this);
return false;
}
if (preferred > valid) {
Log.w(TAG, "IA_PD option with preferred lifetime " + preferred
+ " greater than valid lifetime " + valid);
if (valid < 0) {
Log.w(TAG, "Invalid valid lifetime: " + this);
return false;
}
if (prefixLen > 64) {
Log.w(TAG, "IA_PD option with prefix length " + prefixLen
+ " longer than 64");
if (preferred > valid) {
Log.w(TAG, "Invalid lifetime. Preferred lifetime > valid lifetime: " + this);
return false;
}
// Either preferred lifetime or t2 might be 0 which is valid, then ignore it.
if (preferred != 0 && t2 != 0 && preferred < t2) {
Log.w(TAG, "preferred lifetime " + preferred + " is smaller than T2 " + t2);
if (prefixLen > 64) {
Log.w(TAG, "Invalid prefix length: " + this);
return false;
}
return true;
......@@ -119,8 +141,14 @@ public class IaPrefixOption extends Struct {
*/
public static ByteBuffer build(final short length, final long preferred, final long valid,
final byte prefixLen, final byte[] prefix) {
final IaPrefixOption option = new IaPrefixOption((byte) DHCP6_OPTION_IAPREFIX,
final IaPrefixOption option = new IaPrefixOption(
length /* 25 + IAPrefix options length */, preferred, valid, prefixLen, prefix);
return ByteBuffer.wrap(option.writeToBytes(ByteOrder.BIG_ENDIAN));
}
@Override
public String toString() {
return "IA Prefix, length " + length + ": " + mIpPrefix + ", pref " + preferred + ", valid "
+ valid;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment