aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-t38.c
blob: 824c86eeb9531081a8794065abd6a2b65f735a42 (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
/* packet-t38.c
 * Routines for T.38 packet dissection
 * 2003  Hans Viens
 * 2004  Alejandro Vaquero, add support Conversations for SDP
 * 2006  Alejandro Vaquero, add T30 reassemble and dissection
 *
 * $Id$
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * 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.
 */


/* Depending on what ASN.1 specification is used you may have to change
 * the preference setting regarding Pre-Corrigendum ASN.1 specification:
 * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/1998/T38.html  (Pre-Corrigendum=TRUE)
 * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2003/T38(1998).html (Pre-Corrigendum=TRUE)
 *
 * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2003/T38(2002).html (Pre-Corrigendum=FALSE)
 * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2002/t38.html  (Pre-Corrigendum=FALSE)
 * http://www.itu.int/ITU-T/asn1/database/itu-t/t/t38/2002-Amd1/T38.html (Pre-Corrigendum=FALSE)
 */

/* TO DO:  
 * - TCP desegmentation is currently not supported for T.38 IFP directly over TCP. 
 * - H.245 dissectors should be updated to start conversations for T.38 similar to RTP.
 * - Sometimes the last octet is not high-lighted when selecting something in the tree. Bug in PER dissector? 
 * - Add support for RTP payload audio/t38 (draft-jones-avt-audio-t38-03.txt), i.e. T38 in RTP packets.
 */


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

#include <glib.h>
#include <epan/packet.h>
#include <epan/reassemble.h>
#include <epan/conversation.h>
#include <epan/tap.h>
#include <epan/expert.h>

#include <stdio.h>
#include <string.h>

#include "packet-t38.h"
#include <epan/prefs.h>
#include <epan/ipproto.h>
#include "packet-per.h"
#include <epan/prefs.h>
#include "packet-tpkt.h"
#include <epan/emem.h>

#define PORT_T38 6004  
static guint global_t38_tcp_port = PORT_T38;
static guint global_t38_udp_port = PORT_T38;

static int t38_tap = -1;

/*
* Variables to allow for proper deletion of dissector registration when
* the user changes port from the gui.
*/
static guint tcp_port = 0;
static guint udp_port = 0;

/* dissect using the Pre Corrigendum T.38 ASN.1 specification (1998) */
static gboolean use_pre_corrigendum_asn1_specification = TRUE;

/* dissect packets that looks like RTP version 2 packets as RTP     */
/* instead of as T.38. This may result in that some T.38 UPTL       */
/* packets with sequence number values higher than 32767 may be     */
/* shown as RTP packets.                                            */ 
static gboolean dissect_possible_rtpv2_packets_as_rtp = FALSE;


/* Reassembly of T.38 PDUs over TPKT over TCP */
static gboolean t38_tpkt_reassembly = TRUE;

/* Preference setting whether TPKT header is used when sending T.38 over TCP.
 * The default setting is Maybe where the dissector will look on the first
 * bytes to try to determine whether TPKT header is used or not. This may not
 * work so well in some cases. You may want to change the setting to Always or
 * Newer.
 */
#define T38_TPKT_NEVER 0   /* Assume that there is never a TPKT header    */
#define T38_TPKT_ALWAYS 1  /* Assume that there is always a TPKT header   */
#define T38_TPKT_MAYBE 2   /* Assume TPKT if first octets are 03-00-xx-xx */
static gint t38_tpkt_usage = T38_TPKT_MAYBE;

static const enum_val_t t38_tpkt_options[] = {
  {"never", "Never", T38_TPKT_NEVER},
  {"always", "Always", T38_TPKT_ALWAYS},
  {"maybe", "Maybe", T38_TPKT_MAYBE},
  {NULL, NULL, -1}
};



/* T30 */
static int proto_t30 = -1;
static int hf_t30_Address = -1;
static int hf_t30_Control = -1;
static int hf_t30_Facsimile_Control = -1;
static int hf_t30_fif_sm = -1; 
static int hf_t30_fif_rtif = -1; 
static int hf_t30_fif_3gmn = -1; 
static int hf_t30_fif_v8c = -1; 
static int hf_t30_fif_op = -1; 
static int hf_t30_fif_rtfc = -1;
static int hf_t30_fif_rfo = -1;
static int hf_t30_fif_dsr = -1;
static int hf_t30_fif_dsr_dcs = -1;
static int hf_t30_fif_res = -1;
static int hf_t30_fif_tdcc = -1;
static int hf_t30_fif_rwc = -1;
static int hf_t30_fif_rw_dcs = -1;
static int hf_t30_fif_rlc = -1;
static int hf_t30_fif_rl_dcs = -1;
static int hf_t30_fif_msltcr = -1;
static int hf_t30_fif_mslt_dcs = -1;
static int hf_t30_fif_ext = -1;
static int hf_t30_fif_cm = -1;
static int hf_t30_fif_ecm = -1;
static int hf_t30_fif_fs_dcs = -1;
static int hf_t30_fif_t6 = -1;
static int hf_t30_fif_fvc = -1;
static int hf_t30_fif_mspc = -1;
static int hf_t30_fif_ps = -1;
static int hf_t30_fif_t43 = -1;
static int hf_t30_fif_pi = -1;
static int hf_t30_fif_vc32k = -1;
static int hf_t30_fif_r8x15 = -1;
static int hf_t30_fif_300x300 = -1;
static int hf_t30_fif_r16x15 = -1;
static int hf_t30_fif_ibrp = -1;
static int hf_t30_fif_mbrp = -1;
static int hf_t30_fif_msltchr = -1;
static int hf_t30_fif_rts = -1;
static int hf_t30_fif_sp = -1;
static int hf_t30_fif_sc = -1;
static int hf_t30_fif_passw = -1;
static int hf_t30_fif_sit = -1;
static int hf_t30_fif_rttd = -1;
static int hf_t30_fif_bft = -1;
static int hf_t30_fif_dtm = -1;
static int hf_t30_fif_edi = -1;
static int hf_t30_fif_btm = -1;
static int hf_t30_fif_rttcmmd = -1;
static int hf_t30_fif_chrm = -1;
static int hf_t30_fif_mm = -1;
static int hf_t30_fif_pm26 = -1;
static int hf_t30_fif_dnc = -1;
static int hf_t30_fif_do = -1;
static int hf_t30_fif_jpeg = -1;
static int hf_t30_fif_fcm = -1;
static int hf_t30_fif_pht = -1;
static int hf_t30_fif_12c = -1;
static int hf_t30_fif_ns = -1;
static int hf_t30_fif_ci = -1;
static int hf_t30_fif_cgr = -1;
static int hf_t30_fif_nalet = -1;
static int hf_t30_fif_naleg = -1;
static int hf_t30_fif_spscb = -1;
static int hf_t30_fif_spsco = -1;
static int hf_t30_fif_hkm = -1;
static int hf_t30_fif_rsa = -1;
static int hf_t30_fif_oc = -1;
static int hf_t30_fif_hfx40 = -1;
static int hf_t30_fif_acn2c = -1;
static int hf_t30_fif_acn3c = -1;
static int hf_t30_fif_hfx40i = -1;
static int hf_t30_fif_ahsn2 = -1;
static int hf_t30_fif_ahsn3 = -1;
static int hf_t30_fif_t441 = -1;
static int hf_t30_fif_t442 = -1;
static int hf_t30_fif_t443 = -1;
static int hf_t30_fif_plmss = -1;
static int hf_t30_fif_cg300 = -1;
static int hf_t30_fif_100x100cg = -1;
static int hf_t30_fif_spcbft = -1;
static int hf_t30_fif_ebft = -1;
static int hf_t30_fif_isp = -1;
static int hf_t30_fif_ira = -1;
static int hf_t30_fif_600x600 = -1;
static int hf_t30_fif_1200x1200 = -1;
static int hf_t30_fif_300x600 = -1;
static int hf_t30_fif_400x800 = -1;
static int hf_t30_fif_600x1200 = -1;
static int hf_t30_fif_cg600x600 = -1;
static int hf_t30_fif_cg1200x1200 = -1;
static int hf_t30_fif_dspcam = -1;
static int hf_t30_fif_dspccm = -1;
static int hf_t30_fif_bwmrcp = -1;
static int hf_t30_fif_t45 = -1;
static int hf_t30_fif_sdmc = -1;
static int hf_t30_fif_number = -1;
static int hf_t30_fif_country_code = -1;
static int hf_t30_fif_non_stand_bytes = -1;
static int hf_t30_t4_frame_num = -1;
static int hf_t30_t4_data = -1;
static int hf_t30_partial_page_fcf2 = -1;
static int hf_t30_partial_page_i1 = -1;
static int hf_t30_partial_page_i2 = -1;
static int hf_t30_partial_page_i3 = -1;

static gint ett_t30 = -1;
static gint ett_t30_fif = -1;

/* T38 */
static dissector_handle_t t38_udp_handle;
static dissector_handle_t t38_tcp_handle;
static dissector_handle_t t38_tcp_pdu_handle;
static dissector_handle_t rtp_handle;

static guint32 Type_of_msg_value;
static guint32 Data_Field_field_type_value;
static guint32 Data_value;
static guint32 T30ind_value;
static guint32 Data_Field_item_num;

static int proto_t38 = -1;
static int hf_t38_IFPPacket = -1;
static int hf_t38_Type_of_msg = -1;
static int hf_t38_t30_indicator = -1;
static int hf_t38_data = -1;
static int hf_t38_Data_Field = -1;
static int hf_t38_Data_Field_item = -1;
static int hf_t38_Data_Field_field_type = -1;
static int hf_t38_Data_Field_field_data = -1;
static int hf_t38_UDPTLPacket = -1;
static int hf_t38_seq_number = -1;
static int hf_t38_primary_ifp_packet = -1;
static int hf_t38_primary_ifp_packet_length = -1;
static int hf_t38_error_recovery = -1;
static int hf_t38_secondary_ifp_packets = -1;
static int hf_t38_secondary_ifp_packets_item = -1;
static int hf_t38_secondary_ifp_packets_item_length = -1;
static int hf_t38_fec_info = -1;
static int hf_t38_fec_npackets = -1;
static int hf_t38_fec_data = -1;
static int hf_t38_fec_data_item = -1;

/* T38 setup fields */
static int hf_t38_setup        = -1;
static int hf_t38_setup_frame  = -1;
static int hf_t38_setup_method = -1;

/* T38 Data reassemble fields */
static int hf_data_fragments = -1;
static int hf_data_fragment = -1;
static int hf_data_fragment_overlap = -1;
static int hf_data_fragment_overlap_conflicts = -1;
static int hf_data_fragment_multiple_tails = -1;
static int hf_data_fragment_too_long_fragment = -1;
static int hf_data_fragment_error = -1;
static int hf_data_reassembled_in = -1;

static gint ett_t38 = -1;
static gint ett_t38_IFPPacket = -1;
static gint ett_t38_Type_of_msg = -1;
static gint ett_t38_t30_indicator = -1;
static gint ett_t38_data = -1;
static gint ett_t38_Data_Field = -1;
static gint ett_t38_Data_Field_item = -1;
static gint ett_t38_Data_Field_field_type = -1;
static gint ett_t38_UDPTLPacket = -1;
static gint ett_t38_error_recovery = -1;
static gint ett_t38_secondary_ifp_packets = -1;
static gint ett_t38_fec_info = -1;
static gint ett_t38_fec_data = -1;
static gint ett_t38_setup = -1;

static gint ett_data_fragment = -1;
static gint ett_data_fragments = -1;

static gboolean primary_part = TRUE;
static guint32 seq_number = 0;

/* Tables for reassembly of Data fragments. */
static GHashTable *data_fragment_table = NULL;
static GHashTable *data_reassembled_table = NULL;

static const fragment_items data_frag_items = {
	/* Fragment subtrees */
	&ett_data_fragment,
	&ett_data_fragments,
	/* Fragment fields */
	&hf_data_fragments,
	&hf_data_fragment,
	&hf_data_fragment_overlap,
	&hf_data_fragment_overlap_conflicts,
	&hf_data_fragment_multiple_tails,
	&hf_data_fragment_too_long_fragment,
	&hf_data_fragment_error,
	/* Reassembled in field */
	&hf_data_reassembled_in,
	/* Tag */
	"Data fragments"
};

typedef struct _fragment_key {
	address src;
	address dst;
	guint32	id;
} fragment_key;

static conversation_t *p_conv= NULL;
static t38_conv *p_t38_conv = NULL;
static t38_conv *p_t38_packet_conv = NULL;
static t38_conv_info *p_t38_conv_info = NULL;
static t38_conv_info *p_t38_packet_conv_info = NULL;

/* RTP Version is the first 2 bits of the first octet in the UDP payload*/
#define RTP_VERSION(octet)	((octet) >> 6)

void proto_reg_handoff_t38(void);

static void show_setup_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, conversation_t *p_conv, t38_conv *p_t38_conv);
/* Preferences bool to control whether or not setup info should be shown */
static gboolean global_t38_show_setup_info = TRUE;

/* Can tap up to 4 T38 packets within same packet */
/* We only tap the primary part, not the redundancy */
#define MAX_T38_MESSAGES_IN_PACKET 4
static t38_packet_info t38_info_arr[MAX_T38_MESSAGES_IN_PACKET];
static int t38_info_current=0;
static t38_packet_info *t38_info=NULL;

static void t38_defragment_init(void)
{
	/* Init reassemble tables */
	fragment_table_init(&data_fragment_table);
	reassembled_table_init(&data_reassembled_table);
}


