summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-rpc.c
blob: 2044a207a6eb7eab1365c09ed0dba36310d853e4 (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
/* packet-rpc.c
 * Routines for rpc dissection
 * Copyright 1999, Uwe Girlich <Uwe.Girlich@philosys.de>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-smb.c
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <stdio.h>	/* fprintf() */
#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/exceptions.h>
#include <wsutil/pint.h>
#include <wsutil/str_util.h>
#include <epan/prefs.h>
#include <epan/reassemble.h>
#include <epan/tap.h>
#include <epan/stat_tap_ui.h>
#include <epan/srt_table.h>
#include <epan/show_exception.h>
#include <wsutil/array.h>

#include "packet-rpc.h"
#include "packet-tcp.h"
#include "packet-tls.h"
#include "packet-nfs.h"
#include "packet-gssapi.h"

/*
 * See:
 *
 *	RFC 1831, "RPC: Remote Procedure Call Protocol Specification
 *	Version 2";
 *
 *	RFC 1832, "XDR: External Data Representation Standard";
 *
 *	RFC 2203, "RPCSEC_GSS Protocol Specification".
 *
 * See also
 *
 *	RFC 2695, "Authentication Mechanisms for ONC RPC"
 *
 *	although we don't currently dissect AUTH_DES or AUTH_KERB.
 *
 *	RFC 5531, "Appendix C: Current Number Assignments" defines AUTH_RSA.
 *	AUTH_RSA is not implemented for any known RPC-protocols. The Gluster
 *	protocols (ab)use AUTH_RSA for their own AUTH-flavor. AUTH_RSA is
 *	therefore dissected as the unofficial AUTH_GLUSTER.
 */
void proto_register_rpc(void);
void proto_reg_handoff_rpc(void);

#define RPC_TCP_PORT 111

#define RPC_UDP 0
#define RPC_TCP 1
#define RPC_TLS 2

/* desegmentation of RPC over TCP */
static bool rpc_desegment = true;

/* defragmentation of fragmented RPC over TCP records */
static bool rpc_defragment = true;

/* try to dissect RPC packets for programs that are not known
 * (proprietary ones) by wireshark.
 */
static bool rpc_dissect_unknown_programs;

/* try to find RPC fragment start if normal decode fails
 * (good when starting decode of mid-stream capture)
 */
static bool rpc_find_fragment_start;

static int rpc_tap;

static dissector_handle_t spnego_krb5_wrap_handle;

static const value_string rpc_msg_type[] = {
	{ RPC_CALL,		     "Call" },
	{ RPC_REPLY,		     "Reply" },
	{ 0, NULL }
};

static const value_string rpc_reply_state[] = {
	{ MSG_ACCEPTED,		     "accepted" },
	{ MSG_DENIED,		     "denied" },
	{ 0, NULL }
};

const value_string rpc_auth_flavor[] = {
	{ AUTH_NULL,		     "AUTH_NULL" },
	{ AUTH_UNIX,		     "AUTH_UNIX" },
	{ AUTH_SHORT,		     "AUTH_SHORT" },
	{ AUTH_DES,		     "AUTH_DES" },
	{ AUTH_RSA,		     "AUTH_RSA/Gluster" },
	{ RPCSEC_GSS,		     "RPCSEC_GSS" },
	{ AUTH_TLS,		     "AUTH_TLS" },
	{ AUTH_GSSAPI,		     "AUTH_GSSAPI" },
	{ RPCSEC_GSS_KRB5,	     "RPCSEC_GSS_KRB5" },
	{ RPCSEC_GSS_KRB5I,	     "RPCSEC_GSS_KRB5I" },
	{ RPCSEC_GSS_KRB5P,	     "RPCSEC_GSS_KRB5P" },
	{ RPCSEC_GSS_LIPKEY,	     "RPCSEC_GSS_LIPKEY" },
	{ RPCSEC_GSS_LIPKEY_I,	     "RPCSEC_GSS_LIPKEY_I" },
	{ RPCSEC_GSS_LIPKEY_P,	     "RPCSEC_GSS_LIPKEY_P" },
	{ RPCSEC_GSS_SPKM3,	     "RPCSEC_GSS_SPKM3" },
	{ RPCSEC_GSS_SPKM3I,	     "RPCSEC_GSS_SPKM3I" },
	{ RPCSEC_GSS_SPKM3P,	     "RPCSEC_GSS_SPKM3P" },
	{ AUTH_GLUSTERFS,	     "AUTH_GLUSTERFS" },
	{ AUTH_GLUSTERFS_V3,	     "AUTH_GLUSTERFS_V3" },
	{ 0, NULL }
};

static const value_string rpc_authgss_proc[] = {
	{ RPCSEC_GSS_DATA,	     "RPCSEC_GSS_DATA" },
	{ RPCSEC_GSS_INIT,	     "RPCSEC_GSS_INIT" },
	{ RPCSEC_GSS_CONTINUE_INIT,  "RPCSEC_GSS_CONTINUE_INIT" },
	{ RPCSEC_GSS_DESTROY,	     "RPCSEC_GSS_DESTROY" },
	{ 0, NULL }
};

static const value_string rpc_authgssapi_proc[] = {
	{ AUTH_GSSAPI_EXIT,	     "AUTH_GSSAPI_EXIT" },
	{ AUTH_GSSAPI_INIT,	     "AUTH_GSSAPI_INIT" },
	{ AUTH_GSSAPI_CONTINUE_INIT, "AUTH_GSSAPI_CONTINUE_INIT" },
	{ AUTH_GSSAPI_MSG,	     "AUTH_GSSAPI_MSG" },
	{ AUTH_GSSAPI_DESTROY,	     "AUTH_GSSAPI_DESTROY" },
	{ 0, NULL }
};

const value_string rpc_authgss_svc[] = {
	{ RPCSEC_GSS_SVC_NONE,	     "rpcsec_gss_svc_none" },
	{ RPCSEC_GSS_SVC_INTEGRITY,  "rpcsec_gss_svc_integrity" },
	{ RPCSEC_GSS_SVC_PRIVACY,    "rpcsec_gss_svc_privacy" },
	{ 0, NULL }
};

static const value_string rpc_accept_state[] = {
	{ SUCCESS,		     "RPC executed successfully" },
	{ PROG_UNAVAIL,		     "remote hasn't exported program" },
	{ PROG_MISMATCH,	     "remote can't support version #" },
	{ PROC_UNAVAIL,		     "program can't support procedure" },
	{ GARBAGE_ARGS,		     "procedure can't decode params" },
	{ SYSTEM_ERROR,		     "system errors like memory allocation failure" },
	{ 0, NULL }
};

static const value_string rpc_reject_state[] = {
	{ RPC_MISMATCH,		     "RPC_MISMATCH" },
	{ AUTH_ERROR,		     "AUTH_ERROR" },
	{ 0, NULL }
};

static const value_string rpc_auth_state[] = {
	{ AUTH_BADCRED,		     "bad credential (seal broken)" },
	{ AUTH_REJECTEDCRED,	     "client must begin new session" },
	{ AUTH_BADVERF,		     "bad verifier (seal broken)" },
	{ AUTH_REJECTEDVERF,	     "verifier expired or replayed" },
	{ AUTH_TOOWEAK,		     "rejected for security reasons" },
	{ RPCSEC_GSSCREDPROB,	     "GSS credential problem" },
	{ RPCSEC_GSSCTXPROB,	     "GSS context problem" },
	{ 0, NULL }
};

static const value_string rpc_authdes_namekind[] = {
	{ AUTHDES_NAMEKIND_FULLNAME, "ADN_FULLNAME" },
	{ AUTHDES_NAMEKIND_NICKNAME, "ADN_NICKNAME" },
	{ 0, NULL }
};

/* the protocol number */
static int proto_rpc;
static int proto_rpc_unknown;
static int hf_rpc_reqframe;
static int hf_rpc_repframe;
static int hf_rpc_lastfrag;
static int hf_rpc_fraglen;
static int hf_rpc_xid;
static int hf_rpc_msgtype;
static int hf_rpc_version;
static int hf_rpc_version_min;
static int hf_rpc_version_max;
static int hf_rpc_program;
static int hf_rpc_programversion;
static int hf_rpc_programversion_min;
static int hf_rpc_programversion_max;
static int hf_rpc_procedure;
static int hf_rpc_auth_flavor;
static int hf_rpc_auth_length;
static int hf_rpc_auth_machinename;
static int hf_rpc_auth_stamp;
static int hf_rpc_auth_lk_owner;
static int hf_rpc_auth_pid;
static int hf_rpc_auth_uid;
static int hf_rpc_auth_gid;
static int hf_rpc_auth_flags;
static int hf_rpc_auth_ctime;
static int hf_rpc_authgss_v;
static int hf_rpc_authgss_proc;
static int hf_rpc_authgss_seq;
static int hf_rpc_authgss_svc;
static int hf_rpc_authgss_ctx;
static int hf_rpc_authgss_ctx_create_frame;
static int hf_rpc_authgss_ctx_destroy_frame;
static int hf_rpc_authgss_ctx_len;
static int hf_rpc_authgss_major;
static int hf_rpc_authgss_minor;
static int hf_rpc_authgss_window;
static int hf_rpc_authgss_token_length;
static int hf_rpc_authgss_data_length;
static int hf_rpc_authgss_data;
static int hf_rpc_authgss_token;
static int hf_rpc_authgss_checksum;
static int hf_rpc_authgssapi_v;
static int hf_rpc_authgssapi_msg;
static int hf_rpc_authgssapi_msgv;
static int hf_rpc_authgssapi_handle;
static int hf_rpc_authgssapi_isn;
static int hf_rpc_authdes_namekind;
static int hf_rpc_authdes_netname;
static int hf_rpc_authdes_convkey;
static int hf_rpc_authdes_window;
static int hf_rpc_authdes_nickname;
static int hf_rpc_authdes_timestamp;
static int hf_rpc_authdes_windowverf;
static int hf_rpc_authdes_timeverf;
static int hf_rpc_state_accept;
static int hf_rpc_state_reply;
static int hf_rpc_state_reject;
static int hf_rpc_state_auth;
static int hf_rpc_dup;
static int hf_rpc_call_dup;
static int hf_rpc_reply_dup;
static int hf_rpc_value_follows;
static int hf_rpc_array_len;
static int hf_rpc_time;
static int hf_rpc_fragments;
static int hf_rpc_fragment;
static int hf_rpc_fragment_overlap;
static int hf_rpc_fragment_overlap_conflict;
static int hf_rpc_fragment_multiple_tails;
static int hf_rpc_fragment_too_long_fragment;
static int hf_rpc_fragment_error;
static int hf_rpc_fragment_count;
static int hf_rpc_reassembled_length;
static int hf_rpc_unknown_body;

/* Generated from convert_proto_tree_add_text.pl */
static int hf_rpc_opaque_data;
static int hf_rpc_no_values;
static int hf_rpc_continuation_data;
static int hf_rpc_fill_bytes;
static int hf_rpc_argument_length;
static int hf_rpc_fragment_data;
static int hf_rpc_opaque_length;

static int ett_rpc;
static int ett_rpc_unknown_program;
static int ett_rpc_fragments;
static int ett_rpc_fragment;
static int ett_rpc_fraghdr;
static int ett_rpc_string;
static int ett_rpc_cred;
static int ett_rpc_verf;
static int ett_rpc_gids;
static int ett_rpc_gss_token;
static int ett_rpc_gss_data;
static int ett_rpc_array;
static int ett_rpc_authgssapi_msg;
static int ett_gss_context;
static int ett_gss_wrap;

static expert_field ei_rpc_cannot_dissect;
static expert_field ei_rpc_segment_needed;

static dissector_handle_t rpc_tcp_handle;
static dissector_handle_t rpc_handle;
static dissector_handle_t gssapi_handle;
static dissector_handle_t data_handle;
static dissector_handle_t rpc_tls_handle;

static dissector_table_t  subdissector_call_table;
static dissector_table_t  subdissector_reply_table;


static unsigned max_rpc_tcp_pdu_size = 4 * 1024 * 1024;

static const fragment_items rpc_frag_items = {
	&ett_rpc_fragment,
	&ett_rpc_fragments,
	&hf_rpc_fragments,
	&hf_rpc_fragment,
	&hf_rpc_fragment_overlap,
	&hf_rpc_fragment_overlap_conflict,
	&hf_rpc_fragment_multiple_tails,
	&hf_rpc_fragment_too_long_fragment,
	&hf_rpc_fragment_error,
	&hf_rpc_fragment_count,
	NULL,
	&hf_rpc_reassembled_length,
	/* Reassembled data field */
	NULL,
	"fragments"
};

/* Hash table with info on RPC program numbers */
GHashTable *rpc_progs;

typedef bool (*rec_dissector_t)(tvbuff_t *, packet_info *, proto_tree *,
	tvbuff_t *, fragment_head *, int, uint32_t, bool, bool);

static void show_rpc_fraginfo(tvbuff_t *tvb, tvbuff_t *frag_tvb, proto_tree *tree,
			      uint32_t rpc_rm, fragment_head *ipfd_head, packet_info *pinfo);


static uint32_t rpc_program;
static uint32_t rpc_version;
static int32_t rpc_min_proc = -1;
static int32_t rpc_max_proc = -1;

static void
rpcstat_find_procs(const char *table_name _U_, ftenum_t selector_type _U_, void *key, void *value _U_, void *user_data _U_)
{
	rpc_proc_info_key *k = (rpc_proc_info_key *)key;

	if (k->prog != rpc_program) {
		return;
	}
	if (k->vers != rpc_version) {
		return;
	}
	if (rpc_min_proc == -1) {
		rpc_min_proc = k->proc;
		rpc_max_proc = k->proc;
	}
	if ((int32_t)k->proc < rpc_min_proc) {
		rpc_min_proc = k->proc;
	}
	if ((int32_t)k->proc > rpc_max_proc) {
		rpc_max_proc = k->proc;
	}
}

static void
rpcstat_init(struct register_srt* srt, GArray* srt_array)
{
	rpcstat_tap_data_t* tap_data = (rpcstat_tap_data_t*)get_srt_table_param_data(srt);
	srt_stat_table *rpc_srt_table;
	int i, hf_index;
	header_field_info *hfi;
	static char table_name[100];

	DISSECTOR_ASSERT(tap_data);

	hf_index=rpc_prog_hf(tap_data->program, tap_data->version);
	hfi=proto_registrar_get_nth(hf_index);

	snprintf(table_name, sizeof(table_name), "%s Version %u", tap_data->prog, tap_data->version);
	rpc_srt_table = init_srt_table(table_name, NULL, srt_array, tap_data->num_procedures, NULL, hfi->abbrev, tap_data);
	for (i = 0; i < rpc_srt_table->num_procs; i++)
	{
		const char *proc_name = rpc_proc_name(NULL, tap_data->program, tap_data->version, i);
		init_srt_table_row(rpc_srt_table, i, proc_name);
		wmem_free(NULL, (void *)proc_name);
	}
}

static tap_packet_status
rpcstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *prv, tap_flags_t flags _U_)
{
	srt_stat_table *rpc_srt_table;
	srt_data_t *data = (srt_data_t *)pss;
	const rpc_call_info_value *ri = (const rpc_call_info_value *)prv;
	rpcstat_tap_data_t* tap_data;

	rpc_srt_table = g_array_index(data->srt_array, srt_stat_table*, 0);
	tap_data = (rpcstat_tap_data_t*)rpc_srt_table->table_specific_data;

	if ((int)ri->proc >= rpc_srt_table->num_procs) {
		/* don't handle this since its outside of known table */
		return TAP_PACKET_DONT_REDRAW;
	}
	/* we are only interested in reply packets */
	if (ri->request) {
		return TAP_PACKET_DONT_REDRAW;
	}
	/* we are only interested in certain program/versions */
	if ( (ri->prog != tap_data->program) || (ri->vers != tap_data->version) ) {
		return TAP_PACKET_DONT_REDRAW;
	}

	add_srt_table_data(rpc_srt_table, ri->proc, &ri->req_time, pinfo);
	return TAP_PACKET_REDRAW;

}

static unsigned
rpcstat_param(register_srt_t* srt, const char* opt_arg, char** err)
{
	int pos = 0;
	int program, version;
	rpcstat_tap_data_t* tap_data;

	if (sscanf(opt_arg, ",%d,%d%n", &program, &version, &pos) == 2)
	{
		tap_data = g_new0(rpcstat_tap_data_t, 1);

		tap_data->prog    = rpc_prog_name(program);
		tap_data->program = program;
		tap_data->version = version;

		set_srt_table_param_data(srt, tap_data);

		rpc_program  = tap_data->program;
		rpc_version  = tap_data->version;
		rpc_min_proc = -1;
		rpc_max_proc = -1;
		/* Need to run over both dissector tables */
		dissector_table_foreach ("rpc.call", rpcstat_find_procs, NULL);
		dissector_table_foreach ("rpc.reply", rpcstat_find_procs, NULL);

		tap_data->num_procedures = rpc_max_proc+1;
		if (rpc_min_proc == -1) {
			*err = ws_strdup_printf("Program:%u version:%u isn't supported", rpc_program, rpc_version);
		}
	}
	else
	{
		*err = g_strdup("<program>,<version>[,<filter>]");
	}

	return pos;
}





/***********************************/
/* Hash array with procedure names */
/***********************************/

/* compare 2 keys */
static int
rpc_proc_equal(const void *k1, const void *k2)
{
	const rpc_proc_info_key* key1 = (const rpc_proc_info_key*) k1;
	const rpc_proc_info_key* key2 = (const rpc_proc_info_key*) k2;

	return ((key1->prog == key2->prog &&
		key1->vers == key2->vers &&
		key1->proc == key2->proc) ?
	true : false);
}

/* calculate a hash key */
static unsigned
rpc_proc_hash(const void *k)
{
	const rpc_proc_info_key* key = (const rpc_proc_info_key*) k;

	return (key->prog ^ (key->vers<<16) ^ (key->proc<<24));
}


/*	return the name associated with a previously registered procedure. */
const char *
rpc_proc_name(wmem_allocator_t *allocator, uint32_t prog, uint32_t vers, uint32_t proc)
{
	rpc_proc_info_key key;
	dissector_handle_t dissect_function;
	char *procname;

	key.prog = prog;
	key.vers = vers;
	key.proc = proc;

	/* Look at both tables for possible procedure names */
	if ((dissect_function = dissector_get_custom_table_handle(subdissector_call_table, &key)) != NULL)
		procname = wmem_strdup(allocator, dissector_handle_get_description(dissect_function));
	else if ((dissect_function = dissector_get_custom_table_handle(subdissector_reply_table, &key)) != NULL)
		procname = wmem_strdup(allocator, dissector_handle_get_description(dissect_function));
	else {
		/* happens only with strange program versions or
		   non-existing dissectors */
		procname = wmem_strdup_printf(allocator, "proc-%u", key.proc);
	}
	return procname;
}

/*----------------------------------------*/
/* end of Hash array with procedure names */
/*----------------------------------------*/


/*********************************/
/* Hash array with program names */
/*********************************/

static void
rpc_prog_free_val(void *v)
{
	rpc_prog_info_value *value = (rpc_prog_info_value*)v;

	g_array_free(value->procedure_hfs, true);
	g_free(value);
}

