1

I am trying to find the hostname from the IP addresses in the subnet ( reverse DNS ) from my android app. I have not been able to find an approach which works on a decent number of routers.

Approaches tried so far

  1. getCanonicalHostName

    Works well with some routers. Does not work in the rest and just returns the IP.

  2. DNSJava library.

    I tried the below code which also works with some cases and not in other routers.

    String getHostNameUsingDNSJavaLibrary( String ipAddress )
    {
         try
         {
         Name reverseipAddress = ReverseMap.fromAddress( ipAddress );
         Log.e( TAG ,"reverseipAddress="+ reverseipAddress );
    
         Record[] records;
         Lookup lookup = new Lookup( reverseipAddress, Type.PTR );
         SimpleResolver resolver = new SimpleResolver();
         resolver.setAddress( InetAddress.getByName( ModemIPAddress ) );
         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];
                      return records[0].rdataToString();
                 }
             }
         }
         else
         {
             Log.e( TAG ,"Failed lookup"+ lookup.getResult() ); 
         }
     }
     catch(Exception e)
     {
         Log.e( TAG ,"Exception: " + e);
     }
     return ipAddress;
    

    }

  3. Linux commands

    I have tried running commands like nslookup, host, dig on the android linux terminal but the commands don't work. Eg: OnePlus5T:/ $ dig -x

    /system/bin/sh: dig: inaccessible or not found

Apps like fing do this better. Even fing cannot find all the hostnames of devices in the network but its much better than what I have been able to accomplish. There are similar questions like this Can't do reverse DNS lookup on Android but there hasn't been convincing answers.

Any help to make it better is appreciated.

1 Answers1

0

Try the following instead:

inetAddr = InetAddress.getByName(ipAddress);
inetAddr.getHostName();

Since .getHostName() triggers a network operation you have to ensure that the method is not called within the main / UI thread. Please also note that there may exist IP adresses (especially in local networks) without any assigned host name.

rmunge
  • 3,653
  • 5
  • 19
  • I have tried this and it doesn't get me what I want in all cases. It works on a few routers. – Abhilash anand Jul 12 '21 at 01:07
  • @Abhilashanand are you really sure that all the IP addresses have assigned DNS names? A router may simply use an external DNS server that is completely unaware of internal IP addresses. So as long as the router does not come with an installed DNS server (which also often has to be enabled / configured correctly) that assigns a name to each internal IP address you will always have internal IP addresses without any name. – rmunge Jul 12 '21 at 14:51
  • Especially in private home networks it is very likely that DNS resolution is not setup. That's the main reason why things like SSDP, DLNA and Apple's Bonjour exist (search for "Zero-configuration networking" on wikipedia for more details) – rmunge Jul 12 '21 at 14:57
  • what you say makes sense but my thought process is simple. Fing is able to do it with the same router and I am not. Thoughts? – Abhilash anand Jul 14 '21 at 07:33
  • Fing seems to dermine device names also through UPnP and Bonjour. e.g. my Google Chromecast does not have a DNS name but the fing app gets the name through UPnP. Go to network details of a device in the app - there you can see the detected names per type. – rmunge Jul 14 '21 at 22:01
  • The app seems to detect also names through Mircrosoft's NetBIOS protocol. – rmunge Jul 15 '21 at 06:26
  • Is there anyway I can replicate it. Any API's, libraries etc? – Abhilash anand Jul 15 '21 at 17:04