/* Set up an T38 conversation */
void t38_add_address(packet_info *pinfo,
                     address *addr, int port,
                     int other_port,
                     const gchar *setup_method, guint32 setup_frame_number)
{
        address null_addr;
        conversation_t* p_conv;
        t38_conv* p_conv_data = NULL;

        /*
         * If this isn't the first time this packet has been processed,
         * we've already done this work, so we don't need to do it
         * again.
         */
        if (pinfo->fd->flags.visited)
        {
                return;
        }

        SET_ADDRESS(&null_addr, AT_NONE, 0, NULL);

        /*
         * Check if the ip address and port combination is not
         * already registered as a conversation.
         */
        p_conv = find_conversation( setup_frame_number, addr, &null_addr, PT_UDP, port, other_port,
                                NO_ADDR_B | (!other_port ? NO_PORT_B : 0));

        /*
         * If not, create a new conversation.
         */
        if ( !p_conv || p_conv->setup_frame != setup_frame_number) {
                p_conv = conversation_new( setup_frame_number, addr, &null_addr, PT_UDP,
                                           (guint32)port, (guint32)other_port,
                                                                   NO_ADDR2 | (!other_port ? NO_PORT2 : 0));
        }

        /* Set dissector */
        conversation_set_dissector(p_conv, t38_udp_handle);

        /*
         * Check if the conversation has data associated with it.
         */
        p_conv_data = conversation_get_proto_data(p_conv, proto_t38);

        /*
         * If not, add a new data item.
         */
        if ( ! p_conv_data ) {
                /* Create conversation data */
                p_conv_data = se_alloc(sizeof(t38_conv));

                conversation_add_proto_data(p_conv, proto_t38, p_conv_data);
        }

        /*
         * Update the conversation data.
         */
        strncpy(p_conv_data->setup_method, setup_method, MAX_T38_SETUP_METHOD_SIZE);
        p_conv_data->setup_method[MAX_T38_SETUP_METHOD_SIZE] = '\0';
        p_conv_data->setup_frame_number = setup_frame_number;
		p_conv_data->src_t38_info.reass_ID = 0;
		p_conv_data->src_t38_info.reass_start_seqnum = -1;
		p_conv_data->src_t38_info.reass_data_type = 0;
		p_conv_data->src_t38_info.last_seqnum = -1;
		p_conv_data->src_t38_info.packet_lost = 0;
		p_conv_data->src_t38_info.burst_lost = 0;
		p_conv_data->src_t38_info.time_first_t4_data = 0;


		p_conv_data->dst_t38_info.reass_ID = 0;
		p_conv_data->dst_t38_info.reass_start_seqnum = -1;
		p_conv_data->dst_t38_info.reass_data_type = 0;
		p_conv_data->dst_t38_info.last_seqnum = -1;
		p_conv_data->dst_t38_info.packet_lost = 0;
		p_conv_data->dst_t38_info.burst_lost = 0;
		p_conv_data->dst_t38_info.time_first_t4_data = 0;
}


/* T30 Routines */

static int
dissect_t30_NULL(tvbuff_t *tvb _U_, int offset, packet_info *pinfo _U_, proto_tree *tree _U_)
{
	return offset;
}

static const value_string t30_control_vals[] = {
	{ 0xC0, "non-final frames within the procedure" },
	{ 0xC8, "final frames within the procedure" },
	{ 0,    NULL }
};


#define	T30_FC_DIS	0x01
#define	T30_FC_CSI	0x02
#define	T30_FC_NSF	0x04
#define	T30_FC_DTC	0x81
#define	T30_FC_CIG	0x82
#define	T30_FC_NSC	0x84
#define	T30_FC_PWD	0x83
#define	T30_FC_SEP	0x85
#define	T30_FC_PSA	0x86
#define	T30_FC_CIA	0x87
#define	T30_FC_ISP	0x88
#define	T30_FC_DCS	0x41
#define	T30_FC_TSI	0x42
#define	T30_FC_NSS	0x44
#define	T30_FC_SUB	0x43
#define	T30_FC_SID	0x45
#define	T30_FC_TSA	0x46
#define	T30_FC_IRA	0x47
#define	T30_FC_CFR	0x21
#define	T30_FC_FTT	0x22
#define	T30_FC_CSA	0x24
#define	T30_FC_EOM	0x71
#define	T30_FC_MPS	0x72
#define	T30_FC_EOP	0x74
#define	T30_FC_PRI_EOM	0x79
#define	T30_FC_PRI_MPS	0x7A
#define	T30_FC_PRI_EOP	0x7C
#define	T30_FC_PRI_EOP2	0x78
#define	T30_FC_MCF	0x31
#define	T30_FC_RTP	0x33
#define	T30_FC_RTN	0x32
#define	T30_FC_PIP	0x35
#define	T30_FC_PIN	0x34
#define	T30_FC_FDM	0x3F
#define	T30_FC_DCN	0x5F
#define	T30_FC_CRP	0x58
#define	T30_FC_FNV	0x53
#define	T30_FC_TNR	0x57
#define	T30_FC_TR	0x56
#define	T30_FC_MCF	0x31
#define	T30_FC_PID	0x36
#define	T30_FC_PPR	0x3D
#define	T30_FC_RNR	0x37
#define	T30_FC_CRP	0x58
#define	T30_FC_CTC	0x48
#define	T30_FC_CTR	0x23
#define	T30_FC_PPS	0x7D
#define	T30_FC_EOR	0x73
#define	T30_FC_RR	0x76
#define	T30_FC_ERR	0x38
#define	T30_FC_FCD	0x60
#define	T30_FC_RCP	0x61

const value_string t30_facsimile_control_field_vals[] = {
	{ T30_FC_DIS, "Digital Identification Signal" },
	{ T30_FC_CSI, "Called Subscriber Identification" },
	{ T30_FC_NSF, "Non-Standard Facilities" },
	{ T30_FC_DTC, "Digital Transmit Command" },
	{ T30_FC_CIG, "Calling Subscriber Identification" },
	{ T30_FC_NSC, "Non-Standard facilities Command" },
	{ T30_FC_PWD, "Password" },
	{ T30_FC_SEP, "Selective Polling" },
	{ T30_FC_PSA, "Polled Subaddress" },
	{ T30_FC_CIA, "Calling subscriber Internet Address" },
	{ T30_FC_ISP, "Internet Selective Polling Address" },
	{ T30_FC_DCS, "Digital Command Signal" },
	{ T30_FC_TSI, "Transmitting Subscriber Identification" },
	{ T30_FC_NSS, "Non-Standard facilities Set-up" },
	{ T30_FC_SUB, "Subaddress" },
	{ T30_FC_SID, "Sender Identification" },
	{ T30_FC_TSA, "Transmitting Subscriber Internet address" },
	{ T30_FC_IRA, "Internet Routing Address" },
	{ T30_FC_CFR, "Confirmation To Receive" },
	{ T30_FC_FTT, "Failure To Train" },
	{ T30_FC_CSA, "Called Subscriber Internet Address" },
	{ T30_FC_EOM, "End Of Message" },
	{ T30_FC_MPS, "MultiPage Signal" },
	{ T30_FC_EOP, "End Of Procedure" },
	{ T30_FC_PRI_EOM, "Procedure Interrupt-End Of Message" },
	{ T30_FC_PRI_MPS, "Procedure Interrupt-MultiPage Signal" },
	{ T30_FC_PRI_EOP, "Procedure Interrupt-End Of Procedure" },
	{ T30_FC_PRI_EOP2, "Procedure Interrupt-End Of Procedure" },
	{ T30_FC_MCF, "Message Confirmation" },
	{ T30_FC_RTP, "Retrain Positive" },
	{ T30_FC_RTN, "Retrain Negative" },
	{ T30_FC_PIP, "Procedure Interrupt Positive" },
	{ T30_FC_PIN, "Procedure Interrupt Negative" },
	{ T30_FC_FDM, "File Diagnostics Message" },
	{ T30_FC_DCN, "Disconnect" },
	{ T30_FC_CRP, "Command Repeat" },
	{ T30_FC_FNV, "Field Not Valid" },
	{ T30_FC_TNR, "Transmit not ready" },
	{ T30_FC_TR, "Transmit ready" },
	{ T30_FC_MCF, "Message Confirmation" },
	{ T30_FC_PID, "Procedure Interrupt Disconnect" },
	{ T30_FC_PPR, "Partial Page Request" },
	{ T30_FC_RNR, "Receive Not Ready" },
	{ T30_FC_CRP, "Command Repeat" },
	{ T30_FC_CTC, "Continue To Correct" },
	{ T30_FC_CTR, "Response for Continue To Correct" },
	{ T30_FC_PPS, "Partial Page Signal" },
	{ T30_FC_EOR, "End Of Retransmission" },
	{ T30_FC_RR, "Receive Ready" },
	{ T30_FC_ERR, "Response for End of Retransmission" },
	{ T30_FC_FCD, "Facsimile coded data" },
	{ T30_FC_RCP, "Return to control for partial page" },
	{ 0, NULL }
};

const value_string t30_facsimile_control_field_vals_short[] = {
	{ T30_FC_DIS, "DIS" },
	{ T30_FC_CSI, "CSI" },
	{ T30_FC_NSF, "NSF" },
	{ T30_FC_DTC, "DTC" },
	{ T30_FC_CIG, "CIG" },
	{ T30_FC_NSC, "NSC" },
	{ T30_FC_PWD, "PWD" },
	{ T30_FC_SEP, "SEP" },
	{ T30_FC_PSA, "PSA" },
	{ T30_FC_CIA, "CIA" },
	{ T30_FC_ISP, "ISP" },
	{ T30_FC_DCS, "DCS" },
	{ T30_FC_TSI, "TSI" },
	{ T30_FC_NSS, "NSS" },
	{ T30_FC_SUB, "SUB" },
	{ T30_FC_SID, "SID" },
	{ T30_FC_TSA, "TSA" },
	{ T30_FC_IRA, "IRA" },
	{ T30_FC_CFR, "CFR" },
	{ T30_FC_FTT, "FTT" },
	{ T30_FC_CSA, "CSA" },
	{ T30_FC_EOM, "EOM" },
	{ T30_FC_MPS, "MPS" },
	{ T30_FC_EOP, "EOP" },
	{ T30_FC_PRI_EOM, "PRI_EOM" },
	{ T30_FC_PRI_MPS, "PRI_MPS" },
	{ T30_FC_PRI_EOP, "EOP" },
	{ T30_FC_PRI_EOP2, "EOP2" },
	{ T30_FC_MCF, "MCF" },
	{ T30_FC_RTP, "RTP" },
	{ T30_FC_RTN, "RTN" },
	{ T30_FC_PIP, "PIP" },
	{ T30_FC_PIN, "PIN" },
	{ T30_FC_FDM, "FDM" },
	{ T30_FC_DCN, "DCN" },
	{ T30_FC_CRP, "CRP" },
	{ T30_FC_FNV, "FNV" },
	{ T30_FC_TNR, "TNR" },
	{ T30_FC_TR, "TR" },
	{ T30_FC_MCF, "MCF" },
	{ T30_FC_PID, "PID" },
	{ T30_FC_PPR, "PPR" },
	{ T30_FC_RNR, "RNR" },
	{ T30_FC_CRP, "CRP" },
	{ T30_FC_CTC, "CTC" },
	{ T30_FC_CTR, "CTR" },
	{ T30_FC_PPS, "PPS" },
	{ T30_FC_EOR, "EOR" },
	{ T30_FC_RR, "RR" },
	{ T30_FC_ERR, "ERR" },
	{ T30_FC_FCD, "FCD" },
	{ T30_FC_RCP, "RCP" },
	{ 0, NULL }
};

static const value_string t30_data_signalling_rate_vals[] = {
	{ 0x00, "ITU-T V.27 ter fall-back mode" },
	{ 0x04, "ITU-T V.27 ter" },
	{ 0x08, "ITU-T V.29" },
	{ 0x0C, "ITU-T V.27 ter and V.29" },
	{ 0x02, "Not used" },
	{ 0x06, "Reserved" },
	{ 0x0A, "Not used" },
	{ 0x0E, "Invalid" },
	{ 0x01, "Not used" },
	{ 0x05, "Reserved" },
	{ 0x09, "Not used" },
	{ 0x0D, "ITU-T V.27 ter, V.29, and V.17" },
	{ 0x03, "Not used" },
	{ 0x07, "Reserved" },
	{ 0x0B, "Not used" },
	{ 0x0F, "Reserved" },
};

static const value_string t30_data_signalling_rate_dcs_vals[] = {
	{ 0x00, "2400 bit/s, ITU-T V.27 ter" },
	{ 0x04, "4800 bit/s, ITU-T V.27 ter" },
	{ 0x08, "9600 bit/s, ITU-T V.29" },
	{ 0x0C, "7200 bit/s, ITU-T V.29" },
	{ 0x02, "Invalid" },
	{ 0x06, "Invalid" },
	{ 0x0A, "Reserved" },
	{ 0x0E, "Reserved" },
	{ 0x01, "14 400 bit/s, ITU-T V.17" },
	{ 0x05, "12 000 bit/s, ITU-T V.17" },
	{ 0x09, "9600 bit/s, ITU-T V.17" },
	{ 0x0D, "7200 bit/s, ITU-T V.17" },
	{ 0x03, "Reserved" },
	{ 0x07, "Reserved" },
	{ 0x0B, "Reserved" },
	{ 0x0F, "Reserved" },
};

static const value_string t30_recording_width_capabilities_vals[] = {
	{ 0x00, "Scan line length 215 mm +- 1%" },
	{ 0x01, "Scan line length 215 mm +- 1% and Scan line length 255 mm +- 1% and Scan line length 303 mm +- 1%" },
	{ 0x02, "Scan line length 215 mm +- 1% and Scan line length 255 mm +- 1%" },
	{ 0x03, "Invalid" },
};

static const value_string t30_recording_width_dcs_vals[] = {
	{ 0x00, "Scan line length 215 mm +- 1%" },
	{ 0x01, "Scan line length 303 mm +- 1%" },
	{ 0x02, "Scan line length 255 mm +- 1%" },
	{ 0x03, "Invalid" },
};

