The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
| 19 | import java.net.InetAddress; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 20 | import java.net.Inet4Address; |
| 21 | import java.net.Inet6Address; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import java.net.UnknownHostException; |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 23 | import java.util.Collection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Native methods for managing network interfaces. |
| 29 | * |
| 30 | * {@hide} |
| 31 | */ |
| 32 | public class NetworkUtils { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 33 | |
| 34 | private static final String TAG = "NetworkUtils"; |
| 35 | |
Mike Lockwood | 0900f36 | 2009-07-10 17:24:07 -0400 | [diff] [blame] | 36 | /** Bring the named network interface up. */ |
| 37 | public native static int enableInterface(String interfaceName); |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | /** Bring the named network interface down. */ |
| 40 | public native static int disableInterface(String interfaceName); |
| 41 | |
Wink Saville | 8171e6f | 2011-07-07 16:17:06 -0700 | [diff] [blame] | 42 | /** Setting bit 0 indicates reseting of IPv4 addresses required */ |
| 43 | public static final int RESET_IPV4_ADDRESSES = 0x01; |
| 44 | |
| 45 | /** Setting bit 1 indicates reseting of IPv4 addresses required */ |
| 46 | public static final int RESET_IPV6_ADDRESSES = 0x02; |
| 47 | |
| 48 | /** Reset all addresses */ |
| 49 | public static final int RESET_ALL_ADDRESSES = RESET_IPV4_ADDRESSES | RESET_IPV6_ADDRESSES; |
| 50 | |
| 51 | /** |
| 52 | * Reset IPv6 or IPv4 sockets that are connected via the named interface. |
| 53 | * |
| 54 | * @param interfaceName is the interface to reset |
| 55 | * @param mask {@see #RESET_IPV4_ADDRESSES} and {@see #RESET_IPV6_ADDRESSES} |
| 56 | */ |
| 57 | public native static int resetConnections(String interfaceName, int mask); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Start the DHCP client daemon, in order to have it request addresses |
| 61 | * for the named interface, and then configure the interface with those |
| 62 | * addresses. This call blocks until it obtains a result (either success |
| 63 | * or failure) from the daemon. |
| 64 | * @param interfaceName the name of the interface to configure |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 65 | * @param dhcpResults if the request succeeds, this object is filled in with |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | * the IP address information. |
| 67 | * @return {@code true} for success, {@code false} for failure |
| 68 | */ |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 69 | public native static boolean runDhcp(String interfaceName, DhcpResults dhcpResults); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
Irfan Sheriff | 914ed90 | 2011-06-21 14:26:37 -0700 | [diff] [blame] | 72 | * Initiate renewal on the Dhcp client daemon. This call blocks until it obtains |
| 73 | * a result (either success or failure) from the daemon. |
| 74 | * @param interfaceName the name of the interface to configure |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 75 | * @param dhcpResults if the request succeeds, this object is filled in with |
Irfan Sheriff | 914ed90 | 2011-06-21 14:26:37 -0700 | [diff] [blame] | 76 | * the IP address information. |
| 77 | * @return {@code true} for success, {@code false} for failure |
| 78 | */ |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 79 | public native static boolean runDhcpRenew(String interfaceName, DhcpResults dhcpResults); |
Irfan Sheriff | 914ed90 | 2011-06-21 14:26:37 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | * Shut down the DHCP client daemon. |
| 83 | * @param interfaceName the name of the interface for which the daemon |
| 84 | * should be stopped |
| 85 | * @return {@code true} for success, {@code false} for failure |
| 86 | */ |
| 87 | public native static boolean stopDhcp(String interfaceName); |
| 88 | |
| 89 | /** |
| 90 | * Release the current DHCP lease. |
| 91 | * @param interfaceName the name of the interface for which the lease should |
| 92 | * be released |
| 93 | * @return {@code true} for success, {@code false} for failure |
| 94 | */ |
| 95 | public native static boolean releaseDhcpLease(String interfaceName); |
| 96 | |
| 97 | /** |
| 98 | * Return the last DHCP-related error message that was recorded. |
| 99 | * <p/>NOTE: This string is not localized, but currently it is only |
| 100 | * used in logging. |
| 101 | * @return the most recent error message, if any |
| 102 | */ |
| 103 | public native static String getDhcpError(); |
| 104 | |
| 105 | /** |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 106 | * Convert a IPv4 address from an integer to an InetAddress. |
Jesse Wilson | 07481cc | 2011-01-06 17:18:23 -0800 | [diff] [blame] | 107 | * @param hostAddress an int corresponding to the IPv4 address in network byte order |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 108 | */ |
| 109 | public static InetAddress intToInetAddress(int hostAddress) { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 110 | byte[] addressBytes = { (byte)(0xff & hostAddress), |
| 111 | (byte)(0xff & (hostAddress >> 8)), |
| 112 | (byte)(0xff & (hostAddress >> 16)), |
| 113 | (byte)(0xff & (hostAddress >> 24)) }; |
| 114 | |
| 115 | try { |
Jesse Wilson | 07481cc | 2011-01-06 17:18:23 -0800 | [diff] [blame] | 116 | return InetAddress.getByAddress(addressBytes); |
| 117 | } catch (UnknownHostException e) { |
| 118 | throw new AssertionError(); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 119 | } |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 122 | /** |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 123 | * Convert a IPv4 address from an InetAddress to an integer |
| 124 | * @param inetAddr is an InetAddress corresponding to the IPv4 address |
| 125 | * @return the IP address as an integer in network byte order |
| 126 | */ |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 127 | public static int inetAddressToInt(Inet4Address inetAddr) |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 128 | throws IllegalArgumentException { |
| 129 | byte [] addr = inetAddr.getAddress(); |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 130 | return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) | |
| 131 | ((addr[1] & 0xff) << 8) | (addr[0] & 0xff); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Convert a network prefix length to an IPv4 netmask integer |
| 136 | * @param prefixLength |
| 137 | * @return the IPv4 netmask as an integer in network byte order |
| 138 | */ |
| 139 | public static int prefixLengthToNetmaskInt(int prefixLength) |
| 140 | throws IllegalArgumentException { |
| 141 | if (prefixLength < 0 || prefixLength > 32) { |
| 142 | throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)"); |
| 143 | } |
| 144 | int value = 0xffffffff << (32 - prefixLength); |
| 145 | return Integer.reverseBytes(value); |
| 146 | } |
| 147 | |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 148 | /** |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 149 | * Convert a IPv4 netmask integer to a prefix length |
| 150 | * @param netmask as an integer in network byte order |
| 151 | * @return the network prefix length |
| 152 | */ |
| 153 | public static int netmaskIntToPrefixLength(int netmask) { |
| 154 | return Integer.bitCount(netmask); |
| 155 | } |
| 156 | |
| 157 | /** |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 158 | * Create an InetAddress from a string where the string must be a standard |
| 159 | * representation of a V4 or V6 address. Avoids doing a DNS lookup on failure |
| 160 | * but it will throw an IllegalArgumentException in that case. |
| 161 | * @param addrString |
| 162 | * @return the InetAddress |
| 163 | * @hide |
| 164 | */ |
| 165 | public static InetAddress numericToInetAddress(String addrString) |
| 166 | throws IllegalArgumentException { |
Elliott Hughes | f5bbb57 | 2011-02-15 17:11:29 -0800 | [diff] [blame] | 167 | return InetAddress.parseNumericAddress(addrString); |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 171 | * Get InetAddress masked with prefixLength. Will never return null. |
| 172 | * @param IP address which will be masked with specified prefixLength |
| 173 | * @param prefixLength the prefixLength used to mask the IP |
| 174 | */ |
| 175 | public static InetAddress getNetworkPart(InetAddress address, int prefixLength) { |
| 176 | if (address == null) { |
| 177 | throw new RuntimeException("getNetworkPart doesn't accept null address"); |
| 178 | } |
| 179 | |
| 180 | byte[] array = address.getAddress(); |
| 181 | |
| 182 | if (prefixLength < 0 || prefixLength > array.length * 8) { |
| 183 | throw new RuntimeException("getNetworkPart - bad prefixLength"); |
| 184 | } |
| 185 | |
| 186 | int offset = prefixLength / 8; |
| 187 | int reminder = prefixLength % 8; |
| 188 | byte mask = (byte)(0xFF << (8 - reminder)); |
| 189 | |
| 190 | if (offset < array.length) array[offset] = (byte)(array[offset] & mask); |
| 191 | |
| 192 | offset++; |
| 193 | |
| 194 | for (; offset < array.length; offset++) { |
| 195 | array[offset] = 0; |
| 196 | } |
| 197 | |
| 198 | InetAddress netPart = null; |
| 199 | try { |
| 200 | netPart = InetAddress.getByAddress(array); |
| 201 | } catch (UnknownHostException e) { |
| 202 | throw new RuntimeException("getNetworkPart error - " + e.toString()); |
| 203 | } |
| 204 | return netPart; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Check if IP address type is consistent between two InetAddress. |
| 209 | * @return true if both are the same type. False otherwise. |
| 210 | */ |
| 211 | public static boolean addressTypeMatches(InetAddress left, InetAddress right) { |
| 212 | return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) || |
| 213 | ((left instanceof Inet6Address) && (right instanceof Inet6Address))); |
| 214 | } |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * Convert a 32 char hex string into a Inet6Address. |
| 218 | * throws a runtime exception if the string isn't 32 chars, isn't hex or can't be |
| 219 | * made into an Inet6Address |
| 220 | * @param addrHexString a 32 character hex string representing an IPv6 addr |
| 221 | * @return addr an InetAddress representation for the string |
| 222 | */ |
| 223 | public static InetAddress hexToInet6Address(String addrHexString) |
| 224 | throws IllegalArgumentException { |
| 225 | try { |
| 226 | return numericToInetAddress(String.format("%s:%s:%s:%s:%s:%s:%s:%s", |
| 227 | addrHexString.substring(0,4), addrHexString.substring(4,8), |
| 228 | addrHexString.substring(8,12), addrHexString.substring(12,16), |
| 229 | addrHexString.substring(16,20), addrHexString.substring(20,24), |
| 230 | addrHexString.substring(24,28), addrHexString.substring(28,32))); |
| 231 | } catch (Exception e) { |
| 232 | Log.e("NetworkUtils", "error in hexToInet6Address(" + addrHexString + "): " + e); |
| 233 | throw new IllegalArgumentException(e); |
| 234 | } |
| 235 | } |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Create a string array of host addresses from a collection of InetAddresses |
| 239 | * @param addrs a Collection of InetAddresses |
| 240 | * @return an array of Strings containing their host addresses |
| 241 | */ |
| 242 | public static String[] makeStrings(Collection<InetAddress> addrs) { |
| 243 | String[] result = new String[addrs.size()]; |
| 244 | int i = 0; |
| 245 | for (InetAddress addr : addrs) { |
| 246 | result[i++] = addr.getHostAddress(); |
| 247 | } |
| 248 | return result; |
| 249 | } |
Robert Greenwalt | d4420ab | 2011-12-07 09:58:48 -0800 | [diff] [blame] | 250 | |
| 251 | /** |
| 252 | * Trim leading zeros from IPv4 address strings |
| 253 | * Our base libraries will interpret that as octel.. |
| 254 | * Must leave non v4 addresses and host names alone. |
| 255 | * For example, 192.168.000.010 -> 192.168.0.10 |
| 256 | * TODO - fix base libraries and remove this function |
| 257 | * @param addr a string representing an ip addr |
| 258 | * @return a string propertly trimmed |
| 259 | */ |
| 260 | public static String trimV4AddrZeros(String addr) { |
Robert Greenwalt | 0faacf0 | 2011-12-07 16:43:59 -0800 | [diff] [blame] | 261 | if (addr == null) return null; |
Robert Greenwalt | d4420ab | 2011-12-07 09:58:48 -0800 | [diff] [blame] | 262 | String[] octets = addr.split("\\."); |
| 263 | if (octets.length != 4) return addr; |
| 264 | StringBuilder builder = new StringBuilder(16); |
| 265 | String result = null; |
| 266 | for (int i = 0; i < 4; i++) { |
| 267 | try { |
Robert Greenwalt | 3957b5f | 2011-12-07 13:10:59 -0800 | [diff] [blame] | 268 | if (octets[i].length() > 3) return addr; |
Robert Greenwalt | d4420ab | 2011-12-07 09:58:48 -0800 | [diff] [blame] | 269 | builder.append(Integer.parseInt(octets[i])); |
| 270 | } catch (NumberFormatException e) { |
| 271 | return addr; |
| 272 | } |
| 273 | if (i < 3) builder.append('.'); |
| 274 | } |
| 275 | result = builder.toString(); |
| 276 | return result; |
| 277 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | } |