aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-sccp.c
blob: ee90694299172e09e1de4cb46341399f6318d949 (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
/* packet-sccp.c
 * Routines for Signalling Connection Control Part (SCCP) dissection
 *
 * It is hopefully compliant to:
 *   ANSI T1.112.3-2001
 *   ITU-T Q.713 7/1996
 *   YDN 038-1997 (Chinese ITU variant)
 *   JT-Q713 and NTT-Q713 (Japan)
 *
 *   Note that Japan-specific GTT is incomplete; in particular, the specific
 *   TTs that are defined in TTC and NTT are not decoded in detail.
 *
 * Copyright 2002, Jeff Morriss <jeff.morriss[AT]ulticom.com>
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-m2pa.c
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#include <glib.h>

#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/emem.h>
#include <epan/reassemble.h>
#include <epan/asn1.h>
#include <epan/uat.h>
#include <epan/strutil.h>
#include <epan/expert.h>
#include "packet-mtp3.h"
#include "packet-tcap.h"
#include "packet-sccp.h"
#include "packet-e164.h"
#include "packet-e212.h"
#include "packet-frame.h"
#include <epan/tap.h>
#include <address.h>

/* function prototypes */
void proto_reg_handoff_sccp(void);

static Standard_Type decode_mtp3_standard;

#define SCCP_SI 3

#define SCCP_MSG_TYPE_OFFSET 0
#define SCCP_MSG_TYPE_LENGTH 1
#define POINTER_LENGTH      1
#define POINTER_LENGTH_LONG 2

#define INVALID_LR 0xffffff /* a reserved value */

/* Same as below but with names typed out */
static const value_string sccp_message_type_values[] = {
  { SCCP_MSG_TYPE_CR,     "Connection Request" },
  { SCCP_MSG_TYPE_CC,     "Connection Confirm" },
  { SCCP_MSG_TYPE_CREF,   "Connection Refused" },
  { SCCP_MSG_TYPE_RLSD,   "Released" },
  { SCCP_MSG_TYPE_RLC,    "Release Complete" },
  { SCCP_MSG_TYPE_DT1,    "Data Form 1" },
  { SCCP_MSG_TYPE_DT2,    "Data Form 2" },
  { SCCP_MSG_TYPE_AK,     "Data Acknowledgement" },
  { SCCP_MSG_TYPE_UDT,    "Unitdata" },
  { SCCP_MSG_TYPE_UDTS,   "Unitdata Service" },
  { SCCP_MSG_TYPE_ED,     "Expedited Data" },
  { SCCP_MSG_TYPE_EA,     "Expedited Data Acknowledgement" },
  { SCCP_MSG_TYPE_RSR,    "Reset Request" },
  { SCCP_MSG_TYPE_RSC,    "Reset Confirmation" },
  { SCCP_MSG_TYPE_ERR,    "Error" },
  { SCCP_MSG_TYPE_IT,     "Inactivity Timer" },
  { SCCP_MSG_TYPE_XUDT,   "Extended Unitdata" },
  { SCCP_MSG_TYPE_XUDTS,  "Extended Unitdata Service" },
  { SCCP_MSG_TYPE_LUDT,   "Long Unitdata" },
  { SCCP_MSG_TYPE_LUDTS,  "Long Unitdata Service" },
  { 0,			 NULL } };

/* Same as above but in acronym form (for the Info column) */
const value_string sccp_message_type_acro_values[] = {
  { SCCP_MSG_TYPE_CR,     "CR" },
  { SCCP_MSG_TYPE_CC,     "CC" },
  { SCCP_MSG_TYPE_CREF,   "CREF" },
  { SCCP_MSG_TYPE_RLSD,   "RLSD" },
  { SCCP_MSG_TYPE_RLC,    "RLC" },
  { SCCP_MSG_TYPE_DT1,    "DT1" },
  { SCCP_MSG_TYPE_DT2,    "DT2" },
  { SCCP_MSG_TYPE_AK,     "AK" },
  { SCCP_MSG_TYPE_UDT,    "UDT" },
  { SCCP_MSG_TYPE_UDTS,   "UDTS" },
  { SCCP_MSG_TYPE_ED,     "ED" },
  { SCCP_MSG_TYPE_EA,     "EA" },
  { SCCP_MSG_TYPE_RSR,    "RSR" },
  { SCCP_MSG_TYPE_RSC,    "RSC" },
  { SCCP_MSG_TYPE_ERR,    "ERR" },
  { SCCP_MSG_TYPE_IT,     "IT" },
  { SCCP_MSG_TYPE_XUDT,   "XUDT" },
  { SCCP_MSG_TYPE_XUDTS,  "XUDTS" },
  { SCCP_MSG_TYPE_LUDT,   "LUDT" },
  { SCCP_MSG_TYPE_LUDTS,  "LUDTS" },
  { 0,			 NULL } };

#define PARAMETER_LENGTH_LENGTH 1
#define PARAMETER_LONG_DATA_LENGTH_LENGTH 2
#define PARAMETER_TYPE_LENGTH 1

#define PARAMETER_END_OF_OPTIONAL_PARAMETERS	0x00
#define PARAMETER_DESTINATION_LOCAL_REFERENCE	0x01
#define PARAMETER_SOURCE_LOCAL_REFERENCE	0x02
#define PARAMETER_CALLED_PARTY_ADDRESS		0x03
#define PARAMETER_CALLING_PARTY_ADDRESS		0x04
#define PARAMETER_CLASS				0x05
#define PARAMETER_SEGMENTING_REASSEMBLING	0x06
#define PARAMETER_RECEIVE_SEQUENCE_NUMBER	0x07
#define PARAMETER_SEQUENCING_SEGMENTING		0x08
#define PARAMETER_CREDIT			0x09
#define PARAMETER_RELEASE_CAUSE			0x0a
#define PARAMETER_RETURN_CAUSE			0x0b
#define PARAMETER_RESET_CAUSE			0x0c
#define PARAMETER_ERROR_CAUSE			0x0d
#define PARAMETER_REFUSAL_CAUSE			0x0e
#define PARAMETER_DATA				0x0f
#define PARAMETER_SEGMENTATION			0x10
#define PARAMETER_HOP_COUNTER			0x11
/* Importance is ITU only */
#define PARAMETER_IMPORTANCE			0x12
#define PARAMETER_LONG_DATA			0x13
/* ISNI is ANSI only */
#define PARAMETER_ISNI				0xfa

static const value_string sccp_parameter_values[] = {
  { PARAMETER_END_OF_OPTIONAL_PARAMETERS,	"End of Optional Parameters" },
  { PARAMETER_DESTINATION_LOCAL_REFERENCE,	"Destination Local Reference" },
  { PARAMETER_SOURCE_LOCAL_REFERENCE,		"Source Local Reference" },
  { PARAMETER_CALLED_PARTY_ADDRESS,		"Called Party Address" },
  { PARAMETER_CALLING_PARTY_ADDRESS,		"Calling Party Address" },
  { PARAMETER_CLASS,				"Protocol Class" },
  { PARAMETER_SEGMENTING_REASSEMBLING,		"Segmenting/Reassembling" },
  { PARAMETER_RECEIVE_SEQUENCE_NUMBER,		"Receive Sequence Number" },
  { PARAMETER_SEQUENCING_SEGMENTING,		"Sequencing/Segmenting" },
  { PARAMETER_CREDIT,				"Credit" },
  { PARAMETER_RELEASE_CAUSE,			"Release Cause" },
  { PARAMETER_RETURN_CAUSE,			"Return Cause" },
  { PARAMETER_RESET_CAUSE,			"Reset Cause" },
  { PARAMETER_ERROR_CAUSE,			"Error Cause" },
  { PARAMETER_REFUSAL_CAUSE,			"Refusal Cause" },
  { PARAMETER_DATA,				"Data" },
  { PARAMETER_SEGMENTATION,			"Segmentation" },
  { PARAMETER_HOP_COUNTER,			"Hop Counter" },
  { PARAMETER_IMPORTANCE,			"Importance (ITU)" },
  { PARAMETER_LONG_DATA,			"Long Data" },
  { PARAMETER_ISNI,				"Intermediate Signaling Network Identification (ANSI)" },
  { 0,						 NULL } };


#define END_OF_OPTIONAL_PARAMETERS_LENGTH	1
#define DESTINATION_LOCAL_REFERENCE_LENGTH	3
#define SOURCE_LOCAL_REFERENCE_LENGTH		3
#define PROTOCOL_CLASS_LENGTH				1
#define RECEIVE_SEQUENCE_NUMBER_LENGTH		1
#define CREDIT_LENGTH						1
#define RELEASE_CAUSE_LENGTH				1
#define RETURN_CAUSE_LENGTH					1
#define RESET_CAUSE_LENGTH					1
#define ERROR_CAUSE_LENGTH					1
#define REFUSAL_CAUSE_LENGTH				1
#define HOP_COUNTER_LENGTH					1
#define IMPORTANCE_LENGTH					1


/* Parts of the Called and Calling Address parameters */
/* Address Indicator */
#define ADDRESS_INDICATOR_LENGTH	1
#define ITU_RESERVED_MASK			0x80
#define ANSI_NATIONAL_MASK			0x80
#define ROUTING_INDICATOR_MASK		0x40
#define GTI_MASK					0x3C
#define GTI_SHIFT					2
#define ITU_SSN_INDICATOR_MASK		0x02
#define ITU_PC_INDICATOR_MASK		0x01
#define ANSI_PC_INDICATOR_MASK		0x02
#define ANSI_SSN_INDICATOR_MASK		0x01

static const value_string sccp_national_indicator_values[] = {
  { 0x0,  "Address coded to International standard" },
  { 0x1,  "Address coded to National standard" },
  { 0,    NULL } };

#define ROUTE_ON_GT  0x0
#define ROUTE_ON_SSN 0x1
static const value_string sccp_routing_indicator_values[] = {
  { ROUTE_ON_GT,  "Route on GT" },
  { ROUTE_ON_SSN, "Route on SSN" },
  { 0,		  NULL } };

#define AI_GTI_NO_GT			0x0
#define ITU_AI_GTI_NAI			0x1
#define AI_GTI_TT				0x2
#define ITU_AI_GTI_TT_NP_ES		0x3
#define ITU_AI_GTI_TT_NP_ES_NAI	0x4
static const value_string sccp_itu_global_title_indicator_values[] = {
  { AI_GTI_NO_GT,			"No Global Title" },
  { ITU_AI_GTI_NAI,			"Nature of Address Indicator only" },
  { AI_GTI_TT,				"Translation Type only" },
  { ITU_AI_GTI_TT_NP_ES,	"Translation Type, Numbering Plan, and Encoding Scheme included" },
  { ITU_AI_GTI_TT_NP_ES_NAI,	"Translation Type, Numbering Plan, Encoding Scheme, and Nature of Address Indicator included" },
  { 0,				NULL } };

/* #define AI_GTI_NO_GT		0x0 */
#define ANSI_AI_GTI_TT_NP_ES	0x1
/* #define AI_GTI_TT		0x2 */
static const value_string sccp_ansi_global_title_indicator_values[] = {
  { AI_GTI_NO_GT,			"No Global Title" },
  { ANSI_AI_GTI_TT_NP_ES,	"Translation Type, Numbering Plan, and Encoding Scheme included" },
  { AI_GTI_TT,				"Translation Type only" },
  { 0,				NULL } };

static const value_string sccp_ai_pci_values[] = {
  { 0x1,  "Point Code present" },
  { 0x0,  "Point Code not present" },
  { 0,    NULL } };

static const value_string sccp_ai_ssni_values[] = {
  { 0x1,  "SSN present" },
  { 0x0,  "SSN not present" },
  { 0,    NULL } };

#define ADDRESS_SSN_LENGTH    1
#define INVALID_SSN 0xff
  /* Some values from 3GPP TS 23.003 */
  /*  Japan TTC and NTT define a lot of SSNs, some of which conflict with
   *  these.  They are not added for now.
   */
static const value_string sccp_ssn_values[] = {
  { 0x00,  "SSN not known/not used" },
  { 0x01,  "SCCP management" },
  { 0x02,  "Reserved for ITU-T allocation" },
  { 0x03,  "ISDN User Part" },
  { 0x04,  "OMAP (Operation, Maintenance, and Administration Part)" },
  { 0x05,  "MAP (Mobile Application Part)" },
  { 0x06,  "HLR (Home Location Register)" },
  { 0x07,  "VLR (Visitor Location Register)" },
  { 0x08,  "MSC (Mobile Switching Center)" },
  { 0x09,  "EIC/EIR (Equipment Identifier Center/Equipment Identification Register)" },
  { 0x0a,  "AUC/AC (Authentication Center)" },
  { 0x0b,  "ISDN supplementary services (ITU only)" },
  { 0x0c,  "Reserved for international use (ITU only)" },
  { 0x0d,  "Broadband ISDN edge-to-edge applications (ITU only)" },
  { 0x0e,  "TC test responder (ITU only)" },
  /* The following national network subsystem numbers have been allocated for use within and
   * between GSM/UMTS networks:
   */
  { 0x8e,  "RANAP" },
  { 0x8f,  "RNSAP" },
  { 0x91,  "GMLC(MAP)" },
  { 0x92,  "CAP" },
  { 0x93,  "gsmSCF (MAP) or IM-SSF (MAP) or Presence Network Agent" },
  { 0x94,  "SIWF (MAP)" },
  { 0x95,  "SGSN (MAP)" },
  { 0x96,  "GGSN (MAP)" },
  /* The following national network subsystem numbers have been allocated for use within GSM/UMTS networks:*/
  { 0xf9,  "PCAP" },
  { 0xfa,  "BSC (BSSAP-LE)" },
  { 0xfb,  "MSC (BSSAP-LE)" },
  { 0xfc,  "IOS or SMLC (BSSAP-LE)" },
  { 0xfd,  "BSS O&M (A interface)" },
  { 0xfe,  "BSSAP/BSAP" },
  { 0,     NULL } };


/* * * * * * * * * * * * * * * * *
 * Global Title: ITU GTI == 0001 *
 * * * * * * * * * * * * * * * * */
#define GT_NAI_MASK 0x7F
#define GT_NAI_LENGTH 1
#define GT_NAI_UNKNOWN			0x00
#define GT_NAI_SUBSCRIBER_NUMBER	0x01
#define GT_NAI_RESERVED_NATIONAL	0x02
#define GT_NAI_NATIONAL_SIG_NUM		0x03
#define GT_NAI_INTERNATIONAL_NUM	0x04
static const value_string sccp_nai_values[] = {
  { GT_NAI_UNKNOWN,		"NAI unknown" },
  { GT_NAI_SUBSCRIBER_NUMBER,	"Subscriber Number" },
  { GT_NAI_RESERVED_NATIONAL,	"Reserved for national use" },
  { GT_NAI_NATIONAL_SIG_NUM,	"National significant number" },
  { GT_NAI_INTERNATIONAL_NUM,	"International number" },
  { 0,				NULL } };


#define GT_OE_MASK 0x80
#define GT_OE_EVEN 0
#define GT_OE_ODD  1
static const value_string sccp_oe_values[] = {
  { GT_OE_EVEN,	"Even number of address signals" },
  { GT_OE_ODD,	"Odd number of address signals" },
  { 0,		NULL } };

const value_string sccp_address_signal_values[] = {
  { 0,  "0" },
  { 1,  "1" },
  { 2,  "2" },
  { 3,  "3" },
  { 4,  "4" },
  { 5,  "5" },
  { 6,  "6" },
  { 7,  "7" },
  { 8,  "8" },
  { 9,  "9" },
  { 10, "(spare)" },
  { 11, "11" },
  { 12, "12" },
  { 13, "(spare)" },
  { 14, "(spare)" },
  { 15, "ST" },
  { 0,  NULL } };


/* * * * * * * * * * * * * * * * * * * * *
 * Global Title: ITU and ANSI GTI == 0010 *
 * * * * * * * * * * * * * * * * * * * * */
#define GT_TT_LENGTH 1


/* * * * * * * * * * * * * * * * * * * * * * * * * *
 * Global Title: ITU GTI == 0011, ANSI GTI == 0001 *
 * * * * * * * * * * * * * * * * * * * * * * * * * */
#define GT_NP_MASK 0xf0
#define GT_NP_SHIFT 4
#define GT_NP_ES_LENGTH 1
#define GT_NP_UNKNOWN		0x00
#define GT_NP_ISDN		0x01
#define GT_NP_GENERIC_RESERVED	0x02
#define GT_NP_DATA				0x03
#define GT_NP_TELEX				0x04
#define GT_NP_MARITIME_MOBILE	0x05
#define GT_NP_LAND_MOBILE		0x06
#define GT_NP_ISDN_MOBILE		0x07
#define GT_NP_PRIVATE_NETWORK	0x0e
#define GT_NP_RESERVED			0x0f
static const value_string sccp_np_values[] = {
  { GT_NP_UNKNOWN,			"Unknown" },
  { GT_NP_ISDN,				"ISDN/telephony" },
  { GT_NP_GENERIC_RESERVED,	"Generic (ITU)/Reserved (ANSI)" },
  { GT_NP_DATA,				"Data" },
  { GT_NP_TELEX,			"Telex" },
  { GT_NP_MARITIME_MOBILE,	"Maritime mobile" },
  { GT_NP_LAND_MOBILE,		"Land mobile" },
  { GT_NP_ISDN_MOBILE,		"ISDN/mobile" },
  { GT_NP_PRIVATE_NETWORK,	"Private network or network-specific" },
  { GT_NP_RESERVED,			"Reserved" },
  { 0,				NULL } };

#define GT_ES_MASK     0x0f
#define GT_ES_UNKNOWN  0x0
#define GT_ES_BCD_ODD  0x1
#define GT_ES_BCD_EVEN 0x2
#define GT_ES_NATIONAL 0x3
#define GT_ES_RESERVED 0xf
static const value_string sccp_es_values[] = {
  { GT_ES_UNKNOWN,	"Unknown" },
  { GT_ES_BCD_ODD,	"BCD, odd number of digits" },
  { GT_ES_BCD_EVEN,	"BCD, even number of digits" },
  { GT_ES_NATIONAL,	"National specific" },
  { GT_ES_RESERVED,	"Reserved (ITU)/Spare (ANSI)" },
  { 0,			NULL } };

/* Address signals above */


/* * * * * * * * * * * * * * * * *
 * Global Title: ITU GTI == 0100 *
 * * * * * * * * * * * * * * * * */
/* NP above */
/* ES above */
/* NAI above */
/* Address signals above */


#define CLASS_CLASS_MASK 0xf
#define CLASS_SPARE_HANDLING_MASK 0xf0
static const value_string sccp_class_handling_values [] = {
  { 0x0,  "No special options" },
  { 0x8,  "Return message on error" },
  { 0,    NULL } };


#define SEGMENTING_REASSEMBLING_LENGTH 1
#define SEGMENTING_REASSEMBLING_MASK   0x01
#define NO_MORE_DATA 0
#define MORE_DATA    1
/* This is also used by sequencing-segmenting parameter */
static const value_string sccp_segmenting_reassembling_values [] = {
  { NO_MORE_DATA,	"No more data" },
  { MORE_DATA,		"More data" },
  { 0,			NULL } };


#define RECEIVE_SEQUENCE_NUMBER_LENGTH	1
#define RSN_MASK			0xfe

#define SEQUENCING_SEGMENTING_LENGTH		2
#define SEQUENCING_SEGMENTING_SSN_LENGTH	1
#define SEQUENCING_SEGMENTING_RSN_LENGTH	1
#define SEND_SEQUENCE_NUMBER_MASK			0xfe
#define RECEIVE_SEQUENCE_NUMBER_MASK		0xfe
#define SEQUENCING_SEGMENTING_MORE_MASK		0x01


#define CREDIT_LENGTH 1

#define RELEASE_CAUSE_LENGTH 1
const value_string sccp_release_cause_values [] = {
  { 0x00,  "End user originated" },
  { 0x01,  "End user congestion" },
  { 0x02,  "End user failure" },
  { 0x03,  "SCCP user originated" },
  { 0x04,  "Remote procedure error" },
  { 0x05,  "Inconsistent connection data" },
  { 0x06,  "Access failure" },
  { 0x07,  "Access congestion" },
  { 0x08,  "Subsystem failure" },
  { 0x09,  "Subsystem congestion" },
  { 0x0a,  "MTP failure" },
  { 0x0b,  "Network congestion" },
  { 0x0c,  "Expiration of reset timer" },
  { 0x0d,  "Expiration of receive inactivity timer" },
  { 0x0e,  "Reserved" },
  { 0x0f,  "Unqualified" },
  { 0x10,  "SCCP failure (ITU only)" },
  { 0,     NULL } };


#define RETURN_CAUSE_LENGTH 1
const value_string sccp_return_cause_values [] = {
  { 0x00,  "No translation for an address of such nature" },
  { 0x01,  "No translation for this specific address" },
  { 0x02,  "Subsystem congestion" },
  { 0x03,  "Subsystem failure" },
  { 0x04,  "Unequipped failure" },
  { 0x05,  "MTP failure" },
  { 0x06,  "Network congestion" },
  { 0x07,  "Unqualified" },
  { 0x08,  "Error in message transport" },
  { 0x09,  "Error in local processing" },
  { 0x0a,  "Destination cannot perform reassembly" },
  { 0x0b,  "SCCP failure" },
  { 0x0c,  "Hop counter violation" },
  { 0x0d,  "Segmentation not supported" },
  { 0x0e,  "Segmentation failure" },
  { 0xf7,  "Message change failure (ANSI only)" },
  { 0xf8,  "Invalid INS routing request (ANSI only)" },
  { 0xf9,  "Invalid ISNI routing request (ANSI only)"},
  { 0xfa,  "Unauthorized message (ANSI only)" },
  { 0xfb,  "Message incompatibility (ANSI only)" },
  { 0xfc,  "Cannot perform ISNI constrained routing (ANSI only)" },
  { 0xfd,  "Redundant ISNI constrained routing (ANSI only)" },
  { 0xfe,  "Unable to perform ISNI identification (ANSI only)" },
  { 0,     NULL } };


#define RESET_CAUSE_LENGTH 1
const value_string sccp_reset_cause_values [] = {
  { 0x00,  "End user originated" },
  { 0x01,  "SCCP user originated" },
  { 0x02,  "Message out of order - incorrect send sequence number" },
  { 0x03,  "Message out of order - incorrect receive sequence number" },
  { 0x04,  "Remote procedure error - message out of window" },
  { 0x05,  "Remote procedure error - incorrect send sequence number after (re)initialization" },
  { 0x06,  "Remote procedure error - general" },
  { 0x07,  "Remote end user operational" },
  { 0x08,  "Network operational" },
  { 0x09,  "Access operational" },
  { 0x0a,  "Network congestion" },
  { 0x0b,  "Reserved (ITU)/Not obtainable (ANSI)" },
  { 0x0c,  "Unqualified" },
  { 0,     NULL } };


#define ERROR_CAUSE_LENGTH 1
const value_string sccp_error_cause_values [] = {
  { 0x00,  "Local Reference Number (LRN) mismatch - unassigned destination LRN" },
  { 0x01,  "Local Reference Number (LRN) mismatch - inconsistent source LRN" },
  { 0x02,  "Point code mismatch" },
  { 0x03,  "Service class mismatch" },
  { 0x04,  "Unqualified" },
  { 0,     NULL } };


#define REFUSAL_CAUSE_LENGTH 1
const value_string sccp_refusal_cause_values [] = {
  { 0x00,  "End user originated" },
  { 0x01,  "End user congestion" },
  { 0x02,  "End user failure" },
  { 0x03,  "SCCP user originated" },
  { 0x04,  "Destination address unknown" },
  { 0x05,  "Destination inaccessible" },
  { 0x06,  "Network resource - QOS not available/non-transient" },
  { 0x07,  "Network resource - QOS not available/transient" },
  { 0x08,  "Access failure" },
  { 0x09,  "Access congestion" },
  { 0x0a,  "Subsystem failure" },
  { 0x0b,  "Subsystem congestion" },
  { 0x0c,  "Expiration of connection establishment timer" },
  { 0x0d,  "Incompatible user data" },
  { 0x0e,  "Reserved" },
  { 0x0f,  "Unqualified" },
  { 0x10,  "Hop counter violation" },
  { 0x11,  "SCCP failure (ITU only)" },
  { 0x12,  "No translation for an address of such nature" },
  { 0x13,  "Unequipped user" },
  { 0,     NULL } };


#define SEGMENTATION_LENGTH		4
#define SEGMENTATION_FIRST_SEGMENT_MASK	0x80
#define SEGMENTATION_CLASS_MASK		0x40
#define SEGMENTATION_SPARE_MASK		0x30
#define SEGMENTATION_REMAINING_MASK	0x0f
static const value_string sccp_segmentation_first_segment_values [] = {
  { 1,  "First segment" },
  { 0,  "Not first segment" },
  { 0,  NULL } };
static const value_string sccp_segmentation_class_values [] = {
  { 0,  "Class 0 selected" },
  { 1,  "Class 1 selected" },
  { 0,  NULL } };


#define HOP_COUNTER_LENGTH 1

#define IMPORTANCE_LENGTH		1
#define IMPORTANCE_IMPORTANCE_MASK	0x7


#define ANSI_ISNI_ROUTING_CONTROL_LENGTH 1
#define ANSI_ISNI_MI_MASK		 0x01
#define ANSI_ISNI_IRI_MASK		 0x06
#define ANSI_ISNI_RES_MASK		 0x08
#define ANSI_ISNI_TI_MASK		 0x10
#define ANSI_ISNI_TI_SHIFT		 4
#define ANSI_ISNI_COUNTER_MASK		 0xe0
#define ANSI_ISNI_NETSPEC_MASK		 0x03

static const value_string sccp_isni_mark_for_id_values [] = {
  { 0x0,  "Do not identify networks" },
  { 0x1,  "Identify networks" },
  { 0,    NULL } };

static const value_string sccp_isni_iri_values [] = {
  { 0x0,  "Neither constrained nor suggested ISNI routing" },
  { 0x1,  "Constrained ISNI routing" },
  { 0x2,  "Reserved for suggested ISNI routing" },
  { 0x3,  "Spare" },
  { 0,    NULL } };

#define ANSI_ISNI_TYPE_0 0x0
#define ANSI_ISNI_TYPE_1 0x1
static const value_string sccp_isni_ti_values [] = {
  { ANSI_ISNI_TYPE_0,	"Type zero ISNI parameter format" },
  { ANSI_ISNI_TYPE_1,	"Type one ISNI parameter format" },
  { 0,			NULL } };


/* Initialize the protocol and registered fields */
static int proto_sccp = -1;
static int hf_sccp_message_type = -1;
static int hf_sccp_variable_pointer1 = -1;
static int hf_sccp_variable_pointer2 = -1;
static int hf_sccp_variable_pointer3 = -1;
static int hf_sccp_optional_pointer = -1;
static int hf_sccp_ssn = -1;
static int hf_sccp_gt_digits = -1;

/* Called Party address */
static int hf_sccp_called_national_indicator = -1;
static int hf_sccp_called_routing_indicator = -1;
static int hf_sccp_called_itu_global_title_indicator = -1;
static int hf_sccp_called_ansi_global_title_indicator = -1;
static int hf_sccp_called_itu_ssn_indicator = -1;
static int hf_sccp_called_itu_point_code_indicator = -1;
static int hf_sccp_called_ansi_ssn_indicator = -1;
static int hf_sccp_called_ansi_point_code_indicator = -1;
static int hf_sccp_called_ssn = -1;
static int hf_sccp_called_pc_member = -1;
static int hf_sccp_called_pc_cluster = -1;
static int hf_sccp_called_pc_network = -1;
static int hf_sccp_called_ansi_pc = -1;
static int hf_sccp_called_chinese_pc = -1;
static int hf_sccp_called_itu_pc = -1;
static int hf_sccp_called_japan_pc = -1;
static int hf_sccp_called_gt_nai = -1;
static int hf_sccp_called_gt_oe = -1;
static int hf_sccp_called_gt_tt = -1;
static int hf_sccp_called_gt_np = -1;
static int hf_sccp_called_gt_es = -1;
static int hf_sccp_called_gt_digits = -1;

/* Calling party address */
static int hf_sccp_calling_national_indicator = -1;
static int hf_sccp_calling_routing_indicator = -1;
static int hf_sccp_calling_itu_global_title_indicator = -1;
static int hf_sccp_calling_ansi_global_title_indicator = -1;
static int hf_sccp_calling_itu_ssn_indicator = -1;
static int hf_sccp_calling_itu_point_code_indicator = -1;
static int hf_sccp_calling_ansi_ssn_indicator = -1;
static int hf_sccp_calling_ansi_point_code_indicator = -1;
static int hf_sccp_calling_ssn = -1;
static int hf_sccp_calling_pc_member = -1;
static int hf_sccp_calling_pc_cluster = -1;
static int hf_sccp_calling_pc_network = -1;
static int hf_sccp_calling_ansi_pc = -1;
static int hf_sccp_calling_chinese_pc = -1;
static int hf_sccp_calling_itu_pc = -1;
static int hf_sccp_calling_japan_pc = -1;
static int hf_sccp_calling_gt_nai = -1;
static int hf_sccp_calling_gt_oe = -1;
static int hf_sccp_calling_gt_tt = -1;
static int hf_sccp_calling_gt_np = -1;
static int hf_sccp_calling_gt_es = -1;
static int hf_sccp_calling_gt_digits = -1;

/* Other parameter values */
static int hf_sccp_dlr = -1;
static int hf_sccp_slr = -1;
static int hf_sccp_lr = -1;
static int hf_sccp_class = -1;
static int hf_sccp_handling = -1;
static int hf_sccp_more = -1;
static int hf_sccp_rsn = -1;
static int hf_sccp_sequencing_segmenting_ssn = -1;
static int hf_sccp_sequencing_segmenting_rsn = -1;
static int hf_sccp_sequencing_segmenting_more = -1;
static int hf_sccp_credit = -1;
static int hf_sccp_release_cause = -1;
static int hf_sccp_return_cause = -1;
static int hf_sccp_reset_cause = -1;
static int hf_sccp_error_cause = -1;
static int hf_sccp_refusal_cause = -1;
static int hf_sccp_segmentation_first = -1;
static int hf_sccp_segmentation_class = -1;
static int hf_sccp_segmentation_remaining = -1;
static int hf_sccp_segmentation_slr = -1;
static int hf_sccp_hop_counter = -1;
static int hf_sccp_importance = -1;
static int hf_sccp_ansi_isni_mi = -1;
static int hf_sccp_ansi_isni_iri = -1;
static int hf_sccp_ansi_isni_ti = -1;
static int hf_sccp_ansi_isni_netspec = -1;
static int hf_sccp_ansi_isni_counter = -1;
static int hf_sccp_xudt_msg_fragments = -1;
static int hf_sccp_xudt_msg_fragment = -1;
static int hf_sccp_xudt_msg_fragment_overlap = -1;
static int hf_sccp_xudt_msg_fragment_overlap_conflicts = -1;
static int hf_sccp_xudt_msg_fragment_multiple_tails = -1;
static int hf_sccp_xudt_msg_fragment_too_long_fragment = -1;
static int hf_sccp_xudt_msg_fragment_error = -1;
static int hf_sccp_xudt_msg_reassembled_in = -1;
static int hf_sccp_xudt_msg_reassembled_length = -1;
static int hf_sccp_assoc_msg = -1;
static int hf_sccp_assoc_id = -1;

/* Initialize the subtree pointers */
static gint ett_sccp = -1;
static gint ett_sccp_called = -1;
static gint ett_sccp_called_ai = -1;
static gint ett_sccp_called_pc = -1;
static gint ett_sccp_called_gt = -1;
static gint ett_sccp_calling = -1;
static gint ett_sccp_calling_ai = -1;
static gint ett_sccp_calling_pc = -1;
static gint ett_sccp_calling_gt = -1;
static gint ett_sccp_sequencing_segmenting = -1;
static gint ett_sccp_segmentation = -1;
static gint ett_sccp_ansi_isni_routing_control = -1;
static gint ett_sccp_xudt_msg_fragment = -1;
static gint ett_sccp_xudt_msg_fragments = -1;
static gint ett_sccp_assoc = -1;
static gint ett_sccp_digits = -1;

/* Declarations to desegment XUDT Messages */
static gboolean sccp_xudt_desegment = TRUE;
static gboolean show_key_params = FALSE;
static gboolean set_addresses = FALSE;

static int sccp_tap = -1;


static const fragment_items sccp_xudt_msg_frag_items = {
	/* Fragment subtrees */
	&ett_sccp_xudt_msg_fragment,
	&ett_sccp_xudt_msg_fragments,
	/* Fragment fields */
	&hf_sccp_xudt_msg_fragments,
	&hf_sccp_xudt_msg_fragment,
	&hf_sccp_xudt_msg_fragment_overlap,
	&hf_sccp_xudt_msg_fragment_overlap_conflicts,
	&hf_sccp_xudt_msg_fragment_multiple_tails,
	&hf_sccp_xudt_msg_fragment_too_long_fragment,
	&hf_sccp_xudt_msg_fragment_error,
	/* Reassembled in field */
	&hf_sccp_xudt_msg_reassembled_in,
	/* Reassembled length field */
	&hf_sccp_xudt_msg_reassembled_length,
	/* Tag */
	"SCCP XUDT Message fragments"
};

static GHashTable *sccp_xudt_msg_fragment_table = NULL;
static GHashTable *sccp_xudt_msg_reassembled_table = NULL;


#define SCCP_USER_DATA 0
#define SCCP_USER_TCAP 1
#define SCCP_USER_RANAP 2
#define SCCP_USER_BSSAP 3
#define SCCP_USER_GSMMAP 4
#define SCCP_USER_CAMEL 5
#define SCCP_USER_INAP 6

typedef struct _sccp_user_t {
	guint ni;
	range_t* called_pc;
	range_t* called_ssn;
	guint user;
	gboolean uses_tcap;
	dissector_handle_t* handlep;
} sccp_user_t;

static sccp_user_t* sccp_users;
static guint num_sccp_users;

static dissector_handle_t data_handle;
static dissector_handle_t tcap_handle;
static dissector_handle_t ranap_handle;
static dissector_handle_t bssap_handle;
static dissector_handle_t gsmmap_handle;
static dissector_handle_t camel_handle;
static dissector_handle_t inap_handle;
static dissector_handle_t default_handle;

static const char *default_payload=NULL;

static const value_string sccp_users_vals[] = {
	{ SCCP_USER_DATA,	"Data"},
	{ SCCP_USER_TCAP,	"TCAP"},
	{ SCCP_USER_RANAP,	"RANAP"},
	{ SCCP_USER_BSSAP,	"BSSAP"},
	{ SCCP_USER_GSMMAP,	"GSM MAP"},
	{ SCCP_USER_CAMEL,	"CAMEL"},
	{ SCCP_USER_INAP,	"INAP"},
	{ 0, NULL }
};

/*
 * Here are the global variables associated with
 * the various user definable characteristics of the dissection
 */
static guint32 sccp_source_pc_global = 0;
static gboolean sccp_show_length = FALSE;

static module_t *sccp_module;
static heur_dissector_list_t heur_subdissector_list;

/*  Keep track of SSN value of current message so if/when we get to the data
 *  parameter, we can call appropriate sub-dissector.  TODO: can this info
 *  be stored elsewhere?
 */

static guint8 message_type = 0;
static guint dlr = 0;
static guint slr = 0;

static dissector_table_t sccp_ssn_dissector_table;

static emem_tree_t* assocs = NULL;
static sccp_assoc_info_t* assoc;
static sccp_msg_info_t* sccp_msg;
static sccp_assoc_info_t no_assoc = {0,0,0,0,0,FALSE,FALSE,NULL,NULL,SCCP_PLOAD_NONE,NULL,NULL,NULL,0};
static gboolean trace_sccp = FALSE;
static guint32 next_assoc_id = 0;

static const value_string assoc_protos[] = {
	{  SCCP_PLOAD_BSSAP, "BSSAP" },
	{  SCCP_PLOAD_RANAP, "RANAP" },
	{ 0 , NULL }
};

static sccp_assoc_info_t *
new_assoc(guint32 calling, guint32 called)
{
	sccp_assoc_info_t* a = se_alloc0(sizeof(sccp_assoc_info_t));

	a->id = next_assoc_id++;
	a->calling_dpc = calling;
	a->called_dpc = called;
	a->calling_ssn = INVALID_SSN;
	a->called_ssn = INVALID_SSN;
	a->msgs = NULL;
	a->curr_msg = NULL;
	a->payload = SCCP_PLOAD_NONE;
	a->calling_party = NULL;
	a->called_party = NULL;
	a->extra_info = NULL;

	return a;
}

void
reset_sccp_assoc(void)
{
	assoc = NULL;
}

sccp_assoc_info_t *
get_sccp_assoc(packet_info* pinfo, guint offset, guint32 src_lr, guint32 dst_lr, guint msg_type)
{
    guint32 opck, dpck;
    address* opc = &(pinfo->src);
    address* dpc = &(pinfo->dst);
    guint framenum = pinfo->fd->num;

    if(assoc)
        return assoc;

    opck = opc->type == AT_SS7PC ? mtp3_pc_hash((const mtp3_addr_pc_t *)opc->data) : g_str_hash(ep_address_to_str(opc));
    dpck = dpc->type == AT_SS7PC ? mtp3_pc_hash((const mtp3_addr_pc_t *)dpc->data) : g_str_hash(ep_address_to_str(dpc));


    switch (msg_type) {
        case SCCP_MSG_TYPE_CR:
        {
            /* CR contains the opc,dpc,dlr key of backward messages swapped as dpc,opc,slr  */
            emem_tree_key_t bw_key[] = {
                {1, &dpck}, {1, &opck}, {1, &src_lr}, {0, NULL}
            };

            if (! ( assoc = se_tree_lookup32_array(assocs,bw_key) ) && ! pinfo->fd->flags.visited ) {
                assoc = new_assoc(opck, dpck);
                se_tree_insert32_array(assocs, bw_key, assoc);
                assoc->has_bw_key = TRUE;
            }

            pinfo->p2p_dir = P2P_DIR_SENT;

            break;
        }
        case SCCP_MSG_TYPE_CC:
        {
            emem_tree_key_t fw_key[] = {
                {1, &dpck}, {1, &opck}, {1, &src_lr}, {0, NULL}
            };
            emem_tree_key_t bw_key[] = {
                {1, &opck}, {1, &dpck}, {1, &dst_lr}, {0, NULL}
            };

            if ( ( assoc = se_tree_lookup32_array(assocs, bw_key) ) ) {
                goto got_assoc;
            }

            if ( (assoc = se_tree_lookup32_array(assocs, fw_key) ) ) {
                goto got_assoc;
            }

            assoc = new_assoc(dpck,opck);

     got_assoc:

            pinfo->p2p_dir = P2P_DIR_RECV;

            if ( ! pinfo->fd->flags.visited && ! assoc->has_bw_key ) {
                se_tree_insert32_array(assocs, bw_key, assoc);
                assoc->has_bw_key = TRUE;
            }

            if ( ! pinfo->fd->flags.visited && ! assoc->has_fw_key ) {
                se_tree_insert32_array(assocs, fw_key, assoc);
                assoc->has_fw_key = TRUE;
            }

            break;
        }
        case SCCP_MSG_TYPE_RLC:
        {
            emem_tree_key_t bw_key[] = {
                {1, &dpck}, {1, &opck}, {1, &src_lr}, {0, NULL}
            };
            emem_tree_key_t fw_key[] = {
                {1, &opck}, {1, &dpck}, {1, &dst_lr}, {0, NULL}
            };
            if ( ( assoc = se_tree_lookup32_array(assocs, bw_key) ) ) {
                goto got_assoc_rlc;
            }

            if ( (assoc = se_tree_lookup32_array(assocs, fw_key) ) ) {
                goto got_assoc_rlc;
            }

            assoc = new_assoc(dpck, opck);

     got_assoc_rlc:

            pinfo->p2p_dir = P2P_DIR_SENT;

            if ( ! pinfo->fd->flags.visited && ! assoc->has_bw_key ) {
                se_tree_insert32_array(assocs, bw_key, assoc);
                assoc->has_bw_key = TRUE;
            }

            if ( ! pinfo->fd->flags.visited && ! assoc->has_fw_key ) {
                se_tree_insert32_array(assocs, fw_key, assoc);
                assoc->has_fw_key = TRUE;
            }
            break;
        }
        default:
        {
            emem_tree_key_t key[] = {
                {1, &opck}, {1, &dpck}, {1, &dst_lr}, {0, NULL}
            };

            assoc = se_tree_lookup32_array(assocs, key);

            if (assoc) {
                if (assoc->calling_dpc == dpck) {
                    pinfo->p2p_dir = P2P_DIR_RECV;
                } else {
                    pinfo->p2p_dir = P2P_DIR_SENT;
                }
            }

            break;
        }
    }

    if (assoc && trace_sccp) {
        if ( ! pinfo->fd->flags.visited) {
            sccp_msg_info_t* msg = se_alloc0(sizeof(sccp_msg_info_t));
            msg->framenum = framenum;
            msg->offset = offset;
            msg->data.co.next = NULL;
            msg->data.co.assoc = assoc;
            msg->data.co.label = NULL;
            msg->data.co.comment = NULL;
            msg->type = msg_type;

            if (assoc->msgs) {
                sccp_msg_info_t* m;
                for (m = assoc->msgs; m->data.co.next; m = m->data.co.next) ;
                m->data.co.next = msg;
            } else {
                assoc->msgs = msg;
            }

            assoc->curr_msg = msg;

        } else {

            sccp_msg_info_t* m;

            for (m = assoc->msgs; m; m = m->data.co.next) {
                if (m->framenum == framenum && m->offset == offset) {
                    assoc->curr_msg = m;
                    break;
                }
            }
        }
    }

    return assoc ? assoc : &no_assoc;
}


static void
dissect_sccp_unknown_message(tvbuff_t *message_tvb, proto_tree *sccp_tree)
{
  guint32 message_length;

  message_length = tvb_length(message_tvb);

  proto_tree_add_text(sccp_tree, message_tvb, 0, message_length,
                      "Unknown message (%u byte%s)",
                      message_length, plurality(message_length, "", "s"));
}

static void
dissect_sccp_unknown_param(tvbuff_t *tvb, proto_tree *tree, guint8 type, guint length)
{
  proto_tree_add_text(tree, tvb, 0, length, "Unknown parameter 0x%x (%u byte%s)",
                      type, length, plurality(length, "", "s"));
}

static void
dissect_sccp_dlr_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  proto_item *lr_item, *expert_item;

  if (length != 3) {
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 3, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 3, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  dlr = tvb_get_letoh24(tvb, 0);
  proto_tree_add_uint(tree, hf_sccp_dlr, tvb, 0, length, dlr);
  lr_item = proto_tree_add_uint(tree, hf_sccp_lr, tvb, 0, length, dlr);
  PROTO_ITEM_SET_HIDDEN(lr_item);

  if (show_key_params && check_col(pinfo->cinfo, COL_INFO))
    col_append_fstr(pinfo->cinfo, COL_INFO, "DLR=%d ", dlr);
}

static void
dissect_sccp_slr_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  proto_item *lr_item, *expert_item;

  if (length != 3) {
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 3, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 3, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  slr = tvb_get_letoh24(tvb, 0);
  proto_tree_add_uint(tree, hf_sccp_slr, tvb, 0, length, slr);
  lr_item = proto_tree_add_uint(tree, hf_sccp_lr, tvb, 0, length, slr);
  PROTO_ITEM_SET_HIDDEN(lr_item);

  if (show_key_params && check_col(pinfo->cinfo, COL_INFO))
    col_append_fstr(pinfo->cinfo, COL_INFO, "SLR=%d ", slr);
}


#define is_connectionless(m) \
         ( m == SCCP_MSG_TYPE_UDT || m == SCCP_MSG_TYPE_UDTS  \
        || m == SCCP_MSG_TYPE_XUDT|| m == SCCP_MSG_TYPE_XUDTS \
        || m == SCCP_MSG_TYPE_LUDT|| m == SCCP_MSG_TYPE_LUDTS)

static proto_item *
dissect_sccp_gt_address_information(tvbuff_t *tvb, packet_info *pinfo,
				    proto_tree *tree, guint length,
				    gboolean even_length, gboolean called,
				    gboolean route_on_gt)
{
  guint offset = 0;
  guint8 odd_signal, even_signal;
  proto_item *digits_item, *hidden_item;
  char *gt_digits;

  gt_digits = ep_alloc0(GT_MAX_SIGNALS+1);

  while(offset < length)
  {
    odd_signal = tvb_get_guint8(tvb, offset) & GT_ODD_SIGNAL_MASK;
    even_signal = tvb_get_guint8(tvb, offset) & GT_EVEN_SIGNAL_MASK;
    even_signal >>= GT_EVEN_SIGNAL_SHIFT;

    g_strlcat(gt_digits, val_to_str(odd_signal, sccp_address_signal_values,
				 "Unknown"), GT_MAX_SIGNALS+1);

    /* If the last signal is NOT filler */
    if (offset != (length - 1) || even_length == TRUE)
      g_strlcat(gt_digits, val_to_str(even_signal, sccp_address_signal_values,
				   "Unknown"), GT_MAX_SIGNALS+1);

    offset += GT_SIGNAL_LENGTH;
  }

  if (is_connectionless(message_type) && sccp_msg) {
	guint8** gt_ptr = called ? &(sccp_msg->data.ud.called_gt) : &(sccp_msg->data.ud.calling_gt);

	*gt_ptr  = (guint8 *)ep_strdup(gt_digits);
  }

  digits_item = proto_tree_add_string_format(tree,
					     called ? hf_sccp_called_gt_digits
						    : hf_sccp_calling_gt_digits,
					     tvb, 0, length, gt_digits,
					     "Address information (digits): %s",
					     gt_digits);

  if (set_addresses && route_on_gt) {
    if (called) {
      SET_ADDRESS(&pinfo->dst, AT_STRINGZ, 1+(int)strlen(gt_digits), gt_digits);
    } else {
      SET_ADDRESS(&pinfo->src, AT_STRINGZ, 1+(int)strlen(gt_digits), gt_digits);
    }
  }

  hidden_item = proto_tree_add_string(tree, hf_sccp_gt_digits, tvb, 0, length, gt_digits);
  PROTO_ITEM_SET_HIDDEN(hidden_item);

  return digits_item;
}

static void
dissect_sccp_global_title(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length,
			  guint8 gti, gboolean route_on_gt, gboolean called)
{
  proto_item *gt_item = 0;
  proto_item *digits_item = 0;
  proto_tree *gt_tree = 0;
  proto_tree *digits_tree = 0;
  tvbuff_t *signals_tvb;
  guint offset = 0;
  guint8 odd_even, nai = 0, tt, np = 0, es;
  gboolean even = TRUE;

  /* Shift GTI to where we can work with it */
  gti >>= GTI_SHIFT;

  gt_item = proto_tree_add_text(tree, tvb, offset, length,
				"Global Title 0x%x (%u byte%s)",
				gti, length, plurality(length,"", "s"));
  gt_tree = proto_item_add_subtree(gt_item, called ? ett_sccp_called_gt
						   : ett_sccp_calling_gt);

  /* Decode Transation Type (if present) */
  if ((gti == AI_GTI_TT) ||
      (decode_mtp3_standard != ANSI_STANDARD &&
	  (gti == ITU_AI_GTI_TT_NP_ES || gti == ITU_AI_GTI_TT_NP_ES_NAI)) ||
      (decode_mtp3_standard == ANSI_STANDARD && gti == ANSI_AI_GTI_TT_NP_ES)) {

    tt = tvb_get_guint8(tvb, offset);
    proto_tree_add_uint(gt_tree, called ? hf_sccp_called_gt_tt
					: hf_sccp_calling_gt_tt,
			tvb, offset, GT_TT_LENGTH, tt);
    offset += GT_TT_LENGTH;
  }

  if (gti == AI_GTI_TT) {
    /* Protocol doesn't tell us, so we ASSUME even... */
    even = TRUE;
  }

  /* Decode Numbering Plan and Encoding Scheme (if present) */
  if ((decode_mtp3_standard != ANSI_STANDARD &&
       (gti == ITU_AI_GTI_TT_NP_ES || gti == ITU_AI_GTI_TT_NP_ES_NAI)) ||
      (decode_mtp3_standard == ANSI_STANDARD && gti == ANSI_AI_GTI_TT_NP_ES)) {

    np = tvb_get_guint8(tvb, offset) & GT_NP_MASK;
    proto_tree_add_uint(gt_tree, called ? hf_sccp_called_gt_np
					: hf_sccp_calling_gt_np,
			tvb, offset, GT_NP_ES_LENGTH, np);

    es = tvb_get_guint8(tvb, offset) & GT_ES_MASK;
    proto_tree_add_uint(gt_tree, called ? hf_sccp_called_gt_es
					: hf_sccp_calling_gt_es,
			tvb, offset, GT_NP_ES_LENGTH, es);

    even = (es == GT_ES_BCD_EVEN) ? TRUE : FALSE;

    offset += GT_NP_ES_LENGTH;
  }

  /* Decode Nature of Address Indicator (if present) */
  if (decode_mtp3_standard != ANSI_STANDARD &&
      (gti == ITU_AI_GTI_NAI || gti == ITU_AI_GTI_TT_NP_ES_NAI)) {

    /* Decode Odd/Even Indicator (if present) */
    if (gti == ITU_AI_GTI_NAI) {
      odd_even = tvb_get_guint8(tvb, offset) & GT_OE_MASK;
      proto_tree_add_uint(gt_tree, called ? hf_sccp_called_gt_oe
					  : hf_sccp_calling_gt_oe,
			  tvb, offset, GT_NAI_LENGTH, odd_even);
      even = (odd_even == GT_OE_EVEN) ? TRUE : FALSE;
    }

    nai = tvb_get_guint8(tvb, offset) & GT_NAI_MASK;
    proto_tree_add_uint(gt_tree, called ? hf_sccp_called_gt_nai
					: hf_sccp_calling_gt_nai,
			tvb, offset, GT_NAI_LENGTH, nai);

    offset += GT_NAI_LENGTH;
  }

  /* Decode address signal(s) */
  if (length < offset)
    return;

  signals_tvb = tvb_new_subset(tvb, offset, (length - offset),
			       (length - offset));

  digits_item = dissect_sccp_gt_address_information(signals_tvb, pinfo, gt_tree,
						    (length - offset),
						    even, called, route_on_gt);

  /* Display the country code (if we can) */
  switch(np >> GT_NP_SHIFT) {
	case GT_NP_ISDN:
	case GT_NP_ISDN_MOBILE:
		if(nai == GT_NAI_INTERNATIONAL_NUM) {
			digits_tree = proto_item_add_subtree(digits_item,
							     ett_sccp_digits);
			dissect_e164_cc(signals_tvb, digits_tree, 0, TRUE);
		}
	break;
	case GT_NP_LAND_MOBILE:
		digits_tree = proto_item_add_subtree(digits_item,
						     ett_sccp_digits);
		dissect_e212_mcc_mnc_in_address(signals_tvb, pinfo, digits_tree, 0);
	break;
	default:
	break;
  }
}

static int
dissect_sccp_3byte_pc(tvbuff_t *tvb, proto_tree *call_tree, guint offset,
		      gboolean called)
{
  int hf_pc;

  if (decode_mtp3_standard == ANSI_STANDARD)
  {
    if (called)
      hf_pc = hf_sccp_called_ansi_pc;
    else
      hf_pc = hf_sccp_calling_ansi_pc;
  } else /* CHINESE_ITU_STANDARD */ {
    if (called)
      hf_pc = hf_sccp_called_chinese_pc;
    else
      hf_pc = hf_sccp_calling_chinese_pc;
  }

  /* create and fill the PC tree */
  dissect_mtp3_3byte_pc(tvb, offset, call_tree,
			called ? ett_sccp_called_pc : ett_sccp_calling_pc,
			hf_pc,
			called ? hf_sccp_called_pc_network : hf_sccp_calling_pc_network,
			called ? hf_sccp_called_pc_cluster : hf_sccp_calling_pc_cluster,
			called ? hf_sccp_called_pc_member  : hf_sccp_calling_pc_member,
			0, 0);

  return(offset + ANSI_PC_LENGTH);
}

/*  FUNCTION dissect_sccp_called_calling_param():
 *  Dissect the Calling or Called Party Address parameters.
 *
 *  The boolean 'called' describes whether this function is decoding a
 *  called (TRUE) or calling (FALSE) party address.  There is simply too
 *  much code in this function to have 2 copies of it (one for called, one
 *  for calling).
 *
 *  NOTE:  this function is called even when (!tree) so that we can get
 *  the SSN and subsequently call subdissectors (if and when there's a data
 *  parameter).  Realistically we should put if (!tree)'s around a lot of the
 *  code, but I think that would make it unreadable--and the expense of not
 *  doing so does not appear to be very high.
 */
static void
dissect_sccp_called_calling_param(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo,
				  guint length, gboolean called)
{
  proto_item *call_item = 0, *call_ai_item = 0, *item, *hidden_item, *expert_item;
  proto_tree *call_tree = 0, *call_ai_tree = 0;
  guint offset;
  guint8 national = 0xFFU, routing_ind, gti, pci, ssni, ssn;
  tvbuff_t *gt_tvb;
  dissector_handle_t ssn_dissector = NULL, tcap_ssn_dissector = NULL;
  const char *ssn_dissector_short_name = NULL;
  const char *tcap_ssn_dissector_short_name = NULL;

  call_item = proto_tree_add_text(tree, tvb, 0, length,
				  "%s Party address (%u byte%s)",
				  called ? "Called" : "Calling", length,
				  plurality(length, "", "s"));
  call_tree = proto_item_add_subtree(call_item, called ? ett_sccp_called
						       : ett_sccp_calling);

  call_ai_item = proto_tree_add_text(call_tree, tvb, 0,
				     ADDRESS_INDICATOR_LENGTH,
				     "Address Indicator");
  call_ai_tree = proto_item_add_subtree(call_ai_item, called ? ett_sccp_called_ai
							     : ett_sccp_calling_ai);

  if (decode_mtp3_standard == ANSI_STANDARD)
  {
    national = tvb_get_guint8(tvb, 0) & ANSI_NATIONAL_MASK;
    expert_item = proto_tree_add_uint(call_ai_tree, called ? hf_sccp_called_national_indicator
							   : hf_sccp_calling_national_indicator,
				      tvb, 0, ADDRESS_INDICATOR_LENGTH, national);
    if (national == 0)
          expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_NOTE, "Address is coded to "
				 "international standards.  This doesn't normally happen in ANSI "
				 "networks.");
  }

  routing_ind = tvb_get_guint8(tvb, 0) & ROUTING_INDICATOR_MASK;
  proto_tree_add_uint(call_ai_tree, called ? hf_sccp_called_routing_indicator
					   : hf_sccp_calling_routing_indicator,
		      tvb, 0, ADDRESS_INDICATOR_LENGTH, routing_ind);

  gti = tvb_get_guint8(tvb, 0) & GTI_MASK;

  if (decode_mtp3_standard == ITU_STANDARD ||
      decode_mtp3_standard == CHINESE_ITU_STANDARD ||
      decode_mtp3_standard == JAPAN_STANDARD ||
      national == 0) {

    proto_tree_add_uint(call_ai_tree, called ? hf_sccp_called_itu_global_title_indicator
					     : hf_sccp_calling_itu_global_title_indicator,
			tvb, 0, ADDRESS_INDICATOR_LENGTH, gti);

    ssni = tvb_get_guint8(tvb, 0) & ITU_SSN_INDICATOR_MASK;
    proto_tree_add_uint(call_ai_tree, called ? hf_sccp_called_itu_ssn_indicator
					     : hf_sccp_calling_itu_ssn_indicator,
			tvb, 0, ADDRESS_INDICATOR_LENGTH, ssni);

    pci = tvb_get_guint8(tvb, 0) & ITU_PC_INDICATOR_MASK;
    proto_tree_add_uint(call_ai_tree, called ? hf_sccp_called_itu_point_code_indicator
					     : hf_sccp_calling_itu_point_code_indicator,
			tvb, 0, ADDRESS_INDICATOR_LENGTH, pci);

    offset = ADDRESS_INDICATOR_LENGTH;

    /* Dissect PC (if present) */
    if (pci) {
      if (decode_mtp3_standard == ITU_STANDARD || national == 0) {
        if (length < offset + ITU_PC_LENGTH){
          expert_item = proto_tree_add_text(call_tree, tvb, 0, -1, "Wrong length indicated (%u) should be at least %u, PC is %u octets", length, offset + ITU_PC_LENGTH, ITU_PC_LENGTH);
          expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated");
          PROTO_ITEM_SET_GENERATED(expert_item);
          return;
        }
        proto_tree_add_item(call_tree, called ? hf_sccp_called_itu_pc
                                              : hf_sccp_calling_itu_pc,
                            tvb, offset, ITU_PC_LENGTH, TRUE);
        offset += ITU_PC_LENGTH;

      } else if (decode_mtp3_standard == JAPAN_STANDARD) {

        if (length < offset + JAPAN_PC_LENGTH){
          expert_item = proto_tree_add_text(call_tree, tvb, 0, -1, "Wrong length indicated (%u) should be at least %u, PC is %u octets", length, offset + JAPAN_PC_LENGTH, JAPAN_PC_LENGTH);
          expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated");
          PROTO_ITEM_SET_GENERATED(expert_item);
          return;
        }
        proto_tree_add_item(call_tree, called ? hf_sccp_called_japan_pc
                                              : hf_sccp_calling_japan_pc,
                            tvb, offset, JAPAN_PC_LENGTH, TRUE);

        offset += JAPAN_PC_LENGTH;

      } else /* CHINESE_ITU_STANDARD */ {

        if (length < offset + ANSI_PC_LENGTH){
          expert_item = proto_tree_add_text(call_tree, tvb, 0, -1, "Wrong length indicated (%u) should be at least %u, PC is %u octets", length, offset + ANSI_PC_LENGTH, ANSI_PC_LENGTH);
          expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated");
          PROTO_ITEM_SET_GENERATED(expert_item);
           return;
        }
        offset = dissect_sccp_3byte_pc(tvb, call_tree, offset, called);

      }
    }

    /* Dissect SSN (if present) */
    if (ssni) {
      ssn = tvb_get_guint8(tvb, offset);

      if (called && assoc)
	assoc->called_ssn = ssn;
      else if (assoc)
	assoc->calling_ssn = ssn;

      if (is_connectionless(message_type) && sccp_msg) {
	guint* ssn_ptr = called ? &(sccp_msg->data.ud.called_ssn) : &(sccp_msg->data.ud.calling_ssn);

	*ssn_ptr  = ssn;
      }

      proto_tree_add_uint(call_tree, called ? hf_sccp_called_ssn
					    : hf_sccp_calling_ssn,
			  tvb, offset, ADDRESS_SSN_LENGTH, ssn);
      hidden_item = proto_tree_add_uint(call_tree, hf_sccp_ssn, tvb, offset,
					ADDRESS_SSN_LENGTH, ssn);
      PROTO_ITEM_SET_HIDDEN(hidden_item);

      offset += ADDRESS_SSN_LENGTH;

      /* Get the dissector handle of the dissector registered for this ssn
       * And print it's name.
       */
      ssn_dissector = dissector_get_uint_handle(sccp_ssn_dissector_table, ssn);

      if (ssn_dissector) {
	ssn_dissector_short_name = dissector_handle_get_short_name(ssn_dissector);

	if(ssn_dissector_short_name) {
	  item = proto_tree_add_text(call_tree, tvb, offset - 1, ADDRESS_SSN_LENGTH, "Linked to %s", ssn_dissector_short_name);
	  PROTO_ITEM_SET_GENERATED(item);

	  if (g_ascii_strncasecmp("TCAP", ssn_dissector_short_name, 4)== 0) {
	    tcap_ssn_dissector = get_itu_tcap_subdissector(ssn);

	    if(tcap_ssn_dissector) {
	      tcap_ssn_dissector_short_name = dissector_handle_get_short_name(tcap_ssn_dissector);
	      proto_item_append_text(item,", TCAP SSN linked to %s", tcap_ssn_dissector_short_name);
	    }
	  }
	} /* short name */
      } /* ssn_dissector */
    } /* ssni */

    /* Dissect GT (if present) */
    if (gti != AI_GTI_NO_GT) {
      if (length < offset)
	return;

      gt_tvb = tvb_new_subset(tvb, offset, (length - offset),
			      (length - offset));
      dissect_sccp_global_title(gt_tvb, pinfo, call_tree, (length - offset), gti,
				(routing_ind == ROUTE_ON_GT), called);
    }

  } else if (decode_mtp3_standard == ANSI_STANDARD) {

    proto_tree_add_uint(call_ai_tree, called ? hf_sccp_called_ansi_global_title_indicator
					     : hf_sccp_calling_ansi_global_title_indicator,
			tvb, 0, ADDRESS_INDICATOR_LENGTH, gti);

    pci = tvb_get_guint8(tvb, 0) & ANSI_PC_INDICATOR_MASK;
    proto_tree_add_uint(call_ai_tree, called ? hf_sccp_called_ansi_point_code_indicator
					     : hf_sccp_calling_ansi_point_code_indicator,
			tvb, 0, ADDRESS_INDICATOR_LENGTH, pci);

    ssni = tvb_get_guint8(tvb, 0) & ANSI_SSN_INDICATOR_MASK;
    proto_tree_add_uint(call_ai_tree, called ? hf_sccp_called_ansi_ssn_indicator
					     : hf_sccp_calling_ansi_ssn_indicator,
			tvb, 0, ADDRESS_INDICATOR_LENGTH, ssni);

    offset = ADDRESS_INDICATOR_LENGTH;

    /* Dissect SSN (if present) */
    if (ssni) {
      ssn = tvb_get_guint8(tvb, offset);

      if (called && assoc){
	assoc->called_ssn = ssn;
      }else if (assoc){
	assoc->calling_ssn = ssn;
      }

      if (is_connectionless(message_type) && sccp_msg) {
	guint* ssn_ptr = called ? &(sccp_msg->data.ud.called_ssn) : &(sccp_msg->data.ud.calling_ssn);

	*ssn_ptr  = ssn;
      }

      proto_tree_add_uint(call_tree, called ? hf_sccp_called_ssn
					    : hf_sccp_calling_ssn,
			  tvb, offset, ADDRESS_SSN_LENGTH, ssn);
      hidden_item = proto_tree_add_uint(call_tree, hf_sccp_ssn, tvb, offset,
					ADDRESS_SSN_LENGTH, ssn);
      PROTO_ITEM_SET_HIDDEN(hidden_item);

      offset += ADDRESS_SSN_LENGTH;
    }

    /* Dissect PC (if present) */
    if (pci) {
      offset = dissect_sccp_3byte_pc(tvb, call_tree, offset, called);
    }

    /* Dissect GT (if present) */
    if (gti != AI_GTI_NO_GT) {
      if (length < offset)
		  return;
      gt_tvb = tvb_new_subset(tvb, offset, (length - offset),
			      (length - offset));
      dissect_sccp_global_title(gt_tvb, pinfo, call_tree, (length - offset), gti,
				(routing_ind == ROUTE_ON_GT), called);
    }

  }

}

static void
dissect_sccp_called_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  dissect_sccp_called_calling_param(tvb, tree, pinfo, length, TRUE);
}

static void
dissect_sccp_calling_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  dissect_sccp_called_calling_param(tvb, tree, pinfo, length, FALSE);
}

static void
dissect_sccp_class_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 class, handling;

  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  class = tvb_get_guint8(tvb, 0) & CLASS_CLASS_MASK;
  handling = tvb_get_guint8(tvb, 0) & CLASS_SPARE_HANDLING_MASK;

  proto_tree_add_uint(tree, hf_sccp_class, tvb, 0, length, class);

  if (class == 0 || class == 1)
    proto_tree_add_uint(tree, hf_sccp_handling, tvb, 0, length, handling);
}

static void
dissect_sccp_segmenting_reassembling_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  proto_tree_add_item(tree, hf_sccp_more, tvb, 0, length, FALSE);
}

static void
dissect_sccp_receive_sequence_number_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 rsn;

  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  rsn = tvb_get_guint8(tvb, 0) >> 1;
  proto_tree_add_uint(tree, hf_sccp_rsn, tvb, 0, length, rsn);
}

static void
dissect_sccp_sequencing_segmenting_param(tvbuff_t *tvb, proto_tree *tree, guint length)
{
  guint8 rsn, ssn, more;
  proto_item *param_item;
  proto_tree *param_tree;

  ssn = tvb_get_guint8(tvb, 0) >> 1;
  rsn = tvb_get_guint8(tvb, SEQUENCING_SEGMENTING_SSN_LENGTH) >> 1;
  more = tvb_get_guint8(tvb, SEQUENCING_SEGMENTING_SSN_LENGTH) & SEQUENCING_SEGMENTING_MORE_MASK;

  param_item = proto_tree_add_text(tree, tvb, 0, length, "%s",
				   val_to_str(PARAMETER_SEQUENCING_SEGMENTING,
					      sccp_parameter_values, "Unknown"));
  param_tree = proto_item_add_subtree(param_item,
				      ett_sccp_sequencing_segmenting);

  proto_tree_add_uint(param_tree, hf_sccp_sequencing_segmenting_ssn, tvb, 0,
		      SEQUENCING_SEGMENTING_SSN_LENGTH, ssn);
  proto_tree_add_uint(param_tree, hf_sccp_sequencing_segmenting_rsn, tvb,
		      SEQUENCING_SEGMENTING_SSN_LENGTH,
		      SEQUENCING_SEGMENTING_RSN_LENGTH, rsn);
  proto_tree_add_uint(param_tree, hf_sccp_sequencing_segmenting_more, tvb,
		      SEQUENCING_SEGMENTING_SSN_LENGTH,
		      SEQUENCING_SEGMENTING_RSN_LENGTH, more);
}

static void
dissect_sccp_credit_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 credit;

  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  credit = tvb_get_guint8(tvb, 0);
  proto_tree_add_uint(tree, hf_sccp_credit, tvb, 0, length, credit);
}

static void
dissect_sccp_release_cause_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 cause;

  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  cause = tvb_get_guint8(tvb, 0);
  proto_tree_add_uint(tree, hf_sccp_release_cause, tvb, 0, length, cause);

  if (show_key_params && check_col(pinfo->cinfo, COL_INFO))
    col_append_fstr(pinfo->cinfo, COL_INFO, "Cause=%d ", cause);
}

static void
dissect_sccp_return_cause_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 cause;

  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  cause = tvb_get_guint8(tvb, 0);
  proto_tree_add_uint(tree, hf_sccp_return_cause, tvb, 0, length, cause);

  if (show_key_params && check_col(pinfo->cinfo, COL_INFO))
    col_append_fstr(pinfo->cinfo, COL_INFO, "Cause=%d ", cause);
}

static void
dissect_sccp_reset_cause_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 cause;

  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  cause = tvb_get_guint8(tvb, 0);
  proto_tree_add_uint(tree, hf_sccp_reset_cause, tvb, 0, length, cause);

  if (show_key_params && check_col(pinfo->cinfo, COL_INFO))
    col_append_fstr(pinfo->cinfo, COL_INFO, "Cause=%d ", cause);
}

