blob: 7cab39870e69bfc82546fa9d0ec3f26bfccc82b7 [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
21#include "ccnx-coding-helper.h"
22
23#include "ns3/name-components.h"
24#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
32#define CCN_TT_BITS 3
33#define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1)
34#define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1)
35#define CCN_TT_HBIT ((unsigned char)(1 << 7))
36
37size_t
38CcnxCodingHelper::AppendBlockHeader (Buffer::Iterator start, size_t val, enum ccn_tt tt)
39{
40 unsigned char buf[1+8*((sizeof(val)+6)/7)];
41 unsigned char *p = &(buf[sizeof(buf)-1]);
42 size_t n = 1;
43 p[0] = (CCN_TT_HBIT & ~CCN_CLOSE) |
44 ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
45 (CCN_TT_MASK & tt);
46 val >>= (7-CCN_TT_BITS);
47 while (val != 0) {
48 (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CCN_CLOSE;
49 n++;
50 val >>= 7;
51 }
52 start.Write (p,n);
53 return n;
54}
55
56size_t
57CcnxCodingHelper::AppendNumber (Buffer::Iterator start, uint32_t number)
58{
59 std::ostringstream os;
60 os << number;
61
62 size_t written = 0;
63 written += AppendBlockHeader (start, os.str().size(), CCN_UDATA);
64 written += os.str().size();
65 start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size());
66
67 return written;
68}
69
70
71size_t
72CcnxCodingHelper::CcnxCodingHelper::AppendCloser (Buffer::Iterator start)
73{
74 start.WriteU8 (CCN_CLOSE);
75 return 1;
76}
77
78size_t
Alexander Afanasyev2a5df202011-08-15 22:39:05 -070079CcnxCodingHelper::AppendNameComponents (Buffer::Iterator start, const Name::Components &name)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070080{
Alexander Afanasyev2a5df202011-08-15 22:39:05 -070081 size_t written = 0;
82 BOOST_FOREACH (const std::string &component, name.GetComponents())
83 {
84 written += AppendTaggedBlob (start, CCN_DTAG_Component,
85 reinterpret_cast<const uint8_t*>(component.c_str()), component.size());
86 }
87 return written;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070088}
89
90size_t
91CcnxCodingHelper::AppendTimestampBlob (Buffer::Iterator start, Time time)
92{
93 // the original function implements Markers... thought not sure what are these markers for...
94
95 // Determine miminal number of bytes required to store the timestamp
96 int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough
97 intmax_t ts = time.ToInteger (Time::S) >> 4;
98 for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
99 required_bytes++;
100
101 size_t len = AppendBlockHeader(start, required_bytes, CCN_BLOB);
102
103 // write part with seconds
104 ts = time.ToInteger (Time::S) >> 4;
105 for (int i = 0; i < required_bytes - 2; i++)
106 start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) );
107
108 /* arithmetic contortions are to avoid overflowing 31 bits */
109 ts = ((time.ToInteger (Time::S) & 15) << 12) + ((time.ToInteger (Time::NS) / 5 * 8 + 195312) / 390625);
110 for (int i = required_bytes - 2; i < required_bytes; i++)
111 start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) );
112
113 return len + required_bytes;
114}
115
116size_t
117CcnxCodingHelper::AppendTaggedBlob (Buffer::Iterator start, ccn_dtag dtag,
118 const uint8_t *data, size_t size)
119{
120 size_t written = AppendBlockHeader (start, dtag, CCN_DTAG);
121 if (size>0)
122 {
123 written += AppendBlockHeader (start, size, CCN_BLOB);
124 start.Write (data, size);
125 written += size;
126 }
127 written += AppendCloser (start);
128
129 return written;
130}
131
132
133size_t
134CcnxCodingHelper::Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest)
135{
136 size_t written = 0;
137 written += AppendBlockHeader (start, CCN_DTAG_Interest, CCN_DTAG); // <Interest>
138
139 written += AppendBlockHeader (start, CCN_DTAG_Name, CCN_DTAG); // <Name>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -0700140 written += AppendNameComponents (start, interest.GetName()); // <Component>...</Component>...
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700141 written += AppendCloser (start); // </Name>
142
143 if (interest.GetMinSuffixComponents() >= 0)
144 {
145 written += AppendBlockHeader (start, CCN_DTAG_MinSuffixComponents, CCN_DTAG);
146 written += AppendNumber (start, interest.GetMinSuffixComponents ());
147 written += AppendCloser (start);
148 }
149 if (interest.GetMaxSuffixComponents() >= 0)
150 {
151 written += AppendBlockHeader (start, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
152 written += AppendNumber (start, interest.GetMaxSuffixComponents ());
153 written += AppendCloser (start);
154 }
155 if (interest.GetExclude().size() > 0)
156 {
157 written += AppendBlockHeader (start, CCN_DTAG_Exclude, CCN_DTAG); // <Exclude>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -0700158 written += AppendNameComponents (start, interest.GetExclude()); // <Component>...</Component>...
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700159 written += AppendCloser (start); // </Exclude>
160 }
161 if (interest.IsEnabledChildSelector())
162 {
163 written += AppendBlockHeader (start, CCN_DTAG_ChildSelector, CCN_DTAG);
164 written += AppendNumber (start, 1);
165 written += AppendCloser (start);
166 }
167 if (interest.IsEnabledAnswerOriginKind())
168 {
169 written += AppendBlockHeader (start, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
170 written += AppendNumber (start, 1);
171 written += AppendCloser (start);
172 }
173 if (interest.GetScope() >= 0)
174 {
175 written += AppendBlockHeader (start, CCN_DTAG_Scope, CCN_DTAG);
176 written += AppendNumber (start, interest.GetScope ());
177 written += AppendCloser (start);
178 }
179 if (!interest.GetInterestLifetime().IsZero())
180 {
181 written += AppendBlockHeader (start, CCN_DTAG_InterestLifetime, CCN_DTAG);
182 written += AppendTimestampBlob (start, interest.GetInterestLifetime());
183 written += AppendCloser (start);
184 }
185 if (interest.GetNonce()>0)
186 {
187 uint32_t nonce = interest.GetNonce();
188 written += AppendTaggedBlob (start, CCN_DTAG_Nonce,
189 reinterpret_cast<const uint8_t*>(&nonce),
190 sizeof(nonce));
191 }
192 written += AppendCloser (start); // </Interest>
193
194 return written;
195}
196
197size_t
198CcnxCodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject)
199{
200 size_t written = 0;
201 written += AppendBlockHeader (start, CCN_DTAG_ContentObject, CCN_DTAG); // <ContentObject>
202
203 // fake signature
204 written += AppendBlockHeader (start, CCN_DTAG_Signature, CCN_DTAG); // <Signature>
205 // Signature ::= DigestAlgorithm?
206 // Witness?
207 // SignatureBits
208 written += AppendTaggedBlob (start, CCN_DTAG_SignatureBits, 0, 0); // <SignatureBits />
209 written += AppendCloser (start); // </Signature>
210
Alexander Afanasyev2a5df202011-08-15 22:39:05 -0700211 written += AppendBlockHeader (start, CCN_DTAG_Name, CCN_DTAG); // <Name>
212 written += AppendNameComponents (start, contentObject.GetName()); // <Component>...</Component>...
213 written += AppendCloser (start); // </Name>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700214
215 // fake signature
216 written += AppendBlockHeader (start, CCN_DTAG_SignedInfo, CCN_DTAG); // <SignedInfo>
217 // SignedInfo ::= PublisherPublicKeyDigest
218 // Timestamp
219 // Type?
220 // FreshnessSeconds?
221 // FinalBlockID?
222 // KeyLocator?
223 written += AppendTaggedBlob (start, CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest />
224 written += AppendCloser (start); // </SignedInfo>
225
226 written += AppendBlockHeader (start, CCN_DTAG_Content, CCN_DTAG); // <Content>
227
228 // there is no closing tag !!!
229 return written;
230}
231
232} // namespace ns3