aboutsummaryrefslogtreecommitdiffstats
path: root/utils/osmo-gsm-shark
blob: abfd4e42202946773654916ecf7e7fc03c1d25e8 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
#!/usr/bin/env python3

# osmo-gsm-shark: produce a ladder diagram from and/or filter a GSM network pcap by subscriber.
# Copyright (C) 2019 by Neels Hofmeyr <neels@hofmeyr.de>
#
# All Rights Reserved
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

'''osmo-gsm-shark: produce a ladder diagram from and/or filter a GSM network pcap by subscriber.

Copyright (C) 2019 by Neels Hofmeyr <neels@hofmeyr.de>
SPDX-License-Identifier: GPL-2.0+

This tool uses tshark (pyshark) to analyze a pcap file or a live network capture to:

- Associate almost all messages with a subscriber. It is possible to filter by subscriber.
- Separate the different network elements (BSC, MSC, hNodeB, ...).
- Output a ladder diagram.
- Combine repetitive messages.
- Combine/abstract messages into short activity summary.

Examples:

	osmo-gsm-shark -f trace.pcapng
	osmo-gsm-shark -l any

	osmo-gsm-shark -l any --filter-subscr 901701234567123
	osmo-gsm-shark -f trace.pcapng --filter-msg dtap
'''

import collections
import pyshark
import re
import sys
import types
import time
import traceback

SHOW_ALL_DEBUG = False

SHOW_ALL_LAYERS = False
SCCP_COLLAPSE_STP = True
IUH_COLLAPSE_HNBGW = True # doesnt work

DTAP_COMPL_L3 = ('Location-Updating-Request', 'CM-Service-Request', 'Paging-Response', 'IMSI-Detach-Indication')
GMM_COMPL_L3 = ('Attach-Request', 'Detach-Request')

class Color:
	codes = (
		('red', '\033[1;31m'),
		('green', '\033[1;32m'),
		('yellow', '\033[1;33m'),
		('blue', '\033[1;34m'),
		('purple', '\033[1;35m'),
		('cyan', '\033[1;36m'),
		('darkred', '\033[31m'),
		('darkgreen', '\033[32m'),
		('darkyellow', '\033[33m'),
		('darkblue', '\033[34m'),
		('darkpurple', '\033[35m'),
		('darkcyan', '\033[36m'),
		('darkgrey', '\033[1;30m'),
		('brightwhite', '\033[1;37m'),
		)
	codes_dict = dict(codes)
	end = '\033[0;m'

	def colored(code, text):
		if type(code) is int:
			code = Color.codes[code % len(Color.codes)][1]
		else:
			code = Color.codes_dict[code]
		return f'{code}{text}{Color.end}'


def set_instance_vars_from_args(*ignore, self='s'):
	f = sys._getframe(1).f_locals
	s = f.get(self)
	for k,v in f.items():
		if v is s:
			continue
		if k in ignore:
			continue
		setattr(s, k, v)

def same_nonempty(a, b):
	if isinstance(a, types.GeneratorType):
		return list(a) == list(b)
	return a and a == b

def str_drop(a_str, drop_str):
	if a_str and a_str.startswith(drop_str):
		return a_str[len(drop_str):]
	return a_str

def sane_msgtype(msgtype):
	if not msgtype:
		return msgtype
	return sane_showname(msgtype).replace(' ','-')

re_msgtype_label = re.compile('message.type *', re.I)
def sane_showname(showname):
	if not showname:
		return showname
	if ': ' in showname:
		showname = showname[showname.index(': ')+1:]
	if '(' in showname:
		showname = showname[:showname.index('(')]
	showname = re_msgtype_label.sub('', showname)
	return showname.strip()

def dir_vals(elem):
	strs = []
	for name in dir(elem):
		if name.startswith('_'):
			continue
		try:
			strs.append('%r=%r' % (name, getattr(elem, name)))
		except:
			strs.append('%r=<Exception>' % (name))
	return '\n' + '\n'.join(strs)

def dir_p(p, name):
	return f'=== {name}\n{dir_vals(p.get(name))}\n---{name}'

def str_to_int(nr_str):
	'convert hex or decimal string to int'
	if not nr_str:
		return None
	elif nr_str.startswith('0x'):
		return int(nr_str, 16)
	else:
		return int(nr_str, 10)

def out_text(*args, **kwargs):
	print(*args, **kwargs)

g_ui = None
g_current_msg = None
g_debug_full = False

def to_text(*args, **kwargs):
	if kwargs:
		args = list(args) + [repr(kwargs)]
	return ' '.join(str(arg) for arg in args)

def out_text_now(*args, **kwargs):
	if g_ui is not None:
		g_ui.out_text_now(*args, **kwargs)
	else:
		print(to_text(*args, **kwargs))

def LOG(*args, **kwargs):
	if g_current_msg is not None:
		g_current_msg.log(*args, **kwargs)
	else:
		out_text_now(*args, **kwargs)

def DBG(*args, **kwargs):
	if g_current_msg is not None:
		g_current_msg.dbg(*args, **kwargs)
	else:
		out_text_now(*args, **kwargs)

def ERR(*args, **kwargs):
	LOG(Color.colored('red', '***** ERROR:'), *args, **kwargs)


def trace():
	return ''.join(traceback.format_stack())

def because_str(because):
	if not because:
		return '-'
	t = ['BECAUSE']
	for b in because:
		if isinstance(b, tuple) or isinstance(b, list):
			t.extend(because_str(b).splitlines())
		elif isinstance(b, str):
			t.append(b)
		elif isinstance(b, Message):
			t.append(b.str(show_traits=True, show_conns=True))
		else:
			t.append(str(b))
	return '\n|'.join(t)

def out_error(*args, **kwargs):
	if g_ui is not None:
		g_ui.out_error(*args, **kwargs)
	else:
		out_text_now(Color.colored('red', '*** ERROR:'), *args, **kwargs)
		if g_current_msg:
			out_text_now(Color.colored('red', '*** ERROR: while processing msg'), g_current_msg.str(show_traits=True, show_conns=True))
		out_text_now(trace())

def tmsi_standardize(tmsi):
	try:
		return format(int(tmsi), '08x')
	except:
		return None

# a dict containing a list for each key; l.add(name, item) adds item to the list at key=name.
class listdict(dict):
	'''A dict where each entry is a list of items'''
	def _have(ld, name):
		l = ld.get(name)
		if not l:
			l = []
			ld[name] = l
		return l

	def add(ld, name, item):
		l = ld._have(name)
		l.append(item)
		return ld

	def add_dict(ld, d):
		for k,v in d.items():
			ld.add(k, v)

	def update(ld, other_ld):
		for name, items in other_ld.items():
			ld.extend(name, items)
		return ld

	def extend(ld, name, vals):
		l = ld._have(name)
		l.extend(vals)
		return ld

	def remove(ld, name, item):
		l = ld.get(name)
		if item in l:
			l.remove(item)
	def _have(ld, name):
		l = ld.get(name)
		if not l:
			l = []
			ld[name] = l
		return l

	def add(ld, name, item):
		l = ld._have(name)
		l.append(item)
		return ld

	def add_dict(ld, d):
		for k,v in d.items():
			ld.add(k, v)

	def update(ld, other_ld):
		for name, items in other_ld.items():
			ld.extend(name, items)
		return ld

	def extend(ld, name, vals):
		l = ld._have(name)
		l.extend(vals)
		return ld

	def remove(ld, name, item):
		l = ld.get(name)
		if item in l:
			l.remove(item)

class UniqueList(list):
	def append(s, item):
		if item in s or item is None:
			return False
		super().append(item)
		return True

	def insert(s, idx, item):
		if item in s or item is None:
			return False
		super().insert(idx, item)
		return True

	def extend(s, items):
		added = 0
		for item in items:
			if s.append(item):
				added += 1
		return added

class NamedIds(dict):
	def __init__(s, start_id:int=1):
		set_instance_vars_from_args()

	def next(s, name):
		next_id = s.get(name, s.start_id)
		s[name] = next_id + 1
		return next_id

class Packet:
	def __init__(s, idx, cap_p):
		set_instance_vars_from_args()
		# sanitize impossible attr with dot in its name,
		# seen gsm_a.bssmap and gsm_a.dtap
		for name in dir(s.cap_p):
			if '.' in name:
				new_name = name.replace('.', '_')
			elif not name:
				new_name = 'unnamed'
			else:
				continue
			setattr(s.cap_p, new_name, getattr(s.cap_p, name))

	@classmethod
	def pget(cls, cap_p, tokens, ifnone=None):
		if cap_p is None or len(tokens) < 1:
			return ifnone
		p_field = getattr(cap_p, tokens[0], None)
		if p_field is None:
			p_field = getattr(cap_p, '.'.join(tokens), None)
		if p_field is None:
			return ifnone
		if len(tokens) > 1:
			return Packet.pget(p_field, tokens[1:], ifnone=ifnone)
		return p_field

	def get(s, field, ifnone=None):
		return Packet.pget(s.cap_p, field.split('.'), ifnone=ifnone)

	def str(s, elem_name=None):
		strs = []
		if elem_name:
			elem = s.get(elem_name)
		else:
			elem = s.cap_p
		return dir_vals(elem);

	def field_names(s, elem_name=None, elem=None):
		strs = ['', f'=== {elem_name} ===']
		if elem is None:
			elem = s.get(elem_name)
		if not elem:
			strs.append('None')
		else:
			for f in elem._get_all_fields_with_alternates():
				for n in dir(f):
					if n.startswith('_'):
						continue
					strs.append('%r=%r' % (n, getattr(f, n)))
		return '\n'.join(strs)

	def all_str(s, elem_name=None, elem=None, depth=1000):
		strs = []
		if elem is None:
			if elem_name:
				elem = s.get(elem_name)
			else:
				elem = s.cap_p
				elem_name = '/'
		strs.append('%s:' % elem_name)
		for name in dir(elem):
			if name.startswith('_') or name.endswith('_value') or name in [
					'sort', 'reverse', 'remove', 'pop', 'insert', 'index', 'extend', 'count',
					'copy', 'clear', 'append', 'zfill', 'max', 'min', 'resolution',
					]:
				continue
			try:
				full_name = '%s.%s' % (elem_name, name)
				val = getattr(elem, name)
				if callable(val) or name in ['base16_value']:
					continue
				strs.append('%r=%r' % (full_name, val))
				if depth and type(val) not in [int, str]:
					strs.append(s.all_str(full_name, val, depth-1))
			except:
				pass
		if hasattr(elem, '_get_all_fields_with_alternates'):
			for f in elem._get_all_fields_with_alternates():
				for n in dir(f):
					if n.startswith('_'):
						continue
					full_name = '%r[%r]' % (elem_name, n)
					try:
						val = getattr(f, n)
					except:
						continue
					if callable(val):
						continue
					strs.append('%s=%r' % (full_name, val))
					if depth:
						strs.append(s.all_str(full_name, val, depth-1))
		return '\n'.join(strs)

class IpPort:
	all_ports = {}

	@classmethod
	def _key(cls, ip, port):
		return f'{ip}:{port}'

	@classmethod
	def get(cls, ip, port, entity=None, proto=None, create=True):
		o = IpPort.all_ports.get(IpPort._key(ip, port))
		if o is None and create:
			return IpPort(ip, port, entity, proto)
		if o is not None:
			if entity is not None:
				if o.entity is not None and o.entity is not entity:
					ERR('Port changes:', o, 'to', entity)
					entity.add_port(o.proto, o)
		return o

	def __init__(s, ip:str=None, port:str=None, entity=None, proto=None):
		set_instance_vars_from_args()
		IpPort.all_ports[s.key()] = s

	def __repr__(s):
		r = ''
		tokens = []
		if s.entity:
			tokens.append(f'{s.entity}')
		if s.proto:
			tokens.append(f'{s.proto}')
		tokens.append(s.key())
		return '-'.join(tokens)

	def __eq__(s, other):
		return s is other

	def __hash__(s):
		return hash(s.key())

	def key(s):
		return IpPort._key(s.ip, s.port)

	@classmethod
	def from_sdp(p:Packet):
		ip = p.get('sdp.connection_info_address')
		port = p.get('sdp.media_port')
		return IpPort.get(ip, port)

	@classmethod
	def _from_ip(cls, p:Packet, src_dst:str, port:int):
		ip = p.get('ip.' + src_dst)
		if ip is None:
			ipv6 = p.get('ipv6.' + src_dst)
			if ipv6 is not None:
				ip = '[' + ipv6 + ']'
		return IpPort.get(ip, port)

	@classmethod
	def from_ip_source(cls, p:Packet, port:int):
		return cls._from_ip(p, 'src', port)

	@classmethod
	def from_ip_dest(cls, p:Packet, port:int):
		return cls._from_ip(p, 'dst', port)

	@classmethod
	def from_udp_source(cls, p:Packet):
		return cls.from_ip_source(p, p.get('udp.srcport'))

	@classmethod
	def from_udp_dest(cls, p:Packet):
		return cls.from_ip_dest(p, p.get('udp.dstport'))

	@classmethod
	def from_tcp_source(cls, p:Packet):
		return cls.from_ip_source(p, p.get('tcp.srcport'))

	@classmethod
	def from_tcp_dest(cls, p:Packet):
		return cls.from_ip_dest(p, p.get('tcp.dstport'))

	@classmethod
	def from_sctp_source(cls, p:Packet):
		return cls.from_ip_source(p, p.get('sctp.srcport'))

	@classmethod
	def from_sctp_dest(cls, p:Packet):
		return cls.from_ip_dest(p, p.get('sctp.dstport'))

class Message:
	pass

class Trait:
	def __init__(s, **kwargs):
		if len(kwargs) > 1:
			raise Exception('only one trait allowed per Trait(): %r' % kwargs)
		for k, v in kwargs.items():
			s.name = k
			s.val = v

	def __repr__(s):
		return '%r=%r' % (s.name, s.val)

class Traits(collections.OrderedDict):
	def __init__(s, *args, **kwargs):
		for arg in args:
			s.add(arg)
		s.set_vals(**kwargs)

	def add(s, trait:Trait):
		s[trait.name] = trait

	def set(s, name, val):
		if val is not None:
			s.add(Trait(**{name: val}))

	def set_vals(s, **kwargs):
		for k,v in kwargs.items():
			if v is None:
				continue
			if type(v) not in (str, int, float, bool, IpPort):
				v = str(v)
			s.set(k, v)

	def __repr__(s):
		strs = []
		for k,trait in s.items():
			assert k == trait.name
			strs.append(repr(trait))
		return '{%s}' % ', '.join(strs)

def find_recent_msg(msg:Message, messages:list, my_idx:int, condition_func, max_t=1):
	for i in reversed(range(my_idx)):
		prev_msg = messages[i]
		if not prev_msg:
			continue
		if prev_msg.finalized:
			return None
		if msg.timestamp - prev_msg.timestamp > max_t:
			return None
		if condition_func(prev_msg):
			yield prev_msg
	return None

def find_same_trait(msg:Message, messages:list, my_idx:int, proto:str, name:str, max_t=1, operator=any):
	def same_traits(prev_msg):
		return msg.same_traits(prev_msg, proto, name, operator=operator)
	return find_recent_msg(msg, messages, my_idx, same_traits, max_t)

