blob: bca450731707397e39693369ac6532704aff0d1f [file] [log] [blame]
Jeff Thompson5cae5e52013-07-10 19:41:20 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompson56ec9e22013-08-02 11:34:07 -07006#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07007#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -07008
Jeff Thompson46bd45f2013-08-08 16:46:41 -07009#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "name.hpp"
11#include "publisher-public-key-digest.hpp"
12#include "key.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070013#include "c/data.h"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070014
15namespace ndn {
16
Jeff Thompsonf4585af2013-09-11 14:56:59 -070017/**
18 * A Signature holds the signature bits and other info representing the signature in a data packet.
19 */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020class Signature {
21public:
22 /**
23 * Set the signatureStruct to point to the values in this signature object, without copying any memory.
24 * WARNING: The resulting pointers in signatureStruct are invalid after a further use of this object which could reallocate memory.
25 * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
26 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070027 void get(struct ndn_Signature& signatureStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070028
29 /**
30 * Clear this signature, and set the values by copying from the ndn_Signature struct.
31 * @param signatureStruct a C ndn_Signature struct
32 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070033 void set(const struct ndn_Signature& signatureStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070034
Jeff Thompson4545d712013-09-12 14:40:12 -070035 const Blob& getDigestAlgorithm() const { return digestAlgorithm_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070036
Jeff Thompson4545d712013-09-12 14:40:12 -070037 const Blob& getWitness() const { return witness_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070038
Jeff Thompson4545d712013-09-12 14:40:12 -070039 const Blob& getSignature() const { return signature_; }
Jeff Thompsonf4585af2013-09-11 14:56:59 -070040
41 const PublisherPublicKeyDigest& getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; }
42 PublisherPublicKeyDigest& getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; }
43
44 const KeyLocator& getKeyLocator() const { return keyLocator_; }
45 KeyLocator& getKeyLocator() { return keyLocator_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070046
Jeff Thompson1656e6a2013-08-29 18:01:48 -070047 void setDigestAlgorithm(const std::vector<unsigned char>& digestAlgorithm) { digestAlgorithm_ = digestAlgorithm; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070048 void setDigestAlgorithm(const unsigned char *digestAlgorithm, unsigned int digestAlgorithmLength)
49 {
Jeff Thompson4545d712013-09-12 14:40:12 -070050 digestAlgorithm_ = Blob(digestAlgorithm, digestAlgorithmLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070051 }
52
Jeff Thompson1656e6a2013-08-29 18:01:48 -070053 void setWitness(const std::vector<unsigned char>& witness) { witness_ = witness; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070054 void setWitness(const unsigned char *witness, unsigned int witnessLength)
55 {
Jeff Thompson4545d712013-09-12 14:40:12 -070056 witness_ = Blob(witness, witnessLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070057 }
58
Jeff Thompson1656e6a2013-08-29 18:01:48 -070059 void setSignature(const std::vector<unsigned char>& signature) { signature_ = signature; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070060 void setSignature(const unsigned char *signature, unsigned int signatureLength)
61 {
Jeff Thompson4545d712013-09-12 14:40:12 -070062 signature_ = Blob(signature, signatureLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070063 }
Jeff Thompsonf4585af2013-09-11 14:56:59 -070064
65 void setPublisherPublicKeyDigest(const PublisherPublicKeyDigest& publisherPublicKeyDigest) { publisherPublicKeyDigest_ = publisherPublicKeyDigest; }
66
67 void setKeyLocator(const KeyLocator& keyLocator) { keyLocator_ = keyLocator; }
Jeff Thompsonf842d212013-08-12 11:05:28 -070068
69 /**
Jeff Thompsonf4585af2013-09-11 14:56:59 -070070 * Clear all the fields.
Jeff Thompsonf842d212013-08-12 11:05:28 -070071 */
72 void clear()
73 {
Jeff Thompson4545d712013-09-12 14:40:12 -070074 digestAlgorithm_.reset();
75 witness_.reset();
76 signature_.reset();
Jeff Thompsonf4585af2013-09-11 14:56:59 -070077 publisherPublicKeyDigest_.clear();
78 keyLocator_.clear();
Jeff Thompsonf842d212013-08-12 11:05:28 -070079 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070080
Jeff Thompson5cae5e52013-07-10 19:41:20 -070081private:
Jeff Thompson4545d712013-09-12 14:40:12 -070082 Blob digestAlgorithm_; /**< if empty, the default is 2.16.840.1.101.3.4.2.1 (sha-256) */
83 Blob witness_;
84 Blob signature_;
Jeff Thompsonf4585af2013-09-11 14:56:59 -070085 PublisherPublicKeyDigest publisherPublicKeyDigest_;
86 KeyLocator keyLocator_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070087};
88
Jeff Thompsonf4585af2013-09-11 14:56:59 -070089/**
90 * An MetaInfo holds the meta info which is signed inside the data packet.
91 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070092class MetaInfo {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070093public:
Jeff Thompsonfec716d2013-09-11 13:54:36 -070094 MetaInfo()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070095 {
96 type_ = ndn_ContentType_DATA;
97 freshnessSeconds_ = -1;
98 }
99
100 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700101 * Set the metaInfoStruct to point to the values in this meta info object, without copying any memory.
102 * WARNING: The resulting pointers in metaInfoStruct are invalid after a further use of this object which could reallocate memory.
103 * @param metaInfoStruct a C ndn_MetaInfo struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700104 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700105 void get(struct ndn_MetaInfo& metaInfoStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700106
107 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700108 * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct.
109 * @param metaInfoStruct a C ndn_MetaInfo struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700110 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700111 void set(const struct ndn_MetaInfo& metaInfoStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700112
Jeff Thompson210b92f2013-07-11 15:16:03 -0700113 double getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700114
Jeff Thompsond8776352013-08-16 18:09:30 -0700115 ndn_ContentType getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700116
117 int getFreshnessSeconds() const { return freshnessSeconds_; }
118
Jeff Thompson85db6d72013-09-12 12:41:18 -0700119 const Name::Component& getFinalBlockID() const { return finalBlockID_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700120
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700121 void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
122
Jeff Thompsond8776352013-08-16 18:09:30 -0700123 void setType(ndn_ContentType type) { type_ = type; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700124
125 void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
126
Jeff Thompson85db6d72013-09-12 12:41:18 -0700127 void setFinalBlockID(const std::vector<unsigned char>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700128 void setFinalBlockID(const unsigned char *finalBlockID, unsigned int finalBlockIdLength)
129 {
Jeff Thompson85db6d72013-09-12 12:41:18 -0700130 finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700131 }
132
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700133private:
Jeff Thompson210b92f2013-07-11 15:16:03 -0700134 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
Jeff Thompsond8776352013-08-16 18:09:30 -0700135 ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
Jeff Thompson210b92f2013-07-11 15:16:03 -0700136 int freshnessSeconds_; /**< -1 for none */
Jeff Thompson85db6d72013-09-12 12:41:18 -0700137 Name::Component finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700138};
139
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700140class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700141public:
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700142 Data()
143 {
144 }
145
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700146 Data(const Name& name)
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700147 : name_(name)
148 {
149 }
150
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700151 Blob wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700152 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700153 return wireFormat.encodeData(*this);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700154 }
Jeff Thompsona7516e02013-09-11 17:12:25 -0700155 void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700156 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700157 wireFormat.decodeData(*this, input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700158 }
Jeff Thompsona7516e02013-09-11 17:12:25 -0700159 void wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700160 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700161 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700162 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700163
164 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700165 * Set the dataStruct to point to the values in this interest, without copying any memory.
166 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
167 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700168 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700169 void get(struct ndn_Data& dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700170
171 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700172 * Clear this data object, and set the values by copying from the ndn_Data struct.
173 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700174 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700175 void set(const struct ndn_Data& dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700176
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700177 const Signature& getSignature() const { return signature_; }
178 Signature& getSignature() { return signature_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700179
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700180 const Name& getName() const { return name_; }
181 Name& getName() { return name_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700182
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700183 const MetaInfo& getMetaInfo() const { return metaInfo_; }
184 MetaInfo& getMetaInfo() { return metaInfo_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700185
Jeff Thompson6a513332013-09-12 13:23:58 -0700186 const Blob& getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700187
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700188 void setSignature(const Signature& signature) { signature_ = signature; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700189
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700190 void setName(const Name& name) { name_ = name; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700191
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700192 void setMetainfo(const MetaInfo& metaInfo) { metaInfo_ = metaInfo; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700193
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700194 /**
195 * Set the content to a copy of the data in the vector.
196 * @param content A vector whose contents are copied.
197 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700198 void setContent(const std::vector<unsigned char>& content) { content_ = content; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700199 void setContent(const unsigned char *content, unsigned int contentLength)
200 {
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700201 content_ = Blob(content, contentLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700202 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700203
204 /**
205 * Set content to point to an existing byte array. IMPORTANT: After calling this,
206 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
207 * @param content A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
208 */
209 void setContent(const ptr_lib::shared_ptr<std::vector<unsigned char> > &content) { content_ = content; }
210 void setContent(const ptr_lib::shared_ptr<const std::vector<unsigned char> > &content) { content_ = content; }
211
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700212private:
213 Signature signature_;
214 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700215 MetaInfo metaInfo_;
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700216 Blob content_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700217};
218
219}
220
221#endif