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 | |
Lorenzo Colitti | 566e0cb | 2015-03-06 19:57:39 +0900 | [diff] [blame] | 19 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import java.net.InetAddress; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 21 | import java.net.Inet4Address; |
| 22 | import java.net.Inet6Address; |
Lorenzo Colitti | 566e0cb | 2015-03-06 19:57:39 +0900 | [diff] [blame] | 23 | import java.net.SocketException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import java.net.UnknownHostException; |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 25 | import java.util.Collection; |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 26 | import java.util.Locale; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 28 | import android.os.Parcel; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 30 | import android.util.Pair; |
| 31 | |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Native methods for managing network interfaces. |
| 35 | * |
| 36 | * {@hide} |
| 37 | */ |
| 38 | public class NetworkUtils { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 39 | |
| 40 | private static final String TAG = "NetworkUtils"; |
| 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 | /** |
Lorenzo Colitti | 566e0cb | 2015-03-06 19:57:39 +0900 | [diff] [blame] | 60 | * Attaches a socket filter that accepts DHCP packets to the given socket. |
| 61 | */ |
| 62 | public native static void attachDhcpFilter(FileDescriptor fd) throws SocketException; |
| 63 | |
| 64 | /** |
Paul Jensen | 578a76e | 2016-01-14 14:54:39 -0500 | [diff] [blame] | 65 | * Attaches a socket filter that accepts ICMP6 router advertisement packets to the given socket. |
| 66 | * @param fd the socket's {@link FileDescriptor}. |
| 67 | * @param packetType the hardware address type, one of ARPHRD_*. |
| 68 | */ |
| 69 | public native static void attachRaFilter(FileDescriptor fd, int packetType) throws SocketException; |
| 70 | |
| 71 | /** |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 72 | * Binds the current process to the network designated by {@code netId}. All sockets created |
| 73 | * in the future (and not explicitly bound via a bound {@link SocketFactory} (see |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 74 | * {@link Network#getSocketFactory}) will be bound to this network. Note that if this |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 75 | * {@code Network} ever disconnects all sockets created in this way will cease to work. This |
| 76 | * is by design so an application doesn't accidentally use sockets it thinks are still bound to |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 77 | * a particular {@code Network}. Passing NETID_UNSET clears the binding. |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 78 | */ |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame] | 79 | public native static boolean bindProcessToNetwork(int netId); |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 80 | |
| 81 | /** |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 82 | * Return the netId last passed to {@link #bindProcessToNetwork}, or NETID_UNSET if |
| 83 | * {@link #unbindProcessToNetwork} has been called since {@link #bindProcessToNetwork}. |
| 84 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 85 | public native static int getBoundNetworkForProcess(); |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Binds host resolutions performed by this process to the network designated by {@code netId}. |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 89 | * {@link #bindProcessToNetwork} takes precedence over this setting. Passing NETID_UNSET clears |
| 90 | * the binding. |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 91 | * |
| 92 | * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature(). |
| 93 | */ |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame] | 94 | public native static boolean bindProcessToNetworkForHostResolution(int netId); |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 95 | |
| 96 | /** |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 97 | * Explicitly binds {@code socketfd} to the network designated by {@code netId}. This |
| 98 | * overrides any binding via {@link #bindProcessToNetwork}. |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 99 | * @return 0 on success or negative errno on failure. |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 100 | */ |
Lorenzo Colitti | 9f1274b | 2014-08-21 11:45:54 -0700 | [diff] [blame] | 101 | public native static int bindSocketToNetwork(int socketfd, int netId); |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 102 | |
| 103 | /** |
Lorenzo Colitti | 4ff9f0f | 2015-03-17 17:56:10 +0900 | [diff] [blame] | 104 | * Protect {@code fd} from VPN connections. After protecting, data sent through |
| 105 | * this socket will go directly to the underlying network, so its traffic will not be |
| 106 | * forwarded through the VPN. |
| 107 | */ |
| 108 | public static boolean protectFromVpn(FileDescriptor fd) { |
| 109 | return protectFromVpn(fd.getInt$()); |
| 110 | } |
| 111 | |
| 112 | /** |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 113 | * Protect {@code socketfd} from VPN connections. After protecting, data sent through |
| 114 | * this socket will go directly to the underlying network, so its traffic will not be |
| 115 | * forwarded through the VPN. |
| 116 | */ |
| 117 | public native static boolean protectFromVpn(int socketfd); |
| 118 | |
| 119 | /** |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 120 | * Determine if {@code uid} can access network designated by {@code netId}. |
| 121 | * @return {@code true} if {@code uid} can access network, {@code false} otherwise. |
| 122 | */ |
| 123 | public native static boolean queryUserAccess(int uid, int netId); |
| 124 | |
| 125 | /** |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 126 | * Convert a IPv4 address from an integer to an InetAddress. |
Jesse Wilson | 07481cc | 2011-01-06 17:18:23 -0800 | [diff] [blame] | 127 | * @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] | 128 | */ |
| 129 | public static InetAddress intToInetAddress(int hostAddress) { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 130 | byte[] addressBytes = { (byte)(0xff & hostAddress), |
| 131 | (byte)(0xff & (hostAddress >> 8)), |
| 132 | (byte)(0xff & (hostAddress >> 16)), |
| 133 | (byte)(0xff & (hostAddress >> 24)) }; |
| 134 | |
| 135 | try { |
Jesse Wilson | 07481cc | 2011-01-06 17:18:23 -0800 | [diff] [blame] | 136 | return InetAddress.getByAddress(addressBytes); |
| 137 | } catch (UnknownHostException e) { |
| 138 | throw new AssertionError(); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 139 | } |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 142 | /** |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 143 | * Convert a IPv4 address from an InetAddress to an integer |
| 144 | * @param inetAddr is an InetAddress corresponding to the IPv4 address |
| 145 | * @return the IP address as an integer in network byte order |
| 146 | */ |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 147 | public static int inetAddressToInt(Inet4Address inetAddr) |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 148 | throws IllegalArgumentException { |
| 149 | byte [] addr = inetAddr.getAddress(); |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 150 | return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) | |
| 151 | ((addr[1] & 0xff) << 8) | (addr[0] & 0xff); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Convert a network prefix length to an IPv4 netmask integer |
| 156 | * @param prefixLength |
| 157 | * @return the IPv4 netmask as an integer in network byte order |
| 158 | */ |
| 159 | public static int prefixLengthToNetmaskInt(int prefixLength) |
| 160 | throws IllegalArgumentException { |
| 161 | if (prefixLength < 0 || prefixLength > 32) { |
| 162 | throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)"); |
| 163 | } |
| 164 | int value = 0xffffffff << (32 - prefixLength); |
| 165 | return Integer.reverseBytes(value); |
| 166 | } |
| 167 | |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 168 | /** |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 169 | * Convert a IPv4 netmask integer to a prefix length |
| 170 | * @param netmask as an integer in network byte order |
| 171 | * @return the network prefix length |
| 172 | */ |
| 173 | public static int netmaskIntToPrefixLength(int netmask) { |
| 174 | return Integer.bitCount(netmask); |
| 175 | } |
| 176 | |
| 177 | /** |
Lorenzo Colitti | 475085b | 2015-03-10 01:32:40 +0900 | [diff] [blame] | 178 | * Convert an IPv4 netmask to a prefix length, checking that the netmask is contiguous. |
| 179 | * @param netmask as a {@code Inet4Address}. |
| 180 | * @return the network prefix length |
| 181 | * @throws IllegalArgumentException the specified netmask was not contiguous. |
| 182 | * @hide |
| 183 | */ |
| 184 | public static int netmaskToPrefixLength(Inet4Address netmask) { |
| 185 | // inetAddressToInt returns an int in *network* byte order. |
| 186 | int i = Integer.reverseBytes(inetAddressToInt(netmask)); |
| 187 | int prefixLength = Integer.bitCount(i); |
| 188 | int trailingZeros = Integer.numberOfTrailingZeros(i); |
| 189 | if (trailingZeros != 32 - prefixLength) { |
| 190 | throw new IllegalArgumentException("Non-contiguous netmask: " + Integer.toHexString(i)); |
| 191 | } |
| 192 | return prefixLength; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /** |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 197 | * Create an InetAddress from a string where the string must be a standard |
| 198 | * representation of a V4 or V6 address. Avoids doing a DNS lookup on failure |
| 199 | * but it will throw an IllegalArgumentException in that case. |
| 200 | * @param addrString |
| 201 | * @return the InetAddress |
| 202 | * @hide |
| 203 | */ |
| 204 | public static InetAddress numericToInetAddress(String addrString) |
| 205 | throws IllegalArgumentException { |
Elliott Hughes | f5bbb57 | 2011-02-15 17:11:29 -0800 | [diff] [blame] | 206 | return InetAddress.parseNumericAddress(addrString); |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 210 | * Writes an InetAddress to a parcel. The address may be null. This is likely faster than |
| 211 | * calling writeSerializable. |
| 212 | */ |
| 213 | protected static void parcelInetAddress(Parcel parcel, InetAddress address, int flags) { |
| 214 | byte[] addressArray = (address != null) ? address.getAddress() : null; |
| 215 | parcel.writeByteArray(addressArray); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Reads an InetAddress from a parcel. Returns null if the address that was written was null |
| 220 | * or if the data is invalid. |
| 221 | */ |
| 222 | protected static InetAddress unparcelInetAddress(Parcel in) { |
| 223 | byte[] addressArray = in.createByteArray(); |
| 224 | if (addressArray == null) { |
| 225 | return null; |
| 226 | } |
| 227 | try { |
| 228 | return InetAddress.getByAddress(addressArray); |
| 229 | } catch (UnknownHostException e) { |
| 230 | return null; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 236 | * Masks a raw IP address byte array with the specified prefix length. |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 237 | */ |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 238 | public static void maskRawAddress(byte[] array, int prefixLength) { |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 239 | if (prefixLength < 0 || prefixLength > array.length * 8) { |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 240 | throw new RuntimeException("IP address with " + array.length + |
| 241 | " bytes has invalid prefix length " + prefixLength); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | int offset = prefixLength / 8; |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 245 | int remainder = prefixLength % 8; |
| 246 | byte mask = (byte)(0xFF << (8 - remainder)); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 247 | |
| 248 | if (offset < array.length) array[offset] = (byte)(array[offset] & mask); |
| 249 | |
| 250 | offset++; |
| 251 | |
| 252 | for (; offset < array.length; offset++) { |
| 253 | array[offset] = 0; |
| 254 | } |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get InetAddress masked with prefixLength. Will never return null. |
| 259 | * @param address the IP address to mask with |
| 260 | * @param prefixLength the prefixLength used to mask the IP |
| 261 | */ |
| 262 | public static InetAddress getNetworkPart(InetAddress address, int prefixLength) { |
| 263 | byte[] array = address.getAddress(); |
| 264 | maskRawAddress(array, prefixLength); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 265 | |
| 266 | InetAddress netPart = null; |
| 267 | try { |
| 268 | netPart = InetAddress.getByAddress(array); |
| 269 | } catch (UnknownHostException e) { |
| 270 | throw new RuntimeException("getNetworkPart error - " + e.toString()); |
| 271 | } |
| 272 | return netPart; |
| 273 | } |
| 274 | |
| 275 | /** |
Lorenzo Colitti | 475085b | 2015-03-10 01:32:40 +0900 | [diff] [blame] | 276 | * Returns the implicit netmask of an IPv4 address, as was the custom before 1993. |
| 277 | */ |
| 278 | public static int getImplicitNetmask(Inet4Address address) { |
| 279 | int firstByte = address.getAddress()[0] & 0xff; // Convert to an unsigned value. |
| 280 | if (firstByte < 128) { |
| 281 | return 8; |
| 282 | } else if (firstByte < 192) { |
| 283 | return 16; |
| 284 | } else if (firstByte < 224) { |
| 285 | return 24; |
| 286 | } else { |
| 287 | return 32; // Will likely not end well for other reasons. |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 292 | * Utility method to parse strings such as "192.0.2.5/24" or "2001:db8::cafe:d00d/64". |
| 293 | * @hide |
| 294 | */ |
| 295 | public static Pair<InetAddress, Integer> parseIpAndMask(String ipAndMaskString) { |
| 296 | InetAddress address = null; |
| 297 | int prefixLength = -1; |
| 298 | try { |
| 299 | String[] pieces = ipAndMaskString.split("/", 2); |
| 300 | prefixLength = Integer.parseInt(pieces[1]); |
| 301 | address = InetAddress.parseNumericAddress(pieces[0]); |
| 302 | } catch (NullPointerException e) { // Null string. |
| 303 | } catch (ArrayIndexOutOfBoundsException e) { // No prefix length. |
| 304 | } catch (NumberFormatException e) { // Non-numeric prefix. |
| 305 | } catch (IllegalArgumentException e) { // Invalid IP address. |
| 306 | } |
| 307 | |
| 308 | if (address == null || prefixLength == -1) { |
| 309 | throw new IllegalArgumentException("Invalid IP address and mask " + ipAndMaskString); |
| 310 | } |
| 311 | |
| 312 | return new Pair<InetAddress, Integer>(address, prefixLength); |
| 313 | } |
| 314 | |
| 315 | /** |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 316 | * Check if IP address type is consistent between two InetAddress. |
| 317 | * @return true if both are the same type. False otherwise. |
| 318 | */ |
| 319 | public static boolean addressTypeMatches(InetAddress left, InetAddress right) { |
| 320 | return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) || |
| 321 | ((left instanceof Inet6Address) && (right instanceof Inet6Address))); |
| 322 | } |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 323 | |
| 324 | /** |
| 325 | * Convert a 32 char hex string into a Inet6Address. |
| 326 | * throws a runtime exception if the string isn't 32 chars, isn't hex or can't be |
| 327 | * made into an Inet6Address |
| 328 | * @param addrHexString a 32 character hex string representing an IPv6 addr |
| 329 | * @return addr an InetAddress representation for the string |
| 330 | */ |
| 331 | public static InetAddress hexToInet6Address(String addrHexString) |
| 332 | throws IllegalArgumentException { |
| 333 | try { |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 334 | return numericToInetAddress(String.format(Locale.US, "%s:%s:%s:%s:%s:%s:%s:%s", |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 335 | addrHexString.substring(0,4), addrHexString.substring(4,8), |
| 336 | addrHexString.substring(8,12), addrHexString.substring(12,16), |
| 337 | addrHexString.substring(16,20), addrHexString.substring(20,24), |
| 338 | addrHexString.substring(24,28), addrHexString.substring(28,32))); |
| 339 | } catch (Exception e) { |
| 340 | Log.e("NetworkUtils", "error in hexToInet6Address(" + addrHexString + "): " + e); |
| 341 | throw new IllegalArgumentException(e); |
| 342 | } |
| 343 | } |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 344 | |
| 345 | /** |
| 346 | * Create a string array of host addresses from a collection of InetAddresses |
| 347 | * @param addrs a Collection of InetAddresses |
| 348 | * @return an array of Strings containing their host addresses |
| 349 | */ |
| 350 | public static String[] makeStrings(Collection<InetAddress> addrs) { |
| 351 | String[] result = new String[addrs.size()]; |
| 352 | int i = 0; |
| 353 | for (InetAddress addr : addrs) { |
| 354 | result[i++] = addr.getHostAddress(); |
| 355 | } |
| 356 | return result; |
| 357 | } |
Robert Greenwalt | d4420ab | 2011-12-07 09:58:48 -0800 | [diff] [blame] | 358 | |
| 359 | /** |
| 360 | * Trim leading zeros from IPv4 address strings |
| 361 | * Our base libraries will interpret that as octel.. |
| 362 | * Must leave non v4 addresses and host names alone. |
| 363 | * For example, 192.168.000.010 -> 192.168.0.10 |
| 364 | * TODO - fix base libraries and remove this function |
| 365 | * @param addr a string representing an ip addr |
| 366 | * @return a string propertly trimmed |
| 367 | */ |
| 368 | public static String trimV4AddrZeros(String addr) { |
Robert Greenwalt | 0faacf0 | 2011-12-07 16:43:59 -0800 | [diff] [blame] | 369 | if (addr == null) return null; |
Robert Greenwalt | d4420ab | 2011-12-07 09:58:48 -0800 | [diff] [blame] | 370 | String[] octets = addr.split("\\."); |
| 371 | if (octets.length != 4) return addr; |
| 372 | StringBuilder builder = new StringBuilder(16); |
| 373 | String result = null; |
| 374 | for (int i = 0; i < 4; i++) { |
| 375 | try { |
Robert Greenwalt | 3957b5f | 2011-12-07 13:10:59 -0800 | [diff] [blame] | 376 | if (octets[i].length() > 3) return addr; |
Robert Greenwalt | d4420ab | 2011-12-07 09:58:48 -0800 | [diff] [blame] | 377 | builder.append(Integer.parseInt(octets[i])); |
| 378 | } catch (NumberFormatException e) { |
| 379 | return addr; |
| 380 | } |
| 381 | if (i < 3) builder.append('.'); |
| 382 | } |
| 383 | result = builder.toString(); |
| 384 | return result; |
| 385 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | } |