aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-sip.c
blob: 9870626a3578934349d045c7c60b008b8c4dcf73 (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
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
/* packet-sip.c
 * Routines for the Session Initiation Protocol (SIP) dissection.
 * RFCs 3261-3264
 *
 * TODO:
 *      hf_ display filters for headers of SIP extension RFCs (ongoing)
 *
 * Copyright 2000, Heikki Vatiainen <hessu@cs.tut.fi>
 * Copyright 2001, Jean-Francois Mule <jfm@cablelabs.com>
 * Copyright 2004, Anders Broman <anders.broman@ericsson.com>
 * Copyright 2011, Anders Broman <anders.broman@ericsson.com>, Johan Wahl <johan.wahl@ericsson.com>
 * Copyright 2018, Anders Broman <anders.broman@ericsson.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 */

#include "config.h"

#include <epan/packet.h>

#include <epan/exceptions.h>
#include <epan/exported_pdu.h>
#include <epan/expert.h>
#include <epan/prefs.h>
#include <epan/req_resp_hdrs.h>
#include <epan/stat_tap_ui.h>
#include <epan/tap.h>
#include <epan/proto_data.h>
#include <epan/uat.h>
#include <epan/strutil.h>
#include <epan/to_str.h>

#include <wsutil/str_util.h>
#include <wsutil/strtoi.h>
#include <wsutil/wsgcrypt.h>

#include "packet-ssl.h"

#include "packet-isup.h"
#include "packet-e164.h"
#include "packet-sip.h"

#include "packet-http.h"

#include "packet-sdp.h"  /* SDP needs a transport layer to determine request/response */

/* un-comment the following as well as this line in conversation.c, to enable debug printing */
/* #define DEBUG_CONVERSATION */
#include "conversation_debug.h"

#define TLS_PORT_SIP 5061
#define DEFAULT_SIP_PORT_RANGE "5060"

void proto_register_sip(void);

static gint sip_tap = -1;
static gint exported_pdu_tap = -1;
static dissector_handle_t sip_handle;
static dissector_handle_t sip_tcp_handle;
static dissector_handle_t sigcomp_handle;
static dissector_handle_t sip_diag_handle;
static dissector_handle_t sip_uri_userinfo_handle;
static dissector_handle_t sip_via_branch_handle;
/* Dissector to dissect the text part of an reason code */
static dissector_handle_t sip_reason_code_handle;

/* Initialize the protocol and registered fields */
static gint proto_sip                     = -1;
static gint proto_raw_sip                 = -1;
static gint hf_sip_raw_line               = -1;
static gint hf_sip_msg_hdr                = -1;
static gint hf_sip_Method                 = -1;
static gint hf_Request_Line               = -1;
static gint hf_sip_ruri                   = -1;
static gint hf_sip_ruri_user              = -1;
static gint hf_sip_ruri_host              = -1;
static gint hf_sip_ruri_port              = -1;
static gint hf_sip_ruri_param             = -1;
static gint hf_sip_Status_Code            = -1;
static gint hf_sip_Status_Line            = -1;
static gint hf_sip_display                = -1;
static gint hf_sip_to_addr                = -1;
static gint hf_sip_to_user                = -1;
static gint hf_sip_to_host                = -1;
static gint hf_sip_to_port                = -1;
static gint hf_sip_to_param               = -1;
static gint hf_sip_to_tag                 = -1;
static gint hf_sip_from_addr              = -1;
static gint hf_sip_from_user              = -1;
static gint hf_sip_from_host              = -1;
static gint hf_sip_from_port              = -1;
static gint hf_sip_from_param             = -1;
static gint hf_sip_from_tag               = -1;
static gint hf_sip_tag                    = -1;
static gint hf_sip_pai_addr               = -1;
static gint hf_sip_pai_user               = -1;
static gint hf_sip_pai_host               = -1;
static gint hf_sip_pai_port               = -1;
static gint hf_sip_pai_param              = -1;
static gint hf_sip_pmiss_addr             = -1;
static gint hf_sip_pmiss_user             = -1;
static gint hf_sip_pmiss_host             = -1;
static gint hf_sip_pmiss_port             = -1;
static gint hf_sip_pmiss_param            = -1;
static gint hf_sip_ppi_addr               = -1;
static gint hf_sip_ppi_user               = -1;
static gint hf_sip_ppi_host               = -1;
static gint hf_sip_ppi_port               = -1;
static gint hf_sip_ppi_param              = -1;
static gint hf_sip_tc_addr                = -1;
static gint hf_sip_tc_user                = -1;
static gint hf_sip_tc_host                = -1;
static gint hf_sip_tc_port                = -1;
static gint hf_sip_tc_param               = -1;
static gint hf_sip_tc_turi                = -1;
static gint hf_sip_contact_param          = -1;
static gint hf_sip_resend                 = -1;
static gint hf_sip_original_frame         = -1;
static gint hf_sip_matching_request_frame = -1;
static gint hf_sip_response_time          = -1;
static gint hf_sip_release_time           = -1;
static gint hf_sip_curi                   = -1;
static gint hf_sip_curi_user              = -1;
static gint hf_sip_curi_host              = -1;
static gint hf_sip_curi_port              = -1;
static gint hf_sip_curi_param             = -1;
static gint hf_sip_route                  = -1;
static gint hf_sip_route_user             = -1;
static gint hf_sip_route_host             = -1;
static gint hf_sip_route_port             = -1;
static gint hf_sip_route_param            = -1;
static gint hf_sip_record_route           = -1;
static gint hf_sip_record_route_user      = -1;
static gint hf_sip_record_route_host      = -1;
static gint hf_sip_record_route_port      = -1;
static gint hf_sip_record_route_param     = -1;
static gint hf_sip_service_route          = -1;
static gint hf_sip_service_route_user     = -1;
static gint hf_sip_service_route_host     = -1;
static gint hf_sip_service_route_port     = -1;
static gint hf_sip_service_route_param    = -1;
static gint hf_sip_path                   = -1;
static gint hf_sip_path_user              = -1;
static gint hf_sip_path_host              = -1;
static gint hf_sip_path_port              = -1;
static gint hf_sip_path_param             = -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_via_oc                 = -1;
static gint hf_sip_via_oc_val             = -1;
static gint hf_sip_via_oc_algo            = -1;
static gint hf_sip_via_oc_validity        = -1;
static gint hf_sip_via_oc_seq             = -1;
static gint hf_sip_oc_seq_timestamp       = -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_reason_protocols       = -1;
static gint hf_sip_reason_cause_q850      = -1;
static gint hf_sip_reason_cause_sip       = -1;
static gint hf_sip_reason_cause_other     = -1;
static gint hf_sip_reason_text            = -1;

static gint hf_sip_msg_body               = -1;
static gint hf_sip_sec_mechanism          = -1;
static gint hf_sip_sec_mechanism_alg      = -1;
static gint hf_sip_sec_mechanism_ealg     = -1;
static gint hf_sip_sec_mechanism_prot     = -1;
static gint hf_sip_sec_mechanism_spi_c    = -1;
static gint hf_sip_sec_mechanism_spi_s    = -1;
static gint hf_sip_sec_mechanism_port1    = -1;
static gint hf_sip_sec_mechanism_port_c   = -1;
static gint hf_sip_sec_mechanism_port2    = -1;
static gint hf_sip_sec_mechanism_port_s   = -1;
static gint hf_sip_session_id_sess_id     = -1;
static gint hf_sip_session_id_param       = -1;
static gint hf_sip_session_id_local_uuid  = -1;
static gint hf_sip_session_id_remote_uuid = -1;
static gint hf_sip_continuation           = -1;
static gint hf_sip_feature_cap            = -1;

static gint hf_sip_p_acc_net_i_acc_type   = -1;
static gint hf_sip_p_acc_net_i_ucid_3gpp  = -1;

static gint hf_sip_service_priority = -1;
static gint hf_sip_icid_value = -1;
static gint hf_sip_icid_gen_addr = -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_sip_ext_hdr               = -1;
static gint ett_raw_text                  = -1;
static gint ett_sip_element               = -1;
static gint ett_sip_hist                  = -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_security_client       = -1;
static gint ett_sip_security_server       = -1;
static gint ett_sip_security_verify       = -1;
static gint ett_sip_rack                  = -1;
static gint ett_sip_route                 = -1;
static gint ett_sip_record_route          = -1;
static gint ett_sip_service_route         = -1;
static gint ett_sip_path                  = -1;
static gint ett_sip_ruri                  = -1;
static gint ett_sip_to_uri                = -1;
static gint ett_sip_curi                  = -1;
static gint ett_sip_from_uri              = -1;
static gint ett_sip_pai_uri               = -1;
static gint ett_sip_pmiss_uri             = -1;
static gint ett_sip_ppi_uri               = -1;
static gint ett_sip_tc_uri                = -1;
static gint ett_sip_session_id            = -1;
static gint ett_sip_p_access_net_info     = -1;
static gint ett_sip_p_charging_vector      = -1;
static gint ett_sip_feature_caps          = -1;

static expert_field ei_sip_unrecognized_header = EI_INIT;
static expert_field ei_sip_header_no_colon = EI_INIT;
static expert_field ei_sip_header_not_terminated = EI_INIT;
#if 0
static expert_field ei_sip_odd_register_response = EI_INIT;
#endif
static expert_field ei_sip_sipsec_malformed = EI_INIT;
static expert_field ei_sip_via_sent_by_port = EI_INIT;
static expert_field ei_sip_content_length_invalid = EI_INIT;
static expert_field ei_sip_retry_after_invalid = EI_INIT;
static expert_field ei_sip_Status_Code_invalid = EI_INIT;
static expert_field ei_sip_authorization_invalid = EI_INIT;

/* patterns used for tvb_ws_mempbrk_pattern_guint8 */
static ws_mempbrk_pattern pbrk_comma_semi;
static ws_mempbrk_pattern pbrk_whitespace;
static ws_mempbrk_pattern pbrk_param_end;
static ws_mempbrk_pattern pbrk_param_end_colon_brackets;
static ws_mempbrk_pattern pbrk_header_end_dquote;
static ws_mempbrk_pattern pbrk_tab_sp_fslash;
static ws_mempbrk_pattern pbrk_addr_end;
static ws_mempbrk_pattern pbrk_via_param_end;


/* PUBLISH method added as per http://www.ietf.org/internet-drafts/draft-ietf-sip-publish-01.txt */
static const char *sip_methods[] = {
#define SIP_METHOD_INVALID  0
        "<Invalid method>",      /* Pad so that the real methods start at index 1 */
#define SIP_METHOD_ACK      1
        "ACK",
#define SIP_METHOD_BYE      2
        "BYE",
#define SIP_METHOD_CANCEL   3
        "CANCEL",
#define SIP_METHOD_DO       4
        "DO",
#define SIP_METHOD_INFO     5
        "INFO",
#define SIP_METHOD_INVITE   6
        "INVITE",
#define SIP_METHOD_MESSAGE  7
        "MESSAGE",
#define SIP_METHOD_NOTIFY   8
        "NOTIFY",
#define SIP_METHOD_OPTIONS  9
        "OPTIONS",
#define SIP_METHOD_PRACK    10
        "PRACK",
#define SIP_METHOD_QAUTH    11
        "QAUTH",
#define SIP_METHOD_REFER    12
        "REFER",
#define SIP_METHOD_REGISTER 13
        "REGISTER",
#define SIP_METHOD_SPRACK   14
        "SPRACK",
#define SIP_METHOD_SUBSCRIBE    15
        "SUBSCRIBE",
#define SIP_METHOD_UPDATE   16
        "UPDATE",
#define SIP_METHOD_PUBLISH  17
        "PUBLISH"
};

/* from RFC 3261
 * Updated with info from http://www.iana.org/assignments/sip-parameters
 * (last updated 2009-11-11)
 * 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 */
#define POS_ACCEPT                       1
    { "Accept-Contact",                 "a"  }, /* 2 RFC3841  */
#define POS_ACCEPT_CONTACT               2
    { "Accept-Encoding",                NULL }, /* 3 */
#define POS_ACCEPT_ENCODING              3
    { "Accept-Language",                NULL }, /* 4 */
#define POS_ACCEPT_LANGUAGE              4
    { "Accept-Resource-Priority",       NULL }, /* 5 RFC4412 */
#define POS_ACCEPT_RESOURCE_PRIORITY     5
    { "Alert-Info",                     NULL },
#define POS_ALERT_INFO                   6
    { "Allow",                          NULL },
#define POS_ALLOW                        7
    { "Allow-Events",                   "u"  }, /* 8 RFC3265  */
#define POS_ALLOW_EVENTS                 8
    { "Answer-Mode",                    NULL }, /* 9 RFC5373 */
#define POS_ANSWER_MODE                  9
    { "Authentication-Info",            NULL },
#define POS_AUTHENTICATION_INFO         10
    { "Authorization",                  NULL }, /* 11 */
#define POS_AUTHORIZATION               11
    { "Call-ID",                        "i"  },
#define POS_CALL_ID                     12
    { "Call-Info",                      NULL },
#define POS_CALL_INFO                   13
    { "Contact",                        "m"  },
#define POS_CONTACT                     14
    { "Content-Disposition",            NULL },
#define POS_CONTENT_DISPOSITION         15
    { "Content-Encoding",               "e"  },  /*  16 */
#define POS_CONTENT_ENCODING            16
    { "Content-Language",               NULL },
#define POS_CONTENT_LANGUAGE            17
    { "Content-Length",                 "l"  },
#define POS_CONTENT_LENGTH              18
    { "Content-Type",                   "c"  },
#define POS_CONTENT_TYPE                19
    { "CSeq",                           NULL },
#define POS_CSEQ                        20
    { "Date",                           NULL },  /*  21 */
#define POS_DATE                        21
/*              Encryption (Deprecated)       [RFC3261] */
    { "Error-Info",                     NULL },  /*  22 */
#define POS_ERROR_INFO                  22
    { "Event",                          "o"  },  /*  23 */
#define POS_EVENT                       23
    { "Expires",                        NULL },  /*  24 */
#define POS_EXPIRES                     24
    { "Feature-Caps",                   NULL },  /*  25 [RFC6809 */
#define POS_FEATURE_CAPS                25
    { "Flow-Timer",                     NULL },  /*  26 RFC5626  */
#define POS_FLOW_TIMER                  26
    { "From",                           "f"  },  /*  27 */
#define POS_FROM                        27

    { "Geolocation",                   NULL  },  /*  28 */
#define POS_GEOLOCATION                 28
    { "Geolocation-Error",             NULL  },  /*  29 */
#define POS_GEOLOCATION_ERROR           29
    { "Geolocation-Routing",           NULL  },  /*  30 */
#define POS_GEOLOCATION_ROUTING         30

/*              Hide                          [RFC3261] (deprecated)*/
    { "History-Info",                   NULL },  /*  31 RFC4244  */
#define POS_HISTORY_INFO                31
    { "Identity",                       "y"  },  /*  32 RFC4474  */
#define POS_IDENTITY                    32
    { "Identity-Info",                  "n"  },  /*  33 RFC4474  */
#define POS_IDENTITY_INFO               33
    { "Info-Package",                   NULL },  /*  34 RFC-ietf-sipcore-info-events-10.txt  */
#define POS_INFO_PKG                    34
    { "In-Reply-To",                    NULL },  /*  35 RFC3261  */
#define POS_IN_REPLY_TO                 35
    { "Join",                           NULL },  /*  36 RFC3911  */
#define POS_JOIN                        36
    { "Max-Breadth",                    NULL },  /*  37 RFC5393*/
#define POS_MAX_BREADTH                 37
    { "Max-Forwards",                   NULL },  /*  38 */
#define POS_MAX_FORWARDS                38
    { "MIME-Version",                   NULL },  /*  39 */
#define POS_MIME_VERSION                39
    { "Min-Expires",                    NULL },  /*  40 */
#define POS_MIN_EXPIRES                 40
    { "Min-SE",                         NULL },  /*  41 RFC4028  */
#define POS_MIN_SE                      41
    { "Organization",                   NULL },  /*  42 RFC3261  */
#define POS_ORGANIZATION                42
    { "P-Access-Network-Info",          NULL },  /*  43 RFC3455  */
#define POS_P_ACCESS_NETWORK_INFO       43
    { "P-Answer-State",                 NULL },  /*  44 RFC4964  */
#define POS_P_ANSWER_STATE              44
    { "P-Asserted-Identity",            NULL },  /*  45 RFC3325  */
#define POS_P_ASSERTED_IDENTITY         45
    { "P-Asserted-Service",             NULL },  /*  46 RFC6050  */
#define POS_P_ASSERTED_SERV             46
    { "P-Associated-URI",               NULL },  /*  47 RFC3455  */
#define POS_P_ASSOCIATED_URI            47
    { "P-Called-Party-ID",              NULL },  /*  48 RFC3455  */
#define POS_P_CALLED_PARTY_ID           48
    { "P-Charging-Function-Addresses",  NULL },  /*  49 RFC3455  */
#define POS_P_CHARGING_FUNC_ADDRESSES   49
    { "P-Charging-Vector",              NULL },  /*  50 RFC3455  */
#define POS_P_CHARGING_VECTOR           50
    { "P-DCS-Trace-Party-ID",           NULL },  /*  51 RFC5503  */
#define POS_P_DCS_TRACE_PARTY_ID        51
    { "P-DCS-OSPS",                     NULL },  /*  52 RFC5503  */
#define POS_P_DCS_OSPS                  52
    { "P-DCS-Billing-Info",             NULL },  /*  53 RFC5503  */
#define POS_P_DCS_BILLING_INFO          53
    { "P-DCS-LAES",                     NULL },  /*  54 RFC5503  */
#define POS_P_DCS_LAES                  54
    { "P-DCS-Redirect",                 NULL },  /*  55 RFC5503  */
#define POS_P_DCS_REDIRECT              55
    { "P-Early-Media",                  NULL },  /*  56 RFC5009  */
#define POS_P_EARLY_MEDIA               56
    { "P-Media-Authorization",          NULL },  /*  57 RFC3313  */
#define POS_P_MEDIA_AUTHORIZATION       57
    { "P-Preferred-Identity",           NULL },  /*  58 RFC3325  */
#define POS_P_PREFERRED_IDENTITY        58
    { "P-Preferred-Service",            NULL },  /*  58 RFC6050  */
#define POS_P_PREFERRED_SERV            59
    { "P-Profile-Key",                  NULL },  /*  60 RFC5002  */
#define POS_P_PROFILE_KEY               60
    { "P-Refused-URI-List",             NULL },  /*  61 RFC5318  */
#define POS_P_REFUSED_URI_LST           61
    { "P-Served-User",                  NULL },  /*  62 RFC5502  */
#define POS_P_SERVED_USER               62
    { "P-User-Database",                NULL },  /*  63 RFC4457  */
#define POS_P_USER_DATABASE             63
    { "P-Visited-Network-ID",           NULL },  /*  64 RFC3455  */
#define POS_P_VISITED_NETWORK_ID        64
    { "Path",                           NULL },  /*  65 RFC3327  */
#define POS_PATH                        65
    { "Permission-Missing",             NULL },  /*  66 RFC5360  */
#define POS_PERMISSION_MISSING          66
    { "Policy-Contact",                 NULL },  /*  67 RFC3261  */
#define POS_POLICY_CONTACT              67
    { "Policy-ID",                      NULL },  /*  68 RFC3261  */
#define POS_POLICY_ID                   68
    { "Priority",                       NULL },  /*  69 RFC3261  */
#define POS_PRIORITY                    69
    { "Priv-Answer-Mode",               NULL },  /*  70 RFC5373  */
#define POS_PRIV_ANSWER_MODE            70
    { "Privacy",                        NULL },  /*  71 RFC3323  */
#define POS_PRIVACY                     71
    { "Proxy-Authenticate",             NULL },  /*  72 */
#define POS_PROXY_AUTHENTICATE          72
    { "Proxy-Authorization",            NULL },  /*  73 */
#define POS_PROXY_AUTHORIZATION         73
    { "Proxy-Require",                  NULL },  /*  74 */
#define POS_PROXY_REQUIRE               74
    { "RAck",                           NULL },  /*  75 RFC3262  */
#define POS_RACK                        75
    { "Reason",                         NULL },  /*  76 RFC3326  */
#define POS_REASON                      76
    { "Reason-Phrase",                  NULL },  /*  77 RFC3326  */
#define POS_REASON_PHRASE               77
    { "Record-Route",                   NULL },  /*  78 */
#define POS_RECORD_ROUTE                78
    { "Recv-Info",                      NULL },  /*  79 RFC-ietf-sipcore-info-events-10.txt*/
#define POS_RECV_INFO                   79
    { "Refer-Sub",                      NULL },  /*  80 RFC4488  */
#define POS_REFER_SUB                   80
    { "Refer-To",                       "r"  },  /*  81 RFC3515  */
#define POS_REFER_TO                    81
    { "Referred-By",                    "b"  },  /*  82 RFC3892  */
#define POS_REFERED_BY                  82
    { "Reject-Contact",                 "j"  },  /*  83 RFC3841  */
#define POS_REJECT_CONTACT              83
    { "Replaces",                       NULL },  /*  84 RFC3891  */
#define POS_REPLACES                    84
    { "Reply-To",                       NULL },  /*  85 RFC3261  */
#define POS_REPLY_TO                    85
    { "Request-Disposition",            "d"  },  /*  86 RFC3841  */
#define POS_REQUEST_DISPOSITION         86
    { "Require",                        NULL },  /*  87 RFC3261  */
#define POS_REQUIRE                     87
    { "Resource-Priority",              NULL },  /*  88 RFC4412  */
#define POS_RESOURCE_PRIORITY           88
    /*{ "Response-Key (Deprecated)     [RFC3261]*/
    { "Retry-After",                    NULL },  /*  89 RFC3261  */
#define POS_RETRY_AFTER                 89
    { "Route",                          NULL },  /*  90 RFC3261  */
#define POS_ROUTE                       90
    { "RSeq",                           NULL },  /*  91 RFC3262  */
#define POS_RSEQ                        91
    { "Security-Client",                NULL },  /*  92 RFC3329  */
#define POS_SECURITY_CLIENT             92
    { "Security-Server",                NULL },  /*  93 RFC3329  */
#define POS_SECURITY_SERVER             93
    { "Security-Verify",                NULL },  /*  94 RFC3329  */
#define POS_SECURITY_VERIFY             94
    { "Server",                         NULL },  /*  95 RFC3261  */
#define POS_SERVER                      95
    { "Service-Route",                  NULL },  /*  96 RFC3608  */
#define POS_SERVICE_ROUTE               96
    { "Session-Expires",                "x"  },  /*  97 RFC4028  */
#define POS_SESSION_EXPIRES             97
    { "Session-ID",                     NULL },  /*  98 RFC7329  */
#define POS_SESSION_ID                  98
    { "SIP-ETag",                       NULL },  /*  99 RFC3903  */
#define POS_SIP_ETAG                    99
    { "SIP-If-Match",                   NULL },  /* 100 RFC3903  */
#define POS_SIP_IF_MATCH                100
    { "Subject",                        "s"  },  /* 101 RFC3261  */
#define POS_SUBJECT                     101
    { "Subscription-State",             NULL },  /* 102 RFC3265  */
#define POS_SUBSCRIPTION_STATE          102
    { "Supported",                      "k"  },  /* 103 RFC3261  */
#define POS_SUPPORTED                   103
    { "Suppress-If-Match",              NULL },  /* 104 RFC5839  */
#define POS_SUPPRESS_IF_MATCH           104
    { "Target-Dialog",                  NULL },  /* 105 RFC4538  */
#define POS_TARGET_DIALOG               105
    { "Timestamp",                      NULL },  /* 106 RFC3261  */
#define POS_TIMESTAMP                   106
    { "To",                             "t"  },  /* 107 RFC3261  */
#define POS_TO                          107
    { "Trigger-Consent",                NULL },  /* 108 RFC5360  */
#define POS_TRIGGER_CONSENT             108
    { "Unsupported",                    NULL },  /* 109 RFC3261  */
#define POS_UNSUPPORTED                 109
    { "User-Agent",                     NULL },  /* 110 RFC3261  */
#define POS_USER_AGENT                  110
    { "Via",                            "v"  },  /* 111 RFC3261  */
#define POS_VIA                         111
    { "Warning",                        NULL },  /* 112 RFC3261  */
#define POS_WARNING                     112
    { "WWW-Authenticate",               NULL },  /* 113 RFC3261  */
#define POS_WWW_AUTHENTICATE            113
    { "Diversion",                      NULL },  /* 114 RFC5806  */
#define POS_DIVERSION                   114
    { "User-to-User",                   NULL },  /* 115 draft-johnston-sipping-cc-uui-09  */
#define POS_USER_TO_USER                115
};




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"Answer-Mode"                       RFC5373 */
    -1, /* 10"Authentication-Info"                      */
    -1, /* 11"Authorization",                           */
    -1, /* 12"Call-ID",                                 */
    -1, /* 13"Call-Info"                                */
    -1, /* 14"Contact",                                 */
    -1, /* 15"Content-Disposition",                     */
    -1, /* 16"Content-Encoding",                        */
    -1, /* 17"Content-Language",                        */
    -1, /* 18"Content-Length",                          */
    -1, /* 19"Content-Type",                            */
    -1, /* 20"CSeq",                                    */
    -1, /* 21"Date",                                    */
    -1, /* 22"Error-Info",                              */
    -1, /* 23"Event",                                   */
    -1, /* 24"Expires",                                 */
    -1, /* 25"Feature-Caps",                            */
    -1, /* 26"Flow-Timer",                      RFC5626 */
    -1, /* 27"From",                                    */
    -1, /* 28"Geolocation",                             */
    -1, /* 29"Geolocation-Error",                       */
    -1, /* 30"Geolocation-Routing",                     */
    -1, /* 31"History-Info",                    RFC4244 */
    -1, /* 32"Identity",                                */
    -1, /* 33"Identity-Info",                   RFC4474 */
    -1, /* 34"Info-Package", RFC-ietf-sipcore-info-events-10.txt */
    -1, /* 35"In-Reply-To",                     RFC3261 */
    -1, /* 36"Join",                            RFC3911 */
    -1, /* 37"Max-Breadth"                      RFC5393 */
    -1, /* 38"Max-Forwards",                            */
    -1, /* 39"MIME-Version",                            */
    -1, /* 40"Min-Expires",                             */
    -1, /* 41"Min-SE",                          RFC4028 */
    -1, /* 42"Organization",                            */
    -1, /* 43"P-Access-Network-Info",           RFC3455 */
    -1, /* 44"P-Answer-State",                  RFC4964 */
    -1, /* 45"P-Asserted-Identity",             RFC3325 */
    -1, /* 46"P-Asserted-Service",  RFC-drage-sipping-service-identification-05.txt */
    -1, /* 47"P-Associated-URI",                RFC3455 */
    -1, /* 48"P-Called-Party-ID",               RFC3455 */
    -1, /* 49"P-Charging-Function-Addresses",   RFC3455 */
    -1, /* 50"P-Charging-Vector",               RFC3455 */
    -1, /* 51"P-DCS-Trace-Party-ID",            RFC3603 */
    -1, /* 52"P-DCS-OSPS",                      RFC3603 */
    -1, /* 53"P-DCS-Billing-Info",              RFC3603 */
    -1, /* 54"P-DCS-LAES",                      RFC3603 */
    -1, /* 55"P-DCS-Redirect",                  RFC3603 */
    -1, /* 56"P-Early-Media",                           */
    -1, /* 57"P-Media-Authorization",           RFC3313 */
    -1, /* 58"P-Preferred-Identity",            RFC3325 */
    -1, /* 59"P-Preferred-Service",  RFC-drage-sipping-service-identification-05.txt */
    -1, /* 60"P-Profile-Key",                           */
    -1, /* 61"P-Refused-URI-List",              RFC5318 */
    -1, /* 62"P-Served-User",                   RFC5502 */
    -1, /* 63"P-User-Database                   RFC4457 */
    -1, /* 64"P-Visited-Network-ID",            RFC3455 */
    -1, /* 65"Path",                            RFC3327 */
    -1, /* 66"Permission-Missing"               RFC5360 */
    -1, /* 67"Policy-Contact"                   RFC5360 */
    -1, /* 68"Policy-ID"                        RFC5360 */
    -1, /* 69"Priority"                                 */
    -1, /* 70"Priv-Answer-mode"                 RFC5373 */
    -1, /* 71"Privacy",                         RFC3323 */
    -1, /* 72"Proxy-Authenticate",                      */
    -1, /* 73"Proxy-Authorization",                     */
    -1, /* 74"Proxy-Require",                           */
    -1, /* 75"RAck",                            RFC3262 */
    -1, /* 76"Reason",                          RFC3326 */
    -1, /* 77"Reason-Phrase",                   RFC3326 */
    -1, /* 78"Record-Route",                            */
    -1, /* 79"Recv-Info",   RFC-ietf-sipcore-info-events-10.txt */
    -1, /* 80"Refer-Sub",",                     RFC4488 */
    -1, /* 81"Refer-To",                        RFC3515 */
    -1, /* 82"Referred-By",                             */
    -1, /* 83"Reject-Contact",                  RFC3841 */
    -1, /* 84"Replaces",                        RFC3891 */
    -1, /* 85"Reply-To",                        RFC3261 */
    -1, /* 86"Request-Disposition",             RFC3841 */
    -1, /* 87"Require",                         RFC3261 */
    -1, /* 88"Resource-Priority",               RFC4412 */
    -1, /* 89"Retry-After",                     RFC3261 */
    -1, /* 90"Route",                           RFC3261 */
    -1, /* 91"RSeq",                            RFC3262 */
    -1, /* 92"Security-Client",                 RFC3329 */
    -1, /* 93"Security-Server",                 RFC3329 */
    -1, /* 94"Security-Verify",                 RFC3329 */
    -1, /* 95"Server",                          RFC3261 */
    -1, /* 96"Service-Route",                   RFC3608 */
    -1, /* 97"Session-Expires",                 RFC4028 */
    -1, /* 98"Session-ID",                      RFC7329 */
    -1, /* 99"SIP-ETag",                        RFC3903 */
    -1, /* 100"SIP-If-Match",                    RFC3903 */
    -1, /* 101"Subject",                        RFC3261 */
    -1, /* 102"Subscription-State",             RFC3265 */
    -1, /* 103"Supported",                      RFC3261 */
    -1, /* 104"Suppress-If-Match",              RFC4538 */
    -1, /* 105"Target-Dialog",                  RFC4538 */
    -1, /* 106"Timestamp",                      RFC3261 */
    -1, /* 107"To",                             RFC3261 */
    -1, /* 108"Trigger-Consent"                 RFC5380 */
    -1, /* 109"Unsupported",                    RFC3261 */
    -1, /* 110"User-Agent",                     RFC3261 */
    -1, /* 111"Via",                            RFC3261 */
    -1, /* 112"Warning",                        RFC3261 */
    -1, /* 113"WWW-Authenticate",               RFC3261 */
    -1, /* 114"Diversion",                      RFC5806 */
    -1, /* 115"User-to-User",  draft-johnston-sipping-cc-uui-09 */

};

