Skip to content

Commit 46bac13

Browse files
committed
Added fix for valid mac address required
1 parent 8e8787d commit 46bac13

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010

1111
/**
1212
* Created by mat on 09/12/15.
13-
*
13+
* <p/>
1414
* Looks at the file at /proc/net/arp to find ip/mac addresses from the cache
1515
* We assume that the file has this structure:
16-
*
16+
* <p/>
1717
* IP address HW type Flags HW address Mask Device
1818
* 192.168.18.11 0x1 0x2 00:04:20:06:55:1a * eth0
1919
* 192.168.18.36 0x1 0x2 00:22:43:ab:2a:5b * eth0
20-
*
2120
*/
2221
public class ARPInfo {
2322

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

35-
for(String line : getLinesInARPCache()) {
35+
for (String line : getLinesInARPCache()) {
3636
String[] splitted = line.split(" +");
3737
if (splitted.length >= 4 && ip.equals(splitted[0])) {
3838
String mac = splitted[3];
@@ -54,16 +54,17 @@ public class ARPInfo {
5454
* @param macAddress in format "01:23:45:67:89:ab" to search for
5555
* @return the IP address found or null in format "192.168.0.1"
5656
*/
57-
@Nullable public static String getIPAddressFromMAC(String macAddress) {
57+
@Nullable
58+
public static String getIPAddressFromMAC(String macAddress) {
5859
if (macAddress == null) {
5960
return null;
6061
}
6162

62-
if (!macAddress.matches("..:..:..:..:..:..")){
63+
if (!macAddress.matches("..:..:..:..:..:..")) {
6364
throw new IllegalArgumentException("Invalid MAC Address");
6465
}
6566

66-
for(String line : getLinesInARPCache()) {
67+
for (String line : getLinesInARPCache()) {
6768
String[] splitted = line.split(" +");
6869
if (splitted.length >= 4 && macAddress.equals(splitted[3])) {
6970
String ipAddress = splitted[0];
@@ -78,9 +79,9 @@ public class ARPInfo {
7879
*
7980
* @return list of IP addresses found
8081
*/
81-
public static ArrayList<String> getAllIPAddressesInARPCache(){
82+
public static ArrayList<String> getAllIPAddressesInARPCache() {
8283
ArrayList<String> ipList = new ArrayList<>();
83-
for(Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
84+
for (Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
8485
ipList.add(ipMacPair.first);
8586
}
8687
return ipList;
@@ -91,9 +92,9 @@ public static ArrayList<String> getAllIPAddressesInARPCache(){
9192
*
9293
* @return list of MAC addresses found
9394
*/
94-
public static ArrayList<String> getAllMACAddressesInARPCache(){
95+
public static ArrayList<String> getAllMACAddressesInARPCache() {
9596
ArrayList<String> macList = new ArrayList<>();
96-
for(Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
97+
for (Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
9798
macList.add(ipMacPair.first);
9899
}
99100
return macList;
@@ -105,13 +106,14 @@ public static ArrayList<String> getAllMACAddressesInARPCache(){
105106
*
106107
* @return list of IP/MAC address pairs found
107108
*/
108-
public static ArrayList<Pair<String, String>> getAllIPAndMACAddressesInARPCache(){
109+
public static ArrayList<Pair<String, String>> getAllIPAndMACAddressesInARPCache() {
109110
ArrayList<Pair<String, String>> macList = new ArrayList<>();
110-
for(String line : getLinesInARPCache()) {
111+
for (String line : getLinesInARPCache()) {
111112
String[] splitted = line.split(" +");
112113
if (splitted.length >= 4) {
113-
// Ignore invalid MAC addresses as they have not been found
114-
if (!splitted[3].equals("00:00:00:00:00:00")) {
114+
// Ignore values with invalid MAC addresses
115+
if (splitted[3].matches("..:..:..:..:..:..")
116+
&& !splitted[3].equals("00:00:00:00:00:00")) {
115117
macList.add(new Pair<>(splitted[0], splitted[3]));
116118
}
117119
}
@@ -121,9 +123,10 @@ public static ArrayList<Pair<String, String>> getAllIPAndMACAddressesInARPCache(
121123

122124
/**
123125
* Method to read lines from the ARP Cache
126+
*
124127
* @return the lines of the ARP Cache.
125128
*/
126-
private static ArrayList<String> getLinesInARPCache(){
129+
private static ArrayList<String> getLinesInARPCache() {
127130
ArrayList<String> lines = new ArrayList<>();
128131
BufferedReader br = null;
129132
try {

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ then add a library dependency. **Remember** to check for latest release [here](h
3838

3939
```groovy
4040
dependencies {
41-
compile 'com.github.stealthcopter:AndroidNetworkTools:0.1.1'
41+
compile 'com.github.stealthcopter:AndroidNetworkTools:0.1.2'
4242
}
4343
```
4444

0 commit comments

Comments
 (0)