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?