aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-cfdp.c
blob: 6a9174cc4a5ef103515f77fb8909cb62d7b345fd (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
/* packet-cfdp.c
 * Routines for CCSDS File Delivery Protocol (CFDP) dissection
 * Copyright 2013, Juan Antonio Montesinos juan.mondl@gmail.com
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Slightly updated to allow more in-depth decoding when called
 * with the 'dissect_as_subtree' method and to leverage some
 * of the bitfield display operations: Keith Scott
 * <kscott@mitre.org>.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
#include "config.h"

#include <epan/packet.h>
#include <epan/expert.h>
#include "packet-cfdp.h"

/* The CFDP standard can be found here:
 * https://public.ccsds.org/Pubs/727x0b4s.pdf
 *
 * The Store and Forward Overlay Operations are not included.
 */

void proto_register_cfdp(void);
void proto_reg_handoff_cfdp(void);

/* Initialize the protocol and registered fields */
static int proto_cfdp = -1;
static int hf_cfdp_flags = -1;
static int hf_cfdp_byte2 = -1;
static int hf_cfdp_proxy_fault_hdl_overr = -1;
static int hf_cfdp_proxy_trans_mode = -1;
static int hf_cfdp_proxy_segment_control_byte = -1;
static int hf_cfdp_proxy_put_resp = -1;
static int hf_cfdp_orig_trans_id = -1;
static int hf_cfdp_remote_stat_rep_req = -1;
static int hf_cfdp_remote_stat_rep_resp = -1;
static int hf_cfdp_finish_pdu_flags = -1;
static int hf_cfdp_remote_suspend_resume_req = -1;
static int hf_cfdp_remote_suspend_resume_resp = -1;
static int hf_cfdp_version = -1;
static int hf_cfdp_pdu_type = -1;
static int hf_cfdp_direction = -1;
static int hf_cfdp_trans_mode = -1;
static int hf_cfdp_crc_flag = -1;
static int hf_cfdp_res1 = -1;
static int hf_cfdp_data_length = -1;
static int hf_cfdp_file_data_pdu = -1;
static int hf_cfdp_res2 = -1;
static int hf_cfdp_entid_length = -1;
static int hf_cfdp_res3 = -1;
static int hf_cfdp_transeqnum_length = -1;
static int hf_cfdp_srcid = -1;
static int hf_cfdp_transeqnum = -1;
static int hf_cfdp_dstid = -1;
static int hf_cfdp_file_directive_type = -1;
static int hf_cfdp_file_data_offset = -1;
static int hf_cfdp_progress = -1;
static int hf_cfdp_dir_code_ack = -1;
static int hf_cfdp_dir_subtype_ack = -1;
static int hf_cfdp_condition_code = -1;
static int hf_cfdp_spare_one = -1;
static int hf_cfdp_spare_one_2 = -1;
static int hf_cfdp_spare_two = -1;
static int hf_cfdp_spare_four = -1;
static int hf_cfdp_spare_five = -1;
static int hf_cfdp_spare_seven = -1;
static int hf_cfdp_spare_seven_2 = -1;
static int hf_cfdp_trans_stat_ack = -1;
static int hf_cfdp_file_checksum = -1;
static int hf_cfdp_file_size = -1;
static int hf_cfdp_end_system_stat = -1;
static int hf_cfdp_delivery_code = -1;
static int hf_cfdp_file_stat = -1;
static int hf_cfdp_segment_control = -1;
static int hf_cfdp_src_file_name_len = -1;
static int hf_cfdp_src_file_name = -1;
static int hf_cfdp_dst_file_name_len = -1;
static int hf_cfdp_dst_file_name = -1;
static int hf_cfdp_first_file_name_len = -1;
static int hf_cfdp_first_file_name = -1;
static int hf_cfdp_second_file_name_len = -1;
static int hf_cfdp_second_file_name = -1;
static int hf_cfdp_nak_st_scope = -1;
static int hf_cfdp_nak_sp_scope = -1;
static int hf_cfdp_crc = -1;
static int hf_cfdp_action_code = -1;
static int hf_cfdp_status_code_1 = -1;
static int hf_cfdp_status_code_2 = -1;
static int hf_cfdp_status_code_3 = -1;
static int hf_cfdp_status_code_4 = -1;
static int hf_cfdp_status_code_5 = -1;
static int hf_cfdp_status_code_6 = -1;
static int hf_cfdp_status_code_7 = -1;
static int hf_cfdp_status_code_8 = -1;
static int hf_cfdp_handler_code = -1;
static int hf_cfdp_proxy_msg_type = -1;
static int hf_cfdp_proxy_segment_control = -1;
static int hf_cfdp_proxy_delivery_code = -1;
static int hf_cfdp_response_req = -1;
static int hf_cfdp_directory_name = -1;
static int hf_cfdp_directory_file_name = -1;
static int hf_cfdp_listing_resp_code = -1;
static int hf_cfdp_report_file_name = -1;
static int hf_cfdp_trans_stat = -1;
static int hf_cfdp_trans_stat_2 = -1;
static int hf_cfdp_rep_resp_code = -1;
static int hf_cfdp_suspension_ind = -1;
static int hf_cfdp_tlv_len = - 1;

/* Generated from convert_proto_tree_add_text.pl */
static int hf_cfdp_filestore_message_len = -1;
static int hf_cfdp_filestore_message = -1;
static int hf_cfdp_entity = -1;
static int hf_cfdp_message_to_user = -1;
static int hf_cfdp_flow_label = -1;
static int hf_cfdp_segment_requests = -1;
static int hf_cfdp_user_data = -1;

/* Initialize the subtree pointers */
static gint ett_cfdp = -1;
static gint ett_cfdp_header = -1;
static gint ett_cfdp_flags = -1;
static gint ett_cfdp_byte2 = -1;
static gint ett_cfdp_proxy_fault_hdl_overr = -1;
static gint ett_cfdp_proxy_trans_mode = -1;
static gint ett_cfdp_proxy_segment_control_byte = -1;
static gint ett_cfdp_proxy_put_resp = -1;
static gint ett_cfdp_orig_trans_id = -1;
static gint ett_cfdp_remote_stat_rep_req = -1;
static gint ett_cfdp_remote_stat_rep_resp = -1;
static gint ett_cfdp_file_directive_header = -1;
static gint ett_cfdp_file_data_header = -1;
static gint ett_cfdp_finish_pdu_flags = -1;
static gint ett_cfdp_remote_suspend_resume_req = -1;
static gint ett_cfdp_remote_suspend_resume_resp = -1;
static gint ett_cfdp_fault_location = -1;
static gint ett_cfdp_crc = -1;
static gint ett_cfdp_filestore_req = -1;
static gint ett_cfdp_filestore_resp = -1;
static gint ett_cfdp_msg_to_user = -1;
static gint ett_cfdp_fault_hdl_overr = -1;
static gint ett_cfdp_flow_label = -1;
static gint ett_cfdp_proto = -1;

static expert_field ei_cfdp_bad_length = EI_INIT;


static dissector_handle_t cfdp_handle;

/* Some parameters */
#define CFDP_HEADER_FIXED_FIELDS_LEN 4
#define CFDP_APID 2045

/* Bitmask for the first byte of the Header */
#define HDR_VERSION_CFDP    0xe0
#define HDR_TYPE_CFDP       0x10
#define HDR_DIR             0x08
#define HDR_TMODE           0x04
#define HDR_CRCF            0x02
#define HDR_RES1            0x01

/* Bitmask for the second byte of the Header */
#define HDR_RES2            0x80
#define HDR_LEN_ENT_ID      0x70
#define HDR_RES3            0x08
#define HDR_LEN_TSEQ_NUM    0x07

/* File Directive Codes */
#define EOF_PDU          4
#define FINISHED_PDU     5
#define ACK_PDU          6
#define METADATA_PDU     7
#define NAK_PDU          8
#define PROMPT_PDU       9
#define KEEP_ALIVE_PDU  12

/* TLV Types */
#define FILESTORE_REQ    0
#define FILESTORE_RESP   1
#define MSG_TO_USER      2
#define FAULT_HDL_OVERR  4
#define FLOW_LABEL       5
#define FAULT_LOCATION   6

/* ID for reserved CFDP Messages  */
#define CFDP_MSG_TO_USER 0x63666470

/* Proxy Operations Message Types */
#define PROXY_PUT_REQ           0x00
#define PROXY_MSG_TO_USER       0x01
#define PROXY_FILESTORE_REQ     0x02
#define PROXY_FAULT_HDL_OVERR   0x03
#define PROXY_TRANS_MODE        0x04
#define PROXY_FLOW_LABEL        0x05
#define PROXY_SEGMENT_CONTROL   0x06
#define PROXY_PUT_RESP          0x07
#define PROXY_FILESTORE_RESP    0x08
#define PROXY_PUT_CANCEL        0x09
#define ORIG_TRANS_ID           0x0A
#define DIRECTORY_LIST_REQ      0x10
#define DIRECTORY_LIST_RESP     0x11
#define REMOTE_STAT_REP_REQ     0x20
#define REMOTE_STAT_REP_RESP    0x21
#define REMOTE_SUSPEND_REQ      0x30
#define REMOTE_SUSPEND_RESP     0x31
#define REMOTE_RESUME_REQ       0x38
#define REMOTE_RESUME_RESP      0x39

/* PDU Type */
static const value_string cfdp_pdu_type[] = {
    { 0, "File Directive" },
    { 1, "File Data" },
    { 0, NULL }
};

/* PDU Direction */
static const value_string cfdp_direction[] = {
    { 0, "Toward file receiver" },
    { 1, "Toward file sender" },
    { 0, NULL }
};

/* Transmission mode */
static const value_string cfdp_trans_mode[] = {
    { 0, "Acknowledged" },
    { 1, "Unacknowledged" },
    { 0, NULL }
};

/* CRC */
static const value_string cfdp_crc_flag[] = {
    { 0, "CRC not present" },
    { 1, "CRC present" },
    { 0, NULL }
};

/* File Directive PDU Type */
static const value_string cfdp_file_directive_type[] = {
    {  4, "EOF PDU"},
    {  5, "Finished PDU"},
    {  6, "ACK PDU"},
    {  7, "Metadata PDU"},
    {  8, "NACK PDU"},
    {  9, "Prompt PDU"},
    { 12, "Keep Alive PDU"},
    {  0, NULL}
};

/* Condition codes */
static const value_string cfdp_condition_codes[] = {
    {  0, "No error"},
    {  1, "Positive ACK limit reached"},
    {  2, "Keep alive limit reached"},
    {  3, "Invalid transmission mode"},
    {  4, "Filestore rejection"},
    {  5, "File checksum failure"},
    {  6, "File size error"},
    {  7, "NAK limit reached"},
    {  8, "Inactivity detected"},
    {  9, "Check limit reached"},
    { 14, "Suspend.request received"},
    { 15, "Cancel.request received"},
    {  0, NULL }
};

/* Transaction status */
static const value_string cfdp_trans_stat_ack[] = {
    { 0, "Undefined" },
    { 1, "Active" },
    { 2, "Terminated" },
    { 3, "Unrecognized" },
    { 0, NULL }
};

/* End system status */
static const value_string cfdp_end_system_stat[] = {
    { 0, "Generated by Waypoint" },
    { 1, "Generated by End System" },
    { 0, NULL }
};

/* Delivery code */
static const value_string cfdp_delivery_code[] = {
    { 0, "Data Complete" },
    { 1, "Data incomplete" },
    { 0, NULL }
};

/* Filestore operations action code */
static const value_string cfdp_action_code[] = {
    { 0, "Create File" },
    { 1, "Delete File" },
    { 2, "Rename File" },
    { 3, "Append File" },
    { 4, "Replace File" },
    { 5, "Create Directory" },
    { 6, "Remove Directory" },
    { 7, "Deny File (delete if present)" },
    { 8, "Deny Directory (remove if present)" },
    { 0, NULL }
};

/* Filestore operations status codes */
static const value_string cfdp_status_code_1[] = {
    { 0, "Successful" },
    { 1, "Create not allowed" },
    { 8, "Not performed" },
    { 0, NULL }
};

static const value_string cfdp_status_code_2[] = {
    { 0, "Successful" },
    { 1, "File does not exist" },
    { 2, "Delete not allowed" },
    { 8, "Not performed" },
    { 0, NULL }
};

static const value_string cfdp_status_code_3[] = {
    { 0, "Successful" },
    { 1, "Old File Name does not exist" },
    { 2, "New File Name already exists" },
    { 3, "Rename not allowed" },
    { 8, "Not performed" },
    { 0, NULL }
};

static const value_string cfdp_status_code_4[] = {
    { 0, "Successful" },
    { 1, "File Name 1 does not exist" },
    { 2, "File Name 2 does not exist" },
    { 3, "Append not allowed" },
    { 8, "Not performed" },
    { 0, NULL }
};

static const value_string cfdp_status_code_5[] = {
    { 0, "Successful" },
    { 1, "File Name 1 does not exist" },
    { 2, "File Name 2 does not exist" },
    { 3, "Replace not allowed" },
    { 8, "Not performed" },
    { 0, NULL }
};

static const value_string cfdp_status_code_6[] = {
    { 0, "Successful" },
    { 1, "Directory cannot be created" },
    { 8, "Not performed" },
    { 0, NULL }
};

static const value_string cfdp_status_code_7[] = {
    { 0, "Successful" },
    { 1, "Directory does not exist" },
    { 2, "Delete not allowed" },
    { 8, "Not performed" },
    { 0, NULL }
};

static const value_string cfdp_status_code_8[] = {
    { 0, "Successful" },
    { 1, "Delete not allowed" },
    { 8, "Not performed" },
    { 0, NULL }
};

/* Finished PDU File Status */
static const value_string cfdp_file_stat[] = {
    { 0, "Delivery file discarded deliberately" },
    { 1, "Delivery file discarded due to filestore rejection" },
    { 2, "Delivery file retained in filestore successfully" },
    { 3, "Delivery file status unreported" },
    { 0, NULL }
};

/* Segmentation control */
static const value_string cfdp_segment_control[] = {
    { 0, "Record boundaries respected" },
    { 1, "Record boundaries not respected" },
    { 0, NULL }
};

/* Fault handler override Handler code*/
static const value_string cfdp_handler_codes[] = {
    { 1, "issue Notice of Cancellation" },
    { 2, "issue Notice of Suspension" },
    { 3, "Ignore error" },
    { 4, "Abandon transaction" },
    { 0, NULL }
};

/* Type of Proxy message */
static const value_string cfdp_proxy_msg_type[] = {
    { 0x00, "Proxy Put Request"},
    { 0x01, "Proxy Message To User"},
    { 0x02, "Proxy Filestore Request"},
    { 0x03, "Proxy Fault Handler Override"},
    { 0x04, "Proxy Transmission Mode"},
    { 0x05, "Proxy Flow Label"},
    { 0x06, "Proxy Segmentation Control"},
    { 0x07, "Proxy Put Response"},
    { 0x08, "Proxy Filestore Response"},
    { 0x09, "Proxy Put Cancel"},
    { 0x0A, "Originating Transaction ID"},
    { 0x10, "Directory Listing Request"},
    { 0x11, "Directory Listing Response"},
    { 0x20, "Remote Status Report Request"},
    { 0x21, "Remote Status Report Response"},
    { 0x30, "Remote Suspend Request"},
    { 0x31, "Remote Suspend Response"},
    { 0x38, "Remote Resume Request"},
    { 0x39, "Remote Resume Response"},
    { 0, NULL }
};
static value_string_ext cfdp_proxy_msg_type_ext = VALUE_STRING_EXT_INIT(cfdp_proxy_msg_type);

/* Prompt PDU Response required */
static const value_string cfdp_response_req[] = {
    { 0, "NAK" },
    { 1, "Keep Alive" },
    { 0, NULL }
};

/* Listing response code */
static const value_string cfdp_listing_resp_code[] = {
    { 0, "Successful" },
    { 1, "Unsuccessful" },
    { 0, NULL }
};

/* Report response code */
static const value_string cfdp_rep_resp_code[] = {
    { 0, "Unsuccessful" },
    { 1, "Successful" },
    { 0, NULL }
};

/* Suspension indication */
static const value_string cfdp_suspension_ind[] = {
    { 0, "Not Suspended" },
    { 1, "Suspended" },
    { 0, NULL }
};

/* File Directive codes */
static const value_string cfdp_directive_codes[] = {
    { 0x04, "EOF" },
    { 0x05, "Finished" },
    { 0x06, "ACK" },
    { 0x07, "Metadata" },
    { 0x08, "NAK" },
    { 0x09, "Prompt" },
    { 0x0C, "Keep Alive" },
    { 0, NULL }
};

static int * const cfdp_flags[] = {
  &hf_cfdp_version,
  &hf_cfdp_pdu_type,
  &hf_cfdp_direction,
  &hf_cfdp_trans_mode,
  &hf_cfdp_crc_flag,
  &hf_cfdp_res1,
  NULL
};

static int * const cfdp_byte2[] = {
    &hf_cfdp_res2,
    &hf_cfdp_entid_length,
    &hf_cfdp_res3,
    &hf_cfdp_transeqnum_length,
    NULL
};

static int * const cfdp_proxy_fault_hdl_overr[] = {
    &hf_cfdp_condition_code,
    &hf_cfdp_handler_code,
    NULL
};

static int * const cfdp_proxy_trans_mode [] = {
    &hf_cfdp_spare_seven_2,
    &hf_cfdp_trans_mode,
    NULL
};

static int * const cfdp_proxy_segment_control_byte [] = {
    &hf_cfdp_spare_seven_2,
    &hf_cfdp_proxy_segment_control,
    NULL
};

static int * const cfdp_proxy_put_resp [] = {
    &hf_cfdp_condition_code,
    &hf_cfdp_spare_one,
    &hf_cfdp_proxy_delivery_code,
    &hf_cfdp_file_stat,
    NULL
};

static int * const cfdp_orig_trans_id[] = {
    &hf_cfdp_res2,
    &hf_cfdp_entid_length,
    &hf_cfdp_res3,
    &hf_cfdp_transeqnum_length,
    NULL
};

static int * const cfdp_remote_stat_rep_req[] = {
    &hf_cfdp_res2,
    &hf_cfdp_entid_length,
    &hf_cfdp_res3,
    &hf_cfdp_transeqnum_length,
    NULL
};

static int * const cfdp_remote_stat_rep_resp[] = {
    &hf_cfdp_trans_stat,
    &hf_cfdp_spare_five,
    &hf_cfdp_rep_resp_code,
    &hf_cfdp_spare_one_2,
    &hf_cfdp_entid_length,
    &hf_cfdp_spare_one,
    &hf_cfdp_transeqnum_length,
    NULL
};

static int * const cfdp_finish_pdu_flags [] = {
    &hf_cfdp_condition_code,
    &hf_cfdp_end_system_stat,
    &hf_cfdp_delivery_code,
    &hf_cfdp_file_stat,
    NULL
};

static int * const cfdp_remote_suspend_resume_req [] = {
    &hf_cfdp_spare_one_2,
    &hf_cfdp_entid_length,
    &hf_cfdp_spare_one,
    &hf_cfdp_transeqnum_length,
    NULL
};


static int * const cfdp_remote_suspend_resume_resp [] = {
    &hf_cfdp_suspension_ind,
    &hf_cfdp_trans_stat_2,
    &hf_cfdp_spare_five,
    &hf_cfdp_spare_one_2,
    &hf_cfdp_entid_length,
    &hf_cfdp_spare_one,
    &hf_cfdp_transeqnum_length,
    NULL
};



/* Dissect the Source Entity ID field */
static void
dissect_cfdp_src_entity_id(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint32 offset, guint8 len_ent_id)
{
    if(len_ent_id > 0 && len_ent_id <= 8){
        proto_tree_add_item(tree, hf_cfdp_srcid, tvb, offset, len_ent_id, ENC_BIG_ENDIAN);
    }
    else{
        proto_tree_add_expert_format(tree, pinfo, &ei_cfdp_bad_length, tvb, offset, 0, "Wrong length for the entity ID");
    }
}

/* Dissect the Destination Entity ID field */
static void
dissect_cfdp_dst_entity_id(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint32 offset, guint8 len_ent_id)
{
    if(len_ent_id > 0 && len_ent_id <= 8){
        proto_tree_add_item(tree, hf_cfdp_dstid, tvb, offset, len_ent_id, ENC_BIG_ENDIAN);
    }
    else{
        proto_tree_add_expert_format(tree, pinfo, &ei_cfdp_bad_length, tvb, offset, 0, "Wrong length for the entity ID");
    }
}

/* Dissect the Transaction Sequence Number field */
static void
dissect_cfdp_tseq_num(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint32 offset, guint8 len_tseq_num)
{
    if(len_tseq_num > 0 && len_tseq_num <= 8){
        proto_tree_add_item(tree, hf_cfdp_transeqnum, tvb, offset, len_tseq_num, ENC_BIG_ENDIAN);
    }
    else{
        proto_tree_add_expert_format(tree, pinfo, &ei_cfdp_bad_length, tvb, offset, 0, "Wrong length for transaction sequence number");
    }
}

/* Dissect the Filestore Request TLV */
static guint32 dissect_cfdp_filestore_req_tlv(tvbuff_t *tvb, proto_tree *tree, guint32 ext_offset){

    guint8 tlv_len;

    guint32 offset = ext_offset;
    guint32 length;

    /* Get field length */
    tlv_len = tvb_get_guint8(tvb, offset);
    offset += 1;
    if(tlv_len > 0){
        proto_tree  *cfdp_filestore_req_tree;
        guint8 aux_byte;

        /* Create a TLV subtree */
        cfdp_filestore_req_tree = proto_tree_add_subtree(tree, tvb, offset-2, tlv_len+2,
            ett_cfdp_filestore_req, NULL, "Filestore Request TLV");

        proto_tree_add_uint(cfdp_filestore_req_tree, hf_cfdp_tlv_len, tvb, offset-1, 1, tlv_len);

        aux_byte = tvb_get_guint8(tvb, offset);
        proto_tree_add_uint(cfdp_filestore_req_tree, hf_cfdp_action_code, tvb, offset, 1, aux_byte);
        proto_tree_add_uint(cfdp_filestore_req_tree, hf_cfdp_spare_four, tvb, offset, 1, aux_byte);
        offset += 1;

        proto_tree_add_item_ret_uint(cfdp_filestore_req_tree, hf_cfdp_first_file_name_len, tvb, offset, 1, ENC_NA, &length);
        offset += 1;
        if(length > 0){
            proto_tree_add_item(cfdp_filestore_req_tree, hf_cfdp_first_file_name, tvb, offset, length, ENC_ASCII);
        }
        offset += length;

        proto_tree_add_item_ret_uint(cfdp_filestore_req_tree, hf_cfdp_second_file_name_len, tvb, offset, 1, ENC_NA, &length);
        offset += 1;
        if(length > 0){
            proto_tree_add_item(cfdp_filestore_req_tree, hf_cfdp_second_file_name, tvb, offset, length, ENC_ASCII);
        }
        offset += length;
    }

    return offset;
}

/* Dissect the Filestore Response TLV */
static guint32 dissect_cfdp_filestore_resp_tlv(tvbuff_t *tvb, proto_tree *tree, guint32 ext_offset){

    guint8 tlv_len;

    guint32 offset = ext_offset;

    /* Get field length */
    tlv_len = tvb_get_guint8(tvb, offset);
    offset += 1;
    if(tlv_len > 0){
        proto_tree  *cfdp_filestore_resp_tree;
        guint8 aux_byte;
        guint32 length;

        /* Create a subtree */
        cfdp_filestore_resp_tree = proto_tree_add_subtree(tree, tvb, offset-2, tlv_len+2,
                        ett_cfdp_filestore_resp, NULL, "Filestore Response TLV");

        proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_tlv_len, tvb, offset-1, 1, tlv_len);

        aux_byte = tvb_get_guint8(tvb, offset);
        proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_action_code, tvb, offset, 1, aux_byte);
        switch((aux_byte & 0xF0) >> 4){
            case 0:
                proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_status_code_1, tvb, offset, 1, aux_byte);
                break;
            case 1:
                proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_status_code_2, tvb, offset, 1, aux_byte);
                break;
            case 2:
                proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_status_code_3, tvb, offset, 1, aux_byte);
                break;
            case 3:
                proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_status_code_4, tvb, offset, 1, aux_byte);
                break;
            case 4:
                proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_status_code_5, tvb, offset, 1, aux_byte);
                break;
            case 5:
                proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_status_code_6, tvb, offset, 1, aux_byte);
                break;
            case 6:
                proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_status_code_7, tvb, offset, 1, aux_byte);
                break;
            case 7: case 8:
                proto_tree_add_uint(cfdp_filestore_resp_tree, hf_cfdp_status_code_8, tvb, offset, 1, aux_byte);
                break;

            default:
                break;
        }
        offset += 1;

        proto_tree_add_item_ret_uint(cfdp_filestore_resp_tree, hf_cfdp_first_file_name_len, tvb, offset, 1, ENC_NA, &length);
        offset += 1;
        if(length > 0){
            proto_tree_add_item(cfdp_filestore_resp_tree, hf_cfdp_first_file_name, tvb, offset, length, ENC_ASCII);
        }
        offset += length;

        proto_tree_add_item_ret_uint(cfdp_filestore_resp_tree, hf_cfdp_second_file_name_len, tvb, offset, 1, ENC_NA, &length);
        offset += 1;
        if(length > 0){
            proto_tree_add_item(cfdp_filestore_resp_tree, hf_cfdp_second_file_name, tvb, offset, length, ENC_ASCII);
        }
        offset += length;

        /* Filestore Message */
        proto_tree_add_item_ret_uint(cfdp_filestore_resp_tree, hf_cfdp_filestore_message_len, tvb, offset, 1, ENC_NA, &length);
        offset += 1;
        if(length > 0){
            proto_tree_add_item(cfdp_filestore_resp_tree, hf_cfdp_filestore_message, tvb, offset, length, ENC_NA);
        }
        offset += length;
    }

    return offset+1;
}

