diff options
author | Erik Kline <ek@google.com> | 2015-02-19 19:51:15 +0900 |
---|---|---|
committer | Erik Kline <ek@google.com> | 2015-03-02 12:35:35 +0900 |
commit | 3c3ce17c5a484de0ec02ba6bc890036657ec6eb4 (patch) | |
tree | ebb7db4b8e2325780b671786f0ccb947357e3ff6 /core | |
parent | 500a98a8eef31eba01863eb2d15ba085d597c10d (diff) | |
download | frameworks_base-3c3ce17c5a484de0ec02ba6bc890036657ec6eb4.zip frameworks_base-3c3ce17c5a484de0ec02ba6bc890036657ec6eb4.tar.gz frameworks_base-3c3ce17c5a484de0ec02ba6bc890036657ec6eb4.tar.bz2 |
Refactor NetworkUtils interaction with DHCP.
Separate out starting DHCP (DISCOVER) and RENEW operations from fetching
the results. Add NetworkUtils.getDhcpResults(), to enable quick checks
of any available DhcpResults without extraneous interaction with the
DHCP daemon.
Bug: 19422416
Change-Id: I58808e529dda8429737e749f5caef56d923c0809
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/net/NetworkUtils.java | 44 | ||||
-rw-r--r-- | core/jni/android_net_NetUtils.cpp | 76 |
2 files changed, 77 insertions, 43 deletions
diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java index d2a2997..8003afb 100644 --- a/core/java/android/net/NetworkUtils.java +++ b/core/java/android/net/NetworkUtils.java @@ -56,6 +56,30 @@ public class NetworkUtils { /** * 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 * addresses. This call blocks until it obtains a result (either success * or failure) from the daemon. @@ -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. diff --git a/core/jni/android_net_NetUtils.cpp b/core/jni/android_net_NetUtils.cpp index 8b9f574..e64f1de 100644 --- a/core/jni/android_net_NetUtils.cpp +++ b/core/jni/android_net_NetUtils.cpp @@ -31,27 +31,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); @@ -93,8 +84,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]; @@ -114,15 +105,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); @@ -182,19 +168,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; @@ -255,8 +250,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 }, |