Ilya Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | enum ccn_marker { |
| 19 | CCN_MARKER_NONE = -1, |
| 20 | CCN_MARKER_SEQNUM = 0x00, /**< consecutive block sequence numbers */ |
| 21 | CCN_MARKER_CONTROL = 0xC1, /**< commands, etc. */ |
| 22 | CCN_MARKER_OSEQNUM = 0xF8, /**< deprecated */ |
| 23 | CCN_MARKER_BLKID = 0xFB, /**< nonconsecutive block ids */ |
| 24 | CCN_MARKER_VERSION = 0xFD /**< timestamp-based versioning */ |
| 25 | }; |
| 26 | |
| 27 | |
| 28 | /*********** Interest parsing ***********/ |
| 29 | |
| 30 | /* |
| 31 | * The parse of an interest results in an array of offsets into the |
| 32 | * wire representation, with the start and end of each major element and |
| 33 | * a few of the inportant sub-elements. The following enum allows those |
| 34 | * array items to be referred to symbolically. The *_B_* indices correspond |
| 35 | * to beginning offsets and the *_E_* indices correspond to ending offsets. |
| 36 | * An omitted element has its beginning and ending offset equal to each other. |
| 37 | * Normally these offsets will end up in non-decreasing order. |
| 38 | * Some aliasing tricks may be played here, e.g. since |
| 39 | * offset[CCN_PI_E_ComponentLast] is always equal to |
| 40 | * offset[CCN_PI_E_LastPrefixComponent], |
| 41 | * we may define CCN_PI_E_ComponentLast = CCN_PI_E_LastPrefixComponent. |
| 42 | * However, code should not rely on that, |
| 43 | * since it may change from time to time as the |
| 44 | * interest schema evolves. |
| 45 | */ |
| 46 | enum ccn_parsed_interest_offsetid { |
| 47 | CCN_PI_B_Name, |
| 48 | CCN_PI_B_Component0, |
| 49 | CCN_PI_B_LastPrefixComponent, |
| 50 | CCN_PI_E_LastPrefixComponent, |
| 51 | CCN_PI_E_ComponentLast = CCN_PI_E_LastPrefixComponent, |
| 52 | CCN_PI_E_Name, |
| 53 | CCN_PI_B_MinSuffixComponents, |
| 54 | CCN_PI_E_MinSuffixComponents, |
| 55 | CCN_PI_B_MaxSuffixComponents, |
| 56 | CCN_PI_E_MaxSuffixComponents, |
| 57 | CCN_PI_B_PublisherID, // XXX - rename |
| 58 | CCN_PI_B_PublisherIDKeyDigest, |
| 59 | CCN_PI_E_PublisherIDKeyDigest, |
| 60 | CCN_PI_E_PublisherID, |
| 61 | CCN_PI_B_Exclude, |
| 62 | CCN_PI_E_Exclude, |
| 63 | CCN_PI_B_ChildSelector, |
| 64 | CCN_PI_E_ChildSelector, |
| 65 | CCN_PI_B_AnswerOriginKind, |
| 66 | CCN_PI_E_AnswerOriginKind, |
| 67 | CCN_PI_B_Scope, |
| 68 | CCN_PI_E_Scope, |
| 69 | CCN_PI_B_InterestLifetime, |
| 70 | CCN_PI_E_InterestLifetime, |
| 71 | CCN_PI_B_Nonce, |
| 72 | CCN_PI_E_Nonce, |
| 73 | CCN_PI_B_OTHER, |
| 74 | CCN_PI_E_OTHER, |
| 75 | CCN_PI_E |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | struct ccn_parsed_interest { |
| 80 | int magic; |
| 81 | int prefix_comps; |
| 82 | int min_suffix_comps; |
| 83 | int max_suffix_comps; |
| 84 | int orderpref; |
| 85 | int answerfrom; |
| 86 | int scope; |
| 87 | unsigned short offset[CCN_PI_E+1]; |
| 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * Bitmasks for AnswerOriginKind |
| 92 | */ |
| 93 | #define CCN_AOK_CS 0x1 /* Answer from content store */ |
| 94 | #define CCN_AOK_NEW 0x2 /* OK to produce new content */ |
| 95 | #define CCN_AOK_DEFAULT (CCN_AOK_CS | CCN_AOK_NEW) |
| 96 | #define CCN_AOK_STALE 0x4 /* OK to answer with stale data */ |
| 97 | #define CCN_AOK_EXPIRE 0x10 /* Mark as stale (must have Scope 0) */ |
| 98 | |
| 99 | /*********************************** |
| 100 | * Low-level binary formatting |
| 101 | */ |
| 102 | |
| 103 | /* |
| 104 | * Append a ccnb start marker |
| 105 | * |
| 106 | * This forms the basic building block of ccnb-encoded data. |
| 107 | * c is the buffer to append to. |
| 108 | * Return value is 0, or -1 for error. |
| 109 | */ |
| 110 | int ccn_charbuf_append_tt(struct ccn_charbuf *c, size_t val, enum ccn_tt tt); |
| 111 | |
| 112 | /** |
| 113 | * Append a CCN_CLOSE |
| 114 | * |
| 115 | * Use this to close off an element in ccnb-encoded data. |
| 116 | * @param c is the buffer to append to. |
| 117 | * @returns 0 for success or -1 for error. |
| 118 | */ |
| 119 | int ccn_charbuf_append_closer(struct ccn_charbuf *c); |
| 120 | |
| 121 | /*********************************** |
| 122 | * Slightly higher level binary formatting |
| 123 | */ |
| 124 | |
| 125 | /* |
| 126 | * Append a non-negative integer as a UDATA. |
| 127 | */ |
| 128 | int ccnb_append_number(struct ccn_charbuf *c, int nni); |
| 129 | |
| 130 | /* |
| 131 | * Append a binary timestamp |
| 132 | * as a BLOB using the ccn binary Timestamp representation (12-bit fraction). |
| 133 | */ |
| 134 | int ccnb_append_timestamp_blob(struct ccn_charbuf *c, |
| 135 | enum ccn_marker marker, |
| 136 | long long secs, int nsecs); |
| 137 | |
| 138 | /* |
| 139 | * Append a binary timestamp, using the current time. |
| 140 | */ |
| 141 | int ccnb_append_now_blob(struct ccn_charbuf *c, enum ccn_marker marker); |
| 142 | |
| 143 | /* |
| 144 | * Append a start-of-element marker. |
| 145 | */ |
| 146 | int ccnb_element_begin(struct ccn_charbuf *c, enum ccn_dtag dtag); |
| 147 | |
| 148 | /* |
| 149 | * Append an end-of-element marker. |
| 150 | * This is the same as ccn_charbuf_append_closer() |
| 151 | */ |
| 152 | int ccnb_element_end(struct ccn_charbuf *c); |
| 153 | |
| 154 | /* |
| 155 | * Append a tagged BLOB |
| 156 | */ |
| 157 | int ccnb_append_tagged_blob(struct ccn_charbuf *c, enum ccn_dtag dtag, |
| 158 | const void *data, size_t size); |
| 159 | |
| 160 | /* |
| 161 | * Append a tagged UDATA string, with printf-style formatting |
| 162 | */ |
| 163 | int ccnb_tagged_putf(struct ccn_charbuf *c, enum ccn_dtag dtag, |
| 164 | const char *fmt, ...); |
| 165 | |
| 166 | |
| 167 | |
| 168 | /*********************************** |
| 169 | * Binary decoding |
| 170 | * These routines require that the whole binary object be buffered. |
| 171 | */ |
| 172 | |
| 173 | struct ccn_buf_decoder { |
| 174 | struct ccn_skeleton_decoder decoder; |
| 175 | const unsigned char *buf; |
| 176 | size_t size; |
| 177 | }; |
| 178 | |
| 179 | struct ccn_buf_decoder *ccn_buf_decoder_start(struct ccn_buf_decoder *d, |
| 180 | const unsigned char *buf, size_t size); |
| 181 | |
| 182 | void ccn_buf_advance(struct ccn_buf_decoder *d); |
| 183 | int ccn_buf_advance_past_element(struct ccn_buf_decoder *d); |
| 184 | |
| 185 | /* The match routines return a boolean - true for match */ |
| 186 | int ccn_buf_match_dtag(struct ccn_buf_decoder *d, enum ccn_dtag dtag); |
| 187 | |
| 188 | int ccn_buf_match_some_dtag(struct ccn_buf_decoder *d); |
| 189 | |
| 190 | int ccn_buf_match_some_blob(struct ccn_buf_decoder *d); |
| 191 | int ccn_buf_match_blob(struct ccn_buf_decoder *d, |
| 192 | const unsigned char **bufp, size_t *sizep); |
| 193 | |
| 194 | int ccn_buf_match_udata(struct ccn_buf_decoder *d, const char *s); |
| 195 | |
| 196 | int ccn_buf_match_attr(struct ccn_buf_decoder *d, const char *s); |
| 197 | |
| 198 | /* On error, the parse routines enter an error state and return a negative value. */ |
| 199 | /*int ccn_parse_required_tagged_BLOB(struct ccn_buf_decoder *d, |
| 200 | enum ccn_dtag dtag, |
| 201 | int minlen, int maxlen); |
| 202 | int ccn_parse_optional_tagged_BLOB(struct ccn_buf_decoder *d, |
| 203 | enum ccn_dtag dtag, |
| 204 | int minlen, int maxlen); |
| 205 | int ccn_parse_nonNegativeInteger(struct ccn_buf_decoder *d); |
| 206 | int ccn_parse_optional_tagged_nonNegativeInteger(struct ccn_buf_decoder *d, |
| 207 | enum ccn_dtag dtag); |
| 208 | int ccn_parse_uintmax(struct ccn_buf_decoder *d, uintmax_t *result); |
| 209 | int ccn_parse_tagged_string(struct ccn_buf_decoder *d, |
| 210 | enum ccn_dtag dtag, struct ccn_charbuf *store);*/ |
| 211 | /* check the decoder error state for these two - result can't be negative */ |
| 212 | /*uintmax_t ccn_parse_required_tagged_binary_number(struct ccn_buf_decoder *d, |
| 213 | enum ccn_dtag dtag, |
| 214 | int minlen, int maxlen); |
| 215 | uintmax_t ccn_parse_optional_tagged_binary_number(struct ccn_buf_decoder *d, |
| 216 | enum ccn_dtag dtag, |
| 217 | int minlen, int maxlen, |
| 218 | uintmax_t default_value);*/ |
| 219 | |
| 220 | /** |
| 221 | * Enter an error state if element closer not found. |
| 222 | */ |
| 223 | void ccn_buf_check_close(struct ccn_buf_decoder *d); |
| 224 | |
| 225 | /* |
| 226 | * ccn_ref_tagged_BLOB: Get address & size associated with blob-valued element |
| 227 | * Returns 0 for success, negative value for error. |
| 228 | */ |
| 229 | int ccn_ref_tagged_BLOB(enum ccn_dtag tt, |
| 230 | const unsigned char *buf, |
| 231 | size_t start, size_t stop, |
| 232 | const unsigned char **presult, size_t *psize); |
| 233 | |
| 234 | /* |
| 235 | * ccn_parse_Name: Parses a ccnb-encoded name |
| 236 | * components may be NULL, otherwise is filled in with Component boundary offsets |
| 237 | * Returns the number of Components in the Name, or -1 if there is an error. |
| 238 | */ |
| 239 | int ccn_parse_Name(struct ccn_buf_decoder *d, struct ccn_indexbuf *components); |
| 240 | |
| 241 | /*********************************** |
| 242 | * Authenticators and signatures for content are constructed in charbufs |
| 243 | * using the following routines. |
| 244 | */ |
| 245 | |
| 246 | enum ccn_content_type { |
| 247 | CCN_CONTENT_DATA = 0x0C04C0, |
| 248 | CCN_CONTENT_ENCR = 0x10D091, |
| 249 | CCN_CONTENT_GONE = 0x18E344, |
| 250 | CCN_CONTENT_KEY = 0x28463F, |
| 251 | CCN_CONTENT_LINK = 0x2C834A, |
| 252 | CCN_CONTENT_NACK = 0x34008A |
| 253 | }; |
| 254 | |
| 255 | |
| 256 | /*********** ContentObject parsing ***********/ |
| 257 | /* Analogous to enum ccn_parsed_interest_offsetid, but for content */ |
| 258 | enum ccn_parsed_content_object_offsetid { |
| 259 | CCN_PCO_B_Signature, |
| 260 | CCN_PCO_B_DigestAlgorithm, |
| 261 | CCN_PCO_E_DigestAlgorithm, |
| 262 | CCN_PCO_B_Witness, |
| 263 | CCN_PCO_E_Witness, |
| 264 | CCN_PCO_B_SignatureBits, |
| 265 | CCN_PCO_E_SignatureBits, |
| 266 | CCN_PCO_E_Signature, |
| 267 | CCN_PCO_B_Name, |
| 268 | CCN_PCO_B_Component0, |
| 269 | CCN_PCO_E_ComponentN, |
| 270 | CCN_PCO_E_ComponentLast = CCN_PCO_E_ComponentN, |
| 271 | CCN_PCO_E_Name, |
| 272 | CCN_PCO_B_SignedInfo, |
| 273 | CCN_PCO_B_PublisherPublicKeyDigest, |
| 274 | CCN_PCO_E_PublisherPublicKeyDigest, |
| 275 | CCN_PCO_B_Timestamp, |
| 276 | CCN_PCO_E_Timestamp, |
| 277 | CCN_PCO_B_Type, |
| 278 | CCN_PCO_E_Type, |
| 279 | CCN_PCO_B_FreshnessSeconds, |
| 280 | CCN_PCO_E_FreshnessSeconds, |
| 281 | CCN_PCO_B_FinalBlockID, |
| 282 | CCN_PCO_E_FinalBlockID, |
| 283 | CCN_PCO_B_KeyLocator, |
| 284 | /* Exactly one of Key, Certificate, or KeyName will be present */ |
| 285 | CCN_PCO_B_Key_Certificate_KeyName, |
| 286 | CCN_PCO_B_KeyName_Name, |
| 287 | CCN_PCO_E_KeyName_Name, |
| 288 | CCN_PCO_B_KeyName_Pub, |
| 289 | CCN_PCO_E_KeyName_Pub, |
| 290 | CCN_PCO_E_Key_Certificate_KeyName, |
| 291 | CCN_PCO_E_KeyLocator, |
| 292 | CCN_PCO_E_SignedInfo, |
| 293 | CCN_PCO_B_Content, |
| 294 | CCN_PCO_E_Content, |
| 295 | CCN_PCO_E |
| 296 | }; |
| 297 | |
| 298 | struct ccn_parsed_ContentObject { |
| 299 | int magic; |
| 300 | enum ccn_content_type type; |
| 301 | int name_ncomps; |
| 302 | unsigned short offset[CCN_PCO_E+1]; |
| 303 | unsigned char digest[32]; /* Computed only when needed */ |
| 304 | int digest_bytes; |
| 305 | }; |
| 306 | #endif |