Skip to content

Commit

Permalink
Added improvement for Android 10 devices where /proc/net was restrict…
Browse files Browse the repository at this point in the history
…ed (#66)

This uses `ip sleigh show` to list ip/mac pairs
  • Loading branch information
stealthcopter authored Mar 1, 2020
1 parent 7cb2a5a commit c342c9f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'com.android.tools.build:gradle:3.5.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
78 changes: 55 additions & 23 deletions library/src/main/java/com/stealthcopter/networktools/ARPInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;


/**
* Created by mat on 09/12/15.
*
Expand All @@ -15,6 +17,9 @@
* IP address HW type Flags HW address Mask Device
* 192.168.18.11 0x1 0x2 00:04:20:06:55:1a * eth0
* 192.168.18.36 0x1 0x2 00:22:43:ab:2a:5b * eth0
*
* Also looks at the output from `ip sleigh show` command
*
*/
public class ARPInfo {

Expand All @@ -24,8 +29,7 @@ private ARPInfo() {


/**
* Try to extract a hardware MAC address from a given IP address using the
* ARP cache (/proc/net/arp).
* Try to extract a hardware MAC address from a given IP address
*
* @param ip - IP address to search for
* @return the MAC from the ARP cache or null in format "01:23:45:67:89:ab"
Expand All @@ -35,24 +39,13 @@ public static String getMACFromIPAddress(String ip) {
return null;
}

for (String line : getLinesInARPCache()) {
String[] splitted = line.split(" +");
if (splitted.length >= 4 && ip.equals(splitted[0])) {
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
return mac;
} else {
return null;
}
}
}
return null;
HashMap<String, String> cache = getAllIPAndMACAddressesInARPCache();
return cache.get(ip);
}


/**
* Try to extract a IP address from the given MAC address using the
* ARP cache (/proc/net/arp).
* Try to extract a IP address from the given MAC address
*
* @param macAddress in format "01:23:45:67:89:ab" to search for
* @return the IP address found or null in format "192.168.0.1"
Expand All @@ -66,15 +59,16 @@ public static String getIPAddressFromMAC(String macAddress) {
throw new IllegalArgumentException("Invalid MAC Address");
}

for (String line : getLinesInARPCache()) {
String[] splitted = line.split(" +");
if (splitted.length >= 4 && macAddress.equals(splitted[3])) {
return splitted[0];
HashMap<String, String> cache = getAllIPAndMACAddressesInARPCache();
for (String ip : cache.keySet()) {
if (cache.get(ip).equalsIgnoreCase(macAddress)) {
return ip;
}
}
return null;
}


/**
* Returns all the ip addresses currently in the ARP cache (/proc/net/arp).
*
Expand All @@ -95,19 +89,24 @@ public static ArrayList<String> getAllMACAddressesInARPCache() {


/**
* Returns all the IP/MAC address pairs currently in the ARP cache (/proc/net/arp).
* Returns all the IP/MAC address pairs currently in the following places
*
* 1. ARP cache (/proc/net/arp).
* 2. `ip neigh show` command
*
* @return list of IP/MAC address pairs found
*/
public static HashMap<String, String> getAllIPAndMACAddressesInARPCache() {
HashMap<String, String> macList = new HashMap<>();
HashMap<String, String> macList = getAllIPandMACAddressesFromIPSleigh();
for (String line : getLinesInARPCache()) {
String[] splitted = line.split(" +");
if (splitted.length >= 4) {
// Ignore values with invalid MAC addresses
if (splitted[3].matches("..:..:..:..:..:..")
&& !splitted[3].equals("00:00:00:00:00:00")) {
macList.put(splitted[0], splitted[3]);
if (!macList.containsKey(splitted[0])) {
macList.put(splitted[0], splitted[3]);
}
}
}
}
Expand Down Expand Up @@ -142,4 +141,37 @@ private static ArrayList<String> getLinesInARPCache() {
return lines;
}


/**
* Get the IP / MAC address pairs from `ip sleigh show` command
*
* @return hashmap of ips and mac addresses
*/
private static HashMap<String, String> getAllIPandMACAddressesFromIPSleigh() {
HashMap<String, String> macList = new HashMap<>();

try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("ip neigh show");
proc.waitFor();
int exit = proc.exitValue();

InputStreamReader reader = new InputStreamReader(proc.getInputStream());
BufferedReader buffer = new BufferedReader(reader);
String line;
while ((line = buffer.readLine()) != null) {
String[] splits = line.split(" ");
if (splits.length < 4) {
continue;
}
macList.put(splits[0], splits[4]);
}

} catch (IOException | InterruptedException e) {
e.printStackTrace();
}

return macList;
}

}

0 comments on commit c342c9f

Please sign in to comment.