diff options
author | Robert Greenwalt <rgreenwalt@google.com> | 2011-05-10 15:05:02 -0700 |
---|---|---|
committer | Robert Greenwalt <rgreenwalt@google.com> | 2011-05-18 15:25:43 -0700 |
commit | 59b1a4ede7032c1b4d897e13dd4ede09b5e14743 (patch) | |
tree | 2dde7b9096bc3eea78f0895bf892d39888a89724 /core/java/android | |
parent | 8416e5cf0a8439a3f605629068d3639f2a35e08b (diff) | |
download | frameworks_base-59b1a4ede7032c1b4d897e13dd4ede09b5e14743.zip frameworks_base-59b1a4ede7032c1b4d897e13dd4ede09b5e14743.tar.gz frameworks_base-59b1a4ede7032c1b4d897e13dd4ede09b5e14743.tar.bz2 |
Switch to use netd to add/remove routes.
Also adds support for v6 routes and for removing single routes.
Change-Id: I1c4f08c7938371090944d8d6f603e1e0d6d70c01
Diffstat (limited to 'core/java/android')
-rw-r--r-- | core/java/android/net/NetworkUtils.java | 110 | ||||
-rw-r--r-- | core/java/android/net/RouteInfo.java | 32 | ||||
-rw-r--r-- | core/java/android/os/INetworkManagementService.aidl | 17 |
3 files changed, 69 insertions, 90 deletions
diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java index fbe5379..8a678d6 100644 --- a/core/java/android/net/NetworkUtils.java +++ b/core/java/android/net/NetworkUtils.java @@ -38,32 +38,6 @@ public class NetworkUtils { /** Bring the named network interface down. */ public native static int disableInterface(String interfaceName); - /** - * Add a route to the routing table. - * - * @param interfaceName the interface to route through. - * @param dst the network or host to route to. May be IPv4 or IPv6, e.g. - * "0.0.0.0" or "2001:4860::". - * @param prefixLength the prefix length of the route. - * @param gw the gateway to use, e.g., "192.168.251.1". If null, - * indicates a directly-connected route. - */ - public native static int addRoute(String interfaceName, String dst, - int prefixLength, String gw); - - /** Return the gateway address for the default route for the named interface. */ - public static InetAddress getDefaultRoute(String interfaceName) { - int addr = getDefaultRouteNative(interfaceName); - return intToInetAddress(addr); - } - private native static int getDefaultRouteNative(String interfaceName); - - /** Remove host routes that uses the named interface. */ - public native static int removeHostRoutes(String interfaceName); - - /** Remove the default route for the named interface. */ - public native static int removeDefaultRoute(String interfaceName); - /** Reset any sockets that are connected via the named interface. */ public native static int resetConnections(String interfaceName); @@ -160,6 +134,15 @@ public class NetworkUtils { } /** + * Convert a IPv4 netmask integer to a prefix length + * @param netmask as an integer in network byte order + * @return the network prefix length + */ + public static int netmaskIntToPrefixLength(int netmask) { + return Integer.bitCount(netmask); + } + + /** * Create an InetAddress from a string where the string must be a standard * representation of a V4 or V6 address. Avoids doing a DNS lookup on failure * but it will throw an IllegalArgumentException in that case. @@ -173,60 +156,6 @@ public class NetworkUtils { } /** - * Add a default route through the specified gateway. - * @param interfaceName interface on which the route should be added - * @param gw the IP address of the gateway to which the route is desired, - * @return {@code true} on success, {@code false} on failure - */ - public static boolean addDefaultRoute(String interfaceName, InetAddress gw) { - String dstStr; - String gwStr = gw.getHostAddress(); - - if (gw instanceof Inet4Address) { - dstStr = "0.0.0.0"; - } else if (gw instanceof Inet6Address) { - dstStr = "::"; - } else { - Log.w(TAG, "addDefaultRoute failure: address is neither IPv4 nor IPv6" + - "(" + gwStr + ")"); - return false; - } - return addRoute(interfaceName, dstStr, 0, gwStr) == 0; - } - - /** - * Add a host route. - * @param interfaceName interface on which the route should be added - * @param dst the IP address of the host to which the route is desired, - * this should not be null. - * @param gw the IP address of the gateway to which the route is desired, - * if null, indicates a directly-connected route. - * @return {@code true} on success, {@code false} on failure - */ - public static boolean addHostRoute(String interfaceName, InetAddress dst, - InetAddress gw) { - if (dst == null) { - Log.w(TAG, "addHostRoute: dst should not be null"); - return false; - } - - int prefixLength; - String dstStr = dst.getHostAddress(); - String gwStr = (gw != null) ? gw.getHostAddress() : null; - - if (dst instanceof Inet4Address) { - prefixLength = 32; - } else if (dst instanceof Inet6Address) { - prefixLength = 128; - } else { - Log.w(TAG, "addHostRoute failure: address is neither IPv4 nor IPv6" + - "(" + dst + ")"); - return false; - } - return addRoute(interfaceName, dstStr, prefixLength, gwStr) == 0; - } - - /** * Get InetAddress masked with prefixLength. Will never return null. * @param IP address which will be masked with specified prefixLength * @param prefixLength the prefixLength used to mask the IP @@ -271,4 +200,25 @@ public class NetworkUtils { return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) || ((left instanceof Inet6Address) && (right instanceof Inet6Address))); } + + /** + * Convert a 32 char hex string into a Inet6Address. + * throws a runtime exception if the string isn't 32 chars, isn't hex or can't be + * made into an Inet6Address + * @param addrHexString a 32 character hex string representing an IPv6 addr + * @return addr an InetAddress representation for the string + */ + public static InetAddress hexToInet6Address(String addrHexString) + throws IllegalArgumentException { + try { + return numericToInetAddress(String.format("%s:%s:%s:%s:%s:%s:%s:%s", + addrHexString.substring(0,4), addrHexString.substring(4,8), + addrHexString.substring(8,12), addrHexString.substring(12,16), + addrHexString.substring(16,20), addrHexString.substring(20,24), + addrHexString.substring(24,28), addrHexString.substring(28,32))); + } catch (Exception e) { + Log.e("NetworkUtils", "error in hexToInet6Address(" + addrHexString + "): " + e); + throw new IllegalArgumentException(e); + } + } } diff --git a/core/java/android/net/RouteInfo.java b/core/java/android/net/RouteInfo.java index 39e708a..c5288c4 100644 --- a/core/java/android/net/RouteInfo.java +++ b/core/java/android/net/RouteInfo.java @@ -46,18 +46,16 @@ public class RouteInfo implements Parcelable { public RouteInfo(LinkAddress destination, InetAddress gateway) { if (destination == null) { - try { - if (gateway != null) { - if (gateway instanceof Inet4Address) { - destination = new LinkAddress(Inet4Address.ANY, 0); - } else { - destination = new LinkAddress(Inet6Address.ANY, 0); - } + if (gateway != null) { + if (gateway instanceof Inet4Address) { + destination = new LinkAddress(Inet4Address.ANY, 0); } else { - // no destination, no gateway. invalid. - throw new RuntimeException("Invalid arguments passed in."); + destination = new LinkAddress(Inet6Address.ANY, 0); } - } catch (Exception e) {} + } else { + // no destination, no gateway. invalid. + throw new RuntimeException("Invalid arguments passed in."); + } } if (gateway == null) { if (destination.getAddress() instanceof Inet4Address) { @@ -76,6 +74,20 @@ public class RouteInfo implements Parcelable { this(null, gateway); } + public static RouteInfo makeHostRoute(InetAddress host) { + return makeHostRoute(host, null); + } + + public static RouteInfo makeHostRoute(InetAddress host, InetAddress gateway) { + if (host == null) return null; + + if (host instanceof Inet4Address) { + return new RouteInfo(new LinkAddress(host, 32), gateway); + } else { + return new RouteInfo(new LinkAddress(host, 128), gateway); + } + } + private boolean isDefault() { boolean val = false; if (mGateway != null) { diff --git a/core/java/android/os/INetworkManagementService.aidl b/core/java/android/os/INetworkManagementService.aidl index 212c5fb..5a245f8 100644 --- a/core/java/android/os/INetworkManagementService.aidl +++ b/core/java/android/os/INetworkManagementService.aidl @@ -19,6 +19,7 @@ package android.os; import android.net.InterfaceConfiguration; import android.net.INetworkManagementEventObserver; +import android.net.RouteInfo; import android.net.wifi.WifiConfiguration; /** @@ -57,6 +58,22 @@ interface INetworkManagementService void setInterfaceConfig(String iface, in InterfaceConfiguration cfg); /** + * Retrieves the network routes currently configured on the specified + * interface + */ + RouteInfo[] getRoutes(String iface); + + /** + * Add the specified route to the interface. + */ + void addRoute(String iface, in RouteInfo route); + + /** + * Remove the specified route from the interface. + */ + void removeRoute(String iface, in RouteInfo route); + + /** * Shuts down the service */ void shutdown(); |