aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-sip.c
blob: 8875a69a9c836eea5ab64c7341bd1dd12cf21794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
/* packet-sip.c
 * Routines for the Session Initiation Protocol (SIP) dissection.
 * RFCs 3261-3264
 *
 * TODO: Pay attention to Content-Type: It might not always be SDP.
 *	Content-Type is fixed, mixed/mode is not handled though.
 *       hf_ display filters for headers of SIP extension RFCs:
 *		Done for RFC 3265, RFC 3262
 *		Use hash table for list of headers
 *       Add sip msg body dissection based on Content-Type for:
 *                SDP, MIME, and other types
 *       Align SIP methods with recent Internet Drafts or RFC
 *               (SIP INFO, rfc2976 - done)
 *               (SIP SUBSCRIBE-NOTIFY - done)
 *               (SIP REFER - done)
 *               check for other
 *
 * Copyright 2000, Heikki Vatiainen <hessu@cs.tut.fi>
 * Copyright 2001, Jean-Francois Mule <jfm@cablelabs.com>
 * Copyright 2004, Anders Broman <anders.broman@ericsson.com>
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-cops.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

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

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

#include <epan/prefs.h>

#include <glib.h>
#include <epan/packet.h>
#include <epan/req_resp_hdrs.h>
#include <epan/emem.h>

#include "packet-isup.h"
#include "packet-sip.h"
#include <epan/tap.h>
#include <epan/expert.h>

#include "packet-tcp.h"
#include "packet-ssl.h"

#ifdef NEED_G_ASCII_STRCASECMP_H
#include "g_ascii_strcasecmp.h"
#endif

#define TCP_PORT_SIP 5060
#define UDP_PORT_SIP 5060
#define TLS_PORT_SIP 5061

static gint sip_tap = -1;
static dissector_handle_t sigcomp_handle;

/* Dissectors */
static dissector_handle_t sip_handle = NULL;
static dissector_handle_t sip_tcp_handle = NULL;

/* Initialize the protocol and registered fields */
static gint proto_sip				= -1;
static gint proto_raw_sip			= -1;
static gint hf_raw_sip_line			= -1;
static gint hf_msg_hdr				= -1;
static gint hf_Method				= -1;
static gint hf_Request_Line			= -1;
static gint hf_Status_Code			= -1;
static gint hf_Status_Line			= -1;
static gint hf_sip_display			= -1;
static gint hf_sip_to_addr			= -1;
static gint hf_sip_from_addr		= -1;
static gint hf_sip_tag				= -1;
static gint hf_sip_uri				= -1;
static gint hf_sip_contact_addr		= -1;
static gint hf_sip_contact_item		= -1;
static gint hf_sip_resend			= -1;
static gint hf_sip_original_frame	= -1;

static gint hf_sip_auth                  = -1;
static gint hf_sip_auth_scheme           = -1;
static gint hf_sip_auth_digest_response  = -1;
static gint hf_sip_auth_nc               = -1;
static gint hf_sip_auth_username         = -1;
static gint hf_sip_auth_realm            = -1;
static gint hf_sip_auth_nonce            = -1;
static gint hf_sip_auth_algorithm        = -1;
static gint hf_sip_auth_opaque           = -1;
static gint hf_sip_auth_qop              = -1;
static gint hf_sip_auth_cnonce           = -1;
static gint hf_sip_auth_uri              = -1;
static gint hf_sip_auth_domain           = -1;
static gint hf_sip_auth_stale            = -1;
static gint hf_sip_auth_auts             = -1;
static gint hf_sip_auth_rspauth          = -1;
static gint hf_sip_auth_nextnonce        = -1;
static gint hf_sip_auth_ik               = -1;
static gint hf_sip_auth_ck               = -1;

static gint hf_sip_cseq_seq_no           = -1;
static gint hf_sip_cseq_method           = -1;

static gint hf_sip_via_transport         = -1;
static gint hf_sip_via_sent_by_address   = -1;
static gint hf_sip_via_sent_by_port      = -1;
static gint hf_sip_via_branch            = -1;
static gint hf_sip_via_maddr             = -1;
static gint hf_sip_via_rport             = -1;
static gint hf_sip_via_received          = -1;
static gint hf_sip_via_ttl               = -1;
static gint hf_sip_via_comp              = -1;
static gint hf_sip_via_sigcomp_id        = -1;

static gint hf_sip_rack_rseq_no          = -1;
static gint hf_sip_rack_cseq_no          = -1;
static gint hf_sip_rack_cseq_method      = -1;

static gint hf_sip_msg_body              = -1;

/* Initialize the subtree pointers */
static gint ett_sip 				= -1;
static gint ett_sip_reqresp 		= -1;
static gint ett_sip_hdr 			= -1;
static gint ett_raw_text 			= -1;
static gint ett_sip_element 		= -1;
static gint ett_sip_uri				= -1;
static gint ett_sip_contact_item	= -1;
static gint ett_sip_message_body	= -1;
static gint ett_sip_cseq			= -1;
static gint ett_sip_via				= -1;
static gint ett_sip_reason			= -1;
static gint ett_sip_rack			= -1;

/* PUBLISH method added as per http://www.ietf.org/internet-drafts/draft-ietf-sip-publish-01.txt */
static const char *sip_methods[] = {
        "<Invalid method>",      /* Pad so that the real methods start at index 1 */
        "ACK",
        "BYE",
        "CANCEL",
        "DO",
        "INFO",
        "INVITE",
        "MESSAGE",
        "NOTIFY",
        "OPTIONS",
        "PRACK",
        "QAUTH",
        "REFER",
        "REGISTER",
        "SPRACK",
        "SUBSCRIBE",
        "UPDATE",
        "PUBLISH"
};

/* from RFC 3261
 * Updated with info from http://www.iana.org/assignments/sip-parameters
 * (last updated 2007-07-27)
 * Updated with: http://www.ietf.org/internet-drafts/draft-ietf-sip-resource-priority-05.txt
 */
typedef struct {
        const char *name;
        const char *compact_name;
} sip_header_t;
static const sip_header_t sip_headers[] = {
                { "Unknown-header", 			NULL }, /* 0 Pad so that the real headers start at index 1 */
                { "Accept", 					NULL }, /* 1 */
                { "Accept-Contact",				"a"	 }, /* 2 RFC3841  */
                { "Accept-Encoding", 			NULL }, /* 3 */
                { "Accept-Language", 			NULL }, /* 4 */
                { "Accept-Resource-Priority",	NULL },	/* 5 RFC4412 */
                { "Alert-Info", 				NULL },
                { "Allow", 						NULL },
                { "Allow-Events", 				"u"  },	/* 8 RFC3265  */
                { "Authentication-Info",	 	NULL },
                { "Authorization", 				NULL }, /* 10 */
                { "Call-ID", 					"i"  },
                { "Call-Info", 					NULL },
                { "Contact", 					"m"  },
                { "Content-Disposition", 		NULL },
                { "Content-Encoding", 			"e"  },  /*  15 */
                { "Content-Language", 			NULL },
                { "Content-Length", 			"l"  },
                { "Content-Type", 				"c"  },
                { "CSeq", 						NULL },
                { "Date", 						NULL },  /*  20 */
/*
Encryption (Deprecated)       [RFC3261]
*/
                { "Error-Info", 				NULL },
                { "Event", 						"o"  },
                { "Expires", 					NULL },
                { "From", 						"f"  },  /*  24 */
/*
Hide                          [RFC3261] (deprecated)
*/
                { "History-Info", 				NULL },  /*  25 RFC4244  */
                { "Identity", 					"y"  },  /*	 26 RFC4474  */
                { "Identity-Info", 				"n"  },  /*	 27 RFC4474  */
                { "In-Reply-To", 				NULL },	 /*  28 RFC3261  */
                { "Join",		 				NULL },  /*  29 RFC3911  */
                { "Max-Forwards", 				NULL },  /*  30 */
                { "MIME-Version", 				NULL },  /*  31 */
                { "Min-Expires", 				NULL },  /*  32 */
                { "Min-SE",						NULL },  /*  33 RFC4028  */
                { "Organization", 				NULL },  /*  34 RFC3261  */
                { "P-Access-Network-Info",		NULL },  /*  35 RFC3455  */
                { "P-Answer-State",				NULL },  /*  36 RFC-allen-sipping-poc-p-answer-state-header-05.txt*/
                { "P-Asserted-Identity",        NULL },  /*  37 RFC3325  */
                { "P-Associated-URI",           NULL },	 /*  38 RFC3455  */
                { "P-Called-Party-ID",          NULL },	 /*  39 RFC3455  */
                { "P-Charging-Function-Addresses",NULL },/*  40 RFC3455  */
                { "P-Charging-Vector",          NULL },  /*  41 RFC3455  */
                { "P-DCS-Trace-Party-ID",       NULL },  /*  42 RFC3603  */
                { "P-DCS-OSPS",                 NULL },  /*  43 RFC3603  */
                { "P-DCS-Billing-Info",         NULL },  /*  44 RFC3603  */
                { "P-DCS-LAES",                 NULL },  /*  45 RFC3603  */
                { "P-DCS-Redirect",             NULL },  /*  46 RFC3603  */
                { "P-Early-Media",				NULL },  /*  47 RFC-ejzak-sipping-p-em-auth-04.txt*/
                { "P-Media-Authorization",      NULL },  /*  48 RFC3313  */
                { "P-Preferred-Identity",       NULL },  /*  49 RFC3325  */
                { "P-Profile-Key",				NULL },  /*  50 RFC-camarillo-sipping-profile-key-02.txt */
                { "P-User-Database",			NULL },  /*  51 RFC4457  */
                { "P-Visited-Network-ID",       NULL },  /*  52 RFC3455  */
                { "Path",                       NULL },  /*  53 RFC3327  */
                { "Priority", 					NULL },  /*  54 RFC3261  */
                { "Privacy",                    NULL },  /*  55 RFC3323  */
                { "Proxy-Authenticate", 		NULL },	 /*  56 */
                { "Proxy-Authorization", 		NULL },	 /*  57 */
                { "Proxy-Require", 				NULL },	 /*  58 */
                { "RAck", 						NULL },  /*  59 RFC3262  */
                { "Reason",                     NULL },  /*  60 RFC3326  */
                { "Record-Route", 				NULL },	 /*  61 */
                { "Refer-Sub", 					NULL },  /*  62 RFC4488  */
                { "Refer-To",					"r"  },  /*  63 RFC3515  */
                { "Referred-By",				"b"  },  /*  64 RFC3892  */
                { "Reject-Contact",				"j"  },  /*  65 RFC3841  */
                { "Replaces",					NULL },  /*  66 RFC3891  */
                { "Reply-To", 					NULL },  /*  67 RFC3261  */
                { "Request-Disposition",		"d"  },  /*  68 RFC3841  */
                { "Require", 					NULL },  /*  69 RFC3261  */
                { "Resource-Priority",			NULL },	 /*  70 RFC4412  */
				/*
                { "Response-Key (Deprecated)     [RFC3261]
				*/
                { "Retry-After", 				NULL },  /*  71 RFC3261  */
                { "Route", 						NULL },  /*  72 RFC3261  */
                { "RSeq", 						NULL },  /*  73 RFC3262  */
                { "Security-Client",			NULL },  /*  74 RFC3329  */
                { "Security-Server",			NULL },  /*  75 RFC3329  */
                { "Security-Verify",			NULL },  /*  76 RFC3329  */
                { "Server",			 			NULL },  /*  77 RFC3261  */
                { "Service-Route",				NULL },  /*  78 RFC3608  */
                { "Session-Expires",			"x"  },  /*  79 RFC4028  */
                { "SIP-ETag",					NULL },  /*  80 RFC3903  */
                { "SIP-If-Match",				NULL },  /*  81 RFC3903  */
                { "Subject",					"s"  },  /*  82 RFC3261  */
                { "Subscription-State", 		NULL },  /*  83 RFC3265  */
                { "Supported",					"k"	 },  /*  84 RFC3261  */
                { "Target-Dialog",				NULL },  /*  85 RFC4538  */
                { "Timestamp",					NULL },  /*  86 RFC3261  */
                { "To",							"t"  },  /*  87 RFC3261  */
                { "Unsupported",				NULL },  /*  88 RFC3261  */
                { "User-Agent", 				NULL },  /*  89 RFC3261  */
                { "Via",			 			"v"  },  /*  90 RFC3261  */
                { "Warning",		 			NULL },  /*  91 RFC3261  */
                { "WWW-Authenticate",			NULL },  /*  92 RFC3261  */
};


#define POS_ACCEPT							1
#define POS_ACCEPT_CONTACT					2
#define POS_ACCEPT_ENCODING					3
#define POS_ACCEPT_LANGUAGE					4
#define POS_ACCEPT_RESOURCE_PRIORITY		5
#define POS_ALERT_INFO						6
#define POS_ALLOW							7
#define POS_ALLOW_EVENTS					8
#define POS_AUTHENTICATION_INFO				9
#define POS_AUTHORIZATION					10
#define POS_CALL_ID							11
#define POS_CALL_INFO						12
#define POS_CONTACT							13
#define POS_CONTENT_DISPOSITION				14
#define POS_CONTENT_ENCODING				15
#define POS_CONTENT_LANGUAGE				16
#define POS_CONTENT_LENGTH					17
#define POS_CONTENT_TYPE					18
#define POS_CSEQ							19
#define POS_DATE							20
#define POS_ERROR_INFO						21
#define POS_EVENT							22
#define POS_EXPIRES							23
#define POS_FROM							24
#define POS_HISTORY_INFO					25
#define POS_IDENTITY						26
#define POS_IDENTITY_INFO					27
#define POS_IN_REPLY_TO						28
#define POS_JOIN							29
#define POS_MAX_FORWARDS					30
#define POS_MIME_VERSION					31
#define POS_MIN_EXPIRES						32
#define POS_MIN_SE							33
#define POS_ORGANIZATION					34
#define POS_P_ACCESS_NETWORK_INFO			35
#define POS_P_ASSERTED_IDENTITY				36
#define POS_P_ANSWER_STATE					37
#define POS_P_ASSOCIATED_URI				38
#define POS_P_CALLED_PARTY_ID				39
#define POS_P_CHARGING_FUNCTION_ADDRESSES	40
#define POS_P_CHARGING_VECTOR				41
#define POS_P_DCS_TRACE_PARTY_ID			42
#define POS_P_DCS_OSPS						43
#define POS_P_DCS_BILLING_INFO				44
#define POS_P_DCS_LAES						45
#define POS_P_DCS_REDIRECT					46
#define POS_P_EARLY_MEDIA					47
#define POS_P_MEDIA_AUTHORIZATION			48
#define POS_P_PREFERRED_IDENTITY			49
#define POS_P_PROFILE_KEY					50
#define POS_P_USER_DATABASE					51
#define POS_P_VISITED_NETWORK_ID			52
#define POS_PATH							53
#define POS_PRIORITY						54
#define POS_PRIVACY							55
#define POS_PROXY_AUTHENTICATE				56
#define POS_PROXY_AUTHORIZATION				57
#define POS_PROXY_REQUIRE					58
#define POS_RACK							59
#define POS_REASON							60
#define POS_RECORD_ROUTE					61
#define POS_REFER_SUB						62
#define POS_REFER_TO						63
#define POS_REFERED_BY						64
#define POS_REJECT_CONTACT					65
#define POS_REPLACES						66
#define POS_REPLY_TO						67
#define POS_REQUEST_DISPOSITION				68
#define POS_REQUIRE							69
#define POS_RESOURCE_PRIORITY				70
#define POS_RETRY_AFTER						71
#define POS_ROUTE							72
#define POS_RSEQ							73
#define POS_SECURITY_CLIENT					74
#define POS_SECURITY_SERVER					75
#define POS_SECURITY_VERIFY					76
#define POS_SERVER							77
#define POS_SERVICE_ROUTE					78
#define POS_SESSION_EXPIRES					79
#define POS_SIP_ETAG						80
#define POS_SIP_IF_MATCH					81
#define POS_SUBJECT							82
#define POS_SUBSCRIPTION_STATE				83
#define POS_SUPPORTED						84
#define POS_TARGET_DALOG					85
#define POS_TIMESTAMP						86
#define POS_TO								87
#define POS_UNSUPPORTED						88
#define POS_USER_AGENT						89
#define POS_VIA								90
#define POS_WARNING							91
#define POS_WWW_AUTHENTICATE				92


static gint hf_header_array[] = {
                -1, /* 0"Unknown-header" - Pad so that the real headers start at index 1 */
                -1, /* 1"Accept"										*/
                -1, /* 2"Accept-Contact"						RFC3841	*/
                -1, /* 3"Accept-Encoding"								*/
                -1, /* 4"Accept-Language"								*/
                -1, /* 5"Accept-Resource-Priority"				RFC4412	*/
                -1, /* 6"Alert-Info",									*/
                -1, /* 7"Allow", 										*/
                -1, /* 8"Allow-Events",							RFC3265	*/
                -1, /* 9"Authentication-Info"							*/
                -1, /* 10"Authorization",								*/
                -1, /* 11"Call-ID",										*/
                -1, /* 12"Call-Info"									*/
                -1, /* 13"Contact",										*/
                -1, /* 14"Content-Disposition",							*/
                -1, /* 15"Content-Encoding",							*/
                -1, /* 16"Content-Language",							*/
                -1, /* 17"Content-Length",								*/
                -1, /* 18"Content-Type",								*/
                -1, /* 19"CSeq",										*/
                -1, /* 20"Date",										*/
                -1, /* 21"Error-Info",									*/
                -1, /* 22"Event",										*/
                -1, /* 23"Expires",										*/
                -1, /* 24"From", 										*/
                -1, /* 25"History-Info",						RFC4244 */
                -1, /* 26"Identity",									*/
                -1, /* 27"Identity-Info", 						RFC4474 */
                -1, /* 28"In-Reply-To",							RFC3261	*/
                -1, /* 29"Join",								RFC3911 */
                -1, /* 30"Max-Forwards",								*/
                -1, /* 31"MIME-Version",								*/
                -1, /* 32"Min-Expires",									*/
                -1, /* 33"Min-SE",								RFC4028 */
                -1, /* 34"Organization",								*/
                -1, /* 35"P-Access-Network-Info",				RFC3455	*/
                -1, /* 36"P-Asserted-Identity",					RFC3325	*/
				-1, /* 37"P-Asserted-Identity",							*/
                -1, /* 38"P-Associated-URI",					RFC3455	*/
                -1, /* 39"P-Called-Party-ID",					RFC3455	*/
                -1, /* 40"P-Charging-Function-Addresses",		RFC3455 */
                -1, /* 41"P-Charging-Vector",					RFC3455 */
                -1, /* 42"P-DCS-Trace-Party-ID",				RFC3603 */
                -1, /* 43"P-DCS-OSPS",							RFC3603 */
                -1, /* 44"P-DCS-Billing-Info",					RFC3603 */
                -1, /* 45"P-DCS-LAES",							RFC3603 */
                -1, /* 46"P-DCS-Redirect",						RFC3603 */
				-1, /* 47"P-Early-Media",								*/
                -1, /* 48"P-Media-Authorization",				RFC3313 */
                -1, /* 49"P-Preferred-Identity",				RFC3325 */
				-1, /* 50"P-Profile-Key",								*/
				-1, /* 51"P-User-Database						RFC4457 */
                -1, /* 52"P-Visited-Network-ID",				RFC3455 */
                -1, /* 53"Path",								RFC3327 */
                -1, /* 54"Priority"										*/
                -1, /* 55"Privacy",								RFC3323 */
                -1, /* 56"Proxy-Authenticate",							*/
                -1, /* 57"Proxy-Authorization",							*/
                -1, /* 58"Proxy-Require",								*/
                -1, /* 59"RAck",								RFC3262 */
                -1, /* 60"Reason",								RFC3326 */
                -1, /* 61"Record-Route",								*/
                -1, /* 62"Refer-Sub",",							RFC4488 */
                -1, /* 63"Refer-To",							RFC3515 */
                -1, /* 64"Referred-By",									*/
                -1, /* 65"Reject-Contact",						RFC3841 */
                -1, /* 66"Replaces",							RFC3891 */
                -1, /* 67"Reply-To",							RFC3261 */
                -1, /* 68"Request-Disposition",					RFC3841 */
                -1, /* 69"Require",								RFC3261 */
				-1, /* 70"Resource-Priority",					RFC4412 */
				-1, /* 71"Retry-After",							RFC3261 */
                -1, /* 72"Route",								RFC3261 */
                -1, /* 73"RSeq",								RFC3262 */
                -1, /* 74"Security-Client",						RFC3329 */
                -1, /* 75"Security-Server",						RFC3329 */
                -1, /* 76"Security-Verify",						RFC3329 */
                -1, /* 77"Server",								RFC3261 */
                -1, /* 78"Service-Route",						RFC3608 */
                -1, /* 79"Session-Expires",						RFC4028 */
                -1, /* 80"SIP-ETag",							RFC3903 */
                -1, /* 81"SIP-If-Match",						RFC3903 */
                -1, /* 82"Subject",								RFC3261 */
                -1, /* 83"Subscription-State",					RFC3265 */
                -1, /* 84"Supported",							RFC3261 */
				-1, /* 85"Target-Dialog",						RFC4538 */
                -1, /* 86"Timestamp",							RFC3261 */
                -1, /* 87"To",									RFC3261 */
                -1, /* 88"Unsupported",							RFC3261 */
                -1, /* 89"User-Agent",							RFC3261 */
                -1, /* 90"Via",									RFC3261 */
                -1, /* 91"Warning",								RFC3261 */
                -1, /* 92"WWW-Authenticate",					RFC3261 */

};

/* Track associations between parameter name and hf item */
typedef struct {
	const char  *param_name;
	const gint  *hf_item;
} header_parameter_t;

static header_parameter_t auth_parameters_hf_array[] =
{
	{"response",        &hf_sip_auth_digest_response},
	{"nc",              &hf_sip_auth_nc},
	{"username",        &hf_sip_auth_username},
	{"realm",           &hf_sip_auth_realm},
	{"nonce",           &hf_sip_auth_nonce},
	{"algorithm",       &hf_sip_auth_algorithm},
	{"opaque",          &hf_sip_auth_opaque},
	{"qop",             &hf_sip_auth_qop},
	{"cnonce",          &hf_sip_auth_cnonce},
	{"uri",             &hf_sip_auth_uri},
	{"domain",          &hf_sip_auth_domain},
	{"stale",           &hf_sip_auth_stale},
	{"auts",            &hf_sip_auth_auts},
	{"rspauth",         &hf_sip_auth_rspauth},
	{"nextnonce",       &hf_sip_auth_nextnonce},
	{"ik",              &hf_sip_auth_ik},
	{"ck",              &hf_sip_auth_ck}
};

static header_parameter_t via_parameters_hf_array[] =
{
	{"branch",        &hf_sip_via_branch},
	{"maddr",         &hf_sip_via_maddr},
	{"rport",         &hf_sip_via_rport},
	{"received",      &hf_sip_via_received},
	{"ttl",           &hf_sip_via_ttl},
	{"comp",          &hf_sip_via_comp},
	{"sigcomp-id",    &hf_sip_via_sigcomp_id}
};



/*
 * Type of line.  It's either a SIP Request-Line, a SIP Status-Line, or
 * another type of line.
 */
typedef enum {
	REQUEST_LINE,
	STATUS_LINE,
	OTHER_LINE
} line_type_t;

/* Preferences */
static guint sip_tcp_port = TCP_PORT_SIP;
static guint sip_tls_port = TLS_PORT_SIP;

/* global_sip_raw_text determines whether we are going to display		*/
/* the raw text of the SIP message, much like the MEGACO dissector does.	*/
static gboolean global_sip_raw_text = FALSE;
/* strict_sip_version determines whether the SIP dissector enforces
 * the SIP version to be "SIP/2.0". */
static gboolean strict_sip_version = TRUE;

/*
 * desegmentation of SIP headers
 * (when we are over TCP or another protocol providing the desegmentation API)
 */
static gboolean sip_desegment_headers = TRUE;

/*
 * desegmentation of SIP bodies
 * (when we are over TCP or another protocol providing the desegmentation API)
 */
static gboolean sip_desegment_body = TRUE;

/* Gloabl variables */
static guint saved_sip_tcp_port;
static guint saved_sip_tls_port;

/* Forward declaration we need below */
void proto_reg_handoff_sip(void);
static gboolean dissect_sip_common(tvbuff_t *tvb, int offset, packet_info *pinfo,
    proto_tree *tree, gboolean is_heur, gboolean use_reassembly);
static line_type_t sip_parse_line(tvbuff_t *tvb, int offset, gint linelen,
    guint *token_1_len);
static gboolean sip_is_known_request(tvbuff_t *tvb, int meth_offset,
    guint meth_len, guint *meth_idx);
static gint sip_is_known_sip_header(tvbuff_t *tvb, int offset,
    guint header_len);
static void dfilter_sip_request_line(tvbuff_t *tvb, proto_tree *tree,
    guint meth_len);
static void dfilter_sip_status_line(tvbuff_t *tvb, proto_tree *tree);
static void tvb_raw_text_add(tvbuff_t *tvb, int offset, int length, proto_tree *tree);
static guint sip_is_packet_resend(packet_info *pinfo,
				gchar* cseq_method,
				gchar* call_id,
				guchar cseq_number_set, guint32 cseq_number,
				line_type_t line_type);


/* SIP content type and internet media type used by other dissectors
 * are the same.  List of media types from IANA at:
 * http://www.iana.org/assignments/media-types/index.html */
static dissector_table_t media_type_dissector_table;

static heur_dissector_list_t heur_subdissector_list;

#define SIP2_HDR "SIP/2.0"
#define SIP2_HDR_LEN (strlen (SIP2_HDR))

/* Store the info needed by the SIP tap for one packet */
static sip_info_value_t *stat_info;

/* The buffer size for the cseq_method name */
#define MAX_CSEQ_METHOD_SIZE 16

/****************************************************************************
 * Conversation-type definitions
 *
 * For each call, keep track of the current cseq number and state of
 * transaction, in order to be able to detect retransmissions.
 *
 * Don't use the conservation mechanism, but instead:
 * - store with each dissected packet original frame (if any)
 * - maintain a global hash table of
 *   (call_id, source_addr, dest_addr) -> (cseq, transaction_state, frame)
 ****************************************************************************/

static GHashTable *sip_hash = NULL;           /* Hash table */

/* Types for hash table keys and values */
#define MAX_CALL_ID_SIZE 128
typedef struct
{
	char call_id[MAX_CALL_ID_SIZE];
	address source_address;
	guint32 source_port;
	address dest_address;
	guint32 dest_port;
} sip_hash_key;


typedef enum
{
	nothing_seen,
	request_seen,
	provisional_response_seen,
	final_response_seen
} transaction_state_t;

typedef struct
{
	guint32 cseq;
	transaction_state_t transaction_state;
	gchar method[MAX_CSEQ_METHOD_SIZE];
	guint32 response_code;
	gint frame_number;
} sip_hash_value;


/************************/
/* Hash table functions */

/* Equal keys */
static gint sip_equal(gconstpointer v, gconstpointer v2)
{
	const sip_hash_key* val1 = v;
	const sip_hash_key* val2 = v2;

	/* Call id must match */
	if (strcmp(val1->call_id, val2->call_id) != 0)
	{
		return 0;
	}

	/* Addresses must match */
	return  (ADDRESSES_EQUAL(&(val1->source_address), &(val2->source_address))) &&
		(val1->source_port == val2->source_port) &&
		(ADDRESSES_EQUAL(&(val1->dest_address), &(val2->dest_address))) &&
		(val1->dest_port == val2->dest_port);
}

/* Compute a hash value for a given key. */
/* Don't try to use addresses here, call-id should be almost unique. */
static guint sip_hash_func(gconstpointer v)
{
	gint n;
	const sip_hash_key *key = v;
	guint value = strlen(key->call_id);
	gint chars_to_use = value / 4;

	/* First few characters from the call-id should be enough... */
	for (n=0; n < chars_to_use; n++)
	{
		value += key->call_id[n];
	}

	return value;
}


/* Initializes the hash table and the mem_chunk area each time a new
 * file is loaded or re-loaded in wireshark */
static void
sip_init_protocol(void)
{
	/* Destroy any existing hashes. */
	if (sip_hash)
		g_hash_table_destroy(sip_hash);

	/* Now create them over */
	sip_hash = g_hash_table_new(sip_hash_func, sip_equal);
}

/* Structure to collect info about a sip uri */
typedef struct _uri_offset_info
{
	gint display_name_start;
	gint display_name_end;
	gint uri_start;
	gint uri_end;
	gint uri_parameters_start;
	gint uri_parameters_end;
	gint name_addr_start;
	gint name_addr_end;
} uri_offset_info;

/* Code to parse a sip uri.
 * Returns Offset end off parsing or -1 for unsuccessful parsing
 */
static gint
dissect_sip_uri(tvbuff_t *tvb, packet_info *pinfo _U_, gint start_offset,
                gint line_end_offset, uri_offset_info *uri_offsets)
{
	gchar c;
	gint i;
	gint current_offset;
	gint queried_offset;
	gint colon_offset;
	gint comma_offset;
	gint semicolon_offset;
	gint question_mark_offset;
	gboolean uri_without_angle_quotes = FALSE;

	/* skip Spaces and Tabs */
	current_offset = tvb_skip_wsp(tvb, start_offset, line_end_offset - start_offset);

	if(current_offset >= line_end_offset) {
		/* Nothing to parse */
		return -1;
	}

	uri_offsets->name_addr_start = current_offset;

	/* First look, if we have a display name */
	c=tvb_get_guint8(tvb, current_offset);
	switch(c)
	{
		case '"':
			/* We have a display name, look for the next unescaped '"' */
			uri_offsets->display_name_start = current_offset;
			do
			{
				queried_offset = tvb_find_guint8(tvb, current_offset + 1, line_end_offset - (current_offset + 1), '"');
				if(queried_offset == -1)
				{
					/* malformed URI */
					return -1;
				}
				current_offset = queried_offset;

				/* Is it escaped? */
				/* count back slashes before '"' */
				for(i=1;tvb_get_guint8(tvb, queried_offset - i) == '\\';i++);
				i--;

				if(i % 2 == 0)
				{
					/* not escaped */
					break;
				}
			} while (current_offset < line_end_offset);
			if(current_offset >= line_end_offset)
			{
				/* malformed URI */
				return -1;
			}

			uri_offsets->display_name_end = current_offset;

			/* find start of the URI */
			queried_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, '<');
			if(queried_offset == -1)
			{
				/* malformed Uri */
				return -1;
			}
			current_offset = queried_offset + 1;
			break;

		case '<':
			/* We don't have a display name */
			current_offset++;
			break;

		default:
			/* We have either an URI without angles or a display name with a limited character set */
			/* Look for the right angle quote or colon */
			queried_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, '<');
			colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ':');
			if(queried_offset != -1 && colon_offset != -1)
			{
				if(queried_offset < colon_offset)
				{
					/* we have an URI with angle quotes */
					uri_offsets->display_name_start = current_offset;
					uri_offsets->display_name_end = queried_offset - 1;
					current_offset = queried_offset + 1;
				}
				else
				{
					/* we have an URI without angle quotes */
					uri_without_angle_quotes = TRUE;
				}
			}
			else
			{
				if(queried_offset != -1)
				{
					/* we have an URI with angle quotes */
					uri_offsets->display_name_start = current_offset;
					uri_offsets->display_name_end = queried_offset - 1;
					current_offset = queried_offset + 1;
					break;
				}
				if(colon_offset != -1)
				{
					/* we have an URI without angle quotes */
					uri_without_angle_quotes = TRUE;
					break;
				}
				/* If this point is reached, we can't parse the URI */
				return -1;
			}
			break;
	}

	/* Start parsing of URI */
	uri_offsets->uri_start = current_offset;
	if(uri_without_angle_quotes == TRUE)
	{
		/* look for the first ',' or ';' which will mark the end of this URI
		 * In this case a semicolon indicates a header field parameter, and not an uri parameter.
		 */
		comma_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ',');
		semicolon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ';');

		if (semicolon_offset != -1 && comma_offset != -1)
		{
			if(semicolon_offset < comma_offset)
			{
				uri_offsets->uri_end = semicolon_offset - 1;
			}
			else
			{
				uri_offsets->uri_end = comma_offset - 1;
			}
		}
		else
		{
			if (semicolon_offset != -1)
			{
				uri_offsets->uri_end = semicolon_offset - 1;
			}
			if (comma_offset != -1)
			{
				uri_offsets->uri_end = comma_offset - 1;
			}
			/* If both offsets are equal to -1, we don't have a semicolon or a comma.
			 * In that case, we assume that the end of the URI is at the line end
			 */
			uri_offsets->uri_end = line_end_offset - 2;
		}
		uri_offsets->name_addr_end = uri_offsets->uri_end;
		current_offset = uri_offsets->uri_end + 1; /* Now save current_offset, as it is the value to be returned now */
	}
	else
	{
		/* look for closing angle quote */
		queried_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, '>');
		if(queried_offset == -1)
		{
			/* malformed Uri */
			return -1;
		}
		uri_offsets->name_addr_end = queried_offset;
		uri_offsets->uri_end = queried_offset - 1;
		current_offset = queried_offset; /* Now save current_offset. It contains the value we have to return */

		/* Look for '@' within URI */
		queried_offset = tvb_find_guint8(tvb, uri_offsets->uri_start, uri_offsets->uri_end - uri_offsets->uri_start, '@');
		if(queried_offset == -1)
		{
			/* no '@': look for the first ';' or '?' in the URI */
			question_mark_offset = tvb_find_guint8(tvb, uri_offsets->uri_start, uri_offsets->uri_end - uri_offsets->uri_start, '?');
			semicolon_offset = tvb_find_guint8(tvb, uri_offsets->uri_start, uri_offsets->uri_end - uri_offsets->uri_start, ';');
		}
		else
		{
			/* with '@': look for the first ';' or '?' behind the '@' */
			question_mark_offset = tvb_find_guint8(tvb, queried_offset, uri_offsets->uri_end - queried_offset, '?');
			semicolon_offset = tvb_find_guint8(tvb, queried_offset, uri_offsets->uri_end - queried_offset, ';');
		}

		/* Set Parameter*/
		if (semicolon_offset != -1 && question_mark_offset != -1)
		{
			if(semicolon_offset < question_mark_offset)
			{
				uri_offsets->uri_parameters_start = semicolon_offset;
			}
			else
			{
				uri_offsets->uri_parameters_start = question_mark_offset;
			}
			uri_offsets->uri_parameters_end = uri_offsets->uri_end;
			uri_offsets->uri_end = uri_offsets->uri_parameters_start - 1;
		}
		else
		{
			if (semicolon_offset != -1)
			{
				uri_offsets->uri_parameters_start = semicolon_offset;
				uri_offsets->uri_parameters_end = uri_offsets->uri_end;
				uri_offsets->uri_end = uri_offsets->uri_parameters_start - 1;
			}
			if (question_mark_offset != -1)
			{
				uri_offsets->uri_parameters_start = question_mark_offset;
				uri_offsets->uri_parameters_end = uri_offsets->uri_end;
				uri_offsets->uri_end = uri_offsets->uri_parameters_start - 1;
			}
			/* If both offsets are equal to -1, we don't have a semicolon or a question mark.
			 * In that case, we don't have to save any offsets.
			 */
		}

	}

	return current_offset;
}


/* Code to parse a contact header item
 * Returns Offset end off parsing or -1 for unsuccessful parsing
 */
static gint
dissect_sip_contact_item(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint start_offset, gint line_end_offset)
{
	gchar c;
	gint i;
	proto_item *ti = NULL;
	proto_tree *contact_item_tree = NULL, *uri_tree = NULL;

	gint current_offset;
	gint queried_offset;
	gint contact_params_start_offset = -1;
	gint contact_item_end_offset = -1;
	uri_offset_info uri_offsets;

	uri_offsets.display_name_start = -1;
	uri_offsets.display_name_end = -1;
	uri_offsets.uri_start = -1;
	uri_offsets.uri_end = -1;
	uri_offsets.uri_parameters_start = -1;
	uri_offsets.uri_parameters_end = -1;
	uri_offsets.name_addr_start = -1;
	uri_offsets.name_addr_end = -1;

	/* skip Spaces and Tabs */
	start_offset = tvb_skip_wsp(tvb, start_offset, line_end_offset - start_offset);

	if(start_offset >= line_end_offset) {
		/* Nothing to parse */
		return -1;
	}

	current_offset = dissect_sip_uri(tvb, pinfo, start_offset, line_end_offset, &uri_offsets);
	if(current_offset == -1)
	{
		/* Parsing failed */
		return -1;
	}

	/* Now look for the end of the contact item */
	while (current_offset < line_end_offset)
	{
		c=tvb_get_guint8(tvb, current_offset);

		if(c == ';' && contact_params_start_offset == -1)
		{
			/* here we start with contact parameters */
			contact_params_start_offset = current_offset;
		}

		if(c == '"')
		{
			/* look for the next unescaped '"' */
			do
			{
				queried_offset = tvb_find_guint8(tvb, current_offset + 1, line_end_offset - (current_offset + 1), '"');
				if(queried_offset == -1)
				{
					/* malformed Contact header */
					return -1;
				}
				current_offset = queried_offset;

				/* Is it escaped?
				 * Look for uneven number of backslashes before '"' */
				for(i=0;tvb_get_guint8(tvb, queried_offset - (i+1) ) == '\\';i++);
				i=i%2;
			} while (i == 1);
		}

		if(c == ',')
		{
			/* end of contact item found. */
			contact_item_end_offset = current_offset - 1; /* remove ',' */
			break;
		}

		current_offset++;
	}

	if(contact_item_end_offset == -1)
		contact_item_end_offset = line_end_offset - 3;  /* remove '\r\n' */

	/* Build the tree, now */
	if(tree)
	{
		ti = proto_tree_add_string(tree, hf_sip_contact_item, tvb, start_offset, contact_item_end_offset - start_offset + 1,
		                           tvb_format_text(tvb, start_offset, contact_item_end_offset - start_offset + 1));
		contact_item_tree = proto_item_add_subtree(ti, ett_sip_contact_item);

		ti = proto_tree_add_string(contact_item_tree, hf_sip_uri, tvb, uri_offsets.name_addr_start, uri_offsets.name_addr_end - uri_offsets.name_addr_start + 1,
		                           tvb_format_text(tvb, uri_offsets.name_addr_start, uri_offsets.name_addr_end - uri_offsets.name_addr_start + 1));
		uri_tree = proto_item_add_subtree(ti, ett_sip_uri);

		if(uri_offsets.display_name_start != -1 && uri_offsets.display_name_end != -1)
		{
			proto_tree_add_string(uri_tree, hf_sip_display, tvb, uri_offsets.display_name_start,
			                      uri_offsets.display_name_end - uri_offsets.display_name_start + 1,
			                      tvb_format_text(tvb, uri_offsets.display_name_start,
			                                      uri_offsets.display_name_end - uri_offsets.display_name_start + 1));
		}

		if(uri_offsets.uri_start != -1 && uri_offsets.uri_end != -1)
		{
			proto_tree_add_string(uri_tree, hf_sip_contact_addr, tvb, uri_offsets.uri_start,
			                      uri_offsets.uri_end - uri_offsets.uri_start + 1,
			                      tvb_format_text(tvb, uri_offsets.uri_start,
			                                      uri_offsets.uri_end - uri_offsets.uri_start + 1));
		}

		/* Parse URI and Contact header Parameters now */
		/* TODO */
	}

	return current_offset;
}

/* Code to parse an authorization header item
 * Returns offset at end of parsing, or -1 for unsuccessful parsing
 */
static gint
dissect_sip_authorization_item(tvbuff_t *tvb, proto_tree *tree, gint start_offset, gint line_end_offset)
{
	gchar c;
	gint current_offset;
	gint equals_offset = 0;
	gchar *name;
	header_parameter_t *auth_parameter;
	guint i = 0;

	/* skip Spaces and Tabs */
	start_offset = tvb_skip_wsp(tvb, start_offset, line_end_offset - start_offset);

	if (start_offset >= line_end_offset)
	{
		/* Nothing to parse */
		return -1;
	}

	current_offset = start_offset;

	/* Now look for the end of the parameter */
	while (current_offset < line_end_offset)
	{
		c = tvb_get_guint8(tvb, current_offset);

		if (c == '=')
		{
			equals_offset = current_offset;
		}

		if(c == '"')
		{
			/* look for the next unescaped '"' */
			do
			{
				current_offset = tvb_find_guint8(tvb, current_offset + 1, line_end_offset - (current_offset + 1), '"');
				if(current_offset == -1)
				{
					/* malformed parameter */
					return -1;
				}

				/* Is it escaped?
				 * Look for uneven number of backslashes before '"' */
				for(i=0;tvb_get_guint8(tvb, current_offset - (i+1) ) == '\\';i++);
				i=i%2;
			} while (i == 1);
			current_offset++;
			current_offset = tvb_skip_wsp(tvb, current_offset, line_end_offset - current_offset);
			continue;
		}

		if (c == ',')
		{
			break;
		}
		current_offset++;
	}

	if (equals_offset == 0)
	{
		/* Give up if equals not found */
		return -1;
	}

	/* Extract the parameter name */
	name = tvb_get_ephemeral_string(tvb, start_offset, equals_offset-start_offset);

	/* Try to add parameter as a filterable item */
	for (auth_parameter = &auth_parameters_hf_array[i];
	     i < array_length(auth_parameters_hf_array);
	     i++, auth_parameter++)
	{
		if (g_ascii_strcasecmp(name, auth_parameter->param_name) == 0)
		{
			proto_tree_add_item(tree, *(auth_parameter->hf_item), tvb,
			                    equals_offset+1, current_offset-equals_offset-1,
			                    FALSE);
			break;
		}
	}

	/* If not matched, just add as text... */
	if (i == array_length(auth_parameters_hf_array))
	{
		proto_tree_add_text(tree, tvb, start_offset, current_offset-start_offset,
		                    "%s", tvb_format_text(tvb, start_offset,
		                                    current_offset-start_offset));
	}

	return current_offset;
}

/* Dissect the details of a Reason header */
static void
dissect_sip_reason_header(tvbuff_t *tvb, proto_tree *tree, gint start_offset, gint line_end_offset){

	gint  current_offset, semi_colon_offset, length;
	gchar *param_name = NULL;
	guint cause_value;

		/* skip Spaces and Tabs */
	start_offset = tvb_skip_wsp(tvb, start_offset, line_end_offset - start_offset);

	if (start_offset >= line_end_offset)
	{
		/* Nothing to parse */
		return;
	}

	current_offset = start_offset;
	semi_colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset-current_offset, ';');
	length = semi_colon_offset - current_offset;
	proto_tree_add_text(tree, tvb, start_offset, length,
		"Reason Protocols: %s", tvb_format_text(tvb, start_offset, length));

	param_name = tvb_get_ephemeral_string(tvb, start_offset, length);
	if (g_ascii_strcasecmp(param_name, "Q.850") == 0){
		current_offset = tvb_find_guint8(tvb, semi_colon_offset, line_end_offset-semi_colon_offset, '=')+1;
		length = line_end_offset - current_offset;

		/* q850_cause_code_vals */
		cause_value = atoi(tvb_get_ephemeral_string(tvb, current_offset, length));
		proto_tree_add_text(tree, tvb, current_offset, length,
			"Cause: %u(0x%x)[%s]", cause_value,cause_value,
			val_to_str(cause_value, q850_cause_code_vals, "Unknown (%d)" ));

	}

}


/* Dissect the details of a Via header */
static void dissect_sip_via_header(tvbuff_t *tvb, proto_tree *tree, gint start_offset, gint line_end_offset)
{
	gint  current_offset;
	gint  transport_start_offset = 0;
	gint  address_start_offset;
	gint  semicolon_offset = 0;
	guint transport_slash_count = 0;
	gboolean transport_name_started = FALSE;
	gboolean colon_seen = FALSE;
	guchar c;
	gchar *param_name = NULL;

	/* skip Spaces and Tabs */
	start_offset = tvb_skip_wsp(tvb, start_offset, line_end_offset - start_offset);

	if (start_offset >= line_end_offset)
	{
		/* Nothing to parse */
		return;
	}

	current_offset = start_offset;

	/* Now look for the end of the SIP/2.0/transport parameter.
	   There may be spaces between the slashes */
	while (current_offset < line_end_offset)
	{
		c = tvb_get_guint8(tvb, current_offset);
		if (c == '/')
		{
			transport_slash_count++;
		}
		else
		if (!transport_name_started && (transport_slash_count == 2) && isalpha(c))
		{
			transport_name_started = TRUE;
			transport_start_offset = current_offset;
		}
		else
		if (transport_name_started && ((c == ' ') || (c == '\t')))
		{
			proto_tree_add_item(tree, hf_sip_via_transport, tvb, transport_start_offset,
			                    current_offset - transport_start_offset, FALSE);

			break;
		}

		current_offset++;
	}

	/* skip Spaces and Tabs */
	current_offset = tvb_skip_wsp(tvb, current_offset, line_end_offset - current_offset);

    /* Now read the address part */
	address_start_offset = current_offset;
	while (current_offset < line_end_offset)
	{
		c = tvb_get_guint8(tvb, current_offset);

		if (colon_seen || (c == ' ') || (c == '\t') || (c == ':') || (c == ';'))
		{
			break;
		}

		current_offset++;
	}
	/* Add address to tree */
	proto_tree_add_item(tree, hf_sip_via_sent_by_address, tvb, address_start_offset,
	                    current_offset - address_start_offset, FALSE);

	/* Transport port number may follow ([space] : [space])*/
	current_offset = tvb_skip_wsp(tvb, current_offset, line_end_offset - current_offset);
	c = tvb_get_guint8(tvb, current_offset);

	if (c == ':')
	{
		/* Port number will follow any space after : */
		gint port_offset;
		colon_seen = TRUE;
		current_offset++;

		/* Skip optional space after colon */
		current_offset = tvb_skip_wsp(tvb, current_offset, line_end_offset - current_offset);

		port_offset = current_offset;

		/* Find digits of port number */
		while (current_offset < line_end_offset)
		{
			c = tvb_get_guint8(tvb, current_offset);

			if (!isdigit(c))
			{
				if (current_offset > port_offset)
				{
					/* Add address port number to tree */
					proto_tree_add_uint(tree, hf_sip_via_sent_by_port, tvb, port_offset,
					                    current_offset - port_offset,
					                    atoi(tvb_get_ephemeral_string(tvb, port_offset,
					                                                  current_offset - port_offset)));
				}
				else
				{
					/* Shouldn't see a colon without a port number given */
					return;
				}
				break;
			}

			current_offset++;
		}
	}

	/* skip Spaces and Tabs */
	current_offset = tvb_skip_wsp(tvb, current_offset, line_end_offset - current_offset);


    /* Dissect any parameters found */
	while (current_offset < line_end_offset)
	{
		gboolean equals_found = FALSE;
		gint parameter_name_end = 0;
		gint parameter_value_end;
		header_parameter_t *via_parameter;
		guint i = 0;

		/* Look for the semicolon that signals the start of a parameter */
		while (current_offset < line_end_offset)
		{
			c = tvb_get_guint8(tvb, current_offset);
			if (c == ';')
			{
				semicolon_offset = current_offset;
				current_offset++;
				break;
			}
			else
			if ((c != ' ') && (c != '\t'))
			{
				return;
			}

			current_offset++;
		}
		if (current_offset == line_end_offset)
		{
			return;
		}

		/* Look for end of parameter name */
		while (current_offset < line_end_offset)
		{
			c = tvb_get_guint8(tvb, current_offset);
			if (!isalpha(c) && (c != '-'))
			{
				break;
			}
			current_offset++;
		}

		/* Not all params have an = */
		if (c == '=')
		{
			equals_found = TRUE;
		}
		parameter_name_end = current_offset;

		/* Read until end of parameter value */
		while (current_offset < line_end_offset)
		{
			c = tvb_get_guint8(tvb, current_offset);
			if ((c == ' ') || (c == '\t') || (c == ';'))
			{
				break;
			}
			current_offset++;
		}

		/* Note parameter name */
		parameter_value_end = current_offset;
		param_name = tvb_get_ephemeral_string(tvb, semicolon_offset+1,
		                                      parameter_name_end - semicolon_offset - 1);

		/* Try to add parameter as a filterable item */
		for (via_parameter = &via_parameters_hf_array[i];
			 i < array_length(via_parameters_hf_array);
			 i++, via_parameter++)
		{
			if (g_ascii_strcasecmp(param_name, via_parameter->param_name) == 0)
			{
				if (equals_found)
				{
					proto_tree_add_item(tree, *(via_parameter->hf_item), tvb,
					                    parameter_name_end+1, current_offset-parameter_name_end-1,
					                    FALSE);
				}
				else
				{
					proto_tree_add_item(tree, *(via_parameter->hf_item), tvb,
					                    semicolon_offset+1, current_offset-semicolon_offset-1,
					                    FALSE);
				}
				break;
			}
		}

		/* If not matched, just add as text... */
		if (i == array_length(via_parameters_hf_array))
		{
			proto_tree_add_text(tree, tvb, semicolon_offset+1, current_offset-semicolon_offset-1,
			                    "%s", tvb_format_text(tvb, semicolon_offset+1,
			                    current_offset-semicolon_offset-1));
		}

		/* skip Spaces and Tabs */
		current_offset = tvb_skip_wsp(tvb, current_offset, line_end_offset - current_offset);
	}

}


