blob: bb1eef626f2ad463d98563c93375648cea02d9a1 [file] [log] [blame]
Alexander Afanasyev834f35c2011-08-16 17:13:50 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev1043c702013-07-15 16:21:09 -07003 * Copyright (c) 2011-2013 University of California, Los Angeles
Alexander Afanasyev834f35c2011-08-16 17:13:50 -07004 *
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 Afanasyev834f35c2011-08-16 17:13:50 -070019 */
20
Alexander Afanasyev1043c702013-07-15 16:21:09 -070021#ifndef NDN_WIRE_CCNB_SYNTAX_H
22#define NDN_WIRE_CCNB_SYNTAX_H
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070023
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070024#include "ns3/ptr.h"
25#include "ns3/nstime.h"
26#include "ns3/buffer.h"
27
Alexander Afanasyev1043c702013-07-15 16:21:09 -070028#include "ns3/ndn-common.h"
29#include "ns3/ndn-name.h"
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070030
Alexander Afanasyev1043c702013-07-15 16:21:09 -070031NDN_NAMESPACE_BEGIN
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070032
Alexander Afanasyev1043c702013-07-15 16:21:09 -070033namespace wire {
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070034
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070035/**
Alexander Afanasyev1043c702013-07-15 16:21:09 -070036 * \brief Helper to encode CCNb blocks
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070037 */
Alexander Afanasyev1043c702013-07-15 16:21:09 -070038class Ccnb
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070039{
40public:
Ilya Moiseenko332add02011-12-24 17:21:25 -080041 /**
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070042 * @brief Append CCNB block header
Alexander Afanasyev1043c702013-07-15 16:21:09 -070043 * @param start Buffer to store serialized
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070044 * @param value dictionary id of the block header
45 * @param block_type Type of CCNB block
46 *
47 * @returns written length
48 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070049 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -070050 AppendBlockHeader (Buffer::Iterator &start, size_t value, uint32_t block_type);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070051
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070052 /**
53 * @brief Estimate size of the CCNB block header
54 * @param value dictionary id of the block header
55 * @returns estimated length
56 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070057 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070058 EstimateBlockHeader (size_t value);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070059
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070060 /**
61 * @brief Add number in CCNB encoding
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070062 * @param start Buffer to store serialized NdnInterest
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070063 * @param number Number to be written
64 *
65 * @returns written length
66 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070067 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070068 AppendNumber (Buffer::Iterator &start, uint32_t number);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070069
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070070 /**
71 * @brief Estimate size of the number in CCNB encoding
72 * @param number Number to be written
73 * @returns estimated length
74 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070075 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070076 EstimateNumber (uint32_t number);
77
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070078 /**
79 * @brief Append CCNB closer tag (estimated size is 1)
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070080 * @param start Buffer to store serialized Interest
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070081 *
82 * @returns written length
83 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070084 static size_t
85 AppendCloser (Buffer::Iterator &start);
86
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070087 /**
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070088 * Append a binary timestamp as a BLOB using the ccn binary
89 * Timestamp representation (12-bit fraction).
90 *
91 * @param start start iterator of the buffer to append to.
92 * @param time - Time object
93 *
94 * @returns written length
95 */
96 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070097 AppendTimestampBlob (Buffer::Iterator &start, const Time &time);
98
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070099 /**
100 * @brief Estimate size of a binary timestamp as a BLOB using CCNB enconding
101 * @param time - Time object
102 * @returns estimated length
103 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700104 static size_t
105 EstimateTimestampBlob (const Time &time);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700106
107 /**
108 * Append a tagged BLOB
109 *
110 * This is a ccnb-encoded element with containing the BLOB as content
111 *
112 * @param start start iterator of the buffer to append to.
113 * @param dtag is the element's dtab
114 * @param data points to the binary data
115 * @param size is the size of the data, in bytes
116 *
117 * @returns written length
118 */
119 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700120 AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag,
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700121 const uint8_t *data, size_t size);
122
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700123 /**
Alexander Afanasyev0aa47bf2013-07-30 10:55:36 -0700124 * Append a tagged BLOB, adding 0-byte padding if necessary
125 *
126 * This is a ccnb-encoded element with containing the BLOB as content
127 *
128 * @param start start iterator of the buffer to append to.
129 * @param dtag is the element's dtab
130 * @param length minimum required length of the added field (padding added if necessary)
131 * @param data points to the binary data
132 * @param size is the size of the data, in bytes
133 *
134 * @returns written length
135 */
136 static size_t
137 AppendTaggedBlobWithPadding (Buffer::Iterator &start, uint32_t dtag,
138 uint32_t length,
139 const uint8_t *data, size_t size);
140
141 /**
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700142 * @brief Estimate size of a tagged BLOB in CCNB enconding
143 * @param dtag is the element's dtab
144 * @param size is the size of the data, in bytes
145 * @returns estimated length
146 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700147 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700148 EstimateTaggedBlob (uint32_t dtag, size_t size);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700149
150 /**
151 * Append value as a tagged BLOB (templated version)
152 *
153 * This is a ccnb-encoded element with containing the BLOB as content
154 *
155 * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof
156 *
157 * @param start start iterator of the buffer to append to.
158 * @param dtag is the element's dtab
159 * @param data a value to add
160 *
161 * @returns written length
162 */
163 template<class T>
164 static inline size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700165 AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700166
167 /**
Alexander Afanasyev0aa47bf2013-07-30 10:55:36 -0700168 * Append value as a tagged BLOB (templated version), add 0-padding if necessary
169 *
170 * This is a ccnb-encoded element with containing the BLOB as content
171 *
172 * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof
173 *
174 * @param start start iterator of the buffer to append to.
175 * @param dtag is the element's dtab
176 * @param length minimum required length of the field
177 * @param data a value to add
178 *
179 * @returns written length
180 */
181 template<class T>
182 static inline size_t
183 AppendTaggedBlobWithPadding (Buffer::Iterator &start, uint32_t dtag, uint32_t length, const T &data);
184
185 /**
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700186 * Append a tagged string (should be a valid UTF-8 coded string)
187 *
188 * This is a ccnb-encoded element with containing UDATA as content
189 *
190 * @param start start iterator of the buffer to append to.
191 * @param dtag is the element's dtab
192 * @param string UTF-8 string to be written
193 *
194 * @returns written length
195 */
196 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700197 AppendString (Buffer::Iterator &start, uint32_t dtag,
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700198 const std::string &string);
199
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700200 /**
201 * @brief Estimate size of the string in CCNB encoding
202 * @param dtag is the element's dtab
203 * @param string UTF-8 string to be written
204 * @returns estimated length
205 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700206 static size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700207 EstimateString (uint32_t dtag, const std::string &string);
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700208
209 ////////////////////////////////
210 // General use wire formatters
211 ////////////////////////////////
212
213 /**
214 * @brief Append Name in CCNB encoding
215 * @param start Buffer to store serialized Interest
216 * @param name constant reference to Name object
217 *
218 * @returns written length
219 */
220 static size_t
221 SerializeName (Buffer::Iterator &start, const Name &name);
222
223 /**
224 * @brief Estimate size of Name in CCNB encoding
225 * @param name constant reference to Name object
226 * @returns estimated length
227 */
228 static size_t
229 SerializedSizeName (const Name &name);
230
231 /**
232 * @brief Deserialize Name from CCNB encodeing
233 * @param start Buffer that stores serialized Interest
234 * @param name Name object
235 */
236 static Ptr<Name>
237 DeserializeName (Buffer::Iterator &start);
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700238}; // Ccnb
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700239
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700240
241template<class T>
Alexander Afanasyev0aa47bf2013-07-30 10:55:36 -0700242inline size_t
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700243Ccnb::AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700244{
245 return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data));
246}
247
Alexander Afanasyev0aa47bf2013-07-30 10:55:36 -0700248template<class T>
249inline size_t
250Ccnb::AppendTaggedBlobWithPadding (Buffer::Iterator &start, uint32_t dtag, uint32_t length, const T &data)
251{
252 return AppendTaggedBlobWithPadding (start, dtag, length, reinterpret_cast<const uint8_t*> (&data), sizeof (data));
253}
254
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700255} // wire
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700256
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700257NDN_NAMESPACE_END
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700258
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700259#endif // NDN_WIRE_CCNB_SYNTAX_H