Merge "API changes for IPv6 MTU support"

This commit is contained in:
Sarah Chin
2020-01-29 15:56:09 +00:00
committed by Gerrit Code Review
2 changed files with 64 additions and 9 deletions

View File

@@ -104,6 +104,11 @@ public final class RouteInfo implements Parcelable {
*/
private final int mType;
/**
* The maximum transmission unit size for this route.
*/
private final int mMtu;
// Derived data members.
// TODO: remove these.
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
@@ -132,6 +137,31 @@ public final class RouteInfo implements Parcelable {
@TestApi
public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway,
@Nullable String iface, @RouteType int type) {
this(destination, gateway, iface, type, 0);
}
/**
* Constructs a 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 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
* @param mtu the maximum transmission unit size for this route
*
* @hide
*/
@SystemApi
public RouteInfo(@Nullable IpPrefix destination, @Nullable InetAddress gateway,
@Nullable String iface, @RouteType int type, int mtu) {
switch (type) {
case RTN_UNICAST:
case RTN_UNREACHABLE:
@@ -161,7 +191,7 @@ public final class RouteInfo implements Parcelable {
} else {
// no destination, no gateway. invalid.
throw new IllegalArgumentException("Invalid arguments passed in: " + gateway + "," +
destination);
destination);
}
}
// TODO: set mGateway to null if there is no gateway. This is more correct, saves space, and
@@ -176,10 +206,10 @@ public final class RouteInfo implements Parcelable {
}
mHasGateway = (!gateway.isAnyLocalAddress());
if ((destination.getAddress() instanceof Inet4Address &&
(gateway instanceof Inet4Address == false)) ||
(destination.getAddress() instanceof Inet6Address &&
(gateway instanceof Inet6Address == false))) {
if ((destination.getAddress() instanceof Inet4Address
&& !(gateway instanceof Inet4Address))
|| (destination.getAddress() instanceof Inet6Address
&& !(gateway instanceof Inet6Address))) {
throw new IllegalArgumentException("address family mismatch in RouteInfo constructor");
}
mDestination = destination; // IpPrefix objects are immutable.
@@ -187,6 +217,7 @@ public final class RouteInfo implements Parcelable {
mInterface = iface; // Strings are immutable.
mType = type;
mIsHost = isHost();
mMtu = mtu;
}
/**
@@ -372,6 +403,17 @@ public final class RouteInfo implements Parcelable {
return mType;
}
/**
* Retrieves the MTU size for this route.
*
* @return The MTU size, or 0 if it has not been set.
* @hide
*/
@SystemApi
public int getMtu() {
return mMtu;
}
/**
* Indicates if this route is a default route (ie, has no destination specified).
*
@@ -476,6 +518,7 @@ public final class RouteInfo implements Parcelable {
val += " unknown type " + mType;
}
}
val += " mtu " + mMtu;
return val;
}
@@ -493,7 +536,7 @@ public final class RouteInfo implements Parcelable {
return Objects.equals(mDestination, target.getDestination()) &&
Objects.equals(mGateway, target.getGateway()) &&
Objects.equals(mInterface, target.getInterface()) &&
mType == target.getType();
mType == target.getType() && mMtu == target.getMtu();
}
/**
@@ -503,7 +546,7 @@ public final class RouteInfo implements Parcelable {
return (mDestination.hashCode() * 41)
+ (mGateway == null ? 0 :mGateway.hashCode() * 47)
+ (mInterface == null ? 0 :mInterface.hashCode() * 67)
+ (mType * 71);
+ (mType * 71) + (mMtu * 89);
}
/**
@@ -522,6 +565,7 @@ public final class RouteInfo implements Parcelable {
dest.writeByteArray(gatewayBytes);
dest.writeString(mInterface);
dest.writeInt(mType);
dest.writeInt(mMtu);
}
/**
@@ -540,8 +584,9 @@ public final class RouteInfo implements Parcelable {
String iface = in.readString();
int type = in.readInt();
int mtu = in.readInt();
return new RouteInfo(dest, gateway, iface, type);
return new RouteInfo(dest, gateway, iface, type, mtu);
}
public RouteInfo[] newArray(int size) {

View File

@@ -258,6 +258,16 @@ public class RouteInfoTest extends TestCase {
assertParcelingIsLossless(r);
r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0");
assertParcelSane(r, 6);
assertParcelSane(r, 7);
}
public void testMtu() {
RouteInfo r;
r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0",
RouteInfo.RTN_UNICAST, 1500);
assertEquals(1500, r.getMtu());
r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0");
assertEquals(0, r.getMtu());
}
}