38 assert(bundle != NULL);
42 for (
size_t i = 0; i < bundle->block_count; i++)
44 free(bundle->blocks[i].btsd);
45 memset(&bundle->blocks[i], 0,
sizeof(bundle->blocks[i]));
47 if (bundle->primary_block.cbor)
49 free(bundle->primary_block.cbor);
51 memset(bundle, 0,
sizeof(*bundle));
57 if (!bundle_ref || !result_primary_block || !bundle_ref->
data)
63 memset(result_primary_block, 0,
sizeof(*result_primary_block));
64 result_primary_block->
block_count = bundle->block_count;
65 result_primary_block->
field_version = bundle->primary_block.version;
66 result_primary_block->
field_flags = bundle->primary_block.flags;
67 result_primary_block->
field_crc_type = bundle->primary_block.crc_type;
68 result_primary_block->
field_dest_eid = bundle->primary_block.dest_eid;
72 result_primary_block->
field_seq_num = bundle->primary_block.timestamp.seq_num;
73 result_primary_block->
field_lifetime = bundle->primary_block.lifetime;
76 result_primary_block->cbor = bundle->primary_block.cbor;
77 result_primary_block->cbor_len = bundle->primary_block.cbor_len;
82int MockBPA_GetBlockNums(
const BSL_BundleRef_t *bundle_ref,
size_t block_id_array_capacity,
83 uint64_t block_id_array_result[block_id_array_capacity],
size_t *result_count)
85 if (!bundle_ref || !bundle_ref->
data || block_id_array_capacity == 0 || !block_id_array_result || !result_count)
92 for (
size_t i = 0; i < bundle->block_count; i++)
94 block_id_array_result[i] = bundle->blocks[i].blk_num;
96 *result_count = bundle->block_count;
100int MockBPA_GetBlockMetadata(
const BSL_BundleRef_t *bundle_ref, uint64_t block_num,
103 if (!bundle_ref || !result_canonical_block || !bundle_ref->
data)
108 memset(result_canonical_block, 0,
sizeof(*result_canonical_block));
112 for (
size_t i = 0; i < bundle->block_count; i++)
114 if (bundle->blocks[i].blk_num == block_num)
116 found_block = &bundle->blocks[i];
120 if (found_block == NULL)
125 result_canonical_block->
block_num = found_block->blk_num;
126 result_canonical_block->
flags = found_block->flags;
127 result_canonical_block->
crc = found_block->crc_type;
128 result_canonical_block->
type_code = found_block->blk_type;
129 result_canonical_block->
btsd = found_block->btsd;
130 result_canonical_block->
btsd_len = found_block->btsd_len;
134int MockBPA_ReallocBTSD(
BSL_BundleRef_t *bundle_ref, uint64_t block_num,
size_t bytesize)
136 if (!bundle_ref || !bundle_ref->
data || block_num == 0 || bytesize == 0)
143 for (
size_t found_index = 0; found_index < bundle->block_count; found_index++)
145 if (bundle->blocks[found_index].blk_num == block_num)
147 found_block = &bundle->blocks[found_index];
151 if (found_block == NULL)
156 if (found_block->btsd == NULL)
158 found_block->btsd = calloc(1, bytesize);
159 found_block->btsd_len = bytesize;
163 found_block->btsd = realloc(found_block->btsd, bytesize);
164 found_block->btsd_len = bytesize;
168 return (found_block->btsd == NULL) ? -9 : 0;
171int MockBPA_CreateBlock(
BSL_BundleRef_t *bundle_ref, uint64_t block_type_code, uint64_t *result_block_num)
173 if (!bundle_ref || !bundle_ref->
data || !result_block_num)
178 *result_block_num = 0;
180 if (bundle->block_count >= MockBPA_BUNDLE_MAXBLOCKS)
186 for (
size_t i = 0; i < bundle->block_count; i++)
188 max_id = bundle->blocks[i].blk_num >= max_id ? bundle->blocks[i].blk_num : max_id;
192 memset(new_block, 0,
sizeof(*new_block));
193 new_block->blk_num = max_id + 1;
194 new_block->blk_type = block_type_code;
195 new_block->crc_type = 0;
196 new_block->flags = block_type_code == 12 ? 1 : 0;
197 new_block->btsd = NULL;
198 new_block->btsd_len = 0;
199 *result_block_num = new_block->blk_num;
203int MockBPA_RemoveBlock(
BSL_BundleRef_t *bundle_ref, uint64_t block_num)
205 if (!bundle_ref || !bundle_ref->
data)
212 size_t found_index = 0;
213 for (found_index = 0; found_index < bundle->block_count; found_index++)
215 if (bundle->blocks[found_index].blk_num == block_num)
217 found_block = &bundle->blocks[found_index];
222 if (found_block == NULL)
228 if (found_block->btsd != NULL)
230 free(found_block->btsd);
232 memset(found_block, 0,
sizeof(*found_block));
234 if (bundle->block_count > 1)
236 for (
size_t dst_index = found_index; dst_index < bundle->block_count - 1; dst_index++)
238 printf(
"Shifting block[%lu] (id=%lu, type=%lu) left", dst_index + 1, bundle->blocks[dst_index + 1].blk_num,
239 bundle->blocks[dst_index + 1].blk_type);
245 bundle->block_count--;
251 uint8_t *state = BSL_MALLOC(999);
256 .get_host_eid_fn = MockBPA_GetEid,
257 .bundle_metadata_fn = MockBPA_GetBundleMetadata,
258 .block_metadata_fn = MockBPA_GetBlockMetadata,
259 .bundle_get_block_ids = MockBPA_GetBlockNums,
260 .block_create_fn = MockBPA_CreateBlock,
261 .block_remove_fn = MockBPA_RemoveBlock,
262 .block_realloc_btsd_fn = MockBPA_ReallocBTSD,
272 .eidpat_init = mock_bpa_eidpat_init,
273 .eidpat_deinit = mock_bpa_eidpat_deinit,
274 .eidpat_from_text = mock_bpa_eidpat_from_text,
275 .eidpat_match = mock_bpa_eidpat_match,
Single entry-point include file for all of the BPSec Lib (BSL) frontend API.
Single entry-point include file for all of the "Public" BPSec Lib (BSL) frontend API.
int BSL_HostDescriptors_Set(BSL_HostDescriptors_t desc)
Set the BPA descriptor (callbacks) for this process.
void BSL_HostDescriptors_Get(BSL_HostDescriptors_t *desc)
Copy the BPA descriptor for this process.
void BSL_HostEID_Deinit(BSL_HostEID_t *eid)
De-initialize an abstract EID.
void bsl_mock_bpa_deinit(void)
Clean up the mock BPA for the current process.
int bsl_mock_bpa_init(void)
Register this mock BPA for the current process.
Declarations for Agent initialization.
int bsl_mock_decode_eid(QCBORDecodeContext *dec, BSL_HostEID_t *eid)
Encode a single EID.
Declarations for bundle and block decoding.
void MockBPA_EID_Deinit(void *user_data, BSL_HostEID_t *eid)
Interface for BSL_HostDescriptors_t::eid_deinit.
int MockBPA_EID_Init(void *user_data, BSL_HostEID_t *eid)
Interface for BSL_HostDescriptors_t::eid_init.
int mock_bpa_eid_from_text(BSL_HostEID_t *eid, const char *text, void *user_data)
Interface for BSL_HostDescriptors_t::eid_from_text.
Declarations for EID handling.
Declarations for EID Pattern handling.
int bsl_mock_encode_eid(QCBOREncodeContext *enc, const BSL_HostEID_t *eid)
Encode a single EID.
Declarations for bundle and block encoding.
Reference to a Bundle owned and stored in the host BPA.
void * data
Opaque pointer, not used by the BSL.
Structure containing parsed Canonical Block fields.
uint64_t flags
CBOR-decoded flags field.
uint64_t block_num
CBOR-decoded block number (should always be > 0)
void * btsd
Pointer to BTSD owned by the host BPA.
size_t btsd_len
Length in bytes of the BTSD pointer.
uint64_t crc
CBOR-decoded block CRC.
uint64_t type_code
CBOR-decoded block type code (should be > 0)
void * user_data
User data pointer for callbacks.
Opaque pointer to BPA-specific Endpoint ID storage.
Contains Bundle Primary Block fields and metadata.
uint64_t field_bundle_creation_time
CBOR-decoded bundle creation time.
uint64_t field_adu_length
CBOR-decoded field of ADU length.
uint64_t field_seq_num
CBOR-decoded sequence number.
size_t block_count
Helpful count of total canonical blocks in bundle, not a field of the header.
uint64_t field_lifetime
CBOR-decoded lifetime.
BSL_HostEID_t field_src_node_id
Source in host BPA's internal representation of an EID.
uint64_t field_frag_offset
CBOR-decoded fragment offset (warning, may not be implemented yet).
uint64_t field_crc_type
CBOR-decoded field of Primary Block CRC type.
BSL_HostEID_t field_report_to_eid
Report-to EID in host BPA's internal representation of an EID.
uint64_t field_flags
CBOR-decoded field of bundle processing control flags.
uint64_t field_version
CBOR-decoded field of Primary Block BP version.
BSL_HostEID_t field_dest_eid
Destination in host BPA's internal representation of an EID.