javadnsjava

How to get multiple domains for the single IP?


Here's the method-

public static String getHostByAddr(byte[] addr) throws UnknownHostException {

    Name name = ReverseMap.fromAddress(InetAddress.getByAddress(addr));

    final String[] servers = new String[] {"208.67.220.220", "208.67.222.222"};
    final Resolver res = new ExtendedResolver(servers);
    final Lookup lookUp = new Lookup(name, Type.PTR);
    lookUp.setResolver(res);
    Record[] records = lookUp.run();
    if (records == null) {
        throw new UnknownHostException();
    }
    return ((PTRRecord) records[0]).getTarget().toString();
}

And here's the call to the above method-

    final InetAddress ip = InetAddress.getByName("198.154.218.168");
    final byte[] bytes = ip.getAddress();
    final String host = getHostByAddr(bytes);
    System.out.println("Host - " + host);

Works fine for most of the cases, but fails when the IP is mapped to multiple domains (???)

Here's the example-

  1. Get IP of securonix.com from here, it is 198.154.218.168
  2. If I pass this IP to the above method it gives error
  3. But if I try the same IP here, it lists down the 4 domains

Is it possible to do this with DNSJava?


Solution

  • In general, you can't. Just as the owner of the domain securonix.com created an entry on their DNS server that translates securonix.com to 198.154.218.168, the owner of the IP address 198.154.218.168 maintains a "reverse" DNS record (usually on a separate server) that maps the IP address to a default DNS name. See the page for "Reverse DNS lookup" on wikipedia for more information on reverse DNS lookups, and note that the reverse DNS lookup -- from IP address to name -- is often maintained by a different owner than the domain name owner and it is NOT just created "automatically" by swapping the name and IP address.

    Furthermore, the site you mention doesn't seem to have reverse DNS entries for that IP address, so you won't ever get an answer for 198.154.218.168 from a standard DNS query; there simply is no (reverse) DNS entry for 198.154.218.168 and the code above fails.

    As an example of the difference between the forward and reverse DNS lookups, when I was first allocated my static IP address, my provider (Comcast) mapped the IP address to some generic name like 75-148-###-###-Houston.hfc.comcastbusiness.net (random IP address example) and I had ask them to modify the reverse DNS entry to map to the name of my domain instead so that a forward and reverse lookup of the IP address and domain name matched. They maintained the lookup of the IP address on their DNS servers, and I maintained the lookup of the domain name on my DNS servers.

    The page at yougetsignal.com must have been doing DNS (forward) lookups of names and storing them in the huge database available for purchase, which allows the web page to find all the names with the same IP. But there is no easy way to do that for an arbitrary IP address by querying DNS servers, unless you have considerable additional information or have already done the millions of lookups like that site.