static const value_string t30_recording_length_capability_vals[] = {
	{ 0x00, "A4 (297 mm)" },
	{ 0x01, "Unlimited" },
	{ 0x02, "A4 (297 mm) and B4 (364 mm)" },
	{ 0x03, "Invalid" },
};

static const value_string t30_recording_length_dcs_vals[] = {
	{ 0x00, "A4 (297 mm)" },
	{ 0x01, "Unlimited" },
	{ 0x02, "B4 (364 mm)" },
	{ 0x03, "Invalid" },
};

static const value_string t30_minimum_scan_line_time_rec_vals[] = {
	{ 0x00, "20 ms at 3.85 l/mm: T7.7 = T3.85" },
	{ 0x01, "40 ms at 3.85 l/mm: T7.7 = T3.85" },
	{ 0x02, "10 ms at 3.85 l/mm: T7.7 = T3.85" },
	{ 0x04, "05 ms at 3.85 l/mm: T7.7 = T3.85" },
	{ 0x03, "10 ms at 3.85 l/mm: T7.7 = 1/2 T3.85" },
	{ 0x06, "20 ms at 3.85 l/mm: T7.7 = 1/2 T3.85" },
	{ 0x05, "40 ms at 3.85 l/mm: T7.7 = 1/2 T3.85" },
	{ 0x07, "00 ms at 3.85 l/mm: T7.7 = T3.85" },
};

static const value_string t30_partial_page_fcf2_vals[] = {
	{ 0x00, "NULL code which indicates the partial page boundary" },
	{ 0xF1, "EOM in optional T.4 error correction mode" },
	{ 0xF2, "MPS in optional T.4 error correction mode" },
	{ 0xF4, "EOP in optional T.4 error correction mode" },
	{ 0xF8, "EOS in optional T.4 error correction mode" },
	{ 0xF9, "PRI-EOM in optional T.4 error correction mode" },
	{ 0xFA, "PRI-MPS in optional T.4 error correction mode" },
	{ 0xFC, "PRI-EOP in optional T.4 error correction mode" },
};

static const value_string t30_minimum_scan_line_time_dcs_vals[] = {
	{ 0x00, "20 ms" },
	{ 0x01, "40 ms" },
	{ 0x02, "10 ms" },
	{ 0x04, "05 ms" },
	{ 0x07, "00 ms" },
};

static const value_string t30_SharedDataMemory_capacity_vals[] = {
	{ 0x00, "Not available" },
	{ 0x01, "Level 1 = 1.0 Mbytes" },
	{ 0x02, "Level 2 = 2.0 Mbytes" },
	{ 0x03, "Level 3 = unlimited (i.e. >= 32 Mbytes)" },
};

static const true_false_string t30_octets_preferred_value = {
  "64 octets preferred",
  "256 octets preferred",
};

static const true_false_string t30_extension_ind_value = {
  "information continues through the next octet",
  "last octet",
};

static const true_false_string t30_compress_value = {
  "Uncompressed mode",
  "Compressed mode",
};

static const true_false_string t30_minimum_scan_value = {
  "T15.4 = 1/2 T7.7",
  "T15.4 = T7.7",
};

static const true_false_string t30_duplex_operation_value = {
  "Duplex  and half duplex operation",
  "Half duplex operation only",
};

static const true_false_string t30_frame_size_dcs_value = {
  "64 octets",
  "256 octets",
};

static const true_false_string t30_res_type_sel_value = {
  "inch based resolution",
  "metric based resolution",
};

guint8 reverse_byte(guint8 val)
{
	return ( ((val & 0x80)>>7) | ((val & 0x40)>>5) |
		((val & 0x20)>>3) | ((val & 0x10)>>1) |
		((val & 0x08)<<1) | ((val & 0x04)<<3) |
		((val & 0x02)<<5) | ((val & 0x01)<<7) );
}

#define LENGTH_T30_NUM	20
gchar * 
t30_get_string_numbers(tvbuff_t *tvb, int offset, int len)
{
	gchar *buf;
	int i;

	/* the lenght must be 20 bytes per T30 rec*/
	if (len != LENGTH_T30_NUM) return NULL;

	buf=ep_alloc(LENGTH_T30_NUM+1);

	for (i=0; i<LENGTH_T30_NUM; i++) 
		buf[LENGTH_T30_NUM-i-1] = reverse_byte(tvb_get_guint8(tvb, offset+i));
	
	/* add end of string */
	buf[LENGTH_T30_NUM] = '\0';

	return g_strstrip(buf);

}

static void
dissect_t30_numbers(tvbuff_t *tvb, int offset, packet_info *pinfo, int len, proto_tree *tree)
{
	gchar *str_num=NULL;

	str_num = t30_get_string_numbers(tvb, offset, len);
	if (str_num) {
		proto_tree_add_string_format(tree, hf_t30_fif_number, tvb, offset, LENGTH_T30_NUM, str_num, "Number: %s", str_num);

		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_fstr(pinfo->cinfo, COL_INFO, " - Number:%s", str_num );	

		g_snprintf(t38_info->desc, MAX_T38_DESC, "Num: %s", str_num);
	}
	else {
		proto_tree_add_text(tree, tvb, offset, tvb_reported_length_remaining(tvb, offset), "[MALFORMED OR SHORT PACKET: number of digits must be 20]");

		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_str(pinfo->cinfo, COL_INFO, " [MALFORMED OR SHORT PACKET: number of digits must be 20]" );	
	}
}

static void
dissect_t30_facsimile_coded_data(tvbuff_t *tvb, int offset, packet_info *pinfo, int len, proto_tree *tree)
{
	guint8 octet;
	gchar *t4_data;

	if (len < 2) {
		proto_tree_add_text(tree, tvb, offset, tvb_reported_length_remaining(tvb, offset), "[MALFORMED OR SHORT PACKET: FCD length must be at least 2 bytes]");
		expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_ERROR, "T30 FCD length must be at least 2 bytes");
		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_str(pinfo->cinfo, COL_INFO, " [MALFORMED OR SHORT PACKET]");				
		return;
	}
	
	octet = tvb_get_guint8(tvb, offset);
	proto_tree_add_uint(tree, hf_t30_t4_frame_num, tvb, offset, 1, reverse_byte(octet));
	offset++;

	if (check_col(pinfo->cinfo, COL_INFO))
		col_append_fstr(pinfo->cinfo, COL_INFO, " - Frame num:%d", reverse_byte(octet));

	g_snprintf(t38_info->desc, MAX_T38_DESC, "Frm num: %d", reverse_byte(octet));

	t4_data = ep_alloc(len-1);
	tvb_memcpy(tvb, t4_data, offset, len-1);
	proto_tree_add_bytes(tree, hf_t30_t4_data, tvb, offset, len-1, t4_data);
}

static void
dissect_t30_non_standard_cap(tvbuff_t *tvb, int offset, packet_info *pinfo, int len, proto_tree *tree)
{
	guint8 octet;
	gchar *non_standard_bytes;

	if (len < 2) {
		proto_tree_add_text(tree, tvb, offset, tvb_reported_length_remaining(tvb, offset), "[MALFORMED OR SHORT PACKET: NSC length must be at least 2 bytes]");
		expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_ERROR, "T30 NSC length must be at least 2 bytes");
		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_str(pinfo->cinfo, COL_INFO, " [MALFORMED OR SHORT PACKET]");				
		return;
	}
	
	octet = tvb_get_guint8(tvb, offset);
	proto_tree_add_uint(tree, hf_t30_fif_country_code, tvb, offset, 1, octet);
	offset++;

	non_standard_bytes = ep_alloc(len-1);
	tvb_memcpy(tvb, non_standard_bytes, offset, len-1);
	proto_tree_add_bytes(tree, hf_t30_fif_non_stand_bytes, tvb, offset, len-1, non_standard_bytes);

}

static void
dissect_t30_partial_page_signal(tvbuff_t *tvb, int offset, packet_info *pinfo, int len, proto_tree *tree)
{
	guint8 octet, page_count, block_count, frame_count;

	if (len != 4) {
		proto_tree_add_text(tree, tvb, offset, tvb_reported_length_remaining(tvb, offset), "[MALFORMED OR SHORT PACKET: PPS length must be 4 bytes]");
		expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_ERROR, "T30 PPS length must be 4 bytes");
		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_str(pinfo->cinfo, COL_INFO, " [MALFORMED OR SHORT PACKET]");				
		return;
	}

	octet = tvb_get_guint8(tvb, offset);
	proto_tree_add_uint(tree, hf_t30_partial_page_fcf2, tvb, offset, 1, octet);
	offset += 1;

	octet = tvb_get_guint8(tvb, offset);
	page_count = reverse_byte(octet);
	proto_tree_add_uint(tree, hf_t30_partial_page_i1, tvb, offset, 1, page_count);
	offset++;

	octet = tvb_get_guint8(tvb, offset);
	block_count = reverse_byte(octet);
	proto_tree_add_uint(tree, hf_t30_partial_page_i2, tvb, offset, 1, block_count);
	offset++;

	octet = tvb_get_guint8(tvb, offset);
	frame_count = reverse_byte(octet);
	proto_tree_add_uint(tree, hf_t30_partial_page_i3, tvb, offset, 1, frame_count);
	offset++;

	if (check_col(pinfo->cinfo, COL_INFO))
		col_append_fstr(pinfo->cinfo, COL_INFO, " - PC:%d BC:%d FC:%d", page_count, block_count, frame_count);

	g_snprintf(t38_info->desc, MAX_T38_DESC, "PC:%d BC:%d FC:%d", page_count, block_count, frame_count);

}

static void
dissect_t30_dis_dtc(tvbuff_t *tvb, int offset, packet_info *pinfo, int len, proto_tree *tree, gboolean dis_dtc)
{
	guint8 octet;

	if (len < 3) {
		proto_tree_add_text(tree, tvb, offset, tvb_reported_length_remaining(tvb, offset), "[MALFORMED OR SHORT PACKET: DIS length must be at least 4 bytes]");
		expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_ERROR, "T30 DIS length must be at least 4 bytes");
		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_str(pinfo->cinfo, COL_INFO, " [MALFORMED OR SHORT PACKET]");				
		return;
	}

	/* bits 1 to 8 */
	octet = tvb_get_guint8(tvb, offset);
	
    proto_tree_add_boolean(tree, hf_t30_fif_sm, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_rtif, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_3gmn, tvb, offset, 1, octet);
    if (dis_dtc) {
		proto_tree_add_boolean(tree, hf_t30_fif_v8c, tvb, offset, 1, octet);
		proto_tree_add_boolean(tree, hf_t30_fif_op, tvb, offset, 1, octet);
	}
	/* bits 9 to 16 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	if (dis_dtc) proto_tree_add_boolean(tree, hf_t30_fif_rtfc, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_rfo, tvb, offset, 1, octet);
	if (dis_dtc) {
		proto_tree_add_uint(tree, hf_t30_fif_dsr, tvb, offset, 1, octet);

		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_fstr(pinfo->cinfo, COL_INFO, " - DSR:%s", val_to_str((octet&0x3C) >> 2, t30_data_signalling_rate_vals, "<unknown>"));

		g_snprintf(t38_info->desc, MAX_T38_DESC, "DSR:%s", val_to_str((octet&0x3C) >> 2, t30_data_signalling_rate_vals, "<unknown>"));
	}
	else {
		proto_tree_add_uint(tree, hf_t30_fif_dsr_dcs, tvb, offset, 1, octet);

		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_fstr(pinfo->cinfo, COL_INFO, " - DSR:%s", val_to_str((octet&0x3C) >> 2, t30_data_signalling_rate_dcs_vals, "<unknown>"));

		g_snprintf(t38_info->desc, MAX_T38_DESC, "DSR:%s", val_to_str((octet&0x3C) >> 2, t30_data_signalling_rate_dcs_vals, "<unknown>"));
	}
    proto_tree_add_boolean(tree, hf_t30_fif_res, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_tdcc, tvb, offset, 1, octet);

	/* bits 17 to 24 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	if (dis_dtc) {
		proto_tree_add_uint(tree, hf_t30_fif_rwc, tvb, offset, 1, octet);
		proto_tree_add_uint(tree, hf_t30_fif_rlc, tvb, offset, 1, octet);
		proto_tree_add_uint(tree, hf_t30_fif_msltcr, tvb, offset, 1, octet);
	} else {
		proto_tree_add_uint(tree, hf_t30_fif_rw_dcs, tvb, offset, 1, octet);
		proto_tree_add_uint(tree, hf_t30_fif_rl_dcs, tvb, offset, 1, octet);
		proto_tree_add_uint(tree, hf_t30_fif_mslt_dcs, tvb, offset, 1, octet);
	}
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 4) ) return;	/* no extension */

	/* bits 25 to 32 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_cm, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ecm, tvb, offset, 1, octet);
	if (!dis_dtc) proto_tree_add_boolean(tree, hf_t30_fif_fs_dcs, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_t6, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 5) ) return;	/* no extension */	

	/* bits 33 to 40 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_fvc, tvb, offset, 1, octet);
    if (dis_dtc) {
		proto_tree_add_boolean(tree, hf_t30_fif_mspc, tvb, offset, 1, octet);
		proto_tree_add_boolean(tree, hf_t30_fif_ps, tvb, offset, 1, octet);
	}
	proto_tree_add_boolean(tree, hf_t30_fif_t43, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_pi, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_vc32k, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 6) ) return;	/* no extension */	

	/* bits 41 to 48 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_r8x15, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_300x300, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_r16x15, tvb, offset, 1, octet);
	if (dis_dtc) {
	    proto_tree_add_boolean(tree, hf_t30_fif_ibrp, tvb, offset, 1, octet);
		proto_tree_add_boolean(tree, hf_t30_fif_mbrp, tvb, offset, 1, octet);
		proto_tree_add_boolean(tree, hf_t30_fif_msltchr, tvb, offset, 1, octet);
		proto_tree_add_boolean(tree, hf_t30_fif_sp, tvb, offset, 1, octet);
	} else {
	    proto_tree_add_boolean(tree, hf_t30_fif_rts, tvb, offset, 1, octet);
	}
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 7) ) return;	/* no extension */	

	/* bits 49 to 56 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_sc, tvb, offset, 1, octet);
	if (dis_dtc) {
		proto_tree_add_boolean(tree, hf_t30_fif_passw, tvb, offset, 1, octet);
		proto_tree_add_boolean(tree, hf_t30_fif_rttd, tvb, offset, 1, octet);
	} else {
		proto_tree_add_boolean(tree, hf_t30_fif_sit, tvb, offset, 1, octet);
    }
    proto_tree_add_boolean(tree, hf_t30_fif_bft, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_dtm, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_edi, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 8) ) return;	/* no extension */	

	/* bits 57 to 64 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

    proto_tree_add_boolean(tree, hf_t30_fif_btm, tvb, offset, 1, octet);
    if (dis_dtc) proto_tree_add_boolean(tree, hf_t30_fif_rttcmmd, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_chrm, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_mm, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 9) ) return;	/* no extension */	

	/* bits 65 to 72 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_pm26, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_dnc, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_do, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_jpeg, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_fcm, tvb, offset, 1, octet);
	if (!dis_dtc) proto_tree_add_boolean(tree, hf_t30_fif_pht, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_12c, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 10) ) return;	/* no extension */	

	/* bits 73 to 80 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_ns, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_ci, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_cgr, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_nalet, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_naleg, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_spscb, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_spsco, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 11) ) return;	/* no extension */	

	/* bits 81 to 88 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_hkm, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_rsa, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_oc, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_hfx40, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_acn2c, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_acn3c, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_hfx40i, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 12) ) return;	/* no extension */	

	/* bits 89 to 96 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_ahsn2, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_ahsn3, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_t441, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_t442, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_t443, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_plmss, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 13) ) return;	/* no extension */	

	/* bits 97 to 104 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_cg300, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_100x100cg, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_spcbft, tvb, offset, 1, octet);
    if (dis_dtc) {
		proto_tree_add_boolean(tree, hf_t30_fif_ebft, tvb, offset, 1, octet);
		proto_tree_add_boolean(tree, hf_t30_fif_isp, tvb, offset, 1, octet);
	}
    proto_tree_add_boolean(tree, hf_t30_fif_ira, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 14) ) return;	/* no extension */	

	/* bits 105 to 112 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_600x600, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_1200x1200, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_300x600, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_400x800, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_600x1200, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_cg600x600, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_cg1200x1200, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) || (len < 15) ) return;	/* no extension */	

	/* bits 113 to 120 */
	offset += 1;
	octet = tvb_get_guint8(tvb, offset);

	proto_tree_add_boolean(tree, hf_t30_fif_dspcam, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_dspccm, tvb, offset, 1, octet);
    if (dis_dtc) proto_tree_add_boolean(tree, hf_t30_fif_bwmrcp, tvb, offset, 1, octet);
    proto_tree_add_boolean(tree, hf_t30_fif_t45, tvb, offset, 1, octet);
	proto_tree_add_uint(tree, hf_t30_fif_sdmc, tvb, offset, 1, octet);
	proto_tree_add_boolean(tree, hf_t30_fif_ext, tvb, offset, 1, octet);

	if ( !(octet & 0x01) ) return;	/* no extension */	
	
}

