| Method from java.net.InetAddress Detail: |
static InetAddress anyLocalAddress() {
return impl.anyLocalAddress();
}
|
public boolean equals(Object obj) {
return false;
}
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. |
public byte[] getAddress() {
return null;
}
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]. |
public static InetAddress[] getAllByName(String host) throws UnknownHostException {
if (host == null || host.length() == 0) {
InetAddress[] ret = new InetAddress[1];
ret[0] = impl.loopbackAddress();
return ret;
}
boolean ipv6Expected = false;
if (host.charAt(0) == '[") {
// This is supposed to be an IPv6 litteral
if (host.length() > 2 && host.charAt(host.length()-1) == ']") {
host = host.substring(1, host.length() -1);
ipv6Expected = true;
} else {
// This was supposed to be a IPv6 address, but it's not!
throw new UnknownHostException(host);
}
}
// if host is an IP address, we won't do further lookup
if (Character.digit(host.charAt(0), 16) != -1
|| (host.charAt(0) == ':")) {
byte[] addr = null;
int numericZone = -1;
String ifname = null;
// see if it is IPv4 address
addr = IPAddressUtil.textToNumericFormatV4(host);
if (addr == null) {
// see if it is IPv6 address
// Check if a numeric or string zone id is present
int pos;
if ((pos=host.indexOf ("%")) != -1) {
numericZone = checkNumericZone (host);
if (numericZone == -1) { /* remainder of string must be an ifname */
ifname = host.substring (pos+1);
}
}
addr = IPAddressUtil.textToNumericFormatV6(host);
} else if (ipv6Expected) {
// Means an IPv4 litteral between brackets!
throw new UnknownHostException("["+host+"]");
}
InetAddress[] ret = new InetAddress[1];
if(addr != null) {
if (addr.length == Inet4Address.INADDRSZ) {
ret[0] = new Inet4Address(null, addr);
} else {
if (ifname != null) {
ret[0] = new Inet6Address(null, addr, ifname);
} else {
ret[0] = new Inet6Address(null, addr, numericZone);
}
}
return ret;
}
} else if (ipv6Expected) {
// We were expecting an IPv6 Litteral, but got something else
throw new UnknownHostException("["+host+"]");
}
return getAllByName0(host);
}
Given the name of a host, returns an array of its IP addresses,
based on the configured name service on the system.
The host name can either be a machine name, such as
"java.sun.com", or a textual representation of its IP
address. If a literal IP address is supplied, only the
validity of the address format is checked.
For host specified in literal IPv6 address,
either the form defined in RFC 2732 or the literal IPv6 address
format defined in RFC 2373 is accepted. A literal IPv6 address may
also be qualified by appending a scoped zone identifier or scope_id.
The syntax and usage of scope_ids is described
here.
If the host is null then an InetAddress
representing an address of the loopback interface is returned.
See RFC 3330
section 2 and RFC 2373
section 2.5.3.
If there is a security manager and host is not
null and host.length() is not equal to zero, the
security manager's
checkConnect method is called
with the hostname and -1
as its arguments to see if the operation is allowed. |
static InetAddress[] getAllByName0(String host,
boolean check) throws UnknownHostException {
/* If it gets here it is presumed to be a hostname */
/* Cache.get can return: null, unknownAddress, or InetAddress[] */
Object obj = null;
Object objcopy = null;
/* make sure the connection to the host is allowed, before we
* give out a hostname
*/
if (check) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkConnect(host, -1);
}
}
obj = getCachedAddress(host);
/* If no entry in cache, then do the host lookup */
if (obj == null) {
obj = getAddressFromNameService(host);
}
if (obj == unknown_array)
throw new UnknownHostException(host);
/* Make a copy of the InetAddress array */
objcopy = ((InetAddress [])obj).clone();
return (InetAddress [])objcopy;
}
package private so SocketPermission can call it |
public static InetAddress getByAddress(byte[] addr) throws UnknownHostException {
return getByAddress(null, addr);
}
Returns an InetAddress object given the raw IP address .
The argument is in network byte order: the highest order
byte of the address is in getAddress()[0].
This method doesn't block, i.e. no reverse name service lookup
is performed.
IPv4 address byte array must be 4 bytes long and IPv6 byte array
must be 16 bytes long |
public static InetAddress getByAddress(String host,
byte[] addr) throws UnknownHostException {
// create the impl
impl = (new InetAddressImplFactory()).create();
// get name service if provided and requested
String provider = null;;
String propPrefix = "sun.net.spi.nameservice.provider.";
int n = 1;
nameServices = new ArrayList< NameService >();
provider = AccessController.doPrivileged(
new GetPropertyAction(propPrefix + n));
while (provider != null) {
NameService ns = createNSProvider(provider);
if (ns != null)
nameServices.add(ns);
n++;
provider = AccessController.doPrivileged(
new GetPropertyAction(propPrefix + n));
}
// if not designate any name services provider,
// creat a default one
if (nameServices.size() == 0) {
NameService ns = createNSProvider("default");
nameServices.add(ns);
}
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 == Inet4Address.INADDRSZ) {
return new Inet4Address(host, addr);
} else if (addr.length == Inet6Address.INADDRSZ) {
byte[] newAddr
= IPAddressUtil.convertFromIPv4MappedAddress(addr);
if (newAddr != null) {
return new Inet4Address(host, newAddr);
} else {
return new Inet6Address(host, addr);
}
}
}
throw new UnknownHostException("addr is of illegal length");
}
Create an InetAddress based on the provided host name and IP address
No name service is checked for the validity of the address.
The host name can either be a machine name, such as
"java.sun.com", or a textual representation of its IP
address.
No validity checking is done on the host name either.
If addr specifies an IPv4 address an instance of Inet4Address
will be returned; otherwise, an instance of Inet6Address
will be returned.
IPv4 address byte array must be 4 bytes long and IPv6 byte array
must be 16 bytes long |
public static InetAddress getByName(String host) throws UnknownHostException {
return InetAddress.getAllByName(host)[0];
}
Determines the IP address of a host, given the host's name.
The host name can either be a machine name, such as
"java.sun.com", or a textual representation of its
IP address. If a literal IP address is supplied, only the
validity of the address format is checked.
For host specified in literal IPv6 address,
either the form defined in RFC 2732 or the literal IPv6 address
format defined in RFC 2373 is accepted. IPv6 scoped addresses are also
supported. See here for a description of IPv6
scoped addresses.
If the host is null then an InetAddress
representing an address of the loopback interface is returned.
See RFC 3330
section 2 and RFC 2373
section 2.5.3. |
public String getCanonicalHostName() {
if (canonicalHostName == null) {
canonicalHostName =
InetAddress.getHostFromNameService(this, true);
}
return canonicalHostName;
}
Gets the fully qualified domain name for this IP address.
Best effort method, meaning we may not be able to return
the FQDN depending on the underlying system configuration.
If there is a security manager, this method first
calls its checkConnect method
with the hostname and -1
as its arguments to see if the calling code is allowed to know
the hostname for this IP address, i.e., to connect to the host.
If the operation is not allowed, it will return
the textual representation of the IP address. |
public String getHostAddress() {
return null;
}
Returns the IP address string in textual presentation. |
public String getHostName() {
return getHostName(true);
}
Gets the host name for this IP address.
If this InetAddress was created with a host name,
this host name will be remembered and returned;
otherwise, a reverse name lookup will be performed
and the result will be returned based on the system
configured name lookup service. If a lookup of the name service
is required, call
getCanonicalHostName .
If there is a security manager, its
checkConnect method is first called
with the hostname and -1
as its arguments to see if the operation is allowed.
If the operation is not allowed, it will return
the textual representation of the IP address. |
String getHostName(boolean check) {
if (hostName == null) {
hostName = InetAddress.getHostFromNameService(this, check);
}
return hostName;
}
Returns the hostname for this address.
If the host is equal to null, then this address refers to any
of the local machine's available network addresses.
this is package private so SocketPermission can make calls into
here without a security check.
If there is a security manager, this method first
calls its checkConnect method
with the hostname and -1
as its arguments to see if the calling code is allowed to know
the hostname for this IP address, i.e., to connect to the host.
If the operation is not allowed, it will return
the textual representation of the IP address. |
public static InetAddress getLocalHost() throws UnknownHostException {
SecurityManager security = System.getSecurityManager();
try {
String local = impl.getLocalHostName();
if (security != null) {
security.checkConnect(local, -1);
}
if (local.equals("localhost")) {
return impl.loopbackAddress();
}
InetAddress ret = null;
synchronized (cacheLock) {
long now = System.currentTimeMillis();
if (cachedLocalHost != null) {
if ((now - cacheTime) < maxCacheTime) // Less than 5s old?
ret = cachedLocalHost;
else
cachedLocalHost = null;
}
// we are calling getAddressFromNameService directly
// to avoid getting localHost from cache
if (ret == null) {
InetAddress[] localAddrs;
try {
localAddrs =
(InetAddress[]) InetAddress.getAddressFromNameService(local);
} catch (UnknownHostException uhe) {
throw new UnknownHostException(local + ": " + uhe.getMessage());
}
cachedLocalHost = localAddrs[0];
cacheTime = now;
ret = localAddrs[0];
}
}
return ret;
} catch (java.lang.SecurityException e) {
return impl.loopbackAddress();
}
}
Returns the address of the local host. This is achieved by retrieving
the name of the host from the system, then resolving that name into
an InetAddress.
Note: The resolved address may be cached for a short period of time.
If there is a security manager, its
checkConnect method is called
with the local host name and -1
as its arguments to see if the operation is allowed.
If the operation is not allowed, an InetAddress representing
the loopback address is returned. |
public static InetAddress getLoopbackAddress() {
return impl.loopbackAddress();
}
Returns the loopback address.
The InetAddress returned will represent the IPv4
loopback address, 127.0.0.1, or the IPv6 loopback
address, ::1. The IPv4 loopback address returned
is only one of many in the form 127.*.*.* |
public int hashCode() {
return -1;
}
Returns a hashcode for this IP address. |
public boolean isAnyLocalAddress() {
return false;
}
Utility routine to check if the InetAddress in a wildcard address. |
public boolean isLinkLocalAddress() {
return false;
}
Utility routine to check if the InetAddress is an link local address. |
public boolean isLoopbackAddress() {
return false;
}
Utility routine to check if the InetAddress is a loopback address. |
public boolean isMCGlobal() {
return false;
}
Utility routine to check if the multicast address has global scope. |
public boolean isMCLinkLocal() {
return false;
}
Utility routine to check if the multicast address has link scope. |
public boolean isMCNodeLocal() {
return false;
}
Utility routine to check if the multicast address has node scope. |
public boolean isMCOrgLocal() {
return false;
}
Utility routine to check if the multicast address has organization scope. |
public boolean isMCSiteLocal() {
return false;
}
Utility routine to check if the multicast address has site scope. |
public boolean isMulticastAddress() {
return false;
}
Utility routine to check if the InetAddress is an
IP multicast address. |
public boolean isReachable(int timeout) throws IOException {
return isReachable(null, 0 , timeout);
}
Test whether that address is reachable. Best effort is made by the
implementation to try to reach the host, but firewalls and server
configuration may block requests resulting in a unreachable status
while some specific ports may be accessible.
A typical implementation will use ICMP ECHO REQUESTs if the
privilege can be obtained, otherwise it will try to establish
a TCP connection on port 7 (Echo) of the destination host.
The timeout value, in milliseconds, indicates the maximum amount of time
the try should take. If the operation times out before getting an
answer, the host is deemed unreachable. A negative value will result
in an IllegalArgumentException being thrown. |
public boolean isReachable(NetworkInterface netif,
int ttl,
int timeout) throws IOException {
if (ttl < 0)
throw new IllegalArgumentException("ttl can't be negative");
if (timeout < 0)
throw new IllegalArgumentException("timeout can't be negative");
return impl.isReachable(this, timeout, netif, ttl);
}
Test whether that address is reachable. Best effort is made by the
implementation to try to reach the host, but firewalls and server
configuration may block requests resulting in a unreachable status
while some specific ports may be accessible.
A typical implementation will use ICMP ECHO REQUESTs if the
privilege can be obtained, otherwise it will try to establish
a TCP connection on port 7 (Echo) of the destination host.
The network interface and ttl parameters
let the caller specify which network interface the test will go through
and the maximum number of hops the packets should go through.
A negative value for the ttl will result in an
IllegalArgumentException being thrown.
The timeout value, in milliseconds, indicates the maximum amount of time
the try should take. If the operation times out before getting an
answer, the host is deemed unreachable. A negative value will result
in an IllegalArgumentException being thrown. |
public boolean isSiteLocalAddress() {
return false;
}
Utility routine to check if the InetAddress is a site local address. |
static Object loadImpl(String implName) {
Object impl;
/*
* Property "impl.prefix" will be prepended to the classname
* of the implementation object we instantiate, to which we
* delegate the real work (like native methods). This
* property can vary across implementations of the java.
* classes. The default is an empty String "".
*/
String prefix = AccessController.doPrivileged(
new GetPropertyAction("impl.prefix", ""));
impl = null;
try {
impl = Class.forName("java.net." + prefix + implName).newInstance();
} catch (ClassNotFoundException e) {
System.err.println("Class not found: java.net." + prefix +
implName + ":\ncheck impl.prefix property " +
"in your properties file.");
} catch (InstantiationException e) {
System.err.println("Could not instantiate: java.net." + prefix +
implName + ":\ncheck impl.prefix property " +
"in your properties file.");
} catch (IllegalAccessException e) {
System.err.println("Cannot access class: java.net." + prefix +
implName + ":\ncheck impl.prefix property " +
"in your properties file.");
}
if (impl == null) {
try {
impl = Class.forName(implName).newInstance();
} catch (Exception e) {
throw new Error("System property impl.prefix incorrect");
}
}
return impl;
}
|
public String toString() {
return ((hostName != null) ? hostName : "")
+ "/" + getHostAddress();
}
Converts this IP address to a String. The
string returned is of the form: hostname / literal IP
address.
If the host name is unresolved, no reverse name service lookup
is performed. The hostname part will be represented by an empty string. |