/* Code to actually dissect the packets */
static int
dissect_sip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	guint8 octet;
	int len;

	octet = tvb_get_guint8(tvb,0);
	if ((octet  & 0xf8) == 0xf8){
		call_dissector(sigcomp_handle, tvb, pinfo, tree);
		return tvb_length(tvb);
	}

	len = dissect_sip_common(tvb, 0, pinfo, tree, FALSE, FALSE);
	if (len < 0)
		return 0;	/* not SIP */
	else
		return len;
}

static void
dissect_sip_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	guint8 octet;
	int offset = 0;
	int len;

	octet = tvb_get_guint8(tvb,0);
	if ((octet  & 0xf8) == 0xf8){
		call_dissector(sigcomp_handle, tvb, pinfo, tree);
		return;
	}

	while (tvb_reported_length_remaining(tvb, offset) != 0) {
		len = dissect_sip_common(tvb, offset, pinfo, tree, TRUE, TRUE);
		if (len <= 0)
			break;
		offset += len;
	}
}

static gboolean
dissect_sip_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	int offset = 0;
	int len;
	gboolean first = TRUE;

	while (tvb_reported_length_remaining(tvb, offset) != 0) {
		len = dissect_sip_common(tvb, offset, pinfo, tree, !first, TRUE);
		if (len == -2) {
			if (first) {
				/*
				 * If the first packet doesn't start with
				 * a valid SIP request or response, don't
				 * treat this as SIP.
				 */
				return FALSE;
			}
			break;
		}
		if (len == -1)
			break;	/* need more data */
		offset += len;
	}
	return TRUE;
}

static gboolean
dissect_sip_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	return dissect_sip_common(tvb, 0, pinfo, tree, FALSE, FALSE) > 0;
}

static int
dissect_sip_common(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree,
    gboolean dissect_other_as_continuation, gboolean use_reassembly)
{
	int orig_offset;
	gint next_offset, linelen;
	int content_length, datalen, reported_datalen;
	line_type_t line_type;
	tvbuff_t *next_tvb;
	gboolean is_known_request;
	gboolean found_match = FALSE;
	const char *descr;
	guint token_1_len = 0;
	guint current_method_idx = 0;
	proto_item *ts = NULL, *ti = NULL, *th = NULL, *sip_element_item = NULL;
	proto_tree *sip_tree = NULL, *reqresp_tree = NULL , *hdr_tree = NULL,
		*sip_element_tree = NULL, *message_body_tree = NULL, *cseq_tree = NULL,
		*via_tree = NULL, *reason_tree = NULL, *rack_tree = NULL;
	guchar contacts = 0, contact_is_star = 0, expires_is_0 = 0;
	guint32 cseq_number = 0;
	guchar  cseq_number_set = 0;
	char    cseq_method[MAX_CSEQ_METHOD_SIZE] = "";
	char	call_id[MAX_CALL_ID_SIZE] = "";
	char *media_type_str = NULL;
	char *media_type_str_lower_case = NULL;
	char *content_type_parameter_str = NULL;
	guint resend_for_packet = 0;
	int strlen_to_copy;


	/*
	 * Note that "tvb_find_line_end()" will return a value that
	 * is not longer than what's in the buffer, so the
	 * "tvb_get_ptr()" calls below won't throw exceptions.
	 *
	 * Note that "tvb_strneql()" doesn't throw exceptions, so
	 * "sip_parse_line()" won't throw an exception.
	 */
	orig_offset = offset;
	linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
	if (tvb_strnlen(tvb, offset, linelen) > -1)
	{
		/*
		 * There's a NULL in the line,
		 * this may be SIP within another protocol.
		 * This heuristic still needs to improve.
		 */
		return -2;
	}
	line_type = sip_parse_line(tvb, offset, linelen, &token_1_len);

	if (line_type == OTHER_LINE) {
		/*
		 * This is neither a SIP request nor response.
		 */
		if (!dissect_other_as_continuation) {
			/*
			 * We were asked to reject this.
			 */
			return -2;
		}

		/*
		 * Just dissect it as a continuation.
		 */
	} else if (use_reassembly) {
		/*
		 * Yes, it's a request or response.
		 * Do header desegmentation if we've been told to,
		 * and do body desegmentation if we've been told to and
		 * we find a Content-Length header.
		 */
		if (!req_resp_hdrs_do_reassembly(tvb, offset, pinfo,
		    sip_desegment_headers, sip_desegment_body)) {
			/*
			 * More data needed for desegmentation.
			 */
			return -1;
		}
	}

	/* Initialise stat info for passing to tap */
	stat_info = ep_alloc(sizeof(sip_info_value_t));
	stat_info->response_code = 0;
	stat_info->request_method = NULL;
	stat_info->reason_phrase = NULL;
	stat_info->resend = 0;
	stat_info->tap_call_id = NULL;
	stat_info->tap_from_addr = NULL;
	stat_info->tap_to_addr = NULL;

	if (check_col(pinfo->cinfo, COL_PROTOCOL))
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "SIP");

	switch (line_type) {

	case REQUEST_LINE:
		is_known_request = sip_is_known_request(tvb, offset, token_1_len, &current_method_idx);
		descr = is_known_request ? "Request" : "Unknown request";
		if (check_col(pinfo->cinfo, COL_INFO)) {
			col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
			             descr,
			             tvb_format_text(tvb, offset, linelen - SIP2_HDR_LEN - 1));
		}
		break;

	case STATUS_LINE:
		descr = "Status";
		if (check_col(pinfo->cinfo, COL_INFO)) {
			col_add_fstr(pinfo->cinfo, COL_INFO, "Status: %s",
			             tvb_format_text(tvb, offset + SIP2_HDR_LEN + 1, linelen - SIP2_HDR_LEN - 1));
		}
		stat_info->reason_phrase = tvb_get_ephemeral_string(tvb, offset + SIP2_HDR_LEN + 5, linelen - (SIP2_HDR_LEN + 5));
		break;

	case OTHER_LINE:
	default: /* Squelch compiler complaints */
		descr = "Continuation";
		if (check_col(pinfo->cinfo, COL_INFO))
			col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
		break;
	}

	if (tree) {
		ts = proto_tree_add_item(tree, proto_sip, tvb, offset, -1, FALSE);
		sip_tree = proto_item_add_subtree(ts, ett_sip);
	}

	switch (line_type) {

	case REQUEST_LINE:
		if (sip_tree) {
			ti = proto_tree_add_string(sip_tree, hf_Request_Line, tvb, offset, next_offset-offset,
			                           tvb_format_text(tvb, offset, linelen));
			reqresp_tree = proto_item_add_subtree(ti, ett_sip_reqresp);
		}
		dfilter_sip_request_line(tvb, reqresp_tree, token_1_len);
		break;

	case STATUS_LINE:
		if (sip_tree) {
			ti = proto_tree_add_string(sip_tree, hf_Status_Line, tvb, offset, next_offset-offset,
			                           tvb_format_text(tvb, offset, linelen));
			reqresp_tree = proto_item_add_subtree(ti, ett_sip_reqresp);
		}
		dfilter_sip_status_line(tvb, reqresp_tree);
		break;

	case OTHER_LINE:
		if (sip_tree) {
			ti = proto_tree_add_text(sip_tree, tvb, offset, next_offset,
			                         "%s line: %s", descr,
			                         tvb_format_text(tvb, offset, linelen));
			reqresp_tree = proto_item_add_subtree(ti, ett_sip_reqresp);
			proto_tree_add_text(sip_tree, tvb, offset, -1,
			                    "Continuation data");
		}
		return tvb_length_remaining(tvb, offset);
	}

	offset = next_offset;
	if (sip_tree) {
		th = proto_tree_add_item(sip_tree, hf_msg_hdr, tvb, offset, -1, FALSE);
		hdr_tree = proto_item_add_subtree(th, ett_sip_hdr);
	}

	/*
	 * Process the headers - if we're not building a protocol tree,
	 * we just do this to find the blank line separating the
	 * headers from the message body.
	 */
	next_offset = offset;
	content_length = -1;
	while (tvb_reported_length_remaining(tvb, offset) > 0) {
		gint line_end_offset;
		gint colon_offset;
		gint semi_colon_offset;
		gint len;
		gint parameter_offset;
		gint parameter_end_offset;
		gint parameter_len;
		gint content_type_len, content_type_parameter_str_len;
		gint header_len;
		gint hf_index;
		gint value_offset;
		gint sub_value_offset;
		gint comma_offset;
		guchar c;
		size_t value_len;
		char *value;

		linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
		if (linelen == 0) {
			/*
			 * This is a blank line separating the
			 * message header from the message body.
			 */
			offset = next_offset;
			break;
		}

		line_end_offset = offset + linelen;
		while ((c = tvb_get_guint8(tvb, next_offset)) == ' ' || c == '\t')
		{
			/*
			 * This line end is not a header seperator.
			 * It just extends the header with another line.
			 * Look for next line end:
			 */
			linelen += (next_offset - line_end_offset);
			linelen += tvb_find_line_end(tvb, next_offset, -1, &next_offset, FALSE);
			line_end_offset = offset + linelen;
		}

		colon_offset = tvb_find_guint8(tvb, offset, linelen, ':');
		if (colon_offset == -1) {
			/*
			 * Malformed header - no colon after the name.
			 */
			if(hdr_tree) {
				proto_tree_add_text(hdr_tree, tvb, offset,
				                    next_offset - offset, "%s",
				                    tvb_format_text(tvb, offset, linelen));
			}
		} else {
			header_len = colon_offset - offset;
			hf_index = sip_is_known_sip_header(tvb, offset, header_len);

			if (hf_index == -1) {
				if(hdr_tree) {
					proto_item *ti = proto_tree_add_text(hdr_tree, tvb,
					                                     offset, next_offset - offset, "%s",
					                                     tvb_format_text(tvb, offset, linelen));
					expert_add_info_format(pinfo, ti,
					                       PI_UNDECODED, PI_NOTE,
					                       "Unrecognised SIP header (%s)",
					                       tvb_format_text(tvb, offset, header_len));
				}
			} else {
				/*
				 * Skip whitespace after the colon.
				 */
				value_offset = tvb_skip_wsp(tvb, colon_offset + 1, line_end_offset - (colon_offset + 1));

				/*
				 * Fetch the value.
				 */
				value_len = line_end_offset - value_offset;
				value = tvb_get_ephemeral_string(tvb, value_offset,
				                       value_len);

				/*
				 * Add it to the protocol tree,
				 * but display the line as is.
				 */
				switch ( hf_index ) {

					case POS_TO :
						if(hdr_tree) {
							sip_element_item = proto_tree_add_string_format(hdr_tree,
							                   hf_header_array[hf_index], tvb,
							                   offset, next_offset - offset,
							                   value, "%s",
							                   tvb_format_text(tvb, offset, linelen));
							sip_element_tree = proto_item_add_subtree( sip_element_item,
							                   ett_sip_element);
						}
						/* See if we have a SIP/SIPS uri enclosed in <>, if so anything in front is
						 * display info.
						 */
						parameter_offset = tvb_find_guint8(tvb, value_offset,value_len, '<');
						if ( parameter_offset != -1){
							len = parameter_offset - value_offset;
							if ( len > 1){
								/* Something in front, must be display info
								 * TODO: Get rid of trailing space(s)
								 */
								proto_tree_add_item(sip_element_tree, hf_sip_display, tvb, value_offset,
								                    len, FALSE);
							}
							parameter_offset ++;
							parameter_end_offset = parameter_offset;
							/* RFC3261 paragraph 20
							 * The Contact, From, and To header fields contain a URI.  If the URI
							 * contains a comma, question mark or semicolon, the URI MUST be
							 * enclosed in angle brackets (< and >).  Any URI parameters are
							 * contained within these brackets.  If the URI is not enclosed in angle
							 * brackets, any semicolon-delimited parameters are header-parameters,
							 * not URI parameters.
							 */
							while (parameter_end_offset < line_end_offset){
								parameter_end_offset++;
								c = tvb_get_guint8(tvb, parameter_end_offset);
								switch (c) {
									case '>':
									case ',':
									case ';':
									case '?':
										goto separator_found;
									default :
									break;
								}
							}
separator_found:
							parameter_len = parameter_end_offset - parameter_offset;
							proto_tree_add_item(sip_element_tree, hf_sip_to_addr, tvb, parameter_offset,
							                    parameter_len, FALSE);
							/*info for the tap for voip_calls.c*/
							stat_info->tap_to_addr=tvb_get_ephemeral_string(tvb, parameter_offset, parameter_len);

							parameter_offset = parameter_end_offset + 1;
							/*
							 * URI parameters ?
							 */
							parameter_end_offset = tvb_find_guint8(tvb, parameter_offset,( line_end_offset - parameter_offset), ';');
							if ( parameter_end_offset == -1)
								parameter_end_offset = line_end_offset;

							offset = parameter_end_offset;
						}
						else
						{
							/* Extract SIP/SIPS URI */
							parameter_offset = value_offset;
							while (parameter_offset < line_end_offset
							       && (tvb_strneql(tvb, parameter_offset, "sip", 3) != 0))
								parameter_offset++;
							len = parameter_offset - value_offset;
							if ( len > 1){
								/* Something in front, must be display info
								 * TODO: Get rid of trailing space(s)
								 */
								proto_tree_add_item(sip_element_tree, hf_sip_display, tvb, value_offset,
								                    len, FALSE);
							}
							parameter_end_offset = tvb_find_guint8(tvb, parameter_offset,
							                                       (line_end_offset - parameter_offset), ';');
							if ( parameter_end_offset == -1)
								parameter_end_offset = line_end_offset;
							parameter_len = parameter_end_offset - parameter_offset;
							proto_tree_add_item(sip_element_tree, hf_sip_to_addr, tvb, parameter_offset,
							                    parameter_len, FALSE);
							/*info for the tap for voip_calls.c*/
							stat_info->tap_to_addr=tvb_get_ephemeral_string(tvb, parameter_offset, parameter_len);
							offset = parameter_end_offset;
						}
						/* Find parameter tag if present.
						 * TODO make this generic to find any interesting parameter
						 * use the same method as for SIP headers ?
						 */

						parameter_offset = offset;
						while (parameter_offset < line_end_offset
						       && (tvb_strneql(tvb, parameter_offset, "tag=", 4) != 0))
							parameter_offset++;
						if ( parameter_offset < line_end_offset ){ /* Tag found */
							parameter_offset = parameter_offset + 4;
							parameter_end_offset = tvb_find_guint8(tvb, parameter_offset,
							                                       (line_end_offset - parameter_offset), ';');
							if ( parameter_end_offset == -1)
								parameter_end_offset = line_end_offset;
							parameter_len = parameter_end_offset - parameter_offset;
							proto_tree_add_item(sip_element_tree, hf_sip_tag, tvb, parameter_offset,
							                    parameter_len, FALSE);

						}
					break;

					case POS_FROM :
						if(hdr_tree) {
							sip_element_item = proto_tree_add_string_format(hdr_tree,
							                   hf_header_array[hf_index], tvb,
							                   offset, next_offset - offset,
							                   value, "%s",
							                   tvb_format_text(tvb, offset, linelen));
							sip_element_tree = proto_item_add_subtree( sip_element_item, ett_sip_element);
						}
						/* See if we have a SIP/SIPS uri enclosed in <>, if so anything in front is
						 * display info.
						 */
						parameter_offset = tvb_find_guint8(tvb, value_offset,value_len, '<');
						if ( parameter_offset != -1){
							len = parameter_offset - value_offset;
							if ( len > 1){
								/* Something in front, must be display info
								 * TODO: Get rid of trailing space(s)
								 */
								proto_tree_add_item(sip_element_tree, hf_sip_display, tvb, value_offset,
								                    len, FALSE);
							}
							parameter_offset ++;
							parameter_end_offset = parameter_offset;
							/* RFC3261 paragraph 20
							 * The Contact, From, and To header fields contain a URI.  If the URI
							 * contains a comma, question mark or semicolon, the URI MUST be
							 * enclosed in angle brackets (< and >).  Any URI parameters are
							 * contained within these brackets.  If the URI is not enclosed in angle
							 * brackets, any semicolon-delimited parameters are header-parameters,
							 * not URI parameters.
							 */
							while (parameter_end_offset < line_end_offset){
								parameter_end_offset++;
								c = tvb_get_guint8(tvb, parameter_end_offset);
								switch (c) {
									case '>':
									case ',':
									case ';':
									case '?':
										goto separator_found2;
									default :
									break;
								}
							}
separator_found2:
							parameter_len = parameter_end_offset - parameter_offset;
							dfilter_store_sip_from_addr(tvb, sip_element_tree,
							                            parameter_offset, parameter_len);
							/*info for the tap for voip_calls.c*/
							stat_info->tap_from_addr=tvb_get_ephemeral_string(tvb, parameter_offset, parameter_len);
							parameter_offset = parameter_end_offset + 1;
							/*
							 * URI parameters ?
							 */
							parameter_end_offset = tvb_find_guint8(tvb, parameter_offset,( line_end_offset - parameter_offset), ';');
							if ( parameter_end_offset == -1)
								parameter_end_offset = line_end_offset;

							offset = parameter_end_offset;
						}
						else
						{
							/* Extract SIP/SIPS URI */
							parameter_offset = value_offset;
							while (parameter_offset < line_end_offset
							       && (tvb_strneql(tvb, parameter_offset, "sip", 3) != 0))
								parameter_offset++;
							len = parameter_offset - value_offset;
							if ( len > 1){
								/* Something in front, must be display info
								 * TODO: Get rid of trailing space(s)
								 */
								proto_tree_add_item(sip_element_tree, hf_sip_display, tvb, value_offset,
								                    len, FALSE);
							}
							parameter_end_offset = tvb_find_guint8(tvb, parameter_offset,
							                                       (line_end_offset - parameter_offset), ';');
							if ( parameter_end_offset == -1)
								parameter_end_offset = line_end_offset;
							parameter_len = parameter_end_offset - parameter_offset;
							proto_tree_add_item(sip_element_tree, hf_sip_from_addr, tvb, parameter_offset,
							                    parameter_len, FALSE);
							/*info for the tap for voip_calls.c*/
							stat_info->tap_from_addr=tvb_get_ephemeral_string(tvb, parameter_offset, parameter_len);
							offset = parameter_end_offset;
						}
						/* Find parameter tag if present.
						 * TODO make this generic to find any interesting parameter
						 * use the same method as for SIP headers ?
						 */

						parameter_offset = offset;
						while (parameter_offset < line_end_offset
						       && (tvb_strneql(tvb, parameter_offset, "tag=", 4) != 0))
							parameter_offset++;
						if ( parameter_offset < line_end_offset ){ /* Tag found */
							parameter_offset = parameter_offset + 4;
							parameter_end_offset = tvb_find_guint8(tvb, parameter_offset,
							                                       (line_end_offset - parameter_offset), ';');
							if ( parameter_end_offset == -1)
								parameter_end_offset = line_end_offset;
							parameter_len = parameter_end_offset - parameter_offset;
							proto_tree_add_item(sip_element_tree, hf_sip_tag, tvb, parameter_offset,
							                    parameter_len, FALSE);

						}
					break;

					case POS_CSEQ :
						/* Store the sequence number */
						cseq_number = atoi(value);
						cseq_number_set = 1;
						stat_info->tap_cseq_number=cseq_number;

						/* Add CSeq  tree */
						if (hdr_tree) {
							ti = proto_tree_add_string_format(hdr_tree,
							                             hf_header_array[hf_index], tvb,
							                             offset, next_offset - offset,
							                             value, "%s",
							                             tvb_format_text(tvb, offset, linelen));
							cseq_tree = proto_item_add_subtree(ti, ett_sip_cseq);
						}

						/* Walk past number and spaces characters to get to start
						   of method name */
						for (sub_value_offset=0; sub_value_offset < (gint)strlen(value); sub_value_offset++)
						{
							if (!isdigit((guchar)value[sub_value_offset]))
							{
								proto_tree_add_uint(cseq_tree, hf_sip_cseq_seq_no,
								                    tvb, value_offset, sub_value_offset,
								                    cseq_number);
								break;
							}
						}

						for (; sub_value_offset < (gint)strlen(value); sub_value_offset++)
						{
							if (isalpha((guchar)value[sub_value_offset]))
							{
								/* Have reached start of method name */
								break;
							}
						}

						if (sub_value_offset == (gint)strlen(value))
						{
							/* Didn't find method name */
							THROW(ReportedBoundsError);
							return offset - orig_offset;
						}

						/* Extract method name from value */
						strlen_to_copy = strlen(value)-sub_value_offset;
						if (strlen_to_copy > MAX_CSEQ_METHOD_SIZE) {
							/* Note the error in the protocol tree */
							if (hdr_tree) {
								proto_tree_add_string_format(hdr_tree,
								                             hf_header_array[hf_index], tvb,
								                             offset, next_offset - offset,
								                             value+sub_value_offset, "%s String too big: %d bytes",
								                             sip_headers[POS_CSEQ].name,
								                             strlen_to_copy);
							}
							THROW(ReportedBoundsError);
							return offset - orig_offset;
						}
						else {
							strncpy(cseq_method, value+sub_value_offset, MIN(strlen_to_copy, MAX_CSEQ_METHOD_SIZE));

							/* Add CSeq method to the tree */
							if (cseq_tree)
							{
								proto_tree_add_item(cseq_tree, hf_sip_cseq_method, tvb,
								                    value_offset + sub_value_offset, strlen_to_copy, FALSE);
							}
						}
					break;

					case POS_RACK :
					{
						int cseq_no_offset;
						int cseq_method_offset;

						/* Add RAck  tree */
						if (hdr_tree) {
							ti = proto_tree_add_string_format(hdr_tree,
							                             hf_header_array[hf_index], tvb,
							                             offset, next_offset - offset,
							                             value, "%s",
							                             tvb_format_text(tvb, offset, linelen));
							rack_tree = proto_item_add_subtree(ti, ett_sip_rack);
						}

						/* RSeq number */
						for (sub_value_offset=0; sub_value_offset < (gint)strlen(value); sub_value_offset++)
						{
							if (!isdigit((guchar)value[sub_value_offset]))
							{
								proto_tree_add_uint(rack_tree, hf_sip_rack_rseq_no,
								                    tvb, value_offset, sub_value_offset,
								                    atoi(value));
								break;
							}
						}

						/* Get to start of CSeq number */
						for ( ; sub_value_offset < (gint)strlen(value); sub_value_offset++)
						{
							if (value[sub_value_offset] != ' ' &&
						        value[sub_value_offset] != '\t')
							{
								break;
							}
						}
						cseq_no_offset = sub_value_offset;

						/* CSeq number */
						for ( ; sub_value_offset < (gint)strlen(value); sub_value_offset++)
						{
							if (!isdigit((guchar)value[sub_value_offset]))
							{
								proto_tree_add_uint(rack_tree, hf_sip_rack_cseq_no,
								                    tvb, value_offset+cseq_no_offset,
								                    sub_value_offset-cseq_no_offset,
								                    atoi(value+cseq_no_offset));
								break;
							}
						}

						/* Get to start of CSeq method name */
						for ( ; sub_value_offset < (gint)strlen(value); sub_value_offset++)
						{
							if (isalpha((guchar)value[sub_value_offset]))
							{
								/* Have reached start of method name */
								break;
							}
						}
						cseq_method_offset = sub_value_offset;

						if (sub_value_offset == (gint)strlen(value))
						{
							/* Didn't find method name */
							THROW(ReportedBoundsError);
							return offset - orig_offset;
						}

						/* Add CSeq method to the tree */
						if (cseq_tree)
						{
							proto_tree_add_item(rack_tree, hf_sip_rack_cseq_method, tvb,
							                    value_offset + sub_value_offset,
							                    strlen(value)-sub_value_offset, FALSE);
						}

						break;
					}

					case POS_CALL_ID :
						/* Store the Call-id */
						strncpy(call_id, value,
						        strlen(value)+1 < MAX_CALL_ID_SIZE ?
						        strlen(value)+1 :
						        MAX_CALL_ID_SIZE);
						stat_info->tap_call_id = ep_strdup(call_id);

						/* Add 'Call-id' string item to tree */
						if(hdr_tree) {
							proto_tree_add_string_format(hdr_tree,
							                             hf_header_array[hf_index], tvb,
							                             offset, next_offset - offset,
							                             value, "%s",
							                             tvb_format_text(tvb, offset, linelen));
						}
					break;

					case POS_EXPIRES :
						if (strcmp(value, "0") == 0)
						{
							expires_is_0 = 1;
						}
						/* Add 'Expires' string item to tree */
						if(hdr_tree) {
							proto_tree_add_uint(hdr_tree,
							                    hf_header_array[hf_index], tvb,
							                    offset, next_offset - offset,
							                    atoi(value));
						}
					break;

					/*
					 * Content-Type is the same as Internet
					 * media type used by other dissectors,
					 * appropriate dissector found by
					 * lookup in "media_type" dissector table.
					 */
					case POS_CONTENT_TYPE :
						if(hdr_tree) {
							proto_tree_add_string_format(hdr_tree,
							                             hf_header_array[hf_index], tvb,
							                             offset, next_offset - offset,
							                             value, "%s",
							                             tvb_format_text(tvb, offset, linelen));
						}
						content_type_len = value_len;
						semi_colon_offset = tvb_find_guint8(tvb, value_offset, value_len, ';');
						/* Content-Type     =  ( "Content-Type" / "c" ) HCOLON media-type
						 * media-type       =  m-type SLASH m-subtype *(SEMI m-parameter)
						 * SEMI    =  SWS ";" SWS ; semicolon
						 * LWS  =  [*WSP CRLF] 1*WSP ; linear whitespace
						 * SWS  =  [LWS] ; sep whitespace
						 */
						if ( semi_colon_offset != -1) {
							gint content_type_end;
							/*
							 * Skip whitespace after the semicolon.
							 */
							parameter_offset = tvb_skip_wsp(tvb, semi_colon_offset +1, value_offset + value_len - (semi_colon_offset +1));
							content_type_end = tvb_skip_wsp_return(tvb, semi_colon_offset-1);
							content_type_len = content_type_end - value_offset;
							content_type_parameter_str_len = value_offset + value_len - parameter_offset;
							content_type_parameter_str = tvb_get_ephemeral_string(tvb, parameter_offset,
							                             content_type_parameter_str_len);
						}
						media_type_str = tvb_get_ephemeral_string(tvb, value_offset, content_type_len);
#if GLIB_MAJOR_VERSION < 2
						media_type_str_lower_case = ep_strdup(media_type_str);
						g_strdown(media_type_str_lower_case);
#else
						media_type_str_lower_case = g_ascii_strdown(media_type_str, -1);
#endif
						/* Debug code
						proto_tree_add_text(hdr_tree, tvb, value_offset,content_type_len,"media_type_str=%s",media_type_str);
						*/
					break;

					case POS_CONTENT_LENGTH :
						content_length = atoi(value);
						if(hdr_tree) {
							proto_tree_add_uint(hdr_tree,
							                    hf_header_array[hf_index], tvb,
							                    offset, next_offset - offset,
							                    content_length);
						}
						break;

					case POS_MAX_FORWARDS :
					case POS_RSEQ :
						if(hdr_tree) {
							proto_tree_add_uint(hdr_tree,
							                    hf_header_array[hf_index], tvb,
							                    offset, next_offset - offset,
							                    atoi(value));
						}
						break;

					case POS_CONTACT :
						if(hdr_tree) {
							sip_element_item = proto_tree_add_string_format(hdr_tree,
							                   hf_header_array[hf_index], tvb,
							                   offset, next_offset - offset,
							                   value, "%s",
							                   tvb_format_text(tvb, offset, linelen));
							sip_element_tree = proto_item_add_subtree( sip_element_item,
							                   ett_sip_element);
						}
						if (strcmp(value, "*") == 0)
						{
							contact_is_star = 1;
							break;
						}

						comma_offset = value_offset;
						while((comma_offset = dissect_sip_contact_item(tvb, pinfo, sip_element_tree, comma_offset, next_offset)) != -1)
						{
							contacts++;
							if(comma_offset == next_offset)
							{
								/* Line End reached: Stop Parsing */
								break;
							}

							if(tvb_get_guint8(tvb, comma_offset) != ',')
							{
								/* Undefined value reached: Stop Parsing */
								break;
							}
							comma_offset++; /* skip comma */
						}
					break;

					case POS_AUTHORIZATION:
					case POS_WWW_AUTHENTICATE:
					case POS_PROXY_AUTHENTICATE:
					case POS_PROXY_AUTHORIZATION:
					case POS_AUTHENTICATION_INFO:
						/* Add tree using whole text of line */
						if (hdr_tree) {
							proto_item *ti;
							/* Add whole line as header tree */
							sip_element_item = proto_tree_add_string_format(hdr_tree,
							                   hf_header_array[hf_index], tvb,
							                   offset, next_offset - offset,
							                   value, "%s",
							                   tvb_format_text(tvb, offset, linelen));
							sip_element_tree = proto_item_add_subtree( sip_element_item,
							                   ett_sip_element);

							/* Set sip.auth as a hidden field/filter */
							ti = proto_tree_add_item(hdr_tree, hf_sip_auth, tvb,
							                         offset, next_offset-offset,
							                         FALSE);
							PROTO_ITEM_SET_HIDDEN(ti);
						}

						/* Parse each individual parameter in the line */
						comma_offset = tvb_pbrk_guint8(tvb, value_offset, line_end_offset - value_offset, " \t\r\n");

						/* Authentication-Info does not begin with the scheme name */
						if (hf_index != POS_AUTHENTICATION_INFO)
						{
							proto_tree_add_item(sip_element_tree, hf_sip_auth_scheme,
							                    tvb, value_offset, comma_offset - value_offset,
							                    FALSE);
						}

						while ((comma_offset = dissect_sip_authorization_item(tvb, sip_element_tree, comma_offset, line_end_offset)) != -1)
						{
							if(comma_offset == line_end_offset)
							{
								/* Line End reached: Stop Parsing */
								break;
							}

							if(tvb_get_guint8(tvb, comma_offset) != ',')
							{
								/* Undefined value reached: Stop Parsing */
								break;
							}
							comma_offset++; /* skip comma */
						}
					break;

					case POS_VIA:
						/* Add Via subtree */
						if (hdr_tree) {
							ti = proto_tree_add_string_format(hdr_tree,
							                             hf_header_array[hf_index], tvb,
							                             offset, next_offset - offset,
							                             value, "%s",
							                             tvb_format_text(tvb, offset, linelen));
							via_tree = proto_item_add_subtree(ti, ett_sip_via);
						}
						dissect_sip_via_header(tvb, via_tree, value_offset, line_end_offset);
						break;
					case POS_REASON:
						if(hdr_tree) {
							ti = proto_tree_add_string_format(hdr_tree,
							                             hf_header_array[hf_index], tvb,
							                             offset, next_offset - offset,
							                             value, "%s",
							                             tvb_format_text(tvb, offset, linelen));
							reason_tree = proto_item_add_subtree(ti, ett_sip_reason);
						}
						dissect_sip_reason_header(tvb, reason_tree, value_offset, line_end_offset);
						break;
					default :
						/* Default case is to assume its an FT_STRING field */
						if(hdr_tree) {
							proto_tree_add_string_format(hdr_tree,
							                             hf_header_array[hf_index], tvb,
							                             offset, next_offset - offset,
							                             value, "%s",
							                             tvb_format_text(tvb, offset, linelen));
						}
					break;
				}/* end switch */
			}/*if HF_index */
		}/* if colon_offset */
		offset = next_offset;
	}/* End while */

	datalen = tvb_length_remaining(tvb, offset);
	reported_datalen = tvb_reported_length_remaining(tvb, offset);
	if (content_length != -1) {
		if (datalen > content_length)
			datalen = content_length;
		if (reported_datalen > content_length)
			reported_datalen = content_length;
	}

	if (datalen > 0) {
		/*
		 * There's a message body starting at "offset".
		 * Set the length of the header item.
		 */
		proto_item_set_end(th, tvb, offset);
		next_tvb = tvb_new_subset(tvb, offset, datalen, reported_datalen);
		if(sip_tree) {
			ti = proto_tree_add_item(sip_tree, hf_sip_msg_body, next_tvb, 0, -1,
			                         FALSE);
			message_body_tree = proto_item_add_subtree(ti, ett_sip_message_body);
		}

		/* give the content type parameters to sub dissectors */

		if ( media_type_str_lower_case != NULL ) {
			void *save_private_data = pinfo->private_data;
			pinfo->private_data = content_type_parameter_str;
			found_match = dissector_try_string(media_type_dissector_table,
			                                   media_type_str_lower_case,
			                                   next_tvb, pinfo,
			                                   message_body_tree);
			pinfo->private_data = save_private_data;
			/* If no match dump as text */
		}
		if ( found_match != TRUE )
		{
            if (!(dissector_try_heuristic(heur_subdissector_list,
                                          next_tvb, pinfo, message_body_tree))) {
                int tmp_offset = 0;
                while (tvb_offset_exists(next_tvb, tmp_offset)) {
                    tvb_find_line_end(next_tvb, tmp_offset, -1, &next_offset, FALSE);
                    linelen = next_offset - tmp_offset;
                    if(message_body_tree) {
                        proto_tree_add_text(message_body_tree, next_tvb,
                                            tmp_offset, linelen, "%s",
                                            tvb_format_text(next_tvb, tmp_offset, linelen));
                        }
                    tmp_offset = next_offset;
                    }/* end while */
			}
		}
		offset += datalen;
	}


	/* Add to info column interesting things learned from header fields. */
	if (check_col(pinfo->cinfo, COL_INFO))
	{
		/* Registration requests */
		if (strcmp(sip_methods[current_method_idx], "REGISTER") == 0)
		{
			if (contact_is_star && expires_is_0)
			{
				col_append_str(pinfo->cinfo, COL_INFO, "    (remove all bindings)");
			}
			else
			if (!contacts)
			{
				col_append_str(pinfo->cinfo, COL_INFO, "    (fetch bindings)");
			}
		}

		/* Registration responses */
		if (line_type == STATUS_LINE && (strcmp(cseq_method, "REGISTER") == 0))
		{
			col_append_fstr(pinfo->cinfo, COL_INFO, "    (%d bindings)", contacts);
		}
	}

	/* Check if this packet is a resend. */
	resend_for_packet = sip_is_packet_resend(pinfo, cseq_method, call_id,
	                                         cseq_number_set, cseq_number,
	                                         line_type);
	/* Mark whether this is a resend for the tap */
	stat_info->resend = (resend_for_packet > 0);

	/* And add the filterable field to the request/response line */
	if (reqresp_tree)
	{
		proto_item *item;
		item = proto_tree_add_boolean(reqresp_tree, hf_sip_resend, tvb, orig_offset, 0,
		                              resend_for_packet > 0);
		PROTO_ITEM_SET_GENERATED(item);
		if (resend_for_packet > 0)
		{
			item = proto_tree_add_uint(reqresp_tree, hf_sip_original_frame,
			                           tvb, orig_offset, 0, resend_for_packet);
			PROTO_ITEM_SET_GENERATED(item);
		}
	}


	if (ts != NULL)
		proto_item_set_len(ts, offset - orig_offset);

	if (global_sip_raw_text)
		tvb_raw_text_add(tvb, orig_offset, offset - orig_offset, tree);

	/* Report this packet to the tap */
	if (!pinfo->in_error_pkt)
	{
		tap_queue_packet(sip_tap, pinfo, stat_info);
	}

	return offset - orig_offset;
}

/* Display filter for SIP Request-Line */
static void
dfilter_sip_request_line(tvbuff_t *tvb, proto_tree *tree, guint meth_len)
{
	char	*string;

	/*
	 * We know we have the entire method; otherwise, "sip_parse_line()"
	 * would have returned OTHER_LINE.
	 */
	string = tvb_get_ephemeral_string(tvb, 0, meth_len);
	if (tree) {
		proto_tree_add_string(tree, hf_Method, tvb, 0, meth_len, string);
	}
	/* Copy request method for telling tap */
	stat_info->request_method = string;
}

/* Display filter for SIP Status-Line */
static void
dfilter_sip_status_line(tvbuff_t *tvb, proto_tree *tree)
{
	char string[3+1];
	gint response_code = 0;

	/*
	 * We know we have the entire status code; otherwise,
	 * "sip_parse_line()" would have returned OTHER_LINE.
	 * We also know that we have a version string followed by a
	 * space at the beginning of the line, for the same reason.
	 */
	tvb_memcpy(tvb, (guint8 *)string, SIP2_HDR_LEN + 1, 3);
	string[3] = '\0';
	response_code = atoi(string);

	/* Add numerical response code to tree */
	if (tree) {
		proto_tree_add_uint(tree, hf_Status_Code, tvb, SIP2_HDR_LEN + 1,
		                    3, response_code);
	}

	/* Add response code for sending to tap */
	stat_info->response_code = response_code;
}

void dfilter_store_sip_from_addr(tvbuff_t *tvb,proto_tree *tree,guint parameter_offset,
					  guint parameter_len)
{
	proto_tree_add_item(tree, hf_sip_from_addr, tvb, parameter_offset,
							parameter_len, FALSE);

}
/* From section 4.1 of RFC 2543:
 *
 * Request-Line  =  Method SP Request-URI SP SIP-Version CRLF
 *
 * From section 5.1 of RFC 2543:
 *
 * Status-Line  =  SIP-version SP Status-Code SP Reason-Phrase CRLF
 *
 * From section 7.1 of RFC 3261:
 *
 * Unlike HTTP, SIP treats the version number as a literal string.
 * In practice, this should make no difference.
 */
static line_type_t
sip_parse_line(tvbuff_t *tvb, int offset, gint linelen, guint *token_1_lenp)
{
	gint space_offset;
	gint token_1_start;
	guint token_1_len;
	gint token_2_start;
	guint token_2_len;
	gint token_3_start;
	guint token_3_len;
	gint colon_pos;

	token_1_start = offset;
	space_offset = tvb_find_guint8(tvb, token_1_start, -1, ' ');
	if ((space_offset == -1) || (space_offset == token_1_start)) {
		/*
		 * Either there's no space in the line (which means
		 * the line is empty or doesn't have a token followed
		 * by a space; neither is valid for a request or status), or
		 * the first character in the line is a space (meaning
		 * the method is empty, which isn't valid for a request,
		 * or the SIP version is empty, which isn't valid for a
		 * status).
		 */
		return OTHER_LINE;
	}
	token_1_len = space_offset - token_1_start;
	token_2_start = space_offset + 1;
	space_offset = tvb_find_guint8(tvb, token_2_start, -1, ' ');
	if (space_offset == -1) {
		/*
		 * There's no space after the second token, so we don't
		 * have a third token.
		 */
		return OTHER_LINE;
	}
	token_2_len = space_offset - token_2_start;
	token_3_start = space_offset + 1;
	token_3_len = token_1_start + linelen - token_3_start;

	*token_1_lenp = token_1_len;

	/*
	 * Is the first token a version string?
	 */
	if ( (strict_sip_version && (
		token_1_len == SIP2_HDR_LEN
		&& tvb_strneql(tvb, token_1_start, SIP2_HDR, SIP2_HDR_LEN) == 0)
	) || (! strict_sip_version && (
		tvb_strncaseeql(tvb, token_1_start, "SIP/", 4) == 0)
	)) {
		/*
		 * Yes, so this is either a Status-Line or something
		 * else other than a Request-Line.  To be a Status-Line,
		 * the second token must be a 3-digit number.
		 */
		if (token_2_len != 3) {
			/*
			 * We don't have 3-character status code.
			 */
			return OTHER_LINE;
		}
		if (!isdigit(tvb_get_guint8(tvb, token_2_start)) ||
		    !isdigit(tvb_get_guint8(tvb, token_2_start + 1)) ||
		    !isdigit(tvb_get_guint8(tvb, token_2_start + 2))) {
			/*
			 * 3 characters yes, 3 digits no.
			 */
			return OTHER_LINE;
		}
		return STATUS_LINE;
	} else {
		/*
		 * No, so this is either a Request-Line or something
		 * other than a Status-Line.  To be a Request-Line, the
		 * second token must be a URI and the third token must
		 * be a version string.
		 */
		if (token_2_len < 3) {
			/*
			 * We don't have a URI consisting of at least 3
			 * characters.
			 */
			return OTHER_LINE;
		}
		colon_pos = tvb_find_guint8(tvb, token_2_start + 1, -1, ':');
		if (colon_pos == -1) {
			/*
			 * There is no colon after the method, so the URI
			 * doesn't have a colon in it, so it's not valid.
			 */
			return OTHER_LINE;
		}
		if (colon_pos >= token_3_start) {
			/*
			 * The colon is in the version string, not the URI.
			 */
			return OTHER_LINE;
		}
		/* XXX - Check for a proper URI prefix? */
		if ( (strict_sip_version && (
			token_3_len != SIP2_HDR_LEN
			|| tvb_strneql(tvb, token_3_start, SIP2_HDR, SIP2_HDR_LEN) == -1)
		) || (! strict_sip_version && (
			tvb_strncaseeql(tvb, token_3_start, "SIP/", 4) == -1)
		)) {
			/*
			 * The version string isn't an SIP version 2.0 version
			 * string.
			 */
			return OTHER_LINE;
		}
		return REQUEST_LINE;
	}
}

static gboolean sip_is_known_request(tvbuff_t *tvb, int meth_offset,
    guint meth_len, guint *meth_idx)
{
        guint i;

        for (i = 1; i < array_length(sip_methods); i++) {
                if (meth_len == strlen(sip_methods[i]) &&
                    tvb_strneql(tvb, meth_offset, sip_methods[i], meth_len) == 0)
                {
                     *meth_idx = i;
                     return TRUE;
                }
        }

        return FALSE;
}

/* Returns index of method in sip_headers */
static gint sip_is_known_sip_header(tvbuff_t *tvb, int offset, guint header_len)
{
        guint i;

        for (i = 1; i < array_length(sip_headers); i++) {
                if (header_len == strlen(sip_headers[i].name) &&
                    tvb_strncaseeql(tvb, offset, sip_headers[i].name, header_len) == 0)
                        return i;
                if (sip_headers[i].compact_name != NULL &&
                    header_len == strlen(sip_headers[i].compact_name) &&
                    tvb_strncaseeql(tvb, offset, sip_headers[i].compact_name, header_len) == 0)
                        return i;
        }

        return -1;
}

/*
 * Display the entire message as raw text.
 */
static void
tvb_raw_text_add(tvbuff_t *tvb, int offset, int length, proto_tree *tree)
{
    proto_tree *raw_tree = NULL;
    proto_item *ti = NULL;
    int next_offset, linelen, end_offset;

    if (tree) {
        ti = proto_tree_add_item(tree, proto_raw_sip, tvb, offset, length, FALSE);
        raw_tree = proto_item_add_subtree(ti, ett_raw_text);
    }

    end_offset = offset + length;

    while (offset < end_offset) {
        tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
        linelen = next_offset - offset;
        if (raw_tree) {
            proto_tree_add_string_format(raw_tree, hf_raw_sip_line, tvb, offset, linelen,
                                         tvb_format_text(tvb, offset, linelen),
                                         "%s",
                                         tvb_format_text(tvb, offset, linelen));
        }
        offset = next_offset;
    }
}

/* Check to see if this packet is a resent request.  Return value is number
   of the original frame this packet seems to be resending (0 = no resend). */
