blob: 45fdc84de680676f9a155dfa3f503427a45d548c [file] [log] [blame]
Alexander Afanasyev834f35c2011-08-16 17:13:50 -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 Afanasyev834f35c2011-08-16 17:13:50 -070019 */
20
21#ifndef _CCNX_ENCODING_HELPER_H_
22#define _CCNX_ENCODING_HELPER_H_
23
24#include <sys/types.h>
25
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080026#include "ccnb-parser/ccnb-parser-common.h"
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070027#include "ns3/ptr.h"
28#include "ns3/nstime.h"
29#include "ns3/buffer.h"
30
31namespace ns3 {
32
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -070033class CcnxNameComponents;
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070034
35class CcnxInterestHeader;
36class CcnxContentObjectHeader;
37
38/**
Ilya Moiseenko332add02011-12-24 17:21:25 -080039 * \brief Helper to encode/decode ccnb formatted CCNx message
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070040 */
41class CcnxEncodingHelper
42{
43public:
Ilya Moiseenko332add02011-12-24 17:21:25 -080044 /**
45 * \brief Serialize CcnxInterestHeader to Buffer::Iterator
46 * @param start Buffer to store serialized CcnxInterestHeader
47 * @param interest Pointer to CcnxInterestHeader to be serialized
48 * @return length of serialized CcnxInterestHeader
49 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070050 static size_t
51 Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest);
52
Ilya Moiseenko332add02011-12-24 17:21:25 -080053 /**
54 * \brief Compute the size of serialized CcnxInterestHeader
55 * @param interest Pointer to CcnxInterestHeader
56 * @return length
57 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070058 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070059 GetSerializedSize (const CcnxInterestHeader &interest);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070060
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070061public:
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070062 /**
63 * @brief Append CCNB block header
64 * @param start Buffer to store serialized CcnxInterestHeader
65 * @param value dictionary id of the block header
66 * @param block_type Type of CCNB block
67 *
68 * @returns written length
69 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070070 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070071 AppendBlockHeader (Buffer::Iterator &start, size_t value, CcnbParser::ccn_tt block_type);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070072
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070073 /**
74 * @brief Estimate size of the CCNB block header
75 * @param value dictionary id of the block header
76 * @returns estimated length
77 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070078 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070079 EstimateBlockHeader (size_t value);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070080
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070081 /**
82 * @brief Add number in CCNB encoding
83 * @param start Buffer to store serialized CcnxInterestHeader
84 * @param number Number to be written
85 *
86 * @returns written length
87 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070088 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070089 AppendNumber (Buffer::Iterator &start, uint32_t number);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070090
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070091 /**
92 * @brief Estimate size of the number in CCNB encoding
93 * @param number Number to be written
94 * @returns estimated length
95 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070096 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070097 EstimateNumber (uint32_t number);
98
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070099 /**
100 * @brief Append CCNB closer tag (estimated size is 1)
101 * @param start Buffer to store serialized CcnxInterestHeader
102 *
103 * @returns written length
104 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700105 static size_t
106 AppendCloser (Buffer::Iterator &start);
107
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700108 /**
109 * @brief Append CcnxNameComponents in CCNB encoding
110 * @param start Buffer to store serialized CcnxInterestHeader
111 * @param name constant reference to CcnxNameComponents object
112 *
113 * @returns written length
114 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700115 static size_t
116 AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name);
117
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700118 /**
119 * @brief Estimate size of CcnxNameComponents in CCNB encoding
120 * @param name constant reference to CcnxNameComponents object
121 * @returns estimated length
122 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700123 static size_t
124 EstimateNameComponents (const CcnxNameComponents &name);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700125
126 /**
127 * Append a binary timestamp as a BLOB using the ccn binary
128 * Timestamp representation (12-bit fraction).
129 *
130 * @param start start iterator of the buffer to append to.
131 * @param time - Time object
132 *
133 * @returns written length
134 */
135 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700136 AppendTimestampBlob (Buffer::Iterator &start, const Time &time);
137
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700138 /**
139 * @brief Estimate size of a binary timestamp as a BLOB using CCNB enconding
140 * @param time - Time object
141 * @returns estimated length
142 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700143 static size_t
144 EstimateTimestampBlob (const Time &time);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700145
146 /**
147 * Append a tagged BLOB
148 *
149 * This is a ccnb-encoded element with containing the BLOB as content
150 *
151 * @param start start iterator of the buffer to append to.
152 * @param dtag is the element's dtab
153 * @param data points to the binary data
154 * @param size is the size of the data, in bytes
155 *
156 * @returns written length
157 */
158 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700159 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700160 const uint8_t *data, size_t size);
161
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700162 /**
163 * @brief Estimate size of a tagged BLOB in CCNB enconding
164 * @param dtag is the element's dtab
165 * @param size is the size of the data, in bytes
166 * @returns estimated length
167 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700168 static size_t
169 EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700170
171 /**
172 * Append value as a tagged BLOB (templated version)
173 *
174 * This is a ccnb-encoded element with containing the BLOB as content
175 *
176 * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof
177 *
178 * @param start start iterator of the buffer to append to.
179 * @param dtag is the element's dtab
180 * @param data a value to add
181 *
182 * @returns written length
183 */
184 template<class T>
185 static inline size_t
186 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data);
187
188 /**
189 * Append a tagged string (should be a valid UTF-8 coded string)
190 *
191 * This is a ccnb-encoded element with containing UDATA as content
192 *
193 * @param start start iterator of the buffer to append to.
194 * @param dtag is the element's dtab
195 * @param string UTF-8 string to be written
196 *
197 * @returns written length
198 */
199 static size_t
200 AppendString (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
201 const std::string &string);
202
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700203 /**
204 * @brief Estimate size of the string in CCNB encoding
205 * @param dtag is the element's dtab
206 * @param string UTF-8 string to be written
207 * @returns estimated length
208 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700209 static size_t
210 EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700211};
212
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700213
214template<class T>
215size_t
216CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data)
217{
218 return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data));
219}
220
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700221} // namespace ns3
222
223#endif // _CCNX_ENCODING_HELPER_H_
224