Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67732b5

Browse files
committed
Added arp cache in SubnetDevices to prevent repeated file reads
1 parent 24a7788 commit 67732b5

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

library/src/main/java/com/stealthcopter/networktools/SubnetDevices.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.net.InetAddress;
77
import java.net.UnknownHostException;
88
import java.util.ArrayList;
9+
import java.util.HashMap;
910
import java.util.List;
1011
import java.util.concurrent.ExecutorService;
1112
import java.util.concurrent.Executors;
@@ -22,6 +23,7 @@ public class SubnetDevices {
2223
private OnSubnetDeviceFound listener;
2324
private int timeOutMillis = 2500;
2425
private boolean cancelled = false;
26+
private HashMap<String, String> ipMacHashMap = null;
2527

2628
// This class is not to be instantiated
2729
private SubnetDevices() {
@@ -73,7 +75,7 @@ public static SubnetDevices fromIPAddress(final String ipAddress) {
7375

7476
subnetDevice.addresses = new ArrayList<>();
7577

76-
// Get addresses from ARP Info first as they are likely to be pingable
78+
// Get addresses from ARP Info first as they are likely to be reachable
7779
subnetDevice.addresses.addAll(ARPInfo.getAllIPAddressesInARPCache());
7880

7981
// Add all missing addresses in subnet
@@ -159,6 +161,10 @@ public SubnetDevices findDevices(final OnSubnetDeviceFound listener) {
159161
@Override
160162
public void run() {
161163

164+
// Load mac addresses into cache var (to avoid hammering the /proc/net/arp file when
165+
// lots of devices are found on the network.
166+
ipMacHashMap = ARPInfo.getAllIPAndMACAddressesInARPCache();
167+
162168
ExecutorService executor = Executors.newFixedThreadPool(noThreads);
163169

164170
for (final String add : addresses) {
@@ -176,6 +182,17 @@ public void run() {
176182
e.printStackTrace();
177183
}
178184

185+
// Loop over devices found and add in the MAC addresses if missing.
186+
// We do this after scanning for all devices as /proc/net/arp may add info
187+
// because of the scan.
188+
ipMacHashMap = ARPInfo.getAllIPAndMACAddressesInARPCache();
189+
for (Device device : devicesFound) {
190+
if (device.mac == null && ipMacHashMap.containsKey(device.ip)) {
191+
device.mac = ipMacHashMap.get(device.ip);
192+
}
193+
}
194+
195+
179196
listener.onFinished(devicesFound);
180197

181198
}
@@ -206,7 +223,12 @@ public void run() {
206223
PingResult pingResult = Ping.onAddress(ia).setTimeOutMillis(timeOutMillis).doPing();
207224
if (pingResult.isReachable) {
208225
Device device = new Device(ia);
209-
device.mac = ARPInfo.getMACFromIPAddress(ia.getHostAddress());
226+
227+
// Add the device MAC address if it is in the cache
228+
if (ipMacHashMap.containsKey(ia.getHostAddress())) {
229+
device.mac = ipMacHashMap.get(ia.getHostAddress());
230+
}
231+
210232
device.time = pingResult.timeTaken;
211233
subnetDeviceFound(device);
212234
}

0 commit comments

Comments
 (0)