/* 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},
    {"oc",            &hf_sip_via_oc},
    {"oc-validity",   &hf_sip_via_oc_validity },
    {"oc-seq",        &hf_sip_via_oc_seq},
    {"oc-algo",       &hf_sip_via_oc_algo}
};


typedef struct {
    gint *hf_sip_addr;
    gint *hf_sip_user;
    gint *hf_sip_host;
    gint *hf_sip_port;
    gint *hf_sip_param;
    gint *ett_uri;
} hf_sip_uri_t;

static hf_sip_uri_t sip_pai_uri = {
    &hf_sip_pai_addr,
    &hf_sip_pai_user,
    &hf_sip_pai_host,
    &hf_sip_pai_port,
    &hf_sip_pai_param,
    &ett_sip_pai_uri
};

static hf_sip_uri_t sip_ppi_uri = {
    &hf_sip_ppi_addr,
    &hf_sip_ppi_user,
    &hf_sip_ppi_host,
    &hf_sip_ppi_port,
    &hf_sip_ppi_param,
    &ett_sip_ppi_uri
};

static hf_sip_uri_t sip_pmiss_uri = {
    &hf_sip_pmiss_addr,
    &hf_sip_pmiss_user,
    &hf_sip_pmiss_host,
    &hf_sip_pmiss_port,
    &hf_sip_pmiss_param,
    &ett_sip_pmiss_uri
};


static hf_sip_uri_t sip_tc_uri = {
    &hf_sip_tc_addr,
    &hf_sip_tc_user,
    &hf_sip_tc_host,
    &hf_sip_tc_port,
    &hf_sip_tc_param,
    &ett_sip_tc_uri
};

static hf_sip_uri_t sip_to_uri = {
    &hf_sip_to_addr,
    &hf_sip_to_user,
    &hf_sip_to_host,
    &hf_sip_to_port,
    &hf_sip_to_param,
    &ett_sip_to_uri
};

static hf_sip_uri_t sip_from_uri = {
    &hf_sip_from_addr,
    &hf_sip_from_user,
    &hf_sip_from_host,
    &hf_sip_from_port,
    &hf_sip_from_param,
    &ett_sip_from_uri
};

static hf_sip_uri_t sip_req_uri = {
    &hf_sip_ruri,
    &hf_sip_ruri_user,
    &hf_sip_ruri_host,
    &hf_sip_ruri_port,
    &hf_sip_ruri_param,
    &ett_sip_ruri
};

static hf_sip_uri_t sip_contact_uri = {
    &hf_sip_curi,
    &hf_sip_curi_user,
    &hf_sip_curi_host,
    &hf_sip_curi_port,
    &hf_sip_curi_param,
    &ett_sip_curi
};

static hf_sip_uri_t sip_route_uri = {
    &hf_sip_route,
    &hf_sip_route_user,
    &hf_sip_route_host,
    &hf_sip_route_port,
    &hf_sip_route_param,
    &ett_sip_route
};

static hf_sip_uri_t sip_record_route_uri = {
    &hf_sip_record_route,
    &hf_sip_record_route_user,
    &hf_sip_record_route_host,
    &hf_sip_record_route_port,
    &hf_sip_record_route_param,
    &ett_sip_record_route
};

static hf_sip_uri_t sip_service_route_uri = {
    &hf_sip_service_route,
    &hf_sip_service_route_user,
    &hf_sip_service_route_host,
    &hf_sip_service_route_port,
    &hf_sip_service_route_param,
    &ett_sip_service_route
};

static hf_sip_uri_t sip_path_uri = {
    &hf_sip_path,
    &hf_sip_path_user,
    &hf_sip_path_host,
    &hf_sip_path_port,
    &hf_sip_path_param,
    &ett_sip_path
};

/*
 * 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_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;
/* global_sip_raw_text_without_crlf determines whether we are going to display  */
/* the raw text of the SIP message with or without the '\r\n'.          */
static gboolean global_sip_raw_text_without_crlf = 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;

/*
 * same source port for retransmissions
 */
static gboolean sip_retrans_the_same_sport = TRUE;

/* whether we hold off tracking RTP conversations until an SDP answer is received */
static gboolean sip_delay_sdp_changes = FALSE;

/* Extension header subdissectors */
static dissector_table_t ext_hdr_subdissector_table;

/* Custom SIP headers */
typedef struct _header_field_t {
    gchar* header_name;
    gchar* header_desc;
} header_field_t;

static header_field_t* sip_custom_header_fields = NULL;
static guint sip_custom_num_header_fields = 0;
static wmem_map_t *sip_custom_header_fields_hash = NULL;

static gboolean
header_fields_update_cb(void *r, char **err)
{
    header_field_t *rec = (header_field_t *)r;
    char c;

    if (rec->header_name == NULL) {
        *err = g_strdup("Header name can't be empty");
        return FALSE;
    }

    g_strstrip(rec->header_name);
    if (rec->header_name[0] == 0) {
        *err = g_strdup("Header name can't be empty");
        return FALSE;
    }

    /* Check for invalid characters (to avoid asserting out when
    * registering the field).
    */
    c = proto_check_field_name(rec->header_name);
    if (c) {
        *err = g_strdup_printf("Header name can't contain '%c'", c);
        return FALSE;
    }

    *err = NULL;
    return TRUE;
}

static void *
header_fields_copy_cb(void* n, const void* o, size_t siz _U_)
{
    header_field_t* new_rec = (header_field_t*)n;
    const header_field_t* old_rec = (const header_field_t*)o;

    new_rec->header_name = g_strdup(old_rec->header_name);
    new_rec->header_desc = g_strdup(old_rec->header_desc);

    return new_rec;
}

static void
header_fields_free_cb(void*r)
{
    header_field_t* rec = (header_field_t*)r;

    g_free(rec->header_name);
    g_free(rec->header_desc);
}

static void
header_fields_initialize_cb(void)
{
    static hf_register_info* hf;
    gint* hf_id;
    guint i;
    gchar* header_name;
    gchar* header_name_key;

    if (hf) {
        guint hf_size = wmem_map_size(sip_custom_header_fields_hash);
        /* Deregister all fields */
        for (i = 0; i < hf_size; i++) {
            proto_deregister_field(proto_sip, *(hf[i].p_id));
            header_name_key = wmem_ascii_strdown(NULL, hf[i].hfinfo.name, -1);
            wmem_map_remove(sip_custom_header_fields_hash, header_name_key);
            wmem_free(NULL, header_name_key);
            wmem_free(wmem_epan_scope(), hf[i].p_id);
        }
        proto_add_deregistered_data(hf);
        hf = NULL;
    }

    if (sip_custom_num_header_fields) {
        hf = g_new0(hf_register_info, sip_custom_num_header_fields);

        for (i = 0; i < sip_custom_num_header_fields; i++) {
            hf_id = wmem_new(wmem_epan_scope(), gint);
            *hf_id = -1;
            header_name = g_strdup(sip_custom_header_fields[i].header_name);
            header_name_key = wmem_ascii_strdown(wmem_epan_scope(), header_name, -1);

            hf[i].p_id = hf_id;
            hf[i].hfinfo.name = header_name;
            hf[i].hfinfo.abbrev = g_strdup_printf("sip.%s", header_name);
            hf[i].hfinfo.type = FT_STRING;
            hf[i].hfinfo.display = BASE_NONE;
            hf[i].hfinfo.strings = NULL;
            hf[i].hfinfo.bitmask = 0;
            hf[i].hfinfo.blurb = g_strdup(sip_custom_header_fields[i].header_desc);
            HFILL_INIT(hf[i]);

            wmem_map_insert(sip_custom_header_fields_hash, header_name_key, hf_id);
        }

        proto_register_field_array(proto_sip, hf, sip_custom_num_header_fields);
    }
}

UAT_CSTRING_CB_DEF(sip_custom_header_fields, header_name, header_field_t)
UAT_CSTRING_CB_DEF(sip_custom_header_fields, header_desc, header_field_t)

/* SIP authorization parameters */
static gboolean global_sip_validate_authorization = FALSE;

typedef struct _authorization_user_t {
    gchar* username;
    gchar* realm;
    gchar* password;
} authorization_user_t;

static authorization_user_t* sip_authorization_users = NULL;
static guint sip_authorization_num_users = 0;

static gboolean
authorization_users_update_cb(void *r, char **err)
{
    authorization_user_t *rec = (authorization_user_t *)r;
    char c;

    if (rec->username == NULL) {
        *err = g_strdup("Username can't be empty");
        return FALSE;
    }

    g_strstrip(rec->username);
    if (rec->username[0] == 0) {
        *err = g_strdup("Username can't be empty");
        return FALSE;
    }

    /* Check for invalid characters (to avoid asserting out when
    * registering the field).
    */
    c = proto_check_field_name(rec->username);
    if (c) {
        *err = g_strdup_printf("Username can't contain '%c'", c);
        return FALSE;
    }

    *err = NULL;
    return TRUE;
}

static void *
authorization_users_copy_cb(void* n, const void* o, size_t siz _U_)
{
    authorization_user_t* new_rec = (authorization_user_t*)n;
    const authorization_user_t* old_rec = (const authorization_user_t*)o;

    new_rec->username = g_strdup(old_rec->username);
    new_rec->realm = g_strdup(old_rec->realm);
    new_rec->password = g_strdup(old_rec->password);

    return new_rec;
}

static void
authorization_users_free_cb(void*r)
{
    authorization_user_t* rec = (authorization_user_t*)r;

    g_free(rec->username);
    g_free(rec->realm);
    g_free(rec->password);
}

UAT_CSTRING_CB_DEF(sip_authorization_users, username, authorization_user_t)
UAT_CSTRING_CB_DEF(sip_authorization_users, realm, authorization_user_t)
UAT_CSTRING_CB_DEF(sip_authorization_users, password, authorization_user_t)

/* Forward declaration we need below */
void proto_reg_handoff_sip(void);
static gboolean dissect_sip_common(tvbuff_t *tvb, int offset, int remaining_length, 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(gchar *header_name, guint header_len);
static void dfilter_sip_request_line(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gint offset,
    guint meth_len, gint linelen);
static void dfilter_sip_status_line(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gint line_end, gint offset);
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);

static guint sip_find_request(packet_info *pinfo,
                gchar* cseq_method,
                gchar* call_id,
                guchar cseq_number_set, guint32 cseq_number,
                guint32 *response_time);

static guint sip_find_invite(packet_info *pinfo,
                gchar* cseq_method,
                gchar* call_id,
                guchar cseq_number_set, guint32 cseq_number,
                guint32 *response_time);

