blob: cb0e559210c1281775ffc55a9c4d5ac63c6314c4 [file] [log] [blame]
Jeff Thompson5cae5e52013-07-10 19:41:20 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompson56ec9e22013-08-02 11:34:07 -07006#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07007#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -07008
Jeff Thompson46bd45f2013-08-08 16:46:41 -07009#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "name.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "key.hpp"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070012#include "c/data.h"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070013
14namespace ndn {
15
Jeff Thompsonf4585af2013-09-11 14:56:59 -070016/**
Jeff Thompson20af0732013-09-12 17:01:45 -070017 * 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 -070018 */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070019class Signature {
20public:
21 /**
Jeff Thompson20af0732013-09-12 17:01:45 -070022 * Return a pointer to a new Signature which is a copy of this signature.
23 * This is pure virtual, the subclass must implement it.
24 */
25 virtual ptr_lib::shared_ptr<Signature> clone() const = 0;
26
27 /**
28 * The virtual destructor.
29 */
30 virtual ~Signature();
31
32 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070033 * Set the signatureStruct to point to the values in this signature object, without copying any memory.
34 * 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 -070035 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070036 * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
37 */
Jeff Thompson20af0732013-09-12 17:01:45 -070038 virtual void get(struct ndn_Signature& signatureStruct) const = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070039
40 /**
41 * Clear this signature, and set the values by copying from the ndn_Signature struct.
Jeff Thompson20af0732013-09-12 17:01:45 -070042 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070043 * @param signatureStruct a C ndn_Signature struct
44 */
Jeff Thompson20af0732013-09-12 17:01:45 -070045 virtual void set(const struct ndn_Signature& signatureStruct) = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070046};
47
Jeff Thompsonf4585af2013-09-11 14:56:59 -070048/**
49 * An MetaInfo holds the meta info which is signed inside the data packet.
50 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070051class MetaInfo {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070052public:
Jeff Thompsonfec716d2013-09-11 13:54:36 -070053 MetaInfo()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070054 {
55 type_ = ndn_ContentType_DATA;
56 freshnessSeconds_ = -1;
57 }
58
59 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070060 * Set the metaInfoStruct to point to the values in this meta info object, without copying any memory.
61 * WARNING: The resulting pointers in metaInfoStruct are invalid after a further use of this object which could reallocate memory.
62 * @param metaInfoStruct a C ndn_MetaInfo struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070063 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070064 void get(struct ndn_MetaInfo& metaInfoStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070065
66 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070067 * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct.
68 * @param metaInfoStruct a C ndn_MetaInfo struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -070069 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070070 void set(const struct ndn_MetaInfo& metaInfoStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070071
Jeff Thompson210b92f2013-07-11 15:16:03 -070072 double getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070073
Jeff Thompsond8776352013-08-16 18:09:30 -070074 ndn_ContentType getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070075
76 int getFreshnessSeconds() const { return freshnessSeconds_; }
77
Jeff Thompson85db6d72013-09-12 12:41:18 -070078 const Name::Component& getFinalBlockID() const { return finalBlockID_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070079
Jeff Thompson46bd45f2013-08-08 16:46:41 -070080 void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
81
Jeff Thompsond8776352013-08-16 18:09:30 -070082 void setType(ndn_ContentType type) { type_ = type; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070083
84 void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
85
Jeff Thompson85db6d72013-09-12 12:41:18 -070086 void setFinalBlockID(const std::vector<unsigned char>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070087 void setFinalBlockID(const unsigned char *finalBlockID, unsigned int finalBlockIdLength)
88 {
Jeff Thompson85db6d72013-09-12 12:41:18 -070089 finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070090 }
91
Jeff Thompson5cae5e52013-07-10 19:41:20 -070092private:
Jeff Thompson210b92f2013-07-11 15:16:03 -070093 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
Jeff Thompsond8776352013-08-16 18:09:30 -070094 ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
Jeff Thompson210b92f2013-07-11 15:16:03 -070095 int freshnessSeconds_; /**< -1 for none */
Jeff Thompson85db6d72013-09-12 12:41:18 -070096 Name::Component finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070097};
98
Jeff Thompson56ec9e22013-08-02 11:34:07 -070099class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700100public:
Jeff Thompson20af0732013-09-12 17:01:45 -0700101 /**
102 * Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature.
103 */
104 Data();
105
106 /**
107 * Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature.
108 * @param name A reference to the name which is copied.
109 */
110 Data(const Name& name);
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700111
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700112 Blob wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700113 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700114 return wireFormat.encodeData(*this);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700115 }
Jeff Thompsona7516e02013-09-11 17:12:25 -0700116 void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700117 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700118 wireFormat.decodeData(*this, input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700119 }
Jeff Thompsona7516e02013-09-11 17:12:25 -0700120 void wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700121 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700122 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700123 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700124
125 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700126 * Set the dataStruct to point to the values in this interest, without copying any memory.
127 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
128 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700129 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700130 void get(struct ndn_Data& dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700131
132 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700133 * Clear this data object, and set the values by copying from the ndn_Data struct.
134 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700135 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700136 void set(const struct ndn_Data& dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700137
Jeff Thompson20af0732013-09-12 17:01:45 -0700138 const Signature* getSignature() const { return signature_.get(); }
139 Signature* getSignature() { return signature_.get(); }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700140
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700141 const Name& getName() const { return name_; }
142 Name& getName() { return name_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700143
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700144 const MetaInfo& getMetaInfo() const { return metaInfo_; }
145 MetaInfo& getMetaInfo() { return metaInfo_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700146
Jeff Thompson6a513332013-09-12 13:23:58 -0700147 const Blob& getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700148
Jeff Thompson20af0732013-09-12 17:01:45 -0700149 /**
150 * Set the signature to a copy of the given signature.
151 * @param signature The signature object which is cloned.
152 */
153 void setSignature(const Signature& signature) { signature_ = signature.clone(); }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700154
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700155 void setName(const Name& name) { name_ = name; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700156
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700157 void setMetainfo(const MetaInfo& metaInfo) { metaInfo_ = metaInfo; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700158
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700159 /**
160 * Set the content to a copy of the data in the vector.
161 * @param content A vector whose contents are copied.
162 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700163 void setContent(const std::vector<unsigned char>& content) { content_ = content; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700164 void setContent(const unsigned char *content, unsigned int contentLength)
165 {
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700166 content_ = Blob(content, contentLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700167 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700168
169 /**
170 * Set content to point to an existing byte array. IMPORTANT: After calling this,
171 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
172 * @param content A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
173 */
174 void setContent(const ptr_lib::shared_ptr<std::vector<unsigned char> > &content) { content_ = content; }
175 void setContent(const ptr_lib::shared_ptr<const std::vector<unsigned char> > &content) { content_ = content; }
176
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700177private:
Jeff Thompson20af0732013-09-12 17:01:45 -0700178 ptr_lib::shared_ptr<Signature> signature_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700179 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700180 MetaInfo metaInfo_;
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700181 Blob content_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700182};
183
184}
185
186#endif