blob: 82dcf5d0b6b5773e26ee7258de50728594d076ac [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 Thompson53412192013-08-06 13:35:50 -070012#include "key.hpp"
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070013#include "util/signed-blob.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070014#include "c/data.h"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070015
16namespace ndn {
17
Jeff Thompsonf4585af2013-09-11 14:56:59 -070018/**
Jeff Thompson20af0732013-09-12 17:01:45 -070019 * 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 -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 */
27 virtual ptr_lib::shared_ptr<Signature> clone() const = 0;
28
29 /**
30 * The virtual destructor.
31 */
32 virtual ~Signature();
33
34 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070035 * Set the signatureStruct to point to the values in this signature object, without copying any memory.
36 * 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 -070037 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070038 * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
39 */
Jeff Thompson20af0732013-09-12 17:01:45 -070040 virtual void get(struct ndn_Signature& signatureStruct) const = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070041
42 /**
43 * Clear this signature, and set the values by copying from the ndn_Signature struct.
Jeff Thompson20af0732013-09-12 17:01:45 -070044 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070045 * @param signatureStruct a C ndn_Signature struct
46 */
Jeff Thompson20af0732013-09-12 17:01:45 -070047 virtual void set(const struct ndn_Signature& signatureStruct) = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070048};
49
Jeff Thompsonf4585af2013-09-11 14:56:59 -070050/**
51 * An MetaInfo holds the meta info which is signed inside the data packet.
52 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070053class MetaInfo {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070054public:
Jeff Thompsonfec716d2013-09-11 13:54:36 -070055 MetaInfo()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070056 {
57 type_ = ndn_ContentType_DATA;
58 freshnessSeconds_ = -1;
59 }
60
61 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070062 * Set the metaInfoStruct to point to the values in this meta info object, without copying any memory.
63 * WARNING: The resulting pointers in metaInfoStruct are invalid after a further use of this object which could reallocate memory.
64 * @param metaInfoStruct a C ndn_MetaInfo struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070065 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070066 void get(struct ndn_MetaInfo& metaInfoStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070067
68 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070069 * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct.
70 * @param metaInfoStruct a C ndn_MetaInfo struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -070071 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070072 void set(const struct ndn_MetaInfo& metaInfoStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070073
Jeff Thompson210b92f2013-07-11 15:16:03 -070074 double getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070075
Jeff Thompsond8776352013-08-16 18:09:30 -070076 ndn_ContentType getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070077
78 int getFreshnessSeconds() const { return freshnessSeconds_; }
79
Jeff Thompson85db6d72013-09-12 12:41:18 -070080 const Name::Component& getFinalBlockID() const { return finalBlockID_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070081
Jeff Thompson46bd45f2013-08-08 16:46:41 -070082 void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
83
Jeff Thompsond8776352013-08-16 18:09:30 -070084 void setType(ndn_ContentType type) { type_ = type; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070085
86 void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
87
Jeff Thompson85db6d72013-09-12 12:41:18 -070088 void setFinalBlockID(const std::vector<unsigned char>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070089 void setFinalBlockID(const unsigned char *finalBlockID, unsigned int finalBlockIdLength)
90 {
Jeff Thompson85db6d72013-09-12 12:41:18 -070091 finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070092 }
93
Jeff Thompson5cae5e52013-07-10 19:41:20 -070094private:
Jeff Thompson210b92f2013-07-11 15:16:03 -070095 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
Jeff Thompsond8776352013-08-16 18:09:30 -070096 ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
Jeff Thompson210b92f2013-07-11 15:16:03 -070097 int freshnessSeconds_; /**< -1 for none */
Jeff Thompson85db6d72013-09-12 12:41:18 -070098 Name::Component finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070099};
100
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700101class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700102public:
Jeff Thompson20af0732013-09-12 17:01:45 -0700103 /**
104 * Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature.
105 */
106 Data();
107
108 /**
109 * Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature.
110 * @param name A reference to the name which is copied.
111 */
112 Data(const Name& name);
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700113
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700114 /**
115 * Encode this Data for a particular wire format. Also, set the wireEncoding field to the encoded result.
116 * This is not const because it updates the wireEncoding.
117 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
118 * @return The encoded byte array.
119 */
120 SignedBlob wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
121
122 /**
123 * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
124 * @param input The input byte array to be decoded.
125 * @param inputLength The length of input.
126 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
127 */
128 void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
129
130 /**
131 * Decode the input using a particular wire format and update this Data. Also, set the wireEncoding field to the input.
132 * @param input The input byte array to be decoded.
133 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
134 */
Jeff Thompsona7516e02013-09-11 17:12:25 -0700135 void wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700136 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700137 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700138 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700139
140 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700141 * Set the dataStruct to point to the values in this interest, without copying any memory.
142 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
143 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700144 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700145 void get(struct ndn_Data& dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700146
147 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700148 * Clear this data object, and set the values by copying from the ndn_Data struct.
149 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700150 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700151 void set(const struct ndn_Data& dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700152
Jeff Thompson20af0732013-09-12 17:01:45 -0700153 const Signature* getSignature() const { return signature_.get(); }
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700154 Signature* getSignature()
155 {
156 // TODO: Should add an OnChanged listener to instead of always calling onChanged.
157 onChanged();
158 return signature_.get();
159 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700160
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700161 const Name& getName() const { return name_; }
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700162 Name& getName()
163 {
164 // TODO: Should add an OnChanged listener to instead of always calling onChanged.
165 onChanged();
166 return name_;
167 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700168
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700169 const MetaInfo& getMetaInfo() const { return metaInfo_; }
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700170 MetaInfo& getMetaInfo()
171 {
172 // TODO: Should add an OnChanged listener to instead of always calling onChanged.
173 onChanged();
174 return metaInfo_;
175 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700176
Jeff Thompson6a513332013-09-12 13:23:58 -0700177 const Blob& getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700178
Jeff Thompson20af0732013-09-12 17:01:45 -0700179 /**
180 * Set the signature to a copy of the given signature.
181 * @param signature The signature object which is cloned.
182 */
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700183 void setSignature(const Signature& signature)
184 {
185 signature_ = signature.clone();
186 onChanged();
187 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700188
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700189 /**
190 * Set name to a copy of the given Name.
191 * @param name The Name which is copied.
192 */
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700193 void setName(const Name& name)
194 {
195 name_ = name;
196 onChanged();
197 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700198
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700199 /**
200 * Set metaInfo to a copy of the given MetaInfo.
201 * @param metaInfo The MetaInfo which is copied.
202 */
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700203 void setMetainfo(const MetaInfo& metaInfo)
204 {
205 metaInfo_ = metaInfo;
206 onChanged();
207 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700208
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700209 /**
210 * Set the content to a copy of the data in the vector.
211 * @param content A vector whose contents are copied.
212 */
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700213 void setContent(const std::vector<unsigned char>& content)
214 {
215 content_ = content;
216 onChanged();
217 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700218 void setContent(const unsigned char *content, unsigned int contentLength)
219 {
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700220 content_ = Blob(content, contentLength);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700221 onChanged();
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700222 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700223
224 /**
225 * Set content to point to an existing byte array. IMPORTANT: After calling this,
226 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
227 * @param content A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
228 */
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700229 void setContent(const ptr_lib::shared_ptr<std::vector<unsigned char> > &content)
230 {
231 content_ = content;
232 onChanged();
233 }
234 void setContent(const ptr_lib::shared_ptr<const std::vector<unsigned char> > &content)
235 {
236 content_ = content;
237 onChanged();
238 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700239
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700240private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700241 /**
242 * Clear the wire encoding.
243 */
244 void onChanged();
245
Jeff Thompson20af0732013-09-12 17:01:45 -0700246 ptr_lib::shared_ptr<Signature> signature_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700247 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700248 MetaInfo metaInfo_;
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700249 Blob content_;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700250 SignedBlob wireEncoding_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700251};
252
253}
254
255#endif