Beispiel #1
0
/**
 * This private function is called by ndn_TlvEncoder_writeNestedTlv to write the TLVs in the body of the Selectors value.
 * @param context This is the ndn_Interest struct pointer which was passed to writeTlv.
 * @param encoder the ndn_TlvEncoder which is calling this.
 * @return 0 for success, else an error code.
 */
static ndn_Error
encodeSelectorsValue(const void *context, struct ndn_TlvEncoder *encoder)
{
  struct ndn_Interest *interest = (struct ndn_Interest *)context;
  ndn_Error error;

  if ((error = ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv
      (encoder, ndn_Tlv_MinSuffixComponents, interest->minSuffixComponents)))
    return error;
  if ((error = ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv
      (encoder, ndn_Tlv_MaxSuffixComponents, interest->maxSuffixComponents)))
    return error;

  if ((error = ndn_TlvEncoder_writeNestedTlv
       (encoder, ndn_Tlv_PublisherPublicKeyLocator, ndn_encodeTlvKeyLocatorValue,
        &interest->keyLocator, 1)))
    return error;

  if (interest->exclude.nEntries > 0) {
    if ((error = ndn_TlvEncoder_writeNestedTlv(encoder, ndn_Tlv_Exclude, encodeExcludeValue, &interest->exclude, 0)))
      return error;
  }

  if ((error = ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlv
      (encoder, ndn_Tlv_ChildSelector, interest->childSelector)))
    return error;

  if (interest->mustBeFresh) {
    if ((error = ndn_TlvEncoder_writeTypeAndLength(encoder, ndn_Tlv_MustBeFresh, 0)))
      return error;
  }
  // else MustBeFresh == false, so nothing to encode.

  return NDN_ERROR_success;
}
Beispiel #2
0
/**
 * This private function is called by ndn_TlvEncoder_writeNestedTlv to write the TLVs in the body of the Exclude value.
 * @param context This is the ndn_Exclude struct pointer which was passed to writeTlv.
 * @param encoder the ndn_TlvEncoder which is calling this.
 * @return 0 for success, else an error code.
 */
static ndn_Error
encodeExcludeValue(const void *context, struct ndn_TlvEncoder *encoder)
{
  struct ndn_Exclude *exclude = (struct ndn_Exclude *)context;

  // TODO: Do we want to order the components (except for ANY)?
  ndn_Error error;
  size_t i;
  for (i = 0; i < exclude->nEntries; ++i) {
    struct ndn_ExcludeEntry *entry = &exclude->entries[i];

    if (entry->type == ndn_Exclude_COMPONENT) {
      if ((error = ndn_encodeTlvNameComponent(&entry->component, encoder)))
        return error;
    }
    else if (entry->type == ndn_Exclude_ANY) {
      if ((error = ndn_TlvEncoder_writeTypeAndLength(encoder, ndn_Tlv_Any, 0)))
        return error;
    }
    else
      return NDN_ERROR_unrecognized_ndn_ExcludeType;
  }

  return NDN_ERROR_success;
}
Beispiel #3
0
 void
 writeTypeAndLength(unsigned int type, size_t length)
 {
   ndn_Error error;
   if ((error = ndn_TlvEncoder_writeTypeAndLength(this, type, length)))
     throw std::runtime_error(ndn_getErrorString(error));
 }
Beispiel #4
0
ndn_Error
ndn_encodeTlvName
  (const struct ndn_Name *name, size_t *signedPortionBeginOffset,
   size_t *signedPortionEndOffset, struct ndn_TlvEncoder *encoder)
{
  size_t nameValueLength = 0;
  size_t i;
  ndn_Error error;

  for (i = 0; i < name->nComponents; ++i)
    nameValueLength += ndn_TlvEncoder_sizeOfBlobTlv(ndn_Tlv_NameComponent, &name->components[i].value);

  if ((error = ndn_TlvEncoder_writeTypeAndLength(encoder, ndn_Tlv_Name, nameValueLength)))
    return error;

  *signedPortionBeginOffset = encoder->offset;

  if (name->nComponents == 0)
    // There is no "final component", so set signedPortionEndOffset arbitrarily.
    *signedPortionEndOffset = *signedPortionBeginOffset;
  else {
    for (i = 0; i < name->nComponents; ++i) {
      if (i == name->nComponents - 1)
        // We will begin the final component.
        *signedPortionEndOffset = encoder->offset;

      if ((error = ndn_TlvEncoder_writeBlobTlv
           (encoder, ndn_Tlv_NameComponent, &name->components[i].value)))
        return error;
    }
  }

  return NDN_ERROR_success;
}
Beispiel #5
0
/**
 * This private function is called by ndn_TlvEncoder_writeNestedTlv to write the TLVs in the body of the MetaInfo value.
 * @param context This is the ndn_MetaInfo struct pointer which was passed to writeTlv.
 * @param encoder the ndn_TlvEncoder which is calling this.
 * @return 0 for success, else an error code.
 */
