Loading core/java/android/net/ConnectivityManager.java +25 −1 Original line number Diff line number Diff line Loading @@ -21,6 +21,9 @@ import android.annotation.SdkConstant.SdkConstantType; import android.os.Binder; import android.os.RemoteException; import java.net.InetAddress; import java.net.UnknownHostException; /** * Class that answers queries about the state of network connectivity. It also * notifies applications when network connectivity changes. Get an instance Loading Loading @@ -309,8 +312,29 @@ public class ConnectivityManager * @return {@code true} on success, {@code false} on failure */ public boolean requestRouteToHost(int networkType, int hostAddress) { InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress); if (inetAddress == null) { return false; } return requestRouteToHostAddress(networkType, inetAddress); } /** * Ensure that a network route exists to deliver traffic to the specified * host via the specified network interface. An attempt to add a route that * already exists is ignored, but treated as successful. * @param networkType the type of the network over which traffic to the specified * host is to be routed * @param hostAddress the IP address of the host to which the route is desired * @return {@code true} on success, {@code false} on failure * @hide */ public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { byte[] address = hostAddress.getAddress(); try { return mService.requestRouteToHost(networkType, hostAddress); return mService.requestRouteToHostAddress(networkType, address); } catch (RemoteException e) { return false; } Loading core/java/android/net/IConnectivityManager.aidl +2 −0 Original line number Diff line number Diff line Loading @@ -47,6 +47,8 @@ interface IConnectivityManager boolean requestRouteToHost(int networkType, int hostAddress); boolean requestRouteToHostAddress(int networkType, in byte[] hostAddress); boolean getBackgroundDataSetting(); void setBackgroundDataSetting(boolean allowBackgroundData); Loading core/java/android/net/MobileDataStateTracker.java +6 −6 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package android.net; import java.net.InetAddress; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; Loading @@ -23,7 +25,6 @@ import android.content.IntentFilter; import android.os.RemoteException; import android.os.Handler; import android.os.ServiceManager; import android.os.SystemProperties; import com.android.internal.telephony.ITelephony; import com.android.internal.telephony.Phone; import com.android.internal.telephony.TelephonyIntents; Loading Loading @@ -460,17 +461,16 @@ public class MobileDataStateTracker extends NetworkStateTracker { * Ensure that a network route exists to deliver traffic to the specified * host via the mobile data network. * @param hostAddress the IP address of the host to which the route is desired, * in network byte order. * @return {@code true} on success, {@code false} on failure */ @Override public boolean requestRouteToHost(int hostAddress) { public boolean requestRouteToHost(InetAddress hostAddress) { if (DBG) { Log.d(TAG, "Requested host route to " + Integer.toHexString(hostAddress) + Log.d(TAG, "Requested host route to " + hostAddress.getHostAddress() + " for " + mApnType + "(" + mInterfaceName + ")"); } if (mInterfaceName != null && hostAddress != -1) { return NetworkUtils.addHostRoute(mInterfaceName, hostAddress) == 0; if (mInterfaceName != null) { return NetworkUtils.addHostRoute(mInterfaceName, hostAddress, null); } else { return false; } Loading core/java/android/net/NetworkStateTracker.java +23 −9 Original line number Diff line number Diff line Loading @@ -18,13 +18,14 @@ package android.net; import java.io.FileWriter; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import android.os.Handler; import android.os.Message; import android.os.SystemProperties; import android.content.Context; import android.text.TextUtils; import android.util.Config; import android.util.Log; Loading Loading @@ -129,13 +130,18 @@ public abstract class NetworkStateTracker extends Handler { } if (mInterfaceName != null && !mPrivateDnsRouteSet) { for (String addrString : getNameServers()) { int addr = NetworkUtils.lookupHost(addrString); if (addr != -1 && addr != 0) { if (DBG) Log.d(TAG, " adding "+addrString+" ("+addr+")"); NetworkUtils.addHostRoute(mInterfaceName, addr); if (addrString != null) { try { InetAddress inetAddress = InetAddress.getByName(addrString); if (DBG) Log.d(TAG, " adding " + addrString); if (NetworkUtils.addHostRoute(mInterfaceName, inetAddress, null)) { mPrivateDnsRouteSet = true; } } catch (UnknownHostException e) { if (DBG) Log.d(TAG, " DNS address " + addrString + " : Exception " + e); } } } mPrivateDnsRouteSet = true; } } Loading @@ -159,8 +165,16 @@ public abstract class NetworkStateTracker extends Handler { Log.d(TAG, "addDefaultRoute for " + mNetworkInfo.getTypeName() + " (" + mInterfaceName + "), GatewayAddr=" + mDefaultGatewayAddr); } NetworkUtils.setDefaultRoute(mInterfaceName, mDefaultGatewayAddr); InetAddress inetAddress = NetworkUtils.intToInetAddress(mDefaultGatewayAddr); if (inetAddress == null) { if (DBG) Log.d(TAG, " Unable to add default route. mDefaultGatewayAddr Error"); } else { if (NetworkUtils.addDefaultRoute(mInterfaceName, inetAddress)) { mDefaultRouteSet = true; } else { if (DBG) Log.d(TAG, " Unable to add default route."); } } } } Loading Loading @@ -398,7 +412,7 @@ public abstract class NetworkStateTracker extends Handler { * @param hostAddress the IP address of the host to which the route is desired * @return {@code true} on success, {@code false} on failure */ public boolean requestRouteToHost(int hostAddress) { public boolean requestRouteToHost(InetAddress hostAddress) { return false; } Loading core/java/android/net/NetworkUtils.java +88 −22 Original line number Diff line number Diff line Loading @@ -17,25 +17,39 @@ package android.net; import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; import android.util.Log; /** * Native methods for managing network interfaces. * * {@hide} */ public class NetworkUtils { private static final String TAG = "NetworkUtils"; /** Bring the named network interface up. */ public native static int enableInterface(String interfaceName); /** Bring the named network interface down. */ public native static int disableInterface(String interfaceName); /** Add a route to the specified host via the named interface. */ public native static int addHostRoute(String interfaceName, int hostaddr); /** Add a default route for the named interface. */ public native static int setDefaultRoute(String interfaceName, int gwayAddr); /** * 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 native static int getDefaultRoute(String interfaceName); Loading Loading @@ -106,26 +120,78 @@ public class NetworkUtils { String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2); /** * Look up a host name and return the result as an int. Works if the argument * is an IP address in dot notation. Obviously, this can only be used for IPv4 * addresses. * @param hostname the name of the host (or the IP address) * @return the IP address as an {@code int} in network byte order * 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. */ public static int lookupHost(String hostname) { 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.getByName(hostname); inetAddress = InetAddress.getByAddress(addressBytes); } catch(UnknownHostException e) { return -1; return null; } return inetAddress; } /** * 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 */ 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; } /** * 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 */ public static boolean addHostRoute(String interfaceName, InetAddress dst, InetAddress gw) { if (dst == null) { Log.w(TAG, "addHostRoute: dst should not be null"); return false; } int prefixLength; String dstStr = dst.getHostAddress(); String gwStr = (gw != null) ? gw.getHostAddress() : null; 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; } byte[] addrBytes; int addr; addrBytes = inetAddress.getAddress(); addr = ((addrBytes[3] & 0xff) << 24) | ((addrBytes[2] & 0xff) << 16) | ((addrBytes[1] & 0xff) << 8) | (addrBytes[0] & 0xff); return addr; return addRoute(interfaceName, dstStr, prefixLength, gwStr) == 0; } } Loading
core/java/android/net/ConnectivityManager.java +25 −1 Original line number Diff line number Diff line Loading @@ -21,6 +21,9 @@ import android.annotation.SdkConstant.SdkConstantType; import android.os.Binder; import android.os.RemoteException; import java.net.InetAddress; import java.net.UnknownHostException; /** * Class that answers queries about the state of network connectivity. It also * notifies applications when network connectivity changes. Get an instance Loading Loading @@ -309,8 +312,29 @@ public class ConnectivityManager * @return {@code true} on success, {@code false} on failure */ public boolean requestRouteToHost(int networkType, int hostAddress) { InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress); if (inetAddress == null) { return false; } return requestRouteToHostAddress(networkType, inetAddress); } /** * Ensure that a network route exists to deliver traffic to the specified * host via the specified network interface. An attempt to add a route that * already exists is ignored, but treated as successful. * @param networkType the type of the network over which traffic to the specified * host is to be routed * @param hostAddress the IP address of the host to which the route is desired * @return {@code true} on success, {@code false} on failure * @hide */ public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { byte[] address = hostAddress.getAddress(); try { return mService.requestRouteToHost(networkType, hostAddress); return mService.requestRouteToHostAddress(networkType, address); } catch (RemoteException e) { return false; } Loading
core/java/android/net/IConnectivityManager.aidl +2 −0 Original line number Diff line number Diff line Loading @@ -47,6 +47,8 @@ interface IConnectivityManager boolean requestRouteToHost(int networkType, int hostAddress); boolean requestRouteToHostAddress(int networkType, in byte[] hostAddress); boolean getBackgroundDataSetting(); void setBackgroundDataSetting(boolean allowBackgroundData); Loading
core/java/android/net/MobileDataStateTracker.java +6 −6 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package android.net; import java.net.InetAddress; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; Loading @@ -23,7 +25,6 @@ import android.content.IntentFilter; import android.os.RemoteException; import android.os.Handler; import android.os.ServiceManager; import android.os.SystemProperties; import com.android.internal.telephony.ITelephony; import com.android.internal.telephony.Phone; import com.android.internal.telephony.TelephonyIntents; Loading Loading @@ -460,17 +461,16 @@ public class MobileDataStateTracker extends NetworkStateTracker { * Ensure that a network route exists to deliver traffic to the specified * host via the mobile data network. * @param hostAddress the IP address of the host to which the route is desired, * in network byte order. * @return {@code true} on success, {@code false} on failure */ @Override public boolean requestRouteToHost(int hostAddress) { public boolean requestRouteToHost(InetAddress hostAddress) { if (DBG) { Log.d(TAG, "Requested host route to " + Integer.toHexString(hostAddress) + Log.d(TAG, "Requested host route to " + hostAddress.getHostAddress() + " for " + mApnType + "(" + mInterfaceName + ")"); } if (mInterfaceName != null && hostAddress != -1) { return NetworkUtils.addHostRoute(mInterfaceName, hostAddress) == 0; if (mInterfaceName != null) { return NetworkUtils.addHostRoute(mInterfaceName, hostAddress, null); } else { return false; } Loading
core/java/android/net/NetworkStateTracker.java +23 −9 Original line number Diff line number Diff line Loading @@ -18,13 +18,14 @@ package android.net; import java.io.FileWriter; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import android.os.Handler; import android.os.Message; import android.os.SystemProperties; import android.content.Context; import android.text.TextUtils; import android.util.Config; import android.util.Log; Loading Loading @@ -129,13 +130,18 @@ public abstract class NetworkStateTracker extends Handler { } if (mInterfaceName != null && !mPrivateDnsRouteSet) { for (String addrString : getNameServers()) { int addr = NetworkUtils.lookupHost(addrString); if (addr != -1 && addr != 0) { if (DBG) Log.d(TAG, " adding "+addrString+" ("+addr+")"); NetworkUtils.addHostRoute(mInterfaceName, addr); if (addrString != null) { try { InetAddress inetAddress = InetAddress.getByName(addrString); if (DBG) Log.d(TAG, " adding " + addrString); if (NetworkUtils.addHostRoute(mInterfaceName, inetAddress, null)) { mPrivateDnsRouteSet = true; } } catch (UnknownHostException e) { if (DBG) Log.d(TAG, " DNS address " + addrString + " : Exception " + e); } } } mPrivateDnsRouteSet = true; } } Loading @@ -159,8 +165,16 @@ public abstract class NetworkStateTracker extends Handler { Log.d(TAG, "addDefaultRoute for " + mNetworkInfo.getTypeName() + " (" + mInterfaceName + "), GatewayAddr=" + mDefaultGatewayAddr); } NetworkUtils.setDefaultRoute(mInterfaceName, mDefaultGatewayAddr); InetAddress inetAddress = NetworkUtils.intToInetAddress(mDefaultGatewayAddr); if (inetAddress == null) { if (DBG) Log.d(TAG, " Unable to add default route. mDefaultGatewayAddr Error"); } else { if (NetworkUtils.addDefaultRoute(mInterfaceName, inetAddress)) { mDefaultRouteSet = true; } else { if (DBG) Log.d(TAG, " Unable to add default route."); } } } } Loading Loading @@ -398,7 +412,7 @@ public abstract class NetworkStateTracker extends Handler { * @param hostAddress the IP address of the host to which the route is desired * @return {@code true} on success, {@code false} on failure */ public boolean requestRouteToHost(int hostAddress) { public boolean requestRouteToHost(InetAddress hostAddress) { return false; } Loading
core/java/android/net/NetworkUtils.java +88 −22 Original line number Diff line number Diff line Loading @@ -17,25 +17,39 @@ package android.net; import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; import android.util.Log; /** * Native methods for managing network interfaces. * * {@hide} */ public class NetworkUtils { private static final String TAG = "NetworkUtils"; /** Bring the named network interface up. */ public native static int enableInterface(String interfaceName); /** Bring the named network interface down. */ public native static int disableInterface(String interfaceName); /** Add a route to the specified host via the named interface. */ public native static int addHostRoute(String interfaceName, int hostaddr); /** Add a default route for the named interface. */ public native static int setDefaultRoute(String interfaceName, int gwayAddr); /** * 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 native static int getDefaultRoute(String interfaceName); Loading Loading @@ -106,26 +120,78 @@ public class NetworkUtils { String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2); /** * Look up a host name and return the result as an int. Works if the argument * is an IP address in dot notation. Obviously, this can only be used for IPv4 * addresses. * @param hostname the name of the host (or the IP address) * @return the IP address as an {@code int} in network byte order * 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. */ public static int lookupHost(String hostname) { 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.getByName(hostname); inetAddress = InetAddress.getByAddress(addressBytes); } catch(UnknownHostException e) { return -1; return null; } return inetAddress; } /** * 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 */ 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; } /** * 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 */ public static boolean addHostRoute(String interfaceName, InetAddress dst, InetAddress gw) { if (dst == null) { Log.w(TAG, "addHostRoute: dst should not be null"); return false; } int prefixLength; String dstStr = dst.getHostAddress(); String gwStr = (gw != null) ? gw.getHostAddress() : null; 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; } byte[] addrBytes; int addr; addrBytes = inetAddress.getAddress(); addr = ((addrBytes[3] & 0xff) << 24) | ((addrBytes[2] & 0xff) << 16) | ((addrBytes[1] & 0xff) << 8) | (addrBytes[0] & 0xff); return addr; return addRoute(interfaceName, dstStr, prefixLength, gwStr) == 0; } }