static int
dissect_t30_hdlc(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	proto_item *it;
	proto_tree *tr;
	proto_tree *tr_fif;
	proto_item *it_fcf;
	guint8 octet;
	guint32	frag_len;
	proto_item *item;

	if (tvb_reported_length_remaining(tvb, offset) < 3) {
		proto_tree_add_text(tree, tvb, offset, tvb_reported_length_remaining(tvb, offset), "[MALFORMED OR SHORT PACKET: hdlc T30 length must be at least 4 bytes]");
		expert_add_info_format(pinfo, NULL, PI_MALFORMED, PI_ERROR, "T30 length must be at least 4 bytes");
		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_str(pinfo->cinfo, COL_INFO, " (HDLC Reassembled: [MALFORMED OR SHORT PACKET])");				
		return offset;
	}

/*	if (tree) {
		proto_item *item;*/
		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_str(pinfo->cinfo, COL_INFO, " (HDLC Reassembled:");				

		it=proto_tree_add_protocol_format(tree, proto_t30, tvb, offset, -1,
	    "ITU-T Recommendation T.30");
		tr=proto_item_add_subtree(it, ett_t30);

		octet = tvb_get_guint8(tvb, offset);
		item = proto_tree_add_uint(tr, hf_t30_Address, tvb, offset, 1, octet);
		if (octet != 0xFF) expert_add_info_format(pinfo, item, PI_REASSEMBLE, PI_WARN, "T30 Address must be 0xFF");
		offset += 1;

		octet = tvb_get_guint8(tvb, offset);
		item = proto_tree_add_uint(tr, hf_t30_Control, tvb, offset, 1, octet);
		if ((octet != 0xC0) && (octet != 0xC8)) expert_add_info_format(pinfo, item, PI_REASSEMBLE, PI_WARN, "T30 Control Field must be 0xC0 or 0xC8");
		offset += 1;

		octet = tvb_get_guint8(tvb, offset);
		it_fcf = proto_tree_add_uint(tr, hf_t30_Facsimile_Control, tvb, offset, 1, octet & 0x7F);
		offset += 1;

		tr_fif = proto_item_add_subtree(it_fcf, ett_t30_fif);

		frag_len = tvb_length_remaining(tvb, offset);
		t38_info->t30_Facsimile_Control = octet;

		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_fstr(pinfo->cinfo, COL_INFO, " %s - %s", val_to_str(octet & 0x7F, t30_facsimile_control_field_vals_short, "<unknown>"),
				val_to_str(octet & 0x7F, t30_facsimile_control_field_vals, "<unknown>") );	

		switch (octet & 0x7F) {
		case T30_FC_DIS:
		case T30_FC_DTC:
			dissect_t30_dis_dtc(tvb, offset, pinfo, frag_len, tr_fif, TRUE);
			break;
		case T30_FC_DCS:
			dissect_t30_dis_dtc(tvb, offset, pinfo, frag_len, tr_fif, FALSE);
			break;
		case T30_FC_CSI:
		case T30_FC_CIG:
		case T30_FC_TSI:
		case T30_FC_PWD:
		case T30_FC_SEP:
		case T30_FC_SUB:
		case T30_FC_SID:
		case T30_FC_PSA:
			dissect_t30_numbers(tvb, offset, pinfo, frag_len, tr_fif);
			break;
		case T30_FC_NSF:
		case T30_FC_NSC:
		case T30_FC_NSS:
			dissect_t30_non_standard_cap(tvb, offset, pinfo, frag_len, tr_fif);
			break;
		case T30_FC_FCD:
			dissect_t30_facsimile_coded_data(tvb, offset, pinfo, frag_len, tr_fif);
			break;
		case T30_FC_PPS:
			dissect_t30_partial_page_signal(tvb, offset, pinfo, frag_len, tr_fif);
			break;
		}

		if (check_col(pinfo->cinfo, COL_INFO))
			col_append_str(pinfo->cinfo, COL_INFO, ")");				

/*	}*/
	
	return offset;
}


/* T38 Routines */

static int
dissect_t38_NULL(tvbuff_t *tvb _U_, int offset, packet_info *pinfo _U_, proto_tree *tree _U_)
{
	return offset;
}

static const per_choice_t t30_indicator_choice[] = {
	{ 0, "no-signal", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 1, "cng", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 2, "ced", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 3, "v21-preamble", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 4, "v27-2400-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 5, "v27-4800-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 6, "v29-7200-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 7, "v29-9600-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 8, "v17-7200-short-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 9, "v17-7200-long-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 10, "v17-9600-short-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 11, "v17-9600-long-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 12, "v17-12000-short-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 13, "v17-12000-long-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 14, "v17-14400-short-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 15, "v17-14400-long-training", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 16, "v8-ansam", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 17, "v8-signal", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 18, "v34-cntl-channel-1200", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 19, "v34-pri-channel", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 20, "v34-CC-retrain", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 21, "v33-12000-training", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 22, "v33-14400-training", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 0, NULL, 0, NULL }
};

const value_string t30_indicator_vals[] = {
	{ 0, "no-signal" },
	{ 1, "cng" },
	{ 2, "ced" },
	{ 3, "v21-preamble" },
	{ 4, "v27-2400-training" },
	{ 5, "v27-4800-training" },
	{ 6, "v29-7200-training" },
	{ 7, "v29-9600-training" },
	{ 8, "v17-7200-short-training" },
	{ 9, "v17-7200-long-training" },
	{ 10, "v17-9600-short-training" },
	{ 11, "v17-9600-long-training" },
	{ 12, "v17-12000-short-training" },
	{ 13, "v17-12000-long-training" },
	{ 14, "v17-14400-short-training" },
	{ 15, "v17-14400-long-training" },
    { 16, "v8-ansam" },
    { 17, "v8-signal" },
    { 18, "v34-cntl-channel-1200" },
    { 19, "v34-pri-channel" },
    { 20, "v34-CC-retrain" },
    { 21, "v33-12000-training" },
    { 22, "v33-14400-training" },
	{ 0, NULL },
};

static int
dissect_t38_t30_indicator(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_choice(tvb, offset, pinfo,
        tree, hf_t38_t30_indicator, ett_t38_t30_indicator,
        t30_indicator_choice, &T30ind_value);

	if (check_col(pinfo->cinfo, COL_INFO) && primary_part){
        col_append_fstr(pinfo->cinfo, COL_INFO, " t30ind: %s",
         val_to_str(T30ind_value,t30_indicator_vals,"<unknown>"));
	}

	/* info for tap */
	if (primary_part)
		t38_info->t30ind_value = T30ind_value;

	return offset;
}

static const per_choice_t data_choice[] = {
	{ 0, "v21", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 1, "v27-2400", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 2, "v27-4800", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 3, "v29-7200", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 4, "v29-9600", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 5, "v17-7200", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 6, "v17-9600", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 7, "v17-12000", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 8, "v17-14400", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 9, "v8", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 10, "v34-pri-rate", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 11, "v34-CC-1200", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 12, "v34-pri-ch", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 13, "v33-12000", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 14, "v33-14400", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 0, NULL, 0, NULL }
};

const value_string t30_data_vals[] = {
	{ 0, "v21" },
	{ 1, "v27-2400" },
	{ 2, "v27-4800" },
	{ 3, "v29-7200" },
	{ 4, "v29-9600" },
	{ 5, "v17-7200" },
	{ 6, "v17-9600" },
	{ 7, "v17-12000" },
	{ 8, "v17-14400" },
	{ 9, "v8" },
	{ 10, "v34-pri-rate" },
	{ 11, "v34-CC-1200" },
	{ 12, "v34-pri-ch" },
	{ 13, "v33-12000" },
	{ 14, "v33-14400" },
	{ 0, NULL },
};

static int
dissect_t38_data(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_choice(tvb, offset, pinfo,
        tree, hf_t38_data, ett_t38_data,
        data_choice, &Data_value);

    if (check_col(pinfo->cinfo, COL_INFO) && primary_part){
        col_append_fstr(pinfo->cinfo, COL_INFO, " data:%s:",
         val_to_str(Data_value,t30_data_vals,"<unknown>"));
	}

	
	/* info for tap */
	if (primary_part)
		t38_info->data_value = Data_value;

	return offset;
}

static const per_choice_t Type_of_msg_choice[] = {
	{ 0, "t30-indicator", ASN1_NO_EXTENSIONS,
		dissect_t38_t30_indicator},
	{ 1, "data", ASN1_NO_EXTENSIONS,
		dissect_t38_data},
	{ 0, NULL, 0, NULL }
};

static const value_string Type_of_msg_vals[] = {
	{ 0, "t30-indicator" },
	{ 1, "data" },
    { 0, NULL}
};
static int
dissect_t38_Type_of_msg(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree, int hf_index) {
  offset = dissect_per_choice(tvb, offset, pinfo, tree, hf_index,
                              ett_t38_Type_of_msg, Type_of_msg_choice,
                              &Type_of_msg_value);

  /* info for tap */
  if (primary_part)
    t38_info->type_msg = Type_of_msg_value;

  return offset;
}

static int dissect_type_of_msg(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree) {
  return dissect_t38_Type_of_msg(tvb, offset, pinfo, tree, hf_t38_Type_of_msg);
}

static const per_choice_t Data_Field_field_type_PreCorrigendum_choice[] = {
	{ 0, "hdlc-data", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 1, "hdlc-sig-end", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 2, "hdlc-fcs-OK", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 3, "hdlc-fcs-BAD", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 4, "hdlc-fcs-OK-sig-end", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 5, "hdlc-fcs-BAD-sig-end", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 6, "t4-non-ecm-data", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 7, "t4-non-ecm-sig-end", ASN1_NO_EXTENSIONS,
		dissect_t38_NULL},
	{ 0, NULL, 0, NULL }
};


static const per_choice_t Data_Field_field_type_choice[] = {
	{ 0, "hdlc-data", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 1, "hdlc-sig-end", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 2, "hdlc-fcs-OK", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 3, "hdlc-fcs-BAD", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 4, "hdlc-fcs-OK-sig-end", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 5, "hdlc-fcs-BAD-sig-end", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 6, "t4-non-ecm-data", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 7, "t4-non-ecm-sig-end", ASN1_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 8, "cm-message", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 9, "jm-message", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 10, "ci-message", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 11, "v34-rate", ASN1_NOT_EXTENSION_ROOT,
		dissect_t38_NULL},
	{ 0, NULL, 0, NULL }
};


static const value_string Data_Field_field_type_vals[] = {
	{ 0, "hdlc-data" },
	{ 1, "hdlc-sig-end" },
	{ 2, "hdlc-fcs-OK" },
	{ 3, "hdlc-fcs-BAD" },
	{ 4, "hdlc-fcs-OK-sig-end" },
	{ 5, "hdlc-fcs-BAD-sig-end" },
	{ 6, "t4-non-ecm-data" },
	{ 7, "t4-non-ecm-sig-end" },
	{ 8, "cm-message" },
	{ 9, "jm-message" },
	{ 10, "ci-message" },
	{ 11, "v34-rate" },
	{ 0, NULL },
};

fragment_data *
force_reassmeble_seq(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
	     GHashTable *fragment_table, guint32 frag_number)
{
	fragment_key key;
	fragment_data *fd_head;
	fragment_data *fd_i;
	fragment_data *last_fd;
	guint32 dfpos, size, packet_lost, burst_lost, seq_num;

	/* create key to search hash with */
	key.src = pinfo->src;
	key.dst = pinfo->dst;
	key.id  = id;

	fd_head = g_hash_table_lookup(fragment_table, &key);

	/* have we already seen this frame ?*/
	if (pinfo->fd->flags.visited) {
		if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
			return fd_head;
		} else {
			return NULL;
		}
	}

	if (fd_head==NULL){
		/* we must have it to continue */
		return NULL;
	}

	/* check for packet lost and count the burst of packet lost */
	packet_lost = 0;
	burst_lost = 0;
	seq_num = 0;
	for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
		if (seq_num != fd_i->offset) {
			packet_lost += fd_i->offset - seq_num;
			if ( (fd_i->offset - seq_num) > burst_lost ) {
				burst_lost = fd_i->offset - seq_num;
			}
		}
		seq_num = fd_i->offset + 1;
	}

	/* we have received an entire packet, defragment it and
     * free all fragments
     */
	size=0;
	last_fd=NULL;
	for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
	  if(!last_fd || last_fd->offset!=fd_i->offset){
	    size+=fd_i->len;
	  }
	  last_fd=fd_i;
	}
	fd_head->data = g_malloc(size);
	fd_head->len = size;		/* record size for caller	*/

	/* add all data fragments */
	dfpos = 0;
	last_fd=NULL;
	for (fd_i=fd_head->next;fd_i && fd_i->len + dfpos <= size;fd_i=fd_i->next) {
	  if (fd_i->len) {
	    if(!last_fd || last_fd->offset!=fd_i->offset){
	      memcpy(fd_head->data+dfpos,fd_i->data,fd_i->len);
	      dfpos += fd_i->len;
	    } else {
	      /* duplicate/retransmission/overlap */
	      fd_i->flags    |= FD_OVERLAP;
	      fd_head->flags |= FD_OVERLAP;
	      if( (last_fd->len!=fd_i->datalen)
		  || memcmp(last_fd->data, fd_i->data, last_fd->len) ){
			fd_i->flags    |= FD_OVERLAPCONFLICT;
			fd_head->flags |= FD_OVERLAPCONFLICT;
	      }
	    }
	  }
	  last_fd=fd_i;
	}

	/* we have defragmented the pdu, now free all fragments*/
	for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
	  if(fd_i->data){
	    g_free(fd_i->data);
	    fd_i->data=NULL;
	  }
	}

	/* mark this packet as defragmented */
	fd_head->flags |= FD_DEFRAGMENTED;
	fd_head->reassembled_in=pinfo->fd->num;

	if (check_col(pinfo->cinfo, COL_INFO))
			col_append_fstr(pinfo->cinfo, COL_INFO, " (t4-data Reassembled: %d pack lost, %d pack burst lost)", packet_lost, burst_lost);
	
	p_t38_packet_conv_info->packet_lost = packet_lost;
	p_t38_packet_conv_info->burst_lost = burst_lost;

	return fd_head;
}