typedef struct
{
    gchar * username;
    gchar * realm;
    gchar * uri;
    gchar * nonce;
    gchar * cnonce;
    gchar * nonce_count;
    gchar * response;
    gchar * qop;
    gchar * algorithm;
    gchar * method;
} sip_authorization_t;

static authorization_user_t * sip_get_authorization(sip_authorization_t *authorization_info);
static gboolean sip_validate_authorization(sip_authorization_t *authorization_info, gchar *password);

static authorization_user_t * sip_get_authorization(sip_authorization_t *authorization_info)
{
    guint i;
    for (i = 0; i < sip_authorization_num_users; i++) {
        if ((!strcmp(sip_authorization_users[i].username, authorization_info->username)) &&
            (!strcmp(sip_authorization_users[i].realm, authorization_info->realm))) {
            return &sip_authorization_users[i];
        }
    }
    return NULL;
}

/* 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 7

/* 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 conversation 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)
 *
 * N.B. This is broken for a couple of reasons:
 * - it won't cope properly with overlapping transactions within the
 *   same dialog
 * - request response mapping won't work where the response uses a different
 *   address pair from the request
 *
 * TODO: proper transaction matching uses RFC fields (use Max-forwards or
 * maybe Via count as extra key to limit view to one hop)
 ****************************************************************************/

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

/* Types for hash table keys and values */
#define MAX_CALL_ID_SIZE 128
#define MAGIC_SOURCE_PORT 0

/* Conversation-type key */
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;

/* Current conversation-type value */
typedef struct
{
    guint32             cseq;
    transaction_state_t transaction_state;
    gchar               method[MAX_CSEQ_METHOD_SIZE];
    nstime_t            request_time;
    guint32             response_code;
    gint                frame_number;
} sip_hash_value;

/* Result to be stored in per-packet info */
typedef struct
{
    gint       original_frame_num;
    gint       response_request_frame_num;
    gint       response_time;
} sip_frame_result_value;


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

/* Equal keys */
static gint sip_equal(gconstpointer v, gconstpointer v2)
{
    const sip_hash_key* val1 = (const sip_hash_key*)v;
    const sip_hash_key* val2 = (const sip_hash_key*)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);
}


/* Initializes the hash table each time a new
 * file is loaded or re-loaded in wireshark */
static void
sip_init_protocol(void)
{
    guint i;
    gchar *value_copy;
    sip_hash = g_hash_table_new(g_str_hash , sip_equal);

    /* Hash table for quick lookup of SIP headers names to hf entry (POS_x) */
    sip_headers_hash = g_hash_table_new(g_str_hash , g_str_equal);
    for (i = 1; i < array_length(sip_headers); i++){
        value_copy = wmem_strdup(wmem_file_scope(), sip_headers[i].name);
        ascii_strdown_inplace(value_copy);
        g_hash_table_insert(sip_headers_hash, (gpointer)value_copy, GINT_TO_POINTER(i));
    }
}

static void
sip_cleanup_protocol(void)
{
     g_hash_table_destroy(sip_hash);
     g_hash_table_destroy(sip_headers_hash);
}

/* Call the export PDU tap with relevant data */
static void
export_sip_pdu(packet_info *pinfo, tvbuff_t *tvb)
{
  exp_pdu_data_t *exp_pdu_data = export_pdu_create_common_tags(pinfo, "sip", EXP_PDU_TAG_PROTO_NAME);

  exp_pdu_data->tvb_captured_length = tvb_captured_length(tvb);
  exp_pdu_data->tvb_reported_length = tvb_reported_length(tvb);
  exp_pdu_data->pdu_tvb = tvb;

  tap_queue_packet(exported_pdu_tap, pinfo, exp_pdu_data);

}

typedef enum
{
    SIP_URI_TYPE_ABSOLUTE_URI,
    SIP_URI_TYPE_SIP,
    SIP_URI_TYPE_TEL

} sip_uri_type_enum_t;

/* Structure to collect info about a sip uri */
typedef struct _uri_offset_info
{
    sip_uri_type_enum_t uri_type;
    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;
    gint uri_user_start;
    gint uri_user_end;
    gint uri_host_start;
    gint uri_host_end;
    gint uri_host_port_start;
    gint uri_host_port_end;
} uri_offset_info;

static void
sip_uri_offset_init(uri_offset_info *uri_offsets){

    /* Initialize the uri_offsets */
    uri_offsets->uri_type = SIP_URI_TYPE_ABSOLUTE_URI;
    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;
    uri_offsets->uri_user_start = -1;
    uri_offsets->uri_user_end = -1;
    uri_offsets->uri_host_start = -1;
    uri_offsets->uri_host_end = -1;
    uri_offsets->uri_host_port_start = -1;
    uri_offsets->uri_host_port_end = -1;

}
/* Code to parse a sip uri.
 * Returns Offset end off parsing or -1 for unsuccessful parsing
 * - sip_uri_offset_init() must have been called first.
 */
static gint
dissect_sip_uri(tvbuff_t *tvb, packet_info *pinfo _U_, gint start_offset,
                gint line_end_offset, uri_offset_info *uri_offsets)
{
    guchar c = '\0';
    gint current_offset;
    gint queried_offset;
    gint parameter_end_offset;
    gboolean in_ipv6 = 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;
    }
    /* Set uri start offset in case this was called directly */
    uri_offsets->uri_start = current_offset;

    /* Check if it's really a sip uri ( it might be a tel uri, parse that?) */
    if (tvb_strneql(tvb, current_offset, "sip", 3) != 0){
        if (uri_offsets->uri_end != -1) {
            /* We know where the URI ends, set the offsets*/
            return uri_offsets->name_addr_end;
        }
        return -1;
    }

    uri_offsets->uri_type = SIP_URI_TYPE_SIP;

    if(uri_offsets->uri_end == -1)
    {
        /* name-addr form was NOT used e.g no closing ">" */
        /* 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.
         */
        int end_offset;

        end_offset = tvb_ws_mempbrk_pattern_guint8(tvb, current_offset, line_end_offset - current_offset, &pbrk_comma_semi, NULL);

        if (end_offset != -1)
        {
            uri_offsets->uri_end = end_offset - 1;
        }
        else
        {
            /* 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 - 3; /* remove '\r\n' */
        }
        uri_offsets->name_addr_end = uri_offsets->uri_end;
    }

    /* Look for URI address parts (user, host, host-port) */

    /* 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 '@' = no user part */
        uri_offsets->uri_host_start = tvb_find_guint8(tvb, uri_offsets->uri_start, uri_offsets->uri_end - uri_offsets->uri_start, ':')+1;
    }
    else
    {
        /* with '@' = with user part */
        uri_offsets->uri_user_start = tvb_find_guint8(tvb, uri_offsets->uri_start, uri_offsets->uri_end - uri_offsets->uri_start, ':')+1;
        uri_offsets->uri_user_end = tvb_find_guint8(tvb, uri_offsets->uri_user_start, uri_offsets->uri_end - uri_offsets->uri_start, '@')-1;
        uri_offsets->uri_host_start = uri_offsets->uri_user_end + 2;
    }

    /* find URI-Host end*/
    parameter_end_offset = uri_offsets->uri_host_start;

    in_ipv6 = (tvb_get_guint8(tvb, parameter_end_offset) == '[');
    while (parameter_end_offset < line_end_offset)
    {
        parameter_end_offset++;
        parameter_end_offset = tvb_ws_mempbrk_pattern_guint8(tvb, parameter_end_offset, line_end_offset - parameter_end_offset, &pbrk_param_end_colon_brackets, &c);
        if (parameter_end_offset == -1)
        {
            parameter_end_offset = line_end_offset;
            break;
        }

        /* after adding character to this switch() , update also pbrk_param_end_colon_brackets */
        switch (c) {
            case '>':
            case ',':
                goto uri_host_end_found;
            case ';':
                uri_offsets->uri_parameters_start = parameter_end_offset + 1;
                goto uri_host_end_found;
            case '?':
            case ' ':
            case '\r':
                goto uri_host_end_found;
            case ':':
                if (!in_ipv6)
                    goto uri_host_end_found;
                break;
            case '[':
                in_ipv6 = TRUE;
                break;
            case ']':
                in_ipv6 = FALSE;
                break;
            default :
                DISSECTOR_ASSERT_NOT_REACHED();
                break;
        }
    }

uri_host_end_found:

    uri_offsets->uri_host_end = parameter_end_offset - 1;

    if (c == ':')
    {
        uri_offsets->uri_host_port_start = parameter_end_offset + 1;
        parameter_end_offset = uri_offsets->uri_host_port_start;
        while (parameter_end_offset < line_end_offset)
        {
            parameter_end_offset++;
            parameter_end_offset = tvb_ws_mempbrk_pattern_guint8(tvb, parameter_end_offset, line_end_offset - parameter_end_offset, &pbrk_param_end, &c);
            if (parameter_end_offset == -1)
            {
                parameter_end_offset = line_end_offset;
                break;
            }

            /* after adding character to this switch(), update also pbrk_param_end */
            switch (c) {
                case '>':
                case ',':
                    goto uri_host_port_end_found;
                case ';':
                    uri_offsets->uri_parameters_start = parameter_end_offset + 1;
                    goto uri_host_port_end_found;
                case '?':
                case ' ':
                case '\r':
                    goto uri_host_port_end_found;
                default :
                    DISSECTOR_ASSERT_NOT_REACHED();
                    break;
            }
        }

    uri_host_port_end_found:

        uri_offsets->uri_host_port_end = parameter_end_offset -1;
    }

    return uri_offsets->name_addr_end;
}

void
dfilter_store_sip_from_addr(tvbuff_t *tvb,proto_tree *tree,guint parameter_offset, guint parameter_len)
{
    proto_item *pi;

    pi = proto_tree_add_item(tree, hf_sip_from_addr, tvb, parameter_offset, parameter_len, ENC_UTF_8|ENC_NA);
    PROTO_ITEM_SET_GENERATED(pi);
}

static proto_item *
sip_proto_tree_add_uint(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start, gint length, gint value_offset, gint value_len)
{
    const char *str;
    unsigned long val;

    /* don't fetch string when field is not referenced */
    if (!proto_field_is_referenced(tree, hfindex))
        return tree;

    str = tvb_get_string_enc(wmem_packet_scope(), tvb, value_offset, value_len, ENC_UTF_8|ENC_NA);
    val = strtoul(str, NULL, 10);

    return proto_tree_add_uint(tree, hfindex, tvb, start, length, (guint32) val);
}

static proto_item *
sip_proto_tree_add_string(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start, gint length, gint value_offset, gint value_len)
{
    const char *str;

    /* don't fetch string when field is not referenced */
    if (!proto_field_is_referenced(tree, hfindex))
        return tree;

    str = tvb_get_string_enc(wmem_packet_scope(), tvb, value_offset, value_len, ENC_UTF_8|ENC_NA);

    return proto_tree_add_string(tree, hfindex, tvb, start, length, str);
}

static void
sip_proto_set_format_text(const proto_tree *tree, proto_item *item, tvbuff_t *tvb, int offset, int length)
{
    if (tree != item && item && PTREE_DATA(item)->visible)
        proto_item_set_text(item, "%s", tvb_format_text(tvb, offset, length));
}
/*
 * XXXX If/when more parameters are added consider doing something similar to what's done in
 * packet-magaco.c for find_megaco_localParam_names() possibly adding the hf to the array and have a generic
 * dissection function here.
 */
static void
dissect_sip_generic_parameters(tvbuff_t *tvb, proto_tree* tree, packet_info *pinfo _U_, gint current_offset, gint line_end_offset)
{
    gint semi_colon_offset, par_name_end_offset, equals_offset, length;
    /* Loop over the generic parameter(s)*/
    while (current_offset < line_end_offset) {
        gchar *param_name = NULL;

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

        semi_colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ';');

        if (semi_colon_offset == -1) {
            semi_colon_offset = line_end_offset;
        }

        length = semi_colon_offset - current_offset;

        /* Parse parameter and value */
        equals_offset = tvb_find_guint8(tvb, current_offset + 1, length, '=');
        if (equals_offset != -1) {
            /* Has value part */
            par_name_end_offset = equals_offset;
            /* Extract the parameter name */
            param_name = tvb_get_string_enc(wmem_packet_scope(), tvb, current_offset, par_name_end_offset - current_offset, ENC_UTF_8 | ENC_NA);
            /* Access-Info fields  */
            if ((param_name != NULL) && (g_ascii_strcasecmp(param_name, "service-priority") == 0)) {
                proto_tree_add_item(tree, hf_sip_service_priority, tvb,
                    equals_offset + 1, semi_colon_offset - equals_offset - 1, ENC_UTF_8 | ENC_NA);
            }
            else {
                proto_tree_add_format_text(tree, tvb, current_offset, length);
            }
        }
        else {
            proto_tree_add_format_text(tree, tvb, current_offset, length);
        }
        current_offset = semi_colon_offset + 1;
    }
}


/*
 *           History-Info = "History-Info" HCOLON
 *                            hi-entry *(COMMA hi-entry)
 *
 *          hi-entry = hi-targeted-to-uri *( SEMI hi-param )
 *          hi-targeted-to-uri= name-addr
 *
 *
 *          hi-param = hi-index / hi-extension
 *
 *          hi-index = "index" EQUAL 1*DIGIT *(DOT 1*DIGIT)
 *
 *          hi-extension = generic-param
 */

static gint
dissect_sip_history_info(tvbuff_t *tvb, proto_tree* tree, packet_info *pinfo _U_, gint current_offset,
                gint line_end_offset)
{
    int comma_offset;
    gboolean first_time = TRUE;

    /* split the line at the commas */
    while (line_end_offset > current_offset){
        comma_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ',');
        if(comma_offset == -1){
            if(first_time == TRUE){
                /* It was only on parameter no need to split it up */
                return line_end_offset;
            }
            /* Last parameter */
            comma_offset = line_end_offset;
        }
        first_time = FALSE;
        proto_tree_add_format_text(tree, tvb, current_offset, comma_offset-current_offset);

        current_offset = comma_offset+1;
    }

    return line_end_offset;

}


/*
 *    The syntax for the P-Charging-Function-Addresses header is described
 *   as follows:
 *
 *      P-Charging-Addr        = "P-Charging-Function-Addresses" HCOLON
 *                               charge-addr-params
 *                               *(SEMI charge-addr-params)
 *      charge-addr-params     = ccf / ecf / generic-param
 *      ccf                    = "ccf" EQUAL gen-value
 *      ecf                    = "ecf" EQUAL gen-value
 *      generic-param          =  token [ EQUAL gen-value ]
 *      gen-value              =  token / host / quoted-string
 *
 */

static gint
dissect_sip_p_charging_func_addresses(tvbuff_t *tvb, proto_tree* tree, packet_info *pinfo _U_, gint current_offset,
                gint line_end_offset)
{
    gint semi_offset, start_quote_offset, end_quote_offset;
    gboolean first_time = TRUE;

    while (line_end_offset > current_offset){
        /* Do we have a quoted string ? */
        start_quote_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, '"');
        if(start_quote_offset>0){
            /* Find end of quoted string */
            end_quote_offset = tvb_find_guint8(tvb, start_quote_offset+1, line_end_offset - (start_quote_offset+1), '"');
            /* Find parameter end */
            if (end_quote_offset>0)
                semi_offset = tvb_find_guint8(tvb, end_quote_offset+1, line_end_offset - (end_quote_offset+1), ';');
            else {
                /* XXX expert info about unterminated string */
                semi_offset = tvb_find_guint8(tvb, start_quote_offset+1, line_end_offset - (start_quote_offset+1), ';');
            }
        }else{
            /* Find parameter end */
            semi_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ';');
        }
        if(semi_offset == -1){
            if(first_time == TRUE){
            /* It was only one parameter no need to split it up */
            return line_end_offset;
            }
            /* Last parameter */
            semi_offset = line_end_offset;
        }
        first_time = FALSE;
        proto_tree_add_format_text(tree, tvb, current_offset, semi_offset-current_offset);

        current_offset = semi_offset+1;

    }

    return current_offset;
}

/*
 *  token         =  1*(alphanum / "-" / "." / "!" / "%" / "*"
 *                    / "_" / "+" / "`" / "'" / "~" )
 *  LWS           =  [*WSP CRLF] 1*WSP ; linear whitespace
 *  name-addr     =  [ display-name ] LAQUOT addr-spec RAQUOT
 *  addr-spec     =  SIP-URI / SIPS-URI / absoluteURI
 *  display-name  =  *(token LWS)/ quoted-string
 *  absoluteURI    =  scheme ":" ( hier-part / opaque-part )
 */

