summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-mtp3mg.c
blob: 25a247776d739352d2155797d4b79d1555e61710 (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
/* packet-mtp3mg.c
 * Routines for Message Transfer Part Level 3 Management and Test dissection
 *
 * It is (hopefully) compliant to:
 *   ANSI T1.111.4-1996
 *   ITU-T Q.704 7/1996
 *   ITU-T Q.707 7/1996 and ANSI T1.111.7-1996 (for SLT message formats)
 *   portions of ITU-T Q.2210 7/1996 (for XCO/XCA message formats)
 *   GF 001-9001 (Chinese ITU variant)
 *   JT-Q704, JT-Q707v2, and NTT-Q704 (Japan)
 *
 *   Note that the division of the Japan SLS into the SLC and A/B bit is not
 *   done.
 *
 * Copyright 2003, Jeff Morriss <jeff.morriss.ws [AT] gmail.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-mtp3.c
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/expert.h>

#include <wsutil/str_util.h>

#include "packet-mtp3.h"

void proto_register_mtp3mg(void);
void proto_reg_handoff_mtp3mg(void);

#define H0H1_LENGTH 1
#define H0_MASK     0x0f
#define H1_MASK     0xf0
#define H1_SHIFT    4

#define H0_CHM 0x01
#define H0_ECM 0x02
#define H0_FCM 0x03
#define H0_TFM 0x04
#define H0_RSM 0x05
#define H0_MIM 0x06     /* not used in Japan */
#define H0_TRM 0x07     /* not used in Japan */
#define H0_DLM 0x08     /* not used in Japan */
#define H0_UFC 0x0a     /* not used in Japan */
static const value_string h0_message_type_values[] = {
  { H0_CHM, "Changeover and changeback messages" },
  { H0_ECM, "Emergency changeover messages" },
  { H0_FCM, "Transfer-controlled and signalling route set congestion messages" },
  { H0_TFM, "Transfer prohibited-allowed-restricted messages" },
  { H0_RSM, "Signalling-route-set-test messages" },
  { H0_MIM, "Management inhibit messages" },
  { H0_TRM, "Traffic restart messages" },
  { H0_DLM, "Signalling-data-link-connection messages" },
  { H0_UFC, "User part flow control messages" },
  { 0,      NULL } };

#define TEST_H0_SLT 0x1
static const value_string test_h0_message_type_values[] = {
  { TEST_H0_SLT, "Test messages" },
  { 0,           NULL } };

#define CHM_H1_COO 0x01
#define CHM_H1_COA 0x02
#define CHM_H1_XCO 0x03
#define CHM_H1_XCA 0x04
#define CHM_H1_CBD 0x05
#define CHM_H1_CBA 0x06
static const value_string chm_h1_message_type_values[] = {
  { CHM_H1_COO, "Changeover-order signal" },
  { CHM_H1_COA, "Changeover-ack signal" },
  { CHM_H1_XCO, "Extended changeover-order signal" },
  { CHM_H1_XCA, "Extended changeover-ack signal" },
  { CHM_H1_CBD, "Changeback-declaration signal" },
  { CHM_H1_CBA, "Changeback-ack signal" },
  { 0,          NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string chm_h1_message_type_acro_values[] = {
  { CHM_H1_COO, "COO" },
  { CHM_H1_COA, "COA" },
  { CHM_H1_XCO, "XCO" },
  { CHM_H1_XCA, "XCA" },
  { CHM_H1_CBD, "CBD" },
  { CHM_H1_CBA, "CBA" },
  { 0,          NULL } };

#define ECM_H1_ECO 0x01
#define ECM_H1_ECA 0x02
static const value_string ecm_h1_message_type_values[] = {
  { ECM_H1_ECO, "Emergency-changeover-order signal" },
  { ECM_H1_ECA, "Emergency-changeover-ack signal" },
  { 0,          NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string ecm_h1_message_type_acro_values[] = {
  { ECM_H1_ECO, "ECO" },
  { ECM_H1_ECA, "ECA" },
  { 0,          NULL } };

#define FCM_H1_RCT 0x01
#define FCM_H1_TFC 0x02
static const value_string fcm_h1_message_type_values[] = {
  { FCM_H1_RCT, "Signalling-route-set-congestion-test signal" },
  { FCM_H1_TFC, "Transfer-controlled signal" },
  { 0,          NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string fcm_h1_message_type_acro_values[] = {
  { FCM_H1_RCT, "RCT" },
  { FCM_H1_TFC, "TFC" },
  { 0,          NULL } };

#define TFM_H1_TFP 0x01
#define TFM_H1_TCP 0x02 /* ANSI only */
#define TFM_H1_TFR 0x03
#define TFM_H1_TCR 0x04 /* ANSI only */
#define TFM_H1_TFA 0x05
#define TFM_H1_TCA 0x06 /* ANSI only */
static const value_string tfm_h1_message_type_values[] = {
  { TFM_H1_TFP, "Transfer-prohibited signal" },
  { TFM_H1_TCP, "Transfer-cluster-prohibited signal (ANSI only)" },
  { TFM_H1_TFR, "Transfer-restricted signal" },
  { TFM_H1_TCR, "Transfer-cluster-restricted signal (ANSI only)" },
  { TFM_H1_TFA, "Transfer-allowed signal" },
  { TFM_H1_TCA, "Transfer-cluster-allowed signal (ANSI only)" },
  { 0,          NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string tfm_h1_message_type_acro_values[] = {
  { TFM_H1_TFP, "TFP" },
  { TFM_H1_TCP, "TCP" },
  { TFM_H1_TFR, "TFR" },
  { TFM_H1_TCR, "TCR" },
  { TFM_H1_TFA, "TFA" },
  { TFM_H1_TCA, "TCA" },
  { 0,          NULL } };

#define RSM_H1_RST 0x01
#define RSM_H1_RSR 0x02
#define RSM_H1_RCP 0x03 /* ANSI only */
#define RSM_H1_RCR 0x04 /* ANSI only */
static const value_string rsm_h1_message_type_values[] = {
  { RSM_H1_RST, "Signalling-route-set-test prohibited signal" },
  { RSM_H1_RSR, "Signalling-route-set-test restricted signal" },
  { RSM_H1_RCP, "Signalling-route-set-test cluster-prohibited signal (ANSI only)" },
  { RSM_H1_RCR, "Signalling-route-set-test cluster-restricted signal (ANSI only)" },
  { 0,          NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string rsm_h1_message_type_acro_values[] = {
  { RSM_H1_RST, "RST" },
  { RSM_H1_RSR, "RSR" },
  { RSM_H1_RCP, "RCP" },
  { RSM_H1_RCR, "RCR" },
  { 0,          NULL } };

#define MIM_H1_LIN 0x01
#define MIM_H1_LUN 0x02
#define MIM_H1_LIA 0x03
#define MIM_H1_LUA 0x04
#define MIM_H1_LID 0x05
#define MIM_H1_LFU 0x06
#define MIM_H1_LLT 0x07 /* LLI in ANSI */
#define MIM_H1_LRT 0x08 /* LRI in ANSI */
static const value_string mim_h1_message_type_values[] = {
  { MIM_H1_LIN, "Link inhibit signal" },
  { MIM_H1_LUN, "Link uninhibit signal" },
  { MIM_H1_LIA, "Link inhibit ack signal" },
  { MIM_H1_LUA, "Link uninhibit ack signal" },
  { MIM_H1_LID, "Link inhibit denied signal" },
  { MIM_H1_LFU, "Link forced uninhibit signal" },
  { MIM_H1_LLT, "Link local inhibit test signal" },
  { MIM_H1_LRT, "Link remote inhibit test signal" },
  { 0,          NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string mim_h1_message_type_acro_values[] = {
  { MIM_H1_LIN, "LIN" },
  { MIM_H1_LUN, "LUN" },
  { MIM_H1_LIA, "LIA" },
  { MIM_H1_LUA, "LUA" },
  { MIM_H1_LID, "LID" },
  { MIM_H1_LFU, "LFU" },
  { MIM_H1_LLT, "LLT (LLI)" },
  { MIM_H1_LRT, "LRT (LRI)" },
  { 0,          NULL } };

#define TRM_H1_TRA 0x01
#define TRM_H1_TRW 0x02 /* ANSI only */
static const value_string trm_h1_message_type_values[] = {
  { TRM_H1_TRA, "Traffic-restart-allowed signal" },
  { TRM_H1_TRW, "Traffic-restart-waiting signal (ANSI only)" },
  { 0,          NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string trm_h1_message_type_acro_values[] = {
  { TRM_H1_TRA, "TRA" },
  { TRM_H1_TRW, "TRW" },
  { 0,          NULL } };

#define DLM_H1_DLC 0x01
#define DLM_H1_CSS 0x02
#define DLM_H1_CNS 0x03
#define DLM_H1_CNP 0x04
static const value_string dlm_h1_message_type_values[] = {
  { DLM_H1_DLC, "Signalling-data-link-connection-order signal" },
  { DLM_H1_CSS, "Connection-successful signal" },
  { DLM_H1_CNS, "Connection-not-successful signal" },
  { DLM_H1_CNP, "Connection-not-possible signal" },
  { 0,          NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string dlm_h1_message_type_acro_values[] = {
  { DLM_H1_DLC, "DLC" },
  { DLM_H1_CSS, "CSS" },
  { DLM_H1_CNS, "CNS" },
  { DLM_H1_CNP, "CNP" },
  { 0,          NULL } };

#define UFC_H1_UPU 0x01
static const value_string ufc_h1_message_type_values[] = {
  { UFC_H1_UPU, "User part unavailable signal" },
  { 0,          NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string ufc_h1_message_type_acro_values[] = {
  { UFC_H1_UPU, "UPU" },
  { 0,          NULL } };

static const value_string upu_cause_values[] = {
  { 0x0, "Unknown" },
  { 0x1, "Unequipped remote user" },
  { 0x2, "Inaccessible remote user" },
  { 0,   NULL } };

#define TEST_H1_SLTM 0x1
#define TEST_H1_SLTA 0x2
static const value_string test_h1_message_type_values[] = {
  { TEST_H1_SLTM, "Signalling link test message" },
  { TEST_H1_SLTA, "Signalling link test acknowledgement message" },
  { 0,            NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string test_h1_message_type_acro_values[] = {
  { TEST_H1_SLTM, "SLTM" },
  { TEST_H1_SLTA, "SLTA" },
  { 0,            NULL } };

#define JAPAN_TEST_SRT 0x23
#define JAPAN_TEST_SRA 0x84
static const value_string japan_test_message_type_values[] = {
  { JAPAN_TEST_SRT, "Signalling routing test message" },
  { JAPAN_TEST_SRA, "Signalling routing test acknowledgement message" },
  { 0,              NULL } };

/* Same as above but in acronym form (for the Info column) */
static const value_string japan_test_message_type_acro_values[] = {
  { JAPAN_TEST_SRT, "SRT" },
  { JAPAN_TEST_SRA, "SRA" },
  { 0,              NULL } };

#define ANSI_COO_LENGTH    2
#define ANSI_COO_SLC_MASK  0x000f
#define ANSI_COO_FSN_MASK  0x07f0
#define ITU_COO_LENGTH     1
#define ITU_COO_FSN_MASK   0x7f
#define ANSI_XCO_LENGTH    4
#define ANSI_XCO_SLC_MASK  0x0000000f
#define ANSI_XCO_FSN_MASK  0x0ffffff0
#define ITU_XCO_LENGTH     3
#define ANSI_CBD_LENGTH    2
#define ANSI_CBD_SLC_MASK  0x000f
#define ANSI_CBD_CBC_MASK  0x0ff0
#define ITU_CBD_LENGTH     1
#define JAPAN_CBD_CBC_MASK 0x3

#define ANSI_ECO_LENGTH   1
#define ANSI_ECO_SLC_MASK 0x0f

#define ANSI_TFC_STATUS_LENGTH       1
#define ANSI_TFC_STATUS_OFFSET       ANSI_PC_LENGTH
#define ANSI_TFC_STATUS_MASK         0x03
#define ITU_TFC_STATUS_LENGTH        ITU_PC_LENGTH
#define ITU_TFC_STATUS_MASK          0xc000
#define JAPAN_TFC_SPARE_OFFSET        0
#define JAPAN_TFC_SPARE_LENGTH        1
#define JAPAN_TFC_APC_OFFSET          JAPAN_TFC_SPARE_LENGTH
#define JAPAN_TFC_STATUS_OFFSET       (JAPAN_TFC_SPARE_LENGTH + JAPAN_PC_LENGTH)
#define JAPAN_TFC_STATUS_LENGTH       1
#define JAPAN_TFC_STATUS_MASK         0x03
#define JAPAN_TFC_STATUS_SPARE_MASK   0xfc

#define JAPAN_TFM_COUNT_OFFSET 0
#define JAPAN_TFM_COUNT_LENGTH 1

#define ANSI_MIM_LENGTH   1
#define ANSI_MIM_SLC_MASK 0x0f

#define ANSI_DLC_LENGTH    3
#define ANSI_DLC_SLC_MASK  0x00000f
#define ANSI_DLC_LINK_MASK 0x03fff0
#define ITU_DLC_LENGTH     2
#define ITU_DLC_LINK_MASK  0x0fff

#define ANSI_UPU_USER_OFFSET ANSI_PC_LENGTH
#define UPU_USER_LENGTH      1
#define UPU_USER_MASK        0x0f
#define UPU_CAUSE_MASK       0xf0
#define ITU_UPU_USER_OFFSET  ITU_PC_LENGTH

#define TEST_LENGTH         1
#define TEST_LENGTH_MASK    0xf0
#define TEST_LENGTH_SHIFT   4
#define TEST_PATTERN_OFFSET TEST_LENGTH
#define ANSI_TEST_SLC_MASK  0x0f

#define JAPAN_SPARE_LENGTH 1
#define JAPAN_H0H1_OFFSET JAPAN_SPARE_LENGTH

#define JAPAN_TEST_SPARE_OFFSET   JAPAN_SPARE_LENGTH + H0H1_LENGTH
#define JAPAN_TEST_SPARE_LENGTH   1
#define JAPAN_TEST_PATTERN_OFFSET (JAPAN_TEST_SPARE_OFFSET + JAPAN_TEST_SPARE_LENGTH)
#define JAPAN_TEST_PATTERN_LENGTH 2
#define JAPAN_TEST_PATTERN 0x7711


/* This list is slightly different from that in packet-mtp3.c */
static const value_string service_indicator_code_vals[] = {
        { 0x0,  "Spare" },
        { 0x1,  "Spare"},
        { 0x2,  "Spare" },
        { 0x3,  "SCCP" },
        { 0x4,  "TUP" },
        { 0x5,  "ISUP" },
        { 0x6,  "DUP (call and circuit related messages)" },
        { 0x7,  "DUP (facility registration and cancellation message)" },
        { 0x8,  "MTP testing user part" },
        { 0x9,  "ISUP-b" },
        { 0xa,  "ISUP-s" },
        { 0xb,  "Spare" },
        { 0xc,  "Spare" },
        { 0xd,  "Spare" },
        { 0xe,  "Spare" },
        { 0xf,  "Spare" },
        { 0,    NULL }
};

/* Initialize the protocol and registered fields */
static int proto_mtp3mg;
static int hf_mtp3mg_h0;
static int hf_mtp3mg_chm_h1;
static int hf_mtp3mg_ecm_h1;
static int hf_mtp3mg_fcm_h1;
static int hf_mtp3mg_tfm_h1;
static int hf_mtp3mg_rsm_h1;
static int hf_mtp3mg_mim_h1;
static int hf_mtp3mg_trm_h1;
static int hf_mtp3mg_dlm_h1;
static int hf_mtp3mg_ufc_h1;
static int hf_mtp3mg_coo_ansi_slc;
static int hf_mtp3mg_coo_ansi_fsn;
static int hf_mtp3mg_coo_itu_fsn;
static int hf_mtp3mg_xco_ansi_slc;
static int hf_mtp3mg_xco_ansi_fsn;
static int hf_mtp3mg_xco_itu_fsn;
static int hf_mtp3mg_cbd_ansi_slc;
static int hf_mtp3mg_cbd_ansi_cbc;
static int hf_mtp3mg_cbd_itu_cbc;
static int hf_mtp3mg_cbd_japan_cbc;
static int hf_mtp3mg_eco_ansi_slc;
static int hf_mtp3mg_ansi_apc;
static int hf_mtp3mg_apc_member;
static int hf_mtp3mg_apc_cluster;
static int hf_mtp3mg_apc_network;
static int hf_mtp3mg_tfc_ansi_status;
static int hf_mtp3mg_itu_apc;
static int hf_mtp3mg_tfc_itu_status;
static int hf_mtp3mg_chinese_apc;
static int hf_mtp3mg_tfc_japan_spare;
static int hf_mtp3mg_japan_apc;
static int hf_mtp3mg_tfc_japan_status;
static int hf_mtp3mg_tfc_japan_status_spare;
static int hf_mtp3mg_tfm_japan_count;
static int hf_mtp3mg_tfm_japan_spare;
static int hf_mtp3mg_rsm_japan_count;
static int hf_mtp3mg_rsm_japan_spare;
static int hf_mtp3mg_mim_ansi_slc;
static int hf_mtp3mg_dlc_ansi_slc;
static int hf_mtp3mg_dlc_ansi_link;
static int hf_mtp3mg_dlc_itu_link;
static int hf_mtp3mg_upu_user;
static int hf_mtp3mg_upu_cause;
static int hf_mtp3test_h0;
static int hf_mtp3mg_test_h1;
static int hf_mtp3mg_test_ansi_slc;
static int hf_mtp3mg_test_length;
static int hf_mtp3mg_japan_test;
static int hf_mtp3mg_japan_test_spare;
static int hf_mtp3mg_japan_test_pattern;
static int hf_mtp3mg_japan_spare;
static int hf_mtp3mg_test_pattern;

/* Initialize the subtree pointers */
static int ett_mtp3mg;
static int ett_mtp3mg_fcm_apc;
static int ett_mtp3mg_tfm_apc;
static int ett_mtp3mg_rsm_apc;
static int ett_mtp3mg_upu_apc;

static expert_field ei_mtp3mg_unknown_message;

static dissector_handle_t mtp3mg_handle;

static void
dissect_mtp3mg_unknown_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    uint8_t message_length;

    message_length = tvb_captured_length(tvb);
    proto_tree_add_expert_format(tree, pinfo, &ei_mtp3mg_unknown_message, tvb, 0, message_length,
                        "Unknown message (%u byte%s)", message_length,
                        plurality(message_length, "", "s"));
}

static void
dissect_mtp3mg_chm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{
    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, chm_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case CHM_H1_COO:
    case CHM_H1_COA:
        if (mtp3_standard == ANSI_STANDARD)
        {
            proto_tree_add_item(tree, hf_mtp3mg_coo_ansi_slc, tvb, 0,
                                ANSI_COO_LENGTH, ENC_LITTLE_ENDIAN);
            proto_tree_add_item(tree, hf_mtp3mg_coo_ansi_fsn, tvb, 0,
                                ANSI_COO_LENGTH, ENC_LITTLE_ENDIAN);
        } else /* ITU_STANDARD, CHINESE_ITU_STANDARD, and JAPAN_STANDARD */ {
            proto_tree_add_item(tree, hf_mtp3mg_coo_itu_fsn, tvb, 0,
                                ITU_COO_LENGTH, ENC_LITTLE_ENDIAN);
        }
        break;

    case CHM_H1_XCO:
    case CHM_H1_XCA:
        if (mtp3_standard == ANSI_STANDARD)
        {
            proto_tree_add_item(tree, hf_mtp3mg_xco_ansi_slc, tvb, 0,
                                ANSI_XCO_LENGTH, ENC_LITTLE_ENDIAN);
            proto_tree_add_item(tree, hf_mtp3mg_xco_ansi_fsn, tvb, 0,
                                ANSI_XCO_LENGTH, ENC_LITTLE_ENDIAN);
        } else /* ITU_STANDARD, CHINESE_ITU_STANDARD, and JAPAN_STANDARD */ {
            proto_tree_add_item(tree, hf_mtp3mg_xco_itu_fsn, tvb, 0,
                                ITU_XCO_LENGTH, ENC_LITTLE_ENDIAN);
        }
        break;

    case CHM_H1_CBD:
    case CHM_H1_CBA:
        if (mtp3_standard == ANSI_STANDARD)
        {
            proto_tree_add_item(tree, hf_mtp3mg_cbd_ansi_slc, tvb, 0,
                                ANSI_CBD_LENGTH, ENC_LITTLE_ENDIAN);
            proto_tree_add_item(tree, hf_mtp3mg_cbd_ansi_cbc, tvb, 0,
                                ANSI_CBD_LENGTH, ENC_LITTLE_ENDIAN);
        } else if (mtp3_standard == JAPAN_STANDARD) {
            proto_tree_add_item(tree, hf_mtp3mg_cbd_japan_cbc, tvb, 0,
                                ITU_CBD_LENGTH, ENC_LITTLE_ENDIAN);
        } else /* ITU_STANDARD and CHINESE_ITU_STANDARD */ {
            proto_tree_add_item(tree, hf_mtp3mg_cbd_itu_cbc, tvb, 0,
                                ITU_CBD_LENGTH, ENC_LITTLE_ENDIAN);
        }
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
  }
}

static void
dissect_mtp3mg_ecm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{

    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, ecm_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case ECM_H1_ECO:
    case ECM_H1_ECA:
        if (mtp3_standard == ANSI_STANDARD)
        {
            proto_tree_add_item(tree, hf_mtp3mg_eco_ansi_slc, tvb, 0,
                                ANSI_ECO_LENGTH, ENC_LITTLE_ENDIAN);
        }
        /* else: nothing to dissect */
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
    }
}

static void
dissect_mtp3mg_fcm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{
    proto_item *apc_item;

    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, fcm_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case FCM_H1_RCT:
        /* nothing to dissect */
        break;

    case FCM_H1_TFC:
        if (mtp3_standard == ITU_STANDARD)
        {

            apc_item = proto_tree_add_item(tree, hf_mtp3mg_itu_apc, tvb, 0,
                                           ITU_PC_LENGTH, ENC_LITTLE_ENDIAN);

            if (mtp3_pc_structured()) {
                uint32_t apc;

                apc = tvb_get_letohs(tvb, 0) & ITU_PC_MASK;
                proto_item_append_text(apc_item, " (%s)", mtp3_pc_to_str(apc));
            }

            /* Congestion level is a national option */
            proto_tree_add_item(tree, hf_mtp3mg_tfc_itu_status, tvb, 0,
                                ITU_TFC_STATUS_LENGTH, ENC_LITTLE_ENDIAN);



        } else if (mtp3_standard == JAPAN_STANDARD) {

            proto_tree_add_item(tree, hf_mtp3mg_tfc_japan_spare, tvb,
                                JAPAN_TFC_SPARE_OFFSET,
                                JAPAN_TFC_SPARE_LENGTH, ENC_LITTLE_ENDIAN);

            apc_item = proto_tree_add_item(tree, hf_mtp3mg_japan_apc, tvb,
                                           JAPAN_TFC_APC_OFFSET,
                                           JAPAN_PC_LENGTH, ENC_LITTLE_ENDIAN);
            if (mtp3_pc_structured()) {
                uint32_t apc;

                apc = tvb_get_letohs(tvb, JAPAN_TFC_APC_OFFSET);
                proto_item_append_text(apc_item, " (%s)", mtp3_pc_to_str(apc));
            }

            proto_tree_add_item(tree, hf_mtp3mg_tfc_japan_status, tvb,
                                JAPAN_TFC_STATUS_OFFSET,
                                JAPAN_TFC_STATUS_LENGTH, ENC_LITTLE_ENDIAN);
            proto_tree_add_item(tree, hf_mtp3mg_tfc_japan_status_spare, tvb,
                                JAPAN_TFC_STATUS_OFFSET,
                                JAPAN_TFC_STATUS_LENGTH, ENC_LITTLE_ENDIAN);

        } else /* ANSI_STANDARD and CHINESE_ITU_STANDARD */ {

            int hf_apc_string;

            if (mtp3_standard == ANSI_STANDARD) {

                hf_apc_string = hf_mtp3mg_ansi_apc;

            } else /* CHINESE_ITU_STANDARD */ {

                hf_apc_string = hf_mtp3mg_chinese_apc;
            }

            dissect_mtp3_3byte_pc(tvb, 0, tree, ett_mtp3mg_fcm_apc,
                                  hf_apc_string, hf_mtp3mg_apc_network,
                                  hf_mtp3mg_apc_cluster,
                                  hf_mtp3mg_apc_member, 0, 0);

            proto_tree_add_item(tree, hf_mtp3mg_tfc_ansi_status, tvb,
                                ANSI_TFC_STATUS_OFFSET, ANSI_TFC_STATUS_LENGTH,
                                ENC_LITTLE_ENDIAN);

        }
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
    }
}

static void
dissect_mtp3mg_tfm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{
    proto_item *apc_item;

    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, tfm_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case TFM_H1_TFP:
    case TFM_H1_TCP:
    case TFM_H1_TFR:
    case TFM_H1_TCR:
    case TFM_H1_TFA:
    case TFM_H1_TCA:
        if (mtp3_standard == ANSI_STANDARD) {
            dissect_mtp3_3byte_pc(tvb, 0, tree, ett_mtp3mg_tfm_apc,
                                  hf_mtp3mg_ansi_apc,
                                  hf_mtp3mg_apc_network,
                                  hf_mtp3mg_apc_cluster,
                                  hf_mtp3mg_apc_member, 0, 0);

        } else if (mtp3_standard == JAPAN_STANDARD) {
            uint8_t count, i;
            uint32_t offset;

            if (h1 == TFM_H1_TCP || h1 == TFM_H1_TCR || h1 == TFM_H1_TCA
                || h1 == TFM_H1_TFR)
                dissect_mtp3mg_unknown_message(tvb, pinfo, tree);

            proto_tree_add_item(tree, hf_mtp3mg_tfm_japan_count, tvb,
                                JAPAN_TFM_COUNT_OFFSET,
                                JAPAN_TFM_COUNT_LENGTH, ENC_LITTLE_ENDIAN);

            count = tvb_get_uint8(tvb, JAPAN_TFM_COUNT_OFFSET);
            offset = JAPAN_TFM_COUNT_LENGTH;
            for (i = 0; i < count; i++)
            {
                apc_item = proto_tree_add_item(tree, hf_mtp3mg_japan_apc, tvb,
                                    offset, JAPAN_PC_LENGTH, ENC_LITTLE_ENDIAN);

                if (mtp3_pc_structured()) {
                    uint32_t apc;

                    apc = tvb_get_letohs(tvb, offset);
                    proto_item_append_text(apc_item, " (%s)", mtp3_pc_to_str(apc));
                }

                offset += JAPAN_PC_LENGTH;
                proto_tree_add_item(tree, hf_mtp3mg_tfm_japan_spare, tvb,
                                    offset, JAPAN_PC_LENGTH, ENC_LITTLE_ENDIAN);
                offset += JAPAN_PC_LENGTH;
            }
        } else /* ITU_STANDARD and CHINESE_ITU_STANDARD */ {

            if (h1 == TFM_H1_TCP || h1 == TFM_H1_TCR || h1 == TFM_H1_TCA)
                dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
            else if (mtp3_standard == ITU_STANDARD)
            {
                apc_item = proto_tree_add_item(tree, hf_mtp3mg_itu_apc,
                                               tvb, 0, ITU_PC_LENGTH, ENC_LITTLE_ENDIAN);

                if (mtp3_pc_structured()) {
                    uint32_t apc;

                    apc = tvb_get_letohs(tvb, 0) & ITU_PC_MASK;
                    proto_item_append_text(apc_item, " (%s)", mtp3_pc_to_str(apc));
                }
            }
            else if (mtp3_standard == CHINESE_ITU_STANDARD)
                dissect_mtp3_3byte_pc(tvb, 0, tree, ett_mtp3mg_tfm_apc,
                                      hf_mtp3mg_chinese_apc,
                                      hf_mtp3mg_apc_network,
                                      hf_mtp3mg_apc_cluster,
                                      hf_mtp3mg_apc_member, 0, 0);
        }
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
    }
}

static void
dissect_mtp3mg_rsm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{
    proto_item *apc_item;

    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, rsm_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case RSM_H1_RST:
    case RSM_H1_RSR:
    case RSM_H1_RCP:
    case RSM_H1_RCR:
        if (mtp3_standard == ANSI_STANDARD) {
            dissect_mtp3_3byte_pc(tvb, 0, tree, ett_mtp3mg_rsm_apc,
                                  hf_mtp3mg_ansi_apc,
                                  hf_mtp3mg_apc_network,
                                  hf_mtp3mg_apc_cluster,
                                  hf_mtp3mg_apc_member, 0, 0);

        } else if (mtp3_standard == JAPAN_STANDARD) {
            if (h1 == RSM_H1_RST) {
                uint32_t offset;
                uint8_t count, i;

                proto_tree_add_item(tree, hf_mtp3mg_rsm_japan_count, tvb,
                                    JAPAN_TFM_COUNT_OFFSET,
                                    JAPAN_TFM_COUNT_LENGTH, ENC_LITTLE_ENDIAN);

                count = tvb_get_uint8(tvb, JAPAN_TFM_COUNT_OFFSET);
                offset = JAPAN_TFM_COUNT_LENGTH;
                for (i = 0; i < count; i++) {
                    apc_item = proto_tree_add_item(tree,
                                                   hf_mtp3mg_japan_apc,
                                                   tvb, offset,
                                                   JAPAN_PC_LENGTH,
                                                   ENC_LITTLE_ENDIAN);
                    if (mtp3_pc_structured()) {
                        uint32_t apc;

                        apc = tvb_get_letohs(tvb, 0);
                        proto_item_append_text(apc_item, " (%s)",
                                               mtp3_pc_to_str(apc));
                    }
                    offset += JAPAN_PC_LENGTH;
                    proto_tree_add_item(tree, hf_mtp3mg_rsm_japan_spare, tvb,
                                        offset, JAPAN_PC_LENGTH,
                                        ENC_LITTLE_ENDIAN);
                    offset += JAPAN_PC_LENGTH;
                }
            } else
                dissect_mtp3mg_unknown_message(tvb, pinfo, tree);

        } else /* ITU_STANDARD and CHINESE_ITU_STANDARD */ {

            if (h1 == RSM_H1_RST || h1 == RSM_H1_RSR) {
                if (mtp3_standard == ITU_STANDARD) {
                    apc_item = proto_tree_add_item(tree, hf_mtp3mg_itu_apc,
                                                   tvb, 0, ITU_PC_LENGTH,
                                                   ENC_LITTLE_ENDIAN);
                    if (mtp3_pc_structured()) {
                        uint32_t apc;

                        apc = tvb_get_letohs(tvb, 0) & ITU_PC_MASK;
                        proto_item_append_text(apc_item, " (%s)", mtp3_pc_to_str(apc));
                    }
                }
                else /* CHINESE_ITU_STANDARD */
                    dissect_mtp3_3byte_pc(tvb, 0, tree, ett_mtp3mg_rsm_apc,
                                          hf_mtp3mg_chinese_apc,
                                          hf_mtp3mg_apc_network,
                                          hf_mtp3mg_apc_cluster,
                                          hf_mtp3mg_apc_member, 0, 0);
            } else
                dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
        }
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
    }
}

static void
dissect_mtp3mg_mim(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{
    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, mim_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case MIM_H1_LIN:
    case MIM_H1_LUN:
    case MIM_H1_LIA:
    case MIM_H1_LUA:
    case MIM_H1_LID:
    case MIM_H1_LFU:
    case MIM_H1_LLT:
    case MIM_H1_LRT:
        if (mtp3_standard == ANSI_STANDARD)
        {
            proto_tree_add_item(tree, hf_mtp3mg_mim_ansi_slc, tvb, 0,
                                ANSI_MIM_LENGTH, ENC_LITTLE_ENDIAN);
        }
        /* else: nothing to dissect */
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
    }
}

static void
dissect_mtp3mg_trm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{
    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, trm_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case TRM_H1_TRA:
        /* nothing to dissect */
        break;
    case TRM_H1_TRW:
        if (mtp3_standard != ANSI_STANDARD)
            dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
        /* else: nothing to dissect */
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
    }
}

static void
dissect_mtp3mg_dlm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{
    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, dlm_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case DLM_H1_DLC:
        if (mtp3_standard == ANSI_STANDARD)
        {
            proto_tree_add_item(tree, hf_mtp3mg_dlc_ansi_slc, tvb, 0,
                                ANSI_DLC_LENGTH, ENC_LITTLE_ENDIAN);
            proto_tree_add_item(tree, hf_mtp3mg_dlc_ansi_link, tvb, 0,
                                ANSI_DLC_LENGTH, ENC_LITTLE_ENDIAN);
        } else /* ITU_STANDARD and CHINESE_ITU_STANDARD */ {
            proto_tree_add_item(tree, hf_mtp3mg_dlc_itu_link, tvb, 0,
                                ITU_DLC_LENGTH, ENC_LITTLE_ENDIAN);
        }
        break;
    case DLM_H1_CSS:
    case DLM_H1_CNS:
    case DLM_H1_CNP:
        /* nothing to dissect */
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
    }
}

static void
dissect_mtp3mg_ufc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{
    proto_item *apc_item;

    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, ufc_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case UFC_H1_UPU:
        if (mtp3_standard == ANSI_STANDARD
            || mtp3_standard == CHINESE_ITU_STANDARD) {
            int hf_apc;

            if (mtp3_standard == ANSI_STANDARD)
                hf_apc = hf_mtp3mg_ansi_apc;
            else /* CHINESE_ITU_STANDARD */
                hf_apc = hf_mtp3mg_chinese_apc;

            dissect_mtp3_3byte_pc(tvb, 0, tree, ett_mtp3mg_upu_apc, hf_apc,
                                  hf_mtp3mg_apc_network,
                                  hf_mtp3mg_apc_cluster,
                                  hf_mtp3mg_apc_member, 0, 0);

            proto_tree_add_item(tree, hf_mtp3mg_upu_user, tvb,
                                ANSI_UPU_USER_OFFSET, UPU_USER_LENGTH, ENC_LITTLE_ENDIAN);
            proto_tree_add_item(tree, hf_mtp3mg_upu_cause, tvb,
                                ANSI_UPU_USER_OFFSET, UPU_USER_LENGTH, ENC_LITTLE_ENDIAN);
        } else if (mtp3_standard == ITU_STANDARD) {

            apc_item = proto_tree_add_item(tree, hf_mtp3mg_itu_apc, tvb, 0,
                                           ITU_PC_LENGTH, ENC_LITTLE_ENDIAN);
            if (mtp3_pc_structured()) {
                uint32_t apc;

                apc = tvb_get_letohs(tvb, 0) & ITU_PC_MASK;
                proto_item_append_text(apc_item, " (%s)", mtp3_pc_to_str(apc));
            }

            proto_tree_add_item(tree, hf_mtp3mg_upu_user, tvb,
                                ITU_UPU_USER_OFFSET, UPU_USER_LENGTH, ENC_LITTLE_ENDIAN);
            proto_tree_add_item(tree, hf_mtp3mg_upu_cause, tvb,
                                ITU_UPU_USER_OFFSET, UPU_USER_LENGTH, ENC_LITTLE_ENDIAN);
        } else { /* JAPAN_STANDARD */

            apc_item = proto_tree_add_item(tree, hf_mtp3mg_japan_apc, tvb,
                                           0, JAPAN_PC_LENGTH,
                                           ENC_LITTLE_ENDIAN);
            if (mtp3_pc_structured()) {
                uint32_t apc;

                apc = tvb_get_letohs(tvb, 0);
                proto_item_append_text(apc_item, " (%s)", mtp3_pc_to_str(apc));
            }

            proto_tree_add_item(tree, hf_mtp3mg_upu_user, tvb,
                                ITU_UPU_USER_OFFSET, UPU_USER_LENGTH, ENC_LITTLE_ENDIAN);
            proto_tree_add_item(tree, hf_mtp3mg_upu_cause, tvb,
                                ITU_UPU_USER_OFFSET, UPU_USER_LENGTH, ENC_LITTLE_ENDIAN);
        }
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
    }
}

static void
dissect_mtp3mg_test(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                   uint8_t h1)
{
    uint8_t length;

    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                 val_to_str_const(h1, test_h1_message_type_acro_values, "Unknown"));

    switch (h1)
    {
    case TEST_H1_SLTM:
    case TEST_H1_SLTA:
        if (mtp3_standard == ANSI_STANDARD)
        {
            proto_tree_add_item(tree, hf_mtp3mg_test_ansi_slc, tvb, 0,
                                TEST_LENGTH, ENC_LITTLE_ENDIAN);
        }

        proto_tree_add_item(tree, hf_mtp3mg_test_length, tvb, 0, TEST_LENGTH,
                            ENC_LITTLE_ENDIAN);

        length = tvb_get_uint8(tvb, 0) >> TEST_LENGTH_SHIFT;
        proto_tree_add_item(tree, hf_mtp3mg_test_pattern, tvb, TEST_PATTERN_OFFSET, length, ENC_NA);
        break;

    default:
        dissect_mtp3mg_unknown_message(tvb, pinfo, tree);
    }
}

static int
dissect_mtp3mg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{

    uint8_t h0, h1;
    tvbuff_t *payload_tvb;

    /* Set up structures needed to add the protocol subtree and manage it */
    proto_item *mtp3mg_item;
    proto_tree *mtp3mg_tree;

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

        /* create display subtree for the protocol */
        mtp3mg_item = proto_tree_add_item(tree, proto_mtp3mg, tvb, 0, -1, ENC_NA);
        mtp3mg_tree = proto_item_add_subtree(mtp3mg_item, ett_mtp3mg);

    /*
     *  Dissect the message
     */
    if(pinfo->match_uint == MTP_SI_MTN || /* ITU */
       pinfo->match_uint == MTP_SI_MTNS)  /* ANSI */
    {   /* Test messages */

        if (mtp3_standard == JAPAN_STANDARD)
        {
            uint8_t h0h1;
            uint16_t test_pattern;
            proto_item *pattern_item;

            proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_japan_spare, tvb, 0,
                                JAPAN_SPARE_LENGTH, ENC_LITTLE_ENDIAN);

            h0h1 = tvb_get_uint8(tvb, JAPAN_H0H1_OFFSET);

            proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_japan_test, tvb,
                                JAPAN_SPARE_LENGTH, H0H1_LENGTH, ENC_LITTLE_ENDIAN);

            col_add_fstr(pinfo->cinfo, COL_INFO, "%s ",
                         val_to_str_const(h0h1, japan_test_message_type_acro_values, "Unknown"));

            switch (h0h1)
            {
            case JAPAN_TEST_SRT:
            case JAPAN_TEST_SRA:
                proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_japan_test_spare,
                                    tvb, JAPAN_TEST_SPARE_OFFSET,
                                    JAPAN_TEST_SPARE_LENGTH, ENC_LITTLE_ENDIAN);

                test_pattern = tvb_get_letohs(tvb, JAPAN_TEST_PATTERN_OFFSET);
                pattern_item = proto_tree_add_item(mtp3mg_tree,
                                                   hf_mtp3mg_japan_test_pattern,
                                                   tvb,
                                                   JAPAN_TEST_PATTERN_OFFSET,
                                                   JAPAN_TEST_PATTERN_LENGTH,
                                                   ENC_LITTLE_ENDIAN);
                proto_item_append_text(pattern_item, " (%s)",
                                       test_pattern == JAPAN_TEST_PATTERN
                                       ? "correct" : "incorrect");
                break;

            default:
                dissect_mtp3mg_unknown_message(tvb, pinfo, mtp3mg_tree);
            }

        } else { /* not JAPAN */
            proto_tree_add_item(mtp3mg_tree, hf_mtp3test_h0, tvb, 0, H0H1_LENGTH, ENC_LITTLE_ENDIAN);
            /* H1 is added below */

            h0 = tvb_get_uint8(tvb, 0) & H0_MASK;
            h1 = (tvb_get_uint8(tvb, 0) & H1_MASK) >> H1_SHIFT;

            payload_tvb = tvb_new_subset_remaining(tvb, H0H1_LENGTH);

            switch (h0)
            {
            case TEST_H0_SLT:
                proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_test_h1, tvb, 0,
                                    H0H1_LENGTH, ENC_LITTLE_ENDIAN);
                dissect_mtp3mg_test(payload_tvb, pinfo, mtp3mg_tree, h1);
                break;

            default:
                col_set_str(pinfo->cinfo, COL_INFO, "Unknown ");

                dissect_mtp3mg_unknown_message(tvb, pinfo, mtp3mg_tree);
            } /* switch */

        }

    } else {    /* Real management messages */


        if (mtp3_standard == JAPAN_STANDARD)
        {
            proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_japan_spare, tvb, 0,
                                JAPAN_SPARE_LENGTH, ENC_LITTLE_ENDIAN);

            /*  Get a tvb w/o the spare byte--it makes for less code below */
            tvb = tvb_new_subset_remaining(tvb, JAPAN_SPARE_LENGTH);
        }

        proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_h0, tvb, 0, H0H1_LENGTH,
                            ENC_LITTLE_ENDIAN);
        /* H1 is added below */

        h0 = tvb_get_uint8(tvb, 0) & H0_MASK;
        h1 = (tvb_get_uint8(tvb, 0) & H1_MASK) >> H1_SHIFT;

        payload_tvb = tvb_new_subset_remaining(tvb, H0H1_LENGTH);

        switch (h0)
        {
        case H0_CHM:
            proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_chm_h1, tvb, 0,
                                H0H1_LENGTH, ENC_LITTLE_ENDIAN);
            dissect_mtp3mg_chm(payload_tvb, pinfo, mtp3mg_tree, h1);
            break;
        case H0_ECM:
            proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_ecm_h1, tvb, 0,
                                H0H1_LENGTH, ENC_LITTLE_ENDIAN);
            dissect_mtp3mg_ecm(payload_tvb, pinfo, mtp3mg_tree, h1);
            break;
        case H0_FCM:
            proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_fcm_h1, tvb, 0,
                                H0H1_LENGTH, ENC_LITTLE_ENDIAN);
            dissect_mtp3mg_fcm(payload_tvb, pinfo, mtp3mg_tree, h1);
            break;
        case H0_TFM:
            proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_tfm_h1, tvb, 0,
                                H0H1_LENGTH, ENC_LITTLE_ENDIAN);
            dissect_mtp3mg_tfm(payload_tvb, pinfo, mtp3mg_tree, h1);
            break;
        case H0_RSM:
            proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_rsm_h1, tvb, 0,
                                H0H1_LENGTH, ENC_LITTLE_ENDIAN);
            dissect_mtp3mg_rsm(payload_tvb, pinfo, mtp3mg_tree, h1);
            break;
        case H0_MIM:
            if (mtp3_standard != JAPAN_STANDARD)
            {
                proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_mim_h1, tvb, 0,
                                    H0H1_LENGTH, ENC_LITTLE_ENDIAN);
                dissect_mtp3mg_mim(payload_tvb, pinfo, mtp3mg_tree, h1);
            } else
                dissect_mtp3mg_unknown_message(tvb, pinfo, mtp3mg_tree);
            break;
        case H0_TRM:
            if (mtp3_standard != JAPAN_STANDARD)
            {
                proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_trm_h1, tvb, 0,
                                    H0H1_LENGTH, ENC_LITTLE_ENDIAN);
                dissect_mtp3mg_trm(payload_tvb, pinfo, mtp3mg_tree, h1);
            } else
                dissect_mtp3mg_unknown_message(tvb, pinfo, mtp3mg_tree);
            break;
        case H0_DLM:
            if (mtp3_standard != JAPAN_STANDARD)
            {
                proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_dlm_h1, tvb, 0,
                                    H0H1_LENGTH, ENC_LITTLE_ENDIAN);
                dissect_mtp3mg_dlm(payload_tvb, pinfo, mtp3mg_tree, h1);
            } else
                dissect_mtp3mg_unknown_message(tvb, pinfo, mtp3mg_tree);
            break;
        case H0_UFC:
            if (mtp3_standard != JAPAN_STANDARD)
            {
                proto_tree_add_item(mtp3mg_tree, hf_mtp3mg_ufc_h1, tvb, 0,
                                    H0H1_LENGTH, ENC_LITTLE_ENDIAN);
                dissect_mtp3mg_ufc(payload_tvb, pinfo, mtp3mg_tree, h1);
            } else
                dissect_mtp3mg_unknown_message(tvb, pinfo, mtp3mg_tree);
            break;

        default:
            col_set_str(pinfo->cinfo, COL_INFO, "Unknown ");

            dissect_mtp3mg_unknown_message(tvb, pinfo, mtp3mg_tree);
        } /* switch */
    } /* else */

    return tvb_captured_length(tvb);
}

