コード例 #1
0
ファイル: ccnbtreetest.c プロジェクト: WU-ARL/ccnx
/**
 * Use standard mkdtemp() to create a subdirectory of the
 * current working directory, and set the TEST_DIRECTORY environment
 * variable with its name.
 */
static int
test_directory_creation(void)
{
    int res;
    struct ccn_charbuf *dirbuf;
    char *temp;
    
    dirbuf = ccn_charbuf_create();
    CHKPTR(dirbuf);
    res = ccn_charbuf_putf(dirbuf, "./%s", "_bt_XXXXXX");
    CHKSYS(res);
    temp = mkdtemp(ccn_charbuf_as_string(dirbuf));
    CHKPTR(temp);
    res = ccn_charbuf_putf(dirbuf, "/%s", "_test");
    CHKSYS(res);
    res = mkdir(ccn_charbuf_as_string(dirbuf), 0777);
    CHKSYS(res);
    printf("Created directory %s\n", ccn_charbuf_as_string(dirbuf));
    setenv("TEST_DIRECTORY", ccn_charbuf_as_string(dirbuf), 1);
    ccn_charbuf_destroy(&dirbuf);
    return(res);
}
コード例 #2
0
ファイル: ccnd_stats.c プロジェクト: shenli-uiuc/Hermes
static void
collect_meter_xml(struct ccnd_handle *h, struct ccn_charbuf *b, struct ccnd_meter *m)
{
    uintmax_t total;
    unsigned rate;
    
    if (m == NULL)
        return;
    total = ccnd_meter_total(m);
    rate = ccnd_meter_rate(h, m);
    ccn_charbuf_putf(b, "<%s><total>%ju</total><persec>%u</persec></%s>",
        m->what, total, rate, m->what);
}
コード例 #3
0
ファイル: encodedecodetest.c プロジェクト: GabrielLiz/ccnx
static void init_all_chars_mixed_encoded(void) {
    struct ccn_charbuf *c;
    int i;
    c = ccn_charbuf_create();
    ccn_charbuf_append(c, "=", 1);
    for (i = 0; i < 256; i+=2) {
        ccn_charbuf_putf(c, "%02x%02X", i, i+1);
    }
    if (c->length >= sizeof(all_chars_mixed_encoded))
        c->length = sizeof(all_chars_mixed_encoded) - 1;
    memcpy(all_chars_mixed_encoded, c->buf, c->length);
    ccn_charbuf_destroy(&c);
}
コード例 #4
0
ファイル: ccnr_init.c プロジェクト: GabrielLiz/ccnx
/**
 * Read the contents of the repository config file
 *
 * Calls r_init_fail and returns NULL in case of error.
 * @returns unparsed content of config file in a newly allocated charbuf
 */
struct ccn_charbuf *
r_init_read_config(struct ccnr_handle *h)
{
    struct ccn_charbuf *path = NULL;
    struct ccn_charbuf *contents = NULL;
    size_t sz = 800;
    ssize_t sres = -1;
    int fd;
    
    h->directory = getenv("CCNR_DIRECTORY");
    if (h->directory == NULL || h->directory[0] == 0)
        h->directory = ".";
    path = ccn_charbuf_create();
    contents = ccn_charbuf_create();
    if (path == NULL || contents == NULL)
        return(NULL);
    ccn_charbuf_putf(path, "%s/config", h->directory);
    fd = open(ccn_charbuf_as_string(path), O_RDONLY);
    if (fd == -1) {
        if (errno == ENOENT)
            sres = 0;
        else
            r_init_fail(h, __LINE__, ccn_charbuf_as_string(path), errno);
    }
    else {
        for (;;) {
            sres = read(fd, ccn_charbuf_reserve(contents, sz), sz);
            if (sres == 0)
                break;
            if (sres < 0) {
                r_init_fail(h, __LINE__, "Read failed reading config", errno);
                break;
            }
            contents->length += sres;
            if (contents->length > 999999) {
                r_init_fail(h, __LINE__, "config file too large", 0);
                sres = -1;
                break;
            }
        }
        close(fd);
    }
    ccn_charbuf_destroy(&path);
    if (sres < 0)
        ccn_charbuf_destroy(&contents);
    return(contents);
}
コード例 #5
0
ファイル: ccnd_stats.c プロジェクト: shenli-uiuc/Hermes
static void
collect_forwarding_html(struct ccnd_handle *h, struct ccn_charbuf *b)
{
    struct hashtb_enumerator ee;
    struct hashtb_enumerator *e = &ee;
    struct ccn_forwarding *f;
    int res;
    struct ccn_charbuf *name = ccn_charbuf_create();
    
    ccn_charbuf_putf(b, "<h4>Forwarding</h4>" NL);
    ccn_charbuf_putf(b, "<ul>");
    hashtb_start(h->nameprefix_tab, e);
    for (; e->data != NULL; hashtb_next(e)) {
        struct nameprefix_entry *ipe = e->data;
        ccn_name_init(name);
        res = ccn_name_append_components(name, e->key, 0, e->keysize);
        if (res < 0)
            abort();
        if (0) {
            ccn_charbuf_putf(b, " <li>");
            ccn_uri_append(b, name->buf, name->length, 1);
            ccn_charbuf_putf(b, "</li>" NL);
        }
        for (f = ipe->forwarding; f != NULL; f = f->next) {
            if ((f->flags & (CCN_FORW_ACTIVE | CCN_FORW_PFXO)) != 0) {
                ccn_name_init(name);
                ccn_name_append_components(name, e->key, 0, e->keysize);
                ccn_charbuf_putf(b, " <li>");
                ccn_uri_append(b, name->buf, name->length, 1);
                ccn_charbuf_putf(b,
                                 " <b>face:</b> %u"
                                 " <b>flags:</b> 0x%x"
                                 " <b>expires:</b> %d",
                                 f->faceid,
                                 f->flags & CCN_FORW_PUBMASK,
                                 f->expires);
                ccn_charbuf_putf(b, "</li>" NL);
            }
        }
    }
    hashtb_end(e);
    ccn_charbuf_destroy(&name);
    ccn_charbuf_putf(b, "</ul>");
}
コード例 #6
0
ファイル: GroupManager.cpp プロジェクト: FreshLeaf8865/mumble
static void init_cached_keystore() {
    ccn_keystore *keystore = cached_keystore;
    int res;
    if (keystore == NULL) {
	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),
				(char *)"Th1s1sn0t8g00dp8ssw0rd.");
	if (res != 0) {
	    printf("Failed to initialize keystore %s\n", ccn_charbuf_as_string(temp));
	    exit(1);
	}
	ccn_charbuf_destroy(&temp);
	cached_keystore = keystore;
    }
}
コード例 #7
0
ファイル: ccnd_stats.c プロジェクト: shenli-uiuc/Hermes
static void
collect_forwarding_xml(struct ccnd_handle *h, struct ccn_charbuf *b)
{
    struct hashtb_enumerator ee;
    struct hashtb_enumerator *e = &ee;
    struct ccn_forwarding *f;
    int res;
    struct ccn_charbuf *name = ccn_charbuf_create();
    
    ccn_charbuf_putf(b, "<forwarding>");
    hashtb_start(h->nameprefix_tab, e);
    for (; e->data != NULL; hashtb_next(e)) {
        struct nameprefix_entry *ipe = e->data;
        for (f = ipe->forwarding, res = 0; f != NULL && !res; f = f->next) {
            if ((f->flags & (CCN_FORW_ACTIVE | CCN_FORW_PFXO)) != 0)
                res = 1;
        }
        if (res) {
            ccn_name_init(name);
            ccn_name_append_components(name, e->key, 0, e->keysize);
            ccn_charbuf_putf(b, "<fentry>");
            ccn_charbuf_putf(b, "<prefix>");
            ccn_uri_append(b, name->buf, name->length, 1);
            ccn_charbuf_putf(b, "</prefix>");
            for (f = ipe->forwarding; f != NULL; f = f->next) {
                if ((f->flags & (CCN_FORW_ACTIVE | CCN_FORW_PFXO)) != 0) {
                    ccn_charbuf_putf(b,
                                     "<dest>"
                                     "<faceid>%u</faceid>"
                                     "<flags>%x</flags>"
                                     "<expires>%d</expires>"
                                     "</dest>",
                                     f->faceid,
                                     f->flags & CCN_FORW_PUBMASK,
                                     f->expires);
                }
            }
            ccn_charbuf_putf(b, "</fentry>");
        }
    }
    hashtb_end(e);
    ccn_charbuf_destroy(&name);
    ccn_charbuf_putf(b, "</forwarding>");
}
コード例 #8
0
ファイル: ccnr_store.c プロジェクト: Emat12/ccnx
/**
 * Read the former size of repoFile1 from index/stable, and remove
 * the latter.
 */
