blob: 3cafcbce3ca527f2eb268667bd7b95cc149e17ce [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: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
21#ifndef _CCNX_CODING_HELPER_H_
22#define _CCNX_CODING_HELPER_H_
23
24#include <sys/types.h>
25#include "ns3/ptr.h"
26#include "ns3/nstime.h"
27#include "ns3/buffer.h"
28
29namespace ns3 {
30
31namespace Name{ class Components; }
32
33class CcnxInterestHeader;
34class CcnxContentObjectHeader;
35
36/**
37 * Helper to encode/decode ccnb formatted CCNx message
38 *
39 */
40class CcnxCodingHelper
41{
42public:
43 static size_t
44 Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest);
45
46 static size_t
47 Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject);
48
49private:
50 /**
51 * Type tag for a ccnb start marker.
52 *
53 * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html
54 */
55 enum ccn_tt {
56 CCN_EXT, /**< starts composite extension - numval is subtype */
57 CCN_TAG, /**< starts composite - numval is tagnamelen-1 */
58 CCN_DTAG, /**< starts composite - numval is tagdict index (enum ccn_dtag) */
59 CCN_ATTR, /**< attribute - numval is attrnamelen-1, value follows */
60 CCN_DATTR, /**< attribute numval is attrdict index */
61 CCN_BLOB, /**< opaque binary data - numval is byte count */
62 CCN_UDATA, /**< UTF-8 encoded character data - numval is byte count */
63 CCN_NO_TOKEN /**< should not occur in encoding */
64 };
65
66 /** CCN_CLOSE terminates composites */
67 enum {CCN_CLOSE = 0};
68
69 // enum ccn_ext_subtype {
70 // /* skip smallest values for now */
71 // CCN_PROCESSING_INSTRUCTIONS = 16 /* <?name:U value:U?> */
72 // };
73
74 /**
75 * DTAG identifies ccnb-encoded elements.
76 *
77 * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html
78 */
79 enum ccn_dtag {
80 CCN_DTAG_Any = 13,
81 CCN_DTAG_Name = 14,
82 CCN_DTAG_Component = 15,
83 CCN_DTAG_Certificate = 16,
84 CCN_DTAG_Collection = 17,
85 CCN_DTAG_CompleteName = 18,
86 CCN_DTAG_Content = 19,
87 CCN_DTAG_SignedInfo = 20,
88 CCN_DTAG_ContentDigest = 21,
89 CCN_DTAG_ContentHash = 22,
90 CCN_DTAG_Count = 24,
91 CCN_DTAG_Header = 25,
92 CCN_DTAG_Interest = 26, /* 20090915 */
93 CCN_DTAG_Key = 27,
94 CCN_DTAG_KeyLocator = 28,
95 CCN_DTAG_KeyName = 29,
96 CCN_DTAG_Length = 30,
97 CCN_DTAG_Link = 31,
98 CCN_DTAG_LinkAuthenticator = 32,
99 CCN_DTAG_NameComponentCount = 33, /* DeprecatedInInterest */
100 CCN_DTAG_RootDigest = 36,
101 CCN_DTAG_Signature = 37,
102 CCN_DTAG_Start = 38,
103 CCN_DTAG_Timestamp = 39,
104 CCN_DTAG_Type = 40,
105 CCN_DTAG_Nonce = 41,
106 CCN_DTAG_Scope = 42,
107 CCN_DTAG_Exclude = 43,
108 CCN_DTAG_Bloom = 44,
109 CCN_DTAG_BloomSeed = 45,
110 CCN_DTAG_AnswerOriginKind = 47,
111 CCN_DTAG_InterestLifetime = 48,
112 CCN_DTAG_Witness = 53,
113 CCN_DTAG_SignatureBits = 54,
114 CCN_DTAG_DigestAlgorithm = 55,
115 CCN_DTAG_BlockSize = 56,
116 CCN_DTAG_FreshnessSeconds = 58,
117 CCN_DTAG_FinalBlockID = 59,
118 CCN_DTAG_PublisherPublicKeyDigest = 60,
119 CCN_DTAG_PublisherCertificateDigest = 61,
120 CCN_DTAG_PublisherIssuerKeyDigest = 62,
121 CCN_DTAG_PublisherIssuerCertificateDigest = 63,
122 CCN_DTAG_ContentObject = 64, /* 20090915 */
123 CCN_DTAG_WrappedKey = 65,
124 CCN_DTAG_WrappingKeyIdentifier = 66,
125 CCN_DTAG_WrapAlgorithm = 67,
126 CCN_DTAG_KeyAlgorithm = 68,
127 CCN_DTAG_Label = 69,
128 CCN_DTAG_EncryptedKey = 70,
129 CCN_DTAG_EncryptedNonceKey = 71,
130 CCN_DTAG_WrappingKeyName = 72,
131 CCN_DTAG_Action = 73,
132 CCN_DTAG_FaceID = 74,
133 CCN_DTAG_IPProto = 75,
134 CCN_DTAG_Host = 76,
135 CCN_DTAG_Port = 77,
136 CCN_DTAG_MulticastInterface = 78,
137 CCN_DTAG_ForwardingFlags = 79,
138 CCN_DTAG_FaceInstance = 80,
139 CCN_DTAG_ForwardingEntry = 81,
140 CCN_DTAG_MulticastTTL = 82,
141 CCN_DTAG_MinSuffixComponents = 83,
142 CCN_DTAG_MaxSuffixComponents = 84,
143 CCN_DTAG_ChildSelector = 85,
144 CCN_DTAG_RepositoryInfo = 86,
145 CCN_DTAG_Version = 87,
146 CCN_DTAG_RepositoryVersion = 88,
147 CCN_DTAG_GlobalPrefix = 89,
148 CCN_DTAG_LocalName = 90,
149 CCN_DTAG_Policy = 91,
150 CCN_DTAG_Namespace = 92,
151 CCN_DTAG_GlobalPrefixName = 93,
152 CCN_DTAG_PolicyVersion = 94,
153 CCN_DTAG_KeyValueSet = 95,
154 CCN_DTAG_KeyValuePair = 96,
155 CCN_DTAG_IntegerValue = 97,
156 CCN_DTAG_DecimalValue = 98,
157 CCN_DTAG_StringValue = 99,
158 CCN_DTAG_BinaryValue = 100,
159 CCN_DTAG_NameValue = 101,
160 CCN_DTAG_Entry = 102,
161 CCN_DTAG_ACL = 103,
162 CCN_DTAG_ParameterizedName = 104,
163 CCN_DTAG_Prefix = 105,
164 CCN_DTAG_Suffix = 106,
165 CCN_DTAG_Root = 107,
166 CCN_DTAG_ProfileName = 108,
167 CCN_DTAG_Parameters = 109,
168 CCN_DTAG_InfoString = 110,
169 CCN_DTAG_StatusResponse = 112,
170 CCN_DTAG_StatusCode = 113,
171 CCN_DTAG_StatusText = 114,
172 CCN_DTAG_SequenceNumber = 256,
173 CCN_DTAG_CCNProtocolDataUnit = 17702112
174 };
175
176 /**
177 * The decoder state is one of these, possibly with some
178 * additional bits set for internal use. A complete parse
179 * ends up in state 0 or an error state. Not all possible
180 * error states are listed here.
181 */
182 enum ccn_decoder_state {
183 CCN_DSTATE_INITIAL = 0,
184 CCN_DSTATE_NEWTOKEN,
185 CCN_DSTATE_NUMVAL,
186 CCN_DSTATE_UDATA,
187 CCN_DSTATE_TAGNAME,
188 CCN_DSTATE_ATTRNAME,
189 CCN_DSTATE_BLOB,
190 /* All error states are negative */
191 CCN_DSTATE_ERR_OVERFLOW = -1,
192 CCN_DSTATE_ERR_ATTR = -2,
193 CCN_DSTATE_ERR_CODING = -3,
194 CCN_DSTATE_ERR_NEST = -4,
195 CCN_DSTATE_ERR_BUG = -5
196 };
197
198
199private:
200 static size_t
201 AppendBlockHeader (Buffer::Iterator start, size_t value, ccn_tt block_type);
202
203 static size_t
204 AppendNumber (Buffer::Iterator start, uint32_t number);
205
206 static size_t
207 AppendCloser (Buffer::Iterator start);
208
209 static size_t
210 AppendName (Buffer::Iterator start, const Name::Components &name);
211
212 /**
213 * Append a binary timestamp as a BLOB using the ccn binary
214 * Timestamp representation (12-bit fraction).
215 *
216 * @param start start iterator of the buffer to append to.
217 * @param time - Time object
218 *
219 * @returns written length
220 */
221 static size_t
222 AppendTimestampBlob (Buffer::Iterator start, Time time);
223
224 /**
225 * Append a tagged BLOB
226 *
227 * This is a ccnb-encoded element with containing the BLOB as content
228 *
229 * @param start start iterator of the buffer to append to.
230 * @param dtag is the element's dtab
231 * @param data points to the binary data
232 * @param size is the size of the data, in bytes
233 *
234 * @returns written length
235 */
236 static size_t
237 AppendTaggedBlob (Buffer::Iterator start, ccn_dtag dtag,
238 const uint8_t *data, size_t size);
239
240};
241
242} // namespace ns3
243
244#endif // _CCNX_CODING_HELPER_H_
245