static int
dissect_t38_Data_Field_field_type(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	if(use_pre_corrigendum_asn1_specification){
		offset=dissect_per_choice(tvb, offset, pinfo,
			tree, hf_t38_Data_Field_field_type, ett_t38_Data_Field_field_type,
			Data_Field_field_type_PreCorrigendum_choice, &Data_Field_field_type_value);
	}
	else{
		offset=dissect_per_choice(tvb, offset, pinfo,
			tree, hf_t38_Data_Field_field_type, ett_t38_Data_Field_field_type,
			Data_Field_field_type_choice, &Data_Field_field_type_value);
	}

    if (check_col(pinfo->cinfo, COL_INFO) && primary_part){
        col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
         val_to_str(Data_Field_field_type_value,Data_Field_field_type_vals,"<unknown>"));
	}

	/* We only reassmeble packets in the Primary part and in the first two Items.						*/
	/* There maybe be t38 packets with more than two Items, but reassemble those packets is not easy	*/
	/* using the current ressaemble functions.															*/
	/* TODO: reassemble all the Items in one frame */
	if (primary_part && (Data_Field_item_num<2)) {
		if (Data_Field_field_type_value == 2 || Data_Field_field_type_value == 4 || Data_Field_field_type_value == 7) {/* hdlc-fcs-OK or hdlc-fcs-OK-sig-end or t4-non-ecm-sig-end*/
			fragment_data *frag_msg = NULL;
			tvbuff_t* new_tvb = NULL;
			gboolean save_fragmented = pinfo->fragmented;

			pinfo->fragmented = TRUE;

			/* if reass_start_seqnum=-1 it means we have received the end of the fragmente, without received any fragment data */
			if (p_t38_packet_conv_info->reass_start_seqnum != -1) {
				frag_msg = fragment_add_seq(tvb, offset, pinfo,
					p_t38_packet_conv_info->reass_ID, /* ID for fragments belonging together */
					data_fragment_table, /* list of message fragments */
					seq_number + Data_Field_item_num - (guint32)p_t38_packet_conv_info->reass_start_seqnum,  /* fragment sequence number */
					/*0,*/
					0, /* fragment length */
					FALSE); /* More fragments */
				if ( Data_Field_field_type_value == 7 ) {
					/* if there was packet lost or other errors during the defrag then frag_msg is NULL. This could also means
					 * there are out of order packets (e.g, got the tail frame t4-non-ecm-sig-end before the last fragment), 
					 * but we will assume there was packet lost instead, which is more usual. So, we are going to reassemble the packet
					 * and get some stat, like packet lost and burst number of packet lost
					*/
					if (!frag_msg) {
						force_reassmeble_seq(tvb, offset, pinfo,
							p_t38_packet_conv_info->reass_ID, /* ID for fragments belonging together */
							data_fragment_table, /* list of message fragments */
							seq_number + Data_Field_item_num - (guint32)p_t38_packet_conv_info->reass_start_seqnum);  /* fragment sequence number */
					} else {
						if (check_col(pinfo->cinfo, COL_INFO))
							col_append_str(pinfo->cinfo, COL_INFO, " (t4-data Reassembled: No packet lost)");	
						
						g_snprintf(t38_info->desc_comment, MAX_T38_DESC, "No packet lost");
					}

					
					if (p_t38_packet_conv_info->packet_lost) {
						g_snprintf(t38_info->desc_comment, MAX_T38_DESC, " Pack lost: %d, Pack burst lost: %d", p_t38_packet_conv_info->packet_lost, p_t38_packet_conv_info->burst_lost);
					} else {
						g_snprintf(t38_info->desc_comment, MAX_T38_DESC, "No packet lost");
					}

					new_tvb = process_reassembled_data(tvb, offset, pinfo,
								"Reassembled Message", frag_msg, &data_frag_items, NULL, tree);

					/* Now reset fragmentation information in pinfo */
					pinfo->fragmented = save_fragmented;

					t38_info->time_first_t4_data = p_t38_packet_conv_info->time_first_t4_data; 
					t38_info->frame_num_first_t4_data = p_t38_packet_conv_info->reass_ID; /* The reass_ID is the Frame number of the first t4 fragment */

				} else {
					new_tvb = process_reassembled_data(tvb, offset, pinfo,
								"Reassembled Message", frag_msg, &data_frag_items, NULL, tree);

					/* Now reset fragmentation information in pinfo */
					pinfo->fragmented = save_fragmented;

					if (new_tvb) dissect_t30_hdlc(new_tvb, 0, pinfo, tree); 
				}
			} else {
				if(tree){
					proto_tree_add_text(tree, tvb, offset, tvb_reported_length_remaining(tvb, offset),
						"[RECEIVED END OF FRAGMENT W/OUT ANY FRAGMENT DATA]");
				}
				if (check_col(pinfo->cinfo, COL_INFO)){
					col_append_fstr(pinfo->cinfo, COL_INFO, " [Malformed?]");
				}
				pinfo->fragmented = save_fragmented;
			}
		}

		/* reset the reassemble ID and the start seq number if it is not HDLC data */
		if ( p_t38_conv && ( ((Data_Field_field_type_value >0) && (Data_Field_field_type_value <6)) || (Data_Field_field_type_value == 7) ) ){
			p_t38_conv_info->reass_ID = 0;
			p_t38_conv_info->reass_start_seqnum = -1;
		}
		t38_info->Data_Field_field_type_value = Data_Field_field_type_value;
	}
    return offset;
}

static int
dissect_t38_Data_Field_field_data(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	tvbuff_t *value_tvb = NULL;
	guint32 value_len;

	offset=dissect_per_octet_string(tvb, offset, pinfo,
        tree, hf_t38_Data_Field_field_data, 1, 65535,
        &value_tvb);
	value_len = tvb_length(value_tvb);

	if (check_col(pinfo->cinfo, COL_INFO) && primary_part){
        if(value_len < 8){
        	col_append_fstr(pinfo->cinfo, COL_INFO, "[%s]",
               tvb_bytes_to_str(value_tvb,0,value_len));
        }
        else {
        	col_append_fstr(pinfo->cinfo, COL_INFO, "[%s...]",
               tvb_bytes_to_str(value_tvb,0,7));
        }
	}

	/* We only reassmeble packets in the Primary part and in the first two Items.						*/
	/* There maybe be t38 packets with more than two Items, but reassemble those packets is not easy	*/
	/* using the current ressaemble functions.															*/
	/* TODO: reassemble all the Items in one frame */
	if (primary_part && (Data_Field_item_num<2)) {
		tvbuff_t* new_tvb = NULL;
		fragment_data *frag_msg = NULL;
	
		/* HDLC Data or t4-non-ecm-data */
		if (Data_Field_field_type_value == 0 || Data_Field_field_type_value == 6) { /* 0=HDLC Data or 6=t4-non-ecm-data*/
			gboolean save_fragmented = pinfo->fragmented;

			pinfo->fragmented = TRUE;

			/* if we have not reassembled this packet and it is the first fragment, reset the reassemble ID and the start seq number*/
			if (p_t38_packet_conv && p_t38_conv && (p_t38_packet_conv_info->reass_ID == 0)) {
				/* we use the first fragment's frame_number as fragment ID because the protocol doesn't provide it */
					p_t38_conv_info->reass_ID = pinfo->fd->num;
					p_t38_conv_info->reass_start_seqnum = seq_number;
					p_t38_conv_info->time_first_t4_data = nstime_to_sec(&pinfo->fd->rel_ts);
					p_t38_packet_conv_info->reass_ID = p_t38_conv_info->reass_ID;
					p_t38_packet_conv_info->reass_start_seqnum = p_t38_conv_info->reass_start_seqnum;
					p_t38_packet_conv_info->time_first_t4_data = p_t38_conv_info->time_first_t4_data;
			}

			frag_msg = fragment_add_seq(value_tvb, 0, pinfo,
				p_t38_packet_conv_info->reass_ID, /* ID for fragments belonging together */
				data_fragment_table, /* list of message fragments */
				seq_number - (guint32)p_t38_packet_conv_info->reass_start_seqnum, /* fragment sequence number */
				value_len, /* fragment length */
				TRUE); /* More fragments */

			new_tvb = process_reassembled_data(tvb, offset, pinfo,
						"Reassembled Message", frag_msg, &data_frag_items, NULL, tree);

			if (!frag_msg) { /* Not last packet of reassembled */
				if (Data_Field_field_type_value == 0) {
					if (check_col(pinfo->cinfo, COL_INFO))
						col_append_fstr(pinfo->cinfo, COL_INFO," (HDLC fragment %u)", seq_number - (guint32)p_t38_packet_conv_info->reass_start_seqnum);
				} else {
					if (check_col(pinfo->cinfo, COL_INFO))
						col_append_fstr(pinfo->cinfo, COL_INFO," (t4-data fragment %u)", seq_number - (guint32)p_t38_packet_conv_info->reass_start_seqnum);
				}
			}

			/* Now reset fragmentation information in pinfo */
			pinfo->fragmented = save_fragmented;
		}
	}

	return offset;
}

static const per_sequence_t Data_Field_item_sequence[] = {
	{ "field-type", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_Data_Field_field_type },
	{ "field-data", ASN1_NO_EXTENSIONS, ASN1_OPTIONAL,
		dissect_t38_Data_Field_field_data },
	{ NULL, 0, 0, NULL }
};

static int
dissect_t38_Data_Field_item(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_sequence(tvb, offset, pinfo,
        tree, hf_t38_Data_Field_item, ett_t38_Data_Field_item,
        Data_Field_item_sequence);

	if (primary_part) Data_Field_item_num++;

	return offset;
}

static const per_sequence_t t38_Data_Field_sequence_of[1] = {
  { ""                            , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_Data_Field_item },
};

static int
dissect_t38_Data_Field(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree, int hf_index) {
  offset = dissect_per_sequence_of(tvb, offset, pinfo, tree, hf_index,
                                      ett_t38_Data_Field, t38_Data_Field_sequence_of);

  return offset;
}
static int dissect_data_field(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree) {
  return dissect_t38_Data_Field(tvb, offset, pinfo, tree, hf_t38_Data_Field);
}

