Stop using LinkProperties for static configuration.

LinkProperties can represent way more complicated configurations
than what we can actually apply to interfaces. This makes it
error-prone to use it to represent static configuration, both
when trying to apply configuration coming from LinkProperties
and when trying to save configuration from current
LinkProperties.

Instead, move static configuration (IPv4 only, since we don't
support static IPv6 configuration) into a separate
StaticIpConfiguration class.

Bug: 16114392
Bug: 16893413
Change-Id: Ib33f35c004e30b6067bb20235ffa43c247d174df
This commit is contained in:
Lorenzo Colitti
2014-07-31 00:48:01 +09:00
parent 5d6a2cd7ca
commit eff82e8099
2 changed files with 52 additions and 47 deletions

View File

@@ -21,7 +21,6 @@ import android.net.IEthernetManager;
import android.net.IpConfiguration; import android.net.IpConfiguration;
import android.net.IpConfiguration.IpAssignment; import android.net.IpConfiguration.IpAssignment;
import android.net.IpConfiguration.ProxySettings; import android.net.IpConfiguration.ProxySettings;
import android.net.LinkProperties;
import android.os.RemoteException; import android.os.RemoteException;
/** /**
@@ -52,16 +51,12 @@ public class EthernetManager {
*/ */
public IpConfiguration getConfiguration() { public IpConfiguration getConfiguration() {
if (mService == null) { if (mService == null) {
return new IpConfiguration(IpAssignment.UNASSIGNED, return new IpConfiguration();
ProxySettings.UNASSIGNED,
new LinkProperties());
} }
try { try {
return mService.getConfiguration(); return mService.getConfiguration();
} catch (RemoteException e) { } catch (RemoteException e) {
return new IpConfiguration(IpAssignment.UNASSIGNED, return new IpConfiguration();
ProxySettings.UNASSIGNED,
new LinkProperties());
} }
} }

View File

