blob: 8d58a652ff88b8c85db5139c8f4e2ea1c4a3552a [file] [log] [blame]
Alexander Afanasyev5d79e682012-11-19 14:12:23 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22#ifndef _NDN_CONTENT_OBJECT_HEADER_CCNB_H_
23#define _NDN_CONTENT_OBJECT_HEADER_CCNB_H_
24
25#include "ns3/integer.h"
26#include "ns3/header.h"
27#include "ns3/simple-ref-count.h"
28#include "ns3/trailer.h"
29#include "ns3/nstime.h"
30
31#include <string>
32#include <vector>
33#include <list>
34
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070035#include "ndn-name.h"
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080036
37namespace ns3 {
38namespace ndn {
39
40/**
41 * Ndn XML definition of ContentObject
42 *
43 * Only few important fields are actually implemented in the simulation
44 *
45 *
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070046 * ContentObject serializes/deserializes header up-to and including <Content> tags
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080047 * Necessary closing tags should be added using ContentObjectTail
48 *
49 * This hacks are necessary to optimize memory use (i.e., virtual payload)
50 *
51 * "<ContentObject><Signature>..</Signature><Name>...</Name><SignedInfo>...</SignedInfo><Content>"
52 *
53 */
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070054class ContentObject : public SimpleRefCount<ContentObject,Header>
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080055{
56public:
57 ////////////////////////////////////////////////////////////////////////////
58 ////////////////////////////////////////////////////////////////////////////
59 ////////////////////////////////////////////////////////////////////////////
60 /**
61 * @brief Class representing Signature section of the content object
62 */
63 class Signature
64 {
65 public:
66 /**
67 * @brief Default constructor. Creates a fake-type signature
68 */
69 inline Signature ();
70
71 /**
72 * @brief Get digest algorithm
73 */
74 inline const std::string &
75 GetDigestAlgorithm () const;
76
77 /**
78 * @brief Set digest algorithm
79 */
80 inline void
81 SetDigestAlgorithm (const std::string &digestAlgorithm);
82
83 /**
84 * @brief Get signature bits
85 */
86 inline uint32_t
87 GetSignatureBits () const;
88
89 /**
90 * @brief Set signature bits
91 */
92 inline void
93 SetSignatureBits (uint32_t signatureBits);
94
95 /**
96 * @brief Default digest algorithm ("2.16.840.1.101.3.4.2.1")
97 */
98 static const std::string DefaultDigestAlgorithm; // = "2.16.840.1.101.3.4.2.1";
99
100 private:
101 std::string m_digestAlgorithm; // if value is `2.16.840.1.101.3.4.2.1`, then SHA-256 (not supported)
102 // in NS-3 value `99.0` represents a fake digest
103 // Witness // not used in NS-3
104 uint32_t m_signatureBits; // in NS-3 a fake signature is a just 32-bits
105 };
106
107 ////////////////////////////////////////////////////////////////////////////
108 ////////////////////////////////////////////////////////////////////////////
109 ////////////////////////////////////////////////////////////////////////////
110
111 /**
112 * @brief Options for the data packet Type
113 */
114 enum ContentType
115 {
116 DATA = 0x0C04C0, // default value. If ContentObject is type of DATA, then ContentType tag will be omitted
117 ENCR = 0x10D091,
118 GONE = 0x18E344,
119 KEY = 0x28463F,
120 LINK = 0x2C834A,
121 NACK = 0x34008A
122 };
123
124 ////////////////////////////////////////////////////////////////////////////
125 ////////////////////////////////////////////////////////////////////////////
126 ////////////////////////////////////////////////////////////////////////////
127
128 /**
129 * @brief Class representing SignedInfo section of the content object
130 */
131 class SignedInfo
132 {
133 public:
134 /**
135 * @brief Default constructor
136 */
137 SignedInfo ();
138
139 /**
140 * @brief Set PublisherPublicKey digest
141 * @param digest a fake 32-bit digest is supported
142 */
143 void
144 SetPublisherPublicKeyDigest (uint32_t digest);
145
146 /**
147 * @brief Get PublisherPublicKey digest
148 */
149 uint32_t
150 GetPublisherPublicKeyDigest () const;
151
152 /**
153 * @brief Set content object timestamp
154 * @param timestamp timestamp
155 */
156 void
157 SetTimestamp (const Time &timestamp);
158
159 /**
160 * @brief Get timestamp of the content object
161 */
162 Time
163 GetTimestamp () const;
164
165 /**
166 * @brief Set ContentObject type
167 * @param type type of the content object
168 */
169 void
170 SetContentType (ContentType type);
171
172 /**
173 * @brief Get ContentObject type
174 */
175 ContentType
176 GetContentType () const;
177
178 /**
179 * @brief Set freshness of the content object
180 * @param freshness Freshness, 0s means infinity
181 */
182 void
183 SetFreshness (const Time &freshness);
184
185 /**
186 * @brief Get freshness of the content object
187 */
188 Time
189 GetFreshness () const;
190
191 /**
192 * @brief Set key locator
193 * @param keyLocator name of the key
194 *
195 * Note that only <KeyName> option for the key locator is supported
196 */
197 void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700198 SetKeyLocator (Ptr<const Name> keyLocator);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800199
200 /**
201 * @brief Get key locator
202 *
203 * Note that only <KeyName> option for the key locator is supported
204 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700205 Ptr<const Name>
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800206 GetKeyLocator () const;
207
208 private:
209 uint32_t m_publisherPublicKeyDigest; // fake publisher key digest
210 Time m_timestamp;
211 ContentType m_type;
212 Time m_freshness;
213 // FinalBlockID
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700214 Ptr<const Name> m_keyLocator; // support only <KeyName> option for KeyLocator
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800215 };
216
217 ////////////////////////////////////////////////////////////////////////////
218 ////////////////////////////////////////////////////////////////////////////
219 ////////////////////////////////////////////////////////////////////////////
220 ////////////////////////////////////////////////////////////////////////////
221 ////////////////////////////////////////////////////////////////////////////
222 ////////////////////////////////////////////////////////////////////////////
223
224 /**
225 * Constructor
226 *
227 * Creates a null header
228 **/
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700229 ContentObject ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800230
231 /**
232 * \brief Set content object name
233 *
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700234 * Sets name of the content object. For example, SetName( Name("prefix")("postfix") );
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800235 **/
236 void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700237 SetName (const Ptr<Name> &name);
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800238
239 /**
240 * @brief Get name of the content object
241 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700242 const Name&
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800243 GetName () const;
244
245 /**
246 * @brief Get smart pointer to the interest name (to avoid extra memory usage)
247 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700248 Ptr<const Name>
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800249 GetNamePtr () const;
250
251 /**
252 * @brief Get editable reference to content object's Signature
253 */
254 inline Signature &
255 GetSignature ();
256
257 /**
258 * @brief Get read-only reference to content object's Signature
259 */
260 inline const Signature &
261 GetSignature () const;
262
263 /**
264 * @brief Get editable reference to content object's SignedInfo
265 */
266 inline SignedInfo &
267 GetSignedInfo ();
268
269 /**
270 * @brief Get read-only reference to content object's SignedInfo
271 */
272 inline const SignedInfo &
273 GetSignedInfo () const;
274
275 //////////////////////////////////////////////////////////////////
276
277 static TypeId GetTypeId (void); ///< @brief Get TypeId
278 virtual TypeId GetInstanceTypeId (void) const; ///< @brief Get TypeId of the instance
279 virtual void Print (std::ostream &os) const; ///< @brief Print out information about the Header into the stream
280 virtual uint32_t GetSerializedSize (void) const; ///< @brief Get size necessary to serialize the Header
281 virtual void Serialize (Buffer::Iterator start) const; ///< @brief Serialize the Header
282 virtual uint32_t Deserialize (Buffer::Iterator start); ///< @brief Deserialize the Header
283
284private:
285 Signature m_signature;
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700286 Ptr<Name> m_name;
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800287 SignedInfo m_signedInfo;
288};
289
290/**
291 * ContentObjectTail should always be 2 bytes, representing two closing tags:
292 * "</Content><ContentObject>"
293 */
294class ContentObjectTail : public Trailer
295{
296public:
297 ContentObjectTail ();
298 //////////////////////////////////////////////////////////////////
299
300 static TypeId GetTypeId (void); ///< @brief Get TypeId
301 virtual TypeId GetInstanceTypeId (void) const; ///< @brief Get TypeId of the instance
302 virtual void Print (std::ostream &os) const; ///< @brief Print out information about Tail into the stream
303 virtual uint32_t GetSerializedSize (void) const; ///< @brief Get size necessary to serialize the Tail
304 virtual void Serialize (Buffer::Iterator start) const; ///< @brief Serialize the Tail
305 virtual uint32_t Deserialize (Buffer::Iterator start); ///< @brief Deserialize the Tail
306};
307
308
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700309ContentObject::Signature::Signature ()
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800310 : m_digestAlgorithm ("99.0")
311 , m_signatureBits (0)
312{
313}
314
315const std::string &
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700316ContentObject::Signature::GetDigestAlgorithm () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800317{
318 return m_digestAlgorithm;
319}
320
321void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700322ContentObject::Signature::SetDigestAlgorithm (const std::string &digestAlgorithm)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800323{
324 m_digestAlgorithm = digestAlgorithm;
325}
326
327uint32_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700328ContentObject::Signature::GetSignatureBits () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800329{
330 return m_signatureBits;
331}
332
333inline void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700334ContentObject::Signature::SetSignatureBits (uint32_t signature)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800335{
336 m_signatureBits = signature;
337}
338
339
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700340ContentObject::Signature &
341ContentObject::GetSignature ()
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800342{
343 return m_signature;
344}
345
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700346const ContentObject::Signature &
347ContentObject::GetSignature () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800348{
349 return m_signature;
350}
351
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700352ContentObject::SignedInfo &
353ContentObject::GetSignedInfo ()
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800354{
355 return m_signedInfo;
356}
357
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700358const ContentObject::SignedInfo &
359ContentObject::GetSignedInfo () const
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800360{
361 return m_signedInfo;
362}
363
364/**
365 * @ingroup ndn-exceptions
366 * @brief Class for ContentObject parsing exception
367 */
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700368class ContentObjectException {};
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800369
370} // namespace ndn
371} // namespace ns3
372
373#endif // _NDN_CONTENT_OBJECT_HEADER_CCNB_H_