aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/megaco/packet-megaco.c
blob: 5bbdf518c6f9e210cc1dd235c2ad2d64535f28be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
/* packet-megaco.c
* Routines for megaco packet disassembly
* RFC 3015
*
* $Id$
*
* Christian Falckenberg, 2002/10/17
* Copyright (c) 2002 by Christian Falckenberg
*                       <christian.falckenberg@nortelnetworks.com>
*
* Christoph Wiest,		2003/06/28
* Modified 2003 by		Christoph Wiest
*						<ch.wiest@tesionmail.de>
* Modifyed 2004 by		Anders Broman
*						<anders.broman@ericsson.com>
* To handle TPKT headers if over TCP
* Modified 2005 by		Karl Knoebl
*						<karl.knoebl@siemens.com>
*	provide info to COL_INFO and some "prettification"
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* Copyright 1999 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.
*/


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

#include "moduleinfo.h"

#include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <epan/packet.h>
#include <epan/addr_resolv.h>
#include <epan/prefs.h>
#include <epan/strutil.h>
#include <epan/sctpppids.h>
#include <epan/dissectors/packet-tpkt.h>

#ifndef ENABLE_STATIC
G_MODULE_EXPORT const gchar version[] = VERSION;
#endif

#define PORT_MEGACO_TXT 2944
#define PORT_MEGACO_BIN 2945


void proto_reg_handoff_megaco(void);

/* Define the megaco proto */
static int proto_megaco			= -1;

/* Define headers for megaco */
static int hf_megaco_version    	= -1;
static int hf_megaco_transaction   	= -1;
static int hf_megaco_transid    	= -1;
static int hf_megaco_Context		= -1;
static int hf_megaco_command_line	= -1;
static int hf_megaco_command		= -1;
static int hf_megaco_termid			= -1;



/* Define headers in subtree for megaco */
static int hf_megaco_modem_descriptor           = -1;
static int hf_megaco_multiplex_descriptor       = -1;
static int hf_megaco_media_descriptor			= -1;
static int hf_megaco_events_descriptor          = -1;
static int hf_megaco_signal_descriptor          = -1;
static int hf_megaco_audit_descriptor           = -1;
static int hf_megaco_servicechange_descriptor	= -1;
static int hf_megaco_digitmap_descriptor		= -1;
static int hf_megaco_statistics_descriptor		= -1;
static int hf_megaco_observedevents_descriptor	= -1;
static int hf_megaco_topology_descriptor		= -1;
static int hf_megaco_error_descriptor			= -1;
static int hf_megaco_TerminationState_descriptor= -1;
static int hf_megaco_Remote_descriptor 			= -1;
static int hf_megaco_Local_descriptor 			= -1;
static int hf_megaco_LocalControl_descriptor 	= -1;
static int hf_megaco_packages_descriptor		= -1;
static int hf_megaco_error_Frame				= -1;
static int hf_megaco_Service_State				= -1;
static int hf_megaco_Event_Buffer_Control		= -1;
static int hf_megaco_mode						= -1;
static int hf_megaco_reserve_group				= -1;
static int hf_megaco_reserve_value				= -1;
static int hf_megaco_streamid 					= -1;
static int hf_megaco_requestid 					= -1;
static int hf_megaco_pkgdname					= -1;
static int hf_megaco_mId						= -1;
static int hf_megaco_h245						= -1;

/* Define the trees for megaco */
static int ett_megaco 							= -1;
static int ett_megaco_command_line 				= -1;
static int ett_megaco_mediadescriptor			= -1;
static int ett_megaco_descriptors 				= -1;
static int ett_megaco_TerminationState			= -1;
static int ett_megaco_Localdescriptor			= -1;
static int ett_megaco_Remotedescriptor			= -1;
static int ett_megaco_LocalControldescriptor	= -1;
static int ett_megaco_auditdescriptor			= -1;
static int ett_megaco_eventsdescriptor			= -1;
static int ett_megaco_observedeventsdescriptor	= -1;
static int ett_megaco_observedevent				= -1;
static int ett_megaco_packagesdescriptor		= -1;
static int ett_megaco_requestedevent			= -1;
static int ett_megaco_signalsdescriptor			= -1;
static int ett_megaco_requestedsignal			= -1;
static int ett_megaco_h245 						= -1;

static dissector_handle_t megaco_text_handle;


/*
* Here are the global variables associated with
* the various user definable characteristics of the dissection
*
* MEGACO has two kinds of message formats: text and binary
*
* global_megaco_raw_text determines whether we are going to display
* the raw text of the megaco message, much like the HTTP dissector does.
*
* global_megaco_dissect_tree determines whether we are going to display
* a detailed tree that expresses a somewhat more semantically meaningful
* decode.
*/
static int global_megaco_txt_tcp_port = PORT_MEGACO_TXT;
static int global_megaco_txt_udp_port = PORT_MEGACO_TXT;
#if 0
static int global_megaco_bin_tcp_port = PORT_MEGACO_BIN;
static int global_megaco_bin_udp_port = PORT_MEGACO_BIN;
#endif
static gboolean global_megaco_raw_text = TRUE;
static gboolean global_megaco_dissect_tree = TRUE;

/*
* Variables to allow for proper deletion of dissector registration when
* the user changes port from the gui.
*/
static int txt_tcp_port = 0;
static int txt_udp_port = 0;
#if 0
static int bin_tcp_port = 0;
static int bin_udp_port = 0;
#endif

