blob: fc99bad3fce971642c04724f8fc5b9a2519612be [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- 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
19namespace ndn {
20namespace wire {
21
22/**
23 * @brief Class for working with ccnb encoding
24 */
25class Ccnb : public Base
26{
27public:
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,
145 CCN_DTAG_Nack = 200,
146 CCN_DTAG_SequenceNumber = 256,
147 CCN_DTAG_CCNProtocolDataUnit = 17702112
148 };
149
150
151 /**
152 * @brief Append CCNB block header
153 * @param os output stream to write
154 * @param value dictionary id of the block header
155 * @param block_type Type of CCNB block
156 */
157 static void
158 appendBlockHeader (std::ostream &os, size_t value, ccn_tt block_type);
159
160 /**
161 * @brief Add number in CCNB encoding
162 * @param os output stream to write
163 * @param number Number to be written
164 *
165 * @returns written length
166 */
167 static void
168 appendNumber (std::ostream &os, uint32_t number);
169
170 /**
171 * @brief Append CCNB closer tag (size is 1)
172 * @param os output stream to write
173 */
174 inline static void
175 appendCloser (std::ostream &os);
176
177 /**
178 * @brief Append Name in CCNB encoding
179 * @param os output stream to write
180 * @param name constant reference to Name object
181 *
182 * @returns written length
183 */
184 static void
185 appendName (std::ostream &os, const Name &name);
186
187 /**
188 * Append a binary timestamp as a BLOB using the ccn binary
189 * Timestamp representation (12-bit fraction).
190 *
191 * @param os output stream to write
192 * @param time reference to time duration object
193 */
194 static void
195 appendTimestampBlob (std::ostream &os, const TimeInterval &timestamp);
196
197 /**
198 * Append a binary timestamp as a BLOB using the ccn binary
199 * Timestamp representation (12-bit fraction).
200 *
201 * @param os output stream to write
202 * @param time reference to Time (posix_time::ptime) object.
203 * This method automatically calculates duration between time and gregorian::date(1970,1,1)
204 * and calls the other version of the method
205 */
206 inline static void
207 appendTimestampBlob (std::ostream &os, const Time &time);
208
209 /**
210 * Append a tagged BLOB
211 *
212 * This is a ccnb-encoded element with containing the BLOB as content
213 *
214 * @param os output stream to write
215 * @param dtag is the element's dtag
216 * @param data points to the binary data
217 * @param size is the size of the data, in bytes
218 */
219 inline static void
220 appendTaggedBlob (std::ostream &os, ccn_dtag dtag, const void *data, size_t size);
221
222 /**
223 * Append a tagged BLOB
224 *
225 * This is a ccnb-encoded element with containing the BLOB as content
226 *
227 * @param os output stream to write
228 * @param dtag is the element's dtag
229 * @param blob reference to the data blob
230 */
231 inline static void
232 appendTaggedBlob (std::ostream &os, ccn_dtag dtag, const Blob &blob);
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 data points to the binary data
242 * @param size is the size of the data, in bytes
243 */
244 inline static void
245 appendTaggedNumber (std::ostream &os, ccn_dtag dtag, uint32_t number);
246
247 /**
248 * Append a tagged string (should be a valid UTF-8 coded string)
249 *
250 * This is a ccnb-encoded element with containing UDATA as content
251 *
252 * @param os output stream to write
253 * @param dtag is the element's dtag
254 * @param string UTF-8 string to be written
255 */
256 inline static void
257 appendString (std::ostream &os, ccn_dtag dtag, const std::string &string);
258
259 /**
260 * @brief Format interest in CCNb encoding
261 * @param os output stream to write
262 * @param interest Interest to be formatted
263 *
264 * @todo For now, this method is used to create Interest template, which doesn't output name to the stream
265 */
266 static void
267 appendInterest (std::ostream &os, const Interest &interest);
268
269 /**
270 * @brief Append exclude filter in CCNb encoding
271 * @param os output stream to write
272 * @param exclude Exclude filter to be formatted
273 */
274 static void
275 appendExclude (std::ostream &os, const Exclude &exclude);
276
277 /**
278 * @brief Append signature in SHA256withRSA format
279 */
280 virtual void
281 appendSignature (std::ostream &os, const signature::Sha256WithRsa &signature, void *userData);
282
283 /**
284 * @brief Format data in CCNb encoding
285 * @param os output stream to write
286 * @param data data to be formatted
287 */
288 void
289 appendData (std::ostream &os, const Data &data);
290};
291
292
293inline void
294Ccnb::appendCloser (std::ostream &os)
295{
296 os.put (Ccnb::CCN_CLOSE_TAG);
297}
298
299inline void
300Ccnb::appendTimestampBlob (std::ostream &os, const Time &time)
301{
302 appendTimestampBlob (os, time - time::UNIX_EPOCH_TIME);
303}
304
305inline void
306Ccnb::appendTaggedBlob (std::ostream &os, Ccnb::ccn_dtag dtag, const void *data, size_t size)
307{
308 appendBlockHeader (os, dtag, Ccnb::CCN_DTAG);
309 /* 2 */
310 if (size>0)
311 {
312 appendBlockHeader (os, size, Ccnb::CCN_BLOB);
313 os.write (reinterpret_cast<const char*> (data), size);
314 /* size */
315 }
316 appendCloser (os);
317 /* 1 */
318}
319
320inline void
321Ccnb::appendTaggedBlob (std::ostream &os, ccn_dtag dtag, const Blob &blob)
322{
323 appendTaggedBlob (os, dtag, blob.buf (), blob.size ());
324}
325
326inline void
327Ccnb::appendTaggedNumber (std::ostream &os, Ccnb::ccn_dtag dtag, uint32_t number)
328{
329 appendBlockHeader (os, dtag, Ccnb::CCN_DTAG);
330 {
331 appendNumber (os, number);
332 }
333 appendCloser (os);
334}
335
336inline void
337Ccnb::appendString (std::ostream &os, Ccnb::ccn_dtag dtag, const std::string &string)
338{
339 appendBlockHeader (os, dtag, Ccnb::CCN_DTAG);
340 {
341 appendBlockHeader (os, string.size (), Ccnb::CCN_UDATA);
342 os.write (string.c_str (), string.size ());
343 }
344 appendCloser (os);
345}
346
347} // wire
348} // ndn
349
350#endif // NDN_WIRE_CCNB_H