blob: c9292ab989fb125109066d502832e2ec191ea831 [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 Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-encoding-helper.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070022
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070023#include "ns3/ndn-name-components.h"
24#include "ns3/ndn-interest-header.h"
25#include "ns3/ndn-content-object-header.h"
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070026
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 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070031namespace ndn {
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070032
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070033size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034EncodingHelper::Serialize (Buffer::Iterator start, const InterestHeader &interest)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070035{
36 size_t written = 0;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070037 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Interest, CcnbParser::CCN_DTAG); // <Interest>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070038
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070039 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -070040 written += AppendNameComponents (start, interest.GetName()); // <Component>...</Component>...
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070041 written += AppendCloser (start); // </Name>
42
43 if (interest.GetMinSuffixComponents() >= 0)
44 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070045 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MinSuffixComponents, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070046 written += AppendNumber (start, interest.GetMinSuffixComponents ());
47 written += AppendCloser (start);
48 }
49 if (interest.GetMaxSuffixComponents() >= 0)
50 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070051 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MaxSuffixComponents, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070052 written += AppendNumber (start, interest.GetMaxSuffixComponents ());
53 written += AppendCloser (start);
54 }
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070055 if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070056 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070057 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Exclude, CcnbParser::CCN_DTAG); // <Exclude>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -070058 written += AppendNameComponents (start, interest.GetExclude()); // <Component>...</Component>...
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070059 written += AppendCloser (start); // </Exclude>
60 }
61 if (interest.IsEnabledChildSelector())
62 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070063 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ChildSelector, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070064 written += AppendNumber (start, 1);
65 written += AppendCloser (start);
66 }
67 if (interest.IsEnabledAnswerOriginKind())
68 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070069 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_AnswerOriginKind, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070070 written += AppendNumber (start, 1);
71 written += AppendCloser (start);
72 }
73 if (interest.GetScope() >= 0)
74 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070075 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Scope, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070076 written += AppendNumber (start, interest.GetScope ());
77 written += AppendCloser (start);
78 }
79 if (!interest.GetInterestLifetime().IsZero())
80 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070081 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_InterestLifetime, CcnbParser::CCN_DTAG);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080082 written += AppendTimestampBlob (start, interest.GetInterestLifetime ());
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070083 written += AppendCloser (start);
84 }
85 if (interest.GetNonce()>0)
86 {
87 uint32_t nonce = interest.GetNonce();
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070088 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Nonce, nonce);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070089 }
Ilya Moiseenkod1f19512011-11-16 14:31:19 -080090
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080091 if (interest.GetNack ()>0)
Ilya Moiseenkod1f19512011-11-16 14:31:19 -080092 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080093 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Nack, CcnbParser::CCN_DTAG);
94 written += AppendNumber (start, interest.GetNack ());
Ilya Moiseenkod1f19512011-11-16 14:31:19 -080095 written += AppendCloser (start);
96 }
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070097 written += AppendCloser (start); // </Interest>
98
99 return written;
100}
101
102size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103EncodingHelper::GetSerializedSize (const InterestHeader &interest)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700104{
105 size_t written = 0;
106 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Interest); // <Interest>
107
108 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name>
109 written += EstimateNameComponents (interest.GetName()); // <Component>...</Component>...
110 written += 1; // </Name>
111
112 if (interest.GetMinSuffixComponents() >= 0)
113 {
114 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MinSuffixComponents);
115 written += EstimateNumber (interest.GetMinSuffixComponents ());
116 written += 1;
117 }
118 if (interest.GetMaxSuffixComponents() >= 0)
119 {
120 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MaxSuffixComponents);
121 written += EstimateNumber (interest.GetMaxSuffixComponents ());
122 written += 1;
123 }
124 if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0)
125 {
126 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Exclude);
127 written += EstimateNameComponents (interest.GetExclude()); // <Component>...</Component>...
128 written += 1; // </Exclude>
129 }
130 if (interest.IsEnabledChildSelector())
131 {
132 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ChildSelector);
133 written += EstimateNumber (1);
134 written += 1;
135 }
136 if (interest.IsEnabledAnswerOriginKind())
137 {
138 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_AnswerOriginKind);
139 written += EstimateNumber (1);
140 written += 1;
141 }
142 if (interest.GetScope() >= 0)
143 {
144 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Scope);
145 written += EstimateNumber (interest.GetScope ());
146 written += 1;
147 }
148 if (!interest.GetInterestLifetime().IsZero())
149 {
150 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_InterestLifetime);
151 written += EstimateTimestampBlob (interest.GetInterestLifetime());
152 written += 1;
153 }
154 if (interest.GetNonce()>0)
155 {
156 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Nonce, sizeof(uint32_t));
157 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800158 if (interest.GetNack ()>0)
Ilya Moiseenkoaa17c8e2011-10-28 13:04:30 -0700159 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800160 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Nack);
161 written += EstimateNumber (interest.GetNack ());
Ilya Moiseenkoaa17c8e2011-10-28 13:04:30 -0700162 written += 1;
163 }
164
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700165 written += 1; // </Interest>
166
167 return written;
168}
169
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700170//////////////////////////////////////////////////////////////////////
171//////////////////////////////////////////////////////////////////////
172//////////////////////////////////////////////////////////////////////
173
174#define CCN_TT_BITS 3
175#define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1)
176#define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1)
177#define CCN_TT_HBIT ((unsigned char)(1 << 7))
178
179size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180EncodingHelper::AppendBlockHeader (Buffer::Iterator &start, size_t val, CcnbParser::ccn_tt tt)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700181{
182 unsigned char buf[1+8*((sizeof(val)+6)/7)];
183 unsigned char *p = &(buf[sizeof(buf)-1]);
184 size_t n = 1;
185 p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) |
186 ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
187 (CCN_TT_MASK & tt);
188 val >>= (7-CCN_TT_BITS);
189 while (val != 0) {
190 (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE;
191 n++;
192 val >>= 7;
193 }
194 start.Write (p,n);
195 return n;
196}
197
198size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700199EncodingHelper::EstimateBlockHeader (size_t value)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700200{
201 value >>= (7-CCN_TT_BITS);
202 size_t n = 1;
203 while (value>0)
204 {
205 value >>= 7;
206 n++;
207 }
208 return n;
209}
210
211size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700212EncodingHelper::AppendNumber (Buffer::Iterator &start, uint32_t number)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700213{
214 std::ostringstream os;
215 os << number;
216
217 size_t written = 0;
218 written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA);
219 written += os.str().size();
220 start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size());
221
222 return written;
223}
224
225size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700226EncodingHelper::EstimateNumber (uint32_t number)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700227{
228 std::ostringstream os;
229 os << number;
230 return EstimateBlockHeader (os.str ().size ()) + os.str ().size ();
231}
232
233size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700234EncodingHelper::AppendCloser (Buffer::Iterator &start)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700235{
236 start.WriteU8 (CcnbParser::CCN_CLOSE);
237 return 1;
238}
239
240size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700241EncodingHelper::AppendNameComponents (Buffer::Iterator &start, const NameComponents &name)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700242{
243 size_t written = 0;
244 BOOST_FOREACH (const std::string &component, name.GetComponents())
245 {
246 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
247 reinterpret_cast<const uint8_t*>(component.c_str()), component.size());
248 }
249 return written;
250}
251
252size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700253EncodingHelper::EstimateNameComponents (const NameComponents &name)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700254{
255 size_t written = 0;
256 BOOST_FOREACH (const std::string &component, name.GetComponents())
257 {
258 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size());
259 }
260 return written;
261}
262
263size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700264EncodingHelper::AppendTimestampBlob (Buffer::Iterator &start, const Time &time)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700265{
266 // the original function implements Markers... thought not sure what are these markers for...
267
268 // Determine miminal number of bytes required to store the timestamp
269 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
270 intmax_t ts = time.ToInteger (Time::S) >> 4;
271 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
272 required_bytes++;
273
274 size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB);
275
276 // write part with seconds
277 ts = time.ToInteger (Time::S) >> 4;
278 for (int i = 0; i < required_bytes - 2; i++)
279 start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) );
280
281 /* arithmetic contortions are to avoid overflowing 31 bits */
282 ts = ((time.ToInteger (Time::S) & 15) << 12) +
283 (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625);
284 for (int i = required_bytes - 2; i < required_bytes; i++)
285 start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) );
286
287 return len + required_bytes;
288}
289
290size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700291EncodingHelper::EstimateTimestampBlob (const Time &time)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700292{
293 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
294 intmax_t ts = time.ToInteger (Time::S) >> 4;
295 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
296 required_bytes++;
297
298 return EstimateBlockHeader (required_bytes) + required_bytes;
299}
300
301size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700302EncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700303 const uint8_t *data, size_t size)
304{
305 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
306 /* 2 */
307 if (size>0)
308 {
309 written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB);
310 start.Write (data, size);
311 written += size;
312 /* size */
313 }
314 written += AppendCloser (start);
315 /* 1 */
316
317 return written;
318}
319
320size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700321EncodingHelper::EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size)
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700322{
Alexander Afanasyeve91ab752011-08-31 19:13:40 -0700323 if (size>0)
324 return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1;
325 else
326 return EstimateBlockHeader (dtag) + 1;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700327}
328
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700329size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700330EncodingHelper::AppendString (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700331 const std::string &string)
332{
333 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
334 {
335 written += AppendBlockHeader (start, string.size (), CcnbParser::CCN_UDATA);
336 start.Write (reinterpret_cast<const uint8_t*> (string.c_str ()), string.size ());
337 written += string.size ();
338 }
339 written += AppendCloser (start);
340
341 return written;
342}
343
344size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700345EncodingHelper::EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700346{
347 return EstimateBlockHeader (dtag) + EstimateBlockHeader (string.size ()) + string.size () + 1;
348}
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700349
350
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700351} // namespace ndn
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700352} // namespace ns3