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 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | |
Robert Greenwalt | b979f79 | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 22 | import java.net.Inet4Address; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 23 | import java.net.InetAddress; |
| 24 | import java.net.InterfaceAddress; |
| 25 | import java.net.UnknownHostException; |
| 26 | |
Elliott Hughes | 34385d3 | 2014-04-28 11:11:32 -0700 | [diff] [blame] | 27 | import static android.system.OsConstants.IFA_F_DADFAILED; |
| 28 | import static android.system.OsConstants.IFA_F_DEPRECATED; |
| 29 | import static android.system.OsConstants.IFA_F_TENTATIVE; |
| 30 | import static android.system.OsConstants.RT_SCOPE_HOST; |
| 31 | import static android.system.OsConstants.RT_SCOPE_LINK; |
| 32 | import static android.system.OsConstants.RT_SCOPE_SITE; |
| 33 | import static android.system.OsConstants.RT_SCOPE_UNIVERSE; |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 34 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 35 | /** |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 36 | * Identifies an IP address on a network link. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 37 | * |
| 38 | * A {@code LinkAddress} consists of: |
| 39 | * <ul> |
| 40 | * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}). |
| 41 | * The address must be unicast, as multicast addresses cannot be assigned to interfaces. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 42 | * <li>Address flags: A bitmask of {@code IFA_F_*} values representing properties |
| 43 | * of the address. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 44 | * <li>Address scope: An integer defining the scope in which the address is unique (e.g., |
| 45 | * {@code RT_SCOPE_LINK} or {@code RT_SCOPE_SITE}). |
| 46 | * <ul> |
| 47 | *<p> |
| 48 | * When constructing a {@code LinkAddress}, the IP address and prefix are required. The flags and |
| 49 | * scope are optional. If they are not specified, the flags are set to zero, and the scope will be |
| 50 | * determined based on the IP address (e.g., link-local addresses will be created with a scope of |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 51 | * {@code RT_SCOPE_LINK}, global addresses with {@code RT_SCOPE_UNIVERSE}, |
| 52 | * etc.) If they are specified, they are not checked for validity. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 53 | * |
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 | */ |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 59 | private InetAddress address; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 62 | * Prefix length. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 63 | */ |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 64 | private int prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 65 | |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 66 | /** |
| 67 | * Address flags. A bitmask of IFA_F_* values. |
| 68 | */ |
| 69 | private int flags; |
| 70 | |
| 71 | /** |
| 72 | * Address scope. One of the RT_SCOPE_* constants. |
| 73 | */ |
| 74 | private int scope; |
| 75 | |
| 76 | /** |
| 77 | * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and |
| 78 | * RFC 6724 section 3.2. |
| 79 | * @hide |
| 80 | */ |
| 81 | static int scopeForUnicastAddress(InetAddress addr) { |
| 82 | if (addr.isAnyLocalAddress()) { |
| 83 | return RT_SCOPE_HOST; |
| 84 | } |
| 85 | |
| 86 | if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) { |
| 87 | return RT_SCOPE_LINK; |
| 88 | } |
| 89 | |
| 90 | // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2 |
| 91 | // says that they are assigned global scope. |
| 92 | if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) { |
| 93 | return RT_SCOPE_SITE; |
| 94 | } |
| 95 | |
| 96 | return RT_SCOPE_UNIVERSE; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Utility function for the constructors. |
| 101 | */ |
| 102 | private void init(InetAddress address, int prefixLength, int flags, int scope) { |
| 103 | if (address == null || |
| 104 | address.isMulticastAddress() || |
| 105 | prefixLength < 0 || |
Robert Greenwalt | b979f79 | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 106 | ((address instanceof Inet4Address) && prefixLength > 32) || |
| 107 | (prefixLength > 128)) { |
| 108 | throw new IllegalArgumentException("Bad LinkAddress params " + address + |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 109 | "/" + prefixLength); |
Robert Greenwalt | b979f79 | 2011-02-11 17:01:02 -0800 | [diff] [blame] | 110 | } |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 111 | this.address = address; |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 112 | this.prefixLength = prefixLength; |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 113 | this.flags = flags; |
| 114 | this.scope = scope; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with |
| 119 | * the specified flags and scope. Flags and scope are not checked for validity. |
| 120 | * @param address The IP address. |
| 121 | * @param prefixLength The prefix length. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 122 | * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address. |
| 123 | * @param scope An integer defining the scope in which the address is unique (e.g., |
| 124 | * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}). |
| 125 | * @hide |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 126 | */ |
| 127 | public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) { |
| 128 | init(address, prefixLength, flags, scope); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 131 | /** |
| 132 | * 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] | 133 | * 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] | 134 | * @param address The IP address. |
| 135 | * @param prefixLength The prefix length. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 136 | * @hide |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 137 | */ |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 138 | public LinkAddress(InetAddress address, int prefixLength) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 139 | this(address, prefixLength, 0, 0); |
| 140 | this.scope = scopeForUnicastAddress(address); |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 143 | /** |
| 144 | * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 145 | * 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] | 146 | * @param interfaceAddress The interface address. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 147 | * @hide |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 148 | */ |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 149 | public LinkAddress(InterfaceAddress interfaceAddress) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 150 | this(interfaceAddress.getAddress(), |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 151 | interfaceAddress.getNetworkPrefixLength()); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * 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] | 156 | * "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] | 157 | * @param string The string to parse. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 158 | * @hide |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 159 | */ |
| 160 | public LinkAddress(String address) { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 161 | this(address, 0, 0); |
| 162 | this.scope = scopeForUnicastAddress(this.address); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or |
| 167 | * "2001:db8::1/64", with the specified flags and scope. |
| 168 | * @param string The string to parse. |
| 169 | * @param flags The address flags. |
| 170 | * @param scope The address scope. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 171 | * @hide |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 172 | */ |
| 173 | public LinkAddress(String address, int flags, int scope) { |
Lorenzo Colitti | 6eb8a62 | 2013-08-08 19:24:09 +0900 | [diff] [blame] | 174 | InetAddress inetAddress = null; |
| 175 | int prefixLength = -1; |
| 176 | try { |
| 177 | String [] pieces = address.split("/", 2); |
| 178 | prefixLength = Integer.parseInt(pieces[1]); |
| 179 | inetAddress = InetAddress.parseNumericAddress(pieces[0]); |
| 180 | } catch (NullPointerException e) { // Null string. |
| 181 | } catch (ArrayIndexOutOfBoundsException e) { // No prefix length. |
| 182 | } catch (NumberFormatException e) { // Non-numeric prefix. |
| 183 | } catch (IllegalArgumentException e) { // Invalid IP address. |
| 184 | } |
| 185 | |
| 186 | if (inetAddress == null || prefixLength == -1) { |
| 187 | throw new IllegalArgumentException("Bad LinkAddress params " + address); |
| 188 | } |
| 189 | |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 190 | init(inetAddress, prefixLength, flags, scope); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 193 | /** |
| 194 | * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64". |
| 195 | * The string representation does not contain the flags and scope, just the address and prefix |
| 196 | * length. |
| 197 | */ |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 198 | @Override |
| 199 | public String toString() { |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 200 | return address.getHostAddress() + "/" + prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 204 | * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if |
| 205 | * their address, prefix length, flags and scope are equal. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 206 | * |
| 207 | * @param obj the object to be tested for equality. |
| 208 | * @return {@code true} if both objects are equal, {@code false} otherwise. |
| 209 | */ |
| 210 | @Override |
| 211 | public boolean equals(Object obj) { |
| 212 | if (!(obj instanceof LinkAddress)) { |
| 213 | return false; |
| 214 | } |
| 215 | LinkAddress linkAddress = (LinkAddress) obj; |
| 216 | return this.address.equals(linkAddress.address) && |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 217 | this.prefixLength == linkAddress.prefixLength && |
| 218 | this.flags == linkAddress.flags && |
| 219 | this.scope == linkAddress.scope; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 222 | /** |
| 223 | * Returns a hashcode for this address. |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 224 | */ |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 225 | @Override |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 226 | public int hashCode() { |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 227 | return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope; |
| 228 | } |
| 229 | |
| 230 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 231 | * Determines whether this {@code LinkAddress} and the provided {@code LinkAddress} |
| 232 | * represent the same address. Two {@code LinkAddresses} represent the same address |
| 233 | * if they have the same IP address and prefix length, even if their properties are |
| 234 | * different. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 235 | * |
| 236 | * @param other the {@code LinkAddress} to compare to. |
| 237 | * @return {@code true} if both objects have the same address and prefix length, {@code false} |
| 238 | * otherwise. |
| 239 | */ |
| 240 | public boolean isSameAddressAs(LinkAddress other) { |
| 241 | return address.equals(other.address) && prefixLength == other.prefixLength; |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 244 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 245 | * Returns the {@link InetAddress} of this {@code LinkAddress}. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 246 | */ |
| 247 | public InetAddress getAddress() { |
| 248 | return address; |
| 249 | } |
| 250 | |
| 251 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 252 | * Returns the prefix length of this {@code LinkAddress}. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 253 | */ |
Irfan Sheriff | 96ca917 | 2010-10-05 16:12:25 -0700 | [diff] [blame] | 254 | public int getNetworkPrefixLength() { |
| 255 | return prefixLength; |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 259 | * Returns the flags of this {@code LinkAddress}. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 260 | */ |
| 261 | public int getFlags() { |
| 262 | return flags; |
| 263 | } |
| 264 | |
| 265 | /** |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 266 | * Returns the scope of this {@code LinkAddress}. |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 267 | */ |
| 268 | public int getScope() { |
| 269 | return scope; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Returns true if this {@code LinkAddress} is global scope and preferred. |
Robert Greenwalt | fd202e6 | 2014-05-18 09:39:18 -0700 | [diff] [blame^] | 274 | * @hide |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 275 | */ |
| 276 | public boolean isGlobalPreferred() { |
| 277 | return (scope == RT_SCOPE_UNIVERSE && |
| 278 | (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED | IFA_F_TENTATIVE)) == 0L); |
| 279 | } |
| 280 | |
| 281 | /** |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 282 | * Implement the Parcelable interface. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 283 | * @hide |
| 284 | */ |
| 285 | public int describeContents() { |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Implement the Parcelable interface. |
| 291 | * @hide |
| 292 | */ |
| 293 | public void writeToParcel(Parcel dest, int flags) { |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 294 | dest.writeByteArray(address.getAddress()); |
| 295 | dest.writeInt(prefixLength); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 296 | dest.writeInt(this.flags); |
| 297 | dest.writeInt(scope); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Implement the Parcelable interface. |
| 302 | * @hide |
| 303 | */ |
| 304 | public static final Creator<LinkAddress> CREATOR = |
| 305 | new Creator<LinkAddress>() { |
| 306 | public LinkAddress createFromParcel(Parcel in) { |
| 307 | InetAddress address = null; |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 308 | try { |
| 309 | address = InetAddress.getByAddress(in.createByteArray()); |
| 310 | } catch (UnknownHostException e) { |
| 311 | // Nothing we can do here. When we call the constructor, we'll throw an |
| 312 | // IllegalArgumentException, because a LinkAddress can't have a null |
| 313 | // InetAddress. |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 314 | } |
Lorenzo Colitti | e1ad184 | 2013-11-27 15:03:10 +0900 | [diff] [blame] | 315 | int prefixLength = in.readInt(); |
Lorenzo Colitti | 6448394 | 2013-11-15 18:43:52 +0900 | [diff] [blame] | 316 | int flags = in.readInt(); |
| 317 | int scope = in.readInt(); |
| 318 | return new LinkAddress(address, prefixLength, flags, scope); |
Irfan Sheriff | ed5d7d1 | 2010-10-01 16:08:28 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | public LinkAddress[] newArray(int size) { |
| 322 | return new LinkAddress[size]; |
| 323 | } |
| 324 | }; |
| 325 | } |