Methods Summary |
---|
private int | deriveNumericScope(java.net.NetworkInterface ifc)
Enumeration addresses = ifc.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = (InetAddress)addresses.nextElement();
if (!(address instanceof Inet6Address)) {
continue;
}
Inet6Address ia6_addr = (Inet6Address)address;
/* check if site or link local prefixes match */
if (!differentLocalAddressTypes(ia6_addr)){
/* type not the same, so carry on searching */
continue;
}
/* found a matching address - return its scope_id */
return ia6_addr.scope_id;
}
throw new UnknownHostException ("no scope_id found");
|
private int | deriveNumericScope(java.lang.String ifname)
Enumeration en;
try {
en = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
throw new UnknownHostException ("could not enumerate local network interfaces");
}
while (en.hasMoreElements()) {
NetworkInterface ifc = (NetworkInterface)en.nextElement();
if (ifc.getName().equals (ifname)) {
Enumeration addresses = ifc.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = (InetAddress)addresses.nextElement();
if (!(address instanceof Inet6Address)) {
continue;
}
Inet6Address ia6_addr = (Inet6Address)address;
/* check if site or link local prefixes match */
if (!differentLocalAddressTypes(ia6_addr)){
/* type not the same, so carry on searching */
continue;
}
/* found a matching address - return its scope_id */
return ia6_addr.scope_id;
}
}
}
throw new UnknownHostException ("No matching address found for interface : " +ifname);
|
private boolean | differentLocalAddressTypes(java.net.Inet6Address other)
if (isLinkLocalAddress() && !other.isLinkLocalAddress()) {
return false;
}
if (isSiteLocalAddress() && !other.isSiteLocalAddress()) {
return false;
}
return true;
|
public boolean | equals(java.lang.Object obj)Compares this object against the specified object.
The result is true if and only if the argument is
not null and it represents the same IP address as
this object.
Two instances of InetAddress represent the same IP
address if the length of the byte arrays returned by
getAddress is the same for both, and each of the
array components is the same for the byte arrays.
if (obj == null ||
!(obj instanceof Inet6Address))
return false;
Inet6Address inetAddr = (Inet6Address)obj;
for (int i = 0; i < INADDRSZ; i++) {
if (ipaddress[i] != inetAddr.ipaddress[i])
return false;
}
return true;
|
public byte[] | getAddress()Returns the raw IP address of this InetAddress
object. The result is in network byte order: the highest order
byte of the address is in getAddress()[0] .
return (byte[])ipaddress.clone();
|
public static java.net.Inet6Address | getByAddress(java.lang.String host, byte[] addr, java.net.NetworkInterface nif)Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
except that the IPv6 scope_id is set to the value corresponding to the given interface
for the address type specified in addr .
The call will fail with an UnknownHostException if the given interface does not have a numeric
scope_id assigned for the given address type (eg. link-local or site-local).
See here for a description of IPv6
scoped addresses.
if (host != null && host.length() > 0 && host.charAt(0) == '[") {
if (host.charAt(host.length()-1) == ']") {
host = host.substring(1, host.length() -1);
}
}
if (addr != null) {
if (addr.length == Inet6Address.INADDRSZ) {
return new Inet6Address(host, addr, nif);
}
}
throw new UnknownHostException("addr is of illegal length");
|
public static java.net.Inet6Address | getByAddress(java.lang.String host, byte[] addr, int scope_id)Create an Inet6Address in the exact manner of {@link InetAddress#getByAddress(String,byte[])}
except that the IPv6 scope_id is set to the given numeric value.
The scope_id is not checked to determine if it corresponds to any interface on the system.
See here for a description of IPv6
scoped addresses.
if (host != null && host.length() > 0 && host.charAt(0) == '[") {
if (host.charAt(host.length()-1) == ']") {
host = host.substring(1, host.length() -1);
}
}
if (addr != null) {
if (addr.length == Inet6Address.INADDRSZ) {
return new Inet6Address(host, addr, scope_id);
}
}
throw new UnknownHostException("addr is of illegal length");
|
public java.lang.String | getHostAddress()Returns the IP address string in textual presentation. If the instance was created
specifying a scope identifier then the scope id is appended to the IP address preceded by
a "%" (per-cent) character. This can be either a numeric value or a string, depending on which
was used to createthe instance.
String s = numericToTextFormat(ipaddress);
if (scope_ifname_set) { /* must check this first */
s = s + "%" + scope_ifname.getName();
} else if (scope_id_set) {
s = s + "%" + scope_id;
}
return s;
|
public int | getScopeId()Returns the numeric scopeId, if this instance is associated with
an interface. If no scoped_id is set, the returned value is zero.
return scope_id;
|
public java.net.NetworkInterface | getScopedInterface()Returns the scoped interface, if this instance was created with
with a scoped interface.
return scope_ifname;
|
public int | hashCode()Returns a hashcode for this IP address.
if (ipaddress != null) {
int hash = 0;
int i=0;
while (i<INADDRSZ) {
int j=0;
int component=0;
while (j<4 && i<INADDRSZ) {
component = (component << 8) + ipaddress[i];
j++;
i++;
}
hash += component;
}
return hash;
} else {
return 0;
}
|
private static native void | init()Perform class load-time initializations.
|
private void | initif(java.lang.String hostName, byte[] addr, java.net.NetworkInterface nif)
this.hostName = hostName;
if (addr.length == INADDRSZ) { // normal IPv6 address
family = IPv6;
ipaddress = (byte[])addr.clone();
}
if (nif != null) {
this.scope_ifname = nif;
scope_ifname_set = true;
scope_id = deriveNumericScope (nif);
scope_id_set = true;
}
|
private void | initstr(java.lang.String hostName, byte[] addr, java.lang.String ifname)
try {
NetworkInterface nif = NetworkInterface.getByName (ifname);
if (nif == null) {
throw new UnknownHostException ("no such interface " + ifname);
}
initif (hostName, addr, nif);
} catch (SocketException e) {
throw new UnknownHostException ("SocketException thrown" + ifname);
}
|
public boolean | isAnyLocalAddress()Utility routine to check if the InetAddress in a wildcard address.
byte test = 0x00;
for (int i = 0; i < INADDRSZ; i++) {
test |= ipaddress[i];
}
return (test == 0x00);
|
public boolean | isIPv4CompatibleAddress()Utility routine to check if the InetAddress is an
IPv4 compatible IPv6 address.
if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) &&
(ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) &&
(ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) &&
(ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) &&
(ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) &&
(ipaddress[10] == 0x00) && (ipaddress[11] == 0x00)) {
return true;
}
return false;
|
public boolean | isLinkLocalAddress()Utility routine to check if the InetAddress is an link local address.
return ((ipaddress[0] & 0xff) == 0xfe
&& (ipaddress[1] & 0xc0) == 0x80);
|
public boolean | isLoopbackAddress()Utility routine to check if the InetAddress is a loopback address.
byte test = 0x00;
for (int i = 0; i < 15; i++) {
test |= ipaddress[i];
}
return (test == 0x00) && (ipaddress[15] == 0x01);
|
public boolean | isMCGlobal()Utility routine to check if the multicast address has global scope.
return ((ipaddress[0] & 0xff) == 0xff
&& (ipaddress[1] & 0x0f) == 0x0e);
|
public boolean | isMCLinkLocal()Utility routine to check if the multicast address has link scope.
return ((ipaddress[0] & 0xff) == 0xff
&& (ipaddress[1] & 0x0f) == 0x02);
|
public boolean | isMCNodeLocal()Utility routine to check if the multicast address has node scope.
return ((ipaddress[0] & 0xff) == 0xff
&& (ipaddress[1] & 0x0f) == 0x01);
|
public boolean | isMCOrgLocal()Utility routine to check if the multicast address has organization scope.
return ((ipaddress[0] & 0xff) == 0xff
&& (ipaddress[1] & 0x0f) == 0x08);
|
public boolean | isMCSiteLocal()Utility routine to check if the multicast address has site scope.
return ((ipaddress[0] & 0xff) == 0xff
&& (ipaddress[1] & 0x0f) == 0x05);
|
public boolean | isMulticastAddress()Utility routine to check if the InetAddress is an IP multicast
address. 11111111 at the start of the address identifies the
address as being a multicast address.
return ((ipaddress[0] & 0xff) == 0xff);
|
public boolean | isSiteLocalAddress()Utility routine to check if the InetAddress is a site local address.
return ((ipaddress[0] & 0xff) == 0xfe
&& (ipaddress[1] & 0xc0) == 0xc0);
|
static java.lang.String | numericToTextFormat(byte[] src)
/*
* Convert IPv6 binary address into presentation (printable) format.
*
* @param src a byte array representing the IPv6 numeric address
* @return a String representing an IPv6 address in
* textual representation format
* @since 1.4
*/
StringBuffer sb = new StringBuffer(39);
for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {
sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)
| (src[(i<<1)+1] & 0xff)));
if (i < (INADDRSZ / INT16SZ) -1 ) {
sb.append(":");
}
}
return sb.toString();
|
private void | readObject(java.io.ObjectInputStream s)restore the state of this object from stream
including the scope information, only if the
scoped interface name is valid on this system
scope_ifname = null;
scope_ifname_set = false;
s.defaultReadObject();
if (ifname != null && !"".equals (ifname)) {
try {
scope_ifname = NetworkInterface.getByName(ifname);
try {
scope_id = deriveNumericScope (scope_ifname);
} catch (UnknownHostException e) {
// should not happen
assert false;
}
} catch (SocketException e) {}
if (scope_ifname == null) {
/* the interface does not exist on this system, so we clear
* the scope information completely */
scope_id_set = false;
scope_ifname_set = false;
scope_id = 0;
}
}
/* if ifname was not supplied, then the numeric info is used */
ipaddress = (byte[])ipaddress.clone();
// Check that our invariants are satisfied
if (ipaddress.length != INADDRSZ) {
throw new InvalidObjectException("invalid address length: "+
ipaddress.length);
}
if (family != IPv6) {
throw new InvalidObjectException("invalid address family type");
}
|
private synchronized void | writeObject(java.io.ObjectOutputStream s)default behavior is overridden in order to write the
scope_ifname field as a String, rather than a NetworkInterface
which is not serializable
if (scope_ifname_set) {
ifname = scope_ifname.getName();
}
s.defaultWriteObject();
|