void
rpc_init_prog(int proto, uint32_t prog, int ett, size_t nvers,
    const rpc_prog_vers_info *versions)
{
	rpc_prog_info_value *value;
	size_t versidx;
	const vsff *proc;

	value = g_new(rpc_prog_info_value, 1);
	value->proto = find_protocol_by_id(proto);
	value->proto_id = proto;
	value->ett = ett;
	value->progname = proto_get_protocol_short_name(value->proto);
	value->procedure_hfs = g_array_new(false, true, sizeof (int));

	g_hash_table_insert(rpc_progs,GUINT_TO_POINTER(prog),value);

	/*
	 * Now register each of the versions of the program.
	 */
	for (versidx = 0; versidx < nvers; versidx++) {
		/*
		 * Add the operation number hfinfo value for this version.
		 */
		value->procedure_hfs = g_array_set_size(value->procedure_hfs,
		    versions[versidx].vers);
		g_array_insert_val(value->procedure_hfs,
		    versions[versidx].vers, *versions[versidx].procedure_hf);

		for (proc = versions[versidx].proc_table; proc->strptr != NULL;
		    proc++) {
			rpc_proc_info_key key;

			key.prog = prog;
			key.vers = versions[versidx].vers;
			key.proc = proc->value;

			if (proc->dissect_call == NULL) {
				fprintf(stderr, "OOPS: No call handler for %s version %u procedure %s\n",
				    proto_get_protocol_long_name(value->proto),
				    versions[versidx].vers,
				    proc->strptr);

				/* Abort out if desired - but don't throw an exception here! */
				if (wireshark_abort_on_dissector_bug)
					REPORT_DISSECTOR_BUG("RPC: No call handler!");

				continue;
			}
			dissector_add_custom_table_handle("rpc.call", g_memdup2(&key, sizeof(rpc_proc_info_key)),
						create_dissector_handle_with_name_and_description(proc->dissect_call, value->proto_id, NULL, proc->strptr));

			if (proc->dissect_reply == NULL) {
				fprintf(stderr, "OOPS: No reply handler for %s version %u procedure %s\n",
				    proto_get_protocol_long_name(value->proto),
				    versions[versidx].vers,
				    proc->strptr);

				/* Abort out if desired - but don't throw an exception here! */
				if (wireshark_abort_on_dissector_bug)
					REPORT_DISSECTOR_BUG("RPC: No reply handler!");

				continue;
			}
			dissector_add_custom_table_handle("rpc.reply", g_memdup2(&key, sizeof(rpc_proc_info_key)),
					create_dissector_handle_with_name_and_description(proc->dissect_reply, value->proto_id, NULL, proc->strptr));
		}
	}
}



/*	return the hf_field associated with a previously registered program.
*/
int
rpc_prog_hf(uint32_t prog, uint32_t vers)
{
	rpc_prog_info_value     *rpc_prog;

	if ((rpc_prog = (rpc_prog_info_value *)g_hash_table_lookup(rpc_progs,GUINT_TO_POINTER(prog)))) {
		return g_array_index(rpc_prog->procedure_hfs, int, vers);
	}
	return -1;
}

/*	return the name associated with a previously registered program. This
	should probably eventually be expanded to use the rpc YP/NIS map
	so that it can give names for programs not handled by wireshark */
const char *
rpc_prog_name(uint32_t prog)
{
	const char *progname = NULL;
	rpc_prog_info_value     *rpc_prog;

	if ((rpc_prog = (rpc_prog_info_value *)g_hash_table_lookup(rpc_progs,GUINT_TO_POINTER(prog))) == NULL) {
		progname = "Unknown";
	}
	else {
		progname = rpc_prog->progname;
	}
	return progname;
}


/*--------------------------------------*/
/* end of Hash array with program names */
/*--------------------------------------*/

/* One of these structures are created for each conversation that contains
 * RPC and contains the state we need to maintain for the conversation.
 */
typedef struct _rpc_conv_info_t {
	wmem_tree_t *xids;
} rpc_conv_info_t;


/* we can not hang this off the conversation structure above since the context
   will be reused across all tcp connections between the client and the server.
   a global tree for all contexts should still be unlikely to have collissions
   here.
*/
wmem_tree_t *authgss_contexts;

unsigned int
rpc_roundup(unsigned int a)
{
	unsigned int mod = a % 4;
	unsigned int ret;
	ret = a + ((mod)? 4-mod : 0);
	/* Check for overflow */
	if (ret < a)
		THROW(ReportedBoundsError);
	return ret;
}


int
dissect_rpc_bool(tvbuff_t *tvb, proto_tree *tree,
		 int hfindex, int offset)
{
	proto_tree_add_item(tree, hfindex, tvb, offset, 4, ENC_BIG_ENDIAN);
	return offset + 4;
}


int
dissect_rpc_uint32(tvbuff_t *tvb, proto_tree *tree,
		   int hfindex, int offset)
{
	proto_tree_add_item(tree, hfindex, tvb, offset, 4, ENC_BIG_ENDIAN);
	return offset + 4;
}


int
dissect_rpc_uint64(tvbuff_t *tvb, proto_tree *tree,
		   int hfindex, int offset)
{
	header_field_info	*hfinfo;

	hfinfo = proto_registrar_get_nth(hfindex);
	DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_UINT64);
	proto_tree_add_item(tree, hfindex, tvb, offset, 8, ENC_BIG_ENDIAN);

	return offset + 8;
}

/*
 * We want to make this function available outside this file and
 * allow callers to pass a dissection function for the opaque data
 */
int
dissect_rpc_opaque_data(tvbuff_t *tvb, int offset,
			proto_tree *tree,
			packet_info *pinfo,
			int hfindex,
			bool fixed_length, uint32_t length,
			bool string_data, const char **string_buffer_ret,
			dissect_function_t *dissect_it)
{
	int data_offset;
	proto_item *string_item = NULL;
	proto_tree *string_tree = NULL;

	uint32_t string_length;
	uint32_t string_length_full;
	uint32_t string_length_packet;
	uint32_t string_length_captured;
	uint32_t string_length_copy;

	int fill_truncated;
	uint32_t fill_length;
	uint32_t fill_length_packet;
	uint32_t fill_length_captured;
	uint32_t fill_length_copy;

	int exception = 0;
	/* int string_item_offset; */

	char *string_buffer = NULL;
	uint8_t *bytes_buffer;
	const char *formatted_text = NULL;

	if (fixed_length) {
		string_length = length;
		data_offset = offset;
	}
	else {
		string_length = tvb_get_ntohl(tvb,offset);
		data_offset = offset + 4;
	}
	string_length_captured = tvb_captured_length_remaining(tvb, data_offset);
	string_length_packet = tvb_reported_length_remaining(tvb, data_offset);
	string_length_full = rpc_roundup(string_length);
	if (string_length_captured < string_length) {
		/* truncated string */
		string_length_copy = string_length_captured;
		fill_truncated = 2;
		fill_length = 0;
		fill_length_copy = 0;
		if (string_length_packet < string_length)
			exception = ReportedBoundsError;
		else
			exception = BoundsError;
	}
	else {
		/* full string data */
		string_length_copy = string_length;
		fill_length = string_length_full - string_length;
		fill_length_captured = tvb_captured_length_remaining(tvb,
		    data_offset + string_length);
		fill_length_packet = tvb_reported_length_remaining(tvb,
		    data_offset + string_length);
		if (fill_length_captured < fill_length) {
			/* truncated fill bytes */
			fill_length_copy = fill_length_packet;
			fill_truncated = 1;
			if (fill_length_packet < fill_length)
				exception = ReportedBoundsError;
			else
				exception = BoundsError;
		}
		else {
			/* full fill bytes */
			fill_length_copy = fill_length;
			fill_truncated = 0;
		}
	}

	/*
	 * If we were passed a dissection routine, make a TVB of the data
	 * and call the dissection routine
	 */

	if (dissect_it) {
		tvbuff_t *opaque_tvb;

		opaque_tvb = tvb_new_subset_length_caplen(tvb, data_offset, string_length_copy,
					    string_length);

		return (*dissect_it)(opaque_tvb, offset, pinfo, tree, NULL);

	}

	if (string_data) {
		string_buffer = tvb_get_string_enc(wmem_packet_scope(), tvb, data_offset, string_length_copy, ENC_ASCII);
	} else {
		bytes_buffer = tvb_memcpy(tvb, wmem_alloc(wmem_packet_scope(), string_length_copy), data_offset, string_length_copy);
	}

	/* calculate a nice printable string */
	if (string_length) {
		if (string_length != string_length_copy) {
			if (string_data) {
				char *formatted;

				formatted = format_text(wmem_packet_scope(), string_buffer, strlen(string_buffer));
				/* copy over the data and append <TRUNCATED> */
				formatted_text=wmem_strdup_printf(wmem_packet_scope(), "%s%s", formatted, RPC_STRING_TRUNCATED);
			} else {
				formatted_text=RPC_STRING_DATA RPC_STRING_TRUNCATED;
			}
		} else {
			if (string_data) {
				formatted_text = format_text(wmem_packet_scope(), string_buffer, strlen(string_buffer));
			} else {
				formatted_text=RPC_STRING_DATA;
			}
		}
	} else {
		formatted_text=RPC_STRING_EMPTY;
	}

	/* string_item_offset = offset; */
	string_tree = proto_tree_add_subtree_format(tree, tvb,offset, -1,
			ett_rpc_string, &string_item, "%s: %s", proto_registrar_get_name(hfindex),
			formatted_text);

	if (!fixed_length) {
		proto_tree_add_uint(string_tree, hf_rpc_opaque_length, tvb,offset, 4, string_length);
		offset += 4;
	}

	if (string_tree) {
		if (string_data) {
			proto_tree_add_string_format(string_tree,
			    hfindex, tvb, offset, string_length_copy,
			    string_buffer,
			    "contents: %s", formatted_text);
		} else {
			proto_tree_add_bytes_format(string_tree,
			    hfindex, tvb, offset, string_length_copy,
			    bytes_buffer,
			    "contents: %s", formatted_text);
		}
	}

	offset += string_length_copy;

	if (fill_length) {
		if (fill_truncated) {
			proto_tree_add_bytes_format_value(string_tree, hf_rpc_fill_bytes, tvb,
					offset, fill_length_copy, NULL, "opaque data<TRUNCATED>");
		}
		else {
			proto_tree_add_bytes_format_value(string_tree, hf_rpc_fill_bytes, tvb,
					offset, fill_length_copy, NULL, "opaque data");
		}
		offset += fill_length_copy;
	}

	proto_item_set_end(string_item, tvb, offset);

	if (string_buffer_ret != NULL)
		*string_buffer_ret = formatted_text;

	/*
	 * If the data was truncated, throw the appropriate exception,
	 * so that dissection stops and the frame is properly marked.
	 */
	if (exception != 0)
		THROW(exception);
	return offset;
}


int
dissect_rpc_string(tvbuff_t *tvb, proto_tree *tree,
		   int hfindex, int offset, const char **string_buffer_ret)
{
	offset = dissect_rpc_opaque_data(tvb, offset, tree, NULL,
	    hfindex, false, 0, true, string_buffer_ret, NULL);
	return offset;
}


int
dissect_rpc_data(tvbuff_t *tvb, proto_tree *tree,
		 int hfindex, int offset)
{
	offset = dissect_rpc_opaque_data(tvb, offset, tree, NULL,
					 hfindex, false, 0, false, NULL, NULL);
	return offset;
}


int
dissect_rpc_bytes(tvbuff_t *tvb, proto_tree *tree,
		  int hfindex, int offset, uint32_t length,
		  bool string_data, const char **string_buffer_ret)
{
	offset = dissect_rpc_opaque_data(tvb, offset, tree, NULL,
	    hfindex, true, length, string_data, string_buffer_ret, NULL);
	return offset;
}


int
dissect_rpc_list(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
		 int offset, dissect_function_t *rpc_list_dissector, void *data)
{
	uint32_t value_follows;

	while (1) {
		value_follows = tvb_get_ntohl(tvb, offset);
		proto_tree_add_boolean(tree,hf_rpc_value_follows, tvb,
			offset, 4, value_follows);
		offset += 4;
		if (value_follows == 1) {
			offset = rpc_list_dissector(tvb, offset, pinfo, tree,
						    data);
		}
		else {
			break;
		}
	}

	return offset;
}

int
dissect_rpc_array(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
	int offset, dissect_function_t *rpc_array_dissector,
	int hfindex)
{
	proto_item* lock_item;
	proto_tree* lock_tree;
	uint32_t	num;

	num = tvb_get_ntohl(tvb, offset);

	lock_item = proto_tree_add_item(tree, hfindex, tvb, offset, -1, ENC_NA);

	lock_tree = proto_item_add_subtree(lock_item, ett_rpc_array);

	if(num == 0) {
		proto_tree_add_item(lock_tree, hf_rpc_no_values, tvb, offset, 4, ENC_NA);
		offset += 4;

		proto_item_set_end(lock_item, tvb, offset);

		return offset;
	}

	offset = dissect_rpc_uint32(tvb, lock_tree,
			hf_rpc_array_len, offset);

	while (num--) {
		offset = rpc_array_dissector(tvb, offset, pinfo, lock_tree, NULL);
	}

	proto_item_set_end(lock_item, tvb, offset);
	return offset;
}

static int
dissect_rpc_authunix_groups(tvbuff_t* tvb, proto_tree* tree, int offset)
{
	unsigned gids_count;
	unsigned gids_i;
	unsigned gids_entry;
	proto_item *gitem = NULL;
	proto_tree *gtree = NULL;

	gids_count = tvb_get_ntohl(tvb,offset);
	gtree = proto_tree_add_subtree_format(tree, tvb, offset,
			4+gids_count*4, ett_rpc_gids, &gitem, "Auxiliary GIDs (%u)", gids_count);
	offset += 4;

	/* first, open with [ */
	if (tree && gids_count > 0)
		proto_item_append_text(gitem, " [");

	for (gids_i = 0 ; gids_i < gids_count; gids_i++) {
		gids_entry = tvb_get_ntohl(tvb,offset);
		if (gtree) {
			proto_tree_add_uint(gtree, hf_rpc_auth_gid, tvb,
				offset, 4, gids_entry);
		}

		/* add at most 16 GIDs to the text */
		if (tree && gids_i < 16) {
			if (gids_i > 0)
				proto_item_append_text(gitem, ", ");

			proto_item_append_text(gitem, "%u", gids_entry);
		} else if (tree && gids_i == 16) {
			proto_item_append_text(gitem, "...");
		}
		offset += 4;
	}

	/* finally, close with ] */
	if (tree && gids_count > 0)
		proto_item_append_text(gitem, "]");

	return offset;
}

static int
dissect_rpc_authunix_cred(tvbuff_t* tvb, proto_tree* tree, int offset)
{
	unsigned stamp;
	unsigned uid;
	unsigned gid;

	stamp = tvb_get_ntohl(tvb,offset);
	proto_tree_add_uint(tree, hf_rpc_auth_stamp, tvb,
			    offset, 4, stamp);
	offset += 4;

	offset = dissect_rpc_string(tvb, tree,
			hf_rpc_auth_machinename, offset, NULL);

	uid = tvb_get_ntohl(tvb,offset);
	proto_tree_add_uint(tree, hf_rpc_auth_uid, tvb,
			    offset, 4, uid);
	offset += 4;

	gid = tvb_get_ntohl(tvb,offset);
	proto_tree_add_uint(tree, hf_rpc_auth_gid, tvb,
			    offset, 4, gid);
	offset += 4;

	offset = dissect_rpc_authunix_groups(tvb, tree, offset);

	return offset;
}

typedef struct _gssauth_context_info_t {
	uint32_t	create_frame;
	uint32_t destroy_frame;
} gssauth_context_info_t;


static int
dissect_rpc_authgss_context(proto_tree *tree, tvbuff_t *tvb, int offset,
			    packet_info *pinfo, rpc_conv_info_t *rpc_conv_info _U_,
			    bool is_create, bool is_destroy)
{
	proto_item *context_item;
	proto_tree *context_tree;
	int old_offset = offset;
	int context_offset;
	uint32_t context_length;
	gssauth_context_info_t *context_info;
	wmem_tree_key_t tkey[2];
	uint32_t key[4] = {0,0,0,0};

	context_tree = proto_tree_add_subtree(tree, tvb, offset, -1,
						ett_gss_context, &context_item, "GSS Context");

	context_length = tvb_get_ntohl(tvb, offset);
	proto_tree_add_item(context_tree, hf_rpc_authgss_ctx_len, tvb, offset, 4, ENC_BIG_ENDIAN);
	offset += 4;

	context_offset = offset;
	proto_tree_add_item(context_tree, hf_rpc_authgss_ctx, tvb, offset, context_length, ENC_NA);
	offset += context_length;

	offset = (offset + 3) & 0xffffffc;

	if (context_length > 16) {
		/* we only track contexts up to 16 bytes in size */
		return offset;
	}

	tvb_memcpy(tvb, key, context_offset, context_length);
	tkey[0].length = 4;
	tkey[0].key    = &key[0];
	tkey[1].length = 0;
	tkey[1].key  = NULL;

	context_info = (gssauth_context_info_t *)wmem_tree_lookup32_array(authgss_contexts, &tkey[0]);
	if(context_info == NULL) {
		tvb_memcpy(tvb, key, context_offset, context_length);
		tkey[0].length = 4;
		tkey[0].key    = &key[0];
		tkey[1].length = 0;
		tkey[1].key  = NULL;

	   	context_info = wmem_new(wmem_file_scope(), gssauth_context_info_t);
		context_info->create_frame  = 0;
		context_info->destroy_frame = 0;
		wmem_tree_insert32_array(authgss_contexts, &tkey[0], context_info);
	}
	if (is_create) {
		context_info->create_frame = pinfo->num;
	}
	if (is_destroy) {
		context_info->destroy_frame = pinfo->num;
	}

	if (context_info->create_frame) {
		proto_item *it;
		it = proto_tree_add_uint(context_tree, hf_rpc_authgss_ctx_create_frame, tvb, 0, 0, context_info->create_frame);
		proto_item_set_generated(it);
	}

	if (context_info->destroy_frame) {
		proto_item *it;
		it = proto_tree_add_uint(context_tree, hf_rpc_authgss_ctx_destroy_frame, tvb, 0, 0, context_info->destroy_frame);
		proto_item_set_generated(it);
	}

	proto_item_set_len(context_item, offset - old_offset);

	return offset;
}

static int
dissect_rpc_authgss_cred(tvbuff_t* tvb, proto_tree* tree, int offset,
			 packet_info *pinfo, rpc_conv_info_t *rpc_conv_info)
{
	unsigned agc_v;
	unsigned agc_proc;
	unsigned agc_seq;
	unsigned agc_svc;

	agc_v = tvb_get_ntohl(tvb, offset);
	proto_tree_add_uint(tree, hf_rpc_authgss_v,
			    tvb, offset, 4, agc_v);
	offset += 4;

	agc_proc = tvb_get_ntohl(tvb, offset);
	proto_tree_add_uint(tree, hf_rpc_authgss_proc,
			    tvb, offset, 4, agc_proc);
	offset += 4;

	agc_seq = tvb_get_ntohl(tvb, offset);
	proto_tree_add_uint(tree, hf_rpc_authgss_seq,
			    tvb, offset, 4, agc_seq);
	offset += 4;

	agc_svc = tvb_get_ntohl(tvb, offset);
	proto_tree_add_uint(tree, hf_rpc_authgss_svc,
			    tvb, offset, 4, agc_svc);
	offset += 4;

	offset = dissect_rpc_authgss_context(tree, tvb, offset, pinfo, rpc_conv_info, false, agc_proc == RPCSEC_GSS_DESTROY ? true : false);

	return offset;
}

static int
dissect_rpc_authdes_desblock(tvbuff_t *tvb, proto_tree *tree,
			     int hfindex, int offset)
{
	proto_tree_add_item(tree, hfindex, tvb, offset, 8, ENC_BIG_ENDIAN);

	return offset + 8;
}

static int
dissect_rpc_authdes_cred(tvbuff_t* tvb, proto_tree* tree, int offset)
{
	unsigned adc_namekind;
	unsigned window = 0;
	unsigned nickname = 0;

	adc_namekind = tvb_get_ntohl(tvb, offset);
	proto_tree_add_uint(tree, hf_rpc_authdes_namekind,
			    tvb, offset, 4, adc_namekind);
	offset += 4;

	switch(adc_namekind)
	{
	case AUTHDES_NAMEKIND_FULLNAME:
		offset = dissect_rpc_string(tvb, tree,
			hf_rpc_authdes_netname, offset, NULL);
		offset = dissect_rpc_authdes_desblock(tvb, tree,
			hf_rpc_authdes_convkey, offset);
		window = tvb_get_ntohl(tvb, offset);
		proto_tree_add_uint(tree, hf_rpc_authdes_window, tvb, offset, 4,
			window);
		offset += 4;
		break;

	case AUTHDES_NAMEKIND_NICKNAME:
		nickname = tvb_get_ntohl(tvb, offset);
		proto_tree_add_uint(tree, hf_rpc_authdes_nickname, tvb, offset, 4,
			nickname);
		offset += 4;
		break;
	}

	return offset;
}

static int
dissect_rpc_authgluster_cred(tvbuff_t* tvb, proto_tree* tree, int offset)
{
	proto_tree_add_item(tree, hf_rpc_auth_lk_owner, tvb, offset,
			    8, ENC_NA);
	offset += 8;

	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_pid, offset);
	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_uid, offset);
	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_gid, offset);
	offset = dissect_rpc_authunix_groups(tvb, tree, offset);

	return offset;
}

static int
dissect_rpc_authglusterfs_v2_cred(tvbuff_t* tvb, proto_tree* tree, int offset)
{
	int len;

	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_pid, offset);
	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_uid, offset);
	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_gid, offset);
	offset = dissect_rpc_authunix_groups(tvb, tree, offset);

	len = tvb_get_ntohl(tvb, offset);
	offset += 4;

	proto_tree_add_item(tree, hf_rpc_auth_lk_owner, tvb, offset,
			    len, ENC_NA);
	offset += len;

	return offset;
}

static int
dissect_rpc_authglusterfs_v3_cred(tvbuff_t* tvb, proto_tree* tree, int offset)
{
	int len;
	nstime_t timestamp;

	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_pid, offset);
	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_uid, offset);
	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_gid, offset);
	offset = dissect_rpc_uint32(tvb, tree, hf_rpc_auth_flags, offset);
	timestamp.secs = tvb_get_ntohl(tvb, offset);
	timestamp.nsecs = (int)tvb_get_ntohi64(tvb, offset + 4);
	if (tree)
		proto_tree_add_time(tree, hf_rpc_auth_ctime, tvb,
					offset, 12, &timestamp);
	offset += 12;

	offset = dissect_rpc_authunix_groups(tvb, tree, offset);

	len = tvb_get_ntohl(tvb, offset);
	offset += 4;

	proto_tree_add_item(tree, hf_rpc_auth_lk_owner, tvb, offset,
			    len, ENC_NA);
	offset += len;

	return offset;
}

static int
dissect_rpc_authgssapi_cred(tvbuff_t* tvb, proto_tree* tree, int offset)
{
	proto_tree_add_item(tree, hf_rpc_authgssapi_v, tvb, offset, 4, ENC_BIG_ENDIAN);
	offset += 4;

	proto_tree_add_item(tree, hf_rpc_authgssapi_msg, tvb, offset, 4, ENC_BIG_ENDIAN);
	offset += 4;

	offset = dissect_rpc_data(tvb, tree, hf_rpc_authgssapi_handle,
			offset);

	return offset;
}

static int
dissect_rpc_cred(tvbuff_t* tvb, proto_tree* tree, int offset,
		 packet_info *pinfo, rpc_conv_info_t *rpc_conv_info)
{
	unsigned flavor;
	unsigned length;

	proto_tree *ctree;

	flavor = tvb_get_ntohl(tvb,offset);
	length = tvb_get_ntohl(tvb,offset+4);
	length = rpc_roundup(length);

	if (tree) {
		ctree = proto_tree_add_subtree(tree, tvb, offset,
					    8+length, ett_rpc_cred, NULL, "Credentials");
		proto_tree_add_uint(ctree, hf_rpc_auth_flavor, tvb,
				    offset, 4, flavor);
		proto_tree_add_uint(ctree, hf_rpc_auth_length, tvb,
				    offset+4, 4, length);

		switch (flavor) {
		case AUTH_UNIX:
			dissect_rpc_authunix_cred(tvb, ctree, offset+8);
			break;
		/*
		case AUTH_SHORT:

		break;
		*/
		case AUTH_DES:
			dissect_rpc_authdes_cred(tvb, ctree, offset+8);
			break;

		case AUTH_RSA:
			/* AUTH_RSA is (ab)used by Gluster */
			dissect_rpc_authgluster_cred(tvb, ctree, offset+8);
			break;

		case RPCSEC_GSS:
			dissect_rpc_authgss_cred(tvb, ctree, offset+8, pinfo, rpc_conv_info);
			break;

		case AUTH_GLUSTERFS:
			dissect_rpc_authglusterfs_v2_cred(tvb, ctree, offset+8);
			break;

		case AUTH_GLUSTERFS_V3:
			dissect_rpc_authglusterfs_v3_cred(tvb, ctree, offset+8);
			break;

		case AUTH_GSSAPI:
			dissect_rpc_authgssapi_cred(tvb, ctree, offset+8);
			break;

		default:
			if (length)
				proto_tree_add_item(ctree, hf_rpc_opaque_data, tvb, offset+8, length, ENC_NA);
		break;
		}
	}
	offset += 8 + length;

	return offset;
}

int
dissect_rpc_opaque_auth(tvbuff_t* tvb, proto_tree* tree, int offset,
			packet_info *pinfo)
{
	conversation_t *conv = NULL;
	rpc_conv_info_t *conv_info = NULL;

	if (pinfo->ptype == PT_TCP)
		conv = find_conversation_pinfo(pinfo, 0);

	if (conv)
		conv_info = (rpc_conv_info_t *)conversation_get_proto_data(conv,
							proto_rpc);

	return dissect_rpc_cred(tvb, tree, offset, pinfo, conv_info);
}

/*
 * XDR opaque object, the contents of which are interpreted as a GSS-API
 * token.
 */
static int
dissect_rpc_authgss_token(tvbuff_t* tvb, proto_tree* tree, int offset,
			  packet_info *pinfo, int hfindex)
{
	uint32_t opaque_length, rounded_length;
	int len_consumed, length, reported_length;
	tvbuff_t *new_tvb;

	proto_item *gitem;
	proto_tree *gtree = NULL;

	opaque_length = tvb_get_ntohl(tvb, offset);
	rounded_length = rpc_roundup(opaque_length);

	gitem = proto_tree_add_item(tree, hfindex, tvb, offset, 4+rounded_length, ENC_NA);
	gtree = proto_item_add_subtree(gitem, ett_rpc_gss_token);
	proto_tree_add_uint(gtree, hf_rpc_authgss_token_length,
			    tvb, offset, 4, opaque_length);
	offset += 4;

	if (opaque_length != 0) {
		length = tvb_captured_length_remaining(tvb, offset);
		reported_length = tvb_reported_length_remaining(tvb, offset);
		if (length > reported_length)
			length = reported_length;
		if ((uint32_t)length > opaque_length)
			length = opaque_length;
		if ((uint32_t)reported_length > opaque_length)
			reported_length = opaque_length;
		new_tvb = tvb_new_subset_length_caplen(tvb, offset, length, reported_length);
		len_consumed = call_dissector(gssapi_handle, new_tvb, pinfo, gtree);
		offset += len_consumed;
	}
	offset = rpc_roundup(offset);
	return offset;
}

/* AUTH_DES verifiers are asymmetrical, so we need to know what type of
 * verifier we're decoding (CALL or REPLY).
 */
static int
dissect_rpc_verf(tvbuff_t* tvb, proto_tree* tree, int offset, int msg_type,
		 packet_info *pinfo)
{
	unsigned flavor;
	unsigned length;

	proto_tree *vtree;

	flavor = tvb_get_ntohl(tvb,offset);
	length = tvb_get_ntohl(tvb,offset+4);
	length = rpc_roundup(length);

	if (tree) {
		vtree = proto_tree_add_subtree(tree, tvb, offset,
					    8+length, ett_rpc_verf, NULL, "Verifier");
		proto_tree_add_uint(vtree, hf_rpc_auth_flavor, tvb,
				    offset, 4, flavor);

		switch (flavor) {
		case AUTH_UNIX:
			proto_tree_add_uint(vtree, hf_rpc_auth_length, tvb,
					    offset+4, 4, length);
			dissect_rpc_authunix_cred(tvb, vtree, offset+8);
			break;
		case AUTH_DES:
			proto_tree_add_uint(vtree, hf_rpc_auth_length, tvb,
				offset+4, 4, length);

			if (msg_type == RPC_CALL)
			{
				unsigned window;

				dissect_rpc_authdes_desblock(tvb, vtree,
					hf_rpc_authdes_timestamp, offset+8);
				window = tvb_get_ntohl(tvb, offset+16);
				proto_tree_add_uint(vtree, hf_rpc_authdes_windowverf, tvb,
					offset+16, 4, window);
			}
			else
			{
				/* must be an RPC_REPLY */
				unsigned nickname;

				dissect_rpc_authdes_desblock(tvb, vtree,
					hf_rpc_authdes_timeverf, offset+8);
				nickname = tvb_get_ntohl(tvb, offset+16);
				proto_tree_add_uint(vtree, hf_rpc_authdes_nickname, tvb,
				 	offset+16, 4, nickname);
			}
			break;
		case RPCSEC_GSS:
			dissect_rpc_authgss_token(tvb, vtree, offset+4, pinfo, hf_rpc_authgss_token);
			break;
		default:
			proto_tree_add_uint(vtree, hf_rpc_auth_length, tvb,
					    offset+4, 4, length);
			if (length)
				proto_tree_add_item(vtree, hf_rpc_opaque_data, tvb, offset+8, length, ENC_NA);
			break;
		}
	}
	offset += 8 + length;

	return offset;
}

static int
dissect_rpc_authgss_initarg(tvbuff_t* tvb, proto_tree* tree, int offset,
			    packet_info *pinfo)
{
	return dissect_rpc_authgss_token(tvb, tree, offset, pinfo, hf_rpc_authgss_token);
}

static int
dissect_rpc_authgss_initres(tvbuff_t* tvb, proto_tree* tree, int offset,
			    packet_info *pinfo, rpc_conv_info_t *rpc_conv_info)
{
	int major, minor, window;

	offset = dissect_rpc_authgss_context(tree, tvb, offset, pinfo, rpc_conv_info, true, false);

	major = tvb_get_ntohl(tvb,offset);
	proto_tree_add_uint(tree, hf_rpc_authgss_major, tvb,
			    offset, 4, major);
	offset += 4;

	minor = tvb_get_ntohl(tvb,offset);
	proto_tree_add_uint(tree, hf_rpc_authgss_minor, tvb,
			    offset, 4, minor);
	offset += 4;

	window = tvb_get_ntohl(tvb,offset);
	proto_tree_add_uint(tree, hf_rpc_authgss_window, tvb,
			    offset, 4, window);
	offset += 4;

	offset = dissect_rpc_authgss_token(tvb, tree, offset, pinfo, hf_rpc_authgss_token);

	return offset;
}

static int
dissect_rpc_authgssapi_initarg(tvbuff_t* tvb, proto_tree* tree, int offset,
			       packet_info *pinfo)
{
	unsigned version;
	proto_tree *mtree;

	mtree = proto_tree_add_subtree(tree, tvb, offset, -1,
		ett_rpc_authgssapi_msg, NULL, "AUTH_GSSAPI Msg");

	version = tvb_get_ntohl(tvb, offset);

	proto_tree_add_uint(mtree, hf_rpc_authgssapi_msgv, tvb, offset, 4, version);
	offset += 4;

	offset = dissect_rpc_authgss_token(tvb, mtree, offset, pinfo, hf_rpc_authgss_token);

	return offset;
}

static int
dissect_rpc_authgssapi_initres(tvbuff_t* tvb, proto_tree* tree, int offset,
			       packet_info *pinfo)
{
	unsigned version;
	unsigned major, minor;
	proto_tree *mtree;

	mtree = proto_tree_add_subtree(tree, tvb, offset, -1,
		    ett_rpc_authgssapi_msg, NULL, "AUTH_GSSAPI Msg");

	version = tvb_get_ntohl(tvb,offset);
	proto_tree_add_uint(mtree, hf_rpc_authgssapi_msgv, tvb,
				    offset, 4, version);
	offset += 4;

	offset = dissect_rpc_data(tvb, mtree, hf_rpc_authgssapi_handle,
			offset);

	major = tvb_get_ntohl(tvb,offset);
	proto_tree_add_uint(mtree, hf_rpc_authgss_major, tvb,
				    offset, 4, major);
	offset += 4;

	minor = tvb_get_ntohl(tvb,offset);
	proto_tree_add_uint(mtree, hf_rpc_authgss_minor, tvb,
				    offset, 4, minor);
	offset += 4;

	offset = dissect_rpc_authgss_token(tvb, mtree, offset, pinfo, hf_rpc_authgss_token);

	offset = dissect_rpc_data(tvb, mtree, hf_rpc_authgssapi_isn, offset);

	return offset;
}

static int
dissect_auth_gssapi_data(tvbuff_t *tvb, proto_tree *tree, int offset)
{
	offset = dissect_rpc_data(tvb, tree, hf_rpc_authgss_data,
			offset);
	return offset;
}

static int
call_dissect_function(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
	int offset, dissector_handle_t dissect_function, const char *progname,
	rpc_call_info_value *rpc_call)
{
	const char *saved_proto;
	tvbuff_t *next_tvb;

	if (dissect_function != NULL) {
		/* set the current protocol name */
		saved_proto = pinfo->current_proto;
		if (progname != NULL)
			pinfo->current_proto = progname;

		/* call the dissector for the next level */
		next_tvb = tvb_new_subset_remaining(tvb, offset);
		offset += call_dissector_with_data(dissect_function, next_tvb, pinfo, tree, rpc_call);

		/* restore the protocol name */
		pinfo->current_proto = saved_proto;
	}

	return offset;
}


static int
dissect_rpc_authgss_integ_data(tvbuff_t *tvb, packet_info *pinfo,
	proto_tree *tree, int offset,
	dissector_handle_t dissect_function,
	const char *progname, rpc_call_info_value *rpc_call)
{
	int reported_length, captured_length;
	uint32_t length, rounded_length, seq;

	proto_tree *gtree;

	reported_length = tvb_reported_length_remaining(tvb, offset);
	captured_length = tvb_captured_length_remaining(tvb, offset);
	length = tvb_get_ntohl(tvb, offset);
	rounded_length = rpc_roundup(length);
	seq = tvb_get_ntohl(tvb, offset+4);

	if (captured_length < reported_length) {
		/* Set rounded length so it does not croak while setting up the
		 * GSS Data subtree when the packet has been truncated so at
		 * least the rest of the packet could be partially dissected */
		rounded_length = captured_length - 4;
	}
	gtree = proto_tree_add_subtree(tree, tvb, offset,
				    4+rounded_length, ett_rpc_gss_data, NULL, "GSS Data");
	proto_tree_add_uint(gtree, hf_rpc_authgss_data_length,
			    tvb, offset, 4, length);
	proto_tree_add_uint(gtree, hf_rpc_authgss_seq,
			    tvb, offset+4, 4, seq);
	offset += 8;

	/* offset = */
	call_dissect_function(tvb, pinfo, gtree, offset, dissect_function, progname, rpc_call);

	offset += rounded_length - 4;
	offset = dissect_rpc_authgss_token(tvb, tree, offset, pinfo, hf_rpc_authgss_checksum);

	return offset;
}

static int
dissect_rpc_authgss_priv_data(tvbuff_t *tvb, proto_tree *tree, int offset,
			      packet_info *pinfo, gssapi_encrypt_info_t* gssapi_encrypt)
{
	int length;
	/* int return_offset; */

	length = tvb_get_ntohl(tvb, offset);
	proto_tree_add_uint(tree, hf_rpc_authgss_data_length,
				    tvb, offset, 4, length);
	offset += 4;

	proto_tree_add_item(tree, hf_rpc_authgss_data, tvb, offset, length,
				ENC_NA);


	/* can't decrypt if we don't have SPNEGO */
	if (!spnego_krb5_wrap_handle) {
		offset += length;
		return offset;
	}

	/* return_offset = */ call_dissector_with_data(spnego_krb5_wrap_handle,
		             tvb_new_subset_remaining(tvb, offset),
			     pinfo, tree, gssapi_encrypt);

	offset += length;
	return offset;
}

static address null_address = ADDRESS_INIT_NONE;

/*
 * Attempt to find a conversation for a call and, if we don't find one,
 * create a new one.
 */
static conversation_t *
get_conversation_for_call(packet_info *pinfo)
{
	conversation_t *conversation;

	/*
	 * If the transport is connection-oriented (TCP or Infiniband),
	 * we use the addresses and ports of both endpoints, because
	 * the addresses and ports of the two endpoints should be
	 * the same for a call and its matching reply.  (XXX - what
	 * if the connection is broken and re-established between
	 * the call and the reply?  Or will the call be re-made on
	 * the new connection?)
	 *
	 * If the transport is connectionless, we don't worry
	 * about the address to which the call was sent, because
	 * there's no guarantee that the reply will come from the
	 * address to which the call was sent.  We also don't
	 * worry about the port *from* which the call was sent,
	 * because some clients (*cough* macOS NFS client *cough*)
	 * might send retransmissions from a different port from
	 * the original request.
	 */
	if (pinfo->ptype == PT_TCP || pinfo->ptype == PT_IBQP || pinfo->ptype == PT_IWARP_MPA) {
		conversation = find_conversation_pinfo(pinfo, 0);
	} else {
		/*
		 * XXX - you currently still have to pass a non-null
		 * pointer for the second address argument even
		 * if you use NO_ADDR_B.
		 */
		conversation = find_conversation(pinfo->num,
		    &pinfo->src, &null_address, conversation_pt_to_conversation_type(pinfo->ptype),
		    pinfo->destport, 0, NO_ADDR_B|NO_PORT_B);
	}

	if (conversation == NULL) {
		if (pinfo->ptype == PT_TCP || pinfo->ptype == PT_IBQP || pinfo->ptype == PT_IWARP_MPA) {
			conversation = conversation_new(pinfo->num,
			    &pinfo->src, &pinfo->dst, conversation_pt_to_conversation_type(pinfo->ptype),
			    pinfo->srcport, pinfo->destport, 0);
		} else {
			conversation = conversation_new(pinfo->num,
			    &pinfo->src, &null_address, conversation_pt_to_conversation_type(pinfo->ptype),
			    pinfo->destport, 0, NO_ADDR2|NO_PORT2);
		}
	}
	return conversation;
}

static conversation_t *
find_conversation_for_reply(packet_info *pinfo)
{
	conversation_t *conversation;

	/*
	 * If the transport is connection-oriented (TCP or Infiniband),
	 * we use the addresses and ports of both endpoints, because
	 * the addresses and ports of the two endpoints should be
	 * the same for a call and its matching reply.  (XXX - what
	 * if the connection is broken and re-established between
	 * the call and the reply?  Or will the call be re-made on
	 * the new connection?)
	 *
	 * If the transport is connectionless, we don't worry
	 * about the address from which the reply was sent,
	 * because there's no guarantee that the call was sent
	 * to the address from which the reply came.  We also
	 * don't worry about the port *to* which the reply was
	 * sent, because some clients (*cough* macOS NFS client
	 * *cough*) might send call retransmissions from a
	 * different port from the original request, so replies
	 * to the original call and a retransmission of the call
	 * might be sent to different ports.
	 */
	if (pinfo->ptype == PT_TCP || pinfo->ptype == PT_IBQP || pinfo->ptype == PT_IWARP_MPA) {
		conversation = find_conversation_pinfo(pinfo, 0);
	} else {
		/*
		 * XXX - you currently still have to pass a non-null
		 * pointer for the second address argument even
		 * if you use NO_ADDR_B.
		 */
		conversation = find_conversation(pinfo->num,
		    &pinfo->dst, &null_address, conversation_pt_to_conversation_type(pinfo->ptype),
		    pinfo->srcport, 0, NO_ADDR_B|NO_PORT_B);
	}
	return conversation;
}

