Fix ApfCapabilities, LinkAddress, RouteInfo, IpPrefix API issues.

Fix: 129362082
Fix: 129361362
Fix: 129360330
Fix: 129362379
Test: atest FrameworksNetTests NetworkStackTests
Change-Id: I05fbc6f98207d5cf002e3cbc5829040af7d6be52
This commit is contained in:
paulhu
2019-03-27 22:26:37 +08:00
parent 539821c978
commit 895a741bf1
4 changed files with 85 additions and 35 deletions

View File

@@ -16,6 +16,7 @@
package android.net; package android.net;
import android.annotation.IntRange;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.SystemApi; import android.annotation.SystemApi;
import android.annotation.TestApi; import android.annotation.TestApi;
@@ -71,7 +72,7 @@ public final class IpPrefix implements Parcelable {
* *
* @hide * @hide
*/ */
public IpPrefix(@NonNull byte[] address, int prefixLength) { public IpPrefix(@NonNull byte[] address, @IntRange(from = 0, to = 128) int prefixLength) {
this.address = address.clone(); this.address = address.clone();
this.prefixLength = prefixLength; this.prefixLength = prefixLength;
checkAndMaskAddressAndPrefixLength(); checkAndMaskAddressAndPrefixLength();
@@ -88,7 +89,7 @@ public final class IpPrefix implements Parcelable {
*/ */
@SystemApi @SystemApi
@TestApi @TestApi
public IpPrefix(@NonNull InetAddress address, int prefixLength) { public IpPrefix(@NonNull InetAddress address, @IntRange(from = 0, to = 128) int prefixLength) {
// We don't reuse the (byte[], int) constructor because it calls clone() on the byte array, // We don't reuse the (byte[], int) constructor because it calls clone() on the byte array,
// which is unnecessary because getAddress() already returns a clone. // which is unnecessary because getAddress() already returns a clone.
this.address = address.getAddress(); this.address = address.getAddress();
@@ -150,13 +151,13 @@ public final class IpPrefix implements Parcelable {
* *
* @return the address in the form of a byte array. * @return the address in the form of a byte array.
*/ */
public InetAddress getAddress() { public @NonNull InetAddress getAddress() {
try { try {
return InetAddress.getByAddress(address); return InetAddress.getByAddress(address);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
// Cannot happen. InetAddress.getByAddress can only throw an exception if the byte // Cannot happen. InetAddress.getByAddress can only throw an exception if the byte
// array is the wrong length, but we check that in the constructor. // array is the wrong length, but we check that in the constructor.
return null; throw new IllegalArgumentException("Address is invalid");
} }
} }
@@ -166,7 +167,7 @@ public final class IpPrefix implements Parcelable {
* *
* @return the address in the form of a byte array. * @return the address in the form of a byte array.
*/ */
public byte[] getRawAddress() { public @NonNull byte[] getRawAddress() {
return address.clone(); return address.clone();
} }
@@ -175,6 +176,7 @@ public final class IpPrefix implements Parcelable {
* *
* @return the prefix length. * @return the prefix length.
*/ */
@IntRange(from = 0, to = 128)
public int getPrefixLength() { public int getPrefixLength() {
return prefixLength; return prefixLength;
} }
@@ -183,10 +185,10 @@ public final class IpPrefix implements Parcelable {
* Determines whether the prefix contains the specified address. * Determines whether the prefix contains the specified address.
* *
* @param address An {@link InetAddress} to test. * @param address An {@link InetAddress} to test.
* @return {@code true} if the prefix covers the given address. * @return {@code true} if the prefix covers the given address. {@code false} otherwise.
*/ */
public boolean contains(InetAddress address) { public boolean contains(@NonNull InetAddress address) {
byte[] addrBytes = (address == null) ? null : address.getAddress(); byte[] addrBytes = address.getAddress();
if (addrBytes == null || addrBytes.length != this.address.length) { if (addrBytes == null || addrBytes.length != this.address.length) {
return false; return false;
} }
@@ -201,7 +203,7 @@ public final class IpPrefix implements Parcelable {
* @param otherPrefix the prefix to test * @param otherPrefix the prefix to test
* @hide * @hide
*/ */
public boolean containsPrefix(IpPrefix otherPrefix) { public boolean containsPrefix(@NonNull IpPrefix otherPrefix) {
if (otherPrefix.getPrefixLength() < prefixLength) return false; if (otherPrefix.getPrefixLength() < prefixLength) return false;
final byte[] otherAddress = otherPrefix.getRawAddress(); final byte[] otherAddress = otherPrefix.getRawAddress();
NetworkUtils.maskRawAddress(otherAddress, prefixLength); NetworkUtils.maskRawAddress(otherAddress, prefixLength);

View File

@@ -25,6 +25,7 @@ import static android.system.OsConstants.RT_SCOPE_LINK;
import static android.system.OsConstants.RT_SCOPE_SITE; import static android.system.OsConstants.RT_SCOPE_SITE;
import static android.system.OsConstants.RT_SCOPE_UNIVERSE; import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
import android.annotation.IntRange;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.SystemApi; import android.annotation.SystemApi;
@@ -170,7 +171,7 @@ public class LinkAddress implements Parcelable {
* Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
* the specified flags and scope. Flags and scope are not checked for validity. * the specified flags and scope. Flags and scope are not checked for validity.
* @param address The IP address. * @param address The IP address.
* @param prefixLength The prefix length. * @param prefixLength The prefix length. Must be &gt;= 0 and &lt;= (32 or 128) (IPv4 or IPv6).
* @param flags A bitmask of {@code IFA_F_*} values representing properties of the address. * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
* @param scope An integer defining the scope in which the address is unique (e.g., * @param scope An integer defining the scope in which the address is unique (e.g.,
* {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}). * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
@@ -178,7 +179,8 @@ public class LinkAddress implements Parcelable {
*/ */
@SystemApi @SystemApi
@TestApi @TestApi
public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) { public LinkAddress(@NonNull InetAddress address, @IntRange(from = 0, to = 128) int prefixLength,
int flags, int scope) {
init(address, prefixLength, flags, scope); init(address, prefixLength, flags, scope);
} }
@@ -186,12 +188,13 @@ public class LinkAddress implements Parcelable {
* Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length. * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
* The flags are set to zero and the scope is determined from the address. * The flags are set to zero and the scope is determined from the address.
* @param address The IP address. * @param address The IP address.
* @param prefixLength The prefix length. * @param prefixLength The prefix length. Must be &gt;= 0 and &lt;= (32 or 128) (IPv4 or IPv6).
* @hide * @hide
*/ */
@SystemApi @SystemApi
@TestApi @TestApi
public LinkAddress(@NonNull InetAddress address, int prefixLength) { public LinkAddress(@NonNull InetAddress address,
@IntRange(from = 0, to = 128) int prefixLength) {
this(address, prefixLength, 0, 0); this(address, prefixLength, 0, 0);
this.scope = scopeForUnicastAddress(address); this.scope = scopeForUnicastAddress(address);
} }
@@ -202,7 +205,7 @@ public class LinkAddress implements Parcelable {
* @param interfaceAddress The interface address. * @param interfaceAddress The interface address.
* @hide * @hide
*/ */
public LinkAddress(InterfaceAddress interfaceAddress) { public LinkAddress(@NonNull InterfaceAddress interfaceAddress) {
this(interfaceAddress.getAddress(), this(interfaceAddress.getAddress(),
interfaceAddress.getNetworkPrefixLength()); interfaceAddress.getNetworkPrefixLength());
} }
@@ -306,6 +309,7 @@ public class LinkAddress implements Parcelable {
/** /**
* Returns the prefix length of this {@code LinkAddress}. * Returns the prefix length of this {@code LinkAddress}.
*/ */
@IntRange(from = 0, to = 128)
public int getPrefixLength() { public int getPrefixLength() {
return prefixLength; return prefixLength;
} }
@@ -316,6 +320,7 @@ public class LinkAddress implements Parcelable {
* @hide * @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
@IntRange(from = 0, to = 128)
public int getNetworkPrefixLength() { public int getNetworkPrefixLength() {
return getPrefixLength(); return getPrefixLength();
} }

View File

@@ -16,6 +16,8 @@
package android.net; package android.net;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.SystemApi; import android.annotation.SystemApi;
import android.annotation.TestApi; import android.annotation.TestApi;
@@ -24,6 +26,8 @@ import android.os.Build;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.Inet4Address; import java.net.Inet4Address;
import java.net.Inet6Address; import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
@@ -51,20 +55,32 @@ import java.util.Objects;
* (IPv4 or IPv6). * (IPv4 or IPv6).
*/ */
public final class RouteInfo implements Parcelable { public final class RouteInfo implements Parcelable {
/** @hide */
@IntDef(value = {
RTN_UNICAST,
RTN_UNREACHABLE,
RTN_THROW,
})
@Retention(RetentionPolicy.SOURCE)
public @interface RouteType {}
/** /**
* The IP destination address for this route. * The IP destination address for this route.
*/ */
@NonNull
private final IpPrefix mDestination; private final IpPrefix mDestination;
/** /**
* The gateway address for this route. * The gateway address for this route.
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
@Nullable
private final InetAddress mGateway; private final InetAddress mGateway;
/** /**
* The interface for this route. * The interface for this route.
*/ */
@Nullable
private final String mInterface; private final String mInterface;
@@ -108,13 +124,14 @@ public final class RouteInfo implements Parcelable {
* @param destination the destination prefix * @param destination the destination prefix
* @param gateway the IP address to route packets through * @param gateway the IP address to route packets through
* @param iface the interface name to send packets on * @param iface the interface name to send packets on
* @param type the type of this route
* *
* @hide * @hide
*/ */
@SystemApi @SystemApi
@TestApi @TestApi
public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway, public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway,
@Nullable String iface, int type) { @Nullable String iface, @RouteType int type) {
switch (type) { switch (type) {
case RTN_UNICAST: case RTN_UNICAST:
case RTN_UNREACHABLE: case RTN_UNREACHABLE:
@@ -173,10 +190,24 @@ public final class RouteInfo implements Parcelable {
} }
/** /**
* Constructs a {@code RouteInfo} object.
*
* If destination is null, then gateway must be specified and the
* constructed route is either the IPv4 default route <code>0.0.0.0</code>
* if the gateway is an instance of {@link Inet4Address}, or the IPv6 default
* route <code>::/0</code> if gateway is an instance of {@link Inet6Address}.
* <p>
* Destination and gateway may not both be null.
*
* @param destination the destination address and prefix in an {@link IpPrefix}
* @param gateway the {@link InetAddress} to route packets through
* @param iface the interface name to send packets on
*
* @hide * @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public RouteInfo(IpPrefix destination, InetAddress gateway, String iface) { public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway,
@Nullable String iface) {
this(destination, gateway, iface, RTN_UNICAST); this(destination, gateway, iface, RTN_UNICAST);
} }
@@ -184,7 +215,8 @@ public final class RouteInfo implements Parcelable {
* @hide * @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public RouteInfo(LinkAddress destination, InetAddress gateway, String iface) { public RouteInfo(@Nullable LinkAddress destination, @Nullable InetAddress gateway,
@Nullable String iface) {
this(destination == null ? null : this(destination == null ? null :
new IpPrefix(destination.getAddress(), destination.getPrefixLength()), new IpPrefix(destination.getAddress(), destination.getPrefixLength()),
gateway, iface); gateway, iface);
@@ -205,7 +237,7 @@ public final class RouteInfo implements Parcelable {
* *
* @hide * @hide
*/ */
public RouteInfo(IpPrefix destination, InetAddress gateway) { public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway) {
this(destination, gateway, null); this(destination, gateway, null);
} }
@@ -215,7 +247,7 @@ public final class RouteInfo implements Parcelable {
* TODO: Remove this. * TODO: Remove this.
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public RouteInfo(LinkAddress destination, InetAddress gateway) { public RouteInfo(@Nullable LinkAddress destination, @Nullable InetAddress gateway) {
this(destination, gateway, null); this(destination, gateway, null);
} }
@@ -227,7 +259,7 @@ public final class RouteInfo implements Parcelable {
* @hide * @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public RouteInfo(InetAddress gateway) { public RouteInfo(@NonNull InetAddress gateway) {
this((IpPrefix) null, gateway, null); this((IpPrefix) null, gateway, null);
} }
@@ -239,35 +271,36 @@ public final class RouteInfo implements Parcelable {
* *
* @hide * @hide
*/ */
public RouteInfo(IpPrefix destination) { public RouteInfo(@NonNull IpPrefix destination) {
this(destination, null, null); this(destination, null, null);
} }
/** /**
* @hide * @hide
*/ */
public RouteInfo(LinkAddress destination) { public RouteInfo(@NonNull LinkAddress destination) {
this(destination, null, null); this(destination, null, null);
} }
/** /**
* @hide * @hide
*/ */
public RouteInfo(IpPrefix destination, int type) { public RouteInfo(@NonNull IpPrefix destination, @RouteType int type) {
this(destination, null, null, type); this(destination, null, null, type);
} }
/** /**
* @hide * @hide
*/ */
public static RouteInfo makeHostRoute(InetAddress host, String iface) { public static RouteInfo makeHostRoute(@NonNull InetAddress host, @Nullable String iface) {
return makeHostRoute(host, null, iface); return makeHostRoute(host, null, iface);
} }
/** /**
* @hide * @hide
*/ */
public static RouteInfo makeHostRoute(InetAddress host, InetAddress gateway, String iface) { public static RouteInfo makeHostRoute(@Nullable InetAddress host, @Nullable InetAddress gateway,
@Nullable String iface) {
if (host == null) return null; if (host == null) return null;
if (host instanceof Inet4Address) { if (host instanceof Inet4Address) {
@@ -290,6 +323,7 @@ public final class RouteInfo implements Parcelable {
* *
* @return {@link IpPrefix} specifying the destination. This is never {@code null}. * @return {@link IpPrefix} specifying the destination. This is never {@code null}.
*/ */
@NonNull
public IpPrefix getDestination() { public IpPrefix getDestination() {
return mDestination; return mDestination;
} }
@@ -298,6 +332,7 @@ public final class RouteInfo implements Parcelable {
* TODO: Convert callers to use IpPrefix and then remove. * TODO: Convert callers to use IpPrefix and then remove.
* @hide * @hide
*/ */
@NonNull
public LinkAddress getDestinationLinkAddress() { public LinkAddress getDestinationLinkAddress() {
return new LinkAddress(mDestination.getAddress(), mDestination.getPrefixLength()); return new LinkAddress(mDestination.getAddress(), mDestination.getPrefixLength());
} }
@@ -308,6 +343,7 @@ public final class RouteInfo implements Parcelable {
* @return {@link InetAddress} specifying the gateway or next hop. This may be * @return {@link InetAddress} specifying the gateway or next hop. This may be
* {@code null} for a directly-connected route." * {@code null} for a directly-connected route."
*/ */
@Nullable
public InetAddress getGateway() { public InetAddress getGateway() {
return mGateway; return mGateway;
} }
@@ -317,6 +353,7 @@ public final class RouteInfo implements Parcelable {
* *
* @return The name of the interface used for this route. * @return The name of the interface used for this route.
*/ */
@Nullable
public String getInterface() { public String getInterface() {
return mInterface; return mInterface;
} }
@@ -330,6 +367,7 @@ public final class RouteInfo implements Parcelable {
*/ */
@TestApi @TestApi
@SystemApi @SystemApi
@RouteType
public int getType() { public int getType() {
return mType; return mType;
} }
@@ -401,6 +439,7 @@ public final class RouteInfo implements Parcelable {
* @hide * @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
@Nullable
public static RouteInfo selectBestRoute(Collection<RouteInfo> routes, InetAddress dest) { public static RouteInfo selectBestRoute(Collection<RouteInfo> routes, InetAddress dest) {
if ((routes == null) || (dest == null)) return null; if ((routes == null) || (dest == null)) return null;

View File

@@ -19,14 +19,17 @@ package android.net.apf;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.SystemApi; import android.annotation.SystemApi;
import android.annotation.TestApi; import android.annotation.TestApi;
import android.content.Context; import android.content.res.Resources;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import com.android.internal.R; import com.android.internal.R;
/** /**
* APF program support capabilities. * APF program support capabilities. APF stands for Android Packet Filtering and it is a flexible
* way to drop unwanted network packets to save power.
*
* See documentation at hardware/google/apf/apf.h
* *
* This class is immutable. * This class is immutable.
* @hide * @hide
@@ -104,10 +107,11 @@ public final class ApfCapabilities implements Parcelable {
} }
/** /**
* Returns true if the APF interpreter advertises support for the data buffer access opcodes * Determines whether the APF interpreter advertises support for the data buffer access opcodes
* LDDW and STDW. * LDDW (LoaD Data Word) and STDW (STore Data Word). Full LDDW (LoaD Data Word) and
* STDW (STore Data Word) support is present from APFv4 on.
* *
* Full LDDW and STDW support is present from APFv4 on. * @return {@code true} if the IWifiStaIface#readApfPacketFilterData is supported.
*/ */
public boolean hasDataAccess() { public boolean hasDataAccess() {
return apfVersionSupported >= 4; return apfVersionSupported >= 4;
@@ -116,14 +120,14 @@ public final class ApfCapabilities implements Parcelable {
/** /**
* @return Whether the APF Filter in the device should filter out IEEE 802.3 Frames. * @return Whether the APF Filter in the device should filter out IEEE 802.3 Frames.
*/ */
public static boolean getApfDrop8023Frames(@NonNull Context context) { public static boolean getApfDrop8023Frames() {
return context.getResources().getBoolean(R.bool.config_apfDrop802_3Frames); return Resources.getSystem().getBoolean(R.bool.config_apfDrop802_3Frames);
} }
/** /**
* @return An array of blacklisted EtherType, packets with EtherTypes within it will be dropped. * @return An array of blacklisted EtherType, packets with EtherTypes within it will be dropped.
*/ */
public static @NonNull int[] getApfEthTypeBlackList(@NonNull Context context) { public static @NonNull int[] getApfEtherTypeBlackList() {
return context.getResources().getIntArray(R.array.config_apfEthTypeBlackList); return Resources.getSystem().getIntArray(R.array.config_apfEthTypeBlackList);
} }
} }