guint sip_is_packet_resend(packet_info *pinfo,
			gchar *cseq_method,
			gchar *call_id,
			guchar cseq_number_set,
			guint32 cseq_number, line_type_t line_type)
{
	guint32 cseq_to_compare = 0;
	sip_hash_key   key;
	sip_hash_key   *p_key = 0;
	sip_hash_value *p_val = 0;
	guint result = 0;

	/* Only consider retransmission of UDP packets */
	if (pinfo->ptype != PT_UDP)
	{
		return 0;
	}

	/* Don't consider packets that appear to be resent only because
	   they are e.g. returned in ICMP unreachable messages. */
	if (pinfo->in_error_pkt)
	{
		return 0;
	}

	/* A broken packet may have no cseq number set. Don't consider it as
	   a resend */
	if (!cseq_number_set)
	{
		return 0;
	}

	/* Return any answer stored from previous dissection */
	if (pinfo->fd->flags.visited)
	{
		return GPOINTER_TO_UINT(p_get_proto_data(pinfo->fd, proto_sip));
	}

	/* No packet entry found, consult global hash table */

	/* Prepare the key */
	strncpy(key.call_id, call_id,
		(strlen(call_id)+1 <= MAX_CALL_ID_SIZE) ?
			strlen(call_id)+1 :
			MAX_CALL_ID_SIZE);
	/*  We're only using these addresses locally (for the hash lookup) so
	 *  there is no need to make a (g_malloc'd) copy of them.
	 */
	SET_ADDRESS(&key.dest_address, pinfo->net_dst.type, pinfo->net_dst.len,
		    pinfo->net_dst.data);
	SET_ADDRESS(&key.source_address, pinfo->net_src.type,
		    pinfo->net_src.len, pinfo->net_src.data);
	key.dest_port = pinfo->destport;
	key.source_port = pinfo->srcport;

	/* Do the lookup */
	p_val = (sip_hash_value*)g_hash_table_lookup(sip_hash, &key);

	if (p_val)
	{
		/* Table entry found, we'll use its value for comparison */
		cseq_to_compare = p_val->cseq;
	}
	else
	{
		/* Need to create a new table entry */

		/* Allocate a new key and value */
		p_key = se_alloc(sizeof(sip_hash_key));
		p_val = se_alloc(sizeof(sip_hash_value));

		/* Just give up if allocations failed */
		if (!p_key || !p_val)
		{
			return 0;
		}

		/* Fill in key and value details */
		g_snprintf(p_key->call_id, MAX_CALL_ID_SIZE, "%s", call_id);
		SE_COPY_ADDRESS(&(p_key->dest_address), &pinfo->net_dst);
		SE_COPY_ADDRESS(&(p_key->source_address), &pinfo->net_src);
		p_key->dest_port = pinfo->destport;
		p_key->source_port = pinfo->srcport;

		p_val->cseq = cseq_number;
		strncpy(p_val->method, cseq_method, MAX_CSEQ_METHOD_SIZE-1);
		p_val->method[MAX_CSEQ_METHOD_SIZE-1] = '\0';
		p_val->transaction_state = nothing_seen;
		p_val->frame_number = 0;

		/* Add entry */
		g_hash_table_insert(sip_hash, p_key, p_val);

		/* Assume have seen no cseq yet */
		cseq_to_compare = 0;
	}


	/******************************************/
	/* Is it a resend???                      */

	/* Does this look like a resent request (discount ACK, CANCEL, or a
	   different method from the original one) ? */

	if ((line_type == REQUEST_LINE) && (cseq_number == cseq_to_compare) &&
	    (p_val->transaction_state == request_seen) &&
	    (strcmp(cseq_method, p_val->method) == 0) &&
	    (strcmp(cseq_method, "ACK") != 0) &&
	    (strcmp(cseq_method, "CANCEL") != 0))
	{
		result = p_val->frame_number;
	}

	/* Does this look like a resent final response ? */
	if ((line_type == STATUS_LINE) && (cseq_number == cseq_to_compare) &&
	    (p_val->transaction_state == final_response_seen) &&
	    (strcmp(cseq_method, p_val->method) == 0) &&
	    (stat_info->response_code >= 200) &&
	    (stat_info->response_code == p_val->response_code))
	{
		result = p_val->frame_number;
	}

	/* Update state for this entry */
	p_val->cseq = cseq_number;

	switch (line_type)
	{
		case REQUEST_LINE:
			p_val->transaction_state = request_seen;
			if (!result)
			{
				p_val->frame_number = pinfo->fd->num;
			}
			break;
		case STATUS_LINE:
			if (stat_info->response_code >= 200)
			{
				p_val->response_code = stat_info->response_code;
				p_val->transaction_state = final_response_seen;
				if (!result)
				{
					p_val->frame_number = pinfo->fd->num;
				}
			}
			else
			{
				p_val->transaction_state = provisional_response_seen;
			}
			break;
		default:
			break;
	}

	/* Store return value with this packet */
	p_add_proto_data(pinfo->fd, proto_sip, GUINT_TO_POINTER(result));

	return result;
}


