Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 6 | #ifndef NDN_DATA_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 7 | #define NDN_DATA_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 9 | #include "common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "name.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 11 | #include "key.hpp" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 12 | #include "c/data.h" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
| 15 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 16 | /** |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 17 | * A Signature is an abstract base class providing an methods to work with the signature information in a Data packet. |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 18 | */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 19 | class Signature { |
| 20 | public: |
| 21 | /** |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 22 | * 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 33 | * 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 Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 35 | * This is pure virtual, the subclass must implement it. |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 36 | * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated. |
| 37 | */ |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 38 | virtual void get(struct ndn_Signature& signatureStruct) const = 0; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Clear this signature, and set the values by copying from the ndn_Signature struct. |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 42 | * This is pure virtual, the subclass must implement it. |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 43 | * @param signatureStruct a C ndn_Signature struct |
| 44 | */ |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 45 | virtual void set(const struct ndn_Signature& signatureStruct) = 0; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 48 | /** |
| 49 | * An MetaInfo holds the meta info which is signed inside the data packet. |
| 50 | */ |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 51 | class MetaInfo { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 52 | public: |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 53 | MetaInfo() |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 54 | { |
| 55 | type_ = ndn_ContentType_DATA; |
| 56 | freshnessSeconds_ = -1; |
| 57 | } |
| 58 | |
| 59 | /** |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 60 | * 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 63 | */ |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 64 | void get(struct ndn_MetaInfo& metaInfoStruct) const; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 67 | * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct. |
| 68 | * @param metaInfoStruct a C ndn_MetaInfo struct |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 69 | */ |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 70 | void set(const struct ndn_MetaInfo& metaInfoStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 71 | |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 72 | double getTimestampMilliseconds() const { return timestampMilliseconds_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 73 | |
Jeff Thompson | d877635 | 2013-08-16 18:09:30 -0700 | [diff] [blame] | 74 | ndn_ContentType getType() const { return type_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 75 | |
| 76 | int getFreshnessSeconds() const { return freshnessSeconds_; } |
| 77 | |
Jeff Thompson | 85db6d7 | 2013-09-12 12:41:18 -0700 | [diff] [blame] | 78 | const Name::Component& getFinalBlockID() const { return finalBlockID_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 79 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 80 | void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; } |
| 81 | |
Jeff Thompson | d877635 | 2013-08-16 18:09:30 -0700 | [diff] [blame] | 82 | void setType(ndn_ContentType type) { type_ = type; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 83 | |
| 84 | void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; } |
| 85 | |
Jeff Thompson | 85db6d7 | 2013-09-12 12:41:18 -0700 | [diff] [blame] | 86 | void setFinalBlockID(const std::vector<unsigned char>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 87 | void setFinalBlockID(const unsigned char *finalBlockID, unsigned int finalBlockIdLength) |
| 88 | { |
Jeff Thompson | 85db6d7 | 2013-09-12 12:41:18 -0700 | [diff] [blame] | 89 | finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 92 | private: |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 93 | double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */ |
Jeff Thompson | d877635 | 2013-08-16 18:09:30 -0700 | [diff] [blame] | 94 | ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */ |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 95 | int freshnessSeconds_; /**< -1 for none */ |
Jeff Thompson | 85db6d7 | 2013-09-12 12:41:18 -0700 | [diff] [blame] | 96 | Name::Component finalBlockID_; /** size 0 for none */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 99 | class Data { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 100 | public: |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 101 | /** |
| 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 Thompson | f5dbd27 | 2013-08-08 16:49:55 -0700 | [diff] [blame] | 111 | |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 112 | Blob wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 113 | { |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 114 | return wireFormat.encodeData(*this); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 115 | } |
Jeff Thompson | a7516e0 | 2013-09-11 17:12:25 -0700 | [diff] [blame] | 116 | void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 117 | { |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 118 | wireFormat.decodeData(*this, input, inputLength); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 119 | } |
Jeff Thompson | a7516e0 | 2013-09-11 17:12:25 -0700 | [diff] [blame] | 120 | void wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 121 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 122 | wireDecode(&input[0], input.size(), wireFormat); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 123 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 126 | * 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 129 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 130 | void get(struct ndn_Data& dataStruct) const; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 133 | * Clear this data object, and set the values by copying from the ndn_Data struct. |
| 134 | * @param dataStruct a C ndn_Data struct |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 135 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 136 | void set(const struct ndn_Data& dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 137 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 138 | const Signature* getSignature() const { return signature_.get(); } |
| 139 | Signature* getSignature() { return signature_.get(); } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 140 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 141 | const Name& getName() const { return name_; } |
| 142 | Name& getName() { return name_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 143 | |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 144 | const MetaInfo& getMetaInfo() const { return metaInfo_; } |
| 145 | MetaInfo& getMetaInfo() { return metaInfo_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 146 | |
Jeff Thompson | 6a51333 | 2013-09-12 13:23:58 -0700 | [diff] [blame] | 147 | const Blob& getContent() const { return content_; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 148 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 149 | /** |
| 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 Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 154 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 155 | void setName(const Name& name) { name_ = name; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 156 | |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 157 | void setMetainfo(const MetaInfo& metaInfo) { metaInfo_ = metaInfo; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 158 | |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 159 | /** |
| 160 | * Set the content to a copy of the data in the vector. |
| 161 | * @param content A vector whose contents are copied. |
| 162 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 163 | void setContent(const std::vector<unsigned char>& content) { content_ = content; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 164 | void setContent(const unsigned char *content, unsigned int contentLength) |
| 165 | { |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 166 | content_ = Blob(content, contentLength); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 167 | } |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 168 | |
| 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 177 | private: |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 178 | ptr_lib::shared_ptr<Signature> signature_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 179 | Name name_; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 180 | MetaInfo metaInfo_; |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 181 | Blob content_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | #endif |