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; |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 24 | import java.util.Locale; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 26 | import android.util.Log; |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 27 | import android.util.Pair; |
| 28 | |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 29 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Native methods for managing network interfaces. |
| 32 | * |
| 33 | * {@hide} |
| 34 | */ |
| 35 | public class NetworkUtils { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 36 | |
| 37 | private static final String TAG = "NetworkUtils"; |
| 38 | |
Mike Lockwood | 0900f36 | 2009-07-10 17:24:07 -0400 | [diff] [blame] | 39 | /** Bring the named network interface up. */ |
| 40 | public native static int enableInterface(String interfaceName); |
| 41 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | /** Bring the named network interface down. */ |
| 43 | public native static int disableInterface(String interfaceName); |
| 44 | |
Wink Saville | 8171e6f | 2011-07-07 16:17:06 -0700 | [diff] [blame] | 45 | /** Setting bit 0 indicates reseting of IPv4 addresses required */ |
| 46 | public static final int RESET_IPV4_ADDRESSES = 0x01; |
| 47 | |
| 48 | /** Setting bit 1 indicates reseting of IPv4 addresses required */ |
| 49 | public static final int RESET_IPV6_ADDRESSES = 0x02; |
| 50 | |
| 51 | /** Reset all addresses */ |
| 52 | public static final int RESET_ALL_ADDRESSES = RESET_IPV4_ADDRESSES | RESET_IPV6_ADDRESSES; |
| 53 | |
| 54 | /** |
| 55 | * Reset IPv6 or IPv4 sockets that are connected via the named interface. |
| 56 | * |
| 57 | * @param interfaceName is the interface to reset |
| 58 | * @param mask {@see #RESET_IPV4_ADDRESSES} and {@see #RESET_IPV6_ADDRESSES} |
| 59 | */ |
| 60 | public native static int resetConnections(String interfaceName, int mask); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Start the DHCP client daemon, in order to have it request addresses |
| 64 | * for the named interface, and then configure the interface with those |
| 65 | * addresses. This call blocks until it obtains a result (either success |
| 66 | * or failure) from the daemon. |
| 67 | * @param interfaceName the name of the interface to configure |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 68 | * @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] | 69 | * the IP address information. |
| 70 | * @return {@code true} for success, {@code false} for failure |
| 71 | */ |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 72 | public native static boolean runDhcp(String interfaceName, DhcpResults dhcpResults); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
Irfan Sheriff | 914ed90 | 2011-06-21 14:26:37 -0700 | [diff] [blame] | 75 | * Initiate renewal on the Dhcp client daemon. This call blocks until it obtains |
| 76 | * a result (either success or failure) from the daemon. |
| 77 | * @param interfaceName the name of the interface to configure |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 78 | * @param dhcpResults if the request succeeds, this object is filled in with |
Irfan Sheriff | 914ed90 | 2011-06-21 14:26:37 -0700 | [diff] [blame] | 79 | * the IP address information. |
| 80 | * @return {@code true} for success, {@code false} for failure |
| 81 | */ |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 82 | public native static boolean runDhcpRenew(String interfaceName, DhcpResults dhcpResults); |
Irfan Sheriff | 914ed90 | 2011-06-21 14:26:37 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | * Shut down the DHCP client daemon. |
| 86 | * @param interfaceName the name of the interface for which the daemon |
| 87 | * should be stopped |
| 88 | * @return {@code true} for success, {@code false} for failure |
| 89 | */ |
| 90 | public native static boolean stopDhcp(String interfaceName); |
| 91 | |
| 92 | /** |
| 93 | * Release the current DHCP lease. |
| 94 | * @param interfaceName the name of the interface for which the lease should |
| 95 | * be released |
| 96 | * @return {@code true} for success, {@code false} for failure |
| 97 | */ |
| 98 | public native static boolean releaseDhcpLease(String interfaceName); |
| 99 | |
| 100 | /** |
| 101 | * Return the last DHCP-related error message that was recorded. |
| 102 | * <p/>NOTE: This string is not localized, but currently it is only |
| 103 | * used in logging. |
| 104 | * @return the most recent error message, if any |
| 105 | */ |
| 106 | public native static String getDhcpError(); |
| 107 | |
| 108 | /** |
Chad Brubaker | 12324b4 | 2013-07-11 13:30:36 -0700 | [diff] [blame] | 109 | * Set the SO_MARK of {@code socketfd} to {@code mark} |
| 110 | */ |
| 111 | public native static void markSocket(int socketfd, int mark); |
| 112 | |
| 113 | /** |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 114 | * Binds the current process to the network designated by {@code netId}. All sockets created |
| 115 | * 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] | 116 | * {@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] | 117 | * {@code Network} ever disconnects all sockets created in this way will cease to work. This |
| 118 | * is by design so an application doesn't accidentally use sockets it thinks are still bound to |
| 119 | * a particular {@code Network}. |
| 120 | */ |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame^] | 121 | public native static boolean bindProcessToNetwork(int netId); |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Clear any process specific {@code Network} binding. This reverts a call to |
| 125 | * {@link #bindProcessToNetwork}. |
| 126 | */ |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame^] | 127 | public native static boolean unbindProcessToNetwork(); |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Return the netId last passed to {@link #bindProcessToNetwork}, or NETID_UNSET if |
| 131 | * {@link #unbindProcessToNetwork} has been called since {@link #bindProcessToNetwork}. |
| 132 | */ |
| 133 | public native static int getNetworkBoundToProcess(); |
| 134 | |
| 135 | /** |
| 136 | * Binds host resolutions performed by this process to the network designated by {@code netId}. |
| 137 | * {@link #bindProcessToNetwork} takes precedence over this setting. |
| 138 | * |
| 139 | * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature(). |
| 140 | */ |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame^] | 141 | public native static boolean bindProcessToNetworkForHostResolution(int netId); |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Clears any process specific {@link Network} binding for host resolution. This does |
| 145 | * not clear bindings enacted via {@link #bindProcessToNetwork}. |
| 146 | * |
| 147 | * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature(). |
| 148 | */ |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame^] | 149 | public native static boolean unbindProcessToNetworkForHostResolution(); |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * Explicitly binds {@code socketfd} to the network designated by {@code netId}. This |
| 153 | * overrides any binding via {@link #bindProcessToNetwork}. |
| 154 | */ |
Paul Jensen | 32a58f0 | 2014-06-20 13:58:14 -0400 | [diff] [blame^] | 155 | public native static boolean bindSocketToNetwork(int socketfd, int netId); |
Paul Jensen | 3876495 | 2014-05-20 11:25:35 -0400 | [diff] [blame] | 156 | |
| 157 | /** |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 158 | * Convert a IPv4 address from an integer to an InetAddress. |
Jesse Wilson | 07481cc | 2011-01-06 17:18:23 -0800 | [diff] [blame] | 159 | * @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] | 160 | */ |
| 161 | public static InetAddress intToInetAddress(int hostAddress) { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 162 | byte[] addressBytes = { (byte)(0xff & hostAddress), |
| 163 | (byte)(0xff & (hostAddress >> 8)), |
| 164 | (byte)(0xff & (hostAddress >> 16)), |
| 165 | (byte)(0xff & (hostAddress >> 24)) }; |
| 166 | |
| 167 | try { |
Jesse Wilson | 07481cc | 2011-01-06 17:18:23 -0800 | [diff] [blame] | 168 | return InetAddress.getByAddress(addressBytes); |
| 169 | } catch (UnknownHostException e) { |
| 170 | throw new AssertionError(); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 171 | } |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 174 | /** |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 175 | * Convert a IPv4 address from an InetAddress to an integer |
| 176 | * @param inetAddr is an InetAddress corresponding to the IPv4 address |
| 177 | * @return the IP address as an integer in network byte order |
| 178 | */ |
Robert Greenwalt | 4717c26 | 2012-10-31 14:32:53 -0700 | [diff] [blame] | 179 | public static int inetAddressToInt(Inet4Address inetAddr) |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 180 | throws IllegalArgumentException { |
| 181 | byte [] addr = inetAddr.getAddress(); |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 182 | return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) | |
| 183 | ((addr[1] & 0xff) << 8) | (addr[0] & 0xff); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Convert a network prefix length to an IPv4 netmask integer |
| 188 | * @param prefixLength |
| 189 | * @return the IPv4 netmask as an integer in network byte order |
| 190 | */ |
| 191 | public static int prefixLengthToNetmaskInt(int prefixLength) |
| 192 | throws IllegalArgumentException { |
| 193 | if (prefixLength < 0 || prefixLength > 32) { |
| 194 | throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)"); |
| 195 | } |
| 196 | int value = 0xffffffff << (32 - prefixLength); |
| 197 | return Integer.reverseBytes(value); |
| 198 | } |
| 199 | |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 200 | /** |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 201 | * Convert a IPv4 netmask integer to a prefix length |
| 202 | * @param netmask as an integer in network byte order |
| 203 | * @return the network prefix length |
| 204 | */ |
| 205 | public static int netmaskIntToPrefixLength(int netmask) { |
| 206 | return Integer.bitCount(netmask); |
| 207 | } |
| 208 | |
| 209 | /** |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 210 | * Create an InetAddress from a string where the string must be a standard |
| 211 | * representation of a V4 or V6 address. Avoids doing a DNS lookup on failure |
| 212 | * but it will throw an IllegalArgumentException in that case. |
| 213 | * @param addrString |
| 214 | * @return the InetAddress |
| 215 | * @hide |
| 216 | */ |
| 217 | public static InetAddress numericToInetAddress(String addrString) |
| 218 | throws IllegalArgumentException { |
Elliott Hughes | f5bbb57 | 2011-02-15 17:11:29 -0800 | [diff] [blame] | 219 | return InetAddress.parseNumericAddress(addrString); |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 223 | * Masks a raw IP address byte array with the specified prefix length. |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 224 | */ |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 225 | public static void maskRawAddress(byte[] array, int prefixLength) { |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 226 | if (prefixLength < 0 || prefixLength > array.length * 8) { |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 227 | throw new RuntimeException("IP address with " + array.length + |
| 228 | " bytes has invalid prefix length " + prefixLength); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int offset = prefixLength / 8; |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 232 | int remainder = prefixLength % 8; |
| 233 | byte mask = (byte)(0xFF << (8 - remainder)); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 234 | |
| 235 | if (offset < array.length) array[offset] = (byte)(array[offset] & mask); |
| 236 | |
| 237 | offset++; |
| 238 | |
| 239 | for (; offset < array.length; offset++) { |
| 240 | array[offset] = 0; |
| 241 | } |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Get InetAddress masked with prefixLength. Will never return null. |
| 246 | * @param address the IP address to mask with |
| 247 | * @param prefixLength the prefixLength used to mask the IP |
| 248 | */ |
| 249 | public static InetAddress getNetworkPart(InetAddress address, int prefixLength) { |
| 250 | byte[] array = address.getAddress(); |
| 251 | maskRawAddress(array, prefixLength); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 252 | |
| 253 | InetAddress netPart = null; |
| 254 | try { |
| 255 | netPart = InetAddress.getByAddress(array); |
| 256 | } catch (UnknownHostException e) { |
| 257 | throw new RuntimeException("getNetworkPart error - " + e.toString()); |
| 258 | } |
| 259 | return netPart; |
| 260 | } |
| 261 | |
| 262 | /** |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 263 | * Utility method to parse strings such as "192.0.2.5/24" or "2001:db8::cafe:d00d/64". |
| 264 | * @hide |
| 265 | */ |
| 266 | public static Pair<InetAddress, Integer> parseIpAndMask(String ipAndMaskString) { |
| 267 | InetAddress address = null; |
| 268 | int prefixLength = -1; |
| 269 | try { |
| 270 | String[] pieces = ipAndMaskString.split("/", 2); |
| 271 | prefixLength = Integer.parseInt(pieces[1]); |
| 272 | address = InetAddress.parseNumericAddress(pieces[0]); |
| 273 | } catch (NullPointerException e) { // Null string. |
| 274 | } catch (ArrayIndexOutOfBoundsException e) { // No prefix length. |
| 275 | } catch (NumberFormatException e) { // Non-numeric prefix. |
| 276 | } catch (IllegalArgumentException e) { // Invalid IP address. |
| 277 | } |
| 278 | |
| 279 | if (address == null || prefixLength == -1) { |
| 280 | throw new IllegalArgumentException("Invalid IP address and mask " + ipAndMaskString); |
| 281 | } |
| 282 | |
| 283 | return new Pair<InetAddress, Integer>(address, prefixLength); |
| 284 | } |
| 285 | |
| 286 | /** |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 287 | * Check if IP address type is consistent between two InetAddress. |
| 288 | * @return true if both are the same type. False otherwise. |
| 289 | */ |
| 290 | public static boolean addressTypeMatches(InetAddress left, InetAddress right) { |
| 291 | return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) || |
| 292 | ((left instanceof Inet6Address) && (right instanceof Inet6Address))); |
| 293 | } |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Convert a 32 char hex string into a Inet6Address. |
| 297 | * throws a runtime exception if the string isn't 32 chars, isn't hex or can't be |
| 298 | * made into an Inet6Address |
| 299 | * @param addrHexString a 32 character hex string representing an IPv6 addr |
| 300 | * @return addr an InetAddress representation for the string |
| 301 | */ |
| 302 | public static InetAddress hexToInet6Address(String addrHexString) |
| 303 | throws IllegalArgumentException { |
| 304 | try { |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 305 | 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] | 306 | addrHexString.substring(0,4), addrHexString.substring(4,8), |
| 307 | addrHexString.substring(8,12), addrHexString.substring(12,16), |
| 308 | addrHexString.substring(16,20), addrHexString.substring(20,24), |
| 309 | addrHexString.substring(24,28), addrHexString.substring(28,32))); |
| 310 | } catch (Exception e) { |
| 311 | Log.e("NetworkUtils", "error in hexToInet6Address(" + addrHexString + "): " + e); |
| 312 | throw new IllegalArgumentException(e); |
| 313 | } |
| 314 | } |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * Create a string array of host addresses from a collection of InetAddresses |
| 318 | * @param addrs a Collection of InetAddresses |
| 319 | * @return an array of Strings containing their host addresses |
| 320 | */ |
| 321 | public static String[] makeStrings(Collection<InetAddress> addrs) { |
| 322 | String[] result = new String[addrs.size()]; |
| 323 | int i = 0; |
| 324 | for (InetAddress addr : addrs) { |
| 325 | result[i++] = addr.getHostAddress(); |
| 326 | } |
| 327 | return result; |
| 328 | } |
Robert Greenwalt | d4420ab | 2011-12-07 09:58:48 -0800 | [diff] [blame] | 329 | |
| 330 | /** |
| 331 | * Trim leading zeros from IPv4 address strings |
| 332 | * Our base libraries will interpret that as octel.. |
| 333 | * Must leave non v4 addresses and host names alone. |
| 334 | * For example, 192.168.000.010 -> 192.168.0.10 |
| 335 | * TODO - fix base libraries and remove this function |
| 336 | * @param addr a string representing an ip addr |
| 337 | * @return a string propertly trimmed |
| 338 | */ |
| 339 | public static String trimV4AddrZeros(String addr) { |
Robert Greenwalt | 0faacf0 | 2011-12-07 16:43:59 -0800 | [diff] [blame] | 340 | if (addr == null) return null; |
Robert Greenwalt | d4420ab | 2011-12-07 09:58:48 -0800 | [diff] [blame] | 341 | String[] octets = addr.split("\\."); |
| 342 | if (octets.length != 4) return addr; |
| 343 | StringBuilder builder = new StringBuilder(16); |
| 344 | String result = null; |
| 345 | for (int i = 0; i < 4; i++) { |
| 346 | try { |
Robert Greenwalt | 3957b5f | 2011-12-07 13:10:59 -0800 | [diff] [blame] | 347 | if (octets[i].length() > 3) return addr; |
Robert Greenwalt | d4420ab | 2011-12-07 09:58:48 -0800 | [diff] [blame] | 348 | builder.append(Integer.parseInt(octets[i])); |
| 349 | } catch (NumberFormatException e) { |
| 350 | return addr; |
| 351 | } |
| 352 | if (i < 3) builder.append('.'); |
| 353 | } |
| 354 | result = builder.toString(); |
| 355 | return result; |
| 356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | } |