diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/bluetooth/BluetoothTetheringDataTracker.java | 19 | ||||
-rw-r--r-- | core/java/android/net/DhcpInfoInternal.java | 87 | ||||
-rw-r--r-- | core/java/android/net/NetworkUtils.java | 25 | ||||
-rw-r--r-- | core/jni/android_net_NetUtils.cpp | 71 |
4 files changed, 155 insertions, 47 deletions
diff --git a/core/java/android/bluetooth/BluetoothTetheringDataTracker.java b/core/java/android/bluetooth/BluetoothTetheringDataTracker.java index 7b083f1..aa1adcb 100644 --- a/core/java/android/bluetooth/BluetoothTetheringDataTracker.java +++ b/core/java/android/bluetooth/BluetoothTetheringDataTracker.java @@ -18,7 +18,7 @@ package android.bluetooth; import android.content.Context; import android.net.ConnectivityManager; -import android.net.DhcpInfo; +import android.net.DhcpInfoInternal; import android.net.LinkAddress; import android.net.LinkCapabilities; import android.net.LinkProperties; @@ -251,23 +251,12 @@ public class BluetoothTetheringDataTracker implements NetworkStateTracker { public void run() { //TODO(): Add callbacks for failure and success case. //Currently this thread runs independently. - DhcpInfo dhcpInfo = new DhcpInfo(); - if (!NetworkUtils.runDhcp(mIface, dhcpInfo)) { + DhcpInfoInternal dhcpInfoInternal = new DhcpInfoInternal(); + if (!NetworkUtils.runDhcp(mIface, dhcpInfoInternal)) { Log.e(TAG, "DHCP request error:" + NetworkUtils.getDhcpError()); return; } - mLinkProperties.addLinkAddress(new LinkAddress( - NetworkUtils.intToInetAddress(dhcpInfo.ipAddress), - NetworkUtils.intToInetAddress(dhcpInfo.netmask))); - mLinkProperties.setGateway(NetworkUtils.intToInetAddress(dhcpInfo.gateway)); - InetAddress dns1Addr = NetworkUtils.intToInetAddress(dhcpInfo.dns1); - if (dns1Addr == null || dns1Addr.equals("0.0.0.0")) { - mLinkProperties.addDns(dns1Addr); - } - InetAddress dns2Addr = NetworkUtils.intToInetAddress(dhcpInfo.dns2); - if (dns2Addr == null || dns2Addr.equals("0.0.0.0")) { - mLinkProperties.addDns(dns2Addr); - } + mLinkProperties = dhcpInfoInternal.makeLinkProperties(); mLinkProperties.setInterfaceName(mIface); mNetworkInfo.setIsAvailable(true); diff --git a/core/java/android/net/DhcpInfoInternal.java b/core/java/android/net/DhcpInfoInternal.java new file mode 100644 index 0000000..7d9bd52 --- /dev/null +++ b/core/java/android/net/DhcpInfoInternal.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net; + +import java.net.InetAddress; +import java.net.Inet4Address; +import java.net.UnknownHostException; + +/** + * A simple object for retrieving the results of a DHCP request. + * Replaces (internally) the IPv4-only DhcpInfo class. + * @hide + */ +public class DhcpInfoInternal { + public String ipAddress; + public String gateway; + public int prefixLength; + + public String dns1; + public String dns2; + + public String serverAddress; + public int leaseDuration; + + public DhcpInfoInternal() { + } + + private int convertToInt(String addr) { + try { + InetAddress inetAddress = NetworkUtils.numericToInetAddress(addr); + if (inetAddress instanceof Inet4Address) { + return NetworkUtils.inetAddressToInt(inetAddress); + } + } catch (IllegalArgumentException e) {} + return 0; + } + + public DhcpInfo makeDhcpInfo() { + DhcpInfo info = new DhcpInfo(); + info.ipAddress = convertToInt(ipAddress); + info.gateway = convertToInt(gateway); + try { + InetAddress inetAddress = NetworkUtils.numericToInetAddress(ipAddress); + info.netmask = NetworkUtils.prefixLengthToNetmaskInt(prefixLength); + } catch (IllegalArgumentException e) {} + info.dns1 = convertToInt(dns1); + info.dns2 = convertToInt(dns2); + info.serverAddress = convertToInt(serverAddress); + info.leaseDuration = leaseDuration; + return info; + } + + public LinkAddress makeLinkAddress() { + return new LinkAddress(NetworkUtils.numericToInetAddress(ipAddress), prefixLength); + } + + public LinkProperties makeLinkProperties() { + LinkProperties p = new LinkProperties(); + p.addLinkAddress(makeLinkAddress()); + p.setGateway(NetworkUtils.numericToInetAddress(gateway)); + p.addDns(NetworkUtils.numericToInetAddress(dns1)); + p.addDns(NetworkUtils.numericToInetAddress(dns2)); + return p; + } + + public String toString() { + return "addr: " + ipAddress + "/" + prefixLength + + " gateway: " + gateway + + " dns: " + dns1 + "," + dns2 + + " dhcpServer: " + serverAddress + + " leaseDuration: " + leaseDuration; + } +} diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java index b0c974c..1c48e7d 100644 --- a/core/java/android/net/NetworkUtils.java +++ b/core/java/android/net/NetworkUtils.java @@ -77,7 +77,7 @@ public class NetworkUtils { * the IP address information. * @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. @@ -150,6 +150,29 @@ public class NetworkUtils { } /** + * 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. * @param interfaceName interface on which the route should be added * @param gw the IP address of the gateway to which the route is desired, diff --git a/core/jni/android_net_NetUtils.cpp b/core/jni/android_net_NetUtils.cpp index f658479..3adf770 100644 --- a/core/jni/android_net_NetUtils.cpp +++ b/core/jni/android_net_NetUtils.cpp @@ -21,6 +21,7 @@ #include <android_runtime/AndroidRuntime.h> #include <utils/Log.h> #include <arpa/inet.h> +#include <cutils/properties.h> extern "C" { 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 dhcp_do_request(const char *ifname, - in_addr_t *ipaddr, - in_addr_t *gateway, - in_addr_t *mask, - in_addr_t *dns1, - in_addr_t *dns2, - in_addr_t *server, + const char *ipaddr, + const char *gateway, + uint32_t *prefixLength, + const char *dns1, + const char *dns2, + const char *server, uint32_t *lease); int dhcp_stop(const char *ifname); int dhcp_release_lease(const char *ifname); @@ -54,16 +55,16 @@ namespace android { * to look them up every time. */ static struct fieldIds { - jclass dhcpInfoClass; + jclass dhcpInfoInternalClass; jmethodID constructorId; jfieldID ipaddress; jfieldID gateway; - jfieldID netmask; + jfieldID prefixLength; jfieldID dns1; jfieldID dns2; jfieldID serverAddress; jfieldID leaseDuration; -} dhcpInfoFieldIds; +} dhcpInfoInternalFieldIds; 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) { 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; const char *nameStr = env->GetStringUTFChars(ifname, NULL); - result = ::dhcp_do_request(nameStr, &ipaddr, &gateway, &mask, - &dns1, &dns2, &server, &lease); + if (nameStr == NULL) return (jboolean)false; + + result = ::dhcp_do_request(nameStr, ipaddr, gateway, &prefixLength, + dns1, dns2, server, &lease); env->ReleaseStringUTFChars(ifname, nameStr); - if (result == 0 && dhcpInfoFieldIds.dhcpInfoClass != NULL) { - env->SetIntField(info, dhcpInfoFieldIds.ipaddress, ipaddr); - env->SetIntField(info, dhcpInfoFieldIds.gateway, gateway); - env->SetIntField(info, dhcpInfoFieldIds.netmask, mask); - env->SetIntField(info, dhcpInfoFieldIds.dns1, dns1); - env->SetIntField(info, dhcpInfoFieldIds.dns2, dns2); - env->SetIntField(info, dhcpInfoFieldIds.serverAddress, server); - env->SetIntField(info, dhcpInfoFieldIds.leaseDuration, lease); + if (result == 0 && dhcpInfoInternalFieldIds.dhcpInfoInternalClass != NULL) { + env->SetObjectField(info, dhcpInfoInternalFieldIds.ipaddress, env->NewStringUTF(ipaddr)); + env->SetObjectField(info, dhcpInfoInternalFieldIds.gateway, env->NewStringUTF(gateway)); + env->SetIntField(info, dhcpInfoInternalFieldIds.prefixLength, prefixLength); + env->SetObjectField(info, dhcpInfoInternalFieldIds.dns1, env->NewStringUTF(dns1)); + env->SetObjectField(info, dhcpInfoInternalFieldIds.dns2, env->NewStringUTF(dns2)); + env->SetObjectField(info, dhcpInfoInternalFieldIds.serverAddress, + env->NewStringUTF(server)); + env->SetIntField(info, dhcpInfoInternalFieldIds.leaseDuration, lease); } return (jboolean)(result == 0); } @@ -209,7 +218,7 @@ static JNINativeMethod gNetworkUtilMethods[] = { (void *)android_net_utils_getDefaultRoute }, { "removeDefaultRoute", "(Ljava/lang/String;)I", (void *)android_net_utils_removeDefaultRoute }, { "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 }, { "releaseDhcpLease", "(Ljava/lang/String;)Z", (void *)android_net_utils_releaseDhcpLease }, { "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); LOG_FATAL_IF(netutils == NULL, "Unable to find class " NETUTILS_PKG_NAME); - dhcpInfoFieldIds.dhcpInfoClass = env->FindClass("android/net/DhcpInfo"); - if (dhcpInfoFieldIds.dhcpInfoClass != NULL) { - dhcpInfoFieldIds.constructorId = env->GetMethodID(dhcpInfoFieldIds.dhcpInfoClass, "<init>", "()V"); - dhcpInfoFieldIds.ipaddress = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "ipAddress", "I"); - dhcpInfoFieldIds.gateway = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "gateway", "I"); - dhcpInfoFieldIds.netmask = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "netmask", "I"); - dhcpInfoFieldIds.dns1 = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "dns1", "I"); - dhcpInfoFieldIds.dns2 = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "dns2", "I"); - dhcpInfoFieldIds.serverAddress = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "serverAddress", "I"); - dhcpInfoFieldIds.leaseDuration = env->GetFieldID(dhcpInfoFieldIds.dhcpInfoClass, "leaseDuration", "I"); + dhcpInfoInternalFieldIds.dhcpInfoInternalClass = env->FindClass("android/net/DhcpInfoInternal"); + if (dhcpInfoInternalFieldIds.dhcpInfoInternalClass != NULL) { + dhcpInfoInternalFieldIds.constructorId = env->GetMethodID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "<init>", "()V"); + dhcpInfoInternalFieldIds.ipaddress = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "ipAddress", "Ljava/lang/String;"); + dhcpInfoInternalFieldIds.gateway = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "gateway", "Ljava/lang/String;"); + dhcpInfoInternalFieldIds.prefixLength = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "prefixLength", "I"); + dhcpInfoInternalFieldIds.dns1 = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "dns1", "Ljava/lang/String;"); + dhcpInfoInternalFieldIds.dns2 = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "dns2", "Ljava/lang/String;"); + dhcpInfoInternalFieldIds.serverAddress = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "serverAddress", "Ljava/lang/String;"); + dhcpInfoInternalFieldIds.leaseDuration = env->GetFieldID(dhcpInfoInternalFieldIds.dhcpInfoInternalClass, "leaseDuration", "I"); } return AndroidRuntime::registerNativeMethods(env, |