static void
r_store_read_stable_point(struct ccnr_handle *h)
{
    struct ccn_charbuf *path = NULL;
    struct ccn_charbuf *cb = NULL;
    int fd;
    int i;
    ssize_t rres;
    uintmax_t val;
    unsigned char c;
    
    path = ccn_charbuf_create();
    cb = ccn_charbuf_create();
    ccn_charbuf_putf(path, "%s/index/stable", h->directory);
    fd = open(ccn_charbuf_as_string(path), O_RDONLY, 0666);
    if (fd != -1) {
        rres = read(fd, ccn_charbuf_reserve(cb, 80), 80);
        if (rres > 0)
            cb->length = rres;
        close(fd);
        if (CCNSHOULDLOG(h, dfsdf, CCNL_INFO))
            ccnr_msg(h, "Last stable at %s", ccn_charbuf_as_string(cb));
    }
    for (val = 0, i = 0; i < cb->length; i++) {
        c = cb->buf[i];
        if ('0' <= c && c <= '9')
            val = val * 10 + (c - '0');
        else
            break;
    }
    if (i == 0 || i < cb->length) {
        ccnr_msg(h, "Bad stable mark - %s", ccn_charbuf_as_string(cb));
        h->stable = 0;
    }
    else {
        h->stable = val;
        unlink(ccn_charbuf_as_string(path));
    }
    ccn_charbuf_destroy(&path);
    ccn_charbuf_destroy(&cb);
}
コード例 #9
0
ファイル: media_pro.cpp プロジェクト: FreshLeaf8865/mumble
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();
}
コード例 #10
0
ファイル: ccnr_io.c プロジェクト: IthacaDream/ccnx
PUBLIC int
r_io_open_repo_data_file(struct ccnr_handle *h, const char *name, int output)
{
    struct ccn_charbuf *temp = NULL;
    int fd = -1;
    struct fdholder *fdholder = NULL;

    temp = ccn_charbuf_create();
    ccn_charbuf_putf(temp, "%s/%s", h->directory, name);
    fd = open(ccn_charbuf_as_string(temp), output ? (O_CREAT | O_WRONLY | O_APPEND) : O_RDONLY, 0666);
    if (fd == -1) {
        if (CCNSHOULDLOG(h, sdf, CCNL_FINE))
            ccnr_msg(h, "open(%s): %s", ccn_charbuf_as_string(temp), strerror(errno));
        ccn_charbuf_destroy(&temp);
        return(-1);
    }
    fdholder = r_io_record_fd(h, fd,
                            temp->buf, temp->length,
                            CCNR_FACE_REPODATA | (output ? CCNR_FACE_NORECV : CCNR_FACE_NOSEND));
    if (fdholder == NULL)
        close_fd(&fd);
    else {
        if (!output) {
            /* Use a larger buffer for indexing an existing repo file */
            if (fdholder->inbuf == NULL) {
                fdholder->inbuf = ccn_charbuf_create();
                fdholder->bufoffset = 0;
            }
            if (fdholder->inbuf != NULL)
                ccn_charbuf_reserve(fdholder->inbuf, 256 * 1024);
        }
        if (CCNSHOULDLOG(h, sdf, CCNL_INFO))
            ccnr_msg(h, "opened fd=%d file=%s", fd, ccn_charbuf_as_string(temp));
    }
    ccn_charbuf_destroy(&temp);
    return(fd);
}
コード例 #11
0
ファイル: ccnd_stats.c プロジェクト: shenli-uiuc/Hermes
static void
collect_faces_xml(struct ccnd_handle *h, struct ccn_charbuf *b)
{
    int i;
    int m;
    int port;
    struct ccn_charbuf *nodebuf;
    
    nodebuf = ccn_charbuf_create();
    ccn_charbuf_putf(b, "<faces>");
    for (i = 0; i < h->face_limit; i++) {
        struct face *face = h->faces_by_faceid[i];
        if (face != NULL && (face->flags & CCN_FACE_UNDECIDED) == 0) {
            ccn_charbuf_putf(b, "<face>");
            ccn_charbuf_putf(b,
                             "<faceid>%u</faceid>"
                             "<faceflags>%04x</faceflags>",
                             face->faceid, face->flags);
            ccn_charbuf_putf(b, "<pending>%d</pending>",
                             face->pending_interests);
            ccn_charbuf_putf(b, "<recvcount>%d</recvcount>",
                             face->recvcount);
            nodebuf->length = 0;
            port = ccn_charbuf_append_sockaddr(nodebuf, face->addr);
            if (port > 0) {
                const char *node = ccn_charbuf_as_string(nodebuf);
                ccn_charbuf_putf(b, "<ip>%s:%d</ip>", node, port);
            }
            if (face->sendface != face->faceid &&
                face->sendface != CCN_NOFACEID)
                ccn_charbuf_putf(b, "<via>%u</via>", face->sendface);
            if (face != NULL && (face->flags & CCN_FACE_PASSIVE) == 0) {
                ccn_charbuf_putf(b, "<meters>");
                for (m = 0; m < CCND_FACE_METER_N; m++)
                    collect_meter_xml(h, b, face->meter[m]);
                ccn_charbuf_putf(b, "</meters>");
            }
            ccn_charbuf_putf(b, "</face>" NL);
        }
    }
    ccn_charbuf_putf(b, "</faces>");
    ccn_charbuf_destroy(&nodebuf);
}
コード例 #12
0
ファイル: media_pro.cpp プロジェクト: FreshLeaf8865/mumble
enum ccn_upcall_res
ccn_text_handler(struct ccn_closure *selfp,
		    enum ccn_upcall_kind kind,
		    struct ccn_upcall_info *info)
{
    UserDataBuf *userBuf  = (UserDataBuf *)selfp->data;
    if (userBuf == NULL || userBuf->iNeedDestroy) {
        //if (userBuf != NULL) delete userBuf;
        //selfp->data = NULL;
        return CCN_UPCALL_RESULT_OK;
    }

