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
commit 895a741bf1

View file

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