FileDocCategorySizeDatePackage
LinkAddress.javaAPI DocAndroid 5.1 API11778Thu Mar 12 22:22:10 GMT 2015android.net

LinkAddress

public class LinkAddress extends Object implements android.os.Parcelable
Identifies 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
address
IPv4 or IPv6 address.
private int
prefixLength
Prefix length.
private int
flags
Address flags. A bitmask of IFA_F_* values.
private int
scope
Address scope. One of the RT_SCOPE_* constants.
public static final Creator
CREATOR
Implement 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.

param
address The IP address.
param
prefixLength The prefix length.
param
flags A bitmask of {@code IFA_F_*} values representing properties of the address.
param
scope An integer defining the scope in which the address is unique (e.g., {@link OsConstants#RT_SCOPE_LINK} or {@link OsConstants#RT_SCOPE_SITE}).
hide

        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.

param
address The IP address.
param
prefixLength The prefix length.
hide

        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.

param
interfaceAddress The interface address.
hide

        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.

param
string The string to parse.
hide

        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.

param
string The string to parse.
param
flags The address flags.
param
scope The address scope.
hide

        // 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 intdescribeContents()
Implement the Parcelable interface.

        return 0;
    
public booleanequals(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.

param
obj the object to be tested for equality.
return
{@code true} if both objects are equal, {@code false} otherwise.

        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.InetAddressgetAddress()
Returns the {@link InetAddress} of this {@code LinkAddress}.

        return address;
    
public intgetFlags()
Returns the flags of this {@code LinkAddress}.

        return flags;
    
public intgetNetworkPrefixLength()
Returns the prefix length of this {@code LinkAddress}. TODO: Delete all callers and remove in favour of getPrefixLength().

hide

        return getPrefixLength();
    
public intgetPrefixLength()
Returns the prefix length of this {@code LinkAddress}.

        return prefixLength;
    
public intgetScope()
Returns the scope of this {@code LinkAddress}.

        return scope;
    
public inthashCode()
Returns a hashcode for this address.

        return address.hashCode() + 11 * prefixLength + 19 * flags + 43 * scope;
    
private voidinit(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 booleanisGlobalPreferred()
Returns true if this {@code LinkAddress} is global scope and preferred.

hide

        /**
         * 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 booleanisIPv6ULA()
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 booleanisSameAddressAs(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.

param
other the {@code LinkAddress} to compare to.
return
{@code true} if both objects have the same address and prefix length, {@code false} otherwise.
hide

        return address.equals(other.address) && prefixLength == other.prefixLength;
    
static intscopeForUnicastAddress(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.

hide

        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.StringtoString()
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 voidwriteToParcel(android.os.Parcel dest, int flags)
Implement the Parcelable interface.

        dest.writeByteArray(address.getAddress());
        dest.writeInt(prefixLength);
        dest.writeInt(this.flags);
        dest.writeInt(scope);