Exemplo n.º 1
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);
}
Exemplo n.º 2
0
/**
 * Assert that an interest has possibly been expressed that matches
 * the seqwriter's data.  This is useful, for example, if the seqwriter
 * was created in response to an interest.
 */
int
ccn_seqw_possible_interest(struct ccn_seqwriter *w)
{
    if (w == NULL || w->cl.data != w)
        return(-1);
    w->interests_possibly_pending = 1;
    ccn_seqw_write(w, NULL, 0);
    return(0);
}
Exemplo n.º 3
0
/**
 * End a batch of writes.
 */
int
ccn_seqw_batch_end(struct ccn_seqwriter *w)
{
    if (w == NULL || w->cl.data != w || w->batching == 0)
        return(-1);
    if (--(w->batching) == 0)
        ccn_seqw_write(w, NULL, 0);
    return(w->batching);
}
Exemplo n.º 4
0
/**
 * Close the seqwriter, which will be freed.
 */
int
ccn_seqw_close(struct ccn_seqwriter *w)
{
    if (w == NULL || w->cl.data != w)
        return(-1);
    w->closed = 1;
    w->interests_possibly_pending = 1;
    w->batching = 0;
    ccn_seqw_write(w, NULL, 0);
    ccn_set_interest_filter(w->h, w->nb, NULL);
    return(0);
}
Exemplo n.º 5
0
int
main(int argc, char **argv)
{
    const char *progname = argv[0];
    struct ccn *ccn = NULL;
    struct ccn_charbuf *name = NULL;
    struct ccn_seqwriter *w = NULL;
    int blocksize = 1024;
    int torepo = 0;
    int scope = 1;
    int i;
    int status = 0;
    int res;
    ssize_t read_res;
    size_t blockread;
    unsigned char *buf = NULL;
    struct ccn_charbuf *templ;

    while ((res = getopt(argc, argv, "hrb:s:")) != -1) {
        switch (res) {
        case 'b':
            blocksize = atoi(optarg);
            if (blocksize <= 0 || blocksize > 4096)
                usage(progname);
            break;
        case 'r':
            torepo = 1;
            break;
        case 's':
            scope = atoi(optarg);
            if (scope < 1 || scope > 3)
                usage(progname);
            break;
        default:
        case 'h':
            usage(progname);
            break;
        }
    }
    argc -= optind;
    argv += optind;
    if (argc != 1)
        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);
    }
    ccn = ccn_create();
    if (ccn_connect(ccn, NULL) == -1) {
        perror("Could not connect to ccnd");
        exit(1);
    }

    buf = calloc(1, blocksize);

    w = ccn_seqw_create(ccn, name);
    if (w == NULL) {
        fprintf(stderr, "ccn_seqw_create failed\n");
        exit(1);
    }
    ccn_seqw_set_block_limits(w, blocksize, blocksize);
    if (torepo) {
        struct ccn_charbuf *name_v = ccn_charbuf_create();
        ccn_seqw_get_name(w, name_v);
        ccn_name_from_uri(name_v, "%C1.R.sw");
        ccn_name_append_nonce(name_v);
        templ = make_template(scope);
        res = ccn_get(ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
        ccn_charbuf_destroy(&templ);
        ccn_charbuf_destroy(&name_v);
        if (res < 0) {
            fprintf(stderr, "No response from repository\n");
            exit(1);
        }
    }
    blockread = 0;
    for (i = 0;; i++) {
        while (blockread < blocksize) {
            ccn_run(ccn, 1);
            read_res = read(0, buf + blockread, blocksize - blockread);
            if (read_res == 0)
                goto cleanup;
            if (read_res < 0) {
                perror("read");
                status = 1;
                goto cleanup;
            }
            blockread += read_res;
        }
        res = ccn_seqw_write(w, buf, blockread);
        while (res == -1) {
            ccn_run(ccn, 100);
            res = ccn_seqw_write(w, buf, blockread);
        }
        if (res != blockread)
            abort(); /* hmm, ccn_seqw_write did a short write or something */
        blockread = 0;
    }

cleanup:
    // flush out any remaining data and close
    if (blockread > 0) {
        res = ccn_seqw_write(w, buf, blockread);
        while (res == -1) {
            ccn_run(ccn, 100);
            res = ccn_seqw_write(w, buf, blockread);
        }
    }
    ccn_seqw_close(w);
    ccn_run(ccn, 1);
    free(buf);
    buf = NULL;
    ccn_charbuf_destroy(&name);
    ccn_destroy(&ccn);
    exit(status);
}
Exemplo n.º 6
0
int
main(int argc, char **argv)
{
    const char *progname = argv[0];
    struct ccn *ccn = NULL;
    struct ccn_charbuf *name = NULL;
    struct ccn_seqwriter *w = NULL;
    int blocksize = CCN_MAX_CONTENT_PAYLOAD / 2;
    int freshness = -1;
    int torepo = 0;
    int scope = 1;
    int i;
    int status = 0;
    int res;
    ssize_t read_res;
    size_t blockread;
    unsigned char *buf = NULL;
    struct ccn_charbuf *templ;
    char *symmetric_suffix = NULL;
    const char *password = NULL;
    char *dir = NULL;
    
    while ((res = getopt(argc, argv, "hrb:s:x:d:p:o:")) != -1) {
        switch (res) {
            case 'b':
                blocksize = atoi(optarg);
                if (blocksize <= 0 || blocksize > CCN_MAX_CONTENT_PAYLOAD)
                    usage(progname);
                break;
            case 'r':
                torepo = 1;
                break;
            case 's':
                scope = atoi(optarg);
                if (scope < 1 || scope > 3)
                    usage(progname);
                break;
            case 'x':
                freshness = atoi(optarg);
                if (freshness < 0)
                    usage(progname);
                break;
            case 'd':
                symmetric_suffix = optarg;
                break;
            case 'p':
                password = optarg;
                break;
            case 'o':
                dir = optarg;
                break;
            case 'h':
                usage(progname);
                break;
        }
    }
    argc -= optind;
    argv += optind;
    if (argc != 1)
        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);
    }
    ccn = ccn_create();
    if (ccn_connect(ccn, NULL) == -1) {
        perror("Could not connect to ccnd");
        exit(1);
    }
    
    buf = calloc(1, blocksize);
    
    w = ccn_seqw_create(ccn, name);
    if (w == NULL) {
        fprintf(stderr, "ccn_seqw_create failed\n");
        exit(1);
    }

    if (symmetric_suffix != NULL) {
        struct ccn_charbuf *key_digest = ccn_charbuf_create();

        if (ccn_get_key_digest_from_suffix(ccn, dir, symmetric_suffix, password, key_digest)) {
            perror("Can't access keystore");
            exit(1);
        }
        ccn_seqw_set_key_digest(w, key_digest->buf, key_digest->length);
        ccn_charbuf_destroy(&key_digest);
    }
    
    ccn_seqw_set_block_limits(w, blocksize, blocksize);
    if (freshness > -1)
        ccn_seqw_set_freshness(w, freshness);
    if (torepo) {
        struct ccn_charbuf *name_v = ccn_charbuf_create();
        ccn_seqw_get_name(w, name_v);
        ccn_name_from_uri(name_v, "%C1.R.sw");
        ccn_name_append_nonce(name_v);
        templ = make_template(scope);
        res = ccn_get(ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
        ccn_charbuf_destroy(&templ);
        ccn_charbuf_destroy(&name_v);
        if (res < 0) {
            fprintf(stderr, "No response from repository\n");
            exit(1);
        }
    }
    blockread = 0;
    for (i = 0;; i++) {
        while (blockread < blocksize) {
            if (ccn_run(ccn, 1) < 0) {
                fprintf(stderr, "Lost connection to ccnd: %s\n",
                        strerror(ccn_geterror(ccn)));
                exit(1);
            }
            read_res = read(0, buf + blockread, blocksize - blockread);
            if (read_res == 0)
                goto cleanup;
            if (read_res < 0) {
                perror("read");
                status = 1;
                goto cleanup;
            }
            blockread += read_res;
        }
        res = ccn_seqw_write(w, buf, blockread);
        while (res == -1) {
            if (ccn_run(ccn, 100) < 0) {
                fprintf(stderr, "Lost connection to ccnd: %s\n",
                        strerror(ccn_geterror(ccn)));
                exit(1);
            }
            res = ccn_seqw_write(w, buf, blockread);
        }
        if (res != blockread)
            abort(); /* hmm, ccn_seqw_write did a short write or something */
        blockread = 0;
    }
    
cleanup:
    // flush out any remaining data and close
    if (blockread > 0) {
        res = ccn_seqw_write(w, buf, blockread);
        while (res == -1) {
            if (ccn_run(ccn, 100) < 0) {
                fprintf(stderr, "Lost connection to ccnd: %s\n",
                        strerror(ccn_geterror(ccn)));
                exit(1);
            }
            res = ccn_seqw_write(w, buf, blockread);
        }
    }
    ccn_seqw_close(w);
    ccn_run(ccn, 1);
    free(buf);
    buf = NULL;
    ccn_charbuf_destroy(&name);
    ccn_destroy(&ccn);
    exit(status);
}
Exemplo n.º 7
0
int
main(int argc, char **argv)
{
    const char *progname = argv[0];
    struct ccn *ccn = NULL;
    struct ccn_charbuf *name = NULL;
    struct ccn_seqwriter *w = NULL;
    long blocksize = 1024;
    int i;
    int status = 0;
    int res;
    ssize_t read_res;
    unsigned char *buf = NULL;
    while ((res = getopt(argc, argv, "hb:")) != -1) {
        switch (res) {
	    case 'b':
	        blocksize = atol(optarg);
                if (blocksize <= 0 || blocksize > 4096)
                    usage(progname);
                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 ccnx 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);
    
    w = ccn_seqw_create(ccn, name);
    if (w == NULL) {
        fprintf(stderr, "ccn_seqw_create failed\n");
        exit(1);
    }
    for (i = 0;; i++) {
        ccn_run(ccn, 1);
        read_res = read(0, buf, blocksize);
        if (read_res < 0) {
            perror("read");
            read_res = 0;
            status = 1;
        }
        if (read_res == 0) {
            ccn_seqw_close(w);
            w = NULL;
            status = 0;
            break;
        }
        res = ccn_seqw_write(w, buf, read_res);
        while (res == -1) {
            ccn_run(ccn, 100);
            res = ccn_seqw_write(w, buf, read_res);
        }
        if (res != read_res)
            abort(); /* hmm, ccn_seqw_write did a short write or something */
    }
    ccn_run(ccn, 1);
    free(buf);
    buf = NULL;
    ccn_charbuf_destroy(&name);
    ccn_destroy(&ccn);
    exit(status);
}