Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 1 | /* -*- 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 | #ifndef _NDN_CONTENT_OBJECT_HEADER_CCNB_H_ |
| 23 | #define _NDN_CONTENT_OBJECT_HEADER_CCNB_H_ |
| 24 | |
| 25 | #include "ns3/integer.h" |
| 26 | #include "ns3/header.h" |
| 27 | #include "ns3/simple-ref-count.h" |
| 28 | #include "ns3/trailer.h" |
| 29 | #include "ns3/nstime.h" |
| 30 | |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | #include <list> |
| 34 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 35 | #include "ndn-name.h" |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 36 | |
| 37 | namespace ns3 { |
| 38 | namespace ndn { |
| 39 | |
| 40 | /** |
| 41 | * Ndn XML definition of ContentObject |
| 42 | * |
| 43 | * Only few important fields are actually implemented in the simulation |
| 44 | * |
| 45 | * |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 46 | * ContentObject serializes/deserializes header up-to and including <Content> tags |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 47 | * Necessary closing tags should be added using ContentObjectTail |
| 48 | * |
| 49 | * This hacks are necessary to optimize memory use (i.e., virtual payload) |
| 50 | * |
| 51 | * "<ContentObject><Signature>..</Signature><Name>...</Name><SignedInfo>...</SignedInfo><Content>" |
| 52 | * |
| 53 | */ |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 54 | class ContentObject : public SimpleRefCount<ContentObject,Header> |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 55 | { |
| 56 | public: |
| 57 | //////////////////////////////////////////////////////////////////////////// |
| 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 | |
| 71 | /** |
| 72 | * @brief Get digest algorithm |
| 73 | */ |
| 74 | inline const std::string & |
| 75 | GetDigestAlgorithm () const; |
| 76 | |
| 77 | /** |
| 78 | * @brief Set digest algorithm |
| 79 | */ |
| 80 | inline void |
| 81 | SetDigestAlgorithm (const std::string &digestAlgorithm); |
| 82 | |
| 83 | /** |
| 84 | * @brief Get signature bits |
| 85 | */ |
| 86 | inline uint32_t |
| 87 | GetSignatureBits () const; |
| 88 | |
| 89 | /** |
| 90 | * @brief Set signature bits |
| 91 | */ |
| 92 | inline void |
| 93 | SetSignatureBits (uint32_t signatureBits); |
| 94 | |
| 95 | /** |
| 96 | * @brief Default digest algorithm ("2.16.840.1.101.3.4.2.1") |
| 97 | */ |
| 98 | 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 | |
| 111 | /** |
| 112 | * @brief Options for the data packet Type |
| 113 | */ |
| 114 | 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 ×tamp); |
| 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 Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 198 | SetKeyLocator (Ptr<const Name> keyLocator); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * @brief Get key locator |
| 202 | * |
| 203 | * Note that only <KeyName> option for the key locator is supported |
| 204 | */ |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 205 | Ptr<const Name> |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 206 | 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 Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 214 | Ptr<const Name> m_keyLocator; // support only <KeyName> option for KeyLocator |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | //////////////////////////////////////////////////////////////////////////// |
| 218 | //////////////////////////////////////////////////////////////////////////// |
| 219 | //////////////////////////////////////////////////////////////////////////// |
| 220 | //////////////////////////////////////////////////////////////////////////// |
| 221 | //////////////////////////////////////////////////////////////////////////// |
| 222 | //////////////////////////////////////////////////////////////////////////// |
| 223 | |
| 224 | /** |
| 225 | * Constructor |
| 226 | * |
| 227 | * Creates a null header |
| 228 | **/ |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 229 | ContentObject (); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * \brief Set content object name |
| 233 | * |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 234 | * Sets name of the content object. For example, SetName( Name("prefix")("postfix") ); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 235 | **/ |
| 236 | void |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 237 | SetName (const Ptr<Name> &name); |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 238 | |
| 239 | /** |
| 240 | * @brief Get name of the content object |
| 241 | */ |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 242 | const Name& |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 243 | GetName () const; |
| 244 | |
| 245 | /** |
| 246 | * @brief Get smart pointer to the interest name (to avoid extra memory usage) |
| 247 | */ |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 248 | Ptr<const Name> |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 249 | GetNamePtr () const; |
| 250 | |
| 251 | /** |
| 252 | * @brief Get editable reference to content object's Signature |
| 253 | */ |
| 254 | inline Signature & |
| 255 | GetSignature (); |
| 256 | |
| 257 | /** |
| 258 | * @brief Get read-only reference to content object's Signature |
| 259 | */ |
| 260 | inline const Signature & |
| 261 | GetSignature () const; |
| 262 | |
| 263 | /** |
| 264 | * @brief Get editable reference to content object's SignedInfo |
| 265 | */ |
| 266 | inline SignedInfo & |
| 267 | GetSignedInfo (); |
| 268 | |
| 269 | /** |
| 270 | * @brief Get read-only reference to content object's SignedInfo |
| 271 | */ |
| 272 | inline const SignedInfo & |
| 273 | GetSignedInfo () const; |
| 274 | |
| 275 | ////////////////////////////////////////////////////////////////// |
| 276 | |
| 277 | 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 |
| 283 | |
| 284 | private: |
| 285 | Signature m_signature; |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 286 | Ptr<Name> m_name; |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 287 | SignedInfo m_signedInfo; |
| 288 | }; |
| 289 | |
| 290 | /** |
| 291 | * ContentObjectTail should always be 2 bytes, representing two closing tags: |
| 292 | * "</Content><ContentObject>" |
| 293 | */ |
| 294 | class ContentObjectTail : public Trailer |
| 295 | { |
| 296 | public: |
| 297 | ContentObjectTail (); |
| 298 | ////////////////////////////////////////////////////////////////// |
| 299 | |
| 300 | 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 |
| 306 | }; |
| 307 | |
| 308 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 309 | ContentObject::Signature::Signature () |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 310 | : m_digestAlgorithm ("99.0") |
| 311 | , m_signatureBits (0) |
| 312 | { |
| 313 | } |
| 314 | |
| 315 | const std::string & |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 316 | ContentObject::Signature::GetDigestAlgorithm () const |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 317 | { |
| 318 | return m_digestAlgorithm; |
| 319 | } |
| 320 | |
| 321 | void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 322 | ContentObject::Signature::SetDigestAlgorithm (const std::string &digestAlgorithm) |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 323 | { |
| 324 | m_digestAlgorithm = digestAlgorithm; |
| 325 | } |
| 326 | |
| 327 | uint32_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 328 | ContentObject::Signature::GetSignatureBits () const |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 329 | { |
| 330 | return m_signatureBits; |
| 331 | } |
| 332 | |
| 333 | inline void |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 334 | ContentObject::Signature::SetSignatureBits (uint32_t signature) |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 335 | { |
| 336 | m_signatureBits = signature; |
| 337 | } |
| 338 | |
| 339 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 340 | ContentObject::Signature & |
| 341 | ContentObject::GetSignature () |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 342 | { |
| 343 | return m_signature; |
| 344 | } |
| 345 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 346 | const ContentObject::Signature & |
| 347 | ContentObject::GetSignature () const |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 348 | { |
| 349 | return m_signature; |
| 350 | } |
| 351 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 352 | ContentObject::SignedInfo & |
| 353 | ContentObject::GetSignedInfo () |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 354 | { |
| 355 | return m_signedInfo; |
| 356 | } |
| 357 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 358 | const ContentObject::SignedInfo & |
| 359 | ContentObject::GetSignedInfo () const |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 360 | { |
| 361 | return m_signedInfo; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * @ingroup ndn-exceptions |
| 366 | * @brief Class for ContentObject parsing exception |
| 367 | */ |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 368 | class ContentObjectException {}; |
Alexander Afanasyev | 5d79e68 | 2012-11-19 14:12:23 -0800 | [diff] [blame] | 369 | |
| 370 | } // namespace ndn |
| 371 | } // namespace ns3 |
| 372 | |
| 373 | #endif // _NDN_CONTENT_OBJECT_HEADER_CCNB_H_ |