/* Some basic utility functions that are specific to this dissector */
static gint tvb_skip_wsp(tvbuff_t* tvb, gint offset);
static gint tvb_skip_wsp_return(tvbuff_t* tvb, gint offset);
/*
* The various functions that either dissect some
* subpart of MEGACO.  These aren't really proto dissectors but they
* are written in the same style.
*
*/
static void
dissect_megaco_descriptors(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gint tvb_descriptors_start_offset, gint tvb_descriptors_end_offset);
static void
dissect_megaco_modemdescriptor(tvbuff_t *tvb, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_multiplexdescriptor(tvbuff_t *tvb, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_mediadescriptor(tvbuff_t *tvb, proto_tree *tree,packet_info *pinfo, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_eventsdescriptor(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_signaldescriptor(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_auditdescriptor(tvbuff_t *tvb, proto_tree *tree,packet_info *pinfo, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_servicechangedescriptor(tvbuff_t *tvb, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_digitmapdescriptor(tvbuff_t *tvb, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_statisticsdescriptor(tvbuff_t *tvb, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_observedeventsdescriptor(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_topologydescriptor(tvbuff_t *tvb, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_errordescriptor(tvbuff_t *tvb, proto_tree *tree, gint tvb_RBRKT, gint tvb_previous_offset);
static void
dissect_megaco_TerminationStatedescriptor(tvbuff_t *tvb, proto_tree *tree, gint tvb_next_offset, gint tvb_current_offset);
static void
dissect_megaco_Localdescriptor(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gint tvb_next_offset, gint tvb_current_offset);
static void
dissect_megaco_Remotedescriptor(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gint tvb_next_offset, gint tvb_current_offset);
static void
dissect_megaco_LocalControldescriptor(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gint tvb_next_offset, gint tvb_current_offset);
static void
dissect_megaco_Packagesdescriptor(tvbuff_t *tvb, proto_tree *tree, gint tvb_next_offset, gint tvb_current_offset);
static void
tvb_raw_text_add(tvbuff_t *tvb, proto_tree *tree);
static void
dissect_megaco_text(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);

static dissector_handle_t sdp_handle;
static dissector_handle_t h245_handle;
static proto_tree *top_tree;
/*
 * dissect_megaco_text over TCP, there will be a TPKT header there
 *
 */
static void dissect_megaco_text_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	int lv_tpkt_len;

	/* This code is copied from the Q.931 dissector, some parts skipped.
	 * Check whether this looks like a TPKT-encapsulated
	 * MEGACO packet.
	 *
	 * The minimum length of a MEGACO message is 6?:
	 * Re-assembly ?
	 */
	lv_tpkt_len = is_tpkt(tvb, 6);
	if (lv_tpkt_len == -1) {
		/*
		 * It's not a TPKT packet;
		 * Is in MEGACO ?
		 */
		dissect_megaco_text(tvb, pinfo, tree);
	}
	dissect_tpkt_encap(tvb, pinfo, tree, TRUE,
	    megaco_text_handle);
}
/*
 * dissect_megaco_text - The dissector for the MEGACO Protocol, using
 * text encoding.
 */
static void
dissect_megaco_text(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	gint		tvb_len, len;
	gint		tvb_offset,tvb_current_offset,tvb_previous_offset,tvb_next_offset,tokenlen;
	gint		tvb_command_start_offset, tvb_command_end_offset;
	gint		tvb_descriptors_start_offset, tvb_descriptors_end_offset;
	proto_tree  *megaco_tree, *megaco_tree_command_line, *ti, *sub_ti;
	proto_item* (*my_proto_tree_add_string)(proto_tree*, int, tvbuff_t*, gint, gint, const char*);

	guint8		word[7];
	guint8		transaction[30];
	guint8		TermID[30];
	guint8		tempchar;
	gint		tvb_RBRKT, tvb_LBRKT,  RBRKT_counter, LBRKT_counter;

	top_tree=tree;

	/* Initialize variables */
	tvb_len						= tvb_length(tvb);
	megaco_tree					= NULL;
	ti							= NULL;
	tvb_previous_offset			= 0;
	tvb_current_offset			= 0;
	tvb_offset					= 0;
	tvb_next_offset				= 0;
	tvb_command_start_offset	= 0;
	tvb_command_end_offset		= 0;
	tvb_RBRKT					= 0;
	tvb_LBRKT					= 0;
	RBRKT_counter				= 0;
	LBRKT_counter				= 0;

	/*
	 * Check to see whether we're really dealing with MEGACO by looking
	 * for the "MEGACO" string or a "!".This needs to be improved when supporting
	 * binary encodings. Bugfix add skipping of leading spaces.
	 */
	tvb_offset = tvb_skip_wsp(tvb, tvb_offset);
	/* Quick fix for MEGACO not following the RFC, hopfully not breaking any thing
	 * Turned out to be TPKT in case of TCP, added some code to handle that.
	 *
	 * tvb_offset = tvb_find_guint8(tvb, tvb_offset, 5, 'M');
	 */
	if(!tvb_get_nstringz0(tvb,tvb_offset,sizeof(word),word)) return;
	if (strncasecmp(word, "MEGACO", 6) != 0 && tvb_get_guint8(tvb, tvb_offset ) != '!'){
			return;
	}


	/* Display MEGACO in protocol column */
	if (check_col(pinfo->cinfo, COL_PROTOCOL))
		col_add_str(pinfo->cinfo, COL_PROTOCOL, "MEGACO");

	/* Build the info tree if we've been given a root */
	if (tree){
		/* Create megaco subtree */
		ti = proto_tree_add_item(tree,proto_megaco,tvb, 0, -1, FALSE);
		megaco_tree = proto_item_add_subtree(ti, ett_megaco);

	}
	if(global_megaco_dissect_tree)
		my_proto_tree_add_string = proto_tree_add_string;
	else
		my_proto_tree_add_string = proto_tree_add_string_hidden;

 	/*  Format of 'message' is = MegacopToken SLASH Version SEP mId SEP messageBody */
	/*  MegacopToken = "MEGACO" or "!"						*/
  	/*  According to H248.1-200205 Annex B Text encoding ( protocol version 2 )     */

	/* Find version */
	tvb_previous_offset = tvb_find_guint8(tvb, 0,
		tvb_len, '/');
	if (tvb_previous_offset == -1) {
		proto_tree_add_text(megaco_tree, tvb, 0, -1,
		    "Sorry, no \"/\" in the MEGACO header, I can't parse this packet");
		return;
	}
	tvb_previous_offset = tvb_previous_offset + 1;
	/* As version should follow /, just add 1, works till ver 9 */
	tvb_current_offset  = tvb_previous_offset + 1;


	tokenlen = tvb_current_offset - tvb_previous_offset;

	if (tree)
		my_proto_tree_add_string(megaco_tree, hf_megaco_version, tvb,
		tvb_previous_offset, tokenlen,
		tvb_format_text(tvb, tvb_previous_offset,
		tokenlen));
	/* Pos of version + 2 should take us past version + SEP					*/

	tvb_previous_offset = tvb_previous_offset + 2;
	/* in case of CRLF				*/
	if (tvb_get_guint8(tvb, tvb_current_offset ) == '\n')
		tvb_previous_offset++;
	if (tvb_get_guint8(tvb, tvb_current_offset ) == '\r')
		tvb_previous_offset++;

	/* mId should follow here,								
	 * mId = (( domainAddress / domainName ) [":" portNumber]) / mtpAddress / deviceName 	
	 * domainAddress = "[" (IPv4address / IPv6address) "]"	
	 * domainName = "<" (ALPHA / DIGIT) *63(ALPHA / DIGIT / "-" /".") ">"			
	 * mtpAddress = MTPToken LBRKT 4*8 (HEXDIG) RBRKT		
	 * MTPToken = ("MTP")									
	 * deviceName = pathNAME								
	 * pathNAME = ["*"] NAME *("/" / "*"/ ALPHA / DIGIT /"_" / "$" )["@" pathDomainName ]
	 */

	tokenlen = tvb_find_line_end( tvb, tvb_previous_offset, -1, &tvb_next_offset, FALSE);
	/* accept white spaces as SEParator too */
	if ( (tvb_current_offset=tvb_find_guint8(tvb, tvb_previous_offset, tokenlen, ' ')) != -1 ) {
		/* SEP after mID might be spaces only */
		tokenlen = tvb_current_offset-tvb_previous_offset;
		tvb_next_offset = tvb_skip_wsp(tvb, tvb_current_offset);
	}

   /* Att this point we should point to the "\n" ending the mId element
    * or to the next character after white space SEP 
	*/

	if (tree)
		my_proto_tree_add_string(megaco_tree, hf_megaco_mId, tvb,
		tvb_previous_offset, tokenlen,
		tvb_format_text(tvb, tvb_previous_offset,
		tokenlen));

	tvb_previous_offset = tvb_next_offset;

/* Next part is 										
 *	: messageBody = ( errorDescriptor / transactionList )					
 * 		errorDescriptor = ErrorToken EQUAL ErrorCode LBRKT [quotedString] RBRKT		
 * 			ErrorToken = ("Error" / "ER")						
 *
 *		transactionList = 1*( transactionRequest / transactionReply /			
 *					transactionPending / transactionResponseAck )		
 *
 *		transactionResponseAck = ResponseAckToken LBRKT 				 
 *			transactionAck*(COMMA transactionAck) RBRKT				
 *				ResponseAckToken = ("TransactionResponseAck"/ "K") 		
 *
 *		transactionPending = PendingToken EQUAL TransactionID LBRKT RBRKT		
 *			PendingToken = ("Pending" / "PN")					
 *
 *		transactionReply = ReplyToken EQUAL TransactionID LBRKT				
 *			[ ImmAckRequiredToken COMMA]( errorDescriptor / actionReplyList ) RBRKT 
 *			ReplyToken = ("Reply" / "P")						
 *
 *		transactionRequest = TransToken EQUAL TransactionID LBRKT			
 *			actionRequest *(COMMA actionRequest) RBRKT				
 *			TransToken = ("Transaction" / "T")					
 */

	tempchar = tvb_get_guint8(tvb, tvb_previous_offset );
	if ( (tempchar >= 'a')&& (tempchar <= 'z'))
		tempchar = tempchar - 0x20;

	switch ( tempchar ){
		/* errorDescriptor */
		case 'E':
			if (check_col(pinfo->cinfo, COL_INFO) )
			col_add_fstr(pinfo->cinfo, COL_INFO, "Error  ");
			tokenlen = tvb_len - tvb_previous_offset;
			if (tree) {
				my_proto_tree_add_string(megaco_tree, hf_megaco_transaction, tvb,
				tvb_previous_offset, tokenlen,
				"Error" );

				tvb_command_start_offset = tvb_previous_offset;
				dissect_megaco_errordescriptor(tvb, megaco_tree, tvb_len-1, tvb_command_start_offset);
			}
			return;
			break;
			/* transactionResponseAck 	
			 * transactionResponseAck = ResponseAckToken LBRKT transactionAck
             *                           *(COMMA transactionAck) RBRKT
			 * transactionAck = transactionID / (transactionID "-" transactionID)
			 */
		case 'K':
			tvb_offset  = tvb_find_guint8(tvb, tvb_offset, tvb_len, '{');
			tokenlen = tvb_offset - tvb_previous_offset;
			my_proto_tree_add_string(megaco_tree, hf_megaco_transaction, tvb,
				tvb_previous_offset, tokenlen,
				"TransactionResponseAck" );

			tvb_previous_offset = tvb_skip_wsp(tvb, tvb_offset+1);
			tvb_current_offset = tvb_find_guint8(tvb, tvb_offset+1, tvb_len, '}');
			tvb_current_offset = tvb_skip_wsp_return(tvb, tvb_current_offset)-1; /* cut last RBRKT */
			len = tvb_current_offset - tvb_previous_offset;

			if (check_col(pinfo->cinfo, COL_INFO) )
				col_add_fstr(pinfo->cinfo, COL_INFO, "%s TransactionResponseAck",
				tvb_format_text(tvb,tvb_previous_offset,len));
			if(tree)
				my_proto_tree_add_string(megaco_tree, hf_megaco_transid, tvb,
				tvb_previous_offset, len,
				tvb_format_text(tvb,tvb_previous_offset,len));
				if(global_megaco_raw_text){
					tvb_raw_text_add(tvb, megaco_tree);
				}
			return;
			break;
		/* Pe and PN is transactionPending, P+"any char" is transactionReply */
		case 'P':
			tempchar = tvb_get_guint8(tvb, tvb_previous_offset + 1 );
			switch ( tempchar ){
				case 'e':
					tokenlen = 7;
					if (tree)
					my_proto_tree_add_string(megaco_tree, hf_megaco_transaction, tvb,
					tvb_previous_offset, tokenlen,
					"Pending" );

					tvb_offset  = tvb_find_guint8(tvb, tvb_previous_offset, tvb_len, '=')+1;
					tvb_previous_offset = tvb_skip_wsp(tvb, tvb_offset);
					len = tvb_len - tvb_offset - 2;
					if (check_col(pinfo->cinfo, COL_INFO) )
					col_add_fstr(pinfo->cinfo, COL_INFO, "%s Pending",
					tvb_format_text(tvb,tvb_offset,len));

					if(tree)
					my_proto_tree_add_string(megaco_tree, hf_megaco_transid, tvb,
					tvb_previous_offset, tokenlen,
					tvb_format_text(tvb,tvb_offset,len));

				return;
				break;
				case 'N':
					tokenlen = 2;
					if (tree)
					my_proto_tree_add_string(megaco_tree, hf_megaco_transaction, tvb,
					tvb_previous_offset, tokenlen,
					"Pending" );

					tvb_offset  = tvb_find_guint8(tvb, tvb_previous_offset, tvb_len, '=')+1;
					len = tvb_len - tvb_offset - 2;
					if (check_col(pinfo->cinfo, COL_INFO) )
					col_add_fstr(pinfo->cinfo, COL_INFO, "%s Pending",
					tvb_format_text(tvb,tvb_offset,len));

					if(tree)
					my_proto_tree_add_string(megaco_tree, hf_megaco_transid, tvb,
					tvb_previous_offset, tokenlen,
					tvb_format_text(tvb,tvb_offset,len));
					return;
					break;
				/* Reply */
   				default :
					tokenlen = 1;
					if (tree)
					my_proto_tree_add_string(megaco_tree, hf_megaco_transaction, tvb,
					tvb_previous_offset, tokenlen,
					"Reply" );

					tvb_offset  = tvb_find_guint8(tvb, tvb_previous_offset, tvb_len, '=')+1;
					tvb_offset = tvb_skip_wsp(tvb, tvb_offset);
					tvb_current_offset  = tvb_find_guint8(tvb, tvb_offset, tvb_len, '{');
					tvb_current_offset  = tvb_skip_wsp_return(tvb, tvb_current_offset-1);
					len = tvb_current_offset - tvb_offset;

					if (check_col(pinfo->cinfo, COL_INFO) )
					col_add_fstr(pinfo->cinfo, COL_INFO, "%s Reply  ",
					tvb_format_text(tvb,tvb_offset,len));
					if(tree)
					my_proto_tree_add_string(megaco_tree, hf_megaco_transid, tvb,
					tvb_offset, len,
					tvb_format_text(tvb,tvb_offset,len));
				break;
				}/* end switch */
			break; /* Case 'P' */
		/* transactionReply */
		case 'R':
			tokenlen = 5;
			if (tree)
			my_proto_tree_add_string(megaco_tree, hf_megaco_transaction, tvb,
			tvb_previous_offset, tokenlen,
			"Reply" );

			tvb_offset  = tvb_find_guint8(tvb, tvb_previous_offset, tvb_len, '=')+1;
			tvb_offset = tvb_skip_wsp(tvb, tvb_offset);
			tvb_current_offset  = tvb_find_guint8(tvb, tvb_offset, tvb_len, '{');
			tvb_current_offset  = tvb_skip_wsp_return(tvb, tvb_current_offset-1);
			len = tvb_current_offset - tvb_offset;

			if (check_col(pinfo->cinfo, COL_INFO) )
				col_add_fstr(pinfo->cinfo, COL_INFO, "%s Reply  ",
				tvb_format_text(tvb,tvb_offset,len));
			if(tree)
				my_proto_tree_add_string(megaco_tree, hf_megaco_transid, tvb,
				tvb_offset, len,
				tvb_format_text(tvb,tvb_offset,len));
			break;
		/* TransactionRequest or TransactionResponseAck	*/

		case 'T':
			tokenlen = 1;
			if (tvb_get_guint8(tvb, tvb_previous_offset + 1 )!= 'r' )
			{
				/* TransactionRequest short notation */
			}
			else{					/* TransactionRequest or TransactionResponseAck	*/
				len = 22;			/* TransactionResponseAck is 22 characters      */
				tokenlen = 11;
				tvb_get_nstringz0(tvb,tvb_previous_offset,len+1,transaction);

 				if ( transaction[19] == 'A'){
					tvb_offset  = tvb_find_guint8(tvb, tvb_offset, tvb_len, '{')+1;
					tvb_offset = tvb_skip_wsp(tvb, tvb_offset);
					tvb_next_offset = tvb_find_guint8(tvb, tvb_offset, tvb_len, '}') - 1;
					tvb_next_offset = tvb_skip_wsp_return(tvb, tvb_next_offset);
					len = tvb_next_offset - tvb_offset;
					if (check_col(pinfo->cinfo, COL_INFO) )
						col_add_fstr(pinfo->cinfo, COL_INFO, "%s TransactionResponseAck",
						tvb_format_text(tvb,tvb_offset,len));
					if(tree)
						len = tvb_len - tvb_previous_offset;
						proto_tree_add_text(megaco_tree, tvb, tvb_previous_offset, -1,
							"%s",tvb_format_text(tvb, tvb_previous_offset, len));

						my_proto_tree_add_string(megaco_tree, hf_megaco_transid, tvb,
										tvb_offset, (tvb_next_offset - tvb_offset),
										tvb_format_text(tvb,tvb_offset,(tvb_next_offset - tvb_offset)));
					if(global_megaco_raw_text){
						tvb_raw_text_add(tvb, megaco_tree);
						}
					return;
					break;
				}
			}/* else  */

			/* TransactionRequest 	*/
			if(tree)
			my_proto_tree_add_string(megaco_tree, hf_megaco_transaction, tvb,
				tvb_previous_offset, tokenlen,
				"Request" );
			tvb_offset  = tvb_find_guint8(tvb, tvb_offset, tvb_len, '=')+1;
			tvb_offset = tvb_skip_wsp(tvb, tvb_offset);
			tvb_current_offset  = tvb_find_guint8(tvb, tvb_offset, tvb_len, '{');
			tvb_current_offset  = tvb_skip_wsp_return(tvb, tvb_current_offset-1);
			len = tvb_current_offset - tvb_offset;
			if (check_col(pinfo->cinfo, COL_INFO) )
				col_add_fstr(pinfo->cinfo, COL_INFO, "%s Request",
				tvb_format_text(tvb,tvb_offset,len));
			if(tree)
				my_proto_tree_add_string(megaco_tree, hf_megaco_transid, tvb,
				tvb_offset,len,
				tvb_format_text(tvb,tvb_offset,len));

			break;
		default :
			ti = proto_tree_add_item(tree,proto_megaco,tvb, 0, -1, FALSE);
			megaco_tree = proto_item_add_subtree(ti, ett_megaco);
			proto_tree_add_text(megaco_tree, tvb, 0, -1,
		    "Sorry, can't understand errorDescriptor / transactionList = %s, can't parse it pos %u",
                         tvb_format_text(tvb,tvb_previous_offset,2),tvb_previous_offset);
		return;
		break;
		} /* end switch */
/* 		Only these remains now									*/
/*		transactionReply = ReplyToken EQUAL TransactionID LBRKT					*/
/*			[ ImmAckRequiredToken COMMA]( errorDescriptor / actionReplyList ) RBRKT		*/
/*			ReplyToken = ("Reply" / "P")							*/

/*		transactionRequest = TransToken EQUAL TransactionID LBRKT				*/
/*			actionRequest *(COMMA actionRequest) RBRKT					*/
/*			TransToken = ("Transaction" / "T")						*/

/* Ready to here etxrab */
if(tree) {   /* Only do the rest if tree built */
		/* Find Context */
nextcontext:
		tvb_previous_offset = tvb_find_guint8(tvb, tvb_current_offset,
			tvb_len, '=')+1;
		tvb_previous_offset = tvb_skip_wsp(tvb, tvb_previous_offset);
		tvb_next_offset = tvb_find_guint8(tvb, tvb_previous_offset,
			tvb_len, '{');
		if (tvb_current_offset >= tvb_next_offset) {
			proto_tree_add_text(megaco_tree, tvb, 0, 0, "[ Parse error: Invalid offset ]");
			return;
		}
		tvb_current_offset = tvb_next_offset;


		tokenlen = tvb_current_offset - tvb_previous_offset;
		tempchar = tvb_get_guint8(tvb, tvb_previous_offset );

		if (tvb_get_guint8(tvb, tvb_current_offset-1 ) == ' '){
			tokenlen--;
		}

		switch ( tempchar ){
		case '$':
			my_proto_tree_add_string(megaco_tree, hf_megaco_Context, tvb,
				tvb_previous_offset, 1,
				"Choose one");
			if (check_col(pinfo->cinfo, COL_INFO) )
				col_append_fstr(pinfo->cinfo, COL_INFO, " |=Choose one");
			break;
		case '*':
			my_proto_tree_add_string(megaco_tree, hf_megaco_Context, tvb,
				tvb_previous_offset, 1,
				"All");
			if (check_col(pinfo->cinfo, COL_INFO) )
				col_append_fstr(pinfo->cinfo, COL_INFO, " |=All");
			break;
		case '-':
			proto_tree_add_text(megaco_tree, tvb, tvb_previous_offset, tokenlen, "Context: NULL" );
			if (check_col(pinfo->cinfo, COL_INFO) )
				col_append_fstr(pinfo->cinfo, COL_INFO, " |=NULL");
			break;
		default:
			my_proto_tree_add_string(megaco_tree, hf_megaco_Context, tvb,
				tvb_previous_offset, tokenlen,
				tvb_format_text(tvb, tvb_previous_offset,
				tokenlen));
			if (check_col(pinfo->cinfo, COL_INFO) )
				col_append_fstr(pinfo->cinfo, COL_INFO, " |=%s",tvb_format_text(tvb, tvb_previous_offset,tokenlen));
		}

		/* Find Commands */


		/* If transaction is ERROR the plugin will call the ERROR subroutine */
		if ( transaction[0] == 'E'){
			tvb_offset = tvb_find_guint8(tvb, 0, tvb_len, '/');
			tvb_command_start_offset = tvb_find_guint8(tvb, tvb_offset, tvb_len, 'E');
			dissect_megaco_errordescriptor(tvb, megaco_tree, tvb_len-1, tvb_command_start_offset);
			return;
		}

		else{

			/* If Transaction is is Request, Reply or Pending */

			tvb_command_start_offset = tvb_skip_wsp(tvb, tvb_current_offset +1);
			tvb_command_end_offset = tvb_command_start_offset;

			tvb_LBRKT = tvb_command_start_offset;
			tvb_RBRKT = tvb_command_start_offset;
		}


		/* The following loop find the individual contexts, commands and call the for every Descriptor a subroutine */

		do {
			tvb_command_end_offset = tvb_find_guint8(tvb, tvb_command_end_offset +1,
				tvb_len, ',');

			if ( tvb_command_end_offset == -1 ){
				tvb_command_end_offset = tvb_len;

			}

			/* checking how many left brackets are before the next comma */

			while ( tvb_find_guint8(tvb, tvb_LBRKT+1,tvb_len, '{') != -1
				&& (tvb_find_guint8(tvb, tvb_LBRKT+1,tvb_len, '{') < tvb_command_end_offset)){

				tvb_LBRKT = tvb_find_guint8(tvb, tvb_LBRKT+1,
					tvb_len, '{');

				LBRKT_counter++;
			}

			/* checking how many right brackets are before the next comma */

			while ( (tvb_find_guint8(tvb, tvb_RBRKT+1,tvb_len, '}') != -1 )
				&& (tvb_find_guint8(tvb, tvb_RBRKT+1,tvb_len, '}') < tvb_command_end_offset)
				&& LBRKT_counter != 0){

				tvb_RBRKT = tvb_find_guint8(tvb, tvb_RBRKT+1,
					tvb_len, '}');
				RBRKT_counter++;


			}

			/* If equal or more right brackets before the comma, one command is complete */

			if ( LBRKT_counter <= RBRKT_counter ){

				tvb_current_offset  = tvb_find_guint8(tvb, tvb_command_start_offset,
					tvb_len, '{');


				/* includes no descriptors */

				if ( LBRKT_counter == 0 ){

					tvb_current_offset = tvb_command_end_offset;

					/* the last command in a context */

					if ( tvb_find_guint8(tvb, tvb_command_start_offset, tvb_len, '}') < tvb_current_offset
						&& tvb_find_guint8(tvb, tvb_command_start_offset, tvb_len, '}') != -1){

						tvb_previous_offset  = tvb_find_guint8(tvb, tvb_command_start_offset,
							tvb_len, '}');


						tvb_previous_offset = tvb_skip_wsp_return(tvb, tvb_previous_offset -1);

						tokenlen =  tvb_previous_offset - tvb_command_start_offset;

					}

					/* not the last command in a context*/

					else{
						tvb_current_offset = tvb_skip_wsp_return(tvb, tvb_current_offset -1);

						tokenlen =  tvb_current_offset - tvb_command_start_offset;
					}
				}

				/* command includes descriptors */

				else{
					tvb_current_offset = tvb_skip_wsp_return(tvb, tvb_current_offset -1);

					tokenlen =  tvb_current_offset - tvb_command_start_offset;
				}

				/* if a next context is specified */

				if ( tvb_get_guint8(tvb, tvb_command_start_offset ) == 'C'){
					tvb_current_offset = tvb_command_start_offset;
					LBRKT_counter = 0;
					RBRKT_counter = 0;
					goto nextcontext;
				}

				/* creation of the megaco_tree_command_line additionally Command and Transaction ID will be printed in this line */

				sub_ti = proto_tree_add_item(megaco_tree,hf_megaco_command_line,tvb,tvb_command_start_offset,tokenlen, FALSE);
				megaco_tree_command_line = proto_item_add_subtree(sub_ti, ett_megaco_command_line);

				tvb_next_offset = tvb_command_start_offset + tokenlen;

				/* Additional value */

				if ( tvb_get_guint8(tvb, tvb_command_start_offset ) == 'O'){

					proto_tree_add_text(megaco_tree_command_line, tvb, tvb_command_start_offset, 2, "O- indicates an optional command" );
					tvb_command_start_offset = tvb_command_start_offset+2;

				}

				/* Additional value */

				if ( tvb_get_guint8(tvb, tvb_command_start_offset ) == 'W'){

					proto_tree_add_text(megaco_tree_command_line, tvb, tvb_command_start_offset, 2, "W- indicates a wildcarded response to a command" );
					tvb_command_start_offset = tvb_command_start_offset+2;

				}



				tvb_offset  = tvb_find_guint8(tvb, tvb_command_start_offset,
					tvb_len, '=');
				tvb_offset = tvb_skip_wsp_return(tvb, tvb_offset -1);
				tokenlen = tvb_offset - tvb_command_start_offset;

				tempchar = tvb_get_guint8(tvb, tvb_command_start_offset);
				if ( (tempchar >= 'a')&& (tempchar <= 'z'))
					tempchar = tempchar - 0x20;

				if ( tempchar != 'E' ){

					if ( tvb_get_guint8(tvb, 0 ) == '!'){

						switch ( tempchar ){

						case 'A':

							tempchar = tvb_get_guint8(tvb, tvb_command_start_offset+1);

							switch ( tempchar ){

							case 'V':
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"AuditValue");
								if (check_col(pinfo->cinfo, COL_INFO) )
									col_append_fstr(pinfo->cinfo, COL_INFO, " AuditValue");
								break;

							case 'C':
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"AuditCapability");
								if (check_col(pinfo->cinfo, COL_INFO) )
									col_append_fstr(pinfo->cinfo, COL_INFO, " AuditCapability");
								break;

							default:
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"Add");
								if (check_col(pinfo->cinfo, COL_INFO) )
									col_append_fstr(pinfo->cinfo, COL_INFO, " Add");
								break;
							}
							break;

						case 'N':
							my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
								tvb_command_start_offset, tokenlen,
								"Notify");
								if (check_col(pinfo->cinfo, COL_INFO) )
									col_append_fstr(pinfo->cinfo, COL_INFO, " Notify");
							break;

						case 'M':

							tempchar = tvb_get_guint8(tvb, tvb_command_start_offset+1);

							switch ( tempchar ){
							case 'F':
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"Modify");
								if (check_col(pinfo->cinfo, COL_INFO) )
									col_append_fstr(pinfo->cinfo, COL_INFO, " Modify");
								break;

							case 'V':
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"Move");
								if (check_col(pinfo->cinfo, COL_INFO) )
									col_append_fstr(pinfo->cinfo, COL_INFO, " Move");
								break;
							}
							break;

						case 'P':
							/*
							PackagesToken	= ("Packages"	/ "PG")
							PendingToken	= ("Pending"	/ "PN")
							PriorityToken	= ("Priority"	/ "PR")
							ProfileToken	= ("Profile"	/ "PF")
							*/
							tempchar = tvb_get_guint8(tvb, tvb_command_start_offset+1);

							switch ( tempchar ){
							case 'G':
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"Packages");
								break;
							case 'N':
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"Pending");
								break;
							case 'R':
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"Priority");
								break;
							case 'F':
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"Profile");
								break;
							}
							break;

						case 'S':
							tempchar = tvb_get_guint8(tvb, tvb_command_start_offset+1);

							switch ( tempchar ){

							case 'C':
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"ServiceChange");
								break;

							default:
								my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
									tvb_command_start_offset, tokenlen,
									"Subtract");
								if (check_col(pinfo->cinfo, COL_INFO) )
									col_append_fstr(pinfo->cinfo, COL_INFO, " Subtract");
								break;
							}
							break;

						default:
							tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;
							tvb_ensure_bytes_exist(tvb, tvb_previous_offset, tokenlen);
							proto_tree_add_string(megaco_tree, hf_megaco_error_Frame, tvb,
								tvb_previous_offset, tokenlen,
								"No Command detectable !");
							return;

							break;
						}
					}
					else{
						my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_command, tvb,
							tvb_command_start_offset, tokenlen,
							tvb_format_text(tvb, tvb_command_start_offset,
							tokenlen));
							if (check_col(pinfo->cinfo, COL_INFO) )
								col_append_fstr(pinfo->cinfo, COL_INFO, " %s",tvb_format_text(tvb, tvb_command_start_offset,tokenlen));
					}


					tvb_offset  = tvb_find_guint8(tvb, tvb_command_start_offset,
						tvb_len, '=');
					tvb_offset = tvb_skip_wsp(tvb, tvb_offset+1);
					tokenlen = tvb_next_offset - tvb_offset;

					tempchar = tvb_get_guint8(tvb, tvb_offset);

					switch ( tempchar ){

					case 'E':
						if ((tokenlen+1 > (int) sizeof(TermID)) || (tokenlen+1 <= 0)) {
							proto_tree_add_text(megaco_tree, tvb, 0, 0, "[ Parse error: Invalid TermID length (%d) ]", tokenlen+1);
							return;
						}
						tvb_get_nstringz0(tvb,tvb_offset,tokenlen+1,TermID);
						TermID[0] = 'e';
						my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_termid, tvb,
							tvb_offset, tokenlen,
							TermID);
						break;

					case '*':
						my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_termid, tvb,
							tvb_offset, tokenlen,
							"WildCard all");
							if (check_col(pinfo->cinfo, COL_INFO) )
								col_append_fstr(pinfo->cinfo, COL_INFO, "=*");
						break;

					case '$':
						my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_termid, tvb,
							tvb_offset, tokenlen,
							"WildCard any");
							if (check_col(pinfo->cinfo, COL_INFO) )
								col_append_fstr(pinfo->cinfo, COL_INFO, "=$");
						break;

					default:
						my_proto_tree_add_string(megaco_tree_command_line, hf_megaco_termid, tvb,
							tvb_offset, tokenlen,
							tvb_format_text(tvb, tvb_offset,
							tokenlen));
							if (check_col(pinfo->cinfo, COL_INFO) )
								col_append_fstr(pinfo->cinfo, COL_INFO, "=%s",tvb_format_text(tvb, tvb_offset,tokenlen));
						break;
					}

			}
			/* Dissect the Descriptors */


			if ( LBRKT_counter != 0 && tvb_current_offset != tvb_command_end_offset){

				tvb_descriptors_start_offset  = tvb_find_guint8(tvb, tvb_command_start_offset,
					tvb_len, '{');

				tvb_descriptors_end_offset = tvb_descriptors_start_offset;


				while ( LBRKT_counter > 0 ){

					tvb_descriptors_end_offset = tvb_find_guint8(tvb, tvb_descriptors_end_offset+1,
						tvb_len, '}');

					LBRKT_counter--;

				}

				tempchar = tvb_get_guint8(tvb, tvb_command_start_offset);

				if ( tempchar == 'E'){
					dissect_megaco_descriptors(tvb, megaco_tree_command_line, pinfo, tvb_command_start_offset-1,tvb_descriptors_end_offset);
				}
				else {
					dissect_megaco_descriptors(tvb, megaco_tree_command_line, pinfo, tvb_descriptors_start_offset,tvb_descriptors_end_offset);
				}
			}
			RBRKT_counter = 0;
			LBRKT_counter = 0;
			tvb_command_start_offset = tvb_skip_wsp(tvb, tvb_command_end_offset +1);
			tvb_LBRKT = tvb_command_start_offset;
			tvb_RBRKT = tvb_command_start_offset;

			}
		} while ( tvb_command_end_offset < tvb_len );
	}
	if(global_megaco_raw_text){
		tvb_raw_text_add(tvb, megaco_tree);
	}
}

