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