static const per_sequence_t IFPPacket_sequence[] = {
  { "type-of-msg"                 , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_type_of_msg },
  { "data-field"                  , ASN1_NO_EXTENSIONS     , ASN1_OPTIONAL    , dissect_data_field },
  { NULL, 0, 0, NULL }
};

static int
dissect_t38_IFPPacket(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_sequence(tvb, offset, pinfo,
        tree, hf_t38_IFPPacket, ett_t38_IFPPacket,
        IFPPacket_sequence);
	return offset;
}

static int
dissect_t38_seq_number(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_constrained_integer(tvb, offset, pinfo,
		tree, hf_t38_seq_number, 0, 65535,
		&seq_number, NULL, FALSE);
	
	/* info for tap */
	if (primary_part)
		t38_info->seq_num = seq_number;

      if (check_col(pinfo->cinfo, COL_INFO)){
        col_append_fstr(pinfo->cinfo, COL_INFO, "Seq=%05u ",seq_number);
	}
	return offset;
}

static int
dissect_t38_primary_ifp_packet(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    guint32 length;

	primary_part = TRUE;

    offset=dissect_per_length_determinant(tvb, offset, pinfo,
        tree, hf_t38_primary_ifp_packet_length, &length);
    offset=dissect_t38_IFPPacket(tvb, offset, pinfo, tree);

	/* if is a valid t38 packet, add to tap */
	if (p_t38_packet_conv && (!pinfo->in_error_pkt) && ((gint32) seq_number != p_t38_packet_conv_info->last_seqnum))
		tap_queue_packet(t38_tap, pinfo, t38_info);

	if (p_t38_conv) p_t38_conv_info->last_seqnum = (gint32) seq_number;

	return offset;
}

static int
dissect_t38_secondary_ifp_packets_item(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    guint32 length;

    offset=dissect_per_length_determinant(tvb, offset, pinfo,
        tree, hf_t38_secondary_ifp_packets_item_length, &length);
    offset=dissect_t38_IFPPacket(tvb, offset, pinfo, tree);
	return offset;
}

static const per_sequence_t SEQUENCE_OF_t38_secondary_ifp_packets_sequence_of[1] = {
  { ""                            , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_secondary_ifp_packets_item },
};

static int
dissect_t38_secondary_ifp_packets(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    /* When the field-data is not present, we MUST offset 1 byte*/
    if((Data_Field_field_type_value != 0) &&
       (Data_Field_field_type_value != 6) &&
	   (Data_Field_field_type_value != 7))
    {
        offset=offset+8;
    }

    offset=dissect_per_sequence_of(tvb, offset, pinfo,
        tree, hf_t38_secondary_ifp_packets, ett_t38_secondary_ifp_packets,
        SEQUENCE_OF_t38_secondary_ifp_packets_sequence_of);
	return offset;
}

static int
dissect_t38_fec_npackets(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_integer(tvb, offset, pinfo,
        tree, hf_t38_fec_npackets,
        NULL, NULL);
	return offset;
}

static int
dissect_t38_fec_data_item(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_octet_string(tvb, offset, pinfo,
        tree, hf_t38_fec_data_item, NO_BOUND, NO_BOUND,
        NULL);
	return offset;
}
static const per_sequence_t T_t38_fec_data_sequence_of[1] = {
  { ""                            , ASN1_NO_EXTENSIONS     , ASN1_NOT_OPTIONAL, dissect_t38_fec_data_item },
};
static int
dissect_t38_fec_data(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    offset=dissect_per_sequence_of(tvb, offset, pinfo,
        tree, hf_t38_fec_data, ett_t38_fec_data,
        T_t38_fec_data_sequence_of);
	return offset;
}

static const per_sequence_t fec_info_sequence[] = {
	{ "fec-npackets", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_fec_npackets },
	{ "fec-data", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_fec_data },
	{ NULL, 0, 0, NULL }
};

static int
dissect_t38_fec_info(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	offset=dissect_per_sequence(tvb, offset, pinfo,
        tree, hf_t38_fec_info, ett_t38_fec_info,
        fec_info_sequence);
	return offset;
}

static const per_choice_t error_recovery_choice[] = {
	{ 0, "secondary-ifp-packets", ASN1_NO_EXTENSIONS,
		dissect_t38_secondary_ifp_packets},
	{ 1, "fec-info", ASN1_NO_EXTENSIONS,
		dissect_t38_fec_info},
	{ 0, NULL, 0, NULL }
};

static const value_string error_recovery_vals[] = {
	{ 0, "secondary-ifp-packets" },
	{ 1, "fec-info" },
    { 0, NULL}
};

static int
dissect_t38_error_recovery(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
	primary_part = FALSE;

    offset=dissect_per_choice(tvb, offset, pinfo,
        tree, hf_t38_error_recovery, ett_t38_error_recovery,
        error_recovery_choice, NULL);

	primary_part = TRUE;

	return offset;
}

static const per_sequence_t UDPTLPacket_sequence[] = {
	{ "seq-number", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_seq_number },
	{ "primary-ifp-packet", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_primary_ifp_packet },
	{ "error-recovery", ASN1_NO_EXTENSIONS, ASN1_NOT_OPTIONAL,
		dissect_t38_error_recovery },
	{ NULL, 0, 0, NULL }
};

static int
dissect_t38_UDPTLPacket(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    /* Initialize to something else than data type */
    Data_Field_field_type_value = 1;

	offset=dissect_per_sequence(tvb, offset, pinfo,
        tree, hf_t38_UDPTLPacket, ett_t38_UDPTLPacket,
        UDPTLPacket_sequence);
    return offset;
}

/* initialize the tap t38_info and the conversation */
static void
init_t38_info_conv(packet_info *pinfo)
{
	/* tap info */
	t38_info_current++;
	if (t38_info_current==MAX_T38_MESSAGES_IN_PACKET) {
		t38_info_current=0;
	}
	t38_info = &t38_info_arr[t38_info_current];

	t38_info->seq_num = 0;
	t38_info->type_msg = 0;
	t38_info->data_value = 0;
	t38_info->t30ind_value =0;
	t38_info->setup_frame_number = 0;
	t38_info->Data_Field_field_type_value = 0;
	t38_info->desc[0] = '\0';
	t38_info->desc_comment[0] = '\0';
	t38_info->time_first_t4_data = 0;
	t38_info->frame_num_first_t4_data = 0;


	/* 
		p_t38_packet_conv hold the conversation info in each of the packets.
		p_t38_conv hold the conversation info used to reassemble the HDLC packets, and also the Setup info (e.g SDP)
		If we already have p_t38_packet_conv in the packet, it means we already reassembled the HDLC packets, so we don't 
		need to use p_t38_conv 
	*/
	p_t38_packet_conv = NULL;
	p_t38_conv = NULL;

	/* Use existing packet info if available */
	 p_t38_packet_conv = p_get_proto_data(pinfo->fd, proto_t38);


	/* find the conversation used for Reassemble and Setup Info */
	p_conv = find_conversation(pinfo->fd->num, &pinfo->net_src, &pinfo->net_dst,
                                   pinfo->ptype,
                                   pinfo->srcport, pinfo->destport, NO_ADDR_B | NO_PORT_B);

	/* create a conv if it doen't exist */
	if (!p_conv) {
		p_conv = conversation_new(pinfo->fd->num, &pinfo->net_src, &pinfo->net_dst,
			      pinfo->ptype, pinfo->srcport, pinfo->destport, NO_ADDR_B | NO_PORT_B);

		/* Set dissector */
		conversation_set_dissector(p_conv, t38_udp_handle);
	}

	if (!p_t38_packet_conv) {
		p_t38_conv = conversation_get_proto_data(p_conv, proto_t38);

		/* create the conversation if it doen't exist */
		if (!p_t38_conv) {
			p_t38_conv = se_alloc(sizeof(t38_conv));
			p_t38_conv->setup_method[0] = '\0';
			p_t38_conv->setup_frame_number = 0;

			p_t38_conv->src_t38_info.reass_ID = 0;
			p_t38_conv->src_t38_info.reass_start_seqnum = -1;
			p_t38_conv->src_t38_info.reass_data_type = 0;
			p_t38_conv->src_t38_info.last_seqnum = -1;
			p_t38_conv->src_t38_info.packet_lost = 0;
			p_t38_conv->src_t38_info.burst_lost = 0;
			p_t38_conv->src_t38_info.time_first_t4_data = 0;

			p_t38_conv->dst_t38_info.reass_ID = 0;
			p_t38_conv->dst_t38_info.reass_start_seqnum = -1;
			p_t38_conv->dst_t38_info.reass_data_type = 0;
			p_t38_conv->dst_t38_info.last_seqnum = -1;
			p_t38_conv->dst_t38_info.packet_lost = 0;
			p_t38_conv->dst_t38_info.burst_lost = 0;
			p_t38_conv->dst_t38_info.time_first_t4_data = 0;

			conversation_add_proto_data(p_conv, proto_t38, p_t38_conv);
		}

		/* copy the t38 conversation info to the packet t38 conversation */
		p_t38_packet_conv = se_alloc(sizeof(t38_conv));
		strcpy(p_t38_packet_conv->setup_method, p_t38_conv->setup_method);
		p_t38_packet_conv->setup_frame_number = p_t38_conv->setup_frame_number;

		memcpy(&(p_t38_packet_conv->src_t38_info), &(p_t38_conv->src_t38_info), sizeof(t38_conv_info));
		memcpy(&(p_t38_packet_conv->dst_t38_info), &(p_t38_conv->dst_t38_info), sizeof(t38_conv_info));

		p_add_proto_data(pinfo->fd, proto_t38, p_t38_packet_conv);
	}

	if (ADDRESSES_EQUAL(&p_conv->key_ptr->addr1, &pinfo->net_src)) {
		p_t38_conv_info = &(p_t38_conv->src_t38_info);
		p_t38_packet_conv_info = &(p_t38_packet_conv->src_t38_info);
	} else {
		p_t38_conv_info = &(p_t38_conv->dst_t38_info);
		p_t38_packet_conv_info = &(p_t38_packet_conv->dst_t38_info);
	}

	/* update t38_info */
	t38_info->setup_frame_number = p_t38_packet_conv->setup_frame_number;
}

/* Entry point for dissection */
static void
dissect_t38_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	guint8 octet1;
	proto_item *it;
	proto_tree *tr;
	guint32 offset=0;

	/*
	 * XXX - heuristic to check for misidentified packets.
	 */
	if (dissect_possible_rtpv2_packets_as_rtp){
		octet1 = tvb_get_guint8(tvb, offset);
		if (RTP_VERSION(octet1) == 2){
			call_dissector(rtp_handle,tvb,pinfo,tree);
			return;
		}
	}

	if (check_col(pinfo->cinfo, COL_PROTOCOL)){
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
	}
	if (check_col(pinfo->cinfo, COL_INFO)){
		col_clear(pinfo->cinfo, COL_INFO);
	}

	primary_part = TRUE;

	/* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
	Data_Field_item_num = 0;

	it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
	tr=proto_item_add_subtree(it, ett_t38);

	/* init tap and conv info */
	init_t38_info_conv(pinfo);

	/* Show Conversation setup info if exists*/
	if (global_t38_show_setup_info) {
		show_setup_info(tvb, pinfo, tr, p_conv, p_t38_packet_conv);
	}

	if (check_col(pinfo->cinfo, COL_INFO)){
		col_append_fstr(pinfo->cinfo, COL_INFO, "UDP: UDPTLPacket ");
	}

	offset=dissect_t38_UDPTLPacket(tvb, offset, pinfo, tr);

	if (offset&0x07){
		offset=(offset&0xfffffff8)+8;
	}
	if (tvb_length_remaining(tvb,offset>>3)>0){
		if (tr){
			proto_tree_add_text(tr, tvb, offset, tvb_reported_length_remaining(tvb, offset),
				"[MALFORMED PACKET or wrong preference settings]");
		}
		if (check_col(pinfo->cinfo, COL_INFO)){
			col_append_fstr(pinfo->cinfo, COL_INFO, " [Malformed?]");
		}
	}
}

static void
dissect_t38_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	proto_item *it;
	proto_tree *tr;
	guint32 offset=0;
	guint16 ifp_packet_number=1;

	if (check_col(pinfo->cinfo, COL_PROTOCOL)){
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "T.38");
	}
	if (check_col(pinfo->cinfo, COL_INFO)){
		col_clear(pinfo->cinfo, COL_INFO);
	}

	primary_part = TRUE;

	/* This indicate the item number in the primary part of the T38 message, it is used for the reassemble of T30 packets */
	Data_Field_item_num = 0;

	it=proto_tree_add_protocol_format(tree, proto_t38, tvb, 0, -1, "ITU-T Recommendation T.38");
	tr=proto_item_add_subtree(it, ett_t38);

	/* init tap and conv info */
	init_t38_info_conv(pinfo);

	/* Show Conversation setup info if exists*/
	if (global_t38_show_setup_info) {
		show_setup_info(tvb, pinfo, tr, p_conv, p_t38_packet_conv);
	}

	if (check_col(pinfo->cinfo, COL_INFO)){
		col_append_fstr(pinfo->cinfo, COL_INFO, "TCP: IFPPacket");
	}

	while(tvb_length_remaining(tvb,offset>>3)>0)
	{
		offset=dissect_t38_IFPPacket(tvb, offset, pinfo, tr);
		ifp_packet_number++;

		if(offset&0x07){
			offset=(offset&0xfffffff8)+8;
		}

		if(tvb_length_remaining(tvb,offset>>3)>0){
			if(t38_tpkt_usage == T38_TPKT_ALWAYS){
				if(tr){
					proto_tree_add_text(tr, tvb, offset, tvb_reported_length_remaining(tvb, offset),
						"[MALFORMED PACKET or wrong preference settings]");
				}
				if (check_col(pinfo->cinfo, COL_INFO)){
					col_append_fstr(pinfo->cinfo, COL_INFO, " [Malformed?]");
				}
				break;
			} 
			else {
				if (check_col(pinfo->cinfo, COL_INFO)){
					col_append_fstr(pinfo->cinfo, COL_INFO, " IFPPacket#%u",ifp_packet_number);
				}
			}
		}
	}

}