static void
dissect_megaco_descriptors(tvbuff_t *tvb, proto_tree *megaco_tree_command_line, packet_info *pinfo, gint tvb_descriptors_start_offset, gint tvb_descriptors_end_offset)
{
	gint		tvb_len, len;
	gint		tvb_current_offset,tvb_previous_offset,tokenlen;
	gint		tvb_RBRKT, tvb_LBRKT,  RBRKT_counter, LBRKT_counter;
	guint8	  tempchar;
	tvb_len  	= tvb_length(tvb);


	len				= 0;
	tvb_RBRKT		= 0;
	tvb_LBRKT		= 0;
	RBRKT_counter	= 0;
	LBRKT_counter	= 0;


	tokenlen = tvb_descriptors_end_offset - tvb_descriptors_start_offset;


	tvb_LBRKT = tvb_skip_wsp(tvb, tvb_descriptors_start_offset +1);

	tvb_previous_offset = tvb_LBRKT;
	tvb_RBRKT = tvb_descriptors_start_offset;



	do {


		tvb_RBRKT = tvb_find_guint8(tvb, tvb_RBRKT+1,
			tvb_len, '}');
		tvb_LBRKT = tvb_find_guint8(tvb, tvb_LBRKT,
			tvb_len, '{');

		tvb_current_offset 	= tvb_find_guint8(tvb, tvb_previous_offset,
			tvb_len, ',');

		if (tvb_current_offset == -1 ){
			tvb_current_offset = tvb_descriptors_end_offset;

		}
		if (tvb_current_offset <= tvb_previous_offset) {
			proto_tree_add_text(megaco_tree_command_line, tvb, 0, 0, "[ Parse error: Invalid offset ]");
			return;
		}



		/* Descriptor includes no parameters */

		if ( tvb_LBRKT > tvb_current_offset || tvb_LBRKT == -1 ){

			if ( tvb_current_offset > tvb_RBRKT ){
				tvb_current_offset = tvb_RBRKT;
			}

			tvb_RBRKT = tvb_skip_wsp_return(tvb, tvb_current_offset-1)-1;
		}

		/* Descriptor includes Parameters */
		if ( (tvb_current_offset > tvb_LBRKT && tvb_LBRKT != -1)){

			while ( tvb_LBRKT != -1 && tvb_RBRKT > tvb_LBRKT ){


				tvb_LBRKT  = tvb_find_guint8(tvb, tvb_LBRKT+1,
					tvb_len, '{');
				if ( tvb_LBRKT < tvb_RBRKT && tvb_LBRKT != -1)
					tvb_RBRKT  = tvb_find_guint8(tvb, tvb_RBRKT+1,
					tvb_len, '}');


			}

		}


		tempchar = tvb_get_guint8(tvb, tvb_previous_offset );
		if ( (tempchar >= 'a')&& (tempchar <= 'z'))
			tempchar = tempchar - 0x20;

		switch ( tempchar ){

		case 'M':
			tempchar = tvb_get_guint8(tvb, tvb_previous_offset+1 );
			switch ( tempchar ){

			case 'o':
				dissect_megaco_modemdescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 'D':
				dissect_megaco_modemdescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 'u':
				dissect_megaco_multiplexdescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 'X':
				dissect_megaco_multiplexdescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 'e':
				dissect_megaco_mediadescriptor(tvb, megaco_tree_command_line, pinfo, tvb_RBRKT, tvb_previous_offset);
				break;
			case ',':
				break;

			default:
				dissect_megaco_mediadescriptor(tvb, megaco_tree_command_line, pinfo, tvb_RBRKT, tvb_previous_offset);
				break;
			}
			break;

		case 'S':
			tempchar = tvb_get_guint8(tvb, tvb_previous_offset+1 );
			switch ( tempchar ){
			case 'i':
				dissect_megaco_signaldescriptor(tvb, pinfo, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 'G':
				dissect_megaco_signaldescriptor(tvb, pinfo, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 'e':
				dissect_megaco_servicechangedescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 'V':
				dissect_megaco_servicechangedescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 'C':
				dissect_megaco_servicechangedescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 't':
				dissect_megaco_statisticsdescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;

			case 'A':
				dissect_megaco_statisticsdescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
				break;
			default:
				tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;
				proto_tree_add_string(megaco_tree_command_line, hf_megaco_error_Frame, tvb,
					tvb_previous_offset, tokenlen,
					"No Descriptor detectable !");
				break;
			}
			break;

		case 'E':
			tempchar = tvb_get_guint8(tvb, tvb_previous_offset+1 );
			if ( tempchar == 'r' || tempchar == 'R'){

				if (  tvb_get_guint8(tvb, tvb_skip_wsp(tvb, tvb_RBRKT +1)) == ';'){
					tvb_RBRKT = tvb_find_guint8(tvb, tvb_RBRKT+1, tvb_len, '}');
					tvb_RBRKT = tvb_skip_wsp_return(tvb, tvb_RBRKT -1)-1;
				}
				dissect_megaco_errordescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
			}
			else{
				dissect_megaco_eventsdescriptor(tvb, pinfo, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
			}
			break;

		case 'A':
			dissect_megaco_auditdescriptor(tvb, megaco_tree_command_line, pinfo, tvb_RBRKT, tvb_previous_offset);
			break;

		case 'D':
			dissect_megaco_digitmapdescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
			break;

		case 'O':
			dissect_megaco_observedeventsdescriptor(tvb, pinfo, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
			break;

		case 'T':
			dissect_megaco_topologydescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
			break;

		case 'P':
			dissect_megaco_Packagesdescriptor(tvb, megaco_tree_command_line, tvb_RBRKT, tvb_previous_offset);
			break;

		default:
			tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;
			proto_tree_add_string(megaco_tree_command_line, hf_megaco_error_Frame, tvb,
				tvb_previous_offset, tokenlen,
				"No Descriptor detectable !");
			break;

	}


	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;

	tvb_current_offset  	= tvb_find_guint8(tvb, tvb_RBRKT,
		tvb_len, ',');
	if (tvb_current_offset == -1 ){
		tvb_current_offset = tvb_descriptors_end_offset;
	}
	tvb_previous_offset = tvb_skip_wsp(tvb, tvb_current_offset+1);
	tvb_LBRKT = tvb_previous_offset;
	tvb_RBRKT = tvb_previous_offset;


	} while ( tvb_current_offset < tvb_descriptors_end_offset );

}

static void
dissect_megaco_modemdescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint 	tokenlen;

	tokenlen = 0;



	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;
	proto_tree_add_string(megaco_tree_command_line, hf_megaco_modem_descriptor, tvb,
					 		tvb_previous_offset, tokenlen,
							tvb_format_text(tvb, tvb_previous_offset,
							tokenlen));

}
static void
dissect_megaco_multiplexdescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint 	tokenlen;

	tokenlen = 0;

	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;
	proto_tree_add_string(megaco_tree_command_line, hf_megaco_multiplex_descriptor, tvb,
					 		tvb_previous_offset, tokenlen,
							tvb_format_text(tvb, tvb_previous_offset,
							tokenlen));

}

/* mediaDescriptor = MediaToken LBRKT mediaParm *(COMMA mediaParm) RBRKT								
 *	MediaToken = ("Media" / "M")													
 *																	
 *		mediaParm = (streamParm / streamDescriptor /terminationStateDescriptor)							
 *																	
 *	; at-most one terminationStateDescriptor											
 *	; and either streamParm(s) or streamDescriptor(s) but not both									
 *			streamParm = ( localDescriptor / remoteDescriptor /localControlDescriptor )					
 *				localDescriptor = LocalToken LBRKT octetString RBRKT							
 *							LocalToken = ("Local" / "L")							
 *							octetString = *(nonEscapeChar)							
 *									nonEscapeChar = ( "\}" / %x01-7C / %x7E-FF )			
 *				remoteDescriptor = RemoteToken LBRKT octetString RBRKT							
 *							RemoteToken = ("Remote" / "R")							
 *				localControlDescriptor = LocalControlToken LBRKT localParm*(COMMA localParm) RBRKT			
 *							LocalControlToken = ("LocalControl" / "O")					
 *							localParm = ( streamMode / propertyParm / reservedValueMode			
 *			streamDescriptor = StreamToken EQUAL StreamID LBRKT streamParm*(COMMA streamParm) RBRKT				
 *							StreamToken = ("Stream" / "ST")							
 *			terminationStateDescriptor = TerminationStateToken LBRKTterminationStateParm 					
 *								*( COMMA terminationStateParm ) RBRKT					
 *							TerminationStateToken = ("TerminationState" / "TS")				
 *							terminationStateParm =(propertyParm / serviceStates / eventBufferControl )	
 */

static void
dissect_megaco_mediadescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line,packet_info *pinfo,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint 	tokenlen;
	gint	tvb_next_offset, tvb_current_offset, tvb_offset, tvb_help_offset;
	guint8	tempchar;


	proto_tree  *megaco_mediadescriptor_tree, *megaco_mediadescriptor_ti;

	tokenlen			= 0;
	tvb_next_offset			= 0;
	tvb_current_offset		= 0;
	tvb_offset			= 0;
	tvb_help_offset			= 0;

	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;


	megaco_mediadescriptor_ti = proto_tree_add_text(megaco_tree_command_line,tvb,tvb_previous_offset,tokenlen,"Media Descriptor");
	megaco_mediadescriptor_tree = proto_item_add_subtree(megaco_mediadescriptor_ti, ett_megaco_mediadescriptor);

	tvb_current_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '=');
	tokenlen = tvb_current_offset  - tvb_previous_offset -1;
	proto_tree_add_text(megaco_mediadescriptor_tree, tvb,	tvb_previous_offset, tokenlen,
		"%s",	tvb_format_text(tvb, tvb_previous_offset,	tokenlen));

	tvb_next_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '{');
	/* If a StreamID is present */

	if ( tvb_find_guint8(tvb, tvb_next_offset+1, tvb_RBRKT, '{') > tvb_current_offset && tvb_current_offset > tvb_previous_offset ){
		tvb_next_offset = tvb_find_guint8(tvb, tvb_next_offset+1, tvb_RBRKT, '{');
		tvb_current_offset = tvb_skip_wsp(tvb, tvb_current_offset +1);

		tvb_offset = tvb_skip_wsp_return(tvb, tvb_next_offset-2);
		tokenlen =  tvb_offset - tvb_current_offset;

		proto_tree_add_string(megaco_mediadescriptor_tree, hf_megaco_streamid, tvb,
			tvb_current_offset, tokenlen,
			tvb_format_text(tvb, tvb_current_offset,
			tokenlen));
	}
	tvb_current_offset = tvb_next_offset ;



	while ( tvb_find_guint8(tvb, tvb_current_offset+1 , tvb_RBRKT, '{') != -1 && tvb_find_guint8(tvb, tvb_current_offset+1 , tvb_RBRKT, '{') < tvb_RBRKT && tvb_next_offset != -1){

		tvb_help_offset = tvb_next_offset;
		tvb_current_offset = tvb_find_guint8(tvb, tvb_current_offset+1 , tvb_RBRKT, '{');
		tvb_next_offset = tvb_find_guint8(tvb, tvb_current_offset+1 , tvb_RBRKT, '}');
		tvb_offset = tvb_skip_wsp_return(tvb, tvb_current_offset-1)-1;

		if ( (tvb_next_offset - tvb_current_offset ) > 3 ){
			tvb_next_offset = tvb_skip_wsp_return(tvb, tvb_next_offset-1);
			tvb_current_offset = tvb_skip_wsp(tvb, tvb_current_offset +1);
		}

		tempchar = tvb_get_guint8(tvb, tvb_offset);

		switch ( tempchar ){

		case 'R':
			/* Remote Descriptor in short message encoding */
			dissect_megaco_Remotedescriptor(tvb,megaco_mediadescriptor_tree, pinfo, tvb_next_offset, tvb_current_offset);
			break;

		case 'L':
			/* Local Descriptor in short message encoding */
			dissect_megaco_Localdescriptor(tvb,megaco_mediadescriptor_tree , pinfo, tvb_next_offset, tvb_current_offset);
			break;

		case 'O':
			/* Local Control Descriptor in short message encoding */
			dissect_megaco_LocalControldescriptor(tvb,megaco_mediadescriptor_tree, pinfo , tvb_next_offset, tvb_current_offset);
			break;

		case 'S':
			/* Termination State Descriptor in short message encoding */
			dissect_megaco_TerminationStatedescriptor(tvb,megaco_mediadescriptor_tree , tvb_next_offset, tvb_current_offset);
			break;

		case 'l':
			/* Local or Local Control Descriptor in long message encoding */
			if (tvb_get_guint8(tvb, tvb_offset-1) == 'a'){
				dissect_megaco_Localdescriptor(tvb,megaco_mediadescriptor_tree , pinfo, tvb_next_offset, tvb_current_offset);
			}
			else{
				dissect_megaco_LocalControldescriptor(tvb,megaco_mediadescriptor_tree, pinfo , tvb_next_offset, tvb_current_offset);
			}
			break;

		case 'e':
			/* Remote or Termination State Descriptor in long message encoding */

			if (tvb_get_guint8(tvb, tvb_offset-2) == 'a'){
				dissect_megaco_TerminationStatedescriptor(tvb,megaco_mediadescriptor_tree , tvb_next_offset, tvb_current_offset);
			}
			else {
				dissect_megaco_Remotedescriptor(tvb,megaco_mediadescriptor_tree , pinfo, tvb_next_offset, tvb_current_offset);
			}

			break;

		default:

			if ( tvb_find_guint8(tvb, tvb_help_offset, tvb_RBRKT, '{') > tvb_find_guint8(tvb, tvb_help_offset, tvb_RBRKT, '=')){

				tvb_help_offset = tvb_find_guint8(tvb, tvb_help_offset, tvb_RBRKT, '=');
				tvb_help_offset = tvb_skip_wsp(tvb, tvb_help_offset +1);

				tokenlen = tvb_offset - tvb_help_offset + 1;

				proto_tree_add_string(megaco_mediadescriptor_tree, hf_megaco_streamid, tvb,
					tvb_help_offset, tokenlen,
					tvb_format_text(tvb, tvb_help_offset,
					tokenlen));

			}
			else {
				tokenlen =  (tvb_RBRKT+1) - tvb_offset;
				proto_tree_add_string(megaco_mediadescriptor_tree, hf_megaco_error_Frame, tvb,
					tvb_offset, tokenlen,
					"No Descriptor detectable !");
			}
			break;

		}
	}
}

