6

I'm trying to write a simple program using Java that, given an IP in either version 4 or 6 format, will return its FQDN. The following code works fine when given an ipv4 address, but will only return the given address when an ipv6 address is entered.

InetAddress inet;
try { inet = InetAddress.getByName(theIpAddress); }
catch(UnknownHostException e) { System.out.println("Unknown Host"); return; }

System.out.println(inet.getHostAddress(););
System.out.println(inet.getHostName(););

Whenever I enter an ipv6 getHostName() will only return the same ipv6, even when I know that the ipv6 will resolve to a FQDN. Also, if I enter an ipv6 host name, such as ipv6.google.com, in place of theIpAddress, the exception will occur.

I'm new to this stuff so would appreciate any assistance. Thanks.

robert_x44
  • 8,904
  • 1
  • 30
  • 37
user561877
  • 81
  • 1
  • 3
  • 2
    Does your host / network definitely have IPv6 DNS working? Does nslookup get the right result? – nobody Jan 04 '11 at 01:21

4 Answers4

2

I've done a quick investigation of what's going on with hostname <-> ipv6 resolution in java 8, Windows 7. Looks like 'default' NameService does not work with ipv6 at all! But! Java comes with another, JNDI based NameService implementation called 'dns,sun'. So, if you enable it using either

-Dsun.net.spi.nameservice.provider.1=dns,sun

or

System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");

you will get bidirectional ip <-> hostname resolution for v4 and v6 addresses like this

InetAddress.getAllByName(...)
address.getHostName()

More info about java ipv6 you can find here http://docs.oracle.com/javase/8/docs/technotes/guides/net/ipv6_guide/

Dmytro Voloshyn
  • 269
  • 4
  • 5
2

The problem was actually the version of Java I was running. Updating Java to 1.6.23, from 1.6.21, allowed ipv6s to resolve to their FQDN.

user561877
  • 81
  • 1
  • 3
1

Try inet.getCanonicalHostName(); which "Gets the fully qualified domain name for this IP address."

If you construct the InetAddress using InetAddress.getByName(), getHostName() will return what you constructed it with. getCanonicalHostName() forces a reverse name lookup.

robert_x44
  • 8,904
  • 1
  • 30
  • 37
-2

Using java.net.InetAddress it is not possible to have ipv6 and ipv4 name resolution etc. The bunch of static methods like getByName etc delegate the lookup to instance of Inet4(or 6)AddressImpl that does

public native InetAddress[] lookupAllHostAddr(String hostname) throws UnknownHostException;

Now the fun is a) all these are private/package local so there is not way to inject the impl classes to the InetAddress class b) Inet4(or 6)AddressImpl classes are themselves package local . So there is no way to say , do a ipv4 or ipv6 lookup/name resolution on the fly. I do not get the way all extension points were blocked for these classes making them of really very limited use and flexibility. The real black-magic happens here , where InetAddress class statically initializes the impls, on what does outcome of method isIPv6Supported() dependent ?? My Linux setup supports ipv6 , i can do dns lookups for ipv6 hostnames like ipv6.google.com. Will appreciate if anybody can point me to the direction of a good net library in java for ipv4/v6 utilities like lookup etc.

class InetAddressImplFactory {

    static InetAddressImpl create() {
    Object o;
    if (isIPv6Supported()) {
        o = InetAddress.loadImpl("Inet6AddressImpl");
    } else {
        o = InetAddress.loadImpl("Inet4AddressImpl");
    }
    return (InetAddressImpl)o;
    }

    static native boolean isIPv6Supported();
}
Gokul Nath KP
  • 13,135
  • 24
  • 77
  • 112
redzedi
  • 1,705
  • 19
  • 27
  • This is mostly incorrect. `InetAddress` will return the appropriate IPv4/6 implementation class. That is the whole reason for the design. It is also not an answer. – user207421 Mar 13 '18 at 04:08