Start using NetworkUtils.numericToInetAddress.

Generates InetAddresses without risking an accidental dns lookup.  For use with supposedly
numeric-only ip address strings.

Change-Id: I694f3976ce1c6382854706f6557ea88a289add3a
This commit is contained in:
Robert Greenwalt
2011-02-22 16:00:42 -08:00
parent 0390191392
commit e590373ea7
9 changed files with 51 additions and 59 deletions

View File

@@ -233,17 +233,11 @@ public final class Proxy {
if (host.equalsIgnoreCase("localhost")) {
return true;
}
// Check we have a numeric address so we don't cause a DNS lookup in getByName.
if (InetAddress.isNumeric(host)) {
if (InetAddress.getByName(host).isLoopbackAddress()) {
return true;
}
if (NetworkUtils.numericToInetAddress(host).isLoopbackAddress()) {
return true;
}
}
} catch (UnknownHostException ignored) {
// Can't happen for a numeric address (InetAddress.getByName).
} catch (IllegalArgumentException iex) {
// Ignore (URI.create)
}
return false;
}

View File

@@ -28,6 +28,7 @@ import android.content.res.Resources.NotFoundException;
import android.net.ConnectivityManager;
import android.net.InterfaceConfiguration;
import android.net.LinkAddress;
import android.net.NetworkUtils;
import android.os.IBinder;
import android.os.INetworkManagementService;
import android.os.ServiceManager;
@@ -365,9 +366,9 @@ final class BluetoothPanProfileHandler {
if (ifcg != null) {
InetAddress addr = null;
if (ifcg.addr == null || (addr = ifcg.addr.getAddress()) == null ||
addr.equals(InetAddress.getByName("0.0.0.0")) ||
addr.equals(InetAddress.getByName("::0"))) {
addr = InetAddress.getByName(address);
addr.equals(NetworkUtils.numericToInetAddress("0.0.0.0")) ||
addr.equals(NetworkUtils.numericToInetAddress("::0"))) {
addr = NetworkUtils.numericToInetAddress(address);
}
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
ifcg.addr = new LinkAddress(addr, BLUETOOTH_PREFIX_LENGTH);

View File

@@ -15,12 +15,11 @@
*/
package com.android.internal.net;
import android.net.NetworkUtils;
import android.util.Config;
import android.util.Log;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.Collection;
@@ -38,13 +37,6 @@ public class DomainNameValidator {
private static final boolean DEBUG = false;
private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
private static Pattern QUICK_IP_PATTERN;
static {
try {
QUICK_IP_PATTERN = Pattern.compile("^[a-f0-9\\.:]+$");
} catch (PatternSyntaxException e) {}
}
private static final int ALT_DNS_NAME = 2;
private static final int ALT_IPA_NAME = 7;
@@ -75,19 +67,11 @@ public class DomainNameValidator {
if (rval) {
try {
// do a quick-dirty IP match first to avoid DNS lookup
rval = QUICK_IP_PATTERN.matcher(domain).matches();
if (rval) {
rval = domain.equals(
InetAddress.getByName(domain).getHostAddress());
}
} catch (UnknownHostException e) {
String errorMessage = e.getMessage();
if (errorMessage == null) {
errorMessage = "unknown host exception";
}
rval = domain.equals(
NetworkUtils.numericToInetAddress(domain).getHostAddress());
} catch (IllegalArgumentException e) {
if (LOG_ENABLED) {
Log.v(TAG, "DomainNameValidator.isIpAddress(): " + errorMessage);
Log.v(TAG, "DomainNameValidator.isIpAddress(): " + e);
}
rval = false;

View File

@@ -286,8 +286,8 @@ public class ConnectivityService extends IConnectivityManager.Stub {
com.android.internal.R.string.config_default_dns_server);
}
try {
mDefaultDns = InetAddress.getByName(dns);
} catch (UnknownHostException e) {
mDefaultDns = NetworkUtils.numericToInetAddress(dns);
} catch (IllegalArgumentException e) {
loge("Error setting defaultDns using " + dns);
}

View File

@@ -270,9 +270,9 @@ class NetworkManagementService extends INetworkManagementService.Stub {
InetAddress addr = null;
int prefixLength = 0;
try {
addr = InetAddress.getByName(st.nextToken(" "));
} catch (UnknownHostException uhe) {
Slog.e(TAG, "Failed to parse ipaddr", uhe);
addr = NetworkUtils.numericToInetAddress(st.nextToken(" "));
} catch (IllegalArgumentException iae) {
Slog.e(TAG, "Failed to parse ipaddr", iae);
}
try {
@@ -451,7 +451,7 @@ class NetworkManagementService extends INetworkManagementService.Stub {
try {
String cmd = "tether dns set";
for (String s : dns) {
cmd += " " + InetAddress.getByName(s).getHostAddress();
cmd += " " + NetworkUtils.numericToInetAddress(s).getHostAddress();
}
try {
mConnector.doCommand(cmd);
@@ -459,7 +459,7 @@ class NetworkManagementService extends INetworkManagementService.Stub {
throw new IllegalStateException(
"Unable to communicate to native daemon for setting tether dns");
}
} catch (UnknownHostException e) {
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Error resolving dns name", e);
}
}
@@ -519,11 +519,11 @@ class NetworkManagementService extends INetworkManagementService.Stub {
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.CHANGE_NETWORK_STATE, "NetworkManagementService");
mConnector.doCommand(String.format("pppd attach %s %s %s %s %s", tty,
InetAddress.getByName(localAddr).getHostAddress(),
InetAddress.getByName(remoteAddr).getHostAddress(),
InetAddress.getByName(dns1Addr).getHostAddress(),
InetAddress.getByName(dns2Addr).getHostAddress()));
} catch (UnknownHostException e) {
NetworkUtils.numericToInetAddress(localAddr).getHostAddress(),
NetworkUtils.numericToInetAddress(remoteAddr).getHostAddress(),
NetworkUtils.numericToInetAddress(dns1Addr).getHostAddress(),
NetworkUtils.numericToInetAddress(dns2Addr).getHostAddress()));
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Error resolving addr", e);
} catch (NativeDaemonConnectorException e) {
throw new IllegalStateException("Error communicating to native daemon to attach pppd", e);

View File

@@ -34,6 +34,7 @@ import android.net.INetworkManagementEventObserver;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.NetworkInfo;
import android.net.NetworkUtils;
import android.os.Binder;
import android.os.Environment;
import android.os.Handler;
@@ -567,7 +568,7 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
try {
ifcg = service.getInterfaceConfig(iface);
if (ifcg != null) {
InetAddress addr = InetAddress.getByName(USB_NEAR_IFACE_ADDR);
InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR);
ifcg.addr = new LinkAddress(addr, USB_PREFIX_LENGTH);
if (enabled) {
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");

View File

@@ -23,6 +23,7 @@ import com.android.internal.util.HierarchicalStateMachine;
import android.net.LinkAddress;
import android.net.LinkCapabilities;
import android.net.LinkProperties;
import android.net.NetworkUtils;
import android.os.AsyncResult;
import android.os.Message;
import android.os.SystemProperties;
@@ -415,11 +416,13 @@ public abstract class DataConnection extends HierarchicalStateMachine {
} else {
addrPrefixLen = 0;
}
if (!InetAddress.isNumeric(addr)) {
InetAddress ia;
try {
ia = NetworkUtils.numericToInetAddress(addr);
} catch (IllegalArgumentException e) {
EventLogTags.writeBadIpAddress(addr);
throw new UnknownHostException("Non-numeric ip addr=" + addr);
}
InetAddress ia = InetAddress.getByName(addr);
if (addrPrefixLen == 0) {
// Assume point to point
addrPrefixLen = (ia instanceof Inet4Address) ? 32 : 128;
@@ -434,11 +437,13 @@ public abstract class DataConnection extends HierarchicalStateMachine {
}
if (response.dnses != null && response.dnses.length > 0) {
for (String addr : response.dnses) {
if (!InetAddress.isNumeric(addr)) {
InetAddress ia;
try {
ia = NetworkUtils.numericToInetAddress(addr);
} catch (IllegalArgumentException e) {
EventLogTags.writePdpBadDnsAddress("dns=" + addr);
throw new UnknownHostException("Non-numeric dns addr=" + addr);
}
InetAddress ia = InetAddress.getByName(addr);
linkProperties.addDns(ia);
}
result = SetupResult.SUCCESS;
@@ -448,12 +453,14 @@ public abstract class DataConnection extends HierarchicalStateMachine {
dnsServers[1] = SystemProperties.get(propertyPrefix + "dns2");
if (isDnsOk(dnsServers)) {
for (String dnsAddr : dnsServers) {
if (!InetAddress.isNumeric(dnsAddr)) {
InetAddress ia;
try {
ia = NetworkUtils.numericToInetAddress(dnsAddr);
} catch (IllegalArgumentException e) {
EventLogTags.writePdpBadDnsAddress("dnsAddr=" + dnsAddr);
throw new UnknownHostException("Non-numeric dns addr="
+ dnsAddr);
}
InetAddress ia = InetAddress.getByName(dnsAddr);
linkProperties.addDns(ia);
}
result = SetupResult.SUCCESS;
@@ -476,11 +483,13 @@ public abstract class DataConnection extends HierarchicalStateMachine {
}
}
for (String addr : response.gateways) {
if (!InetAddress.isNumeric(addr)) {
InetAddress ia;
try {
ia = NetworkUtils.numericToInetAddress(addr);
} catch (IllegalArgumentException e) {
EventLogTags.writePdpBadDnsAddress("gateway=" + addr);
throw new UnknownHostException("Non-numeric gateway addr=" + addr);
}
InetAddress ia = InetAddress.getByName(addr);
linkProperties.addGateway(ia);
}
result = SetupResult.SUCCESS;

View File

@@ -686,13 +686,15 @@ class WifiConfigStore {
} else if (key.equals(IP_ASSIGNMENT_KEY)) {
ipAssignment = IpAssignment.valueOf(in.readUTF());
} else if (key.equals(LINK_ADDRESS_KEY)) {
LinkAddress linkAddr = new LinkAddress(InetAddress.getByName(
in.readUTF()), in.readInt());
LinkAddress linkAddr = new LinkAddress(
NetworkUtils.numericToInetAddress(in.readUTF()), in.readInt());
linkProperties.addLinkAddress(linkAddr);
} else if (key.equals(GATEWAY_KEY)) {
linkProperties.addGateway(InetAddress.getByName(in.readUTF()));
linkProperties.addGateway(
NetworkUtils.numericToInetAddress(in.readUTF()));
} else if (key.equals(DNS_KEY)) {
linkProperties.addDns(InetAddress.getByName(in.readUTF()));
linkProperties.addDns(
NetworkUtils.numericToInetAddress(in.readUTF()));
} else if (key.equals(PROXY_SETTINGS_KEY)) {
proxySettings = ProxySettings.valueOf(in.readUTF());
} else if (key.equals(PROXY_HOST_KEY)) {
@@ -706,7 +708,7 @@ class WifiConfigStore {
} else {
Log.e(TAG, "Ignore unknown key " + key + "while reading");
}
} catch (UnknownHostException e) {
} catch (IllegalArgumentException e) {
Log.e(TAG, "Ignore invalid address while reading" + e);
}
} while (true);

View File

@@ -999,7 +999,8 @@ public class WifiStateMachine extends HierarchicalStateMachine {
ifcg = service.getInterfaceConfig(intf);
if (ifcg != null) {
/* IP/netmask: 192.168.43.1/255.255.255.0 */
ifcg.addr = new LinkAddress(InetAddress.getByName("192.168.43.1"), 24);
ifcg.addr = new LinkAddress(NetworkUtils.numericToInetAddress(
"192.168.43.1"), 24);
ifcg.interfaceFlags = "[up]";
service.setInterfaceConfig(intf, ifcg);