10
10
11
11
/**
12
12
* Created by mat on 09/12/15.
13
- *
13
+ * <p/>
14
14
* Looks at the file at /proc/net/arp to find ip/mac addresses from the cache
15
15
* We assume that the file has this structure:
16
- *
16
+ * <p/>
17
17
* IP address HW type Flags HW address Mask Device
18
18
* 192.168.18.11 0x1 0x2 00:04:20:06:55:1a * eth0
19
19
* 192.168.18.36 0x1 0x2 00:22:43:ab:2a:5b * eth0
20
- *
21
20
*/
22
21
public class ARPInfo {
23
22
@@ -28,11 +27,12 @@ public class ARPInfo {
28
27
* @param ip - IP address to search for
29
28
* @return the MAC from the ARP cache or null in format "01:23:45:67:89:ab"
30
29
*/
31
- @ Nullable public static String getMACFromIPAddress (String ip ) {
30
+ @ Nullable
31
+ public static String getMACFromIPAddress (String ip ) {
32
32
if (ip == null )
33
33
return null ;
34
34
35
- for (String line : getLinesInARPCache ()) {
35
+ for (String line : getLinesInARPCache ()) {
36
36
String [] splitted = line .split (" +" );
37
37
if (splitted .length >= 4 && ip .equals (splitted [0 ])) {
38
38
String mac = splitted [3 ];
@@ -54,16 +54,17 @@ public class ARPInfo {
54
54
* @param macAddress in format "01:23:45:67:89:ab" to search for
55
55
* @return the IP address found or null in format "192.168.0.1"
56
56
*/
57
- @ Nullable public static String getIPAddressFromMAC (String macAddress ) {
57
+ @ Nullable
58
+ public static String getIPAddressFromMAC (String macAddress ) {
58
59
if (macAddress == null ) {
59
60
return null ;
60
61
}
61
62
62
- if (!macAddress .matches ("..:..:..:..:..:.." )){
63
+ if (!macAddress .matches ("..:..:..:..:..:.." )) {
63
64
throw new IllegalArgumentException ("Invalid MAC Address" );
64
65
}
65
66
66
- for (String line : getLinesInARPCache ()) {
67
+ for (String line : getLinesInARPCache ()) {
67
68
String [] splitted = line .split (" +" );
68
69
if (splitted .length >= 4 && macAddress .equals (splitted [3 ])) {
69
70
String ipAddress = splitted [0 ];
@@ -78,9 +79,9 @@ public class ARPInfo {
78
79
*
79
80
* @return list of IP addresses found
80
81
*/
81
- public static ArrayList <String > getAllIPAddressesInARPCache (){
82
+ public static ArrayList <String > getAllIPAddressesInARPCache () {
82
83
ArrayList <String > ipList = new ArrayList <>();
83
- for (Pair <String , String > ipMacPair : getAllIPAndMACAddressesInARPCache ()) {
84
+ for (Pair <String , String > ipMacPair : getAllIPAndMACAddressesInARPCache ()) {
84
85
ipList .add (ipMacPair .first );
85
86
}
86
87
return ipList ;
@@ -91,9 +92,9 @@ public static ArrayList<String> getAllIPAddressesInARPCache(){
91
92
*
92
93
* @return list of MAC addresses found
93
94
*/
94
- public static ArrayList <String > getAllMACAddressesInARPCache (){
95
+ public static ArrayList <String > getAllMACAddressesInARPCache () {
95
96
ArrayList <String > macList = new ArrayList <>();
96
- for (Pair <String , String > ipMacPair : getAllIPAndMACAddressesInARPCache ()) {
97
+ for (Pair <String , String > ipMacPair : getAllIPAndMACAddressesInARPCache ()) {
97
98
macList .add (ipMacPair .first );
98
99
}
99
100
return macList ;
@@ -105,13 +106,14 @@ public static ArrayList<String> getAllMACAddressesInARPCache(){
105
106
*
106
107
* @return list of IP/MAC address pairs found
107
108
*/
108
- public static ArrayList <Pair <String , String >> getAllIPAndMACAddressesInARPCache (){
109
+ public static ArrayList <Pair <String , String >> getAllIPAndMACAddressesInARPCache () {
109
110
ArrayList <Pair <String , String >> macList = new ArrayList <>();
110
- for (String line : getLinesInARPCache ()) {
111
+ for (String line : getLinesInARPCache ()) {
111
112
String [] splitted = line .split (" +" );
112
113
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" )) {
115
117
macList .add (new Pair <>(splitted [0 ], splitted [3 ]));
116
118
}
117
119
}
@@ -121,9 +123,10 @@ public static ArrayList<Pair<String, String>> getAllIPAndMACAddressesInARPCache(
121
123
122
124
/**
123
125
* Method to read lines from the ARP Cache
126
+ *
124
127
* @return the lines of the ARP Cache.
125
128
*/
126
- private static ArrayList <String > getLinesInARPCache (){
129
+ private static ArrayList <String > getLinesInARPCache () {
127
130
ArrayList <String > lines = new ArrayList <>();
128
131
BufferedReader br = null ;
129
132
try {
0 commit comments