static gint
dissect_sip_name_addr_or_addr_spec(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;
    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 of URI */
    uri_offsets->uri_start = current_offset;
    if(uri_without_angle_quotes==FALSE){
        /* name-addr form was used */
        /* 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;
    }
    return dissect_sip_uri(tvb, pinfo, current_offset, line_end_offset, uri_offsets);
}


/*
* Code to add dissected SIP URI Information to proto tree
*/

static proto_tree *
display_sip_uri (tvbuff_t *tvb, proto_tree *sip_element_tree, packet_info *pinfo, uri_offset_info* uri_offsets, hf_sip_uri_t* uri)
{
    proto_item *ti;
    proto_tree *uri_item_tree = NULL;
    tvbuff_t *next_tvb;

    if(uri_offsets->display_name_end != uri_offsets->display_name_start) {
        proto_tree_add_item(sip_element_tree, hf_sip_display, tvb, uri_offsets->display_name_start,
                            uri_offsets->display_name_end - uri_offsets->display_name_start + 1, ENC_UTF_8|ENC_NA);
    }

    ti = proto_tree_add_item(sip_element_tree, *(uri->hf_sip_addr),
                             tvb, uri_offsets->uri_start, uri_offsets->uri_end - uri_offsets->uri_start + 1, ENC_UTF_8|ENC_NA);
    uri_item_tree = proto_item_add_subtree(ti, *(uri->ett_uri));

    if (uri_offsets->uri_type != SIP_URI_TYPE_SIP) {
        return ti;
    }

    if(uri_offsets->uri_user_end > uri_offsets->uri_user_start) {
        proto_tree_add_item(uri_item_tree, *(uri->hf_sip_user), tvb, uri_offsets->uri_user_start,
                            uri_offsets->uri_user_end - uri_offsets->uri_user_start + 1, ENC_UTF_8|ENC_NA);
        if (tvb_get_guint8(tvb, uri_offsets->uri_user_start) == '+') {
            dissect_e164_msisdn(tvb, uri_item_tree, uri_offsets->uri_user_start + 1, uri_offsets->uri_user_end - uri_offsets->uri_user_start, E164_ENC_UTF8);
        }

        /* If we have a SIP diagnostics sub dissector call it */
        if (sip_uri_userinfo_handle) {
            next_tvb = tvb_new_subset_length_caplen(tvb, uri_offsets->uri_user_start, uri_offsets->uri_user_end - uri_offsets->uri_user_start + 1,
                                      uri_offsets->uri_user_end - uri_offsets->uri_user_start + 1);
            call_dissector(sip_uri_userinfo_handle, next_tvb, pinfo, uri_item_tree);
        }

    }

    proto_tree_add_item(uri_item_tree, *(uri->hf_sip_host), tvb, uri_offsets->uri_host_start,
                        uri_offsets->uri_host_end - uri_offsets->uri_host_start + 1, ENC_UTF_8|ENC_NA);

    if(uri_offsets->uri_host_port_end > uri_offsets->uri_host_port_start) {
        proto_tree_add_item(uri_item_tree, *(uri->hf_sip_port), tvb, uri_offsets->uri_host_port_start,
                            uri_offsets->uri_host_port_end - uri_offsets->uri_host_port_start + 1, ENC_UTF_8|ENC_NA);
    }

    if (uri_offsets->uri_parameters_start != -1) {
        /* Move current offset to the start of the first param */
        gint current_offset          = uri_offsets->uri_parameters_start;
        gint uri_params_start_offset = current_offset;
        gint queried_offset;
        gint uri_param_end_offset = -1;
        gchar c;

        /* Put the contact parameters in the tree */

        while (current_offset < uri_offsets->name_addr_end) {
            queried_offset = tvb_ws_mempbrk_pattern_guint8(tvb, current_offset, uri_offsets->name_addr_end - current_offset, &pbrk_comma_semi, &c);

            if (queried_offset == -1) {
                /* Reached line end */
                /* Check if the line ends with a ">", if so decrement end offset. */
                c = tvb_get_guint8(tvb, uri_offsets->name_addr_end);

                if (c == '>') {
                    uri_param_end_offset = uri_offsets->name_addr_end - 1;
                } else {
                    uri_param_end_offset = uri_offsets->name_addr_end;
                }
                current_offset       = uri_offsets->name_addr_end;
            } else if (c==',') {
                uri_param_end_offset = queried_offset;
                current_offset       = queried_offset+1; /* must move forward */
            } else if (c==';') {
                /* More parameters */
                uri_param_end_offset = queried_offset-1;
                current_offset       = tvb_skip_wsp(tvb, queried_offset+1, uri_offsets->name_addr_end - queried_offset + 1);
            }

            proto_tree_add_item(uri_item_tree, *(uri->hf_sip_param), tvb, uri_params_start_offset ,
                uri_param_end_offset - uri_params_start_offset +1, ENC_UTF_8|ENC_NA);

            /* In case there are more parameters, point to the start of it */
            uri_params_start_offset = current_offset;
        }
    }

    return uri_item_tree;
}




/* Code to parse a contact header item
 * Returns Offset end off parsing or -1 for unsuccessful parsing
 * * contact-param  =  (name-addr / addr-spec) *(SEMI contact-params)
 */
static gint
dissect_sip_contact_item(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gint start_offset, gint line_end_offset,
            guchar* contacts_expires_0, guchar* contacts_expires_unknown)
{
    gchar c;
    gint current_offset;
    gint queried_offset;
    gint contact_params_start_offset = -1;
    /*gint contact_param_end_offset = -1;*/
    uri_offset_info uri_offsets;
    gboolean end_of_hdr = FALSE;
    gboolean has_expires_param = FALSE;

    /* 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;
    }

    /* Initialize the uri_offsets */
    sip_uri_offset_init(&uri_offsets);
    /* contact-param  =  (name-addr / addr-spec) *(SEMI contact-params) */
    current_offset = dissect_sip_name_addr_or_addr_spec(tvb, pinfo, start_offset, line_end_offset, &uri_offsets);
    if(current_offset == -1)
    {
        /* Parsing failed */
        return -1;
    }
    display_sip_uri(tvb, tree, pinfo, &uri_offsets, &sip_contact_uri);

    /* check if there's a comma before a ';', in which case we stop parsing this item at the comma */
    queried_offset = tvb_find_guint8(tvb, uri_offsets.uri_end, line_end_offset - uri_offsets.uri_end, ',');

    /* Check if we have contact parameters, the uri should be followed by a ';' */
    contact_params_start_offset = tvb_find_guint8(tvb, uri_offsets.uri_end, line_end_offset - uri_offsets.uri_end, ';');
    /* check if contact-params is present */
    if(contact_params_start_offset == -1) {
        /* no expires param */
        (*contacts_expires_unknown)++;
        return line_end_offset;
    }

    if (queried_offset != -1 && queried_offset < contact_params_start_offset) {
        /* no expires param */
        (*contacts_expires_unknown)++;
        return queried_offset;
    }

    /* Move current offset to the start of the first param */
    contact_params_start_offset++;
    current_offset = contact_params_start_offset;

    /* Put the contact parameters in the tree */

    queried_offset = current_offset;

    while(current_offset< line_end_offset){
        c = '\0';
        queried_offset++;
        queried_offset = (queried_offset < line_end_offset) ? tvb_ws_mempbrk_pattern_guint8(tvb, queried_offset, line_end_offset - queried_offset, &pbrk_header_end_dquote, &c) : -1;
        if (queried_offset != -1)
        {
            switch (c) {
                /* prevent tree from displaying the '\r\n' as part of the param */
                case '\r':
                case '\n':
                    end_of_hdr = TRUE;
                    /* fall through */
                case ',':
                case ';':
                case '"':
                    break;
                default :
                    DISSECTOR_ASSERT_NOT_REACHED();
                    break;
            }
        }

        if (queried_offset == -1) {
            /* Last parameter, line end */
            current_offset = line_end_offset;
        }else if(c=='"'){
            /* Do we have a quoted string ? */
            queried_offset = tvb_find_guint8(tvb, queried_offset+1, line_end_offset - queried_offset, '"');
            if(queried_offset==-1){
                /* We have an opening quote but no closing quote. */
                current_offset = line_end_offset;
            } else {
                current_offset = tvb_ws_mempbrk_pattern_guint8(tvb, queried_offset+1, line_end_offset - queried_offset, &pbrk_comma_semi, &c);
                if(current_offset==-1){
                    /* Last parameter, line end */
                    current_offset = line_end_offset;
                }
            }
        }else{
            current_offset = queried_offset;
        }
        proto_tree_add_item(tree, hf_sip_contact_param, tvb, contact_params_start_offset ,
            current_offset - contact_params_start_offset, ENC_UTF_8|ENC_NA);

        /* need to check for an 'expires' parameter
         * TODO: this should be done in a common way for all headers,
         * but To/From/etc do their own right now so doing the same here
         */
        /* also, this is a bad way of checking param names, but it's what To/From
         * etc, do. But legally "exPiRes = value" is also legit.
         */
        if (tvb_strncaseeql(tvb, contact_params_start_offset, "expires=", 8) == 0) {
            gint32 expire;
            /* if the expires param value is 0, then it's de-registering
             * this assumes the message is a REGISTER request/response, but these
             * contacts_expires_0/contacts_expires_unknown variables only get used then,
             * so that's ok
             */
            if (!ws_strtoi32(tvb_get_string_enc(wmem_packet_scope(), tvb, contact_params_start_offset+8,
                    current_offset - (contact_params_start_offset+8), ENC_UTF_8|ENC_NA), NULL, &expire))
                return contact_params_start_offset+8;
            if (expire == 0) {
                (*contacts_expires_0)++;
                /* it is actually unusual - arguably invalid - for a SIP REGISTER
                 * 200 OK _response_ to contain Contacts with expires=0.
                 *
                 * See Bug https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=10364
                 * Why this warning was removed (3GPP usage, 3GPP TS24.229 )
                 */
#if 0
                if (stat_info && stat_info->response_code > 199 && stat_info->response_code < 300) {
                    proto_tree_add_expert_format(tree, pinfo, &ei_sip_odd_register_response,
                        tvb, contact_params_start_offset, current_offset - contact_params_start_offset,
                        "SIP REGISTER %d response contains Contact with expires=0",
                        stat_info->response_code);
                }
#endif
            } else {
                has_expires_param = TRUE;
            }
        }

        /* In case there are more parameters, point to the start of it */
        contact_params_start_offset = current_offset+1;
        queried_offset = contact_params_start_offset;
        if (end_of_hdr) {
            /* '\r' or '\n' found, stop parsing and also set current offset to end
             * so the return value indicates we reached line end
             */
            current_offset = line_end_offset;
        }
        if (c == ',') {
            /* comma separator found, stop parsing of current contact-param here */
            break;
        }
    }

    if (!has_expires_param) {
        (*contacts_expires_unknown)++;
    }

    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, sip_authorization_t *authorization_info)
{
    gint current_offset, par_name_end_offset, queried_offset, value_offset, value_search_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;
    equals_offset = tvb_find_guint8(tvb, current_offset + 1, line_end_offset - (current_offset + 1), '=');
    if(equals_offset == -1){
        /* malformed parameter */
        return -1;
    }
    par_name_end_offset = equals_offset - 1;
    par_name_end_offset = tvb_skip_wsp_return(tvb,par_name_end_offset);

    /* Extract the parameter name */
    name = tvb_get_string_enc(wmem_packet_scope(), tvb, start_offset, par_name_end_offset-start_offset, ENC_UTF_8|ENC_NA);

    value_offset = tvb_skip_wsp(tvb, equals_offset + 1, line_end_offset - (equals_offset + 1));
    if (tvb_get_guint8(tvb, value_offset) == '\"') {
        /* quoted value */
        value_search_offset = value_offset;
        do {
            value_search_offset++;
            queried_offset = tvb_find_guint8 (tvb, value_search_offset, line_end_offset - value_search_offset, '\"');
        } while ((queried_offset != -1) && (tvb_get_guint8(tvb, queried_offset - 1) == '\\'));
        if (queried_offset == -1) {
            /* Closing quote not found, return line end */
            current_offset = line_end_offset;
        } else {
            /* Include closing quotes */
            current_offset = queried_offset + 1;
        }
    } else {
        /* unquoted value */
        queried_offset = tvb_find_guint8 (tvb, value_offset, line_end_offset - value_offset, ',');
        if (queried_offset == -1) {
            /* Last parameter, line end */
            current_offset = line_end_offset;
        } else {
            current_offset = queried_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,
                                value_offset, current_offset - value_offset,
                                ENC_UTF_8|ENC_NA);
            if (global_sip_validate_authorization) {
                gint real_value_offset = value_offset;
                gint real_value_length = current_offset - value_offset;
                if ((tvb_get_guint8(tvb, value_offset) == '\"') && (tvb_get_guint8(tvb, current_offset - 1) == '\"') && (real_value_length > 1)) {
                    real_value_offset++;
                    real_value_length -= 2;
                }
                if (g_ascii_strcasecmp(name, "response") == 0) {
                    authorization_info->response = tvb_get_string_enc(wmem_packet_scope(), tvb, real_value_offset, real_value_length, ENC_ASCII);
                } else if (g_ascii_strcasecmp(name, "nc") == 0) {
                    authorization_info->nonce_count = tvb_get_string_enc(wmem_packet_scope(), tvb, real_value_offset, real_value_length, ENC_ASCII);
                } else if (g_ascii_strcasecmp(name, "username") == 0) {
                    authorization_info->username = tvb_get_string_enc(wmem_packet_scope(), tvb, real_value_offset, real_value_length, ENC_ASCII);
                } else if (g_ascii_strcasecmp(name, "realm") == 0) {
                    authorization_info->realm = tvb_get_string_enc(wmem_packet_scope(), tvb, real_value_offset, real_value_length, ENC_ASCII);
                } else if (g_ascii_strcasecmp(name, "algorithm") == 0) {
                    authorization_info->algorithm = tvb_get_string_enc(wmem_packet_scope(), tvb, real_value_offset, real_value_length, ENC_ASCII);
                } else if (g_ascii_strcasecmp(name, "nonce") == 0) {
                    authorization_info->nonce = tvb_get_string_enc(wmem_packet_scope(), tvb, real_value_offset, real_value_length, ENC_ASCII);
                } else if (g_ascii_strcasecmp(name, "qop") == 0) {
                    authorization_info->qop = tvb_get_string_enc(wmem_packet_scope(), tvb, real_value_offset, real_value_length, ENC_ASCII);
                } else if (g_ascii_strcasecmp(name, "cnonce") == 0) {
                    authorization_info->cnonce = tvb_get_string_enc(wmem_packet_scope(), tvb, real_value_offset, real_value_length, ENC_ASCII);
                } else if (g_ascii_strcasecmp(name, "uri") == 0) {
                    authorization_info->uri = tvb_get_string_enc(wmem_packet_scope(), tvb, real_value_offset, real_value_length, ENC_ASCII);
                }
            }
            break;
        }
    }

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

    /* Find comma/end of line */
    queried_offset = tvb_find_guint8 (tvb, current_offset, line_end_offset - current_offset, ',');
    if (queried_offset == -1) {
        current_offset = line_end_offset;
    } else {
        current_offset = queried_offset;
    }
    return current_offset;
}

/* Dissect the details of a Reason header
 * Reason            =  "Reason" HCOLON reason-value *(COMMA reason-value)
 * reason-value      =  protocol *(SEMI reason-params)
 * protocol          =  "SIP" / "Q.850" / token
 * reason-params     =  protocol-cause / reason-text / reason-extension
 * protocol-cause    =  "cause" EQUAL cause
 * cause             =  1*DIGIT
 * reason-text       =  "text" EQUAL quoted-string
 * reason-extension  =  generic-param
 */
static void
dissect_sip_reason_header(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gint start_offset, gint line_end_offset){

    gint  current_offset, semi_colon_offset, length, end_quote_offset;
    const guint8 *param_name = NULL;
    guint cause_value;
    sip_reason_code_info_t sip_reason_code_info;

        /* 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, ';');

    if(semi_colon_offset == -1)
        return;

    length = semi_colon_offset - current_offset;
    proto_tree_add_item_ret_string(tree, hf_sip_reason_protocols, tvb, start_offset, length, ENC_UTF_8|ENC_NA, wmem_packet_scope(), &param_name);
    current_offset = tvb_find_guint8(tvb, semi_colon_offset, line_end_offset - semi_colon_offset, '=') + 1;
    /* Do we have a text parameter too? */
    semi_colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ';');

    if (semi_colon_offset == -1){
        length = line_end_offset - current_offset;
    } else {
        /* Text parmeter exist, set length accordingly */
        length = semi_colon_offset - current_offset;
    }

    /* Get cause value */
    cause_value = (guint)strtoul(tvb_get_string_enc(wmem_packet_scope(), tvb, current_offset, length, ENC_UTF_8 | ENC_NA), NULL, 10);

    if (g_ascii_strcasecmp(param_name, "Q.850") == 0){
        proto_tree_add_uint(tree, hf_sip_reason_cause_q850, tvb, current_offset, length, cause_value);
        sip_reason_code_info.protocol_type_num = SIP_PROTO_Q850;
    }
    else if (g_ascii_strcasecmp(param_name, "SIP") == 0) {
        proto_tree_add_uint(tree, hf_sip_reason_cause_sip, tvb, current_offset, length, cause_value);
        sip_reason_code_info.protocol_type_num = SIP_PROTO_SIP;
    }
    else {
        proto_tree_add_uint(tree, hf_sip_reason_cause_other, tvb, current_offset, length, cause_value);
        sip_reason_code_info.protocol_type_num = SIP_PROTO_OTHER;
    }

    if (semi_colon_offset == -1)
        /* Nothing to parse */
        return;

    /* reason-text       =  "text" EQUAL quoted-string */
    current_offset = tvb_find_guint8(tvb, semi_colon_offset, line_end_offset - semi_colon_offset, '"') + 1;
    if (current_offset == -1)
        /* Nothing to parse */
        return;
    end_quote_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, '"');
    if (end_quote_offset == -1)
        /* Nothing to parse */
        return;
    length = end_quote_offset - current_offset;
    proto_tree_add_item(tree, hf_sip_reason_text, tvb, current_offset, length, ENC_UTF_8 | ENC_NA);

    if (sip_reason_code_handle) {
        tvbuff_t *next_tvb;

        sip_reason_code_info.cause_value = cause_value;

        next_tvb = tvb_new_subset_length(tvb, current_offset, length);
        call_dissector_with_data(sip_reason_code_handle, next_tvb, pinfo, tree, &sip_reason_code_info);
    }
}

/* Dissect the details of a security client header
 * sec-mechanism    = mechanism-name *(SEMI mech-parameters)
 *     mech-parameters  = ( preference / digest-algorithm /
 *                          digest-qop / digest-verify / extension )
 *    preference       = "q" EQUAL qvalue
 *    qvalue           = ( "0" [ "." 0*3DIGIT ] )
 *                        / ( "1" [ "." 0*3("0") ] )
 *    digest-algorithm = "d-alg" EQUAL token
 *    digest-qop       = "d-qop" EQUAL token
 *    digest-verify    = "d-ver" EQUAL LDQUOT 32LHEX RDQUOT
 *    extension        = generic-param
 *
 *
 */
static void
dissect_sip_sec_mechanism(tvbuff_t *tvb, packet_info* pinfo, proto_tree *tree, gint start_offset, gint line_end_offset){

    gint  current_offset, semi_colon_offset, length, par_name_end_offset, equals_offset;
    guint32 spi_c;
    guint32 spi_s;
    guint16 port;

    /* 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, ';');
    if(semi_colon_offset == -1){
        semi_colon_offset = line_end_offset;
    }

    length = semi_colon_offset-current_offset;
    proto_tree_add_item(tree, hf_sip_sec_mechanism, tvb,
                                start_offset, length,
                                ENC_UTF_8|ENC_NA);

    current_offset = current_offset + length + 1;


    while(current_offset < line_end_offset){
        gchar *param_name = NULL, *value = NULL;

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

        semi_colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset-current_offset, ';');

        if(semi_colon_offset == -1){
            semi_colon_offset = line_end_offset;
        }

        length = semi_colon_offset - current_offset;

        /* Parse parameter and value */
        equals_offset = tvb_find_guint8(tvb, current_offset + 1, length, '=');
        if(equals_offset != -1){
            /* Has value part */
            par_name_end_offset = equals_offset;
            /* Extract the parameter name */
            param_name = tvb_get_string_enc(wmem_packet_scope(), tvb, current_offset, par_name_end_offset-current_offset, ENC_UTF_8|ENC_NA);
            /* Extract the value */
            value = tvb_get_string_enc(wmem_packet_scope(), tvb, equals_offset+1, semi_colon_offset-equals_offset+1, ENC_UTF_8|ENC_NA);
        }



        /* Protection algorithm to be used */
        if (g_ascii_strcasecmp(param_name, "alg") == 0){
            proto_tree_add_item(tree, hf_sip_sec_mechanism_alg, tvb,
                                equals_offset+1, semi_colon_offset-equals_offset-1,
                                ENC_UTF_8|ENC_NA);

        }else if (g_ascii_strcasecmp(param_name, "ealg") == 0){
            proto_tree_add_item(tree, hf_sip_sec_mechanism_ealg, tvb,
                                equals_offset+1, semi_colon_offset-equals_offset-1,
                                ENC_UTF_8|ENC_NA);

        }else if (g_ascii_strcasecmp(param_name, "prot") == 0){
            proto_tree_add_item(tree, hf_sip_sec_mechanism_prot, tvb,
                                equals_offset+1, semi_colon_offset-equals_offset-1,
                                ENC_UTF_8|ENC_NA);

        }else if (g_ascii_strcasecmp(param_name, "spi-c") == 0){
            if (!value) {
                proto_tree_add_expert(tree, pinfo, &ei_sip_sipsec_malformed,
                                        tvb, current_offset, -1);
            } else {
                spi_c = (guint32)strtoul(value, NULL, 10);
                proto_tree_add_uint(tree, hf_sip_sec_mechanism_spi_c, tvb,
                                    equals_offset+1, semi_colon_offset-equals_offset-1, spi_c);
            }
        }else if (g_ascii_strcasecmp(param_name, "spi-s") == 0){
            if (!value) {
                proto_tree_add_expert(tree, pinfo, &ei_sip_sipsec_malformed,
                                        tvb, current_offset, -1);
            } else {
                spi_s = (guint32)strtoul(value, NULL, 10);
                proto_tree_add_uint(tree, hf_sip_sec_mechanism_spi_s, tvb,
                                    equals_offset+1, semi_colon_offset-equals_offset-1, spi_s);
            }
        }else if (g_ascii_strcasecmp(param_name, "port1") == 0){
            if (!value) {
                proto_tree_add_expert(tree, pinfo, &ei_sip_sipsec_malformed,
                                        tvb, current_offset, -1);
            } else {
                port = (guint16)strtoul(value, NULL, 10);
                proto_tree_add_uint(tree, hf_sip_sec_mechanism_port1, tvb,
                                    equals_offset+1, semi_colon_offset-equals_offset-1, port);
            }
        }else if (g_ascii_strcasecmp(param_name, "port-c") == 0){
            if (!value) {
                proto_tree_add_expert(tree, pinfo, &ei_sip_sipsec_malformed,
                                        tvb, current_offset, -1);
            } else {
                port = (guint32)strtoul(value, NULL, 10);
                proto_tree_add_uint(tree, hf_sip_sec_mechanism_port_c, tvb,
                                    equals_offset+1, semi_colon_offset-equals_offset-1, port);
            }
        }else if (g_ascii_strcasecmp(param_name, "port2") == 0){
            if (!value) {
                proto_tree_add_expert(tree, pinfo, &ei_sip_sipsec_malformed,
                                        tvb, current_offset, -1);
            } else {
                port = (guint32)strtoul(value, NULL, 10);
                proto_tree_add_uint(tree, hf_sip_sec_mechanism_port2, tvb,
                                    equals_offset+1, semi_colon_offset-equals_offset-1, port);
            }
        }else if (g_ascii_strcasecmp(param_name, "port-s") == 0){
            if (!value) {
                proto_tree_add_expert(tree, pinfo, &ei_sip_sipsec_malformed,
                                        tvb, current_offset, -1);
            } else {
                port = (guint32)strtoul(value, NULL, 10);
                proto_tree_add_uint(tree, hf_sip_sec_mechanism_port_s, tvb,
                                    equals_offset+1, semi_colon_offset-equals_offset-1, port);
            }
        }

        else{
            proto_tree_add_format_text(tree, tvb, current_offset, length);
        }
        current_offset = semi_colon_offset+1;
    }

}