static conversation_t *
new_conversation_for_reply(packet_info *pinfo)
{
	conversation_t *conversation;

	switch (pinfo->ptype)
	{
	case PT_TCP:
		conversation = conversation_new(pinfo->num,
		    &pinfo->src, &pinfo->dst, CONVERSATION_TCP,
		    pinfo->srcport, pinfo->destport, 0);
		break;
	case PT_IBQP:
		conversation = conversation_new(pinfo->num,
		    &pinfo->src, &pinfo->dst, CONVERSATION_IBQP,
		    pinfo->srcport, pinfo->destport, 0);
		break;
	case PT_IWARP_MPA:
		conversation = conversation_new(pinfo->num,
		    &pinfo->src, &pinfo->dst, CONVERSATION_IWARP_MPA,
		    pinfo->srcport, pinfo->destport, 0);
		break;
	default:
		conversation = conversation_new(pinfo->num,
		    &pinfo->dst, &null_address, conversation_pt_to_conversation_type(pinfo->ptype),
		    pinfo->srcport, 0, NO_ADDR2|NO_PORT2);
		break;
	}
	return conversation;
}

/*
 * Dissect the arguments to an indirect call; used by the portmapper/RPCBIND
 * dissector for the CALLIT procedure.
 *
 * Record these in the same table as the direct calls
 * so we can find it when dissecting an indirect call reply.
 * (There should not be collissions between xid between direct and
 * indirect calls.)
 */
int
dissect_rpc_indir_call(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
		       int offset, int args_id, uint32_t prog, uint32_t vers, uint32_t proc)
{
	conversation_t* conversation;
	rpc_proc_info_key key;
	rpc_call_info_value *rpc_call;
	dissector_handle_t dissect_function = NULL;
	rpc_conv_info_t *rpc_conv_info=NULL;
	uint32_t xid;

	key.prog = prog;
	key.vers = vers;
	key.proc = proc;
	if ((dissect_function = dissector_get_custom_table_handle(subdissector_call_table, &key)) != NULL) {
		/*
		 * Establish a conversation for the call, for use when
		 * matching calls with replies.
		 */
		conversation = get_conversation_for_call(pinfo);

		/*
		 * Do we already have a state structure for this conv
		 */
		rpc_conv_info = (rpc_conv_info_t *)conversation_get_proto_data(conversation, proto_rpc);
		if (!rpc_conv_info) {
			/* No.  Attach that information to the conversation, and add
			 * it to the list of information structures.
			 */
			rpc_conv_info = wmem_new(wmem_file_scope(), rpc_conv_info_t);
			rpc_conv_info->xids=wmem_tree_new(wmem_file_scope());

			conversation_add_proto_data(conversation, proto_rpc, rpc_conv_info);
		}

		/* Make the dissector for this conversation the non-heuristic
		   RPC dissector. */
		conversation_set_dissector(conversation,
		    (pinfo->ptype == PT_TCP) ? rpc_tcp_handle : rpc_handle);

		/* Dissectors for RPC procedure calls and replies shouldn't
		   create new tvbuffs, and we don't create one ourselves,
		   so we should have been handed the tvbuff for this RPC call;
		   as such, the XID is at offset 0 in this tvbuff. */
		/* look up the request */
		xid = tvb_get_ntohl(tvb, offset);
		rpc_call = (rpc_call_info_value *)wmem_tree_lookup32(rpc_conv_info->xids, xid);
		if (rpc_call == NULL) {
			/* We didn't find it; create a new entry.
			   Prepare the value data.
			   Not all of it is needed for handling indirect
			   calls, so we set a bunch of items to 0. */
			rpc_call = wmem_new(wmem_file_scope(), rpc_call_info_value);
			rpc_call->req_num = 0;
			rpc_call->rep_num = 0;
			rpc_call->prog = prog;
			rpc_call->vers = vers;
			rpc_call->proc = proc;
			rpc_call->private_data = NULL;

			/*
			 * XXX - what about RPCSEC_GSS?
			 * Do we have to worry about it?
			 */
			rpc_call->flavor = FLAVOR_NOT_GSSAPI;
			rpc_call->gss_proc = 0;
			rpc_call->gss_svc = 0;
			/* store it */
			wmem_tree_insert32(rpc_conv_info->xids, xid, (void *)rpc_call);
		}
	}
	else {
		/* We don't know the procedure.
		   Happens only with strange program versions or
		   non-existing dissectors.
		   Just show the arguments as opaque data. */
		offset = dissect_rpc_data(tvb, tree, args_id,
		    offset);
		return offset;
	}

	proto_tree_add_item(tree, hf_rpc_argument_length, tvb, offset, 4, ENC_BIG_ENDIAN);
	offset += 4;

	/* Dissect the arguments */
	offset = call_dissect_function(tvb, pinfo, tree, offset,
			dissect_function, NULL, rpc_call);
	return offset;
}

/*
 * Dissect the results in an indirect reply; used by the portmapper/RPCBIND
 * dissector.
 */
int
dissect_rpc_indir_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
			int offset, int result_id, int prog_id, int vers_id, int proc_id)
{
	conversation_t* conversation;
	rpc_call_info_value *rpc_call;
	const char *procname=NULL;
	dissector_handle_t dissect_function = NULL;
	rpc_conv_info_t *rpc_conv_info=NULL;
	rpc_proc_info_key	key;
	uint32_t xid;

	/*
	 * Look for the matching call in the xid table.
	 * A reply must match a call that we've seen, and the
	 * reply must be sent to the same address that the call came
	 * from, and must come from the port to which the call was sent.
	 */
	conversation = find_conversation_for_reply(pinfo);
	if (conversation == NULL) {
		/* We haven't seen an RPC call for that conversation,
		   so we can't check for a reply to that call.
		   Just show the reply stuff as opaque data. */
		offset = dissect_rpc_data(tvb, tree, result_id,
		    offset);
		return offset;
	}
	/*
	 * Do we already have a state structure for this conv
	 */
	rpc_conv_info = (rpc_conv_info_t *)conversation_get_proto_data(conversation, proto_rpc);
	if (!rpc_conv_info) {
		/* No.  Attach that information to the conversation, and add
		 * it to the list of information structures.
		 */
		rpc_conv_info = wmem_new(wmem_file_scope(), rpc_conv_info_t);
		rpc_conv_info->xids=wmem_tree_new(wmem_file_scope());
		conversation_add_proto_data(conversation, proto_rpc, rpc_conv_info);
	}

	/* The XIDs of the call and reply must match. */
	xid = tvb_get_ntohl(tvb, 0);
	rpc_call = (rpc_call_info_value *)wmem_tree_lookup32(rpc_conv_info->xids, xid);
	if (rpc_call == NULL) {
		/* The XID doesn't match a call from that
		   conversation, so it's probably not an RPC reply.
		   Just show the reply stuff as opaque data. */
		offset = dissect_rpc_data(tvb, tree, result_id,
		    offset);
		return offset;
	}

	key.prog = rpc_call->prog;
	key.vers = rpc_call->vers;
	key.proc = rpc_call->proc;

	dissect_function = dissector_get_custom_table_handle(subdissector_reply_table, &key);
	if (dissect_function != NULL) {
		procname = dissector_handle_get_description(dissect_function);
	}
	else {
		procname=wmem_strdup_printf(wmem_packet_scope(), "proc-%u", rpc_call->proc);
	}

	if ( tree )
	{
		proto_item *tmp_item;

		/* Put the program, version, and procedure into the tree. */
		tmp_item=proto_tree_add_uint_format(tree, prog_id, tvb,
			0, 0, rpc_call->prog, "Program: %s (%u)",
			rpc_prog_name(rpc_call->prog), rpc_call->prog);
		proto_item_set_generated(tmp_item);

		tmp_item=proto_tree_add_uint(tree, vers_id, tvb, 0, 0, rpc_call->vers);
		proto_item_set_generated(tmp_item);

		tmp_item=proto_tree_add_uint_format(tree, proc_id, tvb,
			0, 0, rpc_call->proc, "Procedure: %s (%u)",
			procname, rpc_call->proc);
		proto_item_set_generated(tmp_item);
	}

	if (dissect_function == NULL) {
		/* We don't know how to dissect the reply procedure.
		   Just show the reply stuff as opaque data. */
		offset = dissect_rpc_data(tvb, tree, result_id,
		    offset);
		return offset;
	}

	/* Put the length of the reply value into the tree. */
	proto_tree_add_item(tree, hf_rpc_argument_length, tvb, offset, 4, ENC_BIG_ENDIAN);
	offset += 4;

	/* Dissect the return value */
	offset = call_dissect_function(tvb, pinfo, tree, offset,
			dissect_function, NULL, rpc_call);
	return offset;
}

/*
 * Just mark this as a continuation of an earlier packet.
 */
static void
dissect_rpc_continuation(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	proto_item *rpc_item;
	proto_tree *rpc_tree;

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "RPC");
	col_set_str(pinfo->cinfo, COL_INFO, "Continuation");

	rpc_item = proto_tree_add_item(tree, proto_rpc, tvb, 0, -1, ENC_NA);
	rpc_tree = proto_item_add_subtree(rpc_item, ett_rpc);
	proto_tree_add_item(rpc_tree, hf_rpc_continuation_data, tvb, 0, -1, ENC_NA);
}


int
dissect_rpc_void(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, void *data _U_)
{
	return 0;
}

int
dissect_rpc_unknown(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, void *data _U_)
{
	unsigned captured_length = tvb_captured_length(tvb);

	proto_tree_add_item(tree, hf_rpc_unknown_body, tvb, 0, captured_length, ENC_NA);
	return captured_length;
}

static rpc_prog_info_value *
looks_like_rpc_call(tvbuff_t *tvb, int offset)
{
	uint32_t rpc_prog_key;
	rpc_prog_info_value *rpc_prog;

	if (!tvb_bytes_exist(tvb, offset, 16)) {
		/* Captured data in packet isn't enough to let us
		   tell. */
		return NULL;
	}

	/* XID can be anything, so don't check it.
	   We already have the message type.
	   Check whether an RPC version number of 2 is in the
	   location where it would be, and that an RPC program
	   number we know about is in the location where it would be.

	   Sun's snoop just checks for a message direction of RPC_CALL
	   and a version number of 2, which is a bit of a weak heuristic.

	   We could conceivably check for any of the program numbers
	   in the list at

		ftp://ftp.tau.ac.il/pub/users/eilon/rpc/rpc

	   and report it as RPC (but not dissect the payload if
	   we don't have a subdissector) if it matches. */
	rpc_prog_key = tvb_get_ntohl(tvb, offset + 12);

	/* we only dissect RPC version 2 */
	if (tvb_get_ntohl(tvb, offset + 8) != 2)
		return NULL;

	/* Do we know this program? */
	rpc_prog = (rpc_prog_info_value *)g_hash_table_lookup(rpc_progs, GUINT_TO_POINTER(rpc_prog_key));
	if (rpc_prog == NULL) {
		uint32_t version;

		/*
		 * No.
		 * If the user has specified that he wants to try to
		 * dissect even completely unknown RPC program numbers,
		 * then let him do that.
		 */
		if (!rpc_dissect_unknown_programs) {
			/* They didn't, so just fail. */
			return NULL;
		}

		/*
		 * The user wants us to try to dissect even
		 * unknown program numbers.
		 *
		 * Use some heuristics to keep from matching any
		 * packet with a 2 in the appropriate location.
		 * We check that the program number is neither
		 * 0 nor -1, and that the version is <= 10, which
		 * is better than nothing.
		 */
		if (rpc_prog_key == 0 || rpc_prog_key == 0xffffffff)
			return false;
		version = tvb_get_ntohl(tvb, offset+16);
		if (version > 10)
			return NULL;

		rpc_prog = wmem_new0(wmem_packet_scope(), rpc_prog_info_value);
		rpc_prog->ett = ett_rpc_unknown_program;
		rpc_prog->progname = wmem_strdup_printf(wmem_packet_scope(), "Unknown RPC program %u", rpc_prog_key);
	}

	return rpc_prog;
}

static rpc_call_info_value *
looks_like_rpc_reply(tvbuff_t *tvb, packet_info *pinfo, int offset)
{
	unsigned int xid;
	conversation_t *conversation;
	rpc_conv_info_t *rpc_conv_info;
	rpc_call_info_value *rpc_call;

	/* A reply must match a call that we've seen, and the reply
	   must be sent to the same address that the call came from,
	   and must come from the port to which the call was sent.

	   If the transport is connection-oriented (we check, for
	   now, only for "pinfo->ptype" of PT_TCP), we take
	   into account the port from which the call was sent
	   and the address to which the call was sent, because
	   the addresses and ports of the two endpoints should be
	   the same for all calls and replies.

	   If the transport is connectionless, we don't worry
	   about the address from which the reply was sent,
	   because there's no guarantee that the call was sent
	   to the address from which the reply came.  We also
	   don't worry about the port *to* which the reply was
	   sent, because some clients (*cough* macOS NFS client
	   *cough*) might send retransmissions from a
	   different port from the original request, so replies
	   to the original call and a retransmission of the call
	   might be sent to different ports.

	   Sun's snoop just checks for a message direction of RPC_REPLY
	   and a status of MSG_ACCEPTED or MSG_DENIED, which is a bit
	   of a weak heuristic. */
	xid = tvb_get_ntohl(tvb, offset);
	conversation = find_conversation_for_reply(pinfo);
	if (conversation != NULL) {
		/*
		 * We have a conversation; try to find an RPC
		 * state structure for the conversation.
		 */
		rpc_conv_info = (rpc_conv_info_t *)conversation_get_proto_data(conversation, proto_rpc);
		if (rpc_conv_info != NULL)
			rpc_call = (rpc_call_info_value *)wmem_tree_lookup32(rpc_conv_info->xids, xid);
		else
			rpc_call = NULL;
	} else {
		/*
		 * We don't have a conversation, so we obviously
		 * don't have an RPC state structure for it.
		 */
		rpc_conv_info = NULL;
		rpc_call = NULL;
	}

	if (rpc_call == NULL) {
		/*
		 * We don't have a conversation, or we have one but
		 * there's no RPC state information for it, or
		 * we have RPC state information but the XID doesn't
		 * match a call from the conversation, so it's
		 * probably not an RPC reply.
		 *
		 * Unless we're permitted to scan for embedded records
		 * and this is a connection-oriented transport,
		 * give up.
		 */
		if (((! rpc_find_fragment_start) || (pinfo->ptype != PT_TCP)) && (pinfo->ptype != PT_IBQP) && (pinfo->ptype != PT_IWARP_MPA)) {
			return NULL;
		}

		/*
		 * Do we have a conversation to which to attach
		 * that information?
		 */
		if (conversation == NULL) {
			/*
			 * It's not part of any conversation - create
			 * a new one.
			 */
			conversation = new_conversation_for_reply(pinfo);
		}

		/*
		 * Do we have RPC information for that conversation?
		 */
		if (rpc_conv_info == NULL) {
			/*
			 * No.  Create a new RPC information structure,
			 * and attach it to the conversation.
			 */
			rpc_conv_info = wmem_new(wmem_file_scope(), rpc_conv_info_t);
			rpc_conv_info->xids=wmem_tree_new(wmem_file_scope());

			conversation_add_proto_data(conversation, proto_rpc, rpc_conv_info);
		}

		/*
		 * Define a dummy call for this reply.
		 */
		rpc_call = wmem_new0(wmem_file_scope(), rpc_call_info_value);
		rpc_call->rep_num = pinfo->num;
		rpc_call->xid = xid;
		rpc_call->flavor = FLAVOR_NOT_GSSAPI;  /* total punt */
		rpc_call->req_time = pinfo->abs_ts;

		/* store it */
		wmem_tree_insert32(rpc_conv_info->xids, xid, (void *)rpc_call);
	}

	/* pass rpc_info to subdissectors */
	rpc_call->request = false;
	return rpc_call;
}

