Пример #1
0
ndn_Error
ndn_decodeTlvSignatureInfo
(struct ndn_Signature *signatureInfo, struct ndn_TlvDecoder *decoder)
{
    ndn_Error error;
    size_t beginOffset;
    size_t endOffset;
    uint64_t signatureType;
    int gotExpectedType;

    ndn_Signature_clear(signatureInfo);

    beginOffset = decoder->offset;
    if ((error = ndn_TlvDecoder_readNestedTlvsStart
                 (decoder, ndn_Tlv_SignatureInfo, &endOffset)))
        return error;

    if ((error = ndn_TlvDecoder_readNonNegativeIntegerTlv
                 (decoder, ndn_Tlv_SignatureType, &signatureType)))
        return error;

    if (signatureType == ndn_Tlv_SignatureType_SignatureSha256WithRsa ||
            signatureType == ndn_Tlv_SignatureType_SignatureSha256WithEcdsa ||
            signatureType == ndn_Tlv_SignatureType_SignatureHmacWithSha256) {
        // Assume that the ndn_SignatureType enum has the same values as the TLV
        // signature types.
        signatureInfo->type = signatureType;
        if ((error = ndn_decodeTlvKeyLocator
                     (ndn_Tlv_KeyLocator, &signatureInfo->keyLocator, decoder)))
            return error;
        if ((error = ndn_TlvDecoder_peekType
                     (decoder, ndn_Tlv_ValidityPeriod_ValidityPeriod, endOffset,
                      &gotExpectedType)))
            return error;
        if (gotExpectedType) {
            if ((error = decodeValidityPeriod(&signatureInfo->validityPeriod, decoder)))
                return error;
        }
    }
    else if (signatureType == ndn_Tlv_SignatureType_DigestSha256)
        signatureInfo->type = ndn_SignatureType_DigestSha256Signature;
    else {
        signatureInfo->type = ndn_SignatureType_Generic;
        signatureInfo->genericTypeCode = signatureType;

        // Get the bytes of the SignatureInfo TLV.
        if ((error = ndn_TlvDecoder_getSlice
                     (decoder, beginOffset, endOffset, &signatureInfo->signatureInfoEncoding)))
            return error;
    }

    if ((error = ndn_TlvDecoder_finishNestedTlvs(decoder, endOffset)))
        return error;

    return NDN_ERROR_success;
}
Пример #2
0
ndn_Error
ndn_decodeTlvLpPacket
  (struct ndn_LpPacket *lpPacket, struct ndn_TlvDecoder *decoder)
{
  ndn_Error error;
  size_t endOffset;

  ndn_LpPacket_clear(lpPacket);

  if ((error = ndn_TlvDecoder_readNestedTlvsStart
       (decoder, ndn_Tlv_LpPacket_LpPacket, &endOffset)))
    return error;

  while (decoder->offset < endOffset) {
    // Imitate ndn_TlvDecoder_readTypeAndLength.
    uint64_t fieldTypeVarNumber;
    int fieldType;
    uint64_t fieldLength;
    size_t fieldEndOffset;

    if ((error = ndn_TlvDecoder_readVarNumber(decoder, &fieldTypeVarNumber)))
      return error;
    fieldType = (int)fieldTypeVarNumber;
    if ((error = ndn_TlvDecoder_readVarNumber(decoder, &fieldLength)))
      return error;
    fieldEndOffset = decoder->offset + (size_t)fieldLength;
    if (fieldEndOffset > decoder->inputLength)
      return NDN_ERROR_TLV_length_exceeds_buffer_length;

    if (fieldType == ndn_Tlv_LpPacket_Fragment) {
      // Set the fragment to the bytes of the TLV value.
      if ((error = ndn_TlvDecoder_getSlice
           (decoder, decoder->offset, fieldEndOffset,
            &lpPacket->fragmentWireEncoding)))
        return error;
      ndn_TlvDecoder_seek(decoder, fieldEndOffset);

      // The fragment is supposed to be the last field.
      break;
    }
    else if (fieldType == ndn_Tlv_LpPacket_Nack) {
      struct ndn_NetworkNack *networkNack;
      int code;

      if ((error = ndn_NetworkNack_add(lpPacket, &networkNack)))
        return error;
      if ((error = ndn_TlvDecoder_readOptionalNonNegativeIntegerTlv
           (decoder, ndn_Tlv_LpPacket_NackReason, fieldEndOffset, &code)))
        return error;
      // The enum numeric values are the same as this wire format, so use as is.
      if (code < 0 || code == ndn_NetworkNackReason_NONE)
        // This includes an omitted NackReason.
        networkNack->reason = ndn_NetworkNackReason_NONE;
      else if (code == ndn_NetworkNackReason_CONGESTION ||
               code == ndn_NetworkNackReason_DUPLICATE ||
               code == ndn_NetworkNackReason_NO_ROUTE)
        networkNack->reason = code;
      else {
        // Unrecognized reason.
        networkNack->reason = ndn_NetworkNackReason_OTHER_CODE;
        networkNack->otherReasonCode = code;
      }
    }
    else if (fieldType == ndn_Tlv_LpPacket_IncomingFaceId) {
      struct ndn_IncomingFaceId *incomingFaceId;

      if ((error = ndn_IncomingFaceId_add(lpPacket, &incomingFaceId)))
        return error;
      if ((error = ndn_TlvDecoder_readNonNegativeInteger
           (decoder, fieldLength, &incomingFaceId->faceId)))
        return error;
    }
    else {
      // Unrecognized field type. The conditions for ignoring are here:
      // http://redmine.named-data.net/projects/nfd/wiki/NDNLPv2
      int canIgnore =
        (fieldType >= ndn_Tlv_LpPacket_IGNORE_MIN &&
         fieldType <= ndn_Tlv_LpPacket_IGNORE_MAX &&
         (fieldType & 0x01) == 1);
      if (!canIgnore)
        return NDN_ERROR_did_not_get_the_expected_TLV_type;

      // Ignore.
      ndn_TlvDecoder_seek(decoder, fieldEndOffset);
    }

    if ((error = ndn_TlvDecoder_finishNestedTlvs(decoder, fieldEndOffset)))
      return error;
  }

  if ((error = ndn_TlvDecoder_finishNestedTlvs(decoder, endOffset)))
    return error;

  return NDN_ERROR_success;
}
Пример #3
0
static ndn_Error
ndn_decodeTlvInterestV02
  (struct ndn_Interest *interest, size_t *signedPortionBeginOffset,
   size_t *signedPortionEndOffset, struct ndn_TlvDecoder *decoder)
{
  ndn_Error error;
  size_t endOffset;
  int gotExpectedType;

  if ((error = ndn_TlvDecoder_readNestedTlvsStart(decoder, ndn_Tlv_Interest, &endOffset)))
    return error;

  if ((error = ndn_decodeTlvName
       (&interest->name, signedPortionBeginOffset, signedPortionEndOffset,
        decoder)))
    return error;

  if ((error = ndn_TlvDecoder_peekType(decoder, ndn_Tlv_Selectors, endOffset, &gotExpectedType)))
    return error;
  if (gotExpectedType) {
    if ((error = decodeSelectors(interest, decoder)))
      return error;
  }
  else {
    // Set selectors to none.
    interest->minSuffixComponents = -1;
    interest->maxSuffixComponents = -1;
    ndn_KeyLocator_initialize
      (&interest->keyLocator, interest->keyLocator.keyName.components,
       interest->keyLocator.keyName.maxComponents);
    interest->exclude.nEntries = 0;
    interest->childSelector = -1;
    interest->mustBeFresh = 0;
  }

  // Require a Nonce, but don't force it to be 4 bytes.
  if ((error = ndn_TlvDecoder_readBlobTlv(decoder, ndn_Tlv_Nonce, &interest->nonce)))
    return error;

  if ((error = ndn_TlvDecoder_readOptionalNonNegativeIntegerTlvAsDouble
       (decoder, ndn_Tlv_InterestLifetime, endOffset, &interest->interestLifetimeMilliseconds)))
    return error;

  // Get the encoded sequence of delegations as is.
  if ((error = ndn_TlvDecoder_readOptionalBlobTlv
       (decoder, ndn_Tlv_ForwardingHint, endOffset,
        &interest->forwardingHintWireEncoding)))
    return error;

  if ((error = ndn_TlvDecoder_peekType(decoder, ndn_Tlv_Data, endOffset, &gotExpectedType)))
    return error;
  if (gotExpectedType) {
    // Get the bytes of the Link TLV.
    size_t linkBeginOffset = decoder->offset;
    size_t linkEndOffset;
    if ((error = ndn_TlvDecoder_readNestedTlvsStart
         (decoder, ndn_Tlv_Data, &linkEndOffset)))
      return error;
    ndn_TlvDecoder_seek(decoder, linkEndOffset);

    if ((error = ndn_TlvDecoder_getSlice
         (decoder, linkBeginOffset, linkEndOffset, &interest->linkWireEncoding)))
      return error;
  }
  else
    ndn_Blob_initialize(&interest->linkWireEncoding, 0, 0);
  if ((error = ndn_TlvDecoder_readOptionalNonNegativeIntegerTlv
       (decoder, ndn_Tlv_SelectedDelegation, endOffset,
        &interest->selectedDelegationIndex)))
    return error;
  if (interest->selectedDelegationIndex >= 0 && !interest->linkWireEncoding.value)
    return NDN_ERROR_Interest_has_a_selected_delegation_but_no_link_object;

  if ((error = ndn_TlvDecoder_finishNestedTlvs(decoder, endOffset)))
    return error;

  return NDN_ERROR_success;
}