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