am 439a015b: (-s ours) am 730126bf: (-s ours) am 3259d885: Merge "DO NOT MERGE - backport of l92a02b31 from master" into gingerbread

* commit '439a015baa65c68ff52c7ac92dd522488c16f0d1':
  DO NOT MERGE - backport of l92a02b31 from master
diff --git a/core/java/android/net/DhcpInfo.java b/core/java/android/net/DhcpInfo.java
index 9c81c19..e2660e4 100644
--- a/core/java/android/net/DhcpInfo.java
+++ b/core/java/android/net/DhcpInfo.java
@@ -18,6 +18,7 @@
 
 import android.os.Parcelable;
 import android.os.Parcel;
+import java.net.InetAddress;
 
 /**
  * A simple object for retrieving the results of a DHCP request.
@@ -65,10 +66,7 @@
     }
 
     private static void putAddress(StringBuffer buf, int addr) {
-        buf.append(addr  & 0xff).append('.').
-            append((addr >>>= 8) & 0xff).append('.').
-            append((addr >>>= 8) & 0xff).append('.').
-            append((addr >>>= 8) & 0xff);
+        buf.append(NetworkUtils.intToInetAddress(addr).getHostAddress());
     }
 
     /** Implement the Parcelable interface {@hide} */
diff --git a/core/java/android/net/LinkAddress.java b/core/java/android/net/LinkAddress.java
index 3f03a2a..9c36b12 100644
--- a/core/java/android/net/LinkAddress.java
+++ b/core/java/android/net/LinkAddress.java
@@ -19,6 +19,7 @@
 import android.os.Parcel;
 import android.os.Parcelable;
 
+import java.net.Inet4Address;
 import java.net.InetAddress;
 import java.net.InterfaceAddress;
 import java.net.UnknownHostException;
@@ -38,12 +39,13 @@
      */
     private final int prefixLength;
 
-    public LinkAddress(InetAddress address, InetAddress mask) {
-        this.address = address;
-        this.prefixLength = computeprefixLength(mask);
-    }
-
     public LinkAddress(InetAddress address, int prefixLength) {
+        if (address == null || prefixLength < 0 ||
+                ((address instanceof Inet4Address) && prefixLength > 32) ||
+                (prefixLength > 128)) {
+            throw new IllegalArgumentException("Bad LinkAddress params " + address +
+                    prefixLength);
+        }
         this.address = address;
         this.prefixLength = prefixLength;
     }
@@ -53,18 +55,6 @@
         this.prefixLength = interfaceAddress.getNetworkPrefixLength();
     }
 
-    private static int computeprefixLength(InetAddress mask) {
-        int count = 0;
-        for (byte b : mask.getAddress()) {
-            for (int i = 0; i < 8; ++i) {
-                if ((b & (1 << i)) != 0) {
-                    ++count;
-                }
-            }
-        }
-        return count;
-    }
-
     @Override
     public String toString() {
         return (address == null ? "" : (address.getHostAddress() + "/" + prefixLength));
diff --git a/core/java/android/net/LinkProperties.java b/core/java/android/net/LinkProperties.java
index f1545ea..b6e9751 100644
--- a/core/java/android/net/LinkProperties.java
+++ b/core/java/android/net/LinkProperties.java
@@ -31,7 +31,24 @@
 
 /**
  * Describes the properties of a network link.
- * TODO - consider adding optional fields like Apn and ApnType
+ *
+ * A link represents a connection to a network.
+ * It may have multiple addresses and multiple gateways,
+ * multiple dns servers but only one http proxy.
+ *
+ * Because it's a single network, the dns's
+ * are interchangeable and don't need associating with
+ * particular addresses.  The gateways similarly don't
+ * need associating with particular addresses.
+ *
+ * A dual stack interface works fine in this model:
+ * each address has it's own prefix length to describe
+ * the local network.  The dns servers all return
+ * both v4 addresses and v6 addresses regardless of the
+ * address family of the server itself (rfc4213) and we
+ * don't care which is used.  The gateways will be
+ * selected based on the destination address and the
+ * source address has no relavence.
  * @hide
  */
 public class LinkProperties implements Parcelable {
@@ -39,7 +56,7 @@
     String mIfaceName;
     private Collection<LinkAddress> mLinkAddresses;
     private Collection<InetAddress> mDnses;
-    private InetAddress mGateway;
+    private Collection<InetAddress> mGateways;
     private ProxyProperties mHttpProxy;
 
     public LinkProperties() {
@@ -52,7 +69,7 @@
             mIfaceName = source.getInterfaceName();
             mLinkAddresses = source.getLinkAddresses();
             mDnses = source.getDnses();
-            mGateway = source.getGateway();
+            mGateways = source.getGateways();
             mHttpProxy = new ProxyProperties(source.getHttpProxy());
         }
     }
@@ -89,11 +106,11 @@
         return Collections.unmodifiableCollection(mDnses);
     }
 
