user6431873
user6431873

Reputation:

How to scan and discover all host in network

I'm developing two mobile application (Android and iOS), I have to discover all host in the network.

I have implemented a function that ping all the IP address in a certain range, for example, if my IP address is 192.168.1.3 I scan this range 192.168.1.1 / 192.168.1.255.

The function discorver some host but not all and I don't understand the reason, I used "Fing" application to compare my results, in this case my fuction discover 18/20 hosts but fing 43 host (all).

Another problem is the computation time, I use threads but ping solution waste more time to "ping" all address.

How I can discover all host in my network ?

Could someone explain to me the reason because I can't discover all host like fing ?

Source code that i used:

private static final int NB_THREADS = 10;

public void doScan() {
    Log.i(LOG_TAG, "Start scanning");

    ExecutorService executor = Executors.newFixedThreadPool(NB_THREADS);
    for(int dest=0; dest<255; dest++) {
        String host = "192.168.1." + dest;
        executor.execute(pingRunnable(host));
    }

    Log.i(LOG_TAG, "Waiting for executor to terminate...");
    executor.shutdown();
    try { executor.awaitTermination(60*1000, TimeUnit.MILLISECONDS); } catch (InterruptedException ignored) { }

    Log.i(LOG_TAG, "Scan finished");
}

private Runnable pingRunnable(final String host) {
    return new Runnable() {
        public void run() {
            Log.d(LOG_TAG, "Pinging " + host + "...");
            try {
                InetAddress inet = InetAddress.getByName(host);
                boolean reachable = inet.isReachable(1000);
                Log.d(LOG_TAG, "=> Result: " + (reachable ? "reachable" : "not reachable"));
            } catch (UnknownHostException e) {
                Log.e(LOG_TAG, "Not found", e);
            } catch (IOException e) {
                Log.e(LOG_TAG, "IO Error", e);
            }
        }
    };
}

Upvotes: 1

Views: 1299

Answers (2)

Hamburg is nice
Hamburg is nice

Reputation: 397

@FelipeS already gave advice on how to improve the code. Just on note on why your code may not discover all devices as it is: by using (only) PING, you are assuming and relying on the fact that all network devices support or respond to PING requests. That may not be the case though. So if a device cant or simply doesn't want to respond to your PING, you will not see it.

Reasons might be for security or because it was deemed unnecessary by the respective devs.

Upvotes: 0

FelipeS
FelipeS

Reputation: 306

I ran a packet capture (shown below) while fing was scanning my home network, it seems it doesn't use pings but arp requests instead. It's scanning all the IP in sequence, so perhaps you just need to adjust your code to use arp requests.

(192.168.1.167 is the IP from my mobile phone).

sudo tcpdump -ni eth0 host 192.168.1.167

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

...

01:18:32.695316 ARP, Request who-has 192.168.1.2 tell 192.168.1.167, length 46 01:18:32.715596 ARP, Request who-has 192.168.1.4 tell 192.168.1.167, length 46 01:18:32.725965 ARP, Request who-has 192.168.1.5 tell 192.168.1.167, length 46 01:18:32.736293 ARP, Request who-has 192.168.1.6 tell 192.168.1.167, length 46 01:18:32.746567 ARP, Request who-has 192.168.1.7 tell 192.168.1.167, length 46 01:18:32.761394 ARP, Request who-has 192.168.1.8 tell 192.168.1.167, length 46 01:18:32.769972 ARP, Request who-has 192.168.1.9 tell 192.168.1.167, length 46 01:18:32.777456 ARP, Request who-has 192.168.1.10 tell 192.168.1.167, length 46

...

UPDATE: I forgot to mention that fing also runs a mac lookup, you can get any Mac listed by fing and confirm it on the following site: https://www.wireshark.org/tools/oui-lookup.html. It seems the app performs other requests and lookups but the basic is explained here, I believe.

Upvotes: 1

Related Questions