static void
dissect_t38_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	primary_part = TRUE;

	if(t38_tpkt_usage == T38_TPKT_ALWAYS){
		dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
	} 
	else if((t38_tpkt_usage == T38_TPKT_NEVER) || (is_tpkt(tvb,1) == -1)){
		dissect_t38_tcp_pdu(tvb, pinfo, tree);
	} 
	else {
		dissect_tpkt_encap(tvb,pinfo,tree,t38_tpkt_reassembly,t38_tcp_pdu_handle);
	}
}

static void
dissect_t38(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	if(pinfo->ipproto == IP_PROTO_TCP)
	{
		dissect_t38_tcp(tvb, pinfo, tree);
	}
	else if(pinfo->ipproto == IP_PROTO_UDP)
	{   
		dissect_t38_udp(tvb, pinfo, tree);
	}
}

/* Look for conversation info and display any setup info found */
void show_setup_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, conversation_t *p_conv, t38_conv *p_t38_conv)
{
	proto_tree *t38_setup_tree;
	proto_item *ti;

	if (!p_t38_conv || p_t38_conv->setup_frame_number == 0) {
		/* there is no Setup info */
		return;
	}

	ti =  proto_tree_add_string_format(tree, hf_t38_setup, tvb, 0, 0,
                      "",
                      "Stream setup by %s (frame %u)",
                      p_t38_conv->setup_method,
                      p_t38_conv->setup_frame_number);
    PROTO_ITEM_SET_GENERATED(ti);
    t38_setup_tree = proto_item_add_subtree(ti, ett_t38_setup);
    if (t38_setup_tree)
    {
		/* Add details into subtree */
		proto_item* item = proto_tree_add_uint(t38_setup_tree, hf_t38_setup_frame,
                                                               tvb, 0, 0, p_t38_conv->setup_frame_number);
		PROTO_ITEM_SET_GENERATED(item);
		item = proto_tree_add_string(t38_setup_tree, hf_t38_setup_method,
                                                     tvb, 0, 0, p_t38_conv->setup_method);
		PROTO_ITEM_SET_GENERATED(item);
    }
}



