blob: 0fe4c99ed781da2c960eba5fd3510a00d2ba4c6b [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson56ec9e22013-08-02 11:34:07 -07008#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07009#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -070010
Jeff Thompson46bd45f2013-08-08 16:46:41 -070011#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070012#include "name.hpp"
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070013#include "util/signed-blob.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070014#include "c/data-types.h"
15#include "encoding/wire-format.hpp"
16
17struct ndn_MetaInfo;
18struct ndn_Signature;
19struct ndn_Data;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020
21namespace ndn {
22
Jeff Thompsonf4585af2013-09-11 14:56:59 -070023/**
Jeff Thompson1fe62d42013-09-27 16:49:45 -070024 * 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 -070025 * You must create an object of a subclass, for example Sha256WithRsaSignature.
Jeff Thompsonf4585af2013-09-11 14:56:59 -070026 */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070027class Signature {
28public:
29 /**
Jeff Thompson20af0732013-09-12 17:01:45 -070030 * Return a pointer to a new Signature which is a copy of this signature.
31 * This is pure virtual, the subclass must implement it.
32 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070033 virtual ptr_lib::shared_ptr<Signature>
34 clone() const = 0;
Jeff Thompson20af0732013-09-12 17:01:45 -070035
36 /**
37 * The virtual destructor.
38 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070039 virtual
40 ~Signature();
Jeff Thompson20af0732013-09-12 17:01:45 -070041
42 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070043 * Set the signatureStruct to point to the values in this signature object, without copying any memory.
44 * 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 -070045 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070046 * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
47 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070048 virtual void
49 get(struct ndn_Signature& signatureStruct) const = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070050
51 /**
52 * Clear this signature, and set the values by copying from the ndn_Signature struct.
Jeff Thompson20af0732013-09-12 17:01:45 -070053 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070054 * @param signatureStruct a C ndn_Signature struct
55 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070056 virtual void
57 set(const struct ndn_Signature& signatureStruct) = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070058};
59
Jeff Thompsonf4585af2013-09-11 14:56:59 -070060/**
61 * An MetaInfo holds the meta info which is signed inside the data packet.
62 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070063class MetaInfo {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070064public:
Jeff Thompsonfec716d2013-09-11 13:54:36 -070065 MetaInfo()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070066 {
67 type_ = ndn_ContentType_DATA;
68 freshnessSeconds_ = -1;
69 }
70
71 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070072 * Set the metaInfoStruct to point to the values in this meta info object, without copying any memory.
73 * WARNING: The resulting pointers in metaInfoStruct are invalid after a further use of this object which could reallocate memory.
74 * @param metaInfoStruct a C ndn_MetaInfo struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070075 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070076 void
77 get(struct ndn_MetaInfo& metaInfoStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078
79 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070080 * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct.
81 * @param metaInfoStruct a C ndn_MetaInfo struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -070082 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070083 void
84 set(const struct ndn_MetaInfo& metaInfoStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070085
Jeff Thompson0050abe2013-09-17 12:50:25 -070086 double
87 getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070088
Jeff Thompson0050abe2013-09-17 12:50:25 -070089 ndn_ContentType
90 getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070091
Jeff Thompson0050abe2013-09-17 12:50:25 -070092 int
93 getFreshnessSeconds() const { return freshnessSeconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070094
Jeff Thompson0050abe2013-09-17 12:50:25 -070095 const Name::Component&
96 getFinalBlockID() const { return finalBlockID_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070097
Jeff Thompson0050abe2013-09-17 12:50:25 -070098 void
99 setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700100
Jeff Thompson0050abe2013-09-17 12:50:25 -0700101 void
102 setType(ndn_ContentType type) { type_ = type; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700103
Jeff Thompson0050abe2013-09-17 12:50:25 -0700104 void
105 setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700106
Jeff Thompson0050abe2013-09-17 12:50:25 -0700107 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700108 setFinalBlockID(const std::vector<uint8_t>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700109
110 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700111 setFinalBlockID(const uint8_t* finalBlockID, size_t finalBlockIdLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700112 {
Jeff Thompson85db6d72013-09-12 12:41:18 -0700113 finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700114 }
115
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700116private:
Jeff Thompson210b92f2013-07-11 15:16:03 -0700117 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
Jeff Thompsond8776352013-08-16 18:09:30 -0700118 ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
Jeff Thompson210b92f2013-07-11 15:16:03 -0700119 int freshnessSeconds_; /**< -1 for none */
Jeff Thompson85db6d72013-09-12 12:41:18 -0700120 Name::Component finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700121};
122
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700123class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700124public:
Jeff Thompson20af0732013-09-12 17:01:45 -0700125 /**
126 * Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature.
127 */
128 Data();
129
130 /**
131 * Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature.
132 * @param name A reference to the name which is copied.
133 */
134 Data(const Name& name);
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700135
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700136 /**
137 * Encode this Data for a particular wire format. Also, set the wireEncoding field to the encoded result.
138 * This is not const because it updates the wireEncoding.
139 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
140 * @return The encoded byte array.
141 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700142 SignedBlob
143 wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700144
145 /**
146 * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
147 * @param input The input byte array to be decoded.
148 * @param inputLength The length of input.
149 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
150 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700151 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700152 wireDecode(const uint8_t* input, size_t inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700153
154 /**
155 * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
156 * @param input The input byte array to be decoded.
157 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
158 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700159 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700160 wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700161 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700162 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700163 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700164
165 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700166 * Set the dataStruct to point to the values in this interest, without copying any memory.
167 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
168 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700169 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700170 void
171 get(struct ndn_Data& dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700172
173 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700174 * Clear this data object, and set the values by copying from the ndn_Data struct.
175 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700176 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700177 void
178 set(const struct ndn_Data& dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700179
Jeff Thompson0050abe2013-09-17 12:50:25 -0700180 const Signature*
181 getSignature() const { return signature_.get(); }
182
183 Signature*
184 getSignature()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700185 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700186 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700187 onChanged();
188 return signature_.get();
189 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700190
Jeff Thompson0050abe2013-09-17 12:50:25 -0700191 const Name&
192 getName() const { return name_; }
193
194 Name&
195 getName()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700196 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700197 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700198 onChanged();
199 return name_;
200 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700201
Jeff Thompson0050abe2013-09-17 12:50:25 -0700202 const MetaInfo&
203 getMetaInfo() const { return metaInfo_; }
204
205 MetaInfo&
206 getMetaInfo()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700207 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700208 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700209 onChanged();
210 return metaInfo_;
211 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700212
Jeff Thompson0050abe2013-09-17 12:50:25 -0700213 const Blob&
214 getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700215
Jeff Thompson20af0732013-09-12 17:01:45 -0700216 /**
Jeff Thompsonb697c5d2013-09-17 17:58:37 -0700217 * Return a pointer to the wireEncoding. It may be null.
218 */
219 const SignedBlob&
220 getWireEncoding() const { return wireEncoding_; }
221
222 /**
Jeff Thompson20af0732013-09-12 17:01:45 -0700223 * Set the signature to a copy of the given signature.
224 * @param signature The signature object which is cloned.
225 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700226 void
227 setSignature(const Signature& signature)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700228 {
229 signature_ = signature.clone();
230 onChanged();
231 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700232
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700233 /**
234 * Set name to a copy of the given Name.
235 * @param name The Name which is copied.
236 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700237 void
238 setName(const Name& name)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700239 {
240 name_ = name;
241 onChanged();
242 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700243
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700244 /**
245 * Set metaInfo to a copy of the given MetaInfo.
246 * @param metaInfo The MetaInfo which is copied.
247 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700248 void
249 setMetainfo(const MetaInfo& metaInfo)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700250 {
251 metaInfo_ = metaInfo;
252 onChanged();
253 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700254
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700255 /**
256 * Set the content to a copy of the data in the vector.
257 * @param content A vector whose contents are copied.
258 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700259 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700260 setContent(const std::vector<uint8_t>& content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700261 {
262 content_ = content;
263 onChanged();
264 }
Jeff Thompson3776d1c2013-09-17 14:22:51 -0700265
Jeff Thompson0050abe2013-09-17 12:50:25 -0700266 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700267 setContent(const uint8_t* content, size_t contentLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700268 {
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700269 content_ = Blob(content, contentLength);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700270 onChanged();
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700271 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700272
273 /**
274 * Set content to point to an existing byte array. IMPORTANT: After calling this,
275 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
276 * @param content A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
277 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700278 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700279 setContent(const ptr_lib::shared_ptr<std::vector<uint8_t> > &content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700280 {
281 content_ = content;
282 onChanged();
283 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700284
285 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700286 setContent(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700287 {
288 content_ = content;
289 onChanged();
290 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700291
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700292private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700293 /**
294 * Clear the wire encoding.
295 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700296 void
297 onChanged();
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700298
Jeff Thompson20af0732013-09-12 17:01:45 -0700299 ptr_lib::shared_ptr<Signature> signature_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700300 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700301 MetaInfo metaInfo_;
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700302 Blob content_;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700303 SignedBlob wireEncoding_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700304};
305
306}
307
308#endif