Make the interface with DHCP IPv6 capable.
It doesn't work (yet) for IPv6, but we can remove v4-centric notions from the framework. bug:2542681 Change-Id: I21c058f5c88d07706c9265bf0ea902fc90357e56
This commit is contained in:
@@ -77,7 +77,7 @@ public class NetworkUtils {
|
|||||||
* the IP address information.
|
* the IP address information.
|
||||||
* @return {@code true} for success, {@code false} for failure
|
* @return {@code true} for success, {@code false} for failure
|
||||||
*/
|
*/
|
||||||
public native static boolean runDhcp(String interfaceName, DhcpInfo ipInfo);
|
public native static boolean runDhcp(String interfaceName, DhcpInfoInternal ipInfo);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shut down the DHCP client daemon.
|
* Shut down the DHCP client daemon.
|
||||||
@@ -149,6 +149,29 @@ public class NetworkUtils {
|
|||||||
return Integer.reverseBytes(value);
|
return Integer.reverseBytes(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
* @param addrString
|
||||||
|
* @return the InetAddress
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
public static InetAddress numericToInetAddress(String addrString)
|
||||||
|
throws IllegalArgumentException {
|
||||||
|
// TODO - do this for real, using a hidden method on InetAddress that aborts
|
||||||
|
// instead of doing dns step
|
||||||
|
if (!InetAddress.isNumeric(addrString)) {
|
||||||
|
throw new IllegalArgumentException("numericToInetAddress with non numeric: " +
|
||||||
|
addrString);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return InetAddress.getByName(addrString);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
throw new IllegalArgumentException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a default route through the specified gateway.
|
* Add a default route through the specified gateway.
|
||||||
* @param interfaceName interface on which the route should be added
|
* @param interfaceName interface on which the route should be added
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#include <android_runtime/AndroidRuntime.h>
|
#include <android_runtime/AndroidRuntime.h>
|
||||||
#include <utils/Log.h>
|
#include <utils/Log.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <cutils/properties.h>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
int ifc_enable(const char *ifname);
|
int ifc_enable(const char *ifname);
|
||||||
@@ -32,12 +33,12 @@ int ifc_remove_default_route(const char *ifname);
|
|||||||
int ifc_reset_connections(const char *ifname);
|
int ifc_reset_connections(const char *ifname);
|
||||||
|
|
||||||
int dhcp_do_request(const char *ifname,
|
int dhcp_do_request(const char *ifname,
|
||||||
in_addr_t *ipaddr,
|
const char *ipaddr,
|
||||||
in_addr_t *gateway,
|
const char *gateway,
|
||||||
in_addr_t *mask,
|
uint32_t *prefixLength,
|
||||||
in_addr_t *dns1,
|
const char *dns1,
|
||||||
in_addr_t *dns2,
|
const char *dns2,
|
||||||
in_addr_t *server,
|
const char *server,
|
||||||
uint32_t *lease);
|
uint32_t *lease);
|
||||||
int dhcp_stop(const char *ifname);
|
int dhcp_stop(const char *ifname);
|
||||||
int dhcp_release_lease(const char *ifname);
|
int dhcp_release_lease(const char *ifname);
|
||||||
@@ -54,16 +55,16 @@ namespace android {
|
|||||||
* to look them up every time.
|
* to look them up every time.
|
||||||
*/
|
*/
|
||||||
static struct fieldIds {
|
static struct fieldIds {
|
||||||
jclass dhcpInfoClass;
|
jclass dhcpInfoInternalClass;
|
||||||
jmethodID constructorId;
|
jmethodID constructorId;
|
||||||
jfieldID ipaddress;
|
jfieldID ipaddress;
|
||||||
jfieldID gateway;
|
jfieldID gateway;
|
||||||
jfieldID netmask;
|
jfieldID prefixLength;
|
||||||
jfieldID dns1;
|
jfieldID dns1;
|
||||||
jfieldID dns2;
|
jfieldID dns2;
|
||||||
jfieldID serverAddress;
|
jfieldID serverAddress;
|
||||||
jfieldID leaseDuration;
|
jfieldID leaseDuration;
|
||||||
} dhcpInfoFieldIds;
|
} dhcpInfoInternalFieldIds;
|
||||||
|
|
||||||
static jint android_net_utils_enableInterface(JNIEnv* env, jobject clazz, jstring ifname)
|
static jint android_net_utils_enableInterface(JNIEnv* env, jobject clazz, jstring ifname)
|
||||||
{
|
{
|
||||||
@@ -148,21 +149,29 @@ static jint android_net_utils_resetConnections(JNIEnv* env, jobject clazz, jstri
|
|||||||
static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
|
static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
in_addr_t ipaddr, gateway, mask, dns1, dns2, server;
|
char ipaddr[PROPERTY_VALUE_MAX];
|
||||||
|
uint32_t prefixLength;
|
||||||
|
char gateway[PROPERTY_VALUE_MAX];
|
||||||
|
char dns1[PROPERTY_VALUE_MAX];
|
||||||
|
char dns2[PROPERTY_VALUE_MAX];
|
||||||
|
char server[PROPERTY_VALUE_MAX];
|
||||||
uint32_t lease;
|
uint32_t lease;
|
||||||
|
|
||||||
const char *nameStr = env->GetStringUTFChars(ifname, NULL);
|
const char *nameStr = env->GetStringUTFChars(ifname, NULL);
|
||||||
result = ::dhcp_do_request(nameStr, &ipaddr, &gateway, &mask,
|
if (nameStr == NULL) return (jboolean)false;
|
||||||
&dns1, &dns2, &server, &lease);
|
|
||||||
|
result = ::dhcp_do_request(nameStr, ipaddr, gateway, &prefixLength,
|
||||||
|
dns1, dns2, server, &lease);
|
||||||
env->ReleaseStringUTFChars(ifname, nameStr);
|
env->ReleaseStringUTFChars(ifname, nameStr);
|
||||||
if (result == 0 && dhcpInfoFieldIds.dhcpInfoClass != NULL) {
|
if (result == 0 && dhcpInfoInternalFieldIds.dhcpInfoInternalClass != NULL) {
|
||||||
env->SetIntField(info, dhcpInfoFieldIds.ipaddress, ipaddr);
|
env->SetObjectField(info, dhcpInfoInternalFieldIds.ipaddress, env->NewStringUTF(ipaddr));
|
||||||
env->SetIntField(info, dhcpInfoFieldIds.gateway, gateway);
|
env->SetObjectField(info, dhcpInfoInternalFieldIds.gateway, env->NewStringUTF(gateway));
|
||||||
env->SetIntField(info, dhcpInfoFieldIds.netmask, mask);
|
env->SetIntField(info, dhcpInfoInternalFieldIds.prefixLength, prefixLength);
|
||||||
env->SetIntField(info, dhcpInfoFieldIds.dns1, dns1);
|
env->SetObjectField(info, dhcpInfoInternalFieldIds.dns1, env->NewStringUTF(dns1));
|
||||||
env->SetIntField(info, dhcpInfoFieldIds.dns2, dns2);
|
env->SetObjectField(info, dhcpInfoInternalFieldIds.dns2, env->NewStringUTF(dns2));
|
||||||
env->SetIntField(info, dhcpInfoFieldIds.serverAddress, server);
|
env->SetObjectField(info, dhcpInfoInternalFieldIds.serverAddress,
|
||||||
env->SetIntField(info, dhcpInfoFieldIds.leaseDuration, lease);
|
env->NewStringUTF(server));
|
||||||
|
env->SetIntField(info, dhcpInfoInternalFieldIds.leaseDuration, lease);
|
||||||
}
|
}
|
||||||
return (jboolean)(result == 0);
|
return (jboolean)(result == 0);
|
||||||
}
|
}
|
||||||
@@ -209,7 +218,7 @@ static JNINativeMethod gNetworkUtilMethods[] = {
|
|||||||
(void *)android_net_utils_getDefaultRoute },
|
(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 },
|
||||||
{ "runDhcp", "(Ljava/lang/String;Landroid/net/DhcpInfo;)Z", (void *)android_net_utils_runDhcp },
|
{ "runDhcp", "(Ljava/lang/String;Landroid/net/DhcpInfoInternal;)Z", (void *)android_net_utils_runDhcp },
|
||||||
{ "stopDhcp", "(Ljava/lang/String;)Z", (void *)android_net_utils_stopDhcp },
|
{ "stopDhcp", "(Ljava/lang/String;)Z", (void *)android_net_utils_stopDhcp },
|
||||||
{ "releaseDhcpLease", "(Ljava/lang/String;)Z", (void *)android_net_utils_releaseDhcpLease },
|
{ "releaseDhcpLease", "(Ljava/lang/String;)Z", (void *)android_net_utils_releaseDhcpLease },
|
||||||
{ "getDhcpError", "()Ljava/lang/String;", (void*) android_net_utils_getDhcpError },
|
{ "getDhcpError", "()Ljava/lang/String;", (void*) android_net_utils_getDhcpError },
|
||||||
@@ -220,16 +229,16 @@ int register_android_net_NetworkUtils(JNIEnv* env)
|
|||||||
jclass netutils = env->FindClass(NETUTILS_PKG_NAME);
|
jclass netutils = env->FindClass(NETUTILS_PKG_NAME);
|
||||||
LOG_FATAL_IF(netutils == NULL, "Unable to find class " NETUTILS_PKG_NAME);
|
LOG_FATAL_IF(netutils == NULL, "Unable to find class " NETUTILS_PKG_NAME);
|
||||||
|
|
||||||
dhcpInfoFieldIds.dhcpInfoClass = env->FindClass("android/net/DhcpInfo");
|
dhcpInfoInternalFieldIds.dhcpInfoInternalClass = env->FindClass("android/net/DhcpInfoInternal");
|
||||||
if (dhcpInfoFieldIds.dhcpInfoClass != NULL) {
|
if (dhcpInfoInternalFieldIds.dhcpInfoInternalClass != NULL) {
|
||||||
dhcpInfoFieldIds.constructorId = env->GetMethodID(dhcpInfoFieldIds.dhcpInfoClass, "<init>", "()V");
|
dhcpInfoInternalFieldIds.constructorId = env->GetMethodID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "<init>", "()V");
|
||||||
dhcpInfoFieldIds.ipaddress = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "ipAddress", "I");
|
dhcpInfoInternalFieldIds.ipaddress = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "ipAddress", "Ljava/lang/String;");
|
||||||
dhcpInfoFieldIds.gateway = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "gateway", "I");
|
dhcpInfoInternalFieldIds.gateway = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "gateway", "Ljava/lang/String;");
|
||||||
dhcpInfoFieldIds.netmask = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "netmask", "I");
|
dhcpInfoInternalFieldIds.prefixLength = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "prefixLength", "I");
|
||||||
dhcpInfoFieldIds.dns1 = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "dns1", "I");
|
dhcpInfoInternalFieldIds.dns1 = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "dns1", "Ljava/lang/String;");
|
||||||
dhcpInfoFieldIds.dns2 = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "dns2", "I");
|
dhcpInfoInternalFieldIds.dns2 = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "dns2", "Ljava/lang/String;");
|
||||||
dhcpInfoFieldIds.serverAddress = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "serverAddress", "I");
|
dhcpInfoInternalFieldIds.serverAddress = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "serverAddress", "Ljava/lang/String;");
|
||||||
dhcpInfoFieldIds.leaseDuration = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "leaseDuration", "I");
|
dhcpInfoInternalFieldIds.leaseDuration = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "leaseDuration", "I");
|
||||||
}
|
}
|
||||||
|
|
||||||
return AndroidRuntime::registerNativeMethods(env,
|
return AndroidRuntime::registerNativeMethods(env,
|
||||||
|
|||||||
Reference in New Issue
Block a user