blob: cde73028d472be43374a4ef7da5dba8d4294058d [file] [log] [blame]
Jeff Thompson5cae5e52013-07-10 19:41:20 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07004 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompson56ec9e22013-08-02 11:34:07 -07007#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07008#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -07009
Jeff Thompson46bd45f2013-08-08 16:46:41 -070010#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "name.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070012#include "key.hpp"
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070013#include "util/signed-blob.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070014#include "c/data.h"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070015
16namespace ndn {
17
Jeff Thompsonf4585af2013-09-11 14:56:59 -070018/**
Jeff Thompson20af0732013-09-12 17:01:45 -070019 * A Signature is an abstract base class providing an methods to work with the signature information in a Data packet.
Jeff Thompsonf4585af2013-09-11 14:56:59 -070020 */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070021class Signature {
22public:
23 /**
Jeff Thompson20af0732013-09-12 17:01:45 -070024 * Return a pointer to a new Signature which is a copy of this signature.
25 * This is pure virtual, the subclass must implement it.
26 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070027 virtual ptr_lib::shared_ptr<Signature>
28 clone() const = 0;
Jeff Thompson20af0732013-09-12 17:01:45 -070029
30 /**
31 * The virtual destructor.
32 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070033 virtual
34 ~Signature();
Jeff Thompson20af0732013-09-12 17:01:45 -070035
36 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070037 * Set the signatureStruct to point to the values in this signature object, without copying any memory.
38 * WARNING: The resulting pointers in signatureStruct are invalid after a further use of this object which could reallocate memory.
Jeff Thompson20af0732013-09-12 17:01:45 -070039 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070040 * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
41 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070042 virtual void
43 get(struct ndn_Signature& signatureStruct) const = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070044
45 /**
46 * Clear this signature, and set the values by copying from the ndn_Signature struct.
Jeff Thompson20af0732013-09-12 17:01:45 -070047 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070048 * @param signatureStruct a C ndn_Signature struct
49 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070050 virtual void
51 set(const struct ndn_Signature& signatureStruct) = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070052};
53
Jeff Thompsonf4585af2013-09-11 14:56:59 -070054/**
55 * An MetaInfo holds the meta info which is signed inside the data packet.
56 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070057class MetaInfo {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070058public:
Jeff Thompsonfec716d2013-09-11 13:54:36 -070059 MetaInfo()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070060 {
61 type_ = ndn_ContentType_DATA;
62 freshnessSeconds_ = -1;
63 }
64
65 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070066 * Set the metaInfoStruct to point to the values in this meta info object, without copying any memory.
67 * WARNING: The resulting pointers in metaInfoStruct are invalid after a further use of this object which could reallocate memory.
68 * @param metaInfoStruct a C ndn_MetaInfo struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070069 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070070 void
71 get(struct ndn_MetaInfo& metaInfoStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070072
73 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070074 * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct.
75 * @param metaInfoStruct a C ndn_MetaInfo struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -070076 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070077 void
78 set(const struct ndn_MetaInfo& metaInfoStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070079
Jeff Thompson0050abe2013-09-17 12:50:25 -070080 double
81 getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070082
Jeff Thompson0050abe2013-09-17 12:50:25 -070083 ndn_ContentType
84 getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070085
Jeff Thompson0050abe2013-09-17 12:50:25 -070086 int
87 getFreshnessSeconds() const { return freshnessSeconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070088
Jeff Thompson0050abe2013-09-17 12:50:25 -070089 const Name::Component&
90 getFinalBlockID() const { return finalBlockID_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070091
Jeff Thompson0050abe2013-09-17 12:50:25 -070092 void
93 setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070094
Jeff Thompson0050abe2013-09-17 12:50:25 -070095 void
96 setType(ndn_ContentType type) { type_ = type; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070097
Jeff Thompson0050abe2013-09-17 12:50:25 -070098 void
99 setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700100
Jeff Thompson0050abe2013-09-17 12:50:25 -0700101 void
102 setFinalBlockID(const std::vector<unsigned char>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
103
104 void
105 setFinalBlockID(const unsigned char* finalBlockID, unsigned int finalBlockIdLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700106 {
Jeff Thompson85db6d72013-09-12 12:41:18 -0700107 finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700108 }
109
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700110private:
Jeff Thompson210b92f2013-07-11 15:16:03 -0700111 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
Jeff Thompsond8776352013-08-16 18:09:30 -0700112 ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
Jeff Thompson210b92f2013-07-11 15:16:03 -0700113 int freshnessSeconds_; /**< -1 for none */
Jeff Thompson85db6d72013-09-12 12:41:18 -0700114 Name::Component finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700115};
116
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700117class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700118public:
Jeff Thompson20af0732013-09-12 17:01:45 -0700119 /**
120 * Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature.
121 */
122 Data();
123
124 /**
125 * Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature.
126 * @param name A reference to the name which is copied.
127 */
128 Data(const Name& name);
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700129
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700130 /**
131 * Encode this Data for a particular wire format. Also, set the wireEncoding field to the encoded result.
132 * This is not const because it updates the wireEncoding.
133 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
134 * @return The encoded byte array.
135 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700136 SignedBlob
137 wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700138
139 /**
140 * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
141 * @param input The input byte array to be decoded.
142 * @param inputLength The length of input.
143 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
144 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700145 void
146 wireDecode(const unsigned char* input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700147
148 /**
149 * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
150 * @param input The input byte array to be decoded.
151 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
152 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700153 void
154 wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700155 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700156 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700157 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700158
159 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700160 * Set the dataStruct to point to the values in this interest, without copying any memory.
161 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
162 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700163 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700164 void
165 get(struct ndn_Data& dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700166
167 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700168 * Clear this data object, and set the values by copying from the ndn_Data struct.
169 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700170 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700171 void
172 set(const struct ndn_Data& dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700173
Jeff Thompson0050abe2013-09-17 12:50:25 -0700174 const Signature*
175 getSignature() const { return signature_.get(); }
176
177 Signature*
178 getSignature()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700179 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700180 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700181 onChanged();
182 return signature_.get();
183 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700184
Jeff Thompson0050abe2013-09-17 12:50:25 -0700185 const Name&
186 getName() const { return name_; }
187
188 Name&
189 getName()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700190 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700191 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700192 onChanged();
193 return name_;
194 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700195
Jeff Thompson0050abe2013-09-17 12:50:25 -0700196 const MetaInfo&
197 getMetaInfo() const { return metaInfo_; }
198
199 MetaInfo&
200 getMetaInfo()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700201 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700202 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700203 onChanged();
204 return metaInfo_;
205 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700206
Jeff Thompson0050abe2013-09-17 12:50:25 -0700207 const Blob&
208 getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700209
Jeff Thompson20af0732013-09-12 17:01:45 -0700210 /**
211 * Set the signature to a copy of the given signature.
212 * @param signature The signature object which is cloned.
213 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700214 void
215 setSignature(const Signature& signature)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700216 {
217 signature_ = signature.clone();
218 onChanged();
219 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700220
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700221 /**
222 * Set name to a copy of the given Name.
223 * @param name The Name which is copied.
224 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700225 void
226 setName(const Name& name)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700227 {
228 name_ = name;
229 onChanged();
230 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700231
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700232 /**
233 * Set metaInfo to a copy of the given MetaInfo.
234 * @param metaInfo The MetaInfo which is copied.
235 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700236 void
237 setMetainfo(const MetaInfo& metaInfo)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700238 {
239 metaInfo_ = metaInfo;
240 onChanged();
241 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700242
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700243 /**
244 * Set the content to a copy of the data in the vector.
245 * @param content A vector whose contents are copied.
246 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700247 void
248 setContent(const std::vector<unsigned char>& content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700249 {
250 content_ = content;
251 onChanged();
252 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700253 void
254 setContent(const unsigned char* content, unsigned int contentLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700255 {
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700256 content_ = Blob(content, contentLength);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700257 onChanged();
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700258 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700259
260 /**
261 * Set content to point to an existing byte array. IMPORTANT: After calling this,
262 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
263 * @param content A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
264 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700265 void
266 setContent(const ptr_lib::shared_ptr<std::vector<unsigned char> > &content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700267 {
268 content_ = content;
269 onChanged();
270 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700271
272 void
273 setContent(const ptr_lib::shared_ptr<const std::vector<unsigned char> > &content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700274 {
275 content_ = content;
276 onChanged();
277 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700278
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700279private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700280 /**
281 * Clear the wire encoding.
282 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700283 void
284 onChanged();
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700285
Jeff Thompson20af0732013-09-12 17:01:45 -0700286 ptr_lib::shared_ptr<Signature> signature_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700287 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700288 MetaInfo metaInfo_;
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700289 Blob content_;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700290 SignedBlob wireEncoding_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700291};
292
293}
294
295#endif