static void
dissect_sccp_error_cause_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 cause;

  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  cause = tvb_get_guint8(tvb, 0);
  proto_tree_add_uint(tree, hf_sccp_error_cause, tvb, 0, length, cause);

  if (show_key_params && check_col(pinfo->cinfo, COL_INFO))
    col_append_fstr(pinfo->cinfo, COL_INFO, "Cause=%d ", cause);
}

static void
dissect_sccp_refusal_cause_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 cause;

  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  cause = tvb_get_guint8(tvb, 0);
  proto_tree_add_uint(tree, hf_sccp_refusal_cause, tvb, 0, length, cause);

  if (show_key_params && check_col(pinfo->cinfo, COL_INFO))
    col_append_fstr(pinfo->cinfo, COL_INFO, "Cause=%d ", cause);
}


/* This function is used for both data and long data (ITU only) parameters */
static void
dissect_sccp_data_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    guint8 ssn = INVALID_SSN;
    guint8 other_ssn = INVALID_SSN;
    const mtp3_addr_pc_t* dpc = NULL;
    const mtp3_addr_pc_t* opc = NULL;

    if (trace_sccp && assoc && assoc != &no_assoc) {
        pinfo->sccp_info = assoc->curr_msg;
    } else {
        pinfo->sccp_info = NULL;
    }

    if (assoc) {
        switch (pinfo->p2p_dir) {
        case P2P_DIR_SENT:
            ssn = assoc->calling_ssn;
            other_ssn = assoc->called_ssn;
            dpc = (const mtp3_addr_pc_t*)pinfo->dst.data;
            opc = (const mtp3_addr_pc_t*)pinfo->src.data;
            break;
        case P2P_DIR_RECV:
            ssn = assoc->called_ssn;
            other_ssn = assoc->calling_ssn;
            dpc = (const mtp3_addr_pc_t*)pinfo->src.data;
            opc = (const mtp3_addr_pc_t*)pinfo->dst.data;
            break;
        default:
            ssn = assoc->called_ssn;
            other_ssn = assoc->calling_ssn;
            dpc = (const mtp3_addr_pc_t*)pinfo->dst.data;
            opc = (const mtp3_addr_pc_t*)pinfo->src.data;
            break;
        }
    }


    if (num_sccp_users && pinfo->src.type == AT_SS7PC) {
	guint i;
	dissector_handle_t handle = NULL;
        gboolean uses_tcap = FALSE;

	for (i=0; i < num_sccp_users; i++) {
		sccp_user_t* u = &(sccp_users[i]);

		if (!dpc || dpc->ni != u->ni) continue;

		if (value_is_in_range(u->called_ssn, ssn)  && value_is_in_range(u->called_pc, dpc->pc) ) {
			handle = *(u->handlep);
			uses_tcap = u->uses_tcap;
			break;
		} else if (value_is_in_range(u->called_ssn, other_ssn) && opc && value_is_in_range(u->called_pc, opc->pc) ) {
			handle = *(u->handlep);
			uses_tcap = u->uses_tcap;
			break;
		}
	}

	if (handle) {
		if (uses_tcap) {
			call_tcap_dissector(handle, tvb, pinfo, tree);
		} else {
			call_dissector(handle, tvb, pinfo, tree);
		}
		return;
	}

   }

    if (ssn != INVALID_SSN && dissector_try_uint(sccp_ssn_dissector_table, ssn, tvb, pinfo, tree)) {
		return;
    }

    if (other_ssn != INVALID_SSN && dissector_try_uint(sccp_ssn_dissector_table, other_ssn, tvb, pinfo, tree)) {
		return;
    }

    /* try heuristic subdissector list to see if there are any takers */
    if (dissector_try_heuristic(heur_subdissector_list, tvb, pinfo, tree)) {
		return;
    }

    /* try user default subdissector */
    if (default_handle) {
        call_dissector(default_handle, tvb, pinfo, tree);
        return;
    }

    /* No sub-dissection occured, treat it as raw data */
    call_dissector(data_handle, tvb, pinfo, tree);

}