/* Ethereal Protocol Registration */
void
proto_register_t38(void)
{
	static hf_register_info hf[] =
	{
        {  &hf_t38_IFPPacket,
            { "IFPPacket", "t38.IFPPacket", FT_NONE, BASE_NONE,
		      NULL, 0, "IFPPacket sequence", HFILL }},
        {  &hf_t38_Type_of_msg,
            { "Type of msg", "t38.Type_of_msg_type", FT_UINT32, BASE_DEC,
		      VALS(Type_of_msg_vals), 0, "Type_of_msg choice", HFILL }},
        {  &hf_t38_t30_indicator,
            { "T30 indicator", "t38.t30_indicator", FT_UINT32, BASE_DEC,
              VALS(t30_indicator_vals), 0, "t30_indicator", HFILL }},
        {  &hf_t38_data,
            { "data", "t38.t38_data", FT_UINT32, BASE_DEC,
              VALS(t30_data_vals), 0, "data", HFILL }},
        {  &hf_t38_Data_Field,
            { "Data Field", "t38.Data_Field", FT_NONE, BASE_NONE,
              NULL, 0, "Data_Field sequence of", HFILL }},
        {  &hf_t38_Data_Field_item,
            { "Data_Field_item", "t38.Data_Field_item", FT_NONE, BASE_NONE,
              NULL, 0, "Data_Field_item sequence", HFILL }},
        {  &hf_t38_Data_Field_field_type,
            { "Data_Field_field_type", "t38.Data_Field_field_type", FT_UINT32, BASE_DEC,
              VALS(Data_Field_field_type_vals), 0, "Data_Field_field_type choice", HFILL }},
        {  &hf_t38_Data_Field_field_data,
            { "Data_Field_field_data", "t38.Data_Field_field_data", FT_BYTES, BASE_HEX,
            NULL, 0, "Data_Field_field_data octet string", HFILL }},
        {  &hf_t38_UDPTLPacket,
            { "UDPTLPacket", "t38.UDPTLPacket", FT_NONE, BASE_NONE,
		      NULL, 0, "UDPTLPacket sequence", HFILL }},
        {  &hf_t38_seq_number,
            { "Sequence number", "t38.seq_number", FT_UINT32, BASE_DEC,
		      NULL, 0, "seq_number", HFILL }},
        {  &hf_t38_primary_ifp_packet,
            { "Primary IFPPacket", "t38.primary_ifp_packet", FT_BYTES, BASE_HEX,
              NULL, 0, "primary_ifp_packet octet string", HFILL }},
        {  &hf_t38_primary_ifp_packet_length,
            { "primary_ifp_packet_length", "t38.primary_ifp_packet_length", FT_UINT32, BASE_DEC,
            NULL, 0, "primary_ifp_packet_length", HFILL }},
        {  &hf_t38_error_recovery,
            { "Error recovery", "t38.error_recovery", FT_UINT32, BASE_DEC,
		      VALS(error_recovery_vals), 0, "error_recovery choice", HFILL }},
        {  &hf_t38_secondary_ifp_packets,
            { "Secondary IFPPackets", "t38.secondary_ifp_packets", FT_NONE, BASE_NONE,
              NULL, 0, "secondary_ifp_packets sequence of", HFILL }},
        {  &hf_t38_secondary_ifp_packets_item,
            { "Secondary IFPPackets item", "t38.secondary_ifp_packets_item", FT_BYTES, BASE_HEX,
              NULL, 0, "secondary_ifp_packets_item octet string", HFILL }},
        {  &hf_t38_secondary_ifp_packets_item_length,
            { "secondary_ifp_packets_item_length", "t38.secondary_ifp_packets_item_length", FT_UINT32, BASE_DEC,
            NULL, 0, "secondary_ifp_packets_item_length", HFILL }},
        {  &hf_t38_fec_info,
            { "Fec info", "t38.fec_info", FT_NONE, BASE_NONE,
		      NULL, 0, "fec_info sequence", HFILL }},
        {  &hf_t38_fec_npackets,
            { "Fec npackets", "h245.fec_npackets", FT_INT32, BASE_DEC,
              NULL, 0, "fec_npackets value", HFILL }},
        {  &hf_t38_fec_data,
            { "Fec data", "t38.fec_data", FT_NONE, BASE_NONE,
              NULL, 0, "fec_data sequence of", HFILL }},
        {  &hf_t38_fec_data_item,
            { "t38_fec_data_item", "t38.t38_fec_data_item", FT_BYTES, BASE_HEX,
            NULL, 0, "t38_fec_data_item octet string", HFILL }},
		{   &hf_t38_setup,
		    { "Stream setup", "t38.setup", FT_STRING, BASE_NONE,
		    NULL, 0x0, "Stream setup, method and frame number", HFILL }},
		{   &hf_t38_setup_frame,
            { "Stream frame", "t38.setup-frame", FT_FRAMENUM, BASE_NONE,
            NULL, 0x0, "Frame that set up this stream", HFILL }},
        {   &hf_t38_setup_method,
            { "Stream Method", "t38.setup-method", FT_STRING, BASE_NONE,
            NULL, 0x0, "Method used to set up this stream", HFILL }},
		{&hf_data_fragments,
			{"Message fragments", "data.fragments",
			FT_NONE, BASE_NONE, NULL, 0x00,	NULL, HFILL } },
		{&hf_data_fragment,
			{"Message fragment", "data.fragment",
			FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
		{&hf_data_fragment_overlap,
			{"Message fragment overlap", "data.fragment.overlap",
			FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL } },
		{&hf_data_fragment_overlap_conflicts,
			{"Message fragment overlapping with conflicting data",
			"data.fragment.overlap.conflicts",
			FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL } },
		{&hf_data_fragment_multiple_tails,
			{"Message has multiple tail fragments",
			"data.fragment.multiple_tails", 
			FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL } },
		{&hf_data_fragment_too_long_fragment,
			{"Message fragment too long", "data.fragment.too_long_fragment",
			FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL } },
		{&hf_data_fragment_error,
			{"Message defragmentation error", "data.fragment.error",
			FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
		{&hf_data_reassembled_in,
			{"Reassembled in", "data.reassembled.in",
			FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
	};

	static gint *ett[] =
	{
		&ett_t38,
		&ett_t38_IFPPacket,
		&ett_t38_Type_of_msg,
		&ett_t38_t30_indicator,
		&ett_t38_data,
		&ett_t38_Data_Field,
		&ett_t38_Data_Field_item,
		&ett_t38_Data_Field_field_type,
		&ett_t38_UDPTLPacket,
		&ett_t38_error_recovery,
		&ett_t38_secondary_ifp_packets,
		&ett_t38_fec_info,
		&ett_t38_fec_data,
		&ett_t38_setup,
		&ett_data_fragment,
		&ett_data_fragments
	};

	static hf_register_info hf_t30[] =
	{
        {  &hf_t30_Address,
            { "Address", "t30.Address", FT_UINT8, BASE_HEX,
		      NULL, 0, "Address Field", HFILL }},
        {  &hf_t30_Control,
            { "Control", "t30.Control", FT_UINT8, BASE_HEX,
		      VALS(t30_control_vals), 0, "Address Field", HFILL }},
        {  &hf_t30_Facsimile_Control,
            { "Facsimile Control", "t30.FacsimileControl", FT_UINT8, BASE_DEC,
		      VALS(t30_facsimile_control_field_vals), 0, "Facsimile Control", HFILL }},

		{  &hf_t30_fif_sm,
            { "Store and forward Internet fax- Simple mode (ITU-T T.37)", "t30.fif.sm", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_rtif,
            { "Real-time Internet fax (ITU T T.38)", "t30.fif.rtif", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_3gmn,
            { "3rd Generation Mobile Network", "t30.fif.3gmn", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_v8c,
            { "V.8 capabilities", "t30.fif.v8c", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x04, "", HFILL }},
		{  &hf_t30_fif_op,
            { "Octets preferred", "t30.fif.op", FT_BOOLEAN,  8,
			  TFS(&t30_octets_preferred_value), 0x02, "", HFILL }},
		{  &hf_t30_fif_rtfc,
            { "Ready to transmit a facsimile document (polling)", "t30.fif.rtfc", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_rfo,
            { "Receiver fax operation", "t30.fif.rfo", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_dsr,
            { "Data signalling rate", "t30.fif.dsr", FT_UINT8,  BASE_HEX,
			  VALS(t30_data_signalling_rate_vals), 0x3C, "", HFILL }},
		{  &hf_t30_fif_dsr_dcs,
            { "Data signalling rate", "t30.fif.dsr_dcs", FT_UINT8,  BASE_HEX,
			  VALS(t30_data_signalling_rate_dcs_vals), 0x3C, "", HFILL }},
		{  &hf_t30_fif_res,
            { "R8x7.7 lines/mm and/or 200x200 pels/25.4 mm", "t30.fif.res", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x02, "", HFILL }},
		{  &hf_t30_fif_tdcc,
            { "Two dimensional coding capability", "t30.fif.tdcc", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x01, "", HFILL }},
		{  &hf_t30_fif_rwc,
            { "Recording width capabilities", "t30.fif.rwc", FT_UINT8,  BASE_HEX,
			  VALS(t30_recording_width_capabilities_vals), 0xC0, "", HFILL }},
		{  &hf_t30_fif_rw_dcs,
            { "Recording width", "t30.fif.rw_dcs", FT_UINT8,  BASE_HEX,
			  VALS(t30_recording_width_dcs_vals), 0xC0, "", HFILL }},
		{  &hf_t30_fif_rlc,
            { "Recording length capability", "t30.fif.rlc", FT_UINT8,  BASE_HEX,
			  VALS(t30_recording_length_capability_vals), 0x30, "", HFILL }},
		{  &hf_t30_fif_rl_dcs,
            { "Recording length capability", "t30.fif.rl_dcs", FT_UINT8,  BASE_HEX,
			  VALS(t30_recording_length_dcs_vals), 0x30, "", HFILL }},
		{  &hf_t30_fif_msltcr,
            { "Minimum scan line time capability at the receiver", "t30.fif.msltcr", FT_UINT8,  BASE_HEX,
			  VALS(t30_minimum_scan_line_time_rec_vals), 0x0E, "", HFILL }},
		{  &hf_t30_fif_mslt_dcs,
            { "Minimum scan line time", "t30.fif.mslt_dcs", FT_UINT8,  BASE_HEX,
			  VALS(t30_minimum_scan_line_time_dcs_vals), 0x0E, "", HFILL }},
		{  &hf_t30_fif_ext,
            { "Extension indicator", "t30.fif.ext", FT_BOOLEAN,  8,
			  TFS(&t30_extension_ind_value), 0x01, "", HFILL }},

		{  &hf_t30_fif_cm,
            { "Compress/Uncompress mode", "t30.fif.cm", FT_BOOLEAN,  8,
			  TFS(&t30_compress_value), 0x40, "", HFILL }},
		{  &hf_t30_fif_ecm,
            { "Error correction mode", "t30.fif.ecm", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_fs_dcs,
            { "Frame size", "t30.fif.fs_dcm", FT_BOOLEAN,  8,
			  TFS(&t30_frame_size_dcs_value), 0x10, "", HFILL }},
		{  &hf_t30_fif_t6,
            { "T.6 coding capability", "t30.fif.t6", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x02, "", HFILL }},

		{  &hf_t30_fif_fvc,
            { "Field valid capability", "t30.fif.fvc", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_mspc,
            { "Multiple selective polling capability", "t30.fif.mspc", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_ps,
            { "Polled Subaddress", "t30.fif.ps", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_t43,
            { "T.43 coding", "t30.fif.t43", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_pi,
            { "Plane interleave", "t30.fif.pi", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_vc32k,
            { "Voice coding with 32k ADPCM (ITU T G.726)", "t30.fif.vc32k", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x04, "", HFILL }},

		{  &hf_t30_fif_r8x15,
            { "R8x15.4 lines/mm", "t30.fif.r8x15", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_300x300,
            { "300x300 pels/25.4 mm", "t30.fif.300x300", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_r16x15,
            { "R16x15.4 lines/mm and/or 400x400 pels/25.4 mm", "t30.fif.r16x15", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_ibrp,
            { "Inch based resolution preferred", "t30.fif.ibrp", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_mbrp,
            { "Metric based resolution preferred", "t30.fif.mbrp", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_msltchr,
            { "Minimum scan line time capability for higher resolutions", "t30.fif.msltchr", FT_BOOLEAN,  8,
			  TFS(&t30_minimum_scan_value), 0x04, "", HFILL }},
		{  &hf_t30_fif_rts,
            { "Resolution type selection", "t30.fif.rts", FT_BOOLEAN,  8,
			  TFS(&t30_res_type_sel_value), 0x10, "", HFILL }},
		{  &hf_t30_fif_sp,
            { "Selective polling", "t30.fif.sp", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x02, "", HFILL }},

		{  &hf_t30_fif_sc,
            { "Subaddressing capability", "t30.fif.sc", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_passw,
            { "Password", "t30.fif.passw", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_sit,
            { "Sender Identification transmission", "t30.fif.sit", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_rttd,
            { "Ready to transmit a data file (polling)", "t30.fif.rttd", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_bft,
            { "Binary File Transfer (BFT)", "t30.fif.bft", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_dtm,
            { "Document Transfer Mode (DTM)", "t30.fif.dtm", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x04, "", HFILL }},
		{  &hf_t30_fif_edi,
            { "Electronic Data Interchange (EDI)", "t30.fif.edi", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x02, "", HFILL }},

		{  &hf_t30_fif_btm,
            { "Basic Transfer Mode (BTM)", "t30.fif.btm", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_rttcmmd,
            { "Ready to transmit a character or mixed mode document (polling)", "t30.fif.rttcmmd", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_chrm,
            { "Character mode", "t30.fif.cm", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_mm,
            { "Mixed mode (Annex E/T.4)", "t30.fif.mm", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x04, "", HFILL }},

		{  &hf_t30_fif_pm26,
            { "Processable mode 26 (ITU T T.505)", "t30.fif.pm26", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_dnc,
            { "Digital network capability", "t30.fif.dnc", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_do,
            { "Duplex operation", "t30.fif.do", FT_BOOLEAN,  8,
			  TFS(&t30_duplex_operation_value), 0x20, "", HFILL }},
		{  &hf_t30_fif_jpeg,
            { "JPEG coding", "t30.fif.jpeg", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_fcm,
            { "Full colour mode", "t30.fif.fcm", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_pht,
            { "Preferred Huffman tables", "t30.fif.pht", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_12c,
            { "12 bits/pel component", "t30.fif.12c", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x02, "", HFILL }},

		{  &hf_t30_fif_ns,
            { "No subsampling (1:1:1)", "t30.fif.ns", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_ci,
            { "Custom illuminant", "t30.fif.ci", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_cgr,
            { "Custom gamut range", "t30.fif.cgr", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_nalet,
            { "North American Letter (215.9 x 279.4 mm) capability", "t30.fif.nalet", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_naleg,
            { "North American Legal (215.9 x 355.6 mm) capability", "t30.fif.naleg", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_spscb,
            { "Single-progression sequential coding (ITU-T T.85) basic capability", "t30.fif.spscb", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x04, "", HFILL }},
		{  &hf_t30_fif_spsco,
            { "Single-progression sequential coding (ITU-T T.85) optional L0 capability", "t30.fif.spsco", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x02, "", HFILL }},

		{  &hf_t30_fif_hkm,
            { "HKM key management capability", "t30.fif.hkm", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_rsa,
            { "RSA key management capability", "t30.fif.rsa", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_oc,
            { "Override capability", "t30.fif.oc", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_hfx40,
            { "HFX40 cipher capability", "t30.fif.hfx40", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_acn2c,
            { "Alternative cipher number 2 capability", "t30.fif.acn2c", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_acn3c,
            { "Alternative cipher number 3 capability", "t30.fif.acn3c", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x04, "", HFILL }},
		{  &hf_t30_fif_hfx40i,
            { "HFX40-I hashing capability", "t30.fif.hfx40i", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x02, "", HFILL }},

		{  &hf_t30_fif_ahsn2,
            { "Alternative hashing system number 2 capability", "t30.fif.ahsn2", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_ahsn3,
            { "Alternative hashing system number 3 capability", "t30.fif.ahsn3", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_t441,
            { "T.44 (Mixed Raster Content)", "t30.fif.t441", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_t442,
            { "T.44 (Mixed Raster Content)", "t30.fif.t442", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_t443,
            { "T.44 (Mixed Raster Content)", "t30.fif.t443", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x04, "", HFILL }},
		{  &hf_t30_fif_plmss,
            { "Page length maximum strip size for T.44 (Mixed Raster Content)", "t30.fif.plmss", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x02, "", HFILL }},

		{  &hf_t30_fif_cg300,
            { "Colour/gray-scale 300 pels/25.4 mm x 300 lines/25.4 mm or 400 pels/25.4 mm x 400 lines/25.4 mm resolution", "t30.fif.cg300", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_100x100cg,
            { "100 pels/25.4 mm x 100 lines/25.4 mm for colour/gray scale", "t30.fif.100x100cg", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_spcbft,
            { "Simple Phase C BFT Negotiations capability", "t30.fif.spcbft", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_ebft,
            { "Extended BFT Negotiations capability", "t30.fif.ebft", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_isp,
            { "Internet Selective Polling Address (ISP)", "t30.fif.isp", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_ira,
            { "Internet Routing Address (IRA)", "t30.fif.ira", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x04, "", HFILL }},
	  
		{  &hf_t30_fif_600x600,
            { "600 pels/25.4 mm x 600 lines/25.4 mm", "t30.fif.600x600", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_1200x1200,
            { "1200 pels/25.4 mm x 1200 lines/25.4 mm", "t30.fif.1200x1200", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_300x600,
            { "300 pels/25.4 mm x 600 lines/25.4 mm", "t30.fif.300x600", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_400x800,
            { "400 pels/25.4 mm x 800 lines/25.4 mm", "t30.fif.400x800", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_600x1200,
            { "600 pels/25.4 mm x 1200 lines/25.4 mm", "t30.fif.600x1200", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x08, "", HFILL }},
		{  &hf_t30_fif_cg600x600,
            { "Colour/gray scale 600 pels/25.4 mm x 600 lines/25.4 mm resolution", "t30.fif.cg600x600", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x04, "", HFILL }},
		{  &hf_t30_fif_cg1200x1200,
            { "Colour/gray scale 1200 pels/25.4 mm x 1200 lines/25.4 mm resolution", "t30.fif.cg1200x1200", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x02, "", HFILL }},

		{  &hf_t30_fif_dspcam,
            { "Double sided printing capability (alternate mode)", "t30.fif.dspcam", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x80, "", HFILL }},
		{  &hf_t30_fif_dspccm,
            { "Double sided printing capability (continuous mode)", "t30.fif.dspccm", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x40, "", HFILL }},
		{  &hf_t30_fif_bwmrcp,
            { "Black and white mixed raster content profile (MRCbw)", "t30.fif.bwmrcp", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x20, "", HFILL }},
		{  &hf_t30_fif_t45,
            { "T.45 (run length colour encoding)", "t30.fif.t45", FT_BOOLEAN,  8,
			  TFS(&flags_set_truth), 0x10, "", HFILL }},
		{  &hf_t30_fif_sdmc,
            { "SharedDataMemory capacity", "t30.fif.sdmc", FT_UINT8,  BASE_HEX,
			  VALS(t30_SharedDataMemory_capacity_vals), 0x0C, "", HFILL }},

		{ &hf_t30_fif_number,
		  { "Number", "t30.fif.number", FT_STRING, BASE_NONE, NULL, 0x0,
			"", HFILL }},

        {  &hf_t30_fif_country_code,
            { "ITU-T Country code", "t30.fif.country_code", FT_UINT8, BASE_DEC,
		      NULL, 0, "ITU-T Country code", HFILL }},
        {  &hf_t30_fif_non_stand_bytes,
            { "Non-standard capabilities", "t30.fif.non_standard_cap", FT_BYTES, BASE_HEX,
		      NULL, 0, "Non-standard capabilities", HFILL }},
 
		{  &hf_t30_t4_frame_num,
            { "T.4 Frame number", "t30.t4.frame_num", FT_UINT8, BASE_DEC,
		      NULL, 0, "T.4 Frame number", HFILL }},
        {  &hf_t30_t4_data,
            { "T.4 Facsimile data field", "t30.t4.data", FT_BYTES, BASE_HEX,
		      NULL, 0, "T.4 Facsimile data field", HFILL }},

        {  &hf_t30_partial_page_fcf2,
            { "Post-message command", "t30.pps.fcf2", FT_UINT8, BASE_DEC,
		      VALS(t30_partial_page_fcf2_vals), 0, "Post-message command", HFILL }},			  
		{  &hf_t30_partial_page_i1,
            { "Page counter", "t30.t4.page_count", FT_UINT8, BASE_DEC,
		      NULL, 0, "Page counter", HFILL }},
		{  &hf_t30_partial_page_i2,
            { "Block counter", "t30.t4.block_count", FT_UINT8, BASE_DEC,
		      NULL, 0, "Block counter", HFILL }},
		{  &hf_t30_partial_page_i3,
            { "Frame counter", "t30.t4.frame_count", FT_UINT8, BASE_DEC,
		      NULL, 0, "Frame counter", HFILL }},
	};

	static gint *t30_ett[] =
	{
		&ett_t30,
		&ett_t30_fif,
	};

	module_t *t38_module;

	proto_t38 = proto_register_protocol("T.38", "T.38", "t38");
	proto_register_field_array(proto_t38, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
	register_dissector("t38", dissect_t38, proto_t38);

	/* Init reassemble tables for HDLC */
    register_init_routine(t38_defragment_init);

	t38_tap = register_tap("t38");

	t38_module = prefs_register_protocol(proto_t38, proto_reg_handoff_t38);
	prefs_register_bool_preference(t38_module, "use_pre_corrigendum_asn1_specification",
	    "Use the Pre-Corrigendum ASN.1 specification",
	    "Whether the T.38 dissector should decode using the Pre-Corrigendum T.38 "
		"ASN.1 specification (1998).",
	    &use_pre_corrigendum_asn1_specification);
	prefs_register_bool_preference(t38_module, "dissect_possible_rtpv2_packets_as_rtp",
	    "Dissect possible RTP version 2 packets with RTP dissector",
	    "Whether a UDP packet that looks like RTP version 2 packet will "
		"be dissected as RTP packet or T.38 packet. If enabled there is a risk that T.38 UDPTL "
		"packets with sequence number higher than 32767 may be dissected as RTP.",
	    &dissect_possible_rtpv2_packets_as_rtp);
	prefs_register_uint_preference(t38_module, "tcp.port",
		"T.38 TCP Port",
		"Set the TCP port for T.38 messages",
		10, &global_t38_tcp_port);
	prefs_register_uint_preference(t38_module, "udp.port",
		"T.38 UDP Port",
		"Set the UDP port for T.38 messages",
		10, &global_t38_udp_port);	
	prefs_register_bool_preference(t38_module, "reassembly",
		"Reassemble T.38 PDUs over TPKT over TCP",
		"Whether the dissector should reassemble T.38 PDUs spanning multiple TCP segments "
		"when TPKT is used over TCP. "
        "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
		&t38_tpkt_reassembly);
	prefs_register_enum_preference(t38_module, "tpkt_usage",
		"TPKT used over TCP",
		"Whether T.38 is used with TPKT for TCP",
		(gint *)&t38_tpkt_usage,t38_tpkt_options,FALSE);

	prefs_register_bool_preference(t38_module, "show_setup_info",
                "Show stream setup information",
                "Where available, show which protocol and frame caused "
                "this T.38 stream to be created",
                &global_t38_show_setup_info);

	/* T30 */
	proto_t30 = proto_register_protocol("T.30", "T.30", "t30");
	proto_register_field_array(proto_t30, hf_t30, array_length(hf_t30));
	proto_register_subtree_array(t30_ett, array_length(t30_ett));
}

void
proto_reg_handoff_t38(void)
{
	static int t38_prefs_initialized = FALSE;

	if (!t38_prefs_initialized) {
		t38_udp_handle=create_dissector_handle(dissect_t38_udp, proto_t38);
		t38_tcp_handle=create_dissector_handle(dissect_t38_tcp, proto_t38);
		t38_tcp_pdu_handle=create_dissector_handle(dissect_t38_tcp_pdu, proto_t38);
		t38_prefs_initialized = TRUE;
	}
	else {
		dissector_delete("tcp.port", tcp_port, t38_tcp_handle);
		dissector_delete("udp.port", udp_port, t38_udp_handle);
	}
	tcp_port = global_t38_tcp_port;
	udp_port = global_t38_udp_port;

	dissector_add("tcp.port", tcp_port, t38_tcp_handle);
	dissector_add("udp.port", udp_port, t38_udp_handle);

	rtp_handle = find_dissector("rtp");
}