blob: 20ea101fbd3ed81088a538968775c67ff776cd57 [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -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:
19 */
20
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070021#include "ccnx-encoding-helper.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070022
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070023#include "ns3/ccnx-name-components.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070024#include "ns3/ccnx-interest-header.h"
25#include "ns3/ccnx-content-object-header.h"
26
27#include <sstream>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -070028#include <boost/foreach.hpp>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070029
30namespace ns3 {
31
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070032size_t
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070033CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070034{
35 size_t written = 0;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070036 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Interest, CcnbParser::CCN_DTAG); // <Interest>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070037
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070038 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -070039 written += AppendNameComponents (start, interest.GetName()); // <Component>...</Component>...
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070040 written += AppendCloser (start); // </Name>
41
42 if (interest.GetMinSuffixComponents() >= 0)
43 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070044 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MinSuffixComponents, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070045 written += AppendNumber (start, interest.GetMinSuffixComponents ());
46 written += AppendCloser (start);
47 }
48 if (interest.GetMaxSuffixComponents() >= 0)
49 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070050 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MaxSuffixComponents, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070051 written += AppendNumber (start, interest.GetMaxSuffixComponents ());
52 written += AppendCloser (start);
53 }
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070054 if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070055 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070056 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Exclude, CcnbParser::CCN_DTAG); // <Exclude>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -070057 written += AppendNameComponents (start, interest.GetExclude()); // <Component>...</Component>...
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070058 written += AppendCloser (start); // </Exclude>
59 }
60 if (interest.IsEnabledChildSelector())
61 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070062 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ChildSelector, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070063 written += AppendNumber (start, 1);
64 written += AppendCloser (start);
65 }
66 if (interest.IsEnabledAnswerOriginKind())
67 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070068 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_AnswerOriginKind, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070069 written += AppendNumber (start, 1);
70 written += AppendCloser (start);
71 }
72 if (interest.GetScope() >= 0)
73 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070074 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Scope, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070075 written += AppendNumber (start, interest.GetScope ());
76 written += AppendCloser (start);
77 }
78 if (!interest.GetInterestLifetime().IsZero())
79 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070080 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_InterestLifetime, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070081 written += AppendTimestampBlob (start, interest.GetInterestLifetime());
82 written += AppendCloser (start);
83 }
84 if (interest.GetNonce()>0)
85 {
86 uint32_t nonce = interest.GetNonce();
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070087 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Nonce,
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070088 reinterpret_cast<const uint8_t*>(&nonce),
89 sizeof(nonce));
90 }
Ilya Moiseenkod1f19512011-11-16 14:31:19 -080091
92 if (interest.IsNack ())
93 {
94 written += AppendBlockHeader (start, CcnbParser::NDN_DTAG_Nack, CcnbParser::CCN_DTAG);
95 written += AppendNumber (start, 1);
96 written += AppendCloser (start);
97 }
98 if (interest.IsCongested ())
99 {
100 written += AppendBlockHeader (start, CcnbParser::NDN_DTAG_Congested, CcnbParser::CCN_DTAG);
101 written += AppendNumber (start, 1);
102 written += AppendCloser (start);
103 }
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700104 written += AppendCloser (start); // </Interest>
105
106 return written;
107}
108
109size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700110CcnxEncodingHelper::GetSerializedSize (const CcnxInterestHeader &interest)
111{
112 size_t written = 0;
113 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Interest); // <Interest>
114
115 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
116 written += EstimateNameComponents (interest.GetName()); // <Component>...</Component>...
117 written += 1; // </Name>
118
119 if (interest.GetMinSuffixComponents() >= 0)
120 {
121 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MinSuffixComponents);
122 written += EstimateNumber (interest.GetMinSuffixComponents ());
123 written += 1;
124 }
125 if (interest.GetMaxSuffixComponents() >= 0)
126 {
127 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MaxSuffixComponents);
128 written += EstimateNumber (interest.GetMaxSuffixComponents ());
129 written += 1;
130 }
131 if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0)
132 {
133 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Exclude);
134 written += EstimateNameComponents (interest.GetExclude()); // <Component>...</Component>...
135 written += 1; // </Exclude>
136 }
137 if (interest.IsEnabledChildSelector())
138 {
139 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ChildSelector);
140 written += EstimateNumber (1);
141 written += 1;
142 }
143 if (interest.IsEnabledAnswerOriginKind())
144 {
145 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_AnswerOriginKind);
146 written += EstimateNumber (1);
147 written += 1;
148 }
149 if (interest.GetScope() >= 0)
150 {
151 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Scope);
152 written += EstimateNumber (interest.GetScope ());
153 written += 1;
154 }
155 if (!interest.GetInterestLifetime().IsZero())
156 {
157 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_InterestLifetime);
158 written += EstimateTimestampBlob (interest.GetInterestLifetime());
159 written += 1;
160 }
161 if (interest.GetNonce()>0)
162 {
163 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Nonce, sizeof(uint32_t));
164 }
Ilya Moiseenkoaa17c8e2011-10-28 13:04:30 -0700165 if (interest.IsNack () )
166 {
167 written += EstimateBlockHeader (CcnbParser::NDN_DTAG_Nack);
168 written += EstimateNumber (1);
169 written += 1;
170 }
171 if (interest.IsCongested () )
172 {
173 written += EstimateBlockHeader (CcnbParser::NDN_DTAG_Congested);
174 written += EstimateNumber (1);
175 written += 1;
176 }
177
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700178 written += 1; // </Interest>
179
180 return written;
181}
182
183size_t
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700184CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700185{
186 size_t written = 0;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700187 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ContentObject, CcnbParser::CCN_DTAG); // <ContentObject>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700188
189 // fake signature
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700190 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Signature, CcnbParser::CCN_DTAG); // <Signature>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700191 // Signature ::= DigestAlgorithm?
192 // Witness?
193 // SignatureBits
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700194 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_SignatureBits, 0, 0); // <SignatureBits />
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700195 written += AppendCloser (start); // </Signature>
196
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700197 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -0700198 written += AppendNameComponents (start, contentObject.GetName()); // <Component>...</Component>...
199 written += AppendCloser (start); // </Name>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700200
201 // fake signature
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700202 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700203 // SignedInfo ::= PublisherPublicKeyDigest
204 // Timestamp
205 // Type?
206 // FreshnessSeconds?
207 // FinalBlockID?
208 // KeyLocator?
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700209 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest />
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700210 written += AppendCloser (start); // </SignedInfo>
211
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700212 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700213
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700214 // there are no closing tags !!!
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700215 return written;
216}
217
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700218size_t
219CcnxEncodingHelper::GetSerializedSize (const CcnxContentObjectHeader &contentObject)
220{
221 size_t written = 0;
222 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ContentObject); // <ContentObject>
223
224 // fake signature
225 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Signature); // <Signature>
226 // Signature ::= DigestAlgorithm?
227 // Witness?
228 // SignatureBits
229 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_SignatureBits, 0); // <SignatureBits />
230 written += 1; // </Signature>
231
232 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
233 written += EstimateNameComponents (contentObject.GetName()); // <Component>...</Component>...
234 written += 1; // </Name>
235
236 // fake signature
237 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_SignedInfo); // <SignedInfo>
238 // SignedInfo ::= PublisherPublicKeyDigest
239 // Timestamp
240 // Type?
241 // FreshnessSeconds?
242 // FinalBlockID?
243 // KeyLocator?
244 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0); // <PublisherPublicKeyDigest />
245 written += 1; // </SignedInfo>
246
247 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Content); // <Content>
248
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700249 // there are no closing tags !!!
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700250 return written;
251}
252
253//////////////////////////////////////////////////////////////////////
254//////////////////////////////////////////////////////////////////////
255//////////////////////////////////////////////////////////////////////
256
257#define CCN_TT_BITS 3
258#define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1)
259#define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1)
260#define CCN_TT_HBIT ((unsigned char)(1 << 7))
261
262size_t
263CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator &start, size_t val, CcnbParser::ccn_tt tt)
264{
265 unsigned char buf[1+8*((sizeof(val)+6)/7)];
266 unsigned char *p = &(buf[sizeof(buf)-1]);
267 size_t n = 1;
268 p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) |
269 ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
270 (CCN_TT_MASK & tt);
271 val >>= (7-CCN_TT_BITS);
272 while (val != 0) {
273 (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE;
274 n++;
275 val >>= 7;
276 }
277 start.Write (p,n);
278 return n;
279}
280
281size_t
282CcnxEncodingHelper::EstimateBlockHeader (size_t value)
283{
284 value >>= (7-CCN_TT_BITS);
285 size_t n = 1;
286 while (value>0)
287 {
288 value >>= 7;
289 n++;
290 }
291 return n;
292}
293
294size_t
295CcnxEncodingHelper::AppendNumber (Buffer::Iterator &start, uint32_t number)
296{
297 std::ostringstream os;
298 os << number;
299
300 size_t written = 0;
301 written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA);
302 written += os.str().size();
303 start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size());
304
305 return written;
306}
307
308size_t
309CcnxEncodingHelper::EstimateNumber (uint32_t number)
310{
311 std::ostringstream os;
312 os << number;
313 return EstimateBlockHeader (os.str ().size ()) + os.str ().size ();
314}
315
316size_t
317CcnxEncodingHelper::AppendCloser (Buffer::Iterator &start)
318{
319 start.WriteU8 (CcnbParser::CCN_CLOSE);
320 return 1;
321}
322
323size_t
324CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name)
325{
326 size_t written = 0;
327 BOOST_FOREACH (const std::string &component, name.GetComponents())
328 {
329 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
330 reinterpret_cast<const uint8_t*>(component.c_str()), component.size());
331 }
332 return written;
333}
334
335size_t
336CcnxEncodingHelper::EstimateNameComponents (const CcnxNameComponents &name)
337{
338 size_t written = 0;
339 BOOST_FOREACH (const std::string &component, name.GetComponents())
340 {
341 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size());
342 }
343 return written;
344}
345
346size_t
347CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator &start, const Time &time)
348{
349 // the original function implements Markers... thought not sure what are these markers for...
350
351 // Determine miminal number of bytes required to store the timestamp
352 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
353 intmax_t ts = time.ToInteger (Time::S) >> 4;
354 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
355 required_bytes++;
356
357 size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB);
358
359 // write part with seconds
360 ts = time.ToInteger (Time::S) >> 4;
361 for (int i = 0; i < required_bytes - 2; i++)
362 start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) );
363
364 /* arithmetic contortions are to avoid overflowing 31 bits */
365 ts = ((time.ToInteger (Time::S) & 15) << 12) +
366 (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625);
367 for (int i = required_bytes - 2; i < required_bytes; i++)
368 start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) );
369
370 return len + required_bytes;
371}
372
373size_t
374CcnxEncodingHelper::EstimateTimestampBlob (const Time &time)
375{
376 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
377 intmax_t ts = time.ToInteger (Time::S) >> 4;
378 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
379 required_bytes++;
380
381 return EstimateBlockHeader (required_bytes) + required_bytes;
382}
383
384size_t
385CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
386 const uint8_t *data, size_t size)
387{
388 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
389 /* 2 */
390 if (size>0)
391 {
392 written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB);
393 start.Write (data, size);
394 written += size;
395 /* size */
396 }
397 written += AppendCloser (start);
398 /* 1 */
399
400 return written;
401}
402
403size_t
404CcnxEncodingHelper::EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size)
405{
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700406 if (size>0)
407 return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1;
408 else
409 return EstimateBlockHeader (dtag) + 1;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700410}
411
412
413
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700414} // namespace ns3