-    public void setGateway(InetAddress gateway) {
-        mGateway = gateway;
+    public void addGateway(InetAddress gateway) {
+        mGateways.add(gateway);
     }
-    public InetAddress getGateway() {
-        return mGateway;
+    public Collection<InetAddress> getGateways() {
+        return Collections.unmodifiableCollection(mGateways);
     }
 
     public void setHttpProxy(ProxyProperties proxy) {
@@ -107,7 +124,7 @@
         mIfaceName = null;
         mLinkAddresses = new ArrayList<LinkAddress>();
         mDnses = new ArrayList<InetAddress>();
-        mGateway = null;
+        mGateways = new ArrayList<InetAddress>();
         mHttpProxy = null;
     }
 
@@ -131,10 +148,12 @@
         for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ",";
         dns += "] ";
 
+        String gateways = "Gateways: [";
+        for (InetAddress gw : mGateways) gateways += gw.getHostAddress() + ",";
+        gateways += "] ";
         String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " ");
-        String gateway = (mGateway == null ? "" : "Gateway: " + mGateway.getHostAddress() + " ");
 
-        return ifaceName + linkAddresses + gateway + dns + proxy;
+        return ifaceName + linkAddresses + gateways + dns + proxy;
     }
 
     /**
@@ -152,12 +171,12 @@
         for(InetAddress d : mDnses) {
             dest.writeByteArray(d.getAddress());
         }
-        if (mGateway != null) {
-            dest.writeByte((byte)1);
-            dest.writeByteArray(mGateway.getAddress());
-        } else {
-            dest.writeByte((byte)0);
+
+        dest.writeInt(mGateways.size());
+        for(InetAddress gw : mGateways) {
+            dest.writeByteArray(gw.getAddress());
         }
+
         if (mHttpProxy != null) {
             dest.writeByte((byte)1);
             dest.writeParcelable(mHttpProxy, flags);
@@ -192,10 +211,11 @@
                         netProp.addDns(InetAddress.getByAddress(in.createByteArray()));
                     } catch (UnknownHostException e) { }
                 }
-                if (in.readByte() == 1) {
+                addressCount = in.readInt();
+                for (int i=0; i<addressCount; i++) {
                     try {
-                        netProp.setGateway(InetAddress.getByAddress(in.createByteArray()));
-                    } catch (UnknownHostException e) {}
+                        netProp.addGateway(InetAddress.getByAddress(in.createByteArray()));
+                    } catch (UnknownHostException e) { }
                 }
                 if (in.readByte() == 1) {
                     netProp.setHttpProxy((ProxyProperties)in.readParcelable(null));
diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java
index 8a653dd..b3f3988 100644
--- a/core/java/android/net/NetworkUtils.java
+++ b/core/java/android/net/NetworkUtils.java
@@ -77,7 +77,7 @@
      * 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.
@@ -104,45 +104,20 @@
     public native static String getDhcpError();
 
     /**
-     * When static IP configuration has been specified, configure the network
-     * interface according to the values supplied.
-     * @param interfaceName the name of the interface to configure
-     * @param ipInfo the IP address, default gateway, and DNS server addresses
-     * with which to configure the interface.
-     * @return {@code true} for success, {@code false} for failure
-     */
-    public static boolean configureInterface(String interfaceName, DhcpInfo ipInfo) {
-        return configureNative(interfaceName,
-            ipInfo.ipAddress,
-            ipInfo.netmask,
-            ipInfo.gateway,
-            ipInfo.dns1,
-            ipInfo.dns2);
-    }
-
-    private native static boolean configureNative(
-        String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2);
-
-    /**
      * Convert a IPv4 address from an integer to an InetAddress.
-     * @param hostAddr is an Int corresponding to the IPv4 address in network byte order
-     * @return the IP address as an {@code InetAddress}, returns null if
-     * unable to convert or if the int is an invalid address.
+     * @param hostAddress an int corresponding to the IPv4 address in network byte order
      */
     public static InetAddress intToInetAddress(int hostAddress) {
-        InetAddress inetAddress;
         byte[] addressBytes = { (byte)(0xff & hostAddress),
                                 (byte)(0xff & (hostAddress >> 8)),
                                 (byte)(0xff & (hostAddress >> 16)),
                                 (byte)(0xff & (hostAddress >> 24)) };
 
         try {
-           inetAddress = InetAddress.getByAddress(addressBytes);
-        } catch(UnknownHostException e) {
-           return null;
+           return InetAddress.getByAddress(addressBytes);
+        } catch (UnknownHostException e) {
+           throw new AssertionError();
         }
-
-        return inetAddress;
     }
 
     /**
@@ -175,6 +150,19 @@
     }
 
     /**
+     * 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 {
+        return InetAddress.parseNumericAddress(addrString);
+    }
+
+    /**
      * 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 351f264..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);
@@ -30,15 +31,14 @@
 int ifc_get_default_route(const char *ifname);
 int ifc_remove_default_route(const char *ifname);
 int ifc_reset_connections(const char *ifname);
-int ifc_configure(const char *ifname, in_addr_t ipaddr, in_addr_t netmask, in_addr_t gateway, in_addr_t dns1, in_addr_t dns2);
 
 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);
@@ -55,16 +55,16 @@
  * 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)
 {
@@ -149,21 +149,29 @@
 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);
 }
@@ -193,24 +201,6 @@
     return env->NewStringUTF(::dhcp_get_errmsg());
 }
 
-static jboolean android_net_utils_configureInterface(JNIEnv* env,
-        jobject clazz,
-        jstring ifname,
-        jint ipaddr,
-        jint mask,
-        jint gateway,
-        jint dns1,
-        jint dns2)
-{
-    int result;
-    uint32_t lease;
-
-    const char *nameStr = env->GetStringUTFChars(ifname, NULL);
-    result = ::ifc_configure(nameStr, ipaddr, mask, gateway, dns1, dns2);
-    env->ReleaseStringUTFChars(ifname, nameStr);
-    return (jboolean)(result == 0);
-}
-
 // ----------------------------------------------------------------------------
 
 /*
@@ -228,10 +218,9 @@
        (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 },
-    { "configureNative", "(Ljava/lang/String;IIIII)Z",  (void *)android_net_utils_configureInterface },
     { "getDhcpError", "()Ljava/lang/String;", (void*) android_net_utils_getDhcpError },
 };
 
@@ -240,16 +229,16 @@
     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,
diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java
index e689654..8e39a63 100644
--- a/services/java/com/android/server/ConnectivityService.java
+++ b/services/java/com/android/server/ConnectivityService.java
@@ -1414,13 +1414,13 @@
         LinkProperties p = nt.getLinkProperties();
         if (p == null) return;
         String interfaceName = p.getInterfaceName();
-        InetAddress defaultGatewayAddr = p.getGateway();
+        if (TextUtils.isEmpty(interfaceName)) return;
+        for (InetAddress gateway : p.getGateways()) {
 
-        if ((interfaceName != null) && (defaultGatewayAddr != null )) {
-            if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
+            if (!NetworkUtils.addDefaultRoute(interfaceName, gateway) && DBG) {
                 NetworkInfo networkInfo = nt.getNetworkInfo();
                 log("addDefaultRoute for " + networkInfo.getTypeName() +
-                        " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
+                        " (" + interfaceName + "), GatewayAddr=" + gateway.getHostAddress());
             }
         }
     }
@@ -2142,7 +2142,8 @@
         if (proxy == null) proxy = new ProxyProperties("", 0, "");
         log("sending Proxy Broadcast for " + proxy);
         Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
-        intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
+        intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
+            Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
         intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
         mContext.sendStickyBroadcast(intent);
     }