/* Dissect the details of a Route (and Record-Route) header */
static void dissect_sip_route_header(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, hf_sip_uri_t *sip_route_uri_p, gint start_offset, gint line_end_offset)
{
    gint current_offset;
    uri_offset_info uri_offsets;

    current_offset = start_offset;

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

    if (current_offset >= line_end_offset) {
        return;
    }

    while (current_offset < line_end_offset) {
        current_offset = tvb_find_guint8(tvb, current_offset, (line_end_offset - 1) - current_offset, ',');

        if (current_offset != -1) { /* found any ',' ? */
            sip_uri_offset_init(&uri_offsets);
            current_offset = dissect_sip_name_addr_or_addr_spec(tvb, pinfo, start_offset, current_offset, &uri_offsets);
            if(current_offset == -1)
                return;
            display_sip_uri(tvb, tree, pinfo, &uri_offsets, sip_route_uri_p);

            current_offset++;
            start_offset = current_offset + 1;

        } else {
            /* current_offset = (line_end_offset - 1); */

            sip_uri_offset_init(&uri_offsets);
            current_offset = dissect_sip_name_addr_or_addr_spec(tvb, pinfo, start_offset, line_end_offset, &uri_offsets);
            if(current_offset == -1)
                return;
            display_sip_uri(tvb, tree, pinfo, &uri_offsets, sip_route_uri_p);

            return;
        }

        current_offset++;
    }

    return;
}

/* Dissect the details of a Via header
 *
 * Via               =  ( "Via" / "v" ) HCOLON via-parm *(COMMA via-parm)
 * via-parm          =  sent-protocol LWS sent-by *( SEMI via-params )
 * via-params        =  via-ttl / via-maddr
 *                      / via-received / via-branch
 *                      / via-extension
 * via-ttl           =  "ttl" EQUAL ttl
 * via-maddr         =  "maddr" EQUAL host
 * via-received      =  "received" EQUAL (IPv4address / IPv6address)
 * via-branch        =  "branch" EQUAL token
 * via-extension     =  generic-param
 * sent-protocol     =  protocol-name SLASH protocol-version
 *                      SLASH transport
 * protocol-name     =  "SIP" / token
 * protocol-version  =  token
 * transport         =  "UDP" / "TCP" / "TLS" / "SCTP"
 *                      / other-transport
 * sent-by           =  host [ COLON port ]
 * ttl               =  1*3DIGIT ; 0 to 255
 *
 */
static void dissect_sip_via_header(tvbuff_t *tvb, proto_tree *tree, gint start_offset, gint line_end_offset, packet_info *pinfo)
{
    gint  current_offset;
    gint  address_start_offset;
    gint  semicolon_offset;
    gboolean colon_seen;
    gboolean ipv6_reference;
    gboolean ipv6_address;
    guchar c;
    gchar *param_name = NULL;

    current_offset = start_offset;

    while (1)
    {
        /* Reset flags and counters */
        semicolon_offset = 0;
        ipv6_reference = FALSE;
        ipv6_address = FALSE;
        colon_seen = FALSE;

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

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

        /* Now look for the end of the SIP/2.0/transport parameter.
         *  There may be spaces between the slashes
         *  sent-protocol     =  protocol-name SLASH protocol-version
         *                       SLASH transport
         */

        current_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, '/');
        if (current_offset != -1)
        {
            current_offset++;
            current_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, '/');
        }

        if (current_offset != -1)
        {
            current_offset++;
            /* skip Spaces and Tabs */
            current_offset = tvb_skip_wsp(tvb, current_offset, line_end_offset - current_offset);
        } else
            current_offset = line_end_offset;


        /* We should now be at the start of the first transport name (or at the end of the line) */

        /*
         * transport         =  "UDP" / "TCP" / "TLS" / "SCTP"
         *                      / other-transport
         */
        while (current_offset < line_end_offset)
        {
            int transport_start_offset = current_offset;

            current_offset = tvb_ws_mempbrk_pattern_guint8(tvb, current_offset, line_end_offset - current_offset, &pbrk_tab_sp_fslash, &c);
            if (current_offset != -1){
                proto_tree_add_item(tree, hf_sip_via_transport, tvb, transport_start_offset,
                                    current_offset - transport_start_offset, ENC_UTF_8|ENC_NA);
                /* Check if we have more transport parameters */
                if(c=='/'){
                    current_offset++;
                    continue;
                }
                current_offset = tvb_skip_wsp(tvb, current_offset, line_end_offset - current_offset);
                c = tvb_get_guint8(tvb, current_offset);
                if(c=='/'){
                    current_offset++;
                    continue;
                }
                break;
            }else{
                current_offset = line_end_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)
        {
            current_offset = tvb_ws_mempbrk_pattern_guint8(tvb, current_offset, line_end_offset - current_offset, &pbrk_addr_end, &c);
            if (current_offset == -1)
            {
                current_offset = line_end_offset;
                break;
            }

            if (c == '[') {
                ipv6_reference = TRUE;
                ipv6_address = TRUE;
            }
            else if (c == ']')
            {
                ipv6_reference = FALSE;
            }

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

            current_offset++;
        }
        /* Add address to tree */
        if (ipv6_address == TRUE) {
            proto_tree_add_item(tree, hf_sip_via_sent_by_address, tvb, address_start_offset + 1,
                                current_offset - address_start_offset - 2, ENC_UTF_8|ENC_NA);
        } else {
            proto_tree_add_item(tree, hf_sip_via_sent_by_address, tvb, address_start_offset,
                                current_offset - address_start_offset, ENC_UTF_8|ENC_NA);
        }

        /* 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;
            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 (!g_ascii_isdigit(c))
                {
                    if (current_offset > port_offset)
                    {
                        /* Add address port number to tree */
                        guint16 port;
                        gboolean port_valid;
                        proto_item* pi;
                        port_valid = ws_strtou16(tvb_get_string_enc(wmem_packet_scope(), tvb, port_offset,
                            current_offset - port_offset, ENC_UTF_8|ENC_NA), NULL, &port);
                        pi = proto_tree_add_uint(tree, hf_sip_via_sent_by_port, tvb, port_offset,
                                            current_offset - port_offset, port);
                        if (!port_valid)
                            expert_add_info(pinfo, pi, &ei_sip_via_sent_by_port);
                    }
                    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;
            gboolean found_end_of_parameters = FALSE;
            gint parameter_name_end = 0;
            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'))
                {
                    found_end_of_parameters = TRUE;
                    break;
                }
                current_offset++;
            }

            if (found_end_of_parameters)
            {
                break;
            }

            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 (!g_ascii_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 */
            current_offset = tvb_ws_mempbrk_pattern_guint8(tvb, current_offset, line_end_offset - current_offset, &pbrk_via_param_end, NULL);
            if (current_offset == -1)
                current_offset = line_end_offset;

            /* Note parameter name */
            param_name = tvb_get_string_enc(wmem_packet_scope(), tvb, semicolon_offset+1,
                                                  parameter_name_end - semicolon_offset - 1, ENC_UTF_8|ENC_NA);

            /* 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,
                            ENC_UTF_8 | ENC_NA);

                        if (sip_via_branch_handle && g_ascii_strcasecmp(param_name, "branch") == 0)
                        {
                            tvbuff_t *next_tvb;
                            next_tvb = tvb_new_subset_length_caplen(tvb, parameter_name_end + 1, current_offset - parameter_name_end - 1, current_offset - parameter_name_end - 1);

                            call_dissector(sip_via_branch_handle, next_tvb, pinfo, tree);
                        }
                        else if (g_ascii_strcasecmp(param_name, "oc") == 0) {
                            proto_item *ti;
                            char *value = tvb_get_string_enc(wmem_packet_scope(), tvb, parameter_name_end + 1,
                                current_offset - parameter_name_end - 1, ENC_UTF_8 | ENC_NA);
                            ti = proto_tree_add_uint(tree, hf_sip_via_oc_val, tvb,
                                parameter_name_end + 1, current_offset - parameter_name_end - 1,
                                (guint32)strtoul(value, NULL, 10));
                            PROTO_ITEM_SET_GENERATED(ti);
                        }
                        else if (g_ascii_strcasecmp(param_name, "oc-seq") == 0) {
                            proto_item *ti;
                            nstime_t ts;
                            int dec_p_off = tvb_find_guint8(tvb, parameter_name_end + 1, - 1, '.');
                            char *value;

                            if(dec_p_off > 0){
                                value = tvb_get_string_enc(wmem_packet_scope(), tvb,
                                    parameter_name_end + 1, dec_p_off - parameter_name_end, ENC_UTF_8 | ENC_NA);
                                ts.secs = (guint32)strtoul(value, NULL, 10);
                                value = tvb_get_string_enc(wmem_packet_scope(), tvb,
                                    dec_p_off + 1, current_offset - parameter_name_end - 1, ENC_UTF_8 | ENC_NA);
                                ts.nsecs = (guint32)strtoul(value, NULL, 10) * 1000;
                                ti = proto_tree_add_time(tree, hf_sip_oc_seq_timestamp, tvb,
                                    parameter_name_end + 1, current_offset - parameter_name_end - 1, &ts);
                                PROTO_ITEM_SET_GENERATED(ti);
                            }
                        }
                    }
                    else
                    {
                        proto_tree_add_item(tree, *(via_parameter->hf_item), tvb,
                                            semicolon_offset+1, current_offset-semicolon_offset-1,
                                            ENC_UTF_8|ENC_NA);
                    }
                    break;
                }
            }

            /* If not matched, just add as text... */
            if (i == array_length(via_parameters_hf_array))
            {
                proto_tree_add_format_text(tree, 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);

            /* There may be a comma, followed by more Via entries... */
            if (current_offset < line_end_offset)
            {
                c = tvb_get_guint8(tvb, current_offset);
                if (c == ',')
                {
                    /* Skip it and get out of parameter loop */
                    current_offset++;
                    break;
                }
            }
        }
    }
}

/* Dissect the details of a Session-ID header
 *
 * Session-ID           =  "Session-ID" HCOLON sess-id
 *                         *( SEMI generic-param )
 * sess-id              =  32(DIGIT / %x61-66)  ; 32 chars of [0-9a-f]
 */
static void dissect_sip_session_id_header(tvbuff_t *tvb, proto_tree *tree, gint start_offset, gint line_end_offset)
{
    gint current_offset, semi_colon_offset, equals_offset, length;
    GByteArray *bytes;
    proto_item *pi;

    current_offset = start_offset;
    semi_colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset-current_offset, ';');
    if(semi_colon_offset == -1){
        semi_colon_offset = line_end_offset;
    }

    length = semi_colon_offset-current_offset;
    bytes = g_byte_array_sized_new(16);
    pi = proto_tree_add_bytes_item(tree, hf_sip_session_id_sess_id, tvb,
                              start_offset, length, ENC_UTF_8|ENC_STR_HEX|ENC_SEP_NONE,
                              bytes, NULL, NULL);

    current_offset = current_offset + length + 1;

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

    if (current_offset < line_end_offset) {
        /* Parse parameter and value */
        equals_offset = tvb_find_guint8(tvb, current_offset + 1, length, '=');
        if (equals_offset != -1) {
            /* Extract the parameter name */
            GByteArray *uuid = g_byte_array_sized_new(16);
            guint8 *param_name = tvb_get_string_enc(wmem_packet_scope(), tvb, current_offset,
                                                    tvb_skip_wsp_return(tvb, equals_offset - 1) - current_offset,
                                                    ENC_UTF_8|ENC_NA);

            if ((bytes->len == 16) && (g_ascii_strcasecmp(param_name, "remote") == 0) &&
                tvb_get_string_bytes(tvb, equals_offset + 1, line_end_offset - equals_offset - 1,
                                     ENC_UTF_8|ENC_STR_HEX|ENC_SEP_NONE, uuid, NULL) &&
                (uuid->len == 16)) {
                /* Decode header as draft-ietf-insipid-session-id
                 *
                 * session-id          = "Session-ID" HCOLON session-id-value
                 * session-id-value    = local-uuid *(SEMI sess-id-param)
                 * local-uuid          = sess-uuid / null
                 * remote-uuid         = sess-uuid / null
                 * sess-uuid           = 32(DIGIT / %x61-66)  ;32 chars of [0-9a-f]
                 * sess-id-param       = remote-param / generic-param
                 * remote-param        = "remote" EQUAL remote-uuid
                 * null                = 32("0")
                 */
                e_guid_t guid;

                PROTO_ITEM_SET_HIDDEN(pi);
                guid.data1 = (bytes->data[0] << 24) | (bytes->data[1] << 16) |
                             (bytes->data[2] <<  8) |  bytes->data[3];
                guid.data2 = (bytes->data[4] <<  8) |  bytes->data[5];
                guid.data3 = (bytes->data[6] <<  8) |  bytes->data[7];
                memcpy(guid.data4, &bytes->data[8], 8);
                proto_tree_add_guid(tree, hf_sip_session_id_local_uuid, tvb,
                                    start_offset, semi_colon_offset - start_offset, &guid);
                guid.data1 = (uuid->data[0] << 24) | (uuid->data[1] << 16) |
                             (uuid->data[2] <<  8) |  uuid->data[3];
                guid.data2 = (uuid->data[4] <<  8) |  uuid->data[5];
                guid.data3 = (uuid->data[6] <<  8) |  uuid->data[7];
                memcpy(guid.data4, &uuid->data[8], 8);
                proto_tree_add_guid(tree, hf_sip_session_id_remote_uuid, tvb,
                                    equals_offset + 1, line_end_offset - equals_offset - 1, &guid);
            } else {
                /* Display generic parameter */
                proto_tree_add_item(tree, hf_sip_session_id_param, tvb, current_offset,
                                    line_end_offset - current_offset, ENC_UTF_8|ENC_NA);
            }
            g_byte_array_free(uuid, TRUE);
        } else {
            /* Display generic parameter */
            proto_tree_add_item(tree, hf_sip_session_id_param, tvb, current_offset,
                                line_end_offset - current_offset, ENC_UTF_8|ENC_NA);
        }
    }

    g_byte_array_free(bytes, TRUE);
}

/* Dissect the headers for P-Access-Network-Info Headers
 *
 *  Spec found in 3GPP 24.229 7.2A.4
 *  P-Access-Network-Info  = "P-Access-Network-Info" HCOLON
 *  access-net-spec *(COMMA access-net-spec)
 *  access-net-spec        = (access-type / access-class) *(SEMI access-info)
 *  access-type            = "IEEE-802.11" / "IEEE-802.11a" / "IEEE-802.11b" / "IEEE-802.11g" / "IEEE-802.11n" / "3GPP-GERAN" /
 *                           "3GPP-UTRAN-FDD" / "3GPP-UTRAN-TDD" / "3GPP-E-UTRAN-FDD" / "3GPP-E-UTRAN-TDD" / "ADSL" / "ADSL2" /
 *                           "ADSL2+" / "RADSL" / "SDSL" / "HDSL" / "HDSL2" / "G.SHDSL" / "VDSL" / "IDSL" / "3GPP2-1X" /
 *                           "3GPP2-1X-Femto" / "3GPP2-1X-HRPD" / "3GPP2-UMB" / "DOCSIS" / "IEEE-802.3" / "IEEE-802.3a" /
 *                           "IEEE-802.3e" / "IEEE-802.3i" / "IEEE-802.3j" / "IEEE-802.3u" / "IEEE-802.3ab"/ "IEEE-802.3ae" /
 *                           "IEEE-802.3ak" / "IEEE-802.3aq" / "IEEE-802.3an" / "IEEE-802.3y" / "IEEE-802.3z" / "GPON" /
                             "XGPON1" / "GSTN"/ token
 *  access-class           = "3GPP-GERAN" / "3GPP-UTRAN" / "3GPP-E-UTRAN" / "3GPP-WLAN" / "3GPP-GAN" / "3GPP-HSPA" / token
 *  np                     = "network-provided"
 *  access-info            = cgi-3gpp / utran-cell-id-3gpp / dsl-location / i-wlan-node-id / ci-3gpp2 / ci-3gpp2-femto /
                             eth-location / fiber-location / np / gstn-location / extension-access-info
 *  extension-access-info  = gen-value
 *  cgi-3gpp               = "cgi-3gpp" EQUAL (token / quoted-string)
 *  utran-cell-id-3gpp     = "utran-cell-id-3gpp" EQUAL (token / quoted-string)
 *  i-wlan-node-id         = "i-wlan-node-id" EQUAL (token / quoted-string)
 *  dsl-location           = "dsl-location" EQUAL (token / quoted-string)
 *  eth-location           = "eth-location" EQUAL (token / quoted-string)
 *  fiber-location         = "fiber-location" EQUAL (token / quoted-string)
 *  ci-3gpp2               = "ci-3gpp2" EQUAL (token / quoted-string)
 *  ci-3gpp2-femto         = "ci-3gpp2-femto" EQUAL (token / quoted-string)
 *  gstn-location          = "gstn-location" EQUAL (token / quoted-string)
 *
 */
static void dissect_sip_p_access_network_info_header(tvbuff_t *tvb, proto_tree *tree, gint start_offset, gint line_end_offset)
{

    gint  current_offset, semi_colon_offset, length, par_name_end_offset, equals_offset;

    /* 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;
    }

    /* Get the Access Type / Access Class*/
    current_offset = start_offset;
    semi_colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ';');

    if (semi_colon_offset == -1)
        return;

    length = semi_colon_offset - current_offset;
    proto_tree_add_item(tree, hf_sip_p_acc_net_i_acc_type, tvb, start_offset, length, ENC_UTF_8 | ENC_NA);

    current_offset = current_offset + length + 1;


    while (current_offset < line_end_offset){
        gchar *param_name = NULL;

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

        semi_colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ';');

        if (semi_colon_offset == -1){
            semi_colon_offset = line_end_offset;
        }

        length = semi_colon_offset - current_offset;

        /* Parse parameter and value */
        equals_offset = tvb_find_guint8(tvb, current_offset + 1, length, '=');
        if (equals_offset != -1){
            /* Has value part */
            par_name_end_offset = equals_offset;
            /* Extract the parameter name */
            param_name = tvb_get_string_enc(wmem_packet_scope(), tvb, current_offset, par_name_end_offset - current_offset, ENC_UTF_8 | ENC_NA);
            /* Access-Info fields  */
            if ((param_name != NULL)&&(g_ascii_strcasecmp(param_name, "utran-cell-id-3gpp") == 0)) {
                proto_tree_add_item(tree, hf_sip_p_acc_net_i_ucid_3gpp, tvb,
                    equals_offset + 1, semi_colon_offset - equals_offset - 1, ENC_UTF_8 | ENC_NA);
            }
            else {
                proto_tree_add_format_text(tree, tvb, current_offset, length);
            }
        }
        else {
            proto_tree_add_format_text(tree, tvb, current_offset, length);
        }

        current_offset = semi_colon_offset + 1;
    }
}

/*
The syntax for the P-Charging-Vector header field is described as
follows:

    P-Charging-Vector  = "P-Charging-Vector" HCOLON icid-value
    *(SEMI charge-params)
    charge-params      = icid-gen-addr / orig-ioi / term-ioi /
    transit-ioi / related-icid /
    related-icid-gen-addr / generic-param
    icid-value                = "icid-value" EQUAL gen-value
    icid-gen-addr             = "icid-generated-at" EQUAL host
    orig-ioi                  = "orig-ioi" EQUAL gen-value
    term-ioi                  = "term-ioi" EQUAL gen-value
    transit-ioi               = "transit-ioi" EQUAL transit-ioi-list
    transit-ioi-list          = DQUOTE transit-ioi-param
    *(COMMA transit-ioi-param) DQUOTE
    transit-ioi-param         = transit-ioi-indexed-value /
    transit-ioi-void-value
    transit-ioi-indexed-value = transit-ioi-name "."
    transit-ioi-index
    transit-ioi-name          = ALPHA *(ALPHA / DIGIT)
    transit-ioi-index         = 1*DIGIT
    transit-ioi-void-value    = "void"
    related-icid              = "related-icid" EQUAL gen-value
    related-icid-gen-addr     = "related-icid-generated-at" EQUAL host
*/
static void
dissect_sip_p_charging_vector_header(tvbuff_t *tvb, proto_tree *tree, gint start_offset, gint line_end_offset)
{

    gint  current_offset, semi_colon_offset, length, equals_offset;

    /* 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;
    }

    /* icid-value                = "icid-value" EQUAL gen-value */
    current_offset = start_offset;
    semi_colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ';');

    if (semi_colon_offset == -1) {
        /* No parameters, is that allowed?*/
        semi_colon_offset = line_end_offset;
    }

    length = semi_colon_offset - current_offset;

    /* Parse parameter and value */
    equals_offset = tvb_find_guint8(tvb, current_offset + 1, length, '=');
    if (equals_offset == -1) {
        /* Does not conform to ABNF */
        return;
    }

    /* Get the icid-value */
    proto_tree_add_item(tree, hf_sip_icid_value, tvb,
        equals_offset + 1, semi_colon_offset - equals_offset - 1, ENC_UTF_8 | ENC_NA);

    current_offset = semi_colon_offset + 1;

    /* Add the rest of the parameters to the tree */
    while (current_offset < line_end_offset) {
        gchar *param_name = NULL;
        gint par_name_end_offset;
        /* skip Spaces and Tabs */
        current_offset = tvb_skip_wsp(tvb, current_offset, line_end_offset - current_offset);

        semi_colon_offset = tvb_find_guint8(tvb, current_offset, line_end_offset - current_offset, ';');

        if (semi_colon_offset == -1) {
            semi_colon_offset = line_end_offset;
        }

        length = semi_colon_offset - current_offset;

        /* Parse parameter and value */
        equals_offset = tvb_find_guint8(tvb, current_offset + 1, length, '=');
        if (equals_offset != -1) {
            /* Has value part */
            par_name_end_offset = equals_offset;
            /* Extract the parameter name */
            param_name = tvb_get_string_enc(wmem_packet_scope(), tvb, current_offset, par_name_end_offset - current_offset, ENC_UTF_8 | ENC_NA);
            /* charge-params */
            if ((param_name != NULL) && (g_ascii_strcasecmp(param_name, "icid-gen-addr") == 0)) {
                proto_tree_add_item(tree, hf_sip_icid_gen_addr, tvb,
                    equals_offset + 1, semi_colon_offset - equals_offset - 1, ENC_UTF_8 | ENC_NA);
            }
            else {
                proto_tree_add_format_text(tree, tvb, current_offset, length);
            }
        }
        else {
            proto_tree_add_format_text(tree, tvb, current_offset, length);
        }

        current_offset = semi_colon_offset + 1;
    }


}

