Methods Summary |
---|
private boolean | compareLocalType(java.net.Inet6Address ia)Returns {@code true} if one of following cases applies:
1. both addresses are site local
2. both addresses are link local
3. {@code ia} is neither site local nor link local
if (ia.isSiteLocalAddress() && isSiteLocalAddress()) {
return true;
}
if (ia.isLinkLocalAddress() && isLinkLocalAddress()) {
return true;
}
if (!ia.isSiteLocalAddress() && !ia.isLinkLocalAddress()) {
return true;
}
return false;
|
public boolean | equals(java.lang.Object obj)Compares this instance with the IP address in the object {@code obj} and
returns {@code true} if they are of the same type and represent the same
IP address, {@code false} otherwise. The scope id does not seem to be
part of the comparison.
return super.equals(obj);
|
public static java.net.Inet6Address | getByAddress(java.lang.String host, byte[] addr, int scope_id)Constructs an IPv6 address according to the given {@code host}, {@code
addr} and {@code scope_id}.
if (null == addr || 16 != addr.length) {
// KA020=Illegal IPv6 address
throw new UnknownHostException(Msg.getString("KA020")); //$NON-NLS-1$
}
if (scope_id < 0) {
scope_id = 0;
}
return new Inet6Address(addr, host, scope_id);
|
public static java.net.Inet6Address | getByAddress(java.lang.String host, byte[] addr, java.net.NetworkInterface nif)Gets an IPv6 address instance according to the given {@code host},
{@code addr} and {@code nif}. {@code scope_id} is set according to the
given {@code nif} and the {@code addr} type (for example site-local or
link-local).
Inet6Address address = Inet6Address.getByAddress(host, addr, 0);
// if nif is null, nothing needs to be set.
if (null == nif) {
return address;
}
// find the first address which matches the type addr,
// then set the scope_id, ifname and scopedIf.
Enumeration<InetAddress> addressList = nif.getInetAddresses();
while (addressList.hasMoreElements()) {
InetAddress ia = addressList.nextElement();
if (ia.getAddress().length == 16) {
Inet6Address v6ia = (Inet6Address) ia;
boolean isSameType = v6ia.compareLocalType(address);
if (isSameType) {
address.scope_id_set = true;
address.scope_id = v6ia.scope_id;
address.scope_ifname_set = true;
address.ifname = nif.getName();
address.scopedIf = nif;
break;
}
}
}
// if no address matches the type of addr, throws an
// UnknownHostException.
if (!address.scope_id_set) {
// KA021=Scope id is not found for the given address
throw new UnknownHostException(Msg.getString("KA021")); //$NON-NLS-1$
}
return address;
|
public java.lang.String | getHostAddress()Gets the textual representation of this IP address.
return Inet6Util.createIPAddrStringFromByteArray(ipaddress);
|
public int | getScopeId()Gets the scope id as a number if this address is linked to an interface.
Otherwise returns {@code 0}.
if (scope_id_set) {
return scope_id;
}
return 0;
|
public java.net.NetworkInterface | getScopedInterface()Gets the network interface if this address is instanced with a scoped
network interface. Otherwise returns {@code null}.
if (scope_ifname_set) {
return scopedIf;
}
return null;
|
public int | hashCode()Gets the hashcode of the represented IP address.
/* Returns the low order int as the hash code */
return bytesToInt(ipaddress, 12);
|
public boolean | isAnyLocalAddress()Returns whether this address is a unspecified wildcard address "::" or
not.
for (int i = 0; i < ipaddress.length; i++) {
if (ipaddress[i] != 0) {
return false;
}
}
return true;
|
public boolean | isIPv4CompatibleAddress()Returns whether this address is IPv4 compatible or not. An IPv4
compatible address is prefixed with 96 bits of 0's. The last 32-bits are
varied corresponding with the 32-bit IPv4 address space.
for (int i = 0; i < 12; i++) {
if (ipaddress[i] != 0) {
return false;
}
}
return true;
|
public boolean | isLinkLocalAddress()Returns whether this address is a link-local address or not. A valid IPv6
link-local address is prefixed with 1111111010.
// the first 10 bits need to be 1111111010 (1018)
return (ipaddress[0] == -2) && ((ipaddress[1] & 255) >>> 6) == 2;
|
public boolean | isLoopbackAddress()Returns whether this address is the loopback address or not. The only
valid IPv6 loopback address is "::1".
// The last word must be 1
if (ipaddress[15] != 1) {
return false;
}
// All other words must be 0
for (int i = 0; i < 15; i++) {
if (ipaddress[i] != 0) {
return false;
}
}
return true;
|
public boolean | isMCGlobal()Returns whether this address is a global multicast address or not. A
valid IPv6 global multicast address is 11111111xxxx1110 or FF0E hex.
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0xE
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 14;
|
public boolean | isMCLinkLocal()Returns whether this address is a link-local multicast address or not. A
valid IPv6 link-local multicast address is prefixed with
11111111xxxx0010.
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0x2
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 2;
|
public boolean | isMCNodeLocal()Returns whether this address is a node-local multicast address or not. A
valid IPv6 node-local multicast address is prefixed with
11111111xxxx0001.
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0x1
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 1;
|
public boolean | isMCOrgLocal()Returns whether this address is a organization-local multicast address or
not. A valid IPv6 org-local multicast address is prefixed with
11111111xxxx1000.
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0x8
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 8;
|
public boolean | isMCSiteLocal()Returns whether this address is a site-local multicast address or not. A
valid IPv6 site-local multicast address is prefixed with
11111111xxxx0101.
// the first byte should be 0xFF and the lower 4 bits
// of the second byte should be 0x5
return (ipaddress[0] == -1) && (ipaddress[1] & 15) == 5;
|
public boolean | isMulticastAddress()Returns whether this address is an IP multicast address or not. Valid
IPv6 multicast addresses are binary prefixed with 11111111 or FF (hex).
// Multicast addresses are prefixed with 11111111 (255)
return ipaddress[0] == -1;
|
public boolean | isSiteLocalAddress()Returns whether this address is a site-local address or not. A valid IPv6
site-local address is prefixed with 1111111011.
// the first 10 bits need to be 1111111011 (1019)
return (ipaddress[0] == -2) && ((ipaddress[1] & 255) >>> 6) == 3;
|
private void | readObject(java.io.ObjectInputStream stream)
ObjectInputStream.GetField fields = stream.readFields();
ipaddress = (byte[]) fields.get("ipaddress", null); //$NON-NLS-1$
scope_id = fields.get("scope_id", 0); //$NON-NLS-1$
scope_id_set = fields.get("scope_id_set", false); //$NON-NLS-1$
ifname = (String) fields.get("ifname", null); //$NON-NLS-1$
scope_ifname_set = fields.get("scope_ifname_set", false); //$NON-NLS-1$
if (scope_ifname_set && null != ifname) {
scopedIf = NetworkInterface.getByName(ifname);
}
|
public java.lang.String | toString()Returns a string containing a concise, human-readable description of this
IP address.
if (ifname != null) {
return super.toString() + "%" + ifname; //$NON-NLS-1$
}
if (scope_id != 0) {
return super.toString() + "%" + scope_id; //$NON-NLS-1$
}
return super.toString();
|
private void | writeObject(java.io.ObjectOutputStream stream) //$NON-NLS-1$
ObjectOutputStream.PutField fields = stream.putFields();
if (ipaddress == null) {
fields.put("ipaddress", null); //$NON-NLS-1$
} else {
fields.put("ipaddress", ipaddress); //$NON-NLS-1$
}
fields.put("scope_id", scope_id); //$NON-NLS-1$
fields.put("scope_id_set", scope_id_set); //$NON-NLS-1$
fields.put("scope_ifname_set", scope_ifname_set); //$NON-NLS-1$
fields.put("ifname", ifname); //$NON-NLS-1$
stream.writeFields();
|