static void
dissect_sccp_segmentation_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 first, class, remaining;
  guint32 slrx;
  proto_item *param_item;
  proto_tree *param_tree;

  first = tvb_get_guint8(tvb, 0) & SEGMENTATION_FIRST_SEGMENT_MASK;
  class = tvb_get_guint8(tvb, 0) & SEGMENTATION_CLASS_MASK;
  remaining = tvb_get_guint8(tvb, 0) & SEGMENTATION_REMAINING_MASK;

  slrx = tvb_get_letoh24(tvb, 1);

  param_item = proto_tree_add_text(tree, tvb, 0, length, "%s",
				   val_to_str(PARAMETER_SEGMENTATION,
					      sccp_parameter_values, "Unknown"));
  param_tree = proto_item_add_subtree(param_item, ett_sccp_segmentation);

  proto_tree_add_uint(param_tree, hf_sccp_segmentation_first, tvb, 0, 1, first);
  proto_tree_add_uint(param_tree, hf_sccp_segmentation_class, tvb, 0, 1, class);
  proto_tree_add_uint(param_tree, hf_sccp_segmentation_remaining, tvb, 0, 1,
		      remaining);

  if (length-1 != 3) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length-1, "Wrong length indicated. Expected 3, got %u", length-1);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 3, got %u", length-1);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  proto_tree_add_uint(param_tree, hf_sccp_segmentation_slr, tvb, 1, length-1,
		      slrx);
}

static void
dissect_sccp_hop_counter_param(tvbuff_t *tvb, proto_tree *tree, guint length)
{
  guint8 hops;

  hops = tvb_get_guint8(tvb, 0);
  proto_tree_add_uint(tree, hf_sccp_hop_counter, tvb, 0, length, hops);
}

static void
dissect_sccp_importance_param(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint length)
{
  guint8 importance;

  if (length != 1) {
    proto_item *expert_item;
    expert_item = proto_tree_add_text(tree, tvb, 0, length, "Wrong length indicated. Expected 1, got %u", length);
    expert_add_info_format(pinfo, expert_item, PI_MALFORMED, PI_ERROR, "Wrong length indicated. Expected 1, got %u", length);
    PROTO_ITEM_SET_GENERATED(expert_item);
    return;
  }

  importance = tvb_get_guint8(tvb, 0) & IMPORTANCE_IMPORTANCE_MASK;
  proto_tree_add_uint(tree, hf_sccp_importance, tvb, 0, length, importance);
}

static void
dissect_sccp_isni_param(tvbuff_t *tvb, proto_tree *tree, guint length)
{
  guint8 mi, iri, ti, network;
  guint offset = 0;
  proto_item *param_item;
  proto_tree *param_tree;

  /* Create a subtree for ISNI Routing Control */
  param_item = proto_tree_add_text(tree, tvb, offset, ANSI_ISNI_ROUTING_CONTROL_LENGTH,
				   "ISNI Routing Control");
  param_tree = proto_item_add_subtree(param_item,
				      ett_sccp_ansi_isni_routing_control);

  mi = tvb_get_guint8(tvb, offset) & ANSI_ISNI_MI_MASK;
  proto_tree_add_uint(param_tree, hf_sccp_ansi_isni_mi, tvb, offset,
		      ANSI_ISNI_ROUTING_CONTROL_LENGTH, mi);

  iri = tvb_get_guint8(tvb, offset) & ANSI_ISNI_IRI_MASK;
  proto_tree_add_uint(param_tree, hf_sccp_ansi_isni_iri, tvb, offset,
		      ANSI_ISNI_ROUTING_CONTROL_LENGTH, iri);

  ti = tvb_get_guint8(tvb, offset) & ANSI_ISNI_TI_MASK;
  proto_tree_add_uint(param_tree, hf_sccp_ansi_isni_ti, tvb, offset,
		      ANSI_ISNI_ROUTING_CONTROL_LENGTH, ti);

  proto_tree_add_item(param_tree, hf_sccp_ansi_isni_counter, tvb, offset,
		      ANSI_ISNI_ROUTING_CONTROL_LENGTH, TRUE);

  offset += ANSI_ISNI_ROUTING_CONTROL_LENGTH;

  if ((ti >> ANSI_ISNI_TI_SHIFT) == ANSI_ISNI_TYPE_1) {
    proto_tree_add_uint(param_tree, hf_sccp_ansi_isni_netspec, tvb, offset,
			ANSI_ISNI_ROUTING_CONTROL_LENGTH, ti);
    offset += ANSI_ISNI_ROUTING_CONTROL_LENGTH;
  }

  while (offset < length) {

    network = tvb_get_guint8(tvb, offset);
    proto_tree_add_text(tree, tvb, offset, ANSI_NCM_LENGTH,
			"Network ID network: %d", network);
    offset++;

    network = tvb_get_guint8(tvb, offset);
    proto_tree_add_text(tree, tvb, offset, ANSI_NCM_LENGTH,
			"Network ID cluster: %d", network);
    offset++;
  }

}

/*  FUNCTION dissect_sccp_parameter():
 *  Dissect a parameter given its type, offset into tvb, and length.
 */
static guint16
dissect_sccp_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *sccp_tree,
		       proto_tree *tree, guint8 parameter_type, guint16 offset,
		       guint16 parameter_length)
{
    tvbuff_t *parameter_tvb;

    switch (parameter_type) {
    case PARAMETER_CALLED_PARTY_ADDRESS:
    case PARAMETER_CALLING_PARTY_ADDRESS:
    case PARAMETER_DATA:
    case PARAMETER_LONG_DATA:
    case PARAMETER_SOURCE_LOCAL_REFERENCE:
    case PARAMETER_DESTINATION_LOCAL_REFERENCE:
    case PARAMETER_RELEASE_CAUSE:
    case PARAMETER_RETURN_CAUSE:
    case PARAMETER_RESET_CAUSE:
    case PARAMETER_ERROR_CAUSE:
    case PARAMETER_REFUSAL_CAUSE:

      /*  These parameters must be dissected even if !sccp_tree (so that
       *  assoc information can be created).
       */
      break;

    default:
      if (!sccp_tree) return(parameter_length);

    }

    parameter_tvb = tvb_new_subset(tvb, offset, parameter_length, parameter_length);

    switch (parameter_type) {

    case PARAMETER_END_OF_OPTIONAL_PARAMETERS:
      proto_tree_add_text(sccp_tree, tvb, offset, parameter_length,
			  "End of Optional");
      break;

    case PARAMETER_DESTINATION_LOCAL_REFERENCE:
      dissect_sccp_dlr_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_SOURCE_LOCAL_REFERENCE:
      dissect_sccp_slr_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_CALLED_PARTY_ADDRESS:
      dissect_sccp_called_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_CALLING_PARTY_ADDRESS:
      dissect_sccp_calling_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_CLASS:
      dissect_sccp_class_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_SEGMENTING_REASSEMBLING:
      dissect_sccp_segmenting_reassembling_param(parameter_tvb, pinfo, sccp_tree,
						   parameter_length);
      break;

    case PARAMETER_RECEIVE_SEQUENCE_NUMBER:
      dissect_sccp_receive_sequence_number_param(parameter_tvb, pinfo, sccp_tree,
						 parameter_length);
      break;

    case PARAMETER_SEQUENCING_SEGMENTING:
      dissect_sccp_sequencing_segmenting_param(parameter_tvb, sccp_tree,
					       parameter_length);
      break;

    case PARAMETER_CREDIT:
      dissect_sccp_credit_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_RELEASE_CAUSE:
      dissect_sccp_release_cause_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_RETURN_CAUSE:
      dissect_sccp_return_cause_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_RESET_CAUSE:
      dissect_sccp_reset_cause_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_ERROR_CAUSE:
      dissect_sccp_error_cause_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_REFUSAL_CAUSE:
      dissect_sccp_refusal_cause_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_DATA:
      dissect_sccp_data_param(parameter_tvb, pinfo, tree);

      /* TODO? Re-adjust length of SCCP item since it may be sub-dissected */
      /* sccp_length = proto_item_get_len(sccp_item);
       * sccp_length -= parameter_length;
       * proto_item_set_len(sccp_item, sccp_length);
       */
      break;

    case PARAMETER_SEGMENTATION:
		dissect_sccp_segmentation_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      break;

    case PARAMETER_HOP_COUNTER:
		dissect_sccp_hop_counter_param(parameter_tvb, sccp_tree, parameter_length);
      break;

    case PARAMETER_IMPORTANCE:
      if (decode_mtp3_standard != ANSI_STANDARD)
		  dissect_sccp_importance_param(parameter_tvb, pinfo, sccp_tree, parameter_length);
      else
		  dissect_sccp_unknown_param(parameter_tvb, sccp_tree, parameter_type,
				   parameter_length);
      break;

    case PARAMETER_LONG_DATA:
      dissect_sccp_data_param(parameter_tvb, pinfo, tree);
      break;

    case PARAMETER_ISNI:
      if (decode_mtp3_standard != ANSI_STANDARD)
		  dissect_sccp_unknown_param(parameter_tvb, sccp_tree, parameter_type,
				   parameter_length);
      else
		  dissect_sccp_isni_param(parameter_tvb, sccp_tree, parameter_length);
      break;

    default:
		dissect_sccp_unknown_param(parameter_tvb, sccp_tree, parameter_type,
				 parameter_length);
      break;
    }

    return(parameter_length);
}

/*  FUNCTION dissect_sccp_variable_parameter():
 *  Dissect a variable parameter given its type and offset into tvb.  Length
 *  of the parameter is gotten from tvb[0].
 *  Length returned is sum of (length + parameter).
 */
static guint16
dissect_sccp_variable_parameter(tvbuff_t *tvb, packet_info *pinfo,
				proto_tree *sccp_tree, proto_tree *tree,
				guint8 parameter_type, guint16 offset)
{
  guint16 parameter_length;
  guint8 length_length;

  if (parameter_type != PARAMETER_LONG_DATA)
  {
    parameter_length = tvb_get_guint8(tvb, offset);
    length_length = PARAMETER_LENGTH_LENGTH;
  }
  else
  {
    /* Long data parameter has 16 bit length */
    parameter_length = tvb_get_letohs(tvb, offset);
    length_length = PARAMETER_LONG_DATA_LENGTH_LENGTH;
  }

  if (sccp_tree && sccp_show_length)
  {
    proto_tree_add_text(sccp_tree, tvb, offset, length_length,
			"%s length: %d",
			val_to_str(parameter_type, sccp_parameter_values,
				   "Unknown"),
			parameter_length);
  }

  offset += length_length;

  dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree, parameter_type, offset,
			 parameter_length);

  return(parameter_length + length_length);
}

/*  FUNCTION dissect_sccp_optional_parameters():
 *  Dissect all the optional parameters given the start of the optional
 *  parameters into tvb.  Parameter types and lengths are read from tvb.
 */
static void
dissect_sccp_optional_parameters(tvbuff_t *tvb, packet_info *pinfo,
				 proto_tree *sccp_tree, proto_tree *tree,
				 guint16 offset)
{
  guint8 parameter_type;

  while ((parameter_type = tvb_get_guint8(tvb, offset)) !=
	 PARAMETER_END_OF_OPTIONAL_PARAMETERS) {

    offset += PARAMETER_TYPE_LENGTH;
    offset += dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
					      parameter_type, offset);
  }

  /* Process end of optional parameters */
  dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree, parameter_type, offset,
			 END_OF_OPTIONAL_PARAMETERS_LENGTH);

}

static sccp_msg_info_t* new_ud_msg(packet_info* pinfo, guint32 msg_type _U_) {
	sccp_msg_info_t* m = ep_alloc0(sizeof(sccp_msg_info_t));
	m->framenum = pinfo->fd->num;
	m->data.ud.calling_gt = NULL;
	m->data.ud.called_gt = NULL;

	register_frame_end_routine(reset_sccp_assoc);
	return m;
}