/*
https://tools.ietf.org/html/rfc6809
The ABNF for the Feature-Caps header fields is:

Feature-Caps = "Feature-Caps" HCOLON fc-value
*(COMMA fc-value)
fc-value     = "*" *(SEMI feature-cap)

The ABNF for the feature-capability indicator is:

feature-cap       =  "+" fcap-name [EQUAL LDQUOT (fcap-value-list
/ fcap-string-value ) RDQUOT]
fcap-name         =  ftag-name
fcap-value-list   =  tag-value-list
fcap-string-value =  string-value
;; ftag-name, tag-value-list, string-value defined in RFC 3840

NOTE: In comparison with media feature tags, the "+" sign in front of
the feature-capability indicator name is mandatory.

*/
static void
dissect_sip_p_feature_caps(tvbuff_t *tvb, proto_tree *tree, gint start_offset, gint line_end_offset)
{
    gint current_offset, next_offset, length;
    guint16 semi_plus = 0x3b2b;

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

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

    while (next_offset < line_end_offset) {
        /* Find the end of feature cap or start of feature cap parameter, ";+" should indicate the start of a new feature-cap */
        current_offset = next_offset;
        next_offset = tvb_find_guint16(tvb, current_offset, line_end_offset - current_offset, semi_plus);
        if (next_offset == -1) {
            length = line_end_offset - current_offset;
            next_offset = line_end_offset;
        }
        else {
            length = next_offset - current_offset;
            next_offset += 2;
        }
        proto_tree_add_item(tree, hf_sip_feature_cap, tvb, current_offset, length, ENC_UTF_8 | ENC_NA);
    }
}

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

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

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

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

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

    remaining_length = tvb_reported_length(tvb);
    /* Check if we have enough data or if we need another segment, as a safty measure set a length limit*/
    if (remaining_length < 1500){
        linelen = tvb_find_line_end(tvb, offset, remaining_length, NULL, TRUE);
        if (linelen == -1){
            pinfo->desegment_offset = offset;
            pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
            return -1;
        }
    }
    len = dissect_sip_common(tvb, offset, remaining_length, pinfo, tree, TRUE, TRUE);
    if (len <= 0)
        return len;
    offset += len;
    remaining_length = remaining_length - len;

    /*
     * This is a bit of a cludge as the TCP dissector does not call the dissectors again if not all
     * the data in the segment was dissected and we do not know if we need another segment or not.
     * so DESEGMENT_ONE_MORE_SEGMENT can't be used in all cases.
     *
     */
    while (remaining_length > 0) {
        /* Check if we have enough data or if we need another segment, as a safty measure set a length limit*/
        if (remaining_length < 1500){
            linelen = tvb_find_line_end(tvb, offset, remaining_length, NULL, TRUE);
            if (linelen == -1){
                pinfo->desegment_offset = offset;
                pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
                return -1;
            }
        }
        len = dissect_sip_common(tvb, offset, remaining_length, pinfo, tree, TRUE, TRUE);
        if (len <= 0)
            return len;
        offset += len;
        remaining_length = remaining_length - len;
    }
    return offset;
}

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

    remaining_length = tvb_captured_length(tvb);
    while (remaining_length > 0) {
        len = dissect_sip_common(tvb, offset, remaining_length, 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;
        remaining_length = remaining_length - len;
        first = FALSE;
    }
    return TRUE;
}

static gboolean
dissect_sip_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    int remaining_length = tvb_captured_length(tvb);

    return dissect_sip_common(tvb, 0, remaining_length, pinfo, tree, FALSE, FALSE) > 0;
}

