-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpytnt.py
More file actions
executable file
·1548 lines (1289 loc) · 50.9 KB
/
Copy pathpytnt.py
File metadata and controls
executable file
·1548 lines (1289 loc) · 50.9 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
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
implementation of TNT
"""
import sys
import os
import stat
from enum import Enum, Flag, auto
from scamper import ScamperCtrl, ScamperFile, ScamperTrace, ScamperPing,ScamperAddr
import argparse
import json
import time
from datetime import timedelta, datetime
import logging
from collections import defaultdict
import bz2
from multiprocessing import Pool
tunnels = {}
tracetests = {}
TUNNEL_LIFETIME = 604800
PING_LIFETIME = 604800
class IngressStatus(Enum):
NULL = 0
START = 1
class TntHopType(Flag):
INGR = auto() # ingress LSR
EGR = auto() # egress LSR
INTERN = auto() # internal LSR
EXP = auto() # in explicit tunnel
IMP_QT = auto() # in implicit qttl tun
IMP_UT = auto() # in implicit uturn tun
OPA = auto() # in opaque tunnel
INV = auto() # in invisible tunnel
class TntHopDisc(Flag):
FRPLA = auto() # FRPLA trigger
RTLA = auto() # RTLA trigger
DUP_IP = auto() # Duplicate IP trigger
MTTL = auto() # mTTL trigger
DPR = auto() # discovery with DPR
BRPR = auto() # discovery with BRPR
BUD = auto() # discovery wth buddy IP
INC = auto() # incomplete discovery
REV = auto() # hop was revealed
PREV = auto() # hop is bfr non-rsp ing
NTH_REV = auto() # nothing revealed
INGR_NF = auto() # ingress not found
TGT_NR = auto() # target not reached
BUD_REP = auto() # buddy report
class Trigger(Enum):
NULL = 0
DUP_IP = 1
RTLA = 2
FRPLA = 3
MTTL = 4
class RevMode(Enum):
NULL = 0
DPR = 1
BRPR = 2
@staticmethod
def ittl(ttl) -> int:
if ttl > 128:
return 255
if ttl > 64:
return 128
if ttl > 32:
return 64
return 32
@staticmethod
def process_queue(queue, ctrl, instmap):
while len(queue) > 0:
test = queue.pop(0)
if test.vp not in instmap:
continue
inst = instmap[test.vp]
if test.dst.is_reserved() or test.dst.is_rfc1918():
continue
if test.method == 'trace':
ctrl.do_trace(test.dst, firsthop=test.start_ttl,
userid=test.userid, attempts=2,method='icmp-paris',inst=inst,sync=False)
elif test.method == 'ping':
ctrl.do_ping(test.dst,sync=False,inst=inst,attempts=2,wait_timeout=0.5,wait_probe=0.01)
return
@staticmethod
def process_hops_pings(hops, pings, queue, probed=set(), vp=None):
for hop in hops:
if hop is None:
continue
if hop.src.is_reserved() or hop.src.is_rfc1918():
continue
if pings.is_probed(hop.src):
hop._ping_rttl = pings.get_rttl(hop.src)
else:
hop._trace._waiting += 1
pings.block(hop.src, hop)
if queue is not None and hop.src not in probed:
probed.add(hop.src)
t = Test("ping", hop.src,vp=vp)
queue.append(t)
return queue
@staticmethod
def clear_expired_tunnels():
ct = datetime.now().timestamp()
for tun in tunnels:
if ct - tun._timestamp > TUNNEL_LIFETIME:
tunnels.pop(tun)
class Test:
def __init__(self, method, dst, vp=None):
self.method = method
self.dst = dst
self.start_ttl = None
self.userid = None
self.vp = vp
def __eq__(self,other):
return self.method == other.method and self.dst == other.dst
def __hash__(self):
return hash((self.method, self.dst))
def __str__(self):
return f'{self.method} to {self.dst} from {self.vp}'
class TunnelTest:
def __init__(self, start_addr, next_addr, target_addr,
trigger_type, userid, ingress_status):
self._start_addr = start_addr
self._next_addr = next_addr
self._target_addr = target_addr
self._ingress_status = ingress_status
self._userid = userid
self._trigger_type = trigger_type
self._iteration = 1
self._rev_mode = RevMode.NULL
@property
def dst(self):
return self._target_addr
@property
def userid(self):
return self._userid
class Tunnel:
def __init__(self, start_addr, next_addr):
self._start_addr = start_addr
self._next_addr = next_addr
self._lsp = []
self._status = 0
self._timestamp = datetime.now().timestamp()
def __eq__(self, other):
if(self._start_addr != other._start_addr or
self._next_addr != other._next_addr):
return False
return True
def __hash__(self):
return hash((self._start_addr, self._next_addr))
def __len__(self):
return len(self._lsp)
# add the discovered hops to the tunnel structure stored in the tunnel test
def add(self, lsrs):
# check if hops were discovered
if lsrs is None or len(lsrs) == 0:
return
to_add = lsrs.copy()
to_add.reverse()
if len(to_add) == len(self._lsp):
for i in range(len(to_add)):
a = to_add[i]
l = self._lsp[i]
if a == None:
continue
if l == None:
self._lsp[i] = a
continue
if a.src == l.src:
continue
return
#avoid adding duplicate lsr sequences to the tunnel
for hop in lsrs:
srcs = [lsr.src if lsr is not None else None for lsr in self._lsp]
to_add = [lsr.src if lsr is not None else None for lsr in lsrs]
to_add.reverse()
in_tun = False
for idx in range(len(srcs) - len(to_add)+1):
if srcs[idx: idx + len(to_add)] == to_add:
in_tun = True
break
if not in_tun:
self._lsp.insert(0, hop)
class PingTests:
class _PingState(Enum):
NOT_PROBED = 0
PROBING = 1
PROBED = 2
class _PingResult:
def __init__(self):
self._state = PingTests._PingState(0)
self._rttl = None
self._timestamp = None
def __init__(self):
self._pings = {}
self._blocked = {}
def process(self, ping):
rttls = {}
rttl_freq = 0
rttl = 0
for reply in ping:
if reply.is_icmp_echo_reply():
if reply.reply_ttl not in rttls:
rttls[reply.reply_ttl] = 0
rttls[reply.reply_ttl] += 1
for ttl in sorted(rttls):
if rttls[ttl] > rttl_freq:
rttl = ttl
rttl_freq = rttls[ttl]
if ping.dst not in self._pings:
self._pings[ping.dst] = PingTests._PingResult()
if rttl_freq > 0:
self._pings[ping.dst]._rttl = rttl
self._pings[ping.dst]._state = PingTests._PingState.PROBED
self._pings[ping.dst]._timestamp = datetime.now().timestamp()
def is_probed(self, addr):
if addr not in self._pings:
return False
if self._pings[addr]._state != PingTests._PingState.PROBED:
return False
return True
def get_rttl(self, addr):
if addr not in self._pings:
return None
return self._pings[addr]._rttl
def block(self, addr, test):
if addr not in self._blocked:
self._blocked[addr] = []
self._blocked[addr].append(test)
def blocked(self, addr):
if addr not in self._blocked:
return
while len(self._blocked[addr]) > 0:
yield self._blocked[addr].pop(0)
del self._blocked[addr]
def __len__(self):
return len(self._pings)
def clear_expired(self):
ct = datetime.now().timestamp()
for dst in self._pings:
if ct - self._pings[dst]._timestamp > PING_LIFETIME:
self._pings.pop(dst)
class TntHop:
def __init__(self, hop, trace, vp):
self._hop = hop
self._vp = vp
self._types = TntHopType(0)
self._disc = TntHopDisc(0)
self._iteration = 0
self._ping_rttl = None
self._trace = trace
def __str__(self):
line = f"{self.src:<15} {(self.rtt.total_seconds()*1000):.3f} ms"
hop_reply_ttl = str(self.reply_ttl)
if self.src.is_reserved():
line += " rsvd rTTLs=<" + hop_reply_ttl + ",*>"
else:
if self._ping_rttl is None:
line += " rTTLs=<" + hop_reply_ttl + ",*>"
else:
rttl = self._ping_rttl
line += " rTTLs=<" + hop_reply_ttl + "," + str(rttl) + ">"
if self.is_ttl_exp():
line += " qttl=" + str(self.icmp_q_ttl) + (
((" uturn=" + str(self.uturn()) if self.uturn() != 0 else "")) +
((" frpla=" + str(self.frpla()) if self.frpla() > 0 else "")) +
((" rtla=" + str(self.rtla())) if self.rtla() > 0 else ""))
if self.is_mpls():
line += " [MPLS" + (
(",EXP" if self.is_exp() else "") +
(",OPA" if self.is_opa() else "") +
(",IMP" if self.is_imp() else "") +
(",INV" if self.is_inv() else "") +
(",LSR" if self.is_intern() else "") +
(",EGR" if self.is_egr() else "") +
(",ING" if self.is_ingr() else ""))
# trigger and signatures
line += self.disc_str()
if self.is_inferred():
line += ",INF"
else:
# implicit signatures
line += ((",QTTL" if self.is_imp_qt() else "") +
(",UTURN" if self.is_imp_ut() else ""))
# discovery method
if self.is_rev():
if self.is_dpr():
line += ",DPR"
elif self.is_brpr():
line += ",BRPR"
line += f",step={self._iteration}"
line += "]"
if self.mpls_count > 0:
mttl = self.mplsext.mpls_ttl(0)
mlabel = self.mplsext.mpls_label(0)
line += " Labels " + str(mlabel) + " mTTL=" + str(mttl)
for j in range(1, self.mpls_count):
mttl = self.mplsext.mpls_ttl(j)
mlabel = self.mplsext.mpls_label(j)
line += " | " + str(mlabel) + " mTTL=" + str(mttl)
return line
def process_ping(self, rttl,queue = None):
self._ping_rttl = rttl
self._trace._waiting -= 1
if self._trace.can_identify():
self._trace.identify(queue)
if self._iteration != 0:
self._trace = None
# scamper_trace_hop_disc_trig_print
def disc_str(self):
return ((",DUPIP" if self.is_dupip() else "") +
(",RTLA" if self.is_rtla() else "") +
(",FRPLA" if self.is_frpla() else "") +
(",MTTL" if self.is_mttl() else ""))
@property
def src(self):
if self._hop:
return self._hop.src
return '*'
@property
def vp(self):
return self._vp
def is_ttl_exp(self):
return self._hop.is_icmp_ttl_exp()
@property
def icmp_q_ttl(self):
return self._hop.icmp_q_ttl
@property
def icmpext(self):
return self._hop.icmp_exts
@property
def mpls_count(self):
if self.icmpext is None or self.icmpext.mpls is None:
return 0
return self.icmpext.mpls.mpls_count
@property
def mplsext(self):
if self.icmpext is None:
return None
else:
return self.icmpext.mpls
# @property
# def mpls_ext(self):
# return self._mpls_ext
@property
def probe_ttl(self):
return self._hop.probe_ttl
@property
def reply_ttl(self):
return self._hop.reply_ttl
@property
def rtt(self):
return self._hop.rtt
def is_juniper_imp(self):
er_rttl = self._ping_rttl
if er_rttl is None:
return False
er_ittl = ittl(er_rttl)
te_rttl = self.reply_ttl
te_ittl = ittl(te_rttl)
return te_ittl == 255 and er_ittl == 64
def rtla(self):
# The ping reply TTL must be available
er_rttl = self._ping_rttl
if er_rttl is None:
return 0
er_ittl = ittl(er_rttl)
te_rttl = self.reply_ttl
te_ittl = ittl(te_rttl)
# Router must be <255,X> with X <= 128 @@@
if te_ittl != 255 or er_ittl == 255:
return 0
nb_hops_return_te = te_ittl - te_rttl + 1
nb_hops_return_er = er_ittl - er_rttl + 1
return nb_hops_return_te - nb_hops_return_er
def frpla(self):
te_rttl = self.reply_ttl
te_ittl = ittl(te_rttl)
nb_hops_forward = self.probe_ttl
nb_hops_return = te_ittl - te_rttl + 1
return nb_hops_return - nb_hops_forward
def uturn(self):
# The ping reply TTL must be available
er_rttl = self._ping_rttl
if er_rttl is None:
return 0
er_ittl = ittl(er_rttl)
te_rttl = self.reply_ttl
te_ittl = ittl(te_rttl)
nb_hops_return_te = te_ittl - te_rttl + 1
nb_hops_return_er = er_ittl - er_rttl + 1
return nb_hops_return_te - nb_hops_return_er
def clear_type_lsr(self):
self._types &= (TntHopType.EXP | TntHopType.IMP_QT | TntHopType.IMP_UT |
TntHopType.OPA | TntHopType.INV)
def clear_types(self):
self._types = TntHopType(0)
def set_type(self, hop_type):
self._types |= hop_type
def is_ingr(self):
return self._types & TntHopType.INGR
def is_egr(self):
return self._types & TntHopType.EGR
def is_intern(self):
return self._types & TntHopType.INTERN
def is_exp(self):
return self._types & TntHopType.EXP
def is_imp_qt(self):
return self._types & TntHopType.IMP_QT
def is_imp_ut(self):
return self._types & TntHopType.IMP_UT
def is_imp(self):
return self._types & (TntHopType.IMP_QT | TntHopType.IMP_UT)
def is_opa(self):
return self._types & TntHopType.OPA
def is_inv(self):
return self._types & TntHopType.INV
def is_opa_egr(self):
return self._types & TntHopType.OPA and self._types & TntHopType.EGR
def is_mpls(self):
return self.is_ingr() or self.is_egr() or self.is_intern()
def is_inferred(self):
if not self._types & TntHopType.INTERN:
return False
if self._types & (TntHopType.INV | TntHopType.OPA | TntHopType.IMP_UT |
TntHopType.IMP_QT | TntHopType.EXP):
return False
return True
def clear_disc_trig(self):
self._disc &= (TntHopDisc.FRPLA | TntHopDisc.RTLA | TntHopDisc.DUP_IP |
TntHopDisc.MTTL)
def set_disc(self, hop_disc):
self._disc |= hop_disc
def is_rev(self):
return self._disc & TntHopDisc.REV
def is_dpr(self):
return self._disc & TntHopDisc.DPR
def is_brpr(self):
return self._disc & TntHopDisc.BRPR
def is_dupip(self):
return self._disc & TntHopDisc.DUP_IP
def is_rtla(self):
return self._disc & TntHopDisc.RTLA
def is_frpla(self):
return self._disc & TntHopDisc.FRPLA
def is_mttl(self):
return self._disc & TntHopDisc.MTTL
# scamper_trace_hop_disc_trig_mflags_set
def mpls_disc_trigger_set(self, tunt):
if tunt._trigger_type == Trigger.RTLA:
self.set_disc(TntHopDisc.RTLA)
elif tunt._trigger_type == Trigger.FRPLA:
self.set_disc(TntHopDisc.FRPLA)
elif tunt._trigger_type == Trigger.DUP_IP:
self.set_disc(TntHopDisc.DUP_IP)
elif tunt._trigger_type == Trigger.MTTL:
self.set_disc(TntHopDisc.MTTL)
# scamper_trace_hop_tunnel_type_mflag_set
def mpls_type_trigger_set(self, tunt):
if tunt._trigger_type == Trigger.MTTL:
self.set_type(TntHopType.OPA)
else:
self.set_type(TntHopType.INV)
# scamper_trace_hop_mpls_flags_set
# Update the MPLS flags of an LSR depending on the tunnel test state
def mpls_flags_set(self, tunt):
self._iteration = tunt._iteration
self.set_disc(TntHopDisc.REV)
self.set_type(TntHopType.INTERN)
# update the trigger
self.mpls_disc_trigger_set(tunt)
self.mpls_type_trigger_set(tunt)
# update the revelation mode
if tunt._rev_mode == RevMode.DPR:
self.set_disc(TntHopDisc.DPR)
elif tunt._rev_mode == RevMode.BRPR:
self.set_disc(TntHopDisc.BRPR)
# XXX: buddy_status
# triggers 1/2
def process_mplsext(self, prev):
# flag as MPLS hop
self.set_type(TntHopType.INTERN)
# triggers 1/2: labels -> Explicit/opaque depending
# on the MPLS TTL for the top label
# mplsext = self.mpls_ext
mttl = self.mplsext.mpls_ttl(0)
if mttl > 236 and mttl < 255:
self.set_type(TntHopType.OPA)
else:
self.set_type(TntHopType.EXP)
# if mttl > 1 or mttl < 255:
# self.set_type(TntHopType.EXP)
# else:
# print('Found OPA!')
# self.set_type(TntHopType.OPA)
# identify ingress
if prev is not None and not prev.is_intern():
if prev.is_egr() and not prev.is_juniper_imp():
# if hop tagged as ingress and egress, it could be
# implicit
prev.clear_types()
prev.set_type(TntHopType.INTERN)
self.clear_disc_trig()
else:
# otherwise, should be a real ingress
prev.set_type(TntHopType.INGR)
if self.is_opa():
prev.set_type(TntHopType.OPA)
prev.set_disc(TntHopDisc.MTTL)
else:
prev.set_type(TntHopType.EXP)
# trigger 3
def process_qttl(self, prev, prev2):
# flag the hop
self.set_type(TntHopType.INTERN | TntHopType.IMP_QT)
# identify the entry of the tunnel
if self.icmp_q_ttl != 2:
return
# First LSR
if (prev is not None and prev.icmp_q_ttl is not None and prev.icmp_q_ttl <= 1 and
not prev.is_intern() and not prev.is_opa_egr()):
# Flag the first LSR
prev.clear_type_lsr()
prev.set_type(TntHopType.INTERN | TntHopType.IMP_QT)
elif (prev is None and prev2 is not None and
not prev2.is_intern() and not prev2.is_ingr() and
(not prev2.is_egr() or prev2.is_juniper_imp())):
# ingress hop
prev2.clear_types()
prev2.set_type(TntHopType.INTERN | TntHopType.IMP_QT)
prev2.set_type(TntHopType.INGR)
def process_egr(self, prev, prev2,dst):
if prev is not None:
# previous hop must be an LSR
if not prev.is_intern():
return
if prev.is_exp():
self.set_type(TntHopType.EGR | TntHopType.EXP)
elif prev.is_imp():
# for implicit tunnels, egress is one hop after qTTL <= 1
self.set_type(TntHopType.IMP_QT)
if (self.src == dst or
(prev.icmp_q_ttl <= 1 and prev.src != self.src)):
self.set_type(TntHopType.EGR)
else:
self.set_type(TntHopType.INTERN)
elif prev.is_opa():
prev.clear_type_lsr()
prev.set_type(TntHopType.EGR)
elif prev2 is not None and prev2.icmp_q_ttl is not None:
if prev2.is_imp() and prev2.icmp_q_ttl > 1:
self.set_type(TntHopType.EGR | TntHopType.IMP_QT)
class TntTrace:
def __init__(self, trace,userid=None):
vp = trace.list.monitor.split('.')[0]
# self._trace = trace
self._hops = [TntHop(hop, self, vp) if hop is not None else None for hop in trace.hops()]
# self._firsthop = trace.firsthop
self._tests = []
self._waiting = 0
self._identify_called = False
if userid:
self._userid = userid
else:
self._userid = trace.userid
self._src = trace.src
self._dst = trace.dst
# for i, hop in enumerate(trace.hops()):
# if hop is not None:
# self._hops[i] = TntHop(hop, self)
def __str__(self):
txt = "trace from " + str(self.src) + " to " + str(self.dst) + "\n"
# txt = ""
for i in range(self.firsthop-1, self.hop_count):
hop = self.hop(i)
next_hop = self.hop(i+1)
# print out regular traceroute hop
txt += ((f"{i+1:3} ") + ("*" if hop is None else str(hop)) + "\n")
# print out inferred tunnel hops if there are hops to print
if hop is None or next_hop is None:
continue
tun = Tunnel(hop.src, next_hop.src)
if tun not in tunnels:
continue
tun = tunnels[tun]
for j, lsr in enumerate(tun._lsp):
txt += ("%3s " % (f"H{j+1}"))
txt += ("*" if lsr is None else str(lsr)) + "\n"
txt += "\n"
return txt
# get list of unique addresses observed in the traceroute
def addrs(self):
addrs = {}
for hop in self._hops:
if hop is not None:
addrs[hop.src] = 1
return addrs.keys()
# get the first hop probed in the traceroute
@property
def firsthop(self):
for i, hop in enumerate(self._hops):
if hop is not None:
return i+1
return 1
@property
def is_mpls(self):
for hop in self._hops:
if hop is not None and hop.is_mpls():
return True
return False
# get the maximum TTL probed
@property
def hop_count(self):
return len(self._hops)
@property
def src(self):
return self._src
@property
def dst(self):
return self._dst
@property
def userid(self):
return self._userid
# def set_userid(self, u):
# self.userid
# get the hop for a given TTL
def hop(self, i):
if i < 0 or i >= len(self._hops):
return None
return self._hops[i]
# return observed hops
@property
def hops(self):
return self._hops
@property
def vp(self):
for hop in self._hops:
if hop is not None:
return hop.vp
return None
# queue an invisible/opaque tunnel test for a TNT test
def tunnel_test_add(self, start_addr, next_addr, start_ttl, trigger_type,
userid, ingress_status, queue):
tunnel_test = TunnelTest(start_addr, next_addr, next_addr,
trigger_type, userid, ingress_status)
self._tests.append(tunnel_test)
if queue is not None:
test = Test("trace", next_addr, vp=self.vp)
test.start_ttl = start_ttl
test.userid = userid
queue.append(test)
def can_identify(self):
return self._waiting == 0 and self._identify_called is False
# identify MPLS tunnels and LSRs, if explicit, implicit, or opaque
def identify(self, queue):
self._identify_called = True
for i in range(self.firsthop-1, self.hop_count):
hop = self.hop(i)
prev_hop = self.hop(i-1)
prev2_hop = self.hop(i-2)
# get hop if any response
if hop is None:
# check if previous hop is not opaque
if prev_hop is not None and prev_hop.is_opa():
prev_hop.clear_type_lsr()
prev_hop.set_type(TntHopType.EGR)
continue
if hop.is_ttl_exp():
if hop.mpls_count > 0:
# trigger 1/2: use mplsext included in response
hop.process_mplsext(prev_hop)
continue
if hop.icmp_q_ttl > 1 and hop.icmp_q_ttl != 255:
# trigger 3: q-TTL > 1 -> Implicit
hop.process_qttl(prev_hop, prev2_hop)
continue
# identify egress hops for explicit, implicit, and opaque tunnels
hop.process_egr(prev_hop, prev2_hop, self._dst)
# special loop for invisible tunnels, mandatory due to
# potential overlap with implicit tunnels
for i in range(self.firsthop-1, self.hop_count):
hop = self.hop(i)
prev_hop = self.hop(i-1)
# get hop if any public response, not involved in another tunnel
if hop is None or hop.src.is_reserved() or hop.is_intern():
continue
# duplicate IP address
if prev_hop is not None and prev_hop.src == hop.src:
# first IP is already tagged for another tunnel
if prev_hop.is_egr() or prev_hop.is_intern():
continue
ingress_hop = self.hop(i-2)
start_hop = self.hop(i-3)
else:
ingress_hop = prev_hop
start_hop = self.hop(i-2)
# ingress cannot be inside an implicit or explicit tunnel
if (ingress_hop is not None and not hop.is_opa() and
ingress_hop.is_intern()):
continue
# get the start and ingress hops
if ingress_hop is None:
if start_hop is None or start_hop.src.is_reserved():
continue
start_addr = start_hop.src
ingress_status = IngressStatus.NULL
tmp_hop = start_hop
else:
if ingress_hop.src.is_reserved():
continue
start_addr = ingress_hop.src
ingress_status = IngressStatus.START
tmp_hop = ingress_hop
# get the probing start TTL
if tmp_hop.probe_ttl > 2:
start_ttl = tmp_hop.probe_ttl - 2
else:
start_ttl = 1
# ingress and egress are the same router
if start_addr == hop.src:
continue
# an opaque tunnel is found
if hop.is_opa_egr():
self.tunnel_test_add(start_addr, hop.src, start_ttl,
Trigger.MTTL, self._userid,
ingress_status, queue)
continue
# Egress can not be already an egress for another tunnel
# Triggers are computed on time-exceeded messages
if hop.is_egr() or not hop.is_ttl_exp():
continue
# Trigger 4: Duplicate IP address -> Invisible, UHP
if prev_hop is not None and prev_hop.src == hop.src:
self.tunnel_test_add(start_addr, hop.src, start_ttl,
Trigger.DUP_IP, self._userid,
ingress_status, queue)
continue
# test if potential egress is not a duplicate IP
tmp_hop = self.hop(i+1)
if (tmp_hop is not None and tmp_hop.is_ttl_exp() and
tmp_hop.src == hop.src):
continue
# Trigger 5: RTLA -> Invisible
if hop.rtla() >= 1:
self.tunnel_test_add(start_addr, hop.src, start_ttl,
Trigger.RTLA, self._userid,
ingress_status, queue)
continue
# Trigger 6: FRPLA -> Invisible
if hop.frpla() >= 3:
self.tunnel_test_add(start_addr, hop.src, start_ttl,
Trigger.FRPLA, self._userid,
ingress_status, queue)
continue
def find_test(self, target_addr):
if not isinstance(target_addr, ScamperAddr):
raise ValueError("invalid target_addr")
for test in self._tests:
if test._target_addr == target_addr:
return test
return None
def process_trace(self, trace, pings, queue):
try:
# check if the target was reached
tunt = self.find_test(trace.dst)
if (tunt is None or
trace.hop(trace.hop_count-1) is None or
trace.hop(trace.hop_count-1).src != tunt._target_addr):
return
except Exception as e:
print(f'Error: {e}')
start_hop = None
for i in range(trace.firsthop-1, trace.hop_count):
hop = trace.hop(i)
nxthop = trace.hop(i+1)
if hop is not None and hop.src == tunt._start_addr:
start_hop = i
if nxthop is not None and nxthop.src == tunt._start_addr:
start_hop = i+1
break
# the end of the trace is reached, the ingress was not found
if start_hop is None:
return
# check if the start address is the ingress address
if tunt._ingress_status == IngressStatus.NULL:
start_hop += 1
# the next hop is the target, no LSR was revealed
if start_hop >= trace.hop_count - 2:
return
# push the new LSRs in the list, in reverse order
lsrs = []
for i in range(start_hop+1, trace.hop_count-1):
hop = trace.hop(i)
if hop is not None:
hop = TntHop(hop, self,self.vp)
lsrs.insert(0, hop)
# check if BRPR or DPR step
nlsrs = len(lsrs)
if nlsrs == 1:
tunt._rev_mode = RevMode.BRPR
elif nlsrs > 2:
tunt._rev_mode = RevMode.DPR
else:
tunt._rev_mode = RevMode.DPR
if(lsrs[0] is not None and lsrs[1] is not None and
lsrs[0].src == lsrs[1].src):
tunt._rev_mode = RevMode.BRPR
tun = Tunnel(tunt._start_addr, tunt._next_addr)
if tun not in tunnels:
tunnels[tun] = tun
else:
tun = tunnels[tun]
tun.add(lsrs)
# Update ingress/egress/LH MPLS flags
for j in range(self.firsthop-1, self.hop_count):
ing = self.hop(j)
egr = self.hop(j+1)
if ing is None or egr is None:
continue
if ing.src != tunt._start_addr or egr.src != tunt._next_addr:
continue
ing.mpls_disc_trigger_set(tunt)
ing.mpls_type_trigger_set(tunt)
ing.set_type(TntHopType.INGR)
egr.mpls_disc_trigger_set(tunt)
egr.mpls_type_trigger_set(tunt)
egr.set_type(TntHopType.EGR)
break
# update the MPLS flags of an LSR depending on the tunnel test state
for hop in lsrs:
if hop is not None:
hop.mpls_flags_set(tunt)