Add an API to request route to an IPv6 host.

Add API to create a route to an IPv6 host through a particular
interface.

Change-Id: I7649051e94832576e02b5f5ad17abe093d21d48e
This commit is contained in:
Banavathu, Srinivas Naik
2010-08-10 20:13:53 +05:30
committed by Banavathu Srinivas Naik
parent 2198ddbc38
commit 7601f575aa
5 changed files with 162 additions and 39 deletions

View File

@@ -21,6 +21,9 @@ import android.annotation.SdkConstant.SdkConstantType;
import android.os.Binder; import android.os.Binder;
import android.os.RemoteException; import android.os.RemoteException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/** /**
* Class that answers queries about the state of network connectivity. It also * Class that answers queries about the state of network connectivity. It also
* notifies applications when network connectivity changes. Get an instance * notifies applications when network connectivity changes. Get an instance
@@ -309,8 +312,29 @@ public class ConnectivityManager
* @return {@code true} on success, {@code false} on failure * @return {@code true} on success, {@code false} on failure
*/ */
public boolean requestRouteToHost(int networkType, int hostAddress) { public boolean requestRouteToHost(int networkType, int hostAddress) {
InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
if (inetAddress == null) {
return false;
}
return requestRouteToHostAddress(networkType, inetAddress);
}
/**
* Ensure that a network route exists to deliver traffic to the specified
* host via the specified network interface. An attempt to add a route that
* already exists is ignored, but treated as successful.
* @param networkType the type of the network over which traffic to the specified
* host is to be routed
* @param hostAddress the IP address of the host to which the route is desired
* @return {@code true} on success, {@code false} on failure
* @hide
*/
public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
byte[] address = hostAddress.getAddress();
try { try {
return mService.requestRouteToHost(networkType, hostAddress); return mService.requestRouteToHostAddress(networkType, address);
} catch (RemoteException e) { } catch (RemoteException e) {
return false; return false;
} }

View File

@@ -47,6 +47,8 @@ interface IConnectivityManager
boolean requestRouteToHost(int networkType, int hostAddress); boolean requestRouteToHost(int networkType, int hostAddress);
boolean requestRouteToHostAddress(int networkType, in byte[] hostAddress);
boolean getBackgroundDataSetting(); boolean getBackgroundDataSetting();
void setBackgroundDataSetting(boolean allowBackgroundData); void setBackgroundDataSetting(boolean allowBackgroundData);

View File

@@ -17,25 +17,39 @@
package android.net; package android.net;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import android.util.Log;
/** /**
* Native methods for managing network interfaces. * Native methods for managing network interfaces.
* *
* {@hide} * {@hide}
*/ */
public class NetworkUtils { public class NetworkUtils {
private static final String TAG = "NetworkUtils";
/** Bring the named network interface up. */ /** Bring the named network interface up. */
public native static int enableInterface(String interfaceName); public native static int enableInterface(String interfaceName);
/** Bring the named network interface down. */ /** Bring the named network interface down. */
public native static int disableInterface(String interfaceName); public native static int disableInterface(String interfaceName);
/** Add a route to the specified host via the named interface. */ /**
public native static int addHostRoute(String interfaceName, int hostaddr); * Add a route to the routing table.
*
/** Add a default route for the named interface. */ * @param interfaceName the interface to route through.
public native static int setDefaultRoute(String interfaceName, int gwayAddr); * @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. */ /** Return the gateway address for the default route for the named interface. */
public native static int getDefaultRoute(String interfaceName); public native static int getDefaultRoute(String interfaceName);
@@ -106,26 +120,78 @@ public class NetworkUtils {
String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2); String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2);
/** /**
* Look up a host name and return the result as an int. Works if the argument * Convert a IPv4 address from an integer to an InetAddress.
* is an IP address in dot notation. Obviously, this can only be used for IPv4 * @param hostAddr is an Int corresponding to the IPv4 address in network byte order
* addresses. * @return the IP address as an {@code InetAddress}, returns null if
* @param hostname the name of the host (or the IP address) * unable to convert or if the int is an invalid address.
* @return the IP address as an {@code int} in network byte order
*/ */
public static int lookupHost(String hostname) { public static InetAddress intToInetAddress(int hostAddress) {
InetAddress inetAddress; InetAddress inetAddress;
byte[] addressBytes = { (byte)(0xff & hostAddress),
(byte)(0xff & (hostAddress >> 8)),
(byte)(0xff & (hostAddress >> 16)),
(byte)(0xff & (hostAddress >> 24)) };
try { try {
inetAddress = InetAddress.getByName(hostname); inetAddress = InetAddress.getByAddress(addressBytes);
} catch(UnknownHostException e) { } catch(UnknownHostException e) {
return -1; return null;
} }
byte[] addrBytes;
int addr; return inetAddress;
addrBytes = inetAddress.getAddress(); }
addr = ((addrBytes[3] & 0xff) << 24)
| ((addrBytes[2] & 0xff) << 16) /**
| ((addrBytes[1] & 0xff) << 8) * Add a default route through the specified gateway.
| (addrBytes[0] & 0xff); * @param interfaceName interface on which the route should be added
return addr; * @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;
} }
} }

View File

@@ -66,13 +66,23 @@ static jint android_net_utils_disableInterface(JNIEnv* env, jobject clazz, jstri
return (jint)result; return (jint)result;
} }
static jint android_net_utils_addHostRoute(JNIEnv* env, jobject clazz, jstring ifname, jint addr) static jint android_net_utils_addRoute(JNIEnv* env, jobject clazz, jstring ifname,
jstring dst, jint prefixLength, jstring gw)
{ {
int result; int result;
const char *nameStr = env->GetStringUTFChars(ifname, NULL); const char *nameStr = env->GetStringUTFChars(ifname, NULL);
result = ::ifc_add_host_route(nameStr, addr); const char *dstStr = env->GetStringUTFChars(dst, NULL);
const char *gwStr = NULL;
if (gw != NULL) {
gwStr = env->GetStringUTFChars(gw, NULL);
}
result = ::ifc_add_route(nameStr, dstStr, prefixLength, gwStr);
env->ReleaseStringUTFChars(ifname, nameStr); env->ReleaseStringUTFChars(ifname, nameStr);
env->ReleaseStringUTFChars(dst, dstStr);
if (gw != NULL) {
env->ReleaseStringUTFChars(gw, gwStr);
}
return (jint)result; return (jint)result;
} }
@@ -86,16 +96,6 @@ static jint android_net_utils_removeHostRoutes(JNIEnv* env, jobject clazz, jstri
return (jint)result; return (jint)result;
} }
static jint android_net_utils_setDefaultRoute(JNIEnv* env, jobject clazz, jstring ifname, jint gateway)
{
int result;
const char *nameStr = env->GetStringUTFChars(ifname, NULL);
result = ::ifc_set_default_route(nameStr, gateway);
env->ReleaseStringUTFChars(ifname, nameStr);
return (jint)result;
}
static jint android_net_utils_getDefaultRoute(JNIEnv* env, jobject clazz, jstring ifname) static jint android_net_utils_getDefaultRoute(JNIEnv* env, jobject clazz, jstring ifname)
{ {
int result; int result;
@@ -201,9 +201,9 @@ static JNINativeMethod gNetworkUtilMethods[] = {
{ "enableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_enableInterface }, { "enableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_enableInterface },
{ "disableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_disableInterface }, { "disableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_disableInterface },
{ "addHostRoute", "(Ljava/lang/String;I)I", (void *)android_net_utils_addHostRoute }, { "addRoute", "(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)I",
(void *)android_net_utils_addRoute },
{ "removeHostRoutes", "(Ljava/lang/String;)I", (void *)android_net_utils_removeHostRoutes }, { "removeHostRoutes", "(Ljava/lang/String;)I", (void *)android_net_utils_removeHostRoutes },
{ "setDefaultRoute", "(Ljava/lang/String;I)I", (void *)android_net_utils_setDefaultRoute },
{ "getDefaultRoute", "(Ljava/lang/String;)I", (void *)android_net_utils_getDefaultRoute }, { "getDefaultRoute", "(Ljava/lang/String;)I", (void *)android_net_utils_getDefaultRoute },
{ "removeDefaultRoute", "(Ljava/lang/String;)I", (void *)android_net_utils_removeDefaultRoute }, { "removeDefaultRoute", "(Ljava/lang/String;)I", (void *)android_net_utils_removeDefaultRoute },
{ "resetConnections", "(Ljava/lang/String;)I", (void *)android_net_utils_resetConnections }, { "resetConnections", "(Ljava/lang/String;)I", (void *)android_net_utils_resetConnections },

View File

@@ -27,6 +27,7 @@ import android.net.IConnectivityManager;
import android.net.MobileDataStateTracker; import android.net.MobileDataStateTracker;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.NetworkStateTracker; import android.net.NetworkStateTracker;
import android.net.NetworkUtils;
import android.net.wifi.WifiStateTracker; import android.net.wifi.WifiStateTracker;
import android.os.Binder; import android.os.Binder;
import android.os.Handler; import android.os.Handler;
@@ -49,6 +50,8 @@ import java.io.FileDescriptor;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.net.InetAddress;
import java.net.UnknownHostException;
/** /**
* @hide * @hide
@@ -730,6 +733,8 @@ public class ConnectivityService extends IConnectivityManager.Stub {
} }
/** /**
* @deprecated use requestRouteToHostAddress instead
*
* Ensure that a network route exists to deliver traffic to the specified * Ensure that a network route exists to deliver traffic to the specified
* host via the specified network interface. * host via the specified network interface.
* @param networkType the type of the network over which traffic to the * @param networkType the type of the network over which traffic to the
@@ -739,6 +744,25 @@ public class ConnectivityService extends IConnectivityManager.Stub {
* @return {@code true} on success, {@code false} on failure * @return {@code true} on success, {@code false} on failure
*/ */
public boolean requestRouteToHost(int networkType, int hostAddress) { public boolean requestRouteToHost(int networkType, int hostAddress) {
InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
if (inetAddress == null) {
return false;
}
return requestRouteToHostAddress(networkType, inetAddress.getAddress());
}
/**
* Ensure that a network route exists to deliver traffic to the specified
* host via the specified network interface.
* @param networkType the type of the network over which traffic to the
* specified host is to be routed
* @param hostAddress the IP address of the host to which the route is
* desired
* @return {@code true} on success, {@code false} on failure
*/
public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
enforceChangePermission(); enforceChangePermission();
if (!ConnectivityManager.isNetworkTypeValid(networkType)) { if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
return false; return false;
@@ -748,11 +772,18 @@ public class ConnectivityService extends IConnectivityManager.Stub {
if (tracker == null || !tracker.getNetworkInfo().isConnected() || if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
tracker.isTeardownRequested()) { tracker.isTeardownRequested()) {
if (DBG) { if (DBG) {
Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped"); Slog.d(TAG, "requestRouteToHostAddress on down network " +
"(" + networkType + ") - dropped");
} }
return false; return false;
} }
return tracker.requestRouteToHost(hostAddress);
try {
InetAddress inetAddress = InetAddress.getByAddress(hostAddress);
return tracker.requestRouteToHost(inetAddress);
} catch (UnknownHostException e) {
return false;
}
} }
/** /**