data: Converting Data packet and related fields to TLV
Change-Id: Ie7a6db7352384f14a830046e65a953a2d9bbb71c
diff --git a/include/ndn-cpp/data.hpp b/include/ndn-cpp/data.hpp
index a227c7f..1fb098f 100644
--- a/include/ndn-cpp/data.hpp
+++ b/include/ndn-cpp/data.hpp
@@ -10,51 +10,94 @@
#include "common.hpp"
#include "name.hpp"
-#include "util/signed-blob.hpp"
-#include "c/data-types.h"
-#include "encoding/wire-format.hpp"
+#include "encoding/block.hpp"
-struct ndn_MetaInfo;
-struct ndn_Signature;
-struct ndn_Data;
namespace ndn {
/**
- * A Signature is an abstract base class providing methods to work with the signature information in a Data packet.
- * You must create an object of a subclass, for example Sha256WithRsaSignature.
+ * A Signature is storage for the signature-related information (info and value) in a Data packet.
*/
class Signature {
public:
- /**
- * Return a pointer to a new Signature which is a copy of this signature.
- * This is pure virtual, the subclass must implement it.
- */
- virtual ptr_lib::shared_ptr<Signature>
- clone() const = 0;
+ enum {
+ DigestSha256 = 0,
+ SignatureSha256WithRsa = 1
+ };
- /**
- * The virtual destructor.
- */
- virtual
- ~Signature();
+ Signature()
+ : type_(-1)
+ {
+ }
- /**
- * Set the signatureStruct to point to the values in this signature object, without copying any memory.
- * WARNING: The resulting pointers in signatureStruct are invalid after a further use of this object which could reallocate memory.
- * This is pure virtual, the subclass must implement it.
- * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
- */
- virtual void
- get(struct ndn_Signature& signatureStruct) const = 0;
+ Signature(const Block &info, const Block &value)
+ : info_(info)
+ , value_(value)
+ {
+ Buffer::const_iterator i = info_.value_begin();
+ type_ = Tlv::readVarNumber(i, info_.value_end());
+ }
- /**
- * Clear this signature, and set the values by copying from the ndn_Signature struct.
- * This is pure virtual, the subclass must implement it.
- * @param signatureStruct a C ndn_Signature struct
- */
- virtual void
- set(const struct ndn_Signature& signatureStruct) = 0;
+ operator bool() const
+ {
+ return type_ != -1;
+ }
+
+ uint32_t
+ getType() const
+ {
+ return type_;
+ }
+
+ const Block&
+ getInfo() const
+ {
+ return info_;
+ }
+
+ void
+ setInfo(const Block &info)
+ {
+ info_ = info;
+ if (info_.hasWire() || info_.hasValue())
+ {
+ info_.parse();
+ const Block &signatureType = info_.get(Tlv::SignatureType);
+
+ Buffer::const_iterator i = signatureType.value_begin();
+ type_ = Tlv::readVarNumber(i, signatureType.value_end());
+ }
+ else
+ {
+ type_ = -1;
+ }
+ }
+
+ const Block&
+ getValue() const
+ {
+ return value_;
+ }
+
+ void
+ setValue(const Block &value)
+ {
+ value_ = value;
+ }
+
+ void
+ reset()
+ {
+ type_ = -1;
+ info_.reset();
+ value_.reset();
+ }
+
+private:
+ int32_t type_;
+
+ Block info_;
+ Block value_;
};
/**
@@ -62,56 +105,39 @@
*/
class MetaInfo {
public:
- MetaInfo()
+ enum {
+ TYPE_DEFAULT = 0,
+ TYPE_LINK = 1,
+ TYPE_KEY = 2
+ };
+
+ MetaInfo()
+ : type_(TYPE_DEFAULT)
+ , freshnessPeriod_(-1)
{
- type_ = ndn_ContentType_DATA;
- freshnessSeconds_ = -1;
}
+
+ uint32_t
+ getType() const
+ { return type_; }
+
+ void
+ setType(uint32_t type)
+ { type_ = type; }
+
+ Milliseconds
+ getFreshnessPeriod() const
+ { return freshnessPeriod_; }
+
+ void
+ setFreshnessPeriod(Milliseconds freshnessPeriod)
+ { freshnessPeriod_ = freshnessPeriod; }
- /**
- * Set the metaInfoStruct to point to the values in this meta info object, without copying any memory.
- * WARNING: The resulting pointers in metaInfoStruct are invalid after a further use of this object which could reallocate memory.
- * @param metaInfoStruct a C ndn_MetaInfo struct where the name components array is already allocated.
- */
- void
- get(struct ndn_MetaInfo& metaInfoStruct) const;
-
- /**
- * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct.
- * @param metaInfoStruct a C ndn_MetaInfo struct
- */
- void
- set(const struct ndn_MetaInfo& metaInfoStruct);
-
- MillisecondsSince1970
- getTimestampMilliseconds() const { return timestampMilliseconds_; }
-
- ndn_ContentType
- getType() const { return type_; }
-
- int
- getFreshnessSeconds() const { return freshnessSeconds_; }
-
- const Name::Component&
- getFinalBlockID() const { return finalBlockID_; }
-
- void
- setTimestampMilliseconds(MillisecondsSince1970 timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
-
- void
- setType(ndn_ContentType type) { type_ = type; }
-
- void
- setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
-
- void
- setFinalBlockID(const Name::Component& finalBlockID) { finalBlockID_ = finalBlockID; }
-
private:
- MillisecondsSince1970 timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
- ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
- int freshnessSeconds_; /**< -1 for none */
- Name::Component finalBlockID_; /** size 0 for none */
+ uint32_t type_;
+ Milliseconds freshnessPeriod_;
+
+ Block wire_;
};
class Data {
@@ -119,31 +145,25 @@
/**
* Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature.
*/
- Data();
+ Data()
+ {
+ }
/**
* Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature.
* @param name A reference to the name which is copied.
*/
- Data(const Name& name);
-
- /**
- * The copy constructor: Create a deep copy of the given data object, including a clone of the signature object.
- * @param data The data object to copy.
- */
- Data(const Data& data);
+ Data(const Name& name)
+ : name_(name)
+ {
+ }
/**
* The virtual destructor.
*/
- virtual ~Data();
-
- /**
- * The assignment operator: Copy fields and make a clone of the signature.
- * @param data The other object to copy from.
- * @return A reference to this object.
- */
- Data& operator=(const Data& data);
+ virtual ~Data()
+ {
+ }
/**
* Encode this Data for a particular wire format. If wireFormat is the default wire format, also set the defaultWireEncoding
@@ -152,8 +172,11 @@
* @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
* @return The encoded byte array.
*/
- SignedBlob
- wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
+ const Block&
+ wireEncode() const;
+
+ void
+ wireDecode(const Block &wire);
/**
* Decode the input using a particular wire format and update this Data. If wireFormat is the default wire format, also
@@ -162,155 +185,127 @@
* @param inputLength The length of input.
* @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
*/
- void
- wireDecode(const uint8_t* input, size_t inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
-
- /**
- * Decode the input using a particular wire format and update this Data. If wireFormat is the default wire format, also
- * set the defaultWireEncoding field to the input.
- * @param input The input byte array to be decoded.
- * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
- */
- void
- wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
+ void
+ wireDecode(const uint8_t* input, size_t inputLength);
+
+ const Signature&
+ getSignature() const
{
- wireDecode(&input[0], input.size(), wireFormat);
+ return signature_;
}
/**
- * Set the dataStruct to point to the values in this interest, without copying any memory.
- * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
- * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
- */
- void
- get(struct ndn_Data& dataStruct) const;
-
- /**
- * Clear this data object, and set the values by copying from the ndn_Data struct.
- * @param dataStruct a C ndn_Data struct
- */
- void
- set(const struct ndn_Data& dataStruct);
-
- const Signature*
- getSignature() const { return signature_.get(); }
-
- Signature*
- getSignature()
- {
- // TODO: Should add an OnChanged listener instead of always calling onChanged.
- onChanged();
- return signature_.get();
- }
-
- const Name&
- getName() const { return name_; }
-
- Name&
- getName()
- {
- // TODO: Should add an OnChanged listener instead of always calling onChanged.
- onChanged();
- return name_;
- }
-
- const MetaInfo&
- getMetaInfo() const { return metaInfo_; }
-
- MetaInfo&
- getMetaInfo()
- {
- // TODO: Should add an OnChanged listener instead of always calling onChanged.
- onChanged();
- return metaInfo_;
- }
-
- const Blob&
- getContent() const { return content_; }
-
- /**
- * Return a pointer to the defaultWireEncoding. It may be null.
- */
- const SignedBlob&
- getDefaultWireEncoding() const { return defaultWireEncoding_; }
-
- /**
* Set the signature to a copy of the given signature.
* @param signature The signature object which is cloned.
* @return This Data so that you can chain calls to update values.
*/
Data&
setSignature(const Signature& signature)
- {
- signature_ = signature.clone();
+ {
+ signature_ = signature;
onChanged();
return *this;
}
+ const Name&
+ getName() const
+ {
+ return name_;
+ }
+
/**
* Set name to a copy of the given Name. This is virtual so that a subclass can override to validate the name.
* @param name The Name which is copied.
* @return This Data so that you can chain calls to update values.
*/
- virtual Data&
- setName(const Name& name);
+ void
+ setName(const Name& name)
+ {
+ name_ = name;
+ onChanged();
+ }
+
+ const MetaInfo&
+ getMetaInfo() const { return metaInfo_; }
/**
* Set metaInfo to a copy of the given MetaInfo.
* @param metaInfo The MetaInfo which is copied.
* @return This Data so that you can chain calls to update values.
*/
- Data&
+ void
setMetaInfo(const MetaInfo& metaInfo)
{
metaInfo_ = metaInfo;
onChanged();
- return *this;
}
+ const Block&
+ getContent() const { return content_; }
+
/**
* Set the content to a copy of the data in the vector.
* @param content A vector whose contents are copied.
* @return This Data so that you can chain calls to update values.
*/
- Data&
+ void
setContent(const std::vector<uint8_t>& content)
- {
- content_ = content;
+ {
+ setContent(&content[0], content.size());
onChanged();
- return *this;
}
- Data&
+ void
setContent(const uint8_t* content, size_t contentLength)
- {
- content_ = Blob(content, contentLength);
+ {
+ OBufferStream os;
+ Tlv::writeVarNumber(os, Tlv::Content);
+ Tlv::writeVarNumber(os, contentLength);
+ os.write(reinterpret_cast<const char *>(content), contentLength);
+
+ content_ = Block(os.buf());
onChanged();
- return *this;
}
-
- Data&
- setContent(const Blob& content)
+
+ void
+ setContent(const ConstBufferPtr &contentValue)
+ {
+ content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet
+ onChanged();
+ }
+
+ void
+ setContent(const Block& content)
{
content_ = content;
onChanged();
- return *this;
}
private:
/**
* Clear the wire encoding.
*/
- void
+ inline void
onChanged();
-
- ptr_lib::shared_ptr<Signature> signature_;
+
+private:
Name name_;
MetaInfo metaInfo_;
- Blob content_;
- SignedBlob defaultWireEncoding_;
+ Block content_;
+ Signature signature_;
+
+ Block wire_;
};
-
+
+
+inline void
+Data::onChanged()
+{
+ // The values have changed, so the signature and wire format is invalidated
+ signature_.reset();
+ wire_.reset();
}
+} // namespace ndn
+
#endif
diff --git a/include/ndn-cpp/key-locator.hpp b/include/ndn-cpp/key-locator.hpp
index 5c1f6c1..f6c6f9d 100644
--- a/include/ndn-cpp/key-locator.hpp
+++ b/include/ndn-cpp/key-locator.hpp
@@ -8,88 +8,71 @@
#ifndef NDN_KEY_LOCATOR_HPP
#define NDN_KEY_LOCATOR_HPP
-#include <vector>
-#include "c/key-types.h"
+#include "encoding/tlv-element.hpp"
#include "name.hpp"
-struct ndn_KeyLocator;
-
namespace ndn {
-
+
+namespace error {
+struct KeyLocator : public std::runtime_error { KeyLocator(const std::string &what) : std::runtime_error(what) {} };
+} // error
+
class KeyLocator {
public:
+ enum {
+ KeyLocator_None = -1,
+ KeyLocator_Name = 0
+ };
+
KeyLocator()
- : type_((ndn_KeyLocatorType)-1), keyNameType_((ndn_KeyNameType)-1)
+ : type_(KeyLocator_None)
{
}
-
- /**
- * Clear the keyData and set the type to none.
- */
- void
- clear()
- {
- type_ = (ndn_KeyLocatorType)-1;
- keyNameType_ = (ndn_KeyNameType)-1;
- keyData_.reset();
- }
-
- /**
- * Set the keyLocatorStruct to point to the values in this key locator, without copying any memory.
- * WARNING: The resulting pointers in keyLocatorStruct are invalid after a further use of this object which could reallocate memory.
- * @param keyLocatorStruct a C ndn_KeyLocator struct where the name components array is already allocated.
- */
- void
- get(struct ndn_KeyLocator& keyLocatorStruct) const;
-
- /**
- * Clear this key locator, and set the values by copying from the ndn_KeyLocator struct.
- * @param keyLocatorStruct a C ndn_KeyLocator struct
- */
- void
- set(const struct ndn_KeyLocator& keyLocatorStruct);
- ndn_KeyLocatorType
+ inline bool
+ empty() const
+ {
+ return type_ == KeyLocator_None;
+ }
+
+ uint32_t
getType() const { return type_; }
- const Blob&
- getKeyData() const { return keyData_; }
-
- const Name&
- getKeyName() const { return keyName_; }
-
- Name&
- getKeyName() { return keyName_; }
-
- ndn_KeyNameType
- getKeyNameType() const { return keyNameType_; }
-
void
- setType(ndn_KeyLocatorType type) { type_ = type; }
+ setType(uint32_t type) { type_ = type; }
+ const Block&
+ getValue() const { return value_; }
+
void
- setKeyData(const Blob& keyData) { keyData_ = keyData; }
+ setValue(const Block &value) { value_ = value; }
+
+ ////////////////////////////////////////////////////////
+ // Helper methods for different types of key locators
+ //
+ // For now only Name type is actually supported
+
+ Name
+ getName() const
+ {
+ if (type_ != KeyLocator_Name)
+ throw error::KeyLocator("Requested Name, but KeyLocator is not of the Name type");
+
+ return Name(getValue());
+ }
void
- setKeyName(const Name &keyName) { keyName_ = keyName; }
+ setName(const Name &name)
+ {
+ type_ = KeyLocator_Name;
+ value_ = name.wireEncode();
+ }
- void
- setKeyNameType(ndn_KeyNameType keyNameType) { keyNameType_ = keyNameType; }
-
private:
- ndn_KeyLocatorType type_; /**< -1 for none */
- Blob keyData_; /**< An array for the key data as follows:
- * If type_ is ndn_KeyLocatorType_KEY, the key data.
- * If type_ is ndn_KeyLocatorType_CERTIFICATE, the certificate data.
- * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST, the publisher public key digest.
- * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST, the publisher certificate digest.
- * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST, the publisher issuer key digest.
- * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST, the publisher issuer certificate digest.
- */
- Name keyName_; /**< The key name (only used if type_ is ndn_KeyLocatorType_KEYNAME.) */
- ndn_KeyNameType keyNameType_; /**< The type of data for keyName_, -1 for none. (only used if type_ is ndn_KeyLocatorType_KEYNAME.) */
+ uint32_t type_;
+ Block value_;
};
-
+
}
#endif
diff --git a/include/ndn-cpp/publisher-public-key-digest.hpp b/include/ndn-cpp/publisher-public-key-digest.hpp
deleted file mode 100644
index 289e682..0000000
--- a/include/ndn-cpp/publisher-public-key-digest.hpp
+++ /dev/null
@@ -1,64 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_PUBLISHERPUBLICKEYDIGEST_HPP
-#define NDN_PUBLISHERPUBLICKEYDIGEST_HPP
-
-#include <vector>
-#include "common.hpp"
-#include "util/blob.hpp"
-
-struct ndn_PublisherPublicKeyDigest;
-
-namespace ndn {
-
-/**
- * A PublisherPublicKeyDigest holds the publisher public key digest value, if any.
- * We make a separate class since this is used by multiple other classes.
- */
-class PublisherPublicKeyDigest {
-public:
- PublisherPublicKeyDigest() {
- }
-
- /**
- * Set the publisherPublicKeyDigestStruct to point to the entries in this PublisherPublicKeyDigest, without copying any memory.
- * WARNING: The resulting pointers in publisherPublicKeyDigestStruct are invalid after a further use of this object which could reallocate memory.
- * @param publisherPublicKeyDigestStruct a C ndn_PublisherPublicKeyDigest struct to receive the pointer
- */
- void
- get(struct ndn_PublisherPublicKeyDigest& publisherPublicKeyDigestStruct) const;
-
- /**
- * Clear this PublisherPublicKeyDigest, and copy from the ndn_PublisherPublicKeyDigest struct.
- * @param excludeStruct a C ndn_Exclude struct
- */
- void
- set(const struct ndn_PublisherPublicKeyDigest& publisherPublicKeyDigestStruct);
-
- const Blob&
- getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
-
- void
- setPublisherPublicKeyDigest(const Blob& publisherPublicKeyDigest) { publisherPublicKeyDigest_ = publisherPublicKeyDigest; }
-
- /**
- * Clear the publisherPublicKeyDigest.
- */
- void
- clear()
- {
- publisherPublicKeyDigest_.reset();
- }
-
-private:
- Blob publisherPublicKeyDigest_;
-};
-
-}
-
-#endif
diff --git a/include/ndn-cpp/sha256-with-rsa-signature.hpp b/include/ndn-cpp/sha256-with-rsa-signature.hpp
deleted file mode 100644
index d2e5374..0000000
--- a/include/ndn-cpp/sha256-with-rsa-signature.hpp
+++ /dev/null
@@ -1,103 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_SHA256_WITH_RSA_SIGNATURE_HPP
-#define NDN_SHA256_WITH_RSA_SIGNATURE_HPP
-
-#include "data.hpp"
-#include "key-locator.hpp"
-#include "publisher-public-key-digest.hpp"
-
-namespace ndn {
-
-/**
- * A Sha256WithRsaSignature extends Signature and holds the signature bits and other info representing a
- * SHA256-with-RSA signature in a data packet.
- */
-class Sha256WithRsaSignature : public Signature {
-public:
- /**
- * Return a pointer to a new Sha256WithRsaSignature which is a copy of this signature.
- */
- virtual ptr_lib::shared_ptr<Signature>
- clone() const;
-
- /**
- * Set the signatureStruct to point to the values in this signature object, without copying any memory.
- * WARNING: The resulting pointers in signatureStruct are invalid after a further use of this object which could reallocate memory.
- * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
- */
- virtual void
- get(struct ndn_Signature& signatureStruct) const;
-
- /**
- * Clear this signature, and set the values by copying from the ndn_Signature struct.
- * @param signatureStruct a C ndn_Signature struct
- */
- virtual void
- set(const struct ndn_Signature& signatureStruct);
-
- const Blob&
- getDigestAlgorithm() const { return digestAlgorithm_; }
-
- const Blob&
- getWitness() const { return witness_; }
-
- const Blob&
- getSignature() const { return signature_; }
-
- const PublisherPublicKeyDigest&
- getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
-
- PublisherPublicKeyDigest&
- getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
-
- const KeyLocator&
- getKeyLocator() const { return keyLocator_; }
-
- KeyLocator&
- getKeyLocator() { return keyLocator_; }
-
- void
- setDigestAlgorithm(const Blob& digestAlgorithm) { digestAlgorithm_ = digestAlgorithm; }
-
- void
- setWitness(const Blob& witness) { witness_ = witness; }
-
- void
- setSignature(const Blob& signature) { signature_ = signature; }
-
- void
- setPublisherPublicKeyDigest(const PublisherPublicKeyDigest& publisherPublicKeyDigest) { publisherPublicKeyDigest_ = publisherPublicKeyDigest; }
-
- void
- setKeyLocator(const KeyLocator& keyLocator) { keyLocator_ = keyLocator; }
-
- /**
- * Clear all the fields.
- */
- void
- clear()
- {
- digestAlgorithm_.reset();
- witness_.reset();
- signature_.reset();
- publisherPublicKeyDigest_.clear();
- keyLocator_.clear();
- }
-
-private:
- Blob digestAlgorithm_; /**< if empty, the default is 2.16.840.1.101.3.4.2.1 (sha-256) */
- Blob witness_;
- Blob signature_;
- PublisherPublicKeyDigest publisherPublicKeyDigest_;
- KeyLocator keyLocator_;
-};
-
-}
-
-#endif