Skip to content

Commit 702090d

Browse files
committed
Updated project libraries and added PortScanUDP, and some tidying
1 parent 520d533 commit 702090d

File tree

13 files changed

+121
-74
lines changed

13 files changed

+121
-74
lines changed

app/build.gradle

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,17 @@ android {
5757
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
5858
}
5959
}
60-
61-
productFlavors {
62-
regular { }
63-
// Add a CI only flavor that has extra permissions needed for CI to perform
64-
ci { }
65-
}
66-
67-
}
68-
69-
// Spoon used for CI / Testing
70-
spoon {
71-
debug = true
72-
// To grant permissions to Android M >= devices */
73-
grantAllPermissions = true
7460
}
7561

7662
dependencies {
77-
compile fileTree(dir: 'libs', include: ['*.jar'])
78-
compile "com.android.support:appcompat-v7:$supportLibVer"
79-
compile "com.android.support:design:$supportLibVer"
80-
compile project(':library')
63+
implementation project(':library')
64+
65+
implementation "com.android.support:appcompat-v7:$supportLibVer"
66+
implementation "com.android.support:design:$supportLibVer"
8167

82-
testCompile 'junit:junit:4.12'
68+
testImplementation 'junit:junit:4.12'
8369

84-
androidTestCompile 'com.squareup.spoon:spoon-client:1.6.4'
85-
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
86-
androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2')
70+
androidTestImplementation 'com.squareup.spoon:spoon-client:1.6.4'
71+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
72+
androidTestImplementation('com.android.support.test.espresso:espresso-intents:2.2')
8773
}

app/src/main/java/com/stealthcotper/networktools/MainActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.stealthcopter.networktools.WakeOnLan;
2222
import com.stealthcopter.networktools.ping.PingResult;
2323
import com.stealthcopter.networktools.ping.PingStats;
24-
import com.stealthcopter.networktools.subnet.SubnetInfo;
24+
import com.stealthcopter.networktools.subnet.SubnetDevice;
2525