static void
dissect_sccp_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *sccp_tree,
		     proto_tree *tree)
{
  guint16 variable_pointer1 = 0, variable_pointer2 = 0, variable_pointer3 = 0;
  guint16 optional_pointer = 0, orig_opt_ptr = 0;
  guint16 offset = 0;
  gboolean   save_fragmented;
  tvbuff_t *new_tvb = NULL;
  fragment_data *frag_msg = NULL;
  guint32 source_local_ref=0;
  guint8 more;
  guint msg_offset = tvb_offset_from_real_beginning(tvb);

/* Macro for getting pointer to mandatory variable parameters */
#define VARIABLE_POINTER(var, hf_var, ptr_size) \
    if (ptr_size == POINTER_LENGTH) \
	var = tvb_get_guint8(tvb, offset); \
    else \
	var = tvb_get_letohs(tvb, offset); \
    proto_tree_add_uint(sccp_tree, hf_var, tvb, \
			offset, ptr_size, var); \
    var += offset; \
    if (ptr_size == POINTER_LENGTH_LONG) \
	var += 1; \
    offset += ptr_size;

/* Macro for getting pointer to optional parameters */
#define OPTIONAL_POINTER(ptr_size) \
    if (ptr_size == POINTER_LENGTH) \
	orig_opt_ptr = optional_pointer = tvb_get_guint8(tvb, offset); \
    else \
	orig_opt_ptr = optional_pointer = tvb_get_letohs(tvb, offset); \
    proto_tree_add_uint(sccp_tree, hf_sccp_optional_pointer, tvb, \
			offset, ptr_size, optional_pointer); \
    optional_pointer += offset; \
    if (ptr_size == POINTER_LENGTH_LONG) \
	optional_pointer += 1; \
    offset += ptr_size;


  /* Extract the message type;  all other processing is based on this */
  message_type   = tvb_get_guint8(tvb, SCCP_MSG_TYPE_OFFSET);
  offset = SCCP_MSG_TYPE_LENGTH;

  if (check_col(pinfo->cinfo, COL_INFO)) {
    /*  Do not change col_add_fstr() to col_append_fstr() here: we _want_
     *  this call to overwrite whatever's currently in the INFO column (e.g.,
     *  "DATA" from the SCTP dissector).
     *
     *  If there's something there that should not be overwritten, whoever
     *  put that info there should call col_set_fence() to protect it.
     */
    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
		 val_to_str(message_type, sccp_message_type_acro_values, "Unknown"));
  };

  if (sccp_tree) {
    /* add the message type to the protocol tree */
    proto_tree_add_uint(sccp_tree, hf_sccp_message_type, tvb,
			SCCP_MSG_TYPE_OFFSET, SCCP_MSG_TYPE_LENGTH, message_type);

  };

  /* Starting a new message dissection; clear the global assoc, SLR, and DLR values */
  dlr = INVALID_LR;
  slr = INVALID_LR;
  assoc = NULL;

  no_assoc.calling_dpc = 0;
  no_assoc.called_dpc = 0;
  no_assoc.calling_ssn = INVALID_SSN;
  no_assoc.called_ssn = INVALID_SSN;
  no_assoc.has_fw_key = FALSE;
  no_assoc.has_bw_key = FALSE;
  no_assoc.payload = SCCP_PLOAD_NONE;
  no_assoc.called_party = NULL;
  no_assoc.calling_party = NULL;
  no_assoc.extra_info = NULL;

  switch(message_type) {
  case SCCP_MSG_TYPE_CR:
  /*  TTC and NTT (Japan) say that the connection-oriented messages are
   *  deleted (not standardized), but they appear to be used anyway, so
   *  we'll dissect it...
   */
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SOURCE_LOCAL_REFERENCE,
				     offset, SOURCE_LOCAL_REFERENCE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_CLASS, offset,
				     PROTOCOL_CLASS_LENGTH);
    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    VARIABLE_POINTER(variable_pointer1, hf_sccp_variable_pointer1, POINTER_LENGTH)
    OPTIONAL_POINTER(POINTER_LENGTH)

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLED_PARTY_ADDRESS,
				    variable_pointer1);
    break;

  case SCCP_MSG_TYPE_CC:
    /*  TODO: connection has been established;  theoretically we could keep
     *  keep track of the SLR/DLR with the called/calling from the CR and
     *  track the connection (e.g., on subsequent messages regarding this
     *  SLR we could set the global vars "call*_ssn" so data could get
     *  sub-dissected).
     */
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SOURCE_LOCAL_REFERENCE,
				     offset, SOURCE_LOCAL_REFERENCE_LENGTH);

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_CLASS, offset,
				     PROTOCOL_CLASS_LENGTH);
    OPTIONAL_POINTER(POINTER_LENGTH);
    break;

  case SCCP_MSG_TYPE_CREF:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_REFUSAL_CAUSE, offset,
				     REFUSAL_CAUSE_LENGTH);
    OPTIONAL_POINTER(POINTER_LENGTH);
    break;

  case SCCP_MSG_TYPE_RLSD:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SOURCE_LOCAL_REFERENCE,
				     offset, SOURCE_LOCAL_REFERENCE_LENGTH);

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_RELEASE_CAUSE, offset,
				     RELEASE_CAUSE_LENGTH);

    OPTIONAL_POINTER(POINTER_LENGTH);
    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);
    break;

  case SCCP_MSG_TYPE_RLC:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SOURCE_LOCAL_REFERENCE,
				     offset, SOURCE_LOCAL_REFERENCE_LENGTH);

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);
    break;

  case SCCP_MSG_TYPE_DT1:
    source_local_ref = tvb_get_letoh24(tvb, offset);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    more = tvb_get_guint8(tvb, offset) & SEGMENTING_REASSEMBLING_MASK;

    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SEGMENTING_REASSEMBLING,
				     offset, SEGMENTING_REASSEMBLING_LENGTH);
    VARIABLE_POINTER(variable_pointer1, hf_sccp_variable_pointer1, POINTER_LENGTH)

    /* Reasemble */
    if (!sccp_xudt_desegment) {
		proto_tree_add_text(sccp_tree, tvb, variable_pointer1,
					tvb_get_guint8(tvb, variable_pointer1)+1,
					"Segmented Data");
		dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
						PARAMETER_DATA, variable_pointer1);

    } else {
		save_fragmented = pinfo->fragmented;
		pinfo->fragmented = TRUE;
		frag_msg = fragment_add_seq_next(tvb, variable_pointer1 + 1, pinfo,
					 source_local_ref,						/* ID for fragments belonging together */
					 sccp_xudt_msg_fragment_table,			/* list of message fragments */
					 sccp_xudt_msg_reassembled_table,		/* list of reassembled messages */
					 tvb_get_guint8(tvb,variable_pointer1),	/* fragment length - to the end */
					 more);									/* More fragments? */

		new_tvb = process_reassembled_data(tvb, variable_pointer1 + 1, pinfo,
						   "Reassembled SCCP", frag_msg,
						   &sccp_xudt_msg_frag_items, NULL,
						   tree);

		if (frag_msg && frag_msg->next) { /* Reassembled */
			col_append_str(pinfo->cinfo, COL_INFO,
					   "(Message reassembled) ");
		} else if (more) { /* Not last packet of reassembled message */
			col_append_str(pinfo->cinfo, COL_INFO, "(Message fragment) ");
		}

		pinfo->fragmented = save_fragmented;

		if (new_tvb)
			dissect_sccp_data_param(new_tvb, pinfo, tree);
    }

    /* End reassemble */
    break;

  case SCCP_MSG_TYPE_DT2:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SEQUENCING_SEGMENTING, offset,
				     SEQUENCING_SEGMENTING_LENGTH);
    break;

  case SCCP_MSG_TYPE_AK:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_RECEIVE_SEQUENCE_NUMBER,
				     offset, RECEIVE_SEQUENCE_NUMBER_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_CREDIT, offset, CREDIT_LENGTH);
    break;

  case SCCP_MSG_TYPE_UDT:
    pinfo->sccp_info = sccp_msg = new_ud_msg(pinfo,message_type);

    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_CLASS, offset,
				     PROTOCOL_CLASS_LENGTH);
    VARIABLE_POINTER(variable_pointer1, hf_sccp_variable_pointer1, POINTER_LENGTH)
    VARIABLE_POINTER(variable_pointer2, hf_sccp_variable_pointer2, POINTER_LENGTH)
    VARIABLE_POINTER(variable_pointer3, hf_sccp_variable_pointer3, POINTER_LENGTH)

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLED_PARTY_ADDRESS,
				    variable_pointer1);
    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLING_PARTY_ADDRESS,
				    variable_pointer2);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree, PARAMETER_DATA,
				    variable_pointer3);
    break;

  case SCCP_MSG_TYPE_UDTS:
    pinfo->sccp_info =  sccp_msg = new_ud_msg(pinfo,message_type);

    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_RETURN_CAUSE, offset,
				     RETURN_CAUSE_LENGTH);

    VARIABLE_POINTER(variable_pointer1, hf_sccp_variable_pointer1, POINTER_LENGTH)
    VARIABLE_POINTER(variable_pointer2, hf_sccp_variable_pointer2, POINTER_LENGTH)
    VARIABLE_POINTER(variable_pointer3, hf_sccp_variable_pointer3, POINTER_LENGTH)

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLED_PARTY_ADDRESS,
				    variable_pointer1);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLING_PARTY_ADDRESS,
				    variable_pointer2);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree, PARAMETER_DATA,
				    variable_pointer3);
    break;

  case SCCP_MSG_TYPE_ED:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    VARIABLE_POINTER(variable_pointer1, hf_sccp_variable_pointer1, POINTER_LENGTH);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree, PARAMETER_DATA,
				    variable_pointer1);
    break;

  case SCCP_MSG_TYPE_EA:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);
    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);
    break;

  case SCCP_MSG_TYPE_RSR:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SOURCE_LOCAL_REFERENCE,
				     offset, SOURCE_LOCAL_REFERENCE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_RESET_CAUSE, offset,
				     RESET_CAUSE_LENGTH);
    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);
    break;

  case SCCP_MSG_TYPE_RSC:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SOURCE_LOCAL_REFERENCE,
				     offset, SOURCE_LOCAL_REFERENCE_LENGTH);
    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);
    break;

  case SCCP_MSG_TYPE_ERR:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_ERROR_CAUSE, offset,
				     ERROR_CAUSE_LENGTH);
    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);
    break;

  case SCCP_MSG_TYPE_IT:
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_DESTINATION_LOCAL_REFERENCE,
				     offset,
				     DESTINATION_LOCAL_REFERENCE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SOURCE_LOCAL_REFERENCE,
				     offset, SOURCE_LOCAL_REFERENCE_LENGTH);
    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_CLASS, offset,
				     PROTOCOL_CLASS_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_SEQUENCING_SEGMENTING,
				     offset, SEQUENCING_SEGMENTING_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_CREDIT, offset, CREDIT_LENGTH);
    break;

  case SCCP_MSG_TYPE_XUDT:
    pinfo->sccp_info =  sccp_msg = new_ud_msg(pinfo,message_type);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_CLASS, offset,
				     PROTOCOL_CLASS_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_HOP_COUNTER, offset,
				     HOP_COUNTER_LENGTH);

    VARIABLE_POINTER(variable_pointer1, hf_sccp_variable_pointer1, POINTER_LENGTH)
    VARIABLE_POINTER(variable_pointer2, hf_sccp_variable_pointer2, POINTER_LENGTH)
    VARIABLE_POINTER(variable_pointer3, hf_sccp_variable_pointer3, POINTER_LENGTH)
    OPTIONAL_POINTER(POINTER_LENGTH)

    /*  Optional parameters are Segmentation and Importance
     *  NOTE 2 - Segmentation Should not be present in case of a single XUDT
     *  message.
     */

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLED_PARTY_ADDRESS,
				    variable_pointer1);
    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLING_PARTY_ADDRESS,
				    variable_pointer2);

    if (tvb_get_guint8(tvb, optional_pointer) == PARAMETER_SEGMENTATION){
		if (!sccp_xudt_desegment){
			proto_tree_add_text(sccp_tree, tvb, variable_pointer3, tvb_get_guint8(tvb, variable_pointer3)+1, "Segmented Data");
		} else {
			guint8 octet;
			gboolean more_frag = TRUE;


		    /* Get the first octet of parameter Segmentation, Ch 3.17 in Q.713
		     * Bit 8 of octet 1 is used for First segment indication
		     * Bit 7 of octet 1 is used to keep in the message in sequence
		     *	     delivery option required by the SCCP user
		     * Bits 6 and 5 in octet 1 are spare bits.
		     * Bits 4-1 of octet 1 are used to indicate the number of
		     *		remaining segments.
		     * The values 0000 to 1111 are possible; the value 0000 indicates
		     * the last segment.
		     */
		    octet = tvb_get_guint8(tvb,optional_pointer+2);
		    source_local_ref = tvb_get_letoh24(tvb, optional_pointer+3);

		    if ((octet&0x0f) == 0)
				more_frag = FALSE;

			save_fragmented = pinfo->fragmented;
			pinfo->fragmented = TRUE;
			frag_msg = fragment_add_seq_next(tvb, variable_pointer3 + 1, pinfo,
				source_local_ref,				/* ID for fragments belonging together */
				sccp_xudt_msg_fragment_table,		/* list of message fragments */
				sccp_xudt_msg_reassembled_table,		/* list of reassembled messages */
				tvb_get_guint8(tvb,variable_pointer3),	/* fragment length - to the end */
				more_frag);					/* More fragments? */

			if ((octet&0x80) == 0x80) /*First segment, set number of segments*/
				fragment_set_tot_len(pinfo, source_local_ref, sccp_xudt_msg_fragment_table,(octet & 0xf));

			new_tvb = process_reassembled_data(tvb, variable_pointer3 + 1,
					       pinfo, "Reassembled SCCP",
					       frag_msg,
					       &sccp_xudt_msg_frag_items,
					       NULL, tree);

			if (frag_msg) { /* Reassembled */
				col_append_str(pinfo->cinfo, COL_INFO,"(Message reassembled) ");
			} else { /* Not last packet of reassembled message */
				col_append_str(pinfo->cinfo, COL_INFO,"(Message fragment) ");
			}

			pinfo->fragmented = save_fragmented;

			if (new_tvb)
				dissect_sccp_data_param(new_tvb, pinfo, tree);
		}
    } else {
		dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
					PARAMETER_DATA, variable_pointer3);
    }
    break;

  case SCCP_MSG_TYPE_XUDTS:
    pinfo->sccp_info =  sccp_msg = new_ud_msg(pinfo,message_type);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_RETURN_CAUSE, offset,
				     RETURN_CAUSE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_HOP_COUNTER, offset,
				     HOP_COUNTER_LENGTH);

    VARIABLE_POINTER(variable_pointer1, hf_sccp_variable_pointer1, POINTER_LENGTH)
    VARIABLE_POINTER(variable_pointer2, hf_sccp_variable_pointer2, POINTER_LENGTH)
    VARIABLE_POINTER(variable_pointer3, hf_sccp_variable_pointer3, POINTER_LENGTH)
    OPTIONAL_POINTER(POINTER_LENGTH)

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLED_PARTY_ADDRESS,
				    variable_pointer1);
    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLING_PARTY_ADDRESS,
				    variable_pointer2);

    if (tvb_get_guint8(tvb, optional_pointer) == PARAMETER_SEGMENTATION){
	if (!sccp_xudt_desegment){
	    proto_tree_add_text(sccp_tree, tvb, variable_pointer3, tvb_get_guint8(tvb, variable_pointer3)+1, "Segmented Data");

	} else {
	    guint8 octet;
	    gboolean more_frag = TRUE;


	    /* Get the first octet of parameter Segmentation, Ch 3.17 in Q.713
	     * Bit 8 of octet 1 is used for First segment indication
	     * Bit 7 of octet 1 is used to keep in the message in sequence
	     *	     delivery option required by the SCCP user
	     * Bits 6 and 5 in octet 1 are spare bits.
	     * Bits 4-1 of octet 1 are used to indicate the number of
	     *		remaining segments.
	     * The values 0000 to 1111 are possible; the value 0000 indicates
	     * the last segment.
	     */
	    octet = tvb_get_guint8(tvb,optional_pointer+2);
	    source_local_ref = tvb_get_letoh24(tvb, optional_pointer+3);

	    if ((octet&0x0f) == 0)
		more_frag = FALSE;

	    save_fragmented = pinfo->fragmented;
	    pinfo->fragmented = TRUE;
	    frag_msg = fragment_add_seq_next(tvb, variable_pointer3 + 1, pinfo,
		    source_local_ref,				/* ID for fragments belonging together */
		    sccp_xudt_msg_fragment_table,		/* list of message fragments */
		    sccp_xudt_msg_reassembled_table,		/* list of reassembled messages */
		    tvb_get_guint8(tvb,variable_pointer3),	/* fragment length - to the end */
		    more_frag);					/* More fragments? */

	    if ((octet&0x80) == 0x80) /*First segment, set number of segments*/
		fragment_set_tot_len(pinfo, source_local_ref, sccp_xudt_msg_fragment_table,(octet & 0xf));

	    new_tvb = process_reassembled_data(tvb, variable_pointer3 + 1,
					       pinfo, "Reassembled SCCP",
					       frag_msg,
					       &sccp_xudt_msg_frag_items,
					       NULL, tree);

	    if (frag_msg) { /* Reassembled */
		col_append_str(pinfo->cinfo, COL_INFO,
				   "(Message reassembled) ");
	    } else { /* Not last packet of reassembled message */
		col_append_str(pinfo->cinfo, COL_INFO,
				    "(Message fragment) ");
	    }

	    pinfo->fragmented = save_fragmented;

	    if (new_tvb)
		dissect_sccp_data_param(new_tvb, pinfo, tree);
	}
    } else {
	dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
					PARAMETER_DATA, variable_pointer3);
    }
    break;

  case SCCP_MSG_TYPE_LUDT:
    pinfo->sccp_info =  sccp_msg = new_ud_msg(pinfo,message_type);

    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_CLASS, offset,
				     PROTOCOL_CLASS_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_HOP_COUNTER, offset,
				     HOP_COUNTER_LENGTH);

    VARIABLE_POINTER(variable_pointer1, hf_sccp_variable_pointer1, POINTER_LENGTH_LONG)
    VARIABLE_POINTER(variable_pointer2, hf_sccp_variable_pointer2, POINTER_LENGTH_LONG)
    VARIABLE_POINTER(variable_pointer3, hf_sccp_variable_pointer3, POINTER_LENGTH_LONG)
    OPTIONAL_POINTER(POINTER_LENGTH_LONG)

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLED_PARTY_ADDRESS,
				    variable_pointer1);
    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLING_PARTY_ADDRESS,
				    variable_pointer2);
    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_LONG_DATA, variable_pointer3);
    break;

  case SCCP_MSG_TYPE_LUDTS:
    pinfo->sccp_info =  sccp_msg = new_ud_msg(pinfo,message_type);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_RETURN_CAUSE, offset,
				     RETURN_CAUSE_LENGTH);
    offset += dissect_sccp_parameter(tvb, pinfo, sccp_tree, tree,
				     PARAMETER_HOP_COUNTER, offset,
				     HOP_COUNTER_LENGTH);

    VARIABLE_POINTER(variable_pointer1, hf_sccp_variable_pointer1, POINTER_LENGTH_LONG)
    VARIABLE_POINTER(variable_pointer2, hf_sccp_variable_pointer2, POINTER_LENGTH_LONG)
    VARIABLE_POINTER(variable_pointer3, hf_sccp_variable_pointer3, POINTER_LENGTH_LONG)
    OPTIONAL_POINTER(POINTER_LENGTH_LONG)

    assoc = get_sccp_assoc(pinfo, msg_offset,  slr, dlr, message_type);

    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLED_PARTY_ADDRESS,
				    variable_pointer1);
    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_CALLING_PARTY_ADDRESS,
				    variable_pointer2);
    dissect_sccp_variable_parameter(tvb, pinfo, sccp_tree, tree,
				    PARAMETER_LONG_DATA, variable_pointer3);
    break;

  default:
    dissect_sccp_unknown_message(tvb, sccp_tree);
  }

  if (orig_opt_ptr)
    dissect_sccp_optional_parameters(tvb, pinfo, sccp_tree, tree,
				     optional_pointer);

  if (trace_sccp && assoc && assoc != &no_assoc) {
	  proto_item* pi = proto_tree_add_uint(sccp_tree,hf_sccp_assoc_id,tvb,0,0,assoc->id);
	  proto_tree* pt = proto_item_add_subtree(pi,ett_sccp_assoc);
	  PROTO_ITEM_SET_GENERATED(pi);
	  if (assoc->msgs) {
		sccp_msg_info_t* m;
		  for(m = assoc->msgs; m ; m = m->data.co.next) {
			pi = proto_tree_add_uint( pt,hf_sccp_assoc_msg,tvb,0,0,m->framenum);

			if (assoc->payload != SCCP_PLOAD_NONE)
				proto_item_append_text(pi," %s", val_to_str(assoc->payload, assoc_protos, "Unknown"));

			if (m->data.co.label)
				proto_item_append_text(pi," %s", m->data.co.label);

			if (m->framenum == pinfo->fd->num && m->offset == msg_offset ) {
				tap_queue_packet(sccp_tap, pinfo, m);
				proto_item_append_text(pi," (current)");
			}
			PROTO_ITEM_SET_GENERATED(pi);
		  }
	  }
  }

}

