Skip to content

Commit b917bde

Browse files
committed
merge
2 parents 89b2c44 + ce765d2 commit b917bde

File tree

6 files changed

+104
-70
lines changed

6 files changed

+104
-70
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ android {
3434
targetSdkVersion targetSdkVer
3535

3636
// When updating these, remember to update the vars in the root build.gradle
37-
versionName "0.4.5.2"
38-
versionCode 20
37+
versionName "0.4.5.3"
38+
versionCode 21
3939

4040
archivesBaseName = "AndroidNetworkTools"
4141
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.stealthcotper.networktools"
4+
android:installLocation="auto"
45
>
56

67
<uses-permission android:name="android.permission.INTERNET" />

build.gradle

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.5.1'
9+
classpath 'com.android.tools.build:gradle:3.5.3'
1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files
1212
}
@@ -30,7 +30,9 @@ subprojects {
3030
ext.targetSdkVer = 29
3131
ext.supportLibVer = "27.1.1"
3232

33-
// When updating these, remember to update the vars in app/build.gradle (for FDroid compatibility)
34-
ext.appVersionName = "0.4.5.2"
35-
ext.appVersionCode = 20
33+
// When updating these, remember to update the vars in app/build.gradle (for FDroid comptability)
34+
// Remember to update readme, and lets keep these version numbers in line with the github releases
35+
ext.appVersionName = "0.4.5.3"
36+
ext.appVersionCode = 21
37+
3638
}

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

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
import java.io.BufferedReader;
44
import java.io.FileReader;
55
import java.io.IOException;
6+
import java.io.InputStreamReader;
67
import java.util.ArrayList;
78
import java.util.HashMap;
89