2626
import java.io.IOException;
2727
import java.net.InetAddress;
@@ -222,12 +222,12 @@ private void findSubnetDevices() {
222222

223223
SubnetDevices.fromLocalAddress().findDevices(new SubnetDevices.OnSubnetDeviceFound() {
224224
@Override
225-
public void onDeviceFound(SubnetInfo subnetInfo) {
226-
appendResultsText("Device: " + subnetInfo.ip+" "+subnetInfo.hostname);
225+
public void onDeviceFound(SubnetDevice subnetDevice) {
226+
appendResultsText("Device: " + subnetDevice.ip+" "+ subnetDevice.hostname);
227227
}
228228

229229
@Override
230-
public void onFinished(ArrayList<SubnetInfo> devicesFound) {
230+
public void onFinished(ArrayList<SubnetDevice> devicesFound) {
231231
float timeTaken = (System.currentTimeMillis() - startTimeMillis)/1000.0f;
232232
appendResultsText("Finished "+timeTaken+" s");
233233
}

build.gradle

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
buildscript {
44
repositories {
55
jcenter()
6+
google()
67
}
78
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.2'
9+
classpath 'com.android.tools.build:gradle:3.1.2'
910
// NOTE: Do not place your application dependencies here; they belong
1011
// in the individual module build.gradle files
1112
}
@@ -14,6 +15,7 @@ buildscript {
1415
allprojects {
1516
repositories {
1617
jcenter()
18+
google()
1719
}
1820
}
1921

@@ -22,9 +24,9 @@ task clean(type: Delete) {
2224
}
2325

2426
subprojects {
25-
ext.compileSdkVer = 24
26-
ext.buildToolsVer = "24.0.0"
27-
ext.minSdkVer = 10
28-
ext.targetSdkVer = 24
29-
ext.supportLibVer = "24.0.0"
27+
ext.compileSdkVer = 27
28+
ext.buildToolsVer = "27.0.3"
29+
ext.minSdkVer = 14
30+
ext.targetSdkVer = 27
31+
ext.supportLibVer = "27.1.1"
3032
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Dec 09 11:58:50 GMT 2016
1+
#Tue May 01 19:31:51 BST 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

library/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ android {
1919
}
2020

2121
dependencies {
22-
testCompile 'junit:junit:4.12'
23-
testCompile 'org.mockito:mockito-core:1.10.19'
24-
provided 'com.android.support:appcompat-v7:24.2.0'
22+
// Should be easy enough to replace with a non-android annotations class if want to make it
23+
// a totally java project
24+
implementation 'com.android.support:support-annotations:27.1.1'
25+
testImplementation 'junit:junit:4.12'
26+
testImplementation 'org.mockito:mockito-core:2.10.0'
2527
}

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

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

33
import android.support.annotation.Nullable;
4-
import android.support.v4.util.Pair;
5-
import android.text.TextUtils;
64

75
import java.io.BufferedReader;
86
import java.io.FileReader;
97
import java.io.IOException;
108
import java.util.ArrayList;
9+
import java.util.HashMap;
1110

1211
/**
1312
* Created by mat on 09/12/15.
@@ -86,11 +85,7 @@ public static String getIPAddressFromMAC(String macAddress) {
8685
* @return list of IP addresses found
8786
*/
8887
public static ArrayList<String> getAllIPAddressesInARPCache() {
89-
ArrayList<String> ipList = new ArrayList<>();
90-
for (Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
91-
ipList.add(ipMacPair.first);
92-
}
93-
return ipList;
88+
return new ArrayList<>(getAllIPAndMACAddressesInARPCache().keySet());
9489
}
9590

9691
/**
@@ -99,11 +94,7 @@ public static ArrayList<String> getAllIPAddressesInARPCache() {
9994
* @return list of MAC addresses found
10095
*/
10196
public static ArrayList<String> getAllMACAddressesInARPCache() {
102-
ArrayList<String> macList = new ArrayList<>();
103-
for (Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
104-
macList.add(ipMacPair.second);
105-
}
106-
return macList;
97+
return new ArrayList<>(getAllIPAndMACAddressesInARPCache().values());
10798
}
10899

109100

@@ -112,15 +103,15 @@ public static ArrayList<String> getAllMACAddressesInARPCache() {
112103
*
113104
* @return list of IP/MAC address pairs found
114105
*/
115-
public static ArrayList<Pair<String, String>> getAllIPAndMACAddressesInARPCache() {
116-
ArrayList<Pair<String, String>> macList = new ArrayList<>();
106+
public static HashMap<String, String> getAllIPAndMACAddressesInARPCache() {
107+
HashMap<String, String> macList = new HashMap<>();
117108
for (String line : getLinesInARPCache()) {
118109
String[] splitted = line.split(" +");
119110
if (splitted.length >= 4) {
120111
// Ignore values with invalid MAC addresses
121112
if (splitted[3].matches("..:..:..:..:..:..")
122113
&& !splitted[3].equals("00:00:00:00:00:00")) {
123-
macList.add(new Pair<>(splitted[0], splitted[3]));
114+
macList.put(splitted[0], splitted[3]);
124115
}
125116
}
126117
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.support.annotation.NonNull;
44

55
import com.stealthcopter.networktools.ping.PingResult;
6-
import com.stealthcopter.networktools.subnet.SubnetInfo;
6+
import com.stealthcopter.networktools.subnet.SubnetDevice;
77

88
import java.net.InetAddress;
99
import java.net.UnknownHostException;
@@ -19,16 +19,16 @@ public class SubnetDevices {
1919
private int noThreads = 255;
2020

2121
private ArrayList<String> addresses;
22-
private ArrayList<SubnetInfo> devicesFound;
22+
private ArrayList<SubnetDevice> devicesFound;
2323
private OnSubnetDeviceFound listener;
2424

2525
// This class is not to be instantiated
2626
private SubnetDevices() {
2727
}
2828

2929
public interface OnSubnetDeviceFound {
30-
void onDeviceFound(SubnetInfo subnetInfo);
31-
void onFinished(ArrayList<SubnetInfo> devicesFound);
30+
void onDeviceFound(SubnetDevice subnetDevice);
31+
void onFinished(ArrayList<SubnetDevice> devicesFound);
3232
}
3333

3434
/**
@@ -118,9 +118,9 @@ public void findDevices(final OnSubnetDeviceFound listener) {
118118
this.listener.onFinished(devicesFound);
119119
}
120120

121-
private synchronized void subnetDeviceFound(SubnetInfo subnetInfo){
122-
devicesFound.add(subnetInfo);
123-
listener.onDeviceFound(subnetInfo);
121+
private synchronized void subnetDeviceFound(SubnetDevice subnetDevice){
122+
devicesFound.add(subnetDevice);
123+
listener.onDeviceFound(subnetDevice);
124124
}
125125

126126
public class SubnetDeviceFinderRunnable implements Runnable {
@@ -136,10 +136,10 @@ public void run() {
136136
InetAddress ia = InetAddress.getByName(address);
137137
PingResult pingResult = Ping.onAddress(ia).doPing();
138138
if (pingResult.isReachable) {
139-
SubnetInfo subnetInfo = new SubnetInfo(ia);
140-
subnetInfo.mac = ARPInfo.getMACFromIPAddress(ia.getHostAddress());
141-
subnetInfo.time = pingResult.timeTaken;
142-
subnetDeviceFound(subnetInfo);
139+
SubnetDevice subnetDevice = new SubnetDevice(ia);
140+
subnetDevice.mac = ARPInfo.getMACFromIPAddress(ia.getHostAddress());
141+
subnetDevice.time = pingResult.timeTaken;
142+
subnetDeviceFound(subnetDevice);
143143
}
144144
} catch (UnknownHostException e) {
145145
e.printStackTrace();

library/src/main/java/com/stealthcopter/networktools/ping/PingResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public boolean isReachable(){
2424
}
2525

2626
public boolean hasError(){
27-
return !TextUtils.isEmpty(error);
27+
return error != null;
2828
}
2929

3030
public float getTimeTaken(){

library/src/main/java/com/stealthcopter/networktools/ping/PingStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public PingStats(InetAddress ia, long noPings, long packetsLost, float totalTime
1717
this.ia=ia;
1818
this.noPings = noPings;
1919
this.packetsLost = packetsLost;
20-
this.averageTimeTaken = (100.0f * totalTimeTaken) / noPings;
20+
this.averageTimeTaken = totalTimeTaken / noPings;
2121
this.minTimeTaken = minTimeTaken;
2222
this.maxTimeTaken = maxTimeTaken;
2323
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.stealthcopter.networktools.portscanning;
2+
3+
public class PortInfo {
4+
public String ip;
5+
public int portNo;
6+
public String openState;
7+
public boolean open;
8+
9+
public PortInfo(String ip, int portNo) {
10+
this.portNo = portNo;
11+
this.ip = ip;
12+
}
13+
14+
public boolean isOpen(){return open;}
15+
}

library/src/main/java/com/stealthcopter/networktools/portscanning/PortScanTCP.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ public class PortScanTCP {
1414
private PortScanTCP() {
1515
}
1616

17-
public static boolean scanAddress(InetAddress ia, int portNo, int timeoutMillis){
17+
public static PortInfo scanAddress(InetAddress ia, int portNo, int timeoutMillis){
18+
19+
PortInfo portInfo = new PortInfo(ia.getHostAddress(), portNo);
20+
portInfo.open = false;
21+
1822
Socket s = null;
1923
try {
2024
s = new Socket();
2125
s.connect(new InetSocketAddress(ia, portNo), timeoutMillis);
2226

23-
return true;
27+
portInfo.open = true;
2428
} catch (IOException e) {
2529
// Don't log anything as we are expecting a lot of these from closed ports.
2630
}
@@ -33,7 +37,7 @@ public static boolean scanAddress(InetAddress ia, int portNo, int timeoutMillis)
3337
}
3438
}
3539
}
36-
return false;
40+
return portInfo;
3741
}
3842

3943
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.stealthcopter.networktools.portscanning;
2+
3+
import java.io.IOException;
4+
import java.io.InterruptedIOException;
5+
import java.net.DatagramPacket;
6+
import java.net.DatagramSocket;
7+
import java.net.InetAddress;
8+
import java.net.PortUnreachableException;
9+
import java.net.SocketTimeoutException;
10+
11+
/**
12+
* Created by mat on 13/12/15.
13+
*/
14+
public class PortScanUDP {
15+
16+
// This class is not to be instantiated
17+
private PortScanUDP() {
18+
}
19+
20+
public static PortInfo scanAddress(InetAddress ia, int portNo, int timeoutMillis){
21+
22+
PortInfo portInfo = new PortInfo(ia.getHostAddress(), portNo);
23+
portInfo.open = false;
24+
25+
try {
26+
byte[] bytes = new byte[128];
27+
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
28+
29+
DatagramSocket ds = new DatagramSocket();
30+
ds.setSoTimeout(timeoutMillis);
31+
ds.connect(ia, portNo);
32+
ds.send(dp);
33+
ds.isConnected();
34+
ds.receive(dp);
35+
ds.close();
36+
} catch (PortUnreachableException e) {
37+
portInfo.openState = "closed";
38+
} catch (SocketTimeoutException e) {
39+
portInfo.open = true;
40+
portInfo.openState = "open|filtered";
41+
} catch (IOException e) {
42+
portInfo.openState = "unknown";
43+
}
44+
return portInfo;
45+
}
46+
47+
}

library/src/main/java/com/stealthcopter/networktools/subnet/SubnetInfo.java renamed to library/src/main/java/com/stealthcopter/networktools/subnet/SubnetDevice.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313
* @author <a href="mailto:[email protected]">Matthew Rollings</a>
1414
* Copyright (c) 2014 Intohand Ltd. All rights reserved.
1515
*/
16-
public class SubnetInfo {
17-
public String ip = "";
18-
public String hostname = "";
19-
public String mac = "";
16+
public class SubnetDevice {
17+
public String ip;
18+
public String hostname;
19+
public String mac;
2020

2121

2222
public float time = 0;
2323

24-
public SubnetInfo(InetAddress ip) {
24+
public SubnetDevice(InetAddress ip) {
2525
this.ip = ip.getHostAddress();
2626
this.hostname = ip.getCanonicalHostName();
2727
}
2828

2929
@Override
3030
public String toString() {
31-
return "SubnetInfo{" +
31+
return "SubnetDevice{" +
3232
"ip='" + ip + '\'' +
3333
", hostname='" + hostname + '\'' +
3434
", mac='" + mac + '\'' +

0 commit comments

Comments
 (0)