/* Dissect the Fault Location TLV */
static guint32 dissect_cfdp_fault_location_tlv(tvbuff_t *tvb, proto_tree *tree, guint32 ext_offset){

    guint8 tlv_len;

    guint32 offset = ext_offset;

    /* Get field length */
    tlv_len = tvb_get_guint8(tvb, offset);
    offset += 1;
    if(tlv_len > 0){
        proto_tree  *cfdp_fault_location_tree;

        /* Create a subtree */
        cfdp_fault_location_tree = proto_tree_add_subtree(tree, tvb, offset-2, tlv_len+2,
                                        ett_cfdp_fault_location, NULL, "Fault location TLV");

        proto_tree_add_uint(cfdp_fault_location_tree, hf_cfdp_tlv_len, tvb, offset-1, 1, tlv_len);

        proto_tree_add_item(cfdp_fault_location_tree, hf_cfdp_entity, tvb, offset, tlv_len, ENC_NA);
        offset += tlv_len;
    }

    return offset;
}

/* Dissect the Message to User TLV */
static guint32 dissect_cfdp_msg_to_user_tlv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint32 ext_offset){

    guint8 tlv_type;
    guint8 tlv_len;
    proto_tree  *cfdp_msg_to_user_tree;

    guint32 offset = ext_offset;
    guint32 msg_to_user_id;

    guint64 retval;

    int len_ent_id;
    int len_tseq_num;

    /* Get tlv len */
    tlv_len = tvb_get_guint8(tvb, offset);
    offset += 1;

    /* Create a subtree */
    cfdp_msg_to_user_tree = proto_tree_add_subtree(tree, tvb, offset-2, tlv_len+2,
                                    ett_cfdp_filestore_resp, NULL, "Message To User TLV");

    proto_tree_add_uint(cfdp_msg_to_user_tree, hf_cfdp_tlv_len, tvb, offset-1, 1, tlv_len);

    msg_to_user_id = tvb_get_ntohl(tvb, offset);
    /* Proxy operations */
    if(msg_to_user_id == CFDP_MSG_TO_USER){
        offset += 4;
        tlv_type =  tvb_get_guint8(tvb, offset);
        proto_tree_add_uint(cfdp_msg_to_user_tree, hf_cfdp_proxy_msg_type, tvb, offset, 1, tlv_type);
        offset += 1;
        switch(tlv_type){

            case PROXY_PUT_REQ:
                tlv_len = tvb_get_guint8(tvb, offset);
                offset += 1;
                dissect_cfdp_dst_entity_id(tvb, pinfo, cfdp_msg_to_user_tree, offset, tlv_len);
                offset += tlv_len;

                tlv_len = tvb_get_guint8(tvb, offset);
                offset += 1;
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_src_file_name, tvb, offset, tlv_len, ENC_ASCII);
                offset += tlv_len;

                tlv_len = tvb_get_guint8(tvb, offset);
                offset += 1;
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_dst_file_name, tvb, offset, tlv_len, ENC_ASCII);
                offset += tlv_len;

                break;

            case PROXY_MSG_TO_USER:
                tlv_len = tvb_get_guint8(tvb, offset);
                offset += 1;
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_message_to_user, tvb, offset, tlv_len, ENC_NA);
                offset += tlv_len;
                break;

            case PROXY_FILESTORE_REQ:
                offset = dissect_cfdp_filestore_req_tlv(tvb, cfdp_msg_to_user_tree, offset);
                break;

            case PROXY_FAULT_HDL_OVERR:
                proto_tree_add_bitmask(cfdp_msg_to_user_tree, tvb, offset,
                                     hf_cfdp_proxy_fault_hdl_overr,
                                     ett_cfdp_proxy_fault_hdl_overr,
                                     cfdp_proxy_fault_hdl_overr,
                                     ENC_BIG_ENDIAN);
                offset += 1;
                break;

            case PROXY_TRANS_MODE:
                proto_tree_add_bitmask(cfdp_msg_to_user_tree, tvb, offset,
                                     hf_cfdp_proxy_trans_mode,
                                     ett_cfdp_proxy_trans_mode,
                                     cfdp_proxy_trans_mode,
                                     ENC_BIG_ENDIAN);
                offset += 1;
                break;

            case PROXY_FLOW_LABEL:
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_flow_label, tvb, offset, tlv_len, ENC_NA);
                break;

            case PROXY_SEGMENT_CONTROL:
                proto_tree_add_bitmask(cfdp_msg_to_user_tree, tvb, offset,
                                     hf_cfdp_proxy_segment_control_byte,
                                     ett_cfdp_proxy_segment_control_byte,
                                     cfdp_proxy_segment_control_byte,
                                     ENC_BIG_ENDIAN);
                offset += 1;
                break;

            case PROXY_PUT_RESP:
                proto_tree_add_bitmask(cfdp_msg_to_user_tree, tvb, offset,
                                     hf_cfdp_proxy_put_resp,
                                     ett_cfdp_proxy_put_resp,
                                     cfdp_proxy_put_resp,
                                     ENC_BIG_ENDIAN);
                offset += 1;
                break;

            case PROXY_FILESTORE_RESP:
                offset = dissect_cfdp_filestore_req_tlv(tvb, cfdp_msg_to_user_tree, offset);
                break;

            case PROXY_PUT_CANCEL:
                break;

            case ORIG_TRANS_ID:
                proto_tree_add_bitmask_ret_uint64(cfdp_msg_to_user_tree, tvb, offset,
                                     hf_cfdp_orig_trans_id,
                                     ett_cfdp_orig_trans_id,
                                     cfdp_orig_trans_id,
                                     ENC_BIG_ENDIAN,
                                     &retval);
                offset += 1;

                len_ent_id = ((retval & HDR_LEN_ENT_ID) >> 4) + 1;
                dissect_cfdp_src_entity_id(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_ent_id);
                offset += len_ent_id;

                len_tseq_num = (retval & HDR_LEN_TSEQ_NUM) +1;
                dissect_cfdp_tseq_num(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_tseq_num);
                offset += len_tseq_num;

                break;

            case DIRECTORY_LIST_REQ:
                /* Directory Name */
                tlv_len =  tvb_get_guint8(tvb, offset);
                offset += 1;
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_directory_name, tvb, offset, tlv_len, ENC_ASCII);
                offset += tlv_len;
                /* Directory File Name */
                tlv_len =  tvb_get_guint8(tvb, offset);
                offset += 1;
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_directory_file_name, tvb, offset, tlv_len, ENC_ASCII);
                offset += tlv_len;
                break;

            case DIRECTORY_LIST_RESP:
                /* Listing Response Code */
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_listing_resp_code, tvb, offset, 1, ENC_NA);
                offset += 1;
                /* Directory Name */
                tlv_len =  tvb_get_guint8(tvb, offset);
                offset += 1;
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_directory_name, tvb, offset, tlv_len, ENC_ASCII);
                offset += tlv_len;
                /* Directory File Name */
                tlv_len =  tvb_get_guint8(tvb, offset);
                offset += 1;
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_directory_file_name, tvb, offset, tlv_len, ENC_ASCII);
                offset += tlv_len;
                break;

            case REMOTE_STAT_REP_REQ:
                proto_tree_add_bitmask_ret_uint64(cfdp_msg_to_user_tree, tvb, offset,
                                     hf_cfdp_remote_stat_rep_req,
                                     ett_cfdp_remote_stat_rep_req,
                                     cfdp_remote_stat_rep_req,
                                     ENC_BIG_ENDIAN,
                                     &retval);
                offset += 1;

                len_ent_id = ((retval & HDR_LEN_ENT_ID) >> 4) + 1;
                dissect_cfdp_src_entity_id(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_ent_id);
                offset += len_ent_id;

                len_tseq_num = (retval & HDR_LEN_TSEQ_NUM) +1;
                dissect_cfdp_tseq_num(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_tseq_num);
                offset += len_tseq_num;

                /* Report File Name */
                tlv_len =  tvb_get_guint8(tvb, offset);
                offset += 1;
                proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_report_file_name, tvb, offset, tlv_len, ENC_ASCII);
                offset += tlv_len;
                break;

            case REMOTE_STAT_REP_RESP:
                proto_tree_add_bitmask_ret_uint64(cfdp_msg_to_user_tree, tvb, offset,
                                     hf_cfdp_remote_stat_rep_resp,
                                     ett_cfdp_remote_stat_rep_resp,
                                     cfdp_remote_stat_rep_resp,
                                     ENC_BIG_ENDIAN,
                                     &retval);

                len_ent_id = ((retval & (HDR_LEN_ENT_ID<<8)) >> 12) + 1;
                dissect_cfdp_src_entity_id(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_ent_id);
                offset += len_ent_id;

                len_tseq_num = (retval & HDR_LEN_TSEQ_NUM) +1;
                dissect_cfdp_tseq_num(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_tseq_num);
                offset += len_tseq_num;
                break;

            case REMOTE_SUSPEND_REQ:
            case REMOTE_RESUME_REQ:
                proto_tree_add_bitmask_ret_uint64(cfdp_msg_to_user_tree, tvb, offset,
                                     hf_cfdp_remote_suspend_resume_req,
                                     ett_cfdp_remote_suspend_resume_req,
                                     cfdp_remote_suspend_resume_req,
                                     ENC_BIG_ENDIAN,
                                     &retval);

                offset += 1;

                len_ent_id = ((retval & HDR_LEN_ENT_ID) >> 4) + 1;
                dissect_cfdp_src_entity_id(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_ent_id);
                offset += len_ent_id;

                len_tseq_num = (retval & HDR_LEN_TSEQ_NUM) +1;
                dissect_cfdp_tseq_num(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_tseq_num);
                offset += len_tseq_num;
                break;

            case REMOTE_SUSPEND_RESP:
            case REMOTE_RESUME_RESP:
                proto_tree_add_bitmask_ret_uint64(cfdp_msg_to_user_tree, tvb, offset,
                                     hf_cfdp_remote_suspend_resume_resp,
                                     ett_cfdp_remote_suspend_resume_resp,
                                     cfdp_remote_suspend_resume_resp,
                                     ENC_BIG_ENDIAN,
                                     &retval);
                offset += 2;

                len_ent_id = ((retval & HDR_LEN_ENT_ID) >> 4) + 1;
                dissect_cfdp_src_entity_id(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_ent_id);
                offset += len_ent_id;

                len_tseq_num = (retval & HDR_LEN_TSEQ_NUM) +1;
                dissect_cfdp_tseq_num(tvb, pinfo, cfdp_msg_to_user_tree, offset, len_tseq_num);
                offset += len_tseq_num;
                break;

            default:
                break;
        }
    }else{
        proto_tree_add_item(cfdp_msg_to_user_tree, hf_cfdp_message_to_user, tvb, offset, tlv_len, ENC_NA);
        offset += tlv_len;
    }

    return offset;
}

