java.net.Inet6Address
does not override getByName()
so it will always return the specific IPv4-Address,
unless your parameter itself is in the form of an valid IPv6-Address, in this case this method will return an Inet6Address-Object.
For example:
getByName("stackoverflow.com")
--> Inet4Address
getByName("2001:0db8:85a3:08d3:1319:8a2e:0370:7344")
--> Inet6Address
InetAddress.getByName()-Documentation
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.<
So if you want to get an IPv6-Address you need to define it within your parameter, or configure a DNS-Server to return the IPv6-Address instead of the IPv4-Address.
Another way to retrieve the IPv6-Address is using InetAddress.getAllByName("www.google.at")
which returns all known IP-Addresses of the host.
For example you can use this method to filter the returned array, which return the first IPv6-Address or null
if the host don't have one:
public Inet6Address getIPv6Addresses(InetAddress[] addresses) {
for (InetAddress addr : addresses) {
if (addr instanceof Inet6Address) {
return (Inet6Address) addr;
}
}
return null;
}
UPDATE:
For more functions, especially those affecting DNS-Servers, I recommend using the external library DNSJava, because the plain Java implementation of DNS support is poor.
http://www.dnsjava.org/
Current Code:
public class Ping
{
public void pingHost (String hostName)
{
try
{
InetAddress[] inet = InetAddress.getAllByName(hostName);
String address = this.getIPv4Addresses(inet).getHostAddress();
boolean status = this.getIPv6Addresses(inet).isReachable(5000);
if (status)
{
System.out.println(reverseDns(address) + " Host Reached\t" + this.getIPv6Addresses(inet).getHostAddress());
}
else
{
System.out.println(this.getIPv6Addresses(inet).getCanonicalHostName() + " Host Unreachable");
}
}
catch (UnknownHostException e)
{
System.err.println("Host does not exists");
}
catch (IOException e)
{
System.err.println("Error in reaching the Host");
}
}
public Inet6Address getIPv6Addresses(InetAddress[] addresses)
{
for (InetAddress addr : addresses)
{
if (addr instanceof Inet6Address)
{
return (Inet6Address) addr;
}
}
return null;
}
public Inet4Address getIPv4Addresses(InetAddress[] addresses)
{
for (InetAddress addr : addresses)
{
if (addr instanceof Inet4Address)
{
return (Inet4Address) addr;
}
}
return null;
}
public static String reverseDns(String hostIp) throws IOException
{
Resolver res = new ExtendedResolver();
Name name = ReverseMap.fromAddress(hostIp);
int type = Type.PTR;
int dclass = DClass.IN;
Record rec = Record.newRecord(name, type, dclass);
Message query = Message.newQuery(rec);
Message response = res.send(query);
Record[] answers = response.getSectionArray(Section.ANSWER);
if (answers.length == 0)
return hostIp;
else
return answers[0].rdataToString();
}
}