/* * utility, may need to be exported, to parse the buffer into a given slice * structure. */ static int slice_parse(struct ccns_slice *s, const unsigned char *p, size_t size) { int res = 0; struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = ccn_buf_decoder_start(&decoder, p, size); uintmax_t version; int op; int start; struct ccn_charbuf *clause = NULL; if (!ccn_buf_match_dtag(d, CCN_DTAG_SyncConfigSlice)) return (-1); ccn_buf_advance(d); if (!ccn_buf_match_dtag(d, CCN_DTAG_SyncVersion)) return (-1); ccn_buf_advance(d); ccn_parse_uintmax(d, &version); ccn_buf_check_close(d); if (version != SLICE_VERSION) return (-1); start = d->decoder.token_index; if (ccn_parse_Name(d, NULL) < 0) return(-1); ccn_charbuf_reset(s->topo); res = ccn_charbuf_append(s->topo, p + start, d->decoder.token_index - start); if (res < 0) return(-1); start = d->decoder.token_index; if (ccn_parse_Name(d, NULL) < 0) return(-1); ccn_charbuf_reset(s->prefix); res = ccn_charbuf_append(s->prefix, p + start, d->decoder.token_index - start); if (res < 0) return(-1); if (!ccn_buf_match_dtag(d, CCN_DTAG_SyncConfigSliceList)) return(-1); ccn_buf_advance(d); clause = ccn_charbuf_create(); if (clause == NULL) return(-1); while (ccn_buf_match_dtag(d, CCN_DTAG_SyncConfigSliceOp)) { ccn_buf_advance(d); op = ccn_parse_nonNegativeInteger(d); // op is a small integer ccn_buf_check_close(d); if (op != 0) break; ccn_charbuf_reset(clause); start = d->decoder.token_index; if (ccn_parse_Name(d, NULL) < 0) break; res = ccn_charbuf_append(clause, p + start, d->decoder.token_index - start); ccns_slice_add_clause(s, clause); } ccn_charbuf_destroy(&clause); ccn_buf_check_close(d); /* </SyncConfigSliceList> */ ccn_buf_check_close(d); /* </SyncConfigSlice> */ if (d->decoder.index != size || !CCN_FINAL_DSTATE(d->decoder.state)) return(-1); return(0); }
/* * Utility routine to set the topo and prefix fields to copies of the * passed in charbufs */ int ccns_slice_set_topo_prefix(struct ccns_slice *s, struct ccn_charbuf *t, struct ccn_charbuf *p) { int res = 0; if (t != NULL) { ccn_charbuf_reset(s->topo); res |= ccn_charbuf_append_charbuf(s->topo, t); } if (p != NULL) { ccn_charbuf_reset(s->prefix); res |= ccn_charbuf_append_charbuf(s->prefix, p); } return(res); }
/** * Write a ccns_slice object to a repository. * @param h is the ccn_handle on which to write. * @param slice is a pointer to a ccns_slice object to be written. * @param name, if non-NULL, is a pointer to a charbuf which will be filled * in with the name of the slice that was written. * @returns 0 on success, -1 otherwise. */ int ccns_write_slice(struct ccn *h, struct ccns_slice *slice, struct ccn_charbuf *name) { struct ccn_charbuf *n = NULL; int res; // calculate versioned and segmented name for the slice n = ccn_charbuf_create(); if (n == NULL) return(-1); res = ccns_slice_name(n, slice); if (res < 0) goto Cleanup; res |= ccn_create_version(h, n, CCN_V_NOW, 0, 0); if (name != NULL) { ccn_charbuf_reset(name); res |= ccn_charbuf_append_charbuf(name, n); } res |= ccn_name_append_numeric(n, CCN_MARKER_SEQNUM, 0); if (res < 0) goto Cleanup; res = write_slice(h, slice, n); Cleanup: ccn_charbuf_destroy(&n); return (res); }
void CcnbMsg_setupEncap(CcnbMsg self, size_t size) { ccn_charbuf_reset(self); ccn_charbuf_reserve(self, size); self->length = size; memcpy(self->buf, CCN_EMPTY_PDU, CCN_EMPTY_PDU_LENGTH - 1); self->buf[self->length - 1] = CCN_EMPTY_PDU[CCN_EMPTY_PDU_LENGTH - 1]; }
void CcnbMsg_setupEncap(CcnbMsg self, size_t size) { #ifdef NDNFD assert(false); #endif ccn_charbuf_reset(self); ccn_charbuf_reserve(self, size); self->length = size; memcpy(self->buf, CCN_EMPTY_PDU, CCN_EMPTY_PDU_LENGTH - 1); self->buf[self->length - 1] = CCN_EMPTY_PDU[CCN_EMPTY_PDU_LENGTH - 1]; }
struct ccn_charbuf* SockAddr_hashkey(SockAddr self) { if (self->hashkey == NULL) self->hashkey = ccn_charbuf_create(); else ccn_charbuf_reset(self->hashkey); switch (self->addr->sa_family) { case AF_INET6: { struct sockaddr_in6* sin6 = (struct sockaddr_in6*)self->addr; ccn_charbuf_append(self->hashkey, &(sin6->sin6_addr), sizeof(struct in6_addr)); ccn_charbuf_append(self->hashkey, &(sin6->sin6_port), sizeof(in_port_t)); } break; case AF_PACKET: { struct sockaddr_ll* sll = (struct sockaddr_ll*)self->addr; ccn_charbuf_append(self->hashkey, &(sll->sll_addr), sll->sll_halen); } break; default: ccn_charbuf_append(self->hashkey, self->addr, self->addrlen); break; } return self->hashkey; }
int main(int argc, char **argv) { int opt; int res; char *prog = argv[0]; struct ccn *h; struct ccns_slice *slice; struct ccn_charbuf *prefix = ccn_charbuf_create(); struct ccn_charbuf *topo = ccn_charbuf_create(); struct ccn_charbuf *clause = ccn_charbuf_create(); struct ccn_charbuf *slice_name = ccn_charbuf_create(); struct ccn_charbuf *slice_uri = ccn_charbuf_create(); enum { CREATE = 0, DELETE = 1 } cmd = CREATE; unsigned verbose = 0; unsigned i; if (prefix == NULL || topo == NULL || clause == NULL || slice_name == NULL || slice_uri == NULL) { fprintf(stderr, "Unable to allocate required memory.\n"); exit(1); } while ((opt = getopt(argc, argv, "vh")) != -1) { switch (opt) { case 'v': verbose = 1; break; default: case 'h': usage(prog); break; } } argc -= optind; argv += optind; if (argc < 3) usage(prog); if (strcmp(argv[0], "create") == 0) cmd = CREATE; else if (strcmp(argv[0], "delete") == 0) cmd = DELETE; else usage(prog); slice = ccns_slice_create(); ccn_charbuf_reset(topo); if (0 > ccn_name_from_uri(topo, argv[1])) usage(prog); ccn_charbuf_reset(prefix); if (0 > ccn_name_from_uri(prefix, argv[2])) usage(prog); if (0 > ccns_slice_set_topo_prefix(slice, topo, prefix)) usage(prog); for (i = 3; i < argc; i++) { ccn_charbuf_reset(clause); if (0 > ccn_name_from_uri(clause, argv[i])) usage(prog); else if (0 > ccns_slice_add_clause(slice, clause)) usage(prog); } h = ccn_create(); res = ccn_connect(h, NULL); if (0 > res) { fprintf(stderr, "Unable to connect to ccnd.\n"); exit(1); } switch(cmd) { case CREATE: res = ccns_write_slice(h, slice, slice_name); break; case DELETE: ccns_slice_name(slice_name, slice); res = ccns_delete_slice(h, slice_name); break; } if (verbose || res < 0) { ccn_uri_append(slice_uri, slice_name->buf, slice_name->length, 1); printf("%s slice %s %s\n", cmd == CREATE ? "create" : "delete", ccn_charbuf_as_string(slice_uri), (res < 0) ? "failed" : "succeeded"); } ccns_slice_destroy(&slice); ccn_destroy(&h); ccn_charbuf_destroy(&prefix); ccn_charbuf_destroy(&topo); ccn_charbuf_destroy(&clause); ccn_charbuf_destroy(&slice_name); ccn_charbuf_destroy(&slice_uri); exit(res); }
int main(int argc, char **argv) { struct ccn *h = NULL; struct ccn_charbuf *name = NULL; struct ccn_charbuf *null_name = NULL; struct ccn_charbuf *name_prefix = NULL; struct ccn_charbuf *newface = NULL; struct ccn_charbuf *prefixreg = NULL; struct ccn_charbuf *resultbuf = NULL; struct ccn_charbuf *temp = NULL; struct ccn_charbuf *templ = NULL; const unsigned char *ptr = NULL; size_t length = 0; const char *arg = NULL; const char *progname = NULL; struct ccn_parsed_ContentObject pcobuf = {0}; struct ccn_face_instance face_instance_storage = {0}; struct ccn_face_instance *face_instance = &face_instance_storage; struct ccn_forwarding_entry forwarding_entry_storage = {0}; struct ccn_forwarding_entry *forwarding_entry = &forwarding_entry_storage; struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; struct ccn_charbuf *keylocator_templ = NULL; struct ccn_keystore *keystore = NULL; long expire = -1; int ipproto; unsigned char ccndid_storage[32] = {0}; const unsigned char *ccndid = NULL; size_t ccndid_size = 0; int res; int opt; progname = argv[0]; while ((opt = getopt(argc, argv, "h")) != -1) { switch (opt) { case 'h': default: usage(progname); } } /* Sanity check the URI and argument count */ arg = argv[optind]; if (arg == NULL) usage(progname); name = ccn_charbuf_create(); res = ccn_name_from_uri(name, arg); if (res < 0) { fprintf(stderr, "%s: bad ccn URI: %s\n", progname, arg); exit(1); } if (argc - optind < 3 || argc - optind > 4) usage(progname); h = ccn_create(); res = ccn_connect(h, NULL); if (res < 0) { ccn_perror(h, "ccn_connect"); exit(1); } newface = ccn_charbuf_create(); temp = ccn_charbuf_create(); templ = ccn_charbuf_create(); keylocator_templ = ccn_charbuf_create(); resultbuf = ccn_charbuf_create(); name_prefix = ccn_charbuf_create(); null_name = ccn_charbuf_create(); CHKRES(ccn_name_init(null_name)); keystore = ccn_keystore_create(); /* We need to figure out our local ccnd's CCIDID */ /* Set up our Interest template to indicate scope 1 */ ccn_charbuf_reset(templ); ccnb_element_begin(templ, CCN_DTAG_Interest); ccnb_element_begin(templ, CCN_DTAG_Name); ccnb_element_end(templ); /* </Name> */ ccnb_tagged_putf(templ, CCN_DTAG_Scope, "1"); ccnb_element_end(templ); /* </Interest> */ ccn_charbuf_reset(name); CHKRES(res = ccn_name_from_uri(name, "ccnx:/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY")); CHKRES(res = ccn_get(h, name, templ, 200, resultbuf, &pcobuf, NULL, 0)); res = ccn_ref_tagged_BLOB(CCN_DTAG_PublisherPublicKeyDigest, resultbuf->buf, pcobuf.offset[CCN_PCO_B_PublisherPublicKeyDigest], pcobuf.offset[CCN_PCO_E_PublisherPublicKeyDigest], &ccndid, &ccndid_size); CHKRES(res); if (ccndid_size > sizeof(ccndid_storage)) CHKRES(-1); memcpy(ccndid_storage, ccndid, ccndid_size); ccndid = ccndid_storage; face_instance->action = "newface"; face_instance->ccnd_id = ccndid; face_instance->ccnd_id_size = ccndid_size; if (strcmp(argv[optind + 1], "tcp") == 0) ipproto = 6; else if (strcmp(argv[optind + 1], "udp") == 0) ipproto = 17; else ipproto = atoi(argv[optind + 1]); face_instance->descr.ipproto = ipproto; // XXX - 6 = tcp or 17 = udp face_instance->descr.address = argv[optind + 2]; face_instance->descr.port = argv[optind + 3]; if (face_instance->descr.port == NULL) face_instance->descr.port = CCN_DEFAULT_UNICAST_PORT; face_instance->descr.mcast_ttl = -1; face_instance->lifetime = (~0U) >> 1; CHKRES(res = ccnb_append_face_instance(newface, face_instance)); temp->length = 0; CHKRES(ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME"))); res = ccn_keystore_init(keystore, ccn_charbuf_as_string(temp), "Th1s1sn0t8g00dp8ssw0rd."); CHKRES(res); ccnb_element_begin(keylocator_templ, CCN_DTAG_SignedInfo); ccnb_element_begin(keylocator_templ, CCN_DTAG_KeyLocator); ccnb_element_begin(keylocator_templ, CCN_DTAG_Key); CHKRES(ccn_append_pubkey_blob(keylocator_templ, ccn_keystore_public_key(keystore))); ccnb_element_end(keylocator_templ); /* </Key> */ ccnb_element_end(keylocator_templ); /* </KeyLocator> */ ccnb_element_end(keylocator_templ); /* </SignedInfo> */ sp.template_ccnb = keylocator_templ; sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR; sp.freshness = expire; ccn_charbuf_reset(temp); res = ccn_sign_content(h, temp, null_name, &sp, newface->buf, newface->length); CHKRES(res); /* Create the new face */ CHKRES(ccn_name_init(name)); CHKRES(ccn_name_append_str(name, "ccnx")); CHKRES(ccn_name_append(name, ccndid, ccndid_size)); CHKRES(ccn_name_append(name, "newface", 7)); CHKRES(ccn_name_append(name, temp->buf, temp->length)); res = ccn_get(h, name, templ, 1000, resultbuf, &pcobuf, NULL, 0); if (res < 0) { fprintf(stderr, "no response from face creation request\n"); exit(1); } ptr = resultbuf->buf; length = resultbuf->length; res = ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length); CHKRES(res); face_instance = ccn_face_instance_parse(ptr, length); if (face_instance == NULL) CHKRES(res = -1); CHKRES(face_instance->faceid); /* Finally, register the prefix */ ccn_charbuf_reset(name_prefix); CHKRES(ccn_name_from_uri(name_prefix, arg)); forwarding_entry->action = "prefixreg"; forwarding_entry->name_prefix = name_prefix; forwarding_entry->ccnd_id = ccndid; forwarding_entry->ccnd_id_size = ccndid_size; forwarding_entry->faceid = face_instance->faceid; forwarding_entry->flags = -1; /* let ccnd decide */ forwarding_entry->lifetime = (~0U) >> 1; prefixreg = ccn_charbuf_create(); CHKRES(res = ccnb_append_forwarding_entry(prefixreg, forwarding_entry)); ccn_charbuf_reset(temp); res = ccn_sign_content(h, temp, null_name, &sp, prefixreg->buf, prefixreg->length); CHKRES(res); CHKRES(ccn_name_init(name)); CHKRES(ccn_name_append_str(name, "ccnx")); CHKRES(ccn_name_append(name, ccndid, ccndid_size)); CHKRES(ccn_name_append_str(name, "prefixreg")); CHKRES(ccn_name_append(name, temp->buf, temp->length)); res = ccn_get(h, name, templ, 1000, resultbuf, &pcobuf, NULL, 0); if (res < 0) { fprintf(stderr, "no response from prefix registration request\n"); exit(1); } fprintf(stderr, "Prefix %s will be forwarded to face %d\n", arg, face_instance->faceid); /* We're about to exit, so don't bother to free everything. */ ccn_destroy(&h); exit(res < 0); }
void CcnbOR_clear(CcnbOR self) { memset(self->rd, 0, sizeof(struct ccn_skeleton_decoder)); ccn_charbuf_reset(self->cbuf); self->error = false; }
int main(int argc, char **argv) { struct ccn_keystore *keystore = NULL; int res = 0; struct ccn_charbuf *signed_info = ccn_charbuf_create(); int i; int sec, usec; char msgbuf[PAYLOAD_SIZE]; struct timeval start, end; struct ccn_charbuf *message = ccn_charbuf_create(); struct ccn_charbuf *path = ccn_charbuf_create(); struct ccn_charbuf *seq = ccn_charbuf_create(); struct ccn_charbuf *temp = ccn_charbuf_create(); keystore = ccn_keystore_create(); ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME")); res = ccn_keystore_init(keystore, ccn_charbuf_as_string(temp), "Th1s1sn0t8g00dp8ssw0rd."); if (res != 0) { printf("Failed to initialize keystore %s\n", ccn_charbuf_as_string(temp)); exit(1); } ccn_charbuf_destroy(&temp); res = ccn_signed_info_create(signed_info, /* pubkeyid */ ccn_keystore_public_key_digest(keystore), /* publisher_key_id_size */ ccn_keystore_public_key_digest_length(keystore), /* datetime */ NULL, /* type */ CCN_CONTENT_DATA, /* freshness */ FRESHNESS, /*finalblockid*/ NULL, /* keylocator */ NULL); srandom(time(NULL)); for (i=0; i<PAYLOAD_SIZE; i++) { msgbuf[i] = random(); } printf("Generating %d signed ContentObjects (one . per 100)\n", COUNT); gettimeofday(&start, NULL); for (i=0; i<COUNT; i++) { if (i>0 && (i%100) == 0) { printf("."); fflush(stdout); } ccn_name_init(path); ccn_name_append_str(path, "rtp"); ccn_name_append_str(path, "protocol"); ccn_name_append_str(path, "13.2.117.34"); ccn_name_append_str(path, "domain"); ccn_name_append_str(path, "smetters"); ccn_name_append_str(path, "principal"); ccn_name_append_str(path, "2021915340"); ccn_name_append_str(path, "id"); ccn_charbuf_putf(seq, "%u", i); ccn_name_append(path, seq->buf, seq->length); ccn_name_append_str(path, "seq"); res = ccn_encode_ContentObject(/* out */ message, path, signed_info, msgbuf, PAYLOAD_SIZE, /* digest_algorithm */ NULL, ccn_keystore_private_key(keystore)); ccn_charbuf_reset(message); ccn_charbuf_reset(path); ccn_charbuf_reset(seq); } gettimeofday(&end, NULL); sec = end.tv_sec - start.tv_sec; usec = (int)end.tv_usec - (int)start.tv_usec; while (usec < 0) { sec--; usec += 1000000; } printf("\nComplete in %d.%06d secs\n", sec, usec); return(0); }
int main(int argc, char **argv) { int opt; int res; struct ccn *h; struct ccns_slice *slice; struct ccns_handle *ccns; struct ccn_charbuf *prefix = ccn_charbuf_create(); struct ccn_charbuf *roothash = NULL; struct ccn_charbuf *topo = ccn_charbuf_create(); struct ccn_charbuf *clause = ccn_charbuf_create(); int timeout = 10*1000; unsigned i, j, n; slice = ccns_slice_create(); ccn_charbuf_reset(prefix); ccn_charbuf_reset(topo); while ((opt = getopt(argc, argv, "hf:p:r:t:w:")) != -1) { switch (opt) { case 'f': ccn_charbuf_reset(clause); if (0 > ccn_name_from_uri(clause, optarg)) usage(argv[0]); ccns_slice_add_clause(slice, clause); break; case 'p': ccn_charbuf_reset(prefix); if (0 > ccn_name_from_uri(prefix, optarg)) usage(argv[0]); break; case 'r': n = strlen(optarg); if (n == 0) { roothash = ccn_charbuf_create(); break; } if ((n % 2) != 0) usage(argv[0]); roothash = ccn_charbuf_create_n(n / 2); for (i = 0; i < (n / 2); i++) { j = (hex_value(optarg[2*i]) << 4) | hex_value(optarg[1+2*i]); ccn_charbuf_append_value(roothash, j, 1); } break; case 't': ccn_charbuf_reset(topo); if (0 > ccn_name_from_uri(topo, optarg)) usage(argv[0]); break; case 'w': timeout = atoi(optarg); if (timeout < -1) usage(argv[0]); timeout *= 1000; break; default: case 'h': usage(argv[0]); } } ccns_slice_set_topo_prefix(slice, topo, prefix); h = ccn_create(); res = ccn_connect(h, NULL); ccns = ccns_open(h, slice, &sync_cb, roothash, NULL); ccn_run(h, timeout); ccns_close(&ccns, NULL, NULL); ccns_slice_destroy(&slice); ccn_destroy(&h); exit(res); }
int main(int argc, char *argv[]) { struct ccn_charbuf *buffer; struct ccn_charbuf *signed_info = ccn_charbuf_create(); struct ccn_skeleton_decoder dd = {0}; ssize_t res; char *outname = NULL; int result = 0; char * contents[] = {"INVITE sip:[email protected] SIP/2.0\nVia: SIP/2.0/UDP 127.0.0.1:5060;rport;branch=z9hG4bK519044721\nFrom: <sip:[email protected]>;tag=2105643453\nTo: Test User <sip:[email protected]>\nCall-ID: [email protected]\nCSeq: 20 INVITE\nContact: <sip:[email protected]:5060>\nMax-Forwards: 70\nUser-Agent: Linphone-1.7.1/eXosip\nSubject: Phone call\nExpires: 120\nAllow: INVITE, ACK, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE\nContent-Type: application/sdp\nContent-Length: 448\n\nv=0\no=jthornto 123456 654321 IN IP4 127.0.0.1\ns=A conversation\nc=IN IP4 127.0.0.1\nt=0 0\nm=audio 7078 RTP/AVP 111 110 0 3 8 101\na=rtpmap:111 speex/16000/1\na=rtpmap:110 speex/8000/1\na=rtpmap:0 PCMU/8000/1\na=rtpmap:3 GSM/8000/1\na=rtpmap:8 PCMA/8000/1\na=rtpmap:101 telephone-event/8000\na=fmtp:101 0-11\nm=video 9078 RTP/AVP 97 98 99\na=rtpmap:97 theora/90000\na=rtpmap:98 H263-1998/90000\na=fmtp:98 CIF=1;QCIF=1\na=rtpmap:99 MP4V-ES/90000\n", "Quaer #%2d zjduer badone", "", NULL}; char * paths[] = { "/sip/protocol/parc.com/domain/foo/principal/invite/verb/[email protected]/id", "/d/e/f", "/zero/length/content", NULL}; struct path * cur_path = NULL; struct ccn_keystore *keystore = ccn_keystore_create(); struct ccn_keystore *aes_keystore = ccn_aes_keystore_create(); char *keystore_name = NULL; char *keystore_password = NULL; char *aes_keystore_name = NULL; unsigned char keybuf[32]; int i; while ((i = getopt(argc, argv, "a:k:p:o:")) != -1) { switch (i) { case 'a': aes_keystore_name = optarg; break; case 'k': keystore_name = optarg; break; case 'p': keystore_password = optarg; break; case 'o': outname = optarg; break; default: printf("Usage: %s [-k <keystore>] [-o <outfilename>]\n", argv[0]); exit(1); } } if (keystore_name == NULL) keystore_name = "test.keystore"; if (aes_keystore_name == NULL) aes_keystore_name = "test.aeskeystore"; if (keystore_password == NULL) keystore_password = "******"; res = ccn_keystore_init(keystore, keystore_name, keystore_password); if (res != 0) { printf ("Initializing keystore in %s\n", keystore_name); res = ccn_keystore_file_init(keystore_name, keystore_password, "ccnxuser", 0, 3650); if (res != 0) { fprintf (stderr, "Cannot create keystore [%s]", keystore_name); return res; } res = ccn_keystore_init(keystore, keystore_name, keystore_password); if (res != 0) { printf("Failed to initialize keystore\n"); exit(1); } } res = ccn_aes_keystore_init(aes_keystore, aes_keystore_name, keystore_password); if (res != 0) { printf ("Initializing AES keystore in %s\n", aes_keystore_name); ccn_generate_symmetric_key(keybuf, 256); res = ccn_aes_keystore_file_init(aes_keystore_name, keystore_password, keybuf, 256); if (res != 0) { fprintf (stderr, "Cannot create keystore [%s]", keystore_name); return res; } res = ccn_aes_keystore_init(aes_keystore, aes_keystore_name, keystore_password); if (res != 0) { printf("Failed to initialize keystore\n"); exit(1); } } printf("Creating signed_info\n"); res = ccn_signed_info_create(signed_info, /*pubkeyid*/ccn_keystore_key_digest(keystore), /*publisher_key_id_size*/ccn_keystore_key_digest_length(keystore), /*timestamp*/NULL, /*type*/CCN_CONTENT_GONE, /*freshness*/ 42, /*finalblockid*/NULL, /*keylocator*/NULL); if (res < 0) { printf("Failed to create signed_info!\n"); } res = ccn_skeleton_decode(&dd, signed_info->buf, signed_info->length); if (!(res == signed_info->length && dd.state == 0)) { printf("Failed to decode signed_info! Result %d State %d\n", (int)res, dd.state); result = 1; } memset(&dd, 0, sizeof(dd)); printf("Done with signed_info\n"); result = encode_sample_test(ccn_keystore_key(keystore), ccn_keystore_public_key(keystore), ccn_keystore_digest_algorithm(keystore), paths, contents, signed_info, outname); if (! result) { result = encode_sample_test(ccn_keystore_key(aes_keystore), ccn_keystore_key(aes_keystore), ccn_keystore_digest_algorithm(aes_keystore), paths, contents, signed_info, outname); } /* Now exercise as unit tests */ for (i = 0; paths[i] != NULL && contents[i] != NULL; i++) { printf("Unit test case %d\n", i); cur_path = path_create(paths[i]); buffer = ccn_charbuf_create(); if (encode_message(buffer, cur_path, contents[i], strlen(contents[i]), signed_info, ccn_keystore_key(keystore), ccn_keystore_digest_algorithm(keystore))) { printf("Failed encode\n"); result = 1; } else if (decode_message(buffer, cur_path, contents[i], strlen(contents[i]), ccn_keystore_public_key(keystore))) { printf("Failed decode\n"); result = 1; } ccn_charbuf_destroy(&buffer); buffer = ccn_charbuf_create(); if (encode_message(buffer, cur_path, contents[i], strlen(contents[i]), signed_info, ccn_keystore_key(aes_keystore), ccn_keystore_digest_algorithm(aes_keystore))) { printf("Failed encode\n"); result = 1; } else if (decode_message(buffer, cur_path, contents[i], strlen(contents[i]), ccn_keystore_key(aes_keystore))) { printf("Failed decode\n"); result = 1; } path_destroy(&cur_path); ccn_charbuf_destroy(&buffer); } /* Test the uri encode / decode routines */ init_all_chars_percent_encoded(); init_all_chars_mixed_encoded(); const char *uri_tests[] = { "_+4", "ccnx:/this/is/a/test", "", "ccnx:/this/is/a/test", ".+4", "../test2?x=2", "?x=2", "ccnx:/this/is/a/test2", "_-X", "../should/error", "", "", "_+2", "/missing/scheme", "", "ccnx:/missing/scheme", ".+0", "../../../../../././#/", "#/", "ccnx:/", ".+1", all_chars_percent_encoded, "", all_chars_percent_encoded_canon, "_+1", all_chars_percent_encoded_canon, "", all_chars_percent_encoded_canon, ".+4", "ccnx:/.../.%2e./...././.....///?...", "?...", "ccnx:/.../.../..../.....", "_-X", "/%3G?bad-pecent-encode", "", "", "_-X", "/%3?bad-percent-encode", "", "", "_-X", "/%#bad-percent-encode", "", "", "_+3", "ccnx://[email protected]:42/ignore/host/part of uri", "", "ccnx:/ignore/host/part%20of%20uri", NULL, NULL, NULL, NULL }; const char **u; struct ccn_charbuf *uri_out = ccn_charbuf_create(); buffer = ccn_charbuf_create(); for (u = uri_tests; *u != NULL; u += 4, i++) { printf("Unit test case %d\n", i); if (u[0][0] != '.') buffer->length = 0; res = ccn_name_from_uri(buffer, u[1]); if (!expected_res(res, u[0][1])) { printf("Failed: ccn_name_from_uri wrong res %d\n", (int)res); result = 1; } if (res >= 0) { if (res > strlen(u[1])) { printf("Failed: ccn_name_from_uri long res %d\n", (int)res); result = 1; } else if (0 != strcmp(u[1] + res, u[2])) { printf("Failed: ccn_name_from_uri expecting leftover '%s', got '%s'\n", u[2], u[1] + res); result = 1; } uri_out->length = 0; res = ccn_uri_append(uri_out, buffer->buf, buffer->length, CCN_URI_PERCENTESCAPE | CCN_URI_INCLUDESCHEME); if (!expected_res(res, u[0][2])) { printf("Failed: ccn_uri_append wrong res %d\n", (int)res); result = 1; } if (res >= 0) { if (uri_out->length != strlen(u[3])) { printf("Failed: ccn_uri_append produced wrong number of characters\n"); result = 1; } ccn_charbuf_reserve(uri_out, 1)[0] = 0; if (0 != strcmp((const char *)uri_out->buf, u[3])) { printf("Failed: ccn_uri_append produced wrong output\n"); printf("Expected: %s\n", u[3]); printf(" Actual: %s\n", (const char *)uri_out->buf); result = 1; } } } } ccn_charbuf_destroy(&buffer); ccn_charbuf_destroy(&uri_out); printf("Name marker tests\n"); do { const char *expected_uri = "ccnx:/example.com/.../%01/%FE/%01%02%03%04%05%06%07%08/%FD%10%10%10%10%1F%FF/%00%81"; const char *expected_chopped_uri = "ccnx:/example.com/.../%01/%FE"; const char *expected_bumped_uri = "ccnx:/example.com/.../%01/%FF"; const char *expected_bumped2_uri = "ccnx:/example.com/.../%01/%00%00"; printf("Unit test case %d\n", i++); buffer = ccn_charbuf_create(); uri_out = ccn_charbuf_create(); res = ccn_name_init(buffer); res |= ccn_name_append_str(buffer, "example.com"); res |= ccn_name_append_numeric(buffer, CCN_MARKER_NONE, 0); res |= ccn_name_append_numeric(buffer, CCN_MARKER_NONE, 1); res |= ccn_name_append_numeric(buffer, 0xFE, 0); res |= ccn_name_append_numeric(buffer, CCN_MARKER_NONE, 0x0102030405060708ULL); res |= ccn_name_append_numeric(buffer, CCN_MARKER_VERSION, 0x101010101FFFULL); res |= ccn_name_append_numeric(buffer, CCN_MARKER_SEQNUM, 129); res |= ccn_uri_append(uri_out, buffer->buf, buffer->length, CCN_URI_PERCENTESCAPE | CCN_URI_INCLUDESCHEME); if (res < 0) { printf("Failed: name marker tests had negative res\n"); result = 1; } if (0 != strcmp(ccn_charbuf_as_string(uri_out), expected_uri)) { printf("Failed: name marker tests produced wrong output\n"); printf("Expected: %s\n", expected_uri); printf(" Actual: %s\n", (const char *)uri_out->buf); result = 1; } res = ccn_name_chop(buffer, NULL, 100); if (res != -1) { printf("Failed: ccn_name_chop did not produce error \n"); result = 1; } res = ccn_name_chop(buffer, NULL, 4); if (res != 4) { printf("Failed: ccn_name_chop got wrong length\n"); result = 1; } uri_out->length = 0; ccn_uri_append(uri_out, buffer->buf, buffer->length, CCN_URI_INCLUDESCHEME); if (0 != strcmp(ccn_charbuf_as_string(uri_out), expected_chopped_uri)) { printf("Failed: ccn_name_chop botch\n"); printf("Expected: %s\n", expected_chopped_uri); printf(" Actual: %s\n", (const char *)uri_out->buf); result = 1; } res = ccn_name_next_sibling(buffer); if (res != 4) { printf("Failed: ccn_name_next_sibling got wrong length\n"); result = 1; } uri_out->length = 0; ccn_uri_append(uri_out, buffer->buf, buffer->length, CCN_URI_INCLUDESCHEME); if (0 != strcmp(ccn_charbuf_as_string(uri_out), expected_bumped_uri)) { printf("Failed: ccn_name_next_sibling botch\n"); printf("Expected: %s\n", expected_bumped_uri); printf(" Actual: %s\n", (const char *)uri_out->buf); result = 1; } ccn_name_next_sibling(buffer); uri_out->length = 0; ccn_uri_append(uri_out, buffer->buf, buffer->length, CCN_URI_PERCENTESCAPE | CCN_URI_INCLUDESCHEME); if (0 != strcmp(ccn_charbuf_as_string(uri_out), expected_bumped2_uri)) { printf("Failed: ccn_name_next_sibling botch\n"); printf("Expected: %s\n", expected_bumped2_uri); printf(" Actual: %s\n", (const char *)uri_out->buf); result = 1; } ccn_charbuf_destroy(&buffer); ccn_charbuf_destroy(&uri_out); } while (0); do { const char *expected_uri_mixed = "ccnx:/example.com/.../%01/%FE/=0102030405060708/=FD101010101FFF/=0081"; printf("Unit test case %d\n", i++); buffer = ccn_charbuf_create(); uri_out = ccn_charbuf_create(); res = ccn_name_init(buffer); res |= ccn_name_append_str(buffer, "example.com"); res |= ccn_name_append_numeric(buffer, CCN_MARKER_NONE, 0); res |= ccn_name_append_numeric(buffer, CCN_MARKER_NONE, 1); res |= ccn_name_append_numeric(buffer, 0xFE, 0); res |= ccn_name_append_numeric(buffer, CCN_MARKER_NONE, 0x0102030405060708ULL); res |= ccn_name_append_numeric(buffer, CCN_MARKER_VERSION, 0x101010101FFFULL); res |= ccn_name_append_numeric(buffer, CCN_MARKER_SEQNUM, 129); res |= ccn_uri_append(uri_out, buffer->buf, buffer->length, CCN_URI_MIXEDESCAPE | CCN_URI_INCLUDESCHEME); if (res < 0) { printf("Failed: name marker tests had negative res\n"); result = 1; } if (0 != strcmp(ccn_charbuf_as_string(uri_out), expected_uri_mixed)) { printf("Failed: name marker tests produced wrong output\n"); printf("Expected: %s\n", expected_uri_mixed); printf(" Actual: %s\n", (const char *)uri_out->buf); result = 1; } ccn_charbuf_destroy(&buffer); ccn_charbuf_destroy(&uri_out); } while (0); printf("Empty component encoding test\n"); do { struct ccn_charbuf *name = ccn_charbuf_create(); struct ccn_charbuf *expected_encoding = ccn_charbuf_create(); /* manually create an encoding of <Name><Component/></Name> to ensure * that the regular encoders do not create a 0-length blob as part of * the empty component. */ ccnb_element_begin(expected_encoding, CCN_DTAG_Name); ccnb_element_begin(expected_encoding, CCN_DTAG_Component); ccnb_element_end(expected_encoding); ccnb_element_end(expected_encoding); ccn_name_from_uri(name, "ccnx:/..."); if (expected_encoding->length != name->length) { printf("Failed: encoding length %u but expected %u\n", (unsigned) name->length, (unsigned)expected_encoding->length); result = 1; } for (i = 0; i < expected_encoding->length; i++) { if (expected_encoding->buf[i] != name->buf[i]) { printf("Failed: encoding mismatch at %d, got %d but expected %d\n", i, name->buf[i], expected_encoding->buf[i]); result = 1; } } } while (0); printf("Timestamp tests\n"); do { intmax_t sec; int nsec; int r; int f; struct ccn_charbuf *a[2]; int t0 = 1363899678; printf("Unit test case %d\n", i++); /* Run many increasing inputs and make sure the output is in order. */ a[0] = ccn_charbuf_create(); a[1] = ccn_charbuf_create(); ccnb_append_timestamp_blob(a[1], CCN_MARKER_NONE, t0 - 1, 0); for (f = 0, nsec = 0, sec = t0; sec < t0 + 20; nsec += 122099) { while (nsec >= 1000000000) { sec++; nsec -= 1000000000; } ccn_charbuf_reset(a[f]); r = ccnb_append_timestamp_blob(a[f], CCN_MARKER_NONE, sec, nsec); if (r != 0 || a[f]->length != 7 || memcmp(a[1-f]->buf, a[f]->buf, 6) > 0) { printf("Failed ccnb_append_timestamp_blob(...,%jd,%d)\n", sec, nsec); result = 1; } f = 1 - f; } ccn_charbuf_destroy(&a[0]); ccn_charbuf_destroy(&a[1]); } while (0); printf("Message digest tests\n"); do { printf("Unit test case %d\n", i++); struct ccn_digest *dg = ccn_digest_create(CCN_DIGEST_SHA256); if (dg == NULL) { printf("Failed: ccn_digest_create returned NULL\n"); result = 1; break; } printf("Unit test case %d\n", i++); const unsigned char expected_digest[] = { 0xb3, 0x82, 0xcd, 0xb0, 0xe9, 0x5d, 0xf7, 0x3b, 0xe7, 0xdc, 0x19, 0x81, 0x3a, 0xfd, 0xdf, 0x89, 0xfb, 0xd4, 0xd4, 0xa0, 0xdb, 0x11, 0xa6, 0xba, 0x24, 0x16, 0x5b, 0xad, 0x9d, 0x90, 0x72, 0xb0 }; unsigned char actual_digest[sizeof(expected_digest)] = {0}; const char *data = "Content-centric"; if (ccn_digest_size(dg) != sizeof(expected_digest)) { printf("Failed: wrong digest size\n"); result = 1; break; } printf("Unit test case %d\n", i++); ccn_digest_init(dg); res = ccn_digest_update(dg, data, strlen(data)); if (res != 0) printf("Warning: check res %d\n", (int)res); printf("Unit test case %d\n", i++); res = ccn_digest_final(dg, actual_digest, sizeof(expected_digest)); if (res != 0) printf("Warning: check res %d\n", (int)res); if (0 != memcmp(actual_digest, expected_digest, sizeof(expected_digest))) { printf("Failed: wrong digest\n"); result = 1; break; } } while (0); printf("Really basic PRNG test\n"); do { unsigned char r1[42]; unsigned char r2[42]; printf("Unit test case %d\n", i++); ccn_add_entropy(&i, sizeof(i), 0); /* Not much entropy, really. */ ccn_random_bytes(r1, sizeof(r1)); memcpy(r2, r1, sizeof(r2)); ccn_random_bytes(r2, sizeof(r2)); if (0 == memcmp(r1, r2, sizeof(r2))) { printf("Failed: badly broken PRNG\n"); result = 1; break; } } while (0); printf("Bloom filter tests\n"); do { unsigned char seed1[4] = "1492"; const char *a[13] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen" }; struct ccn_bloom *b1 = NULL; struct ccn_bloom *b2 = NULL; int j, k, t1, t2; unsigned short us; printf("Unit test case %d\n", i++); b1 = ccn_bloom_create(13, seed1); for (j = 0; j < 13; j++) if (ccn_bloom_match(b1, a[j], strlen(a[j]))) break; if (j < 13) { printf("Failed: \"%s\" matched empty Bloom filter\n", a[j]); result = 1; break; } printf("Unit test case %d\n", i++); for (j = 0; j < 13; j++) ccn_bloom_insert(b1, a[j], strlen(a[j])); for (j = 0; j < 13; j++) if (!ccn_bloom_match(b1, a[j], strlen(a[j]))) break; if (j < 13) { printf("Failed: \"%s\" not found when it should have been\n", a[j]); result = 1; break; } printf("Unit test case %d\n", i++); for (j = 0, k = 0; j < 13; j++) if (ccn_bloom_match(b1, a[j]+1, strlen(a[j]+1))) k++; if (k > 0) { printf("Mmm, found %d false positives\n", k); if (k > 2) { result = 1; break; } } unsigned char seed2[5] = "aqfb\0"; for (; seed2[3] <= 'f'; seed2[3]++) { printf("Unit test case %d (%4s) ", i++, seed2); b2 = ccn_bloom_create(13, seed2); for (j = 0; j < 13; j++) ccn_bloom_insert(b2, a[j], strlen(a[j])); for (j = 0, k = 0, us = ~0; us > 0; us--) { t1 = ccn_bloom_match(b1, &us, sizeof(us)); t2 = ccn_bloom_match(b2, &us, sizeof(us)); j += (t1 | t2); k += (t1 & t2); } printf("either=%d both=%d wiresize=%d\n", j, k, ccn_bloom_wiresize(b1)); if (k > 12) { printf("Failed: Bloom seeding may not be effective\n"); result = 1; } ccn_bloom_destroy(&b2); } ccn_bloom_destroy(&b1); } while (0); printf("ccn_sign_content() tests\n"); do { struct ccn *h = ccn_create(); int res; res = unit_tests_for_signing(h, &i, 0); if (res == 1) result = 1; printf("Unit test case %d\n", i++); ccn_destroy(&h); } while (0); do { struct ccn *h = ccn_create(); int res; res = ccn_load_default_key(h, aes_keystore_name, keystore_password); if (res < 0) { result = 1; printf("Failed: res == %d\n", (int)res); } else res = unit_tests_for_signing(h, &i, 1); if (res == 1) result = 1; printf("Unit test case %d\n", i++); ccn_destroy(&h); } while (0); printf("link tests\n"); printf("link tests\n"); do { struct ccn_charbuf *l = ccn_charbuf_create(); struct ccn_charbuf *name = ccn_charbuf_create(); struct ccn_parsed_Link pl = {0}; struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d; struct ccn_indexbuf *comps = ccn_indexbuf_create(); printf("Unit test case %d\n", i++); ccn_name_from_uri(name, "ccnx:/test/link/name"); ccnb_append_Link(l, name, "label", NULL); d = ccn_buf_decoder_start(&decoder, l->buf, l->length); res = ccn_parse_Link(d, &pl, comps); if (res != 3 /* components in name */) { printf("Failed: ccn_parse_Link res == %d\n", (int)res); result = 1; } } while (0); exit(result); }
/** * Create a dictionary by combining a file of key/value pairs with an existing * dictionary. * * @param dict_file the name of a file containing integer,name pairs one per line * @param d a pre-existing dictionary that will be copied in the result * @param rdp a pointer to storage into which a pointer to the result * dictionary will be stored * @result 0 if the new dictionary was created successfully, otherwise -1. */ int ccn_extend_dict(const char *dict_file, struct ccn_dict *d, struct ccn_dict **rdp) { FILE *df = NULL; int i, c; struct ccn_dict_entry *ndd = NULL; int ndc = 0; struct ccn_charbuf *enamebuf = NULL; unsigned int eindex = 0;; struct ccn_dict *nd = NULL; enum scanner_state { S_OVERFLOW = -2, S_ERROR = -1, S_INITIAL = 0, S_INDEX = 1, S_NAME = 2, S_FLUSH = 3 } s = S_INITIAL; if (rdp == NULL) return (-1); enamebuf = ccn_charbuf_create(); if (enamebuf == NULL) return (-1); df = fopen(dict_file, "r"); if (df == NULL) goto err; /* preload result with copy of supplied dictionary */ if (d) { ndd = calloc(d->count, sizeof(*(d->dict))); for (ndc = 0; ndc < d->count; ndc++) { ndd[ndc].index = d->dict[ndc].index; ndd[ndc].name = strdup(d->dict[ndc].name); } } /* parse csv format file */ while ((c = fgetc(df)) != EOF && s >= S_INITIAL) { switch (s) { case S_INITIAL: if (isdigit(c)) { s = S_INDEX; eindex = c - '0'; } else s = S_ERROR; break; case S_INDEX: if (isdigit(c)) { unsigned int teindex = eindex; eindex = 10 * eindex + (c - '0'); if (eindex < teindex) s = S_OVERFLOW; } else if (c == ',') s = S_NAME; else s = S_ERROR; break; case S_NAME: if (isalnum(c)) { ccn_charbuf_append_value(enamebuf, c, 1); } else if (c == ',' || c == '\n') { /* construct entry */ ndd = realloc(ndd, sizeof(*ndd) * (ndc + 1)); ndd[ndc].index = eindex; ndd[ndc].name = strdup(ccn_charbuf_as_string(enamebuf)); ndc++; ccn_charbuf_reset(enamebuf); s = (c == ',') ? S_FLUSH : S_INITIAL; } else s = S_ERROR; break; case S_FLUSH: if (c == '\n') s = S_INITIAL; break; default: break; } } fclose(df); df = NULL; /* handle error exit from parsing and pick up trailing entry without newline */ if (s < 0 || s == S_INDEX) goto err; else if (s == S_NAME) { ndd = realloc(ndd, sizeof(*ndd) * (ndc + 1)); ndd[ndc].index = eindex; ndd[ndc].name = strdup(ccn_charbuf_as_string(enamebuf)); ndc++; } ccn_charbuf_destroy(&enamebuf); /* check for inconsistent duplicate names, mark exact duplicates for removal */ qsort(ndd, ndc, sizeof(*ndd), qsort_compare_dict_names); for (i = 1; i < ndc; i++) { if (strcmp(ndd[i-1].name, ndd[i].name) == 0) { if (ndd[i-1].index == ndd[i].index) { free((void *)ndd[i-1].name); ndd[i-1].name = NULL; } else goto err; } } /* check for inconsistent duplicate index values, * trim the array when we reach the duplicates, marked above, * which sorted to the end. */ qsort(ndd, ndc, sizeof(*ndd), qsort_compare_dict_indices); for (i = 1; i < ndc; i++) { if (ndd[i].name == NULL) { ndc = i; ndd = realloc(ndd, sizeof(*ndd) * ndc); break; } if (ndd[i-1].index == ndd[i].index) goto err; } /* construct the final dictionary object */ nd = calloc(1, sizeof(*nd)); if (nd == NULL) goto err; nd->dict = ndd; nd->count = ndc; *rdp = nd; return (0); err: ccn_charbuf_destroy(&enamebuf); if (df != NULL) fclose(df); if (ndd != NULL) { for (ndc--; ndc >= 0; ndc--) { free((void *)ndd[ndc].name); } } return (-1); }