aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-bpv7.c
blob: 1b2d58a64a7236e56a110a662196d305cf51c8c6 (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
/* packet-bpv7.c
 * Routines for Bundle Protocol Version 7 dissection
 * References:
 *     BPv7: https://datatracker.ietf.org/doc/html/draft-ietf-dtn-bpbis-31
 *
 * Copyright 2019-2021, Brian Sipos <brian.sipos@gmail.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */
#include "config.h"

#include "packet-bpv7.h"
#include "epan/wscbor.h"
#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/proto.h>
#include <epan/expert.h>
#include <epan/to_str.h>
#include <epan/reassemble.h>
#include <epan/exceptions.h>
#include <epan/ftypes/ftypes.h>
#include <wsutil/crc16.h>
#include <wsutil/crc32.h>
#include <wsutil/utf8_entities.h>
#include <stdio.h>
#include <inttypes.h>

/// Glib logging "domain" name
static const char *LOG_DOMAIN = "bpv7";
/// Protocol column name
static const char *const proto_name_bp = "BPv7";
static const char *const proto_name_bp_admin = "BPv7 Admin";

/// Protocol preferences and defaults
static gboolean bp_compute_crc = TRUE;
static gboolean bp_reassemble_payload = TRUE;
static gboolean bp_payload_try_heur = FALSE;

/// Protocol handles
static int proto_bp = -1;
static int proto_bp_admin = -1;
/// Protocol-level data
static bp_history_t *bp_history = NULL;

static dissector_handle_t handle_admin = NULL;
/// Dissect opaque CBOR data
static dissector_handle_t handle_cbor = NULL;
static dissector_handle_t handle_cborseq = NULL;
/// Extension sub-dissectors
static dissector_table_t block_dissectors = NULL;
static dissector_table_t payload_dissectors_dtn_wkssp = NULL;
static dissector_table_t payload_dissectors_dtn_serv = NULL;
static dissector_table_t admin_dissectors = NULL;
/// BTSD heuristic
static heur_dissector_list_t btsd_heur = NULL;

/// Fragment reassembly
static reassembly_table bp_reassembly_table;

static const val64_string eid_schemes[] = {
    {EID_SCHEME_DTN, "dtn"},
    {EID_SCHEME_IPN, "ipn"},
    {0, NULL},
};

static const val64_string crc_vals[] = {
    {BP_CRC_NONE, "None"},
    {BP_CRC_16, "CRC-16"},
    {BP_CRC_32, "CRC-32C"},
    {0, NULL},
};

static const val64_string blocktype_vals[] = {
    {BP_BLOCKTYPE_PAYLOAD, "Payload"},
    {BP_BLOCKTYPE_PREV_NODE, "Previous Node"},
    {BP_BLOCKTYPE_BUNDLE_AGE, "Bundle Age"},
    {BP_BLOCKTYPE_HOP_COUNT, "Hop Count"},
    {BP_BLOCKTYPE_BIB, "Block Integrity Block"},
    {BP_BLOCKTYPE_BCB, "Block Confidentiality Block"},
    {0, NULL},
};

typedef struct {
    /// Type of block
    guint64 type_code;
    /// Limit on total count
    guint64 limit;
} blocktype_limit;
/// Block type count limits
static const blocktype_limit blocktype_limits[] = {
    {BP_BLOCKTYPE_PAYLOAD, 1},
    {BP_BLOCKTYPE_PREV_NODE, 1},
    {BP_BLOCKTYPE_BUNDLE_AGE, 1},
    {BP_BLOCKTYPE_HOP_COUNT, 1},
    // Mandatory last row
    {BP_BLOCKTYPE_INVALID, 0},
};

static const val64_string admin_type_vals[] = {
    {BP_ADMINTYPE_BUNDLE_STATUS, "Bundle Status Report"},
    {0, NULL},
};

static const val64_string status_report_reason_vals[] = {
    {0, "No additional information"},
    {1, "Lifetime expired"},
    {2, "Forwarded over unidirectional link"},
    {3, "Transmission canceled"},
    {4, "Depleted storage"},
    {5, "Destination endpoint ID unintelligible"},
    {6, "No known route to destination from here"},
    {7, "No timely contact with next node on route"},
    {8, "Block unintelligible"},
    {9, "Hop limit exceeded"},
    {10, "Traffic pared"},
    {11, "Block unsupported"},
    {12, "Missing Security Operation"},
    {13, "Unknown Security Operation"},
    {14, "Unexpected Security Operation"},
    {15, "Failed Security Operation"},
    {16, "Conflicting Security Operation"},
    {0, NULL},
};

static int hf_bundle_head = -1;
static int hf_bundle_break = -1;
static int hf_block = -1;

static int hf_crc_type = -1;
static int hf_crc_field_uint16 = -1;
static int hf_crc_field_uint32 = -1;
static int hf_crc_status = -1;

static int hf_time_dtntime = -1;
static int hf_time_utctime = -1;

static int hf_create_ts_time = -1;
static int hf_create_ts_seqno = -1;

static int hf_eid_scheme = -1;
static int hf_eid_dtn_ssp_code = -1;
static int hf_eid_dtn_ssp_text = -1;
static int hf_eid_ipn_node = -1;
static int hf_eid_ipn_service = -1;
static int hf_eid_dtn_wkssp = -1;
static int hf_eid_dtn_serv = -1;

static int hf_primary_version = -1;
static int hf_primary_bundle_flags = -1;
static int hf_primary_bundle_flags_deletion_report = -1;
static int hf_primary_bundle_flags_delivery_report = -1;
static int hf_primary_bundle_flags_forwarding_report = -1;
static int hf_primary_bundle_flags_reception_report = -1;
static int hf_primary_bundle_flags_req_status_time = -1;
static int hf_primary_bundle_flags_user_app_ack = -1;
static int hf_primary_bundle_flags_no_fragment = -1;
static int hf_primary_bundle_flags_payload_admin = -1;
static int hf_primary_bundle_flags_is_fragment = -1;
static int hf_primary_dst_eid = -1;
static int hf_primary_dst_uri = -1;
static int hf_primary_src_nodeid = -1;
static int hf_primary_src_uri = -1;
static int hf_primary_report_nodeid = -1;
static int hf_primary_report_uri = -1;
static int hf_primary_create_ts = -1;
static int hf_primary_lifetime = -1;
static int hf_primary_lifetime_exp = -1;
static int hf_primary_expire_ts = -1;
static int hf_primary_frag_offset = -1;
static int hf_primary_total_length = -1;

static int hf_bundle_ident = -1;
static int hf_bundle_seen = -1;
static int hf_bundle_seen_time_diff = -1;
static int hf_bundle_status_ref = -1;

static int hf_canonical_type_code = -1;
static int hf_canonical_block_num = -1;
static int hf_canonical_block_flags = -1;
static int hf_canonical_block_flags_delete_no_process = -1;
static int hf_canonical_block_flags_status_no_process = -1;
static int hf_canonical_block_flags_remove_no_process = -1;
static int hf_canonical_block_flags_replicate_in_fragment = -1;
static int hf_canonical_data = -1;

static int hf_previous_node_nodeid = -1;
static int hf_previous_node_uri = -1;
static int hf_bundle_age_time = -1;
static int hf_hop_count_limit = -1;
static int hf_hop_count_current = -1;

static int hf_admin_record_type = -1;
static int hf_status_rep = -1;
static int hf_status_rep_status_info = -1;
static int hf_status_assert_val = -1;
static int hf_status_assert_time = -1;
static int hf_status_rep_received = -1;
static int hf_status_rep_forwarded = -1;
static int hf_status_rep_delivered = -1;
static int hf_status_rep_deleted = -1;
static int hf_status_rep_reason_code = -1;
static int hf_status_rep_subj_src_nodeid = -1;
static int hf_status_rep_subj_src_uri = -1;
static int hf_status_rep_subj_ts = -1;
static int hf_status_rep_subj_frag_offset = -1;
static int hf_status_rep_subj_payload_len = -1;
static int hf_status_rep_subj_ident = -1;
static int hf_status_rep_subj_ref = -1;
static int hf_status_time_diff = -1;

static int hf_payload_fragments = -1;
static int hf_payload_fragment = -1;
static int hf_payload_fragment_overlap = -1;
static int hf_payload_fragment_overlap_conflicts = -1;
static int hf_payload_fragment_multiple_tails = -1;
static int hf_payload_fragment_too_long_fragment = -1;
static int hf_payload_fragment_error = -1;
static int hf_payload_fragment_count = -1;
static int hf_payload_reassembled_in = -1;
static int hf_payload_reassembled_length = -1;
static int hf_payload_reassembled_data = -1;
static gint ett_payload_fragment = -1;
static gint ett_payload_fragments = -1;

/// Field definitions
static hf_register_info fields[] = {
    {&hf_bundle_head, {"Indefinite Array", "bpv7.bundle_head", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_bundle_break, {"Indefinite Break", "bpv7.bundle_break", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL}},

    {&hf_block, {"Block", "bpv7.block", FT_PROTOCOL, BASE_NONE, NULL, 0x0, NULL, HFILL}},

    {&hf_crc_type, {"CRC Type", "bpv7.crc_type", FT_UINT64, BASE_DEC | BASE_VAL64_STRING, VALS64(crc_vals), 0x0, NULL, HFILL}},
    {&hf_crc_field_uint16, {"CRC Field Integer", "bpv7.crc_field", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL}},
    {&hf_crc_field_uint32, {"CRC field Integer", "bpv7.crc_field", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}},
    {&hf_crc_status, {"CRC Status", "bpv7.crc_status", FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0, NULL, HFILL}},

    {&hf_time_dtntime, {"DTN Time", "bpv7.time.dtntime", FT_UINT64, BASE_DEC | BASE_UNIT_STRING, &units_milliseconds, 0x0, NULL, HFILL}},
    {&hf_time_utctime, {"UTC Time", "bpv7.time.utctime", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_UTC, NULL, 0x0, NULL, HFILL}},

    {&hf_create_ts_time, {"Time", "bpv7.create_ts.time", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_create_ts_seqno, {"Sequence Number", "bpv7.create_ts.seqno", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_eid_scheme, {"Scheme Code", "bpv7.eid.scheme", FT_UINT64, BASE_DEC | BASE_VAL64_STRING, VALS64(eid_schemes), 0x0, NULL, HFILL}},
    {&hf_eid_dtn_ssp_code, {"DTN SSP", "bpv7.eid.dtn_ssp_code", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_eid_dtn_ssp_text, {"DTN SSP", "bpv7.eid.dtn_ssp_text", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},
    {&hf_eid_ipn_node, {"IPN Node Number", "bpv7.eid.ipn_node", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_eid_ipn_service, {"IPN Service Number", "bpv7.eid.ipn_service", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_eid_dtn_wkssp, {"Well-known SSP", "bpv7.eid.wkssp", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},
    {&hf_eid_dtn_serv, {"Service Name", "bpv7.eid.serv", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},

    {&hf_primary_version, {"Version", "bpv7.primary.version", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_bundle_flags, {"Bundle Flags", "bpv7.primary.bundle_flags", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_bundle_flags_is_fragment, {"Bundle is a fragment", "bpv7.primary.bundle_flags.is_fragment", FT_BOOLEAN, 24, TFS(&tfs_set_notset), BP_BUNDLE_IS_FRAGMENT, NULL, HFILL}},
    {&hf_primary_bundle_flags_payload_admin, {"Payload is an administrative record", "bpv7.primary.bundle_flags.payload_admin", FT_BOOLEAN, 24, TFS(&tfs_set_notset), BP_BUNDLE_PAYLOAD_ADMIN, NULL, HFILL}},
    {&hf_primary_bundle_flags_no_fragment, {"Bundle must not be fragmented", "bpv7.primary.bundle_flags.no_fragment", FT_BOOLEAN, 24, TFS(&tfs_set_notset), BP_BUNDLE_NO_FRAGMENT, NULL, HFILL}},
    {&hf_primary_bundle_flags_user_app_ack, {"Acknowledgement by application is requested", "bpv7.primary.bundle_flags.user_app_ack", FT_BOOLEAN, 24, TFS(&tfs_set_notset), BP_BUNDLE_USER_APP_ACK, NULL, HFILL}},
    {&hf_primary_bundle_flags_req_status_time, {"Status time requested in reports", "bpv7.primary.bundle_flags.req_status_time", FT_BOOLEAN, 24, TFS(&tfs_set_notset), BP_BUNDLE_REQ_STATUS_TIME, NULL, HFILL}},
    {&hf_primary_bundle_flags_reception_report, {"Request reporting of bundle reception", "bpv7.primary.bundle_flags.reception_report", FT_BOOLEAN, 24, TFS(&tfs_set_notset), BP_BUNDLE_REQ_RECEPTION_REPORT, NULL, HFILL}},
    {&hf_primary_bundle_flags_forwarding_report, {"Request reporting of bundle forwarding", "bpv7.primary.bundle_flags.forwarding_report", FT_BOOLEAN, 24, TFS(&tfs_set_notset), BP_BUNDLE_REQ_FORWARDING_REPORT, NULL, HFILL}},
    {&hf_primary_bundle_flags_delivery_report, {"Request reporting of bundle delivery", "bpv7.primary.bundle_flags.delivery_report", FT_BOOLEAN, 24, TFS(&tfs_set_notset), BP_BUNDLE_REQ_DELIVERY_REPORT, NULL, HFILL}},
    {&hf_primary_bundle_flags_deletion_report, {"Request reporting of bundle deletion", "bpv7.primary.bundle_flags.deletion_report", FT_BOOLEAN, 24, TFS(&tfs_set_notset), BP_BUNDLE_REQ_DELETION_REPORT, NULL, HFILL}},
    {&hf_primary_dst_eid, {"Destination Endpoint ID", "bpv7.primary.dst_eid", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_dst_uri, {"Destination URI", "bpv7.primary.dst_uri", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_src_nodeid, {"Source Node ID", "bpv7.primary.src_nodeid", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_src_uri, {"Source URI", "bpv7.primary.src_uri", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_report_nodeid, {"Report-to Node ID", "bpv7.primary.report_nodeid", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_report_uri, {"Report-to URI", "bpv7.primary.report_uri", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_create_ts, {"Creation Timestamp", "bpv7.primary.create_ts", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_lifetime, {"Lifetime", "bpv7.primary.lifetime", FT_UINT64, BASE_DEC | BASE_UNIT_STRING, &units_milliseconds, 0x0, NULL, HFILL}},
    {&hf_primary_lifetime_exp, {"Lifetime Expanded", "bpv7.primary.lifetime_exp", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_expire_ts, {"Expire Time", "bpv7.primary.expire_time", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_UTC, NULL, 0x0, NULL, HFILL}},
    {&hf_primary_frag_offset, {"Fragment Offset", "bpv7.primary.frag_offset", FT_UINT64, BASE_DEC | BASE_UNIT_STRING, &units_octet_octets, 0x0, NULL, HFILL}},
    {&hf_primary_total_length, {"Total Application Data Unit Length", "bpv7.primary.total_len", FT_UINT64, BASE_DEC | BASE_UNIT_STRING, &units_octet_octets, 0x0, NULL, HFILL}},

    {&hf_bundle_ident, {"Bundle Identity", "bpv7.bundle.identity", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},
    {&hf_bundle_seen, {"First Seen", "bpv7.bundle.first_seen", FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_RETRANS_PREV), 0x0, NULL, HFILL}},
    {&hf_bundle_seen_time_diff, {"Seen Time", "bpv7.bundle.seen_time_diff", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_bundle_status_ref, {"Status Bundle", "bpv7.bundle.status_ref", FT_FRAMENUM, BASE_NONE, NULL, 0x0, NULL, HFILL}},

    {&hf_canonical_type_code, {"Type Code", "bpv7.canonical.type_code", FT_UINT64, BASE_DEC | BASE_VAL64_STRING, VALS64(blocktype_vals), 0x0, NULL, HFILL}},
    {&hf_canonical_block_num, {"Block Number", "bpv7.canonical.block_num", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_canonical_block_flags, {"Block Flags", "bpv7.canonical.block_flags", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL}},
    {&hf_canonical_block_flags_replicate_in_fragment, {"Replicate block in fragment", "bpv7.canonical.block_flags.replicate_in_fragment", FT_BOOLEAN, 8, TFS(&tfs_set_notset), BP_BLOCK_REPLICATE_IN_FRAGMENT, NULL, HFILL}},
    {&hf_canonical_block_flags_status_no_process, {"Status bundle if not processed", "bpv7.canonical.block_flags.status_if_no_process", FT_BOOLEAN, 8, TFS(&tfs_set_notset), BP_BLOCK_STATUS_IF_NO_PROCESS, NULL, HFILL}},
    {&hf_canonical_block_flags_delete_no_process, {"Delete bundle if not processed", "bpv7.canonical.block_flags.delete_if_no_process", FT_BOOLEAN, 8, TFS(&tfs_set_notset), BP_BLOCK_DELETE_IF_NO_PROCESS, NULL, HFILL}},
    {&hf_canonical_block_flags_remove_no_process, {"Discard block if not processed", "bpv7.canonical.block_flags.discard_if_no_process", FT_BOOLEAN, 8, TFS(&tfs_set_notset), BP_BLOCK_REMOVE_IF_NO_PROCESS, NULL, HFILL}},
    {&hf_canonical_data, {"Block Type-Specific Data", "bpv7.canonical.data", FT_UINT64, BASE_DEC | BASE_UNIT_STRING, &units_octet_octets, 0x0, NULL, HFILL}},

    {&hf_payload_fragments,
        {"Payload fragments", "bpv7.payload.fragments",
        FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_fragment,
        {"Payload fragment", "bpv7.payload.fragment",
        FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_fragment_overlap,
        {"Payload fragment overlap", "bpv7.payload.fragment.overlap",
        FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_fragment_overlap_conflicts,
        {"Payload fragment overlapping with conflicting data",
        "bpv7.payload.fragment.overlap.conflicts",
        FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_fragment_multiple_tails,
        {"Message has multiple tail fragments",
        "bpv7.payload.fragment.multiple_tails",
        FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_fragment_too_long_fragment,
        {"Payload fragment too long", "bpv7.payload.fragment.too_long_fragment",
        FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_fragment_error,
        {"Payload defragmentation error", "bpv7.payload.fragment.error",
        FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_fragment_count,
        {"Payload fragment count", "bpv7.payload.fragment.count",
        FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_reassembled_in,
        {"Reassembled in", "bpv7.payload.reassembled.in",
        FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_reassembled_length,
        {"Reassembled length", "bpv7.payload.reassembled.length",
        FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
    {&hf_payload_reassembled_data,
        {"Reassembled data", "bpv7.payload.reassembled.data",
        FT_BYTES, BASE_NONE, NULL, 0x00, NULL, HFILL } },

    {&hf_previous_node_nodeid, {"Previous Node ID", "bpv7.previous_node.nodeid", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_previous_node_uri, {"Previous URI", "bpv7.previous_node.uri", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},

    {&hf_bundle_age_time, {"Bundle Age", "bpv7.bundle_age.time", FT_UINT64, BASE_DEC | BASE_UNIT_STRING, &units_milliseconds, 0x0, NULL, HFILL}},

    {&hf_hop_count_limit, {"Hop Limit", "bpv7.hop_count.limit", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_hop_count_current, {"Hop Count", "bpv7.hop_count.current", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},

    {&hf_admin_record_type, {"Record Type Code", "bpv7.admin_rec.type_code", FT_UINT64, BASE_DEC | BASE_VAL64_STRING, VALS64(admin_type_vals), 0x0, NULL, HFILL}},

    {&hf_status_rep, {"Status Report", "bpv7.status_rep", FT_PROTOCOL, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_status_info, {"Status Information", "bpv7.status_rep.status_info", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_assert_val, {"Status Value", "bpv7.status_assert.val", FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_assert_time, {"Status at", "bpv7.status_assert.time", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_received, {"Reporting node received bundle", "bpv7.status_rep.received", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_forwarded, {"Reporting node forwarded bundle", "bpv7.status_rep.forwarded", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_delivered, {"Reporting node delivered bundle", "bpv7.status_rep.delivered", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_deleted, {"Reporting node deleted bundle", "bpv7.status_rep.deleted", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_reason_code, {"Reason Code", "bpv7.status_rep.reason_code", FT_UINT64, BASE_DEC | BASE_VAL64_STRING, VALS64(status_report_reason_vals), 0x0, NULL, HFILL}},
    {&hf_status_rep_subj_src_nodeid, {"Subject Source Node ID", "bpv7.status_rep.subj_src_nodeid", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_subj_src_uri, {"Subject Source URI", "bpv7.status_rep.subj_src_uri", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_subj_ts, {"Subject Creation Timestamp", "bpv7.status_rep.subj_ts", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_subj_frag_offset, {"Subject Fragment Offset", "bpv7.status_rep.subj_frag_offset", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_subj_payload_len, {"Subject Payload Length", "bpv7.status_rep.subj_payload_len", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_subj_ident, {"Subject Identity", "bpv7.status_rep.identity", FT_STRING, STR_UNICODE, NULL, 0x0, NULL, HFILL}},
    {&hf_status_rep_subj_ref, {"Subject Bundle", "bpv7.status_rep.subj_ref", FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_ACK), 0x0, NULL, HFILL}},
    {&hf_status_time_diff, {"Status Time", "bpv7.status_rep.subj_time_diff", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0, NULL, HFILL}},
};

static int *const bundle_flags[] = {
    &hf_primary_bundle_flags_deletion_report,
    &hf_primary_bundle_flags_delivery_report,
    &hf_primary_bundle_flags_forwarding_report,
    &hf_primary_bundle_flags_reception_report,
    &hf_primary_bundle_flags_req_status_time,
    &hf_primary_bundle_flags_user_app_ack,
    &hf_primary_bundle_flags_no_fragment,
    &hf_primary_bundle_flags_payload_admin,
    &hf_primary_bundle_flags_is_fragment,
    NULL
};

static int *const block_flags[] = {
    &hf_canonical_block_flags_remove_no_process,
    &hf_canonical_block_flags_delete_no_process,
    &hf_canonical_block_flags_status_no_process,
    &hf_canonical_block_flags_replicate_in_fragment,
    NULL
};

static int ett_bundle = -1;
static int ett_bundle_flags = -1;
static int ett_block = -1;
static int ett_eid = -1;
static int ett_time = -1;
static int ett_create_ts = -1;
static int ett_block_flags = -1;
static int ett_canonical_data = -1;
static int ett_payload = -1;
static int ett_admin = -1;
static int ett_status_rep = -1;
static int ett_status_info = -1;
static int ett_status_assert = -1;
/// Tree structures
static int *ett[] = {
    &ett_bundle,
    &ett_bundle_flags,
    &ett_block,
    &ett_eid,
    &ett_time,
    &ett_create_ts,
    &ett_block_flags,
    &ett_canonical_data,
    &ett_payload,
    &ett_admin,
    &ett_status_rep,
    &ett_status_info,
    &ett_status_assert,
    &ett_payload_fragment,
    &ett_payload_fragments,
};

static const fragment_items payload_frag_items = {
    /* Fragment subtrees */
    &ett_payload_fragment,
    &ett_payload_fragments,
    /* Fragment fields */
    &hf_payload_fragments,
    &hf_payload_fragment,
    &hf_payload_fragment_overlap,
    &hf_payload_fragment_overlap_conflicts,
    &hf_payload_fragment_multiple_tails,
    &hf_payload_fragment_too_long_fragment,
    &hf_payload_fragment_error,
    &hf_payload_fragment_count,
    /* Reassembled in field */
    &hf_payload_reassembled_in,
    &hf_payload_reassembled_length,
    &hf_payload_reassembled_data,
    /* Tag */
    "Payload fragments"
};

static expert_field ei_invalid_framing = EI_INIT;
static expert_field ei_invalid_bp_version = EI_INIT;
static expert_field ei_eid_scheme_unknown = EI_INIT;
static expert_field ei_eid_ssp_type_invalid = EI_INIT;
static expert_field ei_eid_wkssp_unknown = EI_INIT;
static expert_field ei_block_type_dupe = EI_INIT;
static expert_field ei_sub_type_unknown = EI_INIT;
static expert_field ei_sub_partial_decode = EI_INIT;
static expert_field ei_crc_type_unknown = EI_INIT;
static expert_field ei_block_failed_crc = EI_INIT;
static expert_field ei_block_num_dupe = EI_INIT;
static expert_field ei_block_payload_index = EI_INIT;
static expert_field ei_block_payload_num = EI_INIT;
static expert_field ei_fragment_reassemble_size = EI_INIT;
static expert_field ei_fragment_tot_mismatch = EI_INIT;
static expert_field ei_block_sec_bib_tgt = EI_INIT;
static expert_field ei_block_sec_bcb_tgt = EI_INIT;
static ei_register_info expertitems[] = {
    {&ei_invalid_framing, {"bpv7.invalid_framing", PI_MALFORMED, PI_WARN, "Invalid framing", EXPFILL}},
    {&ei_invalid_bp_version, {"bpv7.invalid_bp_version", PI_MALFORMED, PI_ERROR, "Invalid BP version", EXPFILL}},
    {&ei_eid_scheme_unknown, {"bpv7.eid_scheme_unknown", PI_UNDECODED, PI_WARN, "Unknown Node ID scheme code", EXPFILL}},
    {&ei_eid_ssp_type_invalid, {"bpv7.eid_ssp_type_invalid", PI_UNDECODED, PI_WARN, "Invalid scheme-specific part major type", EXPFILL}},
    {&ei_eid_wkssp_unknown, {"bpv7.eid_wkssp_unknown", PI_UNDECODED, PI_WARN, "Unknown well-known scheme-specific code point", EXPFILL}},
    {&ei_block_type_dupe, {"bpv7.block_type_dupe", PI_PROTOCOL, PI_WARN, "Too many blocks of this type", EXPFILL}},
    {&ei_sub_type_unknown, {"bpv7.sub_type_unknown", PI_UNDECODED, PI_WARN, "Unknown type code", EXPFILL}},
    {&ei_sub_partial_decode, {"bpv7.sub_partial_decode", PI_UNDECODED, PI_WARN, "Data not fully dissected", EXPFILL}},
    {&ei_crc_type_unknown, {"bpv7.crc_type_unknown", PI_UNDECODED, PI_WARN, "Unknown CRC Type code", EXPFILL}},
    {&ei_block_failed_crc, {"bpv7.block_failed_crc", PI_CHECKSUM, PI_WARN, "Block failed CRC", EXPFILL}},
    {&ei_block_num_dupe, {"bpv7.block_num_dupe", PI_PROTOCOL, PI_WARN, "Duplicate block number", EXPFILL}},
    {&ei_block_payload_index, {"bpv7.block_payload_index", PI_PROTOCOL, PI_WARN, "Payload must be the last block", EXPFILL}},
    {&ei_block_payload_num, {"bpv7.block_payload_num", PI_PROTOCOL, PI_WARN, "Invalid payload block number", EXPFILL}},
    {&ei_fragment_reassemble_size, {"bpv7.fragment_reassemble_size", PI_REASSEMBLE, PI_ERROR, "Cannot defragment this size (wireshark limitation)", EXPFILL}},
    {&ei_fragment_tot_mismatch, {"bpv7.fragment_tot_mismatch", PI_REASSEMBLE, PI_ERROR, "Inconsistent total length between fragments", EXPFILL}},
    {&ei_block_sec_bib_tgt, {"bpv7.bpsec.bib_target", PI_COMMENTS_GROUP, PI_COMMENT, "Block is an integrity target", EXPFILL}},
    {&ei_block_sec_bcb_tgt, {"bpv7.bpsec.bcb_target", PI_COMMENTS_GROUP, PI_COMMENT, "Block is a confidentiality target", EXPFILL}},
};

/** Delete an arbitrary object allocated under this file scope.
 *
 * @param obj The object to delete.
 */
static void file_scope_delete(gpointer ptr) {
    wmem_free(wmem_file_scope(), ptr);
}

static bp_creation_ts_t * bp_creation_ts_new(wmem_allocator_t *alloc) {
    bp_creation_ts_t *obj = wmem_new0(alloc, bp_creation_ts_t);
    return obj;
}

void bp_creation_ts_free(wmem_allocator_t *alloc, bp_creation_ts_t *obj) {
    // no sub-deletions
    wmem_free(alloc, obj);
}

gint bp_creation_ts_compare(gconstpointer a, gconstpointer b, gpointer user_data _U_) {
    const bp_creation_ts_t *ats = a;
    const bp_creation_ts_t *bts = b;
    if (ats->abstime.dtntime < bts->abstime.dtntime) {
        return -1;
    }
    else if (ats->abstime.dtntime > bts->abstime.dtntime) {
        return 1;
    }

    if (ats->seqno < bts->seqno) {
        return -1;
    }
    else if (ats->seqno > bts->seqno) {
        return 1;
    }

    return 0;
}

bp_eid_t * bp_eid_new(wmem_allocator_t *alloc) {
    bp_eid_t *obj = wmem_new0(alloc, bp_eid_t);
    return obj;
}

void bp_eid_free(wmem_allocator_t *alloc, bp_eid_t *obj) {
    wmem_free(alloc, (char *)(obj->dtn_wkssp));
    wmem_free(alloc, (char *)(obj->dtn_serv));
    wmem_free(alloc, obj);
}

gboolean bp_eid_equal(gconstpointer a, gconstpointer b) {
    const bp_eid_t *aobj = a;
    const bp_eid_t *bobj = b;
    return aobj->uri && bobj->uri && g_str_equal(aobj->uri, bobj->uri);
}

bp_block_primary_t * bp_block_primary_new(wmem_allocator_t *alloc) {
    bp_block_primary_t *obj = wmem_new0(alloc, bp_block_primary_t);
    obj->dst_eid = bp_eid_new(alloc);
    obj->src_nodeid = bp_eid_new(alloc);
    obj->rep_nodeid = bp_eid_new(alloc);
    obj->frag_offset = NULL;
    obj->total_len = NULL;
    obj->sec.data_i = wmem_map_new(alloc, g_int64_hash, g_int64_equal);
    obj->sec.data_c = wmem_map_new(alloc, g_int64_hash, g_int64_equal);
    return obj;
}

void bp_block_primary_free(wmem_allocator_t *alloc, bp_block_primary_t *obj) {
    if (!obj) {
        return;
    }
    bp_eid_free(alloc, obj->dst_eid);
    bp_eid_free(alloc, obj->src_nodeid);
    bp_eid_free(alloc, obj->rep_nodeid);
    wmem_free(alloc, obj->frag_offset);
    wmem_free(alloc, obj->total_len);
    wmem_free(alloc, obj->sec.data_i);
    wmem_free(alloc, obj->sec.data_c);
    wmem_free(alloc, obj);
}

bp_block_canonical_t * bp_block_canonical_new(wmem_allocator_t *alloc, guint64 blk_ix) {
    bp_block_canonical_t *obj = wmem_new0(alloc, bp_block_canonical_t);
    obj->blk_ix = blk_ix;
    obj->sec.data_i = wmem_map_new(alloc, g_int64_hash, g_int64_equal);
    obj->sec.data_c = wmem_map_new(alloc, g_int64_hash, g_int64_equal);
    return obj;
}

static guint64 * guint64_new(wmem_allocator_t *alloc, const guint64 val) {
    guint64 *obj = wmem_new(alloc, guint64);
    *obj = val;
    return obj;
}

bp_bundle_t * bp_bundle_new(wmem_allocator_t *alloc) {
    bp_bundle_t *obj = wmem_new0(alloc, bp_bundle_t);
    obj->primary = bp_block_primary_new(alloc);
    obj->blocks = wmem_list_new(alloc);
    obj->block_nums = wmem_map_new(alloc, g_int64_hash, g_int64_equal);
    obj->block_types = wmem_map_new(alloc, g_int64_hash, g_int64_equal);
    return obj;
}

void bp_bundle_free(wmem_allocator_t *alloc, bp_bundle_t *obj) {
    bp_bundle_ident_free(alloc, obj->ident);
    bp_block_primary_free(alloc, obj->primary);
    wmem_destroy_list(obj->blocks);
    wmem_free(alloc, obj);
}

bp_bundle_ident_t * bp_bundle_ident_new(wmem_allocator_t *alloc, bp_eid_t *src, bp_creation_ts_t *ts, guint64 *off, guint64 *len) {
    bp_bundle_ident_t *ident = wmem_new(alloc, bp_bundle_ident_t);
    ident->src = src ? wmem_strdup(alloc, src->uri) : NULL;
    ident->ts = ts;
    ident->frag_offset = off;
    ident->total_len = len;
    return ident;
}

void bp_bundle_ident_free(wmem_allocator_t *alloc, bp_bundle_ident_t *obj) {
    wmem_free(alloc, (char *)(obj->src));
    wmem_free(alloc, obj);
}

/** Either both values are defined and equal or both are null.
 */
static gboolean optional_uint64_equal(const guint64 *a, const guint64 *b) {
    if (a && b) {
        return (*a == *b);
    }
    else {
        return (a == NULL) && (b == NULL);
    }
}

gboolean bp_bundle_ident_equal(gconstpointer a, gconstpointer b) {
    const bp_bundle_ident_t *aobj = a;
    const bp_bundle_ident_t *bobj = b;
    return (
        aobj->src && bobj->src && g_str_equal(aobj->src, bobj->src)
        && (aobj->ts->abstime.dtntime == bobj->ts->abstime.dtntime)
        && (aobj->ts->seqno == bobj->ts->seqno)
        && optional_uint64_equal(aobj->frag_offset, bobj->frag_offset)
        && optional_uint64_equal(aobj->total_len, bobj->total_len)
    );
}

guint bp_bundle_ident_hash(gconstpointer key) {
    const bp_bundle_ident_t *obj = key;
    return (
        g_str_hash(obj->src ? obj->src : "")
        ^ g_int64_hash(&(obj->ts->abstime.dtntime))
        ^ g_int64_hash(&(obj->ts->seqno))
    );
}

/** Convert DTN time to time delta.
 * DTN Time is defined in Section 4.1.6.
 *
 * @param dtntime Number of milliseconds from an epoch.
 * @return The associated absolute time.
 */
static nstime_t dtn_to_delta(const gint64 dtntime) {
    nstime_t utctime;
    utctime.secs = dtntime / 1000;
    utctime.nsecs = 1000000 * (dtntime % 1000);
    return utctime;
}

/** Convert DTN time to absolute time.
 * DTN Time is defined in Section 4.1.6.
 *
 * @param dtntime Number of milliseconds from an epoch.
 * @return The associated absolute time.
 */
static nstime_t dtn_to_utctime(const gint64 dtntime) {
    nstime_t utctime;
    utctime.secs = 946684800 + dtntime / 1000;
    utctime.nsecs = 1000000 * (dtntime % 1000);
    return utctime;
}

proto_item * proto_tree_add_cbor_eid(proto_tree *tree, int hfindex, int hfindex_uri, packet_info *pinfo, tvbuff_t *tvb, gint *offset, bp_eid_t *eid) {
    wmem_allocator_t *alloc_eid = wmem_file_scope();
    proto_item *item_eid = proto_tree_add_item(tree, hfindex, tvb, *offset, -1, ENC_NA);
    proto_tree *tree_eid = proto_item_add_subtree(item_eid, ett_eid);
    const gint eid_start = *offset;

    wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
    wscbor_require_array_size(chunk, 2, 2);
    if (!chunk) {
        proto_item_set_len(item_eid, *offset - eid_start);
        return item_eid;
    }

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
    const guint64 *scheme = wscbor_require_uint64(alloc_eid, chunk);
    proto_item *item_scheme = proto_tree_add_cbor_uint64(tree_eid, hf_eid_scheme, pinfo, tvb, chunk, scheme);
    if (!scheme) {
        wscbor_skip_next_item(wmem_packet_scope(), tvb, offset);
        return item_eid;
    }

    wmem_strbuf_t *uribuf = wmem_strbuf_new(alloc_eid, NULL);
    const char *dtn_wkssp = NULL;
    const char *dtn_serv = NULL;
    switch (*scheme) {
        case EID_SCHEME_DTN: {
            chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
            switch (chunk->type_major) {
                case CBOR_TYPE_UINT: {
                    const guint64 *ssp_code = wscbor_require_uint64(wmem_packet_scope(), chunk);
                    proto_item *item = proto_tree_add_cbor_uint64(tree_eid, hf_eid_dtn_ssp_code, pinfo, tvb, chunk, ssp_code);

                    switch (*ssp_code) {
                        case 0:
                            dtn_wkssp = wmem_strdup(alloc_eid, "none");
                            break;
                        default:
                            expert_add_info(pinfo, item, &ei_eid_wkssp_unknown);
                            break;
                    }
                    if (dtn_wkssp) {
                        wmem_strbuf_append_printf(uribuf, "dtn:%s", dtn_wkssp);
                    }
                    break;
                }
                case CBOR_TYPE_STRING: {
                    char *ssp = wscbor_require_tstr(wmem_packet_scope(), chunk);
                    proto_tree_add_cbor_tstr(tree_eid, hf_eid_dtn_ssp_text, pinfo, tvb, chunk);
                    wmem_strbuf_append_printf(uribuf, "dtn:%s", ssp);

                    char *path_sep;
                    if ((path_sep = strrchr(ssp, '/')) != NULL) {
                        dtn_serv = wmem_strdup(alloc_eid, path_sep + 1);
                    }
                    else {
                        // no separator also means no authority part, so it's well-known
                        dtn_wkssp = wmem_strdup(alloc_eid, ssp);
                    }

                    wmem_free(wmem_packet_scope(), ssp);
                    break;
                }
                default: {
                    *offset = chunk->start;
                    wscbor_skip_next_item(wmem_packet_scope(), tvb, offset);
                    tvbuff_t *sub_tvb = tvb_new_subset_length(tvb, chunk->start, *offset);
                    call_dissector(handle_cbor, sub_tvb, pinfo, tree_eid);
                    expert_add_info(pinfo, item_eid, &ei_eid_ssp_type_invalid);
                    break;
                }
            }

            break;
        }
        case EID_SCHEME_IPN: {
            chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
            wscbor_require_array_size(chunk, 2, 2);
            if (!wscbor_skip_if_errors(wmem_packet_scope(), tvb, offset, chunk)) {
                chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
                const guint64 *node = wscbor_require_uint64(wmem_packet_scope(), chunk);
                proto_tree_add_cbor_uint64(tree_eid, hf_eid_ipn_node, pinfo, tvb, chunk, node);

                chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
                const guint64 *service = wscbor_require_uint64(wmem_packet_scope(), chunk);
                proto_tree_add_cbor_uint64(tree_eid, hf_eid_ipn_service, pinfo, tvb, chunk, service);

                wmem_strbuf_append_printf(uribuf, "ipn:%" G_GUINT64_FORMAT ".%" G_GUINT64_FORMAT, node ? *node : 0, service ? *service : 0);
            }
            break;
        }
        default:
            wscbor_skip_next_item(wmem_packet_scope(), tvb, offset);
            expert_add_info(pinfo, item_scheme, &ei_eid_scheme_unknown);
            break;
    }

    if (dtn_wkssp) {
        proto_item *item = proto_tree_add_string(tree_eid, hf_eid_dtn_wkssp, tvb, eid_start, *offset - eid_start, dtn_wkssp);
        PROTO_ITEM_SET_GENERATED(item);
    }
    if (dtn_serv) {
        proto_item *item = proto_tree_add_string(tree_eid, hf_eid_dtn_serv, tvb, eid_start, *offset - eid_start, dtn_serv);
        PROTO_ITEM_SET_GENERATED(item);
    }

    char *uri = NULL;
    if (wmem_strbuf_get_len(uribuf) > 0) {
        uri = wmem_strbuf_finalize(uribuf);

        proto_item *item_uri = proto_tree_add_string(tree_eid, hfindex_uri, tvb, eid_start, *offset - eid_start, uri);
        PROTO_ITEM_SET_GENERATED(item_uri);

        proto_item_append_text(item_eid, ": %s", uri);
    }

    if (eid) {
        eid->scheme = (scheme ? *scheme : 0);
        eid->uri = uri;
        eid->dtn_wkssp = dtn_wkssp;
        eid->dtn_serv = dtn_serv;
    }
    else {
        file_scope_delete(uri);
        file_scope_delete((char *)dtn_wkssp);
        file_scope_delete((char *)dtn_serv);
    }

    proto_item_set_len(item_eid, *offset - eid_start);
    return item_eid;
}

static void proto_tree_add_dtn_time(proto_tree *tree, int hfindex, packet_info *pinfo, tvbuff_t *tvb, gint *offset, bp_dtn_time_t *out) {
    proto_item *item_time = proto_tree_add_item(tree, hfindex, tvb, *offset, -1, 0);
    proto_tree *tree_time = proto_item_add_subtree(item_time, ett_time);
    const gint offset_start = *offset;

    wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
    if (chunk) {
        const guint64 *dtntime = wscbor_require_uint64(wmem_packet_scope(), chunk);
        proto_tree_add_cbor_uint64(tree_time, hf_time_dtntime, pinfo, tvb, chunk, dtntime);

        if (dtntime) {
            if (out) {
                out->dtntime = *dtntime;
            }

            if (*dtntime > 0) {
                const nstime_t utctime = dtn_to_utctime(*dtntime);
                proto_item *item_utctime = proto_tree_add_time(tree_time, hf_time_utctime, tvb, chunk->start, chunk->data_length, &utctime);
                PROTO_ITEM_SET_GENERATED(item_utctime);

                gchar *time_text = abs_time_to_str(wmem_packet_scope(), &utctime, ABSOLUTE_TIME_UTC, TRUE);
                proto_item_append_text(item_time, ": %s", time_text);

                if (out) {
                    out->utctime = utctime;
                }
            }
            else {
                proto_item_append_text(item_time, ": undefined");
            }
        }
        else if (out) {
            out->dtntime = 0;
            nstime_set_zero(&(out->utctime));
        }
    }
    proto_item_set_len(item_time, *offset - offset_start);
}

/** Extract a timestamp.
 *
 * @param tree The tree to write items under.
 * @param hfindex The root item field.
 * @param pinfo Packet info to update.
 * @param tvb Buffer to read from.
 * @param[in,out] offset Starting offset within @c tvb.
 * @param[out] ts If non-null, the timestamp to write to.
 */
static void proto_tree_add_cbor_timestamp(proto_tree *tree, int hfindex, packet_info *pinfo, tvbuff_t *tvb, gint *offset, bp_creation_ts_t *ts) {
    proto_item *item_ts = proto_tree_add_item(tree, hfindex, tvb, *offset, -1, 0);
    proto_tree *tree_ts = proto_item_add_subtree(item_ts, ett_create_ts);

    wscbor_chunk_t *chunk_ts = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
    wscbor_require_array_size(chunk_ts, 2, 2);
    wscbor_chunk_mark_errors(pinfo, item_ts, chunk_ts);
    if (!wscbor_skip_if_errors(wmem_packet_scope(), tvb, offset, chunk_ts)) {
        bp_dtn_time_t abstime;
        proto_tree_add_dtn_time(tree_ts, hf_create_ts_time, pinfo, tvb, offset, &abstime);

        wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
        const guint64 *seqno = wscbor_require_uint64(wmem_file_scope(), chunk);
        proto_tree_add_cbor_uint64(tree_ts, hf_create_ts_seqno, pinfo, tvb, chunk, seqno);

        if (ts) {
            ts->abstime = abstime;
            ts->seqno = (seqno ? *seqno : 0);
        }
    }
    proto_item_set_len(item_ts, *offset - chunk_ts->start);
}

/** Show read-in and actual CRC information.
 *
 * @param tvb The single-block data.
 * @param crc_type Type of CRC to compute.
 * @param crc_field The read-in field value.
 */
static void show_crc_info(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree_block, const guint64 *crc_type, tvbuff_t *crc_field) {
    if (!crc_type || !crc_field) {
        return;
    }

    // Display the data field information
    int hf_crc_field;
    switch (*crc_type) {
        case BP_CRC_16:
            hf_crc_field = hf_crc_field_uint16;
            break;
        case BP_CRC_32:
            hf_crc_field = hf_crc_field_uint32;
            break;
        default:
            hf_crc_field = -1;
            break;
    }

    // Compare against expected result
    guint32 crc_actual = 0;
    guint chksum_flags = PROTO_CHECKSUM_NO_FLAGS;
    if (bp_compute_crc) {
        if (*crc_type == BP_CRC_NONE) {
            chksum_flags |= PROTO_CHECKSUM_NOT_PRESENT;
        }
        else {
            const guint block_len = tvb_reported_length(tvb);
            guint8 *crcbuf = tvb_memdup(pinfo->pool, tvb, 0, block_len);
            switch (*crc_type) {
                case BP_CRC_16:
                    memset(crcbuf + block_len - 2, 0, 2);
                    crc_actual = crc16_ccitt(crcbuf, block_len);
                    break;
                case BP_CRC_32:
                    memset(crcbuf + block_len - 4, 0, 4);
                    crc_actual = ~crc32c_calculate_no_swap(crcbuf, block_len, CRC32C_PRELOAD);
                    break;
                default:
                    break;
            }
            wmem_free(pinfo->pool, crcbuf);

            chksum_flags |= PROTO_CHECKSUM_VERIFY;
        }
    }
    proto_tree_add_checksum(tree_block, crc_field, 0, hf_crc_field, hf_crc_status, &ei_block_failed_crc, pinfo, crc_actual, ENC_BIG_ENDIAN, chksum_flags);
}

static void proto_tree_add_ident(proto_tree *tree, int hfindex, tvbuff_t *tvb, const bp_bundle_ident_t *ident) {
    wmem_strbuf_t *ident_text = wmem_strbuf_new(wmem_packet_scope(), NULL);
    wmem_strbuf_append_printf(
        ident_text,
        "Source: %s, DTN Time: %" G_GUINT64_FORMAT ", Seq: %" G_GUINT64_FORMAT,
        ident->src,
        ident->ts->abstime.dtntime,
        ident->ts->seqno
    );
    if (ident->frag_offset) {
        wmem_strbuf_append_printf(ident_text, ", Frag Offset: %" G_GUINT64_FORMAT, *(ident->frag_offset));
    }
    if (ident->total_len) {
        wmem_strbuf_append_printf(ident_text, ", Total Length: %" G_GUINT64_FORMAT, *(ident->total_len));
    }

    proto_item *item_subj_ident = proto_tree_add_string(tree, hfindex, tvb, 0, 0, wmem_strbuf_get_str(ident_text));
    PROTO_ITEM_SET_GENERATED(item_subj_ident);
    wmem_strbuf_finalize(ident_text);
}


static gint dissect_block_primary(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree_block,
                                  gint start, bp_block_primary_t *block,
                                  bp_bundle_t *bundle _U_) {
    proto_item *item_block = proto_tree_get_parent(tree_block);
    gint field_ix = 0;
    gint offset = start;
    block->item_block = item_block;

    wscbor_chunk_t *chunk_block = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    wscbor_require_array_size(chunk_block, 8, 11);
    wscbor_chunk_mark_errors(pinfo, item_block, chunk_block);
    if (wscbor_skip_if_errors(wmem_packet_scope(), tvb, &offset, chunk_block)) {
        return offset - start;
    }
#if 0
    proto_item_append_text(item_block, ", Items: %" G_GUINT64_FORMAT, chunk_block->head_value);
#endif

    wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    const guint64 *version = wscbor_require_uint64(wmem_packet_scope(), chunk);
    proto_item *item_version = proto_tree_add_cbor_uint64(tree_block, hf_primary_version, pinfo, tvb, chunk, version);
    field_ix++;
    if (version && (*version != 7)) {
        expert_add_info(pinfo, item_version, &ei_invalid_bp_version);
    }

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    const guint64 *flags = wscbor_require_uint64(wmem_packet_scope(), chunk);
    proto_tree_add_cbor_bitmask(tree_block, hf_primary_bundle_flags, ett_bundle_flags, bundle_flags, pinfo, tvb, chunk, flags);
    field_ix++;
    block->flags = (flags ? *flags : 0);

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    guint64 *crc_type = wscbor_require_uint64(wmem_packet_scope(), chunk);
    proto_item *item_crc_type = proto_tree_add_cbor_uint64(tree_block, hf_crc_type, pinfo, tvb, chunk, crc_type);
    field_ix++;
    block->crc_type = (crc_type ? (BundleCrcType)(*crc_type) : BP_CRC_NONE);
    if (crc_type) {
        proto_item_append_text(item_block, ", CRC Type: %s", val64_to_str(*crc_type, crc_vals, "%" G_GUINT64_FORMAT));
    }

    proto_tree_add_cbor_eid(tree_block, hf_primary_dst_eid, hf_primary_dst_uri, pinfo, tvb, &offset, block->dst_eid);
    field_ix++;

    proto_tree_add_cbor_eid(tree_block, hf_primary_src_nodeid, hf_primary_src_uri, pinfo, tvb, &offset, block->src_nodeid);
    field_ix++;

    proto_tree_add_cbor_eid(tree_block, hf_primary_report_nodeid, hf_primary_report_uri, pinfo, tvb, &offset, block->rep_nodeid);
    field_ix++;

    // Complex type
    proto_tree_add_cbor_timestamp(tree_block, hf_primary_create_ts, pinfo, tvb, &offset, &(block->ts));
    field_ix++;

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    const guint64 *lifetime = wscbor_require_uint64(wmem_packet_scope(), chunk);
    proto_tree_add_cbor_uint64(tree_block, hf_primary_lifetime, pinfo, tvb, chunk, lifetime);
    if (lifetime) {
        nstime_t lifetime_exp = dtn_to_delta(*lifetime);
        proto_item *item_lifetime_exp = proto_tree_add_time(tree_block, hf_primary_lifetime_exp, tvb, chunk->start, chunk->head_length, &lifetime_exp);
        PROTO_ITEM_SET_GENERATED(item_lifetime_exp);

        if (block->ts.abstime.dtntime > 0) {
            nstime_t expiretime;
            nstime_sum(&expiretime, &(block->ts.abstime.utctime), &lifetime_exp);
            proto_item *item_expiretime = proto_tree_add_time(tree_block, hf_primary_expire_ts, tvb, 0, 0, &expiretime);
            PROTO_ITEM_SET_GENERATED(item_expiretime);
        }
    }
    field_ix++;

    // optional items
    if (flags && (*flags & BP_BUNDLE_IS_FRAGMENT)) {
        if (!wscbor_require_array_size(chunk_block, field_ix + 1, field_ix + 3)) {
            // Skip whole array
            offset = start;
            wscbor_skip_next_item(wmem_packet_scope(), tvb, &offset);

            return offset - start;
        }

        chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
        block->frag_offset = wscbor_require_uint64(wmem_file_scope(), chunk);
        proto_tree_add_cbor_uint64(tree_block, hf_primary_frag_offset, pinfo, tvb, chunk, block->frag_offset);
        field_ix++;

        chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
        block->total_len = wscbor_require_uint64(wmem_file_scope(), chunk);
        proto_tree_add_cbor_uint64(tree_block, hf_primary_total_length, pinfo, tvb, chunk, block->total_len);
        field_ix++;
    }

    switch (block->crc_type) {
        case BP_CRC_NONE:
            break;
        case BP_CRC_16:
        case BP_CRC_32: {
            if (!wscbor_require_array_size(chunk_block, field_ix + 1, field_ix + 1)) {
                // Skip whole array
                offset = start;
                wscbor_skip_next_item(wmem_packet_scope(), tvb, &offset);

                return offset - start;
            }

            chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
            tvbuff_t *crc_field = wscbor_require_bstr(wmem_file_scope(), chunk);
            field_ix++;
            block->crc_field = crc_field;

            tvbuff_t *tvb_block = tvb_new_subset_length(tvb, start, offset - start);
            show_crc_info(tvb_block, pinfo, tree_block, crc_type, crc_field);
            break;
        }
        default:
            expert_add_info(pinfo, item_crc_type, &ei_crc_type_unknown);
            break;
    }

    return offset - start;
}

static gint dissect_block_canonical(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree_block,
                                    gint start, bp_block_canonical_t *block,
                                    bp_bundle_t *bundle _U_) {
    proto_item *item_block = proto_tree_get_parent(tree_block);
    gint field_ix = 0;
    gint offset = start;
    block->item_block = item_block;

    wscbor_chunk_t *chunk_block = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    wscbor_require_array_size(chunk_block, 5, 6);
    wscbor_chunk_mark_errors(pinfo, item_block, chunk_block);
    if (wscbor_skip_if_errors(wmem_packet_scope(), tvb, &offset, chunk_block)) {
        return offset - start;
    }
#if 0
    proto_item_append_text(item_block, ", Items: %" G_GUINT64_FORMAT, chunk_block->head_value);
#endif

    wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    guint64 *type_code = wscbor_require_uint64(wmem_file_scope(), chunk);
    proto_item *item_type = proto_tree_add_cbor_uint64(tree_block, hf_canonical_type_code, pinfo, tvb, chunk, type_code);
    field_ix++;
    block->type_code = type_code;

    if (type_code) {
        proto_item_append_text(item_block, ": %s", val64_to_str(*type_code, blocktype_vals, "Type %" G_GUINT64_FORMAT));

        // Check duplicate of this type
        guint64 limit = UINT64_MAX;
        for (int ix = 0; ; ++ix) {
            const blocktype_limit *row = blocktype_limits + ix;
            if (row->type_code == BP_BLOCKTYPE_INVALID) {
                break;
            }
            if (row->type_code == *type_code) {
                limit = row->limit;
                break;
            }
        }

        guint64 count = 1; // this block counts regardless of presence in the map
        wmem_list_t *list_found = wmem_map_lookup(bundle->block_types, type_code);
        if (list_found) {
            for (wmem_list_frame_t *it = wmem_list_head(list_found); it;
                    it = wmem_list_frame_next(it)) {
                bp_block_canonical_t *block_found = wmem_list_frame_data(it);
                if (block == block_found) {
                    continue;
                }
                ++count;
            }
        }
        if (count > limit) {
            // First non-identical block triggers the error
            expert_add_info(pinfo, item_type, &ei_block_type_dupe);
        }
    }

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    guint64 *block_num = wscbor_require_uint64(wmem_file_scope(), chunk);
    proto_item *item_block_num = proto_tree_add_cbor_uint64(tree_block, hf_canonical_block_num, pinfo, tvb, chunk, block_num);
    field_ix++;
    block->block_number = block_num;
    if (block_num) {
        proto_item_append_text(item_block, ", Block Num: %" G_GUINT64_FORMAT, *block_num);
    }

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    const guint64 *flags = wscbor_require_uint64(wmem_file_scope(), chunk);
    proto_tree_add_cbor_bitmask(tree_block, hf_canonical_block_flags, ett_block_flags, block_flags, pinfo, tvb, chunk, flags);
    field_ix++;
    block->flags = (flags ? *flags : 0);

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    guint64 *crc_type = wscbor_require_uint64(wmem_file_scope(), chunk);
    proto_item *item_crc_type = proto_tree_add_cbor_uint64(tree_block, hf_crc_type, pinfo, tvb, chunk, crc_type);
    field_ix++;
    block->crc_type = (crc_type ? (BundleCrcType)(*crc_type) : BP_CRC_NONE);
    if (crc_type) {
        proto_item_append_text(item_block, ", CRC Type: %s", val64_to_str(*crc_type, crc_vals, "%" G_GUINT64_FORMAT));
    }

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    tvbuff_t *tvb_data = wscbor_require_bstr(wmem_file_scope(), chunk);
    field_ix++;
    block->data = tvb_data;

    const guint tvb_data_len = (tvb_data ? tvb_reported_length(tvb_data) : 0);
    proto_item *item_data = proto_tree_add_uint64(tree_block, hf_canonical_data, tvb_data, 0, tvb_data_len, tvb_data_len);
    proto_tree *tree_data = proto_item_add_subtree(item_data, ett_canonical_data);
    block->tree_data = tree_data;

    switch (block->crc_type) {
        case BP_CRC_NONE:
            break;
        case BP_CRC_16:
        case BP_CRC_32: {
            if (!wscbor_require_array_size(chunk_block, field_ix + 1, field_ix + 1)) {
                // Skip whole array
                offset = start;
                wscbor_skip_next_item(wmem_packet_scope(), tvb, &offset);

                return offset - start;
            }

            chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
            tvbuff_t *crc_field = wscbor_require_bstr(wmem_file_scope(), chunk);
            field_ix++;
            block->crc_field = crc_field;

            tvbuff_t *tvb_block = tvb_new_subset_length(tvb, start, offset - start);
            show_crc_info(tvb_block, pinfo, tree_block, crc_type, crc_field);
            break;
        }
        default:
            expert_add_info(pinfo, item_crc_type, &ei_crc_type_unknown);
            break;
    }

    wmem_list_append(bundle->blocks, block);

    if (block->type_code) {
        wmem_list_t *type_list = wmem_map_lookup(bundle->block_types, block->type_code);
        if (!type_list) {
            guint64 *key = guint64_new(wmem_file_scope(), *(block->type_code));
            type_list = wmem_list_new(wmem_file_scope());
            wmem_map_insert(bundle->block_types, key, type_list);
        }
        wmem_list_append(type_list, block);
    }
    if (block->block_number) {
        bp_block_canonical_t *found = wmem_map_lookup(bundle->block_nums, block->block_number);
        if (found) {
            expert_add_info(pinfo, item_block_num, &ei_block_num_dupe);
        }
        else {
            guint64 *key = guint64_new(wmem_file_scope(), *(block->block_number));
            wmem_map_insert(bundle->block_nums, key, block);
        }
    }
    // Payload block requirements
    if (block->type_code && (*(block->type_code) == BP_BLOCKTYPE_PAYLOAD)) {
        // must have index zero
        if (block->block_number && (*(block->block_number) != 1)) {
            expert_add_info(pinfo, item_block_num, &ei_block_payload_num);
        }
    }

    return offset - start;
}

typedef struct {
    packet_info *pinfo;
    proto_item *pi;
    expert_field *eiindex;
    const char *sectype;
} bpsec_block_mark_t;
/// Mark blocks with BPSec expert info
static void mark_target_block(gpointer key, gpointer value _U_, gpointer user_data) {
    const guint64 *blk_num = (guint64 *)key;
    const bpsec_block_mark_t *mark = (bpsec_block_mark_t *)user_data;
    expert_add_info_format(
        mark->pinfo, mark->pi, mark->eiindex,
        "Block is targed by %s block number %" G_GUINT64_FORMAT, mark->sectype, *blk_num
    );
}
static void apply_bpsec_mark(const security_mark_t *sec, packet_info *pinfo, proto_item *pi) {
    {
        bpsec_block_mark_t mark;
        mark.pinfo = pinfo;
        mark.pi = pi;
        mark.eiindex = &ei_block_sec_bib_tgt;
        mark.sectype = "BIB";
        wmem_map_foreach(sec->data_i, mark_target_block, &mark);
    }
    {
        bpsec_block_mark_t mark;
        mark.pinfo = pinfo;
        mark.pi = pi;
        mark.eiindex = &ei_block_sec_bcb_tgt;
        mark.sectype = "BCB";
        wmem_map_foreach(sec->data_c, mark_target_block, &mark);
    }
}

/** Extract data from a block (including payload and admin).
 *
 * @param dissector The optional dissector to call.
 * @param context Context for the @c dissector.
 * @param tvb Buffer to read from.
 * @param pinfo Packet info to update.
 * @param tree The tree to write items under.
 * @param payload True if this is bundle payload.
 * @return The number of dissected octets.
 */
static gint dissect_carried_data(dissector_handle_t dissector, void *context, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, gboolean payload _U_) {
    int sublen = 0;
    if (dissector) {
        sublen = call_dissector_only(dissector, tvb, pinfo, tree, context);
        if ((sublen < 0) ||
            ((sublen > 0) && ((guint)sublen < tvb_reported_length(tvb)))) {
            expert_add_info(pinfo, proto_tree_get_parent(tree), &ei_sub_partial_decode);
        }
    }
    else {
        expert_add_info(pinfo, proto_tree_get_parent(tree), &ei_sub_type_unknown);
    }

    if ((sublen <= 0) && bp_payload_try_heur) {
        heur_dtbl_entry_t *entry = NULL;
        if (dissector_try_heuristic(btsd_heur, tvb, pinfo, tree, &entry, context)) {
            sublen = tvb_reported_length(tvb);
        }
    }
    if (sublen == 0) {
        sublen = call_data_dissector(tvb, pinfo, tree);
    }
    return sublen;
}

/** Handle iteration over status subject set.
 *
 */
static void show_status_subj_ref(gpointer key, gpointer val _U_, gpointer data) {
    bp_bundle_ident_t *status_ident = key;
    proto_tree *tree_bundle = data;
    const bp_bundle_t *status_found = wmem_map_lookup(bp_history->bundles, status_ident);
    if (status_found) {
        proto_item *item_subj_ref = proto_tree_add_uint(tree_bundle, hf_bundle_status_ref, NULL, 0, 0, status_found->frame_num);
        PROTO_ITEM_SET_GENERATED(item_subj_ref);
    }
}

/// Top-level protocol dissector
static int dissect_bp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
    {
        const gchar *proto_name = col_get_text(pinfo->cinfo, COL_PROTOCOL);
        if (g_strcmp0(proto_name, proto_name_bp) != 0) {
            col_set_str(pinfo->cinfo, COL_PROTOCOL, proto_name_bp);
            col_clear(pinfo->cinfo, COL_INFO);
        }
    }
    gint offset = 0;

    proto_item *item_bundle = proto_tree_add_item(tree, proto_bp, tvb, 0, -1, ENC_NA);
    proto_tree *tree_bundle = proto_item_add_subtree(item_bundle, ett_bundle);

    bp_bundle_t *bundle = bp_bundle_new(wmem_file_scope());
    bundle->frame_num = pinfo->num;
    bundle->frame_time = pinfo->abs_ts;

    // Read blocks directly from buffer with same addresses as #tvb
    const guint buflen = tvb_reported_length(tvb);

    // Require indefinite-length array type
    wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    proto_item *item_head = proto_tree_add_item(tree_bundle, hf_bundle_head, tvb, chunk->start, chunk->data_length, ENC_NA);
    wscbor_require_array(chunk);
    if (wscbor_chunk_mark_errors(pinfo, item_head, chunk)) {
        return buflen;
    }
    else if (chunk->type_minor != 31) {
        expert_add_info_format(pinfo, item_head, &ei_invalid_framing, "Expected indefinite length array");
        // continue on even for definite-length array
    }

    guint64 block_ix = 0;
    while (TRUE) {
        if (offset >= (gint)buflen) {
            proto_item *item_break = proto_tree_add_item(tree_bundle, hf_bundle_break, tvb, offset, -1, ENC_NA);
            expert_add_info_format(pinfo, item_break, &ei_invalid_framing, "Array break missing");
            break;
        }

        // Either detect BREAK or decode block
        chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
        if (wscbor_is_indefinite_break(chunk)) {
            proto_tree_add_cbor_ctrl(tree_bundle, hf_bundle_break, pinfo, tvb, chunk);
            break;
        }
        offset = chunk->start;

        // Load just the array start
        const gint block_start = offset;
        proto_item *item_block = proto_tree_add_item(tree_bundle, hf_block, tvb, block_start, -1, ENC_NA);
        proto_tree *tree_block = proto_item_add_subtree(item_block, ett_block);

        if (block_ix == 0) {
            // Primary block
            proto_item_prepend_text(item_block, "Primary ");
            bp_block_primary_t *block = bp_block_primary_new(wmem_file_scope());
            offset += dissect_block_primary(tvb, pinfo, tree_block, offset, block, bundle);
            bundle->primary = block;

            if (!(bundle->ident)) {
                bundle->ident = bp_bundle_ident_new(
                    wmem_file_scope(),
                    bundle->primary->src_nodeid,
                    &(bundle->primary->ts),
                    bundle->primary->frag_offset,
                    bundle->primary->total_len
                );
                proto_tree_add_ident(tree_bundle, hf_bundle_ident, tvb, bundle->ident);

                const bp_bundle_t *seen_found = wmem_map_lookup(bp_history->bundles, bundle->ident);
                if (seen_found && (seen_found->frame_num != pinfo->num)) {
                    proto_item *item_seen = proto_tree_add_uint(tree_bundle, hf_bundle_seen, tvb, 0, 0, seen_found->frame_num);
                    PROTO_ITEM_SET_GENERATED(item_seen);

                    nstime_t td;
                    nstime_delta(&td, &(bundle->frame_time), &(seen_found->frame_time));
                    proto_item *item_td = proto_tree_add_time(tree_bundle, hf_bundle_seen_time_diff, tvb, 0, 0, &td);
                    PROTO_ITEM_SET_GENERATED(item_td);
                }

                // Indicate related status (may be multiple)
                wmem_map_t *status_set = wmem_map_lookup(bp_history->admin_status, bundle->ident);
                if (status_set) {
                    wmem_map_foreach(status_set, show_status_subj_ref, tree_bundle);
                }
            }
        }
        else {
            // Non-primary block
            proto_item_prepend_text(item_block, "Canonical ");
            bp_block_canonical_t *block = bp_block_canonical_new(wmem_file_scope(), block_ix);
            offset += dissect_block_canonical(tvb, pinfo, tree_block, offset, block, bundle);
        }

        proto_item_set_len(item_block, offset - block_start);
        block_ix++;
    }

    // Handle block-type-specific data after all blocks are present
    for (wmem_list_frame_t *it = wmem_list_head(bundle->blocks); it;
            it = wmem_list_frame_next(it)) {
        bp_block_canonical_t *block = wmem_list_frame_data(it);

        // Payload block requirements
        if (block->type_code && (*(block->type_code) == BP_BLOCKTYPE_PAYLOAD)) {
            // must be last block (i.e. next is NULL)
            if (wmem_list_frame_next(it)) {
                expert_add_info(pinfo, block->item_block, &ei_block_payload_index);
            }
        }

        if (block->data) {
            // sub-dissect after all is read
            dissector_handle_t data_dissect = NULL;
            if (block->type_code) {
                data_dissect = dissector_get_custom_table_handle(block_dissectors, block->type_code);
            }

            bp_dissector_data_t dissect_data;
            dissect_data.bundle = bundle;
            dissect_data.block = block;
            dissect_carried_data(data_dissect, &dissect_data, block->data, pinfo, block->tree_data, FALSE);
        }
    }

    // Block-data-derived markings
    apply_bpsec_mark(&(bundle->primary->sec), pinfo, bundle->primary->item_block);
    for (wmem_list_frame_t *it = wmem_list_head(bundle->blocks); it;
            it = wmem_list_frame_next(it)) {
        bp_block_canonical_t *block = wmem_list_frame_data(it);
        apply_bpsec_mark(&(block->sec), pinfo, block->item_block);
    }

    proto_item_append_text(item_bundle, ", Blocks: %" G_GUINT64_FORMAT, block_ix);
    if (bundle->primary) {
        const bp_block_primary_t *block = bundle->primary;
        proto_item_append_text(item_bundle, ", Dst: %s", block->dst_eid ? block->dst_eid->uri : NULL);
        proto_item_append_text(item_bundle, ", Src: %s", block->src_nodeid ? block->src_nodeid->uri : NULL);
        if (bundle->ident && (bundle->ident->ts)) {
            proto_item_append_text(item_bundle, ", Time: %" G_GUINT64_FORMAT, bundle->ident->ts->abstime.dtntime);
            proto_item_append_text(item_bundle, ", Seq: %" G_GUINT64_FORMAT, bundle->ident->ts->seqno);
        }
    }
    {
        // Keep bundle metadata around for the whole file
        bp_bundle_t *found = wmem_map_lookup(bp_history->bundles, bundle->ident);
        if (!found) {
            wmem_map_insert(bp_history->bundles, bundle->ident, bundle);
        }
        else {
            bp_bundle_free(wmem_file_scope(), bundle);
        }
    }

    proto_item_set_len(item_bundle, offset);
    return buflen;
}

static gboolean proto_tree_add_status_assertion(proto_tree *tree, int hfassert, packet_info *pinfo, tvbuff_t *tvb, gint *offset) {
    proto_item *item_assert = proto_tree_add_item(tree, hfassert, tvb, *offset, -1, 0);

    gboolean result = FALSE;

    wscbor_chunk_t *chunk_assert = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
    wscbor_require_array_size(chunk_assert, 1, 2);
    wscbor_chunk_mark_errors(pinfo, item_assert, chunk_assert);
    if (!wscbor_skip_if_errors(wmem_packet_scope(), tvb, offset, chunk_assert)) {
        proto_tree *tree_assert = proto_item_add_subtree(item_assert, ett_status_assert);

        wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, offset);
        gboolean *status_val = wscbor_require_boolean(wmem_packet_scope(), chunk);
        proto_tree_add_cbor_boolean(tree_assert, hf_status_assert_val, pinfo, tvb, chunk, status_val);
        if (status_val) {
            result = *status_val;
        }

        if (chunk_assert->head_value > 1) {
            bp_dtn_time_t abstime;
            proto_tree_add_dtn_time(tree_assert, hf_status_assert_time, pinfo, tvb, offset, &abstime);
        }
    }

    proto_item_set_len(item_assert, *offset - chunk_assert->start);
    return result;
}

static int dissect_payload_admin(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data) {
    bp_dissector_data_t *context = (bp_dissector_data_t *)data;
    DISSECTOR_ASSERT(context);
    {
        const gchar *proto_name = col_get_text(pinfo->cinfo, COL_PROTOCOL);
        if (g_strcmp0(proto_name, proto_name_bp_admin) != 0) {
            col_set_str(pinfo->cinfo, COL_PROTOCOL, proto_name_bp_admin);
            col_clear(pinfo->cinfo, COL_INFO);
        }
    }
    proto_item *item_rec = proto_tree_add_item(tree, proto_bp_admin, tvb, 0, -1, ENC_NA);
    gint offset = 0;

    wscbor_chunk_t *chunk_rec = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    wscbor_require_array_size(chunk_rec, 1, 2);
    wscbor_chunk_mark_errors(pinfo, item_rec, chunk_rec);
    if (!wscbor_skip_if_errors(wmem_packet_scope(), tvb, &offset, chunk_rec)) {
        proto_tree *tree_rec = proto_item_add_subtree(item_rec, ett_admin);

        wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
        guint64 *type_code = wscbor_require_uint64(wmem_packet_scope(), chunk);
        proto_tree_add_cbor_uint64(tree_rec, hf_admin_record_type, pinfo, tvb, chunk, type_code);

        dissector_handle_t admin_dissect = NULL;
        if (type_code) {
            proto_item_append_text(item_rec, ": %s", val64_to_str(*type_code, admin_type_vals, "Type %" G_GUINT64_FORMAT));
            admin_dissect = dissector_get_custom_table_handle(admin_dissectors, type_code);
        }
        tvbuff_t *tvb_record = tvb_new_subset_remaining(tvb, offset);
        gint sublen = dissect_carried_data(admin_dissect, context, tvb_record, pinfo, tree_rec, TRUE);
        offset += sublen;
    }

    proto_item_set_len(item_rec, offset);
    return offset;
}

static int dissect_status_report(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data) {
    bp_dissector_data_t *context = (bp_dissector_data_t *)data;
    if (!context) {
        return -1;
    }
    gint offset = 0;

    // Status Information array head
    proto_item *item_status = proto_tree_add_item(tree, hf_status_rep, tvb, offset, -1, ENC_NA);
    proto_tree *tree_status = proto_item_add_subtree(item_status, ett_status_rep);
    guint status_field_ix = 0;

    wscbor_chunk_t *chunk_status = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    wscbor_require_array_size(chunk_status, 4, 6);
    wscbor_chunk_mark_errors(pinfo, item_status, chunk_status);
    if (wscbor_skip_if_errors(wmem_packet_scope(), tvb, &offset, chunk_status)) {
        proto_item_set_len(item_status, offset - chunk_status->start);
        return offset;
    }

    wscbor_chunk_t *chunk;
    gboolean status_received = FALSE;
    gboolean status_forwarded = FALSE;
    gboolean status_delivered = FALSE;
    gboolean status_deleted = FALSE;

    wscbor_chunk_t *chunk_info = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    wscbor_require_array_size(chunk_info, 4, 4);
    {
        proto_item *item_info = proto_tree_add_item(tree_status, hf_status_rep_status_info, tvb, offset, -1, ENC_NA);
        wscbor_chunk_mark_errors(pinfo, item_info, chunk_info);
        if (!wscbor_skip_if_errors(wmem_packet_scope(), tvb, &offset, chunk_info)) {
            proto_tree *tree_info = proto_item_add_subtree(item_info, ett_status_info);

            status_received = proto_tree_add_status_assertion(tree_info, hf_status_rep_received, pinfo, tvb, &offset);
            status_forwarded = proto_tree_add_status_assertion(tree_info, hf_status_rep_forwarded, pinfo, tvb, &offset);
            status_delivered = proto_tree_add_status_assertion(tree_info, hf_status_rep_delivered, pinfo, tvb, &offset);
            status_deleted = proto_tree_add_status_assertion(tree_info, hf_status_rep_deleted, pinfo, tvb, &offset);
        }

        proto_item_set_len(item_info, offset - chunk_info->start);
        status_field_ix++;
    }

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    guint64 *reason_code = wscbor_require_uint64(wmem_packet_scope(), chunk);
    proto_tree_add_cbor_uint64(tree_status, hf_status_rep_reason_code, pinfo, tvb, chunk, reason_code);
    status_field_ix++;

    bp_eid_t *subj_eid = bp_eid_new(wmem_file_scope());
    proto_tree_add_cbor_eid(tree_status, hf_status_rep_subj_src_nodeid, hf_status_rep_subj_src_uri, pinfo, tvb, &offset, subj_eid);
    status_field_ix++;

    bp_creation_ts_t *subj_ts = bp_creation_ts_new(wmem_file_scope());
    proto_tree_add_cbor_timestamp(tree_status, hf_status_rep_subj_ts, pinfo, tvb, &offset, subj_ts);
    status_field_ix++;

    bp_bundle_ident_t *subj = bp_bundle_ident_new(wmem_file_scope(), subj_eid, subj_ts, NULL, NULL);

    if (chunk_info->head_value > status_field_ix) {
        chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
        subj->frag_offset = wscbor_require_uint64(wmem_file_scope(), chunk);
        proto_tree_add_cbor_uint64(tree_status, hf_status_rep_subj_frag_offset, pinfo, tvb, chunk, subj->frag_offset);
        status_field_ix++;
    }

    if (chunk_info->head_value > status_field_ix) {
        chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
        subj->total_len = wscbor_require_uint64(wmem_file_scope(), chunk);
        proto_tree_add_cbor_uint64(tree_status, hf_status_rep_subj_payload_len, pinfo, tvb, chunk, subj->total_len);
        status_field_ix++;
    }

    proto_tree_add_ident(tree_status, hf_status_rep_subj_ident, tvb, subj);

    {
        // Pointer back to subject
        const bp_bundle_t *subj_found = wmem_map_lookup(bp_history->bundles, subj);
        if (subj_found) {
            proto_item *item_subj_ref = proto_tree_add_uint(tree_status, hf_status_rep_subj_ref, tvb, 0, 0, subj_found->frame_num);
            PROTO_ITEM_SET_GENERATED(item_subj_ref);

            nstime_t td;
            nstime_delta(&td, &(context->bundle->frame_time), &(subj_found->frame_time));
            proto_item *item_td = proto_tree_add_time(tree_status, hf_status_time_diff, tvb, 0, 0, &td);
            PROTO_ITEM_SET_GENERATED(item_td);
        }
    }
    {
        // Pointers from subject to this status
        wmem_map_t *status_set = wmem_map_lookup(bp_history->admin_status, subj);
        if (!status_set) {
            status_set = wmem_map_new(wmem_file_scope(), bp_bundle_ident_hash, bp_bundle_ident_equal);
            wmem_map_insert(bp_history->admin_status, subj, status_set);
        }
        else {
            bp_bundle_ident_free(wmem_file_scope(), subj);
        }

        // Back-references to this status
        if (!wmem_map_contains(status_set, context->bundle->ident)) {
            g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "status for %p in frame %d", (void*)context->bundle, context->bundle->frame_num);
            wmem_map_insert(status_set, context->bundle->ident, NULL);
        }
    }

    proto_item *item_admin = proto_tree_get_parent(tree);
    {
        wmem_strbuf_t *status_text = wmem_strbuf_new(wmem_packet_scope(), NULL);
        gboolean sep = FALSE;
        if (status_received) {
            if (sep) {
                wmem_strbuf_append(status_text, "|");
            }
            wmem_strbuf_append(status_text, "RECEIVED");
            sep = TRUE;
        }
        if (status_forwarded) {
            if (sep) {
                wmem_strbuf_append(status_text, "|");
            }
            wmem_strbuf_append(status_text, "FORWARDED");
            sep = TRUE;
        }
        if (status_delivered) {
            if (sep) {
                wmem_strbuf_append(status_text, "|");
            }
            wmem_strbuf_append(status_text, "DELIVERED");
            sep = TRUE;
        }
        if (status_deleted) {
            if (sep) {
                wmem_strbuf_append(status_text, "|");
            }
            wmem_strbuf_append(status_text, "DELETED");
        }
        const char *status_buf = wmem_strbuf_finalize(status_text);
        proto_item_append_text(item_admin, ", Status: %s", status_buf);
        col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%s %s %s Status: %s",
                            context->bundle->primary->src_nodeid->uri,
                            UTF8_RIGHTWARDS_ARROW,
                            context->bundle->primary->dst_eid->uri,
                            status_buf);
    }
    if (reason_code) {
        proto_item_append_text(item_admin, ", Reason: %s", val64_to_str(*reason_code, status_report_reason_vals, "%" G_GUINT64_FORMAT));
    }

    proto_item_set_len(item_status, offset - chunk_status->start);
    return offset;
}

/** Dissector for Bundle Payload block.
 */
static int dissect_block_payload(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data) {
    bp_dissector_data_t *context = (bp_dissector_data_t *)data;
    if (!context) {
        return -1;
    }
    const bp_bundle_t *bundle = context->bundle;

    // Parent bundle tree
    proto_tree *tree_block = proto_tree_get_parent_tree(tree);
    proto_tree *tree_bundle = proto_tree_get_parent_tree(tree_block);
    proto_tree *item_bundle = proto_tree_get_parent(tree_bundle);
    // Back up to top-level
    proto_item *tree_top = proto_tree_get_parent_tree(tree_bundle);

    const gboolean is_fragment = bundle->primary->flags & BP_BUNDLE_IS_FRAGMENT;
    const gboolean is_admin = bundle->primary->flags & BP_BUNDLE_PAYLOAD_ADMIN;
    if (is_admin) {
        proto_item_append_text(item_bundle, ", ADMIN");
    }
    if (is_fragment) {
        proto_item_append_text(item_bundle, ", FRAGMENT");
    }
    const guint payload_len = tvb_reported_length(tvb);
    proto_item_append_text(item_bundle, ", Payload-Size: %d", payload_len);

    // identify bundle regardless of payload decoding
    col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%s %s %s",
                        bundle->primary->src_nodeid->uri,
                        UTF8_RIGHTWARDS_ARROW,
                        bundle->primary->dst_eid->uri);

    // Set if the payload is fully defragmented
    tvbuff_t *tvb_payload = NULL;
    const char *col_suffix = NULL;
    if (is_fragment) {
        col_suffix = " (fragment)";

        if (bp_reassemble_payload) {
            if (!(bundle->primary->frag_offset
                  && bundle->primary->total_len)) {
                return -1;
            }
            // correlate by non-fragment bundle identity hash
            bp_bundle_ident_t *corr_ident = bp_bundle_ident_new(
                wmem_packet_scope(),
                bundle->primary->src_nodeid,
                &(bundle->primary->ts),
                NULL,
                NULL
            );

            if (
                (G_MAXUINT32 < *(bundle->primary->frag_offset))
                || (G_MAXUINT32 < *(bundle->primary->total_len))) {
                expert_add_info(pinfo, bundle->primary->item_block, &ei_fragment_reassemble_size);
            }
            else {
                const guint32 frag_offset = (guint32)*(bundle->primary->frag_offset);
                const guint32 total_len = (guint32)*(bundle->primary->total_len);
                fragment_head *payload_frag_msg = fragment_add_check(
                    &bp_reassembly_table,
                    tvb, 0,
                    pinfo, 0, corr_ident,
                    frag_offset,
                    payload_len,
                    TRUE
                );
                const guint32 old_total_len = fragment_get_tot_len(
                    &bp_reassembly_table,
                    pinfo, 0, corr_ident
                );
                if (old_total_len > 0) {
                    if (total_len != old_total_len) {
                        expert_add_info(pinfo, bundle->primary->item_block, &ei_fragment_tot_mismatch);
                    }
                }
                else {
                    fragment_set_tot_len(
                        &bp_reassembly_table,
                        pinfo, 0, corr_ident,
                        total_len
                    );
                }
                tvb_payload = process_reassembled_data(
                    tvb, 0, pinfo,
                    "Reassembled Payload",
                    payload_frag_msg,
                    &payload_frag_items,
                    NULL,
                    tree_bundle
                );
                if (tvb_payload) {
                    col_suffix = " (reassembled)";
                }
            }
            bp_bundle_ident_free(wmem_packet_scope(), corr_ident);
        }
    }
    else {
        tvb_payload = tvb;
    }
    if (col_suffix) {
        col_append_str(pinfo->cinfo, COL_INFO, col_suffix);
    }
    if (!tvb_payload) {
        return payload_len;
    }

    // confidentiality target (i.e. ciphertext)
    if (wmem_map_size(context->block->sec.data_c) > 0) {
        return payload_len;
    }

    // Payload is known to be administrative, independent of destination EID
    if (is_admin) {
        col_append_str(pinfo->cinfo, COL_INFO, " [Admin]");
        const int sublen = call_dissector_only(handle_admin, tvb_payload, pinfo, tree_top, context);
        if (sublen > 0) {
            return sublen;
        }
    }

    // an EID shouldn't have both of these set
    dissector_handle_t payload_dissect = NULL;
    if (bundle->primary->dst_eid->dtn_wkssp) {
        payload_dissect = dissector_get_string_handle(payload_dissectors_dtn_wkssp, bundle->primary->dst_eid->dtn_wkssp);
    }
    else if (bundle->primary->dst_eid->dtn_serv) {
        payload_dissect = dissector_get_string_handle(payload_dissectors_dtn_serv, bundle->primary->dst_eid->dtn_serv);
    }

    return dissect_carried_data(payload_dissect, &data, tvb_payload, pinfo, tree_top, TRUE);
}

/** Dissector for Previous Node block.
 */
static int dissect_block_prev_node(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
    gint offset = 0;

    proto_tree_add_cbor_eid(tree, hf_previous_node_nodeid, hf_previous_node_uri, pinfo, tvb, &offset, NULL);

    return offset;
}

/** Dissector for Bundle Age block.
 */
static int dissect_block_bundle_age(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
    gint offset = 0;

    wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    const guint64 *age = wscbor_require_uint64(wmem_packet_scope(), chunk);
    proto_tree_add_cbor_uint64(tree, hf_bundle_age_time, pinfo, tvb, chunk, age);

    return offset;
}

/** Dissector for Hop Count block.
 */
static int dissect_block_hop_count(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
    gint offset = 0;

    wscbor_chunk_t *chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    wscbor_require_array_size(chunk, 2, 2);
    if (wscbor_skip_if_errors(wmem_packet_scope(), tvb, &offset, chunk)) {
        return offset;
    }

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    const guint64 *limit = wscbor_require_uint64(wmem_packet_scope(), chunk);
    proto_tree_add_cbor_uint64(tree, hf_hop_count_limit, pinfo, tvb, chunk, limit);

    chunk = wscbor_chunk_read(wmem_packet_scope(), tvb, &offset);
    const guint64 *current = wscbor_require_uint64(wmem_packet_scope(), chunk);
    proto_tree_add_cbor_uint64(tree, hf_hop_count_current, pinfo, tvb, chunk, current);

    return offset;
}

static gboolean btsd_heur_cbor(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
    gint offset = 0;

    // check exactly one item
    wscbor_skip_next_item(wmem_packet_scope(), tvb, &offset);
    if ((guint)offset == tvb_reported_length(tvb)) {
        call_dissector(handle_cbor, tvb, pinfo, tree);
        return TRUE;
    }

    // attempt a multi-item sequence
    TRY {
        offset = call_dissector(handle_cborseq, tvb, pinfo, tree);
    }
    CATCH_ALL {}
    ENDTRY;

    return ((guint)offset == tvb_reported_length(tvb));
}

/// Clear state when new file scope is entered
static void bp_init(void) {
    bp_history = wmem_new0(wmem_file_scope(), bp_history_t);
    // ident keys are owned by the respective bundles
    bp_history->bundles = wmem_map_new(wmem_file_scope(), bp_bundle_ident_hash, bp_bundle_ident_equal);
    // subject ident key is not kept
    bp_history->admin_status = wmem_map_new(wmem_file_scope(), bp_bundle_ident_hash, bp_bundle_ident_equal);
}

static void bp_cleanup(void) {}

/// Re-initialize after a configuration change
static void bp_reinit_config(void) {}


static gpointer fragment_bundle_ident_temporary_key(
        const packet_info *pinfo _U_, const guint32 id _U_, const void *data) {
    return (bp_bundle_ident_t *)data;
}
static gpointer fragment_bundle_ident_persistent_key(
        const packet_info *pinfo _U_, const guint32 id _U_, const void *data) {
    const bp_bundle_ident_t *ident = (const bp_bundle_ident_t *)data;

    bp_bundle_ident_t *key = g_slice_new0(bp_bundle_ident_t);

    if (ident->src) {
        key->src = g_strdup(ident->src);
    }
    if (ident->ts) {
        key->ts = g_slice_new(bp_creation_ts_t);
        key->ts->abstime = ident->ts->abstime;
        key->ts->seqno = ident->ts->seqno;
    }
    if (ident->frag_offset) {
        key->frag_offset = g_slice_new(guint64);
        key->frag_offset = ident->frag_offset;
    }
    if (ident->total_len) {
        key->total_len = g_slice_new(guint64);
        key->total_len = ident->total_len;
    }
    return key;
}
static void fragment_bundle_ident_free_temporary_key(gpointer ptr _U_) {}
static void fragment_bundle_ident_free_persistent_key(gpointer ptr) {
    bp_bundle_ident_t *key = (bp_bundle_ident_t *)ptr;

    if (key->src) {
        g_free((char *)key->src);
    }
    g_slice_free(bp_creation_ts_t, key->ts);
    g_slice_free(guint64, key->frag_offset);
    g_slice_free(guint64, key->total_len);

    g_slice_free(bp_bundle_ident_t, key);
}
static const reassembly_table_functions bundle_reassembly_table_functions = {
    bp_bundle_ident_hash,
    bp_bundle_ident_equal,
    fragment_bundle_ident_temporary_key,
    fragment_bundle_ident_persistent_key,
    fragment_bundle_ident_free_temporary_key,
    fragment_bundle_ident_free_persistent_key
};

/// Overall registration of the protocol
void proto_register_bpv7(void) {
    proto_bp = proto_register_protocol(
        "DTN Bundle Protocol Version 7", /* name */
        "BPv7", /* short name */
        "bpv7" /* abbrev */
    );
    register_init_routine(&bp_init);
    register_cleanup_routine(&bp_cleanup);

    proto_register_field_array(proto_bp, fields, array_length(fields));
    proto_register_subtree_array(ett, array_length(ett));
    expert_module_t *expert = expert_register_protocol(proto_bp);
    expert_register_field_array(expert, expertitems, array_length(expertitems));

    register_dissector("bpv7", dissect_bp, proto_bp);
    block_dissectors = register_custom_dissector_table("bpv7.block_type", "BPv7 Block", proto_bp, g_int64_hash, g_int64_equal);
    // case-sensitive string matching
    payload_dissectors_dtn_wkssp = register_dissector_table("bpv7.payload.dtn_wkssp", "BPv7 Payload (by well-known SSP)", proto_bp, FT_STRING, FALSE);
    payload_dissectors_dtn_serv = register_dissector_table("bpv7.payload.serv", "BPv7 Payload (by service demux)", proto_bp, FT_STRING, FALSE);

    module_t *module_bp = prefs_register_protocol(proto_bp, bp_reinit_config);
    prefs_register_bool_preference(
        module_bp,
        "bp_compute_crc",
        "Compute and compare CRCs",
        "If enabled, the blocks will have CRC checks performed.",
        &bp_compute_crc
    );
    prefs_register_bool_preference(
        module_bp,
        "bp_reassemble_payload",
        "Reassemble fragmented payloads",
        "Whether the dissector should reassemble fragmented bundle payloads.",
        &bp_reassemble_payload
    );
    prefs_register_bool_preference(
        module_bp,
        "bp_payload_try_heur",
        "Attempt heuristic dissection of BTSD/payload",
        "When dissecting block type-specific data and payload and no destination matches, attempt heuristic dissection.",
        &bp_payload_try_heur
    );

    reassembly_table_register(
        &bp_reassembly_table,
        &bundle_reassembly_table_functions
    );

    btsd_heur = register_heur_dissector_list("bpv7.btsd", proto_bp);

    proto_bp_admin = proto_register_protocol(
        "BPv7 Administrative Record", /* name */
        "BPv7 Admin", /* short name */
        "bpv7.admin_rec" /* abbrev */
    );
    handle_admin = create_dissector_handle(dissect_payload_admin, proto_bp_admin);
    admin_dissectors = register_custom_dissector_table("bpv7.admin_record_type", "BPv7 Administrative Record Type", proto_bp_admin, g_int64_hash, g_int64_equal);
}

void proto_reg_handoff_bpv7(void) {
    const int proto_cbor = proto_get_id_by_filter_name("cbor");
    heur_dissector_add("bpv7.btsd", btsd_heur_cbor, "CBOR in Bundle BTSD", "cbor_bpv7", proto_cbor, HEURISTIC_ENABLE);

    handle_cbor = find_dissector("cbor");
    handle_cborseq = find_dissector("cborseq");

    /* Packaged extensions */
    {
        guint64 *key = g_new(guint64, 1);
        *key = BP_BLOCKTYPE_PAYLOAD;
        dissector_handle_t hdl = create_dissector_handle(dissect_block_payload, proto_bp);
        dissector_add_custom_table_handle("bpv7.block_type", key, hdl);
    }
    {
        guint64 *key = g_new(guint64, 1);
        *key = BP_BLOCKTYPE_PREV_NODE;
        dissector_handle_t hdl = create_dissector_handle(dissect_block_prev_node, proto_bp);
        dissector_add_custom_table_handle("bpv7.block_type", key, hdl);
    }
    {
        guint64 *key = g_new(guint64, 1);
        *key = BP_BLOCKTYPE_BUNDLE_AGE;
        dissector_handle_t hdl = create_dissector_handle(dissect_block_bundle_age, proto_bp);
        dissector_add_custom_table_handle("bpv7.block_type", key, hdl);
    }
    {
        guint64 *key = g_new(guint64, 1);
        *key = BP_BLOCKTYPE_HOP_COUNT;
        dissector_handle_t hdl = create_dissector_handle(dissect_block_hop_count, proto_bp);
        dissector_add_custom_table_handle("bpv7.block_type", key, hdl);
    }
    {
        guint64 *key = g_new(guint64, 1);
        *key = BP_ADMINTYPE_BUNDLE_STATUS;
        dissector_handle_t hdl = create_dissector_handle(dissect_status_report, proto_bp);
        dissector_add_custom_table_handle("bpv7.admin_record_type", key, hdl);
    }

    bp_reinit_config();
}