static void
dissect_megaco_h245(tvbuff_t *tvb, packet_info *pinfo, proto_tree *megaco_tree, gint offset, gint len, gchar *msg)
{
	proto_item *item;
	proto_tree *tree;
	guint8 buf[10240];

	item=proto_tree_add_string(megaco_tree, hf_megaco_h245, tvb,
		offset, len, msg );
	tree = proto_item_add_subtree(item, ett_megaco_h245);

	/* arbitrary maximum length */
	if(len<20480){
		int i;
		tvbuff_t *h245_tvb;

		/* first, skip to where the encoded pdu starts, this is
		   the first hex digit after the '=' char.
		*/
		while(1){
			if((*msg==0)||(*msg=='\n')){
				return;
			}
			if(*msg=='='){
				msg++;
				break;
			}
			msg++;
		}
		while(1){
			if((*msg==0)||(*msg=='\n')){
				return;
			}
			if( ((*msg>='0')&&(*msg<='9'))
			||  ((*msg>='a')&&(*msg<='f'))
			||  ((*msg>='A')&&(*msg<='F'))){
				break;
			}
			msg++;
		}
		i=0;
		while( ((*msg>='0')&&(*msg<='9'))
		     ||((*msg>='a')&&(*msg<='f'))
		     ||((*msg>='A')&&(*msg<='F'))  ){
			int val;
			if((*msg>='0')&&(*msg<='9')){
				val=(*msg)-'0';
			} else if((*msg>='a')&&(*msg<='f')){
				val=(*msg)-'a'+10;
			} else if((*msg>='A')&&(*msg<='F')){
				val=(*msg)-'A'+10;
			} else {
				return;
			}
			val<<=4;
			msg++;
			if((*msg>='0')&&(*msg<='9')){
				val|=(*msg)-'0';
			} else if((*msg>='a')&&(*msg<='f')){
				val|=(*msg)-'a'+10;
			} else if((*msg>='A')&&(*msg<='F')){
				val|=(*msg)-'A'+10;
			} else {
				return;
			}
			msg++;

			buf[i]=(guint8)val;
			i++;
		}
		if(i==0){
			return;
		}
		h245_tvb = tvb_new_real_data(buf,i,i);
		tvb_set_child_real_data_tvbuff(tvb,h245_tvb);
		add_new_data_source(pinfo, h245_tvb, "H.245 over MEGACO");
		/* should go through a handle, however,  the two h245 entry
		   points are different, one is over tpkt and the other is raw
		*/
		call_dissector(h245_handle, h245_tvb, pinfo, top_tree);
/*		dissect_h245_MultimediaSystemControlMessage(h245_tvb, pinfo, tree);*/
	}
}


