Use dnsjava to get hostname from ip address from 192.168.1.1 to 192.168.1.254

1.1k views Asked by At

I am trying to use dnsjava in an android app to find hostnames of devices in my local wifi network. Below is the code used:

try
{
String ipAddress = "33.1.168.192";
String dnsblDomain = "in-addr.arpa";
Record[] records;

Lookup lookup = new Lookup(ipAddress + "." + dnsblDomain, Type.PTR);
SimpleResolver resolver = new SimpleResolver();
resolver.setAddress(InetAddress.getByName("192.168.1.1"));
lookup.setResolver(resolver);
records = lookup.run();

if(lookup.getResult() == Lookup.SUCCESSFUL)
{
    for (int i = 0; i < records.length; i++)
    {
        if(records[i] instanceof PTRRecord)
        {
            PTRRecord ptr = (PTRRecord) records[i];
            System.out.println("DNS Record: " + records[0].rdataToString());
        }
    }
} else {
    System.out.println("Failed lookup");
}

} 
catch(Exception e) 
{
System.out.println("Exception: " + e);
}

The code was taken from the below link and it seems to work there for OP: any way to discover Android devices on your network?

192.168.1.33 is an active device on my wifi network . 192.168.1.1 is the router IP . The code reaches "Failed lookup" everytime .

I am not sure where I am going wrong as I am new to dnsJava and Networks. An additional question is , will this yield perfect result when scanned over all 254 ip's ? I am thinking of using this code in prod and need to be sure of that .

Any help is very much appreciated.

1

There are 1 answers

6
tadman On

PTR records for reverse names are not stored in the order you're thinking. In general terms for IP A.B.C.D you need to resolve D.C.B.A.in-addr.arpa, so you'll need to reverse the order of the IP components.