-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPing.java
More file actions
205 lines (191 loc) · 6.67 KB
/
Ping.java
File metadata and controls
205 lines (191 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2023 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.scion.cli;
import static org.scion.cli.util.Util.*;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.scion.cli.util.Errors;
import org.scion.cli.util.ExitCodeException;
import org.scion.cli.util.Util;
import org.scion.jpan.*;
import org.scion.jpan.internal.ScionAddress;
/**
* This demo mimics the "scion ping" command available in scionproto (<a
* href="https://github.com/scionproto/scion">...</a>).
*/
public class Ping {
private Integer localPort = -1;
private int count = 10;
private int intervalMs = 1000;
private boolean startShim = false;
private long localIsdAs = 0;
private InetAddress localIP = null;
private int payloadSize = 0;
private ScionAddress dstAddress;
private String dstUrl;
private InetSocketAddress daemon;
private int timeoutMs = 1000;
public static void main(String... args) {
handleExit(() -> new Ping().run(args));
}
public void run(String... args) throws IOException {
parseArgs(args);
System.setProperty(Constants.PROPERTY_SHIM, startShim ? "true" : "false"); // disable SHIM
if (daemon != null) {
System.setProperty(Constants.PROPERTY_DAEMON, daemon.toString());
}
if (dstUrl == null && dstAddress == null) {
throw new ExitCodeException(2, "Error: missing address or --url");
}
try {
ScionService service = Scion.defaultService();
if (dstUrl != null) {
run(service.lookupPaths(dstUrl, Constants.SCMP_PORT));
} else {
run(
service.getPaths(
dstAddress.getIsdAs(), dstAddress.getInetAddress(), Constants.SCMP_PORT));
}
} finally {
Scion.closeDefault();
}
}
private void parseArgs(String[] argsArray) {
List<String> args = new ArrayList<>(Arrays.asList(argsArray));
while (!args.isEmpty()) {
if (!args.get(0).startsWith("-")) {
if (dstAddress == null) {
dstAddress = parseScionAddress(args);
continue;
}
throw new ExitCodeException(2, Errors.UNEXPECTED_NON_FLAG + args.get(0));
}
switch (args.get(0)) {
case "-c":
case "--count":
count = parseInt("count", args);
break;
case "-h":
case "--help":
Cli.printUsagePing();
throw new ExitCodeException(0);
case "--interval":
intervalMs = parseInt("interval", args);
break;
case "--isd-as":
localIsdAs =
tryParse("isd-as", args.get(1), () -> ScionUtil.parseIA(parseString("isd-as", args)));
break;
case "-l":
case "--local":
localIP = parseIP("local", args);
break;
case "--log.level":
parseAndSetLogLevel(args);
break;
case "--port":
localPort = parseInt("port", args);
break;
case "-s":
case "--payload-size":
payloadSize = parseInt("payload-size", args);
break;
case "--shim":
startShim = true;
break;
case "--sciond":
daemon = parseAddress("sciond", args);
break;
case "--timeout":
timeoutMs = parseInt("timeout", args);
break;
case "--url":
dstUrl = parseString("url", args);
break;
default:
throw new ExitCodeException(2, Errors.UNKNOWN_OPTION + args.get(0));
}
args.remove(0);
}
}
private void run(List<Path> paths) throws IOException {
Path path = paths.get(0);
ByteBuffer data = ByteBuffer.allocate(payloadSize);
for (int i = 0; i < payloadSize; i++) {
data.put((byte) i);
}
String localAddress;
try (ScionDatagramChannel channel = ScionDatagramChannel.open()) {
channel.connect(path);
// We determine the address separately because SCMP will always have 0.0.0.0 as local address
localAddress = channel.getLocalAddress().getAddress().getHostAddress();
}
int nTimeouts = 0;
ScmpSender.Builder builder = ScmpSender.newBuilder();
if (localPort != null) {
builder.setLocalPort(localPort);
}
try (ScmpSender sender = builder.build()) {
sender.setTimeOut(timeoutMs);
println("Listening on port " + sender.getLocalAddress().getPort() + " ...");
println("Resolved local address: ");
println(" " + localAddress);
printPath(path);
for (int i = 0; i < count; i++) {
Scmp.EchoMessage msg = sender.sendEchoRequest(path, data);
if (i == 0) {
printHeader(path.getRemoteSocketAddress(), data, msg);
}
String millis = String.format("%.3f", msg.getNanoSeconds() / (double) 1_000_000);
String echoMsgStr = msg.getSizeReceived() + " bytes from ";
InetAddress addr = msg.getPath().getRemoteAddress();
echoMsgStr += ScionUtil.toStringIA(path.getRemoteIsdAs()) + "," + addr.getHostAddress();
echoMsgStr += ": scmp_seq=" + msg.getSequenceNumber();
if (msg.isTimedOut()) {
echoMsgStr += " Timed out after";
nTimeouts++;
}
echoMsgStr += " time=" + millis + "ms";
println(echoMsgStr);
if (i < count - 1) {
Util.sleep(intervalMs);
}
}
}
if (nTimeouts > 0) {
String msg = "";
if (localPort == null || localPort != 30041) {
msg = ". Try using \"--port 30041\"";
}
throw new ExitCodeException(1, "Number of timeouts: " + nTimeouts + msg);
}
}
private static void printPath(Path path) {
String nl = System.lineSeparator();
String sb = "Using path:" + nl + " Hops: " + ScionUtil.toStringPath(path.getMetadata());
sb += " MTU: " + path.getMetadata().getMtu();
sb += " NextHop: " + path.getMetadata().getInterface().getAddress() + nl;
println(sb);
}
private static void printHeader(ScionSocketAddress dst, ByteBuffer data, Scmp.EchoMessage msg) {
String sb = "PING " + ScionUtil.toStringIA(dst.getIsdAs()) + ",";
sb += dst.getHostString() + ":" + dst.getPort() + " pld=" + data.remaining();
sb += "B scion_pkt=" + msg.getSizeSent() + "B";
println(sb);
}
}