static int
dissect_sip_common(tvbuff_t *tvb, int offset, int remaining_length, 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;
    int found_match = 0;
    const char *descr;
    guint token_1_len = 0;
    guint current_method_idx = SIP_METHOD_INVALID;
    proto_item *ts, *ti_a = NULL, *th = NULL;
    proto_tree *sip_tree, *reqresp_tree      = NULL, *hdr_tree  = NULL,
        *message_body_tree = NULL, *cseq_tree = NULL,
        *via_tree         = NULL, *reason_tree       = NULL, *rack_tree = NULL,
        *route_tree       = NULL, *security_client_tree = NULL, *session_id_tree = NULL,
        *p_access_net_info_tree = NULL;
    guchar contacts = 0, contact_is_star = 0, expires_is_0 = 0, contacts_expires_0 = 0, contacts_expires_unknown = 0;
    guint32 cseq_number = 0;
    guchar  cseq_number_set = 0;
    char    cseq_method[MAX_CSEQ_METHOD_SIZE] = "";
    char    call_id[MAX_CALL_ID_SIZE] = "";
    gchar  *media_type_str_lower_case = NULL;
    http_message_info_t message_info = { HTTP_OTHERS, NULL };
    char   *content_encoding_parameter_str = NULL;
    guint   resend_for_packet = 0;
    guint   request_for_response = 0;
    guint32 response_time = 0;
    int     strlen_to_copy;
    heur_dtbl_entry_t *hdtbl_entry;

    /*
     * If this should be a request of response, do this quick check to see if
     * it begins with a string...
     * Otherwise, SIP heuristics are expensive...
     *
     */

    if (!dissect_other_as_continuation &&
        ((remaining_length < 1) || !g_ascii_isprint(tvb_get_guint8(tvb, offset))))
    {
        return -2;
    }

    /*
     * 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, remaining_length, &next_offset, FALSE);
    if(linelen==0){
        return -2;
    }

    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)&&( pinfo->ptype == PT_TCP)) {
        /*
         * 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
     * Note: this isn't _only_ for taps - internal code here uses it too
     * also store stat info in proto_data for subdissectors
     */
    stat_info = wmem_new0(pinfo->pool, sip_info_value_t);
    p_add_proto_data(pinfo->pool, pinfo, proto_sip, pinfo->curr_layer_num, stat_info);

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

    if (!pinfo->flags.in_error_pkt && have_tap_listener(exported_pdu_tap)) {
        wmem_list_frame_t *cur;
        guint proto_id;
        const gchar *proto_name;
        void *tmp;

        /* For SIP messages with other sip messages embeded in the body, dont export those individually.
         * E.g. if we are called from the mime_multipart dissector don't export the message.
         */
        cur = wmem_list_frame_prev(wmem_list_tail(pinfo->layers));
        tmp = wmem_list_frame_data(cur);
        proto_id = GPOINTER_TO_UINT(tmp);
        proto_name = proto_get_protocol_filter_name(proto_id);
        if (strcmp(proto_name, "mime_multipart") != 0) {
            export_sip_pdu(pinfo, tvb);
        }
    }

    DPRINT2(("------------------------------ dissect_sip_common ------------------------------"));

    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";
        col_add_lstr(pinfo->cinfo, COL_INFO,
                     descr, ": ",
                     tvb_format_text(tvb, offset, linelen - SIP2_HDR_LEN - 1),
                     COL_ADD_LSTR_TERMINATOR);
        DPRINT(("got %s: %s", descr,
                tvb_format_text(tvb, offset, linelen - SIP2_HDR_LEN - 1)));
        break;

    case STATUS_LINE:
        descr = "Status";
        col_add_lstr(pinfo->cinfo, COL_INFO,
                     "Status: ",
                     tvb_format_text(tvb, offset + SIP2_HDR_LEN + 1, linelen - SIP2_HDR_LEN - 1),
                     COL_ADD_LSTR_TERMINATOR);
        stat_info->reason_phrase = tvb_get_string_enc(wmem_packet_scope(), tvb, offset + SIP2_HDR_LEN + 5,
                                                      linelen - (SIP2_HDR_LEN + 5),ENC_UTF_8|ENC_NA);
        DPRINT(("got Response: %s",
                tvb_format_text(tvb, offset + SIP2_HDR_LEN + 1, linelen - SIP2_HDR_LEN - 1)));
        break;

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

    ts = proto_tree_add_item(tree, proto_sip, tvb, offset, -1, ENC_NA);
    sip_tree = proto_item_add_subtree(ts, ett_sip);

    switch (line_type) {

    case REQUEST_LINE:
        if (sip_tree) {
            ti_a = proto_tree_add_item(sip_tree, hf_Request_Line, tvb,
                        offset, linelen, ENC_UTF_8|ENC_NA);

            reqresp_tree = proto_item_add_subtree(ti_a, ett_sip_reqresp);
        }
        dfilter_sip_request_line(tvb, reqresp_tree, pinfo, offset, token_1_len, linelen);
        break;

    case STATUS_LINE:
        if (sip_tree) {
            ti_a = proto_tree_add_item(sip_tree, hf_sip_Status_Line, tvb,
                        offset, linelen, ENC_UTF_8|ENC_NA);
            reqresp_tree = proto_item_add_subtree(ti_a, ett_sip_reqresp);
        }
        dfilter_sip_status_line(tvb, reqresp_tree, pinfo, linelen, offset);
        break;

    case OTHER_LINE:
        if (sip_tree) {
            reqresp_tree = proto_tree_add_subtree_format(sip_tree, tvb, offset, next_offset,
                                     ett_sip_reqresp, NULL, "%s line: %s", descr,
                                     tvb_format_text(tvb, offset, linelen));
            /* XXX: Is adding to 'reqresp_tree as intended ? Changed from original 'sip_tree' */
            proto_tree_add_item(reqresp_tree, hf_sip_continuation, tvb, offset, -1, ENC_NA);
        }
        return remaining_length;
    }

    remaining_length = remaining_length - (next_offset - offset);
    offset = next_offset;

    th = proto_tree_add_item(sip_tree, hf_sip_msg_hdr, tvb, offset,
                                 remaining_length, ENC_UTF_8|ENC_NA);
    proto_item_set_text(th, "Message Header");
    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 (remaining_length > 0) {
        gint line_end_offset;
        gint colon_offset;
        gint semi_colon_offset;
        gint parameter_offset;
        gint parameter_end_offset;
        gint parameter_len;
        gint content_type_len, content_type_parameter_str_len;
        gint header_len;
        gchar *header_name;
        dissector_handle_t ext_hdr_handle;
        gint hf_index;
        gint value_offset;
        gint sub_value_offset;
        gint comma_offset;
        guchar c;
        gint value_len;
        gboolean is_no_header_termination = FALSE;
        proto_tree *tc_uri_item_tree = NULL;
        uri_offset_info uri_offsets;

        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;
        if(tvb_reported_length_remaining(tvb, next_offset) <= 0){
            is_no_header_termination = TRUE;
        }else{
            while (tvb_offset_exists(tvb, next_offset) && ((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.
             */
            expert_add_info(pinfo, th, &ei_sip_header_no_colon);
        } else {
            header_len = colon_offset - offset;
            header_name = (gchar*)tvb_get_string_enc(wmem_packet_scope(), tvb, offset, header_len, ENC_UTF_8|ENC_NA);
            ascii_strdown_inplace(header_name);
            hf_index = sip_is_known_sip_header(header_name, header_len);

            /*
             * Skip whitespace after the colon.
             */
            value_offset = tvb_skip_wsp(tvb, colon_offset + 1, line_end_offset - (colon_offset + 1));

            value_len = (gint) (line_end_offset - value_offset);

            if (hf_index == -1) {
                gint *hf_ptr = (gint*)wmem_map_lookup(sip_custom_header_fields_hash, header_name);
                if (hf_ptr) {
                    sip_proto_tree_add_string(hdr_tree, *hf_ptr, tvb, offset,
                                              next_offset - offset, value_offset, value_len);
                } else {
                    proto_item *ti_c;
                    proto_tree *ti_tree = proto_tree_add_subtree(hdr_tree, tvb,
                                                         offset, next_offset - offset, ett_sip_ext_hdr, &ti_c,
                                                         tvb_format_text(tvb, offset, linelen));

                    ext_hdr_handle = dissector_get_string_handle(ext_hdr_subdissector_table, header_name);
                    if (ext_hdr_handle != NULL) {
                        tvbuff_t *next_tvb2;
                        next_tvb2 = tvb_new_subset_length(tvb, value_offset, value_len);
                        dissector_try_string(ext_hdr_subdissector_table, header_name, next_tvb2, pinfo, ti_tree, NULL);
                    } else {
                        expert_add_info_format(pinfo, ti_c, &ei_sip_unrecognized_header,
                                               "Unrecognised SIP header (%s)",
                                               header_name);
                    }
                }
            } else {
                proto_item *sip_element_item;
                proto_tree *sip_element_tree;

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

                    case POS_TO :

                        /*if(hdr_tree)*/ {
                            proto_item *item;

                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            sip_element_tree = proto_item_add_subtree( sip_element_item,
                                               ett_sip_element);
                            /* To        =  ( "To" / "t" ) HCOLON ( name-addr
                             *               / addr-spec ) *( SEMI to-param )
                             */
                            sip_uri_offset_init(&uri_offsets);
                            if((dissect_sip_name_addr_or_addr_spec(tvb, pinfo, value_offset, line_end_offset+2, &uri_offsets)) != -1){
                                display_sip_uri(tvb, sip_element_tree, pinfo, &uri_offsets, &sip_to_uri);
                                if((uri_offsets.name_addr_start != -1) && (uri_offsets.name_addr_end != -1)){
                                    stat_info->tap_to_addr=tvb_get_string_enc(wmem_packet_scope(), tvb, uri_offsets.name_addr_start,
                                        uri_offsets.name_addr_end - uri_offsets.name_addr_start + 1, ENC_UTF_8|ENC_NA);
                                }
                                offset = uri_offsets.name_addr_end +1;
                            }

                            /* 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_to_tag, tvb, parameter_offset,
                                                    parameter_len, ENC_UTF_8|ENC_NA);
                                item = proto_tree_add_item(sip_element_tree, hf_sip_tag, tvb, parameter_offset,
                                                           parameter_len, ENC_UTF_8|ENC_NA);
                                PROTO_ITEM_SET_HIDDEN(item);

                                /* Tag indicates in-dialog messages, in case we have a INVITE, SUBSCRIBE or REFER, mark it */
                                switch (current_method_idx) {

                                case SIP_METHOD_INVITE:
                                case SIP_METHOD_SUBSCRIBE:
                                case SIP_METHOD_REFER:
                                    col_append_str(pinfo->cinfo, COL_INFO, ", in-dialog");
                                    break;
                                }
                            }
                        } /* if hdr_tree */
                    break;

                    case POS_FROM :
                        /*if(hdr_tree)*/ {
                            proto_item *item;

                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            sip_element_tree = proto_item_add_subtree( sip_element_item, ett_sip_element);
                            /*
                             * From        =  ( "From" / "f" ) HCOLON from-spec
                             * from-spec   =  ( name-addr / addr-spec )
                             *                *( SEMI from-param )
                             */

                            sip_uri_offset_init(&uri_offsets);
                            if((dissect_sip_name_addr_or_addr_spec(tvb, pinfo, value_offset, line_end_offset+2, &uri_offsets)) != -1){
                                display_sip_uri(tvb, sip_element_tree, pinfo, &uri_offsets, &sip_from_uri);
                                if((uri_offsets.name_addr_start != -1) && (uri_offsets.name_addr_end != -1)){
                                    stat_info->tap_from_addr=tvb_get_string_enc(wmem_packet_scope(), tvb, uri_offsets.name_addr_start,
                                        uri_offsets.name_addr_end - uri_offsets.name_addr_start + 1, ENC_UTF_8|ENC_NA);
                                }
                                offset = uri_offsets.name_addr_end +1;
                            }

                            /* 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_from_tag, tvb, parameter_offset,
                                                    parameter_len, ENC_UTF_8|ENC_NA);
                                item = proto_tree_add_item(sip_element_tree, hf_sip_tag, tvb, parameter_offset,
                                                           parameter_len, ENC_UTF_8|ENC_NA);
                                PROTO_ITEM_SET_HIDDEN(item);
                            }
                        }/* hdr_tree */
                    break;

                    case POS_P_ASSERTED_IDENTITY :
                        if(hdr_tree)
                        {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            sip_element_tree = proto_item_add_subtree( sip_element_item,
                                               ett_sip_element);

                            /*
                             * PAssertedID = "P-Asserted-Identity" HCOLON PAssertedID-value
                             *                *(COMMA PAssertedID-value)
                             * PAssertedID-value = name-addr / addr-spec
                             *
                             * Initialize the uri_offsets
                             */
                            sip_uri_offset_init(&uri_offsets);
                            if((dissect_sip_name_addr_or_addr_spec(tvb, pinfo, value_offset, line_end_offset+2, &uri_offsets)) != -1)
                                 display_sip_uri(tvb, sip_element_tree, pinfo, &uri_offsets, &sip_pai_uri);
                        }
                        break;
                    case POS_P_ASSOCIATED_URI:
                        if (hdr_tree)
                        {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                hf_header_array[hf_index], tvb,
                                offset, next_offset - offset,
                                value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);
                            /*
                             * P-Associated-URI       = "P-Associated-URI" HCOLON
                             *                          [p-aso-uri-spec]
                             *                          *(COMMA p-aso-uri-spec)
                             * p-aso-uri-spec         = name-addr *(SEMI ai-param)
                             * ai-param               = generic-param
                             */
                            /* Skip to the end of the URI directly */
                            semi_colon_offset = tvb_find_guint8(tvb, value_offset, line_end_offset - value_offset, '>');
                            if (semi_colon_offset != -1) {
                                semi_colon_offset = tvb_find_guint8(tvb, semi_colon_offset, line_end_offset - semi_colon_offset, ';');
                                if (semi_colon_offset != -1) {
                                    sip_element_tree = proto_item_add_subtree(sip_element_item,
                                        ett_sip_element);
                                    /* We have generic parameters */
                                    dissect_sip_generic_parameters(tvb, sip_element_tree, pinfo, semi_colon_offset + 1, line_end_offset);
                                }
                            }

                        }
                        break;
                    case POS_HISTORY_INFO:
                        if(hdr_tree)
                        {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            sip_element_tree = proto_item_add_subtree( sip_element_item,
                                               ett_sip_hist);
                            dissect_sip_history_info(tvb, sip_element_tree, pinfo, value_offset, line_end_offset);
                        }
                        break;

                    case POS_P_CHARGING_FUNC_ADDRESSES:
                        if(hdr_tree)
                        {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            sip_element_tree = proto_item_add_subtree( sip_element_item,
                                               ett_sip_element);
                            dissect_sip_p_charging_func_addresses(tvb, sip_element_tree, pinfo, value_offset, line_end_offset);
                        }
                        break;

                    case POS_P_PREFERRED_IDENTITY :
                        if(hdr_tree)
                        {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            sip_element_tree = proto_item_add_subtree( sip_element_item,
                                               ett_sip_element);
                            /*
                             * PPreferredID = "P-Preferred-Identity" HCOLON PPreferredID-value
                             *                   *(COMMA PPreferredID-value)
                             * PPreferredID-value = name-addr / addr-spec
                             *
                             * Initialize the uri_offsets
                             */
                            sip_uri_offset_init(&uri_offsets);
                            if((dissect_sip_name_addr_or_addr_spec(tvb, pinfo, value_offset, line_end_offset+2, &uri_offsets)) != -1)
                                 display_sip_uri(tvb, sip_element_tree, pinfo, &uri_offsets, &sip_ppi_uri);
                        }
                        break;

                    case POS_PERMISSION_MISSING :
                        if(hdr_tree)
                        {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            sip_element_tree = proto_item_add_subtree( sip_element_item,
                                                                   ett_sip_element);
                            /*
                             * Permission-Missing  =  "Permission-Missing" HCOLON per-miss-spec
                             *                        *( COMMA per-miss-spec )
                             * per-miss-spec       =  ( name-addr / addr-spec )
                             *                       *( SEMI generic-param )
                             * Initialize the uri_offsets
                             */
                            sip_uri_offset_init(&uri_offsets);
                            if((dissect_sip_name_addr_or_addr_spec(tvb, pinfo, value_offset, line_end_offset+2, &uri_offsets)) != -1)
                                 display_sip_uri(tvb, sip_element_tree, pinfo, &uri_offsets, &sip_pmiss_uri);
                        }
                        break;


                    case POS_TRIGGER_CONSENT :
                        if(hdr_tree)
                        {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            sip_element_tree = proto_item_add_subtree( sip_element_item,
                                                                        ett_sip_element);
                            /*
                             * Trigger-Consent     =  "Trigger-Consent" HCOLON trigger-cons-spec
                             *                        *( COMMA trigger-cons-spec )
                             * trigger-cons-spec   =  ( SIP-URI / SIPS-URI )
                             *                        *( SEMI trigger-param )
                             * trigger-param       =  target-uri / generic-param
                             * target-uri          =  "target-uri" EQUAL
                             *                            LDQUOT *( qdtext / quoted-pair ) RDQUOT
                             * Initialize the uri_offsets
                             */
                            sip_uri_offset_init(&uri_offsets);
                            if((dissect_sip_uri(tvb, pinfo, value_offset, line_end_offset+2, &uri_offsets)) != -1) {

                                tc_uri_item_tree = display_sip_uri(tvb, sip_element_tree, pinfo, &uri_offsets, &sip_tc_uri);
                                if (line_end_offset > uri_offsets.uri_end) {
                                    gint hparam_offset = uri_offsets.uri_end + 1;
                                    /* Is there a header parameter */
                                    if (tvb_find_guint8(tvb, hparam_offset, 1,';')) {
                                        while ((hparam_offset != -1 && hparam_offset < line_end_offset) )  {
                                            /* Is this a target-uri ? */
                                            hparam_offset = hparam_offset + 1;
                                            if (tvb_strncaseeql(tvb, hparam_offset, "target-uri=\"", 12) == 0) {
                                                gint turi_start_offset = hparam_offset + 12;
                                                gint turi_end_offset   = tvb_find_guint8(tvb, turi_start_offset, -1,'\"');
                                                if (turi_end_offset != -1)
                                                    proto_tree_add_item(tc_uri_item_tree, hf_sip_tc_turi, tvb, turi_start_offset,(turi_end_offset - turi_start_offset),ENC_UTF_8|ENC_NA);
                                                else
                                                    break; /* malformed */
                                            }
                                            hparam_offset = tvb_find_guint8(tvb, hparam_offset, -1,';');
                                        }
                                    }
                                }
                            }
                        }/* hdr_tree */
                        break;
                    case POS_RETRY_AFTER:
                    {
                        /* Store the retry number */
                        char *value = tvb_get_string_enc(wmem_packet_scope(), tvb, value_offset, value_len, ENC_UTF_8 | ENC_NA);
                        guint32 retry;
                        gboolean retry_valid = ws_strtou32(value, NULL, &retry);


                        sip_element_item = proto_tree_add_uint(hdr_tree, hf_header_array[hf_index],
                            tvb, offset, next_offset - offset,
                            retry);

                        if (!retry_valid) {
                            expert_add_info(pinfo, sip_element_item, &ei_sip_retry_after_invalid);
                        }
                    }
                    break;
                    case POS_CSEQ :
                    {
                        /* Store the sequence number */
                        char *value = tvb_get_string_enc(wmem_packet_scope(), tvb, value_offset, value_len, ENC_UTF_8|ENC_NA);

                        cseq_number = (guint32)strtoul(value, NULL, 10);
                        cseq_number_set = 1;
                        stat_info->tap_cseq_number=cseq_number;

                        /* Add CSeq  tree */
                        if (hdr_tree) {
                            sip_element_item = proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            cseq_tree = proto_item_add_subtree(sip_element_item, ett_sip_cseq);
                        }

                        /* Walk past number and spaces characters to get to start
                           of method name */
                        for (sub_value_offset=0; sub_value_offset < value_len; sub_value_offset++)
                        {
                            if (!g_ascii_isdigit(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 < value_len; sub_value_offset++)
                        {
                            if (g_ascii_isalpha(value[sub_value_offset]))
                            {
                                /* Have reached start of method name */
                                break;
                            }
                        }

                        if (sub_value_offset == value_len)
                        {
                            /* Didn't find method name */
                            return offset - orig_offset;
                        }

                        /* Extract method name from value */
                        strlen_to_copy = (int)value_len-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);
                            }
                            return offset - orig_offset;
                        }
                        else {
                            g_strlcpy(cseq_method, value+sub_value_offset, 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, ENC_UTF_8|ENC_NA);
                            }
                        }
                    }
                    break;

                    case POS_RACK :
                    {
                        char *value = tvb_get_string_enc(wmem_packet_scope(), tvb, value_offset, value_len, ENC_UTF_8|ENC_NA);
                        int cseq_no_offset;
                        /*int cseq_method_offset;*/

                        /* Add RAck  tree */
                        if (hdr_tree) {
                            sip_element_item = proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            rack_tree = proto_item_add_subtree(sip_element_item, ett_sip_rack);
                        }

                        /* RSeq number */
                        for (sub_value_offset=0; sub_value_offset < value_len; sub_value_offset++)
                        {
                            if (!g_ascii_isdigit(value[sub_value_offset]))
                            {
                                proto_tree_add_uint(rack_tree, hf_sip_rack_rseq_no,
                                                    tvb, value_offset, sub_value_offset,
                                                    (guint32)strtoul(value, NULL, 10));
                                break;
                            }
                        }

                        /* Get to start of CSeq number */
                        for ( ; sub_value_offset < value_len; 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 < value_len; sub_value_offset++)
                        {
                            if (!g_ascii_isdigit(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,
                                                    (guint32)strtoul(value+cseq_no_offset, NULL, 10));
                                break;
                            }
                        }

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

                        if (sub_value_offset == linelen)
                        {
                            /* Didn't find method name */
                            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,
                                                (int)value_len-sub_value_offset, ENC_UTF_8|ENC_NA);
                        }

                        break;
                    }

                    case POS_CALL_ID :
                    {
                        char *value = tvb_get_string_enc(wmem_packet_scope(), tvb, value_offset, value_len, ENC_UTF_8|ENC_NA);

                        /* Store the Call-id */
                        g_strlcpy(call_id, value, MAX_CALL_ID_SIZE);
                        stat_info->tap_call_id = wmem_strdup(wmem_packet_scope(), call_id);

                        /* Add 'Call-id' string item to tree */
                        sip_element_item = proto_tree_add_string(hdr_tree,
                                                    hf_header_array[hf_index], tvb,
                                                    offset, next_offset - offset,
                                                    value);
                        sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);
                    }
                    break;

                    case POS_EXPIRES :
                        if (tvb_strneql(tvb, value_offset, "0", value_len) == 0)
                        {
                            expires_is_0 = 1;
                        }

                        /* Add 'Expires' string item to tree */
                        sip_proto_tree_add_uint(hdr_tree,
                                                hf_header_array[hf_index], tvb,
                                                offset, next_offset - offset,
                                                value_offset, value_len);
                    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 :
                        sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                        sip_proto_set_format_text(hdr_tree, sip_element_item, 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;
                            message_info.media_str = tvb_get_string_enc(wmem_packet_scope(), tvb, parameter_offset,
                                                         content_type_parameter_str_len, ENC_UTF_8|ENC_NA);
                        }
                        media_type_str_lower_case = ascii_strdown_inplace(
                            (gchar *)tvb_get_string_enc(wmem_packet_scope(), tvb, value_offset, content_type_len, ENC_UTF_8|ENC_NA));

                        /* Debug code
                        proto_tree_add_debug_text(hdr_tree, tvb, value_offset,content_type_len,
                                            "media_type_str(lower cased)=%s",media_type_str_lower_case);
                        */
                    break;

                    case POS_CONTENT_LENGTH :
                    {
                        char *value = tvb_get_string_enc(wmem_packet_scope(), tvb, value_offset, value_len, ENC_UTF_8|ENC_NA);
                        gboolean content_length_valid = ws_strtou32(value, NULL, &content_length);

                        sip_element_item = proto_tree_add_uint(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               content_length);
                        sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);
                        if (!content_length_valid)
                            expert_add_info(pinfo, sip_element_item, &ei_sip_content_length_invalid);

                        break;
                    }

                    case POS_MAX_BREADTH :
                    case POS_MAX_FORWARDS :
                    case POS_RSEQ :
                        sip_proto_tree_add_uint(hdr_tree,
                                                hf_header_array[hf_index], tvb,
                                                offset, next_offset - offset,
                                                value_offset, value_len);
                        break;

                    case POS_CONTACT :
                        /*
                         * Contact        =  ("Contact" / "m" ) HCOLON
                         *                   ( STAR / (contact-param *(COMMA contact-param)))
                         * contact-param  =  (name-addr / addr-spec) *(SEMI contact-params)
                         */
                        sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                        sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                        sip_element_tree = proto_item_add_subtree( sip_element_item,
                                               ett_sip_element);

                        /* value_offset points to the first non SWS character after ':' */
                        c = tvb_get_guint8(tvb, value_offset);
                        if (c =='*'){
                            contact_is_star = 1;
                            break;
                        }

                        /*if(hdr_tree)*/ {
                            comma_offset = value_offset;
                            while((comma_offset = dissect_sip_contact_item(tvb, pinfo, sip_element_tree, comma_offset,
                                    next_offset, &contacts_expires_0, &contacts_expires_unknown)) != -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:
                        /* Authorization     =  "Authorization" HCOLON credentials
                         * credentials       =  ("Digest" LWS digest-response)
                         *                      / other-response
                         * digest-response   =  dig-resp *(COMMA dig-resp)
                         * other-response    =  auth-scheme LWS auth-param
                         *                      *(COMMA auth-param)
                         */
                    case POS_WWW_AUTHENTICATE:
                        /* Proxy-Authenticate  =  "Proxy-Authenticate" HCOLON challenge
                         * challenge           =  ("Digest" LWS digest-cln *(COMMA digest-cln))
                         *                        / other-challenge
                         * other-challenge     =  auth-scheme LWS auth-param
                         *                        *(COMMA auth-param)
                         * auth-scheme         =  token
                         */
                    case POS_PROXY_AUTHENTICATE:
                        /* Proxy-Authenticate  =  "Proxy-Authenticate" HCOLON challenge
                         */
                    case POS_PROXY_AUTHORIZATION:
                        /* Proxy-Authorization  =  "Proxy-Authorization" HCOLON credentials
                         */
                    case POS_AUTHENTICATION_INFO:
                        /* Authentication-Info  =  "Authentication-Info" HCOLON ainfo
                         *                        *(COMMA ainfo)
                         * ainfo                =  nextnonce / message-qop
                         *                         / response-auth / cnonce
                         *                         / nonce-count
                         */
                        /* Add tree using whole text of line */
                        if (hdr_tree) {
                            proto_item *ti_c;
                            sip_authorization_t authorization_info = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                            authorization_user_t * authorization_user = NULL;
                            /* Add whole line as header tree */
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                               hf_header_array[hf_index], tvb,
                                               offset, next_offset - offset,
                                               value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, 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_c = proto_tree_add_item(hdr_tree, hf_sip_auth, tvb,
                                                     offset, next_offset-offset,
                                                     ENC_UTF_8|ENC_NA);
                            PROTO_ITEM_SET_HIDDEN(ti_c);

                            /* Authentication-Info does not begin with the scheme name */
                            if (hf_index != POS_AUTHENTICATION_INFO)
                            {
                                /* The first time comma_offset is "start of parameters" */
                                comma_offset = tvb_ws_mempbrk_pattern_guint8(tvb, value_offset, line_end_offset - value_offset, &pbrk_whitespace, NULL);
                                proto_tree_add_item(sip_element_tree, hf_sip_auth_scheme,
                                                    tvb, value_offset, comma_offset - value_offset,
                                                    ENC_UTF_8|ENC_NA);
                            }else{
                                /* The first time comma_offset is "start of parameters" */
                                comma_offset = value_offset;
                            }

                            /* Parse each individual parameter in the line */
                            while ((comma_offset = dissect_sip_authorization_item(tvb, sip_element_tree, comma_offset, line_end_offset, &authorization_info)) != -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 */
                            }
                            if ((authorization_info.response != NULL) && (global_sip_validate_authorization)) { /* If there is a response, check for valid credentials */
                                authorization_user = sip_get_authorization(&authorization_info);
                                if (authorization_user) {
                                    authorization_info.method = wmem_strdup(wmem_packet_scope(), stat_info->request_method);
                                    if (!sip_validate_authorization(&authorization_info, authorization_user->password)) {
                                        proto_tree_add_expert_format(tree, pinfo, &ei_sip_authorization_invalid, tvb, offset, line_end_offset - offset, "SIP digest does not match known password %s", authorization_user->password);
                                    }
                                }
                            }
                        }/*hdr_tree*/
                    break;

                    case POS_ROUTE:
                        /* Add Route subtree */
                        if (hdr_tree) {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            route_tree = proto_item_add_subtree(sip_element_item, ett_sip_route);
                            dissect_sip_route_header(tvb, route_tree, pinfo, &sip_route_uri, value_offset, line_end_offset);
                        }
                        break;
                    case POS_RECORD_ROUTE:
                        /* Add Record-Route subtree */
                        if (hdr_tree) {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            route_tree = proto_item_add_subtree(sip_element_item, ett_sip_route);
                            dissect_sip_route_header(tvb, route_tree, pinfo, &sip_record_route_uri, value_offset, line_end_offset);
                        }
                        break;
                    case POS_SERVICE_ROUTE:
                        /* Add Service-Route subtree */
                        if (hdr_tree) {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            route_tree = proto_item_add_subtree(sip_element_item, ett_sip_route);
                            dissect_sip_route_header(tvb, route_tree, pinfo, &sip_service_route_uri, value_offset, line_end_offset);
                        }
                        break;
                    case POS_PATH:
                        /* Add Path subtree */
                        if (hdr_tree) {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            route_tree = proto_item_add_subtree(sip_element_item, ett_sip_route);
                            dissect_sip_route_header(tvb, route_tree, pinfo, &sip_path_uri, value_offset, line_end_offset);
                        }
                        break;
                    case POS_VIA:
                        /* Add Via subtree */
                        if (hdr_tree) {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            via_tree = proto_item_add_subtree(sip_element_item, ett_sip_via);
                            dissect_sip_via_header(tvb, via_tree, value_offset, line_end_offset, pinfo);
                        }
                        break;
                    case POS_REASON:
                        if(hdr_tree) {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                            reason_tree = proto_item_add_subtree(sip_element_item, ett_sip_reason);
                            dissect_sip_reason_header(tvb, reason_tree, pinfo, value_offset, line_end_offset);
                        }
                        break;
                    case POS_CONTENT_ENCODING:
                        /* Content-Encoding  =  ( "Content-Encoding" / "e" ) HCOLON
                         * content-coding *(COMMA content-coding)
                         */
                        sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                        sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                        content_encoding_parameter_str = ascii_strdown_inplace(tvb_get_string_enc(wmem_packet_scope(), tvb, value_offset,
                                                         (line_end_offset-value_offset), ENC_UTF_8|ENC_NA));
                        break;
                    case POS_SECURITY_CLIENT:
                        /* security-client  = "Security-Client" HCOLON
                         *                     sec-mechanism *(COMMA sec-mechanism)
                         */
                        sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                        sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                        comma_offset = tvb_find_guint8(tvb, value_offset, line_end_offset - value_offset, ',');
                        while(comma_offset<line_end_offset){
                            comma_offset = tvb_find_guint8(tvb, value_offset, line_end_offset - value_offset, ',');
                            if(comma_offset == -1){
                                comma_offset = line_end_offset;
                            }
                            security_client_tree = proto_item_add_subtree(sip_element_item, ett_sip_security_client);
                            dissect_sip_sec_mechanism(tvb, pinfo, security_client_tree, value_offset, comma_offset);
                            comma_offset = value_offset = comma_offset+1;
                        }

                        break;
                    case POS_SECURITY_SERVER:
                        /* security-server  = "Security-Server" HCOLON
                         *                     sec-mechanism *(COMMA sec-mechanism)
                         */
                        sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                        sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                        comma_offset = tvb_find_guint8(tvb, value_offset, line_end_offset - value_offset, ',');
                        while(comma_offset<line_end_offset){
                            comma_offset = tvb_find_guint8(tvb, value_offset, line_end_offset - value_offset, ',');
                            if(comma_offset == -1){
                                comma_offset = line_end_offset;
                            }
                            security_client_tree = proto_item_add_subtree(sip_element_item, ett_sip_security_server);
                            dissect_sip_sec_mechanism(tvb, pinfo, security_client_tree, value_offset, comma_offset);
                            comma_offset = value_offset = comma_offset+1;
                        }

                        break;
                    case POS_SECURITY_VERIFY:
                        /* security-verify  = "Security-Verify" HCOLON
                         *                     sec-mechanism *(COMMA sec-mechanism)
                         */
                        sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                        sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);

                        comma_offset = tvb_find_guint8(tvb, value_offset, line_end_offset - value_offset, ',');
                        while(comma_offset<line_end_offset){
                            comma_offset = tvb_find_guint8(tvb, value_offset, line_end_offset - value_offset, ',');
                            if(comma_offset == -1){
                                comma_offset = line_end_offset;
                            }
                            security_client_tree = proto_item_add_subtree(sip_element_item, ett_sip_security_verify);
                            dissect_sip_sec_mechanism(tvb, pinfo, security_client_tree, value_offset, comma_offset);
                            comma_offset = value_offset = comma_offset+1;
                        }

                        break;
                    case POS_SESSION_ID:
                        if(hdr_tree) {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                            hf_header_array[hf_index], tvb,
                                                            offset, next_offset - offset,
                                                            value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);
                            session_id_tree = proto_item_add_subtree(sip_element_item, ett_sip_session_id);
                            dissect_sip_session_id_header(tvb, session_id_tree, value_offset, line_end_offset);
                        }
                        break;
                    case POS_P_ACCESS_NETWORK_INFO:
                        /* Add P-Access-Network-Info subtree */
                        if (hdr_tree) {
                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                hf_header_array[hf_index], tvb,
                                offset, next_offset - offset,
                                value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);
                            p_access_net_info_tree = proto_item_add_subtree(sip_element_item, ett_sip_p_access_net_info);
                            dissect_sip_p_access_network_info_header(tvb, p_access_net_info_tree, value_offset, line_end_offset);
                        }
                        break;
                    case POS_P_CHARGING_VECTOR:
                        if (hdr_tree) {
                            proto_tree *p_charging_vector_tree;

                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                hf_header_array[hf_index], tvb,
                                offset, next_offset - offset,
                                value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);
                            p_charging_vector_tree = proto_item_add_subtree(sip_element_item, ett_sip_p_charging_vector);
                            dissect_sip_p_charging_vector_header(tvb, p_charging_vector_tree, value_offset, line_end_offset);
                        }
                        break;
                    case POS_FEATURE_CAPS:
                        if (hdr_tree) {
                            proto_tree *feature_caps_tree;

                            sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                hf_header_array[hf_index], tvb,
                                offset, next_offset - offset,
                                value_offset, value_len);
                            sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);
                            feature_caps_tree = proto_item_add_subtree(sip_element_item, ett_sip_feature_caps);
                            dissect_sip_p_feature_caps(tvb, feature_caps_tree, value_offset, line_end_offset);
                        }
                        break;
                    default :
                        /* Default case is to assume it's an FT_STRING field */
                        sip_element_item = sip_proto_tree_add_string(hdr_tree,
                                                         hf_header_array[hf_index], tvb,
                                                         offset, next_offset - offset,
                                                         value_offset, value_len);
                        sip_proto_set_format_text(hdr_tree, sip_element_item, tvb, offset, linelen);
                        break;
                }/* end switch */
            }/*if HF_index */
        }/* if colon_offset */
        if (is_no_header_termination == TRUE){
            /* Header not terminated by empty line CRLF */
            proto_tree_add_expert(hdr_tree, pinfo, &ei_sip_header_not_terminated,
                                    tvb, line_end_offset, -1);
        }
        remaining_length = remaining_length - (next_offset - offset);
        offset = next_offset;
    }/* End while */

    datalen = tvb_captured_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;
    }

    /* Add to info column interesting things learned from header fields. */

    /* for either REGISTER requests or responses, any contacts without expires
     * parameter use the Expires header's value
     */
    if (expires_is_0) {
        /* this may add nothing, but that's ok */
        contacts_expires_0 += contacts_expires_unknown;
    }

    /* Registration requests */
    if (current_method_idx == SIP_METHOD_REGISTER)
    {
        /* TODO: what if there's a *-contact but also non-*-contacts?
         * we should create expert info for that someday I guess
         */
        if (contact_is_star && expires_is_0)
        {
            col_append_str(pinfo->cinfo, COL_INFO, "  (remove all bindings)");
        }
        else
        if (contacts_expires_0 > 0)
        {
            col_append_fstr(pinfo->cinfo, COL_INFO, "  (remove %d binding%s)",
                contacts_expires_0, contacts_expires_0 == 1 ? "":"s");
            if (contacts > contacts_expires_0) {
                col_append_fstr(pinfo->cinfo, COL_INFO, " (add %d binding%s)",
                    contacts - contacts_expires_0,
                    (contacts - contacts_expires_0 == 1) ? "":"s");
            }
        }
        else
        if (!contacts)
        {
            col_append_str(pinfo->cinfo, COL_INFO, "  (fetch bindings)");
        }
        else
        {
            col_append_fstr(pinfo->cinfo, COL_INFO, "  (%d binding%s)",
                contacts, contacts == 1 ? "":"s");
        }
    }

    /* Registration responses - this info only makes sense in 2xx responses */
    if (line_type == STATUS_LINE && (strcmp(cseq_method, "REGISTER") == 0) &&
        stat_info && stat_info->response_code > 199 && stat_info->response_code < 300)
    {
        if (contacts_expires_0 > 0) {
            col_append_fstr(pinfo->cinfo, COL_INFO, "  (removed %d binding%s)",
                contacts_expires_0, contacts_expires_0 == 1 ? "":"s");
            if (contacts > contacts_expires_0) {
                col_append_fstr(pinfo->cinfo, COL_INFO, " (%d binding%s kept)",
                    contacts - contacts_expires_0,
                    (contacts - contacts_expires_0 == 1) ? "":"s");
            }
        } else {
            col_append_fstr(pinfo->cinfo, COL_INFO, "  (%d binding%s)",
                contacts, contacts == 1 ? "":"s");
        }
    }

    /* We've finished writing to the info col for this SIP message
     * Set fence in case there is more than one (SIP)message in the frame
     */
    /* XXX: this produces ugly output, since usually there's only one SIP
     * message in a frame yet this '|' gets added at the end. Need a better
     * way to do this.
     */
    col_append_str(pinfo->cinfo, COL_INFO, " | ");
    col_set_fence(pinfo->cinfo, COL_INFO);

    /* Find the total setup time, Must be done before checking for resend
     * As that will overwrite the "Request packet no".
     */
    if ((line_type == REQUEST_LINE)&&(strcmp(cseq_method, "ACK") == 0))
    {
        request_for_response = sip_find_invite(pinfo, cseq_method, call_id,
                                                cseq_number_set, cseq_number,
                                                &response_time);
        stat_info->setup_time = response_time;
    }

    /* For responses, try to link back to request frame */
    if (line_type == STATUS_LINE)
    {
        request_for_response = sip_find_request(pinfo, cseq_method, call_id,
                                                cseq_number_set, cseq_number,
                                                &response_time);
    }

    /* 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);

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

    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);
        if(content_encoding_parameter_str != NULL &&
            (!strncmp(content_encoding_parameter_str, "gzip", 4) ||
             !strncmp(content_encoding_parameter_str,"deflate",7))){
            /* The body is gzip:ed */
            next_tvb = tvb_uncompress(tvb, offset,  datalen);
            if (next_tvb) {
                add_new_data_source(pinfo, next_tvb, "gunziped data");
                if(sip_tree) {
                    ti_a = proto_tree_add_item(sip_tree, hf_sip_msg_body, next_tvb, 0, -1,
                                         ENC_NA);
                    message_body_tree = proto_item_add_subtree(ti_a, ett_sip_message_body);
                }
            } else {
                next_tvb = tvb_new_subset_length_caplen(tvb, offset, datalen, reported_datalen);
                if(sip_tree) {
                    ti_a = proto_tree_add_item(sip_tree, hf_sip_msg_body, next_tvb, 0, -1,
                                         ENC_NA);
                    message_body_tree = proto_item_add_subtree(ti_a, ett_sip_message_body);
                }
            }
        }else{
            next_tvb = tvb_new_subset_length_caplen(tvb, offset, datalen, reported_datalen);
            if(sip_tree) {
                ti_a = proto_tree_add_item(sip_tree, hf_sip_msg_body, next_tvb, 0, -1,
                                     ENC_NA);
                message_body_tree = proto_item_add_subtree(ti_a, ett_sip_message_body);
            }
        }

        /* give the content type parameters to sub dissectors */
        if ( media_type_str_lower_case != NULL ) {
            /* SDP needs a transport layer to determine request/response */
            if (!strcmp(media_type_str_lower_case, "application/sdp")) {
                /* Resends don't count */
                if (resend_for_packet == 0) {
                    if (line_type == REQUEST_LINE) {
                        DPRINT(("calling setup_sdp_transport() SDP_EXCHANGE_OFFER frame=%d",
                                pinfo->num));
                        DINDENT();
                        setup_sdp_transport(next_tvb, pinfo, SDP_EXCHANGE_OFFER, pinfo->num, sip_delay_sdp_changes);
                        DENDENT();
                    } else if (line_type == STATUS_LINE) {
                        if (stat_info->response_code >= 400) {
                            DPRINT(("calling setup_sdp_transport() SDP_EXCHANGE_ANSWER_REJECT "
                                    "request_frame=%d, this=%d",
                                    request_for_response, pinfo->num));
                            DINDENT();
                            /* SIP client request failed, so SDP offer should fail */
                            setup_sdp_transport(next_tvb, pinfo, SDP_EXCHANGE_ANSWER_REJECT, request_for_response, sip_delay_sdp_changes);
                            DENDENT();
                        }
                        else if ((stat_info->response_code >= 200) && (stat_info->response_code <= 299)) {
                            DPRINT(("calling setup_sdp_transport() SDP_EXCHANGE_ANSWER_ACCEPT "
                                    "request_frame=%d, this=%d",
                                    request_for_response, pinfo->num));
                            DINDENT();
                            /* SIP success request, so SDP offer should be accepted */
                            setup_sdp_transport(next_tvb, pinfo, SDP_EXCHANGE_ANSWER_ACCEPT, request_for_response, sip_delay_sdp_changes);
                            DENDENT();
                        }
                    }
                } else {
                    DPRINT(("calling setup_sdp_transport() resend_for_packet "
                            "request_frame=%d, this=%d",
                            request_for_response, pinfo->num));
                    DINDENT();
                    setup_sdp_transport_resend(pinfo->num, resend_for_packet);
                    DENDENT();
                }
            }

            /* XXX: why is this called even if setup_sdp_transport() was called before? That will
                    parse the SDP a second time, for 'application/sdp' media MIME bodies */
            DPRINT(("calling dissector_try_string()"));
            DINDENT();
            found_match = dissector_try_string(media_type_dissector_table,
                                               media_type_str_lower_case,
                                               next_tvb, pinfo,
                                               message_body_tree, &message_info);
            DENDENT();
            DPRINT(("done calling dissector_try_string() with found_match=%u", found_match));

            if (!found_match &&
                !strncmp(media_type_str_lower_case, "multipart/", sizeof("multipart/")-1)) {
                DPRINT(("calling dissector_try_string() for multipart"));
                DINDENT();
                /* Try to decode the unknown multipart subtype anyway */
                found_match = dissector_try_string(media_type_dissector_table,
                                                   "multipart/",
                                                   next_tvb, pinfo,
                                                   message_body_tree, &message_info);
                DENDENT();
                DPRINT(("done calling dissector_try_string() with found_match=%u", found_match));
            }
            /* If no match dump as text */
        }
        if ( found_match == 0 )
        {
            DPRINT(("calling dissector_try_heuristic() with found_match=0"));
            DINDENT();
            if (!(dissector_try_heuristic(heur_subdissector_list,
                              next_tvb, pinfo, message_body_tree, &hdtbl_entry, NULL))) {
                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;
                    proto_tree_add_format_text(message_body_tree, next_tvb,
                                tmp_offset, linelen);
                    tmp_offset = next_offset;
                }/* end while */
            }
            DENDENT();
        }
        offset += datalen;
    }

    /* 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 (request_for_response > 0)
        {
            item = proto_tree_add_uint(reqresp_tree, hf_sip_matching_request_frame,
                                       tvb, orig_offset, 0, request_for_response);
            PROTO_ITEM_SET_GENERATED(item);
            item = proto_tree_add_uint(reqresp_tree, hf_sip_response_time,
                                       tvb, orig_offset, 0, response_time);
            PROTO_ITEM_SET_GENERATED(item);
            if ((line_type == STATUS_LINE)&&(strcmp(cseq_method, "BYE") == 0)){
                item = proto_tree_add_uint(reqresp_tree, hf_sip_release_time,
                                          tvb, orig_offset, 0, response_time);
                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);

    /* Append a brief summary to the SIP root item */
    if (stat_info->request_method) {
        proto_item_append_text(ts, " (%s)", stat_info->request_method);
    }
    else {
        proto_item_append_text(ts, " (%u)", stat_info->response_code);
    }
    return offset - orig_offset;
}

