blob: acb3966712bb6a794c8979de21969312b85c3c85 [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 Afanasyev8c5046a2012-06-05 16:22:14 -070087 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Nonce, nonce);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070088 }
Ilya Moiseenkod1f19512011-11-16 14:31:19 -080089
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080090 if (interest.GetNack ()>0)
Ilya Moiseenkod1f19512011-11-16 14:31:19 -080091 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080092 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Nack, CcnbParser::CCN_DTAG);
93 written += AppendNumber (start, interest.GetNack ());
Ilya Moiseenkod1f19512011-11-16 14:31:19 -080094 written += AppendCloser (start);
95 }
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070096 written += AppendCloser (start); // </Interest>
97
98 return written;
99}
100
101size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700102CcnxEncodingHelper::GetSerializedSize (const CcnxInterestHeader &interest)
103{
104 size_t written = 0;
105 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Interest); // <Interest>
106
107 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
108 written += EstimateNameComponents (interest.GetName()); // <Component>...</Component>...
109 written += 1; // </Name>
110
111 if (interest.GetMinSuffixComponents() >= 0)
112 {
113 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MinSuffixComponents);
114 written += EstimateNumber (interest.GetMinSuffixComponents ());
115 written += 1;
116 }
117 if (interest.GetMaxSuffixComponents() >= 0)
118 {
119 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MaxSuffixComponents);
120 written += EstimateNumber (interest.GetMaxSuffixComponents ());
121 written += 1;
122 }
123 if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0)
124 {
125 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Exclude);
126 written += EstimateNameComponents (interest.GetExclude()); // <Component>...</Component>...
127 written += 1; // </Exclude>
128 }
129 if (interest.IsEnabledChildSelector())
130 {
131 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ChildSelector);
132 written += EstimateNumber (1);
133 written += 1;
134 }
135 if (interest.IsEnabledAnswerOriginKind())
136 {
137 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_AnswerOriginKind);
138 written += EstimateNumber (1);
139 written += 1;
140 }
141 if (interest.GetScope() >= 0)
142 {
143 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Scope);
144 written += EstimateNumber (interest.GetScope ());
145 written += 1;
146 }
147 if (!interest.GetInterestLifetime().IsZero())
148 {
149 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_InterestLifetime);
150 written += EstimateTimestampBlob (interest.GetInterestLifetime());
151 written += 1;
152 }
153 if (interest.GetNonce()>0)
154 {
155 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Nonce, sizeof(uint32_t));
156 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800157 if (interest.GetNack ()>0)
Ilya Moiseenkoaa17c8e2011-10-28 13:04:30 -0700158 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800159 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Nack);
160 written += EstimateNumber (interest.GetNack ());
Ilya Moiseenkoaa17c8e2011-10-28 13:04:30 -0700161 written += 1;
162 }
163
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700164 written += 1; // </Interest>
165
166 return written;
167}
168
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700169//////////////////////////////////////////////////////////////////////
170//////////////////////////////////////////////////////////////////////
171//////////////////////////////////////////////////////////////////////
172
173#define CCN_TT_BITS 3
174#define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1)
175#define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1)
176#define CCN_TT_HBIT ((unsigned char)(1 << 7))
177
178size_t
179CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator &start, size_t val, CcnbParser::ccn_tt tt)
180{
181 unsigned char buf[1+8*((sizeof(val)+6)/7)];
182 unsigned char *p = &(buf[sizeof(buf)-1]);
183 size_t n = 1;
184 p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) |
185 ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
186 (CCN_TT_MASK & tt);
187 val >>= (7-CCN_TT_BITS);
188 while (val != 0) {
189 (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE;
190 n++;
191 val >>= 7;
192 }
193 start.Write (p,n);
194 return n;
195}
196
197size_t
198CcnxEncodingHelper::EstimateBlockHeader (size_t value)
199{
200 value >>= (7-CCN_TT_BITS);
201 size_t n = 1;
202 while (value>0)
203 {
204 value >>= 7;
205 n++;
206 }
207 return n;
208}
209
210size_t
211CcnxEncodingHelper::AppendNumber (Buffer::Iterator &start, uint32_t number)
212{
213 std::ostringstream os;
214 os << number;
215
216 size_t written = 0;
217 written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA);
218 written += os.str().size();
219 start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size());
220
221 return written;
222}
223
224size_t
225CcnxEncodingHelper::EstimateNumber (uint32_t number)
226{
227 std::ostringstream os;
228 os << number;
229 return EstimateBlockHeader (os.str ().size ()) + os.str ().size ();
230}
231
232size_t
233CcnxEncodingHelper::AppendCloser (Buffer::Iterator &start)
234{
235 start.WriteU8 (CcnbParser::CCN_CLOSE);
236 return 1;
237}
238
239size_t
240CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name)
241{
242 size_t written = 0;
243 BOOST_FOREACH (const std::string &component, name.GetComponents())
244 {
245 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
246 reinterpret_cast<const uint8_t*>(component.c_str()), component.size());
247 }
248 return written;
249}
250
251size_t
252CcnxEncodingHelper::EstimateNameComponents (const CcnxNameComponents &name)
253{
254 size_t written = 0;
255 BOOST_FOREACH (const std::string &component, name.GetComponents())
256 {
257 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size());
258 }
259 return written;
260}
261
262size_t
263CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator &start, const Time &time)
264{
265 // the original function implements Markers... thought not sure what are these markers for...
266
267 // Determine miminal number of bytes required to store the timestamp
268 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
269 intmax_t ts = time.ToInteger (Time::S) >> 4;
270 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
271 required_bytes++;
272
273 size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB);
274
275 // write part with seconds
276 ts = time.ToInteger (Time::S) >> 4;
277 for (int i = 0; i < required_bytes - 2; i++)
278 start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) );
279
280 /* arithmetic contortions are to avoid overflowing 31 bits */
281 ts = ((time.ToInteger (Time::S) & 15) << 12) +
282 (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625);
283 for (int i = required_bytes - 2; i < required_bytes; i++)
284 start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) );
285
286 return len + required_bytes;
287}
288
289size_t
290CcnxEncodingHelper::EstimateTimestampBlob (const Time &time)
291{
292 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
293 intmax_t ts = time.ToInteger (Time::S) >> 4;
294 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
295 required_bytes++;
296
297 return EstimateBlockHeader (required_bytes) + required_bytes;
298}
299
300size_t
301CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
302 const uint8_t *data, size_t size)
303{
304 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
305 /* 2 */
306 if (size>0)
307 {
308 written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB);
309 start.Write (data, size);
310 written += size;
311 /* size */
312 }
313 written += AppendCloser (start);
314 /* 1 */
315
316 return written;
317}
318
319size_t
320CcnxEncodingHelper::EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size)
321{
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700322 if (size>0)
323 return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1;
324 else
325 return EstimateBlockHeader (dtag) + 1;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700326}
327
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700328size_t
329CcnxEncodingHelper::AppendString (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
330 const std::string &string)
331{
332 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
333 {
334 written += AppendBlockHeader (start, string.size (), CcnbParser::CCN_UDATA);
335 start.Write (reinterpret_cast<const uint8_t*> (string.c_str ()), string.size ());
336 written += string.size ();
337 }
338 written += AppendCloser (start);
339
340 return written;
341}
342
343size_t
344CcnxEncodingHelper::EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string)
345{
346 return EstimateBlockHeader (dtag) + EstimateBlockHeader (string.size ()) + string.size () + 1;
347}
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700348
349
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700350} // namespace ns3