Skip to content

Commit

Permalink
Merge pull request #41 from robross0606/feature/ttl
Browse files Browse the repository at this point in the history
Added support for setting ping TTL.
  • Loading branch information
stealthcopter authored Jun 21, 2018
2 parents d47a50c + 34b5d62 commit d3d8f3b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
21 changes: 17 additions & 4 deletions library/src/main/java/com/stealthcopter/networktools/Ping.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stealthcopter.networktools;

import com.stealthcopter.networktools.ping.PingOptions;
import com.stealthcopter.networktools.ping.PingResult;
import com.stealthcopter.networktools.ping.PingStats;
import com.stealthcopter.networktools.ping.PingTools;
Expand Down Expand Up @@ -34,7 +35,7 @@ public interface PingListener {

private String addressString = null;
private InetAddress address;
private int timeOutMillis = 1000;
private final PingOptions pingOptions = new PingOptions();
private int delayBetweenScansMillis = 0;
private int times = 1;
private boolean cancelled = false;
Expand Down Expand Up @@ -74,7 +75,7 @@ public static Ping onAddress(InetAddress ia) {
*/
public Ping setTimeOutMillis(int timeOutMillis) {
if (timeOutMillis < 0) throw new IllegalArgumentException("Times cannot be less than 0");
this.timeOutMillis = timeOutMillis;
pingOptions.setTimeoutMillis(timeOutMillis);
return this;
}

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

/**
* Set the time to live
*
* @param timeToLive - the TTL for each ping
* @return this object to allow chaining
*/
public Ping setTimeToLive(int timeToLive) {
if (timeToLive < 1) throw new IllegalArgumentException("TTL cannot be less than 1");
pingOptions.setTimeToLive(timeToLive);
return this;
}

/**
* Set number of times to ping the address
*
Expand Down Expand Up @@ -145,7 +158,7 @@ public void cancel() {
public PingResult doPing() throws UnknownHostException {
cancelled = false;
resolveAddressString();
return PingTools.doPing(address, timeOutMillis);
return PingTools.doPing(address, pingOptions);
}

/**
Expand Down Expand Up @@ -183,7 +196,7 @@ public void run() {

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

if (pingListener != null) {
pingListener.onResult(pingResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public class PingNative {
private PingNative() {
}

public static PingResult ping(InetAddress host, int timeOutMillis) throws IOException, InterruptedException {
public static PingResult ping(InetAddress host, PingOptions pingOptions) throws IOException, InterruptedException {
PingResult pingResult = new PingResult(host);
StringBuilder echo = new StringBuilder();
Runtime runtime = Runtime.getRuntime();

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

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

Process proc = runtime.exec(pingCommand + " -c 1 -w " + timeoutSeconds + " " + address);
Process proc = runtime.exec(pingCommand + " -c 1 -w " + timeoutSeconds + " -w" + ttl + " " + address);
proc.waitFor();
int exit = proc.exitValue();
String pingError;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.stealthcopter.networktools.ping;

public class PingOptions {
private int timeoutMillis;
private int timeToLive;

public PingOptions() {
timeToLive = 128;
timeoutMillis = 1000;
}

public int getTimeoutMillis() {
return timeoutMillis;
}

public void setTimeoutMillis(int timeoutMillis) {
this.timeoutMillis = Math.max(timeoutMillis, 1000);
}

public int getTimeToLive() {
return timeToLive;
}

public void setTimeToLive(int timeToLive) {
this.timeToLive = Math.max(timeToLive, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;

/**
* Created by mat on 09/12/15.
Expand All @@ -18,14 +19,14 @@ private PingTools() {
* on failure.
*
* @param ia - address to ping
* @param timeOutMillis - timeout in millisecdonds
* @param pingOptions - ping command options
* @return - the ping results
*/
public static PingResult doPing(InetAddress ia, int timeOutMillis) {
public static PingResult doPing(InetAddress ia, PingOptions pingOptions) {

// Try native ping first
try {
return PingTools.doNativePing(ia, timeOutMillis);
return PingTools.doNativePing(ia, pingOptions);
} catch (InterruptedException e) {
PingResult pingResult = new PingResult(ia);
pingResult.isReachable = false;
Expand All @@ -35,21 +36,21 @@ public static PingResult doPing(InetAddress ia, int timeOutMillis) {
}

// Fallback to java based ping
return PingTools.doJavaPing(ia, timeOutMillis);
return PingTools.doJavaPing(ia, pingOptions);
}


/**
* Perform a ping using the native ping binary
*
* @param ia - address to ping
* @param timeOutMillis - timeout in millisecdonds
* @param pingOptions - ping command options
* @return - the ping results
* @throws IOException
* @throws InterruptedException
*/
public static PingResult doNativePing(InetAddress ia, int timeOutMillis) throws IOException, InterruptedException {
return PingNative.ping(ia, timeOutMillis);
public static PingResult doNativePing(InetAddress ia, PingOptions pingOptions) throws IOException, InterruptedException {
return PingNative.ping(ia, pingOptions);
}

/**
Expand All @@ -58,14 +59,14 @@ public static PingResult doNativePing(InetAddress ia, int timeOutMillis) throws
* on port 7 (Echo) of the remote host.
*
* @param ia - address to ping
* @param timeOutMillis - timeout in millisecdonds
* @param pingOptions - ping command options
* @return - the ping results
*/
public static PingResult doJavaPing(InetAddress ia, int timeOutMillis) {
public static PingResult doJavaPing(InetAddress ia, PingOptions pingOptions) {
PingResult pingResult = new PingResult(ia);
try {
long startTime = System.nanoTime();
final boolean reached = ia.isReachable(timeOutMillis);
final boolean reached = ia.isReachable(null, pingOptions.getTimeToLive(), pingOptions.getTimeoutMillis());
pingResult.timeTaken = (System.nanoTime() - startTime) / 1e6f;
pingResult.isReachable = reached;
if (!reached) pingResult.error = "Timed Out";
Expand Down

0 comments on commit d3d8f3b

Please sign in to comment.