/* Dissect the Fault Handler Override TLV */
static guint32 dissect_cfdp_fault_handler_overr_tlv(tvbuff_t *tvb, proto_tree *tree, guint32 ext_offset){

    guint8 aux_byte, tlv_len;
    proto_tree  *cfdp_fault_hdl_overr_tree;

    guint32 offset = ext_offset;

    /* Get tlv len */
    tlv_len = tvb_get_guint8(tvb, offset);
    offset += 1;

    /* Create a subtree */
    cfdp_fault_hdl_overr_tree = proto_tree_add_subtree(tree, tvb, offset-2, tlv_len+2,
                        ett_cfdp_fault_hdl_overr, NULL, "Fault Handler Override TLV");

    proto_tree_add_uint(cfdp_fault_hdl_overr_tree, hf_cfdp_tlv_len, tvb, offset-1, 1, tlv_len);

    aux_byte = tvb_get_guint8(tvb, offset);
    proto_tree_add_uint(cfdp_fault_hdl_overr_tree, hf_cfdp_condition_code, tvb, offset, 1, aux_byte);
    proto_tree_add_uint(cfdp_fault_hdl_overr_tree, hf_cfdp_handler_code, tvb, offset, 1, aux_byte);
    offset += 1;

    return offset;
}

/* Dissect the Flow Label TLV */
static guint32 dissect_cfdp_flow_label_tlv(tvbuff_t *tvb, proto_tree *tree, guint32 ext_offset){

    guint8 tlv_len;
    proto_tree  *cfdp_flow_label_tree;

    guint32 offset = ext_offset;

    /* Get tlv len */
    tlv_len = tvb_get_guint8(tvb, offset);
    offset += 1;

    /* Create a subtree */
    cfdp_flow_label_tree = proto_tree_add_subtree(tree, tvb, offset-2, tlv_len+2,
                                        ett_cfdp_flow_label, NULL, "Flow Label TLV");

    /* It is undefined, so no specific encoding */
    proto_tree_add_item(cfdp_flow_label_tree, hf_cfdp_flow_label, tvb, offset, tlv_len, ENC_NA);

    return offset;
}

