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