static void
dissect_sccp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
  proto_item *sccp_item = NULL;
  proto_tree *sccp_tree = NULL;
  const mtp3_addr_pc_t *mtp3_addr_p;

  if ((pinfo->src.type == AT_SS7PC) &&
    ((mtp3_addr_p = (const mtp3_addr_pc_t *)pinfo->src.data)->type <= CHINESE_ITU_STANDARD))
  {
    /*
     *  Allow a protocol beneath to specify how the SCCP layer should be
     *  dissected.
     *
     *  It is possible to have multiple sets of SCCP traffic some of which is
     *  ITU and some of which is ANSI.
     *  An example is A-interface traffic having ANSI MTP3/ANSI SCCP/3GPP2 IOS
     *  and at the same time ITU MTP3/ITU SCCP/ANSI TCAP/ANSI MAP.
     */
    decode_mtp3_standard = mtp3_addr_p->type;
  }
  else
  {
    decode_mtp3_standard = mtp3_standard;
  }

  /* Make entry in the Protocol column on summary display */
  if (check_col(pinfo->cinfo, COL_PROTOCOL))
    switch(decode_mtp3_standard) {
      case ITU_STANDARD:
	col_set_str(pinfo->cinfo, COL_PROTOCOL, "SCCP (Int. ITU)");
	break;
      case ANSI_STANDARD:
	col_set_str(pinfo->cinfo, COL_PROTOCOL, "SCCP (ANSI)");
	break;
      case CHINESE_ITU_STANDARD:
	col_set_str(pinfo->cinfo, COL_PROTOCOL, "SCCP (Chin. ITU)");
	break;
      case JAPAN_STANDARD:
	col_set_str(pinfo->cinfo, COL_PROTOCOL, "SCCP (Japan)");
	break;
    };

  /* In the interest of speed, if "tree" is NULL, don't do any work not
     necessary to generate protocol tree items. */
  if (tree) {
    /* create the sccp protocol tree */
    sccp_item = proto_tree_add_item(tree, proto_sccp, tvb, 0, -1, FALSE);
    sccp_tree = proto_item_add_subtree(sccp_item, ett_sccp);
  }

  /* Set whether message is UPLINK, DOWNLINK, or of UNKNOWN direction */

  if (pinfo->src.type == AT_SS7PC)
  {
    /*
     * XXX - we assume that the "data" pointers of the source and destination
     * addresses are set to point to "mtp3_addr_pc_t" structures, so that
     * we can safely cast them.
     */
    mtp3_addr_p = (const mtp3_addr_pc_t *)pinfo->src.data;

    if (sccp_source_pc_global == mtp3_addr_p->pc)
    {
       pinfo->p2p_dir = P2P_DIR_SENT;
    }
    else
    {
      /* assuming if src was SS7 PC then dst will be too */
      mtp3_addr_p = (const mtp3_addr_pc_t *)pinfo->dst.data;

      if (sccp_source_pc_global == mtp3_addr_p->pc)
      {
	 pinfo->p2p_dir = P2P_DIR_RECV;
      }
      else
      {
	 pinfo->p2p_dir = P2P_DIR_UNKNOWN;
      }
    }
  }

  /* dissect the message */
  dissect_sccp_message(tvb, pinfo, sccp_tree, tree);

}

/*** SccpUsers Table **/

static struct _sccp_ul {
	guint id;
	gboolean uses_tcap;
	dissector_handle_t* handlep;
	} user_list[] = {
	{SCCP_USER_DATA,FALSE,&data_handle},
	{SCCP_USER_TCAP,FALSE,&tcap_handle},
	{SCCP_USER_RANAP,FALSE,&ranap_handle},
	{SCCP_USER_BSSAP,FALSE,&bssap_handle},
	{SCCP_USER_GSMMAP,TRUE,&gsmmap_handle},
	{SCCP_USER_CAMEL,TRUE,&camel_handle},
	{SCCP_USER_INAP,TRUE,&inap_handle},
	{0,FALSE,NULL}
};

static void sccp_users_update_cb(void* r, const char** err _U_) {
	sccp_user_t* u = r;
	struct _sccp_ul* c;

	for (c=user_list; c->handlep; c++) {
		if (c->id == u->user) {
			u->uses_tcap = c->uses_tcap;
			u->handlep = c->handlep;
			return;
		}
	}

	u->uses_tcap = FALSE;
	u->handlep = &data_handle;
}

static void* sccp_users_copy_cb(void* n, const void* o, size_t siz _U_) {
	const sccp_user_t* u = o;
	sccp_user_t* un = n;

	un->ni = u->ni;
	un->user = u->user;
	un->uses_tcap = u->uses_tcap;
	un->handlep = u->handlep;
	if (u->called_pc) un->called_pc = range_copy(u->called_pc);
	if (u->called_ssn) un->called_ssn = range_copy(u->called_ssn);

	return n;
}

static void sccp_users_free_cb(void*r) {
	sccp_user_t* u = r;
	if (u->called_pc) g_free(u->called_pc);
	if (u->called_ssn) g_free(u->called_ssn);
}


UAT_DEC_CB_DEF(sccp_users, ni, sccp_user_t)
UAT_RANGE_CB_DEF(sccp_users, called_pc,sccp_user_t)
UAT_RANGE_CB_DEF(sccp_users, called_ssn,sccp_user_t)
UAT_VS_DEF(sccp_users, user, sccp_user_t, SCCP_USER_DATA, "Data")

/** End SccpUsersTable **/


static void init_sccp(void) {
    next_assoc_id = 1;
    fragment_table_init (&sccp_xudt_msg_fragment_table);
    reassembled_table_init(&sccp_xudt_msg_reassembled_table);

}

