blob: edb991b91640d1b492c16b1b3d4036efb1a0f590 [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 Thompsonb7aefa002013-09-16 18:22:00 -070012#include "util/signed-blob.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/**
Jeff Thompson20af0732013-09-12 17:01:45 -070018 * 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 -070019 */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020class Signature {
21public:
22 /**
Jeff Thompson20af0732013-09-12 17:01:45 -070023 * Return a pointer to a new Signature which is a copy of this signature.
24 * This is pure virtual, the subclass must implement it.
25 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070026 virtual ptr_lib::shared_ptr<Signature>
27 clone() const = 0;
Jeff Thompson20af0732013-09-12 17:01:45 -070028
29 /**
30 * The virtual destructor.
31 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070032 virtual
33 ~Signature();
Jeff Thompson20af0732013-09-12 17:01:45 -070034
35 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070036 * Set the signatureStruct to point to the values in this signature object, without copying any memory.
37 * 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 -070038 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070039 * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
40 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070041 virtual void
42 get(struct ndn_Signature& signatureStruct) const = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070043
44 /**
45 * Clear this signature, and set the values by copying from the ndn_Signature struct.
Jeff Thompson20af0732013-09-12 17:01:45 -070046 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070047 * @param signatureStruct a C ndn_Signature struct
48 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070049 virtual void
50 set(const struct ndn_Signature& signatureStruct) = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070051};
52
Jeff Thompsonf4585af2013-09-11 14:56:59 -070053/**
54 * An MetaInfo holds the meta info which is signed inside the data packet.
55 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070056class MetaInfo {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070057public:
Jeff Thompsonfec716d2013-09-11 13:54:36 -070058 MetaInfo()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070059 {
60 type_ = ndn_ContentType_DATA;
61 freshnessSeconds_ = -1;
62 }
63
64 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070065 * Set the metaInfoStruct to point to the values in this meta info object, without copying any memory.
66 * WARNING: The resulting pointers in metaInfoStruct are invalid after a further use of this object which could reallocate memory.
67 * @param metaInfoStruct a C ndn_MetaInfo struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070068 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070069 void
70 get(struct ndn_MetaInfo& metaInfoStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070071
72 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070073 * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct.
74 * @param metaInfoStruct a C ndn_MetaInfo struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -070075 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070076 void
77 set(const struct ndn_MetaInfo& metaInfoStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078
Jeff Thompson0050abe2013-09-17 12:50:25 -070079 double
80 getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070081
Jeff Thompson0050abe2013-09-17 12:50:25 -070082 ndn_ContentType
83 getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070084
Jeff Thompson0050abe2013-09-17 12:50:25 -070085 int
86 getFreshnessSeconds() const { return freshnessSeconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070087
Jeff Thompson0050abe2013-09-17 12:50:25 -070088 const Name::Component&
89 getFinalBlockID() const { return finalBlockID_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070090
Jeff Thompson0050abe2013-09-17 12:50:25 -070091 void
92 setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070093
Jeff Thompson0050abe2013-09-17 12:50:25 -070094 void
95 setType(ndn_ContentType type) { type_ = type; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070096
Jeff Thompson0050abe2013-09-17 12:50:25 -070097 void
98 setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070099
Jeff Thompson0050abe2013-09-17 12:50:25 -0700100 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700101 setFinalBlockID(const std::vector<uint8_t>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700102
103 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700104 setFinalBlockID(const uint8_t* finalBlockID, size_t finalBlockIdLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700105 {
Jeff Thompson85db6d72013-09-12 12:41:18 -0700106 finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700107 }
108
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700109private:
Jeff Thompson210b92f2013-07-11 15:16:03 -0700110 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
Jeff Thompsond8776352013-08-16 18:09:30 -0700111 ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
Jeff Thompson210b92f2013-07-11 15:16:03 -0700112 int freshnessSeconds_; /**< -1 for none */
Jeff Thompson85db6d72013-09-12 12:41:18 -0700113 Name::Component finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700114};
115
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700116class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700117public:
Jeff Thompson20af0732013-09-12 17:01:45 -0700118 /**
119 * Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature.
120 */
121 Data();
122
123 /**
124 * Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature.
125 * @param name A reference to the name which is copied.
126 */
127 Data(const Name& name);
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700128
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700129 /**
130 * Encode this Data for a particular wire format. Also, set the wireEncoding field to the encoded result.
131 * This is not const because it updates the wireEncoding.
132 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
133 * @return The encoded byte array.
134 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700135 SignedBlob
136 wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700137
138 /**
139 * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
140 * @param input The input byte array to be decoded.
141 * @param inputLength The length of input.
142 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
143 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700144 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700145 wireDecode(const uint8_t* input, size_t inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700146
147 /**
148 * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
149 * @param input The input byte array to be decoded.
150 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
151 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700152 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700153 wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700154 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700155 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700156 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700157
158 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700159 * Set the dataStruct to point to the values in this interest, without copying any memory.
160 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
161 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700162 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700163 void
164 get(struct ndn_Data& dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700165
166 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700167 * Clear this data object, and set the values by copying from the ndn_Data struct.
168 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700169 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700170 void
171 set(const struct ndn_Data& dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700172
Jeff Thompson0050abe2013-09-17 12:50:25 -0700173 const Signature*
174 getSignature() const { return signature_.get(); }
175
176 Signature*
177 getSignature()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700178 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700179 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700180 onChanged();
181 return signature_.get();
182 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700183
Jeff Thompson0050abe2013-09-17 12:50:25 -0700184 const Name&
185 getName() const { return name_; }
186
187 Name&
188 getName()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700189 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700190 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700191 onChanged();
192 return name_;
193 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700194
Jeff Thompson0050abe2013-09-17 12:50:25 -0700195 const MetaInfo&
196 getMetaInfo() const { return metaInfo_; }
197
198 MetaInfo&
199 getMetaInfo()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700200 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700201 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700202 onChanged();
203 return metaInfo_;
204 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700205
Jeff Thompson0050abe2013-09-17 12:50:25 -0700206 const Blob&
207 getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700208
Jeff Thompson20af0732013-09-12 17:01:45 -0700209 /**
Jeff Thompsonb697c5d2013-09-17 17:58:37 -0700210 * Return a pointer to the wireEncoding. It may be null.
211 */
212 const SignedBlob&
213 getWireEncoding() const { return wireEncoding_; }
214
215 /**
Jeff Thompson20af0732013-09-12 17:01:45 -0700216 * Set the signature to a copy of the given signature.
217 * @param signature The signature object which is cloned.
218 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700219 void
220 setSignature(const Signature& signature)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700221 {
222 signature_ = signature.clone();
223 onChanged();
224 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700225
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700226 /**
227 * Set name to a copy of the given Name.
228 * @param name The Name which is copied.
229 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700230 void
231 setName(const Name& name)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700232 {
233 name_ = name;
234 onChanged();
235 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700236
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700237 /**
238 * Set metaInfo to a copy of the given MetaInfo.
239 * @param metaInfo The MetaInfo which is copied.
240 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700241 void
242 setMetainfo(const MetaInfo& metaInfo)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700243 {
244 metaInfo_ = metaInfo;
245 onChanged();
246 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700247
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700248 /**
249 * Set the content to a copy of the data in the vector.
250 * @param content A vector whose contents are copied.
251 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700252 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700253 setContent(const std::vector<uint8_t>& content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700254 {
255 content_ = content;
256 onChanged();
257 }
Jeff Thompson3776d1c2013-09-17 14:22:51 -0700258
Jeff Thompson0050abe2013-09-17 12:50:25 -0700259 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700260 setContent(const uint8_t* content, size_t contentLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700261 {
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700262 content_ = Blob(content, contentLength);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700263 onChanged();
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700264 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700265
266 /**
267 * Set content to point to an existing byte array. IMPORTANT: After calling this,
268 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
269 * @param content A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
270 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700271 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700272 setContent(const ptr_lib::shared_ptr<std::vector<uint8_t> > &content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700273 {
274 content_ = content;
275 onChanged();
276 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700277
278 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700279 setContent(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700280 {
281 content_ = content;
282 onChanged();
283 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700284
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700285private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700286 /**
287 * Clear the wire encoding.
288 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700289 void
290 onChanged();
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700291
Jeff Thompson20af0732013-09-12 17:01:45 -0700292 ptr_lib::shared_ptr<Signature> signature_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700293 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700294 MetaInfo metaInfo_;
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700295 Blob content_;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700296 SignedBlob wireEncoding_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700297};
298
299}
300
301#endif