static void
dissect_megaco_eventsdescriptor(tvbuff_t *tvb, packet_info *pinfo, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint tokenlen, tvb_current_offset, tvb_next_offset, tvb_help_offset;
	gint tvb_events_end_offset, tvb_events_start_offset, tvb_LBRKT;
	proto_tree  *megaco_eventsdescriptor_tree, *megaco_eventsdescriptor_ti;

	guint8 tempchar;
	gint requested_event_start_offset, requested_event_end_offset;
	proto_tree	*megaco_requestedevent_tree, *megaco_requestedevent_ti;

	tokenlen						= 0;
	tvb_current_offset				= 0;
	tvb_next_offset					= 0;
	tvb_help_offset					= 0;
	tvb_events_end_offset			= 0;
	tvb_events_start_offset			= 0;
	tvb_help_offset					= 0;
	requested_event_start_offset	= 0;
	requested_event_end_offset		= 0;

	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;

	megaco_eventsdescriptor_ti = proto_tree_add_item(megaco_tree_command_line,hf_megaco_events_descriptor,tvb,tvb_previous_offset,tokenlen, FALSE);
	megaco_eventsdescriptor_tree = proto_item_add_subtree(megaco_eventsdescriptor_ti, ett_megaco_eventsdescriptor);



	tvb_current_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '=');
	tvb_next_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '{');

	if ( tvb_current_offset < tvb_RBRKT && tvb_current_offset != -1 ){

		tvb_current_offset = tvb_skip_wsp(tvb, tvb_current_offset +1);
		tvb_help_offset = tvb_skip_wsp_return(tvb, tvb_next_offset-1);

		tokenlen =  tvb_help_offset - tvb_current_offset;

		proto_tree_add_string(megaco_eventsdescriptor_tree, hf_megaco_requestid, tvb,
			tvb_current_offset, tokenlen,
			tvb_format_text(tvb, tvb_current_offset,
			tokenlen));

		tvb_events_end_offset   = tvb_RBRKT;
		tvb_events_start_offset = tvb_previous_offset;

		tvb_RBRKT = tvb_next_offset+1;
		tvb_LBRKT = tvb_next_offset+1;
		tvb_previous_offset = tvb_skip_wsp(tvb, tvb_next_offset+1);


		do {

			tvb_RBRKT = tvb_find_guint8(tvb, tvb_RBRKT+1,
				tvb_events_end_offset, '}');
			tvb_LBRKT = tvb_find_guint8(tvb, tvb_LBRKT,
				tvb_events_end_offset, '{');

			tvb_current_offset 	= tvb_find_guint8(tvb, tvb_previous_offset,
				tvb_events_end_offset, ',');

			if (tvb_current_offset == -1 || tvb_current_offset > tvb_events_end_offset){
				tvb_current_offset = tvb_events_end_offset;
			}


			/* Descriptor includes no parameters */

			if ( tvb_LBRKT > tvb_current_offset || tvb_LBRKT == -1 ){

				tvb_RBRKT = tvb_skip_wsp_return(tvb, tvb_current_offset-1)-1;
			}

			/* Descriptor includes Parameters */

			if ( (tvb_current_offset > tvb_LBRKT && tvb_LBRKT != -1)){

				while ( tvb_LBRKT != -1 && tvb_RBRKT > tvb_LBRKT ){

					tvb_LBRKT  = tvb_find_guint8(tvb, tvb_LBRKT+1,
						tvb_events_end_offset, '{');
					if ( tvb_LBRKT < tvb_RBRKT && tvb_LBRKT != -1)
						tvb_RBRKT  = tvb_find_guint8(tvb, tvb_RBRKT+1,
						tvb_events_end_offset, '}');
				}

			}

			tvb_help_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_events_end_offset, '{');

			/* if there are eventparameter  */

			if ( tvb_help_offset < tvb_RBRKT && tvb_help_offset != -1 ){

				requested_event_start_offset = tvb_help_offset;
				requested_event_end_offset	 = tvb_RBRKT;
				tvb_help_offset = tvb_skip_wsp_return(tvb, tvb_help_offset-1);
				tokenlen = tvb_help_offset - tvb_previous_offset;
			}
			/* no parameters */
			else {
				tokenlen = tvb_RBRKT+1 - tvb_previous_offset;
			}

			megaco_requestedevent_ti = proto_tree_add_item(megaco_eventsdescriptor_tree,hf_megaco_pkgdname,tvb,tvb_previous_offset,tokenlen, FALSE);
			megaco_requestedevent_tree = proto_item_add_subtree(megaco_requestedevent_ti, ett_megaco_requestedevent);

			if ( tvb_help_offset < tvb_RBRKT && tvb_help_offset != -1 ){

				tvb_help_offset = tvb_skip_wsp(tvb, requested_event_start_offset +1);
				tempchar = tvb_get_guint8(tvb, tvb_help_offset);

				requested_event_start_offset = tvb_skip_wsp(tvb, requested_event_start_offset +1);
				requested_event_end_offset = tvb_skip_wsp_return(tvb, requested_event_end_offset-1);

				if ( tempchar == 'D' ){
					dissect_megaco_digitmapdescriptor(tvb, megaco_requestedevent_tree, requested_event_end_offset, requested_event_start_offset);
				}
				else{
					gchar *msg;

					tokenlen = 	requested_event_end_offset - requested_event_start_offset;
					msg=tvb_format_text(tvb,requested_event_start_offset, tokenlen);
					if(!strncmp("h245", msg, 4)){
						dissect_megaco_h245(tvb, pinfo, megaco_requestedevent_tree, requested_event_start_offset, tokenlen, msg);
					} else {
						proto_tree_add_text(megaco_requestedevent_tree, tvb, requested_event_start_offset, tokenlen,
							"%s", msg);
					}
				}

			}

			tvb_current_offset  = tvb_find_guint8(tvb, tvb_RBRKT,
				tvb_events_end_offset, ',');

			if (tvb_current_offset == -1 || tvb_current_offset > tvb_events_end_offset ){
				tvb_current_offset = tvb_events_end_offset;
			}

			tvb_previous_offset = tvb_skip_wsp(tvb, tvb_current_offset+1);

			tvb_LBRKT = tvb_previous_offset;
			tvb_RBRKT = tvb_previous_offset;

		} while ( tvb_current_offset < tvb_events_end_offset );
	}
}

