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.
* @deprecated - use LinkProperties - To be removed 11/2013
* STOPSHIP - make sure we expose LinkProperties through ConnectivityManager
*/
public class DhcpInfo implements Parcelable {
public int ipAddress;
public int gateway;
public int netmask;
public int dns1;
public int dns2;
public int serverAddress;
public int leaseDuration;
public DhcpInfo() {

View File

@@ -51,7 +51,7 @@ import java.util.Collections;
*/
public class LinkProperties implements Parcelable {
String mIfaceName;
private String mIfaceName;
private Collection<LinkAddress> mLinkAddresses = new ArrayList<LinkAddress>();
private Collection<InetAddress> mDnses = new ArrayList<InetAddress>();
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.
* @return {@code true} if both are identical, {@code false} otherwise.
@@ -365,7 +365,6 @@ public class LinkProperties implements Parcelable {
/**
* Implement the Parcelable interface.
* @hide
*/
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getInterfaceName());
@@ -394,19 +393,15 @@ public class LinkProperties implements Parcelable {
/**
* Implement the Parcelable interface.
* @hide
*/
public static final Creator<LinkProperties> CREATOR =
new Creator<LinkProperties>() {
public LinkProperties createFromParcel(Parcel in) {
LinkProperties netProp = new LinkProperties();
String iface = in.readString();
if (iface != null) {
try {
netProp.setInterfaceName(iface);
} catch (Exception e) {
return null;
}
}
int addressCount = in.readInt();
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
* or failure) from the daemon.
* @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.
* @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
* a result (either success or failure) from the daemon.
* @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.
* @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.
@@ -124,12 +124,9 @@ public class NetworkUtils {
* @param inetAddr is an InetAddress corresponding to the IPv4 address
* @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 {
byte [] addr = inetAddr.getAddress();
if (addr.length != 4) {
throw new IllegalArgumentException("Not an IPv4 address");
}
return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |
((addr[1] & 0xff) << 8) | (addr[0] & 0xff);
}

View File

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

View File

@@ -63,15 +63,15 @@ namespace android {
* to look them up every time.
*/
static struct fieldIds {
jmethodID constructorId;
jfieldID ipaddress;
jfieldID prefixLength;
jfieldID dns1;
jfieldID dns2;
jfieldID serverAddress;
jfieldID leaseDuration;
jfieldID vendorInfo;
} dhcpInfoInternalFieldIds;
jmethodID clear;
jmethodID setInterfaceName;
jmethodID addLinkAddress;
jmethodID addGateway;
jmethodID addDns;
jmethodID setServerAddress;
jmethodID setLeaseDuration;
jmethodID setVendorInfo;
} dhcpResultsFieldIds;
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,
jobject info, bool renew)
jobject dhcpResults, bool renew)
{
int result;
char ipaddr[PROPERTY_VALUE_MAX];
@@ -134,42 +134,55 @@ static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstr
env->ReleaseStringUTFChars(ifname, nameStr);
if (result == 0) {
env->SetObjectField(info, dhcpInfoInternalFieldIds.ipaddress, env->NewStringUTF(ipaddr));
env->CallVoidMethod(dhcpResults, dhcpResultsFieldIds.clear);
// set the gateway
jclass cls = env->FindClass("java/net/InetAddress");
jmethodID method = env->GetStaticMethodID(cls, "getByName",
"(Ljava/lang/String;)Ljava/net/InetAddress;");
jvalue args[1];
args[0].l = env->NewStringUTF(gateway);
jobject inetAddressObject = env->CallStaticObjectMethodA(cls, method, args);
// set mIfaceName
// dhcpResults->setInterfaceName(ifname)
env->CallVoidMethod(dhcpResults, dhcpResultsFieldIds.setInterfaceName, ifname);
if (!env->ExceptionOccurred()) {
cls = env->FindClass("android/net/RouteInfo");
method = env->GetMethodID(cls, "<init>", "(Ljava/net/InetAddress;)V");
args[0].l = inetAddressObject;
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();
// set the linkAddress
// dhcpResults->addLinkAddress(inetAddress, prefixLength)
result = env->CallBooleanMethod(dhcpResults, dhcpResultsFieldIds.addLinkAddress,
env->NewStringUTF(ipaddr), prefixLength);
}
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,
if (result == 0) {
// set the gateway
// dhcpResults->addGateway(gateway)
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->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);
}
static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
{
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 },
{ "disableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_disableInterface },
{ "resetConnections", "(Ljava/lang/String;I)I", (void *)android_net_utils_resetConnections },
{ "runDhcp", "(Ljava/lang/String;Landroid/net/DhcpInfoInternal;)Z", (void *)android_net_utils_runDhcp },
{ "runDhcpRenew", "(Ljava/lang/String;Landroid/net/DhcpInfoInternal;)Z", (void *)android_net_utils_runDhcpRenew },
{ "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 },
{ "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 },
@@ -226,16 +239,24 @@ static JNINativeMethod gNetworkUtilMethods[] = {
int register_android_net_NetworkUtils(JNIEnv* env)
{
jclass dhcpInfoInternalClass = env->FindClass("android/net/DhcpInfoInternal");
LOG_FATAL_IF(dhcpInfoInternalClass == NULL, "Unable to find class android/net/DhcpInfoInternal");
dhcpInfoInternalFieldIds.constructorId = env->GetMethodID(dhcpInfoInternalClass, "<init>", "()V");
dhcpInfoInternalFieldIds.ipaddress = env->GetFieldID(dhcpInfoInternalClass, "ipAddress", "Ljava/lang/String;");
dhcpInfoInternalFieldIds.prefixLength = env->GetFieldID(dhcpInfoInternalClass, "prefixLength", "I");
dhcpInfoInternalFieldIds.dns1 = env->GetFieldID(dhcpInfoInternalClass, "dns1", "Ljava/lang/String;");
dhcpInfoInternalFieldIds.dns2 = env->GetFieldID(dhcpInfoInternalClass, "dns2", "Ljava/lang/String;");
dhcpInfoInternalFieldIds.serverAddress = env->GetFieldID(dhcpInfoInternalClass, "serverAddress", "Ljava/lang/String;");
dhcpInfoInternalFieldIds.leaseDuration = env->GetFieldID(dhcpInfoInternalClass, "leaseDuration", "I");
dhcpInfoInternalFieldIds.vendorInfo = env->GetFieldID(dhcpInfoInternalClass, "vendorInfo", "Ljava/lang/String;");
jclass dhcpResultsClass = env->FindClass("android/net/DhcpResults");
LOG_FATAL_IF(dhcpResultsClass == NULL, "Unable to find class android/net/DhcpResults");
dhcpResultsFieldIds.clear =
env->GetMethodID(dhcpResultsClass, "clear", "()V");
dhcpResultsFieldIds.setInterfaceName =
env->GetMethodID(dhcpResultsClass, "setInterfaceName", "(Ljava/lang/String;)V");
dhcpResultsFieldIds.addLinkAddress =
env->GetMethodID(dhcpResultsClass, "addLinkAddress", "(Ljava/lang/String;I)Z");
dhcpResultsFieldIds.addGateway =
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,
NETUTILS_PKG_NAME, gNetworkUtilMethods, NELEM(gNetworkUtilMethods));