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