@@ -20,10 +20,10 @@ import android.net.IpConfiguration;
import android.net.IpConfiguration.IpAssignment; import android.net.IpConfiguration.IpAssignment;
import android.net.IpConfiguration.ProxySettings; import android.net.IpConfiguration.ProxySettings;
import android.net.LinkAddress; import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.NetworkUtils; import android.net.NetworkUtils;
import android.net.ProxyInfo; import android.net.ProxyInfo;
import android.net.RouteInfo; import android.net.RouteInfo;
import android.net.StaticIpConfiguration;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.text.TextUtils; import android.text.TextUtils;
@@ -41,6 +41,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Inet4Address;
import java.util.Map; import java.util.Map;
public class IpConfigStore { public class IpConfigStore {
@@ -69,40 +70,32 @@ public class IpConfigStore {
} }
private boolean writeConfig(DataOutputStream out, int configKey, private boolean writeConfig(DataOutputStream out, int configKey,
IpConfiguration config) throws IOException { IpConfiguration config) throws IOException {
boolean written = false; boolean written = false;
try { try {
LinkProperties linkProperties = config.linkProperties;
switch (config.ipAssignment) { switch (config.ipAssignment) {
case STATIC: case STATIC:
out.writeUTF(IP_ASSIGNMENT_KEY); out.writeUTF(IP_ASSIGNMENT_KEY);
out.writeUTF(config.ipAssignment.toString()); out.writeUTF(config.ipAssignment.toString());
for (LinkAddress linkAddr : linkProperties.getLinkAddresses()) { StaticIpConfiguration staticIpConfiguration = config.staticIpConfiguration;
out.writeUTF(LINK_ADDRESS_KEY); if (staticIpConfiguration != null) {
out.writeUTF(linkAddr.getAddress().getHostAddress()); if (staticIpConfiguration.ipAddress != null) {
out.writeInt(linkAddr.getPrefixLength()); LinkAddress ipAddress = staticIpConfiguration.ipAddress;
} out.writeUTF(LINK_ADDRESS_KEY);
for (RouteInfo route : linkProperties.getRoutes()) { out.writeUTF(ipAddress.getAddress().getHostAddress());
out.writeUTF(GATEWAY_KEY); out.writeInt(ipAddress.getPrefixLength());
LinkAddress dest = route.getDestinationLinkAddress();
if (dest != null) {
out.writeInt(1);
out.writeUTF(dest.getAddress().getHostAddress());
out.writeInt(dest.getPrefixLength());
} else {
out.writeInt(0);
} }
if (route.getGateway() != null) { if (staticIpConfiguration.gateway != null) {
out.writeInt(1); out.writeUTF(GATEWAY_KEY);
out.writeUTF(route.getGateway().getHostAddress()); out.writeInt(0); // Default route.
} else { out.writeInt(1); // Have a gateway.
out.writeInt(0); out.writeUTF(staticIpConfiguration.gateway.getHostAddress());
}
for (InetAddress inetAddr : staticIpConfiguration.dnsServers) {
out.writeUTF(DNS_KEY);
out.writeUTF(inetAddr.getHostAddress());
} }
}
for (InetAddress inetAddr : linkProperties.getDnsServers()) {
out.writeUTF(DNS_KEY);
out.writeUTF(inetAddr.getHostAddress());
} }
written = true; written = true;
break; break;
@@ -121,7 +114,7 @@ public class IpConfigStore {
switch (config.proxySettings) { switch (config.proxySettings) {
case STATIC: case STATIC:
ProxyInfo proxyProperties = linkProperties.getHttpProxy(); ProxyInfo proxyProperties = config.httpProxy;
String exclusionList = proxyProperties.getExclusionListAsString(); String exclusionList = proxyProperties.getExclusionListAsString();
out.writeUTF(PROXY_SETTINGS_KEY); out.writeUTF(PROXY_SETTINGS_KEY);
out.writeUTF(config.proxySettings.toString()); out.writeUTF(config.proxySettings.toString());
@@ -134,7 +127,7 @@ public class IpConfigStore {
written = true; written = true;
break; break;
case PAC: case PAC:
ProxyInfo proxyPacProperties = linkProperties.getHttpProxy(); ProxyInfo proxyPacProperties = config.httpProxy;
out.writeUTF(PROXY_SETTINGS_KEY); out.writeUTF(PROXY_SETTINGS_KEY);
out.writeUTF(config.proxySettings.toString()); out.writeUTF(config.proxySettings.toString());
out.writeUTF(PROXY_PAC_FILE); out.writeUTF(PROXY_PAC_FILE);
@@ -159,7 +152,7 @@ public class IpConfigStore {
out.writeInt(configKey); out.writeInt(configKey);
} }
} catch (NullPointerException e) { } catch (NullPointerException e) {
loge("Failure in writing " + config.linkProperties + e); loge("Failure in writing " + config + e);
} }
out.writeUTF(EOS); out.writeUTF(EOS);
@@ -196,7 +189,7 @@ public class IpConfigStore {
// Default is DHCP with no proxy // Default is DHCP with no proxy
IpAssignment ipAssignment = IpAssignment.DHCP; IpAssignment ipAssignment = IpAssignment.DHCP;
ProxySettings proxySettings = ProxySettings.NONE; ProxySettings proxySettings = ProxySettings.NONE;
LinkProperties linkProperties = new LinkProperties(); StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();
String proxyHost = null; String proxyHost = null;
String pacFileUrl = null; String pacFileUrl = null;
int proxyPort = -1; int proxyPort = -1;
@@ -213,13 +206,23 @@ public class IpConfigStore {
} else if (key.equals(LINK_ADDRESS_KEY)) { } else if (key.equals(LINK_ADDRESS_KEY)) {
LinkAddress linkAddr = new LinkAddress( LinkAddress linkAddr = new LinkAddress(
NetworkUtils.numericToInetAddress(in.readUTF()), in.readInt()); NetworkUtils.numericToInetAddress(in.readUTF()), in.readInt());
linkProperties.addLinkAddress(linkAddr); if (linkAddr.getAddress() instanceof Inet4Address &&
staticIpConfiguration.ipAddress == null) {
staticIpConfiguration.ipAddress = linkAddr;
} else {
loge("Non-IPv4 or duplicate address: " + linkAddr);
}
} else if (key.equals(GATEWAY_KEY)) { } else if (key.equals(GATEWAY_KEY)) {
LinkAddress dest = null; LinkAddress dest = null;
InetAddress gateway = null; InetAddress gateway = null;
if (version == 1) { if (version == 1) {
// only supported default gateways - leave the dest/prefix empty // only supported default gateways - leave the dest/prefix empty
gateway = NetworkUtils.numericToInetAddress(in.readUTF()); gateway = NetworkUtils.numericToInetAddress(in.readUTF());
if (staticIpConfiguration.gateway == null) {
staticIpConfiguration.gateway = gateway;
} else {
loge("Duplicate gateway: " + gateway.getHostAddress());
}
} else { } else {
if (in.readInt() == 1) { if (in.readInt() == 1) {
dest = new LinkAddress( dest = new LinkAddress(
@@ -229,10 +232,16 @@ public class IpConfigStore {
if (in.readInt() == 1) { if (in.readInt() == 1) {
gateway = NetworkUtils.numericToInetAddress(in.readUTF()); gateway = NetworkUtils.numericToInetAddress(in.readUTF());
} }
RouteInfo route = new RouteInfo(dest, gateway);
if (route.isIPv4Default() &&
staticIpConfiguration.gateway == null) {
staticIpConfiguration.gateway = gateway;
} else {
loge("Non-IPv4 default or duplicate route: " + route);
}
} }
linkProperties.addRoute(new RouteInfo(dest, gateway));
} else if (key.equals(DNS_KEY)) { } else if (key.equals(DNS_KEY)) {
linkProperties.addDnsServer( staticIpConfiguration.dnsServers.add(
NetworkUtils.numericToInetAddress(in.readUTF())); NetworkUtils.numericToInetAddress(in.readUTF()));
} else if (key.equals(PROXY_SETTINGS_KEY)) { } else if (key.equals(PROXY_SETTINGS_KEY)) {
proxySettings = ProxySettings.valueOf(in.readUTF()); proxySettings = ProxySettings.valueOf(in.readUTF());
@@ -258,9 +267,11 @@ public class IpConfigStore {
IpConfiguration config = new IpConfiguration(); IpConfiguration config = new IpConfiguration();
networks.put(id, config); networks.put(id, config);
config.linkProperties = linkProperties;
switch (ipAssignment) { switch (ipAssignment) {
case STATIC: case STATIC:
config.staticIpConfiguration = staticIpConfiguration;
config.ipAssignment = ipAssignment;
break;
case DHCP: case DHCP:
config.ipAssignment = ipAssignment; config.ipAssignment = ipAssignment;
break; break;
@@ -276,16 +287,15 @@ public class IpConfigStore {
switch (proxySettings) { switch (proxySettings) {
case STATIC: case STATIC:
config.proxySettings = proxySettings; ProxyInfo proxyInfo =
ProxyInfo ProxyInfo =
new ProxyInfo(proxyHost, proxyPort, exclusionList); new ProxyInfo(proxyHost, proxyPort, exclusionList);
linkProperties.setHttpProxy(ProxyInfo); config.proxySettings = proxySettings;
config.httpProxy = proxyInfo;
break; break;
case PAC: case PAC:
ProxyInfo proxyPacProperties = new ProxyInfo(pacFileUrl);
config.proxySettings = proxySettings; config.proxySettings = proxySettings;
ProxyInfo proxyPacProperties = config.httpProxy = proxyPacProperties;
new ProxyInfo(pacFileUrl);
linkProperties.setHttpProxy(proxyPacProperties);
break; break;
case NONE: case NONE:
config.proxySettings = proxySettings; config.proxySettings = proxySettings;