	switch (kind) {
	case CCN_UPCALL_INTEREST_TIMED_OUT: {
		// if it's short Interest without seq, reexpress
		// TODO: check whether the user is still in the conference
		//	     don't re-express if it is not
		return (CCN_UPCALL_RESULT_REEXPRESS);
	}
	case CCN_UPCALL_CONTENT_UNVERIFIED:
		fprintf(stderr, "unverified content received\n");
		return CCN_UPCALL_RESULT_OK;
	case CCN_UPCALL_FINAL:
        return CCN_UPCALL_RESULT_OK;
	case CCN_UPCALL_CONTENT:
		break;
	default:
		return CCN_UPCALL_RESULT_OK;

	}

    struct data_buffer *buffer = &userBuf->data_buf;
    const unsigned char *content_value;
	size_t len;
    NDNState *state = buffer->state;

	const unsigned char *ccnb = info->content_ccnb;
	size_t ccnb_size = info->pco->offset[CCN_PCO_E];
	struct ccn_indexbuf *comps = info->content_comps;

	ccn_content_get_value(ccnb, ccnb_size, info->pco,
			&content_value, &len);

	unsigned char *msg = (unsigned char *)calloc((len + 1), sizeof(char));
	memcpy(msg, content_value, len);
	msg[len] = '\0';
	QString textMsg = (const char *)msg;
	state->emitTextMsgArrival(userBuf->user_name, textMsg);
	free(msg);

	long seq;
	const unsigned char *seqptr = NULL;
	char *endptr = NULL;
	size_t seq_size = 0;
	int k = comps->n - 2;

	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) {
		return CCN_UPCALL_RESULT_OK;
	}

	seq++;

	struct ccn_charbuf *path = ccn_charbuf_create();
	ccn_name_init(path);
	ccn_name_append_components(path, ccnb, comps->buf[0], comps->buf[k]);
	struct ccn_charbuf *temp = ccn_charbuf_create();
	ccn_charbuf_putf(temp, "%ld", seq);
	ccn_name_append(path, temp->buf, temp->length);
	int res = ccn_express_interest(info->h, path, selfp, NULL);
	if (res < 0) {
		fprintf(stderr, "sending the first interest failed\n");
		exit(1);
	}
	ccn_charbuf_destroy(&path);
	ccn_charbuf_destroy(&temp);

    return CCN_UPCALL_RESULT_OK;
}
コード例 #13
0
ファイル: ccnd_stats.c プロジェクト: shenli-uiuc/Hermes
static struct ccn_charbuf *
collect_stats_html(struct ccnd_handle *h)
{
    struct ccnd_stats stats = {0};
    struct ccn_charbuf *b = ccn_charbuf_create();
    int pid;
    struct utsname un;
    const char *portstr;
    
    portstr = getenv(CCN_LOCAL_PORT_ENVNAME);
    if (portstr == NULL || portstr[0] == 0 || strlen(portstr) > 10)
        portstr = CCN_DEFAULT_UNICAST_PORT;
    uname(&un);
    pid = getpid();
    
    ccnd_collect_stats(h, &stats);
    ccn_charbuf_putf(b,
        "<html xmlns='http://www.w3.org/1999/xhtml'>"
        "<head>"
        "<title>%s ccnd[%d]</title>"
        //"<meta http-equiv='refresh' content='3'>"
        "<style type='text/css'>"
        "/*<![CDATA[*/"
        "p.header {color: white; background-color: blue; width: 100%%} "
        "table.tbl {border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: black} "
        "td {border-style: solid; "
            "border-width: 1.0px 1.0px 1.0px 1.0px; "
            "border-color: #808080 #808080 #808080 #808080; "
            "padding: 6px 6px 6px 6px; "
            "margin-left: auto; margin-right: auto; "
            "text-align: center"
            "} "
        "td.left {text-align: left} "
        "/*]]>*/"
        "</style>"
        "</head>" NL
        "<body bgcolor='#%06X'>"
        "<p class='header'>%s ccnd[%d] local port %s api %d start %ld.%06u now %ld.%06u</p>" NL
        "<div><b>Content items:</b> %llu accessioned,"
        " %d stored, %lu stale, %d sparse, %lu duplicate, %lu sent</div>" NL
        "<div><b>Interests:</b> %d names,"
        " %ld pending, %ld propagating, %ld noted</div>" NL
        "<div><b>Interest totals:</b> %lu accepted,"
        " %lu dropped, %lu sent, %lu stuffed</div>" NL,
        un.nodename,
        pid,
        ccnd_colorhash(h),
        un.nodename,
        pid,
        portstr,
        (int)CCN_API_VERSION,
        h->starttime, h->starttime_usec,
        h->sec,
        h->usec,
        (unsigned long long)h->accession,
        hashtb_n(h->content_tab),
        h->n_stale,
        hashtb_n(h->sparse_straggler_tab),
        h->content_dups_recvd,
        h->content_items_sent,
        hashtb_n(h->nameprefix_tab), stats.total_interest_counts,
        hashtb_n(h->interest_tab) - stats.total_flood_control,
        stats.total_flood_control,
        h->interests_accepted, h->interests_dropped,
        h->interests_sent, h->interests_stuffed);
    if (0)
        ccn_charbuf_putf(b,
                         "<div><b>Active faces and listeners:</b> %d</div>" NL,
                         hashtb_n(h->faces_by_fd) + hashtb_n(h->dgram_faces));
    collect_faces_html(h, b);
    collect_face_meter_html(h, b);
    collect_forwarding_html(h, b);
    ccn_charbuf_putf(b,
        "</body>"
        "</html>" NL);
    return(b);
}
コード例 #14
0
ファイル: ccnd_stats.c プロジェクト: shenli-uiuc/Hermes
static void
collect_face_meter_html(struct ccnd_handle *h, struct ccn_charbuf *b)
{
    int i;
    ccn_charbuf_putf(b, "<h4>Face Activity Rates</h4>");
    ccn_charbuf_putf(b, "<table cellspacing='0' cellpadding='0' class='tbl' summary='face activity rates'>");
    ccn_charbuf_putf(b, "<tbody>" NL);
    ccn_charbuf_putf(b, " <tr><td>        </td>\t"
                        " <td>Bytes/sec In/Out</td>\t"
                        " <td>recv data/intr sent</td>\t"
                        " <td>sent data/intr recv</td></tr>" NL);
    for (i = 0; i < h->face_limit; i++) {
        struct face *face = h->faces_by_faceid[i];
        if (face != NULL && (face->flags & (CCN_FACE_UNDECIDED|CCN_FACE_PASSIVE)) == 0) {
            ccn_charbuf_putf(b, " <tr>");
            ccn_charbuf_putf(b, "<td><b>face:</b> %u</td>\t",
                             face->faceid);
            ccn_charbuf_putf(b, "<td>%6u / %u</td>\t\t",
                                 ccnd_meter_rate(h, face->meter[FM_BYTI]),
                                 ccnd_meter_rate(h, face->meter[FM_BYTO]));
            ccn_charbuf_putf(b, "<td>%9u / %u</td>\t\t",
                                 ccnd_meter_rate(h, face->meter[FM_DATI]),
                                 ccnd_meter_rate(h, face->meter[FM_INTO]));
            ccn_charbuf_putf(b, "<td>%9u / %u</td>",
                                 ccnd_meter_rate(h, face->meter[FM_DATO]),
                                 ccnd_meter_rate(h, face->meter[FM_INTI]));
            ccn_charbuf_putf(b, "</tr>" NL);
        }
    }
    ccn_charbuf_putf(b, "</tbody>");
    ccn_charbuf_putf(b, "</table>");
}
コード例 #15
0
ファイル: ccnd_stats.c プロジェクト: shenli-uiuc/Hermes
static void
collect_faces_html(struct ccnd_handle *h, struct ccn_charbuf *b)
{
    int i;
    struct ccn_charbuf *nodebuf;
    int port;
    
    nodebuf = ccn_charbuf_create();
    ccn_charbuf_putf(b, "<h4>Faces</h4>" NL);
    ccn_charbuf_putf(b, "<ul>");
    for (i = 0; i < h->face_limit; i++) {
        struct face *face = h->faces_by_faceid[i];
        if (face != NULL && (face->flags & CCN_FACE_UNDECIDED) == 0) {
            ccn_charbuf_putf(b, " <li>");
            ccn_charbuf_putf(b, "<b>face:</b> %u <b>flags:</b> 0x%x",
                             face->faceid, face->flags);
            ccn_charbuf_putf(b, " <b>pending:</b> %d",
                             face->pending_interests);
            if (face->recvcount != 0)
                ccn_charbuf_putf(b, " <b>activity:</b> %d",
                                 face->recvcount);
            nodebuf->length = 0;
            port = ccn_charbuf_append_sockaddr(nodebuf, face->addr);
            if (port > 0) {
                const char *node = ccn_charbuf_as_string(nodebuf);
                int chk = CCN_FACE_MCAST | CCN_FACE_UNDECIDED |
                CCN_FACE_NOSEND | CCN_FACE_GG | CCN_FACE_PASSIVE;
                if ((face->flags & chk) == 0)
                    ccn_charbuf_putf(b,
                                     " <b>remote:</b> "
                                     "<a href='http://%s:%s/'>"
                                     "%s:%d</a>",
                                     node, CCN_DEFAULT_UNICAST_PORT,
                                     node, port);
                else if ((face->flags & CCN_FACE_PASSIVE) == 0)
                    ccn_charbuf_putf(b, " <b>remote:</b> %s:%d",
                                     node, port);
                else
                    ccn_charbuf_putf(b, " <b>local:</b> %s:%d",
                                     node, port);
                if (face->sendface != face->faceid &&
                    face->sendface != CCN_NOFACEID)
                    ccn_charbuf_putf(b, " <b>via:</b> %u", face->sendface);
            }
            ccn_charbuf_putf(b, "</li>" NL);
        }
    }
    ccn_charbuf_putf(b, "</ul>");
    ccn_charbuf_destroy(&nodebuf);
}
コード例 #16
0
int
ccnr_init_repo_keystore(struct ccnr_handle *ccnr, struct ccn *h)
{
    struct ccn_charbuf *temp = NULL;
    struct ccn_charbuf *culprit = NULL;
    struct stat statbuf;
    int res = -1;
    char *keystore_path = NULL;
    struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
    
    temp = ccn_charbuf_create();
    culprit = temp;
    ccn_charbuf_putf(temp, "%s/", ccnr->directory);
    res = stat(ccn_charbuf_as_string(temp), &statbuf);
    if (res == -1)
        goto Finish;
    if ((statbuf.st_mode & S_IFDIR) == 0) {
        res = -1;
        errno = ENOTDIR;
        goto Finish;
    }
    ccn_charbuf_putf(temp, "ccnx_repository_keystore");
    keystore_path = strdup(ccn_charbuf_as_string(temp));
    res = stat(keystore_path, &statbuf);
    
    if (res == 0 && h != NULL)
        res = ccn_load_default_key(h, keystore_path, CCNR_KEYSTORE_PASS);
    if (res >= 0) {
        culprit = NULL;
        goto Finish;
    }
    /* No stored keystore that we can access. Create one if we can.*/
    res = ccn_keystore_file_init(keystore_path, CCNR_KEYSTORE_PASS, "Repository", 0, 0);
    if (res != 0) {
        res = -1;
        goto Finish;
    }
    if (CCNSHOULDLOG(ccnr, keystore, CCNL_WARNING))
        ccnr_msg(ccnr, "New repository private key saved in %s", keystore_path);
    if (h != NULL)
        res = ccn_load_default_key(h, keystore_path, CCNR_KEYSTORE_PASS);
Finish:
    if (res >= 0 && h != NULL)
        res = ccn_chk_signing_params(h, NULL, &sp, NULL, NULL, NULL, NULL);
    if (res >= 0 && h != NULL) {
        memcpy(ccnr->ccnr_id, sp.pubid, sizeof(ccnr->ccnr_id));
        if (ccnr->ccnr_keyid == NULL)
            ccnr->ccnr_keyid = ccn_charbuf_create();
        else
            ccnr->ccnr_keyid->length = 0;
        ccn_charbuf_append_value(ccnr->ccnr_keyid, CCN_MARKER_CONTROL, 1);
        ccn_charbuf_append_string(ccnr->ccnr_keyid, ".M.K");
        ccn_charbuf_append_value(ccnr->ccnr_keyid, 0, 1);
        ccn_charbuf_append(ccnr->ccnr_keyid, ccnr->ccnr_id, sizeof(ccnr->ccnr_id));
    }
    if (res < 0) {
        ccnr->running = -1; /* Make note of init failure */
        if (culprit != NULL)
            ccnr_msg(ccnr, "Error accessing keystore - %s: %s\n",
                     strerror(errno), ccn_charbuf_as_string(temp));
    }
    ccn_charbuf_destroy(&temp);
    if (keystore_path != NULL)
        free(keystore_path);
    return(res);
}
コード例 #17
0
ファイル: media_pro.cpp プロジェクト: FreshLeaf8865/mumble
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);
}
コード例 #18
0
ファイル: ccncatchunks.c プロジェクト: GabrielLiz/ccnx
enum ccn_upcall_res
incoming_content(
    struct ccn_closure *selfp,
    enum ccn_upcall_kind kind,
    struct ccn_upcall_info *info)
{
    struct ccn_charbuf *name = NULL;
    struct ccn_charbuf *templ = NULL;
    struct ccn_charbuf *temp = NULL;
    const unsigned char *ccnb = NULL;
    size_t ccnb_size = 0;
    const unsigned char *data = NULL;
    size_t data_size = 0;
    size_t written;
    const unsigned char *ib = NULL; /* info->interest_ccnb */
    struct ccn_indexbuf *ic = NULL;
    int res;
    struct mydata *md = selfp->data;
    
    if (kind == CCN_UPCALL_FINAL) {
        if (md != NULL) {
            selfp->data = NULL;
            free(md);
            md = NULL;
        }
        return(CCN_UPCALL_RESULT_OK);
    }
    if (kind == CCN_UPCALL_INTEREST_TIMED_OUT)
        return(CCN_UPCALL_RESULT_REEXPRESS);
    if (kind != CCN_UPCALL_CONTENT && kind != CCN_UPCALL_CONTENT_UNVERIFIED)
        return(CCN_UPCALL_RESULT_ERR);
    if (md == NULL)
        selfp->data = md = calloc(1, sizeof(*md));
    ccnb = info->content_ccnb;
    ccnb_size = info->pco->offset[CCN_PCO_E];
    ib = info->interest_ccnb;
    ic = info->interest_comps;
    /* XXX - must verify sig, and make sure it is LEAF content */
    res = ccn_content_get_value(ccnb, ccnb_size, info->pco, &data, &data_size);
    if (res < 0) abort();
    if (data_size > CHUNK_SIZE) {
        /* For us this is spam. Give up now. */
        fprintf(stderr, "*** Segment %d found with a data size of %d."
                        " This program only works with segments of 1024 bytes."
                        " Try ccncatchunks2 instead.\n",
                        (int)selfp->intdata, (int)data_size);
        exit(1);
    }
    
    /* OK, we will accept this block. */
    
    written = fwrite(data, data_size, 1, stdout);
    if (written != 1)
        exit(1);
    
    /* A short block signals EOF for us. */
    if (data_size < CHUNK_SIZE)
        exit(0);
    
    /* Ask for the next one */
    name = ccn_charbuf_create();
    ccn_name_init(name);
    if (ic->n < 2) abort();
    res = ccn_name_append_components(name, ib, ic->buf[0], ic->buf[ic->n - 2]);
    if (res < 0) abort();
    temp = ccn_charbuf_create();
    ccn_charbuf_putf(temp, "%d", ++(selfp->intdata));
    ccn_name_append(name, temp->buf, temp->length);
    ccn_charbuf_destroy(&temp);
    templ = make_template(md, info);
    
    res = ccn_express_interest(info->h, name, selfp, templ);
    if (res < 0) abort();
    
    ccn_charbuf_destroy(&templ);
    ccn_charbuf_destroy(&name);
    
    return(CCN_UPCALL_RESULT_OK);
}
コード例 #19
0
ファイル: ccntraffic.c プロジェクト: tomtang5555/saviccnx
enum ccn_upcall_res
incoming_content(
    struct ccn_closure *selfp,
    enum ccn_upcall_kind kind,
    struct ccn_upcall_info *info)
{
    struct ccn_charbuf *name = NULL;
    struct ccn_charbuf *templ = NULL;
    struct ccn_charbuf *temp = NULL;
    const unsigned char *ccnb = NULL;
    size_t ccnb_size = 0;
    const unsigned char *data = NULL;
    size_t data_size = 0;
    // TANG: no need to write data to stdout
    //size_t written;
    const unsigned char *ib = NULL; /* info->interest_ccnb */
    struct ccn_indexbuf *ic = NULL;
    int res;
    struct mydata *md = selfp->data;
    
    if (kind == CCN_UPCALL_FINAL) {
        if (md != NULL) {
            selfp->data = NULL;
            free(md);
            md = NULL;
        }
        return(CCN_UPCALL_RESULT_OK);
    }
    if (kind == CCN_UPCALL_INTEREST_TIMED_OUT)
        return(CCN_UPCALL_RESULT_REEXPRESS);
    if (kind != CCN_UPCALL_CONTENT && kind != CCN_UPCALL_CONTENT_UNVERIFIED)
        return(CCN_UPCALL_RESULT_ERR);
    if (md == NULL)
        selfp->data = md = calloc(1, sizeof(*md));
    ccnb = info->content_ccnb;
    ccnb_size = info->pco->offset[CCN_PCO_E];
    ib = info->interest_ccnb;
    ic = info->interest_comps;
    /* XXX - must verify sig, and make sure it is LEAF content */
    res = ccn_content_get_value(ccnb, ccnb_size, info->pco, &data, &data_size);
    if (res < 0) abort();
    if (data_size > CHUNK_SIZE) {
        /* For us this is spam. Give up now. */
        fprintf(stderr, "*** Segment %d found with a data size of %d."
                        " This program only works with segments of 1024 bytes."
                        " Try ccncatchunks2 instead.\n",
                        (int)selfp->intdata, (int)data_size);
        exit(1);
    }
    
    /* OK, we will accept this block. */
    //sleep(1);
    // TANG: No need to write data to stdout, skip 3 lines
    //written = fwrite(data, data_size, 1, stdout);
    //if (written != 1)
    //    exit(1);
    
    /* A short block signals EOF for us. */
    // TANG: to support data_size smaller than 1024, skip 2 lines
    //if (data_size < CHUNK_SIZE)
    //    exit(0);
    
    /* Ask for the next one */
    name = ccn_charbuf_create();
    ccn_name_init(name);
    if (ic->n < 2) abort();
    res = ccn_name_append_components(name, ib, ic->buf[0], ic->buf[ic->n - 2]);
    if (res < 0) abort();
    temp = ccn_charbuf_create();
	//printf("intdata = %d \n ", selfp->intdata);
    ccn_charbuf_putf(temp, "%d", ++(selfp->intdata));
    ccn_name_append(name, temp->buf, temp->length);
    ccn_charbuf_destroy(&temp);
	
	if(DEBUG){
		//Print out the interest's name
		printf("Interest name = ");
		
		int myres = 0;
		struct ccn_indexbuf* ndx = ccn_indexbuf_create();
		unsigned char* mycomp = NULL;
		size_t mysize = 0;
		
		ndx->n = 0;
		myres = ccn_name_split(name, ndx);
		if(myres < 0){
			fprintf(stderr, "ccn_name_split @ ccntraffic. failed");
		}
		
		int it = 0;
		for(it = 0; it < ndx->n-1; it++){
			mysize = 0;
			myres = ccn_name_comp_get(name->buf, ndx, it, &mycomp, &mysize);
			printf("%s/", mycomp);
			mycomp = NULL;
		}
		
		printf("\n");
	}
	
    templ = make_template(md);

    res = ccn_express_interest(info->h, name, selfp, templ);
    if (res < 0) abort();
    
    ccn_charbuf_destroy(&templ);
    ccn_charbuf_destroy(&name);
    
    return(CCN_UPCALL_RESULT_OK);
}
コード例 #20
0
ファイル: media_pro.cpp プロジェクト: FreshLeaf8865/mumble
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;
}
コード例 #21
0
ファイル: ccninitaeskeystore.c プロジェクト: GabrielLiz/ccnx
int
main(int argc, char **argv)
{
    int res;
    int opt;
    char *dir = NULL;
    struct ccn_charbuf *filename = NULL;
    int force = 0;
    const char *password = NULL;
    char *name = ".ccnx_keystore";
    char *username;
    int fullname = 0;
    char *key = NULL;
    unsigned char keybuf[CCN_SECRET_KEY_LENGTH/8];
    int copylen = CCN_SECRET_KEY_LENGTH/8;
    struct stat statbuf;
    char *digest = NULL;
    int dirset = 0;
    int read_mode = 0;
    struct ccn_keystore *keystore = ccn_aes_keystore_create();
    EVP_PKEY *sk;
    
    while ((opt = getopt(argc, argv, "hfk:p:d:ro:")) != -1) {
        switch (opt) {
            case 'f':
                force = 1;
                break;
            case 'k':
                key = optarg;
                break;
            case 'p':
                password = optarg;
                break;
            case 'o':
                dir = optarg;
                dirset = 1;
                break;
            case 'd':
		digest = optarg;
                break;
	    case 'r':
		read_mode = 1;
		break;
            case 'h':
            default:
                usage(argv[0]);
                exit(1);
        }
    }
    username = argv[optind];
    if (username != NULL) {
        name = username;
        fullname = 1;
    }
    if (dir == NULL){
        dir = getenv("HOME");
    }
    res = stat(dir, &statbuf);
    if (res == -1) {
        perror(dir);
        exit(1);
    } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
        errno = ENOTDIR;
        perror(dir);
        exit(1);
    }
    filename = ccn_charbuf_create();
    if (!dirset) {
        ccn_charbuf_putf(filename, "%s/.ccnx", dir);
        res = stat(ccn_charbuf_as_string(filename), &statbuf);
        if (res == -1) {
            res = mkdir(ccn_charbuf_as_string(filename), 0700);
            if (res != 0) {
                perror(ccn_charbuf_as_string(filename));
                exit(1);
            }
        }
    } else
        ccn_charbuf_append_string(filename, dir);
    
    if (password == NULL)
        password = ccn_get_password();

    ccn_charbuf_append_string(filename, "/");
    ccn_charbuf_append_string(filename, name);

    if (key == NULL) {
	ccn_generate_symmetric_key(keybuf, CCN_SECRET_KEY_LENGTH);
    }
    if (!fullname) {
	if (read_mode) {
    	    ccn_charbuf_append_string(filename, "-");
    	    ccn_charbuf_append_string(filename, digest);
	} else {
            if (key != NULL) {
                memset(keybuf, 0, sizeof(keybuf));
                if (strlen(key) < sizeof(keybuf))
                    copylen = strlen(key);
                memcpy(keybuf, key, copylen);
            }
	    ccn_create_aes_filename_from_key(filename, keybuf, CCN_SECRET_KEY_LENGTH);
	}
    }

    if (read_mode) {
    	res = ccn_aes_keystore_init(keystore, ccn_charbuf_as_string(filename), password);
    } else {
    	if (!force) {
            res = stat(ccn_charbuf_as_string(filename), &statbuf);
            if (res != -1) {
                errno=EEXIST;
                perror(ccn_charbuf_as_string(filename));
                exit(1);
            }
        }

    	res = ccn_aes_keystore_file_init(ccn_charbuf_as_string(filename), password, keybuf, 
			CCN_SECRET_KEY_LENGTH);
    }

    if (res != 0) {
        if (errno != 0)
            perror(ccn_charbuf_as_string(filename));
        else
            fprintf(stderr, "ccn_keystore_file_init: invalid argument\n");
        exit(1);
    }

    if (read_mode) {
        unsigned char *key_data;
        int i;
	sk = (EVP_PKEY *) ccn_keystore_key(keystore);
	key_data = ASN1_STRING_data(EVP_PKEY_get0(sk));
        printf("Retrieved key: 0x");
        for (i = 0; i < ccn_keystore_key_digest_length(keystore); i++)
            printf("%x", key_data[i]);
        printf("\n");
    } else {
        printf("Created keystore: %s\n", ccn_charbuf_as_string(filename));
    }
	
    return(0);
}
コード例 #22
0
ファイル: ccnr_store.c プロジェクト: Emat12/ccnx
PUBLIC void
r_store_init(struct ccnr_handle *h)
{
    struct ccn_btree *btree = NULL;
    struct ccn_btree_node *node = NULL;
    struct hashtb_param param = {0};
    int i;
    int j;
    int res;
    struct ccn_charbuf *path = NULL;
    struct ccn_charbuf *msgs = NULL;
    off_t offset;
    
    path = ccn_charbuf_create();
    param.finalize_data = h;
    param.finalize = 0;
    
    h->cob_limit = r_init_confval(h, "CCNR_CONTENT_CACHE", 16, 2000000, 4201);
    h->cookie_limit = choose_limit(h->cob_limit, (ccnr_cookie)(~0U));
    h->content_by_cookie = calloc(h->cookie_limit, sizeof(h->content_by_cookie[0]));
    CHKPTR(h->content_by_cookie);
    h->content_by_accession_tab = hashtb_create(sizeof(struct content_by_accession_entry), NULL);
    CHKPTR(h->content_by_accession_tab);
    h->btree = btree = ccn_btree_create();
    CHKPTR(btree);
    FAILIF(btree->nextnodeid != 1);
    ccn_charbuf_putf(path, "%s/index", h->directory);
    res = mkdir(ccn_charbuf_as_string(path), 0700);
    if (res != 0 && errno != EEXIST)
        r_init_fail(h, __LINE__, ccn_charbuf_as_string(path), errno);
    else {
        msgs = ccn_charbuf_create();
        btree->io = ccn_btree_io_from_directory(ccn_charbuf_as_string(path), msgs);
        if (btree->io == NULL)
            res = errno;
        if (msgs->length != 0 && CCNSHOULDLOG(h, sffdsdf, CCNL_WARNING)) {
            ccnr_msg(h, "while initializing %s - %s",
                     ccn_charbuf_as_string(path),
                     ccn_charbuf_as_string(msgs));
        }
        ccn_charbuf_destroy(&msgs);
        if (btree->io == NULL)
            r_init_fail(h, __LINE__, ccn_charbuf_as_string(path), res);
    }
    node = ccn_btree_getnode(btree, 1, 0);
    if (btree->io != NULL)
        btree->nextnodeid = btree->io->maxnodeid + 1;
    CHKPTR(node);
    if (node->buf->length == 0) {
        res = ccn_btree_init_node(node, 0, 'R', 0);
        CHKSYS(res);
    }
    ccn_charbuf_destroy(&path);
    if (h->running == -1)
        return;
    r_store_read_stable_point(h);
    h->active_in_fd = -1;
    h->active_out_fd = r_io_open_repo_data_file(h, "repoFile1", 1); /* output */
    offset = lseek(h->active_out_fd, 0, SEEK_END);
    h->startupbytes = offset;
    if (offset != h->stable || node->corrupt != 0) {
        ccnr_msg(h, "Index not current - resetting");
        ccn_btree_init_node(node, 0, 'R', 0);
        node = NULL;
        ccn_btree_destroy(&h->btree);
        path = ccn_charbuf_create();
        /* Remove old index files to avoid confusion */
        for (i = 1, j = 0; i > 0 && j < 3; i++) {
            path->length = 0;
            res = ccn_charbuf_putf(path, "%s/index/%d", h->directory, i);
            if (res >= 0)
                res = unlink(ccn_charbuf_as_string(path));
            if (res < 0)
                j++;
        }
        h->btree = btree = ccn_btree_create();
        path->length = 0;
        ccn_charbuf_putf(path, "%s/index", h->directory);
        btree->io = ccn_btree_io_from_directory(ccn_charbuf_as_string(path), msgs);
        CHKPTR(btree->io);
        btree->io->maxnodeid = 0;
        btree->nextnodeid = 1;
        node = ccn_btree_getnode(btree, 1, 0);
        btree->nextnodeid = btree->io->maxnodeid + 1;
        ccn_btree_init_node(node, 0, 'R', 0);
        h->stable = 0;
        h->active_in_fd = r_io_open_repo_data_file(h, "repoFile1", 0); /* input */
        ccn_charbuf_destroy(&path);
        if (CCNSHOULDLOG(h, dfds, CCNL_INFO))
            ccn_schedule_event(h->sched, 50000, r_store_reindexing, NULL, 0);
    }
    if (CCNSHOULDLOG(h, weuyg, CCNL_FINEST)) {
        FILE *dumpfile = NULL;
        
        path = ccn_charbuf_create();
        ccn_charbuf_putf(path, "%s/index/btree_check.out", h->directory);
        dumpfile = fopen(ccn_charbuf_as_string(path), "w");
        res = ccn_btree_check(btree, dumpfile);
        if (dumpfile != NULL) {
            fclose(dumpfile);
            dumpfile = NULL;
        }
        else
            path->length = 0;
        ccnr_msg(h, "ccn_btree_check returned %d (%s)",
                    res, ccn_charbuf_as_string(path));
        ccn_charbuf_destroy(&path);
        if (res < 0)
            r_init_fail(h, __LINE__, "index is corrupt", res);
    }
    btree->full = r_init_confval(h, "CCNR_BTREE_MAX_FANOUT", 4, 9999, 1999);
    btree->full0 = r_init_confval(h, "CCNR_BTREE_MAX_LEAF_ENTRIES", 4, 9999, 1999);
    btree->nodebytes = r_init_confval(h, "CCNR_BTREE_MAX_NODE_BYTES", 1024, 8388608, 2097152);
    btree->nodepool = r_init_confval(h, "CCNR_BTREE_NODE_POOL", 16, 2000000, 512);
    if (h->running != -1)
        r_store_index_needs_cleaning(h);
}
コード例 #23
0
ファイル: ccnbasicconfig.c プロジェクト: RazortoothRTC/ccnx
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);
}
コード例 #24
0
ファイル: signbenchtest.c プロジェクト: Emat12/ccnx
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);
}
コード例 #25
0
ファイル: ccnsendchunks.c プロジェクト: futtre/ccnx
int
main(int argc, char **argv)
{
    const char *progname = argv[0];
    struct ccn *ccn = NULL;
    struct ccn_charbuf *root = NULL;
    struct ccn_charbuf *name = NULL;
    struct ccn_charbuf *temp = NULL;
    struct ccn_charbuf *templ = NULL;
    struct ccn_charbuf *signed_info = NULL;
    struct ccn_charbuf *keylocator = NULL;
    struct ccn_charbuf *finalblockid = NULL;
    struct ccn_keystore *keystore = NULL;
    long expire = -1;
    long blocksize = 1024;
    int i;
    int status = 0;
    int res;
    ssize_t read_res;
    unsigned char *buf = NULL;
    struct mydata mydata = { 0 };
    struct ccn_closure in_content = {.p=&incoming_content, .data=&mydata};
    struct ccn_closure in_interest = {.p=&incoming_interest, .data=&mydata};
    while ((res = getopt(argc, argv, "hx:b:")) != -1) {
        switch (res) {
            case 'x':
                expire = atol(optarg);
                if (expire <= 0)
                    usage(progname);
                break;
	    case 'b':
	        blocksize = atol(optarg);
                break;
            default:
            case 'h':
                usage(progname);
                break;
        }
    }
    argc -= optind;
    argv += optind;
    if (argv[0] == NULL)
        usage(progname);
    name = ccn_charbuf_create();
    res = ccn_name_from_uri(name, 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);

    ccn = ccn_create();
    if (ccn_connect(ccn, NULL) == -1) {
        perror("Could not connect to ccnd");
        exit(1);
    }
    
    buf = calloc(1, blocksize);
    root = name;
    name = ccn_charbuf_create();
    temp = ccn_charbuf_create();
    templ = ccn_charbuf_create();
    signed_info = ccn_charbuf_create();
    keystore = ccn_keystore_create();
    temp->length = 0;
    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\n");
        exit(1);
    }
    
    name->length = 0;
    ccn_charbuf_append(name, root->buf, root->length);
    
    /* Set up a handler for interests */
    ccn_set_interest_filter(ccn, name, &in_interest);
    
    /* Initiate check to see whether there is already something there. */
    temp->length = 0;
    ccn_charbuf_putf(temp, "%d", 0);
    ccn_name_append(name, temp->buf, temp->length);
    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_MaxSuffixComponents, CCN_DTAG);
    ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
    ccn_charbuf_append(templ, "1", 1);
    ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
    // XXX - use pubid
    ccn_charbuf_append_closer(templ); /* </Interest> */
    res = ccn_express_interest(ccn, name, &in_content, templ);
    if (res < 0) abort();
    
    /* Construct a key locator contining the key itself */
    keylocator = ccn_charbuf_create();
    ccn_charbuf_append_tt(keylocator, CCN_DTAG_KeyLocator, CCN_DTAG);
    ccn_charbuf_append_tt(keylocator, CCN_DTAG_Key, CCN_DTAG);
    res = ccn_append_pubkey_blob(keylocator, ccn_keystore_public_key(keystore));
    if (res < 0)
        ccn_charbuf_destroy(&keylocator);
    else {
        ccn_charbuf_append_closer(keylocator); /* </Key> */
        ccn_charbuf_append_closer(keylocator); /* </KeyLocator> */
    }
    
    for (i = 0;; i++) {
        read_res = read_full(0, buf, blocksize);
        if (read_res < 0) {
            perror("read");
            read_res = 0;
            status = 1;
        }
        signed_info->length = 0;
        if (read_res < blocksize) {
            temp->length = 0;
            ccn_charbuf_putf(temp, "%d", i);
            ccn_name_append(name, temp->buf, temp->length);
            finalblockid = ccn_charbuf_create();
            ccn_charbuf_append_tt(finalblockid, temp->length, CCN_BLOB);
            ccn_charbuf_append(finalblockid, temp->buf, temp->length);
        }
        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*/ expire,
                                     finalblockid,
                                     keylocator);
        /* Put the keylocator in the first block only. */
        ccn_charbuf_destroy(&keylocator);
        if (res < 0) {
            fprintf(stderr, "Failed to create signed_info (res == %d)\n", res);
            exit(1);
        }
        name->length = 0;
        ccn_charbuf_append(name, root->buf, root->length);
        temp->length = 0;
        ccn_charbuf_putf(temp, "%d", i);
        ccn_name_append(name, temp->buf, temp->length);
        temp->length = 0;
        ccn_charbuf_append(temp, buf, read_res);
        temp->length = 0;
        res = ccn_encode_ContentObject(temp,
                                       name,
                                       signed_info,
                                       buf,
                                       read_res,
                                       NULL,
                                       ccn_keystore_private_key(keystore));
        if (res != 0) {
            fprintf(stderr, "Failed to encode ContentObject (res == %d)\n", res);
            exit(1);
        }
        if (i == 0) {
            /* Finish check for old content */
            if (mydata.content_received == 0)
                ccn_run(ccn, 100);
            if (mydata.content_received > 0) {
                fprintf(stderr, "%s: name is in use: %s\n", progname, argv[0]);
                exit(1);
            }
            mydata.outstanding++; /* the first one is free... */
        }
        res = ccn_put(ccn, temp->buf, temp->length);
        if (res < 0) {
            fprintf(stderr, "ccn_put failed (res == %d)\n", res);
            exit(1);
        }
        if (read_res < blocksize)
            break;
        if (mydata.outstanding > 0)
            mydata.outstanding--;
        else
            res = 10;
        res = ccn_run(ccn, res * 100);
        if (res < 0) {
            status = 1;
            break;
        }
    }
    
    free(buf);
    buf = NULL;
    ccn_charbuf_destroy(&root);
    ccn_charbuf_destroy(&name);
    ccn_charbuf_destroy(&temp);
    ccn_charbuf_destroy(&signed_info);
    ccn_charbuf_destroy(&finalblockid);
    ccn_keystore_destroy(&keystore);
    ccn_destroy(&ccn);
    exit(status);
}
コード例 #26
0
ファイル: ccnr_init.c プロジェクト: GabrielLiz/ccnx
/**
 * Load a link to the repo policy from the repoPolicy file and load the link
 * target to extract the actual policy.
 * If a policy file does not exist a new one is created, with a link to a policy
 * based either on the environment variable CCNR_GLOBAL_PREFIX or the system
 * default value of ccnx:/parc.com/csl/ccn/Repos, plus the system defaults for
 * other fields.
 * This routine must be called after the btree code is initialized and capable
 * of returning content objects.
 * Sets the parsed_policy field of the handle to be the new policy.
 */
