Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 7 | #ifndef NDN_DATA_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 8 | #define NDN_DATA_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 10 | #include "common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 11 | #include "name.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 12 | #include "key.hpp" |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 13 | #include "util/signed-blob.hpp" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 14 | #include "c/data.h" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 15 | |
| 16 | namespace ndn { |
| 17 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 18 | /** |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 19 | * 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] | 20 | */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 21 | class Signature { |
| 22 | public: |
| 23 | /** |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 24 | * 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 35 | * 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 Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 37 | * This is pure virtual, the subclass must implement it. |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 38 | * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated. |
| 39 | */ |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 40 | virtual void get(struct ndn_Signature& signatureStruct) const = 0; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * 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] | 44 | * This is pure virtual, the subclass must implement it. |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 45 | * @param signatureStruct a C ndn_Signature struct |
| 46 | */ |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 47 | virtual void set(const struct ndn_Signature& signatureStruct) = 0; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 50 | /** |
| 51 | * An MetaInfo holds the meta info which is signed inside the data packet. |
| 52 | */ |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 53 | class MetaInfo { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 54 | public: |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 55 | MetaInfo() |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 56 | { |
| 57 | type_ = ndn_ContentType_DATA; |
| 58 | freshnessSeconds_ = -1; |
| 59 | } |
| 60 | |
| 61 | /** |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 62 | * 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 65 | */ |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 66 | void get(struct ndn_MetaInfo& metaInfoStruct) const; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 69 | * Clear this meta info, and set the values by copying from the ndn_MetaInfo struct. |
| 70 | * @param metaInfoStruct a C ndn_MetaInfo struct |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 71 | */ |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 72 | void set(const struct ndn_MetaInfo& metaInfoStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 73 | |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 74 | double getTimestampMilliseconds() const { return timestampMilliseconds_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 75 | |
Jeff Thompson | d877635 | 2013-08-16 18:09:30 -0700 | [diff] [blame] | 76 | ndn_ContentType getType() const { return type_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 77 | |
| 78 | int getFreshnessSeconds() const { return freshnessSeconds_; } |
| 79 | |
Jeff Thompson | 85db6d7 | 2013-09-12 12:41:18 -0700 | [diff] [blame] | 80 | const Name::Component& getFinalBlockID() const { return finalBlockID_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 81 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 82 | void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; } |
| 83 | |
Jeff Thompson | d877635 | 2013-08-16 18:09:30 -0700 | [diff] [blame] | 84 | void setType(ndn_ContentType type) { type_ = type; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 85 | |
| 86 | void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; } |
| 87 | |
Jeff Thompson | 85db6d7 | 2013-09-12 12:41:18 -0700 | [diff] [blame] | 88 | void setFinalBlockID(const std::vector<unsigned char>& finalBlockID) { finalBlockID_ = Name::Component(finalBlockID); } |
Jeff Thompson | 3352fc2 | 2013-09-17 12:07:14 -0700 | [diff] [blame^] | 89 | void setFinalBlockID(const unsigned char* finalBlockID, unsigned int finalBlockIdLength) |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 90 | { |
Jeff Thompson | 85db6d7 | 2013-09-12 12:41:18 -0700 | [diff] [blame] | 91 | finalBlockID_ = Name::Component(finalBlockID, finalBlockIdLength); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 94 | private: |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 95 | double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */ |
Jeff Thompson | d877635 | 2013-08-16 18:09:30 -0700 | [diff] [blame] | 96 | ndn_ContentType type_; /**< default is ndn_ContentType_DATA. -1 for none */ |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 97 | int freshnessSeconds_; /**< -1 for none */ |
Jeff Thompson | 85db6d7 | 2013-09-12 12:41:18 -0700 | [diff] [blame] | 98 | Name::Component finalBlockID_; /** size 0 for none */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 101 | class Data { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 102 | public: |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 103 | /** |
| 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 Thompson | f5dbd27 | 2013-08-08 16:49:55 -0700 | [diff] [blame] | 113 | |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 114 | /** |
| 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 | */ |
Jeff Thompson | 3352fc2 | 2013-09-17 12:07:14 -0700 | [diff] [blame^] | 128 | void wireDecode(const unsigned char* input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()); |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 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 Thompson | a7516e0 | 2013-09-11 17:12:25 -0700 | [diff] [blame] | 135 | void wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 136 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 137 | wireDecode(&input[0], input.size(), wireFormat); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 138 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 141 | * 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 Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 144 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 145 | void get(struct ndn_Data& dataStruct) const; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 148 | * Clear this data object, and set the values by copying from the ndn_Data struct. |
| 149 | * @param dataStruct a C ndn_Data struct |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 150 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 151 | void set(const struct ndn_Data& dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 152 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 153 | const Signature* getSignature() const { return signature_.get(); } |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 154 | Signature* getSignature() |
| 155 | { |
Jeff Thompson | 3352fc2 | 2013-09-17 12:07:14 -0700 | [diff] [blame^] | 156 | // TODO: Should add an OnChanged listener instead of always calling onChanged. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 157 | onChanged(); |
| 158 | return signature_.get(); |
| 159 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 160 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 161 | const Name& getName() const { return name_; } |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 162 | Name& getName() |
| 163 | { |
Jeff Thompson | 3352fc2 | 2013-09-17 12:07:14 -0700 | [diff] [blame^] | 164 | // TODO: Should add an OnChanged listener instead of always calling onChanged. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 165 | onChanged(); |
| 166 | return name_; |
| 167 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 168 | |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 169 | const MetaInfo& getMetaInfo() const { return metaInfo_; } |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 170 | MetaInfo& getMetaInfo() |
| 171 | { |
Jeff Thompson | 3352fc2 | 2013-09-17 12:07:14 -0700 | [diff] [blame^] | 172 | // TODO: Should add an OnChanged listener instead of always calling onChanged. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 173 | onChanged(); |
| 174 | return metaInfo_; |
| 175 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 176 | |
Jeff Thompson | 6a51333 | 2013-09-12 13:23:58 -0700 | [diff] [blame] | 177 | const Blob& getContent() const { return content_; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 178 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Set the signature to a copy of the given signature. |
| 181 | * @param signature The signature object which is cloned. |
| 182 | */ |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 183 | void setSignature(const Signature& signature) |
| 184 | { |
| 185 | signature_ = signature.clone(); |
| 186 | onChanged(); |
| 187 | } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 188 | |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Set name to a copy of the given Name. |
| 191 | * @param name The Name which is copied. |
| 192 | */ |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 193 | void setName(const Name& name) |
| 194 | { |
| 195 | name_ = name; |
| 196 | onChanged(); |
| 197 | } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 198 | |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Set metaInfo to a copy of the given MetaInfo. |
| 201 | * @param metaInfo The MetaInfo which is copied. |
| 202 | */ |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 203 | void setMetainfo(const MetaInfo& metaInfo) |
| 204 | { |
| 205 | metaInfo_ = metaInfo; |
| 206 | onChanged(); |
| 207 | } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 208 | |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 209 | /** |
| 210 | * Set the content to a copy of the data in the vector. |
| 211 | * @param content A vector whose contents are copied. |
| 212 | */ |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 213 | void setContent(const std::vector<unsigned char>& content) |
| 214 | { |
| 215 | content_ = content; |
| 216 | onChanged(); |
| 217 | } |
Jeff Thompson | 3352fc2 | 2013-09-17 12:07:14 -0700 | [diff] [blame^] | 218 | void setContent(const unsigned char* content, unsigned int contentLength) |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 219 | { |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 220 | content_ = Blob(content, contentLength); |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 221 | onChanged(); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 222 | } |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 223 | |
| 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 Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 229 | 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 Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 239 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 240 | private: |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Clear the wire encoding. |
| 243 | */ |
| 244 | void onChanged(); |
| 245 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 246 | ptr_lib::shared_ptr<Signature> signature_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 247 | Name name_; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 248 | MetaInfo metaInfo_; |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 249 | Blob content_; |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 250 | SignedBlob wireEncoding_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | } |
| 254 | |
| 255 | #endif |