static ndn_Error
encodeMetaInfoValue(const void *context, struct ndn_TlvEncoder *encoder)
{
  struct ndn_MetaInfo *metaInfo = (struct ndn_MetaInfo *)context;

  ndn_Error error;

  if (!((int)metaInfo->type < 0 || metaInfo->type == ndn_ContentType_BLOB)) {
    // Not the default, so we need to encode the type.
    if (metaInfo->type == ndn_ContentType_LINK ||
        metaInfo->type == ndn_ContentType_KEY ||
        metaInfo->type == ndn_ContentType_NACK) {
      // The ContentType enum is set up with the correct integer for each NDN-TLV ContentType.
      if ((error = ndn_TlvEncoder_writeNonNegativeIntegerTlv
          (encoder, ndn_Tlv_ContentType, metaInfo->type)))
        return error;
    }
    else if (metaInfo->type == ndn_ContentType_OTHER_CODE) {
      if ((error = ndn_TlvEncoder_writeNonNegativeIntegerTlv
          (encoder, ndn_Tlv_ContentType, metaInfo->otherTypeCode)))
        return error;
    }
    else
      // We don't expect this to happen.
      return NDN_ERROR_unrecognized_ndn_ContentType;
  }

  if ((error = ndn_TlvEncoder_writeOptionalNonNegativeIntegerTlvFromDouble
      (encoder, ndn_Tlv_FreshnessPeriod, metaInfo->freshnessPeriod)))
    return error;
  if (metaInfo->finalBlockId.value.value &&
      metaInfo->finalBlockId.value.length > 0) {
    // The FinalBlockId has an inner NameComponent.
    if ((error = ndn_TlvEncoder_writeTypeAndLength
         (encoder, ndn_Tlv_FinalBlockId, ndn_TlvEncoder_sizeOfBlobTlv
            (metaInfo->finalBlockId.type, &metaInfo->finalBlockId.value))))
      return error;
    if ((error = ndn_encodeTlvNameComponent(&metaInfo->finalBlockId, encoder)))
      return error;
  }

  return NDN_ERROR_success;
}
Beispiel #6
0
ndn_Error
ndn_encodeTlvName
  (const struct ndn_Name *name, size_t *signedPortionBeginOffset,
   size_t *signedPortionEndOffset, struct ndn_TlvEncoder *encoder)
{
  size_t nameValueLength = 0;
  size_t i;
  ndn_Error error;

  for (i = 0; i < name->nComponents; ++i) {
    const struct ndn_NameComponent *component = &name->components[i];
    // Get the type the same as in ndn_encodeTlvNameComponent.
    unsigned int type;
    if (component->type == ndn_NameComponentType_OTHER_CODE)
      type = (unsigned int)component->otherTypeCode;
    else
      // The enum values are the same as the TLV type codes.
      type = (unsigned int)component->type;

    nameValueLength += ndn_TlvEncoder_sizeOfBlobTlv(type, &component->value);
  }

  if ((error = ndn_TlvEncoder_writeTypeAndLength(encoder, ndn_Tlv_Name, nameValueLength)))
    return error;

  *signedPortionBeginOffset = encoder->offset;

  if (name->nComponents == 0)
    // There is no "final component", so set signedPortionEndOffset arbitrarily.
    *signedPortionEndOffset = *signedPortionBeginOffset;
  else {
    for (i = 0; i < name->nComponents; ++i) {
      if (i == name->nComponents - 1)
        // We will begin the final component.
        *signedPortionEndOffset = encoder->offset;

      if ((error = ndn_encodeTlvNameComponent(&name->components[i], encoder)))
        return error;
    }
  }

  return NDN_ERROR_success;
}