/* Register the protocol with Wireshark */
void proto_register_sip(void)
{

        /* Setup list of header fields */
        static hf_register_info hf[] = {

		{ &hf_raw_sip_line,
				{ "Raw SIP Line",                "raw_sip.line",
					FT_STRING, BASE_NONE,NULL,0x0,
                       "Raw SIP Line", HFILL }
                },
		{ &hf_msg_hdr,
				{ "Message Header",           "sip.msg_hdr",
                        FT_NONE, 0, NULL, 0,
                        "Message Header in SIP message", HFILL }
                },
		{ &hf_Method,
		       { "Method", 		"sip.Method",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"SIP Method", HFILL }
		},
		{ &hf_Request_Line,
				{ "Request-Line",                "sip.Request-Line",
					FT_STRING, BASE_NONE,NULL,0x0,
                       "SIP Request-Line", HFILL }
                },
		{ &hf_Status_Code,
		       { "Status-Code", 		"sip.Status-Code",
		       FT_UINT32, BASE_DEC,NULL,0x0,
			"SIP Status Code", HFILL }
		},
		{ &hf_Status_Line,
		       { "Status-Line",                 "sip.Status-Line",
		       FT_STRING, BASE_NONE,NULL,0x0,
                       "SIP Status-Line", HFILL }
                },
		{ &hf_sip_display,
			{ "SIP Display info", 		"sip.display.info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Display info", HFILL }
		},
		{ &hf_sip_to_addr,
				{ "SIP to address", 		"sip.to.addr",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: to addr", HFILL }
		},
		{ &hf_sip_from_addr,
		       { "SIP from address", 		"sip.from.addr",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: from addr", HFILL }
		},
		{ &hf_sip_contact_addr,
		       { "SIP contact address", 	"sip.contact.addr",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: contact addr", HFILL }
		},
		{ &hf_sip_uri,
				{ "URI", 		"sip.uri",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: SIP Uri", HFILL }
		},
		{ &hf_sip_contact_item,
		       { "Contact Binding", 		"sip.contact.binding",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: one contact binding", HFILL }
		},
		{ &hf_sip_tag,
		       { "SIP tag", 		"sip.tag",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: tag", HFILL }
		},
		{ &hf_header_array[POS_ACCEPT],
		       { "Accept", 		"sip.Accept",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Accept Header", HFILL }
		},
		{ &hf_header_array[POS_ACCEPT_CONTACT],
		       { "Accept-Contact", 		"sip.Accept-Contact",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3841: Accept-Contact Header", HFILL }
		},
		{ &hf_header_array[POS_ACCEPT_ENCODING],
		       { "Accept-Encoding", 		"sip.Accept-Encoding",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3841: Accept-Encoding Header", HFILL }
		},
		{ &hf_header_array[POS_ACCEPT_LANGUAGE],
		       { "Accept-Language", 		"sip.Accept-Language",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Accept-Language Header", HFILL }
		},
		{ &hf_header_array[POS_ACCEPT_RESOURCE_PRIORITY],
		       { "Accept-Resource-Priority", 		"sip.Accept-Resource-Priority",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Draft: Accept-Resource-Priority Header", HFILL }
		},
		{ &hf_header_array[POS_ALERT_INFO],
		       { "Alert-Info", 		"sip.Alert-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Alert-Info Header", HFILL }
		},
                { &hf_header_array[POS_ALLOW],
		       { "Allow", 		"sip.Allow",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Allow Header", HFILL }
		},
                { &hf_header_array[POS_ALLOW_EVENTS],
		       { "Allow-Events", 		"sip.Allow-Events",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3265: Allow-Events Header", HFILL }
		},
                { &hf_header_array[POS_AUTHENTICATION_INFO],
		       { "Authentication-Info", 		"sip.Authentication-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Authentication-Info Header", HFILL }
		},
                { &hf_header_array[POS_AUTHORIZATION],
		       { "Authorization", 		"sip.Authorization",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Authorization Header", HFILL }
		},
                { &hf_header_array[POS_CALL_ID],
		       { "Call-ID", 		"sip.Call-ID",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Call-ID Header", HFILL }
		},
                { &hf_header_array[POS_CALL_INFO],
		       { "Call-Info", 		"sip.Call-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Call-Info Header", HFILL }
		},
                { &hf_header_array[POS_CONTACT],
		       { "Contact", 		"sip.Contact",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Contact Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_DISPOSITION],
		       { "Content-Disposition", 		"sip.Content-Disposition",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Content-Disposition Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_ENCODING],
		       { "Content-Encoding", 		"sip.Content-Encoding",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Content-Encoding Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_LANGUAGE],
		       { "Content-Language", 		"sip.Content-Language",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Content-Language Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_LENGTH],
		       { "Content-Length", 		"sip.Content-Length",
		       FT_UINT32, BASE_DEC,NULL,0x0,
			"RFC 3261: Content-Length Header", HFILL }
		},
                { &hf_header_array[POS_CONTENT_TYPE],
		       { "Content-Type", 		"sip.Content-Type",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Content-Type Header", HFILL }
		},
                { &hf_header_array[POS_CSEQ],
		       { "CSeq", 		"sip.CSeq",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: CSeq Header", HFILL }
		},
                { &hf_header_array[POS_DATE],
		       { "Date", 		"sip.Date",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Date Header", HFILL }
		},
                { &hf_header_array[POS_ERROR_INFO],
		       { "Error-Info", 		"sip.Error-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Error-Info Header", HFILL }
		},
                { &hf_header_array[POS_EVENT],
		       { "Event", 		"sip.Event",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3265: Event Header", HFILL }
		},
                { &hf_header_array[POS_EXPIRES],
		       { "Expires", 		"sip.Expires",
		       FT_UINT32, BASE_DEC,NULL,0x0,
			"RFC 3261: Expires Header", HFILL }
		},
                { &hf_header_array[POS_FROM],
		       { "From", 		"sip.From",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: From Header", HFILL }
		},
                { &hf_header_array[POS_IN_REPLY_TO],
		       { "In-Reply-To", 		"sip.In-Reply-To",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: In-Reply-To Header", HFILL }
		},
                { &hf_header_array[POS_JOIN],
		       { "Join", 		"sip.Join",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Draft: Join Header", HFILL }
		},
                { &hf_header_array[POS_MAX_FORWARDS],
		       { "Max-Forwards", 		"sip.Max-Forwards",
		       FT_UINT32, BASE_DEC,NULL,0x0,
			"RFC 3261: Max-Forwards Header", HFILL }
		},
                { &hf_header_array[POS_MIME_VERSION],
		       { "MIME-Version", 		"sip.MIME-Version",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: MIME-Version Header", HFILL }
		},
                { &hf_header_array[POS_MIN_EXPIRES],
		       { "Min-Expires", 		"sip.Min-Expires",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Min-Expires Header", HFILL }
		},
                { &hf_header_array[POS_MIN_SE],
		       { "Min-SE", 		"sip.Min-SE",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Draft: Min-SE Header", HFILL }
		},
                { &hf_header_array[POS_ORGANIZATION],
		       { "Organization", 		"sip.Organization",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Organization Header", HFILL }
		},
		{ &hf_header_array[POS_P_ACCESS_NETWORK_INFO],
		       { "P-Access-Network-Info",	"sip.P-Access-Network-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Access-Network-Info Header", HFILL }
		},

		{ &hf_header_array[POS_P_ASSERTED_IDENTITY],
		       { "P-Asserted-Identity",		"sip.P-Asserted-Identity",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3325: P-Asserted-Identity Header", HFILL }
		},
		{ &hf_header_array[POS_P_ANSWER_STATE],
		       { "P-Answer-State",		"sip.P-Answer-State",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Answer-State Header", HFILL }
		},
		{ &hf_header_array[POS_P_ASSOCIATED_URI],
		       { "P-Associated-URI", 		"sip.P-Associated-URI",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3455: P-Associated-URI Header", HFILL }
		},

		{ &hf_header_array[POS_P_CALLED_PARTY_ID],
		       { "P-Called-Party-ID", 		"sip.P-Called-Party-ID",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3455: P-Called-Party-ID Header", HFILL }
		},

		{ &hf_header_array[POS_P_CHARGING_FUNCTION_ADDRESSES],
		       { "P-Charging-Function-Addresses","sip.P-Charging-Function-Addresses",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Charging-Function-Addresses", HFILL }
		},

		{ &hf_header_array[POS_P_CHARGING_VECTOR],
		       { "P-Charging-Vector", 		"sip.P-Charging-Vector",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Charging-Vector Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_TRACE_PARTY_ID],
		       { "P-DCS-Trace-Party-ID", 	"sip.P-DCS-Trace-Party-ID",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-Trace-Party-ID Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_OSPS],
		       { "P-DCS-OSPS", 			"sip.P-DCS-OSPS",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-OSPS Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_BILLING_INFO],
		       { "P-DCS-Billing-Info", 		"sip.P-DCS-Billing-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-Billing-Info Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_LAES],
		       { "P-DCS-LAES", 			"sip.P-DCS-LAES",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-LAES Header", HFILL }
		},

		{ &hf_header_array[POS_P_DCS_REDIRECT],
		       { "P-DCS-Redirect", 		"sip.P-DCS-Redirect",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-DCS-Redirect Header", HFILL }
		},

		{ &hf_header_array[POS_P_EARLY_MEDIA],
		       { "P-Early-Media", 		"sip.P-Early-Media",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Early-Media Header", HFILL }
		},

		{ &hf_header_array[POS_P_MEDIA_AUTHORIZATION],
		       { "P-Media-Authorization", 	"sip.P-Media-Authorization",
		       FT_STRING, BASE_NONE,NULL,0x0,
			   "RFC 3313: P-Media-Authorization Header", HFILL }
		},

		{ &hf_header_array[POS_P_PREFERRED_IDENTITY],
		       { "P-Preferred-Identity",  	"sip.P-Preferred-Identity",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3325: P-Preferred-Identity Header", HFILL }
		},

		{ &hf_header_array[POS_P_PROFILE_KEY],
		       { "P-Profile-Key",  	"sip.P-Profile-Key",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-Profile-Key Header", HFILL }
		},

		{ &hf_header_array[POS_P_USER_DATABASE],
		       { "P-User-Database",  	"sip.P-User-Database",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"P-User-Database Header", HFILL }
		},

		{ &hf_header_array[POS_P_VISITED_NETWORK_ID],
		       { "P-Visited-Network-ID", 	"sip.P-Visited-Network-ID",
		       FT_STRING, BASE_NONE,NULL,0x0,
			   "RFC 3455: P-Visited-Network-ID Header", HFILL }
		},

		{ &hf_header_array[POS_PATH],
		       { "Path", 			"sip.Path",
		       FT_STRING, BASE_NONE,NULL,0x0,
			   "RFC 3327: Path Header", HFILL }
		},

        { &hf_header_array[POS_PRIORITY],
		       { "Priority", 		"sip.Priority",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Priority Header", HFILL }
		},
		{ &hf_header_array[POS_PRIVACY],
		       { "Privacy", 			"sip.Privacy",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Privacy Header", HFILL }
		},

        { &hf_header_array[POS_PROXY_AUTHENTICATE],
		       { "Proxy-Authenticate", 		"sip.Proxy-Authenticate",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Proxy-Authenticate Header", HFILL }
		},
                { &hf_header_array[POS_PROXY_AUTHORIZATION],
		       { "Proxy-Authorization", 		"sip.Proxy-Authorization",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Proxy-Authorization Header", HFILL }
		},

        { &hf_header_array[POS_PROXY_REQUIRE],
		       { "Proxy-Require", 		"sip.Proxy-Require",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Proxy-Require Header", HFILL }
		},
		{ &hf_header_array[POS_RACK],
		       { "RAck", 		"sip.RAck",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3262: RAck Header", HFILL }
		},
		{ &hf_header_array[POS_REASON],
		       { "Reason", 			"sip.Reason",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3326 Reason Header", HFILL }
		},
		{ &hf_header_array[POS_RECORD_ROUTE],
		       { "Record-Route", 		"sip.Record-Route",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Record-Route Header", HFILL }
		},
		{ &hf_header_array[POS_REFER_SUB],
		       { "Refer-Sub", 		"sip.Refer-Sub",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 4488: Refer-Sub Header", HFILL }
		},
		{ &hf_header_array[POS_REFERED_BY],
		       { "Refered By", 		"sip.Refered-by",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3892: Refered-by Header", HFILL }
		},
		{ &hf_header_array[POS_REJECT_CONTACT],
			{ "Reject-Contact", 		"sip.Reject-Contact",
			FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3841: Reject-Contact Header", HFILL }
		},
		{ &hf_header_array[POS_REPLACES],
			{ "Replaces", 		"sip.Replaces",
			FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3891: Replaces Header", HFILL }
		},
		{ &hf_header_array[POS_REPLY_TO],
		       { "Reply-To", 		"sip.Reply-To",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Reply-To Header", HFILL }
		},
		{ &hf_header_array[POS_REQUEST_DISPOSITION],
		       { "Request-Disposition", 	"sip.Request-Disposition",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3841: Request-Disposition Header", HFILL }
		},
		{ &hf_header_array[POS_REQUIRE],
			{ "Require", 		"sip.Require",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Require Header", HFILL }
		},
		{ &hf_header_array[POS_RESOURCE_PRIORITY],
			{ "Resource-Priority", 		"sip.Resource-Priority",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"Draft: Resource-Priority Header", HFILL }
		},
		{ &hf_header_array[POS_RETRY_AFTER],
			{ "Retry-After", 		"sip.Retry-After",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Retry-After Header", HFILL }
		},
		{ &hf_header_array[POS_ROUTE],
		       { "Route", 		"sip.Route",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Route Header", HFILL }
		},
        { &hf_header_array[POS_RSEQ],
		       { "RSeq", 		"sip.RSeq",
		       FT_UINT32, BASE_DEC,NULL,0x0,
			"RFC 3262: RSeq Header", HFILL }
		},
		{ &hf_header_array[ POS_SECURITY_CLIENT],
		       { "Security-Client", 		"sip.Security-Client",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3329 Security-Client Header", HFILL }
		},
		{ &hf_header_array[ POS_SECURITY_SERVER],
		       { "Security-Server", 		"sip.Security-Server",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3329 Security-Server Header", HFILL }
		},
		{ &hf_header_array[ POS_SECURITY_VERIFY],
		       { "Security-Verify", 		"sip.Security-Verify",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3329 Security-Verify Header", HFILL }
		},
		{ &hf_header_array[POS_SERVER],
			{ "Server", 		"sip.Server",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Server Header", HFILL }
		},
		{ &hf_header_array[POS_SERVICE_ROUTE],
		       { "Service-Route", 		"sip.Service-Route",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3608: Service-Route Header", HFILL }
		},
		{ &hf_header_array[POS_SESSION_EXPIRES],
		       { "Session-Expires", 		"sip.Session-Expires",
		       FT_STRING, BASE_NONE,NULL,0x0,
			   "RFC 4028: Session-Expires Header", HFILL }
		},
		{ &hf_header_array[POS_SIP_ETAG],
		       { "ETag", 		"sip.ETag",
		       FT_STRING, BASE_NONE,NULL,0x0,
			   "RFC 3903: SIP-ETag Header", HFILL }
		},
		{ &hf_header_array[POS_SIP_IF_MATCH],
		       { "If_Match", 		"sip.If_Match",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3903: SIP-If-Match Header", HFILL }
		},
		{ &hf_header_array[POS_SUBJECT],
		       { "Subject", 		"sip.Subject",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Subject Header", HFILL }
		},
		{ &hf_header_array[POS_SUBSCRIPTION_STATE],
		       { "Subscription-State", 		"sip.Subscription-State",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3265: Subscription-State Header", HFILL }
		},
		{ &hf_header_array[POS_SUPPORTED],
			{ "Supported", 		"sip.Supported",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Supported Header", HFILL }
		},
		{ &hf_header_array[POS_TARGET_DALOG],
			{ "Target-Dialog", 		"sip.Target-Dialog",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 4538: Target-Dialog Header", HFILL }
		},
		{ &hf_header_array[POS_TIMESTAMP],
			{ "Timestamp", 		"sip.Timestamp",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Timestamp Header", HFILL }
		},
		{ &hf_header_array[POS_TO],
			{ "To", 		"sip.To",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: To Header", HFILL }
		},

		{ &hf_header_array[POS_UNSUPPORTED],
			{ "Unsupported", 		"sip.Unsupported",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Unsupported Header", HFILL }
		},
		{ &hf_header_array[POS_USER_AGENT],
			{ "User-Agent", 		"sip.User-Agent",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: User-Agent Header", HFILL }
		},
		{ &hf_header_array[POS_VIA],
			{ "Via", 		"sip.Via",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Via Header", HFILL }
		},
		{ &hf_header_array[POS_WARNING],
			{ "Warning", 		"sip.Warning",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: Warning Header", HFILL }
		},

		{ &hf_header_array[POS_WWW_AUTHENTICATE],
			{ "WWW-Authenticate", 		"sip.WWW-Authenticate",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3261: WWW-Authenticate Header", HFILL }
		},
		{ &hf_header_array[POS_REFER_TO],
			{ "Refer-To", 			"sip.Refer-To",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 3515: Refer-To Header", HFILL }
		},
		{ &hf_header_array[POS_HISTORY_INFO],
			{ "History-Info", 			"sip.History-Info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 4244: Request History Information", HFILL }
		},
		{ &hf_header_array[POS_IDENTITY],
			{ "Identity", 			"sip.Identity",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 4474: Request Identity", HFILL }
		},
		{ &hf_header_array[POS_IDENTITY_INFO],
			{ "Identity-info", 			"sip.Identity-info",
		       FT_STRING, BASE_NONE,NULL,0x0,
			"RFC 4474: Request Identity-info", HFILL }
		},

		{ &hf_sip_resend,
			{ "Resent Packet", "sip.resend",
			FT_BOOLEAN, BASE_NONE, NULL, 0x0,
			"", HFILL }
		},
		{ &hf_sip_original_frame,
			{ "Suspected resend of frame",  "sip.resend-original",
			FT_FRAMENUM, BASE_NONE, NULL, 0x0,
		    	"Original transmission of frame", HFILL}
		},
		{ &hf_sip_auth,
			{ "Authentication",  "sip.auth",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication", HFILL}
		},
		{ &hf_sip_auth_scheme,
			{ "Authentication Scheme",  "sip.auth.scheme",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Scheme", HFILL}
		},
		{ &hf_sip_auth_digest_response,
			{ "Digest Authentication Response",  "sip.auth.digest.response",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Digest Authentication Response Value ", HFILL}
		},
		{ &hf_sip_auth_nc,
			{ "Nonce Count",  "sip.auth.nc",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Nonce count", HFILL}
		},
		{ &hf_sip_auth_username,
			{ "Username",  "sip.auth.username",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Username", HFILL}
		},
		{ &hf_sip_auth_realm,
			{ "Realm",  "sip.auth.realm",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Realm", HFILL}
		},
		{ &hf_sip_auth_nonce,
			{ "Nonce Value",  "sip.auth.nonce",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Nonce", HFILL}
		},
		{ &hf_sip_auth_algorithm,
			{ "Algorithm",  "sip.auth.algorithm",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Algorithm", HFILL}
		},
		{ &hf_sip_auth_opaque,
			{ "Opaque Value",  "sip.auth.opaque",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Opaque value", HFILL}
		},
		{ &hf_sip_auth_qop,
			{ "QOP",  "sip.auth.qop",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication QOP", HFILL}
		},
		{ &hf_sip_auth_cnonce,
			{ "CNonce Value",  "sip.auth.cnonce",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Client Nonce", HFILL}
		},
		{ &hf_sip_auth_uri,
			{ "Authentication URI",  "sip.auth.uri",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication URI", HFILL}
		},
		{ &hf_sip_auth_domain,
			{ "Authentication Domain",  "sip.auth.domain",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Domain", HFILL}
		},
		{ &hf_sip_auth_stale,
			{ "Stale Flag",  "sip.auth.stale",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Stale Flag", HFILL}
		},
		{ &hf_sip_auth_auts,
			{ "Authentication Token",  "sip.auth.auts",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Token", HFILL}
		},
		{ &hf_sip_auth_rspauth,
			{ "Response auth",  "sip.auth.rspauth",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Response auth", HFILL}
		},
		{ &hf_sip_auth_nextnonce,
			{ "Next Nonce",  "sip.auth.nextnonce",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Next Nonce", HFILL}
		},
		{ &hf_sip_auth_ik,
			{ "Integrity Key",  "sip.auth.ik",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Integrity Key", HFILL}
		},
		{ &hf_sip_auth_ck,
			{ "Cyphering Key",  "sip.auth.ck",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Authentication Cyphering Key", HFILL}
		},
		{ &hf_sip_cseq_seq_no,
			{ "Sequence Number",  "sip.CSeq.seq",
			FT_UINT32, BASE_DEC, NULL, 0x0,
		    	"CSeq header sequence number", HFILL}
		},
		{ &hf_sip_cseq_method,
			{ "Method",  "sip.CSeq.method",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"CSeq header method", HFILL}
		},
		{ &hf_sip_via_transport,
			{ "Transport",  "sip.Via.transport",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"Via header Transport", HFILL}
		},
		{ &hf_sip_via_sent_by_address,
			{ "Sent-by Address",  "sip.Via.sent-by.address",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"Via header Sent-by Address", HFILL}
		},
		{ &hf_sip_via_sent_by_port,
			{ "Sent-by port",  "sip.Via.sent-by.port",
			FT_UINT16, BASE_DEC, NULL, 0x0,
		    	"Via header Sent-by Port", HFILL}
		},
		{ &hf_sip_via_branch,
			{ "Branch",  "sip.Via.branch",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Via Branch", HFILL},
		},
		{ &hf_sip_via_maddr,
			{ "Maddr",  "sip.Via.maddr",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Via Maddr", HFILL},
		},
		{ &hf_sip_via_rport,
			{ "RPort",  "sip.Via.rport",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Via RPort", HFILL},
		},
		{ &hf_sip_via_received,
			{ "Received",  "sip.Via.received",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Via Received", HFILL},
		},
		{ &hf_sip_via_ttl,
			{ "TTL",  "sip.Via.ttl",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Via TTL", HFILL}
		},
		{ &hf_sip_via_comp,
			{ "Comp",  "sip.Via.comp",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Via comp", HFILL}
		},
		{ &hf_sip_via_sigcomp_id,
			{ "Sigcomp identifier",  "sip.Via.sigcomp-id",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"SIP Via sigcomp identifier", HFILL}
		},
		{ &hf_sip_rack_rseq_no,
			{ "RSeq Sequence Number",  "sip.RAck.RSeq.seq",
			FT_UINT32, BASE_DEC, NULL, 0x0,
		    	"RAck RSeq header sequence number (from prov response)", HFILL}
		},
		{ &hf_sip_rack_cseq_no,
			{ "CSeq Sequence Number",  "sip.RAck.CSeq.seq",
			FT_UINT32, BASE_DEC, NULL, 0x0,
		    	"RAck CSeq header sequence number (from prov response)", HFILL}
		},
		{ &hf_sip_rack_cseq_method,
			{ "CSeq Method",  "sip.RAck.CSeq.method",
			FT_STRING, BASE_NONE, NULL, 0x0,
		    	"RAck CSeq header method (from prov response)", HFILL}
		},
		{ &hf_sip_msg_body,
				{ "Message Body",           "sip.msg_body",
				FT_NONE, BASE_NONE, NULL, 0x0,
				"Message Body in SIP message", HFILL }
		}};


	/* Setup protocol subtree array */
	static gint *ett[] = {
		&ett_sip,
		&ett_sip_reqresp,
		&ett_sip_hdr,
		&ett_sip_element,
		&ett_sip_uri,
		&ett_sip_contact_item,
		&ett_sip_message_body,
		&ett_sip_cseq,
		&ett_sip_via,
		&ett_sip_reason,
		&ett_sip_rack
	};
	static gint *ett_raw[] = {
		&ett_raw_text,
	};

	module_t *sip_module;

	/* Register the protocol name and description */
	proto_sip = proto_register_protocol("Session Initiation Protocol",
	                                    "SIP", "sip");
	proto_raw_sip = proto_register_protocol("Session Initiation Protocol (SIP as raw text)",
	                                        "Raw_SIP", "raw_sip");
	new_register_dissector("sip", dissect_sip, proto_sip);
	sip_handle = find_dissector("sip");
	register_dissector("sip.tcp", dissect_sip_tcp, proto_sip);
	sip_tcp_handle = find_dissector("sip.tcp");

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

	/* SIP content type and internet media type used by other dissectors are the same */
	media_type_dissector_table = find_dissector_table("media_type");

	sip_module = prefs_register_protocol(proto_sip, proto_reg_handoff_sip);

  prefs_register_uint_preference(sip_module, "tcp.port",
                                 "SIP TCP Port",
                                 "SIP Server TCP Port",
                                 10, &sip_tcp_port);
  prefs_register_uint_preference(sip_module, "tls.port",
                                 "SIP TLS Port",
                                 "SIP Server TLS Port",
                                 10, &sip_tls_port);

	prefs_register_bool_preference(sip_module, "display_raw_text",
		"Display raw text for SIP message",
		"Specifies that the raw text of the "
		"SIP message should be displayed "
		"in addition to the dissection tree",
		&global_sip_raw_text);
	prefs_register_bool_preference(sip_module, "strict_sip_version",
		"Enforce strict SIP version check (" SIP2_HDR ")",
		"If enabled, only " SIP2_HDR " traffic will be dissected as SIP. "
		"Disable it to allow SIP traffic with a different version "
		"to be dissected as SIP.",
		&strict_sip_version);
	prefs_register_bool_preference(sip_module, "desegment_headers",
	    "Reassemble SIP headers spanning multiple TCP segments",
	    "Whether the SIP dissector should reassemble headers "
	    "of a request spanning multiple TCP segments. "
		"To use this option, you must also enable "
        "\"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
	    &sip_desegment_headers);
	prefs_register_bool_preference(sip_module, "desegment_body",
	    "Reassemble SIP bodies spanning multiple TCP segments",
	    "Whether the SIP dissector should use the "
	    "\"Content-length:\" value, if present, to reassemble "
	    "the body of a request spanning multiple TCP segments, "
	    "and reassemble chunked data spanning multiple TCP segments. "
		"To use this option, you must also enable "
        "\"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
	    &sip_desegment_body);

	register_init_routine(&sip_init_protocol);
    register_heur_dissector_list("sip", &heur_subdissector_list);
	/* Register for tapping */
	sip_tap = register_tap("sip");
}

void
proto_reg_handoff_sip(void)
{
  static gboolean sip_prefs_initialized = FALSE;

  if (sip_prefs_initialized) {
    dissector_delete("tcp.port", saved_sip_tcp_port, sip_tcp_handle);
    ssl_dissector_delete(saved_sip_tls_port, "sip.tcp", TRUE);
  } else {
    sip_prefs_initialized = TRUE;
  }
  /* Set our port number for future use */
  saved_sip_tcp_port = sip_tcp_port;
  dissector_add("tcp.port", saved_sip_tcp_port, sip_tcp_handle);
  saved_sip_tls_port = sip_tls_port;
  ssl_dissector_add(saved_sip_tls_port, "sip.tcp", TRUE);

	dissector_add("udp.port", UDP_PORT_SIP, sip_handle);
	dissector_add_string("media_type", "message/sip", sip_handle);
	sigcomp_handle = find_dissector("sigcomp");

	heur_dissector_add("udp", dissect_sip_heur, proto_sip);
	heur_dissector_add("tcp", dissect_sip_tcp_heur, proto_sip);
	heur_dissector_add("sctp", dissect_sip_heur, proto_sip);
}