Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 036453f

Browse files
committed
Updated traceroute
1 parent 5a598c1 commit 036453f

File tree

4 files changed

+72
-26
lines changed

4 files changed

+72
-26
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@
1212
*/
1313
public class Ping {
1414

15+
// Only try ping using the java method
16+
public static final int PING_JAVA = 0;
17+
18+
// Only try ping using the native method (will only work if native ping binary is found)
19+
public static final int PING_NATIVE = 1;
20+
21+
// Use a hybrid ping that will attempt to use native binary but fallback to using java method
22+
// if it's not found.
23+
public static final int PING_HYBRID = 2;
24+
1525
// This class is not to be instantiated
1626
private Ping() {
1727
}
1828

1929
public interface PingListener {
2030
void onResult(PingResult pingResult);
21-
2231
void onFinished(PingStats pingStats);
23-
2432
void onError(Exception e);
2533
}
2634

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class PingStats {
1212
private final float averageTimeTaken;
1313
private final float minTimeTaken;
1414
private final float maxTimeTaken;
15+
private final boolean isReachable;
1516

1617
public PingStats(InetAddress ia, long noPings, long packetsLost, float totalTimeTaken, float minTimeTaken, float maxTimeTaken) {
1718
this.ia = ia;
@@ -20,16 +21,13 @@ public PingStats(InetAddress ia, long noPings, long packetsLost, float totalTime
2021
this.averageTimeTaken = totalTimeTaken / noPings;
2122
this.minTimeTaken = minTimeTaken;
2223
this.maxTimeTaken = maxTimeTaken;
24+
this.isReachable = noPings - packetsLost > 0;
2325
}
2426

2527
public InetAddress getAddress() {
2628
return ia;
2729
}
2830

29-
public InetAddress getIa() {
30-
return ia;
31-
}
32-
3331
public long getNoPings() {
3432
return noPings;
3533
}
@@ -50,6 +48,10 @@ public float getMaxTimeTaken() {
5048
return maxTimeTaken;
5149
}
5250

51+
public boolean isReachable() {
52+
return isReachable;
53+
}
54+
5355
public long getAverageTimeTakenMillis() {
5456
return (long) (averageTimeTaken * 1000);
5557
}

library/src/main/java/com/stealthcopter/networktools/traceroute/TraceObj.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.stealthcopter.networktools.traceroute;
22

3-
import com.stealthcopter.networktools.ping.PingResult;
4-
5-
class TraceObj {
3+
public class TraceObj {
64
public String address;
75
public String fullString;
86
public String hostname;

library/src/main/java/com/stealthcopter/networktools/traceroute/Traceroute.java

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,30 @@
44
import java.io.InputStreamReader;
55
import java.net.InetAddress;
66
import java.net.UnknownHostException;
7-
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* Traceroute class
12+
*
13+
* Idea: Use nmap if available..
14+
*/
815
public class Traceroute {
916

17+
private int maxHops = 30;
1018
private int timeOutMillis = 1000;
1119
private InetAddress address;
20+
private boolean shouldStopTrace = false;
21+
private ArrayList<TraceObj> traceObjs;
22+
private OnTraceListener onTraceListener;
1223

1324
public interface OnTraceListener {
1425
void onTrace(TraceObj traceObj);
15-
26+
void onFinishTrace(List<TraceObj> traceObjs);
1627
void onError(String error);
1728
}
1829

19-
boolean shouldStopTrace = false;
20-
30+
// This class is not to be instantiated
2131
private Traceroute() {
2232

2333
}
@@ -64,25 +74,47 @@ public void cancel() {
6474
*/
6575
public Traceroute setTimeOutMillis(int timeOutMillis) {
6676
if (timeOutMillis < 0) throw new IllegalArgumentException("Timeout cannot be less than 0");
67-
6877
this.timeOutMillis = Math.min(1000, timeOutMillis);
6978
return this;
7079
}
7180

81+
/**
82+
* Sets the maximum number of hops allowed
83+
*
84+
* @param hops - the maximum number of hops
85+
*
86+
* @return this object to allow chaining
87+
*/
88+
public Traceroute setMaxHops(int hops) {
89+
if (hops < 1) throw new IllegalArgumentException("Hops cannot be less than 1");
90+
this.maxHops = hops;
91+
return this;
92+
}
93+
7294
/**
7395
* @param onTraceListener - the listener to fire portscan results to.
7496
* @return - this object so we can cancel the scan if needed
7597
*/
7698
public void traceRoute(final OnTraceListener onTraceListener) {
7799

100+
this.onTraceListener = onTraceListener;
101+
102+
// If
103+
104+
105+
// TODO: Chunk in a thread
106+
107+
traceObjs = new ArrayList<>();
108+
78109
int hops = 0;
79110

80111
try {
81112

82-
for (int i = 1; i <= 30; i++) {
113+
for (int i = 1; i <= maxHops; i++) {
83114

84115
if (shouldStopTrace) {
85116
shouldStopTrace = false;
117+
finishTrace();
86118
return;
87119
}
88120

@@ -101,7 +133,7 @@ public void traceRoute(final OnTraceListener onTraceListener) {
101133
if (exit < 2) {
102134
InputStreamReader reader = new InputStreamReader(proc.getInputStream());
103135
BufferedReader buffer = new BufferedReader(reader);
104-
String line = "";
136+
String line;
105137
while ((line = buffer.readLine()) != null) {
106138
echo.append(line);
107139
}
@@ -110,9 +142,9 @@ public void traceRoute(final OnTraceListener onTraceListener) {
110142

111143
if (str.contains("Time to live exceeded")) {
112144

113-
final TraceObj traceObj = new TraceObj();
114-
traceObj.address = str.substring(str.indexOf("data.From ") + "data.From ".length(), str.indexOf("icmp_seq") - 1).replace(":", "");
145+
TraceObj traceObj = new TraceObj();
115146
traceObj.fullString = str;
147+
traceObj.address = str.substring(str.indexOf("data.From ") + "data.From ".length(), str.indexOf("icmp_seq") - 1).replace(":", "");
116148
if (traceObj.address.contains(" (")) {
117149
traceObj.hostname = traceObj.address.substring(0, traceObj.address.indexOf(" ("));
118150
traceObj.ip = traceObj.address.substring(traceObj.address.indexOf(" (") + 2, traceObj.address.length() - 1);
@@ -121,20 +153,18 @@ public void traceRoute(final OnTraceListener onTraceListener) {
121153
}
122154
traceObj.hop = hops++;
123155

156+
addTraceObj(traceObj);
157+
124158
if (shouldStopTrace) {
125159
// Stop before possibly doing activity_ping.
126-
onTraceListener.onTrace(traceObj);
127160
shouldStopTrace = false;
128161
return;
129162
}
130-
131-
onTraceListener.onTrace(traceObj);
132163
}
164+
else if (!str.contains("100% packet loss")) {
165+
// Keep going if it's a route, or if we lost all our packets
133166

134-
// Keep going if it's a route, or if we lost all our packets
135-
if (!str.contains("Time to live exceeded") && !str.contains("100% packet loss")) {
136-
137-
final TraceObj traceObj = new TraceObj();
167+
TraceObj traceObj = new TraceObj();
138168

139169
traceObj.address = str.substring(str.indexOf("bytes of data.64 bytes from ") + "bytes of data.64 bytes from ".length(), str.indexOf("icmp_seq") - 1).replace(":", "");
140170
if (traceObj.address.contains(" (")) {
@@ -148,7 +178,8 @@ public void traceRoute(final OnTraceListener onTraceListener) {
148178
traceObj.isTargetDestination = true;
149179
traceObj.time = str.substring(str.indexOf("time="), str.indexOf("ms", str.indexOf("time=")));
150180

151-
onTraceListener.onTrace(traceObj);
181+
addTraceObj(traceObj);
182+
finishTrace();
152183
return;
153184
}
154185

@@ -160,7 +191,14 @@ public void traceRoute(final OnTraceListener onTraceListener) {
160191
} catch (Exception e) {
161192
onTraceListener.onError("Native ping command failed");
162193
}
194+
}
163195

196+
private void addTraceObj(TraceObj traceObj){
197+
traceObjs.add(traceObj);
198+
onTraceListener.onTrace(traceObj);
199+
}
200+
private void finishTrace(){
201+
onTraceListener.onFinishTrace(traceObjs);
164202
}
165203

166204
}

0 commit comments

Comments
 (0)