10+
911
/**
1012
* Looks at the file at /proc/net/arp to fromIPAddress ip/mac addresses from the cache
1113
* We assume that the file has this structure:
1214
*
1315
* IP address HW type Flags HW address Mask Device
1416
* 192.168.18.11 0x1 0x2 00:04:20:06:55:1a * eth0
1517
* 192.168.18.36 0x1 0x2 00:22:43:ab:2a:5b * eth0
18+
*
19+
* Also looks at the output from `ip sleigh show` command
20+
*
1621
*/
1722
public class ARPInfo {
1823

@@ -22,8 +27,7 @@ private ARPInfo() {
2227

2328

2429
/**
25-
* Try to extract a hardware MAC address from a given IP address using the
26-
* ARP cache (/proc/net/arp).
30+
* Try to extract a hardware MAC address from a given IP address
2731
*
2832
* @param ip - IP address to search for
2933
* @return the MAC from the ARP cache or null in format "01:23:45:67:89:ab"
@@ -33,24 +37,13 @@ public static String getMACFromIPAddress(String ip) {
3337
return null;
3438
}
3539

36-
for (String line : getLinesInARPCache()) {
37-
String[] splitted = line.split(" +");
38-
if (splitted.length >= 4 && ip.equals(splitted[0])) {
39-
String mac = splitted[3];
40-
if (mac.matches("..:..:..:..:..:..")) {
41-
return mac;
42-
} else {
43-
return null;
44-
}
45-
}
46-
}
47-
return null;
40+
HashMap<String, String> cache = getAllIPAndMACAddressesInARPCache();
41+
return cache.get(ip);
4842
}
4943

5044

5145
/**
52-
* Try to extract a IP address from the given MAC address using the
53-
* ARP cache (/proc/net/arp).
46+
* Try to extract a IP address from the given MAC address
5447
*
5548
* @param macAddress in format "01:23:45:67:89:ab" to search for
5649
* @return the IP address found or null in format "192.168.0.1"
@@ -64,15 +57,16 @@ public static String getIPAddressFromMAC(String macAddress) {
6457
throw new IllegalArgumentException("Invalid MAC Address");
6558
}
6659

67-
for (String line : getLinesInARPCache()) {
68-
String[] splitted = line.split(" +");
69-
if (splitted.length >= 4 && macAddress.equals(splitted[3])) {
70-
return splitted[0];
60+
HashMap<String, String> cache = getAllIPAndMACAddressesInARPCache();
61+
for (String ip : cache.keySet()) {
62+
if (cache.get(ip).equalsIgnoreCase(macAddress)) {
63+
return ip;
7164
}
7265
}
7366
return null;
7467
}
7568

69+
7670
/**
7771
* Returns all the ip addresses currently in the ARP cache (/proc/net/arp).
7872
*
@@ -93,19 +87,24 @@ public static ArrayList<String> getAllMACAddressesInARPCache() {
9387

9488

9589
/**
96-
* Returns all the IP/MAC address pairs currently in the ARP cache (/proc/net/arp).
90+
* Returns all the IP/MAC address pairs currently in the following places
91+
*
92+
* 1. ARP cache (/proc/net/arp).
93+
* 2. `ip neigh show` command
9794
*
9895
* @return list of IP/MAC address pairs found
9996
*/
10097
public static HashMap<String, String> getAllIPAndMACAddressesInARPCache() {
101-
HashMap<String, String> macList = new HashMap<>();
98+
HashMap<String, String> macList = getAllIPandMACAddressesFromIPSleigh();
10299
for (String line : getLinesInARPCache()) {
103100
String[] splitted = line.split(" +");
104101
if (splitted.length >= 4) {
105102
// Ignore values with invalid MAC addresses
106103
if (splitted[3].matches("..:..:..:..:..:..")
107104
&& !splitted[3].equals("00:00:00:00:00:00")) {
108-
macList.put(splitted[0], splitted[3]);
105+
if (!macList.containsKey(splitted[0])) {
106+
macList.put(splitted[0], splitted[3]);
107+
}
109108
}
110109
}
111110
}
@@ -140,4 +139,37 @@ private static ArrayList<String> getLinesInARPCache() {
140139
return lines;
141140
}
142141

142+
143+
/**
144+
* Get the IP / MAC address pairs from `ip sleigh show` command
145+
*
146+
* @return hashmap of ips and mac addresses
147+
*/
148+
private static HashMap<String, String> getAllIPandMACAddressesFromIPSleigh() {
149+
HashMap<String, String> macList = new HashMap<>();
150+
151+
try {
152+
Runtime runtime = Runtime.getRuntime();
153+
Process proc = runtime.exec("ip neigh show");
154+
proc.waitFor();
155+
int exit = proc.exitValue();
156+
157+
InputStreamReader reader = new InputStreamReader(proc.getInputStream());
158+
BufferedReader buffer = new BufferedReader(reader);
159+
String line;
160+
while ((line = buffer.readLine()) != null) {
161+
String[] splits = line.split(" ");
162+
if (splits.length < 4) {
163+
continue;
164+
}
165+
macList.put(splits[0], splits[4]);
166+
}
167+
168+
} catch (IOException | InterruptedException e) {
169+
e.printStackTrace();
170+
}
171+
172+
return macList;
173+
}
174+
143175
}

library/src/test/java/com/stealthcopter/networktools/IPToolsTest.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.stealthcopter.networktools;
22

3+
import static org.hamcrest.CoreMatchers.is;
34
import org.junit.Ignore;
45
import org.junit.Test;
56

67
import java.net.InetAddress;
78
import java.net.UnknownHostException;
89
import java.util.List;
910

10-
import static org.junit.Assert.assertFalse;
11-
import static org.junit.Assert.assertNotNull;
12-
import static org.junit.Assert.assertTrue;
11+
import static org.junit.Assert.*;
1312

1413
/**
1514
* Created by matthew on 03/11/17.
@@ -36,48 +35,25 @@ String[] getIPv6AddressesHexCompresed() {
3635

3736
@Test
3837
public void testIsIPv4Address() {
38+
assertIPv4Address(getIPv4Addresses(), true);
39+
assertIPv4Address(getIPv6Addresses(), false);
40+
assertIPv4Address(getInvalidIpAddresses(), false);
41+
}
3942

40-
for (String address : getIPv4Addresses()) {
41-
assertTrue(IPTools.isIPv4Address(address));
42-
}
4343

44-
for (String address : getIPv6Addresses()) {
45-
assertFalse(IPTools.isIPv4Address(address));
46-
}
47-
48-
for (String address : getInvalidIpAddresses()) {
49-
assertFalse(IPTools.isIPv4Address(address));
50-
}
51-
}
5244

5345
@Test
5446
public void testIsIPv6Address() {
55-
for (String address : getIPv4Addresses()) {
56-
assertFalse(IPTools.isIPv6Address(address));
57-
}
58-
59-
for (String address : getIPv6Addresses()) {
60-
assertTrue(IPTools.isIPv6Address(address));
61-
}
62-
63-
for (String address : getInvalidIpAddresses()) {
64-
assertFalse(IPTools.isIPv6Address(address));
65-
}
47+
assertIPv6Address(getIPv4Addresses(), false);
48+
assertIPv6Address(getIPv6Addresses(), true);
49+
assertIPv6Address(getInvalidIpAddresses(), false);
6650
}
6751

6852
@Test
6953
public void testIsIPv6AddressesStandard() {
70-
for (String address : getIPv4Addresses()) {
71-
assertFalse(IPTools.isIPv6StdAddress(address));
72-
}
73-
74-
for (String address : getIPv6Addresses()) {
75-
assertTrue(IPTools.isIPv6StdAddress(address));
76-
}
77-
78-
for (String address : getInvalidIpAddresses()) {
79-
assertFalse(IPTools.isIPv6StdAddress(address));
80-
}
54+
assertIPv6StdAddress(getIPv4Addresses(), false);
55+
assertIPv6StdAddress(getIPv6Addresses(), true);
56+
assertIPv6StdAddress(getInvalidIpAddresses(), false);
8157
}
8258

8359
@Test
@@ -123,4 +99,22 @@ public void testLocalAddressesNetwork() throws UnknownHostException {
12399
assertFalse(IPTools.isIpAddressLocalNetwork(InetAddress.getByName("8.8.8.8")));
124100
}
125101

102+
private void assertIPv4Address(String[] ips, boolean isIPv4Address) {
103+
for (String address : ips) {
104+
assertThat(IPTools.isIPv4Address(address), is(isIPv4Address));
105+
}
106+
}
107+
108+
private void assertIPv6Address(String[] ips, boolean isIPv6Address) {
109+
for (String address : ips) {
110+
assertThat(IPTools.isIPv6Address(address), is(isIPv6Address));
111+
}
112+
}
113+
114+
private void assertIPv6StdAddress(String[] ips, boolean isIPv6StdAddress) {
115+
for (String address : ips) {
116+
assertThat(IPTools.isIPv6StdAddress(address), is(isIPv6StdAddress));
117+
}
118+
}
119+
126120
}

readme.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
> :warning: **Not under active development**: I am no longer actively developing this project as I have other priorities. However, I will still review and accept pull requests with bug fixes and enhancements.
2+
13
# Android Network Tools ![image](./app/src/main/res/mipmap-xhdpi/ic_launcher.png)
24

35
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-AndroidNetworkTools-green.svg?style=true)](https://android-arsenal.com/details/1/3112)
@@ -17,9 +19,14 @@ The javadoc should provide all information needed to understand the methods, but
1719

1820
### Sample app
1921

20-
The sample app is published on Google play to allow you to quickly and easier test the library. Enjoy! And please do feedback to us if your tests produce different results.
22+
The sample app is published on Google Play & F-Droid to allow you to quickly and easier test the library. Enjoy! And please do feedback to us if your tests produce different results.
2123

22-
<a href="https://play.google.com/store/apps/details?id=com.stealthcotper.networktools&utm_source=global_co&utm_medium=prtnr&utm_content=Mar2515&utm_campaign=PartBadge&pcampaignid=MKT-Other-global-all-co-prtnr-py-PartBadge-Mar2515-1"><img alt="Get it on Google Play" src="https://play.google.com/intl/en_us/badges/images/generic/en-play-badge.png" width="150"/></a>
24+
[<img src="https://fdroid.gitlab.io/artwork/badge/get-it-on.png"
25+
alt="Get it on F-Droid"
26+
height="70">](https://f-droid.org/packages/com.stealthcotper.networktools/)
27+
[<img src="https://play.google.com/intl/en_us/badges/images/generic/en-play-badge.png"
28+
alt="Get it on Google Play"
29+
height="70">](https://play.google.com/store/apps/details?id=com.stealthcotper.networktools)
2330

2431
## Usage
2532

@@ -41,7 +48,7 @@ then add a library dependency. **Remember** to check for latest release [here](h
4148

4249
```groovy
4350
dependencies {
44-
compile 'com.github.stealthcopter:AndroidNetworkTools:0.4.0'
51+
compile 'com.github.stealthcopter:AndroidNetworkTools:0.4.5.3'
4552
}
4653
```
4754

@@ -140,7 +147,6 @@ Other useful methods:
140147

141148
It's a standard gradle project.
142149

143-
144150
# Contributing
145151

146152
I welcome pull requests, issues and feedback.
@@ -150,4 +156,3 @@ I welcome pull requests, issues and feedback.
150156
- Commit your changes (git commit -am 'Added some feature')
151157
- Push to the branch (git push origin my-new-feature)
152158
- Create new Pull Request
153-

0 commit comments

Comments
 (0)