blob: 112f141807960139c5f46577151ed8a495b2e2ca [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#include "ndn-content-object-header-ccnb.h"
23
24#include "ns3/log.h"
25#include "../helper/ndn-encoding-helper.h"
26#include "../helper/ndn-decoding-helper.h"
27
28#include "../helper/ccnb-parser/common.h"
29#include "../helper/ccnb-parser/visitors/void-depth-first-visitor.h"
30#include "../helper/ccnb-parser/visitors/name-components-visitor.h"
31#include "../helper/ccnb-parser/visitors/non-negative-integer-visitor.h"
32#include "../helper/ccnb-parser/visitors/timestamp-visitor.h"
33#include "../helper/ccnb-parser/visitors/string-visitor.h"
34#include "../helper/ccnb-parser/visitors/uint32t-blob-visitor.h"
35#include "../helper/ccnb-parser/visitors/content-type-visitor.h"
36
37#include "../helper/ccnb-parser/syntax-tree/block.h"
38#include "../helper/ccnb-parser/syntax-tree/dtag.h"
39
40#include <boost/foreach.hpp>
41
42NS_LOG_COMPONENT_DEFINE ("ndn.ContentObjectHeader");
43
44namespace ns3 {
45namespace ndn {
46
47using namespace CcnbParser;
48
49const std::string ContentObjectHeader::Signature::DefaultDigestAlgorithm = "2.16.840.1.101.3.4.2.1";
50
51NS_OBJECT_ENSURE_REGISTERED (ContentObjectHeader);
52NS_OBJECT_ENSURE_REGISTERED (ContentObjectTail);
53
54TypeId
55ContentObjectHeader::GetTypeId (void)
56{
57 static TypeId tid = TypeId ("ns3::ndn::ContentObjectHeader")
58 .SetGroupName ("Ndn")
59 .SetParent<Header> ()
60 .AddConstructor<ContentObjectHeader> ()
61 ;
62 return tid;
63}
64
65ContentObjectHeader::ContentObjectHeader ()
66{
67}
68
69void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070070ContentObjectHeader::SetName (const Ptr<Name> &name)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080071{
72 m_name = name;
73}
74
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070075const Name&
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080076ContentObjectHeader::GetName () const
77{
78 if (m_name==0) throw ContentObjectHeaderException();
79 return *m_name;
80}
81
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070082Ptr<const Name>
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080083ContentObjectHeader::GetNamePtr () const
84{
85 return m_name;
86}
87
88#define CCNB EncodingHelper // just to simplify writing
89
90void
91ContentObjectHeader::Serialize (Buffer::Iterator start) const
92{
93 size_t written = 0;
94 written += CCNB::AppendBlockHeader (start, CCN_DTAG_ContentObject, CCN_DTAG); // <ContentObject>
95
96 // fake signature
97 written += CCNB::AppendBlockHeader (start, CCN_DTAG_Signature, CCN_DTAG); // <Signature>
98 // Signature ::= √DigestAlgorithm?
99 // Witness?
100 // √SignatureBits
101 if (GetSignature ().GetDigestAlgorithm () != Signature::DefaultDigestAlgorithm)
102 {
103 written += CCNB::AppendString (start, CCN_DTAG_DigestAlgorithm, GetSignature ().GetDigestAlgorithm ());
104 }
105 written += CCNB::AppendTaggedBlob (start, CCN_DTAG_SignatureBits, GetSignature ().GetSignatureBits ()); // <SignatureBits />
106 written += CCNB::AppendCloser (start); // </Signature>
107
108 written += CCNB::AppendBlockHeader (start, CCN_DTAG_Name, CCN_DTAG); // <Name>
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700109 written += CCNB::AppendName (start, GetName()); // <Component>...</Component>...
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800110 written += CCNB::AppendCloser (start); // </Name>
111
112 // fake signature
113 written += CCNB::AppendBlockHeader (start, CCN_DTAG_SignedInfo, CCN_DTAG); // <SignedInfo>
114 // SignedInfo ::= √PublisherPublicKeyDigest
115 // √Timestamp
116 // √Type?
117 // √FreshnessSeconds?
118 // FinalBlockID?
119 // KeyLocator?
120 written += CCNB::AppendTaggedBlob (start, CCN_DTAG_PublisherPublicKeyDigest, // <PublisherPublicKeyDigest>...
121 GetSignedInfo ().GetPublisherPublicKeyDigest ());
122
123 written += CCNB::AppendBlockHeader (start, CCN_DTAG_Timestamp, CCN_DTAG); // <Timestamp>...
124 written += CCNB::AppendTimestampBlob (start, GetSignedInfo ().GetTimestamp ());
125 written += CCNB::AppendCloser (start);
126
127 if (GetSignedInfo ().GetContentType () != DATA)
128 {
129 uint8_t type[3];
130 type[0] = (GetSignedInfo ().GetContentType () >> 16) & 0xFF;
131 type[1] = (GetSignedInfo ().GetContentType () >> 8 ) & 0xFF;
132 type[2] = (GetSignedInfo ().GetContentType () ) & 0xFF;
133
134 written += CCNB::AppendTaggedBlob (start, CCN_DTAG_Type, type, 3);
135 }
136 if (GetSignedInfo ().GetFreshness () > Seconds(0))
137 {
138 written += CCNB::AppendBlockHeader (start, CCN_DTAG_FreshnessSeconds, CCN_DTAG);
139 written += CCNB::AppendNumber (start, GetSignedInfo ().GetFreshness ().ToInteger (Time::S));
140 written += CCNB::AppendCloser (start);
141 }
142 if (GetSignedInfo ().GetKeyLocator () != 0)
143 {
144 written += CCNB::AppendBlockHeader (start, CCN_DTAG_KeyLocator, CCN_DTAG); // <KeyLocator>
145 {
146 written += CCNB::AppendBlockHeader (start, CCN_DTAG_KeyName, CCN_DTAG); // <KeyName>
147 {
148 written += CCNB::AppendBlockHeader (start, CCN_DTAG_Name, CCN_DTAG); // <Name>
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700149 written += CCNB::AppendName (start, GetName()); // <Component>...</Component>...
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800150 written += CCNB::AppendCloser (start); // </Name>
151 }
152 written += CCNB::AppendCloser (start); // </KeyName>
153 }
154 written += CCNB::AppendCloser (start); // </KeyLocator>
155 }
156
157 written += CCNB::AppendCloser (start); // </SignedInfo>
158
159 written += CCNB::AppendBlockHeader (start, CCN_DTAG_Content, CCN_DTAG); // <Content>
160
161 // there are no closing tags !!!
162 // The closing tag is handled by ContentObjectTail
163}
164
165uint32_t
166ContentObjectHeader::GetSerializedSize () const
167{
168 size_t written = 0;
169 written += CCNB::EstimateBlockHeader (CCN_DTAG_ContentObject); // <ContentObject>
170
171 // fake signature
172 written += CCNB::EstimateBlockHeader (CCN_DTAG_Signature); // <Signature>
173 // Signature ::= DigestAlgorithm?
174 // Witness?
175 // SignatureBits
176 if (GetSignature ().GetDigestAlgorithm () != Signature::DefaultDigestAlgorithm)
177 {
178 written += CCNB::EstimateString (CCN_DTAG_DigestAlgorithm, GetSignature ().GetDigestAlgorithm ());
179 }
180 written += CCNB::EstimateTaggedBlob (CCN_DTAG_SignatureBits,
181 sizeof (GetSignature ().GetSignatureBits ())); // <SignatureBits />
182 written += 1; // </Signature>
183
184 written += CCNB::EstimateBlockHeader (CCN_DTAG_Name); // <Name>
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700185 written += CCNB::EstimateName (GetName()); // <Component>...</Component>...
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800186 written += 1; // </Name>
187
188 // fake signature
189 written += CCNB::EstimateBlockHeader (CCN_DTAG_SignedInfo); // <SignedInfo>
190 // SignedInfo ::= √PublisherPublicKeyDigest
191 // √Timestamp
192 // √Type?
193 // √FreshnessSeconds?
194 // FinalBlockID?
195 // KeyLocator?
196
197 written += CCNB::EstimateTaggedBlob (CCN_DTAG_PublisherPublicKeyDigest, // <PublisherPublicKeyDigest>...
198 sizeof (GetSignedInfo ().GetPublisherPublicKeyDigest ()));
199
200 written += CCNB::EstimateBlockHeader (CCN_DTAG_Timestamp); // <Timestamp>...
201 written += CCNB::EstimateTimestampBlob (GetSignedInfo ().GetTimestamp ());
202 written += 1;
203
204 if (GetSignedInfo ().GetContentType () != DATA)
205 {
206 written += CCNB::EstimateTaggedBlob (CCN_DTAG_Type, 3);
207 }
208 if (GetSignedInfo ().GetFreshness () > Seconds(0))
209 {
210 written += CCNB::EstimateBlockHeader (CCN_DTAG_FreshnessSeconds);
211 written += CCNB::EstimateNumber (GetSignedInfo ().GetFreshness ().ToInteger (Time::S));
212 written += 1;
213 }
214
215 if (GetSignedInfo ().GetKeyLocator () != 0)
216 {
217 written += CCNB::EstimateBlockHeader (CCN_DTAG_KeyLocator); // <KeyLocator>
218 {
219 written += CCNB::EstimateBlockHeader (CCN_DTAG_KeyName); // <KeyName>
220 {
221 written += CCNB::EstimateBlockHeader (CCN_DTAG_Name); // <Name>
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700222 written += CCNB::EstimateName (GetName()); // <Component>...</Component>...
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800223 written += 1; // </Name>
224 }
225 written += 1; // </KeyName>
226 }
227 written += 1; // </KeyLocator>
228 }
229
230 written += 1; // </SignedInfo>
231
232 written += CCNB::EstimateBlockHeader (CCN_DTAG_Content); // <Content>
233
234 // there are no closing tags !!!
235 // The closing tag is handled by ContentObjectTail
236 return written;
237}
238#undef CCNB
239
240class ContentObjectVisitor : public VoidDepthFirstVisitor
241{
242public:
243 virtual void visit (Dtag &n, boost::any param/*should be ContentObjectHeader* */)
244 {
245 // uint32_t n.m_dtag;
246 // std::list<Ptr<Block> > n.m_nestedBlocks;
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700247 static NameVisitor nameComponentsVisitor;
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800248 static NonNegativeIntegerVisitor nonNegativeIntegerVisitor;
249 static TimestampVisitor timestampVisitor;
250 static StringVisitor stringVisitor;
251 static Uint32tBlobVisitor uint32tBlobVisitor;
252 static ContentTypeVisitor contentTypeVisitor;
253
254 ContentObjectHeader &contentObject = *(boost::any_cast<ContentObjectHeader*> (param));
255
256 switch (n.m_dtag)
257 {
258 case CCN_DTAG_ContentObject:
259 // process nested blocks
260 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
261 {
262 block->accept (*this, param);
263 }
264 break;
265 case CCN_DTAG_Name:
266 {
267 // process name components
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700268 Ptr<Name> name = Create<Name> ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800269
270 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
271 {
272 block->accept (nameComponentsVisitor, &(*name));
273 }
274 contentObject.SetName (name);
275 break;
276 }
277
278 case CCN_DTAG_Signature:
279 // process nested blocks
280 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
281 {
282 block->accept (*this, param);
283 }
284 break;
285
286 case CCN_DTAG_DigestAlgorithm:
287 NS_LOG_DEBUG ("DigestAlgorithm");
288 if (n.m_nestedTags.size ()!=1) // should be exactly one UDATA inside this tag
289 throw CcnbDecodingException ();
290
291 contentObject.GetSignature ().SetDigestAlgorithm
292 (boost::any_cast<std::string> ((*n.m_nestedTags.begin())->accept
293 (stringVisitor)));
294 break;
295
296 case CCN_DTAG_SignatureBits:
297 NS_LOG_DEBUG ("SignatureBits");
298 if (n.m_nestedTags.size ()!=1) // should be only one nested tag
299 throw CcnbDecodingException ();
300
301 contentObject.GetSignature ().SetSignatureBits
302 (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
303 (uint32tBlobVisitor)));
304 break;
305
306 case CCN_DTAG_SignedInfo:
307 // process nested blocks
308 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
309 {
310 block->accept (*this, param);
311 }
312 break;
313
314 case CCN_DTAG_PublisherPublicKeyDigest:
315 NS_LOG_DEBUG ("PublisherPublicKeyDigest");
316 if (n.m_nestedTags.size ()!=1) // should be only one nested tag
317 throw CcnbDecodingException ();
318
319 contentObject.GetSignedInfo ().SetPublisherPublicKeyDigest
320 (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
321 (uint32tBlobVisitor)));
322 break;
323
324 case CCN_DTAG_Timestamp:
325 NS_LOG_DEBUG ("Timestamp");
326 if (n.m_nestedTags.size()!=1) // should be exactly one nested tag
327 throw CcnbDecodingException ();
328
329 contentObject.GetSignedInfo ().SetTimestamp
330 (boost::any_cast<Time> ((*n.m_nestedTags.begin())->accept
331 (timestampVisitor)));
332 break;
333
334 case CCN_DTAG_Type:
335 NS_LOG_DEBUG ("Type");
336 if (n.m_nestedTags.size ()!=1) // should be only one nested tag
337 throw CcnbDecodingException ();
338
339 contentObject.GetSignedInfo ().SetContentType
340 (static_cast<ContentObjectHeader::ContentType>
341 (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
342 (contentTypeVisitor))));
343 break;
344
345 case CCN_DTAG_FreshnessSeconds:
346 NS_LOG_DEBUG ("FreshnessSeconds");
347
348 if (n.m_nestedTags.size()!=1) // should be exactly one nested tag
349 throw CcnbDecodingException ();
350
351 contentObject.GetSignedInfo ().SetFreshness
352 (Seconds
353 (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
354 (nonNegativeIntegerVisitor))));
355 break;
356
357 case CCN_DTAG_KeyLocator:
358 // process nested blocks
359 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
360 {
361 block->accept (*this, param);
362 }
363 break;
364
365 case CCN_DTAG_KeyName:
366 {
367 if (n.m_nestedTags.size ()!=1) // should be exactly one nested tag
368 throw CcnbDecodingException ();
369
370 Ptr<BaseTag> nameTag = DynamicCast<BaseTag>(n.m_nestedTags.front ());
371 if (nameTag == 0)
372 throw CcnbDecodingException ();
373
374 // process name components
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700375 Ptr<Name> name = Create<Name> ();
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800376
377 BOOST_FOREACH (Ptr<Block> block, nameTag->m_nestedTags)
378 {
379 block->accept (nameComponentsVisitor, &(*name));
380 }
381 contentObject.GetSignedInfo ().SetKeyLocator (name);
382 break;
383 }
384
385 case CCN_DTAG_Content: // !!! HACK
386 // This hack was necessary for memory optimizations (i.e., content is virtual payload)
387 NS_ASSERT_MSG (n.m_nestedTags.size() == 0, "Parser should have stopped just after processing <Content> tag");
388 break;
389
390 default: // ignore all other stuff
391 break;
392 }
393 }
394};
395
396uint32_t
397ContentObjectHeader::Deserialize (Buffer::Iterator start)
398{
399 static ContentObjectVisitor contentObjectVisitor;
400
401 Buffer::Iterator i = start;
402 Ptr<Block> root = Block::ParseBlock (i);
403 root->accept (contentObjectVisitor, this);
404
405 return i.GetDistanceFrom (start);
406}
407
408TypeId
409ContentObjectHeader::GetInstanceTypeId (void) const
410{
411 return GetTypeId ();
412}
413
414void
415ContentObjectHeader::Print (std::ostream &os) const
416{
417 os << "D: " << GetName ();
418 // os << "<ContentObject><Name>" << GetName () << "</Name><Content>";
419}
420
421////////////////////////////////////////////////////////////////////////////////////////////////////
422
423ContentObjectTail::ContentObjectTail ()
424{
425}
426
427TypeId
428ContentObjectTail::GetTypeId (void)
429{
430 static TypeId tid = TypeId ("ns3::ndn::ContentObjectTail")
431 .SetParent<Trailer> ()
432 .AddConstructor<ContentObjectTail> ()
433 ;
434 return tid;
435}
436
437TypeId
438ContentObjectTail::GetInstanceTypeId (void) const
439{
440 return GetTypeId ();
441}
442
443void
444ContentObjectTail::Print (std::ostream &os) const
445{
446 os << "</Content></ContentObject>";
447}
448
449uint32_t
450ContentObjectTail::GetSerializedSize (void) const
451{
452 return 2;
453}
454
455void
456ContentObjectTail::Serialize (Buffer::Iterator start) const
457{
458 Buffer::Iterator i = start;
459 i.Prev (2); // Trailer interface requires us to go backwards
460
461 i.WriteU8 (0x00); // </Content>
462 i.WriteU8 (0x00); // </ContentObject>
463}
464
465uint32_t
466ContentObjectTail::Deserialize (Buffer::Iterator start)
467{
468 Buffer::Iterator i = start;
469 i.Prev (2); // Trailer interface requires us to go backwards
470
471 uint8_t closing_tag_content = i.ReadU8 ();
472 NS_ASSERT_MSG (closing_tag_content==0, "Should be a closing tag </Content> (0x00)");
473
474 uint8_t closing_tag_content_object = i.ReadU8 ();
475 NS_ASSERT_MSG (closing_tag_content_object==0, "Should be a closing tag </ContentObject> (0x00)");
476
477 return 2;
478}
479
480///////////////////////////////////////////////////////////////////////
481///////////////////////////////////////////////////////////////////////
482///////////////////////////////////////////////////////////////////////
483
484ContentObjectHeader::SignedInfo::SignedInfo ()
485 : m_publisherPublicKeyDigest (0)
486 // , m_timestamp
487 , m_type (DATA)
488 // , m_freshness
489 // , FinalBlockID
490 // , KeyLocator
491{
492}
493
494void
495ContentObjectHeader::SignedInfo::SetPublisherPublicKeyDigest (uint32_t digest)
496{
497 m_publisherPublicKeyDigest = digest;
498}
499
500uint32_t
501ContentObjectHeader::SignedInfo::GetPublisherPublicKeyDigest () const
502{
503 return m_publisherPublicKeyDigest;
504}
505
506void
507ContentObjectHeader::SignedInfo::SetTimestamp (const Time &timestamp)
508{
509 m_timestamp = timestamp;
510}
511
512Time
513ContentObjectHeader::SignedInfo::GetTimestamp () const
514{
515 return m_timestamp;
516}
517
518void
519ContentObjectHeader::SignedInfo::SetContentType (ContentObjectHeader::ContentType type)
520{
521 m_type = type;
522}
523
524ContentObjectHeader::ContentType
525ContentObjectHeader::SignedInfo::GetContentType () const
526{
527 return m_type;
528}
529
530void
531ContentObjectHeader::SignedInfo::SetFreshness (const Time &freshness)
532{
533 m_freshness = freshness;
534}
535
536Time
537ContentObjectHeader::SignedInfo::GetFreshness () const
538{
539 return m_freshness;
540}
541
542void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700543ContentObjectHeader::SignedInfo::SetKeyLocator (Ptr<const Name> keyLocator)
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800544{
545 m_keyLocator = keyLocator;
546}
547
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700548Ptr<const Name>
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800549ContentObjectHeader::SignedInfo::GetKeyLocator () const
550{
551 return m_keyLocator;
552}
553
554} // namespace ndn
555} // namespace ns3