0

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 Answers1

0

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • I have . I am using , String ipAddress = "33.1.168.192"; –  Aug 22 '17 at 17:17
  • Unless you have a DNS entry for `33.1.168.192.in-addr.arpa`, which is typically not the case, then this will never produce any useful results. You'll need a custom DNS server that overrides `168.192.in-addr.arpa`. – tadman Aug 22 '17 at 17:21
  • ok so i guess i cannot use this method since i cannot expect my users to have an entry as well right –  Aug 22 '17 at 17:31
  • It's a non-routing block, so you'll have to provide your own answers. There's alternatives like Windows WINS or [mDNS](https://en.wikipedia.org/wiki/Multicast_DNS) which might be what you're looking for. – tadman Aug 22 '17 at 17:37
  • I am basically trying to find whether the device is up or not . isReachable command , /system/bin/ping and arp is fine . but even a combination of all these 3 may not fetch accurate results which is why I am straying into alternative domains which I am not so familiar with –  Aug 22 '17 at 17:46
  • Also scanning 254 ip's with isreachable , /system/bin/ping and arp table multiple times has an effect on the battery which I dont like . trying better ways . so that may be i don't have to search all 254 when only 4-5 are around normally –  Aug 22 '17 at 17:49