Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[#4](https://github.com/netsec-ethz/scion-java-multiping/pull/4)
- Added path probing for showpaths and --no-probe
[#5](https://github.com/netsec-ethz/scion-java-multiping/pull/5)
- Added path probing for ping and --healthy-only
[#6](https://github.com/netsec-ethz/scion-java-multiping/pull/6)

### Fixed

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/scion/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ private static void printUsage() {
println(" jpan-cli [command]");
println();
println("Available commands: ");
println(" address Show (one of) this host's SCION address(es)");
println(" help Help about any command");
println(" ping Test connectivity to a remote SCION host using SCMP echo packets");
println(" address Show (one of) this host's SCION address(es)");
println(" help Help about any command");
println(" ping Test connectivity to a remote SCION host using SCMP echo packets");
println(" ping-responder Starting a server that responds to incoming echo requests.");
println(" showpaths Display paths to a SCION AS");
println(" traceroute Trace the SCION route to a remote SCION AS using SCMP traceroute");
println(" version Show the SCION version information");
println(" showpaths Display paths to a SCION AS");
println(" traceroute Trace the SCION route to a remote SCION AS using SCMP traceroute");
println(" version Show the SCION version information");
println("");
println("Flags:");
println(" -h, --help help for jpan-cli");
Expand Down
43 changes: 36 additions & 7 deletions src/main/java/org/scion/cli/Ping.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import org.scion.cli.util.Errors;
import org.scion.cli.util.ExitCodeException;
import org.scion.cli.util.Prober;
import org.scion.cli.util.Util;
import org.scion.jpan.*;
import org.scion.jpan.internal.ScionAddress;
Expand All @@ -45,6 +44,8 @@ public class Ping {
private String dstUrl;
private InetSocketAddress daemon;
private int timeoutMs = 1000;
private boolean healthyOnly = false;
private final Random rnd = new Random();

public static void main(String... args) {
handleExit(() -> new Ping().run(args));
Expand All @@ -61,13 +62,20 @@ public void run(String... args) throws IOException {
}
try {
ScionService service = Scion.defaultService();
List<Path> paths;
if (dstUrl != null) {
run(service.lookupPaths(dstUrl, Constants.SCMP_PORT));
paths = service.lookupPaths(dstUrl, Constants.SCMP_PORT);
} else {
run(
paths =
service.getPaths(
dstAddress.getIsdAs(), dstAddress.getInetAddress(), Constants.SCMP_PORT));
dstAddress.getIsdAs(), dstAddress.getInetAddress(), Constants.SCMP_PORT);
}
if (paths.isEmpty()) {
String src = ScionUtil.toStringIA(service.getLocalIsdAs());
String dst = ScionUtil.toStringIA(dstAddress.getIsdAs());
throw new ExitCodeException(2, "No path found from " + src + " to " + dst);
}
run(paths);
} finally {
Scion.closeDefault();
}
Expand All @@ -92,6 +100,9 @@ private void parseArgs(String[] argsArray) {
case "--help":
Cli.printUsagePing();
throw new ExitCodeException(0);
case "--healthy-only":
healthyOnly = true;
break;
case "--interval":
intervalMs = parseInt("interval", args);
break;
Expand Down Expand Up @@ -133,7 +144,25 @@ private void parseArgs(String[] argsArray) {
}

private void run(List<Path> paths) throws IOException {
Path path = paths.get(0);
Path path;
if (healthyOnly) {
Map<Integer, Prober.Status> isActive = Prober.probe(localPort, timeoutMs, paths);
List<Path> newPaths = new ArrayList<>();
for (int i = 0; i < paths.size(); i++) {
if (isActive.getOrDefault(i, Prober.Status.Unknown) == Prober.Status.Alive) {
newPaths.add(paths.get(i));
}
}
if (newPaths.isEmpty()) {
String src = ScionUtil.toStringIA(Scion.defaultService().getLocalIsdAs());
String dst = ScionUtil.toStringIA(dstAddress.getIsdAs());
throw new ExitCodeException(2, "No path found from " + src + " to " + dst);
}
path = newPaths.get(rnd.nextInt(paths.size()));
} else {
path = paths.get(0);
}

ByteBuffer data = ByteBuffer.allocate(payloadSize);
for (int i = 0; i < payloadSize; i++) {
data.put((byte) i);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/scion/cli/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import org.scion.jpan.Scion;
import org.scion.jpan.ScionService;
import org.scion.jpan.ScionUtil;
import org.scion.jpan.internal.IPHelper;
import org.scion.jpan.internal.ScionAddress;
Expand All @@ -31,9 +34,18 @@ public class Util {
public static boolean PRINT = true;
public static boolean DELAYED_PRINT = false; // print only at newlines
private static final StringBuilder sb = new StringBuilder();
private static Supplier<ScionService> serviceSupplier = Scion::defaultService;

private Util() {}

public static void setServiceSupplier(Supplier<ScionService> serviceSupplier) {
Util.serviceSupplier = serviceSupplier;
}

public static ScionService defaultService() {
return serviceSupplier.get();
}

public static void sleep(long millis) {
try {
Thread.sleep(millis);
Expand Down
Loading