-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathasviz.js
654 lines (599 loc) · 18.8 KB
/
asviz.js
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
* Copyright 2017 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.
*/
var selectedService;
var colorServiceSelect = "black";
var colorServiceDeselect = "gray";
var colorPaths = "black";
var colorSegCore = "red";
var colorSegUp = "green";
var colorSegDown = "blue";
// AS Topology Graph
var sv_link_dist = 75; // link distance
var ft_h = 14; // font height
var sv_tx_dy = 25; // service node label y-offset
var as_r = 130; // AS node radius
var sv_r = 11; // service node radius
var as_st = 6; // AS node stroke
var sv_st = 2; // service node stroke
/*
* Focuses the active tab to the tab label.
*/
function activateTab(tab) {
$('.tab-pane a[href="#' + tab + '"]').tab('show');
};
/*
* Returns the tab label of the active tab
*/
function getTab() {
return $("ul#sampleTabs li.active")
}
/*
* Creates on-click handler that will draw/hide selected path arcs.
*/
function setupPathSelection() {
// handle path graph-only selection and color
$("#as-iflist ul > li").click(function() {
var type = $(this).attr("seg-type");
var idx = parseInt($(this).attr("seg-num"));
setPaths(type, idx, this.className == "open");
});
}
function setPaths(type, idx, open) {
if (open) {
console.log(type + idx + ' opened');
var num = idx + 1;
if (type == 'CORE') {
addSegments(resCore, idx, num, colorSegCore, type);
} else if (type == 'DOWN') {
addSegments(resDown, idx, num, colorSegDown, type);
} else if (type == 'UP') {
addSegments(resUp, idx, num, colorSegUp, type);
} else if (type == 'PATH') {
var latencies = getPathLatencyMin(formatPathString(resPath, idx,
type));
addPaths(resPath, idx, num, colorPaths, type, latencies);
}
self.segType = type;
self.segNum = idx;
} else {
console.log(type + idx + ' closed');
removePaths();
self.segType = undefined;
self.segNum = undefined;
}
}
/*
* Produces a formatted path string for purposes of matching like: [1-ff00:0:111
* 76>49 1-ff00:0:110 49>53 1-ff00:0:112]. Returns empty string for no path.
*/
function formatPathString(res, idx, type) {
if (typeof idx === 'undefined') {
return '';
}
var hops = res.if_lists[idx].interfaces;
if (hops === 0 || type != 'PATH') {
return '';
}
var path = "[";
for (var i = 0; i < hops.length; i += 2) {
var prev = hops[i];
var next = hops[i + 1];
if (i > 0) {
path += ' ';
}
path += prev.ISD + '-' + prev.AS + ' ' + prev.IFID + '>' + next.IFID;
if (i == (hops.length - 2)) {
path += ' ' + next.ISD + '-' + next.AS;
}
}
path += "]";
return path;
}
function formatPathStringAll(res, type) {
var paths = "";
for (var i = 0; i < res.if_lists.length; i++) {
var path = formatPathString(res, i, type);
if (path != "") {
if (i > 0) {
paths += ",";
}
paths += formatPathString(res, i, type);
}
}
return paths;
}
/*
* Adds D3 forwarding path links with arrows and a title to paths graph.
*/
function addPaths(res, idx, num, color, type, latencies) {
if (graphPath) {
drawPath(res, idx, color, latencies);
if (res.if_lists[idx].expTime) {
drawTitle(type + ' ' + num, color, res.if_lists[idx].expTime);
} else {
drawTitle(type + ' ' + num, color);
}
}
if (wv_map) {
updateGMapAsLinks(res, idx, color);
}
}
/*
* Adds D3 segment links with arrows, title, and timer to paths graph.
*/
function addSegments(res, idx, num, color, type) {
if (graphPath) {
drawPath(res, idx, color);
drawTitle(type + ' SEGMENT ' + num, color, res.if_lists[idx].expTime);
}
if (wv_map) {
updateGMapAsLinks(res, idx, color);
}
}
/*
* Removes D3 path links, title, and timer from paths graph.
*/
function removePaths() {
if (graphPath) {
restorePath();
removeTitle();
}
if (wv_map) {
updateGMapAsLinks();
}
}
/*
* Test incoming segments for proper beaconing order, and invert when needed.
* Since modifying up segments, can effect analysis of core segments, we analyze
* in this order: paths, core, up, down.
*/
function orderPaths(src, dst) {
// fwd paths should begin with src ia
if (isIAHead(resPath, src)) {
resPath.if_lists = invertInterfaces(resPath.if_lists);
}
if (resUp.if_lists.length > 0) {
// if up segments, core should match one ia from up segments
if (isCoreInverted(resCore, resUp)) {
resCore.if_lists = invertInterfaces(resCore.if_lists);
}
} else {
// if no up segments, core segments begin with src ia
if (isIAHead(resCore, src)) {
resCore.if_lists = invertInterfaces(resCore.if_lists);
}
}
// up segments should begin with src ia
if (isIAHead(resUp, src)) {
resUp.if_lists = invertInterfaces(resUp.if_lists);
}
// down segments should end with dst ia
if (isIATail(resDown, dst)) {
resDown.if_lists = invertInterfaces(resDown.if_lists);
}
}
/*
* Utility to invert the hop order.
*/
function invertInterfaces(if_lists) {
for (i in if_lists) {
if_lists[i].interfaces.reverse();
}
return if_lists;
}
/*
* Discover if IA is at the beginning of this set of segments.
*/
function isIAHead(segs, ia) {
reverse = false;
for (s in segs.if_lists) {
head = segs.if_lists[s].interfaces[0];
if ((head.ISD + '-' + head.AS) != ia) {
reverse = true;
}
}
return reverse;
}
/*
* Discover if IA is at the end of this set of segments.
*/
function isIATail(segs, ia) {
reverse = false;
for (s in segs.if_lists) {
tail = segs.if_lists[s].interfaces.slice(-1)[0];
if ((tail.ISD + '-' + tail.AS) != ia) {
reverse = true;
}
}
return reverse;
}
/*
* Discover if core segment heads can be found within up segments.
*/
function isCoreInverted(csegs, usegs) {
reverse = true;
for (c in csegs.if_lists) {
head = csegs.if_lists[c].interfaces[0];
for (u in usegs.if_lists) {
for (i in usegs.if_lists[u].interfaces) {
_if = usegs.if_lists[u].interfaces[i];
if ((head.ISD + '-' + head.AS) == (_if.ISD + '-' + _if.AS)) {
reverse = false;
}
}
}
}
return reverse;
}
/*
* Handle open and close of data tree suggested by stackoverflow.com/a/38765843
* and stackoverflow.com/a/38765843.
*/
function setupListTree() {
var tree = document.querySelectorAll('ul.tree a:not(:last-child)');
for (var i = 0; i < tree.length; i++) {
tree[i].addEventListener('click', function(e) {
e.preventDefault();
var parent = e.target.parentElement;
var classList = parent.classList;
var closeAllOpenSiblings = function() {
// MWF: ':scope .open' fails in MS IE/Edge, removed ':scope'
var opensubs = parent.parentElement.querySelectorAll('.open');
for (var i = 0; i < opensubs.length; i++) {
opensubs[i].classList.remove('open');
}
}
if (classList.contains("open")) {
classList.remove('open');
} else {
closeAllOpenSiblings();
classList.add('open');
}
});
}
}
/*
* Translates incoming AS topology data to D3-compatible json.
*/
function parseTopo(topo) {
var data = {};
data.links = topo.links.map(function(value) {
var nodes = topo.nodes;
var link = {};
link.type = value.type || 'default';
for (i = 0; i < nodes.length; i++) {
if (nodes[i].name === value.source) {
link.source = i;
}
if (nodes[i].name === value.target) {
link.target = i;
}
}
return link;
});
data.nodes = topo.nodes;
return data;
};
/*
* Initializes AS topology graph, its handlers, and renders it.
*/
function drawAsTopo(div_id, json_as_topo, width, height) {
var graphAs = parseTopo(json_as_topo);
console.log(JSON.stringify(graphAs));
var svgAs = d3.select("#" + div_id).append("svg").attr("height", height)
.attr("width", width);
var color = d3.scale.category10();
var nodes = graphAs.nodes;
var links = graphAs.links;
var texts = svgAs.selectAll("text").data(nodes).enter().append("text")
.attr("dy", sv_tx_dy).attr("text-anchor", "middle").attr("fill",
"black").attr("font-family", "sans-serif").attr(
"font-size", ft_h + "px").text(function(d) {
return d.name;
});
var colaAs = cola.d3adaptor().size([ width, height ]).linkDistance(
sv_link_dist).avoidOverlaps(true);
colaAs.nodes(nodes).links(links).start(30)
var edges = svgAs.selectAll("line").data(links).enter().append("line")
.style("stroke-linecap", "round").attr("class", function(d) {
return "link " + d.type;
}).attr("marker-end", "url(#end)");
var nodes = svgAs.selectAll("circle").data(nodes).enter().append("circle")
.attr("r", function(d) {
return (d.type == "root") ? as_r : sv_r - 1;
}).on("click", onAsServiceClick).attr("opacity", function(d) {
return 0.5;
}).style("fill", function(d, i) {
return (d.type == "root") ? "none" : color(d.group);
}).style("stroke-width", function(d) {
return (d.type == "root") ? as_st : sv_st;
}).style("stroke", function(d) {
return colorServiceDeselect;
}).call(colaAs.drag);
colaAs.on("tick", function() {
edges.attr("x1", function(d) {
return Math.max(sv_r, Math.min(width - sv_r, d.source.x));
}).attr("y1", function(d) {
return Math.max(sv_r, Math.min(height - sv_r - ft_h, d.source.y));
}).attr("x2", function(d) {
return Math.max(sv_r, Math.min(width - sv_r, d.target.x));
}).attr("y2", function(d) {
return Math.max(sv_r, Math.min(height - sv_r - ft_h, d.target.y));
});
nodes.attr("cx", function(d) {
return d.x = Math.max(sv_r, Math.min(width - sv_r, d.x));
}).attr("cy", function(d) {
return d.y = Math.max(sv_r, Math.min(height - sv_r - ft_h, d.y));
});
texts.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
});
}
/*
* Creates handler to display AS topology service details per-click.
*/
function onAsServiceClick(d) {
ser_name = d.name;
document.getElementById("as-selection").innerHTML = ser_name;
// display node details
console.log(d);
$('#service_table tbody > tr').remove();
var k;
var graph_vars = [ 'x', 'y', 'px', 'py', 'fixed', 'weight', 'index',
'group', 'variable', 'bounds' ];
for (k in d) {
if (typeof d[k] !== 'function' && !graph_vars.includes(k)) {
$('#service_table').find('tbody').append(
"<tr><td>" + k + "</td><td>" + d[k] + "</td></tr>");
}
}
// allow root AS and all services to be highlighted
if (!selectedService) {
selectedService = this;
updateNodeSelected(true, selectedService);
} else {
updateNodeSelected(false, selectedService);
selectedService = this;
updateNodeSelected(true, selectedService);
}
}
/*
* Maintains highlight status for selected service nodes.
*/
function updateNodeSelected(isSelected, selected) {
d3.select(selected).style('stroke',
isSelected ? colorServiceSelect : colorServiceDeselect);
d3.select(selected).attr('opacity', isSelected ? 1 : 0.5);
}
var svc_pre = {
0 : "unset",
1 : "bs",
2 : "ps",
3 : "cs",
4 : "sb",
5 : "sig",
6 : "ds",
};
var svc_service = {
0 : "unset",
1 : "BEACON",
2 : "PATH",
3 : "CERTIFICATE",
4 : "SIBRA",
5 : "GATEWAY",
6 : "DISCOVERY",
};
function get_json_as_topo(data, width, height) {
var nodes = [];
var links = [];
var gidx = 0;
if (typeof data.as_info === 'undefined') {
return {
"nodes" : [],
"links" : []
};
}
var a = data.as_info.Entries[0];
var ia = iaRaw2Read(a.RawIsdas);
var iaf = iaRaw2File(a.RawIsdas);
// asinfo: add AS root node
var asnode = {};
asnode.name = ia;
asnode.service = "ISD-AS";
asnode.mtu = a.Mtu;
asnode.is_core_as = a.IsCore;
asnode.type = "root";
asnode.group = gidx;
// fix root in center
asnode.x = width / 2;
asnode.y = height / 2;
asnode.fixed = true;
nodes.push(asnode);
var sinfos = data.svc_info.Entries;
for (s in sinfos) {
var st = sinfos[s].ServiceType;
gidx += 1;
var hinfos = sinfos[s].HostInfos;
for (h in hinfos) {
// svcinfo: add all service nodes
var snode = {};
snode.name = svc_pre[st] + iaf + "-" + (parseInt(h) + 1);
snode.service = svc_service[st];
snode.ttl = sinfos[s].Ttl;
if (hinfos[h].Addrs.Ipv4) {
snode.ipv4 = ipv4Raw2Read(hinfos[h].Addrs.Ipv4);
}
if (hinfos[h].Addrs.Ipv6) {
snode.ipv6 = ipv6Raw2Read(hinfos[h].Addrs.Ipv6);
}
snode.port = hinfos[h].Port;
snode.type = "service";
snode.group = gidx;
nodes.push(snode);
// svcinfo: add internal links to service nodes
var slink = {};
slink.source = asnode.name;
slink.target = snode.name;
slink.type = "as-in";
links.push(slink);
}
}
// consider each unique ip/port as a single border router, which may
// have multiple interfaces
gidx += 1;
var brs = {};
var iinfos = data.if_info.RawEntries;
for (i in iinfos) {
var ipv4, ipv6, port;
if (iinfos[i].HostInfo.Addrs.Ipv4) {
ipv4 = ipv4Raw2Read(iinfos[i].HostInfo.Addrs.Ipv4);
}
if (iinfos[i].HostInfo.Addrs.Ipv6) {
ipv6 = ipv6Raw2Read(iinfos[i].HostInfo.Addrs.Ipv6);
}
port = iinfos[i].HostInfo.Port;
var brkey = ipv4 + "-" + ipv6 + "-" + port;
if (!brs.hasOwnProperty(brkey)) {
// ifinfo: add all interface BR nodes
var snode = {};
brs[brkey] = "br" + iaf + "-" + (Object.keys(brs).length + 1);
snode.name = brs[brkey];
snode.service = "BORDER";
if (iinfos[i].HostInfo.Addrs.Ipv4) {
snode.ipv4 = ipv4;
}
if (iinfos[i].HostInfo.Addrs.Ipv6) {
snode.ipv6 = ipv6;
}
snode.port = port;
snode.if_id = iinfos[i].IfID;
snode.type = "router";
snode.group = gidx;
nodes.push(snode);
// ifinfo: add all interface BR links
var slink = {};
slink.source = asnode.name;
slink.target = snode.name;
slink.type = "as-in";
links.push(slink);
}
// ifinfo: add all interface dest AS nodes
var lnode = {};
lnode.name = "(" + iinfos[i].IfID + ")";
lnode.service = "ISD-AS";
lnode.type = "interface";
lnode.link_type = "parent";
lnode.group = 0;
nodes.push(lnode);
// ifinfo: add external links BR to dest AS nodes
var llink = {};
llink.source = brs[brkey];
llink.target = lnode.name;
llink.type = "as-parent";
links.push(llink);
}
var graph = {};
graph.nodes = nodes;
graph.links = links;
return graph;
}
function json2html(json, root) {
var html = "<ul" + (root ? " class='tree'" : "") + ">";
// var html = "<ul class='tree'>";
jQuery.each(json, function(key, value) {
html += "<li><a href='#'><b>" + key + "</b></a>";
if (typeof value !== "object") {
if (key.includes("Time")) {
// make time readable
var t = new Date(0);
t.setUTCSeconds(value);
html += ": " + t.toLocaleDateString() + " "
+ t.toLocaleTimeString();
} else {
html += ": " + value;
}
} else {
html += json2html(value, false);
}
})
return html + "</ul>";
}
function showError(err) {
if (err && err != '') {
console.error(err);
$("#as-error").html(err);
$("#as-error").css('color', 'red');
} else {
$("#as-error").empty();
}
}
function htmlDecode(htmlIn) {
var e = document.createElement('div');
e.innerHTML = htmlIn;
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}
function ab2str(ab) {
return String.fromCharCode.apply(null, new Uint8Array(ab));
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i = 0; i < str.length; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function toBytesUInt32(num) {
var ab = new ArrayBuffer(4);
var view = new DataView(ab);
view.setInt32(0, num, false);
return ab;
}
function fromBytesUInt32(ab) {
var view = new DataView(ab);
return view.getInt32(0, false);
}
function reduceHex(hex) {
return parseInt(hex, 16).toString(16);
}
function iaRawCompose(rawIa) {
var parts = [];
var hex = rawIa.toString(16);
var isd = parseInt(hex.slice(0, -12), 16);
var as1 = reduceHex(hex.slice(-12, -8));
var as2 = reduceHex(hex.slice(-8, -4));
var as3 = reduceHex(hex.slice(-4));
return [ isd, as1, as2, as3 ];
}
function iaRaw2Read(rawIa) {
var parts = iaRawCompose(rawIa);
return parts[0] + '-' + parts.slice(1, 4).join(':');
}
function iaRaw2File(rawIa) {
var parts = iaRawCompose(rawIa);
return parts[0] + '-' + parts.slice(1, 4).join('_');
}
function ipv4Raw2Read(rawIpv4) {
var b = atob(rawIpv4); // decode
var a = new Uint8Array(str2ab(b));
var ipv4 = a.join('.');
return ipv4;
}
// TODO: needs decoding
function ipv6Raw2Read(rawIpv6) {
return rawIpv6;
}