static bool
dissect_rpc_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
		    tvbuff_t *frag_tvb, fragment_head *ipfd_head, int tp_type,
		    uint32_t rpc_rm, bool first_pdu, bool can_defragment)
{
	uint32_t	msg_type;
	rpc_call_info_value *rpc_call;
	rpc_prog_info_value *rpc_prog;
	uint32_t rpc_prog_key;

	unsigned int xid;
	unsigned int rpcvers;
	unsigned int prog = 0;
	unsigned int vers = 0;
	unsigned int proc = 0;
	unsigned int cred_flavor = 0;
	flavor_t flavor = FLAVOR_UNKNOWN;
	unsigned int gss_proc = 0;
	unsigned int gss_svc = 0;
	protocol_t *proto = NULL;
	int	proto_id = 0;
	int	ett = 0;
	int	procedure_hf;

	unsigned int reply_state;
	unsigned int accept_state;
	unsigned int reject_state;

	const char *msg_type_name;
	const char *progname;
	const char *procname;

	unsigned int vers_low;
	unsigned int vers_high;

	unsigned int auth_state;

	proto_item *rpc_item = NULL;
	proto_tree *rpc_tree = NULL;

	proto_item *pitem = NULL;
	proto_tree *ptree = NULL;
	int offset = (tp_type != RPC_UDP && tvb == frag_tvb) ? 4 : 0;

	rpc_proc_info_key	key;
	conversation_t* conversation;
	nstime_t ns;

	dissector_handle_t dissect_function;
	bool dissect_rpc_flag = true;

	rpc_conv_info_t *rpc_conv_info=NULL;
	gssapi_encrypt_info_t gssapi_encrypt;

	/*
	 * Check to see whether this looks like an RPC call or reply.
	 */
	if (!tvb_bytes_exist(tvb, offset, 8)) {
		/* Captured data in packet isn't enough to let us tell. */
		return false;
	}

	/* both directions need at least this */
	msg_type = tvb_get_ntohl(tvb, offset + 4);

	switch (msg_type) {

	case RPC_CALL:
		/* Check for RPC call. */
		rpc_prog = looks_like_rpc_call(tvb, offset);
		if (rpc_prog == NULL)
			return false;
		rpc_call = NULL;
		break;

	case RPC_REPLY:
		/* Check for RPC reply. */
		rpc_call = looks_like_rpc_reply(tvb, pinfo, offset);
		if (rpc_call == NULL)
			return false;
		rpc_prog = NULL;
		break;

	default:
		/* The putative message type field contains neither
		   RPC_CALL nor RPC_REPLY, so it's not an RPC call or
		   reply. */
		return false;
	}

	if (tp_type != RPC_UDP) {
		/*
		 * This is RPC-over-TCP; check if this is the last
		 * fragment.
		 */
		if (!(rpc_rm & RPC_RM_LASTFRAG)) {
			/*
			 * This isn't the last fragment.
			 * If we're doing reassembly, just return
			 * true to indicate that this looks like
			 * the beginning of an RPC message,
			 * and let them do fragment reassembly.
			 */
			if (can_defragment)
				return true;
		}
	}

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

	rpc_item = proto_tree_add_item(tree, proto_rpc, tvb, 0, -1,
		    ENC_NA);
	rpc_tree = proto_item_add_subtree(rpc_item, ett_rpc);

	if (tp_type != RPC_UDP) {
		show_rpc_fraginfo(tvb, frag_tvb, rpc_tree, rpc_rm,
		    ipfd_head, pinfo);
	}

	xid      = tvb_get_ntohl(tvb, offset);
	proto_tree_add_item(rpc_tree,hf_rpc_xid, tvb,
			offset, 4, ENC_BIG_ENDIAN);

	msg_type_name = val_to_str(msg_type,rpc_msg_type,"%u");
	if (rpc_tree) {
		proto_tree_add_uint(rpc_tree, hf_rpc_msgtype, tvb,
			offset+4, 4, msg_type);
		proto_item_append_text(rpc_item, ", Type:%s XID:0x%08x", msg_type_name, xid);
	}

	offset += 8;

	switch (msg_type) {

	case RPC_CALL:
		proto = rpc_prog->proto;
		proto_id = rpc_prog->proto_id;
		ett = rpc_prog->ett;
		progname = rpc_prog->progname;

		rpcvers = tvb_get_ntohl(tvb, offset);
		if (rpc_tree) {
			proto_tree_add_uint(rpc_tree,
				hf_rpc_version, tvb, offset, 4, rpcvers);
		}

		prog = tvb_get_ntohl(tvb, offset + 4);

		if (rpc_tree) {
			proto_tree_add_uint_format_value(rpc_tree,
				hf_rpc_program, tvb, offset+4, 4, prog,
				"%s (%u)", progname, prog);
		}

		/* Set the protocol name to the underlying
		   program name. */
		col_set_str(pinfo->cinfo, COL_PROTOCOL, progname);

		vers = tvb_get_ntohl(tvb, offset+8);
		if (rpc_tree) {
			proto_tree_add_uint(rpc_tree,
				hf_rpc_programversion, tvb, offset+8, 4, vers);
		}

		proc = tvb_get_ntohl(tvb, offset+12);

		key.prog = prog;
		key.vers = vers;
		key.proc = proc;

		if ((dissect_function = dissector_get_custom_table_handle(subdissector_call_table, &key)) != NULL) {
			procname = dissector_handle_get_description(dissect_function);
		}
		else {
			/* happens only with unknown program or version
			 * numbers
			 */
			dissect_function = data_handle;
			procname=wmem_strdup_printf(wmem_packet_scope(), "proc-%u", proc);
		}

		/* Check for RPCSEC_GSS and AUTH_GSSAPI */
		if (tvb_bytes_exist(tvb, offset+16, 4)) {
			cred_flavor = tvb_get_ntohl(tvb, offset+16);
			switch (cred_flavor) {

			case RPCSEC_GSS:
				/*
				 * It's GSS-API authentication...
				 */
				if (tvb_bytes_exist(tvb, offset+28, 8)) {
					/*
					 * ...and we have the procedure
					 * and service information for it.
					 */
					flavor = FLAVOR_GSSAPI;
					gss_proc = tvb_get_ntohl(tvb, offset+28);
					gss_svc = tvb_get_ntohl(tvb, offset+36);
				} else {
					/*
					 * ...but the procedure and service
					 * information isn't available.
					 */
					flavor = FLAVOR_GSSAPI_NO_INFO;
				}
				break;

			case AUTH_GSSAPI:
				/*
				 * AUTH_GSSAPI flavor.  If auth_msg is true,
				 * then this is an AUTH_GSSAPI message and
				 * not an application level message.
				 */
				if (tvb_bytes_exist(tvb, offset+28, 4)) {
					if (tvb_get_ntohl(tvb, offset+28)) {
						flavor = FLAVOR_AUTHGSSAPI_MSG;
						gss_proc = proc;
						procname = val_to_str(gss_proc,
						    rpc_authgssapi_proc, "Unknown (%d)");
					} else {
						flavor = FLAVOR_AUTHGSSAPI;
					}
				}
				break;

			default:
				/*
				 * It's not GSS-API authentication.
				 */
				flavor = FLAVOR_NOT_GSSAPI;
				break;
			}
		}

		proto_tree_add_uint_format_value(rpc_tree,
				hf_rpc_procedure, tvb, offset+12, 4, proc,
				"%s (%u)", procname, proc);

		/* Print the program version, procedure name, and message type (call or reply). */
		if (first_pdu)
			col_clear(pinfo->cinfo, COL_INFO);
		else
			col_append_str(pinfo->cinfo, COL_INFO, "  ; ");
		/* Special case for NFSv4 - if the type is COMPOUND, do not print the procedure name */
		if (vers==4 && prog==NFS_PROGRAM && !strcmp(procname, "COMPOUND"))
			col_append_fstr(pinfo->cinfo, COL_INFO,"V%u %s", vers,
					msg_type_name);
		else
			col_append_fstr(pinfo->cinfo, COL_INFO,"V%u %s %s",
					vers, procname, msg_type_name);

		/*
		 * Establish a conversation for the call, for use when
		 * matching calls with replies.
		 */
		conversation = get_conversation_for_call(pinfo);

		/*
		 * Do we already have a state structure for this conv
		 */
		rpc_conv_info = (rpc_conv_info_t *)conversation_get_proto_data(conversation, proto_rpc);
		if (!rpc_conv_info) {
			/* No.  Attach that information to the conversation, and add
			 * it to the list of information structures.
			 */
			rpc_conv_info = wmem_new(wmem_file_scope(), rpc_conv_info_t);
			rpc_conv_info->xids=wmem_tree_new(wmem_file_scope());
			conversation_add_proto_data(conversation, proto_rpc, rpc_conv_info);
		}


		/* Make the dissector for this conversation the non-heuristic
		   RPC dissector. */
		if (tp_type != RPC_TLS && cred_flavor != AUTH_TLS) {
			conversation_set_dissector(conversation,
				(pinfo->ptype == PT_TCP) ? rpc_tcp_handle : rpc_handle);
		}

		/* look up the request */
		rpc_call = (rpc_call_info_value *)wmem_tree_lookup32(rpc_conv_info->xids, xid);
		if (rpc_call) {
			/* We've seen a request with this XID, with the same
			   source and destination, before - but was it
			   *this* request? */
			if (pinfo->num != rpc_call->req_num) {
				/* No, so it's a duplicate request.
				   Mark it as such. */
				col_prepend_fstr(pinfo->cinfo, COL_INFO,
						 "[RPC retransmission of #%u]",
						 rpc_call->req_num);
				proto_tree_add_item(rpc_tree, hf_rpc_dup, tvb,
						    0, 0, ENC_NA);
				proto_tree_add_uint(rpc_tree, hf_rpc_call_dup,
						    tvb, 0,0, rpc_call->req_num);
			}
			if(rpc_call->rep_num){
				col_append_fstr(pinfo->cinfo, COL_INFO," (Reply In %u)", rpc_call->rep_num);
			}
		} else {
			/* Prepare the value data.
			   "req_num" and "rep_num" are frame numbers;
			   frame numbers are 1-origin, so we use 0
			   to mean "we don't yet know in which frame
			   the reply for this call appears". */
			rpc_call = wmem_new(wmem_file_scope(), rpc_call_info_value);
			rpc_call->req_num = pinfo->num;
			rpc_call->rep_num = 0;
			rpc_call->prog = prog;
			rpc_call->vers = vers;
			rpc_call->proc = proc;
			rpc_call->private_data = NULL;
			rpc_call->xid = xid;
			rpc_call->flavor = flavor;
			rpc_call->gss_proc = gss_proc;
			rpc_call->gss_svc = gss_svc;
			rpc_call->req_time = pinfo->abs_ts;

			/* store it */
			wmem_tree_insert32(rpc_conv_info->xids, xid, (void *)rpc_call);
		}

		if(rpc_call->rep_num){
			proto_item *tmp_item;

			tmp_item=proto_tree_add_uint_format(rpc_tree, hf_rpc_reqframe,
			    tvb, 0, 0, rpc_call->rep_num,
			    "The reply to this request is in frame %u",
			    rpc_call->rep_num);
			proto_item_set_generated(tmp_item);
		}

		offset += 16;

		offset = dissect_rpc_cred(tvb, rpc_tree, offset, pinfo, rpc_conv_info);
		offset = dissect_rpc_verf(tvb, rpc_tree, offset, msg_type, pinfo);

		/* pass rpc_info to subdissectors */
		rpc_call->request=true;

		/* go to the next dissector */

		break;	/* end of RPC call */

	case RPC_REPLY:
		/* we know already the type from the calling routine,
		   and we already have "rpc_call" set above. */
		key.prog = prog = rpc_call->prog;
		key.vers = vers = rpc_call->vers;
		key.proc = proc = rpc_call->proc;
		flavor = rpc_call->flavor;
		gss_proc = rpc_call->gss_proc;
		gss_svc = rpc_call->gss_svc;

		dissect_function = dissector_get_custom_table_handle(subdissector_reply_table, &key);
		if (dissect_function != NULL) {
			procname = dissector_handle_get_description(dissect_function);
		}
		else {
			/* happens only with unknown program or version
			 * numbers
			 */
			dissect_function = data_handle;
			procname=wmem_strdup_printf(wmem_packet_scope(), "proc-%u", rpc_call->proc);
		}

		/*
		 * If this is an AUTH_GSSAPI message, then the RPC procedure
		 * is not an application procedure, but rather an auth level
		 * procedure, so it would be misleading to print the RPC
		 * procname.  Replace the RPC procname with the corresponding
		 * AUTH_GSSAPI procname.
		 */
		if (flavor == FLAVOR_AUTHGSSAPI_MSG) {
			procname = val_to_str_const(gss_proc, rpc_authgssapi_proc, "(null)");
		}

		rpc_prog_key = prog;
		if ((rpc_prog = (rpc_prog_info_value *)g_hash_table_lookup(rpc_progs,GUINT_TO_POINTER(rpc_prog_key))) == NULL) {
			proto = NULL;
			proto_id = proto_rpc_unknown;
			ett = 0;
			progname = "Unknown";
		}
		else {
			proto = rpc_prog->proto;
			proto_id = rpc_prog->proto_id;
			ett = rpc_prog->ett;
			progname = rpc_prog->progname;

			/* Set the protocol name to the underlying
			   program name. */
			col_set_str(pinfo->cinfo, COL_PROTOCOL, progname);
		}

		/* Print the program version, procedure name, and message type (call or reply). */
		if (first_pdu)
			col_clear(pinfo->cinfo, COL_INFO);
		else
			col_append_str(pinfo->cinfo, COL_INFO, "  ; ");
		/* Special case for NFSv4 - if the type is COMPOUND, do not print the procedure name */
		if (vers==4 && prog==NFS_PROGRAM && !strcmp(procname, "COMPOUND"))
			col_append_fstr(pinfo->cinfo, COL_INFO,"V%u %s",
					vers, msg_type_name);
		else
			col_append_fstr(pinfo->cinfo, COL_INFO,"V%u %s %s",
					vers, procname, msg_type_name);

		if (rpc_tree) {
			proto_item *tmp_item;
			tmp_item=proto_tree_add_uint_format_value(rpc_tree,
				hf_rpc_program, tvb, 0, 0, prog,
				"%s (%u)", progname, prog);
			proto_item_set_generated(tmp_item);
			tmp_item=proto_tree_add_uint(rpc_tree,
				hf_rpc_programversion, tvb, 0, 0, vers);
			proto_item_set_generated(tmp_item);
			tmp_item=proto_tree_add_uint_format_value(rpc_tree,
				hf_rpc_procedure, tvb, 0, 0, proc,
				"%s (%u)", procname, proc);
			proto_item_set_generated(tmp_item);
		}

		reply_state = tvb_get_ntohl(tvb,offset);
		proto_tree_add_uint(rpc_tree, hf_rpc_state_reply, tvb,
				offset, 4, reply_state);
		offset += 4;

		/* Indicate the frame to which this is a reply. */
		if (rpc_call->req_num) {
			proto_item *tmp_item;

			tmp_item=proto_tree_add_uint_format(rpc_tree, hf_rpc_repframe,
			    tvb, 0, 0, rpc_call->req_num,
			    "This is a reply to a request in frame %u",
			    rpc_call->req_num);
			proto_item_set_generated(tmp_item);

			nstime_delta(&ns, &pinfo->abs_ts, &rpc_call->req_time);
			tmp_item=proto_tree_add_time(rpc_tree, hf_rpc_time, tvb, offset, 0,
				&ns);
			proto_item_set_generated(tmp_item);

			col_append_fstr(pinfo->cinfo, COL_INFO," (Call In %u)", rpc_call->req_num);
		}


		if (rpc_call->rep_num == 0) {
			/* We have not yet seen a reply to that call, so
			   this must be the first reply; remember its
			   frame number. */
			rpc_call->rep_num = pinfo->num;
		} else {
			/* We have seen a reply to this call - but was it
			   *this* reply? */
			if (rpc_call->rep_num != pinfo->num) {
				proto_item *tmp_item;

				/* No, so it's a duplicate reply.
				   Mark it as such. */
				col_prepend_fstr(pinfo->cinfo, COL_INFO,
						"[RPC duplicate of #%u]", rpc_call->rep_num);
				tmp_item=proto_tree_add_item(rpc_tree,
					hf_rpc_dup, tvb, 0,0, ENC_NA);
				proto_item_set_generated(tmp_item);

				tmp_item=proto_tree_add_uint(rpc_tree,
					hf_rpc_reply_dup, tvb, 0,0, rpc_call->rep_num);
				proto_item_set_generated(tmp_item);
			}
		}

		switch (reply_state) {

		case MSG_ACCEPTED:
			offset = dissect_rpc_verf(tvb, rpc_tree, offset, msg_type, pinfo);
			accept_state = tvb_get_ntohl(tvb,offset);
			if (rpc_tree) {
				proto_tree_add_uint(rpc_tree, hf_rpc_state_accept, tvb,
					offset, 4, accept_state);
			}
			offset += 4;
			switch (accept_state) {

			case SUCCESS:
				/* go to the next dissector */
				break;

			case PROG_MISMATCH:
				vers_low = tvb_get_ntohl(tvb,offset);
				vers_high = tvb_get_ntohl(tvb,offset+4);
				if (rpc_tree) {
					proto_tree_add_uint(rpc_tree,
						hf_rpc_programversion_min,
						tvb, offset, 4, vers_low);
					proto_tree_add_uint(rpc_tree,
						hf_rpc_programversion_max,
						tvb, offset+4, 4, vers_high);
				}
				offset += 8;

				/*
				 * There's no protocol reply, so don't
				 * try to dissect it.
				 */
				dissect_rpc_flag = false;
				break;

			default:
				/*
				 * There's no protocol reply, so don't
				 * try to dissect it.
				 */
				dissect_rpc_flag = false;
				break;
			}
			break;

		case MSG_DENIED:
			reject_state = tvb_get_ntohl(tvb,offset);
			if (rpc_tree) {
				proto_tree_add_uint(rpc_tree,
					hf_rpc_state_reject, tvb, offset, 4,
					reject_state);
			}
			offset += 4;

			if (reject_state==RPC_MISMATCH) {
				vers_low = tvb_get_ntohl(tvb,offset);
				vers_high = tvb_get_ntohl(tvb,offset+4);
				if (rpc_tree) {
					proto_tree_add_uint(rpc_tree,
						hf_rpc_version_min,
						tvb, offset, 4, vers_low);
					proto_tree_add_uint(rpc_tree,
						hf_rpc_version_max,
						tvb, offset+4, 4, vers_high);
				}
				offset += 8;
			} else if (reject_state==AUTH_ERROR) {
				auth_state = tvb_get_ntohl(tvb,offset);
				if (rpc_tree) {
					proto_tree_add_uint(rpc_tree,
						hf_rpc_state_auth, tvb, offset, 4,
						auth_state);
				}
				offset += 4;
			}

			/*
			 * There's no protocol reply, so don't
			 * try to dissect it.
			 */
			dissect_rpc_flag = false;
			break;

		default:
			/*
			 * This isn't a valid reply state, so we have
			 * no clue what's going on; don't try to dissect
			 * the protocol reply.
			 */
			dissect_rpc_flag = false;
			break;
		}
		break; /* end of RPC reply */

	default:
		/*
		 * The switch statement at the top returned if
		 * this was neither an RPC call nor a reply.
		 */
		DISSECTOR_ASSERT_NOT_REACHED();
	}

	/* now we know, that RPC was shorter */
	if (rpc_item) {
		if (offset < 0)
			THROW(ReportedBoundsError);
		tvb_ensure_bytes_exist(tvb, offset, 0);
		proto_item_set_end(rpc_item, tvb, offset);
	}

	if (!dissect_rpc_flag) {
		/*
		 * There's no RPC call or reply here; just dissect
		 * whatever's left as data.
		 */
		call_dissector(data_handle,
		    tvb_new_subset_remaining(tvb, offset), pinfo, rpc_tree);
		return true;
	}

	/* we must queue this packet to the tap system before we actually
	   call the subdissectors since short packets (i.e. nfs read reply)
	   will cause an exception and execution would never reach the call
	   to tap_queue_packet() in that case
	*/
	tap_queue_packet(rpc_tap, pinfo, rpc_call);


	/* If this is encrypted data we have to try to decrypt the data first before we
	 * we create a tree.
	 * the reason for this is because if we can decrypt the data we must create the
	 * item/tree for the next protocol using the decrypted tvb and not the current
	 * tvb.
	 */
	memset(&gssapi_encrypt, 0, sizeof(gssapi_encrypt));
	gssapi_encrypt.decrypt_gssapi_tvb=DECRYPT_GSSAPI_NORMAL;

	if (flavor == FLAVOR_GSSAPI && gss_proc == RPCSEC_GSS_DATA && gss_svc == RPCSEC_GSS_SVC_PRIVACY) {
		proto_tree *gss_tree;

		gss_tree = proto_tree_add_subtree(tree, tvb, offset, -1, ett_gss_wrap, NULL, "GSS-Wrap");

		offset = dissect_rpc_authgss_priv_data(tvb, gss_tree, offset, pinfo, &gssapi_encrypt);
		if (gssapi_encrypt.gssapi_decrypted_tvb) {
			proto_tree_add_item(gss_tree, hf_rpc_authgss_seq, gssapi_encrypt.gssapi_decrypted_tvb, 0, 4, ENC_BIG_ENDIAN);

			/* Switcheroo to the new tvb that contains the decrypted payload */
			tvb = gssapi_encrypt.gssapi_decrypted_tvb;
			offset = 4;
		}
	}


	/* create here the program specific sub-tree */
	if (tree && (flavor != FLAVOR_AUTHGSSAPI_MSG)) {
		proto_item *tmp_item;

		pitem = proto_tree_add_item(tree, proto_id, tvb, offset, tvb_reported_length_remaining(tvb, offset), ENC_NA);
		ptree = proto_item_add_subtree(pitem, ett);

		tmp_item=proto_tree_add_uint(ptree,
				hf_rpc_programversion, tvb, 0, 0, vers);
		proto_item_set_generated(tmp_item);
		if (rpc_prog && rpc_prog->procedure_hfs && (rpc_prog->procedure_hfs->len > vers) )
			procedure_hf = g_array_index(rpc_prog->procedure_hfs, int, vers);
		else {
			/*
			 * No such element in the GArray.
			 */
			procedure_hf = 0;
		}
		if (procedure_hf != 0 && procedure_hf != -1) {
			tmp_item=proto_tree_add_uint(ptree,
				procedure_hf, tvb, 0, 0, proc);
			proto_item_set_generated(tmp_item);
		} else {
			tmp_item=proto_tree_add_uint_format_value(ptree,
				hf_rpc_procedure, tvb, 0, 0, proc,
				"%s (%u)", procname, proc);
			proto_item_set_generated(tmp_item);
		}
	}

	/* proto==0 if this is an unknown program */
	if( (proto==0) || !proto_is_protocol_enabled(proto)){
		dissect_function = data_handle;
	}

	/*
	 * Don't call any subdissector if we have no more date to dissect.
	 */
	if (tvb_reported_length_remaining(tvb, offset) == 0) {
		return true;
	}

	/*
	 * Handle RPCSEC_GSS and AUTH_GSSAPI specially.
	 */
	switch (flavor) {

	case FLAVOR_UNKNOWN:
		/*
		 * We don't know the authentication flavor, so we can't
		 * dissect the payload.
		 */
		proto_tree_add_expert_format(ptree, pinfo, &ei_rpc_cannot_dissect, tvb, offset, -1,
		    "Unknown authentication flavor - cannot dissect");
		return true;

	case FLAVOR_NOT_GSSAPI:
		/*
		 * It's not GSS-API authentication.  Just dissect the
		 * payload.
		 */
		offset = call_dissect_function(tvb, pinfo, ptree, offset,
				dissect_function, progname, rpc_call);
		break;

	case FLAVOR_GSSAPI_NO_INFO:
		/*
		 * It's GSS-API authentication, but we don't have the
		 * procedure and service information, so we can't dissect
		 * the payload.
		 */
		proto_tree_add_expert_format(ptree, pinfo, &ei_rpc_cannot_dissect, tvb, offset, -1,
		    "GSS-API authentication, but procedure and service unknown - cannot dissect");
		return true;

	case FLAVOR_GSSAPI:
		/*
		 * It's GSS-API authentication, and we have the procedure
		 * and service information; process the GSS-API stuff,
		 * and process the payload if there is any.
		 */
		switch (gss_proc) {

		case RPCSEC_GSS_INIT:
		case RPCSEC_GSS_CONTINUE_INIT:
			if (msg_type == RPC_CALL) {
				offset = dissect_rpc_authgss_initarg(tvb,
					ptree, offset, pinfo);
			}
			else {
				offset = dissect_rpc_authgss_initres(tvb,
					ptree, offset, pinfo, rpc_conv_info);
			}
			break;

		case RPCSEC_GSS_DATA:
			if (gss_svc == RPCSEC_GSS_SVC_NONE) {
				offset = call_dissect_function(tvb,
						pinfo, ptree, offset,
						dissect_function,
						progname, rpc_call);
			}
			else if (gss_svc == RPCSEC_GSS_SVC_INTEGRITY) {
				offset = dissect_rpc_authgss_integ_data(tvb,
						pinfo, ptree, offset,
						dissect_function,
						progname, rpc_call);
			}
			else if (gss_svc == RPCSEC_GSS_SVC_PRIVACY) {
				if (gssapi_encrypt.gssapi_decrypted_tvb) {
					call_dissect_function(
						gssapi_encrypt.gssapi_decrypted_tvb,
						pinfo, ptree, 4,
						dissect_function,
						progname, rpc_call);
					offset = tvb_reported_length(gssapi_encrypt.gssapi_decrypted_tvb);
				}
			}
			break;

		default:
			break;
		}
		break;

	case FLAVOR_AUTHGSSAPI_MSG:
		/*
		 * This is an AUTH_GSSAPI message.  It contains data
		 * only for the authentication procedure and not for the
		 * application level RPC procedure.  Reset the column
		 * protocol and info fields to indicate that this is
		 * an RPC auth level message, then process the args.
		 */
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "RPC");
		col_add_fstr(pinfo->cinfo, COL_INFO,
				"%s %s XID 0x%x",
				val_to_str(gss_proc, rpc_authgssapi_proc, "Unknown (%d)"),
				msg_type_name, xid);

		switch (gss_proc) {

		case AUTH_GSSAPI_INIT:
		case AUTH_GSSAPI_CONTINUE_INIT:
		case AUTH_GSSAPI_MSG:
			if (msg_type == RPC_CALL) {
			    offset = dissect_rpc_authgssapi_initarg(tvb,
				rpc_tree, offset, pinfo);
			} else {
			    offset = dissect_rpc_authgssapi_initres(tvb,
				rpc_tree, offset, pinfo);
			}
			break;

		case AUTH_GSSAPI_DESTROY:
			offset = dissect_rpc_data(tvb, rpc_tree,
			    hf_rpc_authgss_data, offset);
			break;

		case AUTH_GSSAPI_EXIT:
			break;
		}

		/* Adjust the length to account for the auth message. */
		if (rpc_item) {
			proto_item_set_end(rpc_item, tvb, offset);
		}
		break;

	case FLAVOR_AUTHGSSAPI:
		/*
		 * An RPC with AUTH_GSSAPI authentication.  The data
		 * portion is always private, so don't call the dissector.
		 */
		offset = dissect_auth_gssapi_data(tvb, ptree, offset);
		break;
	}

	if (tvb_reported_length_remaining(tvb, offset) > 0) {
		/*
		 * dissect any remaining bytes (incomplete dissection) as pure
		 * data in the ptree
		 */

		call_dissector(data_handle,
			       tvb_new_subset_remaining(tvb, offset), pinfo, ptree);
	}

	/* XXX this should really loop over all fhandles registered for the frame */
	if(nfs_fhandle_reqrep_matching){
		switch (msg_type) {
		case RPC_CALL:
			if(rpc_call && rpc_call->rep_num){
				dissect_fhandle_hidden(pinfo,
						ptree, rpc_call->rep_num);
			}
			break;
		case RPC_REPLY:
			if(rpc_call && rpc_call->req_num){
				dissect_fhandle_hidden(pinfo,
						ptree, rpc_call->req_num);
			}
			break;
		}
	}

	return true;
}