static void
dissect_megaco_signaldescriptor(tvbuff_t *tvb, packet_info *pinfo, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint tokenlen, tvb_current_offset, tvb_next_offset, tvb_help_offset;
	gint tvb_signals_end_offset, tvb_signals_start_offset, tvb_LBRKT;
	proto_tree  *megaco_signalsdescriptor_tree, *megaco_signalsdescriptor_ti;

	gint requested_signal_start_offset, requested_signal_end_offset;
	proto_tree	*megaco_requestedsignal_tree, *megaco_requestedsignal_ti;

	tokenlen						= 0;
	tvb_current_offset				= 0;
	tvb_next_offset					= 0;
	tvb_help_offset					= 0;
	tvb_signals_end_offset			= 0;
	tvb_signals_start_offset		= 0;
	tvb_LBRKT						= 0;
	requested_signal_start_offset	= 0;
	requested_signal_end_offset		= 0;

	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;

	megaco_signalsdescriptor_ti = proto_tree_add_item(megaco_tree_command_line,hf_megaco_signal_descriptor,tvb,tvb_previous_offset,tokenlen, FALSE);
	megaco_signalsdescriptor_tree = proto_item_add_subtree(megaco_signalsdescriptor_ti, ett_megaco_signalsdescriptor);

	tvb_current_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '{');
	tvb_next_offset = tvb_skip_wsp(tvb, tvb_current_offset+1);
	if (check_col(pinfo->cinfo, COL_INFO) )
		col_append_fstr(pinfo->cinfo, COL_INFO, " (Signal:%s)",tvb_format_text(tvb, tvb_current_offset,tokenlen-tvb_current_offset+tvb_previous_offset));

	tvb_signals_end_offset   = tvb_RBRKT;
	tvb_signals_start_offset = tvb_previous_offset;

	if ( tvb_current_offset < tvb_RBRKT && tvb_current_offset != -1 && tvb_next_offset != tvb_signals_end_offset){


		tvb_RBRKT = tvb_next_offset+1;
		tvb_LBRKT = tvb_next_offset+1;
		tvb_previous_offset = tvb_next_offset;


		do {

			tvb_RBRKT = tvb_find_guint8(tvb, tvb_RBRKT+1,
				tvb_signals_end_offset, '}');
			tvb_LBRKT = tvb_find_guint8(tvb, tvb_LBRKT,
				tvb_signals_end_offset, '{');

			tvb_current_offset 	= tvb_find_guint8(tvb, tvb_previous_offset,
				tvb_signals_end_offset, ',');

			if (tvb_current_offset == -1 || tvb_current_offset > tvb_signals_end_offset){
				tvb_current_offset = tvb_signals_end_offset;
			}


			/* Descriptor includes no parameters */

			if ( tvb_LBRKT > tvb_current_offset || tvb_LBRKT == -1 ){

				tvb_RBRKT = tvb_skip_wsp_return(tvb, tvb_current_offset-1)-1;
			}

			/* Descriptor includes Parameters */

			if ( (tvb_current_offset > tvb_LBRKT && tvb_LBRKT != -1)){

				while ( tvb_LBRKT != -1 && tvb_RBRKT > tvb_LBRKT ){

					tvb_LBRKT  = tvb_find_guint8(tvb, tvb_LBRKT+1,
						tvb_signals_end_offset, '{');
					if ( tvb_LBRKT < tvb_RBRKT && tvb_LBRKT != -1)
						tvb_RBRKT  = tvb_find_guint8(tvb, tvb_RBRKT+1,
						tvb_signals_end_offset, '}');
				}

			}

			tvb_help_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_signals_end_offset, '{');

			/* if there are signalparameter  */

			if ( tvb_help_offset < tvb_RBRKT && tvb_help_offset != -1 ){

				requested_signal_start_offset = tvb_help_offset;
				requested_signal_end_offset	 = tvb_RBRKT;
				tvb_help_offset = tvb_skip_wsp_return(tvb, tvb_help_offset-1);
				tokenlen = tvb_help_offset - tvb_previous_offset;
			}
			/* no parameters */
			else {
				tokenlen = tvb_RBRKT+1 - tvb_previous_offset;
			}


			megaco_requestedsignal_ti = proto_tree_add_item(megaco_signalsdescriptor_tree,hf_megaco_pkgdname,tvb,tvb_previous_offset,tokenlen, FALSE);
			megaco_requestedsignal_tree = proto_item_add_subtree(megaco_requestedsignal_ti, ett_megaco_requestedsignal);

			if ( tvb_help_offset < tvb_RBRKT && tvb_help_offset != -1 ){
				gchar *msg;

				requested_signal_start_offset = tvb_skip_wsp(tvb, requested_signal_start_offset +1);
				requested_signal_end_offset = tvb_skip_wsp_return(tvb, requested_signal_end_offset-1);

				tokenlen = 	requested_signal_end_offset - requested_signal_start_offset;

				msg=tvb_format_text(tvb,requested_signal_start_offset, tokenlen+1);
				if(!strncmp("h245", msg, 4)){
					dissect_megaco_h245(tvb, pinfo, megaco_requestedsignal_tree, requested_signal_start_offset, tokenlen, msg);
				} else {
					proto_tree_add_text(megaco_requestedsignal_tree, tvb, requested_signal_start_offset, tokenlen,
						"%s", msg);
				}

			}

			tvb_current_offset  = tvb_find_guint8(tvb, tvb_RBRKT,
				tvb_signals_end_offset, ',');

			if (tvb_current_offset == -1 || tvb_current_offset > tvb_signals_end_offset || tvb_current_offset < tvb_previous_offset){
				tvb_current_offset = tvb_signals_end_offset;
			}

			tvb_previous_offset = tvb_skip_wsp(tvb, tvb_current_offset+1);

			tvb_LBRKT = tvb_previous_offset;
			tvb_RBRKT = tvb_previous_offset;

		} while ( tvb_current_offset < tvb_signals_end_offset );
	}


}
static void
dissect_megaco_auditdescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line, packet_info *pinfo,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint 	tokenlen;
	proto_tree  *megaco_auditdescriptor_tree, *megaco_auditdescriptor_ti;

	tokenlen = 0;

	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;

	megaco_auditdescriptor_ti = proto_tree_add_item(megaco_tree_command_line,hf_megaco_audit_descriptor,tvb,tvb_previous_offset,tokenlen, FALSE);
	megaco_auditdescriptor_tree = proto_item_add_subtree(megaco_auditdescriptor_ti, ett_megaco_auditdescriptor);


	tvb_previous_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '{');

	if ( tvb_skip_wsp(tvb, tvb_previous_offset +1) != tvb_RBRKT ){
		dissect_megaco_descriptors(tvb, megaco_auditdescriptor_tree, pinfo, tvb_previous_offset,tvb_RBRKT);
	}
}
static void
dissect_megaco_servicechangedescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint 	tokenlen;

	tokenlen = 0;

	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;
	proto_tree_add_string(megaco_tree_command_line, hf_megaco_servicechange_descriptor, tvb,
					 		tvb_previous_offset, tokenlen,
							tvb_format_text(tvb, tvb_previous_offset,
							tokenlen));

}
static void
dissect_megaco_digitmapdescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint 	tokenlen;

	tokenlen = 0;

	tokenlen =  tvb_RBRKT - tvb_previous_offset;
	proto_tree_add_string(megaco_tree_command_line, hf_megaco_digitmap_descriptor, tvb,
					 		tvb_previous_offset, tokenlen,
							tvb_format_text(tvb, tvb_previous_offset,
							tokenlen));

}
static void
dissect_megaco_statisticsdescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint 	tokenlen;

	tokenlen = 0;

	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;
	proto_tree_add_string(megaco_tree_command_line, hf_megaco_statistics_descriptor, tvb,
					 		tvb_previous_offset, tokenlen,
							tvb_format_text(tvb, tvb_previous_offset,
							tokenlen));

}
static void
dissect_megaco_observedeventsdescriptor(tvbuff_t *tvb, packet_info *pinfo, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint tokenlen, tvb_current_offset, tvb_next_offset, tvb_help_offset;
	gint tvb_observedevents_end_offset, tvb_observedevents_start_offset, tvb_LBRKT;
	proto_tree  *megaco_observedeventsdescriptor_tree, *megaco_observedeventsdescriptor_ti;

	guint8 tempchar;
	gint requested_event_start_offset, requested_event_end_offset, param_start_offset, param_end_offset;
	proto_tree	*megaco_observedevent_tree, *megaco_observedevent_ti;

	tokenlen						= 0;
	tvb_current_offset				= 0;
	tvb_next_offset					= 0;
	tvb_help_offset					= 0;
	tvb_observedevents_end_offset	= 0;
	tvb_observedevents_start_offset	= 0;
	tvb_LBRKT						= 0;
	requested_event_start_offset	= 0;
	requested_event_end_offset	= 0;



	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;

	megaco_observedeventsdescriptor_ti = proto_tree_add_item(megaco_tree_command_line,hf_megaco_observedevents_descriptor,tvb,tvb_previous_offset,tokenlen, FALSE);
	megaco_observedeventsdescriptor_tree = proto_item_add_subtree(megaco_observedeventsdescriptor_ti, ett_megaco_observedeventsdescriptor);



	tvb_current_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '=');
	tvb_next_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '{');

	if ( tvb_current_offset < tvb_RBRKT && tvb_current_offset != -1 ){

		tvb_current_offset = tvb_skip_wsp(tvb, tvb_current_offset +1);
		tvb_help_offset = tvb_skip_wsp_return(tvb, tvb_next_offset-1);

		tokenlen =  tvb_help_offset - tvb_current_offset;

		proto_tree_add_string(megaco_observedeventsdescriptor_tree, hf_megaco_requestid, tvb,
			tvb_current_offset, tokenlen,
			tvb_format_text(tvb, tvb_current_offset,
			tokenlen));

		tvb_observedevents_end_offset   = tvb_RBRKT;
		tvb_observedevents_start_offset = tvb_previous_offset;

		tvb_RBRKT = tvb_next_offset+1;
		tvb_LBRKT = tvb_next_offset+1;
		tvb_previous_offset = tvb_skip_wsp(tvb, tvb_next_offset+1);


		do {

			tvb_RBRKT = tvb_find_guint8(tvb, tvb_RBRKT+1,
				tvb_observedevents_end_offset, '}');
			tvb_LBRKT = tvb_find_guint8(tvb, tvb_LBRKT,
				tvb_observedevents_end_offset, '{');

			tvb_current_offset 	= tvb_find_guint8(tvb, tvb_previous_offset,
				tvb_observedevents_end_offset, ',');

			if (tvb_current_offset == -1 || tvb_current_offset > tvb_observedevents_end_offset){
				tvb_current_offset = tvb_observedevents_end_offset;
			}


			/* Descriptor includes no parameters */

			if ( tvb_LBRKT > tvb_current_offset || tvb_LBRKT == -1 ){

				tvb_RBRKT = tvb_skip_wsp_return(tvb, tvb_current_offset-1)-1;
			}

			/* Descriptor includes Parameters */

			if ( (tvb_current_offset > tvb_LBRKT && tvb_LBRKT != -1)){

				while ( tvb_LBRKT != -1 && tvb_RBRKT > tvb_LBRKT ){

					tvb_LBRKT  = tvb_find_guint8(tvb, tvb_LBRKT+1,
						tvb_observedevents_end_offset, '{');
					if ( tvb_LBRKT < tvb_RBRKT && tvb_LBRKT != -1){
						tvb_RBRKT  = tvb_find_guint8(tvb, tvb_RBRKT+1,
							tvb_observedevents_end_offset, '}');
					}
				}

			}

			tvb_help_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_observedevents_end_offset, '{');

			/* if there are eventparameter  */

			if ( tvb_help_offset < tvb_RBRKT && tvb_help_offset != -1 ){

				requested_event_start_offset = tvb_help_offset;
				requested_event_end_offset	 = tvb_RBRKT;
				tvb_help_offset = tvb_skip_wsp_return(tvb, tvb_help_offset-1);
				tokenlen = tvb_help_offset - tvb_previous_offset;
			}
			/* no parameters */
			else {
				tokenlen = tvb_RBRKT+1 - tvb_previous_offset;
			}

			megaco_observedevent_ti = proto_tree_add_item(megaco_observedeventsdescriptor_tree,hf_megaco_pkgdname,tvb,tvb_previous_offset,tokenlen, FALSE);
			megaco_observedevent_tree = proto_item_add_subtree(megaco_observedevent_ti, ett_megaco_observedevent);

			if ( tvb_help_offset < tvb_RBRKT && tvb_help_offset != -1 ){

				tvb_help_offset = tvb_skip_wsp(tvb, requested_event_start_offset +1);
				tempchar = tvb_get_guint8(tvb, tvb_help_offset);

				requested_event_start_offset = tvb_skip_wsp(tvb, requested_event_start_offset +1)-1;
				requested_event_end_offset = tvb_skip_wsp_return(tvb, requested_event_end_offset-1);

				tvb_help_offset = requested_event_start_offset;

				do {
					gchar *msg;

					param_start_offset = tvb_skip_wsp(tvb, tvb_help_offset+1);

					tvb_help_offset = tvb_find_guint8(tvb, tvb_help_offset+1,requested_event_end_offset, ',');

					if ( tvb_help_offset > requested_event_end_offset || tvb_help_offset == -1){
						tvb_help_offset = requested_event_end_offset;
					}

					param_end_offset = tvb_skip_wsp(tvb, tvb_help_offset-1);

					tokenlen = 	param_end_offset - param_start_offset+1;
					msg=tvb_format_text(tvb,param_start_offset, tokenlen);
					if(!strncmp("h245", msg, 4)){
						dissect_megaco_h245(tvb, pinfo, megaco_observedevent_tree, param_start_offset, tokenlen, msg);
					} else {
						proto_tree_add_text(megaco_observedevent_tree, tvb, param_start_offset, tokenlen,
							"%s", msg);
					}


				} while ( tvb_help_offset < requested_event_end_offset );
			}

			tvb_previous_offset = tvb_current_offset;
			tvb_current_offset  = tvb_find_guint8(tvb, tvb_RBRKT,
				tvb_observedevents_end_offset, ',');

			if (tvb_current_offset == -1 || tvb_current_offset > tvb_observedevents_end_offset ){
				tvb_current_offset = tvb_observedevents_end_offset;
			}
			if (tvb_current_offset <= tvb_previous_offset) {
				proto_tree_add_text(megaco_observedevent_tree, tvb, 0, 0, "[ Parse error: Invalid offset ]");
				return;
			}

			tvb_previous_offset = tvb_skip_wsp(tvb, tvb_current_offset+1);

			tvb_LBRKT = tvb_previous_offset;
			tvb_RBRKT = tvb_previous_offset;

		} while ( tvb_current_offset < tvb_observedevents_end_offset );
	}
}
static void
dissect_megaco_topologydescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint 	tokenlen;

	tokenlen = 0;

	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;
	proto_tree_add_string(megaco_tree_command_line, hf_megaco_topology_descriptor, tvb,
					 		tvb_previous_offset, tokenlen,
							tvb_format_text(tvb, tvb_previous_offset,
							tokenlen));

}
static void
dissect_megaco_Packagesdescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint tokenlen, tvb_current_offset, tvb_next_offset, tvb_help_offset;
	gint tvb_packages_end_offset, tvb_packages_start_offset, tvb_LBRKT;
	proto_tree  *megaco_packagesdescriptor_tree, *megaco_packagesdescriptor_ti;

	tokenlen					= 0;
	tvb_current_offset			= 0;
	tvb_next_offset				= 0;
	tvb_help_offset				= 0;
	tvb_packages_end_offset		= 0;
	tvb_packages_start_offset	= 0;
	tvb_LBRKT					= 0;

	tokenlen =  (tvb_RBRKT+1) - tvb_previous_offset;

	megaco_packagesdescriptor_ti = proto_tree_add_item(megaco_tree_command_line,hf_megaco_packages_descriptor,tvb,tvb_previous_offset,tokenlen, FALSE);
	megaco_packagesdescriptor_tree = proto_item_add_subtree(megaco_packagesdescriptor_ti, ett_megaco_packagesdescriptor);



	tvb_current_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '=');
	tvb_next_offset = tvb_find_guint8(tvb, tvb_previous_offset, tvb_RBRKT, '{');

	if ( tvb_current_offset < tvb_RBRKT && tvb_current_offset != -1 ){

		tvb_current_offset = tvb_skip_wsp(tvb, tvb_current_offset +1);
		tvb_help_offset = tvb_skip_wsp_return(tvb, tvb_next_offset-1);

		tokenlen =  tvb_help_offset - tvb_current_offset;

		proto_tree_add_string(megaco_packagesdescriptor_tree, hf_megaco_requestid, tvb,
			tvb_current_offset, tokenlen,
			tvb_format_text(tvb, tvb_current_offset,
			tokenlen));

		tvb_packages_end_offset   = tvb_RBRKT;
		tvb_packages_start_offset = tvb_previous_offset;

		tvb_RBRKT = tvb_next_offset+1;
		tvb_LBRKT = tvb_next_offset+1;
		tvb_previous_offset = tvb_skip_wsp(tvb, tvb_next_offset+1);


		do {

			tvb_RBRKT = tvb_find_guint8(tvb, tvb_RBRKT+1,
				tvb_packages_end_offset, '}');
			tvb_LBRKT = tvb_find_guint8(tvb, tvb_LBRKT,
				tvb_packages_end_offset, '{');

			tvb_current_offset 	= tvb_find_guint8(tvb, tvb_previous_offset,
				tvb_packages_end_offset, ',');

			if (tvb_current_offset == -1 || tvb_current_offset > tvb_packages_end_offset){
				tvb_current_offset = tvb_packages_end_offset;
			}


			/* Descriptor includes no parameters */

			if ( tvb_LBRKT > tvb_current_offset || tvb_LBRKT == -1 ){

				tvb_RBRKT = tvb_skip_wsp_return(tvb, tvb_current_offset-1)-1;
			}

			/* Descriptor includes Parameters */

			if ( (tvb_current_offset > tvb_LBRKT && tvb_LBRKT != -1)){

				while ( tvb_LBRKT != -1 && tvb_RBRKT > tvb_LBRKT ){

					tvb_LBRKT  = tvb_find_guint8(tvb, tvb_LBRKT+1,
						tvb_packages_end_offset, '{');
					if ( tvb_LBRKT < tvb_RBRKT && tvb_LBRKT != -1)
						tvb_RBRKT  = tvb_find_guint8(tvb, tvb_RBRKT+1,
						tvb_packages_end_offset, '}');
				}

			}

			tokenlen = tvb_RBRKT+1 - tvb_previous_offset;

			proto_tree_add_text(megaco_packagesdescriptor_tree, tvb, tvb_previous_offset, tokenlen,
				"%s", tvb_format_text(tvb,tvb_previous_offset,
				tokenlen));


			tvb_current_offset  	= tvb_find_guint8(tvb, tvb_RBRKT,
				tvb_packages_end_offset, ',');

			if (tvb_current_offset == -1 || tvb_current_offset > tvb_packages_end_offset ){
				tvb_current_offset = tvb_packages_end_offset;
			}

			tvb_previous_offset = tvb_skip_wsp(tvb, tvb_current_offset+1);

			tvb_LBRKT = tvb_previous_offset;
			tvb_RBRKT = tvb_previous_offset;

		} while ( tvb_current_offset < tvb_packages_end_offset );
	}

}
/* The list of error code values is fetched from http://www.iana.org/assignments/megaco-h248 	*/
/* 2003-08-28											*/