/* Dissect the End of File PDU */
static guint32 dissect_cfdp_eof_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint32 ext_offset, guint ext_packet_len){

    guint8 aux_byte, tlv_type, tlv_len;
    proto_tree  *cfdp_fault_location_tree;

    guint32 offset = ext_offset;
    guint   cfdp_packet_data_length = ext_packet_len;

    aux_byte = tvb_get_guint8(tvb, offset);
    proto_tree_add_uint(tree, hf_cfdp_condition_code, tvb, offset, 1, aux_byte);
    proto_tree_add_uint(tree, hf_cfdp_spare_four, tvb, offset, 1, aux_byte);
    offset += 1;

    col_add_fstr(pinfo->cinfo, COL_INFO, "EOF (%s)",  val_to_str_const((aux_byte & 0xF0) >> 4, cfdp_condition_codes, "Reserved Code"));

    proto_tree_add_checksum(tree, tvb, offset, hf_cfdp_file_checksum, -1, NULL, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
    offset += 4;

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

    if(offset < cfdp_packet_data_length){
        tlv_type = tvb_get_guint8(tvb, offset);
        offset += 1;
        if(tlv_type == FAULT_LOCATION){
            tlv_len = tvb_get_guint8(tvb, offset);
            offset += 1;
            cfdp_fault_location_tree = proto_tree_add_subtree(tree, tvb, offset-2, tlv_len+2,
                        ett_cfdp_fault_location, NULL, "Fault location TLV");

            proto_tree_add_item(cfdp_fault_location_tree, hf_cfdp_entity, tvb, offset, tlv_len, ENC_NA);
            offset += tlv_len;
        }
    }

    return offset;
}

/* Dissect the Finished PDU */
static guint32 dissect_cfdp_finished_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint32 ext_offset, guint ext_packet_len){

    guint32 offset = ext_offset;
    guint8  tlv_type;
    guint64 aux_byte;

    guint cfdp_packet_data_length = offset+ext_packet_len;

    proto_tree_add_bitmask_ret_uint64(tree, tvb, offset,
                         hf_cfdp_finish_pdu_flags,
                         ett_cfdp_finish_pdu_flags,
                         cfdp_finish_pdu_flags,
                         ENC_BIG_ENDIAN,
                         &aux_byte);
    offset += 1;

    col_add_fstr(pinfo->cinfo, COL_INFO, "Finished PDU (%s)",  val_to_str_const((aux_byte & 0xF0) >> 4, cfdp_condition_codes, "Reserved Code"));

    /* Add TLV fields */
    while(offset < cfdp_packet_data_length-1){
        tlv_type = tvb_get_guint8(tvb, offset);
        offset += 1;
        switch(tlv_type){
            case 0x00:
                offset += 2;
                break;
            case FILESTORE_RESP:
                offset = dissect_cfdp_filestore_resp_tlv(tvb, tree, offset);
                break;

            case FAULT_LOCATION:
                offset = dissect_cfdp_fault_location_tlv(tvb, tree, offset);
                break;

            default:
                break;
        }
    }

    return offset;
}

