Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #ifndef NDN_WIRE_CCNB_H |
| 12 | #define NDN_WIRE_CCNB_H |
| 13 | |
| 14 | #include "base.h" |
| 15 | |
| 16 | #include "ndn-cpp/interest.h" |
| 17 | #include "ndn-cpp/data.h" |
| 18 | |
| 19 | namespace ndn { |
| 20 | namespace wire { |
| 21 | |
| 22 | /** |
| 23 | * @brief Class for working with ccnb encoding |
| 24 | */ |
| 25 | class Ccnb : public Base |
| 26 | { |
| 27 | public: |
| 28 | /** |
| 29 | * \brief Type tag for a ccnb start marker. |
| 30 | * |
| 31 | * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html |
| 32 | */ |
| 33 | enum ccn_tt { |
| 34 | CCN_EXT, /**< starts composite extension - numval is subtype */ |
| 35 | CCN_TAG, /**< starts composite - numval is tagnamelen-1 */ |
| 36 | CCN_DTAG, /**< starts composite - numval is tagdict index (enum ccn_dtag) */ |
| 37 | CCN_ATTR, /**< attribute - numval is attrnamelen-1, value follows */ |
| 38 | CCN_DATTR, /**< attribute numval is attrdict index */ |
| 39 | CCN_BLOB, /**< opaque binary data - numval is byte count */ |
| 40 | CCN_UDATA, /**< UTF-8 encoded character data - numval is byte count */ |
| 41 | CCN_NO_TOKEN /**< should not occur in encoding */ |
| 42 | }; |
| 43 | |
| 44 | /** \brief CCN_CLOSE_TAG terminates composites */ |
| 45 | enum {CCN_CLOSE_TAG = 0}; |
| 46 | |
| 47 | /** |
| 48 | * \brief DTAG identifies ccnb-encoded elements. |
| 49 | * |
| 50 | * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html |
| 51 | */ |
| 52 | enum ccn_dtag { |
| 53 | CCN_DTAG_Any = 13, |
| 54 | CCN_DTAG_Name = 14, |
| 55 | CCN_DTAG_Component = 15, |
| 56 | CCN_DTAG_Certificate = 16, |
| 57 | CCN_DTAG_Collection = 17, |
| 58 | CCN_DTAG_CompleteName = 18, |
| 59 | CCN_DTAG_Content = 19, |
| 60 | CCN_DTAG_SignedInfo = 20, |
| 61 | CCN_DTAG_ContentDigest = 21, |
| 62 | CCN_DTAG_ContentHash = 22, |
| 63 | CCN_DTAG_Count = 24, |
| 64 | CCN_DTAG_Header = 25, |
| 65 | CCN_DTAG_Interest = 26, /* 20090915 */ |
| 66 | CCN_DTAG_Key = 27, |
| 67 | CCN_DTAG_KeyLocator = 28, |
| 68 | CCN_DTAG_KeyName = 29, |
| 69 | CCN_DTAG_Length = 30, |
| 70 | CCN_DTAG_Link = 31, |
| 71 | CCN_DTAG_LinkAuthenticator = 32, |
| 72 | CCN_DTAG_NameComponentCount = 33, /* DeprecatedInInterest */ |
| 73 | CCN_DTAG_RootDigest = 36, |
| 74 | CCN_DTAG_Signature = 37, |
| 75 | CCN_DTAG_Start = 38, |
| 76 | CCN_DTAG_Timestamp = 39, |
| 77 | CCN_DTAG_Type = 40, |
| 78 | CCN_DTAG_Nonce = 41, |
| 79 | CCN_DTAG_Scope = 42, |
| 80 | CCN_DTAG_Exclude = 43, |
| 81 | CCN_DTAG_Bloom = 44, |
| 82 | CCN_DTAG_BloomSeed = 45, |
| 83 | CCN_DTAG_AnswerOriginKind = 47, |
| 84 | CCN_DTAG_InterestLifetime = 48, |
| 85 | CCN_DTAG_Witness = 53, |
| 86 | CCN_DTAG_SignatureBits = 54, |
| 87 | CCN_DTAG_DigestAlgorithm = 55, |
| 88 | CCN_DTAG_BlockSize = 56, |
| 89 | CCN_DTAG_FreshnessSeconds = 58, |
| 90 | CCN_DTAG_FinalBlockID = 59, |
| 91 | CCN_DTAG_PublisherPublicKeyDigest = 60, |
| 92 | CCN_DTAG_PublisherCertificateDigest = 61, |
| 93 | CCN_DTAG_PublisherIssuerKeyDigest = 62, |
| 94 | CCN_DTAG_PublisherIssuerCertificateDigest = 63, |
| 95 | CCN_DTAG_ContentObject = 64, /* 20090915 */ |
| 96 | CCN_DTAG_WrappedKey = 65, |
| 97 | CCN_DTAG_WrappingKeyIdentifier = 66, |
| 98 | CCN_DTAG_WrapAlgorithm = 67, |
| 99 | CCN_DTAG_KeyAlgorithm = 68, |
| 100 | CCN_DTAG_Label = 69, |
| 101 | CCN_DTAG_EncryptedKey = 70, |
| 102 | CCN_DTAG_EncryptedNonceKey = 71, |
| 103 | CCN_DTAG_WrappingKeyName = 72, |
| 104 | CCN_DTAG_Action = 73, |
| 105 | CCN_DTAG_FaceID = 74, |
| 106 | CCN_DTAG_IPProto = 75, |
| 107 | CCN_DTAG_Host = 76, |
| 108 | CCN_DTAG_Port = 77, |
| 109 | CCN_DTAG_MulticastInterface = 78, |
| 110 | CCN_DTAG_ForwardingFlags = 79, |
| 111 | CCN_DTAG_FaceInstance = 80, |
| 112 | CCN_DTAG_ForwardingEntry = 81, |
| 113 | CCN_DTAG_MulticastTTL = 82, |
| 114 | CCN_DTAG_MinSuffixComponents = 83, |
| 115 | CCN_DTAG_MaxSuffixComponents = 84, |
| 116 | CCN_DTAG_ChildSelector = 85, |
| 117 | CCN_DTAG_RepositoryInfo = 86, |
| 118 | CCN_DTAG_Version = 87, |
| 119 | CCN_DTAG_RepositoryVersion = 88, |
| 120 | CCN_DTAG_GlobalPrefix = 89, |
| 121 | CCN_DTAG_LocalName = 90, |
| 122 | CCN_DTAG_Policy = 91, |
| 123 | CCN_DTAG_Namespace = 92, |
| 124 | CCN_DTAG_GlobalPrefixName = 93, |
| 125 | CCN_DTAG_PolicyVersion = 94, |
| 126 | CCN_DTAG_KeyValueSet = 95, |
| 127 | CCN_DTAG_KeyValuePair = 96, |
| 128 | CCN_DTAG_IntegerValue = 97, |
| 129 | CCN_DTAG_DecimalValue = 98, |
| 130 | CCN_DTAG_StringValue = 99, |
| 131 | CCN_DTAG_BinaryValue = 100, |
| 132 | CCN_DTAG_NameValue = 101, |
| 133 | CCN_DTAG_Entry = 102, |
| 134 | CCN_DTAG_ACL = 103, |
| 135 | CCN_DTAG_ParameterizedName = 104, |
| 136 | CCN_DTAG_Prefix = 105, |
| 137 | CCN_DTAG_Suffix = 106, |
| 138 | CCN_DTAG_Root = 107, |
| 139 | CCN_DTAG_ProfileName = 108, |
| 140 | CCN_DTAG_Parameters = 109, |
| 141 | CCN_DTAG_InfoString = 110, |
| 142 | CCN_DTAG_StatusResponse = 112, |
| 143 | CCN_DTAG_StatusCode = 113, |
| 144 | CCN_DTAG_StatusText = 114, |
Jeff Thompson | 79ba55c | 2013-06-23 19:04:28 -0700 | [diff] [blame] | 145 | CCN_DTAG_SyncNode = 115, |
| 146 | CCN_DTAG_SyncNodeKind = 116, |
| 147 | CCN_DTAG_SyncNodeElement = 117, |
| 148 | CCN_DTAG_SyncVersion = 118, |
| 149 | CCN_DTAG_SyncNodeElements = 119, |
| 150 | CCN_DTAG_SyncContentHash = 120, |
| 151 | CCN_DTAG_SyncLeafCount = 121, |
| 152 | CCN_DTAG_SyncTreeDepth = 122, |
| 153 | CCN_DTAG_SyncByteCount = 123, |
| 154 | CCN_DTAG_SyncConfigSlice = 124, |
| 155 | CCN_DTAG_SyncConfigSliceList = 125, |
| 156 | CCN_DTAG_SyncConfigSliceOp = 126, |
| 157 | CCN_DTAG_SyncNodeDeltas = 127, |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 158 | CCN_DTAG_SequenceNumber = 256, |
| 159 | CCN_DTAG_CCNProtocolDataUnit = 17702112 |
| 160 | }; |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * @brief Append CCNB block header |
| 165 | * @param os output stream to write |
| 166 | * @param value dictionary id of the block header |
| 167 | * @param block_type Type of CCNB block |
| 168 | */ |
| 169 | static void |
| 170 | appendBlockHeader (std::ostream &os, size_t value, ccn_tt block_type); |
| 171 | |
| 172 | /** |
| 173 | * @brief Add number in CCNB encoding |
| 174 | * @param os output stream to write |
| 175 | * @param number Number to be written |
| 176 | * |
| 177 | * @returns written length |
| 178 | */ |
| 179 | static void |
| 180 | appendNumber (std::ostream &os, uint32_t number); |
| 181 | |
| 182 | /** |
| 183 | * @brief Append CCNB closer tag (size is 1) |
| 184 | * @param os output stream to write |
| 185 | */ |
| 186 | inline static void |
| 187 | appendCloser (std::ostream &os); |
| 188 | |
| 189 | /** |
| 190 | * @brief Append Name in CCNB encoding |
| 191 | * @param os output stream to write |
| 192 | * @param name constant reference to Name object |
| 193 | * |
| 194 | * @returns written length |
| 195 | */ |
| 196 | static void |
| 197 | appendName (std::ostream &os, const Name &name); |
| 198 | |
| 199 | /** |
| 200 | * Append a binary timestamp as a BLOB using the ccn binary |
| 201 | * Timestamp representation (12-bit fraction). |
| 202 | * |
| 203 | * @param os output stream to write |
| 204 | * @param time reference to time duration object |
| 205 | */ |
| 206 | static void |
| 207 | appendTimestampBlob (std::ostream &os, const TimeInterval ×tamp); |
| 208 | |
| 209 | /** |
| 210 | * Append a binary timestamp as a BLOB using the ccn binary |
| 211 | * Timestamp representation (12-bit fraction). |
| 212 | * |
| 213 | * @param os output stream to write |
| 214 | * @param time reference to Time (posix_time::ptime) object. |
| 215 | * This method automatically calculates duration between time and gregorian::date(1970,1,1) |
| 216 | * and calls the other version of the method |
| 217 | */ |
| 218 | inline static void |
| 219 | appendTimestampBlob (std::ostream &os, const Time &time); |
| 220 | |
| 221 | /** |
| 222 | * Append a tagged BLOB |
| 223 | * |
| 224 | * This is a ccnb-encoded element with containing the BLOB as content |
| 225 | * |
| 226 | * @param os output stream to write |
| 227 | * @param dtag is the element's dtag |
| 228 | * @param data points to the binary data |
| 229 | * @param size is the size of the data, in bytes |
| 230 | */ |
| 231 | inline static void |
| 232 | appendTaggedBlob (std::ostream &os, ccn_dtag dtag, const void *data, size_t size); |
| 233 | |
| 234 | /** |
| 235 | * Append a tagged BLOB |
| 236 | * |
| 237 | * This is a ccnb-encoded element with containing the BLOB as content |
| 238 | * |
| 239 | * @param os output stream to write |
| 240 | * @param dtag is the element's dtag |
| 241 | * @param blob reference to the data blob |
| 242 | */ |
| 243 | inline static void |
| 244 | appendTaggedBlob (std::ostream &os, ccn_dtag dtag, const Blob &blob); |
| 245 | |
| 246 | /** |
| 247 | * Append a tagged BLOB |
| 248 | * |
| 249 | * This is a ccnb-encoded element with containing the BLOB as content |
| 250 | * |
| 251 | * @param os output stream to write |
| 252 | * @param dtag is the element's dtag |
| 253 | * @param data points to the binary data |
| 254 | * @param size is the size of the data, in bytes |
| 255 | */ |
| 256 | inline static void |
| 257 | appendTaggedNumber (std::ostream &os, ccn_dtag dtag, uint32_t number); |
| 258 | |
| 259 | /** |
| 260 | * Append a tagged string (should be a valid UTF-8 coded string) |
| 261 | * |
| 262 | * This is a ccnb-encoded element with containing UDATA as content |
| 263 | * |
| 264 | * @param os output stream to write |
| 265 | * @param dtag is the element's dtag |
| 266 | * @param string UTF-8 string to be written |
| 267 | */ |
| 268 | inline static void |
| 269 | appendString (std::ostream &os, ccn_dtag dtag, const std::string &string); |
| 270 | |
| 271 | /** |
| 272 | * @brief Format interest in CCNb encoding |
| 273 | * @param os output stream to write |
| 274 | * @param interest Interest to be formatted |
| 275 | * |
| 276 | * @todo For now, this method is used to create Interest template, which doesn't output name to the stream |
| 277 | */ |
| 278 | static void |
| 279 | appendInterest (std::ostream &os, const Interest &interest); |
| 280 | |
| 281 | /** |
| 282 | * @brief Append exclude filter in CCNb encoding |
| 283 | * @param os output stream to write |
| 284 | * @param exclude Exclude filter to be formatted |
| 285 | */ |
| 286 | static void |
| 287 | appendExclude (std::ostream &os, const Exclude &exclude); |
| 288 | |
| 289 | /** |
| 290 | * @brief Append signature in SHA256withRSA format |
| 291 | */ |
| 292 | virtual void |
| 293 | appendSignature (std::ostream &os, const signature::Sha256WithRsa &signature, void *userData); |
| 294 | |
| 295 | /** |
| 296 | * @brief Format data in CCNb encoding |
| 297 | * @param os output stream to write |
| 298 | * @param data data to be formatted |
| 299 | */ |
| 300 | void |
| 301 | appendData (std::ostream &os, const Data &data); |
| 302 | }; |
| 303 | |
| 304 | |
| 305 | inline void |
| 306 | Ccnb::appendCloser (std::ostream &os) |
| 307 | { |
| 308 | os.put (Ccnb::CCN_CLOSE_TAG); |
| 309 | } |
| 310 | |
| 311 | inline void |
| 312 | Ccnb::appendTimestampBlob (std::ostream &os, const Time &time) |
| 313 | { |
| 314 | appendTimestampBlob (os, time - time::UNIX_EPOCH_TIME); |
| 315 | } |
| 316 | |
| 317 | inline void |
| 318 | Ccnb::appendTaggedBlob (std::ostream &os, Ccnb::ccn_dtag dtag, const void *data, size_t size) |
| 319 | { |
| 320 | appendBlockHeader (os, dtag, Ccnb::CCN_DTAG); |
| 321 | /* 2 */ |
| 322 | if (size>0) |
| 323 | { |
| 324 | appendBlockHeader (os, size, Ccnb::CCN_BLOB); |
| 325 | os.write (reinterpret_cast<const char*> (data), size); |
| 326 | /* size */ |
| 327 | } |
| 328 | appendCloser (os); |
| 329 | /* 1 */ |
| 330 | } |
| 331 | |
| 332 | inline void |
| 333 | Ccnb::appendTaggedBlob (std::ostream &os, ccn_dtag dtag, const Blob &blob) |
| 334 | { |
| 335 | appendTaggedBlob (os, dtag, blob.buf (), blob.size ()); |
| 336 | } |
| 337 | |
| 338 | inline void |
| 339 | Ccnb::appendTaggedNumber (std::ostream &os, Ccnb::ccn_dtag dtag, uint32_t number) |
| 340 | { |
| 341 | appendBlockHeader (os, dtag, Ccnb::CCN_DTAG); |
| 342 | { |
| 343 | appendNumber (os, number); |
| 344 | } |
| 345 | appendCloser (os); |
| 346 | } |
| 347 | |
| 348 | inline void |
| 349 | Ccnb::appendString (std::ostream &os, Ccnb::ccn_dtag dtag, const std::string &string) |
| 350 | { |
| 351 | appendBlockHeader (os, dtag, Ccnb::CCN_DTAG); |
| 352 | { |
| 353 | appendBlockHeader (os, string.size (), Ccnb::CCN_UDATA); |
| 354 | os.write (string.c_str (), string.size ()); |
| 355 | } |
| 356 | appendCloser (os); |
| 357 | } |
| 358 | |
| 359 | } // wire |
| 360 | } // ndn |
| 361 | |
| 362 | #endif // NDN_WIRE_CCNB_H |