am "Refactor NetworkUtils interaction with DHCP."

merged from goog/mirror-m-wireless-internal-release
25d2dba Refactor NetworkUtils interaction with DHCP.
This commit is contained in:
Vinit Deshpande
2015-03-15 13:40:27 -07:00
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);
/**
* 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
* for the named interface, and then configure the interface with those
@@ -64,17 +88,31 @@ public class NetworkUtils {
* the IP address information.
* @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.
* @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 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.

View File

@@ -32,27 +32,18 @@ int ifc_enable(const char *ifname);
int ifc_disable(const char *ifname);
int ifc_reset_connections(const char *ifname, int reset_mask);
int dhcp_do_request(const char * const ifname,
const char *ipaddr,
const char *gateway,
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 *gateway,
uint32_t *prefixLength,
const char *dns[],
const char *server,
uint32_t *lease,
const char *vendorInfo,
const char *domains,
const char *mtu);
int dhcp_start(const char * const ifname);
int dhcp_start_renew(const char * const ifname);
int dhcp_get_results(const char * const ifname,
const char *ipaddr,
const char *gateway,
uint32_t *prefixLength,
const char *dns[],
const char *server,
uint32_t *lease,
const char *vendorInfo,
const char *domains,
const char *mtu);
int dhcp_stop(const char *ifname);
int dhcp_release_lease(const char *ifname);
@@ -94,8 +85,8 @@ static jint android_net_utils_resetConnections(JNIEnv* env, jobject clazz,
return (jint)result;
}
static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstring ifname,
jobject dhcpResults, bool renew)
static jboolean android_net_utils_getDhcpResults(JNIEnv* env, jobject clazz, jstring ifname,
jobject dhcpResults)
{
int result;
char ipaddr[PROPERTY_VALUE_MAX];
@@ -115,15 +106,10 @@ static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstr
const char *nameStr = env->GetStringUTFChars(ifname, NULL);
if (nameStr == NULL) return (jboolean)false;
if (renew) {
result = ::dhcp_do_request_renew(nameStr, ipaddr, gateway, &prefixLength,
dns, server, &lease, vendorInfo, domains, mtu);
} else {
result = ::dhcp_do_request(nameStr, ipaddr, gateway, &prefixLength,
dns, server, &lease, vendorInfo, domains, mtu);
}
result = ::dhcp_get_results(nameStr, ipaddr, gateway, &prefixLength,
dns, server, &lease, vendorInfo, domains, mtu);
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);
@@ -183,19 +169,28 @@ static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstr
return (jboolean)(result == 0);
}
static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
static jboolean android_net_utils_startDhcp(JNIEnv* env, jobject clazz, jstring ifname)
{
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,
jobject info)
static jboolean android_net_utils_startDhcpRenew(JNIEnv* env, jobject clazz, jstring ifname)
{
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)
{
int result;
@@ -256,8 +251,9 @@ static jboolean android_net_utils_protectFromVpn(JNIEnv *env, jobject thiz, jint
static JNINativeMethod gNetworkUtilMethods[] = {
/* name, signature, funcPtr */
{ "resetConnections", "(Ljava/lang/String;I)I", (void *)android_net_utils_resetConnections },
{ "runDhcp", "(Ljava/lang/String;Landroid/net/DhcpResults;)Z", (void *)android_net_utils_runDhcp },
{ "runDhcpRenew", "(Ljava/lang/String;Landroid/net/DhcpResults;)Z", (void *)android_net_utils_runDhcpRenew },
{ "startDhcp", "(Ljava/lang/String;)Z", (void *)android_net_utils_startDhcp },
{ "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 },
{ "releaseDhcpLease", "(Ljava/lang/String;)Z", (void *)android_net_utils_releaseDhcpLease },
{ "getDhcpError", "()Ljava/lang/String;", (void*) android_net_utils_getDhcpError },