34 static uint8_t bundle_decode_block(
struct mmem *bundlemem, uint8_t *buffer,
int max_len);
35 static int bundle_encode_block(
struct bundle_block_t *block, uint8_t *buffer, uint8_t max_len);
41 struct bundle_slot_t *bs;
44 bs = bundleslot_get_free();
47 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Could not allocate slot for a bundle");
54 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Could not allocate memory for a bundle");
59 memset(bundle, 0,
sizeof(
struct bundle_t));
60 bundle->rec_time=(uint32_t) clock_seconds();
61 bundle->num_blocks = 0;
70 struct bundle_block_t *block;
74 n =
mmem_realloc(bundlemem, bundlemem->size + d_len +
sizeof(
struct bundle_block_t));
84 block = (
struct bundle_block_t *) bundle->block_data;
85 for (i=0;i<bundle->num_blocks;i++) {
87 block->flags &= ~BUNDLE_BLOCK_FLAG_LAST;
88 block = (
struct bundle_block_t *) &block->payload[block->block_size];
92 block->flags = BUNDLE_BLOCK_FLAG_LAST |
flags;
93 block->block_size = d_len;
97 memcpy(block->payload, data, d_len);
105 struct bundle_block_t *block = (
struct bundle_block_t *) bundle->block_data;
107 if (i >= bundle->num_blocks)
111 block = (
struct bundle_block_t *) &block->payload[block->block_size];
120 struct bundle_block_t *block = (
struct bundle_block_t *) bundle->block_data;
123 for(i=0; i<bundle->num_blocks; i++) {
124 if( block->type == type ) {
128 block = (
struct bundle_block_t *) &block->payload[block->block_size];
141 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_DBG,
"set attr %lx",*val);
144 bundle->flags = *val;
145 bundle->custody = 0x08 &(uint8_t) *val;
148 bundle->dst_node = *val;
151 bundle->dst_srv = *val;
154 bundle->src_node = *val;
157 bundle->src_srv = *val;
160 bundle->rep_node = *val;
163 bundle->rep_srv = *val;
166 bundle->cust_node = *val;
169 bundle->cust_srv = *val;
172 bundle->tstamp = *val;
174 case TIME_STAMP_SEQ_NR:
175 bundle->tstamp_seq = *val;
178 bundle->lifetime = *val;
182 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Dictionary length needs to be 0 for CBHE");
185 bundle->frag_offs = *val;
190 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Unknown attribute");
199 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_DBG,
"get attr: %d in %lx", attr, *val);
202 *val = bundle->flags;
205 *val = bundle->dst_node;
208 *val = bundle->dst_srv;
211 *val = bundle->src_node;
214 *val = bundle->src_srv;
217 *val = bundle->rep_node;
220 *val = bundle->rep_srv;
223 *val = bundle->cust_node;
226 *val = bundle->cust_srv;
229 *val = bundle->tstamp;
231 case TIME_STAMP_SEQ_NR:
232 *val = bundle->tstamp_seq;
235 *val = bundle->lifetime;
241 *val = bundle->frag_offs;
246 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Unknown attribute");
254 uint32_t primary_size, value;
256 struct mmem *bundlemem;
266 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_DBG,
"rec bptr: %p blptr:%p",bundle,buffer);
269 if (buffer[0] != 0x06) {
270 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Version 0x%02x not supported", buffer[0]);
276 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->flags);
280 offs +=
sdnv_decode(&buffer[offs], size-offs, &primary_size);
281 primary_size += offs;
284 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->dst_node);
285 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->dst_srv);
288 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->src_node);
289 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->src_srv);
292 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->rep_node);
293 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->rep_srv);
296 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->cust_node);
297 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->cust_srv);
300 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->tstamp);
303 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->tstamp_seq);
306 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->lifetime);
309 offs +=
sdnv_decode(&buffer[offs], size-offs, &value);
311 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Bundle does not use CBHE.");
315 if (bundle->flags & BUNDLE_FLAG_FRAGMENT) {
316 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_INF,
"Bundle is a fragment");
319 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->frag_offs);
322 offs +=
sdnv_decode(&buffer[offs], size-offs, &bundle->app_len);
325 if (offs != primary_size) {
326 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Problem decoding the primary bundle block.");
331 while (size-offs > 1) {
332 ret = bundle_decode_block(bundlemem, &buffer[offs], size-offs);
350 static uint8_t bundle_decode_block(
struct mmem *bundlemem, uint8_t *buffer,
int max_len)
352 uint8_t type, block_offs, offs = 0;
353 uint32_t
flags, size;
355 struct bundle_block_t *block;
362 offs +=
sdnv_decode(&buffer[offs], max_len-offs, &flags);
365 offs +=
sdnv_decode(&buffer[offs], max_len-offs, &size);
366 if (size > max_len-offs) {
367 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Bundle payload length too big.");
371 block_offs = bundlemem->size;
373 n =
mmem_realloc(bundlemem, bundlemem->size +
sizeof(
struct bundle_block_t) + size);
375 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_ERR,
"Bundle payload length too big for MMEM.");
380 bundle->num_blocks++;
383 block = (
struct bundle_block_t *)((uint8_t *)bundle + block_offs);
385 block->flags =
flags;
386 block->block_size = size;
389 memcpy(block->payload, &buffer[offs], block->block_size);
391 return offs + block->block_size;
397 uint8_t offs = 0, blklen_offs, i;
398 int ret, blklen_size;
400 struct bundle_block_t *block;
407 ret =
sdnv_encode(bundle->flags, &buffer[offs], max_len - offs);
418 ret =
sdnv_encode(bundle->dst_node, &buffer[offs], max_len - offs);
423 ret =
sdnv_encode(bundle->dst_srv, &buffer[offs], max_len - offs);
429 ret =
sdnv_encode(bundle->src_node, &buffer[offs], max_len - offs);
434 ret =
sdnv_encode(bundle->src_srv, &buffer[offs], max_len - offs);
440 ret =
sdnv_encode(bundle->rep_node, &buffer[offs], max_len - offs);
445 ret =
sdnv_encode(bundle->rep_srv, &buffer[offs], max_len - offs);
451 ret =
sdnv_encode(bundle->cust_node, &buffer[offs], max_len - offs);
456 ret =
sdnv_encode(bundle->cust_srv, &buffer[offs], max_len - offs);
462 ret =
sdnv_encode(bundle->tstamp, &buffer[offs], max_len - offs);
468 ret =
sdnv_encode(bundle->tstamp_seq, &buffer[offs], max_len - offs);
474 ret =
sdnv_encode(bundle->lifetime, &buffer[offs], max_len - offs);
480 ret =
sdnv_encode(0l, &buffer[offs], max_len - offs);
485 if (bundle->flags & BUNDLE_FLAG_FRAGMENT) {
486 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_INF,
"Bundle is a fragment");
489 ret =
sdnv_encode(bundle->frag_offs, &buffer[offs], max_len - offs);
495 ret =
sdnv_encode(bundle->app_len, &buffer[offs], max_len - offs);
502 value = offs - blklen_offs - 1;
505 if (blklen_size > 1) {
506 memmove(&buffer[blklen_offs+blklen_size], &buffer[blklen_offs+1], value);
508 ret =
sdnv_encode(value, &buffer[blklen_offs], blklen_size);
512 block = (
struct bundle_block_t *) bundle->block_data;
513 for (i=0;i<bundle->num_blocks;i++) {
514 offs += bundle_encode_block(block, &buffer[offs], max_len - offs);
516 block = (
struct bundle_block_t *) &block->payload[block->block_size];
522 static int bundle_encode_block(
struct bundle_block_t *block, uint8_t *buffer, uint8_t max_len)
529 buffer[offs] = block->type;
533 ret =
sdnv_encode(block->flags, &buffer[offs], max_len - offs);
539 value = block->block_size;
540 ret =
sdnv_encode(value, &buffer[offs], max_len - offs);
546 memcpy(&buffer[offs], block->payload, block->block_size);
547 offs += block->block_size;
554 struct bundle_slot_t *bs;
556 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_DBG,
"bundle_increment(%p) %u", bundle, bundle->rec_time);
558 bs = container_of(bundlemem,
struct bundle_slot_t, bundle);
559 return bundleslot_increment(bs);
564 struct bundle_slot_t *bs;
566 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_DBG,
"bundle_decrement(%p) %u", bundle, bundle->rec_time);
568 bs = container_of(bundlemem,
struct bundle_slot_t, bundle);
569 return bundleslot_decrement(bs);
574 struct bundle_slot_t *bs;
576 LOG(LOGD_DTN, LOG_BUNDLE, LOGL_DBG,
"delete %p %u", bundle,bundle->rec_time);
578 bs = container_of(bundlemem,
struct bundle_slot_t, bundle);
585 dest.u8[1] = (eid & 0x000000FF) >> 0;
586 dest.u8[0] = (eid & 0x0000FF00) >> 8;
592 eid = (dest->u8[1] & 0xFF) + ((dest->u8[0] & 0xFF) << 8);