/* Dissect the ACK PDU */
static guint32 dissect_cfdp_ack_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint32 ext_offset){

    guint8 aux_byte;
    guint32 offset = ext_offset;

    aux_byte = tvb_get_guint8(tvb, offset);
    proto_tree_add_uint(tree, hf_cfdp_dir_code_ack, tvb, offset, 1, aux_byte);
    proto_tree_add_uint(tree, hf_cfdp_dir_subtype_ack, tvb, offset, 1, aux_byte);
    offset += 1;

    col_add_fstr(pinfo->cinfo, COL_INFO, "ACK PDU (%s)",  val_to_str_const((aux_byte & 0xF0) >> 4, cfdp_directive_codes, "Unknown PDU"));

    aux_byte = tvb_get_guint8(tvb, offset);
    proto_tree_add_uint(tree, hf_cfdp_condition_code, tvb, offset, 1, aux_byte);
    proto_tree_add_uint(tree, hf_cfdp_spare_two, tvb, offset, 1, aux_byte);
    proto_tree_add_uint(tree, hf_cfdp_trans_stat_ack, tvb, offset, 1, aux_byte);
    offset += 1;

    return offset;
}

/* Dissect the Metadata PDU */
static guint32 dissect_cfdp_metadata_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint32 ext_offset, guint ext_packet_len){

    guint8 aux_byte, tlv_type;
    guint  cfdp_packet_data_length = ext_packet_len;
    guint32 length;

    guint32 offset = ext_offset;

    aux_byte = tvb_get_guint8(tvb, offset);
    proto_tree_add_uint(tree, hf_cfdp_segment_control, tvb, offset, 1, aux_byte);
    proto_tree_add_uint(tree, hf_cfdp_spare_seven, tvb, offset, 1, aux_byte);
    offset += 1;
    proto_tree_add_item(tree, hf_cfdp_file_size, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item_ret_uint(tree, hf_cfdp_src_file_name_len, tvb, offset, 1, ENC_NA, &length);
    offset += 1;
    if(length >0){
        proto_tree_add_item(tree, hf_cfdp_src_file_name, tvb, offset, length, ENC_ASCII);
    }
    offset += length;
    proto_tree_add_item_ret_uint(tree, hf_cfdp_dst_file_name_len, tvb, offset, 1, ENC_NA, &length);
    offset += 1;
    if(length >0){
        proto_tree_add_item(tree, hf_cfdp_dst_file_name, tvb, offset, length, ENC_ASCII);
    }
    offset += length;
    /* Add TLV fields */
    while(offset < cfdp_packet_data_length){
        tlv_type = tvb_get_guint8(tvb, offset);
        offset += 1;
        switch(tlv_type){
            case FILESTORE_REQ:
                offset = dissect_cfdp_filestore_req_tlv(tvb, tree, offset);
                break;

            case MSG_TO_USER:
                offset = dissect_cfdp_msg_to_user_tlv(tvb, pinfo, tree, offset);
                break;

            case FAULT_HDL_OVERR:
                offset = dissect_cfdp_fault_handler_overr_tlv(tvb, tree, offset);
                break;

            case FLOW_LABEL:
                offset = dissect_cfdp_flow_label_tlv(tvb, tree, offset);
                break;

            default:
                break;
        }
    }

    return offset;
}

/* Dissect the NAK PDU */
static guint32 dissect_cfdp_nak_pdu(tvbuff_t *tvb, proto_tree *tree, guint32 ext_offset, guint ext_packet_len){

    guint32 offset = ext_offset;
    guint cfdp_packet_data_length = ext_packet_len;

    proto_tree_add_item(tree, hf_cfdp_nak_st_scope, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_cfdp_nak_sp_scope, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_cfdp_segment_requests, tvb, offset, cfdp_packet_data_length-9, ENC_NA);
    offset += cfdp_packet_data_length-9;

    return offset;
}

/* Dissect the Prompt PDU */
static guint32 dissect_cfdp_prompt_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint32 ext_offset){

    guint8 aux_byte;
    guint32 offset = ext_offset;

    aux_byte = tvb_get_guint8(tvb, offset);
    proto_tree_add_uint(tree, hf_cfdp_response_req, tvb, offset, 1, aux_byte);
    proto_tree_add_uint(tree, hf_cfdp_spare_seven, tvb, offset, 1, aux_byte);
    offset += 1;

    col_add_fstr(pinfo->cinfo, COL_INFO, "Prompt PDU (%s)",  val_to_str_const((aux_byte & 0x80) >> 7, cfdp_response_req, "Unknown"));

    return offset;
}

/* Dissect the Keep Alive PDU */
static guint32 dissect_cfdp_keep_alive_pdu(tvbuff_t *tvb, proto_tree *tree, guint32 ext_offset){

    guint32 offset = ext_offset;

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

    return offset;
}

