2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.util.Pair;
23 import java.net.Inet4Address;
24 import java.net.Inet6Address;
25 import java.net.InetAddress;
26 import java.net.InterfaceAddress;
27 import java.net.UnknownHostException;
29 import static android.system.OsConstants.IFA_F_DADFAILED;
30 import static android.system.OsConstants.IFA_F_DEPRECATED;
31 import static android.system.OsConstants.IFA_F_OPTIMISTIC;
32 import static android.system.OsConstants.IFA_F_TENTATIVE;
33 import static android.system.OsConstants.RT_SCOPE_HOST;
34 import static android.system.OsConstants.RT_SCOPE_LINK;
35 import static android.system.OsConstants.RT_SCOPE_SITE;
36 import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
39 * Identifies an IP address on a network link.
41 * A {@code LinkAddress} consists of:
43 * <li>An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}).
44 * The address must be unicast, as multicast addresses cannot be assigned to interfaces.
45 * <li>Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties
46 * of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}).
47 * <li>Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which
48 * the address is unique (e.g.,
49 * {@code android.system.OsConstants.RT_SCOPE_LINK} or
50 * {@code android.system.OsConstants.RT_SCOPE_UNIVERSE}).
53 public class LinkAddress implements Parcelable {
55 * IPv4 or IPv6 address.
57 private InetAddress address;
62 private int prefixLength;
65 * Address flags. A bitmask of IFA_F_* values.
70 * Address scope. One of the RT_SCOPE_* constants.
75 * Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
76 * RFC 6724 section 3.2.
79 static int scopeForUnicastAddress(InetAddress addr) {
80 if (addr.isAnyLocalAddress()) {
84 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
88 // isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
89 // says that they are assigned global scope.
90 if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
94 return RT_SCOPE_UNIVERSE;
98 * Utility function to check if |address| is a Unique Local IPv6 Unicast Address
99 * (a.k.a. "ULA"; RFC 4193).
101 * Per RFC 4193 section 8, fc00::/7 identifies these addresses.
103 private boolean isIPv6ULA() {
104 if (address != null && address instanceof Inet6Address) {
105 byte[] bytes = address.getAddress();
106 return ((bytes[0] & (byte)0xfc) == (byte)0xfc);
112 * Utility function for the constructors.
114 private void init(InetAddress address, int prefixLength, int flags, int scope) {
115 if (address == null ||
116 address.isMulticastAddress() ||
118 ((address instanceof Inet4Address) && prefixLength > 32) ||
119 (prefixLength > 128)) {
120 throw new IllegalArgumentException("Bad LinkAddress params " + address +
123 this.address = address;
124 this.prefixLength = prefixLength;
130 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
131 * the specified flags and scope. Flags and scope are not checked for validity.
132 * @param address The IP address.
133 * @param prefixLength The prefix length.
134 * @param flags A bitmask of {@code IFA_F_*} values representing properties of the address.
135 * @param scope An integer defining the scope in which the address is unique (e.g.,
136 * {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
139 public LinkAddress(InetAddress address, int prefixLength, int flags, int scope) {
140 init(address, prefixLength, flags, scope);
144 * Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
145 * The flags are set to zero and the scope is determined from the address.
146 * @param address The IP address.
147 * @param prefixLength The prefix length.
150 public LinkAddress(InetAddress address, int prefixLength) {
151 this(address, prefixLength, 0, 0);
152 this.scope = scopeForUnicastAddress(address);
156 * Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
157 * The flags are set to zero and the scope is determined from the address.
158 * @param interfaceAddress The interface address.
161 public LinkAddress(InterfaceAddress interfaceAddress) {
162 this(interfaceAddress.getAddress(),
163 interfaceAddress.getNetworkPrefixLength());
167 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
168 * "2001:db8::1/64". The flags are set to zero and the scope is determined from the address.
169 * @param string The string to parse.
172 public LinkAddress(String address) {
174 this.scope = scopeForUnicastAddress(this.address);
178 * Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
179 * "2001:db8::1/64", with the specified flags and scope.
180 * @param string The string to parse.
181 * @param flags The address flags.
182 * @param scope The address scope.
185 public LinkAddress(String address, int flags, int scope) {
186 // This may throw an IllegalArgumentException; catching it is the caller's responsibility.
187 Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(address);
188 init(ipAndMask.first, ipAndMask.second, flags, scope);
192 * Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
193 * The string representation does not contain the flags and scope, just the address and prefix
197 public String toString() {
198 return address.getHostAddress() + "/" + prefixLength;
202 * Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
203 * their address, prefix length, flags and scope are equal. Thus, for example, two addresses
204 * that have the same address and prefix length are not equal if one of them is deprecated and
207 * @param obj the object to be tested for equality.
208 * @return {@code true} if both objects are equal, {@code false} otherwise.
211 public boolean equals(Object obj) {
212 if (!(obj instanceof LinkAddress)) {
215 LinkAddress linkAddress = (LinkAddress) obj;
216 return this.address.equals(linkAddress.address) &&
217 this.prefixLength == linkAddress.prefixLength &&
218 this.flags == linkAddress.flags &&
219 this.scope == linkAddress.scope;
223 * Returns a hashcode for this address.
226 public int hashCode() {
227 return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
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
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}
241 public boolean isSameAddressAs(LinkAddress other) {
242 return address.equals(other.address) && prefixLength == other.prefixLength;
246 * Returns the {@link InetAddress} of this {@code LinkAddress}.
248 public InetAddress getAddress() {
253 * Returns the prefix length of this {@code LinkAddress}.
255 public int getPrefixLength() {
260 * Returns the prefix length of this {@code LinkAddress}.
261 * TODO: Delete all callers and remove in favour of getPrefixLength().
264 public int getNetworkPrefixLength() {
265 return getPrefixLength();
269 * Returns the flags of this {@code LinkAddress}.
271 public int getFlags() {
276 * Returns the scope of this {@code LinkAddress}.
278 public int getScope() {
283 * Returns true if this {@code LinkAddress} is global scope and preferred.
286 public boolean isGlobalPreferred() {
288 * Note that addresses flagged as IFA_F_OPTIMISTIC are
289 * simultaneously flagged as IFA_F_TENTATIVE (when the tentative
290 * state has cleared either DAD has succeeded or failed, and both
291 * flags are cleared regardless).
293 return (scope == RT_SCOPE_UNIVERSE &&
295 (flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L &&
296 ((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L));
300 * Implement the Parcelable interface.
302 public int describeContents() {
307 * Implement the Parcelable interface.
309 public void writeToParcel(Parcel dest, int flags) {
310 dest.writeByteArray(address.getAddress());
311 dest.writeInt(prefixLength);
312 dest.writeInt(this.flags);
313 dest.writeInt(scope);
317 * Implement the Parcelable interface.
319 public static final Creator<LinkAddress> CREATOR =
320 new Creator<LinkAddress>() {
321 public LinkAddress createFromParcel(Parcel in) {
322 InetAddress address = null;
324 address = InetAddress.getByAddress(in.createByteArray());
325 } catch (UnknownHostException e) {
326 // Nothing we can do here. When we call the constructor, we'll throw an
327 // IllegalArgumentException, because a LinkAddress can't have a null
330 int prefixLength = in.readInt();
331 int flags = in.readInt();
332 int scope = in.readInt();
333 return new LinkAddress(address, prefixLength, flags, scope);
336 public LinkAddress[] newArray(int size) {
337 return new LinkAddress[size];