blob: 767d57bedef63361cadfb0a9a425a16e0de1e8a0 [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 // }
193 Ccnb::AppendTaggedBlob (start, CcnbParser::CCN_DTAG_SignatureBits, m_data->GetSignature ()); // <SignatureBits />
194 Ccnb::AppendCloser (start); // </Signature>
195
196 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
197 Ccnb::AppendName (start, m_data->GetName()); // <Component>...</Component>...
198 Ccnb::AppendCloser (start); // </Name>
199
200 // fake signature
201 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo>
202 // SignedInfo ::= √PublisherPublicKeyDigest
203 // √Timestamp
204 // √Type?
205 // √FreshnessSeconds?
206 // FinalBlockID?
207 // KeyLocator?
208 // Ccnb::AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, // <PublisherPublicKeyDigest>...
209 // GetSignedInfo ().GetPublisherPublicKeyDigest ());
210
211 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_Timestamp, CcnbParser::CCN_DTAG); // <Timestamp>...
212 Ccnb::AppendTimestampBlob (start, m_data->GetTimestamp ());
213 Ccnb::AppendCloser (start);
214
215 // if (GetSignedInfo ().GetContentType () != DATA)
216 // {
217 // uint8_t type[3];
218 // type[0] = (GetSignedInfo ().GetContentType () >> 16) & 0xFF;
219 // type[1] = (GetSignedInfo ().GetContentType () >> 8 ) & 0xFF;
220 // type[2] = (GetSignedInfo ().GetContentType () ) & 0xFF;
221
222 // Ccnb::AppendTaggedBlob (start, CCN_DTAG_Type, type, 3);
223 // }
224 if (m_data->GetFreshness () > Seconds(0))
225 {
226 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_FreshnessSeconds, CcnbParser::CCN_DTAG);
227 Ccnb::AppendNumber (start, m_data->GetFreshness ().ToInteger (Time::S));
228 Ccnb::AppendCloser (start);
229 }
230 if (m_data->GetKeyLocator () != 0)
231 {
232 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_KeyLocator, CcnbParser::CCN_DTAG); // <KeyLocator>
233 {
234 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_KeyName, CcnbParser::CCN_DTAG); // <KeyName>
235 {
236 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
237 Ccnb::AppendName (start, *m_data->GetKeyLocator ()); // <Component>...</Component>...
238 Ccnb::AppendCloser (start); // </Name>
239 }
240 Ccnb::AppendCloser (start); // </KeyName>
241 }
242 Ccnb::AppendCloser (start); // </KeyLocator>
243 }
244
245 Ccnb::AppendCloser (start); // </SignedInfo>
246
247 Ccnb::AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content>
248
249 // there are no closing tags !!!
250 // The closing tag is handled by ContentObjectTail
251}
252
253uint32_t
254Data::GetSerializedSize () const
255{
256 size_t written = 0;
257 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_ContentObject); // <ContentObject>
258
259 // fake signature
260 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Signature); // <Signature>
261 // Signature ::= DigestAlgorithm?
262 // Witness?
263 // SignatureBits
264 // if (GetSignature ().GetDigestAlgorithm () != Signature::DefaultDigestAlgorithm)
265 // {
266 // written += Ccnb::EstimateString (CcnbParser::CCN_DTAG_DigestAlgorithm, GetSignature ().GetDigestAlgorithm ());
267 // }
268 written += Ccnb::EstimateTaggedBlob (CcnbParser::CCN_DTAG_SignatureBits,
269 sizeof (m_data->GetSignature ())); // <SignatureBits />
270 written += 1; // </Signature>
271
272 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
273 written += Ccnb::EstimateName (m_data->GetName ()); // <Component>...</Component>...
274 written += 1; // </Name>
275
276 // fake signature
277 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_SignedInfo); // <SignedInfo>
278 // SignedInfo ::= √PublisherPublicKeyDigest
279 // √Timestamp
280 // √Type?
281 // √FreshnessSeconds?
282 // FinalBlockID?
283 // KeyLocator?
284
285 // written += Ccnb::EstimateTaggedBlob (CCN_DTAG_PublisherPublicKeyDigest, // <PublisherPublicKeyDigest>...
286 // sizeof (GetSignedInfo ().GetPublisherPublicKeyDigest ()));
287
288 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Timestamp); // <Timestamp>...
289 written += Ccnb::EstimateTimestampBlob (m_data->GetTimestamp ());
290 written += 1;
291
292 // if (GetSignedInfo ().GetContentType () != DATA)
293 // {
294 // written += Ccnb::EstimateTaggedBlob (CcnbParser::CCN_DTAG_Type, 3);
295 // }
296 if (m_data->GetFreshness () > Seconds(0))
297 {
298 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_FreshnessSeconds);
299 written += Ccnb::EstimateNumber (m_data->GetFreshness ().ToInteger (Time::S));
300 written += 1;
301 }
302
303 if (m_data->GetKeyLocator () != 0)
304 {
305 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_KeyLocator); // <KeyLocator>
306 {
307 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_KeyName); // <KeyName>
308 {
309 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
310 written += Ccnb::EstimateName (*m_data->GetKeyLocator ()); // <Component>...</Component>...
311 written += 1; // </Name>
312 }
313 written += 1; // </KeyName>
314 }
315 written += 1; // </KeyLocator>
316 }
317
318 written += 1; // </SignedInfo>
319
320 written += Ccnb::EstimateBlockHeader (CcnbParser::CCN_DTAG_Content); // <Content>
321
322 // there are no closing tags !!!
323 // The closing tag is handled by ContentObjectTail
324 return written;
325}
326
327class ContentObjectVisitor : public CcnbParser::VoidDepthFirstVisitor
328{
329public:
330 virtual void visit (CcnbParser::Dtag &n, boost::any param/*should be ContentObject* */)
331 {
332 // uint32_t n.m_dtag;
333 // std::list< Ptr<CcnbParser::Block> > n.m_nestedBlocks;
334 static CcnbParser::NameVisitor nameVisitor;
335 static CcnbParser::NonNegativeIntegerVisitor nonNegativeIntegerVisitor;
336 static CcnbParser::TimestampVisitor timestampVisitor;
337 static CcnbParser::StringVisitor stringVisitor;
338 static CcnbParser::Uint32tBlobVisitor uint32tBlobVisitor;
339 static CcnbParser::ContentTypeVisitor contentTypeVisitor;
340
341 ndn::ContentObject &contentObject = *(boost::any_cast<ndn::ContentObject*> (param));
342
343 switch (n.m_dtag)
344 {
345 case CcnbParser::CCN_DTAG_ContentObject:
346 // process nested blocks
347 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.m_nestedTags)
348 {
349 block->accept (*this, param);
350 }
351 break;
352 case CcnbParser::CCN_DTAG_Name:
353 {
354 // process name components
355 Ptr<Name> name = Create<Name> ();
356
357 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.m_nestedTags)
358 {
359 block->accept (nameVisitor, &(*name));
360 }
361 contentObject.SetName (name);
362 break;
363 }
364
365 case CcnbParser::CCN_DTAG_Signature:
366 // process nested blocks
367 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.m_nestedTags)
368 {
369 block->accept (*this, param);
370 }
371 break;
372
373 // case CCN_DTAG_DigestAlgorithm:
374 // NS_LOG_DEBUG ("DigestAlgorithm");
375 // if (n.m_nestedTags.size ()!=1) // should be exactly one UDATA inside this tag
376 // throw CcnbParser::CcnbDecodingException ();
377
378 // contentObject.GetSignature ().SetDigestAlgorithm
379 // (boost::any_cast<std::string> ((*n.m_nestedTags.begin())->accept
380 // (stringVisitor)));
381 // break;
382
383 case CcnbParser::CCN_DTAG_SignatureBits:
384 NS_LOG_DEBUG ("SignatureBits");
385 if (n.m_nestedTags.size ()!=1) // should be only one nested tag
386 throw CcnbParser::CcnbDecodingException ();
387
388 contentObject.SetSignature
389 (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
390 (uint32tBlobVisitor)));
391 break;
392
393 case CcnbParser::CCN_DTAG_SignedInfo:
394 // process nested blocks
395 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.m_nestedTags)
396 {
397 block->accept (*this, param);
398 }
399 break;
400
401 // case CCN_DTAG_PublisherPublicKeyDigest:
402 // NS_LOG_DEBUG ("PublisherPublicKeyDigest");
403 // if (n.m_nestedTags.size ()!=1) // should be only one nested tag
404 // throw CcnbParser::CcnbDecodingException ();
405
406 // contentObject.GetSignedInfo ().SetPublisherPublicKeyDigest
407 // (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
408 // (uint32tBlobVisitor)));
409 // break;
410
411 case CcnbParser::CCN_DTAG_Timestamp:
412 NS_LOG_DEBUG ("Timestamp");
413 if (n.m_nestedTags.size()!=1) // should be exactly one nested tag
414 throw CcnbParser::CcnbDecodingException ();
415
416 contentObject.SetTimestamp
417 (boost::any_cast<Time> ((*n.m_nestedTags.begin())->accept
418 (timestampVisitor)));
419 break;
420
421 // case CCN_DTAG_Type:
422 // NS_LOG_DEBUG ("Type");
423 // if (n.m_nestedTags.size ()!=1) // should be only one nested tag
424 // throw CcnbParser::CcnbDecodingException ();
425
426 // contentObject.GetSignedInfo ().SetContentType
427 // (static_cast<Data::ContentType>
428 // (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
429 // (contentTypeVisitor))));
430 // break;
431
432 case CcnbParser::CCN_DTAG_FreshnessSeconds:
433 NS_LOG_DEBUG ("FreshnessSeconds");
434
435 if (n.m_nestedTags.size()!=1) // should be exactly one nested tag
436 throw CcnbParser::CcnbDecodingException ();
437
438 contentObject.SetFreshness
439 (Seconds
440 (boost::any_cast<uint32_t> ((*n.m_nestedTags.begin())->accept
441 (nonNegativeIntegerVisitor))));
442 break;
443
444 case CcnbParser::CCN_DTAG_KeyLocator:
445 // process nested blocks
446 BOOST_FOREACH (Ptr<CcnbParser::Block> block, n.m_nestedTags)
447 {
448 block->accept (*this, param);
449 }
450 break;
451
452 case CcnbParser::CCN_DTAG_KeyName:
453 {
454 if (n.m_nestedTags.size ()!=1) // should be exactly one nested tag
455 throw CcnbParser::CcnbDecodingException ();
456
457 Ptr<CcnbParser::BaseTag> nameTag = DynamicCast<CcnbParser::BaseTag>(n.m_nestedTags.front ());
458 if (nameTag == 0)
459 throw CcnbParser::CcnbDecodingException ();
460
461 // process name components
462 Ptr<Name> name = Create<Name> ();
463
464 BOOST_FOREACH (Ptr<CcnbParser::Block> block, nameTag->m_nestedTags)
465 {
466 block->accept (nameVisitor, &(*name));
467 }
468 contentObject.SetKeyLocator (name);
469 break;
470 }
471
472 case CcnbParser::CCN_DTAG_Content: // !!! HACK
473 // This hack was necessary for memory optimizations (i.e., content is virtual payload)
474 NS_ASSERT_MSG (n.m_nestedTags.size() == 0, "Parser should have stopped just after processing <Content> tag");
475 break;
476
477 default: // ignore all other stuff
478 break;
479 }
480 }
481};
482
483uint32_t
484Data::Deserialize (Buffer::Iterator start)
485{
486 static ContentObjectVisitor contentObjectVisitor;
487
488 Buffer::Iterator i = start;
489 Ptr<CcnbParser::Block> root = CcnbParser::Block::ParseBlock (i);
490 root->accept (contentObjectVisitor, GetPointer (m_data));
491
492 return i.GetDistanceFrom (start);
493}
494
495void
496Data::Print (std::ostream &os) const
497{
498 os << "D: " << m_data->GetName ();
499 // os << "<ContentObject><Name>" << GetName () << "</Name><Content>";
500}
501
502} // ccnb
503} // wire
504
505NDN_NAMESPACE_END