3

I'm trying to write a simple java program that will return the dns names of ip addresses which I do using the following code:

InetAddress host = InetAddress.getByName(ip);
String dnsName = host.getHostName();

When a dns name is registered getHostName() returns this name and when there is no existent dns name the ip address is returned.

For many addresses the above code doesn't return any while the nslookup command returns.

For example, for the address 82.117.193.169 nslookup returns peer-AS31042.sbb.rs while getHostName() returns only the address. This doesn't happen for all the addresses but for a large number of cases.

Vasilis
  • 2,721
  • 7
  • 33
  • 54

2 Answers2

1

Your computer may not be configured to use DNS by default, even if it is available on demand.

I would try

ping 82.117.193.169

and see whether it resolves the IP address into a hostname.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • when I ping using the IP address it doen't resolve the address to hostname but when I ping with the hostname it resolves it to IP address. Isn't this the expected behaviour? – Vasilis Sep 03 '12 at 18:16
  • The DNS needs to provide two mappings, one maps hostname to IP addresses, but you also need a reverse mapping from IP address to hostname if you want a reverse lookup. – Peter Lawrey Sep 03 '12 at 18:31
  • Is it possible to configure the use of reverse DNS by default? – Vasilis Sep 03 '12 at 19:32
  • Old DNS servers couldn't do this, but its been tens years since I configured such a server. ;) – Peter Lawrey Sep 03 '12 at 19:45
1

It is due to the "A" record check - Java wants that IP number you're looking up, was listed there. They call it "XXX" and I understand why:

private static String getHostFromNameService(InetAddress addr, boolean check) {
String host = null;
for (NameService nameService : nameServices) {
    try {
        // first lookup the hostname
        host = nameService.getHostByAddr(addr.getAddress());

        /* check to see if calling code is allowed to know
         * the hostname for this IP address, ie, connect to the host
         */
        if (check) {
            SecurityManager sec = System.getSecurityManager();
            if (sec != null) {
                sec.checkConnect(host, -1);
            }
        }

        /* now get all the IP addresses for this hostname,
         * and make sure one of them matches the original IP
         * address. We do this to try and prevent spoofing.
         */

        InetAddress[] arr = InetAddress.getAllByName0(host, check);
        boolean ok = false;

        if(arr != null) {
            for(int i = 0; !ok && i < arr.length; i++) {
                ok = addr.equals(arr[i]);
            }
        }

        //XXX: if it looks a spoof just return the address?
        if (!ok) {
            host = addr.getHostAddress();
            return host;
        }

        break;
BUKTOP
  • 867
  • 10
  • 22