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

Skip to content

Commit d3d8f3b

Browse files
Merge pull request stealthcopter#41 from robross0606/feature/ttl
Added support for setting ping TTL.
2 parents d47a50c + 34b5d62 commit d3d8f3b

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

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

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

3+
import com.stealthcopter.networktools.ping.PingOptions;
34
import com.stealthcopter.networktools.ping.PingResult;
45
import com.stealthcopter.networktools.ping.PingStats;
56
import com.stealthcopter.networktools.ping.PingTools;
@@ -34,7 +35,7 @@ public interface PingListener {
3435

3536
private String addressString = null;
3637
private InetAddress address;
37-
private int timeOutMillis = 1000;
38+
private final PingOptions pingOptions = new PingOptions();
3839
private int delayBetweenScansMillis = 0;
3940
private int times = 1;
4041
private boolean cancelled = false;
@@ -74,7 +75,7 @@ public static Ping onAddress(InetAddress ia) {
7475
*/
7576
public Ping setTimeOutMillis(int timeOutMillis) {
7677
if (timeOutMillis < 0) throw new IllegalArgumentException("Times cannot be less than 0");
77-
this.timeOutMillis = timeOutMillis;
78+
pingOptions.setTimeoutMillis(timeOutMillis);
7879
return this;
7980
}
8081

@@ -91,6 +92,18 @@ public Ping setDelayMillis(int delayBetweenScansMillis) {
9192
return this;
9293
}
9394

95+
/**
96+
* Set the time to live
97+
*
98+
* @param timeToLive - the TTL for each ping
99+
* @return this object to allow chaining
100+
*/
101+
public Ping setTimeToLive(int timeToLive) {
102+
if (timeToLive < 1) throw new IllegalArgumentException("TTL cannot be less than 1");
103+
pingOptions.setTimeToLive(timeToLive);
104+
return this;
105+
}
106+
94107
/**
95108
* Set number of times to ping the address
96109
*
@@ -145,7 +158,7 @@ public void cancel() {
145158
public PingResult doPing() throws UnknownHostException {
146159
cancelled = false;
147160
resolveAddressString();
148-
return PingTools.doPing(address, timeOutMillis);
161+
return PingTools.doPing(address, pingOptions);
149162
}
150163

151164
/**
@@ -183,7 +196,7 @@ public void run() {
183196

184197
// times == 0 is the case that we can continuous scanning
185198
while (noPings > 0 || times == 0) {
186-
PingResult pingResult = PingTools.doPing(address, timeOutMillis);
199+
PingResult pingResult = PingTools.doPing(address, pingOptions);
187200

188201
if (pingListener != null) {
189202
pingListener.onResult(pingResult);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public class PingNative {
1616
private PingNative() {
1717
}
1818

19-
public static PingResult ping(InetAddress host, int timeOutMillis) throws IOException, InterruptedException {
19+
public static PingResult ping(InetAddress host, PingOptions pingOptions) throws IOException, InterruptedException {
2020
PingResult pingResult = new PingResult(host);
2121
StringBuilder echo = new StringBuilder();
2222
Runtime runtime = Runtime.getRuntime();
2323

24-
int timeoutSeconds = timeOutMillis / 1000;
25-
if (timeoutSeconds < 0) timeoutSeconds = 1;
24+
int timeoutSeconds = Math.max(pingOptions.getTimeoutMillis() / 1000, 1);
25+
int ttl = Math.max(pingOptions.getTimeToLive(), 1);
2626

2727
String address = host.getHostAddress();
2828
String pingCommand = "ping";
@@ -40,7 +40,7 @@ public static PingResult ping(InetAddress host, int timeOutMillis) throws IOExce
4040
address = host.getHostName();
4141
}
4242

43-
Process proc = runtime.exec(pingCommand + " -c 1 -w " + timeoutSeconds + " " + address);
43+
Process proc = runtime.exec(pingCommand + " -c 1 -w " + timeoutSeconds + " -w" + ttl + " " + address);
4444
proc.waitFor();
4545
int exit = proc.exitValue();
4646
String pingError;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.stealthcopter.networktools.ping;
2+
3+
public class PingOptions {
4+
private int timeoutMillis;
5+
private int timeToLive;
6+
7+
public PingOptions() {
8+
timeToLive = 128;
9+
timeoutMillis = 1000;
10+
}
11+
12+
public int getTimeoutMillis() {
13+
return timeoutMillis;
14+
}
15+
16+
public void setTimeoutMillis(int timeoutMillis) {
17+
this.timeoutMillis = Math.max(timeoutMillis, 1000);
18+
}
19+
20+
public int getTimeToLive() {
21+
return timeToLive;
22+
}
23+
24+
public void setTimeToLive(int timeToLive) {
25+
this.timeToLive = Math.max(timeToLive, 1);
26+
}
27+
}

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.net.InetAddress;
5+
import java.net.NetworkInterface;
56

67
/**
78
* Created by mat on 09/12/15.
@@ -18,14 +19,14 @@ private PingTools() {
1819
* on failure.
1920
*
2021
* @param ia - address to ping
21-
* @param timeOutMillis - timeout in millisecdonds
22+
* @param pingOptions - ping command options
2223
* @return - the ping results
2324
*/
24-
public static PingResult doPing(InetAddress ia, int timeOutMillis) {
25+
public static PingResult doPing(InetAddress ia, PingOptions pingOptions) {
2526

2627
// Try native ping first
2728
try {
28-
return PingTools.doNativePing(ia, timeOutMillis);
29+
return PingTools.doNativePing(ia, pingOptions);
2930
} catch (InterruptedException e) {
3031
PingResult pingResult = new PingResult(ia);
3132
pingResult.isReachable = false;
@@ -35,21 +36,21 @@ public static PingResult doPing(InetAddress ia, int timeOutMillis) {
3536
}
3637

3738
// Fallback to java based ping
38-
return PingTools.doJavaPing(ia, timeOutMillis);
39+
return PingTools.doJavaPing(ia, pingOptions);
3940
}
4041

4142

4243
/**
4344
* Perform a ping using the native ping binary
4445
*
4546
* @param ia - address to ping
46-
* @param timeOutMillis - timeout in millisecdonds
47+
* @param pingOptions - ping command options
4748
* @return - the ping results
4849
* @throws IOException
4950
* @throws InterruptedException
5051
*/
51-
public static PingResult doNativePing(InetAddress ia, int timeOutMillis) throws IOException, InterruptedException {
52-
return PingNative.ping(ia, timeOutMillis);
52+
public static PingResult doNativePing(InetAddress ia, PingOptions pingOptions) throws IOException, InterruptedException {
53+
return PingNative.ping(ia, pingOptions);
5354
}
5455

5556
/**
@@ -58,14 +59,14 @@ public static PingResult doNativePing(InetAddress ia, int timeOutMillis) throws
5859
* on port 7 (Echo) of the remote host.
5960
*
6061
* @param ia - address to ping
61-
* @param timeOutMillis - timeout in millisecdonds
62+
* @param pingOptions - ping command options
6263
* @return - the ping results
6364
*/
64-
public static PingResult doJavaPing(InetAddress ia, int timeOutMillis) {
65+
public static PingResult doJavaPing(InetAddress ia, PingOptions pingOptions) {
6566
PingResult pingResult = new PingResult(ia);
6667
try {
6768
long startTime = System.nanoTime();
68-
final boolean reached = ia.isReachable(timeOutMillis);
69+
final boolean reached = ia.isReachable(null, pingOptions.getTimeToLive(), pingOptions.getTimeoutMillis());
6970
pingResult.timeTaken = (System.nanoTime() - startTime) / 1e6f;
7071
pingResult.isReachable = reached;
7172
if (!reached) pingResult.error = "Timed Out";

0 commit comments

Comments
 (0)