Reconcile with honeycomb-release
Change-Id: I83818e0c61f0fcb53b154cd0a85f18924745912f
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index cab8ed2..419288b 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -215,18 +215,43 @@
/**
* Bluetooth data connection. This is used for Bluetooth reverse tethering.
- * @hide
*/
public static final int TYPE_BLUETOOTH = 7;
- /** {@hide} */
+ /**
+ * Dummy data connection. This should not be used on shipping devices.
+ */
public static final int TYPE_DUMMY = 8;
- /** {@hide} */
+
+ /**
+ * Ethernet data connection. This may be via USB dongle or more
+ * traditional means.
+ */
public static final int TYPE_ETHERNET = 9;
- /** {@hide} TODO: Need to adjust this for WiMAX. */
- public static final int MAX_RADIO_TYPE = TYPE_DUMMY;
- /** {@hide} TODO: Need to adjust this for WiMAX. */
- public static final int MAX_NETWORK_TYPE = TYPE_DUMMY;
+
+ /**
+ * Over the air Adminstration.
+ * {@hide}
+ */
+ public static final int TYPE_MOBILE_FOTA = 10;
+
+ /**
+ * IP Multimedia Subsystem
+ * {@hide}
+ */
+ public static final int TYPE_MOBILE_IMS = 11;
+
+ /**
+ * Carrier Branded Services
+ * {@hide}
+ */
+ public static final int TYPE_MOBILE_CBS = 12;
+
+ /** {@hide} */
+ public static final int MAX_RADIO_TYPE = TYPE_MOBILE_CBS;
+
+ /** {@hide} */
+ public static final int MAX_NETWORK_TYPE = TYPE_MOBILE_CBS;
public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
@@ -670,4 +695,16 @@
return null;
}
}
+
+ /**
+ * @param networkType The network who's dependence has changed
+ * @param met Boolean - true if network use is ok, false if not
+ * {@hide}
+ */
+ public void setDataDependency(int networkType, boolean met) {
+ try {
+ mService.setDataDependency(networkType, met);
+ } catch (RemoteException e) {
+ }
+ }
}
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/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl
index 70ab4f1..8be492c 100644
--- a/core/java/android/net/IConnectivityManager.aidl
+++ b/core/java/android/net/IConnectivityManager.aidl
@@ -92,4 +92,6 @@
void setGlobalProxy(in ProxyProperties p);
ProxyProperties getProxy();
+
+ void setDataDependency(int networkType, boolean met);
}
diff --git a/core/java/android/net/LinkAddress.java b/core/java/android/net/LinkAddress.java
index 3f03a2a..f6a114c 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));
@@ -88,6 +78,14 @@
this.prefixLength == linkAddress.prefixLength;
}
+ @Override
+ /*
+ * generate hashcode based on significant fields
+ */
+ public int hashCode() {
+ return ((null == address) ? 0 : address.hashCode()) + prefixLength;
+ }
+
/**
* Returns the InetAddress for this address.
*/
diff --git a/core/java/android/net/LinkProperties.java b/core/java/android/net/LinkProperties.java
index f1545ea..19894a0 100644
--- a/core/java/android/net/LinkProperties.java
+++ b/core/java/android/net/LinkProperties.java
@@ -19,11 +19,9 @@
import android.net.ProxyProperties;
import android.os.Parcelable;
import android.os.Parcel;
-import android.util.Log;
+import android.text.TextUtils;
import java.net.InetAddress;
-import java.net.NetworkInterface;
-import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
@@ -31,7 +29,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 +54,7 @@
String mIfaceName;
private Collection<LinkAddress> mLinkAddresses;
private Collection<InetAddress> mDnses;
- private InetAddress mGateway;
+ private Collection<RouteInfo> mRoutes;
private ProxyProperties mHttpProxy;
public LinkProperties() {
@@ -52,8 +67,9 @@
mIfaceName = source.getInterfaceName();
mLinkAddresses = source.getLinkAddresses();
mDnses = source.getDnses();
- mGateway = source.getGateway();
- mHttpProxy = new ProxyProperties(source.getHttpProxy());
+ mRoutes = source.getRoutes();
+ mHttpProxy = (source.getHttpProxy() == null) ?
+ null : new ProxyProperties(source.getHttpProxy());
}
}
@@ -74,7 +90,7 @@
}
public void addLinkAddress(LinkAddress address) {
- mLinkAddresses.add(address);
+ if (address != null) mLinkAddresses.add(address);
}
public Collection<LinkAddress> getLinkAddresses() {
@@ -82,18 +98,18 @@
}
public void addDns(InetAddress dns) {
- mDnses.add(dns);
+ if (dns != null) mDnses.add(dns);
}
public Collection<InetAddress> getDnses() {
return Collections.unmodifiableCollection(mDnses);
}
- public void setGateway(InetAddress gateway) {
- mGateway = gateway;
+ public void addRoute(RouteInfo route) {
+ if (route != null) mRoutes.add(route);
}
- public InetAddress getGateway() {
- return mGateway;
+ public Collection<RouteInfo> getRoutes() {
+ return Collections.unmodifiableCollection(mRoutes);
}
public void setHttpProxy(ProxyProperties proxy) {
@@ -107,7 +123,7 @@
mIfaceName = null;
mLinkAddresses = new ArrayList<LinkAddress>();
mDnses = new ArrayList<InetAddress>();
- mGateway = null;
+ mRoutes = new ArrayList<RouteInfo>();
mHttpProxy = null;
}
@@ -124,17 +140,80 @@
String ifaceName = (mIfaceName == null ? "" : "InterfaceName: " + mIfaceName + " ");
String linkAddresses = "LinkAddresses: [";
- for (LinkAddress addr : mLinkAddresses) linkAddresses += addr.toString();
+ for (LinkAddress addr : mLinkAddresses) linkAddresses += addr.toString() + ",";
linkAddresses += "] ";
String dns = "DnsAddresses: [";
for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ",";
dns += "] ";
+ String routes = "Routes: [";
+ for (RouteInfo route : mRoutes) routes += route.toString() + ",";
+ routes += "] ";
String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " ");
- String gateway = (mGateway == null ? "" : "Gateway: " + mGateway.getHostAddress() + " ");
- return ifaceName + linkAddresses + gateway + dns + proxy;
+ return ifaceName + linkAddresses + routes + dns + proxy;
+ }
+
+
+ @Override
+ /**
+ * Compares this {@code LinkProperties} instance against the target
+ * LinkProperties in {@code obj}. Two LinkPropertieses are equal if
+ * all their fields are equal in values.
+ *
+ * For collection fields, such as mDnses, containsAll() is used to check
+ * if two collections contains the same elements, independent of order.
+ * There are two thoughts regarding containsAll()
+ * 1. Duplicated elements. eg, (A, B, B) and (A, A, B) are equal.
+ * 2. Worst case performance is O(n^2).
+ *
+ * @param obj the object to be tested for equality.
+ * @return {@code true} if both objects are equal, {@code false} otherwise.
+ */
+ public boolean equals(Object obj) {
+ if (this == obj) return true;
+
+ if (!(obj instanceof LinkProperties)) return false;
+
+ boolean sameAddresses;
+ boolean sameDnses;
+ boolean sameRoutes;
+
+ LinkProperties target = (LinkProperties) obj;
+
+ Collection<InetAddress> targetAddresses = target.getAddresses();
+ Collection<InetAddress> sourceAddresses = getAddresses();
+ sameAddresses = (sourceAddresses.size() == targetAddresses.size()) ?
+ sourceAddresses.containsAll(targetAddresses) : false;
+
+ Collection<InetAddress> targetDnses = target.getDnses();
+ sameDnses = (mDnses.size() == targetDnses.size()) ?
+ mDnses.containsAll(targetDnses) : false;
+
+ Collection<RouteInfo> targetRoutes = target.getRoutes();
+ sameRoutes = (mRoutes.size() == targetRoutes.size()) ?
+ mRoutes.containsAll(targetRoutes) : false;
+
+ return
+ sameAddresses && sameDnses && sameRoutes
+ && TextUtils.equals(getInterfaceName(), target.getInterfaceName())
+ && (getHttpProxy() == null ? target.getHttpProxy() == null :
+ getHttpProxy().equals(target.getHttpProxy()));
+ }
+
+ @Override
+ /**
+ * generate hashcode based on significant fields
+ * Equal objects must produce the same hash code, while unequal objects
+ * may have the same hash codes.
+ */
+ public int hashCode() {
+ return ((null == mIfaceName) ? 0 : mIfaceName.hashCode()
+ + mLinkAddresses.size() * 31
+ + mDnses.size() * 37
+ + mRoutes.size() * 41
+ + ((null == mHttpProxy) ? 0 : mHttpProxy.hashCode()));
}
/**
@@ -152,12 +231,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(mRoutes.size());
+ for(RouteInfo route : mRoutes) {
+ dest.writeParcelable(route, flags);
}
+
if (mHttpProxy != null) {
dest.writeByte((byte)1);
dest.writeParcelable(mHttpProxy, flags);
@@ -192,10 +271,9 @@
netProp.addDns(InetAddress.getByAddress(in.createByteArray()));
} catch (UnknownHostException e) { }
}
- if (in.readByte() == 1) {
- try {
- netProp.setGateway(InetAddress.getByAddress(in.createByteArray()));
- } catch (UnknownHostException e) {}
+ addressCount = in.readInt();
+ for (int i=0; i<addressCount; i++) {
+ netProp.addRoute((RouteInfo)in.readParcelable(null));
}
if (in.readByte() == 1) {
netProp.setHttpProxy((ProxyProperties)in.readParcelable(null));
diff --git a/core/java/android/net/NetworkConfig.java b/core/java/android/net/NetworkConfig.java
new file mode 100644
index 0000000..3cc0bc5
--- /dev/null
+++ b/core/java/android/net/NetworkConfig.java
@@ -0,0 +1,80 @@
+/*
+ * 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 android.util.Log;
+
+/**
+ * Describes the buildtime configuration of a network.
+ * Holds settings read from resources.
+ * @hide
+ */
+public class NetworkConfig {
+ /**
+ * Human readable string
+ */
+ public String name;
+
+ /**
+ * Type from ConnectivityManager
+ */
+ public int type;
+
+ /**
+ * the radio number from radio attributes config
+ */
+ public int radio;
+
+ /**
+ * higher number == higher priority when turning off connections
+ */
+ public int priority;
+
+ /**
+ * indicates the boot time dependencyMet setting
+ */
+ public boolean dependencyMet;
+
+ /**
+ * indicates the default restoral timer in seconds
+ * if the network is used as a special network feature
+ * -1 indicates no restoration of default
+ */
+ public int restoreTime;
+
+ /**
+ * input string from config.xml resource. Uses the form:
+ * [Connection name],[ConnectivityManager connection type],
+ * [associated radio-type],[priority],[dependencyMet]
+ */
+ public NetworkConfig(String init) {
+ String fragments[] = init.split(",");
+ name = fragments[0].trim().toLowerCase();
+ type = Integer.parseInt(fragments[1]);
+ radio = Integer.parseInt(fragments[2]);
+ priority = Integer.parseInt(fragments[3]);
+ restoreTime = Integer.parseInt(fragments[4]);
+ dependencyMet = Boolean.parseBoolean(fragments[5]);
+ }
+
+ /**
+ * Indicates if this network is supposed to be default-routable
+ */
+ public boolean isDefault() {
+ return (type == radio);
+ }
+}
diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java
index 8a653dd..8a678d6 100644
--- a/core/java/android/net/NetworkUtils.java
+++ b/core/java/android/net/NetworkUtils.java
@@ -38,32 +38,6 @@
/** Bring the named network interface down. */
public native static int disableInterface(String interfaceName);
- /**
- * Add a route to the routing table.
- *
- * @param interfaceName the interface to route through.
- * @param dst the network or host to route to. May be IPv4 or IPv6, e.g.
- * "0.0.0.0" or "2001:4860::".
- * @param prefixLength the prefix length of the route.
- * @param gw the gateway to use, e.g., "192.168.251.1". If null,
- * indicates a directly-connected route.
- */
- public native static int addRoute(String interfaceName, String dst,
- int prefixLength, String gw);
-
- /** Return the gateway address for the default route for the named interface. */
- public static InetAddress getDefaultRoute(String interfaceName) {
- int addr = getDefaultRouteNative(interfaceName);
- return intToInetAddress(addr);
- }
- private native static int getDefaultRouteNative(String interfaceName);
-
- /** Remove host routes that uses the named interface. */
- public native static int removeHostRoutes(String interfaceName);
-
- /** Remove the default route for the named interface. */
- public native static int removeDefaultRoute(String interfaceName);
-
/** Reset any sockets that are connected via the named interface. */
public native static int resetConnections(String interfaceName);
@@ -77,7 +51,17 @@
* 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);
+
+ /**
+ * 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
+ * the IP address information.
+ * @return {@code true} for success, {@code false} for failure
+ */
+ public native static boolean runDhcpRenew(String interfaceName, DhcpInfoInternal ipInfo);
/**
* Shut down the DHCP client daemon.
@@ -104,45 +88,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,56 +134,91 @@
}
/**
- * 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,
- * @return {@code true} on success, {@code false} on failure
+ * Convert a IPv4 netmask integer to a prefix length
+ * @param netmask as an integer in network byte order
+ * @return the network prefix length
*/
- public static boolean addDefaultRoute(String interfaceName, InetAddress gw) {
- String dstStr;
- String gwStr = gw.getHostAddress();
-
- if (gw instanceof Inet4Address) {
- dstStr = "0.0.0.0";
- } else if (gw instanceof Inet6Address) {
- dstStr = "::";
- } else {
- Log.w(TAG, "addDefaultRoute failure: address is neither IPv4 nor IPv6" +
- "(" + gwStr + ")");
- return false;
- }
- return addRoute(interfaceName, dstStr, 0, gwStr) == 0;
+ public static int netmaskIntToPrefixLength(int netmask) {
+ return Integer.bitCount(netmask);
}
/**
- * Add a host route.
- * @param interfaceName interface on which the route should be added
- * @param dst the IP address of the host to which the route is desired,
- * this should not be null.
- * @param gw the IP address of the gateway to which the route is desired,
- * if null, indicates a directly-connected route.
- * @return {@code true} on success, {@code false} on failure
+ * 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 boolean addHostRoute(String interfaceName, InetAddress dst,
- InetAddress gw) {
- if (dst == null) {
- Log.w(TAG, "addHostRoute: dst should not be null");
- return false;
+ public static InetAddress numericToInetAddress(String addrString)
+ throws IllegalArgumentException {
+ return InetAddress.parseNumericAddress(addrString);
+ }
+
+ /**
+ * Get InetAddress masked with prefixLength. Will never return null.
+ * @param IP address which will be masked with specified prefixLength
+ * @param prefixLength the prefixLength used to mask the IP
+ */
+ public static InetAddress getNetworkPart(InetAddress address, int prefixLength) {
+ if (address == null) {
+ throw new RuntimeException("getNetworkPart doesn't accept null address");
}
- int prefixLength;
- String dstStr = dst.getHostAddress();
- String gwStr = (gw != null) ? gw.getHostAddress() : null;
+ byte[] array = address.getAddress();
- if (dst instanceof Inet4Address) {
- prefixLength = 32;
- } else if (dst instanceof Inet6Address) {
- prefixLength = 128;
- } else {
- Log.w(TAG, "addHostRoute failure: address is neither IPv4 nor IPv6" +
- "(" + dst + ")");
- return false;
+ if (prefixLength < 0 || prefixLength > array.length * 8) {
+ throw new RuntimeException("getNetworkPart - bad prefixLength");
}
- return addRoute(interfaceName, dstStr, prefixLength, gwStr) == 0;
+
+ int offset = prefixLength / 8;
+ int reminder = prefixLength % 8;
+ byte mask = (byte)(0xFF << (8 - reminder));
+
+ if (offset < array.length) array[offset] = (byte)(array[offset] & mask);
+
+ offset++;
+
+ for (; offset < array.length; offset++) {
+ array[offset] = 0;
+ }
+
+ InetAddress netPart = null;
+ try {
+ netPart = InetAddress.getByAddress(array);
+ } catch (UnknownHostException e) {
+ throw new RuntimeException("getNetworkPart error - " + e.toString());
+ }
+ return netPart;
+ }
+
+ /**
+ * Check if IP address type is consistent between two InetAddress.
+ * @return true if both are the same type. False otherwise.
+ */
+ public static boolean addressTypeMatches(InetAddress left, InetAddress right) {
+ return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) ||
+ ((left instanceof Inet6Address) && (right instanceof Inet6Address)));
+ }
+
+ /**
+ * Convert a 32 char hex string into a Inet6Address.
+ * throws a runtime exception if the string isn't 32 chars, isn't hex or can't be
+ * made into an Inet6Address
+ * @param addrHexString a 32 character hex string representing an IPv6 addr
+ * @return addr an InetAddress representation for the string
+ */
+ public static InetAddress hexToInet6Address(String addrHexString)
+ throws IllegalArgumentException {
+ try {
+ return numericToInetAddress(String.format("%s:%s:%s:%s:%s:%s:%s:%s",
+ addrHexString.substring(0,4), addrHexString.substring(4,8),
+ addrHexString.substring(8,12), addrHexString.substring(12,16),
+ addrHexString.substring(16,20), addrHexString.substring(20,24),
+ addrHexString.substring(24,28), addrHexString.substring(28,32)));
+ } catch (Exception e) {
+ Log.e("NetworkUtils", "error in hexToInet6Address(" + addrHexString + "): " + e);
+ throw new IllegalArgumentException(e);
+ }
}
}
diff --git a/core/java/android/net/ProxyProperties.java b/core/java/android/net/ProxyProperties.java
index cbe4445..44dbec1 100644
--- a/core/java/android/net/ProxyProperties.java
+++ b/core/java/android/net/ProxyProperties.java
@@ -163,6 +163,16 @@
return 0;
}
+ @Override
+ /*
+ * generate hashcode based on significant fields
+ */
+ public int hashCode() {
+ return ((null == mHost) ? 0 : mHost.hashCode())
+ + ((null == mExclusionList) ? 0 : mExclusionList.hashCode())
+ + mPort;
+ }
+
/**
* Implement the Parcelable interface.
* @hide
diff --git a/core/java/android/net/RouteInfo.java b/core/java/android/net/RouteInfo.java
new file mode 100644
index 0000000..8e5ddda
--- /dev/null
+++ b/core/java/android/net/RouteInfo.java
@@ -0,0 +1,246 @@
+/*
+ * Copyright (C) 2011 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 android.os.Parcel;
+import android.os.Parcelable;
+
+import java.net.UnknownHostException;
+import java.net.InetAddress;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+
+import java.util.Collection;
+
+/**
+ * A simple container for route information.
+ *
+ * @hide
+ */
+public class RouteInfo implements Parcelable {
+ /**
+ * The IP destination address for this route.
+ */
+ private final LinkAddress mDestination;
+
+ /**
+ * The gateway address for this route.
+ */
+ private final InetAddress mGateway;
+
+ private final boolean mIsDefault;
+
+ public RouteInfo(LinkAddress destination, InetAddress gateway) {
+ if (destination == null) {
+ if (gateway != null) {
+ if (gateway instanceof Inet4Address) {
+ destination = new LinkAddress(Inet4Address.ANY, 0);
+ } else {
+ destination = new LinkAddress(Inet6Address.ANY, 0);
+ }
+ } else {
+ // no destination, no gateway. invalid.
+ throw new RuntimeException("Invalid arguments passed in.");
+ }
+ }
+ if (gateway == null) {
+ if (destination.getAddress() instanceof Inet4Address) {
+ gateway = Inet4Address.ANY;
+ } else {
+ gateway = Inet6Address.ANY;
+ }
+ }
+ mDestination = new LinkAddress(NetworkUtils.getNetworkPart(destination.getAddress(),
+ destination.getNetworkPrefixLength()), destination.getNetworkPrefixLength());
+ mGateway = gateway;
+ mIsDefault = isDefault();
+ }
+
+ public RouteInfo(InetAddress gateway) {
+ this(null, gateway);
+ }
+
+ public static RouteInfo makeHostRoute(InetAddress host) {
+ return makeHostRoute(host, null);
+ }
+
+ public static RouteInfo makeHostRoute(InetAddress host, InetAddress gateway) {
+ if (host == null) return null;
+
+ if (host instanceof Inet4Address) {
+ return new RouteInfo(new LinkAddress(host, 32), gateway);
+ } else {
+ return new RouteInfo(new LinkAddress(host, 128), gateway);
+ }
+ }
+
+ private boolean isDefault() {
+ boolean val = false;
+ if (mGateway != null) {
+ if (mGateway instanceof Inet4Address) {
+ val = (mDestination == null || mDestination.getNetworkPrefixLength() == 0);
+ } else {
+ val = (mDestination == null || mDestination.getNetworkPrefixLength() == 0);
+ }
+ }
+ return val;
+ }
+
+ public LinkAddress getDestination() {
+ return mDestination;
+ }
+
+ public InetAddress getGateway() {
+ return mGateway;
+ }
+
+ public boolean isDefaultRoute() {
+ return mIsDefault;
+ }
+
+ public String toString() {
+ String val = "";
+ if (mDestination != null) val = mDestination.toString();
+ if (mGateway != null) val += " -> " + mGateway.getHostAddress();
+ return val;
+ }
+
+ public int describeContents() {
+ return 0;
+ }
+
+ public void writeToParcel(Parcel dest, int flags) {
+ if (mDestination == null) {
+ dest.writeByte((byte) 0);
+ } else {
+ dest.writeByte((byte) 1);
+ dest.writeByteArray(mDestination.getAddress().getAddress());
+ dest.writeInt(mDestination.getNetworkPrefixLength());
+ }
+
+ if (mGateway == null) {
+ dest.writeByte((byte) 0);
+ } else {
+ dest.writeByte((byte) 1);
+ dest.writeByteArray(mGateway.getAddress());
+ }
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) return true;
+
+ if (!(obj instanceof RouteInfo)) return false;
+
+ RouteInfo target = (RouteInfo) obj;
+
+ boolean sameDestination = ( mDestination == null) ?
+ target.getDestination() == null
+ : mDestination.equals(target.getDestination());
+
+ boolean sameAddress = (mGateway == null) ?
+ target.getGateway() == null
+ : mGateway.equals(target.getGateway());
+
+ return sameDestination && sameAddress
+ && mIsDefault == target.mIsDefault;
+ }
+
+ @Override
+ public int hashCode() {
+ return (mDestination == null ? 0 : mDestination.hashCode())
+ + (mGateway == null ? 0 :mGateway.hashCode())
+ + (mIsDefault ? 3 : 7);
+ }
+
+ public static final Creator<RouteInfo> CREATOR =
+ new Creator<RouteInfo>() {
+ public RouteInfo createFromParcel(Parcel in) {
+ InetAddress destAddr = null;
+ int prefix = 0;
+ InetAddress gateway = null;
+
+ if (in.readByte() == 1) {
+ byte[] addr = in.createByteArray();
+ prefix = in.readInt();
+
+ try {
+ destAddr = InetAddress.getByAddress(addr);
+ } catch (UnknownHostException e) {}
+ }
+
+ if (in.readByte() == 1) {
+ byte[] addr = in.createByteArray();
+
+ try {
+ gateway = InetAddress.getByAddress(addr);
+ } catch (UnknownHostException e) {}
+ }
+
+ LinkAddress dest = null;
+
+ if (destAddr != null) {
+ dest = new LinkAddress(destAddr, prefix);
+ }
+
+ return new RouteInfo(dest, gateway);
+ }
+
+ public RouteInfo[] newArray(int size) {
+ return new RouteInfo[size];
+ }
+ };
+
+ private boolean matches(InetAddress destination) {
+ if (destination == null) return false;
+
+ // if the destination is present and the route is default.
+ // return true
+ if (isDefault()) return true;
+
+ // match the route destination and destination with prefix length
+ InetAddress dstNet = NetworkUtils.getNetworkPart(destination,
+ mDestination.getNetworkPrefixLength());
+
+ return mDestination.getAddress().equals(dstNet);
+ }
+
+ /**
+ * Find the route from a Collection of routes that best matches a given address.
+ * May return null if no routes are applicable.
+ * @param routes a Collection of RouteInfos to chose from
+ * @param dest the InetAddress your trying to get to
+ * @return the RouteInfo from the Collection that best fits the given address
+ */
+ public static RouteInfo selectBestRoute(Collection<RouteInfo> routes, InetAddress dest) {
+ if ((routes == null) || (dest == null)) return null;
+
+ RouteInfo bestRoute = null;
+ // pick a longest prefix match under same address type
+ for (RouteInfo route : routes) {
+ if (NetworkUtils.addressTypeMatches(route.mDestination.getAddress(), dest)) {
+ if ((bestRoute != null) &&
+ (bestRoute.mDestination.getNetworkPrefixLength() >=
+ route.mDestination.getNetworkPrefixLength())) {
+ continue;
+ }
+ if (route.matches(dest)) bestRoute = route;
+ }
+ }
+ return bestRoute;
+ }
+}
diff --git a/core/jni/android_net_NetUtils.cpp b/core/jni/android_net_NetUtils.cpp
index 351f264..ddae505 100644
--- a/core/jni/android_net_NetUtils.cpp
+++ b/core/jni/android_net_NetUtils.cpp
@@ -1,16 +1,16 @@
/*
* Copyright 2008, 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
+ * 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
+ * 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
+ * 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.
*/
@@ -21,25 +21,31 @@
#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);
int ifc_disable(const char *ifname);
-int ifc_add_route(const char *ifname, const char *destStr, uint32_t prefixLen, const char *gwStr);
-int ifc_remove_host_routes(const char *ifname);
-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_do_request_renew(const char *ifname,
+ 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);
char *dhcp_get_errmsg();
@@ -55,16 +61,14 @@
* to look them up every time.
*/
static struct fieldIds {
- jclass dhcpInfoClass;
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)
{
@@ -86,56 +90,6 @@
return (jint)result;
}
-static jint android_net_utils_addRoute(JNIEnv* env, jobject clazz, jstring ifname,
- jstring dst, jint prefixLength, jstring gw)
-{
- int result;
-
- const char *nameStr = env->GetStringUTFChars(ifname, NULL);
- const char *dstStr = env->GetStringUTFChars(dst, NULL);
- const char *gwStr = NULL;
- if (gw != NULL) {
- gwStr = env->GetStringUTFChars(gw, NULL);
- }
- result = ::ifc_add_route(nameStr, dstStr, prefixLength, gwStr);
- env->ReleaseStringUTFChars(ifname, nameStr);
- env->ReleaseStringUTFChars(dst, dstStr);
- if (gw != NULL) {
- env->ReleaseStringUTFChars(gw, gwStr);
- }
- return (jint)result;
-}
-
-static jint android_net_utils_removeHostRoutes(JNIEnv* env, jobject clazz, jstring ifname)
-{
- int result;
-
- const char *nameStr = env->GetStringUTFChars(ifname, NULL);
- result = ::ifc_remove_host_routes(nameStr);
- env->ReleaseStringUTFChars(ifname, nameStr);
- return (jint)result;
-}
-
-static jint android_net_utils_getDefaultRoute(JNIEnv* env, jobject clazz, jstring ifname)
-{
- int result;
-
- const char *nameStr = env->GetStringUTFChars(ifname, NULL);
- result = ::ifc_get_default_route(nameStr);
- env->ReleaseStringUTFChars(ifname, nameStr);
- return (jint)result;
-}
-
-static jint android_net_utils_removeDefaultRoute(JNIEnv* env, jobject clazz, jstring ifname)
-{
- int result;
-
- const char *nameStr = env->GetStringUTFChars(ifname, NULL);
- result = ::ifc_remove_default_route(nameStr);
- env->ReleaseStringUTFChars(ifname, nameStr);
- return (jint)result;
-}
-
static jint android_net_utils_resetConnections(JNIEnv* env, jobject clazz, jstring ifname)
{
int result;
@@ -146,28 +100,77 @@
return (jint)result;
}
-static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
+static jboolean android_net_utils_runDhcpCommon(JNIEnv* env, jobject clazz, jstring ifname,
+ jobject info, bool renew)
{
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;
+
+ if (renew) {
+ result = ::dhcp_do_request_renew(nameStr, ipaddr, gateway, &prefixLength,
+ dns1, dns2, server, &lease);
+ } else {
+ 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) {
+ env->SetObjectField(info, dhcpInfoInternalFieldIds.ipaddress, env->NewStringUTF(ipaddr));
+
+ // 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);
+
+ 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();
+ }
+
+ 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);
}
+static jboolean android_net_utils_runDhcp(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
+{
+ return android_net_utils_runDhcpCommon(env, clazz, ifname, info, false);
+}
+
+static jboolean android_net_utils_runDhcpRenew(JNIEnv* env, jobject clazz, jstring ifname, jobject info)
+{
+ return android_net_utils_runDhcpCommon(env, clazz, ifname, info, true);
+}
+
+
static jboolean android_net_utils_stopDhcp(JNIEnv* env, jobject clazz, jstring ifname)
{
int result;
@@ -193,24 +196,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);
-}
-
// ----------------------------------------------------------------------------
/*
@@ -221,36 +206,25 @@
{ "enableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_enableInterface },
{ "disableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_disableInterface },
- { "addRoute", "(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)I",
- (void *)android_net_utils_addRoute },
- { "removeHostRoutes", "(Ljava/lang/String;)I", (void *)android_net_utils_removeHostRoutes },
- { "getDefaultRouteNative", "(Ljava/lang/String;)I",
- (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 },
+ { "runDhcpRenew", "(Ljava/lang/String;Landroid/net/DhcpInfoInternal;)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 },
- { "configureNative", "(Ljava/lang/String;IIIII)Z", (void *)android_net_utils_configureInterface },
{ "getDhcpError", "()Ljava/lang/String;", (void*) android_net_utils_getDhcpError },
};
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");
- }
+ 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");
return AndroidRuntime::registerNativeMethods(env,
NETUTILS_PKG_NAME, gNetworkUtilMethods, NELEM(gNetworkUtilMethods));
diff --git a/core/tests/coretests/src/android/net/LinkPropertiesTest.java b/core/tests/coretests/src/android/net/LinkPropertiesTest.java
new file mode 100644
index 0000000..e3b6b5f
--- /dev/null
+++ b/core/tests/coretests/src/android/net/LinkPropertiesTest.java
@@ -0,0 +1,200 @@
+/*
+ * 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 android.net.LinkProperties;
+import android.net.RouteInfo;
+import android.test.suitebuilder.annotation.SmallTest;
+import junit.framework.TestCase;
+
+import java.net.InetAddress;
+
+public class LinkPropertiesTest extends TestCase {
+ private static String ADDRV4 = "75.208.6.1";
+ private static String ADDRV6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
+ private static String DNS1 = "75.208.7.1";
+ private static String DNS2 = "69.78.7.1";
+ private static String GATEWAY1 = "75.208.8.1";
+ private static String GATEWAY2 = "69.78.8.1";
+ private static String NAME = "qmi0";
+
+ @SmallTest
+ public void testEqualsNull() {
+ LinkProperties source = new LinkProperties();
+ LinkProperties target = new LinkProperties();
+
+ assertFalse(source == target);
+ assertTrue(source.equals(target));
+ assertTrue(source.hashCode() == target.hashCode());
+ }
+
+ @SmallTest
+ public void testEqualsSameOrder() {
+ try {
+ LinkProperties source = new LinkProperties();
+ source.setInterfaceName(NAME);
+ // set 2 link addresses
+ source.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ source.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+ // set 2 dnses
+ source.addDns(NetworkUtils.numericToInetAddress(DNS1));
+ source.addDns(NetworkUtils.numericToInetAddress(DNS2));
+ // set 2 gateways
+ source.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1)));
+ source.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
+
+ LinkProperties target = new LinkProperties();
+
+ // All fields are same
+ target.setInterfaceName(NAME);
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS1));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS2));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1)));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
+
+ assertTrue(source.equals(target));
+ assertTrue(source.hashCode() == target.hashCode());
+
+ target.clear();
+ // change Interface Name
+ target.setInterfaceName("qmi1");
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS1));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS2));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1)));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
+ assertFalse(source.equals(target));
+
+ target.clear();
+ target.setInterfaceName(NAME);
+ // change link addresses
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress("75.208.6.2"), 32));
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS1));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS2));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1)));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
+ assertFalse(source.equals(target));
+
+ target.clear();
+ target.setInterfaceName(NAME);
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+ // change dnses
+ target.addDns(NetworkUtils.numericToInetAddress("75.208.7.2"));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS2));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1)));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
+ assertFalse(source.equals(target));
+
+ target.clear();
+ target.setInterfaceName(NAME);
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS1));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS2));
+ // change gateway
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress("75.208.8.2")));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
+ assertFalse(source.equals(target));
+
+ } catch (Exception e) {
+ throw new RuntimeException(e.toString());
+ //fail();
+ }
+ }
+
+ @SmallTest
+ public void testEqualsDifferentOrder() {
+ try {
+ LinkProperties source = new LinkProperties();
+ source.setInterfaceName(NAME);
+ // set 2 link addresses
+ source.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ source.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+ // set 2 dnses
+ source.addDns(NetworkUtils.numericToInetAddress(DNS1));
+ source.addDns(NetworkUtils.numericToInetAddress(DNS2));
+ // set 2 gateways
+ source.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1)));
+ source.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
+
+ LinkProperties target = new LinkProperties();
+ // Exchange order
+ target.setInterfaceName(NAME);
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS2));
+ target.addDns(NetworkUtils.numericToInetAddress(DNS1));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY2)));
+ target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress(GATEWAY1)));
+
+ assertTrue(source.equals(target));
+ assertTrue(source.hashCode() == target.hashCode());
+ } catch (Exception e) {
+ fail();
+ }
+ }
+
+ @SmallTest
+ public void testEqualsDuplicated() {
+ try {
+ LinkProperties source = new LinkProperties();
+ // set 3 link addresses, eg, [A, A, B]
+ source.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ source.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ source.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+
+ LinkProperties target = new LinkProperties();
+ // set 3 link addresses, eg, [A, B, B]
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV4), 32));
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+ target.addLinkAddress(new LinkAddress(
+ NetworkUtils.numericToInetAddress(ADDRV6), 128));
+
+ assertTrue(source.equals(target));
+ assertTrue(source.hashCode() == target.hashCode());
+ } catch (Exception e) {
+ fail();
+ }
+ }
+
+}
diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java
index 2f12a95..ca2adb4 100644
--- a/services/java/com/android/server/ConnectivityService.java
+++ b/services/java/com/android/server/ConnectivityService.java
@@ -24,20 +24,25 @@
import android.database.ContentObserver;
import android.net.ConnectivityManager;
import android.net.DummyDataStateTracker;
+import android.net.EthernetDataTracker;
import android.net.IConnectivityManager;
+import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.MobileDataStateTracker;
+import android.net.NetworkConfig;
import android.net.NetworkInfo;
import android.net.NetworkStateTracker;
import android.net.NetworkUtils;
import android.net.Proxy;
import android.net.ProxyProperties;
+import android.net.RouteInfo;
import android.net.vpn.VpnManager;
import android.net.wifi.WifiStateTracker;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
+import android.os.INetworkManagementService;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
@@ -57,6 +62,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
+import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
@@ -78,6 +84,10 @@
private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
"android.telephony.apn-restore";
+ // used in recursive route setting to add gateways for the host for which
+ // a host route was requested.
+ private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
+
private Tethering mTethering;
private boolean mTetheringConfigValid = false;
@@ -117,6 +127,8 @@
private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
+ private INetworkManagementService mNetd;
+
private static final int ENABLED = 1;
private static final int DISABLED = 0;
@@ -188,6 +200,14 @@
private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
MAX_NETWORK_STATE_TRACKER_EVENT + 9;
+ /**
+ * used internally to set external dependency met/unmet
+ * arg1 = ENABLED (met) or DISABLED (unmet)
+ * arg2 = NetworkType
+ */
+ private static final int EVENT_SET_DEPENDENCY_MET =
+ MAX_NETWORK_STATE_TRACKER_EVENT + 10;
+
private Handler mHandler;
// list of DeathRecipients used to make sure features are turned off when
@@ -216,28 +236,7 @@
private SettingsObserver mSettingsObserver;
- private static class NetworkAttributes {
- /**
- * Class for holding settings read from resources.
- */
- public String mName;
- public int mType;
- public int mRadio;
- public int mPriority;
- public NetworkInfo.State mLastState;
- public NetworkAttributes(String init) {
- String fragments[] = init.split(",");
- mName = fragments[0].toLowerCase();
- mType = Integer.parseInt(fragments[1]);
- mRadio = Integer.parseInt(fragments[2]);
- mPriority = Integer.parseInt(fragments[3]);
- mLastState = NetworkInfo.State.UNKNOWN;
- }
- public boolean isDefault() {
- return (mType == mRadio);
- }
- }
- NetworkAttributes[] mNetAttributes;
+ NetworkConfig[] mNetConfigs;
int mNetworksDefined;
private static class RadioAttributes {
@@ -286,8 +285,8 @@
com.android.internal.R.string.config_default_dns_server);
}
try {
- mDefaultDns = InetAddress.getByName(dns);
- } catch (UnknownHostException e) {
+ mDefaultDns = NetworkUtils.numericToInetAddress(dns);
+ } catch (IllegalArgumentException e) {
loge("Error setting defaultDns using " + dns);
}
@@ -304,7 +303,7 @@
mNetworkPreference = getPersistedNetworkPreference();
mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
- mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
+ mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
// Load device network attributes from resources
String[] raStrings = context.getResources().getStringArray(
@@ -327,23 +326,23 @@
com.android.internal.R.array.networkAttributes);
for (String naString : naStrings) {
try {
- NetworkAttributes n = new NetworkAttributes(naString);
- if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
+ NetworkConfig n = new NetworkConfig(naString);
+ if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
loge("Error in networkAttributes - ignoring attempt to define type " +
- n.mType);
+ n.type);
continue;
}
- if (mNetAttributes[n.mType] != null) {
+ if (mNetConfigs[n.type] != null) {
loge("Error in networkAttributes - ignoring attempt to redefine type " +
- n.mType);
+ n.type);
continue;
}
- if (mRadioAttributes[n.mRadio] == null) {
+ if (mRadioAttributes[n.radio] == null) {
loge("Error in networkAttributes - ignoring attempt to use undefined " +
- "radio " + n.mRadio + " in network type " + n.mType);
+ "radio " + n.radio + " in network type " + n.type);
continue;
}
- mNetAttributes[n.mType] = n;
+ mNetConfigs[n.type] = n;
mNetworksDefined++;
} catch(Exception e) {
// ignore it - leave the entry null
@@ -357,16 +356,16 @@
int currentLowest = 0;
int nextLowest = 0;
while (insertionPoint > -1) {
- for (NetworkAttributes na : mNetAttributes) {
+ for (NetworkConfig na : mNetConfigs) {
if (na == null) continue;
- if (na.mPriority < currentLowest) continue;
- if (na.mPriority > currentLowest) {
- if (na.mPriority < nextLowest || nextLowest == 0) {
- nextLowest = na.mPriority;
+ if (na.priority < currentLowest) continue;
+ if (na.priority > currentLowest) {
+ if (na.priority < nextLowest || nextLowest == 0) {
+ nextLowest = na.priority;
}
continue;
}
- mPriorityList[insertionPoint--] = na.mType;
+ mPriorityList[insertionPoint--] = na.type;
}
currentLowest = nextLowest;
nextLowest = 0;
@@ -392,7 +391,7 @@
* to change very often.
*/
for (int netType : mPriorityList) {
- switch (mNetAttributes[netType].mRadio) {
+ switch (mNetConfigs[netType].radio) {
case ConnectivityManager.TYPE_WIFI:
if (DBG) log("Starting Wifi Service.");
WifiStateTracker wst = new WifiStateTracker();
@@ -408,26 +407,33 @@
break;
case ConnectivityManager.TYPE_MOBILE:
mNetTrackers[netType] = new MobileDataStateTracker(netType,
- mNetAttributes[netType].mName);
+ mNetConfigs[netType].name);
mNetTrackers[netType].startMonitoring(context, mHandler);
break;
case ConnectivityManager.TYPE_DUMMY:
mNetTrackers[netType] = new DummyDataStateTracker(netType,
- mNetAttributes[netType].mName);
+ mNetConfigs[netType].name);
mNetTrackers[netType].startMonitoring(context, mHandler);
break;
case ConnectivityManager.TYPE_BLUETOOTH:
mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
mNetTrackers[netType].startMonitoring(context, mHandler);
break;
+ case ConnectivityManager.TYPE_ETHERNET:
+ mNetTrackers[netType] = EthernetDataTracker.getInstance();
+ mNetTrackers[netType].startMonitoring(context, mHandler);
+ break;
default:
loge("Trying to create a DataStateTracker for an unknown radio type " +
- mNetAttributes[netType].mRadio);
+ mNetConfigs[netType].radio);
continue;
}
}
- mTethering = new Tethering(mContext, mHandler.getLooper());
+ IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
+ INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
+
+ mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
!mTethering.isDunRequired()) &&
(mTethering.getTetherableUsbRegexs().length != 0 ||
@@ -469,8 +475,8 @@
private void handleSetNetworkPreference(int preference) {
if (ConnectivityManager.isNetworkTypeValid(preference) &&
- mNetAttributes[preference] != null &&
- mNetAttributes[preference].isDefault()) {
+ mNetConfigs[preference] != null &&
+ mNetConfigs[preference].isDefault()) {
if (mNetworkPreference != preference) {
final ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
@@ -538,18 +544,8 @@
*/
public NetworkInfo getActiveNetworkInfo() {
enforceAccessPermission();
- for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
- if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
- continue;
- }
- NetworkStateTracker t = mNetTrackers[type];
- NetworkInfo info = t.getNetworkInfo();
- if (info.isConnected()) {
- if (DBG && type != mActiveDefaultNetwork) {
- loge("connected default network is not mActiveDefaultNetwork!");
- }
- return info;
- }
+ if (mActiveDefaultNetwork != -1) {
+ return mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
}
return null;
}
@@ -585,7 +581,7 @@
public LinkProperties getActiveLinkProperties() {
enforceAccessPermission();
for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
- if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
+ if (mNetConfigs[type] == null || !mNetConfigs[type].isDefault()) {
continue;
}
NetworkStateTracker t = mNetTrackers[type];
@@ -687,7 +683,7 @@
}
enforceChangePermission();
if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
- mNetAttributes[networkType] == null) {
+ mNetConfigs[networkType] == null) {
return Phone.APN_REQUEST_FAILED;
}
@@ -696,15 +692,10 @@
// TODO - move this into the MobileDataStateTracker
int usedNetworkType = networkType;
if(networkType == ConnectivityManager.TYPE_MOBILE) {
- if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
- usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
- } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
- usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
- } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
- TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
- usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
- } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
- usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
+ usedNetworkType = convertFeatureToNetworkType(feature);
+ if (usedNetworkType < 0) {
+ Slog.e(TAG, "Can't match any netTracker!");
+ usedNetworkType = networkType;
}
}
NetworkStateTracker network = mNetTrackers[usedNetworkType];
@@ -730,9 +721,13 @@
mNetRequestersPids[usedNetworkType].add(currentPid);
}
}
- mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
- f), getRestoreDefaultNetworkDelay());
+ int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
+
+ if (restoreTimer >= 0) {
+ mHandler.sendMessageDelayed(
+ mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
+ }
if ((ni.isConnectedOrConnecting() == true) &&
!network.isTeardownRequested()) {
@@ -848,15 +843,9 @@
// TODO - move to MobileDataStateTracker
int usedNetworkType = networkType;
if (networkType == ConnectivityManager.TYPE_MOBILE) {
- if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
- usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
- } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
- usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
- } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
- TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
- usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
- } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
- usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
+ usedNetworkType = convertFeatureToNetworkType(feature);
+ if (usedNetworkType < 0) {
+ usedNetworkType = networkType;
}
}
tracker = mNetTrackers[usedNetworkType];
@@ -934,7 +923,7 @@
}
try {
InetAddress addr = InetAddress.getByAddress(hostAddress);
- return addHostRoute(tracker, addr);
+ return addHostRoute(tracker, addr, 0);
} catch (UnknownHostException e) {}
return false;
}
@@ -947,24 +936,49 @@
* TODO - deprecate
* @return {@code true} on success, {@code false} on failure
*/
- private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
- if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
- return false;
- }
+ private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
+ LinkProperties lp = nt.getLinkProperties();
+ if ((lp == null) || (hostAddress == null)) return false;
- LinkProperties p = nt.getLinkProperties();
- if (p == null) return false;
- String interfaceName = p.getInterfaceName();
-
+ String interfaceName = lp.getInterfaceName();
if (DBG) {
- log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
+ log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
+ cycleCount);
}
- if (interfaceName != null) {
- return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
- } else {
+ if (interfaceName == null) {
if (DBG) loge("addHostRoute failed due to null interface name");
return false;
}
+
+ RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
+ InetAddress gatewayAddress = null;
+ if (bestRoute != null) {
+ gatewayAddress = bestRoute.getGateway();
+ // if the best route is ourself, don't relf-reference, just add the host route
+ if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
+ }
+ if (gatewayAddress != null) {
+ if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
+ loge("Error adding hostroute - too much recursion");
+ return false;
+ }
+ if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
+ }
+
+ RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
+
+ try {
+ mNetd.addRoute(interfaceName, route);
+ return true;
+ } catch (Exception ex) {
+ return false;
+ }
+ }
+
+ // TODO support the removal of single host routes. Keep a ref count of them so we
+ // aren't over-zealous
+ private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
+ return false;
}
/**
@@ -1010,6 +1024,24 @@
return retVal;
}
+ public void setDataDependency(int networkType, boolean met) {
+ enforceChangePermission();
+ if (DBG) {
+ log("setDataDependency(" + networkType + ", " + met + ")");
+ }
+ mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
+ (met ? ENABLED : DISABLED), networkType));
+ }
+
+ private void handleSetDependencyMet(int networkType, boolean met) {
+ if (mNetTrackers[networkType] != null) {
+ if (DBG) {
+ log("handleSetDependencyMet(" + networkType + ", " + met + ")");
+ }
+ mNetTrackers[networkType].setDependencyMet(met);
+ }
+ }
+
/**
* @see ConnectivityManager#setMobileDataEnabled(boolean)
*/
@@ -1018,7 +1050,7 @@
if (DBG) log("setMobileDataEnabled(" + enabled + ")");
mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
- (enabled ? ENABLED : DISABLED), 0));
+ (enabled ? ENABLED : DISABLED), 0));
}
private void handleSetMobileData(boolean enabled) {
@@ -1079,7 +1111,7 @@
* getting the disconnect for a network that we explicitly disabled
* in accordance with network preference policies.
*/
- if (!mNetAttributes[prevNetType].isDefault()) {
+ if (!mNetConfigs[prevNetType].isDefault()) {
List pids = mNetRequestersPids[prevNetType];
for (int i = 0; i<pids.size(); i++) {
Integer pid = (Integer)pids.get(i);
@@ -1104,7 +1136,7 @@
info.getExtraInfo());
}
- if (mNetAttributes[prevNetType].isDefault()) {
+ if (mNetConfigs[prevNetType].isDefault()) {
tryFailover(prevNetType);
if (mActiveDefaultNetwork != -1) {
NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
@@ -1134,7 +1166,7 @@
* Try to reconnect on all available and let them hash it out when
* more than one connects.
*/
- if (mNetAttributes[prevNetType].isDefault()) {
+ if (mNetConfigs[prevNetType].isDefault()) {
if (mActiveDefaultNetwork == prevNetType) {
mActiveDefaultNetwork = -1;
}
@@ -1144,12 +1176,12 @@
// TODO - don't filter by priority now - nice optimization but risky
// int currentPriority = -1;
// if (mActiveDefaultNetwork != -1) {
-// currentPriority = mNetAttributes[mActiveDefaultNetwork].mPriority;
+// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
// }
for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
if (checkType == prevNetType) continue;
- if (mNetAttributes[checkType] == null) continue;
- if (!mNetAttributes[checkType].isDefault()) continue;
+ if (mNetConfigs[checkType] == null) continue;
+ if (!mNetConfigs[checkType].isDefault()) continue;
// Enabling the isAvailable() optimization caused mobile to not get
// selected if it was in the middle of error handling. Specifically
@@ -1161,7 +1193,7 @@
// complete before it is really complete.
// if (!mNetTrackers[checkType].isAvailable()) continue;
-// if (currentPriority >= mNetAttributes[checkType].mPriority) continue;
+// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
NetworkStateTracker checkTracker = mNetTrackers[checkType];
NetworkInfo checkInfo = checkTracker.getNetworkInfo();
@@ -1234,7 +1266,7 @@
info.setFailover(false);
}
- if (mNetAttributes[info.getType()].isDefault()) {
+ if (mNetConfigs[info.getType()].isDefault()) {
tryFailover(info.getType());
if (mActiveDefaultNetwork != -1) {
NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
@@ -1267,6 +1299,9 @@
}
void systemReady() {
+ IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
+ mNetd = INetworkManagementService.Stub.asInterface(b);
+
synchronized(this) {
mSystemReady = true;
if (mInitialBroadcast != null) {
@@ -1287,11 +1322,11 @@
// if this is a default net and other default is running
// kill the one not preferred
- if (mNetAttributes[type].isDefault()) {
+ if (mNetConfigs[type].isDefault()) {
if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
if ((type != mNetworkPreference &&
- mNetAttributes[mActiveDefaultNetwork].mPriority >
- mNetAttributes[type].mPriority) ||
+ mNetConfigs[mActiveDefaultNetwork].priority >
+ mNetConfigs[type].priority) ||
mNetworkPreference == mActiveDefaultNetwork) {
// don't accept this one
if (DBG) {
@@ -1310,6 +1345,7 @@
}
if (!teardown(otherNet)) {
loge("Network declined teardown request");
+ teardown(thisNet);
return;
}
}
@@ -1355,14 +1391,33 @@
handleDnsConfigurationChange(netType);
if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
- if (mNetAttributes[netType].isDefault()) {
+ if (mNetConfigs[netType].isDefault()) {
handleApplyDefaultProxy(netType);
addDefaultRoute(mNetTrackers[netType]);
} else {
+ // many radios add a default route even when we don't want one.
+ // remove the default route unless we need it for our active network
+ if (mActiveDefaultNetwork != -1) {
+ LinkProperties defaultLinkProperties =
+ mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
+ LinkProperties newLinkProperties =
+ mNetTrackers[netType].getLinkProperties();
+ String defaultIface = defaultLinkProperties.getInterfaceName();
+ if (defaultIface != null &&
+ !defaultIface.equals(newLinkProperties.getInterfaceName())) {
+ removeDefaultRoute(mNetTrackers[netType]);
+ }
+ }
addPrivateDnsRoutes(mNetTrackers[netType]);
}
+
+ /** Notify TetheringService if interface name has been changed. */
+ if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
+ Phone.REASON_LINK_PROPERTIES_CHANGED)) {
+ handleTetherIfaceChange(netType);
+ }
} else {
- if (mNetAttributes[netType].isDefault()) {
+ if (mNetConfigs[netType].isDefault()) {
removeDefaultRoute(mNetTrackers[netType]);
} else {
removePrivateDnsRoutes(mNetTrackers[netType]);
@@ -1383,16 +1438,13 @@
if (interfaceName != null && !privateDnsRouteSet) {
Collection<InetAddress> dnsList = p.getDnses();
for (InetAddress dns : dnsList) {
- if (DBG) log(" adding " + dns);
- NetworkUtils.addHostRoute(interfaceName, dns, null);
+ addHostRoute(nt, dns, 0);
}
nt.privateDnsRouteSet(true);
}
}
private void removePrivateDnsRoutes(NetworkStateTracker nt) {
- // TODO - we should do this explicitly but the NetUtils api doesnt
- // support this yet - must remove all. No worse than before
LinkProperties p = nt.getLinkProperties();
if (p == null) return;
String interfaceName = p.getInterfaceName();
@@ -1402,7 +1454,17 @@
log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
" (" + interfaceName + ")");
}
- NetworkUtils.removeHostRoutes(interfaceName);
+
+ Collection<InetAddress> dnsList = p.getDnses();
+ for (InetAddress dns : dnsList) {
+ if (DBG) log(" removing " + dns);
+ RouteInfo route = RouteInfo.makeHostRoute(dns);
+ try {
+ mNetd.removeRoute(interfaceName, route);
+ } catch (Exception ex) {
+ loge("error (" + ex + ") removing dns route " + route);
+ }
+ }
nt.privateDnsRouteSet(false);
}
}
@@ -1412,13 +1474,29 @@
LinkProperties p = nt.getLinkProperties();
if (p == null) return;
String interfaceName = p.getInterfaceName();
- InetAddress defaultGatewayAddr = p.getGateway();
+ if (TextUtils.isEmpty(interfaceName)) return;
- if ((interfaceName != null) && (defaultGatewayAddr != null )) {
- if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
- NetworkInfo networkInfo = nt.getNetworkInfo();
- log("addDefaultRoute for " + networkInfo.getTypeName() +
- " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
+ for (RouteInfo route : p.getRoutes()) {
+ //TODO - handle non-default routes
+ if (route.isDefaultRoute()) {
+ if (DBG) log("adding default route " + route);
+ InetAddress gateway = route.getGateway();
+ if (addHostRoute(nt, gateway, 0)) {
+ try {
+ mNetd.addRoute(interfaceName, route);
+ } catch (Exception e) {
+ loge("error adding default route " + route);
+ continue;
+ }
+ if (DBG) {
+ NetworkInfo networkInfo = nt.getNetworkInfo();
+ log("addDefaultRoute for " + networkInfo.getTypeName() +
+ " (" + interfaceName + "), GatewayAddr=" +
+ gateway.getHostAddress());
+ }
+ } else {
+ loge("error adding host route for default route " + route);
+ }
}
}
}
@@ -1429,11 +1507,22 @@
if (p == null) return;
String interfaceName = p.getInterfaceName();
- if (interfaceName != null) {
- if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
- NetworkInfo networkInfo = nt.getNetworkInfo();
- log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
- interfaceName + ")");
+ if (interfaceName == null) return;
+
+ for (RouteInfo route : p.getRoutes()) {
+ //TODO - handle non-default routes
+ if (route.isDefaultRoute()) {
+ try {
+ mNetd.removeRoute(interfaceName, route);
+ } catch (Exception ex) {
+ loge("error (" + ex + ") removing default route " + route);
+ continue;
+ }
+ if (DBG) {
+ NetworkInfo networkInfo = nt.getNetworkInfo();
+ log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
+ interfaceName + ")");
+ }
}
}
}
@@ -1518,7 +1607,7 @@
{
if (DBG) log("reassessPidDns for pid " + myPid);
for(int i : mPriorityList) {
- if (mNetAttributes[i].isDefault()) {
+ if (mNetConfigs[i].isDefault()) {
continue;
}
NetworkStateTracker nt = mNetTrackers[i];
@@ -1600,7 +1689,7 @@
if (p == null) return;
Collection<InetAddress> dnses = p.getDnses();
boolean changed = false;
- if (mNetAttributes[netType].isDefault()) {
+ if (mNetConfigs[netType].isDefault()) {
int j = 1;
if (dnses.size() == 0 && mDefaultDns != null) {
String dnsString = mDefaultDns.getHostAddress();
@@ -1647,7 +1736,7 @@
}
}
- private int getRestoreDefaultNetworkDelay() {
+ private int getRestoreDefaultNetworkDelay(int networkType) {
String restoreDefaultNetworkDelayStr = SystemProperties.get(
NETWORK_RESTORE_DELAY_PROP_NAME);
if(restoreDefaultNetworkDelayStr != null &&
@@ -1657,7 +1746,14 @@
} catch (NumberFormatException e) {
}
}
- return RESTORE_DEFAULT_NETWORK_DELAY;
+ // if the system property isn't set, use the value for the apn type
+ int ret = RESTORE_DEFAULT_NETWORK_DELAY;
+
+ if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
+ (mNetConfigs[networkType] != null)) {
+ ret = mNetConfigs[networkType].restoreTime;
+ }
+ return ret;
}
@Override
@@ -1731,23 +1827,6 @@
info = (NetworkInfo) msg.obj;
int type = info.getType();
NetworkInfo.State state = info.getState();
- // only do this optimization for wifi. It going into scan mode for location
- // services generates alot of noise. Meanwhile the mms apn won't send out
- // subsequent notifications when on default cellular because it never
- // disconnects.. so only do this to wifi notifications. Fixed better when the
- // APN notifications are standardized.
- if (mNetAttributes[type].mLastState == state &&
- mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
- if (DBG) {
- // TODO - remove this after we validate the dropping doesn't break
- // anything
- log("Dropping ConnectivityChange for " +
- info.getTypeName() + ": " +
- state + "/" + info.getDetailedState());
- }
- return;
- }
- mNetAttributes[type].mLastState = state;
if (DBG) log("ConnectivityChange for " +
info.getTypeName() + ": " +
@@ -1786,8 +1865,7 @@
break;
case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
info = (NetworkInfo) msg.obj;
- type = info.getType();
- handleConnectivityChange(type);
+ handleConnectivityChange(info.getType());
break;
case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
String causedBy = null;
@@ -1841,6 +1919,13 @@
case EVENT_APPLY_GLOBAL_HTTP_PROXY:
{
handleDeprecatedGlobalHttpProxy();
+ break;
+ }
+ case EVENT_SET_DEPENDENCY_MET:
+ {
+ boolean met = (msg.arg1 == ENABLED);
+ handleSetDependencyMet(msg.arg2, met);
+ break;
}
}
}
@@ -2140,7 +2225,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);
}
@@ -2166,6 +2252,14 @@
}
}
+ private void handleTetherIfaceChange(int type) {
+ String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
+
+ if (isTetheringSupported()) {
+ mTethering.handleTetherIfaceChange(iface);
+ }
+ }
+
private void log(String s) {
Slog.d(TAG, s);
}
@@ -2173,4 +2267,24 @@
private void loge(String s) {
Slog.e(TAG, s);
}
+ int convertFeatureToNetworkType(String feature){
+ int networkType = -1;
+ if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
+ networkType = ConnectivityManager.TYPE_MOBILE_MMS;
+ } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
+ networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
+ } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
+ TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
+ networkType = ConnectivityManager.TYPE_MOBILE_DUN;
+ } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
+ networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
+ } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
+ networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
+ } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
+ networkType = ConnectivityManager.TYPE_MOBILE_IMS;
+ } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
+ networkType = ConnectivityManager.TYPE_MOBILE_CBS;
+ }
+ return networkType;
+ }
}