blob: 26fb9f98f29bc469ad4e7c03e5c82fb50f24b8ca [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
101 setFinalBlockID(const std::vector<unsigned char>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
102
103 void
104 setFinalBlockID(const unsigned char* finalBlockID, unsigned int 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
145 wireDecode(const unsigned char* input, unsigned int 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
153 wireDecode(const std::vector<unsigned char>& 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 /**
210 * Set the signature to a copy of the given signature.
211 * @param signature The signature object which is cloned.
212 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700213 void
214 setSignature(const Signature& signature)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700215 {
216 signature_ = signature.clone();
217 onChanged();
218 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700219
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700220 /**
221 * Set name to a copy of the given Name.
222 * @param name The Name which is copied.
223 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700224 void
225 setName(const Name& name)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700226 {
227 name_ = name;
228 onChanged();
229 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700230
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700231 /**
232 * Set metaInfo to a copy of the given MetaInfo.
233 * @param metaInfo The MetaInfo which is copied.
234 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700235 void
236 setMetainfo(const MetaInfo& metaInfo)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700237 {
238 metaInfo_ = metaInfo;
239 onChanged();
240 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700241
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700242 /**
243 * Set the content to a copy of the data in the vector.
244 * @param content A vector whose contents are copied.
245 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700246 void
247 setContent(const std::vector<unsigned char>& content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700248 {
249 content_ = content;
250 onChanged();
251 }
Jeff Thompson3776d1c2013-09-17 14:22:51 -0700252
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