blob: fb87899b47b542b942859d50e8920e68908091ab [file] [log] [blame]
Alexander Afanasyev1043c702013-07-15 16:21:09 -07001/* -*- 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 "../ccnb.h"
23
24#include "wire-ccnb.h"
25
26#include "ns3/log.h"
27
28#include "ccnb-parser/common.h"
29#include "ccnb-parser/visitors/void-depth-first-visitor.h"
30#include "ccnb-parser/visitors/name-visitor.h"
31#include "ccnb-parser/visitors/non-negative-integer-visitor.h"
32#include "ccnb-parser/visitors/timestamp-visitor.h"
33#include "ccnb-parser/visitors/string-visitor.h"
34#include "ccnb-parser/visitors/uint32t-blob-visitor.h"
35#include "ccnb-parser/visitors/content-type-visitor.h"
36
37#include "ccnb-parser/syntax-tree/block.h"
38#include "ccnb-parser/syntax-tree/dtag.h"
39
40#include <boost/foreach.hpp>
41
42NS_LOG_COMPONENT_DEFINE ("ndn.wire.Ccnb.Data");
43
44NDN_NAMESPACE_BEGIN
45
46namespace wire {
47namespace ccnb {
48
49// const std::string DefaultDigestAlgorithm = "2.16.840.1.101.3.4.2.1";
50
51class DataTrailer : public Trailer
52{
53public:
54 DataTrailer ()
55 {
56 }
57
58 static TypeId GetTypeId ()
59 {
60 static TypeId tid = TypeId ("ns3::ndn::Data::Ccnb::Closer")
61 .SetGroupName ("Ndn")
62 .SetParent<Trailer> ()
63 .AddConstructor<DataTrailer> ()
64 ;
65 return tid;
66 }
67
68 virtual TypeId GetInstanceTypeId (void) const
69 {
70 return GetTypeId ();
71 }
72
73 virtual void Print (std::ostream &os) const
74 {
75 }
76
77 virtual uint32_t GetSerializedSize (void) const
78 {
79 return 2;
80 }
81
82 virtual void Serialize (Buffer::Iterator end) const
83 {
84 Buffer::Iterator i = end;
85 i.Prev (2); // Trailer interface requires us to go backwards
86
87 i.WriteU8 (0x00); // </Content>
88 i.WriteU8 (0x00); // </ContentObject>
89 }
90
91 virtual uint32_t Deserialize (Buffer::Iterator end)
92 {
93 Buffer::Iterator i = end;
94 i.Prev (2); // Trailer interface requires us to go backwards
95
96 uint8_t closing_tag_content = i.ReadU8 ();
97 NS_ASSERT_MSG (closing_tag_content==0, "Should be a closing tag </Content> (0x00)");
98
99 uint8_t closing_tag_content_object = i.ReadU8 ();
100 NS_ASSERT_MSG (closing_tag_content_object==0, "Should be a closing tag </ContentObject> (0x00)");
101
102 return 2;
103 }
104};
105
106NS_OBJECT_ENSURE_REGISTERED (Data);
107NS_OBJECT_ENSURE_REGISTERED (DataTrailer);
108
109TypeId
110Data::GetTypeId (void)
111{
112 static TypeId tid = TypeId ("ns3::ndn::Data::Ccnb")
113 .SetGroupName ("Ndn")
114 .SetParent<Header> ()
115 .AddConstructor<Data> ()
116 ;
117 return tid;
118}
119
120TypeId
121Data::GetInstanceTypeId (void) const
122{
123 return GetTypeId ();
124}
125
126Data::Data ()
127 : m_data (Create<ndn::ContentObject> ())
128{
129}
130
131Data::Data (Ptr<ndn::ContentObject> data)
132 : m_data (data)
133{
134}
135
136Ptr<ndn::ContentObject>
137Data::GetData ()
138{
139 return m_data;
140}
141
142Ptr<Packet>
143Data::ToWire (Ptr<const ndn::ContentObject> data)
144{
145 static DataTrailer trailer;
146
147 Ptr<const Packet> p = data->GetWire ();
148 if (!p)
149 {
150 Ptr<Packet> packet = Create<Packet> (*data->GetPayload ());
151 Data wireEncoding (ConstCast<ndn::ContentObject> (data));
152 packet->AddHeader (wireEncoding);
153 packet->AddTrailer (trailer);
154 data->SetWire (packet);
155
156 p = packet;
157 }
158
159 return p->Copy ();
160}
161
162Ptr<ndn::ContentObject>
163Data::FromWire (Ptr<Packet> packet)
164{
165 static DataTrailer trailer;
166
167 Ptr<ndn::ContentObject> data = Create<ndn::ContentObject> ();
168 data->SetWire (packet->Copy ());
169
170 Data wireEncoding (data);
171 packet->RemoveHeader (wireEncoding);
172 packet->RemoveTrailer (trailer);
173
174 data->SetPayload (packet);
175
176 return data;
177}
178
179void
180Data::Serialize (Buffer::Iterator start) const
181{
182 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_ContentObject, CcnbParser::CCN_DTAG); // <ContentObject>
183
184 // fake signature
185 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_Signature, CcnbParser::CCN_DTAG); // <Signature>
186 // Signature ::= √DigestAlgorithm?
187 // Witness?
188 // √SignatureBits
189 // if (GetSignature ().GetDigestAlgorithm () != Signature::DefaultDigestAlgorithm)
190 // {
191 // Ccnb::AppendString (start, CcnbParser::CCN_DTAG_DigestAlgorithm, GetSignature ().GetDigestAlgorithm ());
192 // }
Alexander Afanasyev0aa47bf2013-07-30 10:55:36 -0700193 Ccnb::AppendString (start, CcnbParser::CCN_DTAG_DigestAlgorithm, "NOP");
194 Ccnb::AppendTaggedBlobWithPadding (start, CcnbParser::CCN_DTAG_SignatureBits, 16, m_data->GetSignature ()); // <SignatureBits />
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700195 Ccnb::AppendCloser (start); // </Signature>
196
197 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700198 Ccnb::SerializeName (start, m_data->GetName()); // <Component>...</Component>...
199 Ccnb::AppendCloser (start); // </Name>
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700200
201 // fake signature
202 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo>
203 // SignedInfo ::= √PublisherPublicKeyDigest
204 // √Timestamp
205 // √Type?
206 // √FreshnessSeconds?
207 // FinalBlockID?
208 // KeyLocator?
209 // Ccnb::AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, // <PublisherPublicKeyDigest>...
210 // GetSignedInfo ().GetPublisherPublicKeyDigest ());
211
212 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_Timestamp, CcnbParser::CCN_DTAG); // <Timestamp>...
213 Ccnb::AppendTimestampBlob (start, m_data->GetTimestamp ());
214 Ccnb::AppendCloser (start);
215
216 // if (GetSignedInfo ().GetContentType () != DATA)
217 // {
218 // uint8_t type[3];
219 // type[0] = (GetSignedInfo ().GetContentType () >> 16) & 0xFF;
220 // type[1] = (GetSignedInfo ().GetContentType () >> 8 ) & 0xFF;
221 // type[2] = (GetSignedInfo ().GetContentType () ) & 0xFF;
222
223 // Ccnb::AppendTaggedBlob (start, CCN_DTAG_Type, type, 3);
224 // }
225 if (m_data->GetFreshness () > Seconds(0))
226 {
227 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_FreshnessSeconds, CcnbParser::CCN_DTAG);
228 Ccnb::AppendNumber (start, m_data->GetFreshness ().ToInteger (Time::S));
229 Ccnb::AppendCloser (start);
230 }
231 if (m_data->GetKeyLocator () != 0)
232 {
233 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_KeyLocator, CcnbParser::CCN_DTAG); // <KeyLocator>
234 {
235 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_KeyName, CcnbParser::CCN_DTAG); // <KeyName>
236 {
237 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700238 Ccnb::SerializeName (start, *m_data->GetKeyLocator ()); // <Component>...</Component>...
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700239 Ccnb::AppendCloser (start); // </Name>
240 }
241 Ccnb::AppendCloser (start); // </KeyName>
242 }
243 Ccnb::AppendCloser (start); // </KeyLocator>
244 }
245
246 Ccnb::AppendCloser (start); // </SignedInfo>
247
248 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content>
249
Alexander Afanasyevb4bf3ef2013-07-30 10:40:55 -0700250 uint32_t payloadSize = m_data->GetPayload ()->GetSize ();
251 if (payloadSize > 0)
252 Ccnb::AppendBlockHeader (start, payloadSize, CcnbParser::CCN_BLOB);
253
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700254 // there are no closing tags !!!
255 // The closing tag is handled by ContentObjectTail
256}
257
258uint32_t
259Data::GetSerializedSize () const
260{
261 size_t written = 0;
262 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_ContentObject); // <ContentObject>
263
264 // fake signature
265 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Signature); // <Signature>
266 // Signature ::= DigestAlgorithm?
267 // Witness?
268 // SignatureBits
269 // if (GetSignature ().GetDigestAlgorithm () != Signature::DefaultDigestAlgorithm)
270 // {
271 // written += Ccnb::EstimateString (CcnbParser::CCN_DTAG_DigestAlgorithm, GetSignature ().GetDigestAlgorithm ());
272 // }
Alexander Afanasyev0aa47bf2013-07-30 10:55:36 -0700273 written += Ccnb::EstimateString (CcnbParser::CCN_DTAG_DigestAlgorithm, "NOP");
274 // "signature" will be always padded to 16 octets
275 written += Ccnb::EstimateTaggedBlob (CcnbParser::CCN_DTAG_SignatureBits, 16); // <SignatureBits />
276 // written += Ccnb::EstimateTaggedBlob (CcnbParser::CCN_DTAG_SignatureBits, sizeof (m_data->GetSignature ())); // <SignatureBits />
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700277 written += 1; // </Signature>
278
279 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700280 written += Ccnb::SerializedSizeName (m_data->GetName ()); // <Component>...</Component>...
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700281 written += 1; // </Name>
282
283 // fake signature
284 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_SignedInfo); // <SignedInfo>
285 // SignedInfo ::= √PublisherPublicKeyDigest
286 // √Timestamp
287 // √Type?
288 // √FreshnessSeconds?
289 // FinalBlockID?
290 // KeyLocator?
291
292 // written += Ccnb::EstimateTaggedBlob (CCN_DTAG_PublisherPublicKeyDigest, // <PublisherPublicKeyDigest>...
293 // sizeof (GetSignedInfo ().GetPublisherPublicKeyDigest ()));
294
295 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Timestamp); // <Timestamp>...
296 written += Ccnb::EstimateTimestampBlob (m_data->GetTimestamp ());
297 written += 1;
298
299 // if (GetSignedInfo ().GetContentType () != DATA)
300 // {
301 // written += Ccnb::EstimateTaggedBlob (CcnbParser::CCN_DTAG_Type, 3);
302 // }
303 if (m_data->GetFreshness () > Seconds(0))
304 {
305 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_FreshnessSeconds);
306 written += Ccnb::EstimateNumber (m_data->GetFreshness ().ToInteger (Time::S));
307 written += 1;
308 }
309
310 if (m_data->GetKeyLocator () != 0)
311 {
312 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_KeyLocator); // <KeyLocator>
313 {
314 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_KeyName); // <KeyName>
315 {
316 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700317 written += Ccnb::SerializedSizeName (*m_data->GetKeyLocator ()); // <Component>...</Component>...
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700318 written += 1; // </Name>
319 }
320 written += 1; // </KeyName>
321 }
322 written += 1; // </KeyLocator>
323 }
324
325 written += 1; // </SignedInfo>
326
327 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Content); // <Content>
328
Alexander Afanasyevb4bf3ef2013-07-30 10:40:55 -0700329 uint32_t payloadSize = m_data->GetPayload ()->GetSize ();
330 if (payloadSize > 0)
331 written += Ccnb::EstimateBlockHeader (payloadSize);
332
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700333 // there are no closing tags !!!
334 // The closing tag is handled by ContentObjectTail
335 return written;
336}
337
338class ContentObjectVisitor : public CcnbParser::VoidDepthFirstVisitor
339{
340public:
341 virtual void visit (CcnbParser::Dtag &n, boost::any param/*should be ContentObject* */)
342 {
343 // uint32_t n.m_dtag;
344 // std::list< Ptr<CcnbParser::Block> > n.m_nestedBlocks;
345 static CcnbParser::NameVisitor nameVisitor;
346 static CcnbParser::NonNegativeIntegerVisitor nonNegativeIntegerVisitor;
347 static CcnbParser::TimestampVisitor timestampVisitor;
348 static CcnbParser::StringVisitor stringVisitor;
349 static CcnbParser::Uint32tBlobVisitor uint32tBlobVisitor;
350 static CcnbParser::ContentTypeVisitor contentTypeVisitor;
351
352 ndn::ContentObject &contentObject = *(boost::any_cast<ndn::ContentObject*> (param));
353
354 switch (n.m_dtag)
355 {
356 case CcnbParser::CCN_DTAG_ContentObject:
357 // process nested blocks
358 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.m_nestedTags)
359 {
360 block->accept (*this, param);
361 }
362 break;
363 case CcnbParser::CCN_DTAG_Name:
364 {
365 // process name components
366 Ptr<Name> name = Create<Name> ();
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700367 n.accept (nameVisitor, GetPointer (name));
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700368 contentObject.SetName (name);
369 break;
370 }
371
372 case CcnbParser::CCN_DTAG_Signature:
373 // process nested blocks
374 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.m_nestedTags)
375 {
376 block->accept (*this, param);
377 }
378 break;
379
380 // case CCN_DTAG_DigestAlgorithm:
381 // NS_LOG_DEBUG ("DigestAlgorithm");
382 // if (n.m_nestedTags.size ()!=1) // should be exactly one UDATA inside this tag
383 // throw CcnbParser::CcnbDecodingException ();
384
385 // contentObject.GetSignature ().SetDigestAlgorithm
386 // (boost::any_cast<std::string> ((*n.m_nestedTags.begin())->accept
387 // (stringVisitor)));
388 // break;
389
390 case CcnbParser::CCN_DTAG_SignatureBits:
391 NS_LOG_DEBUG ("SignatureBits");
392 if (n.m_nestedTags.size ()!=1) // should be only one nested tag
393 throw CcnbParser::CcnbDecodingException ();
394
395 contentObject.SetSignature
396 (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
397 (uint32tBlobVisitor)));
398 break;
399
400 case CcnbParser::CCN_DTAG_SignedInfo:
401 // process nested blocks
402 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.m_nestedTags)
403 {
404 block->accept (*this, param);
405 }
406 break;
407
408 // case CCN_DTAG_PublisherPublicKeyDigest:
409 // NS_LOG_DEBUG ("PublisherPublicKeyDigest");
410 // if (n.m_nestedTags.size ()!=1) // should be only one nested tag
411 // throw CcnbParser::CcnbDecodingException ();
412
413 // contentObject.GetSignedInfo ().SetPublisherPublicKeyDigest
414 // (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
415 // (uint32tBlobVisitor)));
416 // break;
417
418 case CcnbParser::CCN_DTAG_Timestamp:
419 NS_LOG_DEBUG ("Timestamp");
420 if (n.m_nestedTags.size()!=1) // should be exactly one nested tag
421 throw CcnbParser::CcnbDecodingException ();
422
423 contentObject.SetTimestamp
424 (boost::any_cast<Time> ((*n.m_nestedTags.begin())->accept
425 (timestampVisitor)));
426 break;
427
428 // case CCN_DTAG_Type:
429 // NS_LOG_DEBUG ("Type");
430 // if (n.m_nestedTags.size ()!=1) // should be only one nested tag
431 // throw CcnbParser::CcnbDecodingException ();
432
433 // contentObject.GetSignedInfo ().SetContentType
434 // (static_cast<Data::ContentType>
435 // (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
436 // (contentTypeVisitor))));
437 // break;
438
439 case CcnbParser::CCN_DTAG_FreshnessSeconds:
440 NS_LOG_DEBUG ("FreshnessSeconds");
441
442 if (n.m_nestedTags.size()!=1) // should be exactly one nested tag
443 throw CcnbParser::CcnbDecodingException ();
444
445 contentObject.SetFreshness
446 (Seconds
447 (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
448 (nonNegativeIntegerVisitor))));
449 break;
450
451 case CcnbParser::CCN_DTAG_KeyLocator:
452 // process nested blocks
453 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.m_nestedTags)
454 {
455 block->accept (*this, param);
456 }
457 break;
458
459 case CcnbParser::CCN_DTAG_KeyName:
460 {
461 if (n.m_nestedTags.size ()!=1) // should be exactly one nested tag
462 throw CcnbParser::CcnbDecodingException ();
463
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700464 // process name components
465 Ptr<Name> name = Create<Name> ();
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700466 n.accept (nameVisitor, GetPointer (name));
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700467 contentObject.SetKeyLocator (name);
468 break;
469 }
470
471 case CcnbParser::CCN_DTAG_Content: // !!! HACK
472 // This hack was necessary for memory optimizations (i.e., content is virtual payload)
473 NS_ASSERT_MSG (n.m_nestedTags.size() == 0, "Parser should have stopped just after processing <Content> tag");
474 break;
475
476 default: // ignore all other stuff
477 break;
478 }
479 }
480};
481
482uint32_t
483Data::Deserialize (Buffer::Iterator start)
484{
485 static ContentObjectVisitor contentObjectVisitor;
486
487 Buffer::Iterator i = start;
488 Ptr<CcnbParser::Block> root = CcnbParser::Block::ParseBlock (i);
489 root->accept (contentObjectVisitor, GetPointer (m_data));
490
491 return i.GetDistanceFrom (start);
492}
493
494void
495Data::Print (std::ostream &os) const
496{
497 os << "D: " << m_data->GetName ();
498 // os << "<ContentObject><Name>" << GetName () << "</Name><Content>";
499}
500
501} // ccnb
502} // wire
503
504NDN_NAMESPACE_END