0

I'm trying to retrieve the host name (or domain) of an IP address with dnsjava library by using a SimpleResolver and the ReverseMap and I wrote the following code:

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.xbill.DNS.Lookup;
import org.xbill.DNS.PTRRecord;
import org.xbill.DNS.Record;
import org.xbill.DNS.Resolver;
import org.xbill.DNS.ReverseMap;
import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.Type;
import org.xbill.DNS.lookup.LookupResult;
import org.xbill.DNS.lookup.LookupSession;

public class IPToName {

    public static void main(String[] args) throws UnknownHostException {
        findHostNameWithLookup();
        findHostNameWithLookupSession();
    }

    public static void findHostNameWithLookup() throws UnknownHostException {
        Resolver resolver = new SimpleResolver(InetAddress.getByName("208.67.222.222"));//Open DNS server
        Collection<String> hostNames = new ArrayList<>();
        for (Integer type : Arrays.asList(Type.A, Type.AAAA, Type.PTR)) {
            final Lookup lookUp = new Lookup(ReverseMap.fromAddress("151.101.1.69"), type);//Stackoverflow.com server
            lookUp.setResolver(resolver);
            Record[] records = lookUp.run();
            if (records != null) {
                for (int i = 0; i < records.length; i++) {
                    if (records[i] instanceof PTRRecord) {
                        hostNames.add(records[i].rdataToString());
                    }
                }
            }
        }
        hostNames.stream().forEach(System.out::println);
    }

    public static void findHostNameWithLookupSession() throws UnknownHostException {
        LookupSession lookupSession = LookupSession.builder().resolver(
            new SimpleResolver(InetAddress.getByName("208.67.222.222"))
        ).build();
        Collection<CompletableFuture<LookupResult>> hostNamesRetrievers = new ArrayList<>();
        for (Integer type : Arrays.asList(Type.A, Type.AAAA, Type.PTR)) {
            hostNamesRetrievers.add(
                lookupSession.lookupAsync(ReverseMap.fromAddress("151.101.1.69"), type).toCompletableFuture()
            );
        }
        hostNamesRetrievers.stream().forEach(hostNamesRetriever -> {
            try {
                List<Record> records = hostNamesRetriever.join().getRecords();
                if (records != null) {
                    for (Record record : records) {
                        System.out.println(record.rdataToString());
                    }
                }
            } catch (Throwable exc) {
                exc.printStackTrace();
            }
        });
    }

}

But with the method findHostNameWithLookup I receive a null array of records and with the findHostNameWithLookupSession I receive all NoSuchDomainException: considering that the istruction ReverseMap.fromAddress("151.101.1.69") generate the String 69.1.101.151.in-addr.arpa.: does anyone know what's wrong?

  • 1
    You really need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – g00se Oct 31 '22 at 14:12
  • But what I posted is a minimal example. Do you mean I should add fully qualified names for classes? – Edgar Becker Oct 31 '22 at 14:27
  • ... Ok, I modified the code I posted – Edgar Becker Oct 31 '22 at 14:30
  • This IP address `151.101.1.69` has no PTR records, you can see that doing `dig -x 151.101.1.69`. So nothing wrong with your code. Again, it is NOT guaranteed that every IP address has a `PTR` record. Your code just needs to make sure to handle the status code back (here you should get a `NXDOMAIN`), and start parsing only if success. You also have to guard against possibly getting other record types (ex: `CNAME`) instead of the one you expect, so you need to test for that before trying to extract data. – Patrick Mevzek Oct 31 '22 at 15:24
  • *Lookup is considered legacy (but not yet deprecated). Use LookupSession instead, which is thread safe and fully async.* according to the API docs. Interestingly that call, and the one you're using, are aynchronous, so even if there *are* PTR records, I'm not sure how the record retrieval would be synchronized with the main thread – g00se Oct 31 '22 at 16:28
  • I modified the example and also added the version with LookupSession. I've also added other types to the query, but it doesn't work. I also added other information in the post. I am starting to think that retrieving the host name from the IP was not possible with this library – Edgar Becker Oct 31 '22 at 18:17
  • Did you actually read the comment of @PatrickMevzek? – g00se Oct 31 '22 at 18:38
  • Yes and I added new types: `Arrays.asList(Type.A, Type.AAAA, Type.PTR)` and I don't know what to do. I launched the dit and I saw that there is this PTR record: `69.1.101.151.in-addr.arpa. IN PTR` – Edgar Becker Oct 31 '22 at 18:42
  • Do a reverse on 185.230.63.186. Your code will work – g00se Oct 31 '22 at 19:11
  • Ok now I understand: so for certain IP Addresses there is no way to retrieve their host names? – Edgar Becker Oct 31 '22 at 19:19
  • afaik, not unless they have PTR records – g00se Oct 31 '22 at 19:24
  • Ok, if any of you post the answer I will accept it – Edgar Becker Oct 31 '22 at 19:27
  • I think @PatrickMevzek should do that - it's his key observation – g00se Oct 31 '22 at 19:31

0 Answers0