/* Display filter for SIP Request-Line */
static void
dfilter_sip_request_line(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gint offset, guint meth_len, gint linelen)
{
    const guint8    *value;
    guint   parameter_len = meth_len;
    uri_offset_info uri_offsets;

    /*
     * We know we have the entire method; otherwise, "sip_parse_line()"
     * would have returned OTHER_LINE.
     * Request-Line  =  Method SP Request-URI SP SIP-Version CRLF
     * SP = single space
     * Request-URI    =  SIP-URI / SIPS-URI / absoluteURI
     */

    /* get method string*/
    proto_tree_add_item_ret_string(tree, hf_sip_Method, tvb, offset, parameter_len, ENC_ASCII | ENC_NA,
        wmem_packet_scope(), &value);

    /* Copy request method for telling tap */
    stat_info->request_method = value;

    if (tree) {
        /* build Request-URI tree*/
        offset=offset + parameter_len+1;
        sip_uri_offset_init(&uri_offsets);
        /* calc R-URI len*/
        uri_offsets.uri_end = tvb_find_guint8(tvb, offset, linelen, ' ')-1;
        dissect_sip_uri(tvb, pinfo, offset, offset + linelen, &uri_offsets);
        display_sip_uri(tvb, tree, pinfo, &uri_offsets, &sip_req_uri);
    }
}

/* Display filter for SIP Status-Line */
static void
dfilter_sip_status_line(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gint line_end, gint offset)
{
    gint response_code = 0;
    gboolean response_code_valid;
    proto_item* pi;
    int diag_len;
    tvbuff_t *next_tvb;

    /*
     * 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.
     */
    offset = offset + SIP2_HDR_LEN + 1;
    response_code_valid = ws_strtoi32(tvb_get_string_enc(wmem_packet_scope(), tvb, offset, 3,
        ENC_UTF_8|ENC_NA), NULL, &response_code);

    /* Add numerical response code to tree */
    pi = proto_tree_add_uint(tree, hf_sip_Status_Code, tvb, offset, 3, response_code);
    if (!response_code_valid)
        expert_add_info(pinfo, pi, &ei_sip_Status_Code_invalid);

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

    /* Skip past the response code and possible trailing space */
    offset = offset + 3 + 1;

    /* Check for diagnostics */
    diag_len = line_end - (SIP2_HDR_LEN + 1 + 3 + 1);
    if((diag_len) <= 0)
        return;

    /* If we have a SIP diagnostics sub dissector call it */
    if(sip_diag_handle){
        next_tvb = tvb_new_subset_length(tvb, offset, diag_len);
        call_dissector_only(sip_diag_handle, next_tvb, pinfo, tree, NULL);
    }
}

/* 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 (!g_ascii_isdigit(tvb_get_guint8(tvb, token_2_start)) ||
            !g_ascii_isdigit(tvb_get_guint8(tvb, token_2_start + 1)) ||
            !g_ascii_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;
    gchar *meth_name;

    meth_name = tvb_get_string_enc(wmem_packet_scope(), tvb, meth_offset, meth_len, ENC_UTF_8|ENC_NA);

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

    return FALSE;
}

/*
 * Returns index of method in sip_headers
 * Header namne should be in lower case
 */
static gint sip_is_known_sip_header(gchar *header_name, guint header_len)
{
    guint pos;

    /* Compact name is one character long */
    if(header_len>1){
        pos = GPOINTER_TO_UINT(g_hash_table_lookup(sip_headers_hash, header_name));
        if (pos!=0)
            return pos;
    }

    /* Look for compact name match */
    for (pos = 1; pos < array_length(sip_headers); pos++) {
        if (sip_headers[pos].compact_name != NULL &&
                header_len == strlen(sip_headers[pos].compact_name) &&
                g_ascii_strncasecmp(header_name, sip_headers[pos].compact_name, header_len) == 0)
            return pos;
    }

    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;
    proto_item *ti;
    int next_offset, linelen, end_offset;
    char *str;

    ti = proto_tree_add_item(tree, proto_raw_sip, tvb, offset, length, ENC_NA);
    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) {
            if (global_sip_raw_text_without_crlf)
                str = tvb_format_text_wsp(wmem_packet_scope(), tvb, offset, linelen);
            else
                str = tvb_format_text(tvb, offset, linelen);
            proto_tree_add_string_format(raw_tree, hf_sip_raw_line, tvb, offset, linelen,
                             str,
                             "%s",
                             str);
        }
        offset = next_offset;
    }
}

/* Check to see if this packet is a resent request.  Return value is the frame 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;
    sip_frame_result_value *sip_frame_result = NULL;
    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->flags.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)
    {
        sip_frame_result = (sip_frame_result_value*)p_get_proto_data(wmem_file_scope(), pinfo, proto_sip, pinfo->curr_layer_num);
        if (sip_frame_result != NULL)
        {
            return sip_frame_result->original_frame_num;
        }
        else
        {
            return 0;
        }
    }

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

    /* Prepare the key */
    g_strlcpy(key.call_id, call_id, 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;
    if (sip_retrans_the_same_sport) {
        key.source_port = pinfo->srcport;
    } else {
        key.source_port = MAGIC_SOURCE_PORT;
    }

    /* 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;

        /* First time through, must update value with current details if
            cseq number has changed */
        if (cseq_number != p_val->cseq)
        {
            p_val->cseq = cseq_number;
            g_strlcpy(p_val->method, cseq_method, MAX_CSEQ_METHOD_SIZE);
            p_val->transaction_state = nothing_seen;
            p_val->frame_number = 0;
            if (line_type == REQUEST_LINE)
            {
                p_val->request_time = pinfo->abs_ts;
            }
        }
    }
    else
    {
        /* Need to create a new table entry */

        /* Allocate a new key and value */
        p_key = wmem_new(wmem_file_scope(), sip_hash_key);
        p_val = wmem_new0(wmem_file_scope(), sip_hash_value);

        /* Fill in key and value details */
        g_snprintf(p_key->call_id, MAX_CALL_ID_SIZE, "%s", call_id);
        copy_address_wmem(wmem_file_scope(), &(p_key->dest_address), &pinfo->net_dst);
        copy_address_wmem(wmem_file_scope(), &(p_key->source_address), &pinfo->net_src);
        p_key->dest_port = pinfo->destport;
        if (sip_retrans_the_same_sport) {
            p_key->source_port = pinfo->srcport;
        } else {
            p_key->source_port = MAGIC_SOURCE_PORT;
        }

        p_val->cseq = cseq_number;
        g_strlcpy(p_val->method, cseq_method, MAX_CSEQ_METHOD_SIZE);
        p_val->transaction_state = nothing_seen;
        if (line_type == REQUEST_LINE)
        {
            p_val->request_time = pinfo->abs_ts;
        }

        /* 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)
            {
                /* This frame is the original request */
                p_val->frame_number = pinfo->num;
                p_val->request_time = pinfo->abs_ts;
            }
            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)
                {
                    /* This frame is the original response */
                    p_val->frame_number = pinfo->num;
                }
            }
            else
            {
                p_val->transaction_state = provisional_response_seen;
            }
            break;
        default:
            break;
    }

    sip_frame_result = (sip_frame_result_value *)p_get_proto_data(wmem_file_scope(), pinfo, proto_sip, pinfo->curr_layer_num);
    if (sip_frame_result == NULL)
    {
        sip_frame_result = wmem_new0(wmem_file_scope(), sip_frame_result_value);
        p_add_proto_data(wmem_file_scope(), pinfo, proto_sip, pinfo->curr_layer_num, sip_frame_result);
    }

    /* Store return value with this packet */
    sip_frame_result->original_frame_num = result;

    return result;
}


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

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

    /* Ignore error (usually ICMP) frames */
    if (pinfo->flags.in_error_pkt)
    {
        return 0;
    }

    /* A broken packet may have no cseq number set. Ignore. */
    if (!cseq_number_set)
    {
        return 0;
    }

    /* Return any answer stored from previous dissection */
    if (pinfo->fd->flags.visited)
    {
        sip_frame_result = (sip_frame_result_value*)p_get_proto_data(wmem_file_scope(), pinfo, proto_sip, pinfo->curr_layer_num);
        if (sip_frame_result != NULL)
        {
            *response_time = sip_frame_result->response_time;
            return sip_frame_result->response_request_frame_num;
        }
        else
        {
            return 0;
        }
    }

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

    /* Prepare the key */
    g_strlcpy(key.call_id, call_id, MAX_CALL_ID_SIZE);

    /* Looking for matching request, so reverse addresses for this lookup */
    set_address(&key.dest_address, pinfo->net_src.type, pinfo->net_src.len,
            pinfo->net_src.data);
    set_address(&key.source_address, pinfo->net_dst.type, pinfo->net_dst.len,
            pinfo->net_dst.data);
    key.dest_port = pinfo->srcport;
    key.source_port = pinfo->destport;

    /* 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
    {
        /* We don't have the request */
        return 0;
    }


    /**************************************************/
    /* Is it a response to a request that we've seen? */
    if ((cseq_number == cseq_to_compare) &&
        (p_val->transaction_state == request_seen) &&
        (strcmp(cseq_method, p_val->method) == 0))
    {
        result = p_val->frame_number;
    }


    /* Store return value with this packet */
    sip_frame_result = (sip_frame_result_value *)p_get_proto_data(wmem_file_scope(), pinfo, proto_sip, pinfo->curr_layer_num);
    if (sip_frame_result == NULL)
    {
        /* Allocate and set all values to zero */
        sip_frame_result = wmem_new0(wmem_file_scope(), sip_frame_result_value);
        p_add_proto_data(wmem_file_scope(), pinfo, proto_sip, pinfo->curr_layer_num, sip_frame_result);
    }

    sip_frame_result->response_request_frame_num = result;

    /* Work out response time */
    seconds_between_packets = (gint)
        (pinfo->abs_ts.secs - p_val->request_time.secs);
    nseconds_between_packets =
         pinfo->abs_ts.nsecs - p_val->request_time.nsecs;
    sip_frame_result->response_time = (seconds_between_packets*1000) +
                                      (nseconds_between_packets / 1000000);
    *response_time = sip_frame_result->response_time;

    return result;
}

/*
 * Find the initial INVITE to calculate the total setup time
 */
guint sip_find_invite(packet_info *pinfo,
            gchar *cseq_method _U_,
            gchar *call_id,
            guchar cseq_number_set,
            guint32 cseq_number _U_,
            guint32 *response_time)
{
#if 0
    guint32 cseq_to_compare = 0;
#endif
    sip_hash_key   key;
    sip_hash_value *p_val = 0;
    sip_frame_result_value *sip_frame_result = NULL;
    guint result = 0;
    gint seconds_between_packets;
    gint nseconds_between_packets;

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

    /* Ignore error (usually ICMP) frames */
    if (pinfo->flags.in_error_pkt)
    {
        return 0;
    }

    /* A broken packet may have no cseq number set. Ignore. */
    if (!cseq_number_set)
    {
        return 0;
    }

    /* Return any answer stored from previous dissection */
    if (pinfo->fd->flags.visited)
    {
        sip_frame_result = (sip_frame_result_value*)p_get_proto_data(wmem_file_scope(), pinfo, proto_sip, pinfo->curr_layer_num);
        if (sip_frame_result != NULL)
        {
            *response_time = sip_frame_result->response_time;
            return sip_frame_result->response_request_frame_num;
        }
        else
        {
            return 0;
        }
    }

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

    /* Prepare the key */
    g_strlcpy(key.call_id, call_id, MAX_CALL_ID_SIZE);

    /* Looking for matching INVITE */
    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)
    {
#if 0
        /* Table entry found, we'll use its value for comparison */
        cseq_to_compare = p_val->cseq;
#endif
    }
    else
    {
        /* We don't have the request */
        return 0;
    }


    /**************************************************/
    /* Is it a response to a request that we've seen? */
#if 0
    if ((cseq_number == cseq_to_compare) &&
        (p_val->transaction_state == request_seen) &&
        (strcmp(cseq_method, p_val->method) == 0))
    {
        result = p_val->frame_number;
    }
#endif

    result = p_val->frame_number;

    /* Store return value with this packet */
    sip_frame_result = (sip_frame_result_value *)p_get_proto_data(wmem_file_scope(), pinfo, proto_sip, pinfo->curr_layer_num);
    if (