blob: 64fc3e5f529cc1c62979240e6c3c7fded566ca38 [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -07002/*
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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#ifndef _NDN_CONTENT_OBJECT_HEADER_H_
23#define _NDN_CONTENT_OBJECT_HEADER_H_
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070024
Alexander Afanasyev2536e202011-08-12 14:13:10 -070025#include "ns3/integer.h"
26#include "ns3/header.h"
Alexander Afanasyeve275cf82012-04-18 14:25:02 -070027#include "ns3/simple-ref-count.h"
Alexander Afanasyev070aa482011-08-20 00:38:25 -070028#include "ns3/trailer.h"
Alexander Afanasyev9568f952012-04-05 16:09:14 -070029#include "ns3/nstime.h"
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070030
31#include <string>
32#include <vector>
33#include <list>
34
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070035#include "ndn-name-components.h"
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070036
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037namespace ns3 {
38namespace ndn {
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070039
40/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070041 * Ndn XML definition of ContentObject
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070042 *
43 * Only few important fields are actually implemented in the simulation
44 *
45 *
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070046 * ContentObjectHeader serializes/deserializes header up-to and including <Content> tags
47 * Necessary closing tags should be added using ContentObjectTail
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070048 *
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070049 * This hacks are necessary to optimize memory use (i.e., virtual payload)
50 *
51 * "<ContentObject><Signature>..</Signature><Name>...</Name><SignedInfo>...</SignedInfo><Content>"
52 *
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070053 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070054class ContentObjectHeader : public SimpleRefCount<ContentObjectHeader,Header>
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -070055{
56public:
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070057 ////////////////////////////////////////////////////////////////////////////
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
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070071 /**
72 * @brief Get digest algorithm
73 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070074 inline const std::string &
75 GetDigestAlgorithm () const;
76
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070077 /**
78 * @brief Set digest algorithm
79 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070080 inline void
81 SetDigestAlgorithm (const std::string &digestAlgorithm);
82
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070083 /**
84 * @brief Get signature bits
85 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070086 inline uint32_t
87 GetSignatureBits () const;
88
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070089 /**
90 * @brief Set signature bits
91 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070092 inline void
93 SetSignatureBits (uint32_t signatureBits);
94
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070095 /**
96 * @brief Default digest algorithm ("2.16.840.1.101.3.4.2.1")
97 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070098 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
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700111 /**
112 * @brief Options for the data packet Type
113 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700114 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 Afanasyev2b4c9472012-08-09 15:00:38 -0700198 SetKeyLocator (Ptr<const NameComponents> keyLocator);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700199
200 /**
201 * @brief Get key locator
202 *
203 * Note that only <KeyName> option for the key locator is supported
204 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700205 Ptr<const NameComponents>
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700206 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 Afanasyev2b4c9472012-08-09 15:00:38 -0700214 Ptr<const NameComponents> m_keyLocator; // support only <KeyName> option for KeyLocator
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700215 };
216
217 ////////////////////////////////////////////////////////////////////////////
218 ////////////////////////////////////////////////////////////////////////////
219 ////////////////////////////////////////////////////////////////////////////
220 ////////////////////////////////////////////////////////////////////////////
221 ////////////////////////////////////////////////////////////////////////////
222 ////////////////////////////////////////////////////////////////////////////
223
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700224 /**
225 * Constructor
226 *
227 * Creates a null header
228 **/
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700229 ContentObjectHeader ();
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700230
231 /**
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700232 * \brief Set content object name
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700233 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700234 * Sets name of the content object. For example, SetName( NameComponents("prefix")("postfix") );
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700235 **/
236 void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700237 SetName (const Ptr<NameComponents> &name);
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700238
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700239 /**
240 * @brief Get name of the content object
241 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700242 const NameComponents&
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700243 GetName () const;
244
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700245 /**
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700246 * @brief Get smart pointer to the interest name (to avoid extra memory usage)
247 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700248 Ptr<const NameComponents>
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700249 GetNamePtr () const;
250
251 /**
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700252 * @brief Get editable reference to content object's Signature
253 */
254 inline Signature &
255 GetSignature ();
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700256
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700257 /**
258 * @brief Get read-only reference to content object's Signature
259 */
260 inline const Signature &
261 GetSignature () const;
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700262
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700263 /**
264 * @brief Get editable reference to content object's SignedInfo
265 */
266 inline SignedInfo &
267 GetSignedInfo ();
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700268
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700269 /**
270 * @brief Get read-only reference to content object's SignedInfo
271 */
272 inline const SignedInfo &
273 GetSignedInfo () const;
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700274
275 //////////////////////////////////////////////////////////////////
276
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700277 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
Alexander Afanasyev9568f952012-04-05 16:09:14 -0700283
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700284private:
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700285 Signature m_signature;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700286 Ptr<NameComponents> m_name;
Alexander Afanasyev9568f952012-04-05 16:09:14 -0700287 SignedInfo m_signedInfo;
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700288};
289
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700290/**
291 * ContentObjectTail should always be 2 bytes, representing two closing tags:
292 * "</Content><ContentObject>"
293 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700294class ContentObjectTail : public Trailer
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700295{
296public:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700297 ContentObjectTail ();
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700298 //////////////////////////////////////////////////////////////////
299
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700300 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
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700306};
307
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700308
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700309ContentObjectHeader::Signature::Signature ()
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700310 : m_digestAlgorithm ("99.0")
311 , m_signatureBits (0)
312{
313}
314
315const std::string &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700316ContentObjectHeader::Signature::GetDigestAlgorithm () const
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700317{
318 return m_digestAlgorithm;
319}
320
321void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700322ContentObjectHeader::Signature::SetDigestAlgorithm (const std::string &digestAlgorithm)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700323{
324 m_digestAlgorithm = digestAlgorithm;
325}
326
327uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700328ContentObjectHeader::Signature::GetSignatureBits () const
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700329{
330 return m_signatureBits;
331}
332
333inline void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700334ContentObjectHeader::Signature::SetSignatureBits (uint32_t signature)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700335{
336 m_signatureBits = signature;
337}
338
339
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700340ContentObjectHeader::Signature &
341ContentObjectHeader::GetSignature ()
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700342{
343 return m_signature;
344}
345
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700346const ContentObjectHeader::Signature &
347ContentObjectHeader::GetSignature () const
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700348{
349 return m_signature;
350}
351
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700352ContentObjectHeader::SignedInfo &
353ContentObjectHeader::GetSignedInfo ()
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700354{
355 return m_signedInfo;
356}
357
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700358const ContentObjectHeader::SignedInfo &
359ContentObjectHeader::GetSignedInfo () const
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700360{
361 return m_signedInfo;
362}
363
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700364/**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700365 * @ingroup ndn-exceptions
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700366 * @brief Class for ContentObject parsing exception
367 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700368class ContentObjectHeaderException {};
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700369
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700370} // namespace ndn
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700371} // namespace ns3
372
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700373#endif // _NDN_CONTENT_OBJECT_HEADER_H_