4

Is there a way in Java to tell it to return IPv6 only? I've tried everything and can't get it to work.

try
    {
        InetAddress inet = InetAddress.getByName(hostName);

        boolean status = inet.isReachable(5000);

        if (status)
        {
            System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress());
        }
        else
        {
            System.out.println(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");
    }

The line I use to try to return IPv6 only:

System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress());

This keeps returning IPv4. Anyone have any idea of why its doing this?

Spudley
  • 166,037
  • 39
  • 233
  • 307
Nick
  • 597
  • 3
  • 14
  • 34

2 Answers2

3

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();
  }

}
Nick
  • 597
  • 3
  • 14
  • 34
Pr0gr4mm3r
  • 6,170
  • 1
  • 18
  • 23
  • Sorry I'm little confused (sort of new to this), so there is no way for me to return/display IPv6 using DNS name? To sum it up, is there a way for me to return IPv6 address by using DNS name? Thanks – Nick Aug 15 '12 at 18:29
  • It depends on the used DNS-Server and of course the host too ... If the host dont enabled IPv6 or the DNS returns just the IPv4-Addresses you are out of luck ... but you could give InetAddress.getAllByName("stackoverflow.com") a try .. which returns all known IP-Addresses for this domain. – Pr0gr4mm3r Aug 15 '12 at 18:35
  • These are local computers which all have IPv6 enabled. I have a program that I wrote in C# that returns both IPv4 and IPv6 as well as full DNS names, but I can't figure out how to do it in Java. – Nick Aug 15 '12 at 18:41
  • Then InetAddress.getAllByName("..."); will do the trick .. I tried it in my own network and it worked there. Beware this method will return both addresstypes. – Pr0gr4mm3r Aug 15 '12 at 18:43
  • Now that I got IPv6 returned is there a way to do the same thing but this time return full DNS name? Thanks again – Nick Aug 15 '12 at 19:45
  • Looking over dnsjava, do you know which class of the library will let me get full DNS Name? I can't seem to find it. – Nick Aug 16 '12 at 16:38
  • Try the function at the end of this http://www.oreillynet.com/onjava/blog/2005/11/reverse_dns_lookup_and_java.html – Pr0gr4mm3r Aug 16 '12 at 17:40
  • It only seems to return the IP and not the DNS name. – Nick Aug 16 '12 at 18:26
  • That's a little bit strange, because in my test cases it only fails if the host, doesn't own a DNS name. – Pr0gr4mm3r Aug 16 '12 at 18:57
  • Hmm, here is my code if you want to check (I'll post it above). Maybe I'm doing something wrong – Nick Aug 16 '12 at 19:02
  • It gets more and more weird ... Even with your code everything worked as expected in my environment, if the host has an IPv6-Address assigned. Please try using Record[] reverseDns = reverseDns("173.194.70.100"); for (Record r : reverseDns) { System.out.println(r.rdataToString()); } This should print "fa-in-f100.1e100.net." – Pr0gr4mm3r Aug 16 '12 at 20:27
  • It returns full DNS for some IP's but not for all. – Nick Aug 17 '12 at 13:11
  • Hmm.. this behavior appears only if the DNS cannot connect the IP with a DNS name.. maybe you should check your DNS settings, if the reverse zone is correctly set. – Pr0gr4mm3r Aug 17 '12 at 14:55
  • I don't get how it works with on my C# program but it won't work in Java? I just use this command and it works: dnsName = System.Net.Dns.GetHostEntry(list.ElementAt(currentLine)).HostName; – Nick Aug 20 '12 at 17:07
  • 1
    `InetAddress.getByName("ipv6.google.com")` can return `Inet6Address` http://rextester.com/PNY17654 (Example). – Steve-o Feb 05 '14 at 01:50
  • Getting both and then selecting ipv4 for my ftp client fixed it not working with the ipv6 the DNS provided with getByName. Thank you Pr0gramm3r – FrankKrumnow Dec 07 '22 at 14:53
1

You could try to define JVM_ARGS

-Djava.net.preferIPv4Stack=false -Djava.net.preferIPv6Addresses=true

with that props it will prefer IPv6 addr on InetAddress#getByName

More info: https://docs.oracle.com/javase/8/docs/technotes/guides/net/ipv6_guide/

lanwen
  • 2,251
  • 1
  • 17
  • 30