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