Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 22 | #include "ndn-content-object-header.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 24 | #include "ns3/log.h" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 25 | #include "../helper/ndn-encoding-helper.h" |
| 26 | #include "../helper/ndn-decoding-helper.h" |
Alexander Afanasyev | 45b92d4 | 2011-08-14 23:11:38 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 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" |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 37 | #include "../helper/ccnb-parser/syntax-tree/block.h" |
| 38 | #include "../helper/ccnb-parser/syntax-tree/dtag.h" |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 39 | |
| 40 | #include <boost/foreach.hpp> |
| 41 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 42 | NS_LOG_COMPONENT_DEFINE ("ndn.ContentObjectHeader"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 44 | namespace ns3 { |
| 45 | namespace ndn { |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 46 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 47 | using namespace CcnbParser; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 49 | const std::string ContentObjectHeader::Signature::DefaultDigestAlgorithm = "2.16.840.1.101.3.4.2.1"; |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 51 | NS_OBJECT_ENSURE_REGISTERED (ContentObjectHeader); |
| 52 | NS_OBJECT_ENSURE_REGISTERED (ContentObjectTail); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 53 | |
| 54 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 55 | ContentObjectHeader::GetTypeId (void) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 56 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 57 | static TypeId tid = TypeId ("ns3::ndn::ContentObjectHeader") |
| 58 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 59 | .SetParent<Header> () |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 60 | .AddConstructor<ContentObjectHeader> () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 61 | ; |
| 62 | return tid; |
| 63 | } |
| 64 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 65 | ContentObjectHeader::ContentObjectHeader () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 66 | { |
| 67 | } |
| 68 | |
| 69 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 70 | ContentObjectHeader::SetName (const Ptr<NameComponents> &name) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 71 | { |
| 72 | m_name = name; |
| 73 | } |
| 74 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 75 | const NameComponents& |
| 76 | ContentObjectHeader::GetName () const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 77 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 78 | if (m_name==0) throw ContentObjectHeaderException(); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 79 | return *m_name; |
| 80 | } |
| 81 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 82 | Ptr<const NameComponents> |
| 83 | ContentObjectHeader::GetNamePtr () const |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 84 | { |
| 85 | return m_name; |
| 86 | } |
| 87 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 88 | #define CCNB EncodingHelper // just to simplify writing |
Alexander Afanasyev | 9568f95 | 2012-04-05 16:09:14 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 90 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 91 | ContentObjectHeader::Serialize (Buffer::Iterator start) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 92 | { |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 93 | size_t written = 0; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 94 | written += CCNB::AppendBlockHeader (start, CCN_DTAG_ContentObject, CCN_DTAG); // <ContentObject> |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 95 | |
| 96 | // fake signature |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 97 | written += CCNB::AppendBlockHeader (start, CCN_DTAG_Signature, CCN_DTAG); // <Signature> |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 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> |
| 109 | written += CCNB::AppendNameComponents (start, GetName()); // <Component>...</Component>... |
| 110 | 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 | } |
Alexander Afanasyev | faf9581 | 2012-06-05 21:28:11 -0700 | [diff] [blame] | 136 | if (GetSignedInfo ().GetFreshness () > Seconds(0)) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 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 | } |
Alexander Afanasyev | faf9581 | 2012-06-05 21:28:11 -0700 | [diff] [blame] | 142 | if (GetSignedInfo ().GetKeyLocator () != 0) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 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> |
| 149 | written += CCNB::AppendNameComponents (start, GetName()); // <Component>...</Component>... |
| 150 | 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 !!! |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 162 | // The closing tag is handled by ContentObjectTail |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 166 | ContentObjectHeader::GetSerializedSize () const |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 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> |
| 185 | written += CCNB::EstimateNameComponents (GetName()); // <Component>...</Component>... |
| 186 | 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 | } |
Alexander Afanasyev | faf9581 | 2012-06-05 21:28:11 -0700 | [diff] [blame] | 208 | if (GetSignedInfo ().GetFreshness () > Seconds(0)) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 209 | { |
| 210 | written += CCNB::EstimateBlockHeader (CCN_DTAG_FreshnessSeconds); |
| 211 | written += CCNB::EstimateNumber (GetSignedInfo ().GetFreshness ().ToInteger (Time::S)); |
| 212 | written += 1; |
| 213 | } |
| 214 | |
Alexander Afanasyev | faf9581 | 2012-06-05 21:28:11 -0700 | [diff] [blame] | 215 | if (GetSignedInfo ().GetKeyLocator () != 0) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 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> |
| 222 | written += CCNB::EstimateNameComponents (GetName()); // <Component>...</Component>... |
| 223 | 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 !!! |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 235 | // The closing tag is handled by ContentObjectTail |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 236 | return written; |
| 237 | } |
| 238 | #undef CCNB |
| 239 | |
| 240 | class ContentObjectVisitor : public VoidDepthFirstVisitor |
| 241 | { |
| 242 | public: |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 243 | virtual void visit (Dtag &n, boost::any param/*should be ContentObjectHeader* */) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 244 | { |
| 245 | // uint32_t n.m_dtag; |
| 246 | // std::list<Ptr<Block> > n.m_nestedBlocks; |
| 247 | static NameComponentsVisitor nameComponentsVisitor; |
| 248 | static NonNegativeIntegerVisitor nonNegativeIntegerVisitor; |
| 249 | static TimestampVisitor timestampVisitor; |
| 250 | static StringVisitor stringVisitor; |
| 251 | static Uint32tBlobVisitor uint32tBlobVisitor; |
| 252 | static ContentTypeVisitor contentTypeVisitor; |
| 253 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 254 | ContentObjectHeader &contentObject = *(boost::any_cast<ContentObjectHeader*> (param)); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 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 Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 268 | Ptr<NameComponents> name = Create<NameComponents> (); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 269 | |
| 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 |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 340 | (static_cast<ContentObjectHeader::ContentType> |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 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 Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 375 | Ptr<NameComponents> name = Create<NameComponents> (); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 376 | |
| 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 | |
| 396 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 397 | ContentObjectHeader::Deserialize (Buffer::Iterator start) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 398 | { |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 399 | static ContentObjectVisitor contentObjectVisitor; |
| 400 | |
| 401 | Buffer::Iterator i = start; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 402 | Ptr<Block> root = Block::ParseBlock (i); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 403 | root->accept (contentObjectVisitor, this); |
| 404 | |
| 405 | return i.GetDistanceFrom (start); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 409 | ContentObjectHeader::GetInstanceTypeId (void) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 410 | { |
| 411 | return GetTypeId (); |
| 412 | } |
| 413 | |
| 414 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 415 | ContentObjectHeader::Print (std::ostream &os) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 416 | { |
Alexander Afanasyev | 7f3e49e | 2012-04-30 00:17:07 -0700 | [diff] [blame] | 417 | os << "D: " << GetName (); |
| 418 | // os << "<ContentObject><Name>" << GetName () << "</Name><Content>"; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 421 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 422 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 423 | ContentObjectTail::ContentObjectTail () |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 424 | { |
| 425 | } |
| 426 | |
| 427 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 428 | ContentObjectTail::GetTypeId (void) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 429 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 430 | static TypeId tid = TypeId ("ns3::ndn::ContentObjectTail") |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 431 | .SetParent<Trailer> () |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 432 | .AddConstructor<ContentObjectTail> () |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 433 | ; |
| 434 | return tid; |
| 435 | } |
| 436 | |
| 437 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 438 | ContentObjectTail::GetInstanceTypeId (void) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 439 | { |
| 440 | return GetTypeId (); |
| 441 | } |
| 442 | |
| 443 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 444 | ContentObjectTail::Print (std::ostream &os) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 445 | { |
| 446 | os << "</Content></ContentObject>"; |
| 447 | } |
| 448 | |
| 449 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 450 | ContentObjectTail::GetSerializedSize (void) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 451 | { |
| 452 | return 2; |
| 453 | } |
| 454 | |
| 455 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 456 | ContentObjectTail::Serialize (Buffer::Iterator start) const |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 457 | { |
| 458 | Buffer::Iterator i = start; |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 459 | i.Prev (2); // Trailer interface requires us to go backwards |
| 460 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 461 | i.WriteU8 (0x00); // </Content> |
| 462 | i.WriteU8 (0x00); // </ContentObject> |
| 463 | } |
| 464 | |
| 465 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 466 | ContentObjectTail::Deserialize (Buffer::Iterator start) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 467 | { |
| 468 | Buffer::Iterator i = start; |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 469 | i.Prev (2); // Trailer interface requires us to go backwards |
| 470 | |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 471 | uint8_t closing_tag_content = i.ReadU8 (); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 472 | NS_ASSERT_MSG (closing_tag_content==0, "Should be a closing tag </Content> (0x00)"); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 473 | |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 474 | uint8_t closing_tag_content_object = i.ReadU8 (); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 475 | NS_ASSERT_MSG (closing_tag_content_object==0, "Should be a closing tag </ContentObject> (0x00)"); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 476 | |
| 477 | return 2; |
| 478 | } |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 479 | |
| 480 | /////////////////////////////////////////////////////////////////////// |
| 481 | /////////////////////////////////////////////////////////////////////// |
| 482 | /////////////////////////////////////////////////////////////////////// |
| 483 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 484 | ContentObjectHeader::SignedInfo::SignedInfo () |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 485 | : m_publisherPublicKeyDigest (0) |
| 486 | // , m_timestamp |
| 487 | , m_type (DATA) |
| 488 | // , m_freshness |
| 489 | // , FinalBlockID |
| 490 | // , KeyLocator |
| 491 | { |
| 492 | } |
| 493 | |
| 494 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 495 | ContentObjectHeader::SignedInfo::SetPublisherPublicKeyDigest (uint32_t digest) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 496 | { |
| 497 | m_publisherPublicKeyDigest = digest; |
| 498 | } |
| 499 | |
| 500 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 501 | ContentObjectHeader::SignedInfo::GetPublisherPublicKeyDigest () const |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 502 | { |
| 503 | return m_publisherPublicKeyDigest; |
| 504 | } |
| 505 | |
| 506 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 507 | ContentObjectHeader::SignedInfo::SetTimestamp (const Time ×tamp) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 508 | { |
| 509 | m_timestamp = timestamp; |
| 510 | } |
| 511 | |
| 512 | Time |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 513 | ContentObjectHeader::SignedInfo::GetTimestamp () const |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 514 | { |
| 515 | return m_timestamp; |
| 516 | } |
| 517 | |
| 518 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 519 | ContentObjectHeader::SignedInfo::SetContentType (ContentObjectHeader::ContentType type) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 520 | { |
| 521 | m_type = type; |
| 522 | } |
| 523 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 524 | ContentObjectHeader::ContentType |
| 525 | ContentObjectHeader::SignedInfo::GetContentType () const |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 526 | { |
| 527 | return m_type; |
| 528 | } |
| 529 | |
| 530 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 531 | ContentObjectHeader::SignedInfo::SetFreshness (const Time &freshness) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 532 | { |
| 533 | m_freshness = freshness; |
| 534 | } |
| 535 | |
| 536 | Time |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 537 | ContentObjectHeader::SignedInfo::GetFreshness () const |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 538 | { |
| 539 | return m_freshness; |
| 540 | } |
| 541 | |
| 542 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 543 | ContentObjectHeader::SignedInfo::SetKeyLocator (Ptr<const NameComponents> keyLocator) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 544 | { |
| 545 | m_keyLocator = keyLocator; |
| 546 | } |
| 547 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 548 | Ptr<const NameComponents> |
| 549 | ContentObjectHeader::SignedInfo::GetKeyLocator () const |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 550 | { |
| 551 | return m_keyLocator; |
| 552 | } |
| 553 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 554 | } // namespace ndn |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 555 | } // namespace ns3 |