blob: a54c24983747da875fb3450dd6c47a64973c41aa [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 Thompson9a8e82f2013-10-17 14:13:43 -070086 MillisecondsSince1970
Jeff Thompson0050abe2013-09-17 12:50:25 -070087 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
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070099 setTimestampMilliseconds(MillisecondsSince1970 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 Thompson70d0e462013-10-14 13:44:32 -0700106
107 void
Jeff Thompsonee2bf572013-11-19 12:40:10 -0800108 setFinalBlockID(const Name::Component& finalBlockID) { finalBlockID_ = finalBlockID; }
109
110 void
Jeff Thompson70d0e462013-10-14 13:44:32 -0700111 setFinalBlockID(const Blob& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700112
Jeff Thompson0050abe2013-09-17 12:50:25 -0700113 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700114 setFinalBlockID(const std::vector<uint8_t>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700115
116 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700117 setFinalBlockID(const uint8_t* finalBlockID, size_t finalBlockIdLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700118 {
Jeff Thompson85db6d72013-09-12 12:41:18 -0700119 finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700120 }
121
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700122private:
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700123 MillisecondsSince1970 timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
Jeff Thompsond8776352013-08-16 18:09:30 -0700124 ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
Jeff Thompson210b92f2013-07-11 15:16:03 -0700125 int freshnessSeconds_; /**< -1 for none */
Jeff Thompson85db6d72013-09-12 12:41:18 -0700126 Name::Component finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700127};
128
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700129class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700130public:
Jeff Thompson20af0732013-09-12 17:01:45 -0700131 /**
132 * Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature.
133 */
134 Data();
135
136 /**
137 * Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature.
138 * @param name A reference to the name which is copied.
139 */
140 Data(const Name& name);
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700141
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700142 /**
Jeff Thompson25bfdca2013-10-16 17:05:41 -0700143 * The copy constructor: Create a deep copy of the given data object, including a clone of the signature object.
144 * @param data The data object to copy.
145 */
146 Data(const Data& data);
147
148 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700149 * The virtual destructor.
150 */
Jeff Thompson25bfdca2013-10-16 17:05:41 -0700151 virtual ~Data();
152
153 /**
154 * The assignment operator: Copy fields and make a clone of the signature.
155 * @param data The other object to copy from.
156 * @return A reference to this object.
157 */
158 Data& operator=(const Data& data);
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700159
160 /**
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700161 * Encode this Data for a particular wire format. If wireFormat is the default wire format, also set the defaultWireEncoding
162 * field to the encoded result.
163 * Even though this is const, if wireFormat is the default wire format we update the defaultWireEncoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700164 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
165 * @return The encoded byte array.
166 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700167 SignedBlob
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700168 wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700169
170 /**
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700171 * Decode the input using a particular wire format and update this Data. If wireFormat is the default wire format, also
172 * set the defaultWireEncoding field to the input.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700173 * @param input The input byte array to be decoded.
174 * @param inputLength The length of input.
175 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
176 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700177 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700178 wireDecode(const uint8_t* input, size_t inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700179
180 /**
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700181 * Decode the input using a particular wire format and update this Data. If wireFormat is the default wire format, also
182 * set the defaultWireEncoding field to the input.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700183 * @param input The input byte array to be decoded.
184 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
185 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700186 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700187 wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700188 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700189 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700190 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700191
192 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700193 * Set the dataStruct to point to the values in this interest, without copying any memory.
194 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
195 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700196 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700197 void
198 get(struct ndn_Data& dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700199
200 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700201 * Clear this data object, and set the values by copying from the ndn_Data struct.
202 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700203 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700204 void
205 set(const struct ndn_Data& dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700206
Jeff Thompson0050abe2013-09-17 12:50:25 -0700207 const Signature*
208 getSignature() const { return signature_.get(); }
209
210 Signature*
211 getSignature()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700212 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700213 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700214 onChanged();
215 return signature_.get();
216 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700217
Jeff Thompson0050abe2013-09-17 12:50:25 -0700218 const Name&
219 getName() const { return name_; }
220
221 Name&
222 getName()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700223 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700224 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700225 onChanged();
226 return name_;
227 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700228
Jeff Thompson0050abe2013-09-17 12:50:25 -0700229 const MetaInfo&
230 getMetaInfo() const { return metaInfo_; }
231
232 MetaInfo&
233 getMetaInfo()
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700234 {
Jeff Thompson3352fc22013-09-17 12:07:14 -0700235 // TODO: Should add an OnChanged listener instead of always calling onChanged.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700236 onChanged();
237 return metaInfo_;
238 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700239
Jeff Thompson0050abe2013-09-17 12:50:25 -0700240 const Blob&
241 getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700242
Jeff Thompson20af0732013-09-12 17:01:45 -0700243 /**
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700244 * Return a pointer to the defaultWireEncoding. It may be null.
Jeff Thompsonb697c5d2013-09-17 17:58:37 -0700245 */
246 const SignedBlob&
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700247 getDefaultWireEncoding() const { return defaultWireEncoding_; }
Jeff Thompsonb697c5d2013-09-17 17:58:37 -0700248
249 /**
Jeff Thompson20af0732013-09-12 17:01:45 -0700250 * Set the signature to a copy of the given signature.
251 * @param signature The signature object which is cloned.
Jeff Thompson6d591972013-10-17 11:16:32 -0700252 * @return This Data so that you can chain calls to update values.
Jeff Thompson20af0732013-09-12 17:01:45 -0700253 */
Jeff Thompson6d591972013-10-17 11:16:32 -0700254 Data&
Jeff Thompson0050abe2013-09-17 12:50:25 -0700255 setSignature(const Signature& signature)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700256 {
257 signature_ = signature.clone();
258 onChanged();
Jeff Thompson6d591972013-10-17 11:16:32 -0700259 return *this;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700260 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700261
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700262 /**
Jeff Thompson6d591972013-10-17 11:16:32 -0700263 * Set name to a copy of the given Name. This is virtual so that a subclass can override to validate the name.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700264 * @param name The Name which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700265 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700266 */
Jeff Thompson6d591972013-10-17 11:16:32 -0700267 virtual Data&
268 setName(const Name& name);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700269
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700270 /**
271 * Set metaInfo to a copy of the given MetaInfo.
272 * @param metaInfo The MetaInfo which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700273 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700274 */
Jeff Thompson6d591972013-10-17 11:16:32 -0700275 Data&
Jeff Thompson0050abe2013-09-17 12:50:25 -0700276 setMetainfo(const MetaInfo& metaInfo)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700277 {
278 metaInfo_ = metaInfo;
279 onChanged();
Jeff Thompson6d591972013-10-17 11:16:32 -0700280 return *this;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700281 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700282
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700283 /**
284 * Set the content to a copy of the data in the vector.
285 * @param content A vector whose contents are copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700286 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700287 */
Jeff Thompson6d591972013-10-17 11:16:32 -0700288 Data&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700289 setContent(const std::vector<uint8_t>& content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700290 {
291 content_ = content;
292 onChanged();
Jeff Thompson6d591972013-10-17 11:16:32 -0700293 return *this;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700294 }
Jeff Thompson3776d1c2013-09-17 14:22:51 -0700295
Jeff Thompson6d591972013-10-17 11:16:32 -0700296 Data&
Jeff Thompson97223af2013-09-24 17:01:27 -0700297 setContent(const uint8_t* content, size_t contentLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700298 {
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700299 content_ = Blob(content, contentLength);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700300 onChanged();
Jeff Thompson6d591972013-10-17 11:16:32 -0700301 return *this;
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700302 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700303
Jeff Thompson6d591972013-10-17 11:16:32 -0700304 Data&
Jeff Thompson63631b72013-10-14 14:00:58 -0700305 setContent(const Blob& content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700306 {
307 content_ = content;
308 onChanged();
Jeff Thompson6d591972013-10-17 11:16:32 -0700309 return *this;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700310 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700311
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700312private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700313 /**
314 * Clear the wire encoding.
315 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700316 void
317 onChanged();
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700318
Jeff Thompson20af0732013-09-12 17:01:45 -0700319 ptr_lib::shared_ptr<Signature> signature_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700320 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700321 MetaInfo metaInfo_;
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700322 Blob content_;
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700323 SignedBlob defaultWireEncoding_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700324};
325
326}
327
328#endif