From e590373ea71251cfffc8f22f011e2e6335dce716 Mon Sep 17 00:00:00 2001 From: Robert Greenwalt Date: Tue, 22 Feb 2011 16:00:42 -0800 Subject: Start using NetworkUtils.numericToInetAddress. Generates InetAddresses without risking an accidental dns lookup. For use with supposedly numeric-only ip address strings. Change-Id: I694f3976ce1c6382854706f6557ea88a289add3a --- .../java/com/android/server/ConnectivityService.java | 4 ++-- .../com/android/server/NetworkManagementService.java | 20 ++++++++++---------- .../com/android/server/connectivity/Tethering.java | 3 ++- 3 files changed, 14 insertions(+), 13 deletions(-) (limited to 'services') diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java index 8e39a63..b49f216 100644 --- a/services/java/com/android/server/ConnectivityService.java +++ b/services/java/com/android/server/ConnectivityService.java @@ -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); } diff --git a/services/java/com/android/server/NetworkManagementService.java b/services/java/com/android/server/NetworkManagementService.java index eaf68b0..44f5df2 100644 --- a/services/java/com/android/server/NetworkManagementService.java +++ b/services/java/com/android/server/NetworkManagementService.java @@ -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); diff --git a/services/java/com/android/server/connectivity/Tethering.java b/services/java/com/android/server/connectivity/Tethering.java index f24f96c..f9f0c4a 100644 --- a/services/java/com/android/server/connectivity/Tethering.java +++ b/services/java/com/android/server/connectivity/Tethering.java @@ -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"); -- cgit v1.1