void
proto_register_mtp3mg(void)
{

    /* Setup list of header fields  See Section 1.6.1 for details*/
    static hf_register_info hf[] = {
        { &hf_mtp3mg_h0,
            { "H0 (Message Group)", "mtp3mg.h0",
              FT_UINT8, BASE_HEX, VALS(h0_message_type_values), H0_MASK,
              "Message group identifier", HFILL }},
        { &hf_mtp3mg_chm_h1,
            { "H1 (Message)", "mtp3mg.h1",
              FT_UINT8, BASE_HEX, VALS(chm_h1_message_type_values), H1_MASK,
              "Message type", HFILL }},
        { &hf_mtp3mg_ecm_h1,
            { "H1 (Message)", "mtp3mg.h1",
              FT_UINT8, BASE_HEX, VALS(ecm_h1_message_type_values), H1_MASK,
              "Message type", HFILL }},
        { &hf_mtp3mg_fcm_h1,
            { "H1 (Message)", "mtp3mg.h1",
              FT_UINT8, BASE_HEX, VALS(fcm_h1_message_type_values), H1_MASK,
              "Message type", HFILL }},
        { &hf_mtp3mg_tfm_h1,
            { "H1 (Message)", "mtp3mg.h1",
              FT_UINT8, BASE_HEX, VALS(tfm_h1_message_type_values), H1_MASK,
              "Message type", HFILL }},
        { &hf_mtp3mg_rsm_h1,
            { "H1 (Message)", "mtp3mg.h1",
              FT_UINT8, BASE_HEX, VALS(rsm_h1_message_type_values), H1_MASK,
              "Message type", HFILL }},
        { &hf_mtp3mg_mim_h1,
            { "H1 (Message)", "mtp3mg.h1",
              FT_UINT8, BASE_HEX, VALS(mim_h1_message_type_values), H1_MASK,
              "Message type", HFILL }},
        { &hf_mtp3mg_trm_h1,
            { "H1 (Message)", "mtp3mg.h1",
              FT_UINT8, BASE_HEX, VALS(trm_h1_message_type_values), H1_MASK,
              "Message type", HFILL }},
        { &hf_mtp3mg_dlm_h1,
            { "H1 (Message)", "mtp3mg.h1",
              FT_UINT8, BASE_HEX, VALS(dlm_h1_message_type_values), H1_MASK,
              "Message type", HFILL }},
        { &hf_mtp3mg_ufc_h1,
            { "H1 (Message)", "mtp3mg.h1",
              FT_UINT8, BASE_HEX, VALS(ufc_h1_message_type_values), H1_MASK,
              "Message type", HFILL }},
        { &hf_mtp3mg_coo_ansi_slc,
            { "Signalling Link Code", "mtp3mg.slc",
              FT_UINT16, BASE_DEC, NULL, ANSI_COO_SLC_MASK,
              "SLC of affected link", HFILL }},
        { &hf_mtp3mg_coo_ansi_fsn,
            { "Forward Sequence Number", "mtp3mg.fsn",
              FT_UINT16, BASE_DEC, NULL, ANSI_COO_FSN_MASK,
              "Forward Sequence Number of last accepted message", HFILL }},
        { &hf_mtp3mg_coo_itu_fsn,
            { "Forward Sequence Number", "mtp3mg.fsn",
              FT_UINT8, BASE_DEC, NULL, ITU_COO_FSN_MASK,
              "Forward Sequence Number of last accepted message", HFILL }},
        { &hf_mtp3mg_xco_ansi_slc,
            { "Signalling Link Code", "mtp3mg.slc",
              FT_UINT32, BASE_DEC, NULL, ANSI_XCO_SLC_MASK,
              "SLC of affected link", HFILL }},
        { &hf_mtp3mg_xco_ansi_fsn,
            { "Forward Sequence Number", "mtp3mg.fsn",
              FT_UINT32, BASE_DEC, NULL, ANSI_XCO_FSN_MASK,
              "Forward Sequence Number of last accepted message", HFILL }},
        { &hf_mtp3mg_xco_itu_fsn,
            { "Forward Sequence Number", "mtp3mg.fsn",
              FT_UINT24, BASE_DEC, NULL, 0x0,
              "Forward Sequence Number of last accepted message", HFILL }},
        { &hf_mtp3mg_cbd_ansi_slc,
            { "Signalling Link Code", "mtp3mg.slc",
              FT_UINT16, BASE_DEC, NULL, ANSI_CBD_SLC_MASK,
              "SLC of affected link", HFILL }},
        { &hf_mtp3mg_cbd_ansi_cbc,
            { "Change Back Code", "mtp3mg.cbc",
              FT_UINT16, BASE_DEC, NULL, ANSI_CBD_CBC_MASK,
              NULL, HFILL }},
        { &hf_mtp3mg_cbd_itu_cbc,
            { "Change Back Code", "mtp3mg.cbc",
              FT_UINT8, BASE_DEC, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_cbd_japan_cbc,
            { "Change Back Code", "mtp3mg.cbc",
              FT_UINT8, BASE_DEC, NULL, JAPAN_CBD_CBC_MASK,
              NULL, HFILL }},
        { &hf_mtp3mg_eco_ansi_slc,
            { "Signalling Link Code", "mtp3mg.slc",
              FT_UINT8, BASE_DEC, NULL, ANSI_ECO_SLC_MASK,
              "SLC of affected link", HFILL }},
        { &hf_mtp3mg_ansi_apc,
            { "Affected Point Code", "mtp3mg.ansi_apc",
              FT_STRING, BASE_NONE, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_apc_member,
            { "Affected Point Code member", "mtp3mg.apc.member",
              FT_UINT24, BASE_DEC, NULL, ANSI_MEMBER_MASK,
              NULL, HFILL }},
        { &hf_mtp3mg_apc_cluster,
            { "Affected Point Code cluster", "mtp3mg.apc.cluster",
              FT_UINT24, BASE_DEC, NULL, ANSI_CLUSTER_MASK,
              NULL, HFILL }},
        { &hf_mtp3mg_apc_network,
            { "Affected Point Code network", "mtp3mg.apc.network",
              FT_UINT24, BASE_DEC, NULL, ANSI_NETWORK_MASK,
              NULL, HFILL }},
        { &hf_mtp3mg_tfc_ansi_status,
            { "Status", "mtp3mg.status",
              FT_UINT8, BASE_DEC, NULL, ANSI_TFC_STATUS_MASK,
              "Congestion status", HFILL }},
        { &hf_mtp3mg_itu_apc,
            { "Affected Point Code (ITU)", "mtp3mg.apc",
              FT_UINT16, BASE_DEC, NULL, ITU_PC_MASK,
              NULL, HFILL }},
        { &hf_mtp3mg_tfc_itu_status,
            { "Status", "mtp3mg.status",
              FT_UINT16, BASE_DEC, NULL, ITU_TFC_STATUS_MASK,
              "Congestion status", HFILL }},
        { &hf_mtp3mg_chinese_apc,
            { "Affected Point Code", "mtp3mg.chinese_apc",
              FT_STRING, BASE_NONE, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_tfc_japan_spare,
            { "TFC spare (Japan)", "mtp3mg.japan_spare",
              FT_UINT8, BASE_HEX, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_japan_apc,
            { "Affected Point Code", "mtp3mg.japan_apc",
              FT_UINT16, BASE_DEC, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_tfc_japan_status,
            { "Status", "mtp3mg.japan_status",
              FT_UINT8, BASE_DEC, NULL, JAPAN_TFC_STATUS_MASK,
              NULL, HFILL }},
        { &hf_mtp3mg_tfc_japan_status_spare,
            { "Spare (Japan)", "mtp3mg.japan_spare",
              FT_UINT8, BASE_HEX, NULL, JAPAN_TFC_STATUS_SPARE_MASK,
              NULL, HFILL }},
        { &hf_mtp3mg_tfm_japan_count,
            { "Count of Affected Point Codes (Japan)", "mtp3mg.japan_count",
              FT_UINT8, BASE_DEC, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_tfm_japan_spare,
            { "Spare (Japan)", "mtp3mg.japan_spare",
              FT_UINT16, BASE_DEC, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_rsm_japan_count,
            { "Count of Affected Point Codes (Japan)", "mtp3mg.japan_count",
              FT_UINT8, BASE_DEC, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_rsm_japan_spare,
            { "Spare (Japan)", "mtp3mg.japan_spare",
              FT_UINT16, BASE_DEC, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_mim_ansi_slc,
            { "Signalling Link Code", "mtp3mg.slc",
              FT_UINT8, BASE_DEC, NULL, ANSI_MIM_SLC_MASK,
              "SLC of affected link", HFILL }},
        { &hf_mtp3mg_dlc_ansi_slc,
            { "Signalling Link Code", "mtp3mg.slc",
              FT_UINT24, BASE_DEC, NULL, ANSI_DLC_SLC_MASK,
              "SLC of affected link", HFILL }},
        { &hf_mtp3mg_dlc_ansi_link,
            { "Link", "mtp3mg.link",
              FT_UINT24, BASE_DEC, NULL, ANSI_DLC_LINK_MASK,
              "CIC of BIC used to carry data", HFILL }},
        { &hf_mtp3mg_dlc_itu_link,
            { "Link", "mtp3mg.link",
              FT_UINT16, BASE_DEC, NULL, ITU_DLC_LINK_MASK,
              "CIC of BIC used to carry data", HFILL }},
        { &hf_mtp3mg_upu_user,
            { "User", "mtp3mg.user",
              FT_UINT8, BASE_HEX, VALS(service_indicator_code_vals), UPU_USER_MASK,
              "Unavailable user part", HFILL }},
        { &hf_mtp3mg_upu_cause,
            { "Cause", "mtp3mg.cause",
              FT_UINT8, BASE_HEX, VALS(upu_cause_values), UPU_CAUSE_MASK,
              "Cause of user unavailability", HFILL }},
        { &hf_mtp3test_h0,
            { "H0 (Message Group)", "mtp3mg.test.h0",
              FT_UINT8, BASE_HEX, VALS(test_h0_message_type_values), H0_MASK,
              "Message group identifier", HFILL }},
        { &hf_mtp3mg_test_h1,
            { "H1 (Message)", "mtp3mg.test.h1",
              FT_UINT8, BASE_HEX, VALS(test_h1_message_type_values), H1_MASK,
              "SLT message type", HFILL }},
        { &hf_mtp3mg_test_length,
            { "Test length", "mtp3mg.test.length",
              FT_UINT8, BASE_DEC, NULL, H1_MASK,
              "Signalling link test pattern length", HFILL }},
        { &hf_mtp3mg_japan_test,
            { "Japan test message", "mtp3mg.test",
              FT_UINT8, BASE_HEX, VALS(japan_test_message_type_values), 0x0,
              "Japan test message type", HFILL }},
        { &hf_mtp3mg_japan_test_spare,
            { "Japan test message spare", "mtp3mg.test.spare",
              FT_UINT8, BASE_HEX, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_japan_test_pattern,
            { "Japan test message pattern", "mtp3mg.test.pattern",
              FT_UINT16, BASE_HEX, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_japan_spare,
            { "Japan management spare", "mtp3mg.spare",
              FT_UINT8, BASE_HEX, NULL, 0x0,
              NULL, HFILL }},
        { &hf_mtp3mg_test_ansi_slc,
            { "Signalling Link Code", "mtp3mg.slc",
              FT_UINT8, BASE_DEC, NULL, ANSI_TEST_SLC_MASK,
              "SLC of affected link", HFILL }},
        { &hf_mtp3mg_test_pattern,
            { "Test pattern", "mtp3mg.test_pattern",
              FT_BYTES, BASE_NONE, NULL, 0x0,
              NULL, HFILL }},
  };

    /* Setup protocol subtree array */
    static int *ett[] = {
        &ett_mtp3mg,
        &ett_mtp3mg_fcm_apc,
        &ett_mtp3mg_tfm_apc,
        &ett_mtp3mg_rsm_apc,
        &ett_mtp3mg_upu_apc
    };

    static ei_register_info ei[] = {
        { &ei_mtp3mg_unknown_message, { "mtp3mg.unknown_message", PI_PROTOCOL, PI_WARN, "Unknown message", EXPFILL }},
    };

    expert_module_t* expert_mtp3mg;

    /* Register the protocol name and description */
    proto_mtp3mg = proto_register_protocol("Message Transfer Part Level 3 Management",
                                           "MTP3MG", "mtp3mg");
    mtp3mg_handle = register_dissector("mtp3mg", dissect_mtp3mg, proto_mtp3mg);

    /* Required calls to register the header fields and subtrees used */
    proto_register_field_array(proto_mtp3mg, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    expert_mtp3mg = expert_register_protocol(proto_mtp3mg);
    expert_register_field_array(expert_mtp3mg, ei, array_length(ei));

}

void
proto_reg_handoff_mtp3mg(void)
{
    dissector_add_uint("mtp3.service_indicator", MTP_SI_SNM, mtp3mg_handle);

    /*  SI 1 is unused in ANSI and SI 2 is unused in ITU, so it's okay for us
     *  to grab both (regardless of mtp3.standard setting) here.
     */
    dissector_add_uint("mtp3.service_indicator", MTP_SI_MTN, mtp3mg_handle);
    dissector_add_uint("mtp3.service_indicator", MTP_SI_MTNS, mtp3mg_handle);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */