struct ccndc_data * ccndc_initialize_data(void) { struct ccndc_data *self; const char *msg = "Unable to initialize ccndc"; int res; self = calloc(1, sizeof(*self)); if (self == NULL) { ON_ERROR_EXIT (-1, msg); } self->ccn_handle = ccn_create(); ON_ERROR_EXIT(ccn_connect(self->ccn_handle, NULL), "Unable to connect to local ccnd"); ON_ERROR_EXIT(ccndc_get_ccnd_id(self), "Unable to obtain ID of local ccnd"); /* Set up an Interest template to indicate scope 1 (Local) */ self->local_scope_template = ccn_charbuf_create(); res = ccnb_element_begin(self->local_scope_template, CCN_DTAG_Interest); res |= ccnb_element_begin(self->local_scope_template, CCN_DTAG_Name); res |= ccnb_element_end(self->local_scope_template); /* </Name> */ res |= ccnb_tagged_putf(self->local_scope_template, CCN_DTAG_Scope, "1"); res |= ccnb_element_end(self->local_scope_template); /* </Interest> */ ON_ERROR_EXIT(res, msg); /* Create a null name */ self->no_name = ccn_charbuf_create(); ON_ERROR_EXIT(ccn_name_init(self->no_name), msg); self->lifetime = (~0U) >> 1; return self; }
/** * Write a file named index/stable that contains the size of * repoFile1 when the repository is shut down. */ static int r_store_write_stable_point(struct ccnr_handle *h) { struct ccn_charbuf *path = NULL; struct ccn_charbuf *cb = NULL; int fd; path = ccn_charbuf_create(); cb = ccn_charbuf_create(); ccn_charbuf_putf(path, "%s/index/stable", h->directory); unlink(ccn_charbuf_as_string(path)); /* Should not exist, but just in case. */ fd = open(ccn_charbuf_as_string(path), O_CREAT | O_EXCL | O_WRONLY | O_TRUNC, 0666); if (fd == -1) { ccnr_msg(h, "cannot write stable mark %s: %s", ccn_charbuf_as_string(path), strerror(errno)); unlink(ccn_charbuf_as_string(path)); } else { ccn_charbuf_putf(cb, "%ju", (uintmax_t)(h->stable)); write(fd, cb->buf, cb->length); close(fd); if (CCNSHOULDLOG(h, dfsdf, CCNL_INFO)) ccnr_msg(h, "Index marked stable - %s", ccn_charbuf_as_string(cb)); } ccn_charbuf_destroy(&path); ccn_charbuf_destroy(&cb); return(0); }
char * get_orig_router_from_info_content_name(struct ccn_charbuf * content_name) { int start,end; start=0; struct ccn_indexbuf *comps=ccn_indexbuf_create(); ccn_name_split (content_name, comps); end=check_for_name_component_in_name(content_name,comps,"nlsr"); struct ccn_charbuf *temp=ccn_charbuf_create(); ccn_name_init(temp); ccn_name_append_components( temp, content_name->buf, comps->buf[start], comps->buf[end]); struct ccn_charbuf *temp1=ccn_charbuf_create(); ccn_uri_append(temp1, temp->buf, temp->length, 0); char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1, sizeof(char)); memcpy(orig_router,ccn_charbuf_as_string(temp1), strlen(ccn_charbuf_as_string(temp1))); orig_router[strlen(orig_router)]='\0'; ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&temp1); ccn_indexbuf_destroy(&comps); return orig_router; }
/** * * 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; }
static int add_delete_ccn_face(struct ccn *h, const char *uri, const char *address, const unsigned int p, int operation) { struct ccn_charbuf *prefix; char port[6]; struct ccn_charbuf *local_scope_template = ccn_charbuf_create(); struct ccn_charbuf *no_name = ccn_charbuf_create(); unsigned char ccndid_storage[32] = {0}; unsigned char *ccndid = ccndid_storage; size_t ccndid_size = 0; struct ccn_face_instance *fi; struct ccn_face_instance *nfi; int res; prefix = ccn_charbuf_create(); res = ccn_name_from_uri(prefix, uri); ON_ERROR_CLEANUP(res); memset(port, 0, 6); sprintf(port, "%d", p); init_data(local_scope_template, no_name); ccndid_size = get_ccndid(h, local_scope_template, ccndid); if (ccndid_size != sizeof(ccndid_storage)) { fprintf(stderr, "Incorrect size for ccnd id in response\n"); ON_ERROR_CLEANUP(-1); } /* construct a face instance for new face request */ fi = construct_face(ccndid, ccndid_size, address, port); ON_NULL_CLEANUP(fi); /* send new face request to actually create a new face */ nfi = create_face(h, local_scope_template, no_name, fi); ON_NULL_CLEANUP(nfi); res = register_unregister_prefix(h, local_scope_template, no_name, prefix, nfi, operation); ON_ERROR_CLEANUP(res); ccn_charbuf_destroy(&local_scope_template); ccn_charbuf_destroy(&no_name); ccn_face_instance_destroy(&fi); ccn_face_instance_destroy(&nfi); ccn_charbuf_destroy(&prefix); return 0; cleanup: ccn_charbuf_destroy(&prefix); ccn_charbuf_destroy(&local_scope_template); ccn_charbuf_destroy(&no_name); ccn_face_instance_destroy(&fi); ccn_face_instance_destroy(&nfi); return -1; }
int get_content_by_content_name(char *content_name, unsigned char **content_data, char *orig_router) { int ret=-1; struct ccn_charbuf *name = NULL; struct ccn_charbuf *templ = NULL; struct ccn_charbuf *resultbuf = NULL; struct ccn_parsed_ContentObject pcobuf = { 0 }; int res; int allow_stale = 1; int content_only = 1; int scope = -1; const unsigned char *ptr,*ptr_in; size_t length,length_in; int resolve_version = CCN_V_HIGHEST; int timeout_ms = 3000; const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12; unsigned lifetime_l12 = lifetime_default; int get_flags = 0; name = ccn_charbuf_create(); res = ccn_name_from_uri(name,content_name); if (res < 0) { fprintf(stderr, "Bad ccn URI: %s\n", content_name); ccn_charbuf_destroy(&name); return ret; } if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) { templ = 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); /* </Name> */ if (allow_stale) { ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG); ccnb_append_number(templ, CCN_AOK_DEFAULT | CCN_AOK_STALE); ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */ } if (scope != -1) { ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope); } if (lifetime_l12 != lifetime_default) { /* * Choose the interest lifetime so there are at least 3 * expressions (in the unsatisfied case). */ unsigned char buf[3] = { 0 }; int i; for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8) buf[i] = lifetime_l12 & 0xff; ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf)); }
int ccn_dhcp_content_parse(const unsigned char *p, size_t size, struct ccn_dhcp_entry *tail) { struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = ccn_buf_decoder_start(&decoder, p, size); int i; int count; struct ccn_dhcp_entry *de = tail; if (ccn_buf_match_dtag(d, CCN_DTAG_Entry)) { ccn_buf_advance(d); count = ccn_parse_optional_tagged_nonNegativeInteger(d, CCN_DTAG_Count); for (i = 0; i < count; i ++) { struct ccn_charbuf *store = ccn_charbuf_create(); size_t start; size_t end; int host_off = -1; int port_off = -1; de->next = calloc(1, sizeof(*de)); de = de->next; memset(de, 0, sizeof(*de)); de->store = store; de->next = NULL; if (ccn_buf_match_dtag(d, CCN_DTAG_Name)) { de->name_prefix = ccn_charbuf_create(); start = d->decoder.token_index; ccn_parse_Name(d, NULL); end = d->decoder.token_index; ccn_charbuf_append(de->name_prefix, p + start, end - start); } else de->name_prefix = NULL; host_off = ccn_parse_tagged_string(d, CCN_DTAG_Host, store); port_off = ccn_parse_tagged_string(d, CCN_DTAG_Port, store); char *b = (char *)store->buf; char *h = b + host_off; char *p = b + port_off; if (host_off >= 0) memcpy((void *)de->address, h, strlen(h)); if (port_off >= 0) memcpy((void *)de->port, p, strlen(p)); } } else d->decoder.state = -__LINE__; if (d->decoder.index != size || !CCN_FINAL_DSTATE(d->decoder.state)) ccn_dhcp_content_destroy(tail->next); return count; }
char * get_orig_router_from_lsa_name(struct ccn_charbuf * content_name) { int start=0; size_t comp_size; const unsigned char *second_last_comp; char *second_comp_type; char *sep="."; char *rem; struct ccn_indexbuf *components=ccn_indexbuf_create(); struct ccn_charbuf *name=ccn_charbuf_create(); ccn_name_from_uri(name,nlsr->slice_prefix); ccn_name_split (name, components); start=components->n-2; ccn_charbuf_destroy(&name); ccn_indexbuf_destroy(&components); struct ccn_indexbuf *comps=ccn_indexbuf_create(); ccn_name_split (content_name, comps); ccn_name_comp_get( content_name->buf, comps, comps->n-1-2, &second_last_comp, &comp_size); second_comp_type=strtok_r((char *)second_last_comp, sep, &rem); if ( strcmp( second_comp_type, "lsId" ) == 0 ){ ccn_name_chop(content_name,comps,-3); } else{ ccn_name_chop(content_name,comps,-2); } struct ccn_charbuf *temp=ccn_charbuf_create(); ccn_name_init(temp); ccn_name_append_components( temp, content_name->buf, comps->buf[start+1], comps->buf[comps->n - 1]); struct ccn_charbuf *temp1=ccn_charbuf_create(); ccn_uri_append(temp1, temp->buf, temp->length, 0); char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1, sizeof(char)); memcpy(orig_router,ccn_charbuf_as_string(temp1), strlen(ccn_charbuf_as_string(temp1))); orig_router[strlen(orig_router)]='\0'; ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&temp1); ccn_indexbuf_destroy(&comps); return orig_router; }
/** * Verify a signed interest * * params are as returned in upcall info structure * key is what should be used to verify * * returns: * -1 for parsing error * 0 for incorrect signature / unverified * 1 for proper verification * */ int verify_signed_interest(const unsigned char *ccnb, const struct ccn_indexbuf *comps, size_t num_comps, size_t start, size_t stop, struct ccn_pkey* key) { fprintf(stderr,"verifying signed interest...\n"); // What is info->interest_comps->n ? //fprintf(stderr, "Interest components %d\n", (int) info->interest_comps->n); unsigned char* comp; size_t size; int res; // Create a charbuf with the matched interest name incl nonce struct ccn_charbuf* name = ccn_charbuf_create(); ccn_name_init(name); res = ccn_name_append_components(name, ccnb, start, stop); // Last component, should be the signature res = ccn_name_comp_get(ccnb, comps, num_comps, (const unsigned char**)&comp, &size); if (memcmp(NS_SIGNATURE, comp, NS_SIGNATURE_LEN) != 0) { fprintf(stderr, "debug: Last component not tagged as a signature.\n"); return(-1); } // Parse our nameless, dataless content object that follows the namespace // and replace the name with the implicit name from the interest, so that // we can use the standard signature verification calls. Could be made // more efficient with different library calls. struct ccn_charbuf* co_with_name = ccn_charbuf_create(); unsigned char* co = &comp[NS_SIGNATURE_LEN]; replace_name(co_with_name, co, size-NS_SIGNATURE_LEN, name); //fprintf(stderr, "replace_name == %d (%s)\n", res, (res==0)?"ok":"fail"); // For now, use standard routines to verify signature struct ccn_parsed_ContentObject pco = {0}; fprintf(stderr,"verifying signed interest...2\n"); res = ccn_parse_ContentObject(co_with_name->buf, co_with_name->length, &pco, NULL); if (!res) { // Verify the signature against the authorized public key given to us, passed through to the handler res = ccn_verify_signature(co_with_name->buf, pco.offset[CCN_PCO_E], &pco, key ); } else { fprintf(stderr, "debug: Constructed content object parse failed (res==%d)\n", res); } fprintf(stderr,"verifying signed interest...3\n"); ccn_charbuf_destroy(&co_with_name); ccn_charbuf_destroy(&name); return (res); }
/** * Base loop for the background CCN task * * This is the main execution loop for the background task responsible for * interacting with the CCN network. It is from this point that many of the above methods are * called to work the inbound messages from ccnx as well as sending out the data messages. * * \param data the task context information setup by the parent sink element thread */ static void ccn_event_thread (void *data) { Gstccnxsink *me = (Gstccnxsink *) data; struct ccn_charbuf *filtName; struct ccn_charbuf *temp; int res = 0; GST_DEBUG ("CCNxSink event: *** event thread starting"); temp = ccn_charbuf_create (); filtName = ccn_charbuf_create (); /* A closure is what defines what to do when an inbound interest arrives */ if ((me->ccn_closure = calloc (1, sizeof (struct ccn_closure))) == NULL) { GST_ELEMENT_ERROR (me, RESOURCE, READ, (NULL), ("closure alloc failed")); return; } /* We setup the closure to contain the sink element context reference, and also tell it what function to call */ me->ccn_closure->data = me; me->ccn_closure->p = new_interests; me->timeouts = 0; ccn_charbuf_append (filtName, me->name->buf, me->name->length); /* This call will set up a handler for interests we expect to get from clients */ // hDump(DUMP_ADDR(filtName->buf), DUMP_SIZE(filtName->length)); ccn_set_interest_filter (me->ccn, filtName, me->ccn_closure); GST_DEBUG ("CCNxSink event: interest filter registered\n"); /* Some debugging information */ temp->length = 0; ccn_uri_append (temp, me->name->buf, me->name->length, TRUE); GST_DEBUG ("CCNxSink event: using uri: %s\n", ccn_charbuf_as_string (temp)); /* Now that the interest is registered, we loop around waiting for something to do */ /* We pass control to ccnx for a while so it can work with any incoming or outgoing data */ /* and then we check our fifo queue for work to do. That's about it! */ /* We check to see if any problems have caused our ccnd connection to fail, and we reconnect */ while (res >= 0) { GST_DEBUG ("CCNxSink event: *** looping"); res = ccn_run (me->ccn, 50); check_fifo (me); if (res < 0 && ccn_get_connection_fd (me->ccn) == -1) { GST_DEBUG ("CCNxSink event: need to reconnect..."); /* Try reconnecting, after a bit of delay */ msleep ((30 + (getpid () % 30)) * 1000); res = ccn_connect (me->ccn, ccndHost ()); } } GST_DEBUG ("CCNxSink event: *** event thread ending"); }
void NdnMediaProcess::initPipe(struct ccn_closure *selfp, struct ccn_upcall_info *info, UserDataBuf *userBuf) { fprintf(stderr, "initializing pipe\n"); // get seq const unsigned char *ccnb = info->content_ccnb; size_t ccnb_size = info->pco->offset[CCN_PCO_E]; struct ccn_indexbuf *comps = info->content_comps; long seq; const unsigned char *seqptr = NULL; char *endptr = NULL; size_t seq_size = 0; int k = comps->n - 2; if (userBuf->seq < 0) { seq = ccn_ref_tagged_BLOB(CCN_DTAG_Component, ccnb, comps->buf[k], comps->buf[k + 1], &seqptr, &seq_size); if (seq >= 0) { seq = strtol((const char *)seqptr, &endptr, 10); if (endptr != ((const char *)seqptr) + seq_size) seq = -1; } if (seq >= 0) { userBuf->seq = seq; } else { return; } } fprintf(stderr, "fetched content with seq %d\n", seq); // send hint-ahead interests for (int i = 0; i < hint_ahead; i ++) { userBuf->seq++; struct ccn_charbuf *pathbuf = ccn_charbuf_create(); ccn_name_init(pathbuf); ccn_name_append_components(pathbuf, ccnb, comps->buf[0], comps->buf[k]); struct ccn_charbuf *temp = ccn_charbuf_create(); ccn_charbuf_putf(temp, "%ld", userBuf->seq); ccn_name_append(pathbuf, temp->buf, temp->length); // no need to trylock as we already have the lock // this should use pipe callback, selfp is normal callback int res = ccn_express_interest(info->h, pathbuf, userBuf->data_buf.pipe_callback, NULL); if (res < 0) { fprintf(stderr, "Sending interest failed at normal processor\n"); std::exit(1); } ccn_charbuf_destroy(&pathbuf); ccn_charbuf_destroy(&temp); } }
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; }
struct ccn_proxy * ccn_proxy_init(const char *filter_uri, const char *prefix_uri) { struct ccn_proxy *proxy = calloc(1, sizeof(struct ccn_proxy)); struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = &decoder; int res; DEBUG_PRINT("IN %d %s\n", __LINE__, __func__); /* Convert URI to name this proxy is responsible for */ proxy->prefix = ccn_charbuf_create(); res = ccn_name_from_uri(proxy->prefix, prefix_uri); if (res < 0) { DEBUG_PRINT("ABORT %d %s bad ccn URI: %s\n", __LINE__, __func__, prefix_uri); abort(); } d = ccn_buf_decoder_start(d, proxy->prefix->buf, proxy->prefix->length); proxy->prefix_comps = ccn_indexbuf_create(); proxy->prefix_ncomps = ccn_parse_Name(d, proxy->prefix_comps); proxy->filter = ccn_charbuf_create(); res = ccn_name_from_uri(proxy->filter, filter_uri); if (res < 0) { DEBUG_PRINT("ABORT %d %s bad ccn URI: %s\n", __LINE__, __func__, filter_uri); abort(); } /* Initialization should be done by ccn_proxy_connect() */ proxy->handle_name = ccn_charbuf_create(); ccn_charbuf_append_string(proxy->handle_name, "in/outb"); DEBUG_PRINT("OUT %d %s\n", __LINE__, __func__); return(proxy); }
int sync_cb(struct ccns_handle *h, struct ccn_charbuf *lhash, struct ccn_charbuf *rhash, struct ccn_charbuf *name) { char *hexL; char *hexR; struct ccn_charbuf *uri = ccn_charbuf_create(); ccn_uri_append(uri, name->buf, name->length, 1); if (lhash == NULL || lhash->length == 0) { hexL = strdup("none"); } else hexL = hex_string(lhash->buf, lhash->length); if (rhash == NULL || rhash->length == 0) { hexR = strdup("none"); } else hexR = hex_string(rhash->buf, rhash->length); printf("%s %s %s\n", ccn_charbuf_as_string(uri), hexL, hexR); fflush(stdout); free(hexL); free(hexR); ccn_charbuf_destroy(&uri); return(0); }
/* * 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); }
/** * Produce ccnr debug output. * Output is produced via h->logger under the control of h->debug; * prepends decimal timestamp and process identification. * Caller should not supply newlines. * @param h the ccnr handle * @param fmt printf-like format string */ void ccnr_msg(struct ccnr_handle *h, const char *fmt, ...) { struct timeval t; va_list ap; struct ccn_charbuf *b; int res; if (h == NULL || h->debug == 0 || h->logger == 0) return; b = ccn_charbuf_create(); if (b == NULL) return; gettimeofday(&t, NULL); if ((h->debug >= CCNL_FINE) && ((h->logbreak-- < 0 && t.tv_sec != h->logtime) || t.tv_sec >= h->logtime + 30)) { ccn_charbuf_putf(b, "%ld.000000 ccnr[%d]: %s ____________________ %s", (long)t.tv_sec, h->logpid, h->portstr ? h->portstr : "", ctime(&t.tv_sec)); h->logtime = t.tv_sec; h->logbreak = 30; } ccn_charbuf_putf(b, "%ld.%06u ccnr[%d]: %s\n", (long)t.tv_sec, (unsigned)t.tv_usec, h->logpid, fmt); va_start(ap, fmt); /* b should already have null termination, but use call for cleanliness */ res = (*h->logger)(h->loggerdata, ccn_charbuf_as_string(b), ap); va_end(ap); ccn_charbuf_destroy(&b); /* if there's no one to hear, don't make a sound */ if (res < 0) h->debug = 0; }
/** * Make a new fdholder corresponding to the fd */ PUBLIC struct fdholder * r_io_record_fd(struct ccnr_handle *h, int fd, void *who, socklen_t wholen, int setflags) { int res; struct fdholder *fdholder = NULL; res = fcntl(fd, F_SETFL, O_NONBLOCK); if (res == -1) ccnr_msg(h, "fcntl: %s", strerror(errno)); fdholder = calloc(1, sizeof(*fdholder)); if (fdholder == NULL) return(fdholder); fdholder->name = ccn_charbuf_create(); if (fdholder->name == NULL) abort(); if (who != NULL) ccn_charbuf_append(fdholder->name, who, wholen); fdholder->filedesc = fd; init_face_flags(h, fdholder, setflags); res = r_io_enroll_face(h, fdholder); if (res == -1) { ccn_charbuf_destroy(&fdholder->name); free(fdholder); fdholder = NULL; } return(fdholder); }
static struct ccn_charbuf * ccn_charbuf_duplicate(struct ccn_charbuf *c) { struct ccn_charbuf *ans = ccn_charbuf_create(); ccn_charbuf_append(ans, c->buf, c->length); return(ans); }
/** * Produce ccnd debug output. * Output is produced via h->logger under the control of h->debug; * prepends decimal timestamp and process identification. * Caller should not supply newlines. * @param h the ccnd handle * @param fmt printf-like format string */ void ccnd_msg(struct ccnd_handle *h, const char *fmt, ...) { struct timeval t; va_list ap; struct ccn_charbuf *b; int res; const char *portstr; if (h == NULL || h->debug == 0 || h->logger == 0) return; b = ccn_charbuf_create(); gettimeofday(&t, NULL); if (((h->debug & 64) != 0) && ((h->logbreak-- < 0 && t.tv_sec != h->logtime) || t.tv_sec >= h->logtime + 30)) { portstr = h->portstr; if (portstr == NULL) portstr = ""; ccn_charbuf_putf(b, "%ld.000000 ccnd[%d]: %s ____________________ %s", (long)t.tv_sec, h->logpid, h->portstr, ctime(&t.tv_sec)); h->logtime = t.tv_sec; h->logbreak = 30; } ccn_charbuf_putf(b, "%ld.%06u ccnd[%d]: %s\n", (long)t.tv_sec, (unsigned)t.tv_usec, h->logpid, fmt); va_start(ap, fmt); res = (*h->logger)(h->loggerdata, (const char *)b->buf, ap); va_end(ap); ccn_charbuf_destroy(&b); /* if there's no one to hear, don't make a sound */ if (res < 0) h->debug = 0; }
static int post_face_notice(struct ccnr_handle *ccnr, unsigned filedesc) { struct fdholder *fdholder = ccnr_r_io_fdholder_from_fd(ccnr, filedesc); struct ccn_charbuf *msg = ccn_charbuf_create(); int res = -1; int port; // XXX - text version for trying out stream stuff - replace with ccnb if (fdholder == NULL) ccn_charbuf_putf(msg, "destroyface(%u);\n", filedesc); else { ccn_charbuf_putf(msg, "newface(%u, 0x%x", filedesc, fdholder->flags); if (fdholder->name->length != 0 && (fdholder->flags & (CCNR_FACE_INET | CCNR_FACE_INET6)) != 0) { ccn_charbuf_putf(msg, ", "); port = ccn_charbuf_append_sockaddr(msg, (struct sockaddr *)fdholder->name->buf); if (port < 0) msg->length--; else if (port > 0) ccn_charbuf_putf(msg, ":%d", port); } ccn_charbuf_putf(msg, ");\n", filedesc); } res = ccn_seqw_write(ccnr->notice, msg->buf, msg->length); ccn_charbuf_destroy(&msg); return(res); }
PyObject * CCNObject_New_charbuf(enum _pyccn_capsules type, struct ccn_charbuf **charbuf) { struct ccn_charbuf *p; PyObject *py_o; assert(type == CONTENT_OBJECT || type == EXCLUSION_FILTER || type == INTEREST || type == KEY_LOCATOR || type == NAME || type == SIGNATURE || type == SIGNED_INFO); p = ccn_charbuf_create(); if (!p) return PyErr_NoMemory(); py_o = CCNObject_New(type, p); if (!py_o) { ccn_charbuf_destroy(&p); return NULL; } if (charbuf) *charbuf = p; return py_o; }
struct ccn_charbuf* CcnbOR_read(CcnbOR self) { if (NBS_error(self->nbs)) { self->error = true; return NULL; } uint8_t* buf = (uint8_t*)malloc(2048); size_t readSize = NBS_read(self->nbs, buf, 2048, NULL); if (readSize == 0) { free(buf); return NULL; } ssize_t consumeSize = ccn_skeleton_decode(self->rd, buf, readSize); if (self->rd->state < 0) self->error = true; else ccn_charbuf_append(self->cbuf, buf, consumeSize); if (consumeSize < readSize) NBS_pushback(self->nbs, buf, consumeSize, readSize - consumeSize, NULL); else free(buf); if (CCN_FINAL_DSTATE(self->rd->state)) { struct ccn_charbuf* cbuf = self->cbuf; self->cbuf = ccn_charbuf_create(); CcnbOR_clear(self); return cbuf; } return NULL; }
void CcnCC_sendContent(CcnCC self, struct ccn_charbuf* name, TimeSpan expires, void* data, size_t size) { struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; if (expires >= 0) { sp.template_ccnb = ccn_charbuf_create(); ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG); ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", expires / 1000); sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS; ccn_charbuf_append_closer(sp.template_ccnb); } struct ccn_charbuf* content = ccn_charbuf_create(); if (0 == ccn_sign_content(self->ccnh, content, name, &sp, data, size)) { ccn_put(self->ccnh, content->buf, content->length); } ccn_charbuf_destroy(&sp.template_ccnb); ccn_charbuf_destroy(&content); }
int gen_test(struct ccn_charbuf **name, struct ccn_indexbuf **comps) { int res; intmax_t secs = 1234567890; int nsecs = 6000000; *name = ccn_charbuf_create(); ccn_name_init(*name); res = ccn_create_version(NULL, *name, 0, secs, nsecs); if (res < 0) { printf("Unable to create version\n"); return(-__LINE__); } *comps = ccn_indexbuf_create(); struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = &decoder; ccn_buf_decoder_start(d, (*name)->buf, (*name)->length); res = ccn_parse_Name(d, *comps); if (res < 0) { printf("Unable to parse name\n"); return(-__LINE__); } return(0); }
int main() { struct ccn *ccn = NULL; struct ccn_charbuf *name = NULL; int res; struct ccn_closure *incoming = NULL; ccn = ccn_create(); if (ccn_connect(ccn, NULL) == -1) { perror("Could not connect to ccnd"); exit(1); } incoming = calloc(1, sizeof(*incoming)); incoming->p = &incoming_content; name = ccn_charbuf_create(); res = ccn_name_from_uri(name, "ccnx:/FOX/test"); //ccn_express_persistent_interest(ccn, name, incoming, NULL, 1); ccn_express_interest(ccn, name, incoming, NULL); printf("Interests send\n", res); res = ccn_run(ccn, 10000); return 0; }
static void ccn_publish_client_mountpoint() { dropbear_log(LOG_WARNING,"Enter ccn_publish_client_mountpoint"); int result; struct ccn_charbuf *mountpoint; char client_id_str[6]; char *client_name_str = NULL; mountpoint = ccn_charbuf_create(); if( mountpoint == NULL ) dropbear_exit("Failed to allocate client mountpoint charbuf"); client_name_str = strdup((const char*)cli_opts.ccnxdomain); strcat(client_name_str,"/ssh/"); sprintf(client_id_str,"%6d",rand()); strcat(client_name_str,client_id_str); cli_opts.ccnxdomain = client_name_str; result = ccn_name_from_uri(mountpoint,cli_opts.ccnxdomain); if( result < 0 ) dropbear_exit("Can't resolve client domain"); dropbear_log(LOG_WARNING,"Listening at"); print_ccnb_charbuf(mountpoint); result = ccn_set_interest_filter(cli_opts.ssh_ccn,mountpoint,&newServerAction); }
int ccns_slice_name(struct ccn_charbuf *nm, struct ccns_slice *s) { struct ccn_charbuf *c; struct ccn_digest *digest = NULL; struct ccn_charbuf *hash = NULL; int res = 0; c = ccn_charbuf_create(); if (c == NULL) return (-1); res = append_slice(c, s); if (res < 0) goto Cleanup; digest = ccn_digest_create(CCN_DIGEST_SHA256); hash = ccn_charbuf_create_n(ccn_digest_size(digest)); if (hash == NULL) goto Cleanup; ccn_digest_init(digest); res |= ccn_digest_update(digest, c->buf, c->length); res |= ccn_digest_final(digest, hash->buf, hash->limit); if (res < 0) goto Cleanup; hash->length = hash->limit; if (ccn_name_from_uri(nm, "ccnx:/%C1.M.S.localhost/%C1.S.cs") < 0) res = -1; res |= ccn_name_append(nm, hash->buf, hash->length); Cleanup: ccn_charbuf_destroy(&c); ccn_digest_destroy(&digest); ccn_charbuf_destroy(&hash); return (res); }
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); }
/** * 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); }
static struct ccn_charbuf * resolve_templ(struct ccn_charbuf *templ, unsigned const char *vcomp, int size) { if (templ == NULL) templ = ccn_charbuf_create(); if (size < 3 || size > 16) { ccn_charbuf_destroy(&templ); return(NULL); } templ->length = 0; 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); /* </Name> */ ccn_charbuf_append_tt(templ, CCN_DTAG_Exclude, CCN_DTAG); append_filter_all(templ); ccn_charbuf_append_tt(templ, CCN_DTAG_Component, CCN_DTAG); ccn_charbuf_append_tt(templ, size, CCN_BLOB); ccn_charbuf_append(templ, vcomp, size); ccn_charbuf_append_closer(templ); /* </Component> */ append_future_vcomp(templ); append_filter_all(templ); ccn_charbuf_append_closer(templ); /* </Exclude> */ answer_highest(templ); answer_passive(templ); ccn_charbuf_append_closer(templ); /* </Interest> */ return(templ); }