Merge "Remove DhcpInfoInternal"

This commit is contained in:
Robert Greenwalt
2012-11-09 09:51:21 -08:00
committed by Android (Google) Code Review
5 changed files with 85 additions and 67 deletions

View File

@@ -22,16 +22,17 @@ import java.net.InetAddress;
/** /**
* A simple object for retrieving the results of a DHCP request. * A simple object for retrieving the results of a DHCP request.
* @deprecated - use LinkProperties - To be removed 11/2013
* STOPSHIP - make sure we expose LinkProperties through ConnectivityManager
*/ */
public class DhcpInfo implements Parcelable { public class DhcpInfo implements Parcelable {
public int ipAddress; public int ipAddress;
public int gateway; public int gateway;
public int netmask; public int netmask;
public int dns1; public int dns1;
public int dns2; public int dns2;
public int serverAddress; public int serverAddress;
public int leaseDuration; public int leaseDuration;
public DhcpInfo() { public DhcpInfo() {

View File

@@ -51,7 +51,7 @@ import java.util.Collections;
*/ */
public class LinkProperties implements Parcelable { public class LinkProperties implements Parcelable {
String mIfaceName; private String mIfaceName;
private Collection<LinkAddress> mLinkAddresses = new ArrayList<LinkAddress>(); private Collection<LinkAddress> mLinkAddresses = new ArrayList<LinkAddress>();
private Collection<InetAddress> mDnses = new ArrayList<InetAddress>(); private Collection<InetAddress> mDnses = new ArrayList<InetAddress>();
private Collection<RouteInfo> mRoutes = new ArrayList<RouteInfo>(); private Collection<RouteInfo> mRoutes = new ArrayList<RouteInfo>();
@@ -181,7 +181,7 @@ public class LinkProperties implements Parcelable {
} }
/** /**
* Compares this {@code LinkProperties} interface name against the target * Compares this {@code LinkProperties} interface addresses against the target
* *
* @param target LinkProperties to compare. * @param target LinkProperties to compare.
* @return {@code true} if both are identical, {@code false} otherwise. * @return {@code true} if both are identical, {@code false} otherwise.
@@ -365,7 +365,6 @@ public class LinkProperties implements Parcelable {
/** /**
* Implement the Parcelable interface. * Implement the Parcelable interface.
* @hide
*/ */
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getInterfaceName()); dest.writeString(getInterfaceName());
@@ -394,19 +393,15 @@ public class LinkProperties implements Parcelable {
/** /**
* Implement the Parcelable interface. * Implement the Parcelable interface.
* @hide
*/ */
public static final Creator<LinkProperties> CREATOR = public static final Creator<LinkProperties> CREATOR =
new Creator<LinkProperties>() { new Creator<LinkProperties>() {
public LinkProperties createFromParcel(Parcel in) { public LinkProperties createFromParcel(Parcel in) {
LinkProperties netProp = new LinkProperties(); LinkProperties netProp = new LinkProperties();
String iface = in.readString(); String iface = in.readString();
if (iface != null) { if (iface != null) {
try {
netProp.setInterfaceName(iface); netProp.setInterfaceName(iface);
} catch (Exception e) {
return null;
}
} }
int addressCount = in.readInt(); int addressCount = in.readInt();
for (int i=0; i<addressCount; i++) { for (int i=0; i<addressCount; i++) {

View File

@@ -62,21 +62,21 @@ public class NetworkUtils {
* addresses. This call blocks until it obtains a result (either success * addresses. This call blocks until it obtains a result (either success
* or failure) from the daemon. * or failure) from the daemon.
* @param interfaceName the name of the interface to configure * @param interfaceName the name of the interface to configure
* @param ipInfo 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 runDhcp(String interfaceName, DhcpInfoInternal ipInfo); public native static boolean runDhcp(String interfaceName, DhcpResults 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 ipInfo 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, DhcpInfoInternal ipInfo); public native static boolean runDhcpRenew(String interfaceName, DhcpResults dhcpResults);
/** /**
* Shut down the DHCP client daemon. * Shut down the DHCP client daemon.
@@ -124,12 +124,9 @@ public class NetworkUtils {
* @param inetAddr is an InetAddress corresponding to the IPv4 address * @param inetAddr is an InetAddress corresponding to the IPv4 address
* @return the IP address as an integer in network byte order * @return the IP address as an integer in network byte order
*/ */
public static int inetAddressToInt(InetAddress inetAddr) public static int inetAddressToInt(Inet4Address inetAddr)
throws IllegalArgumentException { throws IllegalArgumentException {
byte [] addr = inetAddr.getAddress(); byte [] addr = inetAddr.getAddress();
if (addr.length != 4) {
throw new IllegalArgumentException("Not an IPv4 address");
}
return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) | return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |
((addr[1] & 0xff) << 8) | (addr[0] & 0xff); ((addr[1] & 0xff) << 8) | (addr[0] & 0xff);
} }

View File

@@ -76,6 +76,10 @@ public class RouteInfo implements Parcelable {
this(null, gateway); this(null, gateway);
} }
public RouteInfo(LinkAddress host) {
this(host, null);
}
public static RouteInfo makeHostRoute(InetAddress host) { public static RouteInfo makeHostRoute(InetAddress host) {
return makeHostRoute(host, null); return makeHostRoute(host, null);
} }

View File

@@ -63,15 +63,15 @@ namespace android {
* to look them up every time. * to look them up every time.
*/ */
static struct fieldIds { static struct fieldIds {
jmethodID constructorId; jmethodID clear;
jfieldID ipaddress; jmethodID setInterfaceName;
jfieldID prefixLength; jmethodID addLinkAddress;
jfieldID dns1; jmethodID addGateway;
jfieldID dns2; jmethodID addDns;
jfieldID serverAddress; jmethodID setServerAddress;
jfieldID leaseDuration; jmethodID setLeaseDuration;
jfieldID vendorInfo; jmethodID setVendorInfo;
} dhcpInfoInternalFieldIds; } dhcpResultsFieldIds;
static jint android_net_utils_enableInterface(JNIEnv* env, jobject clazz, jstring ifname) static jint android_net_utils_enableInterface(JNIEnv* env, jobject clazz, jstring ifname)
{ {
@@ -109,7 +109,7 @@ static jint android_net_utils_resetConnections(JNIEnv* env, jobject clazz,
} }
static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstring ifname, static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstring ifname,
jobject info, bool renew) jobject dhcpResults, bool renew)
{ {
int result; int result;
char ipaddr[PROPERTY_VALUE_MAX]; char ipaddr[PROPERTY_VALUE_MAX];
@@ -134,42 +134,55 @@ static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstr
env->ReleaseStringUTFChars(ifname, nameStr); env->ReleaseStringUTFChars(ifname, nameStr);
if (result == 0) { if (result == 0) {
env->SetObjectField(info, dhcpInfoInternalFieldIds.ipaddress, env->NewStringUTF(ipaddr)); env->CallVoidMethod(dhcpResults, dhcpResultsFieldIds.clear);
// set the gateway // set mIfaceName
jclass cls = env->FindClass("java/net/InetAddress"); // dhcpResults->setInterfaceName(ifname)
jmethodID method = env->GetStaticMethodID(cls, "getByName", env->CallVoidMethod(dhcpResults, dhcpResultsFieldIds.setInterfaceName, ifname);
"(Ljava/lang/String;)Ljava/net/InetAddress;");
jvalue args[1];
args[0].l = env->NewStringUTF(gateway);
jobject inetAddressObject = env->CallStaticObjectMethodA(cls, method, args);
if (!env->ExceptionOccurred()) { // set the linkAddress
cls = env->FindClass("android/net/RouteInfo"); // dhcpResults->addLinkAddress(inetAddress, prefixLength)
method = env->GetMethodID(cls, "<init>", "(Ljava/net/InetAddress;)V"); result = env->CallBooleanMethod(dhcpResults, dhcpResultsFieldIds.addLinkAddress,
args[0].l = inetAddressObject; env->NewStringUTF(ipaddr), prefixLength);
jobject routeInfoObject = env->NewObjectA(cls, method, args);
cls = env->FindClass("android/net/DhcpInfoInternal");
method = env->GetMethodID(cls, "addRoute", "(Landroid/net/RouteInfo;)V");
args[0].l = routeInfoObject;
env->CallVoidMethodA(info, method, args);
} else {
// if we have an exception (host not found perhaps), just don't add the route
env->ExceptionClear();
} }
env->SetIntField(info, dhcpInfoInternalFieldIds.prefixLength, prefixLength); if (result == 0) {
env->SetObjectField(info, dhcpInfoInternalFieldIds.dns1, env->NewStringUTF(dns1)); // set the gateway
env->SetObjectField(info, dhcpInfoInternalFieldIds.dns2, env->NewStringUTF(dns2)); // dhcpResults->addGateway(gateway)
env->SetObjectField(info, dhcpInfoInternalFieldIds.serverAddress, result = env->CallBooleanMethod(dhcpResults,
dhcpResultsFieldIds.addGateway, env->NewStringUTF(gateway));
}
if (result == 0) {
// dhcpResults->addDns(new InetAddress(dns1))
result = env->CallBooleanMethod(dhcpResults,
dhcpResultsFieldIds.addDns, env->NewStringUTF(dns1));
}
if (result == 0) {
result = env->CallBooleanMethod(dhcpResults,
dhcpResultsFieldIds.addDns, env->NewStringUTF(dns2));
}
if (result == 0) {
// dhcpResults->setServerAddress(new InetAddress(server))
result = env->CallBooleanMethod(dhcpResults, dhcpResultsFieldIds.setServerAddress,
env->NewStringUTF(server)); env->NewStringUTF(server));
env->SetIntField(info, dhcpInfoInternalFieldIds.leaseDuration, lease); }
env->SetObjectField(info, dhcpInfoInternalFieldIds.vendorInfo, env->NewStringUTF(vendorInfo));
if (result == 0) {
// dhcpResults->setLeaseDuration(lease)
env->CallVoidMethod(dhcpResults,
dhcpResultsFieldIds.setLeaseDuration, lease);
// dhcpResults->setVendorInfo(vendorInfo)
env->CallVoidMethod(dhcpResults, dhcpResultsFieldIds.setVendorInfo,
env->NewStringUTF(vendorInfo));
} }
return (jboolean)(result == 0); return (jboolean)(result == 0);
} }
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)
{ {
return android_net_utils_runDhcpCommon(env, clazz, ifname, info, false); return android_net_utils_runDhcpCommon(env, clazz, ifname, info, false);
@@ -217,8 +230,8 @@ static JNINativeMethod gNetworkUtilMethods[] = {
{ "enableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_enableInterface }, { "enableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_enableInterface },
{ "disableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_disableInterface }, { "disableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_disableInterface },
{ "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/DhcpInfoInternal;)Z", (void *)android_net_utils_runDhcp }, { "runDhcp", "(Ljava/lang/String;Landroid/net/DhcpResults;)Z", (void *)android_net_utils_runDhcp },
{ "runDhcpRenew", "(Ljava/lang/String;Landroid/net/DhcpInfoInternal;)Z", (void *)android_net_utils_runDhcpRenew }, { "runDhcpRenew", "(Ljava/lang/String;Landroid/net/DhcpResults;)Z", (void *)android_net_utils_runDhcpRenew },
{ "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 },
@@ -226,16 +239,24 @@ static JNINativeMethod gNetworkUtilMethods[] = {
int register_android_net_NetworkUtils(JNIEnv* env) int register_android_net_NetworkUtils(JNIEnv* env)
{ {
jclass dhcpInfoInternalClass = env->FindClass("android/net/DhcpInfoInternal"); jclass dhcpResultsClass = env->FindClass("android/net/DhcpResults");
LOG_FATAL_IF(dhcpInfoInternalClass == NULL, "Unable to find class android/net/DhcpInfoInternal"); LOG_FATAL_IF(dhcpResultsClass == NULL, "Unable to find class android/net/DhcpResults");
dhcpInfoInternalFieldIds.constructorId = env->GetMethodID(dhcpInfoInternalClass, "<init>", "()V"); dhcpResultsFieldIds.clear =
dhcpInfoInternalFieldIds.ipaddress = env->GetFieldID(dhcpInfoInternalClass, "ipAddress", "Ljava/lang/String;"); env->GetMethodID(dhcpResultsClass, "clear", "()V");
dhcpInfoInternalFieldIds.prefixLength = env->GetFieldID(dhcpInfoInternalClass, "prefixLength", "I"); dhcpResultsFieldIds.setInterfaceName =
dhcpInfoInternalFieldIds.dns1 = env->GetFieldID(dhcpInfoInternalClass, "dns1", "Ljava/lang/String;"); env->GetMethodID(dhcpResultsClass, "setInterfaceName", "(Ljava/lang/String;)V");
dhcpInfoInternalFieldIds.dns2 = env->GetFieldID(dhcpInfoInternalClass, "dns2", "Ljava/lang/String;"); dhcpResultsFieldIds.addLinkAddress =
dhcpInfoInternalFieldIds.serverAddress = env->GetFieldID(dhcpInfoInternalClass, "serverAddress", "Ljava/lang/String;"); env->GetMethodID(dhcpResultsClass, "addLinkAddress", "(Ljava/lang/String;I)Z");
dhcpInfoInternalFieldIds.leaseDuration = env->GetFieldID(dhcpInfoInternalClass, "leaseDuration", "I"); dhcpResultsFieldIds.addGateway =
dhcpInfoInternalFieldIds.vendorInfo = env->GetFieldID(dhcpInfoInternalClass, "vendorInfo", "Ljava/lang/String;"); env->GetMethodID(dhcpResultsClass, "addGateway", "(Ljava/lang/String;)Z");
dhcpResultsFieldIds.addDns =
env->GetMethodID(dhcpResultsClass, "addDns", "(Ljava/lang/String;)Z");
dhcpResultsFieldIds.setServerAddress =
env->GetMethodID(dhcpResultsClass, "setServerAddress", "(Ljava/lang/String;)Z");
dhcpResultsFieldIds.setLeaseDuration =
env->GetMethodID(dhcpResultsClass, "setLeaseDuration", "(I)V");
dhcpResultsFieldIds.setVendorInfo =
env->GetMethodID(dhcpResultsClass, "setVendorInfo", "(Ljava/lang/String;)V");
return AndroidRuntime::registerNativeMethods(env, return AndroidRuntime::registerNativeMethods(env,
NETUTILS_PKG_NAME, gNetworkUtilMethods, NELEM(gNetworkUtilMethods)); NETUTILS_PKG_NAME, gNetworkUtilMethods, NELEM(gNetworkUtilMethods));