Expose public APIs for IP & static IP configuration

Create public API for IP and static IP configuration.

Bug: 209840828
Test: atest android.net.cts.IpConfigurationTest
Test: atest android.net.cts.StaticIpConfigurationTest
Test: atest android.net.dhcp.DhcpResultsParcelableUtilTest
Change-Id: I720f168d1023806970919ca5dd44239a276826b6
This commit is contained in:
Etan Cohen
2021-12-12 02:14:06 +00:00
parent e7036bec68
commit 1e86b3af26
6 changed files with 216 additions and 53 deletions

View File

@@ -26,6 +26,7 @@ import android.os.Parcelable;
import com.android.net.module.util.InetAddressUtils;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
@@ -33,24 +34,7 @@ import java.util.Objects;
/**
* Class that describes static IP configuration.
*
* <p>This class is different from {@link LinkProperties} because it represents
* configuration intent. The general contract is that if we can represent
* a configuration here, then we should be able to configure it on a network.
* The intent is that it closely match the UI we have for configuring networks.
*
* <p>In contrast, {@link LinkProperties} represents current state. It is much more
* expressive. For example, it supports multiple IP addresses, multiple routes,
* stacked interfaces, and so on. Because LinkProperties is so expressive,
* using it to represent configuration intent as well as current state causes
* problems. For example, we could unknowingly save a configuration that we are
* not in fact capable of applying, or we could save a configuration that the
* UI cannot display, which has the potential for malicious code to hide
* hostile or unexpected configuration from the user.
*
* @hide
*/
@SystemApi
public final class StaticIpConfiguration implements Parcelable {
/** @hide */
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@@ -69,10 +53,14 @@ public final class StaticIpConfiguration implements Parcelable {
@Nullable
public String domains;
/** @hide */
@SystemApi
public StaticIpConfiguration() {
dnsServers = new ArrayList<>();
}
/** @hide */
@SystemApi
public StaticIpConfiguration(@Nullable StaticIpConfiguration source) {
this();
if (source != null) {
@@ -84,6 +72,8 @@ public final class StaticIpConfiguration implements Parcelable {
}
}
/** @hide */
@SystemApi
public void clear() {
ipAddress = null;
gateway = null;
@@ -94,7 +84,7 @@ public final class StaticIpConfiguration implements Parcelable {
/**
* Get the static IP address included in the configuration.
*/
public @Nullable LinkAddress getIpAddress() {
public @NonNull LinkAddress getIpAddress() {
return ipAddress;
}
@@ -130,10 +120,15 @@ public final class StaticIpConfiguration implements Parcelable {
private String mDomains;
/**
* Set the IP address to be included in the configuration; null by default.
* Set the IP address to be included in the configuration.
*
* @return The {@link Builder} for chaining.
*/
public @NonNull Builder setIpAddress(@Nullable LinkAddress ipAddress) {
public @NonNull Builder setIpAddress(@NonNull LinkAddress ipAddress) {
if (ipAddress != null && !(ipAddress.getAddress() instanceof Inet4Address)) {
throw new IllegalArgumentException(
"Only IPv4 addresses can be used for the IP configuration");
}
mIpAddress = ipAddress;
return this;
}
@@ -143,6 +138,10 @@ public final class StaticIpConfiguration implements Parcelable {
* @return The {@link Builder} for chaining.
*/
public @NonNull Builder setGateway(@Nullable InetAddress gateway) {
if (gateway != null && !(gateway instanceof Inet4Address)) {
throw new IllegalArgumentException(
"Only IPv4 addresses can be used for the gateway configuration");
}
mGateway = gateway;
return this;
}
@@ -153,6 +152,12 @@ public final class StaticIpConfiguration implements Parcelable {
*/
public @NonNull Builder setDnsServers(@NonNull Iterable<InetAddress> dnsServers) {
Objects.requireNonNull(dnsServers);
for (InetAddress inetAddress: dnsServers) {
if (!(inetAddress instanceof Inet4Address)) {
throw new IllegalArgumentException(
"Only IPv4 addresses can be used for the DNS server configuration");
}
}
mDnsServers = dnsServers;
return this;
}
@@ -171,6 +176,8 @@ public final class StaticIpConfiguration implements Parcelable {
/**
* Create a {@link StaticIpConfiguration} from the parameters in this {@link Builder}.
* @return The newly created StaticIpConfiguration.
* @throws IllegalArgumentException if an invalid configuration is attempted, e.g.
* if an IP Address was not configured via {@link #setIpAddress(LinkAddress)}.
*/
public @NonNull StaticIpConfiguration build() {
final StaticIpConfiguration config = new StaticIpConfiguration();
@@ -188,7 +195,9 @@ public final class StaticIpConfiguration implements Parcelable {
/**
* Add a DNS server to this configuration.
* @hide
*/
@SystemApi
public void addDnsServer(@NonNull InetAddress server) {
dnsServers.add(server);
}
@@ -197,7 +206,9 @@ public final class StaticIpConfiguration implements Parcelable {
* Returns the network routes specified by this object. Will typically include a
* directly-connected route for the IP address's local subnet and a default route.
* @param iface Interface to include in the routes.
* @hide
*/
@SystemApi
public @NonNull List<RouteInfo> getRoutes(@Nullable String iface) {
List<RouteInfo> routes = new ArrayList<RouteInfo>(3);
if (ipAddress != null) {
@@ -305,7 +316,7 @@ public final class StaticIpConfiguration implements Parcelable {
/** Implement the Parcelable interface */
@Override
public void writeToParcel(Parcel dest, int flags) {
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeParcelable(ipAddress, flags);
InetAddressUtils.parcelInetAddress(dest, gateway, flags);
dest.writeInt(dnsServers.size());
@@ -316,7 +327,7 @@ public final class StaticIpConfiguration implements Parcelable {
}
/** @hide */
public static StaticIpConfiguration readFromParcel(Parcel in) {
public static @NonNull StaticIpConfiguration readFromParcel(Parcel in) {
final StaticIpConfiguration s = new StaticIpConfiguration();
s.ipAddress = in.readParcelable(null);
s.gateway = InetAddressUtils.unparcelInetAddress(in);