/* Code to actually dissect the packets */
static int
dissect_cfdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    int          offset          = 0;
    proto_item  *cfdp_packet;
    proto_item  *cfdp_tree;
    proto_item  *cfdp_header;
    proto_tree  *cfdp_header_tree;
    gint cfdp_packet_length;
    gint cfdp_packet_reported_length;
    gint cfdp_packet_header_length;
    gint cfdp_packet_data_length;
    gint length;
    guint8 first_byte;
    guint64 retval;
    gint len_ent_id;
    gint len_tseq_num;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "CFDP");
    col_clear(pinfo->cinfo, COL_INFO);

    cfdp_packet_reported_length = tvb_reported_length_remaining(tvb, 0);
    cfdp_packet_header_length = (tvb_get_guint8(tvb, 3) & HDR_LEN_TSEQ_NUM) + 1 + 2*(((tvb_get_guint8(tvb, 3) & HDR_LEN_ENT_ID) >>4) +1) + CFDP_HEADER_FIXED_FIELDS_LEN;
    cfdp_packet_length = tvb_get_ntohs(tvb, 1) + cfdp_packet_header_length;

    /* Min length is size of header plus 2 octets, whereas max length is reported length.
     * If the length field in the CFDP header is outside of these bounds,
     * use the value it violates.  Otherwise, use the length field value.
     */
    if(cfdp_packet_length > cfdp_packet_reported_length)
        length = cfdp_packet_reported_length;
    else if(cfdp_packet_length < cfdp_packet_header_length + 2)
        length = cfdp_packet_header_length + 2;
    else
        length = cfdp_packet_length;

    /* Build the cfdp tree */
    cfdp_packet = proto_tree_add_item(tree, proto_cfdp, tvb, 0, length, ENC_NA);
    cfdp_tree   = proto_item_add_subtree(cfdp_packet, ett_cfdp);

    cfdp_header_tree = proto_tree_add_subtree(cfdp_tree, tvb, offset, cfdp_packet_header_length,
                                                                ett_cfdp_header, &cfdp_header, "CFDP Header");

    first_byte = tvb_get_guint8(tvb, offset);

    /* CRC code is not included in the packet data length */
    cfdp_packet_data_length = tvb_get_ntohs(tvb, 1)-2*((first_byte & HDR_CRCF) >>1);

    proto_tree_add_bitmask(cfdp_header_tree, tvb, offset,
                         hf_cfdp_flags,
                         ett_cfdp_flags,
                         cfdp_flags,
                         ENC_BIG_ENDIAN);
    offset += 1;

    proto_tree_add_item(cfdp_header_tree, hf_cfdp_data_length, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    proto_tree_add_bitmask_ret_uint64(cfdp_header_tree, tvb, offset,
                         hf_cfdp_byte2,
                         ett_cfdp_byte2,
                         cfdp_byte2,
                         ENC_BIG_ENDIAN,
                         &retval);
    offset += 1;

    len_ent_id = ((retval & HDR_LEN_ENT_ID) >> 4) + 1;
    dissect_cfdp_src_entity_id(tvb, pinfo, cfdp_header_tree, offset, len_ent_id);
    offset += len_ent_id;

    len_tseq_num = (retval & HDR_LEN_TSEQ_NUM) +1;
    dissect_cfdp_tseq_num(tvb, pinfo, cfdp_header_tree, offset, len_tseq_num);
    offset += len_tseq_num;

    dissect_cfdp_dst_entity_id(tvb, pinfo, cfdp_header_tree, offset, len_ent_id);
    offset += len_ent_id;

    proto_item_set_end(cfdp_header, tvb, offset);

    /* Build the File Directive or the File Data tree */
    if(!(first_byte & HDR_TYPE_CFDP))
    {
        proto_item *cfdp_file_directive_header;
        proto_tree *cfdp_file_directive_header_tree;
        guint8      directive_code;

        cfdp_file_directive_header_tree = proto_tree_add_subtree(cfdp_tree, tvb, offset, cfdp_packet_data_length,
                                                        ett_cfdp_file_directive_header, &cfdp_file_directive_header, "CFDP File Directive");

        directive_code = tvb_get_guint8(tvb, offset);
        proto_tree_add_uint(cfdp_file_directive_header_tree, hf_cfdp_file_directive_type, tvb, offset, 1, directive_code);
        offset += 1;

        col_add_fstr(pinfo->cinfo, COL_INFO, "%s PDU",  val_to_str(directive_code, cfdp_directive_codes, "Reserved (%d)"));

        switch(directive_code)
        {
            case EOF_PDU:
                offset = dissect_cfdp_eof_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset, cfdp_packet_data_length);
                break;

            case FINISHED_PDU:
                offset = dissect_cfdp_finished_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset, cfdp_packet_data_length);
                break;

            case ACK_PDU:
                offset = dissect_cfdp_ack_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset);
                break;

            case METADATA_PDU:
                offset = dissect_cfdp_metadata_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset, cfdp_packet_data_length);
                break;

            case NAK_PDU:
                offset = dissect_cfdp_nak_pdu(tvb, cfdp_file_directive_header_tree, offset, cfdp_packet_data_length);
                break;

            case PROMPT_PDU:
                offset = dissect_cfdp_prompt_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset);
                break;

            case KEEP_ALIVE_PDU:
                offset = dissect_cfdp_keep_alive_pdu(tvb, cfdp_file_directive_header_tree, offset);
                break;

            default:
                break;
        }

        proto_item_set_end(cfdp_file_directive_header, tvb, offset);

    }else{
        proto_tree  *cfdp_file_data_header_tree;

        col_add_fstr(pinfo->cinfo, COL_INFO, "File Data PDU");

        cfdp_file_data_header_tree = proto_tree_add_subtree(cfdp_tree, tvb, offset, cfdp_packet_data_length,
                                                            ett_cfdp_file_data_header, NULL, "CFDP File Data");

        proto_tree_add_item(cfdp_file_data_header_tree, hf_cfdp_file_data_offset, tvb, offset, 4, ENC_BIG_ENDIAN);

        offset += 4;

        proto_tree_add_item(cfdp_file_data_header_tree, hf_cfdp_user_data, tvb, offset, cfdp_packet_data_length-4, ENC_NA);
        offset += cfdp_packet_data_length-4;

    }
    if(first_byte & HDR_CRCF){
        proto_item  *cfdp_crc;
        proto_tree  *cfdp_crc_tree;

        cfdp_crc_tree = proto_tree_add_subtree(cfdp_tree, tvb, offset, 2, ett_cfdp_crc, &cfdp_crc, "CRC");

        proto_tree_add_item(cfdp_crc_tree, hf_cfdp_crc, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset += 2;

        proto_item_set_end(cfdp_crc, tvb, offset);
    }
    /* Give the data dissector any bytes past the CFDP packet length */
    call_data_dissector(tvb_new_subset_remaining(tvb, offset), pinfo, tree);
    return tvb_captured_length(tvb);
}

void
dissect_cfdp_as_subtree(tvbuff_t *tvb,  packet_info *pinfo, proto_tree *tree, int offset)
{
    proto_tree  *cfdp_header_tree = NULL;
    proto_tree *cfdp_sub_tree = NULL;
    proto_item *payload_item = NULL;
    proto_tree *cfdp_tree = NULL;
    proto_item  *cfdp_header;
    gint cfdp_data_len;
    gint len_ent_id;
    gint len_tseq_num;
    guint64 first_byte;
    guint64 retval;

    cfdp_tree = proto_tree_add_subtree(tree, tvb, offset, -1, ett_cfdp_proto,
                                       &payload_item, "Payload Data: CFDP Protocol");

    cfdp_sub_tree   = proto_item_add_subtree(cfdp_tree, ett_cfdp);
    cfdp_header_tree = proto_tree_add_subtree(cfdp_sub_tree, tvb, offset, -1,
                                              ett_cfdp_header, &cfdp_header, "CFDP Header");

    proto_tree_add_bitmask_ret_uint64(cfdp_header_tree, tvb, offset,
                         hf_cfdp_flags,
                         ett_cfdp_flags,
                         cfdp_flags,
                         ENC_BIG_ENDIAN,
                         &first_byte);
    offset += 1;

    guint cfdp_data_end;
    cfdp_data_len = tvb_get_guint16 (tvb, offset, ENC_BIG_ENDIAN);
    proto_tree_add_item(cfdp_header_tree, hf_cfdp_data_length, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    proto_tree_add_bitmask_ret_uint64(cfdp_header_tree, tvb, offset,
                         hf_cfdp_byte2,
                         ett_cfdp_byte2,
                         cfdp_byte2,
                         ENC_BIG_ENDIAN,
                         &retval);
    offset += 1;

    len_ent_id = ((retval & HDR_LEN_ENT_ID) >> 4) + 1;
    dissect_cfdp_src_entity_id(tvb, pinfo, cfdp_header_tree, offset, len_ent_id);
    offset += len_ent_id;

    len_tseq_num = (retval & HDR_LEN_TSEQ_NUM) +1;
    dissect_cfdp_tseq_num(tvb, pinfo, cfdp_header_tree, offset, len_tseq_num);
    offset += len_tseq_num;

    dissect_cfdp_dst_entity_id(tvb, pinfo, cfdp_header_tree, offset, len_ent_id);
    offset += len_ent_id;

    cfdp_data_end = offset+cfdp_data_len;

    /* Build the File Directive or the File Data tree */
    if(!(first_byte & HDR_TYPE_CFDP))
    {
        proto_item *cfdp_file_directive_header;
        proto_tree *cfdp_file_directive_header_tree;
        guint8      directive_code;

        cfdp_file_directive_header_tree = proto_tree_add_subtree(cfdp_tree, tvb, offset, cfdp_data_len,
                                                        ett_cfdp_file_directive_header, &cfdp_file_directive_header,
                                                        "CFDP File Directive");

        directive_code = tvb_get_guint8(tvb, offset);
        proto_tree_add_uint(cfdp_file_directive_header_tree, hf_cfdp_file_directive_type, tvb, offset, 1, directive_code);
        offset += 1;

        col_add_fstr(pinfo->cinfo, COL_INFO, "%s PDU",  val_to_str(directive_code, cfdp_directive_codes, "Reserved (%d)"));

        switch(directive_code)
        {
            case EOF_PDU:
                offset = dissect_cfdp_eof_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset, cfdp_data_len);
                break;

            case FINISHED_PDU:
                offset = dissect_cfdp_finished_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset, cfdp_data_len);
                break;

            case ACK_PDU:
                offset = dissect_cfdp_ack_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset);
                break;

            case METADATA_PDU:
                offset = dissect_cfdp_metadata_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset, cfdp_data_len);
                break;

            case PROMPT_PDU:
                offset = dissect_cfdp_prompt_pdu(tvb, pinfo, cfdp_file_directive_header_tree, offset);
                break;

            case KEEP_ALIVE_PDU:
                offset = dissect_cfdp_keep_alive_pdu(tvb, cfdp_file_directive_header_tree, offset);
                break;

            default:
                break;
        }

    }else{
        proto_tree  *cfdp_file_data_header_tree;

        col_add_fstr(pinfo->cinfo, COL_INFO, "File Data PDU");

        cfdp_file_data_header_tree = proto_tree_add_subtree(cfdp_tree, tvb, offset, cfdp_data_len,
                                                            ett_cfdp_file_data_header, NULL, "CFDP File Data");

        proto_tree_add_item(cfdp_file_data_header_tree, hf_cfdp_file_data_offset, tvb, offset, 4, ENC_BIG_ENDIAN);

        offset += 4;

        proto_tree_add_item(cfdp_file_data_header_tree, hf_cfdp_user_data, tvb, offset, cfdp_data_len-4, ENC_NA);
        offset += cfdp_data_len-4;

    }
    if(first_byte & HDR_CRCF){
        proto_item  *cfdp_crc;
        proto_tree  *cfdp_crc_tree;

        cfdp_crc_tree = proto_tree_add_subtree(cfdp_tree, tvb, offset, 2, ett_cfdp_crc, &cfdp_crc, "CRC");

        proto_tree_add_item(cfdp_crc_tree, hf_cfdp_crc, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset += 2;

        proto_item_set_end(cfdp_crc, tvb, offset);
    }

    if ( cfdp_data_end>(guint)offset ) {
        proto_tree_add_string(cfdp_header_tree, hf_cfdp_file_data_pdu, tvb, offset, cfdp_data_len,
                              wmem_strdup_printf(pinfo->pool, "<%d bytes>", cfdp_data_len));
    }
    return;
}