class dddict(dict):
	@classmethod
	def _get(cls, d, keys, create=False):
		if not keys:
			return d
		k = keys[0]
		v = d.get(k)
		if len(keys) > 0:
			if v is None:
				if create:
					v = {}
					d[k] = v
				else:
					return None
		r = cls._get(v, keys[1:], create=create)
		return r

	def gget(s, keys, create=None):
		v = dddict._get(s, keys, create=False)
		if v is None:
			if create is None:
				return None
			else:
				return s.sset(keys, create)
		return v

	def sset(s, keys, item):
		d = dddict._get(s, keys[:-1], create=True)
		d[keys[-1]] = item
		return item

	def ppop(s, keys):
		d = dddict._get(s, keys[:-1])
		if d is None:
			return None
		r = d.pop(keys[-1])
		if not d:
			if len(keys) > 1:
				s.ppop(keys[:-1])
		return r

	@classmethod
	def _count(cls, d):
		if isinstance(d, dict):
			count = 0
			for val in d.values():
				count += cls._count(val)
			return count
		return 1

	def count(s):
		return dddict._count(s)

	@classmethod
	def _all(cls, d):
		if isinstance(d, dict):
			for val in d.values():
				yield from cls._all(val)
		else:
			yield d

	def all(s):
		return dddict._all(s)


class Conn:
	open_conns = dddict()
	closed_conns = dddict()

	'''One end of a time-limited connection for a protocol layer'''
	def __init__(s, proto:str, port:IpPort, conn_id:str, start_msg:Message, close_msg:Message=None, idx=-1,
		     entity=None, counterparts:list=[], subscriber_conn=None, merge_counterparts=True, add_message=True,
		     meta=False):
		set_instance_vars_from_args('entity', 'add_message', 'merge_counterparts')
		s.tx_messages = UniqueList()
		s.rx_messages = UniqueList()
		if s.subscriber_conn:
			s.subscriber_conn.conns.append(s)

		s.keys = (proto, port.key(), conn_id)
		Conn.open_conns.sset(s.keys, s)
		# A counterpart is the same conn seen from the other side.
		# For example, if a BSC opens a conn, the conterpart is the MSC's perspective on the same conn.
		s.counterparts = UniqueList()
		for cp in counterparts:
			if cp is None:
				continue
			s.counterparts.append(cp)
			cp.counterparts.append(s)

		if entity:
			entity.add_port(proto, port, from_msg=start_msg)

		if add_message:
			s.add_message(start_msg)

		if not counterparts:
			LOG(Color.colored('green', f'New conn (now {Conn.count_open_conns()})'), s.proto, '%r' % s.conn_id)
		if merge_counterparts:
			for other_conn in s.counterparts:
				s.merge_subscr_conns(other_conn)

	@classmethod
	def _find(cls, keys, find_in_closed_conns=False):
		ret = Conn.open_conns.gget(keys)
		if ret is not None:
			return ret
		if find_in_closed_conns:
			return Conn.closed_conns.gget(keys)
		return None

	@classmethod
	def find(cls, proto:str, port:IpPort, conn_id:str, find_in_closed_conns=False):
		return cls._find((proto, port.key(), conn_id), find_in_closed_conns=find_in_closed_conns)

	@classmethod
	def open(cls, proto:str, port:IpPort, conn_id:str, *args, **kwargs):
		exists = cls.find(proto, port, conn_id)
		if exists is not None:
			ERR('Conn already open:', type(exists), repr(exists))
			LOG(trace())
		return Conn(proto, port, conn_id, *args, **kwargs)

	@classmethod
	def find_or_open(cls, proto:str, port:IpPort, conn_id:str, *args, **kwargs):
		exists = cls.find(proto, port, conn_id)
		if exists is not None:
			return exists
		return Conn(proto, port, conn_id, *args, **kwargs)

	@classmethod
	def open_2way(cls, proto:str, conn_id:str, msg:Message, *args, **kwargs):
		conn1 = cls.open(proto, msg.src, conn_id, msg, *args, **kwargs)
		cls.open(proto, msg.dst, conn_id, msg, *args, counterparts=[conn1], **kwargs)
		return conn1

	@classmethod
	def close_conn(cls, conn, msg):
		conn._add_message(msg)
		conn.close_msg = msg
		Conn.open_conns.ppop(conn.keys)
		l = Conn.closed_conns.gget(conn.keys, create=[])
		l.append(conn)
		LOG(Color.colored('blue', f'Close conn ({Conn.count_open_conns()} left)'), conn)

	@classmethod
	def close(cls, proto, port, conn_id, close_msg, close_counterparts=True, if_exists=False):
		conn = Conn.find(proto, port, conn_id)
		if conn is None or not conn.is_open():
			if if_exists == False:
				ERR('Cannot close, conn not open:', Conn.key_label(proto, port, conn_id))
			return None
		Conn.close_conn(conn, close_msg)
		assert Conn.find(proto, port, conn_id) is None
		if close_counterparts:
			for cp in conn.counterparts:
				if cp.is_open():
					Conn.close_conn(cp, close_msg)
		return conn

	@classmethod
	def message(cls, proto, port, conn_id, msg):
		conn = cls.find(proto, port, conn_id)
		if conn is None:
			return None
		conn.add_message(msg)
		return conn

	def _add_message(s, msg):
		if s.port == msg.src:
			if s.meta:
				msg.meta_conns.append(s)
			else:
				msg.src_conns.append(s)
			s.tx_messages.append(msg)
		elif s.port == msg.dst:
			if s.meta:
				msg.meta_conns.append(s)
			else:
				msg.dst_conns.append(s)
			s.rx_messages.append(msg)

	def add_message(s, msg, add_to_counterparts=True):
		if s.close_msg:
			out_error('Message on a closed conn', s, msg)
		s._add_message(msg)
		if add_to_counterparts:
			for cp in s.counterparts:
				cp._add_message(msg)

	@classmethod
	def key_label(cls, proto, port, conn_id):
		if conn_id:
			return f'{proto}:{conn_id}@{port}'
		else:
			return f'{proto}@{port}'

	def label(s):
		return s.key_label(s.proto, s.port, s.conn_id)

	def __repr__(s):
		tokens = [s.label()]
		for r in s.counterparts:
			if r is None:
				tokens.append('None')
			else:
				tokens.append(r.label())
		return ' -> '.join(tokens)

	def merge_subscr_conns(s, other_conn):
		if other_conn is None:
			return
		if not isinstance(other_conn, Conn):
			for item in other_conn:
				s.merge_subscr_conns(item)
			return
		assert isinstance(other_conn, Conn)
		if s.subscriber_conn is not None and s.subscriber_conn is other_conn.subscriber_conn:
			return
		s.subscriber_conn = SubscriberConn.merge(s.subscriber_conn, other_conn.subscriber_conn)
		if s.subscriber_conn is None:
			s.subscriber_conn = SubscriberConn()
		s.subscriber_conn.add_conn(s)
		other_conn.subscriber_conn = s.subscriber_conn
		s.subscriber_conn.add_conn(other_conn)

	def find_entity(s, kind, with_port=None):
		if s.subscriber_conn:
			return s.subscriber_conn.find_entity(kind, with_port=with_port)
		return None, None

	def is_open(s):
		return s.close_msg is None and Conn._find(s.keys) is s

	@classmethod
	def count_open_conns(cls):
		count = 0
		have = set()
		for conn in cls.open_conns.all():
			do_count = True
			for conn2 in conn.counterparts:
				if conn2.label() in have:
					do_count = False
				have.add(conn2.label())
			if conn.label() in have:
				do_count = False
			have.add(conn.label())
			if do_count:
				count += 1
		return count


class Layer:

	_classes = {}
	traits = None
	proto = None
	cap_p_name = None

	def __init__(s, msg:Message, proto:str, msgtype:str, traits:Traits, minor=False, hidden=False, cap_p_name:str=None):
		set_instance_vars_from_args()
		if proto in msg.layers:
			raise Exception(f'duplicate proto {proto} for message')
		msg.layers[proto] = s
		s.msgtype = sane_msgtype(s.msgtype)
		if not s.cap_p_name:
			s.cap_p_name = s.proto
		s.__class__.proto = s.proto
		s.__class__.cap_p_name = s.cap_p_name

	def label(s):
		if s.msgtype:
			return f'{s.proto}.{s.msgtype}'
		else:
			return s.proto

	def identify_entities(s, msg:Message, messages, my_idx):
		'''return a list of Message.EntityIdent instances describing source and/or destination entity that message msg identifies.'''
		return None

	@classmethod
	def identify_conns(s, messages, my_idx):
		pass

	def collapse(s, messages, my_idx):
		'''return the message itself if it remains in messages, if another absorbed it return the other message,
		or if if it is dropped completely return None'''
		return messages[my_idx]

	@classmethod
	def classes(cls):
		if not Layer._classes:
			for cls in Layer.__subclasses__():
				name = cls.__name__
				if not name.startswith('Layer_'):
					continue
				proto_name = name[len('Layer_'):]
				Layer._classes[proto_name] = cls
				#cls.proto = proto_name
		return Layer._classes

	@classmethod
	def parse(cls, msg:Message):

		for proto_name,child_class in Layer.classes().items():
			if not msg.p.get(proto_name):
				continue
			child_class(msg)


class Message:

	def __init__(s, p:Packet, finalized=False):
		set_instance_vars_from_args()
		s.layers = collections.OrderedDict()
		s.count = 1
		s.count_back = 0
		s.timestamp = float(p.cap_p.sniff_timestamp)
		s.hide = False
		s.src = None
		s.dst = None
		s.src_conns = []
		s.dst_conns = []
		s.meta_conns = []
		s.absorbed = UniqueList()
		s.strong_relation = True
		s.debug = SHOW_ALL_DEBUG
		s._log = []

	def log(s, *text, **kwtext):
		s._log.append(to_text(*text, *kwtext))

	def dbg(s, *text, **kwtext):
		if s.debug:
			s._log.append(to_text(*text, *kwtext))

	def is_minor(s):
		return all(l.minor for l in s.layers.values())

	def get_trait(s, proto:str, name:str, ifnone=None):
		# allow alternative lists for proto, like s.get_trait(('tcp', 'udp'), 'src')
		if proto is None:
			proto = s.layers.keys()
		if type(proto) is not str:
			for proto_ in proto:
				val = s.get_trait(proto_, name, None)
				if val is not None:
					return val
			return ifnone
		# allow alternative lists for name, like s.get_trait('tcp', ('src', 'dst))
		if type(name) is not str:
			for name_ in name:
				val = s.get_trait(proto, name_, None)
				if val is not None:
					return val
			return ifnone

		if name == 'timestamp':
			return int(s.timestamp)
		layer = s.layers.get(proto, None)
		if not layer:
			return ifnone
		if name == 'msgtype':
			return layer.msgtype
		trait = layer.traits.get(name, None)
		if trait is None:
			return ifnone
		if trait.val is None:
			return ifnone
		return trait.val

	def get_traits(s, proto=None, names=None, proto_and_names=None):
		pn = []
		if proto or names:
			if proto is None or isinstance(proto, str):
				proto = [proto]
			for p in proto:
				pn.append((p, names))
		if proto_and_names:
			pn.extend(proto_and_names)
		for proto_, names in pn:
			if proto_ is None:
				proto_ = s.layers.keys()
			if isinstance(proto_, str):
				proto_ = [proto_]
			for proto in proto_:
				if names is None:
					l = s.layers.get(proto, None)
					if not l:
						continue
					names = l.traits.keys()
				if type(names) is str:
					names = [names]
				for name in names:
					result = s.get_trait(proto, name, ifnone=None)
					if result is not None:
						yield (proto, name, result)

	def get_all_traits(s, proto:str):
		layer = s.layers.get(proto)
		if not layer:
			return {}
		return layer.traits

	def same_traits(s, other_msg, proto:str, name:str, allow_unset=False, operator=all):
		if type(proto) is not str:
			return operator(
				s.same_traits(other_msg, proto_, name, allow_unset=allow_unset)
				for proto_ in proto
				)

		if name is None:
			my_traits = s.get_all_traits(proto)
			other_traits = other_msg.get_all_traits(proto)
			names = set(my_traits.keys())
			names.update(other_traits.keys())
			name = list(names)

		if type(name) is not str:
			return operator(
				s.same_traits(other_msg, proto, name_, allow_unset=allow_unset)
				for name_ in name
				)

		val = s.get_trait(proto, name)
		other_val = other_msg.get_trait(proto, name)
		if not allow_unset:
			if val is None or other_val is None:
				return False
		return val == other_val

	def set_trait(s, proto, name, val):
		layer = s.layers.get(proto, None)
		if layer is None:
			layer = Layer(s, proto, None, Traits(Trait(name, val)))
		else:
			layer.traits.set(name, val)

	def collapse(s, messages, my_idx):
		'''iterate backwards over recent messages and see if messages can be combined'''
		orig_msg = messages[my_idx]
		for layer in s.layers.values():
			msg = layer.collapse(messages, my_idx)
			if orig_msg is not msg:
				break
		return msg

	def absorb_msg(s, other_msg):
		global g_current_msg
		if g_current_msg is other_msg:
			g_current_msg = s
			s._log.extend(other_msg._log)
		if other_msg and other_msg is not s:
			s.absorbed.append(other_msg)
			if other_msg.absorbed:
				other_absorbed = other_msg.absorbed
				other_msg.absorbed = []
				for oa in other_absorbed:
					s.absorb_msg(oa)

	def identify_conns(s, messages, my_idx):
		msg = messages[my_idx]
		for layer_class in Layer.classes().values():
			if layer_class.proto not in msg.layers:
				continue
			layer_class.identify_conns(messages, my_idx)

	class EntityIdent:
		def __init__(s, proto=None, src_port=None, src_kind=None, src_entity=None, dst_port=None, dst_kind=None, dst_entity=None, rename=False):
			set_instance_vars_from_args()

	def identify_entities(s, messages, my_idx):
		'''From protocol and message discriminators, see if we can identify the src and dst port of the message
		to be of a specific core network entity.'''
		for layer in s.layers.values():
			try:
				identifieds = layer.identify_entities(s, messages, my_idx)
				if identifieds is None:
					continue
				if isinstance(identifieds, Message.EntityIdent):
					identifieds = [identifieds]

				for ident in identifieds:
					if ident is None:
						continue
					Entity.find_or_create(ident.proto, ident.src_kind,
								ident.src_port or s.src,
								ident.src_entity, from_msg=s,
								rename=(ident.rename is True or ident.rename == 'src'))
					Entity.find_or_create(ident.proto, ident.dst_kind,
								ident.dst_port or s.dst,
								ident.dst_entity, from_msg=s,
								rename=(ident.rename is True or ident.rename == 'dst'))
			except:
				raise Exception(f'In layer {layer} {s}')

	def find_entity(s, kind, with_port=None):
		for conn in (s.src_conns + s.dst_conns):
			ret = conn.find_entity(kind, with_port=with_port)
			if ret and ret[0] is not None:
				return ret
		return None, None

	def get_port(s, entity_kind:str):
		if s.src_entity_is(entity_kind):
			return s.src
		elif s.dst_entity_is(entity_kind):
			return s.dst
		return None

	def entity(s, *kinds):
		if s.src.entity and s.src.entity.kind in kinds:
			return s.src.entity
		if s.dst.entity and s.dst.entity.kind in kinds:
			return s.dst.entity

	def src_entity_is(s, *kinds):
		return s.src.entity and s.src.entity.kind in kinds

	def dst_entity_is(s, *kinds):
		return s.dst.entity and s.dst.entity.kind in kinds

	def same_src_dst(s, other, forward=None, reverse=None):
		# assume forward and reverse if neither are set.
		# if one of them is set to True, assume the other as False.
		if forward is None and reverse is None:
			forward = True
			reverse = True
		a = (s.src.key(), s.dst.key())
		b = (other.src.key(), other.dst.key())
		if forward and reverse:
			return set(a) == set(b)
		elif forward:
			return a == b
		elif reverse:
			return a == tuple(reversed(b))
		else:
			return False

	@classmethod
	def parse(cls, p:Packet):
		msg = Message(p)
		Layer.parse(msg)
		msg.src = msg.get_trait(('tcp','udp','sctp'), 'src')
		msg.dst = msg.get_trait(('tcp','udp','sctp'), 'dst')
		if msg.src is None or msg.dst is None:
			return None
		assert isinstance(msg.src, IpPort)
		assert isinstance(msg.dst, IpPort)
		return msg

	def label(s):
		label = []
		for l in s.layers.values():
			if not SHOW_ALL_LAYERS:
				if l.minor:
					continue
				if l.hidden and not all((ll.minor or ll.hidden) for ll in s.layers.values()):
					continue
			label.insert(0, l.label())
		return '/'.join(label)

	def related_subscribers(s):
		subscribers = UniqueList()
		src_sc = s.src_subscriber_conn()
		if src_sc:
			subscribers.append(src_sc.subscriber)
		dst_sc = s.dst_subscriber_conn()
		if dst_sc:
			subscribers.append(dst_sc.subscriber)
		for a in s.absorbed:
			subscribers.extend(a.related_subscribers())
		return subscribers

	def is_subscriber_related(s, subscriber):
		return subscriber in s.related_subscribers()

	def __repr__(s):
		return s.__str__()

	def __str__(s):
		return s.str()

	def str(s, ladder=False, one_column_per_kind=False, show_traits=True, show_conns=True):
		t_name = []
		t_name.extend(subscr.label() for subscr in s.related_subscribers())
		name = s.label()
		if name:
			t_name.append(name)

		src = str(s.src)
		dst = str(s.dst)

		if s.src.entity is not None:
			src_str = s.src.entity.label()
		else:
			src_str = src

		if s.count and dst == src:
			dst_str = '(self)'
		elif s.dst.entity is not None:
			dst_str = s.dst.entity.label()
		else:
			dst_str = dst

		src_pos = 0
		dst_pos = 0
		if s.src.entity:
			src_pos = s.src.entity.labelcolumn(one_column_per_kind)
		if s.dst.entity:
			dst_pos = s.dst.entity.labelcolumn(one_column_per_kind)

		# allows injecting informational fake messages (Entity.news)
		if not s.count and not s.count_back:
			dst_pos = src_pos

		if not s.src.entity and not s.dst.entity:
			if src > dst:
				src_pos = dst_pos + 1
			else:
				dst_pos = src_pos + 1

		if not ladder:
			if src_pos > dst_pos:
				src_pos = 1
				dst_pos = 0
			else:
				src_pos = 0
				dst_pos = 1

		if src_pos <= dst_pos:
			left_pos = src_pos
			right_pos = dst_pos
			left_label = src_str
			right_label = dst_str
			to_left_count = s.count_back
			to_right_count = s.count
		else:
			left_pos = dst_pos
			right_pos = src_pos
			left_label = dst_str
			right_label = src_str
			to_left_count = s.count
			to_right_count = s.count_back

		left_strs = []
		left_strs.append(left_label)
		if to_left_count:
			left_strs.append('<')
			if to_left_count > 1:
				left_strs.append(f'{to_left_count}')

		right_strs = []
		if to_right_count:
			if to_right_count > 1:
				right_strs.append(f'{to_right_count}')
			right_strs.append('>')
		right_strs.append(right_label)

		real_left_pos = max(0, left_pos - (len(left_label)//2))
		real_right_pos = right_pos - (len(right_label)//2) + len(right_label) + 1 - (len(right_label)&1)

		left_str = ''.join(left_strs)
		right_str = ''.join(right_strs)

		mid_gap = real_right_pos - real_left_pos - len(right_str) - len(left_str)
		mid_gap = max(1, mid_gap)

		if not ladder:
			mid_name_margin = 6
		else:
			mid_name_margin = mid_gap - len(name)
		if to_left_count or to_right_count:
			if mid_name_margin > 50:
				mid_gap_strs = ['-' * int(mid_name_margin // 2),
						name,
						'-' * int(mid_name_margin - (mid_name_margin//2))]
				name = ''
			else:
				mid_gap_strs = ['-' * int(mid_gap)]
		else:
			mid_gap_strs = []

		t = [' ' * int(real_left_pos),
		     left_str,]
		t.extend(mid_gap_strs)
		t.append(right_str)

		if ladder:
			t = [''.join(t)]
			right_end = len(t[0])
			label_pos = Entity.textcol_one_per_kind if one_column_per_kind else Entity.textcol_one_per_entity
			diff = label_pos - right_end
			if diff > 0:
				t.append(' ' * int(diff))

		if show_traits:
			if isinstance(show_traits, str):
				show_traits = [show_traits]
			for proto,l in s.layers.items():
				if not l.traits:
					continue
				if (show_traits is not True) and (proto not in show_traits):
					continue
				t_name.append('%s%s' % (proto, l.traits))

		if show_conns:
			conns = set()
			for c in (s.src_conns + s.dst_conns):
				conns.add(f'{c.proto}:{c.conn_id}')
				#conns.add(f'{c}')
			t_name.append(' '.join(conns))

		idxs = [s.p.idx] + [a.p.idx for a in s.absorbed]
		if len(idxs) <= 3:
			t_name.append('+'.join(str(i) for i in sorted(idxs)))
		else:
			t_name.append(f'{min(idxs)}-{max(idxs)}')
		t_name.append(f'{s.timestamp:.3f}')
		t.append(' ')
		t = [''.join(t)]
		indent = '\n' + (' ' * len(t[0]) + ' | ')
		t.append(' '.join(t_name))

		for l in s._log:
			t.append(indent)
			t.append(l)
		return ''.join(t)


	def src_subscriber_conn(s):
		for conn in s.src_conns:
			if conn.subscriber_conn is not None:
				return conn.subscriber_conn
		return None

	def dst_subscriber_conn(s):
		for conn in s.dst_conns:
			if conn.subscriber_conn is not None:
				return conn.subscriber_conn
		return None

	def find_message(s, proto, trait, value):
		for subscr_conn in (s.src_subscriber_conn(), s.dst_subscriber_conn()):
			if subscr_conn is None:
				continue
			msg = subscr_conn.find_message(proto, trait, value)
			if msg:
				return msg
		return None



class Entity:
	'''A core network program like BSC, MSC, ...'''
	KINDS_SORTING = ('MS', 'BTS', 'PCU', 'hNodeB', 'BSC', 'MGW@BSC', 'HNBGW', 'STP', 'MSC', 'MGW@MSC', 'MGW',
			 'SGSN', 'HLR', 'SIP', 'SIPcon', 'PBX', 'GGSN')
	KINDS_SORTING_EXIST = ()
	entities = listdict()
	state_version = 1 # whether to update cached text columns
	spacing = 5
	label_spacing = 2
	textcol_one_per_kind = 0
	textcol_one_per_entity = 0

	# proxy / forwarding addresses to ignore, like the STP port
	blacklist = []

	def __init__(s, kind:str):
		set_instance_vars_from_args()
		s.idx = None
		s.state_version = 0
		s.ports = listdict()
		s.labelcol_one_per_kind = 0
		s.labelcol_one_per_entity = 0
		Entity.add(s)

	@classmethod
	def add(cls, entity):
		Entity.entities.add(entity.kind, entity)
		entity.idx = entity.idx_in_kind()
		if entity.kind not in Entity.KINDS_SORTING_EXIST:
			# a new kind has come up, refresh Entity.KINDS_SORTING_EXIST
			exist = []
			for k in Entity.KINDS_SORTING:
				if k in Entity.entities.keys():
					exist.append(k)
			for k in Entity.entities.keys():
				if k not in exist:
					exist.append(k)
			Entity.KINDS_SORTING_EXIST = tuple(exist)

		Entity.state_version += 1

	@classmethod
	def count_entities(cls, kind):
		l = Entity.entities.get(kind)
		return len(l)

	@classmethod
	def add_to_blacklist(cls, port:IpPort):
		if port in cls.blacklist:
			return
		cls.blacklist.append(port);

	def rename(s, to_kind):
		Entity.entities.remove(s.kind, s)
		was_kind = s.kind
		s.kind = to_kind
		Entity.add(s)
		for proto,l in s.ports.items():
			for port in l:
				LOG(Color.colored('yellow', 'Rename port'), 'from', was_kind, 'to', s, proto, port)

	@classmethod
	def find_or_create(cls, proto, kind, port, matched_entity=None, from_msg=None, rename=False):
		if not port:
			return None
		if port in Entity.blacklist:
			return None

		found_entity = port.entity
		if found_entity and matched_entity and (found_entity is not matched_entity):
			LOG(Color.colored('purple', 'Renaming entity port:'), port, 'to', matched_entity)
		if not matched_entity:
			matched_entity = found_entity
		if matched_entity:
			if kind and matched_entity.kind != kind and rename:
				matched_entity.rename(kind)
			matched_entity.add_port(proto, port, from_msg=from_msg)
			return matched_entity

		if kind is None or rename:
			for l in Entity.entities.values():
				for e in l:
					if e.has_port(port):
						if kind and e.kind != kind and rename:
							e.rename(kind)
						return e

			if kind is None:
				return None
		else:
			l = Entity.entities.get(kind)
			if l:
				for e in l:
					if e.has_port(port):
						return e
		e = Entity(kind)
		e.add_port(proto, port, from_msg=from_msg)
		return e

	def absorb(s, other_entity):
		'''Merge two entities to one, adopting the other's ports'''
		for port in other_entity.ports:
			s.add_port(port)
		del other_entity

	def label(s):
		idx = ''
		if s.idx:
			idx = str(s.idx + 1)
		return f'{s.kind}{idx}'

	def __repr__(s):
		return s.label()
	def __str__(s):
		return repr(s)

	def kind_idx(s):
		'''this entity kind's position in the currently known entity kinds:
		For 'BSC', if we've seen BTS, BSC and MSC, return 1.'''
		return Entity.KINDS_SORTING_EXIST.index(s.kind)

	def idx_in_all(s):
		'''this entity kind's position in all currently known entities:
		For the second 'BSC', if we've seen 2 BTS, 3 BSC and 1 MSC, return 2 (BTS) + 1 (second BSC) = 3.'''
		idx = 0
		for k in Entity.KINDS_SORTING_EXIST:
			if k == s.kind:
				idx += Entity.entities.get(s.kind).index(s)
				return idx
			idx += Entity.count_entities(k)
		return idx

	def idx_in_kind(s):
		'''this entity kind's position in the list of entities of the same kind'''
		return Entity.entities.get(s.kind).index(s)

	def check_update_state(s):
		if s.state_version == Entity.state_version:
			return
		Entity.calculate_textcolumns()
		s.state_version = Entity.state_version

	def labelcolumn(s, one_column_per_kind=False, mid=True):
		s.check_update_state()
		if one_column_per_kind:
			midcol = s.labelcol_one_per_kind
		else:
			midcol = s.labelcol_one_per_entity

		if mid:
			ret = midcol
		else:
			ret = int(midcol - (len(s.label()) // 2))
		return ret

	@classmethod
	def calculate_textcolumns(cls):
		'''In text rendering of a ladder diagram, return the text column for this entity,
		if rendering each entity in its own column (not sharing one column per entity kind)'''
		entity_col = 0
		kind_col = 0
		for k in Entity.KINDS_SORTING_EXIST:
			l = Entity.entities.get(k)

			kind_col += len(k) // 2
			for e in l:
				e.labelcol_one_per_kind = kind_col
				Entity.textcol_one_per_kind = kind_col + len(e.label()) + Entity.spacing

				entity_col += len(e.kind)//2
				e.labelcol_one_per_entity = entity_col
				entity_col += len(e.label()) + Entity.spacing
				Entity.textcol_one_per_entity = entity_col

			kind_col += len(k) + Entity.spacing

	def has_port(s, port, proto=None):
		if proto:
			if port in s.ports.get(proto, []):
				return proto
			return None
		for proto,l in s.ports.items():
			if port in l:
				return proto
		return None

	def remove_port(s, port):
		for proto,l in s.ports.items():
			if port in l:
				l.remove(port)
				port.entity = None
				return

	def add_port(s, proto, port, from_msg=None):
		if port.entity and port.entity is not s:
			port.entity.remove_port(port)
		if s.has_port(port, proto=proto):
			return
		s.ports.add(proto, port)
		port.entity = s
		port.proto = proto
		LOG(Color.colored('cyan', 'New port:'), port)


class Subscriber:
	next_ident = 1
	next_tmsi_idx = 1
	imsis = {}
	tmsis = {}
	imeis = {}
	msisdns = {}

	def __init__(s, imsi:str=None, tmsi=None, imei=None, msisdn=None):
		set_instance_vars_from_args()
		s.subscriber_conns = UniqueList()
		s.tmsis = set()
		s.tmsi_idx = 0
		s.set_last_tmsi(tmsi)
		s.ident = Subscriber.next_ident
		Subscriber.next_ident += 1
		Subscriber.update_dicts(s)

	def set_last_tmsi(s, tmsi):
		if tmsi is None:
			return
		s.tmsi = tmsi
		if (not isinstance(tmsi, str)) or len(str(tmsi)) < 8:
			raise Exception('Invalid TMSI: ' + str(tmsi))
		s.tmsi_idx = Subscriber.next_tmsi_idx
		Subscriber.next_tmsi_idx += 1
		s.tmsis.add(tmsi)
		Subscriber.tmsis[s.tmsi] = s

	@classmethod
	def update_dicts(cls, s):
		if s.imsi:
			Subscriber.imsis[s.imsi] = s
		for tmsi in s.tmsis:
			Subscriber.tmsis[tmsi] = s
		if s.imei:
			Subscriber.imeis[s.imei] = s
		if s.msisdn:
			Subscriber.msisdns[s.msisdn] = s

	def label(s, full=False):
		l = []
		if full or (not s.imsi and not s.tmsi and not s.imei and not s.msisdn):
			l.append(f'subscr{s.ident}')
		if s.imsi and (full or not s.msisdn):
			l.append(f'imsi{s.imsi}')

		if s.tmsi and (full or not s.imsi):
			l.append(f'tmsi{s.tmsi}')
		if s.imei and (full or (not s.imsi and not s.tmsi)):
			l.append(f'imei{s.imei}')
		if s.msisdn:
			l.append(f'msisdn{s.msisdn}')
		return Color.colored(s.ident, ':'.join(l))

	def __repr__(s):
		return s.label(full=True)
	def __str__(s):
		return s.label()

	@classmethod
	def identify_subscriber(cls, msg:Message):
		imsi = msg.get_trait(('dtap','bssmap','rsl','gsup'), 'imsi')
		tmsi = tmsi_standardize(msg.get_trait(('dtap','bssmap','rsl'), 'tmsi'))
		imei = msg.get_trait('dtap', 'imei')
		msisdn = msg.get_trait('gsup', 'msisdn')

		if not (imsi or tmsi or imei or msisdn):
			return
		#if not msg.src_conns and not msg.dst_conns:
		#	return

		# could start out with subscr = None, but to use a few less subscriber.ids start out with a present
		# subscriber, if any.
		subscr_conn = msg.src_subscriber_conn() or msg.dst_subscriber_conn()
		if subscr_conn is not None:
			subscr = subscr_conn.subscriber
		else:
			subscr = None

		if imsi:
			subscr = Subscriber.merge(Subscriber.by_imsi(imsi), subscr)
		if tmsi:
			subscr = Subscriber.merge(Subscriber.by_tmsi(tmsi), subscr)
		if imei:
			subscr = Subscriber.merge(Subscriber.by_imei(imei), subscr)
		if msisdn:
			subscr = Subscriber.merge(Subscriber.by_msisdn(msisdn), subscr)

		if subscr is None:
			return
		if subscr_conn is None:
			subscr_conn = SubscriberConn()
		subscr_conn.set_subscriber(subscr)

		for c in (msg.src_conns + msg.dst_conns):
			if c.subscriber_conn is None:
				subscr_conn.add_conn(c)
				c.subscriber_conn = subscr_conn
			else:
				c.subscriber_conn = SubscriberConn.merge(c.subscriber_conn, subscr_conn)

	@classmethod
	def by_imsi(cls, imsi):
		subscr = Subscriber.imsis.get(imsi)
		if not subscr:
			subscr = Subscriber(imsi=imsi)
		return subscr

	@classmethod
	def by_tmsi(cls, tmsi):
		subscr = Subscriber.tmsis.get(tmsi)
		if not subscr:
			subscr = Subscriber(tmsi=tmsi)
		return subscr

	@classmethod
	def by_imei(cls, imei):
		subscr = Subscriber.imeis.get(imei)
		if not subscr:
			subscr = Subscriber(imei=imei)
		return subscr

	@classmethod
	def by_msisdn(cls, msisdn):
		subscr = Subscriber.msisdns.get(msisdn)
		if not subscr:
			subscr = Subscriber(msisdn=msisdn)
		return subscr

	@classmethod
	def merge(cls, a, b):
		assert a is None or isinstance(a, Subscriber)
		assert b is None or isinstance(b, Subscriber)
		if a is None and b is None:
			return None
		if a is None:
			return b
		if b is None or b is a:
			return a

		if not a.imsi and a.ident > b.ident:
			return cls.merge(b, a)

		if a.imsi and b.imsi and a.imsi != b.imsi:
			out_error(f'cannot absorb, subscriber would change IMSI: {b.imsi} -> {a.imsi}')
			return None
		if a.imei and b.imei and a.imei != b.imei:
			out_error(f'cannot absorb, subscriber would change IMEI: {b.imei} -> {a.imei}')
			return None

		if b.imsi:
			a.imsi = b.imsi
			b.imsi = None

		if b.tmsis:
			a.tmsis.update(b.tmsis)
			b.tmsis = set()
			if b.tmsi_idx > a.tmsi_idx:
				a.set_last_tmsi(b.tmsi)
			b.tmsi = None

		if b.imei:
			a.imei = b.imei
			b.imei = None

		if b.msisdn:
			if a.msisdn and a.msisdn != b.msisdn:
				LOG(f'subscriber {a} changes MSISDN: {a.msisdn} -> {b.msisdn}')
			a.msisdn = b.msisdn
			b.msisdn = None

		Subscriber.update_dicts(a)

		for sc in b.subscriber_conns:
			a.add_subscriber_conn(sc)
		b.subscriber_conns = []
		return a

	def add_subscriber_conn(s, subscriber_conn):
		if subscriber_conn.subscriber is s:
			return
		if subscriber_conn.subscriber:
			subscriber_conn.subscriber.subscriber_conns.remove(subscriber_conn)
		s.subscriber_conns.append(subscriber_conn)
		subscriber_conn.subscriber = s
		assert subscriber_conn in subscriber_conn.subscriber.subscriber_conns

	def find_entity(s, kind, with_port=None):
		for subscriber_conn in reversed(s.subscriber_conns):
			found, found_subscriber_conn = subscriber_conn.find_entity(kind, ask_subscriber=False,
										   with_port=with_port)
			if found is not None:
				return found, found_subscriber_conn
		return None, None

class SubscriberConn:
	'''A SubscriberConn is a collection of conns that feed into each other.
	For example, the Abis and the BSSMAP link for the same subscriber are related, as well as the MGCP and
	RTP spoken for that subscriber.
	If a subscriber disconnects and connects again, that is a new separate SubscriberConn;
	also if a subscriber would concurrently attach in twice somehow, that would be separate SubscriberConn
	instances.

	Note that a Message's src_conns and a dst_conns are not necessarily listed in the same SubscriberConn, for example
	for RTP, SIP or SMS, the messages may pass from one subscriber to another.'''

	def __init__(s, subscriber=None):
		set_instance_vars_from_args()
		s.conns = UniqueList()

	@classmethod
	def merge(cls, a, b):
		assert a is None or isinstance(a, SubscriberConn)
		assert b is None or isinstance(b, SubscriberConn)
		if a is None and b is None:
			return None
		if a is None:
			return b
		if b is None or b is a:
			return a
		b_subscr = b.subscriber
		if b.subscriber:
			b.subscriber.subscriber_conns.remove(b)
			b.subscriber = None
		a.subscriber = Subscriber.merge(a.subscriber, b_subscr)
		if a.subscriber:
			a.subscriber.subscriber_conns.append(a)
		for conn in b.conns:
			conn.subscriber_conn = a
			a.conns.append(conn)
		b.conns = None
		return a

	def find_entity(s, kind, with_port=None, ask_subscriber=True):
		if ask_subscriber and s.subscriber is not None:
			return s.subscriber.find_entity(kind, with_port=with_port)
		if with_port is not None and isinstance(with_port, str):
			with_port = [with_port]
		for conn in reversed(s.conns):
			if with_port is not None and conn.port.proto not in with_port:
				continue
			if conn.port.entity is not None and conn.port.entity.kind == kind:
				return conn.port.entity, s
		return None, None

	def find_message(s, proto, trait, val):
		for conn in reversed(s.conns):
			for msgs in (conn.tx_messages, conn.rx_messages):
				for msg in msgs:
					for p, t, v in msg.get_traits(proto, trait):
						if val is None or val == v:
							return msg
		return None

	def set_subscriber(s, subscriber):
		s.subscriber = subscriber
		if subscriber is not None:
			subscriber.subscriber_conns.append(s)

	def add_conn(s, conn):
		s.conns.append(conn)
		conn.subscriber_conn = s

	def __repr__(s):
		return f'{s.subscriber}~{s.conns}'


class Layer_tcp(Layer):
	def __init__(s, msg:Message):
		p = msg.p
		traits = Traits(
			src=IpPort.from_tcp_source(p),
			dst=IpPort.from_tcp_dest(p),
			)
		super().__init__(msg=msg, proto='tcp', msgtype=None, traits=traits, minor=True)

class Layer_udp(Layer):
	def __init__(s, msg:Message):
		p = msg.p
		traits = Traits(
			src=IpPort.from_udp_source(p),
			dst=IpPort.from_udp_dest(p),
			)
		super().__init__(msg=msg, proto='udp', msgtype=None, traits=traits, minor=True)

class Layer_sctp(Layer):
	def __init__(s, msg:Message):
		p = msg.p
		traits = Traits(
			src=IpPort.from_sctp_source(p),
			dst=IpPort.from_sctp_dest(p),
			stream_id = p.get('sctp.data_sid'),
			stream_seq = p.get('sctp.data_ssn'),
			)
		super().__init__(msg=msg, proto='sctp', msgtype=None, traits=traits, minor=True)

class Layer_rtp(Layer):
	def __init__(s, msg:Message):
		pt = msg.p.get('rtp.p_type')

		iuup_msgtype = sane_showname(msg.p.get('iuup.pdu_type.showname'))

		if iuup_msgtype is not None:
			msgtype = f'{pt}.{iuup_msgtype}'
		else:
			msgtype = pt

		traits = Traits(
			pt=pt,
			iuup=iuup_msgtype
			)
		super().__init__(msg=msg, proto='rtp', msgtype=msgtype, traits=traits)

	def collapse(s, messages, my_idx):
		msgtype = s.msg.get_trait('rtp', 'msgtype')
		src = s.msg.src
		dst = s.msg.dst
		for i in reversed(range(my_idx)):
			prev_msg = messages[i]
			if not prev_msg:
				continue
			if prev_msg.finalized:
				break
			if not 'rtp' in prev_msg.layers:
				if prev_msg.is_minor():
					continue
				else:
					break
			if prev_msg.get_trait('rtp', 'msgtype') != msgtype:
				continue
			if s.msg.same_src_dst(prev_msg, forward=True):
				# found a recent RTP similar RTP packet, combine
				prev_msg.count += 1
				messages[my_idx] = None
				prev_msg.absorb_msg(s.msg)
				return prev_msg
			if 1 and s.msg.same_src_dst(prev_msg, reverse=True):
				# same but backwards
				prev_msg.count_back += 1
				messages[my_idx] = None
				prev_msg.absorb_msg(s.msg)
				return prev_msg
		return s.msg

	# identify_entities: RTP ports are identified by watching RSL and MGCP, see Layer_gsm_abis_rsl.identify_conns_rtp

	@classmethod
	def identify_conns(cls, messages:list, my_idx:int):
		msg = messages[my_idx]

		conn = Conn.find('rtp', msg.src, conn_id=msg.src.key())
		if conn is not None:
			conn.add_message(msg)

		conn = Conn.find('rtp', msg.dst, conn_id=msg.dst.key())
		if conn is not None:
			conn.add_message(msg)

class Layer_mgcp(Layer):
	def __init__(s, msg:Message):
		p = msg.p
		verb = p.get('mgcp.req_verb')
		rsp = p.get('mgcp.rsp_rspstring')
		msgtype = verb or rsp or '?'
		tid = p.get('mgcp.transid', '')

		rtp_port = None
		sdp_rtp_ip = p.get('sdp.connection_info_address')
		sdp_rtp_port = p.get('sdp.media_port')
		if sdp_rtp_ip and sdp_rtp_port:
			rtp_port = IpPort.get(sdp_rtp_ip, sdp_rtp_port)

		if rsp:
			endp = p.get('mgcp.param_specificendpointid')
		else:
			endp = p.get('mgcp.req_endpoint')
			if endp and endp.startswith('rtpbridge/*@'):
				endp = None
		traits = Traits(
			tid=tid,
			endp=endp,
			ci=p.get('mgcp.param_connectionid'),
			verb=verb,
			rsp=rsp,
			rtp_port=rtp_port,
			)

		s.tid = tid
		super().__init__(msg=msg, proto='mgcp', msgtype=msgtype, traits=traits)

	def label(s):
		return f'mgcp.{s.tid}.{s.msgtype}'

	def identify_entities(s, msg:Message, messages, my_idx):
		if msg.get_trait('mgcp', 'verb') == 'CRCX':
			dst_kind = 'MGW'
			if msg.src_entity_is('BSC'):
				dst_kind = 'MGW@BSC'
			elif msg.src_entity_is('MSC'):
				dst_kind = 'MGW@MSC'
			return Message.EntityIdent(proto='mgcp', dst_kind=dst_kind)
		elif msg.get_trait('mgcp', 'rsp') and msg.src_entity_is('MGW', 'MGW@BSC','MGW@MSC'):
			rtp = msg.get_trait('mgcp', 'rtp_port')
			if rtp:
				msg.src.entity.add_port('rtp', rtp)
		return None

	@classmethod
	def find_req(cls, messages, my_idx):
		msg = messages[my_idx]
		for match in find_same_trait(msg, messages, my_idx, 'mgcp', 'tid'):
			if match.get_trait('mgcp', 'rsp'):
				continue
			if not match.same_src_dst(msg, reverse=True):
				continue
			return match
		return None

	@classmethod
	def identify_conns(cls, messages:list, my_idx:int):
		msg = messages[my_idx]
		proto = 'mgcp'

		if msg.get_trait('mgcp', 'verb'):
			endp = msg.get_trait('mgcp', 'endp')
			mgw_port = msg.dst
			endp_conn = Conn.find(proto, mgw_port, endp)
			if endp_conn is not None:
				endp_conn.add_message(msg)
				for c in (msg.src_conns + msg.dst_conns):
					endp_conn.merge_subscr_conns(c)

			ci = msg.get_trait('mgcp', 'ci')
			conn_id = f'{endp}:{ci}'
			conn = Conn.find(proto, mgw_port, conn_id)
			if conn:
				conn.add_message(msg)
				for c in (msg.src_conns + msg.dst_conns + msg.meta_conns):
					conn.merge_subscr_conns(c)

		if msg.get_trait('mgcp', 'rsp') == 'OK':
			req = cls.find_req(messages, my_idx)
			if req is None:
				ERR('MGCP response without request')
				return
			mgw_port = req.dst

			verb = req.get_trait('mgcp', 'verb')
			if verb == 'CRCX':
				# The MGCP connection
				endp = msg.get_trait('mgcp', 'endp') or req.get_trait('mgcp', 'endp')
				ci = msg.get_trait('mgcp', 'ci')
				if not endp or not ci:
					ERR('MGCP CRCX with endp =', endp, 'ci =', ci)

				# creating two levels of conn: a meta conn with just endp,
				# and a proper one with endp+ci
				# endp:
				endp_conn = Conn.find(proto, mgw_port, endp)
				if not endp_conn:
					endp_conn = Conn.open(proto, mgw_port, conn_id=endp, start_msg=msg)

				# endp+ci:
				conn_id = f'{endp}:{ci}'
				conn = Conn.find(proto, mgw_port, conn_id)
				if not conn:
					conn = Conn.open(proto, mgw_port, conn_id, msg)
				conn.add_message(req)
				conn.add_message(msg)
				conn.merge_subscr_conns(endp_conn)

				# The RTP connection set up by MGCP
				rtp_port = msg.get_trait('mgcp', 'rtp_port')
				if rtp_port:
					rtp_conn = Conn.open('rtp', rtp_port, conn_id=rtp_port.key(), start_msg=msg, merge_counterparts=False,
							     entity=conn.port.entity)
					rtp_conn.merge_subscr_conns(conn)
					# bssmap or ranap layer will mention this rtp_port in their Assignment / RAB-Assignment

			else:
				endp = req.get_trait('mgcp', 'endp')
				ci = req.get_trait('mgcp', 'ci')
				Conn.message(proto, mgw_port, endp, msg)

				conn_id = f'{endp}:{ci}'
				Conn.message(proto, mgw_port, conn_id, msg)

				if verb == 'DLCX':
					# The MGCP connection
					endp = req.get_trait('mgcp', 'endp')
					ci = req.get_trait('mgcp', 'ci')
					if not endp:
						ERR('MGCP DLCX without endp')

					def close_ci(ci):
						ci_conn_id = f'{endp}:{ci}'
						# go through all RTP ports created in this conn
						ci_conn = Conn.find(proto, mgw_port, ci_conn_id)
						if ci_conn is not None:
							all_rtp_ports = UniqueList()
							for msgs in (ci_conn.rx_messages, ci_conn.tx_messages):
								for msg in msgs:
									all_rtp_ports.append(msg.get_trait('mgcp', 'rtp_port'))

							for rtp_port in all_rtp_ports:
								Conn.close('rtp', rtp_port, conn_id=rtp_port.key(), close_msg=msg, if_exists=True)
							Conn.close(proto, mgw_port, ci_conn_id, msg)

					if ci:
						close_ci(ci)
					else:
						endp_conn = Conn.find(proto, mgw_port, endp)
						all_ci = UniqueList()
						if endp_conn:
							for msgs in (endp_conn.tx_messages, endp_conn.rx_messages):
								for msg in msgs:
									all_ci.append(msg.get_trait('mgcp', 'ci'))
						for ci in all_ci:
							close_ci(ci)

					Conn.close(proto, mgw_port, endp, msg, if_exists=True)


		elif msg.get_trait('mgcp', 'verb') == 'MDCX':
			# The RTP connection set up by BSC or MSC
			rtp_port = msg.get_trait('mgcp', 'rtp_port')
			if rtp_port and Conn.find('rtp', rtp_port, conn_id=rtp_port.key()) is None:
				rtp_conn = Conn.open('rtp', rtp_port, conn_id=rtp_port.key(), start_msg=msg, merge_counterparts=False)
				for c in msg.src_conns:
					rtp_conn.merge_subscr_conns(c)

class Layer_sccp(Layer):
	def __init__(s, msg:Message):
		p = msg.p
		msgtype = p.get('sccp.message_type.showname')
		traits = Traits(
			src_lref=p.get('sccp.slr'),
			dst_lref=p.get('sccp.dlr'),
			)
		super().__init__(msg=msg, proto='sccp', msgtype=msgtype, traits=traits, hidden=True)

	def collapse(s, messages, my_idx):
		msg = s.msg

		# cut out STP hop
		if SCCP_COLLAPSE_STP:
			src = msg.src
			t = msg.timestamp
			for i in reversed(range(my_idx)):
				prev_msg = messages[i]
				if not prev_msg:
					continue
				if t - prev_msg.timestamp > 1:
					break
				if prev_msg.absorbed:
					continue
				prev_sccp = prev_msg.layers.get(s.proto, None)
				if prev_sccp is None:
					continue
				#if src != prev_msg.dst:
				#	continue
				if s.msgtype != prev_sccp.msgtype:
					continue
				if not msg.same_traits(prev_msg, 'sccp', ('src_lref', 'dst_lref'), allow_unset=True):
					continue
				if not msg.same_traits(prev_msg, 'sctp', 'stream_id'):
					continue
				if not msg.same_traits(prev_msg, 'm3ua', ('opc', 'dpc')):
					continue
				if not msg.same_traits(prev_msg, 'm3ua', 'message_length'):
					continue

				prev_msg.set_trait('sctp', 'dst', msg.get_trait('sctp', 'dst'))
				prev_msg.dst = msg.dst
				prev_msg.absorb_msg(msg)
				messages[i] = None
				messages[my_idx] = prev_msg
				Entity.add_to_blacklist(src)
				return prev_msg
		return msg

	@classmethod
	def identify_conns(cls, messages:list, my_idx:int):
		msg = messages[my_idx]
		proto = 'sccp'
		msgtype = msg.get_trait(proto, 'msgtype')
		if SCCP_COLLAPSE_STP and not msg.absorbed:
			return
		src_id = msg.get_trait(proto, 'src_lref')
		dst_id = msg.get_trait(proto, 'dst_lref')
		if msgtype == 'Connection-Request':
			Conn.open(proto, msg.src, src_id, msg)
		elif msgtype == 'Connection-Confirm':
			Conn.open(proto, msg.src, src_id, msg,
				counterparts=[Conn.find(proto, msg.dst, dst_id)])
		elif msgtype in ('Release-Complete',):
			Conn.close(proto, msg.src, src_id, msg)
		else:
			if src_id:
				Conn.message(proto, msg.src, src_id, msg)
			if dst_id:
				Conn.message(proto, msg.dst, dst_id, msg)


class Layer_m3ua(Layer):
	def __init__(s, msg:Message):
		traits = Traits(
			opc = msg.p.get('m3ua.protocol_data_opc'),
			dpc = msg.p.get('m3ua.protocol_data_dpc'),
			message_length = msg.p.get('m3ua.message_length'),
			)
		super().__init__(msg=msg, proto='m3ua', msgtype=None, traits=traits, minor=True)

# wireshark commonly falsely classifies a BSSMAP Ciphering Mode Command as RNSAP PDU
class Layer_rnsap(Layer):
	def __init__(s, msg:Message):
		p = msg.p
		traits = Traits()
		msgtype = 'Cipher Mode Command'
		super().__init__(msg=msg, proto='bssmap', msgtype=msgtype, traits=traits)

class Layer_bssap(Layer):
	'BSSAP, either BSS Management (see Layer_gsm_a_bssmap) or Direct Transfer (see Layer_gsm_a_dtap)'
	def __init__(s, msg:Message):
		msgtype = msg.p.get('bssap.msgtype.showname')
		msgtype = msg.p.get('bssap.pdu_type.showname')

		traits = Traits(
			msgtype_nr=int(msg.p.get('bssap.pdu_type'), 16),
			)

		super().__init__(msg=msg, proto='bssap', msgtype=msgtype, traits=traits, minor=True)

class Layer_bssgp(Layer):
	def __init__(s, msg:Message):
		msgtype = sane_msgtype(msg.p.get('bssgp.pdu_type.showname'))
		traits = Traits(
				tlli=msg.p.get('bssgp.gsm_a_rr_tlli'),
			)
		super().__init__(msg=msg, proto='bssgp', msgtype=msgtype, traits=traits)

	def identify_entities(s, msg:Message, messages, my_idx):
		if msg.get_trait('bssgp', 'msgtype') == 'FLOW-CONTROL-BVC':
			return Message.EntityIdent(proto='bssgp', src_kind='PCU', dst_kind='SGSN')
		return None

	@classmethod
	def identify_conns(cls, messages:list, my_idx:int):
		msg = messages[my_idx]
		proto = 'bssgp'
		msgtype = msg.get_trait('dtap', 'msgtype')
		tlli = msg.get_trait('bssgp', 'tlli')
		conn_id = tlli
		if not conn_id:
			return
		if msgtype == 'Attach-Request':
			Conn.open(proto, msg.src, conn_id, msg,
				  counterparts=[Conn.open(proto, msg.dst, conn_id, msg)])
		elif msgtype == 'Attach-Accept':
			conn = Conn.close(proto, msg.src, conn_id, msg)
			new_conn_id = msg.get_trait('dtap', 'tmsi')
			new_conn = Conn.open(proto, msg.src, new_conn_id, msg,
				  counterparts=[Conn.open(proto, msg.dst, new_conn_id, msg)])
			new_conn.merge_subscr_conns(conn)
		elif msgtype == 'Attach-Complete':
			Conn.close(proto, msg.src, conn_id, msg)
		else:
			conn = Conn.message(proto, msg.src, conn_id, msg)




class Layer_hnbap(Layer):
	def __init__(s, msg:Message):
		def strip_till_dash(dashstr):
			if not dashstr or not '-' in dashstr:
				return dashstr
			dash = dashstr.rindex('-')
			return dashstr[dash+1:]

		msgtype = strip_till_dash(msg.p.get('hnbap.procedurecode.showname'))
		pdutype = strip_till_dash(sane_msgtype(msg.p.get('hnbap.hnbap_pdu.showname')))
		pdutype_nr = msg.p.get('hnbap.hnbap_pdu')
		traits = Traits(
			msgtype_nr=int(msg.p.get('hnbap.procedurecode')),
			pdutype=pdutype,
			pdutype_nr=int(pdutype_nr),
			)
		super().__init__(msg=msg, proto='hnbap', msgtype=msgtype, traits=traits)

	def identify_entities(s, msg:Message, messages, my_idx):
		if (msg.get_trait('hnbap', 'msgtype') in ('Register', 'HNBRegister', 'UERegister')) and (msg.get_trait('hnbap', 'pdutype_nr') == 0):
			return Message.EntityIdent(proto='Iuh', src_kind='hNodeB', dst_kind='HNBGW')
		return None

class Layer_rua(Layer):
	def __init__(s, msg:Message):
		def strip_till_dash(dashstr):
			if not dashstr or not '-' in dashstr:
				return dashstr
			dash = dashstr.rindex('-')
			return dashstr[dash+1:]

		msgtype = strip_till_dash(msg.p.get('rua.procedurecode.showname'))
		pdutype = strip_till_dash(sane_msgtype(msg.p.get('rua.rua_pdu.showname')))
		pdutype_nr = msg.p.get('rua.rua_pdu')
		cn_domain_i = msg.p.get('rua.cn_domainindicator')
		cn_domain = None
		if cn_domain_i == '0':
			cn_domain = 'cs'
		elif cn_domain_i == '1':
			cn_domain = 'ps'
		traits = Traits(
			msgtype_nr=int(msg.p.get('rua.procedurecode')),
			pdutype=pdutype,
			cn_domain=cn_domain,
			rua_ctx=msg.p.get('rua.context_id'),
			)
		super().__init__(msg=msg, proto='rua', msgtype=msgtype, traits=traits, cap_p_name='rua')

	@classmethod
	def identify_conns(cls, messages:list, my_idx:int):
		msg = messages[my_idx]
		proto = 'rua'
		msgtype = msg.get_trait(proto, 'msgtype')
		conn_id = (msg.get_trait(proto, 'cn_domain') or '?') + ':' + (msg.get_trait(proto, 'rua_ctx') or '?')
		if msgtype == 'Connect':
			conn = Conn.open(proto, msg.src, conn_id, msg)
			Conn.open(proto, msg.dst, conn_id, msg, counterparts=[conn])
		elif msgtype == 'Disconnect':
			Conn.close(proto, msg.dst, conn_id, msg)
		else:
			Conn.message(proto, msg.src, conn_id, msg)


class Layer_ranap(Layer):
	def __init__(s, msg:Message):
		msgtype = msg.p.get('ranap.rab_assignmentrequest_element'
			) or msg.p.get('ranap.rab_assignmentresponse_element'
			) or msg.p.get('ranap.initiatingmessage_element')

		rtp_port = None
		ip = msg.p.get('ranap.nsap_ipv4_addr')
		port_bin = msg.p.get('ranap.bindingid.binary_value')
		if ip and port_bin and len(port_bin) >= 2:
			port = int.from_bytes(port_bin[:2], "big")
			rtp_port = IpPort.get(ip, port)

		traits = Traits(
			rtp_port=rtp_port,
			)
		super().__init__(msg=msg, proto='ranap', msgtype=msgtype, traits=traits)

	def collapse(s, messages, my_idx):
		msg = s.msg

		# cut out HNBGW hop
		if IUH_COLLAPSE_HNBGW:
			src = msg.src
			t = msg.timestamp
			for i in reversed(range(my_idx)):
				prev_msg = messages[i]
				if not prev_msg:
					continue
				if t - prev_msg.timestamp > 1:
					break
				if src != prev_msg.dst:
					continue
				if msg.src.entity is not prev_msg.dst.entity:
					continue
				# DOESNT WORK
				if not msg.same_traits(prev_msg, 'ranap', None):
					continue
				if not msg.same_traits(prev_msg, 'sccp', ('src_lref', 'dst_lref'), allow_unset=True):
					continue
				if not msg.same_traits(prev_msg, 'sctp', 'stream_id'):
					continue
				if not msg.same_traits(prev_msg, 'm3ua', ('opc', 'dpc')):
					continue

				prev_msg.set_trait('sctp', 'dst', msg.get_trait('sctp', 'dst'))
				prev_msg.dst = msg.dst
				prev_msg.absorb_msg(msg)
				messages[i] = None
				messages[my_idx] = prev_msg
				Entity.add_to_blacklist(src)
				return prev_msg
		return msg

	def identify_entities(s, msg:Message, messages, my_idx):
		ids = []
		ids.append(s.identify_attach(msg, messages, my_idx))

		msgtype = msg.get_trait('ranap', 'msgtype')
		rtp_port = msg.get_trait('ranap', 'rtp_port')

		if rtp_port and msgtype == 'RAB-AssignmentRequest':
			# associate the MSC's MGCP port, but take care to not say the STP is an MSC
			crcx_ok = msg.find_message('mgcp', 'rtp_port', rtp_port)
			if crcx_ok and crcx_ok.src_entity_is('MGW'):
				mgw = crcx_ok.src.entity

				msc = None
				if msg.src_entity_is('MSC'):
					msc = msg.src.entity

				ids.append(Message.EntityIdent(proto='mgcp', src_port=crcx_ok.src, src_entity=mgw, src_kind='MGW@MSC', rename=True,
						dst_entity=msc, dst_port=crcx_ok.dst if msc else None))

		if rtp_port and msgtype == 'RAB-AssignmentResponse' and msg.src_entity_is('hNodeB'):
			ids.append(Message.EntityIdent(proto='rtp', src_kind='hNodeB', src_port=rtp_port, src_entity=msg.src.entity))

		return ids

	def identify_attach(s, msg:Message, messages, my_idx):
		ids = []
		dst_kind = None
		msgtype = msg.get_trait('dtap', 'msgtype')
		if msgtype in DTAP_COMPL_L3:
			dst_kind = 'MSC'
			proto = 'IuCS'
		elif msgtype in GMM_COMPL_L3:
			dst_kind = 'SGSN'
			proto = 'IuPS'

		if not dst_kind:
			return None

		if 'rua' in msg.layers:
			return Message.EntityIdent(proto='Iuh', src_kind='hNodeB', dst_kind='HNBGW')

		# don't mistake the STP as MSC or SGSN
		if SCCP_COLLAPSE_STP and not msg.absorbed:
			return None
		if not SCCP_COLLAPSE_STP and msg.src_entity_is('HNBGW'):
			return None

		# FIXME: below only makes sense with SCCP_COLLAPSE_STP == True
		if not SCCP_COLLAPSE_STP:
			return None

		# find a HNBGW that has recently received the same LU,
		# associate IuCS port
		src_entity = None
		for match in find_same_trait(msg, messages, my_idx, 'dtap', None):
			if 'rua' not in match.layers:
				continue
			if not match.dst_entity_is('HNBGW'):
				continue
			src_entity = match.dst.entity
			if src_entity:
				break

		return Message.EntityIdent(proto=proto, src_kind='HNBGW', src_entity=src_entity, dst_kind=dst_kind)

	@classmethod
	def identify_conns(cls, messages, my_idx):
		msg = messages[my_idx]

		if SCCP_COLLAPSE_STP and not msg.absorbed:
			return

		rtp_port = msg.get_trait('ranap', 'rtp_port')
		if rtp_port:
			rtp_conn = Conn.find('rtp', rtp_port, rtp_port.key())
			if rtp_conn:
				for c in msg.src_conns:
					rtp_conn.merge_subscr_conns(c)


class Layer_gsm_a_bssmap(Layer):
	def __init__(s, msg:Message):
		p = msg.p
		msgtype = p.get('gsm_a_bssmap.msgtype.showname')

		rtp_port = None
		ip = p.get('gsm_a_bssmap.aoip_trans_ipv4')
		port = p.get('gsm_a_bssmap.aoip_trans_port')
		if ip and port:
			rtp_port = IpPort.get(ip, port)

		tmsi = tmsi_standardize(p.get('gsm_a_bssmap.gsm_a_tmsi'))

		traits = Traits(
			msgtype_nr=str_to_int(p.get('gsm_a_bssmap.msgtype')),
			rtp_port=rtp_port,
			imsi=p.get('gsm_a_bssmap.e212_imsi'),
			tmsi=tmsi,
			)
		super().__init__(msg=msg, proto='bssmap', msgtype=msgtype, traits=traits, cap_p_name='gsm_a_bssmap')

	def identify_entities(s, msg:Message, messages, my_idx):
		# don't mistake the STP as MSC or BSC
		if SCCP_COLLAPSE_STP and not msg.absorbed:
			return None

		msgtype = msg.get_trait('bssmap', 'msgtype')
		if msgtype in ('Complete-Layer-3-Information', 'Clear-Complete'):

			# associate BSC BSSMAP port with BSC RSL port
			src_entity = None
			for match in find_same_trait(msg, messages, my_idx, 'dtap', ('tmsi', 'imsi')):
				if 'rsl' not in match.layers:
					continue
				if not match.dst.entity or match.dst.entity.kind != 'BSC':
					continue
				src_entity = match.dst.entity
				if src_entity:
					break

			return Message.EntityIdent(proto='bssmap', src_kind='BSC', dst_kind='MSC', src_entity = src_entity)

		if msgtype == 'Assignment-Request':
			# This Assignment-Request's rtp_port should match an earlier MGCP CRCX-OK message, and we now
			# know that this MSC asked for it.
			rtp_port = msg.get_trait('bssmap', 'rtp_port')
			if not rtp_port:
				return None

			def cond(prev_msg):
				return prev_msg.get_trait('mgcp', 'rtp_port') == rtp_port
			crcx_ok = None
			for prev_msg in find_recent_msg(msg, messages, my_idx, cond):
				crcx_ok = prev_msg
				break
			if not crcx_ok:
				return None
			msc = msg.src.entity
			mgw = crcx_ok.src.entity
			return Message.EntityIdent(proto='mgcp', src_port=crcx_ok.src, src_entity=mgw, src_kind='MGW@MSC', rename=True,
					dst_port=crcx_ok.dst, dst_entity=msc, dst_kind='MSC')


		return None

	@classmethod
	def identify_conns(cls, messages, my_idx):
		msg = messages[my_idx]
		msgtype = msg.get_trait('bssmap', 'msgtype')

		if SCCP_COLLAPSE_STP and not msg.absorbed:
			return

		# Paging does not have a proper end, it may never be answered.
		# The RSL paging command hopefully happens, and closes this Conn.
		if msgtype == 'Paging':
			imsi = msg.get_trait('bssmap', 'imsi')
			if imsi is not None:
				c = Conn.open('bssmap', msg.dst, f'page_imsi{imsi}', msg)
				Conn.close_conn(c, msg)
			tmsi = msg.get_trait('bssmap', 'tmsi')
			if tmsi is not None:
				c = Conn.open('bssmap', msg.dst, f'page_tmsi{tmsi}', msg)
				Conn.close_conn(c, msg)

		rtp_port = msg.get_trait('bssmap', 'rtp_port')
		if rtp_port is None:
			return

		rtp_conn = Conn.find('rtp', rtp_port, rtp_port.key())
		if rtp_conn is None:
			return
		for c in msg.src_conns:
			rtp_conn.merge_subscr_conns(c)

		def cond(prev_msg):
			return prev_msg.get_trait('mgcp', 'rtp_port') == rtp_port
		crcx_ok = None
		for prev_msg in find_recent_msg(msg, messages, my_idx, cond):
			for c in (prev_msg.src_conns + prev_msg.dst_conns):
				rtp_conn.merge_subscr_conns(c)


class Layer_gsm_abis_rsl(Layer):
	def __init__(s, msg:Message):
		p = msg.p
		msgtype = sane_msgtype(p.get('gsm_abis_rsl.msg_type.showname'))
		msgtype_nr = p.get('gsm_abis_rsl.msg_type')
		msgtype_nr = str_to_int(msgtype_nr)

		ch = None
		ch_imm_ass = None

		# For Immediate Assignment, the assigned TS/chan is more interesting
		ts = p.get('gsm_a_ccch.gsm_a_rr_timeslot')
		cbits = (p.get('gsm_a_ccch.gsm_a_rr_sdcch4_sdcchc4_cbch')
			or p.get('gsm_a_ccch.gsm_a_rr_sdcch8_sdcchc8_cbch'))
		try:
			ch_ts = int(ts)
			ch_cbits = int(cbits)
			ch_imm_ass = f'{ch_ts}-{ch_cbits}'
		except:
			pass

		# normal RSL messages on a given TS/chan
		ts = p.get('gsm_abis_rsl.ch_no_tn')
		cbits = p.get('gsm_abis_rsl.ch_no_cbits')
		if ts is not None and cbits is not None:
			try:
				ch_ts = int(ts)
				ch_cbits = int(cbits)
				ch = f'{ch_ts}-{ch_cbits}'
			except:
				raise

		ch_assign = None
		new_ch_ts = p.get('gsm_a_dtap.gsm_a_rr_timeslot')
		new_ch_ss = p.get('gsm_a_dtap.gsm_a_rr_tch_facch_sacchf')
		if new_ch_ts and new_ch_ss:
			ch_assign = f'{new_ch_ts}-{new_ch_ss}'

		rtp_local_port = None
		ipacc_rtp_local_ip = p.get('gsm_abis_rsl.ipacc_local_ip')
		ipacc_rtp_local_port = p.get('gsm_abis_rsl.ipacc_local_port')
		if ipacc_rtp_local_ip and ipacc_rtp_local_port:
			rtp_local_port = IpPort.get(ipacc_rtp_local_ip, ipacc_rtp_local_port)

		rtp_remote_port = None
		ipacc_rtp_remote_ip = p.get('gsm_abis_rsl.ipacc_remote_ip')
		ipacc_rtp_remote_port = p.get('gsm_abis_rsl.ipacc_remote_port')
		if ipacc_rtp_remote_ip and ipacc_rtp_remote_port:
			rtp_remote_port = IpPort.get(ipacc_rtp_remote_ip, ipacc_rtp_remote_port)

		req_ref_l = (p.get('gsm_abis_rsl.req_ref_ra'),
				p.get('gsm_abis_rsl.req_ref_t1prim'),
				p.get('gsm_abis_rsl.req_ref_t2'),
				p.get('gsm_abis_rsl.req_ref_t3'))
		if not all(req_ref_l):
			req_ref_l = (p.get('gsm_a_ccch.gsm_a_rr_ra'),
				p.get('gsm_a_ccch.gsm_a_rr_t1prim'),
				p.get('gsm_a_ccch.gsm_a_rr_t2'),
				p.get('gsm_a_ccch.gsm_a_rr_t3'))
		req_ref = None
		if all(req_ref_l):
			req_ref = '-'.join(req_ref_l)

		try:
			page_tmsi = tmsi_standardize(msg.p.cap_p.gsm_abis_rsl._all_fields['3gpp.tmsi'])
		except:
			page_tmsi = None

		traits = Traits(
			msgtype_nr=msgtype_nr,
			ch=ch,
			ch_imm_ass=ch_imm_ass,
			ch_assign=ch_assign,
			chan_type=p.get('gsm_abis_rsl.ch_type'),
			rtp_port=rtp_local_port,
			rtp_remote_port=rtp_remote_port,
			tmsi=tmsi_standardize(p.get('gsm_abis_rsl.gsm_a_tmsi')),
			imsi=p.get('gsm_abis_rsl.gsm_a_imsi'),
			page_tmsi=page_tmsi,
			arfcn = p.get('gsm_a_dtap.gsm_a_rr_single_channel_arfcn') or p.get('gsm_abis_rsl.gsm_a_rr_single_channel_arfcn'),
			req_ref = req_ref,
			)

		super().__init__(msg=msg, proto='rsl', msgtype=msgtype, traits=traits, cap_p_name='gsm_abis_rsl')
		# ignore CCCH Load INDication
		#if msgtype_nr == 18:
		#	msg.hide = True

	def identify_entities(s, msg:Message, messages, my_idx):
		ids = []
		msgtype = msg.get_trait('rsl', 'msgtype')

		if msgtype in ('RF-RESource-INDication', 'CCCH-LOAD-INDication', 'CHANnel-ReQuireD', ):
			# INDication from BTS to BSC
			ids.append(Message.EntityIdent(proto='rsl', src_kind='BTS', dst_kind='BSC'))
		if msgtype in ('CHANnel-ACTIVation', 'IMMEDIATE-ASSIGN-COMMAND'):
			# from BSC to BTS
			ids.append(Message.EntityIdent(proto='rsl', dst_kind='BTS', src_kind='BSC'))

		rtp_port = msg.get_trait('rsl', 'rtp_port')
		if rtp_port and msg.src_entity_is('BTS') and msgtype in (
				'ip.access-CRCX-ACK', 'ip.access-MDCX-ACK'):
			ids.append(Message.EntityIdent(proto='rtp', src_kind='BTS', src_entity=msg.src.entity, src_port=rtp_port))

		if msgtype == 'ip.access-MDCX':
			# This ip.a MDCX's rtp_remote_port should match an earlier MGCP CRCX-OK message, and we now
			# know that this BSC asked for it.

			rtp_port = msg.get_trait('rsl', 'rtp_remote_port')
			if not rtp_port:
				return None

			def cond(prev_msg):
				return prev_msg.get_trait('mgcp', 'rtp_port') == rtp_port
			crcx_ok = None
			for prev_msg in find_recent_msg(msg, messages, my_idx, cond):
				crcx_ok = prev_msg
				break
			if not crcx_ok:
				return None
			bsc = msg.src.entity
			mgw = crcx_ok.src.entity
			ids.append( Message.EntityIdent(proto='mgcp', src_port=crcx_ok.src, src_entity=mgw, src_kind='MGW@BSC', rename=True,
					dst_port=crcx_ok.dst, dst_entity=bsc, dst_kind='BSC') )

		return ids

	def collapse(s, messages, my_idx):
		# combine duplicates like rsl.CCCH-LOAD-INDication
		for i in reversed(range(my_idx)):
			prev_msg = messages[i]
			if not prev_msg:
				continue
			if prev_msg.finalized:
				break
			# stop combining at any non-rsl (and non-minor) message
			if not 'rsl' in prev_msg.layers:
				if all(l.minor for l in prev_msg.layers.values()):
					continue
				else:
					break
			if not same_nonempty(prev_msg.get_traits('rsl'), s.msg.get_traits('rsl')):
				continue
			if s.msg.same_src_dst(prev_msg, forward=True):
				# found a recent similar packet, combine
				prev_msg.count += 1
				messages[my_idx] = None
				prev_msg.absorb_msg(s.msg)
				return prev_msg
		return s.msg

	@classmethod
	def identify_conns_ra(cls, messages:list, my_idx:int):
		msg = messages[my_idx]

		msgtype = msg.get_trait('rsl', 'msgtype')

		bts = msg.entity('BTS')
		if bts is None:
			return None
		bts_port = msg.get_port('BTS')
		if bts_port is None:
			return None
		ra = msg.get_trait('rsl', 'req_ref')
		if ra is None:
			return None
		bts_ra = f'{bts_port.entity.label()}.ra{ra}'

		proto = 'rsl'
		conn_id = bts_ra

		conn = None

		if msgtype == 'CHANnel-ReQuireD':
			if not msg.src_entity_is('BTS'):
				return
			bts = msg.src.entity
			bts_port = msg.src
			bsc = msg.dst.entity
			bsc_port = msg.dst
			conn = Conn.open(proto, bts_port, conn_id, msg, entity=bts)
			Conn.open(proto, bsc_port, conn_id, msg, entity=bsc, counterparts=[conn])

		elif msgtype in ('CHANnel-ACTIVation'):
			conn = Conn.message(proto, msg.dst, conn_id, msg)

		elif msgtype == 'IMMEDIATE-ASSIGN-COMMAND':
			# the RA token has fulfilled its use as soon as an IMM ASS happened
			Conn.close(proto, msg.dst, conn_id, msg)

		else:
			Conn.message(proto, msg.dst, conn_id, msg)

		if conn:
			for c in (msg.src_conns + msg.dst_conns):
				conn.merge_subscr_conns(c)

	@classmethod
	def identify_conns_ch(cls, messages:list, my_idx:int):
		msg = messages[my_idx]
		proto = 'rsl'
		msgtype = msg.get_trait('rsl', 'msgtype')

		# For Immediate Assignment, the assigned TS/chan is the one to match on
		if msgtype == 'IMMEDIATE-ASSIGN-COMMAND':
			ch = msg.get_trait('rsl', 'ch_imm_ass')
		else:
			ch = msg.get_trait('rsl', 'ch')

		if ch is None:
			return None

		if msgtype in ('CHANnel-ACTIVation') or msg.src_entity_is('BSC'):
			bsc = msg.src.entity
			bsc_port = msg.src
			bts = msg.dst.entity
			bts_port = msg.dst
		elif msg.src_entity_is('BTS'):
			bts = msg.src.entity
			bts_port = msg.src
			bsc = msg.dst.entity
			bsc_port = msg.dst
		else:
			return None

		if bts_port.entity is None:
			return None

		bts_ch = f'{bts_port.entity.label()}.ch{ch}'
		conn_id = bts_ch

		if msgtype == 'CHANnel-ACTIVation':
			conn = Conn.open(proto, bts_port, conn_id, msg, entity=bts)
			Conn.open(proto, bsc_port, conn_id, msg, entity=bsc, counterparts=[conn])
		elif msgtype == 'RF-CHANnel-RELease-ACKnowledge':
			conn = Conn.close(proto, msg.src, conn_id, msg)
		else:
			conn = Conn.message(proto, bts_port, conn_id, msg)
		if conn:
			for c in (msg.src_conns + msg.dst_conns):
				conn.merge_subscr_conns(c)

		# when changing to a new ch, e.g. a regular Assignment Command to change from SDCCH to TCH,
		# associate the new channel with the conn
		ch_assign = msg.get_trait('rsl', 'ch_assign')
		if ch_assign:
			conn_id = f'{bts_port.entity.label()}.ch{ch_assign}'
			conn = Conn.find(proto, bts_port, conn_id)
			if conn:
				conn.add_message(msg)
				for c in (msg.src_conns + msg.dst_conns):
					conn.merge_subscr_conns(c)


	@classmethod
	def identify_conns_rtp(cls, messages:list, my_idx:int):
		msg = messages[my_idx]
		# BTS RTP
		rtp_port = msg.get_trait('rsl', 'rtp_port')
		if rtp_port and msg.src_entity_is('BTS') and msg.get_trait('rsl','msgtype') == 'ip.access-CRCX-ACK':
			rtp_port.entity = msg.src.entity
			rtp_conn = Conn.open('rtp', rtp_port, conn_id=rtp_port.key(), start_msg=msg, add_message=False)
			for c in (msg.src_conns + msg.dst_conns):
				rtp_conn.merge_subscr_conns(c)

		# MGW@BSC RTP towards BTS
		rtp_port = msg.get_trait('rsl', 'rtp_remote_port')
		if rtp_port:
			rtp_conn = Conn.find('rtp', rtp_port, conn_id=rtp_port.key())
			if rtp_conn:
				rtp_conn.add_message(msg)
				for c in (msg.src_conns + msg.dst_conns):
					rtp_conn.merge_subscr_conns(c)

	@classmethod
	def identify_conns_paging(cls, messages:list, my_idx:int):
		msg = messages[my_idx]
		msgtype = msg.get_trait('rsl', 'msgtype')

		if msgtype != 'PAGING-CoMmanD':
			return

		tmsi = msg.get_trait('rsl', 'page_tmsi')
		if tmsi is None:
			return
		conn_id = f'page_tmsi{tmsi}'

		subscr = Subscriber.by_tmsi(tmsi)
		if subscr is None:
			return

		rsl_conn = Conn.open('rsl', msg.src, conn_id, msg)
		subscr_conn = SubscriberConn()
		subscr.add_subscriber_conn(subscr_conn)
		subscr_conn.add_conn(rsl_conn)

		# Paging does not have a proper end, it may never be answered.
		# A Conn wants to be closed at some point. Just close it directly.
		Conn.close_conn(rsl_conn, msg)

		# Expecting a recent Paging on BSSMAP
		bssmap_paging = None
		def cond(prev_msg):
			return (prev_msg.get_trait('bssmap', 'msgtype') == 'Paging'
				and prev_msg.get_trait('bssmap', 'tmsi') == tmsi)
		for match in find_recent_msg(msg, messages, my_idx, cond):
			bssmap_paging = match
			break

		if bssmap_paging is None:
			return

		bssmap_conn = Conn.find('bssmap', bssmap_paging.dst, conn_id, find_in_closed_conns=True)
		if bssmap_conn:
			rsl_conn.merge_subscr_conns(bssmap_conn)

	@classmethod
	def identify_conns(cls, messages:list, my_idx:int):
		cls.identify_conns_ra(messages, my_idx)
		cls.identify_conns_ch(messages, my_idx)
		cls.identify_conns_rtp(messages, my_idx)
		cls.identify_conns_paging(messages, my_idx)

class Layer_gsm_a_dtap(Layer):
	def __init__(s, msg:Message):
		dtap = msg.p.get('gsm_a_dtap')
		assert dtap is not None

		msgtype = None
		msgtype_nr = None
		for f in dtap._get_all_fields_with_alternates():
			if f.name.startswith('gsm_a.dtap.msg_') and f.name.endswith('_type'):
				msgtype = f.showname_value
				try:
					msgtype_nr = int(f.raw_value)
				except:
					pass
		traits = Traits(
			msgtype_nr=msgtype_nr,
			imsi=msg.p.get('gsm_a_dtap.e212_imsi') or msg.p.get('gsm_a_dtap.gsm_a_imsi'),
			tmsi=tmsi_standardize(msg.p.get('gsm_a_dtap.gsm_a_tmsi') or msg.p.get('gsm_a_dtap.3gpp_tmsi')),
			imei=msg.p.get('gsm_a_dtap.gsm_a_imei'),
			to_msisdn=msg.p.get('gsm_a_dtap.cld_party_bcd_num'),
			)
		super().__init__(msg=msg, proto='dtap', msgtype=msgtype, traits=traits, cap_p_name='gsm_a_dtap')


class Layer_gsup(Layer):
	def __init__(s, msg:Message):
		msgtype = sane_msgtype(msg.p.get('gsup.msg_type.showname'))
		msisdn = None
		if '-forwardSM-' not in msgtype:
			msisdn = msg.p.get('gsup.e164_msisdn')
		to_msisdn = msg.p.get('gsup.gsm_sms_tp_da')

		msgtype_nr = None
		is_request = None
		try:
			msgtype_nr = int(msg.p.get('gsup.msg_type'))
			is_request = not (msgtype_nr & 0x3)
		except:
			pass

		session_state = None
		try:
			session_state = int(msg.p.get('gsup.session_state'))
		except:
			pass

		traits = Traits(
			imsi=msg.p.get('gsup.e212_imsi'),
			msgtype_nr=msgtype_nr,
			is_request=is_request,
			cn_domain=sane_showname(msg.p.get('gsup.cn_domain.showname')),
			msisdn=msisdn,
			to_msisdn=to_msisdn,
			source_name=msg.p.get('gsup.source_name_text'),
			destination_name=msg.p.get('gsup.destination_name_text'),
			session_id=msg.p.get('gsup.session_id'),
			session_state=session_state,
			)
		super().__init__(msg=msg, proto='gsup', msgtype=msgtype, traits=traits)

	@classmethod
	def identify_conns(cls, messages:list, my_idx:int):
		msg = messages[my_idx]
		imsi = msg.get_trait('gsup', 'imsi')
		is_request = msg.get_trait('gsup', 'is_request')
		session_id = msg.get_trait('gsup', 'session_id')
		session_state = msg.get_trait('gsup', 'session_state')
		if not imsi:
			return
		proto = 'gsup'
		if session_id:
			conn_id = f'{imsi}:{session_id}'
			have = Conn.find(proto, msg.src, conn_id) or Conn.find(proto, msg.dst, conn_id)
			if have is None:
				have = Conn.open(proto, msg.src, conn_id, msg,
					  counterparts=[Conn.open(proto, msg.dst, conn_id, msg)])
			if session_state is not None and session_state == 0x3:
				Conn.close(proto, msg.src, conn_id, msg)
			else:
				have.add_message(msg)

		else:
			conn_id = f'{imsi}'

			if is_request:
				have = Conn.find(proto, msg.src, conn_id)
				if have is None:
					Conn.open(proto, msg.src, conn_id, msg)
				else:
					have.add_message(msg)
			else:
				have = Conn.find(proto, msg.dst, conn_id)
				if have is None:
					# it's a stray response? anyway create a conn for association with a subscriber
					Conn.open(proto, msg.dst, conn_id, msg)
				Conn.close(proto, msg.dst, conn_id, msg)

	def identify_entities(s, msg:Message, messages, my_idx):
		if msg.get_trait('gsup', 'msgtype') in ('SendAuthInfo-Request', 'UpdateLocation-Request', 'PurgeMS-Request'):
			cn = msg.get_trait('gsup', 'cn_domain')
			src_kind = None
			src_entity = None
			src_subscr_conn = None
			if msg.get_trait('gsup', 'source_name'):
				# proxy forwarding
				src_kind = 'HLR'
				traits = [name for proto, name, result in msg.get_traits('gsup') if name not in ('source_name', 'destination_name')]
				for match in find_same_trait(msg, messages, my_idx, 'gsup', traits):
					if not match.dst_entity_is('HLR'):
						continue
					src_entity = match.dst.entity
					break
			else:
				if cn == 'CS':
					src_kind = 'MSC'
				elif cn == 'PS':
					src_kind = 'SGSN'

				if src_kind:
					# associate MSC GSUP port with MSC BSSMAP port
					imsi = msg.get_trait('gsup', 'imsi')
					if imsi:
						subscr = Subscriber.by_imsi(imsi)
						def cond(prev_msg):
							return (prev_msg.dst_entity_is(src_kind)
								and prev_msg.is_subscriber_related(subscr))
						for match in find_recent_msg(msg, messages, my_idx, cond):
							src_entity = match.dst.entity
							break

					# i forgot whatever this does:
					if not src_entity:
						src_entity, src_subscr_conn = s.msg.find_entity(src_kind,
												with_port=('bssgp', 'IuPS'))
				else:
					# no cn_domain in the GSUP message, try to guess
					msc, msc_subscr_conn = s.msg.find_entity('MSC')
					sgsn, sgsn_subscr_conn = s.msg.find_entity('SGSN')
					if msc and not sgsn:
						src_entity = msc
						src_subscr_conn = msc_subscr_conn
					if sgsn and not msc:
						src_entity = sgsn
						src_subscr_conn = sgsn_subscr_conn

			if src_subscr_conn is not None and msg.src_conns:
				for c in msg.src_conns:
					c.subscriber_conn = SubscriberConn.merge(c.subscriber_conn, src_subscr_conn)

			return Message.EntityIdent(proto='gsup', src_kind=src_kind, dst_kind='HLR', src_entity=src_entity, dst_entity=msg.dst.entity)

class Layer_sip(Layer):
	def __init__(s, msg:Message):
		method = msg.p.get('sip.method')
		cseq_method = msg.p.get('sip.cseq_method')
		status_code = msg.p.get('sip.status_code')
		status_line = msg.p.get('sip.status_line')
		if status_line:
			if '--' in status_line:
				status_line = status_line[:status_line.index('--')]
			status_line = status_line.strip()
			status = status_line.split()[-1]
		else:
			status = status_code
		if status:
			msgtype = f'{cseq_method}-{status}'
		elif method:
			msgtype = method
		else:
			msgtype = cseq_method

		sip_from_host = msg.p.get('sip.from_host')
		sip_from_port = msg.p.get('sip.from_port')
		sip_from = IpPort.get(sip_from_host, sip_from_port)

		rtp_port = None
		ip = msg.p.get('sip.sdp_connection_info_address')
		port = msg.p.get('sip.sdp_media_port')
		if ip and port:
			rtp_port = IpPort.get(ip, port)

		server = msg.p.get('sip.Server')
		agent = msg.p.get('sip.User-Agent')

		traits = Traits(
			sip_agent=server or agent,
			sip_from=sip_from,
			call_id = msg.p.get('sip.call_id'),
			method = cseq_method,
			seq = msg.p.get('sip.cseq_seq'),
			to_msisdn = msg.p.get('sip.to_user'),
			from_msisdn = msg.p.get('sip.from_user'),
			from_tag = msg.p.get('sip.from_tag'),
			r_uri = msg.p.get('sip.r_uri'),
			status_code = status_code,
			rtp_port=rtp_port,
			)
		super().__init__(msg=msg, proto='sip', msgtype=msgtype, traits=traits)

	def identify_entities(s, msg:Message, messages, my_idx):
		src_kind = 'SIP'
		sip_from = msg.get_trait('sip', 'sip_from')
		agent = msg.get_trait('sip', 'sip_agent')

		rename = False
		if agent and sip_from and sip_from == msg.src:
			rename = 'src'
			if agent.startswith('kamailio'):
				src_kind = 'PBX'
			elif agent.startswith('sofia'):
				src_kind = 'SIPCON'
			else:
				rename = False

		return Message.EntityIdent(proto='sip', src_kind=src_kind, dst_kind='SIP', rename=rename)

	@classmethod
	def identify_conns(cls, messages:list, my_idx:int):
		msg = messages[my_idx]

		msgtype = msg.get_trait('sip', 'msgtype')
		call_id = msg.get_trait('sip', 'call_id')
		sip_from = msg.get_trait('sip', 'sip_from')

		if msgtype in ('INVITE','INVITE-OK') and sip_from and sip_from == msg.src:
			conn = Conn.open('sip', msg.src, call_id, start_msg=msg)
			rtp_port = msg.get_trait('sip', 'rtp_port')
			if rtp_port is None:
				return

			rtp_conn = Conn.find('rtp', rtp_port, conn_id=rtp_port.key())
			if rtp_conn is None:
				return
			if not conn.subscriber_conn:
				conn.merge_subscr_conns(rtp_conn)
		else:
			conn = Conn.message('sip', msg.src, call_id, msg)

		if conn is None:
			return


class Layer_gsmtap_log(Layer):
	def __init__(s, msg:Message):
		app = msg.p.get('gsmtap_log.ident')
		level = msg.p.get('gsmtap_log.level')
		level_str = sane_showname(msg.p.get('gsmtap_log.level.showname'))
		logmsg = msg.p.get('gsmtap_log.string')
		cat = msg.p.get('gsmtap_log.subsys')

		if level_str != 'ERROR':
			return

		return

		msgtype = f'{app}.{level_str}'
		msg.log(Color.colored('red', logmsg))

		traits = Traits(
			msgtype=msgtype,
			level=level,
			app=app,
			cat=cat,
			logmsg=logmsg,
			)
		super().__init__(msg=msg, proto='log', msgtype=msgtype, traits=traits)

	def identify_entities(s, msg:Message, messages, my_idx):
		app = msg.get_trait('log', 'app')
		if app.startswith('Osmo'):
			app = app[4:]
		return Message.EntityIdent(proto='log', src_kind=app, dst_kind='LOG')


class MessageFilter:
	def __init__(s, layer=None, idx=None, values=[], negate=False):
		set_instance_vars_from_args()

	def matches(s, msg:Message):
		r = s._matches(msg)
		if s.negate:
			return not r
		return r

	def _matches(s, msg:Message):
		if s.layer and s.layer not in msg.layers:
			return False
		if s.idx and not (msg.p.idx == s.idx or any(a.p.idx == s.idx for a in msg.absorbed)):
			return False
		if s.values:
			layers = s.layer or msg.layers.keys()
			for k,v in s.values:
				for proto, name, result in msg.get_traits(layers, k):
					if v is None and name == k:
						return True
					if result is None or result == v:
						return True
			p_layers = None
			if s.layer:
				msg_layer = msg.layers.get(s.layer)
				if msg_layer:
					p_layers = [msg_layer.cap_p_name]
				else:
					p_layers = [s.layer]
			else:
				p_layers = [layer.cap_p_name for layer in msg.layers.values()]
			for k,v in s.values:
				for p_layer in p_layers:
					p_val = msg.p.get(p_layer + '.' + k)
					if p_val is None:
						continue
					if v is None:
						return True
					if v == str(p_val):
						return True
			return False
		return True

	@classmethod
	def debug(cls, flt_list, msg:Message):
		layers = set()
		for flt in flt_list:
			if flt.negate:
				continue
			if flt.layer is None:
				layers.update(msg.layers.keys())
			else:
				layers.add(flt.layer)
		for layer_name, layer in msg.layers.items():
			if layer_name not in layers:
				continue
			LOG(dir_p(msg.p, layer.cap_p_name))

	def __repr__(s):
		t = []
		if s.negate:
			t.append('NOT')
		if s.layer:
			t.append(f'layer {s.layer!r}')
		if s.idx:
			t.append(f'packet nr {s.idx}')
		if s.values:
			t_v = [(f'{k} == {v!r}' if v is not None else f'has {k}') for k,v in s.values]
			t.append('values: ' + (' or '.join(t_v)))
		return ', '.join(t)

	word_re = re.compile('^[a-zA-Z0-9_=-]*')
	@classmethod
	def parse(cls, spec_str):
		if spec_str is None:
			return []
		if not spec_str:
			return [MessageFilter()]
		filters = []
		token = None
		try:
			for token in spec_str.split(','):
				negate = False
				if token.startswith('!'):
					token = token[1:]
					negate = True
				layer = cls.word_re.match(token).group()
				flt = MessageFilter(layer=layer, negate=negate)
				rest = cls.word_re.split(token)[1]
				while rest:
					char = rest[0]
					rest = rest[1:]
					word = cls.word_re.match(rest).group()
					rest = cls.word_re.split(rest)[1]
					if char == '#':
						flt.idx = int(word)
					elif char == '.':
						if not flt.values:
							flt.values = []
						if '=' in word:
							name = word[:word.index('=')]
							val = word[word.index('=')+1:]
							flt.values.append((name, val))
						else:
							flt.values.append((word, None))
					else:
						raise Exception('Unknown token: %r' % (char + word))
				filters.append(flt)
			return filters
		except:
			out_error('Some mistake in message filter: %r in token %r' % (spec_str, token))
			raise

	@classmethod
	def match(cls, flt_list, msg):
		any_match = False
		for flt in flt_list:
			r = flt.matches(msg)
			if flt.negate and not r:
				return False
			any_match = any_match or r
		return any_match


	DOC = '''messagefilter examples, for --filter-msg and --debug:
	dtap
		All messages that contain a DTAP layer.
	dtap.msgtype=Identity-Request
		All DTAP msgtype=Identity-Request messages; for debug, show
		dtap layer.
	.msgtype=Identity-Request
		All msgtype=Identity-Request messages; for debug, show all
		layers. (Value names can be either parsed traits or raw packet
		names.)
	sccp.src_lref=0x00010000.dst_lref=0x00010000
		All messages with an SCCP layer and either src_lref or dst_lref
		== 0x00010000.
	.imsi
		All messages where any layer contains a value named 'imsi'.
	'#123'
		Message number 123 (don't forget to quote for the shell).
	'gsup,#614,sccp.src_lref=0x00010000'
		Show all GSUP messages, packet number 614 and all SCCP with
		given source local reference.
	'!rsl.msgtype=CCCH-LOAD-INDication'
		Don't show CCCH-LOAD-INDication message types (quote for the
		shell).
	'''

class UI:
	def __init__(s, opts, finalize_after_seconds=5):
		set_instance_vars_from_args()

		s.messages = []
		s.finalized_idx = -1

		s.show_traits = None
		if s.opts.show_traits:
			if s.opts.show_traits == 'all':
				s.show_traits = True
			else:
				s.show_traits = s.opts.show_traits.split(',')
		s.show_conns = None
		if s.opts.show_conns:
			if s.opts.show_conns == 'all':
				s.show_conns = True
			else:
				s.show_conns = s.opts.show_conns.split(',')
		s.filter_msg = MessageFilter.parse(s.opts.filter_msg)
		for flt in s.filter_msg:
			out_text_now('Filter-msg:', flt)
		s.debug = MessageFilter.parse(s.opts.debug)
		for dbg in s.debug:
			out_text_now('Debug:', dbg)

		s.filter_subscr = []
		if s.opts.filter_subscr:
			tokens = s.opts.filter_subscr.split(',')
			names = ('imsi', '0x', 'imei', 'msisdn', 'tmsi')
			for token in tokens:
				handled = False
				for name in names:
					if token.startswith(name):
						token_val = token[len(name):]
						if not token_val.isdigit():
							continue
						s.filter_subscr.append(name + token[len(name):])
						handled = True
						break
				if not handled:
					s.filter_subscr.append(token)
					s.filter_subscr.extend([name + token for name in names])

	def out_text_now(s, *args, **kwargs):
		'to be implemented by child class'
		assert False

	def out_error(s, *args, **kwargs):
		'to be implemented by child class'
		assert False

	def msg_finalized(s, msg, apply_filter=True):
		'to be implemented by child class'
		assert False

	def flush_msg(s, msg):
		msg.finalized = True
		s.msg_finalized(msg)

	def msg_filter(s, msg):
		'''Return True when the message passes active message filtering, False if it should be hidden'''
		if s.filter_msg and not MessageFilter.match(s.filter_msg, msg):
			return False
		if all(l.minor for l in msg.layers.values()):
			return False
		if msg.hide:
			return False
		if s.filter_subscr:
			match = False
			match_vals = set()
			for subscr in msg.related_subscribers():
				match_vals.update((f'imsi{subscr.imsi}', f'imei{subscr.imei}', f'msisdn{subscr.msisdn}'))
				match_vals.update(subscr.tmsis)
				match_vals.update(f'tmsi{tmsi[2:]}' for tmsi in subscr.tmsis)
				match_vals.update(f'tmsi{tmsi}' for tmsi in subscr.tmsis)

			if not any(token in match_vals for token in s.filter_subscr):
				return False
		return True

	def flush(s, timestamp_now=0, finalize_after_seconds=0):
		flush_t = timestamp_now - finalize_after_seconds
		for i in range(s.finalized_idx+1, len(s.messages)):
			msg = s.messages[i]
			if not msg:
				continue
			if timestamp_now and msg.timestamp > flush_t:
				break
			s.finalized_idx = i
			s.flush_msg(msg)

	def start(s):
		pass

	def stop(s):
		pass

	def add_msg(s, msg):
		global g_current_msg
		s.flush(msg.timestamp, s.finalize_after_seconds)
		try:
			if s.debug and any(dbg_filter.matches(msg) for dbg_filter in s.debug):
				msg.debug = True
			g_current_msg = msg
			if not msg.layers:
				return
			s.messages.append(msg)
			idx = len(s.messages) - 1
			changed_msg = msg.collapse(s.messages, idx)
			# if the received message was absorbed by another, continue to identify the modified message using the
			# new index
			if changed_msg is not None and changed_msg is not msg:
				msg = changed_msg
				idx = s.messages.index(msg)
			msg.identify_entities(s.messages, idx)
			msg.identify_conns(s.messages, idx)
			Subscriber.identify_subscriber(msg)

		except:
			s.flush()

			out_error('Exception')
			raise

	def process_messages(s, msg_src):
		for msg in msg_src.next():
			if 0 and msg.p.idx in (3676,):
				msg.log('rsl all_fields ', msg.p.cap_p.gsm_abis_rsl._all_fields)
				msg.log(msg.p.all_str('gsm_abis_rsl'))

			s.add_msg(msg)
		s.flush()
		msg_src.done()

class UI_Plain(UI):
	def out_text_now(s, *args, **kwargs):
		print(to_text(*args, **kwargs))

	def out_error(s, *args, **kwargs):
		s.out_text_now(Color.colored('red', '*** ERROR:'), *args, **kwargs)
		if g_current_msg:
			s.out_text_now(Color.colored('red', '*** ERROR: while processing msg'), g_current_msg.str(show_traits=True, show_conns=True))
		s.out_text_now(trace())

	def msg_print(s, msg, apply_filter=True):
		if apply_filter and not s.msg_filter(msg):
			return
		s.out_text_now(msg.str(ladder=True, one_column_per_kind=True, show_traits=s.show_traits, show_conns=s.show_conns))
		if s.debug and MessageFilter.match(s.debug, msg):
			MessageFilter.debug(s.debug, msg)

	def msg_finalized(s, msg):
		s.msg_print(msg, apply_filter=True)

class UI_Quiet(UI_Plain):
	def msg_finalized(s, msg):
		pass

class UI_Curses(UI):
	pass

class MsgSource:
	def __init__(s, opts):
		set_instance_vars_from_args()
		s.start_t = None
		s.p_min_t = None
		s.p_max_t = None
		s.end_t = None

	def _next_cap_p(s):
		assert False

	def next(s) -> Message:
		p_idx = 0
		s.start_t = time.time()
		warn_t = s.start_t
		warn_p_t = None
		for cap_p in s._next_cap_p():
			p_idx += 1
			if p_idx < s.opts.packet_start:
				continue
			if s.opts.packet_count and (p_idx - s.opts.packet_start) > s.opts.packet_count:
				break
			if s.opts.packet_end and p_idx > s.opts.packet_end:
				break
			msg = Message.parse(Packet(p_idx, cap_p))
			if msg is None or not msg.layers:
				continue
			s.p_min_t = msg.timestamp if s.p_min_t is None else min(s.p_min_t, msg.timestamp)
			s.p_max_t = msg.timestamp if s.p_max_t is None else max(s.p_max_t, msg.timestamp)

			now = time.time()
			if warn_p_t is None or now > warn_t + 3:
				if warn_p_t:
					packet_time = s.p_max_t - warn_p_t
					real_time = now - warn_t
					if real_time > (1.3 * packet_time):
						out_text_now(f'! taking longer to calculate than packets arrive by {100.*(real_time - packet_time)/packet_time:.1f}%')
				warn_t = now
				warn_p_t = s.p_max_t

			yield msg

	def done(s):
		if s.start_t is None or s.p_min_t is None:
			out_text_now('Nothing processed.')
		s.end_t = time.time()
		out_text_now(f'packet time: {s.p_max_t - s.p_min_t:.1f}s  in real time: {s.end_t - s.start_t:.1f}s')

class MsgSource_File(MsgSource):
	def __init__(s, path, opts):
		set_instance_vars_from_args()
		super().__init__(opts)

	def _next_cap_p(s):
		for cap_p in pyshark.FileCapture(s.path):
			yield cap_p

class MsgSource_Live(MsgSource):
	def __init__(s, iface, opts):
		set_instance_vars_from_args()
		super().__init__(opts)

	def _next_cap_p(s):
		for cap_p in pyshark.LiveCapture(s.iface).sniff_continuously():
			yield cap_p

class MsgSource_Pipe(MsgSource):
	def __init__(s, opts):
		super().__init__(opts)

	def _next_cap_p(s):
		from pyshark.capture.pipe_capture import PipeCapture
		for cap_p in PipeCapture(sys.stdin):
			yield cap_p

def run_tests():
	def out_test(*args, **kwargs):
		print(*args, **kwargs)

	d = dddict()
	d.sset(('a', 'b', 'c'), 'abc')
	d.sset(('a', 'b', 'd'), 'abd')
	out_test(d)
	assert d == {'a': {'b': {'c': 'abc', 'd': 'abd'}}}
	def verify_gget(keys, expect):
		val = d.gget(keys)
		out_test('gget:', keys,'=',val)
		assert val == expect
	verify_gget(('a', 'b', 'c'), 'abc')
	verify_gget(('a', 'b', 'd'), 'abd')
	verify_gget(('a', 'b', 'x'), None)
	verify_gget(('a', 'b'), {'c': 'abc', 'd': 'abd'})
	verify_gget(('a',), {'b': {'c': 'abc', 'd': 'abd'}})
	verify_gget(('x',), None)
	def verify_ppop(keys, expect):
		try:
			val = d.ppop(keys)
			assert expect is not None
		except KeyError:
			assert expect is None
			out_test('ppop:', keys,'=',None)
			return
		out_test('ppop:', keys,'=',val)
		assert val == expect
		assert d.gget(keys) is None
	verify_ppop(('a', 'b', 'c'), 'abc')
	verify_ppop(('a', 'b', 'd'), 'abd')
	verify_ppop(('a', 'b', 'x'), None)
	d.sset(('a', 'b', 'c'), 'abc')
	d.sset(('a', 'b', 'd'), 'abd')
	verify_ppop(('a', 'b'), {'c': 'abc', 'd': 'abd'})
	d.sset(('a', 'b', 'c'), 'abc')
	d.sset(('a', 'b', 'd'), 'abd')
	verify_ppop(('a',), {'b': {'c': 'abc', 'd': 'abd'}})
	verify_ppop(('x',), None)

SUBSCRIBERFILTER_DOC = '''subscriberfilter examples, for --filter-subscr:
	123
		Show subscriber where any value matches 123 (probably only MSISDN will
		match, because '123' is too short for IMSI etc).
	imsi123456789012345
	imei123456789012345
	msisdn123456789012345
	tmsi1234abcd
		Show subscriber with the given IMSI/IMEI/MSISDN/TMSI.
	imsi123456,imsi987654
		Show both these IMSIs.
	imsi123456,msisdn123,1234abcd,imei987654
		Show all of these subscribers: IMSI 123456, MSISDN 123, TMSI 0x1234abcd
		and IMEI 987654.
'''
def parse_args():
	import argparse
	parser = argparse.ArgumentParser(description=__doc__
		+ '\n' + SUBSCRIBERFILTER_DOC
		+'\n' + MessageFilter.DOC,
			formatter_class=argparse.RawDescriptionHelpFormatter)
	parser.add_argument('--pcap-file', '-f', metavar='file')
	parser.add_argument('--live-capture', '-l', metavar='interface')
	parser.add_argument('--stdin-capture', '-i', action='store_true')
	parser.add_argument('--packet-start', '-S', default=0, type=int)
	parser.add_argument('--packet-count', '-C', default=0, type=int)
	parser.add_argument('--packet-end', '-E', default=0, type=int)
	parser.add_argument('--ui', '-u', metavar='USER-INTERFACE',
		help='How to display messages: plain / p = print to stdout as plain-text;'
		' curses / c = interactive curses interface; none / n = quiet',
		default='plain')
	parser.add_argument('--filter-subscr', default=None,
		help='Show only messages related to the given subscriberfilter')
	parser.add_argument('--filter-msg', default=None, metavar='messagefilter',
		help='Show only messages matching this messagefilter')
	parser.add_argument('--show-traits', default=None)
	parser.add_argument('--show-conns', default=None, help="'all' for all, or specific conn names (comma separated)")
	parser.add_argument('--collapse-stp', '-s', default=None, help="BSSAP via STP may appear duplicated. '-s1' collapses the duplicates, -s0 does not.")
	parser.add_argument('--test', action='store_true')
	parser.add_argument('--debug', metavar='messagefilter',
		help='Show a lot more info on messages matching this messagefilter')
	return parser.parse_args()

def main():
	opts = parse_args()

	if opts.test:
		run_tests()
	else:
		if (opts.collapse_stp or '').upper() in ['1', 'Y', 'YES', 'TRUE']:
			SCCP_COLLAPSE_STP = True
		if (opts.collapse_stp or '').upper() in ['0', 'N', 'NO', 'FALSE']:
			SCCP_COLLAPSE_STP = False
		ui_class = None
		ui_type = opts.ui or 'none'
		if 'plain'.startswith(ui_type):
			ui_class = UI_Plain
		elif 'curses'.startswith(ui_type):
			ui_class = UI_Curses
		elif 'none'.startswith(ui_type):
			ui_class = UI_Quiet
		else:
			ERR('Unknown UI type:', repr(ui_type))
			return 1
		g_ui = ui_class(opts)
		msg_source = None
		if opts.pcap_file:
			msg_source = MsgSource_File(opts.pcap_file, opts)
		elif opts.live_capture:
			msg_source = MsgSource_Live(opts.live_capture, opts)
		elif opts.stdin_capture:
			msg_source = MsgSource_Pipe(opts)
		else:
			ERR('No message source, try `-l any` or `-f my.pcap`')
			return 1
		g_ui.process_messages(msg_source)
		g_ui.flush()
		if Conn.open_conns:
			print('still open conns:', repr(Conn.open_conns))
	return 0

if __name__ == '__main__':
	if False:
		import cProfile
		cProfile.run('main()', sort='tottime')
	else:
		exit(main())
# vim: noexpandtab tabstop=8 shiftwidth=8