static bool
dissect_rpc_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
	return (bool)dissect_rpc_message(tvb, pinfo, tree, NULL, NULL, RPC_UDP, 0,
	    true, false);
}

static int
dissect_rpc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
	if (!dissect_rpc_message(tvb, pinfo, tree, NULL, NULL, RPC_UDP, 0,
	    true, false)) {
		if (tvb_reported_length(tvb) != 0)
			dissect_rpc_continuation(tvb, pinfo, tree);
	}
	return tvb_captured_length(tvb);
}


/* Defragmentation of RPC-over-TCP records */
/* table to hold defragmented RPC records */
static reassembly_table rpc_fragment_table;

/*
 * XXX - can we eliminate this by defining our own key-handling functions
 * for rpc_fragment_table?  (Note that those functions must look at
 * not only the addresses of the endpoints, but the ports of the endpoints,
 * so that they don't try to combine fragments from different TCP
 * connections.)
 */
static wmem_map_t *rpc_reassembly_table;

typedef struct _rpc_fragment_key {
	uint32_t conv_id;
	uint32_t seq;
	uint32_t offset;
	uint32_t port;
	/* xxx */
	uint32_t start_seq;
} rpc_fragment_key;

static unsigned
rpc_fragment_hash(const void *k)
{
	const rpc_fragment_key *key = (const rpc_fragment_key *)k;

	return key->conv_id + key->seq;
}

static int
rpc_fragment_equal(const void *k1, const void *k2)
{
	const rpc_fragment_key *key1 = (const rpc_fragment_key *)k1;
	const rpc_fragment_key *key2 = (const rpc_fragment_key *)k2;

	return key1->conv_id == key2->conv_id &&
	    key1->seq == key2->seq && key1->port == key2->port;
}

static void
show_rpc_fragheader(tvbuff_t *tvb, proto_tree *tree, uint32_t rpc_rm)
{
	proto_tree *hdr_tree;
	uint32_t fraglen;

	if (tree) {
		fraglen = rpc_rm & RPC_RM_FRAGLEN;

		hdr_tree = proto_tree_add_subtree_format(tree, tvb, 0, 4,
		    ett_rpc_fraghdr, NULL, "Fragment header: %s%u %s",
		    (rpc_rm & RPC_RM_LASTFRAG) ? "Last fragment, " : "",
		    fraglen, plurality(fraglen, "byte", "bytes"));

		proto_tree_add_boolean(hdr_tree, hf_rpc_lastfrag, tvb, 0, 4,
		    rpc_rm);
		proto_tree_add_uint(hdr_tree, hf_rpc_fraglen, tvb, 0, 4,
		    rpc_rm);
	}
}

static void
show_rpc_fragment(tvbuff_t *tvb, proto_tree *tree, uint32_t rpc_rm)
{
	if (tree) {
		/*
		 * Show the fragment header and the data for the fragment.
		 */
		show_rpc_fragheader(tvb, tree, rpc_rm);
		proto_tree_add_item(tree, hf_rpc_fragment_data, tvb, 4, -1, ENC_NA);
	}
}

static void
make_frag_tree(tvbuff_t *tvb, proto_tree *tree, int proto, int ett,
	       uint32_t rpc_rm)
{
	proto_item *frag_item;
	proto_tree *frag_tree;

	if (tree == NULL)
		return;		/* nothing to do */

	frag_item = proto_tree_add_protocol_format(tree, proto, tvb, 0, -1,
	    "%s Fragment", proto_get_protocol_name(proto));
	frag_tree = proto_item_add_subtree(frag_item, ett);
	show_rpc_fragment(tvb, frag_tree, rpc_rm);
}

void
show_rpc_fraginfo(tvbuff_t *tvb, tvbuff_t *frag_tvb, proto_tree *tree,
		  uint32_t rpc_rm, fragment_head *ipfd_head, packet_info *pinfo)
{
	proto_item *frag_tree_item;

	if (tree == NULL)
		return;		/* don't do any work */

	if (tvb != frag_tvb) {
		/*
		 * This message was not all in one fragment,
		 * so show the fragment header *and* the data
		 * for the fragment (which is the last fragment),
		 * and a tree with information about all fragments.
		 */
		show_rpc_fragment(frag_tvb, tree, rpc_rm);

		/*
		 * Show a tree with information about all fragments.
		 */
		show_fragment_tree(ipfd_head, &rpc_frag_items, tree, pinfo, tvb, &frag_tree_item);
	} else {
		/*
		 * This message was all in one fragment, so just show
		 * the fragment header.
		 */
		show_rpc_fragheader(tvb, tree, rpc_rm);
	}
}

static bool
call_message_dissector(tvbuff_t *tvb, tvbuff_t *rec_tvb, packet_info *pinfo,
		       proto_tree *tree, tvbuff_t *frag_tvb, rec_dissector_t dissector,
		       fragment_head *ipfd_head, int tp_type, uint32_t rpc_rm,
		       bool first_pdu, bool can_defragment)
{
	const char *saved_proto;
	volatile bool rpc_succeeded;

	saved_proto = pinfo->current_proto;
	rpc_succeeded = false;
	TRY {
		rpc_succeeded = (*dissector)(rec_tvb, pinfo, tree,
		    frag_tvb, ipfd_head, tp_type, rpc_rm, first_pdu,
		    can_defragment);
	}
	CATCH_NONFATAL_ERRORS {
		/*
		 * Somebody threw an exception that means that there
		 * was a problem dissecting the payload; that means
		 * that a dissector was found, so we don't need to
		 * dissect the payload as data or update the protocol
		 * or info columns.
		 *
		 * Just show the exception and then continue dissecting.
		 */
		show_exception(tvb, pinfo, tree, EXCEPT_CODE, GET_MESSAGE);
		pinfo->current_proto = saved_proto;

		/*
		 * We treat this as a "successful" dissection of
		 * an RPC packet, as "dissect_rpc_message()"
		 * *did* decide it was an RPC packet, throwing
		 * an exception while dissecting it as such.
		 */
		rpc_succeeded = true;
	}
	ENDTRY;
	return rpc_succeeded;
}

static int
dissect_rpc_fragment(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree,
		     rec_dissector_t dissector, bool is_heur, int proto, int ett,
		     bool first_pdu, struct tcpinfo *tcpinfo, struct tlsinfo *tlsinfo)
{
	uint32_t seq;
	uint32_t rpc_rm;
	uint32_t len;
	int tp_type;
	int tvb_len, tvb_reported_len;
	tvbuff_t *frag_tvb;
	conversation_t *conversation = NULL;
	bool rpc_succeeded;
	bool save_fragmented;
	bool can_defragment;
	rpc_fragment_key old_rfk, *rfk, *new_rfk;
	fragment_head *ipfd_head;
	tvbuff_t *rec_tvb;

	if (pinfo == NULL || (tcpinfo == NULL && tlsinfo == NULL)) {
		return 0;
	}

	seq = (tcpinfo != NULL ? tcpinfo->seq : tlsinfo->seq) + offset;
	tp_type = (tcpinfo != NULL ? RPC_TCP : RPC_TLS);

	/*
	 * Get the record mark.
	 */
	if (!tvb_bytes_exist(tvb, offset, 4)) {
		/*
		 * XXX - we should somehow arrange to handle
		 * a record mark split across TCP segments.
		 */
		return 0;	/* not enough to tell if it's valid */
	}
	rpc_rm = tvb_get_ntohl(tvb, offset);

	len = rpc_rm & RPC_RM_FRAGLEN;

	/*
	 * Do TCP desegmentation, if enabled.
	 *
	 * reject fragments bigger than this preference setting.
	 * This is arbitrary, but should at least prevent
	 * some crashes from either packets with really
	 * large RPC-over-TCP fragments or from stuff that's
	 * not really valid for this protocol.
	 */
	if (len > max_rpc_tcp_pdu_size)
		return 0;	/* pretend it's not valid */

	can_defragment = rpc_defragment;
	len += 4;	/* include record mark */
	tvb_reported_len = tvb_reported_length_remaining(tvb, offset);

	if ((int)len > tvb_reported_len) {
		/* This frame doesn't have all the data for this message.
		 *
		 * If this is a heuristic dissector, check whether it looks
		 * like the beginning of an RPC call or reply.
		 */
		if (is_heur) {
			uint32_t msg_type;

			if (!tvb_bytes_exist(tvb, offset + 4, 8)) {
				/*
				 * Captured data in packet isn't
				 * enough to let us tell.
				 */
				return 0;
			}

			/* both directions need at least this */
			msg_type = tvb_get_ntohl(tvb, offset + 4 + 4);

			switch (msg_type) {

			case RPC_CALL:
				/* Check for RPC call. */
				if (looks_like_rpc_call(tvb,
				    offset + 4) == NULL) {
					/* Doesn't look like a call. */
					return 0;
				}
				break;

			case RPC_REPLY:
				/* Check for RPC reply. */
				if (looks_like_rpc_reply(tvb, pinfo,
				    offset + 4) == NULL) {
					/* Doesn't look like a reply. */
					return 0;
				}
				break;

			default:
				/* The putative message type field
				   contains neither RPC_CALL nor
				   RPC_REPLY, so it's not an RPC
				   call or reply. */
				return 0;
			}

			/* Get this conversation, creating it if
			   it doesn't already exist, and make the
			   dissector for it the non-heuristic RPC
			   dissector for RPC-over-TCP. */
			conversation = find_or_create_conversation(pinfo);
			if (tcpinfo != NULL) {
				conversation_set_dissector(conversation, rpc_tcp_handle);
			}
		}

		/* OK, we think it is RPC. Can we desegment? */
		if (rpc_desegment && pinfo->can_desegment) {
			/* Yes, so tell the TCP dissector how much
			 * more data we need. */

			pinfo->desegment_offset = offset;
			pinfo->desegment_len = len - tvb_reported_len;
			return -((int32_t) pinfo->desegment_len);
		} else {
			/* No. */
			expert_add_info(pinfo, NULL, &ei_rpc_segment_needed);
			can_defragment = false;
		}
	} else if (tvb_reported_len > (int)len) {
		tvb_reported_len = len;
	}

	tvb_len = tvb_captured_length_remaining(tvb, offset);
	if (tvb_len < (int)len) {
		/*  We don't have all the data for this fragment. */
		can_defragment = false;
	}if (tvb_len > (int)len) {
		tvb_len = len;
	}

	frag_tvb = tvb_new_subset_length_caplen(tvb, offset, tvb_len,
	    tvb_reported_len);

	/*
	 * If we're not defragmenting, just hand this to the
	 * disssector.
	 *
	 * We defragment only if we should (rpc_defragment true) *and* we
	 * can (tvb_len <= len, so that we have all the data in the fragment).
	 */
	if (!can_defragment) {
		/*
		 * We can't defragment.  Hand the dissector the tvbuff for the
		 * fragment as the tvbuff for the record.
		 */
		rec_tvb = frag_tvb;
		ipfd_head = NULL;

		/*
		 * Mark this as fragmented, so if somebody throws an
		 * exception, we don't report it as a malformed frame.
		 */
		save_fragmented = pinfo->fragmented;
		pinfo->fragmented = true;
		rpc_succeeded = call_message_dissector(tvb, rec_tvb, pinfo,
		    tree, frag_tvb, dissector, ipfd_head, tp_type, rpc_rm,
		    first_pdu, can_defragment);
		pinfo->fragmented = save_fragmented;
		if (!rpc_succeeded)
			return 0;	/* not RPC */
		return len;
	}

	/*
	 * First, we check to see if this fragment is part of a record
	 * that we're in the process of defragmenting.
	 *
	 * The key is the conversation ID for the conversation to which
	 * the packet belongs and the current sequence number.
	 *
	 * We must first find the conversation and, if we don't find
	 * one, create it.
	 */
	conversation = find_or_create_conversation(pinfo);
	old_rfk.conv_id = conversation->conv_index;
	old_rfk.seq = seq;
	old_rfk.port = pinfo->srcport;
	rfk = (rpc_fragment_key *)wmem_map_lookup(rpc_reassembly_table, &old_rfk);

	if (rfk == NULL) {
		/*
		 * This fragment was not found in our table, so it doesn't
		 * contain a continuation of a higher-level PDU.
		 * Is it the last fragment?
		 */
		if (!(rpc_rm & RPC_RM_LASTFRAG)) {
			/*
			 * This isn't the last fragment, so we don't
			 * have the complete record.
			 *
			 * It's the first fragment we've seen, so if
			 * it's truly the first fragment of the record,
			 * and it has enough data, the dissector can at
			 * least check whether it looks like a valid
			 * message, as it contains the start of the
			 * message.
			 *
			 * The dissector should not dissect anything
			 * if the "last fragment" flag isn't set in
			 * the record marker, so it shouldn't throw
			 * an exception.
			 */
			if (!(*dissector)(frag_tvb, pinfo, tree, frag_tvb,
			    NULL, tp_type, rpc_rm, first_pdu, can_defragment))
				return 0;	/* not valid */

			/*
			 * OK, now start defragmentation with that
			 * fragment.  Add this fragment, and set up
			 * next packet/sequence number as well.
			 *
			 * We must remember this fragment.
			 */

			rfk = wmem_new(wmem_file_scope(), rpc_fragment_key);
			rfk->conv_id = conversation->conv_index;
			rfk->seq = seq;
			rfk->port = pinfo->srcport;
			rfk->offset = 0;
			rfk->start_seq = seq;
			wmem_map_insert(rpc_reassembly_table, rfk, rfk);

			/*
			 * Start defragmentation.
			 */
			ipfd_head = fragment_add_multiple_ok(&rpc_fragment_table,
			    tvb, offset + 4,
			    pinfo, rfk->start_seq, NULL,
			    rfk->offset, len - 4, true);

			/*
			 * Make sure that defragmentation isn't complete;
			 * it shouldn't be, as this is the first fragment
			 * we've seen, and the "last fragment" bit wasn't
			 * set on it.
			 */
			if (ipfd_head == NULL) {
				new_rfk = wmem_new(wmem_file_scope(), rpc_fragment_key);
				new_rfk->conv_id = rfk->conv_id;
				new_rfk->seq = seq + len;
				new_rfk->port = pinfo->srcport;
				new_rfk->offset = rfk->offset + len - 4;
				new_rfk->start_seq = rfk->start_seq;
				wmem_map_insert(rpc_reassembly_table, new_rfk,
					new_rfk);

				/*
				 * This is part of a fragmented record,
				 * but it's not the first part.
				 * Show it as a record marker plus data, under
				 * a top-level tree for this protocol.
				 */
				col_set_str(pinfo->cinfo, COL_PROTOCOL, "RPC");
				col_set_str(pinfo->cinfo, COL_INFO, "Fragment");
				make_frag_tree(frag_tvb, tree, proto, ett,rpc_rm);

				/*
				 * No more processing need be done, as we don't
				 * have a complete record.
				 */
				return len;
			} else {
				/* oddly, we have a first fragment, not marked as last,
				 * but which the defragmenter thinks is complete.
				 * So rather than creating a fragment reassembly tree,
				 * we simply throw away the partial fragment structure
				 * and fall though to our "sole fragment" processing below.
				 */
			}
		}

		/*
		 * This is the first fragment we've seen, and it's also
		 * the last fragment; that means the record wasn't
		 * fragmented.  Hand the dissector the tvbuff for the
		 * fragment as the tvbuff for the record.
		 */
		rec_tvb = frag_tvb;
		ipfd_head = NULL;
	} else {
		/*
		 * OK, this fragment was found, which means it continues
		 * a record.  This means we must defragment it.
		 * Add it to the defragmentation lists.
		 */
		ipfd_head = fragment_add_multiple_ok(&rpc_fragment_table,
		    tvb, offset + 4, pinfo, rfk->start_seq, NULL,
		    rfk->offset, len - 4, !(rpc_rm & RPC_RM_LASTFRAG));

		if (ipfd_head == NULL) {
			/*
			 * fragment_add_multiple_ok() returned NULL.
			 * This means that defragmentation is not
			 * completed yet.
			 *
			 * We must add an entry to the hash table with
			 * the sequence number following this fragment
			 * as the starting sequence number, so that when
			 * we see that fragment we'll find that entry.
			 *
			 * XXX - as TCP stream data is not currently
			 * guaranteed to be provided in order to dissectors,
			 * RPC fragments aren't guaranteed to be provided
			 * in order, either.
			 */
			new_rfk = wmem_new(wmem_file_scope(), rpc_fragment_key);
			new_rfk->conv_id = rfk->conv_id;
			new_rfk->seq = seq + len;
			new_rfk->port = pinfo->srcport;
			new_rfk->offset = rfk->offset + len - 4;
			new_rfk->start_seq = rfk->start_seq;
			wmem_map_insert(rpc_reassembly_table, new_rfk,
			    new_rfk);

			/*
			 * This is part of a fragmented record,
			 * but it's not the first part.
			 * Show it as a record marker plus data, under
			 * a top-level tree for this protocol,
			 * but don't hand it to the dissector
			 */
			col_set_str(pinfo->cinfo, COL_PROTOCOL, "RPC");
			col_set_str(pinfo->cinfo, COL_INFO, "Fragment");
			make_frag_tree(frag_tvb, tree, proto, ett, rpc_rm);

			/*
			 * No more processing need be done, as we don't
			 * have a complete record.
			 */
			return len;
		}

		/*
		 * It's completely defragmented.
		 *
		 * We only call subdissector for the last fragment.
		 * XXX - this assumes in-order delivery of RPC
		 * fragments, which requires in-order delivery of TCP
		 * segments.
		 */
		if (!(rpc_rm & RPC_RM_LASTFRAG)) {
			/*
			 * Well, it's defragmented, but this isn't
			 * the last fragment; this probably means
			 * this isn't the first pass, so we don't
			 * need to start defragmentation.
			 *
			 * This is part of a fragmented record,
			 * but it's not the first part.
			 * Show it as a record marker plus data, under
			 * a top-level tree for this protocol,
			 * but don't show it to the dissector.
			 */
			col_set_str(pinfo->cinfo, COL_PROTOCOL, "RPC");
			col_set_str(pinfo->cinfo, COL_INFO, "Fragment");
			make_frag_tree(frag_tvb, tree, proto, ett, rpc_rm);

			/*
			 * No more processing need be done, as we
			 * only disssect the data with the last
			 * fragment.
			 */
			return len;
		}

		/*
		 * OK, this is the last segment.
		 * Create a tvbuff for the defragmented
		 * record.
		 */

		/*
		 * Create a new TVB structure for
		 * defragmented data.
		 */
		rec_tvb = tvb_new_chain(tvb, ipfd_head->tvb_data);

		/*
		 * Add defragmented data to the data source list.
		 */
		add_new_data_source(pinfo, rec_tvb, "Defragmented");
	}

	/*
	 * We have something to hand to the RPC message
	 * dissector.
	 */
	if (!call_message_dissector(tvb, rec_tvb, pinfo, tree,
	    frag_tvb, dissector, ipfd_head, tp_type, rpc_rm,
	    first_pdu, can_defragment))
		return 0;	/* not RPC */
	return len;
}  /* end of dissect_rpc_fragment() */

