blob: 1fb098fec924a82123bc8ae79c474c3de3f14afb [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson56ec9e22013-08-02 11:34:07 -07008#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07009#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -070010
Jeff Thompson46bd45f2013-08-08 16:46:41 -070011#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070012#include "name.hpp"
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080013#include "encoding/block.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070014
Jeff Thompson5cae5e52013-07-10 19:41:20 -070015
16namespace ndn {
17
Jeff Thompsonf4585af2013-09-11 14:56:59 -070018/**
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080019 * A Signature is storage for the signature-related information (info and value) in a Data packet.
Jeff Thompsonf4585af2013-09-11 14:56:59 -070020 */
Jeff Thompson5cae5e52013-07-10 19:41:20 -070021class Signature {
22public:
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080023 enum {
24 DigestSha256 = 0,
25 SignatureSha256WithRsa = 1
26 };
Jeff Thompson20af0732013-09-12 17:01:45 -070027
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080028 Signature()
29 : type_(-1)
30 {
31 }
Jeff Thompson20af0732013-09-12 17:01:45 -070032
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080033 Signature(const Block &info, const Block &value)
34 : info_(info)
35 , value_(value)
36 {
37 Buffer::const_iterator i = info_.value_begin();
38 type_ = Tlv::readVarNumber(i, info_.value_end());
39 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070040
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080041 operator bool() const
42 {
43 return type_ != -1;
44 }
45
46 uint32_t
47 getType() const
48 {
49 return type_;
50 }
51
52 const Block&
53 getInfo() const
54 {
55 return info_;
56 }
57
58 void
59 setInfo(const Block &info)
60 {
61 info_ = info;
62 if (info_.hasWire() || info_.hasValue())
63 {
64 info_.parse();
65 const Block &signatureType = info_.get(Tlv::SignatureType);
66
67 Buffer::const_iterator i = signatureType.value_begin();
68 type_ = Tlv::readVarNumber(i, signatureType.value_end());
69 }
70 else
71 {
72 type_ = -1;
73 }
74 }
75
76 const Block&
77 getValue() const
78 {
79 return value_;
80 }
81
82 void
83 setValue(const Block &value)
84 {
85 value_ = value;
86 }
87
88 void
89 reset()
90 {
91 type_ = -1;
92 info_.reset();
93 value_.reset();
94 }
95
96private:
97 int32_t type_;
98
99 Block info_;
100 Block value_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700101};
102
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700103/**
104 * An MetaInfo holds the meta info which is signed inside the data packet.
105 */
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700106class MetaInfo {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700107public:
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800108 enum {
109 TYPE_DEFAULT = 0,
110 TYPE_LINK = 1,
111 TYPE_KEY = 2
112 };
113
114 MetaInfo()
115 : type_(TYPE_DEFAULT)
116 , freshnessPeriod_(-1)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700117 {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700118 }
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800119
120 uint32_t
121 getType() const
122 { return type_; }
123
124 void
125 setType(uint32_t type)
126 { type_ = type; }
127
128 Milliseconds
129 getFreshnessPeriod() const
130 { return freshnessPeriod_; }
131
132 void
133 setFreshnessPeriod(Milliseconds freshnessPeriod)
134 { freshnessPeriod_ = freshnessPeriod; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700135
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700136private:
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800137 uint32_t type_;
138 Milliseconds freshnessPeriod_;
139
140 Block wire_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700141};
142
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700143class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700144public:
Jeff Thompson20af0732013-09-12 17:01:45 -0700145 /**
146 * Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature.
147 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800148 Data()
149 {
150 }
Jeff Thompson20af0732013-09-12 17:01:45 -0700151
152 /**
153 * Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature.
154 * @param name A reference to the name which is copied.
155 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800156 Data(const Name& name)
157 : name_(name)
158 {
159 }
Jeff Thompson25bfdca2013-10-16 17:05:41 -0700160
161 /**
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700162 * The virtual destructor.
163 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800164 virtual ~Data()
165 {
166 }
Jeff Thompsonc69163b2013-10-12 13:49:50 -0700167
168 /**
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700169 * Encode this Data for a particular wire format. If wireFormat is the default wire format, also set the defaultWireEncoding
170 * field to the encoded result.
171 * Even though this is const, if wireFormat is the default wire format we update the defaultWireEncoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700172 * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat().
173 * @return The encoded byte array.
174 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800175 const Block&
176 wireEncode() const;
177
178 void
179 wireDecode(const Block &wire);
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700180
181 /**
Jeff Thompsonf1ffba82013-10-19 17:57:12 -0700182 * Decode the input using a particular wire format and update this Data. If wireFormat is the default wire format, also
183 * set the defaultWireEncoding field to the input.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700184 * @param input The input byte array to be decoded.
185 * @param inputLength The length of input.
186 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat().
187 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800188 void
189 wireDecode(const uint8_t* input, size_t inputLength);
190
191 const Signature&
192 getSignature() const
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700193 {
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800194 return signature_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700195 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700196
197 /**
Jeff Thompson20af0732013-09-12 17:01:45 -0700198 * Set the signature to a copy of the given signature.
199 * @param signature The signature object which is cloned.
Jeff Thompson6d591972013-10-17 11:16:32 -0700200 * @return This Data so that you can chain calls to update values.
Jeff Thompson20af0732013-09-12 17:01:45 -0700201 */
Jeff Thompson6d591972013-10-17 11:16:32 -0700202 Data&
Jeff Thompson0050abe2013-09-17 12:50:25 -0700203 setSignature(const Signature& signature)
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800204 {
205 signature_ = signature;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700206 onChanged();
Jeff Thompson6d591972013-10-17 11:16:32 -0700207 return *this;
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700208 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700209
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800210 const Name&
211 getName() const
212 {
213 return name_;
214 }
215
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700216 /**
Jeff Thompson6d591972013-10-17 11:16:32 -0700217 * Set name to a copy of the given Name. This is virtual so that a subclass can override to validate the name.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700218 * @param name The Name which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700219 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700220 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800221 void
222 setName(const Name& name)
223 {
224 name_ = name;
225 onChanged();
226 }
227
228 const MetaInfo&
229 getMetaInfo() const { return metaInfo_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700230
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700231 /**
232 * Set metaInfo to a copy of the given MetaInfo.
233 * @param metaInfo The MetaInfo which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700234 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700235 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800236 void
Jeff Thompson6b58c2f2013-11-19 17:14:25 -0800237 setMetaInfo(const MetaInfo& metaInfo)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700238 {
239 metaInfo_ = metaInfo;
240 onChanged();
241 }
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700242
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800243 const Block&
244 getContent() const { return content_; }
245
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700246 /**
247 * Set the content to a copy of the data in the vector.
248 * @param content A vector whose contents are copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700249 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700250 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800251 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700252 setContent(const std::vector<uint8_t>& content)
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800253 {
254 setContent(&content[0], content.size());
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700255 onChanged();
256 }
Jeff Thompson3776d1c2013-09-17 14:22:51 -0700257
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800258 void
Jeff Thompson97223af2013-09-24 17:01:27 -0700259 setContent(const uint8_t* content, size_t contentLength)
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800260 {
261 OBufferStream os;
262 Tlv::writeVarNumber(os, Tlv::Content);
263 Tlv::writeVarNumber(os, contentLength);
264 os.write(reinterpret_cast<const char *>(content), contentLength);
265
266 content_ = Block(os.buf());
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700267 onChanged();
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700268 }
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800269
270 void
271 setContent(const ConstBufferPtr &contentValue)
272 {
273 content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet
274 onChanged();
275 }
276
277 void
278 setContent(const Block& content)
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700279 {
280 content_ = content;
281 onChanged();
282 }
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700283
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700284private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700285 /**
286 * Clear the wire encoding.
287 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800288 inline void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700289 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800290
291private:
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700292 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700293 MetaInfo metaInfo_;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800294 Block content_;
295 Signature signature_;
296
297 Block wire_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700298};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800299
300
301inline void
302Data::onChanged()
303{
304 // The values have changed, so the signature and wire format is invalidated
305 signature_.reset();
306 wire_.reset();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700307}
308
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800309} // namespace ndn
310
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700311#endif