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