LinkAddresspublic class LinkAddress extends Object implements android.os.ParcelableIdentifies an IP address on a network link.
A {@code LinkAddress} consists of:
- An IP address and prefix length (e.g., {@code 2001:db8::1/64} or {@code 192.0.2.1/24}).
The address must be unicast, as multicast addresses cannot be assigned to interfaces.
- Address flags: A bitmask of {@code OsConstants.IFA_F_*} values representing properties
of the address (e.g., {@code android.system.OsConstants.IFA_F_OPTIMISTIC}).
- Address scope: One of the {@code OsConstants.IFA_F_*} values; defines the scope in which
the address is unique (e.g.,
{@code android.system.OsConstants.RT_SCOPE_LINK} or
{@code android.system.OsConstants.RT_SCOPE_UNIVERSE}).
|
Fields Summary |
---|
private InetAddress | addressIPv4 or IPv6 address. | private int | prefixLengthPrefix length. | private int | flagsAddress flags. A bitmask of IFA_F_* values. | private int | scopeAddress scope. One of the RT_SCOPE_* constants. | public static final Creator | CREATORImplement the Parcelable interface. |
Constructors Summary |
---|
public LinkAddress(InetAddress address, int prefixLength, int flags, int scope)Constructs a new {@code LinkAddress} from an {@code InetAddress} and prefix length, with
the specified flags and scope. Flags and scope are not checked for validity.
init(address, prefixLength, flags, scope);
| public LinkAddress(InetAddress address, int prefixLength)Constructs a new {@code LinkAddress} from an {@code InetAddress} and a prefix length.
The flags are set to zero and the scope is determined from the address.
this(address, prefixLength, 0, 0);
this.scope = scopeForUnicastAddress(address);
| public LinkAddress(InterfaceAddress interfaceAddress)Constructs a new {@code LinkAddress} from an {@code InterfaceAddress}.
The flags are set to zero and the scope is determined from the address.
this(interfaceAddress.getAddress(),
interfaceAddress.getNetworkPrefixLength());
| public LinkAddress(String address)Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
"2001:db8::1/64". The flags are set to zero and the scope is determined from the address.
this(address, 0, 0);
this.scope = scopeForUnicastAddress(this.address);
| public LinkAddress(String address, int flags, int scope)Constructs a new {@code LinkAddress} from a string such as "192.0.2.5/24" or
"2001:db8::1/64", with the specified flags and scope.
// This may throw an IllegalArgumentException; catching it is the caller's responsibility.
Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(address);
init(ipAndMask.first, ipAndMask.second, flags, scope);
|
Methods Summary |
---|
public int | describeContents()Implement the Parcelable interface.
return 0;
| public boolean | equals(java.lang.Object obj)Compares this {@code LinkAddress} instance against {@code obj}. Two addresses are equal if
their address, prefix length, flags and scope are equal. Thus, for example, two addresses
that have the same address and prefix length are not equal if one of them is deprecated and
the other is not.
if (!(obj instanceof LinkAddress)) {
return false;
}
LinkAddress linkAddress = (LinkAddress) obj;
return this.address.equals(linkAddress.address) &&
this.prefixLength == linkAddress.prefixLength &&
this.flags == linkAddress.flags &&
this.scope == linkAddress.scope;
| public java.net.InetAddress | getAddress()Returns the {@link InetAddress} of this {@code LinkAddress}.
return address;
| public int | getFlags()Returns the flags of this {@code LinkAddress}.
return flags;
| public int | getNetworkPrefixLength()Returns the prefix length of this {@code LinkAddress}.
TODO: Delete all callers and remove in favour of getPrefixLength().
return getPrefixLength();
| public int | getPrefixLength()Returns the prefix length of this {@code LinkAddress}.
return prefixLength;
| public int | getScope()Returns the scope of this {@code LinkAddress}.
return scope;
| public int | hashCode()Returns a hashcode for this address.
return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
| private void | init(java.net.InetAddress address, int prefixLength, int flags, int scope)Utility function for the constructors.
if (address == null ||
address.isMulticastAddress() ||
prefixLength < 0 ||
((address instanceof Inet4Address) && prefixLength > 32) ||
(prefixLength > 128)) {
throw new IllegalArgumentException("Bad LinkAddress params " + address +
"/" + prefixLength);
}
this.address = address;
this.prefixLength = prefixLength;
this.flags = flags;
this.scope = scope;
| public boolean | isGlobalPreferred()Returns true if this {@code LinkAddress} is global scope and preferred.
/**
* Note that addresses flagged as IFA_F_OPTIMISTIC are
* simultaneously flagged as IFA_F_TENTATIVE (when the tentative
* state has cleared either DAD has succeeded or failed, and both
* flags are cleared regardless).
*/
return (scope == RT_SCOPE_UNIVERSE &&
!isIPv6ULA() &&
(flags & (IFA_F_DADFAILED | IFA_F_DEPRECATED)) == 0L &&
((flags & IFA_F_TENTATIVE) == 0L || (flags & IFA_F_OPTIMISTIC) != 0L));
| private boolean | isIPv6ULA()Utility function to check if |address| is a Unique Local IPv6 Unicast Address
(a.k.a. "ULA"; RFC 4193).
Per RFC 4193 section 8, fc00::/7 identifies these addresses.
if (address != null && address instanceof Inet6Address) {
byte[] bytes = address.getAddress();
return ((bytes[0] & (byte)0xfc) == (byte)0xfc);
}
return false;
| public boolean | isSameAddressAs(android.net.LinkAddress other)Determines whether this {@code LinkAddress} and the provided {@code LinkAddress}
represent the same address. Two {@code LinkAddresses} represent the same address
if they have the same IP address and prefix length, even if their properties are
different.
return address.equals(other.address) && prefixLength == other.prefixLength;
| static int | scopeForUnicastAddress(java.net.InetAddress addr)Utility function to determines the scope of a unicast address. Per RFC 4291 section 2.5 and
RFC 6724 section 3.2.
if (addr.isAnyLocalAddress()) {
return RT_SCOPE_HOST;
}
if (addr.isLoopbackAddress() || addr.isLinkLocalAddress()) {
return RT_SCOPE_LINK;
}
// isSiteLocalAddress() returns true for private IPv4 addresses, but RFC 6724 section 3.2
// says that they are assigned global scope.
if (!(addr instanceof Inet4Address) && addr.isSiteLocalAddress()) {
return RT_SCOPE_SITE;
}
return RT_SCOPE_UNIVERSE;
| public java.lang.String | toString()Returns a string representation of this address, such as "192.0.2.1/24" or "2001:db8::1/64".
The string representation does not contain the flags and scope, just the address and prefix
length.
return address.getHostAddress() + "/" + prefixLength;
| public void | writeToParcel(android.os.Parcel dest, int flags)Implement the Parcelable interface.
dest.writeByteArray(address.getAddress());
dest.writeInt(prefixLength);
dest.writeInt(this.flags);
dest.writeInt(scope);
|
|