void GroupManager::enumerate() { ccn_charbuf *interest_path = NULL; ccn_charbuf *templ = NULL; interest_path = ccn_charbuf_create(); if (interest_path == NULL ) { return; } ccn_name_from_uri(interest_path, BROADCAST_PREFIX); ccn_name_append_str(interest_path, confName.toLocal8Bit().constData()); ccn_name_append_str(interest_path, "speaker-list"); // update exclusive filter according to recently known remote users QHash<QString, RemoteUser *>::const_iterator it = (pGroupManager->qhRemoteUsers).constBegin(); QList<QString> toExclude; while (it != (pGroupManager->qhRemoteUsers).constEnd()) { RemoteUser *ru = it.value(); if (ru && !ru->needRefresh()) { toExclude.append(ru->getName()); } ++it; } // TODO: do not exclude local user (when staleness comes to play) // always exclude localuser by now toExclude.append(userName); expressEnumInterest(interest_path, toExclude); }
/** * * Create new face by sending out a request Interest * The actual new face instance is returned * */ static struct ccn_face_instance *create_face(struct ccn *h, struct ccn_charbuf *local_scope_template, struct ccn_charbuf *no_name, struct ccn_face_instance *face_instance) { struct ccn_charbuf *newface = NULL; struct ccn_charbuf *signed_info = NULL; struct ccn_charbuf *temp = NULL; struct ccn_charbuf *name = NULL; struct ccn_charbuf *resultbuf = NULL; struct ccn_parsed_ContentObject pcobuf = {0}; struct ccn_face_instance *new_face_instance = NULL; const unsigned char *ptr = NULL; size_t length = 0; int res = 0; /* Encode the given face instance */ newface = ccn_charbuf_create(); ccnb_append_face_instance(newface, face_instance); temp = ccn_charbuf_create(); res = ccn_sign_content(h, temp, no_name, NULL, newface->buf, newface->length); resultbuf = ccn_charbuf_create(); /* Construct the Interest name that will create the face */ name = ccn_charbuf_create(); ccn_name_init(name); ccn_name_append_str(name, "ccnx"); ccn_name_append(name, face_instance->ccnd_id, face_instance->ccnd_id_size); ccn_name_append_str(name, face_instance->action); ccn_name_append(name, temp->buf, temp->length); /* send Interest to retrieve Data that contains the newly created face */ res = ccn_get(h, name, local_scope_template, 1000, resultbuf, &pcobuf, NULL, 0); ON_ERROR_CLEANUP(res); /* decode Data to get the actual face instance */ res = ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length); ON_ERROR_CLEANUP(res); new_face_instance = ccn_face_instance_parse(ptr, length); ccn_charbuf_destroy(&newface); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&resultbuf); ccn_charbuf_destroy(&name); return new_face_instance; cleanup: ccn_charbuf_destroy(&newface); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&resultbuf); ccn_charbuf_destroy(&name); return NULL; }
void ccn_ssh_connect(char *remote_name_str) { dropbear_log(LOG_WARNING,"Enter ccn_ssh_connect"); int result; struct ccn_charbuf *remote_name; /* struct ccn_pkey *server_pkey; void *encrypted_local_name; size_t encrypted_local_name_length; */ struct ccn_charbuf *connect_template; remote_name = ccn_charbuf_create(); result = ccn_name_from_uri(remote_name,remote_name_str); if( result < 0 ) dropbear_exit("Could not parse server uri"); ccn_name_append_str(remote_name,"ssh"); ccn_name_append_str(remote_name,"client"); /* server_pkey = ssh_ccn_retrieve_public_key( cli_opts.ssh_ccn, cli_opts.remote_name_str, cli_opts.ccn_cached_keystore); if( server_pkey == NULL ) dropbear_exit("Could not get server public key"); result = ccn_pubkey_encrypt(server_pkey, cli_opts.ccnxdomain, strlen(cli_opts.ccnxdomain), &encrypted_local_name, &encrypted_local_name_length); ccn_name_append(remote_name,encrypted_local_name); */ ccn_name_append(remote_name,cli_opts.ccnxdomain,strlen(cli_opts.ccnxdomain)); dropbear_log(LOG_WARNING,"Connecting to remote:"); print_ccnb_charbuf(remote_name); connect_template = make_connect_template(); result = ccn_express_interest(cli_opts.ssh_ccn, remote_name, &newServerAction, connect_template); if( result < 0 ) dropbear_exit("Failed to express interest to server"); }
static int start_interest(struct sync_diff_data *diff_data) { static char *here = "sync_track.start_interest"; struct SyncRootStruct *root = diff_data->root; struct SyncBaseStruct *base = root->base; struct ccns_handle *ch = diff_data->client_data; struct SyncHashCacheEntry *ce = ch->next_ce; struct ccn_charbuf *prefix = SyncCopyName(diff_data->root->topoPrefix); int res = 0; struct ccn *ccn = base->sd->ccn; if (ccn == NULL) { ccn_charbuf_destroy(&prefix); return SyncNoteFailed(root, here, "bad ccn handle", __LINE__); } res |= ccn_name_append_str(prefix, "\xC1.S.ra"); res |= ccn_name_append(prefix, root->sliceHash->buf, root->sliceHash->length); if (ce != NULL) { // append the best component seen res |= ccn_name_append(prefix, ce->hash->buf, ce->hash->length); } else { // append an empty component res |= ccn_name_append(prefix, "", 0); } struct SyncNameAccum *excl = SyncExclusionsFromHashList(root, NULL, ch->hashSeen); struct ccn_charbuf *template = SyncGenInterest(NULL,
int andana_server_connect(struct andana_server *server) { int res; res = ccn_proxy_connect(server->proxy); if (res != 0) { return(res); } struct ccn_charbuf *session_namespace = ccn_charbuf_create(); ccn_charbuf_append_charbuf(session_namespace, server->proxy->filter); ccn_name_append_str(session_namespace, "CREATESESSION"); server->session_handler.p = &andana_server_session_listener; server->session_handler.data = server; res = ccn_set_interest_filter(server->proxy->handle, session_namespace, &(server->session_handler)); ccn_charbuf_destroy(&session_namespace); return(res); }
int encode_message(struct ccn_charbuf *message, struct path * name_path, char *data, size_t len, struct ccn_charbuf *signed_info, const void *pkey, const char *digest_algorithm) { struct ccn_charbuf *path = ccn_charbuf_create(); int i; int res; if (path == NULL || ccn_name_init(path) == -1) { fprintf(stderr, "Failed to allocate or initialize content path\n"); return -1; } for (i = 0; i < name_path->count; i++) { ccn_name_append_str(path, name_path->comps[i]); } res = ccn_encode_ContentObject(message, path, signed_info, data, len, digest_algorithm, pkey); if (res != 0) { fprintf(stderr, "Failed to encode ContentObject\n"); } ccn_charbuf_destroy(&path); return(res); }
static int do_ping(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) { struct ccn_ping_client *client = clienth; if (client->total >= 0 && client->sent >= client->total) return 0; struct ccn_charbuf *name = ccn_charbuf_create(); long int rnum; char rnumstr[20]; int res; ccn_charbuf_append(name, client->prefix->buf, client->prefix->length); if (client->number < 0) rnum = random(); else { rnum = client->number; client->number ++; } memset(&rnumstr, 0, 20); sprintf(rnumstr, "%ld", rnum); ccn_name_append_str(name, rnumstr); res = ccn_express_interest(client->h, name, client->closure, NULL); add_ccn_ping_entry(client, name, rnum); client->sent ++; sta.sent ++; ccn_charbuf_destroy(&name); if (res >= 0) return client->interval * 1000000; else return 0; }
/* send optional leave notification before actually leaves */ void GroupManager::sendLeaveInterest() { ccn_charbuf *interest_path = ccn_charbuf_create(); if (interest_path == NULL ) { return; } ccn_name_from_uri(interest_path, (const char*)BROADCAST_PREFIX); ccn_name_append_str(interest_path, confName.toLocal8Bit().constData()); ccn_name_append_str(interest_path, "leave"); ccn_name_append_str(interest_path, userName.toLocal8Bit().constData()); static ccn_charbuf *templ = NULL; mutex_trylock(); ccn_express_interest(ccn, interest_path, leave_closure, NULL); mutex_unlock(); ccn_charbuf_destroy(&interest_path); templ = NULL; }
int GroupManager::ccn_open() { ccn_charbuf *interest_filter_path; interest_filter_path = ccn_charbuf_create(); if (interest_filter_path == NULL ) { DPRINT("Failed to allocate or initialize interest filter path"); return -1; } ccn_name_from_uri(interest_filter_path, (const char *) BROADCAST_PREFIX); ccn_name_append_str(interest_filter_path, confName.toLocal8Bit().constData()); ccn_name_append_str(interest_filter_path, "speaker-list"); ccn_set_interest_filter(ccn, interest_filter_path, req_closure); ccn_charbuf_destroy(&interest_filter_path); ccn_charbuf *leave_filter = ccn_charbuf_create(); ccn_name_from_uri(leave_filter, (const char *) BROADCAST_PREFIX); ccn_name_append_str(leave_filter, confName.toLocal8Bit().constData()); ccn_name_append_str(leave_filter, "leave"); ccn_set_interest_filter(ccn, leave_filter, leave_closure); ccn_charbuf_destroy(&leave_filter); return 0; }
void CcnCC_registerControlPrefix(CcnCC self) { if (self->regControlPrefix) return; struct ccn_charbuf* prefix = ccn_charbuf_create(); ccn_name_from_uri(prefix, "ccnx:/ccnx/ndnld"); ccn_name_append(prefix, self->ccndid, CCNDID_length); ccn_name_append_str(prefix, "control"); struct ccn_closure* action = (struct ccn_closure*)calloc(1, sizeof(struct ccn_closure)); action->data = self; action->p = &CcnCC_controlInterest; ccn_set_interest_filter(self->ccnh, prefix, action); ccn_charbuf_destroy(&prefix); }
void CcnLAC_fetchFaceid(CcnLAC self, CCNDID ccndid) { struct ccn_charbuf* prefix = ccn_charbuf_create(); ccn_name_from_uri(prefix, "ccnx:/ccnx/ndnld"); ccn_name_append(prefix, ccndid, CCNDID_length); ccn_name_append_str(prefix, "discover-faceid"); struct ccn_forwarding_entry* fe = CcnH_buildForwardingEntry(CcnPrefixOp_selfreg, ccndid, 0, prefix); fe->flags = CCN_FORW_LAST | CCN_FORW_LOCAL; fe->lifetime = 1; CcnH_regForwardingEntry(self->ccnh, ccndid, fe, self, &CcnLAC_fetchFaceidCb); ccn_charbuf_destroy(&prefix); free(fe); }
int NdnMediaProcess::checkInterest() { int res = 0; QHash<QString,UserDataBuf *>::iterator it; ruMutex->lock(); for ( it = qhRemoteUser.begin(); it != qhRemoteUser.end(); ++it ) { if (!it.value()->interested) { /* Use a template to express our order preference for the first packet. */ struct ccn_charbuf *templ = ccn_charbuf_create(); struct ccn_charbuf *path = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); ccn_charbuf_append_tt(templ, CCN_DTAG_ChildSelector, CCN_DTAG); ccn_charbuf_append_tt(templ, 1, CCN_UDATA); ccn_charbuf_append(templ, "1", 1); /* low bit 1: rightmost */ ccn_charbuf_append_closer(templ); /*<ChildSelector>*/ ccn_charbuf_append_closer(templ); ccn_name_from_uri(path, it.key().toLocal8Bit().constData()); ccn_name_append_str(path, "audio"); if (res >= 0) { if (it.value()->data_buf.callback->p == NULL) {fprintf(stderr, "data_buf.callback is NULL!\n"); exit(1); } /* int c = 0; while (pthread_mutex_trylock(&ccn_mutex) != 0) { c++; if (c > 10000000) { fprintf(stderr, "cannot obtain lock! %s:%d\n", __FILE__, __LINE__); std::exit(1); } } */ pthread_mutex_lock(&ccn_mutex); res = ccn_express_interest(ndnState.ccn, path, it.value()->data_buf.callback, templ); pthread_mutex_unlock(&ccn_mutex); it.value()->interested = 1; fprintf(stderr, "short interest sent\n"); } if (res < 0) { fprintf(stderr, "sending the first interest failed\n"); exit(1); } ccn_charbuf_destroy(&path); ccn_charbuf_destroy(&templ); } } ruMutex->unlock(); return res; }
/** * Initiates a stream to download metadata (relations) * * @param id identifier of the ccn stream * @param pname name of the object containing the metadata * @param pmetaname name of the meta object to download * @param pipeline number of parallel requisitions for chunks */ bool CCNGet::initMetaStream(string id, string pname, string pmetaname, int pipeline) { name = ccn_charbuf_create(); name->length = 0; ccn_name_from_uri(name, pname.c_str()); templ = make_template(0,-1); ccn = ccn_create(); if (ccn_connect(ccn,NULL) == -1) return false; if (ccn_resolve_version(ccn, name, CCN_V_HIGHEST, 1000) < 1) return false; ccn_name_append(name, meta, sizeof(meta)); ccn_name_append_str(name, pmetaname.c_str()); fetch = ccn_fetch_new(ccn); stream = ccn_fetch_open(fetch, name, id.c_str(), templ, pipeline, CCN_V_HIGHEST, ASSUMEFIXED); if (stream == NULL) return false; else return true; }
struct ccn_charbuf* CcnH_signForwardingEntry(struct ccn* ccnh, CCNDID ccndid, struct ccn_forwarding_entry* fe) { struct ccn_charbuf* request = ccn_charbuf_create(); ccnb_append_forwarding_entry(request, fe); struct ccn_charbuf* emptyname = ccn_charbuf_create(); ccn_name_init(emptyname); struct ccn_charbuf* signed_request = ccn_charbuf_create(); ccn_sign_content(ccnh, signed_request, emptyname, NULL, request->buf, request->length); struct ccn_charbuf* reqname = ccn_charbuf_create(); ccn_name_from_uri(reqname, "ccnx:/ccnx"); ccn_name_append(reqname, ccndid, CCNDID_length); ccn_name_append_str(reqname, fe->action); ccn_name_append(reqname, signed_request->buf, signed_request->length); ccn_charbuf_destroy(&request); ccn_charbuf_destroy(&emptyname); ccn_charbuf_destroy(&signed_request); return reqname; }
void NdnMediaProcess::tick() { // send new interest for every speaker ruMutex->lock(); QHash<QString, UserDataBuf *>::const_iterator it = qhRemoteUser.constBegin(); while (it != qhRemoteUser.constEnd()) { QString userName = it.key(); UserDataBuf *udb = it.value(); if (udb != NULL && udb->seq >= 0) { udb->seq++; struct ccn_charbuf *pathbuf = ccn_charbuf_create(); ccn_name_from_uri(pathbuf, userName.toLocal8Bit().constData()); ccn_name_append_str(pathbuf, "audio"); struct ccn_charbuf *temp = ccn_charbuf_create(); ccn_charbuf_putf(temp, "%ld", udb->seq); ccn_name_append(pathbuf, temp->buf, temp->length); /* int c = 0; while (pthread_mutex_trylock(&ccn_mutex) != 0) { c++; if (c > 10000000) { fprintf(stderr, "cannot obtain lock! %s:%d\n", __FILE__, __LINE__); std::exit(1); } } */ pthread_mutex_lock(&ccn_mutex); int res = ccn_express_interest(ndnState.ccn, pathbuf, udb->data_buf.pipe_callback, NULL); pthread_mutex_unlock(&ccn_mutex); if (res < 0) { fprintf(stderr, "Sending interest failed at normal processor\n"); exit(1); } ccn_charbuf_destroy(&pathbuf); ccn_charbuf_destroy(&temp); } it++; } ruMutex->unlock(); }
/************************************************ * Text ************************************************/ int NdnMediaProcess::fetchNdnText() { int res = 0; QHash<QString,UserDataBuf *>::iterator it; ruMutex->lock(); for ( it = qhRemoteUser.begin(); it != qhRemoteUser.end(); ++it ) { if (!it.value()->texted) { struct ccn_charbuf *templ = ccn_charbuf_create(); struct ccn_charbuf *path = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); ccn_charbuf_append_tt(templ, CCN_DTAG_ChildSelector, CCN_DTAG); ccn_charbuf_append_tt(templ, 1, CCN_UDATA); ccn_charbuf_append(templ, "1", 1); /* low bit 1: rightmost */ ccn_charbuf_append_closer(templ); /*<ChildSelector>*/ ccn_charbuf_append_closer(templ); ccn_name_from_uri(path, it.key().toLocal8Bit().constData()); ccn_name_append_str(path, "text"); if (res >= 0) { if (it.value()->data_buf.text_callback->p == NULL) {fprintf(stderr, "data_buf.text_callback is NULL!\n"); exit(1); } pthread_mutex_lock(&ccn_mutex); res = ccn_express_interest(ndnState.ccn, path, it.value()->data_buf.text_callback, templ); pthread_mutex_unlock(&ccn_mutex); it.value()->texted = 1; fprintf(stderr, "short interest sent\n"); } if (res < 0) { fprintf(stderr, "sending the first interest failed\n"); exit(1); } ccn_charbuf_destroy(&path); ccn_charbuf_destroy(&templ); } } ruMutex->unlock(); return res; }
/** * * Bind a prefix to a face * */ static int register_unregister_prefix(struct ccn *h, struct ccn_charbuf *local_scope_template, struct ccn_charbuf *no_name, struct ccn_charbuf *name_prefix, struct ccn_face_instance *face_instance, int operation) { struct ccn_charbuf *temp = NULL; struct ccn_charbuf *resultbuf = NULL; struct ccn_charbuf *signed_info = NULL; struct ccn_charbuf *name = NULL; struct ccn_charbuf *prefixreg = NULL; struct ccn_parsed_ContentObject pcobuf = {0}; struct ccn_forwarding_entry forwarding_entry_storage = {0}; struct ccn_forwarding_entry *forwarding_entry = &forwarding_entry_storage; struct ccn_forwarding_entry *new_forwarding_entry; const unsigned char *ptr = NULL; size_t length = 0; int res; /* Register or unregister the prefix */ forwarding_entry->action = (operation == OP_REG) ? "prefixreg" : "unreg"; forwarding_entry->name_prefix = name_prefix; forwarding_entry->ccnd_id = face_instance->ccnd_id; forwarding_entry->ccnd_id_size = face_instance->ccnd_id_size; forwarding_entry->faceid = face_instance->faceid; forwarding_entry->flags = -1; forwarding_entry->lifetime = 2100; prefixreg = ccn_charbuf_create(); ccnb_append_forwarding_entry(prefixreg, forwarding_entry); temp = ccn_charbuf_create(); res = ccn_sign_content(h, temp, no_name, NULL, prefixreg->buf, prefixreg->length); resultbuf = ccn_charbuf_create(); /* construct Interest containing prefixreg request */ name = ccn_charbuf_create(); ccn_name_init(name); ccn_name_append_str(name, "ccnx"); ccn_name_append(name, face_instance->ccnd_id, face_instance->ccnd_id_size); ccn_name_append_str(name, (operation == OP_REG) ? "prefixreg" : "unreg"); ccn_name_append(name, temp->buf, temp->length); /* send Interest, get Data */ res = ccn_get(h, name, local_scope_template, 1000, resultbuf, &pcobuf, NULL, 0); ON_ERROR_CLEANUP(res); res = ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length); ON_ERROR_CLEANUP(res); /* extract new forwarding entry from Data */ new_forwarding_entry = ccn_forwarding_entry_parse(ptr, length); ON_NULL_CLEANUP(new_forwarding_entry); res = new_forwarding_entry->faceid; ccn_forwarding_entry_destroy(&new_forwarding_entry); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&resultbuf); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&prefixreg); return res; cleanup: ccn_forwarding_entry_destroy(&new_forwarding_entry); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&resultbuf); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&prefixreg); return -1; }
void GroupManager::incomingInterest(ccn_upcall_info *info) { int res; const char *data = NULL; const unsigned char *requester = NULL; const unsigned char *refresher = NULL; const unsigned char *filter = NULL; size_t filter_len = 0; ccn_charbuf *signed_info = NULL; ccn_charbuf *name = NULL; ccn_charbuf *content = NULL; RemoteUser *refreshUser = NULL; // requesterPrefix starts from index 4 to (info->interest_comps->n - 2) int nameEnd = 0; nameEnd = (info->interest_comps)->n - 2; /* construct reply data * name format: * /ndn/broadcast/conference/conference-name/speaker-list/username */ signed_info = ccn_charbuf_create(); struct ccn_charbuf *keylocator = ccn_charbuf_create(); ccn_create_keylocator(keylocator, ccn_keystore_public_key(cached_keystore)); res = ccn_signed_info_create(signed_info, /*pubkeyid*/ get_my_publisher_key_id(), /*publisher_key_id_size*/ get_my_publisher_key_id_length(), /*datetime*/ NULL, /*type*/ CCN_CONTENT_DATA, ///*freshness*/ -1, /*freshness*/ FRESHNESS, /*finalblockid*/ NULL, /*keylocator*/ keylocator); if (res < 0) { DPRINT("FAILED TO CREATE signed_info (res == %d)", res); } name = ccn_charbuf_create(); content = ccn_charbuf_create(); ccn_name_init(name); ccn_name_append_components(name, info->interest_ccnb, info->interest_comps->buf[0], info->interest_comps->buf[nameEnd + 1]); // append own username ccn_name_append_str(name, userName.toLocal8Bit().constData()); // get user list, the caller need to free the data buffer allocated int dlen = userListtoXml(&data); ccn_encode_ContentObject(content, name, signed_info, data, dlen, NULL, get_my_private_key()); // already have the lock, no need to trylock ccn_put(info->h, content->buf, content->length); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&content); if (data != NULL) { free((void *)data); data = NULL; } }
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); }
int NdnMediaProcess::sendNdnText(const char *text) { #define CHARBUF_DESTROY \ ccn_charbuf_destroy(&message);\ ccn_charbuf_destroy(&path); \ ccn_charbuf_destroy(&seq); UserDataBuf *userBuf = localUdb; if (userBuf == NULL) return -1; int res = 0; int seq_num = -1; struct ccn_charbuf *message = ccn_charbuf_create(); struct ccn_charbuf *path = ccn_charbuf_create(); struct ccn_charbuf *seq = ccn_charbuf_create(); ccn_name_init(path); seq_num = textSeq++; ccn_name_from_uri(path, localUdb->user_name.toLocal8Bit().constData()); ccn_name_append_str(path, "text"); if (seq_num < 0) { res = -1; CHARBUF_DESTROY; return res; } ccn_charbuf_putf(seq, "%ld", seq_num); ccn_name_append(path, seq->buf, seq->length); struct ccn_charbuf *signed_info = ccn_charbuf_create(); if (cached_keystore == NULL) init_cached_keystore(); ccn_charbuf *keylocator = ccn_charbuf_create(); ccn_create_keylocator(keylocator, ccn_keystore_public_key(cached_keystore)); /* Create signed_info */ res = ccn_signed_info_create(signed_info, /* pubkeyid */ get_my_publisher_key_id(), /* publisher_key_id_size */ get_my_publisher_key_id_length(), /* datetime */ NULL, /* type */ CCN_CONTENT_DATA, /* freshness */ FRESHNESS, /* finalblockid */ NULL, /* keylocator */ keylocator); if (res != 0) { fprintf(stderr, "signed_info_create failed %d (line %d)\n", res, __LINE__); } res = ccn_encode_ContentObject( /* out */ message, path, signed_info, text, strlen(text), /* keyLocator */ NULL, get_my_private_key()); if (res != 0) { fprintf(stderr, "encode_ContentObject failed %d (line %d)\n", res, __LINE__); CHARBUF_DESTROY; return res; } ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&keylocator); pthread_mutex_lock(&ccn_mutex); res = ccn_put(ndnState.ccn, message->buf, message->length); pthread_mutex_unlock(&ccn_mutex); }
int main (int argc, char *argv[]) { struct ccn_charbuf *buffer = ccn_charbuf_create(); struct ccn_charbuf *signed_info = ccn_charbuf_create(); struct ccn_skeleton_decoder dd = {0}; ssize_t res; char *outname = NULL; int fd; 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(); char *home = getenv("HOME"); char *keystore_suffix = "/.ccnx/.ccnx_keystore"; char *keystore_name = NULL; int i; if (argc == 3 && strcmp(argv[1], "-o") == 0) { outname = argv[2]; } else { printf("Usage: %s -o <outfilename>\n", argv[0]); exit(1); } if (home == NULL) { printf("Unable to determine home directory for keystore\n"); exit(1); } keystore_name = calloc(1, strlen(home) + strlen(keystore_suffix) + 1); strcat(keystore_name, home); strcat(keystore_name, keystore_suffix); if (0 != ccn_keystore_init(keystore, keystore_name, "Th1s1sn0t8g00dp8ssw0rd.")) { printf("Failed to initialize keystore\n"); exit(1); } printf("Creating signed_info\n"); 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_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"); printf("Encoding sample message data length %d\n", (int)strlen(contents[0])); cur_path = path_create(paths[0]); if (encode_message(buffer, cur_path, contents[0], strlen(contents[0]), signed_info, ccn_keystore_private_key(keystore))) { printf("Failed to encode message!\n"); } else { printf("Encoded sample message length is %d\n", (int)buffer->length); res = ccn_skeleton_decode(&dd, buffer->buf, buffer->length); if (!(res == buffer->length && dd.state == 0)) { printf("Failed to decode! Result %d State %d\n", (int)res, dd.state); result = 1; } if (outname != NULL) { fd = open(outname, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU); if (fd == -1) perror(outname); res = write(fd, buffer->buf, buffer->length); close(fd); } if (decode_message(buffer, cur_path, contents[0], strlen(contents[0]), ccn_keystore_public_key(keystore)) != 0) { result = 1; } printf("Expect signature verification failure: "); if (buffer->length >= 20) buffer->buf[buffer->length - 20] += 1; if (decode_message(buffer, cur_path, contents[0], strlen(contents[0]), ccn_keystore_public_key(keystore)) == 0) { result = 1; } } path_destroy(&cur_path); ccn_charbuf_destroy(&buffer); printf("Done with sample message\n"); /* 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_private_key(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; } path_destroy(&cur_path); ccn_charbuf_destroy(&buffer); } /* Test the uri encode / decode routines */ init_all_chars_percent_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, 1); 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, 1); 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, 1); 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, 1); 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, 1); 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); 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(); struct ccn_charbuf *co = ccn_charbuf_create(); struct ccn_signing_params sparm = CCN_SIGNING_PARAMS_INIT; struct ccn_parsed_ContentObject pco = {0}; struct ccn_charbuf *name = ccn_charbuf_create(); printf("Unit test case %d\n", i++); ccn_name_from_uri(name, "ccnx:/test/data/%00%42"); res = ccn_sign_content(h, co, name, NULL, "DATA", 4); if (res != 0) { printf("Failed: res == %d\n", (int)res); result = 1; } sparm.template_ccnb = ccn_charbuf_create(); res = ccn_parse_ContentObject(co->buf, co->length, &pco, NULL); if (res != 0) { printf("Failed: ccn_parse_ContentObject res == %d\n", (int)res); result = 1; break; } ccn_charbuf_append(sparm.template_ccnb, co->buf + pco.offset[CCN_PCO_B_SignedInfo], pco.offset[CCN_PCO_E_SignedInfo] - pco.offset[CCN_PCO_B_SignedInfo]); sparm.sp_flags = CCN_SP_TEMPL_TIMESTAMP; printf("Unit test case %d\n", i++); res = ccn_sign_content(h, co, name, &sparm, "DATA", 4); if (res != 0) { printf("Failed: res == %d\n", (int)res); result = 1; } printf("Unit test case %d\n", i++); sparm.sp_flags = -1; res = ccn_sign_content(h, co, name, &sparm, "DATA", 4); if (res != -1) { printf("Failed: res == %d\n", (int)res); result = 1; } ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&sparm.template_ccnb); ccn_charbuf_destroy(&co); ccn_destroy(&h); } while (0); 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); }
int sign_content_with_user_defined_keystore(struct ccn_charbuf *content_name, struct ccn_charbuf *resultbuf, const void *data, size_t data_size, char *keystore_path, char *keystore_passphrase, char *key_repo_name, char *site_name, char *router_name){ if ( nlsr->debugging ) printf("sign_content_with_user_defined_keystore called\n"); int res; struct ccn_charbuf * pubid_out=ccn_charbuf_create(); struct ccn_charbuf * keyname; struct ccn_keystore *keystore = NULL; keystore=ccn_keystore_create(); res=ccn_keystore_init(keystore, keystore_path,keystore_passphrase ); if ( res < 0 ){ if ( nlsr->debugging ) printf("Error in initiating keystore :(\n"); ccn_keystore_destroy(&keystore); return -1; } res=ccn_load_private_key (nlsr->ccn, keystore_path, keystore_passphrase, pubid_out); if(res < 0 ){ if ( nlsr->debugging ) printf("Error in loading keystore :( \n"); ccn_charbuf_destroy(&pubid_out); return -1; } char *baseuri=(char *)calloc(strlen(key_repo_name)+strlen(site_name)+ strlen(router_name)+strlen("/%C1.R.N.Start")+5,sizeof(char)); memcpy(baseuri,key_repo_name,strlen(key_repo_name)+1); if ( site_name[0] != '/') memcpy(baseuri+strlen(baseuri),"/",1); memcpy(baseuri+strlen(baseuri),site_name,strlen(site_name)+1); memcpy(baseuri+strlen(baseuri),"/%C1.R.N.Start",strlen("/%C1.R.N.Start")); memcpy(baseuri+strlen(baseuri),router_name,strlen(router_name)+1); baseuri[strlen(baseuri)]='\0'; keyname=ccn_charbuf_create(); if(keyname == NULL ){ ccn_charbuf_destroy(&pubid_out); free(baseuri); return -1; } ccn_name_from_uri(keyname,baseuri); if ( res < 0 ){ if ( nlsr->debugging ) printf("Bad URI format: %s\n",baseuri); ccn_charbuf_destroy(&pubid_out); ccn_charbuf_destroy(&keyname); free(baseuri); return -1; } ccn_name_append_str(keyname,"nlsr"); struct ccn_charbuf *keyid = ccn_charbuf_create(); ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1); ccn_charbuf_append_string(keyid, ".M.K"); ccn_charbuf_append_value(keyid, 0, 1); ccn_charbuf_append_charbuf(keyid, pubid_out); ccn_name_append(keyname, keyid->buf, keyid->length); struct ccn_charbuf *uri = ccn_charbuf_create(); ccn_uri_append(uri, keyname->buf, keyname->length, 0); if ( nlsr->debugging ) printf("Key Name Included when processing content: %s\n", ccn_charbuf_as_string(uri)); ccn_charbuf_destroy(&uri); struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; sp.type = CCN_CONTENT_DATA; sp.template_ccnb = ccn_charbuf_create(); ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG); ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG); ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG); ccn_charbuf_append(sp.template_ccnb, keyname->buf, keyname->length); ccn_charbuf_append_closer(sp.template_ccnb); // KeyName closer ccn_charbuf_append_closer(sp.template_ccnb); // KeyLocator closer ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR; sp.sp_flags |= CCN_SP_FINAL_BLOCK; sp.freshness = 60; if (pubid_out->length != sizeof(sp.pubid)){ if ( nlsr->debugging ) printf("Size of pubid and sp.pubid is not equal"); ccn_charbuf_destroy(&keyname); ccn_charbuf_destroy(&pubid_out); free(baseuri); return -1; } memcpy(sp.pubid, pubid_out->buf, pubid_out->length); res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size); if( res < 0 ){ if ( nlsr->debugging ) printf("Content signing error \n"); ccn_charbuf_destroy(&sp.template_ccnb); ccn_charbuf_destroy(&keyid); ccn_charbuf_destroy(&keyname); ccn_charbuf_destroy(&pubid_out); free(baseuri); return -1; } ccn_charbuf_destroy(&sp.template_ccnb); ccn_charbuf_destroy(&keyid); ccn_charbuf_destroy(&keyname); ccn_charbuf_destroy(&pubid_out); free(baseuri); return 0; }
void GroupManager::expressEnumInterest(struct ccn_charbuf *interest, QList<QString> &toExclude) { if (toExclude.size() == 0) { mutex_trylock(); int res = ccn_express_interest(ccn, interest, join_closure, NULL); mutex_unlock(); if (res < 0) { critical("express interest failed!"); } ccn_charbuf_destroy(&interest); return; } struct ccn_charbuf **excl = NULL; if (toExclude.size() > 0) { excl = new ccn_charbuf *[toExclude.size()]; for (int i = 0; i < toExclude.size(); i ++) { QString compName = toExclude.at(i); struct ccn_charbuf *comp = ccn_charbuf_create(); ccn_name_init(comp); ccn_name_append_str(comp, compName.toStdString().c_str()); excl[i] = comp; comp = NULL; } qsort(excl, toExclude.size(), sizeof(excl[0]), &namecompare); } int begin = 0; bool excludeLow = false; bool excludeHigh = true; while (begin < toExclude.size()) { if (begin != 0) { excludeLow = true; } struct ccn_charbuf *templ = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); // <Interest> ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); // <Name> ccn_charbuf_append_closer(templ); // </Name> ccn_charbuf_append_tt(templ, CCN_DTAG_Exclude, CCN_DTAG); // <Exclude> if (excludeLow) { append_bf_all(templ); } for (; begin < toExclude.size(); begin++) { struct ccn_charbuf *comp = excl[begin]; if (comp->length < 4) abort(); // we are being conservative here if (interest->length + templ->length + comp->length > 1350) { break; } ccn_charbuf_append(templ, comp->buf + 1, comp->length - 2); } if (begin == toExclude.size()) { excludeHigh = false; } if (excludeHigh) { append_bf_all(templ); } ccn_charbuf_append_closer(templ); // </Exclude> ccn_charbuf_append_closer(templ); // </Interest> mutex_trylock(); int res = ccn_express_interest(ccn, interest, join_closure, templ); mutex_unlock(); if (res < 0) { critical("express interest failed!"); } ccn_charbuf_destroy(&templ); } ccn_charbuf_destroy(&interest); for (int i = 0; i < toExclude.size(); i++) { ccn_charbuf_destroy(&excl[i]); } if (excl != NULL) { delete []excl; } }
int main(int argc, char *argv[]) { const char *progname = argv[0]; struct ccn_ping_client client = {.sent = 0, .received = 0, .total = -1, .number = -1, .interval = 1}; struct ccn_closure in_content = {.p = &incoming_content}; struct hashtb_param param = {0}; int res; struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = &handle_interrupt; sigaction(SIGINT, &sa, &osa); memset(&sta, 0, sizeof(sta)); gettimeofday(&sta.start, 0); sta.min = INT_MAX; while ((res = getopt(argc, argv, "hi:c:n:")) != -1) { switch (res) { case 'c': client.total = atol(optarg); if (client.total <= 0) usage(progname); break; case 'i': client.interval = atof(optarg); if (client.interval < PING_MIN_INTERVAL) usage(progname); break; case 'n': client.number = atol(optarg); if (client.number < 0) usage(progname); break; case 'h': default: usage(progname); break; } } if (client.number < 0) srandom(time(NULL)); argc -= optind; argv += optind; if (argv[0] == NULL) usage(progname); sta.prefix = argv[0]; client.original_prefix = argv[0]; client.prefix = ccn_charbuf_create(); res = ccn_name_from_uri(client.prefix, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad ccn URI: %s\n", progname, argv[0]); exit(1); } if (argv[1] != NULL) fprintf(stderr, "%s warning: extra arguments ignored\n", progname); //append "/ping" to the given name prefix res = ccn_name_append_str(client.prefix, PING_COMPONENT); if (res < 0) { fprintf(stderr, "%s: error constructing ccn URI: %s/%s\n", progname, argv[0], PING_COMPONENT); exit(1); } /* Connect to ccnd */ client.h = ccn_create(); if (ccn_connect(client.h, NULL) == -1) { perror("Could not connect to ccnd"); exit(1); } client.closure = &in_content; in_content.data = &client; client.ccn_ping_table = hashtb_create(sizeof(struct ccn_ping_entry), ¶m); client.sched = ccn_schedule_create(&client, &ccn_ping_ticker); client.event = ccn_schedule_event(client.sched, 0, &do_ping, NULL, 0); printf("CCNPING %s\n", client.original_prefix); res = 0; while (res >= 0 && (client.total <= 0 || client.sent < client.total || hashtb_n(client.ccn_ping_table) > 0)) { if (client.total <= 0 || client.sent < client.total) ccn_schedule_run(client.sched); res = ccn_run(client.h, 10); } ccn_schedule_destroy(&client.sched); ccn_destroy(&client.h); ccn_charbuf_destroy(&client.prefix); print_statistics(); return 0; }
// creates a full structure without action, if proto == "face" only the // faceid (from cmd_host parameter) and lifetime will be filled in. struct ccn_face_instance * parse_ccn_face_instance(struct ccndc_data *self, const char *cmd_proto, const char *cmd_host, const char *cmd_port, const char *cmd_mcastttl, const char *cmd_mcastif, int freshness) { struct ccn_face_instance *entry; struct addrinfo hints = {.ai_family = AF_UNSPEC, .ai_flags = (AI_ADDRCONFIG)}; struct addrinfo mcasthints = {.ai_family = AF_UNSPEC, .ai_flags = (AI_ADDRCONFIG | AI_NUMERICHOST)}; struct addrinfo *raddrinfo = NULL; struct addrinfo *mcastifaddrinfo = NULL; char rhostnamebuf [NI_MAXHOST]; char rhostportbuf [NI_MAXSERV]; int off_address = -1, off_port = -1, off_source_address = -1; int res; int socktype; entry = calloc(1, sizeof(*entry)); if (entry == NULL) { ccndc_warn(__LINE__, "Fatal error: memory allocation failed"); goto ExitOnError; } // allocate storage for Face data entry->store = ccn_charbuf_create(); if (entry->store == NULL) { ccndc_warn(__LINE__, "Fatal error: memory allocation failed"); goto ExitOnError; } // copy static info entry->ccnd_id = (const unsigned char *)self->ccnd_id; entry->ccnd_id_size = self->ccnd_id_size; if (cmd_proto == NULL) { ccndc_warn(__LINE__, "command error, missing address type\n"); goto ExitOnError; } if (strcasecmp(cmd_proto, "udp") == 0) { entry->descr.ipproto = IPPROTO_UDP; socktype = SOCK_DGRAM; } else if (strcasecmp(cmd_proto, "tcp") == 0) { entry->descr.ipproto = IPPROTO_TCP; socktype = SOCK_STREAM; } else if (strcasecmp(cmd_proto, "face") == 0) { errno = 0; unsigned long faceid = strtoul(cmd_host, (char **)NULL, 10); if (errno == ERANGE || errno == EINVAL || faceid > UINT_MAX || faceid == 0) { ccndc_warn(__LINE__, "command error, face number invalid or out of range '%s'\n", cmd_host); goto ExitOnError; } entry->faceid = (unsigned) faceid; entry->lifetime = freshness; return (entry); } else { ccndc_warn(__LINE__, "command error, unrecognized address type '%s'\n", cmd_proto); goto ExitOnError; } if (cmd_host == NULL) { ccndc_warn(__LINE__, "command error, missing hostname\n"); goto ExitOnError; } if (cmd_port == NULL || cmd_port[0] == 0) cmd_port = CCN_DEFAULT_UNICAST_PORT; hints.ai_socktype = socktype; res = getaddrinfo(cmd_host, cmd_port, &hints, &raddrinfo); if (res != 0 || raddrinfo == NULL) { ccndc_warn(__LINE__, "command error, getaddrinfo for host [%s] port [%s]: %s\n", cmd_host, cmd_port, gai_strerror(res)); goto ExitOnError; } res = getnameinfo(raddrinfo->ai_addr, raddrinfo->ai_addrlen, rhostnamebuf, sizeof(rhostnamebuf), rhostportbuf, sizeof(rhostportbuf), NI_NUMERICHOST | NI_NUMERICSERV); freeaddrinfo(raddrinfo); if (res != 0) { ccndc_warn(__LINE__, "command error, getnameinfo: %s\n", gai_strerror(res)); goto ExitOnError; } off_address = entry->store->length; res = ccn_charbuf_append(entry->store, rhostnamebuf, strlen(rhostnamebuf)+1); if (res != 0) { ccndc_warn(__LINE__, "Cannot append to charbuf"); goto ExitOnError; } off_port = entry->store->length; res = ccn_charbuf_append(entry->store, rhostportbuf, strlen(rhostportbuf)+1); if (res != 0) { ccndc_warn(__LINE__, "Cannot append to charbuf"); goto ExitOnError; } entry->descr.mcast_ttl = -1; if (cmd_mcastttl != NULL) { char *endptr; entry->descr.mcast_ttl = strtol(cmd_mcastttl, &endptr, 10); if ((endptr != &cmd_mcastttl[strlen(cmd_mcastttl)]) || entry->descr.mcast_ttl < 0 || entry->descr.mcast_ttl > 255) { ccndc_warn(__LINE__, "command error, invalid multicast ttl: %s\n", cmd_mcastttl); goto ExitOnError; } } if (cmd_mcastif != NULL) { res = getaddrinfo(cmd_mcastif, NULL, &mcasthints, &mcastifaddrinfo); if (res != 0) { ccndc_warn(__LINE__, "command error, incorrect multicat interface [%s]: " "mcastifaddr getaddrinfo: %s\n", cmd_mcastif, gai_strerror(res)); goto ExitOnError; } res = getnameinfo(mcastifaddrinfo->ai_addr, mcastifaddrinfo->ai_addrlen, rhostnamebuf, sizeof(rhostnamebuf), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV); freeaddrinfo(mcastifaddrinfo); if (res != 0) { ccndc_warn(__LINE__, "command error, getnameinfo: %s\n", gai_strerror(res)); goto ExitOnError; } off_source_address = entry->store->length; res = ccn_charbuf_append(entry->store, rhostnamebuf, strlen(rhostnamebuf)+1); if (res != 0) { ccndc_warn(__LINE__, "Cannot append to charbuf"); goto ExitOnError; } } entry->descr.address = (const char *)(entry->store->buf + off_address); entry->descr.port = (const char *)(entry->store->buf + off_port); if (off_source_address >= 0) { entry->descr.source_address = (const char *)(entry->store->buf + off_source_address); } entry->lifetime = freshness; return entry; ExitOnError: ccn_face_instance_destroy(&entry); return (NULL); } struct ccn_face_instance * parse_ccn_face_instance_from_face(struct ccndc_data *self, const char *cmd_faceid) { struct ccn_face_instance *entry = calloc(1, sizeof(*entry)); // allocate storage for Face data entry->store = ccn_charbuf_create(); // copy static info entry->ccnd_id = (const unsigned char *)self->ccnd_id; entry->ccnd_id_size = self->ccnd_id_size; /* destroy a face - the URI field will hold the face number */ if (cmd_faceid == NULL) { ccndc_warn(__LINE__, "command error, missing face number for destroyface\n"); goto ExitOnError; } char *endptr; int facenumber = strtol(cmd_faceid, &endptr, 10); if ((endptr != &cmd_faceid[strlen(cmd_faceid)]) || facenumber < 0) { ccndc_warn(__LINE__, "command error invalid face number for destroyface: %d\n", facenumber); goto ExitOnError; } entry->faceid = facenumber; return entry; ExitOnError: ccn_face_instance_destroy(&entry); return (NULL); } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // "private section /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// int ccndc_get_ccnd_id(struct ccndc_data *self) { struct ccn_charbuf *name = NULL; struct ccn_charbuf *resultbuf = NULL; struct ccn_parsed_ContentObject pcobuf = {0}; char ccndid_uri[] = "ccnx:/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY"; const unsigned char *ccndid_result; int res = 0; name = ccn_charbuf_create(); if (name == NULL) { ccndc_warn(__LINE__, "Unable to allocate storage for service locator name charbuf\n"); return -1; } resultbuf = ccn_charbuf_create(); if (resultbuf == NULL) { ccndc_warn(__LINE__, "Unable to allocate storage for result charbuf"); res = -1; goto Cleanup; } res = ccn_name_from_uri(name, ccndid_uri); if (res < 0) { ccndc_warn(__LINE__, "Unable to parse service locator URI for ccnd key"); goto Cleanup; } res = ccn_get(self->ccn_handle, name, self->local_scope_template, 4500, resultbuf, &pcobuf, NULL, 0); if (res < 0) { ccndc_warn(__LINE__, "Unable to get key from ccnd"); goto Cleanup; } res = ccn_ref_tagged_BLOB (CCN_DTAG_PublisherPublicKeyDigest, resultbuf->buf, pcobuf.offset[CCN_PCO_B_PublisherPublicKeyDigest], pcobuf.offset[CCN_PCO_E_PublisherPublicKeyDigest], &ccndid_result, &self->ccnd_id_size); if (res < 0) { ccndc_warn(__LINE__, "Unable to parse ccnd response for ccnd id"); goto Cleanup; } if (self->ccnd_id_size > sizeof (self->ccnd_id)) { ccndc_warn(__LINE__, "Incorrect size for ccnd id in response"); goto Cleanup; } memcpy(self->ccnd_id, ccndid_result, self->ccnd_id_size); Cleanup: ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&resultbuf); return (res); } struct ccn_face_instance * ccndc_do_face_action(struct ccndc_data *self, const char *action, struct ccn_face_instance *face_instance) { struct ccn_charbuf *newface = NULL; struct ccn_charbuf *signed_info = NULL; struct ccn_charbuf *temp = NULL; struct ccn_charbuf *name = NULL; struct ccn_charbuf *resultbuf = NULL; struct ccn_parsed_ContentObject pcobuf = {0}; struct ccn_face_instance *new_face_instance = NULL; const unsigned char *ptr = NULL; size_t length = 0; int res = 0; face_instance->action = action; /* Encode the given face instance */ newface = ccn_charbuf_create(); ON_NULL_CLEANUP(newface); ON_ERROR_CLEANUP(ccnb_append_face_instance(newface, face_instance)); temp = ccn_charbuf_create(); ON_NULL_CLEANUP(temp); res = ccn_sign_content(self->ccn_handle, temp, self->no_name, NULL, newface->buf, newface->length); ON_ERROR_CLEANUP(res); resultbuf = ccn_charbuf_create(); ON_NULL_CLEANUP(resultbuf); /* Construct the Interest name that will create the face */ name = ccn_charbuf_create(); ON_NULL_CLEANUP(name); ON_ERROR_CLEANUP(ccn_name_init(name)); ON_ERROR_CLEANUP(ccn_name_append_str(name, "ccnx")); ON_ERROR_CLEANUP(ccn_name_append(name, face_instance->ccnd_id, face_instance->ccnd_id_size)); ON_ERROR_CLEANUP(ccn_name_append_str(name, face_instance->action)); ON_ERROR_CLEANUP(ccn_name_append(name, temp->buf, temp->length)); res = ccn_get(self->ccn_handle, name, self->local_scope_template, 1000, resultbuf, &pcobuf, NULL, 0); ON_ERROR_CLEANUP(res); ON_ERROR_CLEANUP(ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length)); new_face_instance = ccn_face_instance_parse(ptr, length); ON_NULL_CLEANUP(new_face_instance); ccn_charbuf_destroy(&newface); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&resultbuf); ccn_charbuf_destroy(&name); return (new_face_instance); Cleanup: ccn_charbuf_destroy(&newface); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&resultbuf); ccn_charbuf_destroy(&name); ccn_face_instance_destroy(&new_face_instance); return (NULL); } int ccndc_do_prefix_action(struct ccndc_data *self, const char *action, struct ccn_forwarding_entry *forwarding_entry) { struct ccn_charbuf *temp = NULL; struct ccn_charbuf *resultbuf = NULL; struct ccn_charbuf *signed_info = NULL; struct ccn_charbuf *name = NULL; struct ccn_charbuf *prefixreg = NULL; struct ccn_parsed_ContentObject pcobuf = {0}; struct ccn_forwarding_entry *new_forwarding_entry = NULL; const unsigned char *ptr = NULL; size_t length = 0; int res; forwarding_entry->action = action; prefixreg = ccn_charbuf_create(); ON_NULL_CLEANUP(prefixreg); ON_ERROR_CLEANUP(ccnb_append_forwarding_entry(prefixreg, forwarding_entry)); temp = ccn_charbuf_create(); ON_NULL_CLEANUP(temp); res = ccn_sign_content(self->ccn_handle, temp, self->no_name, NULL, prefixreg->buf, prefixreg->length); ON_ERROR_CLEANUP(res); resultbuf = ccn_charbuf_create(); ON_NULL_CLEANUP(resultbuf); name = ccn_charbuf_create(); ON_ERROR_CLEANUP(ccn_name_init(name)); ON_ERROR_CLEANUP(ccn_name_append_str(name, "ccnx")); ON_ERROR_CLEANUP(ccn_name_append(name, forwarding_entry->ccnd_id, forwarding_entry->ccnd_id_size)); ON_ERROR_CLEANUP(ccn_name_append_str(name, forwarding_entry->action)); ON_ERROR_CLEANUP(ccn_name_append(name, temp->buf, temp->length)); res = ccn_get(self->ccn_handle, name, self->local_scope_template, 1000, resultbuf, &pcobuf, NULL, 0); ON_ERROR_CLEANUP(res); ON_ERROR_CLEANUP(ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length)); new_forwarding_entry = ccn_forwarding_entry_parse(ptr, length); ON_NULL_CLEANUP(new_forwarding_entry); res = new_forwarding_entry->faceid; ccn_forwarding_entry_destroy(&new_forwarding_entry); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&resultbuf); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&prefixreg); return (res); /* This is where ON_ERROR_CLEANUP sends us in case of an error * and we must free any storage we allocated before returning. */ Cleanup: ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&resultbuf); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&prefixreg); return (-1); }
int main(int argc, char** argv) { int res = 0; struct ccn* ccn_pub; struct ccn* ccn_rec; // Will hold the public/private key used for signing struct ccn_pkey* public_key = NULL; struct ccn_pkey* private_key = NULL; int complete=0; int outstanding_interests=0; // We need two ccn handles because the same handle cannot be used // to answer interests it issues. // ccn_pub = ccn_create(); if (ccn_connect(ccn_pub, NULL) == -1) { fprintf(stderr, "Could not connect to ccnd"); return(1); } ccn_rec = ccn_create(); if (ccn_connect(ccn_rec, NULL) == -1) { fprintf(stderr, "Could not connect to ccnd"); return(1); } // Closure to handle upcalls struct ccn_closure *cl = NULL; cl = (struct ccn_closure*) calloc(1, sizeof(struct ccn_closure)); cl->p = &packet_handler; handler_data h_data = { &complete, &outstanding_interests, &public_key, &ccn_pub, &ccn_rec}; cl->data = &h_data; // Setup our one test name without signature // The nonce here is just a random string, to avoid content store caching struct ccn_charbuf* name; name = ccn_charbuf_create(); ccn_name_from_uri(name, TEST_URI); ccn_name_append_nonce(name); fprintf(stderr, "Our name: %s/<nonce>\n", TEST_URI); // Set up a filter for interests in that name res = ccn_set_interest_filter(ccn_pub, name, cl); if (res < 0) { fprintf(stderr, "Failed to register interest (res == %d)\n", res); return(1); } // Get our default keys -- Why do we have to do all this work?? // Borrowed from ccn_client.c struct ccn_signing_params name_sp = CCN_SIGNING_PARAMS_INIT; struct ccn_signing_params p = CCN_SIGNING_PARAMS_INIT; struct ccn_keystore *keystore = NULL; struct ccn_charbuf *timestamp = NULL; struct ccn_charbuf *finalblockid = NULL; struct ccn_charbuf *keylocator = NULL; unsigned char* public_key_digest = NULL; size_t public_key_digest_length = 0; res = ccn_chk_signing_params(ccn_pub, &name_sp, &p, ×tamp, &finalblockid, &keylocator); if (res < 0) return(res); // For this test, use our default signing keys get_default_keys(ccn_pub, &p, &keystore, &public_key, &public_key_digest, &public_key_digest_length, &private_key); // We'll need a KeyLocator for our ContentObject // So continue borrowed code /* Construct a key locator containing the key itself */ build_keylocator_from_key(&keylocator, public_key); // And a SignedInfo too struct ccn_charbuf *signed_info = ccn_charbuf_create(); res = ccn_signed_info_create(signed_info, public_key_digest, public_key_digest_length, timestamp, p.type, p.freshness, 0, /* FinalBlockID is optional */ keylocator); // * Test using interests for a name that has one more component than our registered prefix // This is more representative of real apps... // 20-May-2011 ccn_name_append_str(name, "some stuff in a name component"); ccn_name_append_nonce(name); ccn_name_append_nonce(name); // Sign the interest struct ccn_charbuf *name_signed = ccn_charbuf_create(); sign_interest(name_signed, name, signed_info, NULL /* default digest alg */, private_key); // Express the signed interest from a different ccn handle so we get the packet res = ccn_express_interest(ccn_rec, name_signed, cl, NULL); // TODO: AnswerOriginKind could limit to signed interest? outstanding_interests++; // Express an interest with an incorrect namespace struct ccn_charbuf *name_signed_copy = ccn_charbuf_create(); ccn_charbuf_append_charbuf(name_signed_copy, name_signed); size_t k = name->length + 10; // Seek into the namespace part of the buffer name_signed_copy->buf[k] = name_signed_copy->buf[k] + 1; res = ccn_express_interest(ccn_rec, name_signed_copy, cl, NULL); // TODO: AnswerOriginKind could limit to signed interest? outstanding_interests++; // Express an interest with bogus signature name_signed_copy = ccn_charbuf_create(); ccn_charbuf_append_charbuf(name_signed_copy, name_signed); k = name->length + 30; // Seek into the signature part of the buffer name_signed_copy->buf[k] = name_signed_copy->buf[k] + 1; res = ccn_express_interest(ccn_rec, name_signed_copy, cl, NULL); // TODO: AnswerOriginKind could limit to signed interest? outstanding_interests++; if (res < 0) { fprintf(stderr, "Error expressing interest (res == %d)\n", res); } cl = NULL; // freed by ccn? while(!complete && outstanding_interests>0) { // Not sure how to handle two ccn_runs? ccn_run(ccn_rec, 100); /* stop if we run dry for .1 sec */ ccn_run(ccn_pub, 100); /* stop if we run dry for .1 sec */ fflush(stdout); } ccn_charbuf_destroy(×tamp); ccn_charbuf_destroy(&keylocator); ccn_charbuf_destroy(&finalblockid); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&name_signed); ccn_charbuf_destroy(&name_signed_copy); ccn_destroy(&ccn_pub); ccn_destroy(&ccn_rec); fflush(stderr); return(0); }
int NdnMediaProcess::ndnDataSend(const void *buf, size_t len) { #define CHARBUF_DESTROY \ ccn_charbuf_destroy(&message);\ ccn_charbuf_destroy(&path); \ ccn_charbuf_destroy(&seq); UserDataBuf *userBuf = localUdb; if (userBuf == NULL) return -1; int res = 0; int seq_num = -1; struct ccn_charbuf *message = ccn_charbuf_create(); struct ccn_charbuf *path = ccn_charbuf_create(); struct ccn_charbuf *seq = ccn_charbuf_create(); unsigned char *ccn_msg = NULL; size_t ccn_msg_size = 0; ccn_name_init(path); seq_num = localSeq++; ccn_name_from_uri(path, localUdb->user_name.toLocal8Bit().constData()); ccn_name_append_str(path, "audio"); if (seq_num < 0) { res = -1; CHARBUF_DESTROY; return res; } ccn_charbuf_putf(seq, "%ld", seq_num); ccn_name_append(path, seq->buf, seq->length); struct ccn_charbuf *signed_info = ccn_charbuf_create(); if (cached_keystore == NULL) init_cached_keystore(); ccn_charbuf *keylocator = ccn_charbuf_create(); ccn_create_keylocator(keylocator, ccn_keystore_public_key(cached_keystore)); /* Create signed_info */ res = ccn_signed_info_create(signed_info, /* pubkeyid */ get_my_publisher_key_id(), /* publisher_key_id_size */ get_my_publisher_key_id_length(), /* datetime */ NULL, /* type */ CCN_CONTENT_DATA, /* freshness */ FRESHNESS, /* finalblockid */ NULL, /* keylocator */ keylocator); if (res != 0) { fprintf(stderr, "signed_info_create failed %d (line %d)\n", res, __LINE__); } if (isPrivate) { unsigned char *enc_buf = NULL; size_t enc_len = 0; res = symEncrypt(sessionKey, NULL, (const unsigned char *)buf, len, &enc_buf, &enc_len, AES_BLOCK_SIZE); if (res != 0) { fprintf(stderr, "can not decrypt audio\n"); std::exit(1); } res = ccn_encode_ContentObject( /* out */ message, path, signed_info, enc_buf, enc_len, /* keyLocator */ NULL, get_my_private_key()); if (enc_buf != NULL) { free(enc_buf); enc_buf = NULL; } } else { res = ccn_encode_ContentObject( /* out */ message, path, signed_info, buf, len, /* keyLocator */ NULL, get_my_private_key()); } if (res != 0) { fprintf(stderr, "encode_ContentObject failed %d (line %d)\n", res, __LINE__); CHARBUF_DESTROY; return res; } ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&keylocator); ccn_msg = (unsigned char *)calloc(1, message->length); ccn_msg_size = message->length; memcpy(ccn_msg, message->buf, message->length); /* { struct ccn_parsed_ContentObject o = {0}; res = ccn_parse_ContentObject(ccn_msg, ccn_msg_size, &o, NULL); if (res < 0) { fprintf(stderr, "created bad ContentObject, res = %d\n", res); abort(); } } */ struct buf_list *p = NULL, *b = userBuf->data_buf.buflist; while (b != NULL) { p = b; b = b->link; } b = (struct buf_list*)calloc(1, sizeof(struct buf_list)); if (b == NULL) { CHARBUF_DESTROY; return -1; } if (p != NULL) p->link = b; else userBuf->data_buf.buflist = b; b->buf = ccn_msg; b->len = ccn_msg_size; b->link = NULL; CHARBUF_DESTROY; return res; }
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[]) { 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); }