static const value_string MEGACO_error_code_vals[] = {

	{400, "Syntax error in message"},
	{401, "Protocol Error"},
	{402, "Unauthorized"},
	{403, "Syntax error in transaction request"},
	{406, "Version Not Supported"},
	{410, "Incorrect identifier"},
	{411, "The transaction refers to an unknown ContextId"},
	{412, "No ContextIDs available"},
	{421, "Unknown action or illegal combination of actions"},
	{422, "Syntax Error in Action"},
	{430, "Unknown TerminationID"},
	{431, "No TerminationID matched a wildcard"},
	{432, "Out of TerminationIDs or No TerminationID available"},
	{433, "TerminationID is already in a Context"},
	{434, "Max number of Terminations in a Context exceeded"},
	{435, "Termination ID is not in specified Context"},
	{440, "Unsupported or unknown Package"},
	{441, "Missing Remote or Local Descriptor"},
	{442, "Syntax Error in Command"},
	{443, "Unsupported or Unknown Command"},
	{444, "Unsupported or Unknown Descriptor"},
	{445, "Unsupported or Unknown Property"},
	{446, "Unsupported or Unknown Parameter"},
	{447, "Descriptor not legal in this command"},
	{448, "Descriptor appears twice in a command"},
	{450, "No such property in this package"},
	{451, "No such event in this package"},
	{452, "No such signal in this package"},
	{453, "No such statistic in this package"},
	{454, "No such parameter value in this package"},
	{455, "Property illegal in this Descriptor"},
	{456, "Property appears twice in this Descriptor"},
	{457, "Missing parameter in signal or event"},
	{458, "Unexpected Event/Request ID"},
	{459, "Unsupported or Unknown Profile"},
	{471, "Implied Add for Multiplex failure"},

	{500, "Internal software Failure in MG"},
	{501, "Not Implemented"},
	{502, "Not ready."},
	{503, "Service Unavailable"},
	{504, "Command Received from unauthorized entity"},
	{505, "Transaction Request Received before a Service Change Reply has been received"},
	{506, "Number of Transaction Pendings Exceeded"},
	{510, "Insufficient resources"},
	{512, "Media Gateway unequipped to detect requested Event"},
	{513, "Media Gateway unequipped to generate requested Signals"},
	{514, "Media Gateway cannot send the specified announcement"},
	{515, "Unsupported Media Type"},
	{517, "Unsupported or invalid mode"},
	{518, "Event buffer full"},
	{519, "Out of space to store digit map"},
	{520, "Digit Map undefined in the MG"},
	{521, "Termination is ServiceChangeing"},
	{526, "Insufficient bandwidth"},
	{529, "Internal hardware failure in MG"},
	{530, "Temporary Network failure"},
	{531, "Permanent Network failure"},
	{532, "Audited Property, Statistic, Event or Signal does not exist"},
	{533, "Response exceeds maximum transport PDU size"},
	{534, "Illegal write or read only property"},
	{540, "Unexpected initial hook state"},
	{581, "Does Not Exist"},

	{600, "Illegal syntax within an announcement specification"},
	{601, "Variable type not supported"},
	{602, "Variable value out of range"},
	{603, "Category not supported"},
	{604, "Selector type not supported"},
	{605, "Selector value not supported"},
	{606, "Unknown segment ID"},
	{607, "Mismatch between play specification and provisioned data"},
	{608, "Provisioning error"},
	{609, "Invalid offset"},
	{610, "No free segment IDs"},
	{611, "Temporary segment not found"},
	{612, "Segment in use"},
	{613, "ISP port limit overrun"},
	{614, "No modems available"},
	{615, "Calling number unacceptable"},
	{616, "Called number unacceptable"},
	{  0, NULL }
};



static void
dissect_megaco_errordescriptor(tvbuff_t *tvb, proto_tree *megaco_tree_command_line,  gint tvb_RBRKT, gint tvb_previous_offset)
{

	gint 		tokenlen;
	gint		error_code;
	guint8	error[4];
	gint 		tvb_next_offset, tvb_current_offset,tvb_len;

	tvb_len			= tvb_length(tvb);
	tokenlen		= 0;
	tvb_next_offset		= 0;
	tvb_current_offset	= 0;
	tvb_len			= 0;


	tvb_current_offset = tvb_find_guint8(tvb, tvb_previous_offset , tvb_RBRKT, '=');
	tvb_current_offset = tvb_skip_wsp(tvb, tvb_current_offset +1);
	tvb_get_nstringz0(tvb,tvb_current_offset,4,error);
	error_code = atoi(error);
	proto_tree_add_string_hidden(megaco_tree_command_line, hf_megaco_error_descriptor, tvb,
					 		tvb_current_offset, 3,
							tvb_format_text(tvb, tvb_current_offset,
							3));

	tokenlen =  (tvb_RBRKT) - tvb_previous_offset+1;


	proto_tree_add_string(megaco_tree_command_line, hf_megaco_error_descriptor, tvb,
					 		tvb_previous_offset, tokenlen,
							tvb_format_text(tvb, tvb_previous_offset,
							tokenlen));

	proto_tree_add_text(megaco_tree_command_line, tvb, tvb_current_offset, 3,
	    "Error code: %s",
	    val_to_str(error_code, MEGACO_error_code_vals,
	      "Unknown (%u)"));

}
static void
dissect_megaco_TerminationStatedescriptor(tvbuff_t *tvb, proto_tree *megaco_mediadescriptor_tree,  gint tvb_next_offset, gint tvb_current_offset)
{
	gint tokenlen;
	gint tvb_offset, tvb_help_offset;
	guint8 tempchar;

	proto_tree  *megaco_TerminationState_tree, *megaco_TerminationState_ti;

	tokenlen		= 0;
	tvb_offset		= 0;
	tvb_help_offset = 0;

	tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_next_offset, '=');

	tokenlen = tvb_next_offset - tvb_current_offset;

	megaco_TerminationState_ti = proto_tree_add_item(megaco_mediadescriptor_tree,hf_megaco_TerminationState_descriptor,tvb,tvb_current_offset,tokenlen, FALSE);
	megaco_TerminationState_tree = proto_item_add_subtree(megaco_TerminationState_ti, ett_megaco_TerminationState);

	while ( tvb_offset < tvb_next_offset && tvb_offset != -1 ){

		tempchar = tvb_get_guint8(tvb, tvb_current_offset);
		tvb_help_offset = tvb_current_offset;

		tvb_current_offset = tvb_skip_wsp(tvb, tvb_offset +1);

		switch ( tempchar ){

		case 'S':
			tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_offset, ',');
			if ( tvb_offset == -1 || tvb_offset > tvb_next_offset ){
				tvb_offset = tvb_next_offset;
			}

			tempchar = tvb_get_guint8(tvb, tvb_current_offset);
			tokenlen = tvb_offset - tvb_current_offset;

			proto_tree_add_string(megaco_TerminationState_tree, hf_megaco_Service_State, tvb,
				tvb_current_offset, tokenlen,
				tvb_format_text(tvb, tvb_current_offset,
				tokenlen));

			break;

		case 'B':

			tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_offset, ',');
			if ( tvb_offset == -1 || tvb_offset > tvb_next_offset ){
				tvb_offset = tvb_next_offset;
			}

			tempchar = tvb_get_guint8(tvb, tvb_current_offset);
			tokenlen = tvb_offset - tvb_current_offset;

			proto_tree_add_string(megaco_TerminationState_tree, hf_megaco_Event_Buffer_Control, tvb,
				tvb_current_offset, tokenlen,
				tvb_format_text(tvb, tvb_current_offset,
				tokenlen));

			break;

		case 'E':
			tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_offset, ',');
			if ( tvb_offset == -1 || tvb_offset > tvb_next_offset ){
				tvb_offset = tvb_next_offset;
			}

			tempchar = tvb_get_guint8(tvb, tvb_current_offset);
			tokenlen = tvb_offset - tvb_current_offset;

			proto_tree_add_string(megaco_TerminationState_tree, hf_megaco_Event_Buffer_Control, tvb,
				tvb_current_offset, tokenlen,
				tvb_format_text(tvb, tvb_current_offset,
				tokenlen));

			break;

		default:
			tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_offset, ',');
			if ( tvb_offset == -1 || tvb_offset > tvb_next_offset ){
				tvb_offset = tvb_next_offset;
			}

			tempchar = tvb_get_guint8(tvb, tvb_help_offset);
			tokenlen = tvb_offset - tvb_help_offset;

			proto_tree_add_text(megaco_TerminationState_tree, tvb, tvb_help_offset, tokenlen,
				"%s", tvb_format_text(tvb,tvb_help_offset,
				tokenlen));
			break;
		}


		tvb_current_offset = tvb_skip_wsp(tvb, tvb_offset +1);
		tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_next_offset, '=');
	}
}

static void
dissect_megaco_Localdescriptor(tvbuff_t *tvb, proto_tree *megaco_mediadescriptor_tree,packet_info *pinfo, gint tvb_next_offset, gint tvb_current_offset)
{
	gint tokenlen;
	tvbuff_t *next_tvb;

	proto_tree  *megaco_localdescriptor_tree, *megaco_localdescriptor_ti;

	tokenlen = 0;

	tokenlen = tvb_next_offset - tvb_current_offset;



	megaco_localdescriptor_ti = proto_tree_add_item(megaco_mediadescriptor_tree,hf_megaco_Local_descriptor,tvb,tvb_current_offset,tokenlen, FALSE);
	megaco_localdescriptor_tree = proto_item_add_subtree(megaco_localdescriptor_ti, ett_megaco_Localdescriptor);

	tokenlen = tvb_next_offset - tvb_current_offset;
	if ( tokenlen > 3 ){
		next_tvb = tvb_new_subset(tvb, tvb_current_offset, tokenlen, tokenlen);
		call_dissector(sdp_handle, next_tvb, pinfo, megaco_localdescriptor_tree);
	}
}
static void
dissect_megaco_Remotedescriptor(tvbuff_t *tvb, proto_tree *megaco_mediadescriptor_tree,packet_info *pinfo, gint tvb_next_offset, gint tvb_current_offset)
{
	gint tokenlen;
	tvbuff_t *next_tvb;


	proto_tree  *megaco_Remotedescriptor_tree, *megaco_Remotedescriptor_ti;

	tokenlen = 0;

	tokenlen = tvb_next_offset - tvb_current_offset;

	megaco_Remotedescriptor_ti = proto_tree_add_item(megaco_mediadescriptor_tree,hf_megaco_Remote_descriptor,tvb,tvb_current_offset,tokenlen, FALSE);
	megaco_Remotedescriptor_tree = proto_item_add_subtree(megaco_Remotedescriptor_ti, ett_megaco_Remotedescriptor);

	if ( tokenlen > 3 ){
		next_tvb = tvb_new_subset(tvb, tvb_current_offset, tokenlen, tokenlen);
		call_dissector(sdp_handle, next_tvb, pinfo, megaco_Remotedescriptor_tree);
	}
}
static void
dissect_megaco_LocalControldescriptor(tvbuff_t *tvb, proto_tree *megaco_mediadescriptor_tree, packet_info *pinfo,  gint tvb_next_offset, gint tvb_current_offset)
{
	gint tokenlen;
	gint tvb_offset,tvb_help_offset;
	guint8 tempchar;


	proto_tree  *megaco_LocalControl_tree, *megaco_LocalControl_ti;

	tokenlen		= 0;
	tvb_offset		= 0;
	tvb_help_offset = 0;

	tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_next_offset, '=');

	tokenlen = tvb_next_offset - tvb_current_offset;

	megaco_LocalControl_ti = proto_tree_add_item(megaco_mediadescriptor_tree,hf_megaco_LocalControl_descriptor,tvb,tvb_current_offset,tokenlen, FALSE);
	megaco_LocalControl_tree = proto_item_add_subtree(megaco_LocalControl_ti, ett_megaco_LocalControldescriptor);

	while ( tvb_offset < tvb_next_offset && tvb_offset != -1 ){

		tempchar = tvb_get_guint8(tvb, tvb_current_offset);
		tvb_help_offset	= tvb_current_offset;
		tvb_current_offset = tvb_skip_wsp(tvb, tvb_offset +1);


		switch ( tempchar ){

		case 'M':
			tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_offset, ',');
			if ( tvb_offset == -1 || tvb_offset > tvb_next_offset ){
				tvb_offset = tvb_next_offset;
			}

			tokenlen = tvb_offset - tvb_current_offset;

			proto_tree_add_string(megaco_LocalControl_tree, hf_megaco_mode, tvb,
				tvb_current_offset, tokenlen,
				tvb_format_text(tvb, tvb_current_offset,
				tokenlen));
			if (check_col(pinfo->cinfo, COL_INFO) )
				col_append_fstr(pinfo->cinfo, COL_INFO, " (Mode:%s)",tvb_format_text(tvb, tvb_current_offset,tokenlen));
			tvb_current_offset = tvb_skip_wsp(tvb, tvb_offset +1);
			break;

		case 'R':
			if ( tvb_get_guint8(tvb, tvb_help_offset+1) == 'V' || tvb_get_guint8(tvb, tvb_help_offset+8)  == 'V'){

				tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_offset, ',');
				if ( tvb_offset == -1 || tvb_offset > tvb_next_offset ){
					tvb_offset = tvb_next_offset;
				}

				tokenlen = tvb_offset - tvb_current_offset;

				proto_tree_add_string(megaco_LocalControl_tree, hf_megaco_reserve_value, tvb,
					tvb_current_offset, tokenlen,
					tvb_format_text(tvb, tvb_current_offset,
					tokenlen));

				tvb_current_offset = tvb_skip_wsp(tvb, tvb_offset +1);
			}
			else {
				tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_offset, ',');
				if ( tvb_offset == -1 || tvb_offset > tvb_next_offset ){
					tvb_offset = tvb_next_offset;
				}

				tokenlen = tvb_offset - tvb_current_offset;

				proto_tree_add_string(megaco_LocalControl_tree, hf_megaco_reserve_group, tvb,
					tvb_current_offset, tokenlen,
					tvb_format_text(tvb, tvb_current_offset,
					tokenlen));
				tvb_current_offset = tvb_skip_wsp(tvb, tvb_offset +1);
			}
			break;

		default:
			tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_offset, ',');
			if ( tvb_offset == -1 || tvb_offset > tvb_next_offset ){
				tvb_offset = tvb_next_offset;
			}

			tokenlen = tvb_offset - tvb_help_offset;

			proto_tree_add_text(megaco_LocalControl_tree, tvb, tvb_help_offset, tokenlen,
				"%s", tvb_format_text(tvb,tvb_help_offset,
				tokenlen));
			tvb_current_offset = tvb_skip_wsp(tvb, tvb_offset +1);

			break;
		}
		tvb_offset = tvb_find_guint8(tvb, tvb_current_offset , tvb_next_offset, '=');
	}
}
/* Copied from MGCP dissector, prints whole message in raw text */

