diff options
| author | Etan Cohen <etancohen@google.com> | 2021-12-12 02:14:06 +0000 |
|---|---|---|
| committer | Etan Cohen <etancohen@google.com> | 2022-02-07 19:57:07 +0000 |
| commit | 1e86b3af2633a31a13285e51d63f40cd88697a3c (patch) | |
| tree | 2f4bb21493201f3396b08dcde1eb64feddb49942 /framework/src | |
| parent | e7036bec68e599128ab9a9109f8bdac98d4ae930 (diff) | |
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
Diffstat (limited to 'framework/src')
| -rw-r--r-- | framework/src/android/net/IpConfiguration.java | 88 | ||||
| -rw-r--r-- | framework/src/android/net/StaticIpConfiguration.java | 55 |
2 files changed, 118 insertions, 25 deletions
diff --git a/framework/src/android/net/IpConfiguration.java b/framework/src/android/net/IpConfiguration.java index d5f8b2edb6..99835aa6fb 100644 --- a/framework/src/android/net/IpConfiguration.java +++ b/framework/src/android/net/IpConfiguration.java @@ -28,16 +28,16 @@ import android.os.Parcelable; import java.util.Objects; /** - * A class representing a configured network. - * @hide + * A class representing the IP configuration of a network. */ -@SystemApi public final class IpConfiguration implements Parcelable { private static final String TAG = "IpConfiguration"; // This enum has been used by apps through reflection for many releases. // Therefore they can't just be removed. Duplicating these constants to // give an alternate SystemApi is a worse option than exposing them. + /** @hide */ + @SystemApi @SuppressLint("Enum") public enum IpAssignment { /* Use statically configured IP settings. Configuration can be accessed @@ -59,6 +59,8 @@ public final class IpConfiguration implements Parcelable { // This enum has been used by apps through reflection for many releases. // Therefore they can't just be removed. Duplicating these constants to // give an alternate SystemApi is a worse option than exposing them. + /** @hide */ + @SystemApi @SuppressLint("Enum") public enum ProxySettings { /* No proxy is to be used. Any existing proxy settings @@ -94,6 +96,8 @@ public final class IpConfiguration implements Parcelable { null : new ProxyInfo(httpProxy); } + /** @hide */ + @SystemApi public IpConfiguration() { init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null); } @@ -107,6 +111,8 @@ public final class IpConfiguration implements Parcelable { init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy); } + /** @hide */ + @SystemApi public IpConfiguration(@NonNull IpConfiguration source) { this(); if (source != null) { @@ -115,34 +121,58 @@ public final class IpConfiguration implements Parcelable { } } + /** @hide */ + @SystemApi public @NonNull IpAssignment getIpAssignment() { return ipAssignment; } + /** @hide */ + @SystemApi public void setIpAssignment(@NonNull IpAssignment ipAssignment) { this.ipAssignment = ipAssignment; } + /** + * Get the current static IP configuration (possibly null). Configured via + * {@link Builder#setStaticIpConfiguration(StaticIpConfiguration)}. + * + * @return Current static IP configuration. + */ public @Nullable StaticIpConfiguration getStaticIpConfiguration() { return staticIpConfiguration; } + /** @hide */ + @SystemApi public void setStaticIpConfiguration(@Nullable StaticIpConfiguration staticIpConfiguration) { this.staticIpConfiguration = staticIpConfiguration; } + /** @hide */ + @SystemApi public @NonNull ProxySettings getProxySettings() { return proxySettings; } + /** @hide */ + @SystemApi public void setProxySettings(@NonNull ProxySettings proxySettings) { this.proxySettings = proxySettings; } + /** + * The proxy configuration of this object. + * + * @return The proxy information of this object configured via + * {@link Builder#setHttpProxy(ProxyInfo)}. + */ public @Nullable ProxyInfo getHttpProxy() { return httpProxy; } + /** @hide */ + @SystemApi public void setHttpProxy(@Nullable ProxyInfo httpProxy) { this.httpProxy = httpProxy; } @@ -220,4 +250,56 @@ public final class IpConfiguration implements Parcelable { return new IpConfiguration[size]; } }; + + /** + * Builder used to construct {@link IpConfiguration} objects. + */ + public static final class Builder { + private StaticIpConfiguration mStaticIpConfiguration; + private ProxyInfo mProxyInfo; + + /** + * Set a static IP configuration. + * + * @param config Static IP configuration. + * @return A {@link Builder} object to allow chaining. + */ + public @NonNull Builder setStaticIpConfiguration(@Nullable StaticIpConfiguration config) { + mStaticIpConfiguration = config; + return this; + } + + /** + * Set a proxy configuration. + * + * @param proxyInfo Proxy configuration. + * @return A {@link Builder} object to allow chaining. + */ + public @NonNull Builder setHttpProxy(@Nullable ProxyInfo proxyInfo) { + mProxyInfo = proxyInfo; + return this; + } + + /** + * Construct an {@link IpConfiguration}. + * + * @return A new {@link IpConfiguration} object. + */ + public @NonNull IpConfiguration build() { + IpConfiguration config = new IpConfiguration(); + config.setStaticIpConfiguration(mStaticIpConfiguration); + config.setIpAssignment( + mStaticIpConfiguration == null ? IpAssignment.DHCP : IpAssignment.STATIC); + + config.setHttpProxy(mProxyInfo); + if (mProxyInfo == null) { + config.setProxySettings(ProxySettings.NONE); + } else { + config.setProxySettings( + mProxyInfo.getPacFileUrl() == null ? ProxySettings.STATIC + : ProxySettings.PAC); + } + return config; + } + } } diff --git a/framework/src/android/net/StaticIpConfiguration.java b/framework/src/android/net/StaticIpConfiguration.java index 7904f7a4ec..194cffd164 100644 --- a/framework/src/android/net/StaticIpConfiguration.java +++ b/framework/src/android/net/StaticIpConfiguration.java @@ -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); |
