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); }
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); }
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 torepo = 0; int i; int status = 0; int res; ssize_t read_res; unsigned char *buf = NULL; while ((res = getopt(argc, argv, "hrb:")) != -1) { switch (res) { case 'b': blocksize = atol(optarg); if (blocksize <= 0 || blocksize > 4096) usage(progname); break; case 'r': torepo = 1; 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); } 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); ccn_get(ccn, name_v, NULL, 2000, NULL, NULL, NULL, 0); ccn_charbuf_destroy(&name_v); } 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); }