void
proto_register_cfdp(void)
{
    static hf_register_info hf[] = {

        { &hf_cfdp_flags,
        { "Flags", "cfdp.flags",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_byte2,
        { "Byte2", "cfdp.byte2",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_proxy_fault_hdl_overr,
        { "Proxy Fault HDL Overr", "cfdp.proxy_fault_hdl_overr",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_proxy_trans_mode,
        { "Proxy Transmission Mode", "cfdp.proxy_trans_mode",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_proxy_segment_control_byte,
        { "Proxy Segment Control", "cfdp.proxy_segment_control",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_proxy_put_resp,
        { "Proxy Put Response", "cfdp.proxy_put_response",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_orig_trans_id,
        { "Originating Transaction ID", "cfdp.orig_trans_id",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_remote_stat_rep_req,
        { "Remote Status Report Request", "cfdp.remote_status_rep_req",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_remote_stat_rep_resp,
        { "Remote Status Report Response", "cfdp.remote_status_rep_resp",
            FT_UINT16, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_finish_pdu_flags,
        { "Finish PDU flags", "cfdp.finish_pdu_flags",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_remote_suspend_resume_req,
        { "Remote Suspend/Resume Request", "cfdp.remote_suspend_resume_req",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_remote_suspend_resume_resp,
        { "Remote Suspend/Resume Response", "cfdp.remote_suspend_resume_resp",
            FT_UINT16, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_version,
        { "Version", "cfdp.version",
            FT_UINT8, BASE_DEC, NULL, HDR_VERSION_CFDP,
            NULL, HFILL }
        },
        { &hf_cfdp_pdu_type,
            { "PDU Type", "cfdp.pdu_type",
            FT_UINT8, BASE_DEC, VALS(cfdp_pdu_type), HDR_TYPE_CFDP,
            NULL, HFILL }
        },
        { &hf_cfdp_direction,
            { "Direction", "cfdp.direction",
            FT_UINT8, BASE_DEC, VALS(cfdp_direction), HDR_DIR,
            NULL, HFILL }
        },
        { &hf_cfdp_trans_mode,
            { "Trans. Mode", "cfdp.trans_mode",
            FT_UINT8, BASE_DEC, VALS(cfdp_trans_mode), HDR_TMODE,
            NULL, HFILL }
        },
        { &hf_cfdp_crc_flag,
            { "CRC Flag", "cfdp.crc_flag",
            FT_UINT8, BASE_DEC, VALS(cfdp_crc_flag), HDR_CRCF,
            NULL, HFILL }
        },
        { &hf_cfdp_res1,
            { "Bit reserved 1", "cfdp.res1",
            FT_UINT8, BASE_DEC, NULL, HDR_RES1,
            NULL, HFILL }
        },
        { &hf_cfdp_data_length,
            { "PDU Data length", "cfdp.data_length",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        {&hf_cfdp_file_data_pdu,
         {"CFDP File PDU Data", "cfdp.file_data_pdu",
          FT_STRINGZPAD, BASE_NONE, NULL, 0x0, NULL, HFILL}
        },
        { &hf_cfdp_res2,
            { "Bit reserved 2", "cfdp.res2",
            FT_UINT8, BASE_DEC, NULL, 0x80,
            NULL, HFILL }
        },
        { &hf_cfdp_entid_length,
            { "Length of entity IDs", "cfdp.entid_length",
            FT_UINT8, BASE_DEC, NULL, 0x70,
            NULL, HFILL }
        },
        { &hf_cfdp_res3,
            { "Bit reserved 3", "cfdp.res3",
            FT_UINT8, BASE_DEC, NULL, 0x08,
            NULL, HFILL }
        },
        { &hf_cfdp_transeqnum_length,
            { "Length of Transaction sequence number", "cfdp.transeqnum_length",
            FT_UINT8, BASE_DEC, NULL, 0x07,
            NULL, HFILL }
        },
        { &hf_cfdp_srcid,
            { "Source entity ID", "cfdp.srcid",
            FT_UINT64, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_transeqnum,
            { "Transaction sequence number", "cfdp.transeqnum",
            FT_UINT64, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_dstid,
            { "Destination entity ID", "cfdp.dstid",
            FT_UINT64, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_file_directive_type,
            { "File Directive type", "cfdp.fdtype",
            FT_UINT8, BASE_DEC, VALS(cfdp_file_directive_type), 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_file_data_offset,
            { "Offset", "cfdp.offset",
            FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_progress,
            { "Progress", "cfdp.progress",
            FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_dir_code_ack,
            { "PDU acknowledged", "cfdp.dir_code_ack",
            FT_UINT8, BASE_DEC, VALS(cfdp_file_directive_type), 0xf0,
            NULL, HFILL }
        },
        { &hf_cfdp_dir_subtype_ack,
            { "Directive subtype code", "cfdp.dir_subtype_ack",
            FT_UINT8, BASE_DEC, NULL, 0x0f,
            NULL, HFILL }
        },
        { &hf_cfdp_condition_code,
            { "Condition Code", "cfdp.condition_code",
            FT_UINT8, BASE_DEC, VALS(cfdp_condition_codes), 0xf0,
            NULL, HFILL }
        },
        { &hf_cfdp_spare_one,
            { "Spare", "cfdp.spare_one",
            FT_UINT8, BASE_DEC, NULL, 0x08,
            NULL, HFILL }
        },
        { &hf_cfdp_spare_one_2,
            { "Spare", "cfdp.spare_one_2",
            FT_UINT8, BASE_DEC, NULL, 0x80,
            NULL, HFILL }
        },
        { &hf_cfdp_spare_two,
            { "Spare", "cfdp.spare_two",
            FT_UINT8, BASE_DEC, NULL, 0x0c,
            NULL, HFILL }
        },
        { &hf_cfdp_spare_four,
            { "Spare", "cfdp.spare_four",
            FT_UINT8, BASE_DEC, NULL, 0x0f,
            NULL, HFILL }
        },
        { &hf_cfdp_spare_five,
            { "Spare", "cfdp.spare_five_b",
            FT_UINT16, BASE_DEC, NULL, 0x3E00,
            NULL, HFILL }
        },
        { &hf_cfdp_spare_seven,
            { "Spare", "cfdp.spare_seven",
            FT_UINT8, BASE_DEC, NULL, 0x7f,
            NULL, HFILL }
        },
        { &hf_cfdp_spare_seven_2,
            { "Spare", "cfdp.spare_seven_2",
            FT_UINT8, BASE_DEC, NULL, 0xfe,
            NULL, HFILL }
        },
        { &hf_cfdp_trans_stat_ack,
            { "Transaction status", "cfdp.trans_stat_ack",
            FT_UINT8, BASE_DEC, VALS(cfdp_trans_stat_ack), 0x03,
            NULL, HFILL }
        },
        { &hf_cfdp_trans_stat,
            { "Transaction status B", "cfdp.trans_stat_b",
            FT_UINT16, BASE_DEC, VALS(cfdp_trans_stat_ack), 0xC000,
            NULL, HFILL }
        },
        { &hf_cfdp_trans_stat_2,
            { "Transaction status", "cfdp.trans_stat_2_b",
            FT_UINT8, BASE_DEC, VALS(cfdp_trans_stat_ack), 0x60,
            NULL, HFILL }
        },
        { &hf_cfdp_file_checksum,
            { "Checksum", "cfdp.checksum",
            FT_UINT32, BASE_HEX, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_file_size,
            { "File size", "cfdp.file_size",
            FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_end_system_stat,
            { "End system status", "cfdp.end_system_stat",
            FT_UINT8, BASE_DEC, VALS(cfdp_end_system_stat), 0x08,
            NULL, HFILL }
        },
        { &hf_cfdp_delivery_code,
            { "Delivery code", "cfdp.delivery_code",
            FT_UINT8, BASE_DEC, VALS(cfdp_delivery_code), 0x04,
            NULL, HFILL }
        },
        { &hf_cfdp_file_stat,
            { "File status", "cfdp.file_status",
            FT_UINT8, BASE_DEC, VALS(cfdp_file_stat), 0x03,
            NULL, HFILL }
        },
        { &hf_cfdp_segment_control,
            { "Segmentation control", "cfdp.segment_control",
            FT_UINT8, BASE_DEC, VALS(cfdp_segment_control), 0x80,
            NULL, HFILL }
        },
        { &hf_cfdp_tlv_len,
            { "Length", "cfdp.tlv_length",
            FT_UINT8, BASE_DEC, NULL, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_src_file_name_len,
            {"Length of source file name", "cfdp.src_file_name_len", FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_src_file_name,
            {"Source file name", "cfdp.src_file_name", FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_dst_file_name_len,
            {"Length of destination file name", "cfdp.dst_file_name_len", FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_dst_file_name,
            {"Destination file name", "cfdp.dst_file_name", FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_first_file_name_len,
            {"Length of first file name", "cfdp.first_file_name_len", FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_first_file_name,
            {"First file name", "cfdp.first_file_name", FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_second_file_name_len,
            {"Length of second file name", "cfdp.second_file_name_len", FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_second_file_name,
            {"Second file name", "cfdp.second_file_name", FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_nak_st_scope,
            {"Start of scope", "cfdp.nak_st_scope", FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_nak_sp_scope,
            {"End of scope", "cfdp.nak_sp_scope", FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_crc,
            {"CRC", "cfdp.crc", FT_UINT16, BASE_HEX, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_action_code,
            {"Action code", "cfdp.action_code", FT_UINT8, BASE_DEC, VALS(cfdp_action_code), 0xF0,
            NULL, HFILL}
        },
        { &hf_cfdp_status_code_1,
            {"Status code", "cfdp.status_code_1", FT_UINT8, BASE_DEC, VALS(cfdp_status_code_1), 0x0F,
            NULL, HFILL}
        },
        { &hf_cfdp_status_code_2,
            {"Status code", "cfdp.status_code_2", FT_UINT8, BASE_DEC, VALS(cfdp_status_code_2), 0x0F,
            NULL, HFILL}
        },
        { &hf_cfdp_status_code_3,
            {"Status code", "cfdp.status_code_3", FT_UINT8, BASE_DEC, VALS(cfdp_status_code_3), 0x0F,
            NULL, HFILL}
        },
        { &hf_cfdp_status_code_4,
            {"Status code", "cfdp.status_code_4", FT_UINT8, BASE_DEC, VALS(cfdp_status_code_4), 0x0F,
            NULL, HFILL}
        },
        { &hf_cfdp_status_code_5,
            {"Status code", "cfdp.status_code_5", FT_UINT8, BASE_DEC, VALS(cfdp_status_code_5), 0x0F,
            NULL, HFILL}
        },
        { &hf_cfdp_status_code_6,
            {"Status code", "cfdp.status_code_6", FT_UINT8, BASE_DEC, VALS(cfdp_status_code_6), 0x0F,
            NULL, HFILL}
        },
        { &hf_cfdp_status_code_7,
            {"Status code", "cfdp.status_code_7", FT_UINT8, BASE_DEC, VALS(cfdp_status_code_7), 0x0F,
            NULL, HFILL}
        },
        { &hf_cfdp_status_code_8,
            {"Status code", "cfdp.status_code_8", FT_UINT8, BASE_DEC, VALS(cfdp_status_code_8), 0x0F,
            NULL, HFILL}
        },
        { &hf_cfdp_handler_code,
            { "Handler Code", "cfdp.handler_code",
            FT_UINT8, BASE_DEC, VALS(cfdp_handler_codes), 0x0F,
            NULL, HFILL }
        },
        { &hf_cfdp_proxy_msg_type,
            { "Proxy Message Type", "cfdp.proxy_msg_type",
            FT_UINT8, BASE_DEC | BASE_EXT_STRING, &cfdp_proxy_msg_type_ext, 0x0,
            NULL, HFILL }
        },
        { &hf_cfdp_proxy_segment_control,
            { "Segmentation control", "cfdp.proxy_segment_control",
            FT_UINT8, BASE_DEC, VALS(cfdp_segment_control), 0x01,
            NULL, HFILL }
        },
        { &hf_cfdp_proxy_delivery_code,
            { "Delivery code", "cfdp.proxy_delivery_code",
            FT_UINT8, BASE_DEC, VALS(cfdp_delivery_code), 0x04,
            NULL, HFILL }
        },
        { &hf_cfdp_response_req,
            { "Response required", "cfdp.response_req",
            FT_UINT8, BASE_DEC, VALS(cfdp_response_req), 0x80,
            NULL, HFILL }
        },
        { &hf_cfdp_directory_name,
            {"Directory Name", "cfdp.directory_name", FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_directory_file_name,
            {"Directory File Name", "cfdp.directory_file_name", FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_listing_resp_code,
            {"Listing Response Code", "cfdp.listing_resp_code",
            FT_UINT8, BASE_DEC, VALS(cfdp_listing_resp_code), 0x80,
            NULL, HFILL}
        },
        { &hf_cfdp_report_file_name,
            {"Report File Name", "cfdp.report_file_name", FT_STRING, BASE_NONE, NULL, 0x0,
            NULL, HFILL}
        },
        { &hf_cfdp_rep_resp_code,
            {"Report Response Code", "cfdp.rep_resp_code_b",
            FT_UINT16, BASE_DEC, VALS(cfdp_rep_resp_code), 0x0100,
            NULL, HFILL}
        },
        { &hf_cfdp_suspension_ind,
            {"Suspension indicator", "cfdp.suspension_ind_b",
            FT_UINT8, BASE_DEC, VALS(cfdp_suspension_ind), 0x80,
            NULL, HFILL}
        },
        { &hf_cfdp_filestore_message_len,
            {"Length of filestore message", "cfdp.filestore_message_len", FT_UINT32, BASE_DEC, NULL, 0x0,
            NULL, HFILL}
        },

        /* Generated from convert_proto_tree_add_text.pl */
        { &hf_cfdp_filestore_message, { "Filestore Message", "cfdp.filestore_message", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
        { &hf_cfdp_entity, { "Entity", "cfdp.entity", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
        { &hf_cfdp_message_to_user, { "Message to User", "cfdp.message_to_user", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
        { &hf_cfdp_flow_label, { "Flow label", "cfdp.flow_label", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
        { &hf_cfdp_segment_requests, { "Segment requests", "cfdp.segment_requests", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
        { &hf_cfdp_user_data, { "User Data", "cfdp.user_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},

    };

    /* Setup protocol subtree array */
    static gint *ett[] = {
        &ett_cfdp,
        &ett_cfdp_flags,
        &ett_cfdp_byte2,
        &ett_cfdp_proxy_fault_hdl_overr,
        &ett_cfdp_proxy_trans_mode,
        &ett_cfdp_proxy_segment_control_byte,
        &ett_cfdp_proxy_put_resp,
        &ett_cfdp_orig_trans_id,
        &ett_cfdp_remote_suspend_resume_req,
        &ett_cfdp_remote_suspend_resume_resp,
        &ett_cfdp_remote_stat_rep_req,
        &ett_cfdp_remote_stat_rep_resp,
        &ett_cfdp_finish_pdu_flags,
        &ett_cfdp_header,
        &ett_cfdp_file_directive_header,
        &ett_cfdp_file_data_header,
        &ett_cfdp_fault_location,
        &ett_cfdp_crc,
        &ett_cfdp_filestore_req,
        &ett_cfdp_filestore_resp,
        &ett_cfdp_msg_to_user,
        &ett_cfdp_fault_hdl_overr,
        &ett_cfdp_flow_label,
        &ett_cfdp_proto
    };

    static ei_register_info ei[] = {
        { &ei_cfdp_bad_length, { "cfdp.bad_length", PI_MALFORMED, PI_ERROR, "Bad length field", EXPFILL }},
    };

    expert_module_t* expert_cfdp;

    /* Register the protocol name and description */
    proto_cfdp = proto_register_protocol("CFDP", "CFDP", "cfdp");

    /* Required function calls to register the header fields and subtrees used */
    proto_register_field_array(proto_cfdp, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    expert_cfdp = expert_register_protocol(proto_cfdp);
    expert_register_field_array(expert_cfdp, ei, array_length(ei));

    cfdp_handle = register_dissector("cfdp", dissect_cfdp, proto_cfdp);
}

void
proto_reg_handoff_cfdp(void)
{
    dissector_add_uint("ccsds.apid", CFDP_APID, cfdp_handle);
    dissector_add_for_decode_as_with_preference("udp.port", cfdp_handle);
}

/*
 * Editor modelines - https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */