Merge "Refactor NetworkUtils interaction with DHCP." into m-wireless-dev

This commit is contained in:
Erik Kline
2015-03-03 06:34:06 +00:00
committed by Android Partner Code Review
2 changed files with 77 additions and 43 deletions

View File

@@ -54,6 +54,30 @@ public class NetworkUtils {
*/ */
public native static int resetConnections(String interfaceName, int mask); public native static int resetConnections(String interfaceName, int mask);
/**
* Start the DHCP client daemon, in order to have it request addresses
* for the named interface. This returns {@code true} if the DHCPv4 daemon
* starts, {@code false} otherwise. This call blocks until such time as a
* result is available or the default discovery timeout has been reached.
* Callers should check {@link #getDhcpResults} to determine whether DHCP
* succeeded or failed, and if it succeeded, to fetch the {@link DhcpResults}.
* @param interfaceName the name of the interface to configure
* @return {@code true} for success, {@code false} for failure
*/
public native static boolean startDhcp(String interfaceName);
/**
* Initiate renewal on the DHCP client daemon for the named interface. This
* returns {@code true} if the DHCPv4 daemon has been notified, {@code false}
* otherwise. This call blocks until such time as a result is available or
* the default renew timeout has been reached. Callers should check
* {@link #getDhcpResults} to determine whether DHCP succeeded or failed,
* and if it succeeded, to fetch the {@link DhcpResults}.
* @param interfaceName the name of the interface to configure
* @return {@code true} for success, {@code false} for failure
*/
public native static boolean startDhcpRenew(String interfaceName);
/** /**
* Start the DHCP client daemon, in order to have it request addresses * Start the DHCP client daemon, in order to have it request addresses
* for the named interface, and then configure the interface with those * for the named interface, and then configure the interface with those
@@ -64,17 +88,31 @@ 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, DhcpResults dhcpResults); public static boolean runDhcp(String interfaceName, DhcpResults dhcpResults) {
return startDhcp(interfaceName) && getDhcpResults(interfaceName, dhcpResults);
}
/** /**
* Initiate renewal on the Dhcp client daemon. This call blocks until it obtains * Initiate renewal on the DHCP client daemon. This call blocks until it obtains
* a result (either success or failure) from the daemon. * a result (either success or failure) from the daemon.
* @param interfaceName the name of the interface to configure * @param interfaceName the name of the interface to configure
* @param dhcpResults if the request succeeds, this object is filled in with * @param dhcpResults if the request succeeds, this object is filled in with
* 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 runDhcpRenew(String interfaceName, DhcpResults dhcpResults); public static boolean runDhcpRenew(String interfaceName, DhcpResults dhcpResults) {
return startDhcpRenew(interfaceName) && getDhcpResults(interfaceName, dhcpResults);
}
/**
* Fetch results from the DHCP client daemon. This call returns {@code true} if
* if there are results available to be read, {@code false} otherwise.
* @param interfaceName the name of the interface to configure
* @param dhcpResults if the request succeeds, this object is filled in with
* the IP address information.
* @return {@code true} for success, {@code false} for failure
*/
public native static boolean getDhcpResults(String interfaceName, DhcpResults dhcpResults);
/** /**
* Shut down the DHCP client daemon. * Shut down the DHCP client daemon.

View File

@@ -31,18 +31,9 @@ int ifc_enable(const char *ifname);
int ifc_disable(const char *ifname); int ifc_disable(const char *ifname);
int ifc_reset_connections(const char *ifname, int reset_mask); int ifc_reset_connections(const char *ifname, int reset_mask);
int dhcp_do_request(const char * const ifname, int dhcp_start(const char * const ifname);
const char *ipaddr, int dhcp_start_renew(const char * const ifname);
const char *gateway, int dhcp_get_results(const char * const ifname,
uint32_t *prefixLength,
const char *dns[],
const char *server,
uint32_t *lease,
const char *vendorInfo,
const char *domains,
const char *mtu);
int dhcp_do_request_renew(const char * const ifname,
const char *ipaddr, const char *ipaddr,
const char *gateway, const char *gateway,
uint32_t *prefixLength, uint32_t *prefixLength,
@@ -93,8 +84,8 @@ static jint android_net_utils_resetConnections(JNIEnv* env, jobject clazz,
return (jint)result; return (jint)result;
} }
static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstring ifname, static jboolean android_net_utils_getDhcpResults(JNIEnv* env, jobject clazz, jstring ifname,
jobject dhcpResults, bool renew) jobject dhcpResults)
{ {
int result; int result;
char ipaddr[PROPERTY_VALUE_MAX]; char ipaddr[PROPERTY_VALUE_MAX];
@@ -114,15 +105,10 @@ static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstr
const char *nameStr = env->GetStringUTFChars(ifname, NULL); const char *nameStr = env->GetStringUTFChars(ifname, NULL);
if (nameStr == NULL) return (jboolean)false; if (nameStr == NULL) return (jboolean)false;
if (renew) { result = ::dhcp_get_results(nameStr, ipaddr, gateway, &prefixLength,
result = ::dhcp_do_request_renew(nameStr, ipaddr, gateway, &prefixLength,
dns, server, &lease, vendorInfo, domains, mtu); dns, server, &lease, vendorInfo, domains, mtu);
} else {
result = ::dhcp_do_request(nameStr, ipaddr, gateway, &prefixLength,
dns, server, &lease, vendorInfo, domains, mtu);
}
if (result != 0) { if (result != 0) {
ALOGD("dhcp_do_request failed : %s (%s)", nameStr, renew ? "renew" : "new"); ALOGD("dhcp_get_results failed : %s (%s)", nameStr);
} }
env->ReleaseStringUTFChars(ifname, nameStr); env->ReleaseStringUTFChars(ifname, nameStr);
@@ -182,19 +168,28 @@ static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstr
return (jboolean)(result == 0); return (jboolean)(result == 0);
} }
static jboolean android_net_utils_startDhcp(JNIEnv* env, jobject clazz, jstring ifname)
static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
{ {
return android_net_utils_runDhcpCommon(env, clazz, ifname, info, false); const char *nameStr = env->GetStringUTFChars(ifname, NULL);
if (nameStr == NULL) return (jboolean)false;
if (::dhcp_start(nameStr) != 0) {
ALOGD("dhcp_start failed : %s", nameStr);
return (jboolean)false;
}
return (jboolean)true;
} }
static jboolean android_net_utils_runDhcpRenew(JNIEnv* env, jobject clazz, jstring ifname, static jboolean android_net_utils_startDhcpRenew(JNIEnv* env, jobject clazz, jstring ifname)
jobject info)
{ {
return android_net_utils_runDhcpCommon(env, clazz, ifname, info, true); const char *nameStr = env->GetStringUTFChars(ifname, NULL);
if (nameStr == NULL) return (jboolean)false;
if (::dhcp_start_renew(nameStr) != 0) {
ALOGD("dhcp_start_renew failed : %s", nameStr);
return (jboolean)false;
}
return (jboolean)true;
} }
static jboolean android_net_utils_stopDhcp(JNIEnv* env, jobject clazz, jstring ifname) static jboolean android_net_utils_stopDhcp(JNIEnv* env, jobject clazz, jstring ifname)
{ {
int result; int result;
@@ -255,8 +250,9 @@ static jboolean android_net_utils_protectFromVpn(JNIEnv *env, jobject thiz, jint
static JNINativeMethod gNetworkUtilMethods[] = { static JNINativeMethod gNetworkUtilMethods[] = {
/* name, signature, funcPtr */ /* name, signature, funcPtr */
{ "resetConnections", "(Ljava/lang/String;I)I", (void *)android_net_utils_resetConnections }, { "resetConnections", "(Ljava/lang/String;I)I", (void *)android_net_utils_resetConnections },
{ "runDhcp", "(Ljava/lang/String;Landroid/net/DhcpResults;)Z", (void *)android_net_utils_runDhcp }, { "startDhcp", "(Ljava/lang/String;)Z", (void *)android_net_utils_startDhcp },
{ "runDhcpRenew", "(Ljava/lang/String;Landroid/net/DhcpResults;)Z", (void *)android_net_utils_runDhcpRenew }, { "startDhcpRenew", "(Ljava/lang/String;)Z", (void *)android_net_utils_startDhcpRenew },
{ "getDhcpResults", "(Ljava/lang/String;Landroid/net/DhcpResults;)Z", (void *)android_net_utils_getDhcpResults },
{ "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 },