static void tvb_raw_text_add(tvbuff_t *tvb, proto_tree *tree){

  gint tvb_linebegin,tvb_lineend,tvb_len,linelen;

  tvb_linebegin = 0;
  tvb_len = tvb_length(tvb);

  proto_tree_add_text(tree, tvb, 0, -1,"-------------- (RAW text output) ---------------");

  do {
    linelen = tvb_find_line_end(tvb,tvb_linebegin,-1,&tvb_lineend,FALSE);
    proto_tree_add_text(tree, tvb, tvb_linebegin, linelen,
			"%s", tvb_format_text(tvb,tvb_linebegin,
					      linelen));
    tvb_linebegin = tvb_lineend;
  } while ( tvb_lineend < tvb_len );
}

/* Register all the bits needed with the filtering engine */

void
proto_register_megaco(void)
{
	static hf_register_info hf[] = {
		{ &hf_megaco_audit_descriptor,
		{ "Audit Descriptor", "megaco.audit", FT_STRING, BASE_DEC, NULL, 0x0,
		"Audit Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_command_line,
		{ "Command line", "megaco.command_line", FT_STRING, BASE_DEC, NULL, 0x0,
		"Commands of this message ", HFILL }},
		{ &hf_megaco_command,
		{ "Command", "megaco.command", FT_STRING, BASE_DEC, NULL, 0x0,
		"Command of this message ", HFILL }},
		{ &hf_megaco_Context,
		{ "Context", "megaco.context", FT_STRING, BASE_DEC, NULL, 0x0,
		"Context ID of this massage ", HFILL }},
		{ &hf_megaco_digitmap_descriptor,
		{ "DigitMap Descriptor", "megaco.digitmap", FT_STRING, BASE_DEC, NULL, 0x0,
		"DigitMap Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_error_descriptor,
		{ "ERROR Descriptor", "megaco.error", FT_STRING, BASE_DEC, NULL, 0x0,
		"Error Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_error_Frame,
		{ "ERROR frame", "megaco.error_frame", FT_STRING, BASE_DEC, NULL, 0x0,
		"Syntax error ", HFILL }},
		{ &hf_megaco_Event_Buffer_Control,
		{ "Event Buffer Control", "megaco.eventbuffercontrol", FT_STRING, BASE_DEC, NULL, 0x0,
		"Event Buffer Control in Termination State Descriptor", HFILL }},
		{ &hf_megaco_events_descriptor,
		{ "Events Descriptor", "megaco.events", FT_STRING, BASE_DEC, NULL, 0x0,
		"Events Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_Local_descriptor,
		{ "Local Descriptor", "megaco.localdescriptor", FT_STRING, BASE_DEC, NULL, 0x0,
		"Local Descriptor in Media Descriptor ", HFILL }},
		{ &hf_megaco_LocalControl_descriptor,
		{ "Local Control Descriptor", "megaco.localcontroldescriptor", FT_STRING, BASE_DEC, NULL, 0x0,
		"Local Control Descriptor in Media Descriptor ", HFILL }},
		{ &hf_megaco_media_descriptor,
		{ "Media Descriptor", "megaco.media", FT_STRING, BASE_DEC, NULL, 0x0,
		"Media Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_modem_descriptor,
		{ "Modem Descriptor", "megaco.modem", FT_STRING, BASE_DEC, NULL, 0x0,
		"Modem Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_mode,
		{ "Mode", "megaco.mode", FT_STRING, BASE_DEC, NULL, 0x0,
		"Mode  sendonly/receiveonly/inactive/loopback", HFILL }},
		{ &hf_megaco_multiplex_descriptor,
		{ "Multiplex Descriptor", "megaco.multiplex", FT_STRING, BASE_DEC, NULL, 0x0,
		"Multiplex Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_observedevents_descriptor,
		{ "Observed Events Descriptor", "megaco.observedevents", FT_STRING, BASE_DEC, NULL, 0x0,
		"Observed Events Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_packages_descriptor,
		{ "Packages Descriptor", "megaco.packagesdescriptor", FT_STRING, BASE_DEC, NULL, 0x0,
		"Packages Descriptor", HFILL }},
		{ &hf_megaco_pkgdname,
		{ "pkgdName", "megaco.pkgdname", FT_STRING, BASE_DEC, NULL, 0x0,
		"PackageName SLASH ItemID", HFILL }},
		{ &hf_megaco_Remote_descriptor,
		{ "Remote Descriptor", "megaco.remotedescriptor", FT_STRING, BASE_DEC, NULL, 0x0,
		"Remote Descriptor in Media Descriptor ", HFILL }},
		{ &hf_megaco_reserve_group,
		{ "Reserve Group", "megaco.reservegroup", FT_STRING, BASE_DEC, NULL, 0x0,
		"Reserve Group on or off", HFILL }},
		{ &hf_megaco_reserve_value,
		{ "Reserve Value", "megaco.reservevalue", FT_STRING, BASE_DEC, NULL, 0x0,
		"Reserve Value on or off", HFILL }},
		{ &hf_megaco_requestid,
		{ "RequestID", "megaco.requestid", FT_STRING, BASE_DEC, NULL, 0x0,
		"RequestID in Events or Observedevents Descriptor ", HFILL }},
		{ &hf_megaco_servicechange_descriptor,
		{ "Service Change Descriptor", "megaco.servicechange", FT_STRING, BASE_DEC, NULL, 0x0,
		"Service Change Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_Service_State,
		{ "Service State", "megaco.servicestates", FT_STRING, BASE_DEC, NULL, 0x0,
		"Service States in Termination State Descriptor", HFILL }},
		{ &hf_megaco_signal_descriptor,
		{ "Signal Descriptor", "megaco.signal", FT_STRING, BASE_DEC, NULL, 0x0,
		"Signal Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_statistics_descriptor,
		{ "Statistics Descriptor", "megaco.statistics", FT_STRING, BASE_DEC, NULL, 0x0,
		"Statistics Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_streamid,
		{ "StreamID", "megaco.streamid", FT_STRING, BASE_DEC, NULL, 0x0,
		"StreamID in the Media Descriptor ", HFILL }},
		{ &hf_megaco_termid,
		{ "Termination ID", "megaco.termid", FT_STRING, BASE_DEC, NULL, 0x0,
		"Termination ID of this Command ", HFILL }},
		{ &hf_megaco_TerminationState_descriptor,
		{ "Termination State Descriptor", "megaco.terminationstate", FT_STRING, BASE_DEC, NULL, 0x0,
		"Termination State Descriptor in Media Descriptor ", HFILL }},
		{ &hf_megaco_topology_descriptor,
		{ "Topology Descriptor", "megaco.topology", FT_STRING, BASE_DEC, NULL, 0x0,
		"Topology Descriptor of the megaco Command ", HFILL }},
		{ &hf_megaco_transaction,
		{ "Transaction", "megaco.transaction", FT_STRING, BASE_DEC, NULL, 0x0,
		"Message Originator", HFILL }},
		{ &hf_megaco_transid,
		{ "Transaction ID", "megaco.transid", FT_STRING, BASE_DEC, NULL, 0x0,
		"Transaction ID of this message", HFILL }},
		{ &hf_megaco_mId,
		{ "MediagatewayID", "megaco.mId", FT_STRING, BASE_DEC, NULL, 0x0,
		"Mediagateway ID", HFILL }},
		{ &hf_megaco_version,
		{ "Version", "megaco.version", FT_STRING, BASE_DEC, NULL, 0x0,
		"Version", HFILL }},
		{ &hf_megaco_h245,
		{ "h245", "megaco.h245", FT_STRING, BASE_DEC, NULL, 0x0,
		"Embedded H.245 message", HFILL }},

		/* Add more fields here */
	};
	static gint *ett[] = {
		&ett_megaco,
			&ett_megaco_command_line,
			&ett_megaco_descriptors,
			&ett_megaco_mediadescriptor,
			&ett_megaco_TerminationState,
			&ett_megaco_Remotedescriptor,
			&ett_megaco_Localdescriptor,
			&ett_megaco_LocalControldescriptor,
			&ett_megaco_auditdescriptor,
			&ett_megaco_eventsdescriptor,
			&ett_megaco_observedeventsdescriptor,
			&ett_megaco_observedevent,
			&ett_megaco_packagesdescriptor,
			&ett_megaco_requestedevent,
			&ett_megaco_signalsdescriptor,
			&ett_megaco_requestedsignal,
			&ett_megaco_h245,
	};
	module_t *megaco_module;

	proto_megaco = proto_register_protocol("MEGACO",
					   "MEGACO", "megaco");

	register_dissector("megaco", dissect_megaco_text, proto_megaco);

	proto_register_field_array(proto_megaco, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

	/* Register our configuration options, particularly our ports */

	megaco_module = prefs_register_protocol(proto_megaco, proto_reg_handoff_megaco);

	prefs_register_uint_preference(megaco_module, "tcp.txt_port",
		"MEGACO Text TCP Port",
		"Set the TCP port for MEGACO text messages",
		10, &global_megaco_txt_tcp_port);

	prefs_register_uint_preference(megaco_module, "udp.txt_port",
		"MEGACO Text UDP Port",
		"Set the UDP port for MEGACO text messages",
		10, &global_megaco_txt_udp_port);

#if 0
	prefs_register_uint_preference(megaco_module, "tcp.bin_port",
		"MEGACO Binary TCP Port",
		"Set the TCP port for MEGACO binary messages",
		10, &global_megaco_bin_tcp_port);

	prefs_register_uint_preference(megaco_module, "udp.bin_port",
		"MEGACO Binary UDP Port",
		"Set the UDP port for MEGACO binary messages",
		10, &global_megaco_bin_udp_port);
#endif

	prefs_register_bool_preference(megaco_module, "display_raw_text",
		"Display raw text for MEGACO message",
		"Specifies that the raw text of the "
		"MEGACO message should be displayed "
		"instead of (or in addition to) the "
		"dissection tree",
		&global_megaco_raw_text);

	prefs_register_bool_preference(megaco_module, "display_dissect_tree",
		"Display tree dissection for MEGACO message",
		"Specifies that the dissection tree of the "
		"MEGACO message should be displayed "
		"instead of (or in addition to) the "
		"raw text",
		&global_megaco_dissect_tree);
}




/* The registration hand-off routine */
void
proto_reg_handoff_megaco(void)
{
	static int megaco_prefs_initialized = FALSE;
	static dissector_handle_t megaco_text_tcp_handle;

	sdp_handle = find_dissector("sdp");
	h245_handle = find_dissector("h245dg");

	if (!megaco_prefs_initialized) {
		megaco_text_handle = create_dissector_handle(dissect_megaco_text,
			proto_megaco);
		megaco_text_tcp_handle = create_dissector_handle(dissect_megaco_text_tcp,
			proto_megaco);

		megaco_prefs_initialized = TRUE;
	}
	else {
		dissector_delete("tcp.port", txt_tcp_port, megaco_text_tcp_handle);
		dissector_delete("udp.port", txt_udp_port, megaco_text_handle);
#if 0
		dissector_delete("tcp.port", bin_tcp_port, megaco_text_tcp_handle);
		dissector_delete("udp.port", bin_udp_port, megaco_bin_handle);
#endif
	}

	/* Set our port number for future use */

	txt_tcp_port = global_megaco_txt_tcp_port;
	txt_udp_port = global_megaco_txt_udp_port;

#if 0
	bin_tcp_port = global_megaco_bin_tcp_port;
	bin_udp_port = global_megaco_bin_udp_port;
#endif

	dissector_add("tcp.port", global_megaco_txt_tcp_port, megaco_text_tcp_handle);
	dissector_add("udp.port", global_megaco_txt_udp_port, megaco_text_handle);
#if 0
	dissector_add("tcp.port", global_megaco_bin_tcp_port, megaco_bin_handle);
	dissector_add("udp.port", global_megaco_bin_udp_port, megaco_bin_handle);
#endif
	/* XXX - text or binary?  Does that depend on the port number? */
	dissector_add("sctp.ppi", H248_PAYLOAD_PROTOCOL_ID,   megaco_text_handle);

}

/*
* tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
*		 character following offset or offset + maxlength -1 whichever
*		 is smaller.
*
* Parameters:
* tvb - The tvbuff in which we are skipping whitespaces, tab and end_of_line characters.
* offset - The offset in tvb from which we begin trying to skip whitespace.
*
* Returns: The position in tvb of the first non-whitespace
*/
static gint tvb_skip_wsp(tvbuff_t* tvb, gint offset ){
	gint counter = offset;
	gint end,tvb_len;
	guint8 tempchar;
	tvb_len = tvb_length(tvb);
	end = tvb_len;

	for(counter = offset; counter < end &&
		((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
		tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter++);
	return (counter);
}
static gint tvb_skip_wsp_return(tvbuff_t* tvb, gint offset){
	gint counter = offset;
	gint end;
	guint8 tempchar;
	end = 0;

	for(counter = offset; counter > end &&
		((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
		tempchar == '\t' || tempchar == '\n' || tempchar == '\r'); counter--);
	counter++;
	return (counter);
}


/* Start the functions we need for the plugin stuff */

#ifndef ENABLE_STATIC

G_MODULE_EXPORT void
plugin_register(void)
{
	/* register the new protocol, protocol fields, and subtrees */
	if (proto_megaco == -1) { /* execute protocol initialization only once */
		proto_register_megaco();
	}
}

G_MODULE_EXPORT void
plugin_reg_handoff(void){
	proto_reg_handoff_megaco();
}

#endif

/* End the functions we need for plugin stuff */