Skip to content

Commit

Permalink
Added fix for valid mac address required
Browse files Browse the repository at this point in the history
  • Loading branch information
stealthcopter committed Aug 25, 2016
1 parent 8e8787d commit 46bac13
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
37 changes: 20 additions & 17 deletions library/src/main/java/com/stealthcopter/networktools/ARPInfo.java
Expand Up @@ -10,14 +10,13 @@

/**
* Created by mat on 09/12/15.
*
* <p/>
* Looks at the file at /proc/net/arp to find ip/mac addresses from the cache
* We assume that the file has this structure:
*
* <p/>
* 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
*
*/
public class ARPInfo {

Expand All @@ -28,11 +27,12 @@ public class ARPInfo {
* @param ip - IP address to search for
* @return the MAC from the ARP cache or null in format "01:23:45:67:89:ab"
*/
@Nullable public static String getMACFromIPAddress(String ip) {
@Nullable
public static String getMACFromIPAddress(String ip) {
if (ip == null)
return null;

for(String line : getLinesInARPCache()) {
for (String line : getLinesInARPCache()) {
String[] splitted = line.split(" +");
if (splitted.length >= 4 && ip.equals(splitted[0])) {
String mac = splitted[3];
Expand All @@ -54,16 +54,17 @@ public class ARPInfo {
* @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"
*/
@Nullable public static String getIPAddressFromMAC(String macAddress) {
@Nullable
public static String getIPAddressFromMAC(String macAddress) {
if (macAddress == null) {
return null;
}

if (!macAddress.matches("..:..:..:..:..:..")){
if (!macAddress.matches("..:..:..:..:..:..")) {
throw new IllegalArgumentException("Invalid MAC Address");
}

for(String line : getLinesInARPCache()) {
for (String line : getLinesInARPCache()) {
String[] splitted = line.split(" +");
if (splitted.length >= 4 && macAddress.equals(splitted[3])) {
String ipAddress = splitted[0];
Expand All @@ -78,9 +79,9 @@ public class ARPInfo {
*
* @return list of IP addresses found
*/
public static ArrayList<String> getAllIPAddressesInARPCache(){
public static ArrayList<String> getAllIPAddressesInARPCache() {
ArrayList<String> ipList = new ArrayList<>();
for(Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
for (Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
ipList.add(ipMacPair.first);
}
return ipList;
Expand All @@ -91,9 +92,9 @@ public static ArrayList<String> getAllIPAddressesInARPCache(){
*
* @return list of MAC addresses found
*/
public static ArrayList<String> getAllMACAddressesInARPCache(){
public static ArrayList<String> getAllMACAddressesInARPCache() {
ArrayList<String> macList = new ArrayList<>();
for(Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
for (Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
macList.add(ipMacPair.first);
}
return macList;
Expand All @@ -105,13 +106,14 @@ public static ArrayList<String> getAllMACAddressesInARPCache(){
*
* @return list of IP/MAC address pairs found
*/
public static ArrayList<Pair<String, String>> getAllIPAndMACAddressesInARPCache(){
public static ArrayList<Pair<String, String>> getAllIPAndMACAddressesInARPCache() {
ArrayList<Pair<String, String>> macList = new ArrayList<>();
for(String line : getLinesInARPCache()) {
for (String line : getLinesInARPCache()) {
String[] splitted = line.split(" +");
if (splitted.length >= 4) {
// Ignore invalid MAC addresses as they have not been found
if (!splitted[3].equals("00:00:00:00:00:00")) {
// Ignore values with invalid MAC addresses
if (splitted[3].matches("..:..:..:..:..:..")
&& !splitted[3].equals("00:00:00:00:00:00")) {
macList.add(new Pair<>(splitted[0], splitted[3]));
}
}
Expand All @@ -121,9 +123,10 @@ public static ArrayList<Pair<String, String>> getAllIPAndMACAddressesInARPCache(

/**
* Method to read lines from the ARP Cache
*
* @return the lines of the ARP Cache.
*/
private static ArrayList<String> getLinesInARPCache(){
private static ArrayList<String> getLinesInARPCache() {
ArrayList<String> lines = new ArrayList<>();
BufferedReader br = null;
try {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -38,7 +38,7 @@ then add a library dependency. **Remember** to check for latest release [here](h

```groovy
dependencies {
compile 'com.github.stealthcopter:AndroidNetworkTools:0.1.1'
compile 'com.github.stealthcopter:AndroidNetworkTools:0.1.2'
}
```

Expand Down

0 comments on commit 46bac13

Please sign in to comment.