blob: 416c1bbf5e36edb20a923826b2d6328521fee9c0 [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 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 Thompson20af0732013-09-12 17:01:45 -070018 * 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 -070019 */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020class Signature {
21public:
22 /**
Jeff Thompson20af0732013-09-12 17:01:45 -070023 * Return a pointer to a new Signature which is a copy of this signature.
24 * This is pure virtual, the subclass must implement it.
25 */
26 virtual ptr_lib::shared_ptr<Signature> clone() const = 0;
27
28 /**
29 * The virtual destructor.
30 */
31 virtual ~Signature();
32
33 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070034 * Set the signatureStruct to point to the values in this signature object, without copying any memory.
35 * 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 -070036 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070037 * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated.
38 */
Jeff Thompson20af0732013-09-12 17:01:45 -070039 virtual void get(struct ndn_Signature& signatureStruct) const = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070040
41 /**
42 * Clear this signature, and set the values by copying from the ndn_Signature struct.
Jeff Thompson20af0732013-09-12 17:01:45 -070043 * This is pure virtual, the subclass must implement it.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070044 * @param signatureStruct a C ndn_Signature struct
45 */
Jeff Thompson20af0732013-09-12 17:01:45 -070046 virtual void set(const struct ndn_Signature& signatureStruct) = 0;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070047};
48
Jeff Thompsonf4585af2013-09-11 14:56:59 -070049/**
50 * An MetaInfo holds the meta info which is signed inside the data packet.
51 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070052class MetaInfo {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070053public:
Jeff Thompsonfec716d2013-09-11 13:54:36 -070054 MetaInfo()
Jeff Thompson5cae5e52013-07-10 19:41:20 -070055 {
56 type_ = ndn_ContentType_DATA;
57 freshnessSeconds_ = -1;
58 }
59
60 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070061 * Set the metaInfoStruct to point to the values in this meta info object, without copying any memory.
62 * WARNING: The resulting pointers in metaInfoStruct are invalid after a further use of this object which could reallocate memory.
63 * @param metaInfoStruct a C ndn_MetaInfo struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070064 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070065 void get(struct ndn_MetaInfo& metaInfoStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070066
67 /**
Jeff Thompsonfec716d2013-09-11 13:54:36 -070068 * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct.
69 * @param metaInfoStruct a C ndn_MetaInfo struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -070070 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -070071 void set(const struct ndn_MetaInfo& metaInfoStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070072
Jeff Thompson210b92f2013-07-11 15:16:03 -070073 double getTimestampMilliseconds() const { return timestampMilliseconds_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070074
Jeff Thompsond8776352013-08-16 18:09:30 -070075 ndn_ContentType getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070076
77 int getFreshnessSeconds() const { return freshnessSeconds_; }
78
Jeff Thompson85db6d72013-09-12 12:41:18 -070079 const Name::Component& getFinalBlockID() const { return finalBlockID_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070080
Jeff Thompson46bd45f2013-08-08 16:46:41 -070081 void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; }
82
Jeff Thompsond8776352013-08-16 18:09:30 -070083 void setType(ndn_ContentType type) { type_ = type; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070084
85 void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; }
86
Jeff Thompson85db6d72013-09-12 12:41:18 -070087 void setFinalBlockID(const std::vector<unsigned char>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070088 void setFinalBlockID(const unsigned char *finalBlockID, unsigned int finalBlockIdLength)
89 {
Jeff Thompson85db6d72013-09-12 12:41:18 -070090 finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070091 }
92
Jeff Thompson5cae5e52013-07-10 19:41:20 -070093private:
Jeff Thompson210b92f2013-07-11 15:16:03 -070094 double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */
Jeff Thompsond8776352013-08-16 18:09:30 -070095 ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */
Jeff Thompson210b92f2013-07-11 15:16:03 -070096 int freshnessSeconds_; /**< -1 for none */
Jeff Thompson85db6d72013-09-12 12:41:18 -070097 Name::Component finalBlockID_; /** size 0 for none */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070098};
99
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700100class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700101public:
Jeff Thompson20af0732013-09-12 17:01:45 -0700102 /**
103 * Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature.
104 */
105 Data();
106
107 /**
108 * Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature.
109 * @param name A reference to the name which is copied.
110 */
111 Data(const Name& name);
Jeff Thompsonf5dbd272013-08-08 16:49:55 -0700112
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700113 Blob wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700114 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700115 return wireFormat.encodeData(*this);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700116 }
Jeff Thompsona7516e02013-09-11 17:12:25 -0700117 void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700118 {
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700119 wireFormat.decodeData(*this, input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700120 }
Jeff Thompsona7516e02013-09-11 17:12:25 -0700121 void wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700122 {
Jeff Thompson67e9e0a2013-08-02 19:16:19 -0700123 wireDecode(&input[0], input.size(), wireFormat);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700124 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700125
126 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700127 * Set the dataStruct to point to the values in this interest, without copying any memory.
128 * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory.
129 * @param dataStruct a C ndn_Data struct where the name components array is already allocated.
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700130 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700131 void get(struct ndn_Data& dataStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700132
133 /**
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700134 * Clear this data object, and set the values by copying from the ndn_Data struct.
135 * @param dataStruct a C ndn_Data struct
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700136 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700137 void set(const struct ndn_Data& dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700138
Jeff Thompson20af0732013-09-12 17:01:45 -0700139 const Signature* getSignature() const { return signature_.get(); }
140 Signature* getSignature() { return signature_.get(); }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700141
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700142 const Name& getName() const { return name_; }
143 Name& getName() { return name_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700144
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700145 const MetaInfo& getMetaInfo() const { return metaInfo_; }
146 MetaInfo& getMetaInfo() { return metaInfo_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700147
Jeff Thompson6a513332013-09-12 13:23:58 -0700148 const Blob& getContent() const { return content_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700149
Jeff Thompson20af0732013-09-12 17:01:45 -0700150 /**
151 * Set the signature to a copy of the given signature.
152 * @param signature The signature object which is cloned.
153 */
154 void setSignature(const Signature& signature) { signature_ = signature.clone(); }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700155
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700156 /**
157 * Set name to a copy of the given Name.
158 * @param name The Name which is copied.
159 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700160 void setName(const Name& name) { name_ = name; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700161
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700162 /**
163 * Set metaInfo to a copy of the given MetaInfo.
164 * @param metaInfo The MetaInfo which is copied.
165 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700166 void setMetainfo(const MetaInfo& metaInfo) { metaInfo_ = metaInfo; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700167
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700168 /**
169 * Set the content to a copy of the data in the vector.
170 * @param content A vector whose contents are copied.
171 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700172 void setContent(const std::vector<unsigned char>& content) { content_ = content; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700173 void setContent(const unsigned char *content, unsigned int contentLength)
174 {
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700175 content_ = Blob(content, contentLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700176 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700177
178 /**
179 * Set content to point to an existing byte array. IMPORTANT: After calling this,
180 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
181 * @param content A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
182 */
183 void setContent(const ptr_lib::shared_ptr<std::vector<unsigned char> > &content) { content_ = content; }
184 void setContent(const ptr_lib::shared_ptr<const std::vector<unsigned char> > &content) { content_ = content; }
185
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700186private:
Jeff Thompson20af0732013-09-12 17:01:45 -0700187 ptr_lib::shared_ptr<Signature> signature_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700188 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700189 MetaInfo metaInfo_;
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700190 Blob content_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700191};
192
193}
194
195#endif