static int
load_policy(struct ccnr_handle *ccnr)
{
    int fd;
    ssize_t res;
    struct content_entry *content = NULL;
    const unsigned char *content_msg = NULL;
    struct ccn_parsed_ContentObject pco = {0};
    struct ccn_parsed_Link pl = {0};
    struct ccn_indexbuf *nc = NULL;
    struct ccn_charbuf *basename = NULL;
    struct ccn_charbuf *policy = NULL;
    struct ccn_charbuf *policy_cob = NULL;
    struct ccn_charbuf *policyFileName;
    const char *global_prefix;
    const unsigned char *buf = NULL;
    size_t length = 0;
    int segment = 0;
    int final = 0;
    struct ccn_buf_decoder decoder;
    struct ccn_buf_decoder *d;
    
    policyFileName = ccn_charbuf_create();
    ccn_charbuf_putf(policyFileName, "%s/repoPolicy", ccnr->directory);
    ccnr->parsed_policy = ccnr_parsed_policy_create();
    fd = open(ccn_charbuf_as_string(policyFileName), O_RDONLY);
    if (fd >= 0) {
        ccnr->policy_link_cob = ccn_charbuf_create();
        ccn_charbuf_reserve(ccnr->policy_link_cob, 4096);   // limits the size of the policy link
        ccnr->policy_link_cob->length = 0;    // clear the buffer
        res = read(fd, ccnr->policy_link_cob->buf, ccnr->policy_link_cob->limit - ccnr->policy_link_cob->length);
        close(fd);
        if (res == -1) {
            r_init_fail(ccnr, __LINE__, "Error reading repoPolicy file.", errno);
            ccn_charbuf_destroy(&ccnr->policy_link_cob);
            ccn_charbuf_destroy(&policyFileName);
            return(-1);
        }
        ccnr->policy_link_cob->length = res;
        nc = ccn_indexbuf_create();
        res = ccn_parse_ContentObject(ccnr->policy_link_cob->buf,
                                      ccnr->policy_link_cob->length, &pco, nc);
        res = ccn_ref_tagged_BLOB(CCN_DTAG_Content, ccnr->policy_link_cob->buf,
                                  pco.offset[CCN_PCO_B_Content],
                                  pco.offset[CCN_PCO_E_Content],
                                  &buf, &length);
        d = ccn_buf_decoder_start(&decoder, buf, length);
        res = ccn_parse_Link(d, &pl, NULL);
        if (res <= 0) {
            ccnr_msg(ccnr, "Policy link is malformed.");
            goto CreateNewPolicy;
        }
        basename = ccn_charbuf_create();
        ccn_charbuf_append(basename, buf + pl.offset[CCN_PL_B_Name],
                           pl.offset[CCN_PL_E_Name] - pl.offset[CCN_PL_B_Name]);
        ccnr->policy_name = ccn_charbuf_create(); // to detect writes to this name
        ccn_charbuf_append_charbuf(ccnr->policy_name, basename); // has version
        ccn_name_chop(ccnr->policy_name, NULL, -1); // get rid of version
        policy = ccn_charbuf_create();
        // if we fail to retrieve the link target, report and then create a new one
        do {
            ccn_name_append_numeric(basename, CCN_MARKER_SEQNUM, segment++);
            content = r_store_lookup_ccnb(ccnr, basename->buf, basename->length);
            if (content == NULL) {
                ccnr_debug_ccnb(ccnr, __LINE__, "policy lookup failed for", NULL,
                                basename->buf, basename->length);
                break;
            }
            ccn_name_chop(basename, NULL, -1);
            content_msg = r_store_content_base(ccnr, content);
            if (content_msg == NULL) {
                ccnr_debug_ccnb(ccnr, __LINE__, "Unable to read policy object", NULL,
                                basename->buf, basename->length);
                break;
            }
            res = ccn_parse_ContentObject(content_msg, r_store_content_size(ccnr, content), &pco, nc);
            res = ccn_ref_tagged_BLOB(CCN_DTAG_Content, content_msg,
                                      pco.offset[CCN_PCO_B_Content],
                                      pco.offset[CCN_PCO_E_Content],
                                      &buf, &length);
            ccn_charbuf_append(policy, buf, length);
            final = ccn_is_final_pco(content_msg, &pco, nc);
        } while (!final && segment < 100);