void adm_build_mid_str(uint8_t flag, char *nn, int nn_len, int offset, uint8_t *mid_str) { uint8_t *cursor = NULL; Sdnv len; Sdnv off; uint32_t nn_size; uint8_t *tmp = NULL; int size = 0; DTNMP_DEBUG_ENTRY("adm_build_mid_str", "(%d, %s, %d, %d)", flag, nn, nn_len, offset); encodeSdnv(&len, nn_len + 1); encodeSdnv(&off, offset); tmp = utils_string_to_hex((unsigned char*)nn, &nn_size); size = 1 + nn_size + len.length + off.length + 1; if(size > ADM_MID_ALLOC) { DTNMP_DEBUG_ERR("adm_build_mid_str", "Size %d bigger than max MID size of %d.", size, ADM_MID_ALLOC); DTNMP_DEBUG_EXIT("adm_build_mid_str","->.", NULL); MRELEASE(tmp); return; } cursor = mid_str; memcpy(cursor, &flag, 1); cursor += 1; memcpy(cursor, len.text, len.length); cursor += len.length; memcpy(cursor, tmp, nn_size); cursor += nn_size; memcpy(cursor, off.text, off.length); cursor += off.length; memset(cursor, 0, 1); // NULL terminator. DTNMP_DEBUG_EXIT("adm_build_mid_str","->%s", mid_str); MRELEASE(tmp); return; }
/** * \brief serializes a register agent message into a buffer. * * \author Ed Birrane * * \note The returned message must be de-allocated from the memory pool. * * \return NULL - Failure * !NULL - The serialized message. * * \param[in] msg The message to serialize. * \param[out] len The length of the serialized message. */ uint8_t *msg_serialize_reg_agent(adm_reg_agent_t *msg, uint32_t *len) { Sdnv id; uint8_t *result = NULL; uint8_t *cursor = NULL; DTNMP_DEBUG_ENTRY("msg_serialize_reg_agent","(0x%x, 0x%x)", (unsigned long)msg, (unsigned long) len); /* Step 0: Sanity Checks. */ if((msg == NULL) || (len == NULL)) { DTNMP_DEBUG_ERR("msg_serialize_reg_agent","Bad Args",NULL); DTNMP_DEBUG_EXIT("msg_serialize_reg_agent","->NULL",NULL); return NULL; } /* * STEP 1: Figure out the size of the entire message. That includes the * length of the header, acl list, SDNV holding length, and data. */ int id_len = strlen(msg->agent_id.name); encodeSdnv(&id,id_len); *len = id.length + id_len; /* STEP 4: Allocate the serialized message. */ if((result = (uint8_t*)MTAKE(*len)) == NULL) { DTNMP_DEBUG_ERR("msg_serialize_reg_agent","Can't alloc %d bytes", *len); *len = 0; DTNMP_DEBUG_EXIT("msg_serialize_reg_agent","->NULL",NULL); return NULL; } /* Step 5: Populate the serialized message. */ cursor = result; memcpy(cursor, id.text, id.length); cursor += id.length; memcpy(cursor, msg->agent_id.name, id_len); cursor += id_len; /* Step 6: Last sanity check. */ if((cursor - result) != *len) { DTNMP_DEBUG_ERR("msg_serialize_reg_agent","Wrote %d bytes but allcated %d", (unsigned long) (cursor - result), *len); *len = 0; MRELEASE(result); DTNMP_DEBUG_EXIT("msg_serialize_reg_agent","->NULL",NULL); return NULL; } DTNMP_DEBUG_EXIT("msg_serialize_reg_agent","->0x%x",(unsigned long)result); return result; }
expr_result_t ion_node_get_outducts(Lyst params) { char names[2048]; char *ptrs[128]; int num = 0; Sdnv nm_sdnv; uint8_t *cursor = NULL; expr_result_t result; result.type = EXPR_TYPE_BLOB; result.length = 0; result.value = NULL; bpnm_outductNames_get((char *) names, ptrs, &num); encodeSdnv(&nm_sdnv, num); result.length = nm_sdnv.length + /* NUM as SDNV length */ strlen(ptrs[num-1]) + /* length of last string */ (ptrs[num-1] - names) + /* # bytes to get to last string */ 1; /* Final NULL terminator. */ result.value = (uint8_t *) STAKE(result.length); cursor = result.value; memcpy(cursor,nm_sdnv.text, nm_sdnv.length); cursor += nm_sdnv.length; memcpy(cursor, names, result.length - nm_sdnv.length); return result; }
int ltpei_add_xmit_trailer_extension(LtpXmitSeg *segment, char tag, int valueLength, char *value) { Sdr sdr = getIonsdr(); Sdnv sdnv; LtpExtensionOutbound extension; Object addr; CHKERR(segment); CHKERR(ionLocked()); encodeSdnv(&sdnv, valueLength); if (segment->pdu.trailerExtensions == 0) { if ((segment->pdu.trailerExtensions = sdr_list_create(sdr)) == 0) { return -1; /* No space in SDR heap. */ } } extension.tag = tag; extension.length = valueLength; if (valueLength == 0) { extension.value = 0; } else { CHKERR(value); extension.value = sdr_insert(sdr, value, valueLength); if (extension.value == 0) { return -1; /* No space in SDR heap. */ } } if ((addr = sdr_insert(sdr, (char *) &extension, sizeof(LtpExtensionOutbound))) == 0) { return -1; /* No space in SDR heap. */ } if (sdr_list_insert_last(sdr, segment->pdu.trailerExtensions, addr) == 0) { return -1; /* No space in SDR heap. */ } segment->pdu.trailerExtensionsCount++; segment->pdu.trailerLength += (1 + sdnv.length + valueLength); return 0; }
expr_result_t ltp_get_engines(Lyst params) { unsigned int ids[32]; uint8_t *cursor = NULL; int num = 0; unsigned long val = 0; Sdnv num_sdnv; expr_result_t result; result.type = EXPR_TYPE_BLOB; ltpnm_spanEngineIds_get(ids, &num); if(num > 32) { fprintf(stderr,"We do not support more than 32 engines. Aborting.\n"); exit(1); } encodeSdnv(&num_sdnv, num); result.length = num_sdnv.length + /* NUM as SDNV length */ (num * sizeof(unsigned long)); result.value = (uint8_t *) STAKE(result.length); cursor = result.value; memcpy(cursor,num_sdnv.text, num_sdnv.length); cursor += num_sdnv.length; for(int i = 0; i < num; i++) { val = ids[i]; memcpy(cursor,&val, sizeof(val)); cursor += sizeof(val); } return result; }
int mid_internal_serialize(mid_t *mid) { Sdnv iss; Sdnv tag; uint32_t oid_size = 0; uint8_t *oid_val = NULL; uint8_t *cursor = NULL; DTNMP_DEBUG_ENTRY("mid_internal_serialize","(%#llx)",(unsigned long) mid); /* Step 0: Sanity Check. */ if(mid == NULL) { DTNMP_DEBUG_ERR("mid_internal_serialize","Bad args.",NULL); DTNMP_DEBUG_EXIT("mid_internal_serialize","->0",NULL); return 0; } /* Step 1: Serialize the OID for sizing. */ if((oid_val = oid_serialize(mid->oid, &oid_size)) == NULL) { DTNMP_DEBUG_ERR("mid_internal_serialize","Can't serialize OID.",NULL); DTNMP_DEBUG_EXIT("mid_internal_serialize","->0",NULL); return 0; } /* Step 2: If there is a serialized version of the MID already, wipe it.*/ if(mid->raw != NULL) { MRELEASE(mid->raw); mid->raw = NULL; } mid->raw_size = 0; /* Step 3: Build the SDNVs. */ encodeSdnv(&iss, mid->issuer); encodeSdnv(&tag, mid->tag); /* Step 4: Figure out the size of the serialized MID. */ mid->raw_size = 1 + oid_size; /* Add issuer if present. */ if(MID_GET_FLAG_ISS(mid->flags)) { mid->raw_size += iss.length; } /* Add tag if present. */ if(MID_GET_FLAG_TAG(mid->flags)) { mid->raw_size += tag.length; } /* Step 5: Allocate space for the serialized MID. */ if((mid->raw = (uint8_t *)MTAKE(mid->raw_size)) == NULL) { DTNMP_DEBUG_ERR("mid_internal_serialize","Can't alloc %d bytes.", mid->raw_size); mid->raw_size = 0; MRELEASE(oid_val); DTNMP_DEBUG_EXIT("mid_internal_serialize","->0",NULL); return 0; } /* Step 6: Populate the serialized MID. */ cursor = mid->raw; *cursor = mid->flags; cursor++; /* Add issuer if present. */ if(MID_GET_FLAG_ISS(mid->flags)) { memcpy(cursor, iss.text, iss.length); cursor += iss.length; } /* Copy the OID. */ memcpy(cursor,oid_val, oid_size); cursor += oid_size; /* Add tag if present. */ if(MID_GET_FLAG_TAG(mid->flags)) { memcpy(cursor, tag.text, tag.length); cursor += tag.length; } /* Step 7: Final sanity check. */ if((cursor - mid->raw) != mid->raw_size) { DTNMP_DEBUG_ERR("mid_internal_serialize","Copied %d bytes, expected %d bytes.", (cursor - mid->raw), mid->raw_size); mid->raw_size = 0; MRELEASE(mid->raw); mid->raw = NULL; MRELEASE(oid_val); DTNMP_DEBUG_EXIT("mid_internal_serialize","->0",NULL); return 0; } DTNMP_DEBUG_EXIT("mid_internal_serialize","->1",NULL); return 1; }
uint8_t *midcol_serialize(Lyst mids, uint32_t *size) { uint8_t *result = NULL; Sdnv num_sdnv; LystElt elt; DTNMP_DEBUG_ENTRY("midcol_serialize","(%#llx, %#llx)", (unsigned long) mids, (unsigned long) size); /* Step 0: Sanity Check */ if((mids == NULL) || (size == NULL)) { DTNMP_DEBUG_ERR("midcol_serialize","Bad args.", NULL); DTNMP_DEBUG_EXIT("midcol_serialize","->NULL",NULL); return NULL; } /* Step 1: Calculate the size. */ /* Consider the size of the SDNV holding # MIDS.*/ encodeSdnv(&num_sdnv, lyst_length(mids)); *size = num_sdnv.length; /* Walk through each MID, make sure it is serialized, and look at size. */ for(elt = lyst_first(mids); elt; elt = lyst_next(elt)) { mid_t *cur_mid = (mid_t *) lyst_data(elt); if(cur_mid != NULL) { if((cur_mid->raw == NULL) || (cur_mid->raw_size == 0)) { /* \todo check return code. */ mid_internal_serialize(cur_mid); } if((cur_mid->raw == NULL) || (cur_mid->raw_size == 0)) { DTNMP_DEBUG_WARN("midcol_serialize","MID didn't serialize.", NULL); } else { *size += cur_mid->raw_size; } } else { DTNMP_DEBUG_WARN("midcol_serialize","Found NULL MID?", NULL); } } /* Step 3: Allocate the space for the serialized list. */ if((result = (uint8_t*) MTAKE(*size)) == NULL) { DTNMP_DEBUG_ERR("midcol_serialize","Can't alloc %d bytes", *size); *size = 0; DTNMP_DEBUG_EXIT("midcol_serialize","->NULL",NULL); return NULL; } /* Step 4: Walk through list again copying as we go. */ uint8_t *cursor = result; /* COpy over the number of MIDs in the collection. */ memcpy(cursor, num_sdnv.text, num_sdnv.length); cursor += num_sdnv.length; for(elt = lyst_first(mids); elt; elt = lyst_next(elt)) { mid_t *cur_mid = (mid_t *) lyst_data(elt); if(cur_mid != NULL) { if((cur_mid->raw != NULL) && (cur_mid->raw_size > 0)) { memcpy(cursor,cur_mid->raw, cur_mid->raw_size); cursor += cur_mid->raw_size; } } else { DTNMP_DEBUG_WARN("midcol_serialize","Found NULL MID?", NULL); } } /* Step 5: Final sanity check. */ if((cursor - result) != *size) { DTNMP_DEBUG_ERR("midcol_serialize","Wrote %d bytes not %d bytes", (cursor - result), *size); *size = 0; MRELEASE(result); DTNMP_DEBUG_EXIT("midcol_serialize","->NULL",NULL); return NULL; } DTNMP_DEBUG_EXIT("midcol_serialize","->%#llx",(unsigned long) result); return result; }
uint8_t *msg_serialize_stat_msg(adm_stat_msg_t *msg, uint32_t *len) { uint8_t *result = NULL; uint8_t *cursor = NULL; uint8_t *code = NULL; uint32_t code_size = 0; uint8_t *list = NULL; uint32_t list_size = 0; Sdnv time; DTNMP_DEBUG_ENTRY("msg_serialize_stat_msg","(0x%x, 0x%x)", (unsigned long)msg, (unsigned long) len); /* Step 0: Sanity Checks. */ if((msg == NULL) || (len == NULL)) { DTNMP_DEBUG_ERR("msg_serialize_stat_msg","Bad Args",NULL); DTNMP_DEBUG_EXIT("msg_serialize_stat_msg","->NULL",NULL); return NULL; } /* STEP 3: Serialize the Code. */ if((code = mid_serialize(msg->code, &code_size)) == NULL) { DTNMP_DEBUG_ERR("msg_serialize_stat_msg","Can't serialize code.", NULL); DTNMP_DEBUG_EXIT("msg_serialize_stat_msg","->NULL",NULL); return NULL; } /* STEP 4: Serialize the MID Collection. */ if((list = midcol_serialize(msg->generators, &list_size)) == NULL) { DTNMP_DEBUG_ERR("msg_serialize_stat_msg","Can't serialize code.", NULL); MRELEASE(code); DTNMP_DEBUG_EXIT("msg_serialize_stat_msg","->NULL",NULL); return NULL; } /* Step 5: Build the timestamp SDNV. */ encodeSdnv(&time, msg->time); /* STEP 6: Figure out the size of the entire message. */ *len = code_size + list_size + time.length; /* STEP 7: Allocate the serialized message. */ if((result = (uint8_t*)MTAKE(*len)) == NULL) { DTNMP_DEBUG_ERR("msg_serialize_stat_msg","Can't alloc %d bytes", *len); *len = 0; MRELEASE(code); MRELEASE(list); DTNMP_DEBUG_EXIT("msg_serialize_stat_msg","->NULL",NULL); return NULL; } /* Step 8: Populate the serialized message. */ cursor = result; memcpy(cursor,code,code_size); cursor += code_size; MRELEASE(code); memcpy(cursor, time.text, time.length); cursor += time.length; memcpy(cursor, list, list_size); cursor += list_size; MRELEASE(list); /* Step 9: Last sanity check. */ if((cursor - result) != *len) { DTNMP_DEBUG_ERR("msg_serialize_stat_msg","Wrote %d bytes but alloc %d", (unsigned long) (cursor - result), *len); *len = 0; MRELEASE(result); DTNMP_DEBUG_EXIT("msg_serialize_stat_msg","->NULL",NULL); return NULL; } DTNMP_DEBUG_EXIT("msg_serialize_stat_msg","->0x%x",(unsigned long)result); return result; }
uint8_t *ctrl_serialize(ctrl_exec_t *ctrl, uint32_t *len) { uint8_t *result = NULL; uint8_t *cursor = NULL; Sdnv time_sdnv; Sdnv status_sdnv; uint8_t *contents = NULL; uint32_t contents_len = 0; AMP_DEBUG_ENTRY("ctrl_serialize","(0x%x, 0x%x)", (unsigned long)ctrl, (unsigned long) len); /* Step 0: Sanity Checks. */ if((ctrl == NULL) || (len == NULL)) { AMP_DEBUG_ERR("ctrl_serialize","Bad Args",NULL); AMP_DEBUG_EXIT("ctrl_serialize","->NULL",NULL); return NULL; } *len = 0; /* Step 1: Serialize contents individually. */ encodeSdnv(&time_sdnv, ctrl->time); encodeSdnv(&status_sdnv, ctrl->status); if((contents = mid_serialize(ctrl->mid, &contents_len)) == NULL) { AMP_DEBUG_ERR("ctrl_serialize","Can't serialize MID.",NULL); AMP_DEBUG_EXIT("ctrl_serialize","->NULL",NULL); return NULL; } /* Step 2: Figure out the length. */ *len = time_sdnv.length + status_sdnv.length + sizeof(eid_t) + contents_len; /* STEP 3: Allocate the serialized message. */ if((result = (uint8_t*)STAKE(*len)) == NULL) { AMP_DEBUG_ERR("ctrl_serialize","Can't alloc %d bytes", *len); *len = 0; SRELEASE(contents); AMP_DEBUG_EXIT("ctrl_serialize","->NULL",NULL); return NULL; } /* Step 4: Populate the serialized message. */ cursor = result; memcpy(cursor,time_sdnv.text,time_sdnv.length); cursor += time_sdnv.length; memcpy(cursor,status_sdnv.text,status_sdnv.length); cursor += status_sdnv.length; memcpy(cursor,&(ctrl->sender),sizeof(eid_t)); cursor += sizeof(eid_t); memcpy(cursor, contents, contents_len); cursor += contents_len; SRELEASE(contents); /* Step 5: Last sanity check. */ if((cursor - result) != *len) { AMP_DEBUG_ERR("ctrl_serialize","Wrote %d bytes but allcated %d", (unsigned long) (cursor - result), *len); *len = 0; SRELEASE(result); AMP_DEBUG_EXIT("ctrl_serialize","->NULL",NULL); return NULL; } AMP_DEBUG_EXIT("ctrl_serialize","->0x%x",(unsigned long)result); return result; }