-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowpaths.java
More file actions
300 lines (279 loc) · 9.48 KB
/
Showpaths.java
File metadata and controls
300 lines (279 loc) · 9.48 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// 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.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.scion.cli.util.ExitCodeException;
import org.scion.jpan.*;
/**
* This demo mimics the "scion ping" command available in scionproto (<a
* href="https://github.com/scionproto/scion">...</a>). This demo also demonstrates different ways
* of connecting to a network: <br>
* - JUNIT_MOCK shows how to use the mock network in this library (for JUnit tests) <br>
* - SCION_PROTO shows how to connect to a local topology from the scionproto go implementation such
* as "tiny". Note that the constants for "minimal" differ somewhat from the scionproto topology.
* <br>
* - PRODUCTION shows different ways how to connect to the production network. Note: While the
* production network uses the dispatcher, the demo needs to use port 30041.
*
* <p>Commented out lines show alternative ways to connect or alternative destinations.
*/
public class Showpaths {
private static long localIsdAs = 0;
private static InetAddress localIP = null;
private static InetSocketAddress daemon;
private static Long isdAs;
private static boolean extended = false;
private static int maxPaths = 10;
public static void main(String... args) {
handleExit(() -> run(args));
}
public static void run(String... args) throws IOException {
parseArgs(args);
if (daemon != null) {
System.setProperty(Constants.PROPERTY_DAEMON, daemon.toString());
}
try {
run();
} finally {
Scion.closeDefault();
}
}
private static void parseArgs(String[] argsArray) {
List<String> args = new ArrayList<>(Arrays.asList(argsArray));
while (!args.isEmpty()) {
switch (args.get(0)) {
case "-e":
case "--extended":
extended = true;
break;
case "-h":
case "--help":
Cli.printUsageShowpaths();
throw new ExitCodeException(0);
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 "-m":
case "--maxpaths":
maxPaths = parseInt("--maxpaths", args);
break;
case "--sciond":
daemon = parseAddress("sciond", args);
break;
default:
if (isdAs == null) {
isdAs = parseIsdAs(args);
continue;
} else {
throw new ExitCodeException(2, "Unknown option: " + args.get(0));
}
}
args.remove(0);
}
if (isdAs == null) {
throw new ExitCodeException(2, "Please provide a destination ISD/AS.");
}
}
public static int run() throws IOException {
ScionService service = Scion.defaultService();
// dummy address
InetSocketAddress destinationAddress =
new InetSocketAddress(InetAddress.getLoopbackAddress(), 12345);
List<Path> paths = service.getPaths(isdAs, destinationAddress);
if (paths.isEmpty()) {
String src = ScionUtil.toStringIA(service.getLocalIsdAs());
String dst = ScionUtil.toStringIA(isdAs);
throw new IOException("No path found from " + src + " to " + dst);
}
println("Available paths to " + ScionUtil.toStringIA(isdAs));
int id = 0;
for (Path path : paths) {
if (id >= maxPaths) {
break;
}
String localIP;
try (ScionDatagramChannel channel = ScionDatagramChannel.open()) {
channel.connect(path);
localIP = channel.getLocalAddress().getAddress().getHostAddress();
}
PathMetadata meta = path.getMetadata();
String header = "[" + id++ + "] Hops: " + ScionUtil.toStringPath(meta);
if (extended) {
println(header);
printExtended(path, localIP);
} else {
String compact =
" MTU: "
+ meta.getMtu()
+ " NextHop: "
+ path.getFirstHopAddress().getHostString()
+ ":"
+ path.getFirstHopAddress().getPort()
+ " LocalIP: "
+ localIP;
println(header + compact);
}
}
return paths.size();
}
private static void printExtended(Path path, String localIP) {
StringBuilder sb = new StringBuilder();
String NL = System.lineSeparator();
PathMetadata meta = path.getMetadata();
sb.append(" MTU: ").append(meta.getMtu()).append(NL);
sb.append(" NextHop: ").append(path.getFirstHopAddress().getHostString()).append(NL);
sb.append(" Expires: ").append(toStringExpiry(meta)).append(NL);
sb.append(" Latency: ").append(toStringLatency(meta)).append(NL);
sb.append(" Bandwidth: ").append(toStringBandwidth(meta)).append(NL);
sb.append(" Geo: ").append(toStringGeo(meta)).append(NL);
sb.append(" LinkType: ").append(toStringLinkType(meta)).append(NL);
sb.append(" Notes: ").append(toStringNotes(meta)).append(NL);
sb.append(" SupportsEPIC: ").append(toStringEPIC(meta)).append(NL);
// TODO, see private/app/path/pathprobe/paths.go
sb.append(" Status: ").append("unknown").append(NL);
// TODO use destination IP from returned packet from probe
sb.append(" LocalIP: ").append(localIP).append(NL);
println(sb.toString());
}
private static String toStringExpiry(PathMetadata meta) {
Instant exp = Instant.ofEpochSecond(meta.getExpiration());
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z").withZone(ZoneId.of("UTC"));
Instant now = Instant.now();
long s = Duration.between(now, exp).getSeconds();
String ds = String.format("(%dh%02dm%02ds)", s / 3600, (s % 3600) / 60, (s % 60));
return formatter.format(exp) + " UTC " + ds;
}
private static String toStringLatency(PathMetadata meta) {
int latencyMs = 0;
boolean latencyComplete = true;
for (int l : meta.getLatencyList()) {
if (l >= 0) {
latencyMs += l;
} else {
latencyComplete = false;
}
}
if (latencyComplete) {
return latencyMs + "ms";
} else {
return ">" + latencyMs + "ms (information incomplete)";
}
}
private static String toStringBandwidth(PathMetadata meta) {
long bw = Long.MAX_VALUE;
boolean bwComplete = true;
for (long l : meta.getBandwidthList()) {
if (l > 0) {
bw = Math.min(bw, l);
} else {
bwComplete = false;
}
}
bw = bw == Long.MAX_VALUE ? 0 : bw;
String bwString = bw + "KBit/s";
if (!bwComplete) {
bwString += " (information incomplete)";
}
return bwString;
}
private static String toStringGeo(PathMetadata meta) {
StringBuilder s = new StringBuilder("[");
for (PathMetadata.GeoCoordinates g : meta.getGeoList()) {
if (s.length() > 1) {
s.append(" > ");
}
if (g.getLatitude() == 0 && g.getLongitude() == 0 && g.getAddress().isEmpty()) {
s.append("N/A");
} else {
s.append(g.getLatitude()).append(",").append(g.getLongitude());
String addr = g.getAddress().replace("\n", ", ");
s.append(" (\"").append(addr).append("\")");
}
}
s.append("]");
return s.toString();
}
private static String toStringLinkType(PathMetadata meta) {
StringBuilder s = new StringBuilder("[");
for (PathMetadata.LinkType lt : meta.getLinkTypeList()) {
if (s.length() > 1) {
s.append(", ");
}
switch (lt) {
case UNSPECIFIED:
s.append("unset");
break;
case DIRECT:
s.append("direct");
break;
case MULTI_HOP:
s.append("multihop");
break;
case OPEN_NET:
s.append("opennet");
break;
default:
s.append("unset");
break;
}
}
s.append("]");
return s.toString();
}
private static String toStringEPIC(PathMetadata meta) {
PathMetadata.EpicAuths ea = meta.getEpicAuths();
if (ea == null) {
return "false";
}
if (ea.getAuthLhvf() != null && ea.getAuthLhvf().length == 16) {
return "true";
}
if (ea.getAuthPhvf() != null && ea.getAuthPhvf().length == 16) {
return "true";
}
return "false";
}
private static String toStringNotes(PathMetadata meta) {
StringBuilder s = new StringBuilder("[");
int i = 0;
for (String note : meta.getNotesList()) {
if (note != null && !note.isEmpty()) {
if (s.length() > 1) {
s.append(", ");
}
long isdAs = meta.getInterfacesList().get(Math.max(0, i * 2 - 1)).getIsdAs();
s.append(ScionUtil.toStringIA(isdAs));
s.append(": \"").append(note).append("\"");
}
i++;
}
s.append("]");
return s.toString();
}
}