blob: f486c01a75309954710db97f8ead09d10c2cd85a [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 Afanasyeva46844b2011-11-21 19:13:26 -080081 written += AppendTimestampBlob (start, interest.GetInterestLifetime ());
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070082 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
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080092 if (interest.GetNack ()>0)
Ilya Moiseenkod1f19512011-11-16 14:31:19 -080093 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080094 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Nack, CcnbParser::CCN_DTAG);
95 written += AppendNumber (start, interest.GetNack ());
Ilya Moiseenkod1f19512011-11-16 14:31:19 -080096 written += AppendCloser (start);
97 }
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070098 written += AppendCloser (start); // </Interest>
99
100 return written;
101}
102
103size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700104CcnxEncodingHelper::GetSerializedSize (const CcnxInterestHeader &interest)
105{
106 size_t written = 0;
107 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Interest); // <Interest>
108
109 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
110 written += EstimateNameComponents (interest.GetName()); // <Component>...</Component>...
111 written += 1; // </Name>
112
113 if (interest.GetMinSuffixComponents() >= 0)
114 {
115 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MinSuffixComponents);
116 written += EstimateNumber (interest.GetMinSuffixComponents ());
117 written += 1;
118 }
119 if (interest.GetMaxSuffixComponents() >= 0)
120 {
121 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MaxSuffixComponents);
122 written += EstimateNumber (interest.GetMaxSuffixComponents ());
123 written += 1;
124 }
125 if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0)
126 {
127 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Exclude);
128 written += EstimateNameComponents (interest.GetExclude()); // <Component>...</Component>...
129 written += 1; // </Exclude>
130 }
131 if (interest.IsEnabledChildSelector())
132 {
133 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ChildSelector);
134 written += EstimateNumber (1);
135 written += 1;
136 }
137 if (interest.IsEnabledAnswerOriginKind())
138 {
139 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_AnswerOriginKind);
140 written += EstimateNumber (1);
141 written += 1;
142 }
143 if (interest.GetScope() >= 0)
144 {
145 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Scope);
146 written += EstimateNumber (interest.GetScope ());
147 written += 1;
148 }
149 if (!interest.GetInterestLifetime().IsZero())
150 {
151 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_InterestLifetime);
152 written += EstimateTimestampBlob (interest.GetInterestLifetime());
153 written += 1;
154 }
155 if (interest.GetNonce()>0)
156 {
157 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Nonce, sizeof(uint32_t));
158 }
Ilya Moiseenkoaa17c8e2011-10-28 13:04:30 -0700159 if (interest.IsNack () )
160 {
161 written += EstimateBlockHeader (CcnbParser::NDN_DTAG_Nack);
162 written += EstimateNumber (1);
163 written += 1;
164 }
165 if (interest.IsCongested () )
166 {
167 written += EstimateBlockHeader (CcnbParser::NDN_DTAG_Congested);
168 written += EstimateNumber (1);
169 written += 1;
170 }
171
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700172 written += 1; // </Interest>
173
174 return written;
175}
176
177size_t
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700178CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700179{
180 size_t written = 0;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700181 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ContentObject, CcnbParser::CCN_DTAG); // <ContentObject>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700182
183 // fake signature
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700184 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Signature, CcnbParser::CCN_DTAG); // <Signature>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700185 // Signature ::= DigestAlgorithm?
186 // Witness?
187 // SignatureBits
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700188 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_SignatureBits, 0, 0); // <SignatureBits />
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700189 written += AppendCloser (start); // </Signature>
190
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700191 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -0700192 written += AppendNameComponents (start, contentObject.GetName()); // <Component>...</Component>...
193 written += AppendCloser (start); // </Name>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700194
195 // fake signature
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700196 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700197 // SignedInfo ::= PublisherPublicKeyDigest
198 // Timestamp
199 // Type?
200 // FreshnessSeconds?
201 // FinalBlockID?
202 // KeyLocator?
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700203 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest />
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700204 written += AppendCloser (start); // </SignedInfo>
205
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700206 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700207
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700208 // there are no closing tags !!!
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700209 return written;
210}
211
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700212size_t
213CcnxEncodingHelper::GetSerializedSize (const CcnxContentObjectHeader &contentObject)
214{
215 size_t written = 0;
216 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ContentObject); // <ContentObject>
217
218 // fake signature
219 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Signature); // <Signature>
220 // Signature ::= DigestAlgorithm?
221 // Witness?
222 // SignatureBits
223 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_SignatureBits, 0); // <SignatureBits />
224 written += 1; // </Signature>
225
226 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
227 written += EstimateNameComponents (contentObject.GetName()); // <Component>...</Component>...
228 written += 1; // </Name>
229
230 // fake signature
231 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_SignedInfo); // <SignedInfo>
232 // SignedInfo ::= PublisherPublicKeyDigest
233 // Timestamp
234 // Type?
235 // FreshnessSeconds?
236 // FinalBlockID?
237 // KeyLocator?
238 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0); // <PublisherPublicKeyDigest />
239 written += 1; // </SignedInfo>
240
241 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Content); // <Content>
242
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700243 // there are no closing tags !!!
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700244 return written;
245}
246
247//////////////////////////////////////////////////////////////////////
248//////////////////////////////////////////////////////////////////////
249//////////////////////////////////////////////////////////////////////
250
251#define CCN_TT_BITS 3
252#define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1)
253#define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1)
254#define CCN_TT_HBIT ((unsigned char)(1 << 7))
255
256size_t
257CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator &start, size_t val, CcnbParser::ccn_tt tt)
258{
259 unsigned char buf[1+8*((sizeof(val)+6)/7)];
260 unsigned char *p = &(buf[sizeof(buf)-1]);
261 size_t n = 1;
262 p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) |
263 ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
264 (CCN_TT_MASK & tt);
265 val >>= (7-CCN_TT_BITS);
266 while (val != 0) {
267 (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE;
268 n++;
269 val >>= 7;
270 }
271 start.Write (p,n);
272 return n;
273}
274
275size_t
276CcnxEncodingHelper::EstimateBlockHeader (size_t value)
277{
278 value >>= (7-CCN_TT_BITS);
279 size_t n = 1;
280 while (value>0)
281 {
282 value >>= 7;
283 n++;
284 }
285 return n;
286}
287
288size_t
289CcnxEncodingHelper::AppendNumber (Buffer::Iterator &start, uint32_t number)
290{
291 std::ostringstream os;
292 os << number;
293
294 size_t written = 0;
295 written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA);
296 written += os.str().size();
297 start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size());
298
299 return written;
300}
301
302size_t
303CcnxEncodingHelper::EstimateNumber (uint32_t number)
304{
305 std::ostringstream os;
306 os << number;
307 return EstimateBlockHeader (os.str ().size ()) + os.str ().size ();
308}
309
310size_t
311CcnxEncodingHelper::AppendCloser (Buffer::Iterator &start)
312{
313 start.WriteU8 (CcnbParser::CCN_CLOSE);
314 return 1;
315}
316
317size_t
318CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name)
319{
320 size_t written = 0;
321 BOOST_FOREACH (const std::string &component, name.GetComponents())
322 {
323 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
324 reinterpret_cast<const uint8_t*>(component.c_str()), component.size());
325 }
326 return written;
327}
328
329size_t
330CcnxEncodingHelper::EstimateNameComponents (const CcnxNameComponents &name)
331{
332 size_t written = 0;
333 BOOST_FOREACH (const std::string &component, name.GetComponents())
334 {
335 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size());
336 }
337 return written;
338}
339
340size_t
341CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator &start, const Time &time)
342{
343 // the original function implements Markers... thought not sure what are these markers for...
344
345 // Determine miminal number of bytes required to store the timestamp
346 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
347 intmax_t ts = time.ToInteger (Time::S) >> 4;
348 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
349 required_bytes++;
350
351 size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB);
352
353 // write part with seconds
354 ts = time.ToInteger (Time::S) >> 4;
355 for (int i = 0; i < required_bytes - 2; i++)
356 start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) );
357
358 /* arithmetic contortions are to avoid overflowing 31 bits */
359 ts = ((time.ToInteger (Time::S) & 15) << 12) +
360 (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625);
361 for (int i = required_bytes - 2; i < required_bytes; i++)
362 start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) );
363
364 return len + required_bytes;
365}
366
367size_t
368CcnxEncodingHelper::EstimateTimestampBlob (const Time &time)
369{
370 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
371 intmax_t ts = time.ToInteger (Time::S) >> 4;
372 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
373 required_bytes++;
374
375 return EstimateBlockHeader (required_bytes) + required_bytes;
376}
377
378size_t
379CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
380 const uint8_t *data, size_t size)
381{
382 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
383 /* 2 */
384 if (size>0)
385 {
386 written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB);
387 start.Write (data, size);
388 written += size;
389 /* size */
390 }
391 written += AppendCloser (start);
392 /* 1 */
393
394 return written;
395}
396
397size_t
398CcnxEncodingHelper::EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size)
399{
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700400 if (size>0)
401 return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1;
402 else
403 return EstimateBlockHeader (dtag) + 1;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700404}
405
406
407
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700408} // namespace ns3