/* Register the protocol with Wireshark */
void
proto_register_sccp(void)
{
  /* Setup list of header fields */
  static hf_register_info hf[] = {
    { &hf_sccp_message_type,
      { "Message Type", "sccp.message_type",
	FT_UINT8, BASE_HEX, VALS(sccp_message_type_values), 0x0,
	NULL, HFILL}},
    { &hf_sccp_variable_pointer1,
      { "Pointer to first Mandatory Variable parameter", "sccp.variable_pointer1",
	FT_UINT16, BASE_DEC, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_variable_pointer2,
      { "Pointer to second Mandatory Variable parameter", "sccp.variable_pointer2",
	FT_UINT16, BASE_DEC, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_variable_pointer3,
      { "Pointer to third Mandatory Variable parameter", "sccp.variable_pointer3",
	FT_UINT16, BASE_DEC, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_optional_pointer,
      { "Pointer to Optional parameter", "sccp.optional_pointer",
	FT_UINT16, BASE_DEC, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_ssn,
      { "Called or Calling SubSystem Number", "sccp.ssn",
	FT_UINT8, BASE_DEC, VALS(sccp_ssn_values), 0x0,
	NULL, HFILL}},
    { &hf_sccp_gt_digits,
      { "Called or Calling GT Digits",
	"sccp.digits",
	FT_STRING, BASE_NONE, NULL, 0x0,
	NULL, HFILL }},
    { &hf_sccp_called_national_indicator,
      { "National Indicator", "sccp.called.ni",
	FT_UINT8, BASE_HEX, VALS(sccp_national_indicator_values), ANSI_NATIONAL_MASK,
	NULL, HFILL}},
    { &hf_sccp_called_routing_indicator,
      { "Routing Indicator", "sccp.called.ri",
	FT_UINT8, BASE_HEX, VALS(sccp_routing_indicator_values), ROUTING_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_called_itu_global_title_indicator,
      { "Global Title Indicator", "sccp.called.gti",
	FT_UINT8, BASE_HEX, VALS(sccp_itu_global_title_indicator_values), GTI_MASK,
	NULL, HFILL}},
    { &hf_sccp_called_ansi_global_title_indicator,
      { "Global Title Indicator", "sccp.called.gti",
	FT_UINT8, BASE_HEX, VALS(sccp_ansi_global_title_indicator_values), GTI_MASK,
	NULL, HFILL}},
    { &hf_sccp_called_itu_ssn_indicator,
      { "SubSystem Number Indicator", "sccp.called.ssni",
	FT_UINT8, BASE_HEX, VALS(sccp_ai_ssni_values), ITU_SSN_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_called_itu_point_code_indicator,
      { "Point Code Indicator", "sccp.called.pci",
	FT_UINT8, BASE_HEX, VALS(sccp_ai_pci_values), ITU_PC_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_called_ansi_ssn_indicator,
      { "SubSystem Number Indicator", "sccp.called.ssni",
	FT_UINT8, BASE_HEX, VALS(sccp_ai_ssni_values), ANSI_SSN_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_called_ansi_point_code_indicator,
      { "Point Code Indicator", "sccp.called.pci",
	FT_UINT8, BASE_HEX, VALS(sccp_ai_pci_values), ANSI_PC_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_called_ssn,
      { "SubSystem Number", "sccp.called.ssn",
	FT_UINT8, BASE_DEC, VALS(sccp_ssn_values), 0x0,
	NULL, HFILL}},
    { &hf_sccp_called_itu_pc,
      { "PC", "sccp.called.pc",
	FT_UINT16, BASE_DEC, NULL, ITU_PC_MASK,
	NULL, HFILL}},
    { &hf_sccp_called_ansi_pc,
      { "PC", "sccp.called.ansi_pc",
	FT_STRING, BASE_NONE, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_called_chinese_pc,
      { "PC", "sccp.called.chinese_pc",
	FT_STRING, BASE_NONE, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_called_japan_pc,
      { "PC", "sccp.called.pc",
	FT_UINT16, BASE_DEC, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_called_pc_network,
      { "PC Network",
	"sccp.called.network",
	FT_UINT24, BASE_DEC, NULL, ANSI_NETWORK_MASK,
	NULL, HFILL }},
    { &hf_sccp_called_pc_cluster,
      { "PC Cluster",
	"sccp.called.cluster",
	FT_UINT24, BASE_DEC, NULL, ANSI_CLUSTER_MASK,
	NULL, HFILL }},
    { &hf_sccp_called_pc_member,
      { "PC Member",
	"sccp.called.member",
	FT_UINT24, BASE_DEC, NULL, ANSI_MEMBER_MASK,
	NULL, HFILL }},
    { &hf_sccp_called_gt_nai,
      { "Nature of Address Indicator",
	"sccp.called.nai",
	FT_UINT8, BASE_HEX, VALS(sccp_nai_values), GT_NAI_MASK,
	NULL, HFILL }},
    { &hf_sccp_called_gt_oe,
      { "Odd/Even Indicator",
	"sccp.called.oe",
	FT_UINT8, BASE_HEX, VALS(sccp_oe_values), GT_OE_MASK,
	NULL, HFILL }},
    { &hf_sccp_called_gt_tt,
      { "Translation Type",
	"sccp.called.tt",
	FT_UINT8, BASE_HEX, NULL, 0x0,
	NULL, HFILL }},
    { &hf_sccp_called_gt_np,
      { "Numbering Plan",
	"sccp.called.np",
	FT_UINT8, BASE_HEX, VALS(sccp_np_values), GT_NP_MASK,
	NULL, HFILL }},
    { &hf_sccp_called_gt_es,
      { "Encoding Scheme",
	"sccp.called.es",
	FT_UINT8, BASE_HEX, VALS(sccp_es_values), GT_ES_MASK,
	NULL, HFILL }},
    { &hf_sccp_called_gt_digits,
      { "GT Digits",
	"sccp.called.digits",
	FT_STRING, BASE_NONE, NULL, 0x0,
	NULL, HFILL }},
    { &hf_sccp_calling_national_indicator,
      { "National Indicator", "sccp.calling.ni",
	FT_UINT8, BASE_HEX, VALS(sccp_national_indicator_values), ANSI_NATIONAL_MASK,
	NULL, HFILL}},
    { &hf_sccp_calling_routing_indicator,
      { "Routing Indicator", "sccp.calling.ri",
	FT_UINT8, BASE_HEX, VALS(sccp_routing_indicator_values), ROUTING_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_calling_itu_global_title_indicator,
      { "Global Title Indicator", "sccp.calling.gti",
	FT_UINT8, BASE_HEX, VALS(sccp_itu_global_title_indicator_values), GTI_MASK,
	NULL, HFILL}},
    { &hf_sccp_calling_ansi_global_title_indicator,
      { "Global Title Indicator", "sccp.calling.gti",
	FT_UINT8, BASE_HEX, VALS(sccp_ansi_global_title_indicator_values), GTI_MASK,
	NULL, HFILL}},
    { &hf_sccp_calling_itu_ssn_indicator,
      { "SubSystem Number Indicator", "sccp.calling.ssni",
	FT_UINT8, BASE_HEX, VALS(sccp_ai_ssni_values), ITU_SSN_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_calling_itu_point_code_indicator,
      { "Point Code Indicator", "sccp.calling.pci",
	FT_UINT8, BASE_HEX, VALS(sccp_ai_pci_values), ITU_PC_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_calling_ansi_ssn_indicator,
      { "SubSystem Number Indicator", "sccp.calling.ssni",
	FT_UINT8, BASE_HEX, VALS(sccp_ai_ssni_values), ANSI_SSN_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_calling_ansi_point_code_indicator,
      { "Point Code Indicator", "sccp.calling.pci",
	FT_UINT8, BASE_HEX, VALS(sccp_ai_pci_values), ANSI_PC_INDICATOR_MASK,
	NULL, HFILL}},
    { &hf_sccp_calling_ssn,
      { "SubSystem Number", "sccp.calling.ssn",
	FT_UINT8, BASE_DEC, VALS(sccp_ssn_values), 0x0,
	NULL, HFILL}},
    { &hf_sccp_calling_itu_pc,
      { "PC", "sccp.calling.pc",
	FT_UINT16, BASE_DEC, NULL, ITU_PC_MASK,
	NULL, HFILL}},
    { &hf_sccp_calling_ansi_pc,
      { "PC", "sccp.calling.ansi_pc",
	FT_STRING, BASE_NONE, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_calling_chinese_pc,
      { "PC", "sccp.calling.chinese_pc",
	FT_STRING, BASE_NONE, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_calling_japan_pc,
      { "PC", "sccp.calling.pc",
	FT_UINT16, BASE_DEC, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_calling_pc_network,
      { "PC Network",
	"sccp.calling.network",
	FT_UINT24, BASE_DEC, NULL, ANSI_NETWORK_MASK,
	NULL, HFILL }},
    { &hf_sccp_calling_pc_cluster,
      { "PC Cluster",
	"sccp.calling.cluster",
	FT_UINT24, BASE_DEC, NULL, ANSI_CLUSTER_MASK,
	NULL, HFILL }},
    { &hf_sccp_calling_pc_member,
      { "PC Member",
	"sccp.calling.member",
	FT_UINT24, BASE_DEC, NULL, ANSI_MEMBER_MASK,
	NULL, HFILL }},
    { &hf_sccp_calling_gt_nai,
      { "Nature of Address Indicator",
	"sccp.calling.nai",
	FT_UINT8, BASE_HEX, VALS(sccp_nai_values), GT_NAI_MASK,
	NULL, HFILL }},
    { &hf_sccp_calling_gt_oe,
      { "Odd/Even Indicator",
	"sccp.calling.oe",
	FT_UINT8, BASE_HEX, VALS(sccp_oe_values), GT_OE_MASK,
	NULL, HFILL }},
    { &hf_sccp_calling_gt_tt,
      { "Translation Type",
	"sccp.calling.tt",
	FT_UINT8, BASE_HEX, NULL, 0x0,
	NULL, HFILL }},
    { &hf_sccp_calling_gt_np,
      { "Numbering Plan",
	"sccp.calling.np",
	FT_UINT8, BASE_HEX, VALS(sccp_np_values), GT_NP_MASK,
	NULL, HFILL }},
    { &hf_sccp_calling_gt_es,
      { "Encoding Scheme",
	"sccp.calling.es",
	FT_UINT8, BASE_HEX, VALS(sccp_es_values), GT_ES_MASK,
	NULL, HFILL }},
    { &hf_sccp_calling_gt_digits,
      { "GT Digits",
	"sccp.calling.digits",
	FT_STRING, BASE_NONE, NULL, 0x0,
	NULL, HFILL }},
    { &hf_sccp_dlr,
      { "Destination Local Reference", "sccp.dlr",
	FT_UINT24, BASE_HEX, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_slr,
      { "Source Local Reference", "sccp.slr",
	FT_UINT24, BASE_HEX, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_lr,
    { "Local Reference", "sccp.lr",
      FT_UINT24, BASE_HEX, NULL, 0x0,
      NULL, HFILL}},
    { &hf_sccp_class,
      { "Class", "sccp.class",
	FT_UINT8, BASE_HEX, NULL, CLASS_CLASS_MASK,
	NULL, HFILL}},
    { &hf_sccp_handling,
      { "Message handling", "sccp.handling",
	FT_UINT8, BASE_HEX, VALS(sccp_class_handling_values), CLASS_SPARE_HANDLING_MASK,
	NULL, HFILL}},
    { &hf_sccp_more,
      { "More data", "sccp.more",
	FT_UINT8, BASE_HEX, VALS(sccp_segmenting_reassembling_values), SEGMENTING_REASSEMBLING_MASK,
	NULL, HFILL}},
    { &hf_sccp_rsn,
      { "Receive Sequence Number", "sccp.rsn",
	FT_UINT8, BASE_HEX, NULL, RSN_MASK,
	NULL, HFILL}},
    { &hf_sccp_sequencing_segmenting_ssn,
      { "Sequencing Segmenting: Send Sequence Number", "sccp.sequencing_segmenting.ssn",
	FT_UINT8, BASE_HEX, NULL, SEND_SEQUENCE_NUMBER_MASK,
	NULL, HFILL}},
    { &hf_sccp_sequencing_segmenting_rsn,
      { "Sequencing Segmenting: Receive Sequence Number", "sccp.sequencing_segmenting.rsn",
	FT_UINT8, BASE_HEX, NULL, RECEIVE_SEQUENCE_NUMBER_MASK,
	NULL, HFILL}},
    { &hf_sccp_sequencing_segmenting_more,
      { "Sequencing Segmenting: More", "sccp.sequencing_segmenting.more",
	FT_UINT8, BASE_HEX, VALS(sccp_segmenting_reassembling_values), SEQUENCING_SEGMENTING_MORE_MASK,
	NULL, HFILL}},
    { &hf_sccp_credit,
      { "Credit", "sccp.credit",
	FT_UINT8, BASE_HEX, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_release_cause,
      { "Release Cause", "sccp.release_cause",
	FT_UINT8, BASE_HEX, VALS(sccp_release_cause_values), 0x0,
	NULL, HFILL}},
    { &hf_sccp_return_cause,
      { "Return Cause", "sccp.return_cause",
	FT_UINT8, BASE_HEX, VALS(sccp_return_cause_values), 0x0,
	NULL, HFILL}},
    { &hf_sccp_reset_cause,
      { "Reset Cause", "sccp.reset_cause",
	FT_UINT8, BASE_HEX, VALS(sccp_reset_cause_values), 0x0,
	NULL, HFILL}},
    { &hf_sccp_error_cause,
      { "Error Cause", "sccp.error_cause",
	FT_UINT8, BASE_HEX, VALS(sccp_error_cause_values), 0x0,
	NULL, HFILL}},
    { &hf_sccp_refusal_cause,
      { "Refusal Cause", "sccp.refusal_cause",
	FT_UINT8, BASE_HEX, VALS(sccp_refusal_cause_values), 0x0,
	NULL, HFILL}},
    { &hf_sccp_segmentation_first,
      { "Segmentation: First", "sccp.segmentation.first",
	FT_UINT8, BASE_HEX, VALS(sccp_segmentation_first_segment_values), SEGMENTATION_FIRST_SEGMENT_MASK,
	NULL, HFILL}},
    { &hf_sccp_segmentation_class,
      { "Segmentation: Class", "sccp.segmentation.class",
	FT_UINT8, BASE_HEX, VALS(sccp_segmentation_class_values), SEGMENTATION_CLASS_MASK,
	NULL, HFILL}},
    { &hf_sccp_segmentation_remaining,
      { "Segmentation: Remaining", "sccp.segmentation.remaining",
	FT_UINT8, BASE_HEX, NULL, SEGMENTATION_REMAINING_MASK,
	NULL, HFILL}},
    { &hf_sccp_segmentation_slr,
      { "Segmentation: Source Local Reference", "sccp.segmentation.slr",
	FT_UINT24, BASE_HEX, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_hop_counter,
      { "Hop Counter", "sccp.hops",
	FT_UINT8, BASE_HEX, NULL, 0x0,
	NULL, HFILL}},
    { &hf_sccp_importance,
      { "Importance", "sccp.importance",
	FT_UINT8, BASE_HEX, NULL, IMPORTANCE_IMPORTANCE_MASK,
	NULL, HFILL}},
    /* ISNI is ANSI only */
    { &hf_sccp_ansi_isni_mi,
      { "ISNI Mark for Identification Indicator", "sccp.isni.mi",
	FT_UINT8, BASE_HEX, VALS(sccp_isni_mark_for_id_values), ANSI_ISNI_MI_MASK,
	NULL, HFILL}},
    { &hf_sccp_ansi_isni_iri,
      { "ISNI Routing Indicator", "sccp.isni.iri",
	FT_UINT8, BASE_HEX, VALS(sccp_isni_iri_values), ANSI_ISNI_IRI_MASK,
	NULL, HFILL}},
    { &hf_sccp_ansi_isni_ti,
      { "ISNI Type Indicator", "sccp.isni.ti",
	FT_UINT8, BASE_HEX, VALS(sccp_isni_ti_values), ANSI_ISNI_TI_MASK,
	NULL, HFILL}},
    { &hf_sccp_ansi_isni_netspec,
      { "ISNI Network Specific (Type 1)", "sccp.isni.netspec",
	FT_UINT8, BASE_HEX, NULL, ANSI_ISNI_NETSPEC_MASK,
	NULL, HFILL}},
    { &hf_sccp_ansi_isni_counter,
      { "ISNI Counter", "sccp.isni.counter",
	FT_UINT8, BASE_DEC, NULL, ANSI_ISNI_COUNTER_MASK,
	NULL, HFILL}},
    {&hf_sccp_xudt_msg_fragments,
	{"Message fragments", "sccp.msg.fragments",
	FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL }
    },
    {&hf_sccp_xudt_msg_fragment,
	{"Message fragment", "sccp.msg.fragment",
	FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
    },
    {&hf_sccp_xudt_msg_fragment_overlap,
	{"Message fragment overlap", "sccp.msg.fragment.overlap",
	FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
    },
    {&hf_sccp_xudt_msg_fragment_overlap_conflicts,
	{"Message fragment overlapping with conflicting data", "sccp.msg.fragment.overlap.conflicts",
	FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
    },
    {&hf_sccp_xudt_msg_fragment_multiple_tails,
	{"Message has multiple tail fragments", "sccp.msg.fragment.multiple_tails",
	FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
    },
    {&hf_sccp_xudt_msg_fragment_too_long_fragment,
	{"Message fragment too long", "sccp.msg.fragment.too_long_fragment",
	FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }
    },
    {&hf_sccp_xudt_msg_fragment_error,
	{"Message defragmentation error", "sccp.msg.fragment.error",
	FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
    },
    {&hf_sccp_xudt_msg_reassembled_in,
	{"Reassembled in", "sccp.msg.reassembled.in",
	FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
    },
    {&hf_sccp_xudt_msg_reassembled_length,
	{"Reassembled SCCP length", "sccp.msg.reassembled.length",
	FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL }
    },
    { &hf_sccp_assoc_id,
      { "Association ID", "sccp.assoc.id",
	FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_sccp_assoc_msg,
	{"Message in frame", "sccp.assoc.msg",
	FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
    },

  };

  /* Setup protocol subtree array */
  static gint *ett[] = {
    &ett_sccp,
    &ett_sccp_called,
    &ett_sccp_called_ai,
    &ett_sccp_called_pc,
    &ett_sccp_called_gt,
    &ett_sccp_calling,
    &ett_sccp_calling_ai,
    &ett_sccp_calling_pc,
    &ett_sccp_calling_gt,
    &ett_sccp_sequencing_segmenting,
    &ett_sccp_segmentation,
    &ett_sccp_ansi_isni_routing_control,
    &ett_sccp_xudt_msg_fragment,
    &ett_sccp_xudt_msg_fragments,
    &ett_sccp_assoc,
    &ett_sccp_digits
  };


  static uat_field_t users_flds[] = {
		UAT_FLD_DEC(sccp_users, ni, "Network Indicator", "Network Indicator"),
		UAT_FLD_RANGE(sccp_users, called_pc, "Called DPCs", 0xFFFFFF, "DPCs for which this protocol is to be used"),
		UAT_FLD_RANGE(sccp_users, called_ssn, "Called SSNs", 255, "Called SSNs for which this protocol is to be used"),
		UAT_FLD_VS(sccp_users, user, "User protocol", sccp_users_vals, "The User Protocol"),
		UAT_END_FIELDS
  };


  uat_t* users_uat = uat_new("SCCP Users Table",
	sizeof(sccp_user_t),
	"sccp_users",
	TRUE,
	(void*) &sccp_users,
	&num_sccp_users,
	UAT_CAT_PORTS,
	"ChSccpUsers",
	sccp_users_copy_cb,
	sccp_users_update_cb,
	sccp_users_free_cb,
	NULL,
	users_flds );

 /* Register the protocol name and description */
  proto_sccp = proto_register_protocol("Signalling Connection Control Part",
				       "SCCP", "sccp");

  register_dissector("sccp", dissect_sccp, proto_sccp);

  /* Required function calls to register the header fields and subtrees used */
  proto_register_field_array(proto_sccp, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));


  sccp_ssn_dissector_table = register_dissector_table("sccp.ssn", "SCCP SSN", FT_UINT8, BASE_DEC);

  register_heur_dissector_list("sccp", &heur_subdissector_list);

  sccp_module = prefs_register_protocol(proto_sccp, proto_reg_handoff_sccp);

  prefs_register_uint_preference(sccp_module, "source_pc",
				 "Source PC (in hex)",
				 "The source point code (usually MSC) (to determine whether message is uplink or downlink)",
				 16, &sccp_source_pc_global);

  prefs_register_bool_preference(sccp_module, "show_length", "Show length",
				 "Show parameter length in the protocol tree",
				 &sccp_show_length);

  prefs_register_bool_preference(sccp_module, "defragment_xudt",
				 "Reassemble XUDT messages",
				 "Whether XUDT messages should be reassembled",
				 &sccp_xudt_desegment);

  prefs_register_bool_preference(sccp_module, "trace_sccp",
				 "Trace Associations",
				 "Whether to keep information about messages and their associations",
				 &trace_sccp);


  prefs_register_bool_preference(sccp_module, "show_more_info",
				 "Show key parameters in Info Column",
				 "Show SLR, DLR, and CAUSE Parameters in the Information Column of the Summary",
				 &show_key_params);


  prefs_register_uat_preference(sccp_module, "users_table", "Users Table",
				 "A table that enumerates user protocols to be used against specific PCs and SSNs",
				 users_uat);

  prefs_register_bool_preference(sccp_module, "set_addresses", "Set source and destination GT addresses",
				 "Set the source and destination addresses to the GT digits (if RI=GT)."
				 "  This may affect TCAP's ability to recognize which messages belong to which TCAP session.", &set_addresses);

  prefs_register_string_preference(sccp_module, "default_payload", "Default Payload",
                 "The protocol which should be used to dissect the payload if nothing else has claimed it",
                 &default_payload);

  register_init_routine(&init_sccp);

  assocs = se_tree_create(EMEM_TREE_TYPE_RED_BLACK, "sccp_associations");

  sccp_tap = register_tap("sccp");

}

void
proto_reg_handoff_sccp(void)
{
  dissector_handle_t sccp_handle;

  static gboolean initialised=FALSE;

  if (!initialised) {
    sccp_handle = find_dissector("sccp");

    dissector_add_uint("wtap_encap", WTAP_ENCAP_SCCP, sccp_handle);
    dissector_add_uint("mtp3.service_indicator", SCCP_SI, sccp_handle);
    dissector_add_string("tali.opcode", "sccp", sccp_handle);

    data_handle = find_dissector("data");
    tcap_handle = find_dissector("tcap");
    ranap_handle = find_dissector("ranap");
    bssap_handle = find_dissector("bssap");
    gsmmap_handle = find_dissector("gsm_map");
    camel_handle = find_dissector("camel");
    inap_handle = find_dissector("inap");

    initialised = TRUE;
  }

  default_handle = find_dissector(default_payload);
}