/**
 * Scans tvb, starting at given offset, to see if we can find
 * what looks like a valid RPC-over-TCP reply header.
 *
 * @param tvb Buffer to inspect for RPC reply header.
 * @param offset Offset to begin search of tvb at.
 *
 * @return -1 if no reply header found, else offset to start of header
 *         (i.e., to the RPC record mark field).
 */

static int
find_rpc_over_tcp_reply_start(tvbuff_t *tvb, int offset)
{

	/*
	 * Looking for partial header sequence.  From beginning of
	 * stream-style header, including "record mark", full ONC-RPC
	 * looks like:
	 *    BE int32    record mark (rfc 1831 sec. 10)
	 *    ?  int32    XID (rfc 1831 sec. 8)
	 *    BE int32    msg_type (ibid sec. 8, call = 0, reply = 1)
	 *
	 * -------------------------------------------------------------
	 * Then reply-specific fields are
	 *    BE int32    reply_stat (ibid, accept = 0, deny = 1)
	 *
	 * Then, assuming accepted,
	 *   opaque_auth
	 *    BE int32    auth_flavor (ibid, none = 0)
	 *    BE int32    ? auth_len (ibid, none = 0)
	 *
	 *    BE int32    accept_stat (ibid, success = 0, errs are 1..5 in rpc v2)
	 *
	 * -------------------------------------------------------------
	 * Or, call-specific fields are
	 *    BE int32    rpc_vers (rfc 1831 sec 8, always == 2)
	 *    BE int32    prog (NFS == 000186A3)
	 *    BE int32    prog_ver (NFS v2/3 == 2 or 3)
	 *    BE int32    proc_id (NFS, <= 256 ???)
	 *   opaque_auth
	 *    ...
	 */

	/* Initially, we search only for something matching the template
	 * of a successful reply with no auth verifier.
	 * Our first qualification test is search for a string of zero bytes,
	 * corresponding the four uint32_t values
	 *    reply_stat
	 *    auth_flavor
	 *    auth_len
	 *    accept_stat
	 *
	 * If this string of zeros matches, then we go back and check the
	 * preceding msg_type and record_mark fields.
	 */

	const int      cbZeroTail = 4 * 4;     /* four uint32_t of zeros */
	const int      ibPatternStart = 3 * 4;    /* offset of zero fill from reply start */
	const uint8_t * pbWholeBuf;    /* all of tvb, from offset onwards */
	const int      NoMatch = -1;

	int      ibSearchStart;       /* offset of search start, in case of false hits. */

	const    uint8_t * pbBuf;

	int      cbInBuf;       /* bytes in tvb, from offset onwards */

	uint32_t ulMsgType;
	uint32_t ulRecMark;

	int      i;


	cbInBuf = tvb_reported_length_remaining(tvb, offset);

	/* start search at first possible location */
	ibSearchStart = ibPatternStart;

	if (cbInBuf < (cbZeroTail + ibSearchStart)) {
		/* nothing to search, so claim no RPC */
		return (NoMatch);
	}

	pbWholeBuf = tvb_get_ptr(tvb, offset, cbInBuf);
	if (pbWholeBuf == NULL) {
		/* probably never take this, as get_ptr seems to assert */
		return (NoMatch);
	}

	while ((cbInBuf - ibSearchStart) > cbZeroTail) {
		/* First test for long tail of zeros, starting at the back.
		 * A failure lets us skip the maximum possible buffer amount.
		 */
		pbBuf = pbWholeBuf + ibSearchStart + cbZeroTail - 1;
		for (i = cbZeroTail; i > 0;  i --)
			{
			if (*pbBuf != 0)
				{
				/* match failure.  Since we need N contiguous zeros,
				 * we can increment next match start so zero testing
				 * begins right after this failure spot.
				 */
				ibSearchStart += i;
				pbBuf = NULL;
				break;
				}

			pbBuf --;
			}

		if (pbBuf == NULL) {
			continue;
		}

		/* got a match in zero-fill region, verify reply ID and
		 * record mark fields */
		ulMsgType = pntoh32 (pbWholeBuf + ibSearchStart - 4);
		ulRecMark = pntoh32 (pbWholeBuf + ibSearchStart - ibPatternStart);

		if ((ulMsgType == RPC_REPLY) &&
			 ((ulRecMark & ~0x80000000) <= (unsigned) max_rpc_tcp_pdu_size)) {
			/* looks ok, try dissect */
			return (offset + ibSearchStart - ibPatternStart);
		}

		/* no match yet, nor egregious miss either.  Inch along to next try */
		ibSearchStart ++;
	}

	return (NoMatch);

}  /* end of find_rpc_over_tcp_reply_start() */

/**
 * Scans tvb for what looks like a valid RPC call / reply header.
 * If found, calls standard dissect_rpc_fragment() logic to digest
 * the (hopefully valid) fragment.
 *
 * With any luck, one invocation of this will be sufficient to get
 * us back in alignment with the stream, and no further calls to
 * this routine will be needed for a given conversation.  As if.  :-)
 *
 * Can return:
 *       Same as dissect_rpc_fragment().  Will return zero (no frame)
 *       if no valid RPC header is found.
 */

static int
find_and_dissect_rpc_fragment(tvbuff_t *tvb, int offset, packet_info *pinfo,
			      proto_tree *tree, rec_dissector_t dissector,
			      bool is_heur, int proto, int ett,
			      struct tcpinfo *tcpinfo, struct tlsinfo *tlsinfo)
{

	int   offReply;
	int   len;

	offReply = find_rpc_over_tcp_reply_start(tvb, offset);
	if (offReply < 0) {
		/* could search for request, but not needed (or testable) thus far */
		return 0;    /* claim no RPC */
	}

	len = dissect_rpc_fragment(tvb, offReply,
				   pinfo, tree,
				   dissector, is_heur, proto, ett,
				   true /* force first-pdu state */, tcpinfo, tlsinfo);

	/* misses are reported as-is */
	if (len == 0)
	{
		return 0;
	}

	/* returning a non-zero length, correct it to reflect the extra offset
	 * we found necessary
	 */
	if (len > 0) {
		len += offReply - offset;
	}
	else {
		/* negative length seems to only be used as a flag,
		 * don't mess it up until found necessary
		 */
/*      len -= offReply - offset; */
	}

	return (len);

}  /* end of find_and_dissect_rpc_fragment */


/*
 * Returns true if it looks like ONC RPC, false otherwise.
 */
static bool
dissect_rpc_tcp_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
		       bool is_heur, struct tcpinfo *tcpinfo, struct tlsinfo *tlsinfo)
{
	int offset = 0;
	bool saw_rpc = false;
	bool first_pdu = true;
	int len;

	while (tvb_reported_length_remaining(tvb, offset) != 0) {
		/*
		 * Process this fragment.
		 */
		len = dissect_rpc_fragment(tvb, offset, pinfo, tree,
		    dissect_rpc_message, is_heur, proto_rpc, ett_rpc,
		    first_pdu, tcpinfo, tlsinfo);

		if ((len == 0) && first_pdu && rpc_find_fragment_start) {
			/*
			 * Try discarding some leading bytes from tvb, on assumption
			 * that we are looking at the middle of a stream-based transfer
			 */
			len = find_and_dissect_rpc_fragment(tvb, offset, pinfo, tree,
				 dissect_rpc_message, is_heur, proto_rpc, ett_rpc,
				 tcpinfo, tlsinfo);
		}

		first_pdu = false;
		if (len < 0) {
			/*
			 * dissect_rpc_fragment() thinks this is ONC RPC,
			 * but we need more data from the TCP stream for
			 * this fragment.
			 */
			return true;
		}
		if (len == 0) {
			/*
			 * It's not RPC.  Stop processing.
			 */
			break;
		}

		/*  Set fence so whatever the subdissector put in the
		 *  Info column stays there.
		 *
		 *  This is useful when some ONC RPC protocol is
		 *  carrying another protocol that can also run atop
		 *  other protocols, so that the other protocol's
		 *  dissector has to clear the Info column to add
		 *  its own material, and there are multiple PDUs
		 *  in one frame.  If the fence isn't set, the Info
		 *  column will only reflect the information from
		 *  the first PDU in the frame.
		 */
		col_set_fence(pinfo->cinfo, COL_INFO);

		/* PDU tracking
		  If the length indicates that the PDU continues beyond
		  the end of this tvb, then tell TCP about it so that it
		  knows where the next PDU starts.
		  This is to help TCP detect when PDUs are not aligned to
		  segment boundaries and allow it to find RPC headers
		  that starts in the middle of a TCP segment.
		*/
		if(!pinfo->fd->visited){
			if(len>tvb_reported_length_remaining(tvb, offset)){
				pinfo->want_pdu_tracking=2;
				pinfo->bytes_until_next_pdu=len-tvb_reported_length_remaining(tvb, offset);
			}
		}
		offset += len;
		saw_rpc = true;
	}
	return saw_rpc;
}

static bool
dissect_rpc_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
	struct tcpinfo* tcpinfo = (struct tcpinfo *)data;

	return (bool)dissect_rpc_tcp_common(tvb, pinfo, tree, true, tcpinfo, NULL);
}

static int
dissect_rpc_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
{
	struct tcpinfo* tcpinfo = (struct tcpinfo *)data;

	if (!dissect_rpc_tcp_common(tvb, pinfo, tree, false, tcpinfo, NULL))
		dissect_rpc_continuation(tvb, pinfo, tree);

	return tvb_reported_length(tvb);
}

static bool
dissect_rpc_tls_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
	struct tlsinfo *tlsinfo = (struct tlsinfo *)data;

	if (dissect_rpc_tcp_common(tvb, pinfo, tree, true, NULL, tlsinfo)) {
		if (tlsinfo != NULL) {
			*(tlsinfo->app_handle) = rpc_tls_handle;
		}
		return true;
	}
	return false;
}

