Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Hugo Benichi | fd31b9d | 2017-06-23 10:07:08 +0900 | [diff] [blame] | 19 | import static android.system.OsConstants.IFA_F_DADFAILED; |
| 20 | import static android.system.OsConstants.IFA_F_DEPRECATED; |
| 21 | import static android.system.OsConstants.IFA_F_OPTIMISTIC; |
| 22 | import static android.system.OsConstants.IFA_F_TENTATIVE; |
| 23 | import static android.system.OsConstants.RT_SCOPE_HOST; |
| 24 | import static android.system.OsConstants.RT_SCOPE_LINK; |
| 25 | import static android.system.OsConstants.RT_SCOPE_SITE; |
| 26 | import static android.system.OsConstants.RT_SCOPE_UNIVERSE; |
| 27 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 30 | import android.util.Pair; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 31 | |
Robert Greenwalt | b979f79 | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 32 | import java.net.Inet4Address; |
Erik Kline | befe778 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 33 | import java.net.Inet6Address; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 34 | import java.net.InetAddress; |
| 35 | import java.net.InterfaceAddress; |
| 36 | import java.net.UnknownHostException; |
| 37 | |
| 38 | /** |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 39 | * Identifies an IP address on a network link. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 40 | * |
| 41 | * A {@code LinkAddress} consists of: |
| 42 | * <ul> |
| 43 | * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}). |
| 44 | * The address must be unicast, as multicast addresses cannot be assigned to interfaces. |
Lorenzo Colitti | 7dc78cf | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 45 | * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties |
| 46 | * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}). |
| 47 | * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which |
| 48 | * the address is unique (e.g., |
| 49 | * {@code android.system.OsConstants.RT_SCOPE_LINK} or |
| 50 | * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}). |
| 51 | * </ul> |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 52 | */ |
| 53 | public class LinkAddress implements Parcelable { |
| 54 | /** |
| 55 | * IPv4 or IPv6 address. |
| 56 | */ |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 57 | private InetAddress address; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 60 | * Prefix length. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 61 | */ |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 62 | private int prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 63 | |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 64 | /** |
| 65 | * Address flags. A bitmask of IFA_F_* values. |
| 66 | */ |
| 67 | private int flags; |
| 68 | |
| 69 | /** |
| 70 | * Address scope. One of the RT_SCOPE_* constants. |
| 71 | */ |
| 72 | private int scope; |
| 73 | |
| 74 | /** |
| 75 | * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and |
| 76 | * RFC 6724 section 3.2. |
| 77 | * @hide |
| 78 | */ |
Hugo Benichi | 1dfb6b6 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 79 | private static int scopeForUnicastAddress(InetAddress addr) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 80 | if (addr.isAnyLocalAddress()) { |
| 81 | return RT_SCOPE_HOST; |
| 82 | } |
| 83 | |
| 84 | if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) { |
| 85 | return RT_SCOPE_LINK; |
| 86 | } |
| 87 | |
| 88 | // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2 |
| 89 | // says that they are assigned global scope. |
| 90 | if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) { |
| 91 | return RT_SCOPE_SITE; |
| 92 | } |
| 93 | |
| 94 | return RT_SCOPE_UNIVERSE; |
| 95 | } |
| 96 | |
| 97 | /** |
Erik Kline | befe778 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 98 | * Utility function to check if |address| is a Unique Local IPv6 Unicast Address |
| 99 | * (a.k.a. "ULA"; RFC 4193). |
| 100 | * |
| 101 | * Per RFC 4193 section 8, fc00::/7 identifies these addresses. |
| 102 | */ |
| 103 | private boolean isIPv6ULA() { |
Hugo Benichi | 1dfb6b6 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 104 | if (isIPv6()) { |
Erik Kline | befe778 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 105 | byte[] bytes = address.getAddress(); |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 106 | return ((bytes[0] & (byte)0xfe) == (byte)0xfc); |
Erik Kline | befe778 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | /** |
Hugo Benichi | 1dfb6b6 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 112 | * @return true if the address is IPv6. |
| 113 | * @hide |
| 114 | */ |
| 115 | public boolean isIPv6() { |
| 116 | return address instanceof Inet6Address; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @return true if the address is IPv4 or is a mapped IPv4 address. |
| 121 | * @hide |
| 122 | */ |
| 123 | public boolean isIPv4() { |
| 124 | return address instanceof Inet4Address; |
| 125 | } |
| 126 | |
| 127 | /** |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 128 | * Utility function for the constructors. |
| 129 | */ |
| 130 | private void init(InetAddress address, int prefixLength, int flags, int scope) { |
| 131 | if (address == null || |
| 132 | address.isMulticastAddress() || |
| 133 | prefixLength < 0 || |
Hugo Benichi | 1dfb6b6 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 134 | (address instanceof Inet4Address && prefixLength > 32) || |
Robert Greenwalt | b979f79 | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 135 | (prefixLength > 128)) { |
| 136 | throw new IllegalArgumentException("Bad LinkAddress params " + address + |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 137 | "/" + prefixLength); |
Robert Greenwalt | b979f79 | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 138 | } |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 139 | this.address = address; |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 140 | this.prefixLength = prefixLength; |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 141 | this.flags = flags; |
| 142 | this.scope = scope; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with |
| 147 | * the specified flags and scope. Flags and scope are not checked for validity. |
| 148 | * @param address The IP address. |
| 149 | * @param prefixLength The prefix length. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 150 | * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address. |
| 151 | * @param scope An integer defining the scope in which the address is unique (e.g., |
| 152 | * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}). |
| 153 | * @hide |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 154 | */ |
| 155 | public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) { |
| 156 | init(address, prefixLength, flags, scope); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 159 | /** |
| 160 | * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 161 | * The flags are set to zero and the scope is determined from the address. |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 162 | * @param address The IP address. |
| 163 | * @param prefixLength The prefix length. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 164 | * @hide |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 165 | */ |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 166 | public LinkAddress(InetAddress address, int prefixLength) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 167 | this(address, prefixLength, 0, 0); |
| 168 | this.scope = scopeForUnicastAddress(address); |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 169 | } |
| 170 | |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 171 | /** |
| 172 | * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 173 | * The flags are set to zero and the scope is determined from the address. |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 174 | * @param interfaceAddress The interface address. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 175 | * @hide |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 176 | */ |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 177 | public LinkAddress(InterfaceAddress interfaceAddress) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 178 | this(interfaceAddress.getAddress(), |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 179 | interfaceAddress.getNetworkPrefixLength()); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 184 | * "2001:db8::1/64". The flags are set to zero and the scope is determined from the address. |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 185 | * @param string The string to parse. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 186 | * @hide |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 187 | */ |
| 188 | public LinkAddress(String address) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 189 | this(address, 0, 0); |
| 190 | this.scope = scopeForUnicastAddress(this.address); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or |
| 195 | * "2001:db8::1/64", with the specified flags and scope. |
| 196 | * @param string The string to parse. |
| 197 | * @param flags The address flags. |
| 198 | * @param scope The address scope. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 199 | * @hide |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 200 | */ |
| 201 | public LinkAddress(String address, int flags, int scope) { |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 202 | // This may throw an IllegalArgumentException; catching it is the caller's responsibility. |
Hugo Benichi | 1dfb6b6 | 2017-08-08 13:06:04 +0900 | [diff] [blame] | 203 | // TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24". |
Lorenzo Colitti | 8c6c2c3 | 2014-06-12 13:41:17 +0900 | [diff] [blame] | 204 | Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(address); |
| 205 | init(ipAndMask.first, ipAndMask.second, flags, scope); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 208 | /** |
| 209 | * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64". |
| 210 | * The string representation does not contain the flags and scope, just the address and prefix |
| 211 | * length. |
| 212 | */ |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 213 | @Override |
| 214 | public String toString() { |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 215 | return address.getHostAddress() + "/" + prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 219 | * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if |
Lorenzo Colitti | 7dc78cf | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 220 | * their address, prefix length, flags and scope are equal. Thus, for example, two addresses |
| 221 | * that have the same address and prefix length are not equal if one of them is deprecated and |
| 222 | * the other is not. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 223 | * |
| 224 | * @param obj the object to be tested for equality. |
| 225 | * @return {@code true} if both objects are equal, {@code false} otherwise. |
| 226 | */ |
| 227 | @Override |
| 228 | public boolean equals(Object obj) { |
| 229 | if (!(obj instanceof LinkAddress)) { |
| 230 | return false; |
| 231 | } |
| 232 | LinkAddress linkAddress = (LinkAddress) obj; |
| 233 | return this.address.equals(linkAddress.address) && |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 234 | this.prefixLength == linkAddress.prefixLength && |
| 235 | this.flags == linkAddress.flags && |
| 236 | this.scope == linkAddress.scope; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 239 | /** |
| 240 | * Returns a hashcode for this address. |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 241 | */ |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 242 | @Override |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 243 | public int hashCode() { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 244 | return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope; |
| 245 | } |
| 246 | |
| 247 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 248 | * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress} |
| 249 | * represent the same address. Two {@code LinkAddresses} represent the same address |
| 250 | * if they have the same IP address and prefix length, even if their properties are |
| 251 | * different. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 252 | * |
| 253 | * @param other the {@code LinkAddress} to compare to. |
| 254 | * @return {@code true} if both objects have the same address and prefix length, {@code false} |
| 255 | * otherwise. |
Lorenzo Colitti | 7dc78cf | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 256 | * @hide |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 257 | */ |
| 258 | public boolean isSameAddressAs(LinkAddress other) { |
| 259 | return address.equals(other.address) && prefixLength == other.prefixLength; |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 262 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 263 | * Returns the {@link InetAddress} of this {@code LinkAddress}. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 264 | */ |
| 265 | public InetAddress getAddress() { |
| 266 | return address; |
| 267 | } |
| 268 | |
| 269 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 270 | * Returns the prefix length of this {@code LinkAddress}. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 271 | */ |
Lorenzo Colitti | 7dc78cf | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 272 | public int getPrefixLength() { |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 273 | return prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
Lorenzo Colitti | 7dc78cf | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 277 | * Returns the prefix length of this {@code LinkAddress}. |
| 278 | * TODO: Delete all callers and remove in favour of getPrefixLength(). |
| 279 | * @hide |
| 280 | */ |
| 281 | public int getNetworkPrefixLength() { |
| 282 | return getPrefixLength(); |
| 283 | } |
| 284 | |
| 285 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 286 | * Returns the flags of this {@code LinkAddress}. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 287 | */ |
| 288 | public int getFlags() { |
| 289 | return flags; |
| 290 | } |
| 291 | |
| 292 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 293 | * Returns the scope of this {@code LinkAddress}. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 294 | */ |
| 295 | public int getScope() { |
| 296 | return scope; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Returns true if this {@code LinkAddress} is global scope and preferred. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame] | 301 | * @hide |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 302 | */ |
| 303 | public boolean isGlobalPreferred() { |
Erik Kline | befe778 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 304 | /** |
| 305 | * Note that addresses flagged as IFA_F_OPTIMISTIC are |
| 306 | * simultaneously flagged as IFA_F_TENTATIVE (when the tentative |
| 307 | * state has cleared either DAD has succeeded or failed, and both |
| 308 | * flags are cleared regardless). |
| 309 | */ |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 310 | return (scope == RT_SCOPE_UNIVERSE && |
Erik Kline | befe778 | 2014-10-20 19:46:56 +0900 | [diff] [blame] | 311 | !isIPv6ULA() && |
| 312 | (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L && |
| 313 | ((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L)); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 317 | * Implement the Parcelable interface. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 318 | */ |
| 319 | public int describeContents() { |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Implement the Parcelable interface. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 325 | */ |
| 326 | public void writeToParcel(Parcel dest, int flags) { |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 327 | dest.writeByteArray(address.getAddress()); |
| 328 | dest.writeInt(prefixLength); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 329 | dest.writeInt(this.flags); |
| 330 | dest.writeInt(scope); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Implement the Parcelable interface. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 335 | */ |
| 336 | public static final Creator<LinkAddress> CREATOR = |
| 337 | new Creator<LinkAddress>() { |
| 338 | public LinkAddress createFromParcel(Parcel in) { |
| 339 | InetAddress address = null; |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 340 | try { |
| 341 | address = InetAddress.getByAddress(in.createByteArray()); |
| 342 | } catch (UnknownHostException e) { |
| 343 | // Nothing we can do here. When we call the constructor, we'll throw an |
| 344 | // IllegalArgumentException, because a LinkAddress can't have a null |
| 345 | // InetAddress. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 346 | } |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 347 | int prefixLength = in.readInt(); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 348 | int flags = in.readInt(); |
| 349 | int scope = in.readInt(); |
| 350 | return new LinkAddress(address, prefixLength, flags, scope); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | public LinkAddress[] newArray(int size) { |
| 354 | return new LinkAddress[size]; |
| 355 | } |
| 356 | }; |
| 357 | } |