blob: 319e5460c8236faa711ba88d6d33d757e115e259 [file] [log] [blame]
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -07001/*
2 * ccn_ccn.h
3 * Abstraction
4 *
5 * Created by Ilya on 7/29/11.
6 * Copyright 2011 __MyCompanyName__. All rights reserved.
7 *
8 */
9
10#ifndef CCN_CCN_DEFINED
11#define CCN_CCN_DEFINED
12
13#include <stdint.h>
14#include "ccn_coding.h"
15#include "ccn_charbuf.h"
16#include "ccn_indexbuf.h"
17
Ilya Moiseenko7e254b72011-08-04 19:06:10 -070018/***********************************
19 * Writing Names
20 * Names for interests are constructed in charbufs using
21 * the following routines.
22 */
23
24/*
25 * ccn_name_init: reset charbuf to represent an empty Name in binary format
26 * Return value is 0, or -1 for error.
27 */
28int ccn_name_init(struct ccn_charbuf *c);
29
30/*
31 * ccn_name_append: add a Component to a Name
32 * The component is an arbitrary string of n octets, no escaping required.
33 * Return value is 0, or -1 for error.
34 */
35int ccn_name_append(struct ccn_charbuf *c, const void *component, size_t n);
36
37/*
38 * ccn_name_append_str: add a Component that is a \0 terminated string.
39 * The component added is the bytes of the string without the \0.
40 * This function is convenient for those applications that construct
41 * component names from simple strings.
42 * Return value is 0, or -1 for error
43 */
44int ccn_name_append_str(struct ccn_charbuf *c, const char *s);
45
46/*
47 * ccn_name_append_components: add sequence of ccnb-encoded Components
48 * to a ccnb-encoded Name
49 * start and stop are offsets from ccnb
50 * Return value is 0, or -1 for obvious error
51 */
52int ccn_name_append_components(struct ccn_charbuf *c,
53 const unsigned char *ccnb,
54 size_t start, size_t stop);
55
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -070056enum ccn_marker {
Ilya Moiseenko7e254b72011-08-04 19:06:10 -070057 CCN_MARKER_NONE = -1,
58 CCN_MARKER_SEQNUM = 0x00, /**< consecutive block sequence numbers */
59 CCN_MARKER_CONTROL = 0xC1, /**< commands, etc. */
60 CCN_MARKER_OSEQNUM = 0xF8, /**< deprecated */
61 CCN_MARKER_BLKID = 0xFB, /**< nonconsecutive block ids */
62 CCN_MARKER_VERSION = 0xFD /**< timestamp-based versioning */
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -070063};
64
Ilya Moiseenko7e254b72011-08-04 19:06:10 -070065/*
66 * ccn_name_append_numeric: add binary Component to ccnb-encoded Name
67 * These are special components used for marking versions, fragments, etc.
68 * Return value is 0, or -1 for error
69 * see doc/technical/NameConventions.html
70 */
71int ccn_name_append_numeric(struct ccn_charbuf *c,
72 enum ccn_marker tag, uintmax_t value);
73
74/*
75 * ccn_name_append_nonce: add nonce Component to ccnb-encoded Name
76 * Uses %C1.N.n marker.
77 * see doc/technical/NameConventions.html
78 */
79int ccn_name_append_nonce(struct ccn_charbuf *c);
80
81/*
82 * ccn_name_split: find Component boundaries in a ccnb-encoded Name
83 * Thin veneer over ccn_parse_Name().
84 * returns -1 for error, otherwise the number of Components
85 * components arg may be NULL to just do a validity check
86 */
87int ccn_name_split(const struct ccn_charbuf *c,
88 struct ccn_indexbuf* components);
89
90/*
91 * ccn_name_chop: Chop the name down to n components.
92 * returns -1 for error, otherwise the new number of Components
93 * components arg may be NULL; if provided it must be consistent with
94 * some prefix of the name, and is updated accordingly.
95 * n may be negative to say how many components to remove instead of how
96 * many to leave, e.g. -1 will remove just the last component.
97 */
98int ccn_name_chop(struct ccn_charbuf *c,
99 struct ccn_indexbuf* components, int n);
100
101
102
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -0700103
104/*********** Interest parsing ***********/
105
106/*
107 * The parse of an interest results in an array of offsets into the
108 * wire representation, with the start and end of each major element and
109 * a few of the inportant sub-elements. The following enum allows those
110 * array items to be referred to symbolically. The *_B_* indices correspond
111 * to beginning offsets and the *_E_* indices correspond to ending offsets.
112 * An omitted element has its beginning and ending offset equal to each other.
113 * Normally these offsets will end up in non-decreasing order.
114 * Some aliasing tricks may be played here, e.g. since
115 * offset[CCN_PI_E_ComponentLast] is always equal to
116 * offset[CCN_PI_E_LastPrefixComponent],
117 * we may define CCN_PI_E_ComponentLast = CCN_PI_E_LastPrefixComponent.
118 * However, code should not rely on that,
119 * since it may change from time to time as the
120 * interest schema evolves.
121 */
122enum ccn_parsed_interest_offsetid {
123 CCN_PI_B_Name,
124 CCN_PI_B_Component0,
125 CCN_PI_B_LastPrefixComponent,
126 CCN_PI_E_LastPrefixComponent,
127 CCN_PI_E_ComponentLast = CCN_PI_E_LastPrefixComponent,
128 CCN_PI_E_Name,
129 CCN_PI_B_MinSuffixComponents,
130 CCN_PI_E_MinSuffixComponents,
131 CCN_PI_B_MaxSuffixComponents,
132 CCN_PI_E_MaxSuffixComponents,
133 CCN_PI_B_PublisherID, // XXX - rename
134 CCN_PI_B_PublisherIDKeyDigest,
135 CCN_PI_E_PublisherIDKeyDigest,
136 CCN_PI_E_PublisherID,
137 CCN_PI_B_Exclude,
138 CCN_PI_E_Exclude,
139 CCN_PI_B_ChildSelector,
140 CCN_PI_E_ChildSelector,
141 CCN_PI_B_AnswerOriginKind,
142 CCN_PI_E_AnswerOriginKind,
143 CCN_PI_B_Scope,
144 CCN_PI_E_Scope,
145 CCN_PI_B_InterestLifetime,
146 CCN_PI_E_InterestLifetime,
147 CCN_PI_B_Nonce,
148 CCN_PI_E_Nonce,
149 CCN_PI_B_OTHER,
150 CCN_PI_E_OTHER,
151 CCN_PI_E
152};
153
154
155struct ccn_parsed_interest {
156 int magic;
157 int prefix_comps;
158 int min_suffix_comps;
159 int max_suffix_comps;
160 int orderpref;
161 int answerfrom;
162 int scope;
163 unsigned short offset[CCN_PI_E+1];
164};
165
166/*
167 * Bitmasks for AnswerOriginKind
168 */
169#define CCN_AOK_CS 0x1 /* Answer from content store */
170#define CCN_AOK_NEW 0x2 /* OK to produce new content */
171#define CCN_AOK_DEFAULT (CCN_AOK_CS | CCN_AOK_NEW)
172#define CCN_AOK_STALE 0x4 /* OK to answer with stale data */
173#define CCN_AOK_EXPIRE 0x10 /* Mark as stale (must have Scope 0) */
174
175/***********************************
176 * Low-level binary formatting
177 */
178
179/*
180 * Append a ccnb start marker
181 *
182 * This forms the basic building block of ccnb-encoded data.
183 * c is the buffer to append to.
184 * Return value is 0, or -1 for error.
185 */
186int ccn_charbuf_append_tt(struct ccn_charbuf *c, size_t val, enum ccn_tt tt);
187
188/**
189 * Append a CCN_CLOSE
190 *
191 * Use this to close off an element in ccnb-encoded data.
192 * @param c is the buffer to append to.
193 * @returns 0 for success or -1 for error.
194 */
195int ccn_charbuf_append_closer(struct ccn_charbuf *c);
196
197/***********************************
198 * Slightly higher level binary formatting
199 */
200
201/*
202 * Append a non-negative integer as a UDATA.
203 */
204int ccnb_append_number(struct ccn_charbuf *c, int nni);
205
206/*
207 * Append a binary timestamp
208 * as a BLOB using the ccn binary Timestamp representation (12-bit fraction).
209 */
210int ccnb_append_timestamp_blob(struct ccn_charbuf *c,
211 enum ccn_marker marker,
212 long long secs, int nsecs);
213
214/*
215 * Append a binary timestamp, using the current time.
216 */
217int ccnb_append_now_blob(struct ccn_charbuf *c, enum ccn_marker marker);
218
219/*
220 * Append a start-of-element marker.
221 */
222int ccnb_element_begin(struct ccn_charbuf *c, enum ccn_dtag dtag);
223
224/*
225 * Append an end-of-element marker.
226 * This is the same as ccn_charbuf_append_closer()
227 */
228int ccnb_element_end(struct ccn_charbuf *c);
229
230/*
231 * Append a tagged BLOB
232 */
233int ccnb_append_tagged_blob(struct ccn_charbuf *c, enum ccn_dtag dtag,
234 const void *data, size_t size);
235
236/*
237 * Append a tagged UDATA string, with printf-style formatting
238 */
239int ccnb_tagged_putf(struct ccn_charbuf *c, enum ccn_dtag dtag,
240 const char *fmt, ...);
241
242
243
244/***********************************
245 * Binary decoding
246 * These routines require that the whole binary object be buffered.
247 */
248
249struct ccn_buf_decoder {
250 struct ccn_skeleton_decoder decoder;
251 const unsigned char *buf;
252 size_t size;
253};
254
255struct ccn_buf_decoder *ccn_buf_decoder_start(struct ccn_buf_decoder *d,
256 const unsigned char *buf, size_t size);
257
258void ccn_buf_advance(struct ccn_buf_decoder *d);
259int ccn_buf_advance_past_element(struct ccn_buf_decoder *d);
260
261/* The match routines return a boolean - true for match */
262int ccn_buf_match_dtag(struct ccn_buf_decoder *d, enum ccn_dtag dtag);
263
264int ccn_buf_match_some_dtag(struct ccn_buf_decoder *d);
265
266int ccn_buf_match_some_blob(struct ccn_buf_decoder *d);
267int ccn_buf_match_blob(struct ccn_buf_decoder *d,
268 const unsigned char **bufp, size_t *sizep);
269
270int ccn_buf_match_udata(struct ccn_buf_decoder *d, const char *s);
271
272int ccn_buf_match_attr(struct ccn_buf_decoder *d, const char *s);
273
274/* On error, the parse routines enter an error state and return a negative value. */
275/*int ccn_parse_required_tagged_BLOB(struct ccn_buf_decoder *d,
276 enum ccn_dtag dtag,
277 int minlen, int maxlen);
278int ccn_parse_optional_tagged_BLOB(struct ccn_buf_decoder *d,
279 enum ccn_dtag dtag,
280 int minlen, int maxlen);
281int ccn_parse_nonNegativeInteger(struct ccn_buf_decoder *d);
282int ccn_parse_optional_tagged_nonNegativeInteger(struct ccn_buf_decoder *d,
283 enum ccn_dtag dtag);
284int ccn_parse_uintmax(struct ccn_buf_decoder *d, uintmax_t *result);
285int ccn_parse_tagged_string(struct ccn_buf_decoder *d,
286 enum ccn_dtag dtag, struct ccn_charbuf *store);*/
287/* check the decoder error state for these two - result can't be negative */
288/*uintmax_t ccn_parse_required_tagged_binary_number(struct ccn_buf_decoder *d,
289 enum ccn_dtag dtag,
290 int minlen, int maxlen);
291uintmax_t ccn_parse_optional_tagged_binary_number(struct ccn_buf_decoder *d,
292 enum ccn_dtag dtag,
293 int minlen, int maxlen,
294 uintmax_t default_value);*/
295
296/**
297 * Enter an error state if element closer not found.
298 */
299void ccn_buf_check_close(struct ccn_buf_decoder *d);
300
301/*
302 * ccn_ref_tagged_BLOB: Get address & size associated with blob-valued element
303 * Returns 0 for success, negative value for error.
304 */
305int ccn_ref_tagged_BLOB(enum ccn_dtag tt,
306 const unsigned char *buf,
307 size_t start, size_t stop,
308 const unsigned char **presult, size_t *psize);
309
310/*
311 * ccn_parse_Name: Parses a ccnb-encoded name
312 * components may be NULL, otherwise is filled in with Component boundary offsets
313 * Returns the number of Components in the Name, or -1 if there is an error.
314 */
315int ccn_parse_Name(struct ccn_buf_decoder *d, struct ccn_indexbuf *components);
316
317/***********************************
318 * Authenticators and signatures for content are constructed in charbufs
319 * using the following routines.
320 */
321
322enum ccn_content_type {
323 CCN_CONTENT_DATA = 0x0C04C0,
324 CCN_CONTENT_ENCR = 0x10D091,
325 CCN_CONTENT_GONE = 0x18E344,
326 CCN_CONTENT_KEY = 0x28463F,
327 CCN_CONTENT_LINK = 0x2C834A,
328 CCN_CONTENT_NACK = 0x34008A
329};
330
331
332/*********** ContentObject parsing ***********/
333/* Analogous to enum ccn_parsed_interest_offsetid, but for content */
334enum ccn_parsed_content_object_offsetid {
335 CCN_PCO_B_Signature,
336 CCN_PCO_B_DigestAlgorithm,
337 CCN_PCO_E_DigestAlgorithm,
338 CCN_PCO_B_Witness,
339 CCN_PCO_E_Witness,
340 CCN_PCO_B_SignatureBits,
341 CCN_PCO_E_SignatureBits,
342 CCN_PCO_E_Signature,
343 CCN_PCO_B_Name,
344 CCN_PCO_B_Component0,
345 CCN_PCO_E_ComponentN,
346 CCN_PCO_E_ComponentLast = CCN_PCO_E_ComponentN,
347 CCN_PCO_E_Name,
348 CCN_PCO_B_SignedInfo,
349 CCN_PCO_B_PublisherPublicKeyDigest,
350 CCN_PCO_E_PublisherPublicKeyDigest,
351 CCN_PCO_B_Timestamp,
352 CCN_PCO_E_Timestamp,
353 CCN_PCO_B_Type,
354 CCN_PCO_E_Type,
355 CCN_PCO_B_FreshnessSeconds,
356 CCN_PCO_E_FreshnessSeconds,
357 CCN_PCO_B_FinalBlockID,
358 CCN_PCO_E_FinalBlockID,
359 CCN_PCO_B_KeyLocator,
360 /* Exactly one of Key, Certificate, or KeyName will be present */
361 CCN_PCO_B_Key_Certificate_KeyName,
362 CCN_PCO_B_KeyName_Name,
363 CCN_PCO_E_KeyName_Name,
364 CCN_PCO_B_KeyName_Pub,
365 CCN_PCO_E_KeyName_Pub,
366 CCN_PCO_E_Key_Certificate_KeyName,
367 CCN_PCO_E_KeyLocator,
368 CCN_PCO_E_SignedInfo,
369 CCN_PCO_B_Content,
370 CCN_PCO_E_Content,
371 CCN_PCO_E
372};
373
374struct ccn_parsed_ContentObject {
375 int magic;
376 enum ccn_content_type type;
377 int name_ncomps;
378 unsigned short offset[CCN_PCO_E+1];
379 unsigned char digest[32]; /* Computed only when needed */
380 int digest_bytes;
381};
Ilya Moiseenko7e254b72011-08-04 19:06:10 -0700382
383int ccn_encode_ContentObject(struct ccn_charbuf *buf,
384 const struct ccn_charbuf *Name,
385 const void *data,
386 size_t size);
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -0700387#endif