static int
dissect_rpc_tls(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
{
	struct tlsinfo* tlsinfo = (struct tlsinfo *)data;

	col_set_writable(pinfo->cinfo, COL_PROTOCOL, true);
	if (dissect_rpc_tcp_common(tvb, pinfo, tree, false, NULL, tlsinfo)) {
		col_set_writable(pinfo->cinfo, COL_PROTOCOL, false);
		col_set_writable(pinfo->cinfo, COL_INFO, false);
	} else {
		dissect_rpc_continuation(tvb, pinfo, tree);
	}

	return tvb_reported_length(tvb);
}

/* Tap statistics */
typedef enum
{
	PROGRAM_NAME_COLUMN,
	PROGRAM_NUM_COLUMN,
	VERSION_COLUMN,
	CALLS_COLUMN,
	MIN_SRT_COLUMN,
	MAX_SRT_COLUMN,
	AVG_SRT_COLUMN
} rpc_prog_stat_columns;

static stat_tap_table_item rpc_prog_stat_fields[] = {
	{TABLE_ITEM_STRING, TAP_ALIGN_LEFT, "Program", "%-25s"},
	{TABLE_ITEM_UINT, TAP_ALIGN_RIGHT, "Program Num", "%u"},
	{TABLE_ITEM_UINT, TAP_ALIGN_RIGHT, "Version", "%u"},
	{TABLE_ITEM_UINT, TAP_ALIGN_RIGHT, "Calls", "%u"},
	{TABLE_ITEM_FLOAT, TAP_ALIGN_RIGHT, "Min SRT (s)", "%.2f"},
	{TABLE_ITEM_FLOAT, TAP_ALIGN_RIGHT, "Max SRT (s)", "%.2f"},
	{TABLE_ITEM_FLOAT, TAP_ALIGN_RIGHT, "Avg SRT (s)", "%.2f"}
};

static void rpc_prog_stat_init(stat_tap_table_ui* new_stat)
{
	const char *table_name = "ONC-RPC Program Statistics";
	int num_fields = array_length(rpc_prog_stat_fields);
	stat_tap_table *table;

	table = stat_tap_find_table(new_stat, table_name);
	if (table) {
		if (new_stat->stat_tap_reset_table_cb) {
			new_stat->stat_tap_reset_table_cb(table);
		}
		return;
	}

	table = stat_tap_init_table(table_name, num_fields, 0, NULL);
	stat_tap_add_table(new_stat, table);
}

static tap_packet_status
rpc_prog_stat_packet(void *tapdata, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *rciv_ptr, tap_flags_t flags _U_)
{
	stat_data_t* stat_data = (stat_data_t*)tapdata;
	const rpc_call_info_value *ri = (const rpc_call_info_value *)rciv_ptr;
	int num_fields = array_length(rpc_prog_stat_fields);
	nstime_t delta;
	double delta_s = 0.0;
	unsigned call_count;
	unsigned element;
	bool found = false;
	stat_tap_table* table;
	stat_tap_table_item_type* item_data;

	table = g_array_index(stat_data->stat_tap_data->tables, stat_tap_table*, 0);

	for (element = 0; element < table->num_elements; element++)
	{
		stat_tap_table_item_type *program_data, *version_data;
		program_data = stat_tap_get_field_data(table, element, PROGRAM_NUM_COLUMN);
		version_data = stat_tap_get_field_data(table, element, VERSION_COLUMN);

		if ((ri->prog == program_data->value.uint_value) && (ri->vers == version_data->value.uint_value)) {
			found = true;
			break;
		}
	}

	if (!found) {
		/* Add a new row */
		stat_tap_table_item_type items[array_length(rpc_prog_stat_fields)];
		memset(items, 0, sizeof(items));

		items[PROGRAM_NAME_COLUMN].type = TABLE_ITEM_STRING;
		items[PROGRAM_NAME_COLUMN].value.string_value = g_strdup(rpc_prog_name(ri->prog));
		items[PROGRAM_NUM_COLUMN].type = TABLE_ITEM_UINT;
		items[PROGRAM_NUM_COLUMN].value.uint_value = ri->prog;
		items[VERSION_COLUMN].type = TABLE_ITEM_UINT;
		items[VERSION_COLUMN].value.uint_value = ri->vers;
		items[CALLS_COLUMN].type = TABLE_ITEM_UINT;
		items[MIN_SRT_COLUMN].type = TABLE_ITEM_FLOAT;
		items[MAX_SRT_COLUMN].type = TABLE_ITEM_FLOAT;
		items[AVG_SRT_COLUMN].type = TABLE_ITEM_FLOAT;

		stat_tap_init_table_row(table, element, num_fields, items);
	}

	/* we are only interested in reply packets */
	if (ri->request) {
		return TAP_PACKET_DONT_REDRAW;
	}

	item_data = stat_tap_get_field_data(table, element, CALLS_COLUMN);
	item_data->value.uint_value++;
	call_count = item_data->value.uint_value;
	stat_tap_set_field_data(table, element, CALLS_COLUMN, item_data);

	/* calculate time delta between request and reply */
	nstime_delta(&delta, &pinfo->abs_ts, &ri->req_time);
	delta_s = nstime_to_sec(&delta);

	item_data = stat_tap_get_field_data(table, element, MIN_SRT_COLUMN);
	if (item_data->value.float_value == 0.0 || delta_s < item_data->value.float_value) {
		item_data->value.float_value = delta_s;
		stat_tap_set_field_data(table, element, MIN_SRT_COLUMN, item_data);
	}

	item_data = stat_tap_get_field_data(table, element, MAX_SRT_COLUMN);
	if (item_data->value.float_value == 0.0 || delta_s > item_data->value.float_value) {
		item_data->value.float_value = delta_s;
		stat_tap_set_field_data(table, element, MAX_SRT_COLUMN, item_data);
	}

	item_data = stat_tap_get_field_data(table, element, AVG_SRT_COLUMN);
	item_data->user_data.float_value += delta_s;
	item_data->value.float_value = item_data->user_data.float_value / call_count;
	stat_tap_set_field_data(table, element, AVG_SRT_COLUMN, item_data);

	return TAP_PACKET_REDRAW;
}

static void
rpc_prog_stat_reset(stat_tap_table* table)
{
	unsigned element;
	stat_tap_table_item_type* item_data;

	for (element = 0; element < table->num_elements; element++)
	{
		item_data = stat_tap_get_field_data(table, element, CALLS_COLUMN);
		item_data->value.uint_value = 0;
		stat_tap_set_field_data(table, element, CALLS_COLUMN, item_data);
		item_data = stat_tap_get_field_data(table, element, MIN_SRT_COLUMN);
		item_data->value.float_value = 0.0;
		stat_tap_set_field_data(table, element, MIN_SRT_COLUMN, item_data);
		item_data = stat_tap_get_field_data(table, element, MAX_SRT_COLUMN);
		item_data->value.float_value = 0.0;
		stat_tap_set_field_data(table, element, MAX_SRT_COLUMN, item_data);
		item_data = stat_tap_get_field_data(table, element, AVG_SRT_COLUMN);
		item_data->value.float_value = 0.0;
		stat_tap_set_field_data(table, element, AVG_SRT_COLUMN, item_data);
	}
}

static void
rpc_prog_stat_free_table_item(stat_tap_table* table _U_, unsigned row _U_, unsigned column, stat_tap_table_item_type* field_data)
{
	if (column != PROGRAM_NAME_COLUMN) return;
	g_free((char*)field_data->value.string_value);
}

static void
rpc_shutdown(void)
{
	g_hash_table_destroy(rpc_progs);
}

/* will be called once from register.c at startup time */
void
proto_register_rpc(void)
{
	static hf_register_info hf[] = {
		{ &hf_rpc_reqframe, {
			"Request Frame", "rpc.reqframe", FT_FRAMENUM, BASE_NONE,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_repframe, {
			"Reply Frame", "rpc.repframe", FT_FRAMENUM, BASE_NONE,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_lastfrag, {
			"Last Fragment", "rpc.lastfrag", FT_BOOLEAN, 32,
			TFS(&tfs_yes_no), RPC_RM_LASTFRAG, NULL, HFILL }},
		{ &hf_rpc_fraglen, {
			"Fragment Length", "rpc.fraglen", FT_UINT32, BASE_DEC,
			NULL, RPC_RM_FRAGLEN, NULL, HFILL }},
		{ &hf_rpc_xid, {
			"XID", "rpc.xid", FT_UINT32, BASE_HEX_DEC,
			NULL, 0, "Transaction identifier", HFILL }},
		{ &hf_rpc_msgtype, {
			"Message Type", "rpc.msgtyp", FT_UINT32, BASE_DEC,
			VALS(rpc_msg_type), 0, NULL, HFILL }},
		{ &hf_rpc_state_reply, {
			"Reply State", "rpc.replystat", FT_UINT32, BASE_DEC,
			VALS(rpc_reply_state), 0, NULL, HFILL }},
		{ &hf_rpc_state_accept, {
			"Accept State", "rpc.state_accept", FT_UINT32, BASE_DEC,
			VALS(rpc_accept_state), 0, NULL, HFILL }},
		{ &hf_rpc_state_reject, {
			"Reject State", "rpc.state_reject", FT_UINT32, BASE_DEC,
			VALS(rpc_reject_state), 0, NULL, HFILL }},
		{ &hf_rpc_state_auth, {
			"Auth State", "rpc.state_auth", FT_UINT32, BASE_DEC,
			VALS(rpc_auth_state), 0, NULL, HFILL }},
		{ &hf_rpc_version, {
			"RPC Version", "rpc.version", FT_UINT32, BASE_DEC,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_version_min, {
			"RPC Version (Minimum)", "rpc.version.min", FT_UINT32,
			BASE_DEC, NULL, 0, "Program Version (Minimum)", HFILL }},
		{ &hf_rpc_version_max, {
			"RPC Version (Maximum)", "rpc.version.max", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_program, {
			"Program", "rpc.program", FT_UINT32, BASE_DEC,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_programversion, {
			"Program Version", "rpc.programversion", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_programversion_min, {
			"Program Version (Minimum)", "rpc.programversion.min", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_programversion_max, {
			"Program Version (Maximum)", "rpc.programversion.max", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_procedure, {
			"Procedure", "rpc.procedure", FT_UINT32, BASE_DEC,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_auth_flavor, {
			"Flavor", "rpc.auth.flavor", FT_UINT32, BASE_DEC,
			VALS(rpc_auth_flavor), 0, NULL, HFILL }},
		{ &hf_rpc_auth_length, {
			"Length", "rpc.auth.length", FT_UINT32, BASE_DEC,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_auth_stamp, {
			"Stamp", "rpc.auth.stamp", FT_UINT32, BASE_HEX,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_auth_lk_owner, {
			"Lock Owner", "rpc.auth.lk_owner", FT_BYTES, BASE_NONE,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_auth_pid, {
			"PID", "rpc.auth.pid", FT_UINT32, BASE_DEC,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_auth_uid, {
			"UID", "rpc.auth.uid", FT_UINT32, BASE_DEC,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_auth_gid, {
			"GID", "rpc.auth.gid", FT_UINT32, BASE_DEC,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_auth_flags, {
			"FLAGS", "rpc.auth.flags", FT_UINT32, BASE_HEX,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_auth_ctime, {
			"CTIME", "rpc.auth.ctime", FT_ABSOLUTE_TIME,
			ABSOLUTE_TIME_LOCAL, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_v, {
			"GSS Version", "rpc.authgss.version", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_proc, {
			"GSS Procedure", "rpc.authgss.procedure", FT_UINT32,
			BASE_DEC, VALS(rpc_authgss_proc), 0, NULL, HFILL }},
		{ &hf_rpc_authgss_seq, {
			"GSS Sequence Number", "rpc.authgss.seqnum", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_svc, {
			"GSS Service", "rpc.authgss.service", FT_UINT32,
			BASE_DEC, VALS(rpc_authgss_svc), 0, NULL, HFILL }},
		{ &hf_rpc_authgss_ctx, {
			"GSS Context", "rpc.authgss.context", FT_BYTES,
			BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_ctx_create_frame, {
			"Created in frame", "rpc.authgss.context.created_frame", FT_FRAMENUM,
			BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_ctx_destroy_frame, {
			"Destroyed in frame", "rpc.authgss.context.destroyed_frame", FT_FRAMENUM,
			BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_ctx_len, {
			"GSS Context Length", "rpc.authgss.context.length", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_major, {
			"GSS Major Status", "rpc.authgss.major", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_minor, {
			"GSS Minor Status", "rpc.authgss.minor", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_window, {
			"GSS Sequence Window", "rpc.authgss.window", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_token_length, {
			"GSS Token Length", "rpc.authgss.token_length", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_data_length, {
			"Length", "rpc.authgss.data.length", FT_UINT32,
			BASE_DEC, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_data, {
			"GSS Data", "rpc.authgss.data", FT_BYTES,
			BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_checksum, {
			"GSS Checksum", "rpc.authgss.checksum", FT_BYTES,
			BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgss_token, {
			"GSS Token", "rpc.authgss.token", FT_BYTES,
			BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgssapi_v, {
			"AUTH_GSSAPI Version", "rpc.authgssapi.version",
			FT_UINT32, BASE_DEC, NULL, 0, NULL,
			HFILL }},
		{ &hf_rpc_authgssapi_msg, {
			"AUTH_GSSAPI Message", "rpc.authgssapi.message",
			FT_BOOLEAN, BASE_NONE, TFS(&tfs_yes_no), 0x0, NULL,
			HFILL }},
		{ &hf_rpc_authgssapi_msgv, {
			"Msg Version", "rpc.authgssapi.msgversion",
			FT_UINT32, BASE_DEC, NULL, 0, NULL,
			HFILL }},
		{ &hf_rpc_authgssapi_handle, {
			"Client Handle", "rpc.authgssapi.handle",
			FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authgssapi_isn, {
			"Signed ISN", "rpc.authgssapi.isn",
			FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authdes_namekind, {
			"Namekind", "rpc.authdes.namekind", FT_UINT32, BASE_DEC,
			VALS(rpc_authdes_namekind), 0, NULL, HFILL }},
		{ &hf_rpc_authdes_netname, {
			"Netname", "rpc.authdes.netname", FT_STRING,
			BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authdes_convkey, {
			"Conversation Key (encrypted)", "rpc.authdes.convkey", FT_UINT64,
			BASE_HEX, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authdes_window, {
			"Window (encrypted)", "rpc.authdes.window", FT_UINT32,
			BASE_HEX, NULL, 0, "Windows (encrypted)", HFILL }},
		{ &hf_rpc_authdes_nickname, {
			"Nickname", "rpc.authdes.nickname", FT_UINT32,
			BASE_HEX, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authdes_timestamp, {
			"Timestamp (encrypted)", "rpc.authdes.timestamp", FT_UINT64,
			BASE_HEX, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authdes_windowverf, {
			"Window verifier (encrypted)", "rpc.authdes.windowverf", FT_UINT32,
			BASE_HEX, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_authdes_timeverf, {
			"Timestamp verifier (encrypted)", "rpc.authdes.timeverf", FT_UINT64,
			BASE_HEX, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_auth_machinename, {
			"Machine Name", "rpc.auth.machinename", FT_STRING,
			BASE_NONE, NULL, 0, NULL, HFILL }},
		{ &hf_rpc_dup, {
			"Duplicate Call/Reply", "rpc.dup", FT_NONE, BASE_NONE,
			NULL, 0, NULL, HFILL }},
		{ &hf_rpc_call_dup, {
			"Duplicate to the call in", "rpc.call.dup", FT_FRAMENUM, BASE_NONE,
			NULL, 0, "This is a duplicate to the call in frame", HFILL }},
		{ &hf_rpc_reply_dup, {
			"Duplicate to the reply in", "rpc.reply.dup", FT_FRAMENUM, BASE_NONE,
			NULL, 0, "This is a duplicate to the reply in frame", HFILL }},
		{ &hf_rpc_value_follows, {
			"Value Follows", "rpc.value_follows", FT_BOOLEAN, BASE_NONE,
			TFS(&tfs_yes_no), 0x0, NULL, HFILL }},
		{ &hf_rpc_array_len, {
			"num", "rpc.array.len", FT_UINT32, BASE_DEC,
			NULL, 0, "Length of RPC array", HFILL }},

          /* Generated from convert_proto_tree_add_text.pl */
          { &hf_rpc_opaque_length, { "length", "rpc.opaque_length", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
          { &hf_rpc_fill_bytes, { "fill bytes", "rpc.fill_bytes", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
          { &hf_rpc_no_values, { "no values", "rpc.array_no_values", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
          { &hf_rpc_opaque_data, { "opaque data", "rpc.opaque_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
          { &hf_rpc_argument_length, { "Argument length", "rpc.argument_length", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
          { &hf_rpc_continuation_data, { "Continuation data", "rpc.continuation_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
          { &hf_rpc_fragment_data, { "Fragment Data", "rpc.fragment_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},

		{ &hf_rpc_time, {
			"Time from request", "rpc.time", FT_RELATIVE_TIME, BASE_NONE,
			NULL, 0, "Time between Request and Reply for ONC-RPC calls", HFILL }},

		{ &hf_rpc_fragment_overlap,
		{ "Fragment overlap",	"rpc.fragment.overlap", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
			"Fragment overlaps with other fragments", HFILL }},

		{ &hf_rpc_fragment_overlap_conflict,
		{ "Conflicting data in fragment overlap",	"rpc.fragment.overlap.conflict", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
			"Overlapping fragments contained conflicting data", HFILL }},

		{ &hf_rpc_fragment_multiple_tails,
		{ "Multiple tail fragments found",	"rpc.fragment.multipletails", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
			"Several tails were found when defragmenting the packet", HFILL }},

		{ &hf_rpc_fragment_too_long_fragment,
		{ "Fragment too long",	"rpc.fragment.toolongfragment", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
			"Fragment contained data past end of packet", HFILL }},

		{ &hf_rpc_fragment_error,
		{ "Defragmentation error", "rpc.fragment.error", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
			"Defragmentation error due to illegal fragments", HFILL }},

		{ &hf_rpc_fragment_count,
		{ "Fragment count", "rpc.fragment.count", FT_UINT32, BASE_DEC, NULL, 0x0,
			NULL, HFILL }},

		{ &hf_rpc_fragment,
		{ "RPC Fragment", "rpc.fragment", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
			NULL, HFILL }},

		{ &hf_rpc_fragments,
		{ "RPC Fragments", "rpc.fragments", FT_NONE, BASE_NONE, NULL, 0x0,
			NULL, HFILL }},

		{ &hf_rpc_reassembled_length,
		{ "Reassembled RPC length", "rpc.reassembled.length", FT_UINT32, BASE_DEC, NULL, 0x0,
			"The total length of the reassembled payload", HFILL }},

		{ &hf_rpc_unknown_body,
		{ "Unknown RPC call/reply body", "rpc.unknown_body", FT_NONE, BASE_NONE, NULL, 0x0,
			NULL, HFILL }},
	};
	static int *ett[] = {
		&ett_rpc,
		&ett_rpc_fragments,
		&ett_rpc_fragment,
		&ett_rpc_fraghdr,
		&ett_rpc_string,
		&ett_rpc_cred,
		&ett_rpc_verf,
		&ett_rpc_gids,
		&ett_rpc_gss_token,
		&ett_rpc_gss_data,
		&ett_rpc_array,
		&ett_rpc_authgssapi_msg,
		&ett_rpc_unknown_program,
		&ett_gss_context,
		&ett_gss_wrap,
	};
	static ei_register_info ei[] = {
		{ &ei_rpc_cannot_dissect, { "rpc.cannot_dissect", PI_UNDECODED, PI_WARN, "Cannot dissect", EXPFILL }},
		{ &ei_rpc_segment_needed, { "rpc.segment_needed", PI_REASSEMBLE, PI_NOTE, "Need another TCP segment but cannot desegment or not enabled (check RPC and TCP preferences)", EXPFILL }},
	};

	module_t *rpc_module;
	expert_module_t* expert_rpc;

	static tap_param rpc_prog_stat_params[] = {
		{ PARAM_FILTER, "filter", "Filter", NULL, true }
	};

	static stat_tap_table_ui rpc_prog_stat_table = {
		REGISTER_PACKET_STAT_GROUP_UNSORTED,
		"ONC-RPC Programs",
		"rpc",
		"rpc,programs",
		rpc_prog_stat_init,
		rpc_prog_stat_packet,
		rpc_prog_stat_reset,
		rpc_prog_stat_free_table_item,
		NULL,
		array_length(rpc_prog_stat_fields), rpc_prog_stat_fields,
		array_length(rpc_prog_stat_params), rpc_prog_stat_params,
		NULL,
		0
	};

	proto_rpc = proto_register_protocol("Remote Procedure Call", "RPC", "rpc");
	proto_rpc_unknown = proto_register_protocol("Unknown RPC protocol", "Unknown RPC", "rpc-unknown");

	subdissector_call_table = register_custom_dissector_table("rpc.call", "RPC Call Functions", proto_rpc, rpc_proc_hash, rpc_proc_equal, g_free);
	subdissector_reply_table = register_custom_dissector_table("rpc.reply", "RPC Reply Functions", proto_rpc, rpc_proc_hash, rpc_proc_equal, g_free);

	/* this is a dummy dissector for all those unknown rpc programs */
	proto_register_field_array(proto_rpc, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
	expert_rpc = expert_register_protocol(proto_rpc);
	expert_register_field_array(expert_rpc, ei, array_length(ei));

	rpc_reassembly_table = wmem_map_new_autoreset(wmem_epan_scope(), wmem_file_scope(), rpc_fragment_hash, rpc_fragment_equal);
	reassembly_table_register(&rpc_fragment_table,
	    &addresses_ports_reassembly_table_functions);

	rpc_module = prefs_register_protocol(proto_rpc, NULL);
	prefs_register_bool_preference(rpc_module, "desegment_rpc_over_tcp",
	    "Reassemble RPC over TCP messages spanning multiple TCP segments",
	    "Whether the RPC dissector should reassemble messages spanning multiple TCP segments."
	    " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
		&rpc_desegment);
	prefs_register_bool_preference(rpc_module, "defragment_rpc_over_tcp",
		"Reassemble fragmented RPC-over-TCP messages",
		"Whether the RPC dissector should defragment RPC-over-TCP messages.",
		&rpc_defragment);

	prefs_register_uint_preference(rpc_module, "max_tcp_pdu_size", "Maximum size of a RPC-over-TCP PDU",
		"Set the maximum size of RPCoverTCP PDUs. "
		" If the size field of the record marker is larger "
		"than this value it will not be considered a valid RPC PDU.",
		 10, &max_rpc_tcp_pdu_size);

	prefs_register_bool_preference(rpc_module, "dissect_unknown_programs",
		"Dissect unknown RPC program numbers",
		"Whether the RPC dissector should attempt to dissect RPC PDUs containing programs that are not known to Wireshark. This will make the heuristics significantly weaker and elevate the risk for falsely identifying and misdissecting packets significantly.",
		&rpc_dissect_unknown_programs);

	prefs_register_bool_preference(rpc_module, "find_fragment_start",
		"Attempt to locate start-of-fragment in partial RPC-over-TCP captures",
		"Whether the RPC dissector should attempt to locate RPC PDU boundaries when initial fragment alignment is not known.  This may cause false positives, or slow operation.",
		&rpc_find_fragment_start);

	rpc_handle = register_dissector("rpc", dissect_rpc, proto_rpc);
	rpc_tcp_handle = register_dissector("rpc-tcp", dissect_rpc_tcp, proto_rpc);
	rpc_tls_handle = register_dissector("rpc-with-tls", dissect_rpc_tls, proto_rpc);
	rpc_tap = register_tap("rpc");

	register_srt_table(proto_rpc, NULL, 1, rpcstat_packet, rpcstat_init, rpcstat_param);
	register_stat_tap_table_ui(&rpc_prog_stat_table);

	/*
	 * Init the hash tables.  Dissectors for RPC protocols must
	 * have a "handoff registration" routine that registers the
	 * protocol with RPC; they must not do it in their protocol
	 * registration routine, as their protocol registration
	 * routine might be called before this routine is called and
	 * thus might be called before the hash tables are initialized,
	 * but it's guaranteed that all protocol registration routines
	 * will be called before any handoff registration routines
	 * are called.
	 */
	rpc_progs = g_hash_table_new_full(g_direct_hash, g_direct_equal,
			NULL, rpc_prog_free_val);

	authgss_contexts=wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());

	register_shutdown_routine(rpc_shutdown);
}

void
proto_reg_handoff_rpc(void)
{
	/* tcp/udp port 111 is used by portmapper which is an onc-rpc service.
	   we register onc-rpc on this port so that we can choose RPC in
	   the list offered by DecodeAs, and so that traffic to or from
	   port 111 from or to a higher-numbered port is dissected as RPC
	   even if there's a dissector registered on the other port (it's
	   probably RPC traffic from some randomly-chosen port that happens
	   to match some port for which we have a dissector)
	*/
	dissector_add_uint_with_preference("tcp.port", RPC_TCP_PORT, rpc_tcp_handle);
	dissector_add_uint_with_preference("udp.port", RPC_TCP_PORT, rpc_handle);
	dissector_add_string("tls.alpn", "sunrpc", rpc_tls_handle);

	heur_dissector_add("tcp", dissect_rpc_tcp_heur, "RPC over TCP", "rpc_tcp", proto_rpc, HEURISTIC_ENABLE);
	heur_dissector_add("udp", dissect_rpc_heur, "RPC over UDP", "rpc_udp", proto_rpc, HEURISTIC_ENABLE);
	heur_dissector_add("tls", dissect_rpc_tls_heur, "RPC with TLS", "rpc_tls", proto_rpc, HEURISTIC_ENABLE);
	gssapi_handle = find_dissector_add_dependency("gssapi", proto_rpc);
	spnego_krb5_wrap_handle = find_dissector_add_dependency("spnego-krb5-wrap", proto_rpc);
	data_handle = find_dissector("data");
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * ex: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */