blob: f8cc893310e08daa0820a01e303741be72cff216 [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 Thompson1fe62d42013-09-27 16:49:45 -070018 * A Signature is an abstract base class providing methods to work with the signature information in a Data packet.
Jeff Thompsonfd2a7ef2013-09-27 16:50:54 -070019 * You must create an object of a subclass, for example Sha256WithRsaSignature.
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
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700102 setFinalBlockID(const std::vector<uint8_t>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700103
104 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700105 setFinalBlockID(const uint8_t* finalBlockID, size_t 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
Jeff Thompson97223af2013-09-24 17:01:27 -0700146 wireDecode(const uint8_t* input, size_t 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
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700154 wireDecode(const std::vector<uint8_t>& 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 /**
Jeff Thompsonb697c5d2013-09-17 17:58:37 -0700211 * Return a pointer to the wireEncoding. It may be null.
212 */
213 const SignedBlob&
214 getWireEncoding() const { return wireEncoding_; }
215
216 /**
Jeff Thompson20af0732013-09-12 17:01:45 -0700217 * Set the signature to a copy of the given signature.
218 * @param signature The signature object which is cloned.
219 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700220 void
221 setSignature(const Signature& signature)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700222 {
223 signature_ = signature.clone();
224 onChanged();
225 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700226
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700227 /**
228 * Set name to a copy of the given Name.
229 * @param name The Name which is copied.
230 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700231 void
232 setName(const Name& name)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700233 {
234 name_ = name;
235 onChanged();
236 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700237
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700238 /**
239 * Set metaInfo to a copy of the given MetaInfo.
240 * @param metaInfo The MetaInfo which is copied.
241 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700242 void
243 setMetainfo(const MetaInfo& metaInfo)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700244 {
245 metaInfo_ = metaInfo;
246 onChanged();
247 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700248
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700249 /**
250 * Set the content to a copy of the data in the vector.
251 * @param content A vector whose contents are copied.
252 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700253 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700254 setContent(const std::vector<uint8_t>& content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700255 {
256 content_ = content;
257 onChanged();
258 }
Jeff Thompson3776d1c2013-09-17 14:22:51 -0700259
Jeff Thompson0050abe2013-09-17 12:50:25 -0700260 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700261 setContent(const uint8_t* content, size_t contentLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700262 {
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700263 content_ = Blob(content, contentLength);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700264 onChanged();
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700265 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700266
267 /**
268 * Set content to point to an existing byte array. IMPORTANT: After calling this,
269 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
270 * @param content A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
271 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700272 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700273 setContent(const ptr_lib::shared_ptr<std::vector<uint8_t> > &content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700274 {
275 content_ = content;
276 onChanged();
277 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700278
279 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700280 setContent(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700281 {
282 content_ = content;
283 onChanged();
284 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700285
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700286private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700287 /**
288 * Clear the wire encoding.
289 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700290 void
291 onChanged();
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700292
Jeff Thompson20af0732013-09-12 17:01:45 -0700293 ptr_lib::shared_ptr<Signature> signature_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700294 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700295 MetaInfo metaInfo_;
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700296 Blob content_;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700297 SignedBlob wireEncoding_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700298};
299
300}
301
302#endif