|
17 | 17 | package android.net; |
18 | 18 |
|
19 | 19 | import java.net.InetAddress; |
| 20 | +import java.net.Inet4Address; |
| 21 | +import java.net.Inet6Address; |
20 | 22 | import java.net.UnknownHostException; |
21 | 23 |
|
| 24 | +import android.util.Log; |
| 25 | + |
22 | 26 | /** |
23 | 27 | * Native methods for managing network interfaces. |
24 | 28 | * |
25 | 29 | * {@hide} |
26 | 30 | */ |
27 | 31 | public class NetworkUtils { |
| 32 | + |
| 33 | + private static final String TAG = "NetworkUtils"; |
| 34 | + |
28 | 35 | /** Bring the named network interface up. */ |
29 | 36 | public native static int enableInterface(String interfaceName); |
30 | 37 |
|
31 | 38 | /** Bring the named network interface down. */ |
32 | 39 | public native static int disableInterface(String interfaceName); |
33 | 40 |
|
34 | | - /** Add a route to the specified host via the named interface. */ |
35 | | - public native static int addHostRoute(String interfaceName, int hostaddr); |
36 | | - |
37 | | - /** Add a default route for the named interface. */ |
38 | | - public native static int setDefaultRoute(String interfaceName, int gwayAddr); |
| 41 | + /** |
| 42 | + * Add a route to the routing table. |
| 43 | + * |
| 44 | + * @param interfaceName the interface to route through. |
| 45 | + * @param dst the network or host to route to. May be IPv4 or IPv6, e.g. |
| 46 | + * "0.0.0.0" or "2001:4860::". |
| 47 | + * @param prefixLength the prefix length of the route. |
| 48 | + * @param gw the gateway to use, e.g., "192.168.251.1". If null, |
| 49 | + * indicates a directly-connected route. |
| 50 | + */ |
| 51 | + public native static int addRoute(String interfaceName, String dst, |
| 52 | + int prefixLength, String gw); |
39 | 53 |
|
40 | 54 | /** Return the gateway address for the default route for the named interface. */ |
41 | 55 | public native static int getDefaultRoute(String interfaceName); |
@@ -106,26 +120,78 @@ private native static boolean configureNative( |
106 | 120 | String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2); |
107 | 121 |
|
108 | 122 | /** |
109 | | - * Look up a host name and return the result as an int. Works if the argument |
110 | | - * is an IP address in dot notation. Obviously, this can only be used for IPv4 |
111 | | - * addresses. |
112 | | - * @param hostname the name of the host (or the IP address) |
113 | | - * @return the IP address as an {@code int} in network byte order |
| 123 | + * Convert a IPv4 address from an integer to an InetAddress. |
| 124 | + * @param hostAddr is an Int corresponding to the IPv4 address in network byte order |
| 125 | + * @return the IP address as an {@code InetAddress}, returns null if |
| 126 | + * unable to convert or if the int is an invalid address. |
114 | 127 | */ |
115 | | - public static int lookupHost(String hostname) { |
| 128 | + public static InetAddress intToInetAddress(int hostAddress) { |
116 | 129 | InetAddress inetAddress; |
| 130 | + byte[] addressBytes = { (byte)(0xff & hostAddress), |
| 131 | + (byte)(0xff & (hostAddress >> 8)), |
| 132 | + (byte)(0xff & (hostAddress >> 16)), |
| 133 | + (byte)(0xff & (hostAddress >> 24)) }; |
| 134 | + |
117 | 135 | try { |
118 | | - inetAddress = InetAddress.getByName(hostname); |
119 | | - } catch (UnknownHostException e) { |
120 | | - return -1; |
| 136 | + inetAddress = InetAddress.getByAddress(addressBytes); |
| 137 | + } catch(UnknownHostException e) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + return inetAddress; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Add a default route through the specified gateway. |
| 146 | + * @param interfaceName interface on which the route should be added |
| 147 | + * @param gw the IP address of the gateway to which the route is desired, |
| 148 | + * @return {@code true} on success, {@code false} on failure |
| 149 | + */ |
| 150 | + public static boolean addDefaultRoute(String interfaceName, InetAddress gw) { |
| 151 | + String dstStr; |
| 152 | + String gwStr = gw.getHostAddress(); |
| 153 | + |
| 154 | + if (gw instanceof Inet4Address) { |
| 155 | + dstStr = "0.0.0.0"; |
| 156 | + } else if (gw instanceof Inet6Address) { |
| 157 | + dstStr = "::"; |
| 158 | + } else { |
| 159 | + Log.w(TAG, "addDefaultRoute failure: address is neither IPv4 nor IPv6" + |
| 160 | + "(" + gwStr + ")"); |
| 161 | + return false; |
| 162 | + } |
| 163 | + return addRoute(interfaceName, dstStr, 0, gwStr) == 0; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Add a host route. |
| 168 | + * @param interfaceName interface on which the route should be added |
| 169 | + * @param dst the IP address of the host to which the route is desired, |
| 170 | + * this should not be null. |
| 171 | + * @param gw the IP address of the gateway to which the route is desired, |
| 172 | + * if null, indicates a directly-connected route. |
| 173 | + * @return {@code true} on success, {@code false} on failure |
| 174 | + */ |
| 175 | + public static boolean addHostRoute(String interfaceName, InetAddress dst, |
| 176 | + InetAddress gw) { |
| 177 | + if (dst == null) { |
| 178 | + Log.w(TAG, "addHostRoute: dst should not be null"); |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + int prefixLength; |
| 183 | + String dstStr = dst.getHostAddress(); |
| 184 | + String gwStr = (gw != null) ? gw.getHostAddress() : null; |
| 185 | + |
| 186 | + if (dst instanceof Inet4Address) { |
| 187 | + prefixLength = 32; |
| 188 | + } else if (dst instanceof Inet6Address) { |
| 189 | + prefixLength = 128; |
| 190 | + } else { |
| 191 | + Log.w(TAG, "addHostRoute failure: address is neither IPv4 nor IPv6" + |
| 192 | + "(" + dst + ")"); |
| 193 | + return false; |
121 | 194 | } |
122 | | - byte[] addrBytes; |
123 | | - int addr; |
124 | | - addrBytes = inetAddress.getAddress(); |
125 | | - addr = ((addrBytes[3] & 0xff) << 24) |
126 | | - | ((addrBytes[2] & 0xff) << 16) |
127 | | - | ((addrBytes[1] & 0xff) << 8) |
128 | | - | (addrBytes[0] & 0xff); |
129 | | - return addr; |
| 195 | + return addRoute(interfaceName, dstStr, prefixLength, gwStr) == 0; |
130 | 196 | } |
131 | 197 | } |
0 commit comments