blob: e63c67ce8b0ad4e1f1df7e44dbd2d4f0bf1614d7 [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 *
Ilya Moiseenko332add02011-12-24 17:21:25 -080018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070019 */
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 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800159 if (interest.GetNack ()>0)
Ilya Moiseenkoaa17c8e2011-10-28 13:04:30 -0700160 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800161 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Nack);
162 written += EstimateNumber (interest.GetNack ());
Ilya Moiseenkoaa17c8e2011-10-28 13:04:30 -0700163 written += 1;
164 }
165
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700166 written += 1; // </Interest>
167
168 return written;
169}
170
171size_t
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700172CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700173{
174 size_t written = 0;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700175 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ContentObject, CcnbParser::CCN_DTAG); // <ContentObject>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700176
177 // fake signature
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700178 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Signature, CcnbParser::CCN_DTAG); // <Signature>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700179 // Signature ::= DigestAlgorithm?
180 // Witness?
181 // SignatureBits
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700182 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_SignatureBits, 0, 0); // <SignatureBits />
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700183 written += AppendCloser (start); // </Signature>
184
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700185 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -0700186 written += AppendNameComponents (start, contentObject.GetName()); // <Component>...</Component>...
187 written += AppendCloser (start); // </Name>
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_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700191 // SignedInfo ::= PublisherPublicKeyDigest
192 // Timestamp
193 // Type?
194 // FreshnessSeconds?
195 // FinalBlockID?
196 // KeyLocator?
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700197 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest />
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700198 written += AppendCloser (start); // </SignedInfo>
199
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700200 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700201
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700202 // there are no closing tags !!!
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700203 return written;
204}
205
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700206size_t
207CcnxEncodingHelper::GetSerializedSize (const CcnxContentObjectHeader &contentObject)
208{
209 size_t written = 0;
210 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ContentObject); // <ContentObject>
211
212 // fake signature
213 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Signature); // <Signature>
214 // Signature ::= DigestAlgorithm?
215 // Witness?
216 // SignatureBits
217 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_SignatureBits, 0); // <SignatureBits />
218 written += 1; // </Signature>
219
220 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
221 written += EstimateNameComponents (contentObject.GetName()); // <Component>...</Component>...
222 written += 1; // </Name>
223
224 // fake signature
225 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_SignedInfo); // <SignedInfo>
226 // SignedInfo ::= PublisherPublicKeyDigest
227 // Timestamp
228 // Type?
229 // FreshnessSeconds?
230 // FinalBlockID?
231 // KeyLocator?
232 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0); // <PublisherPublicKeyDigest />
233 written += 1; // </SignedInfo>
234
235 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Content); // <Content>
236
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700237 // there are no closing tags !!!
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700238 return written;
239}
240
241//////////////////////////////////////////////////////////////////////
242//////////////////////////////////////////////////////////////////////
243//////////////////////////////////////////////////////////////////////
244
245#define CCN_TT_BITS 3
246#define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1)
247#define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1)
248#define CCN_TT_HBIT ((unsigned char)(1 << 7))
249
250size_t
251CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator &start, size_t val, CcnbParser::ccn_tt tt)
252{
253 unsigned char buf[1+8*((sizeof(val)+6)/7)];
254 unsigned char *p = &(buf[sizeof(buf)-1]);
255 size_t n = 1;
256 p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) |
257 ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
258 (CCN_TT_MASK & tt);
259 val >>= (7-CCN_TT_BITS);
260 while (val != 0) {
261 (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE;
262 n++;
263 val >>= 7;
264 }
265 start.Write (p,n);
266 return n;
267}
268
269size_t
270CcnxEncodingHelper::EstimateBlockHeader (size_t value)
271{
272 value >>= (7-CCN_TT_BITS);
273 size_t n = 1;
274 while (value>0)
275 {
276 value >>= 7;
277 n++;
278 }
279 return n;
280}
281
282size_t
283CcnxEncodingHelper::AppendNumber (Buffer::Iterator &start, uint32_t number)
284{
285 std::ostringstream os;
286 os << number;
287
288 size_t written = 0;
289 written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA);
290 written += os.str().size();
291 start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size());
292
293 return written;
294}
295
296size_t
297CcnxEncodingHelper::EstimateNumber (uint32_t number)
298{
299 std::ostringstream os;
300 os << number;
301 return EstimateBlockHeader (os.str ().size ()) + os.str ().size ();
302}
303
304size_t
305CcnxEncodingHelper::AppendCloser (Buffer::Iterator &start)
306{
307 start.WriteU8 (CcnbParser::CCN_CLOSE);
308 return 1;
309}
310
311size_t
312CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name)
313{
314 size_t written = 0;
315 BOOST_FOREACH (const std::string &component, name.GetComponents())
316 {
317 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
318 reinterpret_cast<const uint8_t*>(component.c_str()), component.size());
319 }
320 return written;
321}
322
323size_t
324CcnxEncodingHelper::EstimateNameComponents (const CcnxNameComponents &name)
325{
326 size_t written = 0;
327 BOOST_FOREACH (const std::string &component, name.GetComponents())
328 {
329 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size());
330 }
331 return written;
332}
333
334size_t
335CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator &start, const Time &time)
336{
337 // the original function implements Markers... thought not sure what are these markers for...
338
339 // Determine miminal number of bytes required to store the timestamp
340 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
341 intmax_t ts = time.ToInteger (Time::S) >> 4;
342 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
343 required_bytes++;
344
345 size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB);
346
347 // write part with seconds
348 ts = time.ToInteger (Time::S) >> 4;
349 for (int i = 0; i < required_bytes - 2; i++)
350 start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) );
351
352 /* arithmetic contortions are to avoid overflowing 31 bits */
353 ts = ((time.ToInteger (Time::S) & 15) << 12) +
354 (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625);
355 for (int i = required_bytes - 2; i < required_bytes; i++)
356 start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) );
357
358 return len + required_bytes;
359}
360
361size_t
362CcnxEncodingHelper::EstimateTimestampBlob (const Time &time)
363{
364 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
365 intmax_t ts = time.ToInteger (Time::S) >> 4;
366 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
367 required_bytes++;
368
369 return EstimateBlockHeader (required_bytes) + required_bytes;
370}
371
372size_t
373CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
374 const uint8_t *data, size_t size)
375{
376 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
377 /* 2 */
378 if (size>0)
379 {
380 written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB);
381 start.Write (data, size);
382 written += size;
383 /* size */
384 }
385 written += AppendCloser (start);
386 /* 1 */
387
388 return written;
389}
390
391size_t
392CcnxEncodingHelper::EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size)
393{
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700394 if (size>0)
395 return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1;
396 else
397 return EstimateBlockHeader (dtag) + 1;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700398}
399
400
401
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700402} // namespace ns3