blob: 3dd7ad60c59bd2fbf8355639251d6ed188b08615 [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
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
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
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070038CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator start, size_t val, CcnbParser::ccn_tt tt)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070039{
40 unsigned char buf[1+8*((sizeof(val)+6)/7)];
41 unsigned char *p = &(buf[sizeof(buf)-1]);
42 size_t n = 1;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070043 p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) |
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070044 ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
45 (CCN_TT_MASK & tt);
46 val >>= (7-CCN_TT_BITS);
47 while (val != 0) {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070048 (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE;
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070049 n++;
50 val >>= 7;
51 }
52 start.Write (p,n);
53 return n;
54}
55
56size_t
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070057CcnxEncodingHelper::AppendNumber (Buffer::Iterator start, uint32_t number)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070058{
59 std::ostringstream os;
60 os << number;
61
62 size_t written = 0;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070063 written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070064 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
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070072CcnxEncodingHelper::CcnxEncodingHelper::AppendCloser (Buffer::Iterator start)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070073{
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070074 start.WriteU8 (CcnbParser::CCN_CLOSE);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070075 return 1;
76}
77
78size_t
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -070079CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator start, const CcnxNameComponents &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 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070084 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
Alexander Afanasyev2a5df202011-08-15 22:39:05 -070085 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
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070091CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator start, Time time)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -070092{
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
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700101 size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700102
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
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700117CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator start, CcnbParser::ccn_dtag dtag,
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700118 const uint8_t *data, size_t size)
119{
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700120 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700121 if (size>0)
122 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700123 written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700124 start.Write (data, size);
125 written += size;
126 }
127 written += AppendCloser (start);
128
129 return written;
130}
131
132
133size_t
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700134CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700135{
136 size_t written = 0;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700137 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Interest, CcnbParser::CCN_DTAG); // <Interest>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700138
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700139 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::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 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700145 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MinSuffixComponents, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700146 written += AppendNumber (start, interest.GetMinSuffixComponents ());
147 written += AppendCloser (start);
148 }
149 if (interest.GetMaxSuffixComponents() >= 0)
150 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700151 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MaxSuffixComponents, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700152 written += AppendNumber (start, interest.GetMaxSuffixComponents ());
153 written += AppendCloser (start);
154 }
155 if (interest.GetExclude().size() > 0)
156 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700157 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Exclude, CcnbParser::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 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700163 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ChildSelector, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700164 written += AppendNumber (start, 1);
165 written += AppendCloser (start);
166 }
167 if (interest.IsEnabledAnswerOriginKind())
168 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700169 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_AnswerOriginKind, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700170 written += AppendNumber (start, 1);
171 written += AppendCloser (start);
172 }
173 if (interest.GetScope() >= 0)
174 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700175 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Scope, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700176 written += AppendNumber (start, interest.GetScope ());
177 written += AppendCloser (start);
178 }
179 if (!interest.GetInterestLifetime().IsZero())
180 {
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700181 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_InterestLifetime, CcnbParser::CCN_DTAG);
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700182 written += AppendTimestampBlob (start, interest.GetInterestLifetime());
183 written += AppendCloser (start);
184 }
185 if (interest.GetNonce()>0)
186 {
187 uint32_t nonce = interest.GetNonce();
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700188 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Nonce,
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700189 reinterpret_cast<const uint8_t*>(&nonce),
190 sizeof(nonce));
191 }
192 written += AppendCloser (start); // </Interest>
193
194 return written;
195}
196
197size_t
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700198CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject)
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700199{
200 size_t written = 0;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700201 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ContentObject, CcnbParser::CCN_DTAG); // <ContentObject>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700202
203 // fake signature
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700204 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Signature, CcnbParser::CCN_DTAG); // <Signature>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700205 // Signature ::= DigestAlgorithm?
206 // Witness?
207 // SignatureBits
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700208 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_SignatureBits, 0, 0); // <SignatureBits />
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700209 written += AppendCloser (start); // </Signature>
210
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700211 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name>
Alexander Afanasyev2a5df202011-08-15 22:39:05 -0700212 written += AppendNameComponents (start, contentObject.GetName()); // <Component>...</Component>...
213 written += AppendCloser (start); // </Name>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700214
215 // fake signature
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700216 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700217 // SignedInfo ::= PublisherPublicKeyDigest
218 // Timestamp
219 // Type?
220 // FreshnessSeconds?
221 // FinalBlockID?
222 // KeyLocator?
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700223 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest />
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700224 written += AppendCloser (start); // </SignedInfo>
225
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700226 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content>
Alexander Afanasyevc74a6022011-08-15 20:01:35 -0700227
228 // there is no closing tag !!!
229 return written;
230}
231
232} // namespace ns3