static int send_interest(void) { ndn_shared_block_t* sn = ndn_name_from_uri(uri, strlen(uri)); if (sn == NULL) { printf("client (pid=%" PRIkernel_pid "): cannot create name from uri " "\"%s\"\n", handle->id, uri); return NDN_APP_ERROR; } ndn_shared_block_t* sin = ndn_name_append_uint32(&sn->block, count); ndn_shared_block_release(sn); if (sin == NULL) { printf("client (pid=%" PRIkernel_pid "): cannot append component to " "name \"%s\"\n", handle->id, uri); return NDN_APP_ERROR; } uint32_t lifetime = 1000; // 1 sec /* printf("client (pid=%" PRIkernel_pid "): express interest, name=", handle->id); ndn_name_print(&sin->block); putchar('\n'); */ if (ndn_app_express_interest(handle, &sin->block, NULL, lifetime, on_data, on_timeout) != 0) { printf("client (pid=%" PRIkernel_pid "): failed to express interest\n", handle->id); ndn_shared_block_release(sin); return NDN_APP_ERROR; } ndn_shared_block_release(sin); return NDN_APP_CONTINUE; }
/** * should probably return a new cob, rather than reusing one. * should publish link as: * NDNRID_POLICY_URI("ndn:/%C1.M.S.localhost/%C1.M.SRV/repository/POLICY)/%C1.M.K--pubid--/--version--/%00 * should have key locator which is the key name of the repository */ PUBLIC struct ndn_charbuf * ndnr_init_policy_link_cob(struct ndnr_handle *ndnr, struct ndn *h, struct ndn_charbuf *targetname) { struct ndn_signing_params sp = NDN_SIGNING_PARAMS_INIT; struct ndn_charbuf *name = ndn_charbuf_create(); struct ndn_charbuf *pubid = ndn_charbuf_create(); struct ndn_charbuf *pubkey = ndn_charbuf_create(); struct ndn_charbuf *keyid = ndn_charbuf_create(); struct ndn_charbuf *content = ndn_charbuf_create(); struct ndn_charbuf *cob = ndn_charbuf_create(); struct ndn_charbuf *answer = NULL; int res; res = ndn_get_public_key(h, NULL, pubid, pubkey); if (res < 0) goto Bail; if (ndn_name_from_uri(name, NDNRID_POLICY_URI) < 0) goto Bail; res |= ndn_charbuf_append_value(keyid, NDN_MARKER_CONTROL, 1); res |= ndn_charbuf_append_string(keyid, ".M.K"); res |= ndn_charbuf_append_value(keyid, 0, 1); res |= ndn_charbuf_append_charbuf(keyid, pubid); res |= ndn_name_append(name, keyid->buf, keyid->length); res |= ndn_create_version(h, name, NDN_V_NOW, 0, 0); if (ndn_name_from_uri(name, "%00") < 0) goto Bail; sp.sp_flags |= NDN_SP_FINAL_BLOCK; sp.type = NDN_CONTENT_LINK; res |= ndnb_append_Link(content, targetname, "Repository Policy", NULL); if (res != 0) goto Bail; res |= ndn_sign_content(h, cob, name, &sp, content->buf, content->length); if (res != 0) goto Bail; answer = cob; cob = NULL; Bail: ndn_charbuf_destroy(&name); ndn_charbuf_destroy(&pubid); ndn_charbuf_destroy(&pubkey); ndn_charbuf_destroy(&keyid); ndn_charbuf_destroy(&content); ndn_charbuf_destroy(&cob); return (answer); }
static void run_server(int sig_type, int sz) { printf("server (pid=%" PRIkernel_pid "): start\n", thread_getpid()); handle = ndn_app_create(); if (handle == NULL) { printf("server (pid=%" PRIkernel_pid "): cannot create app handle\n", thread_getpid()); return; } signature_type = sig_type; content_size = sz; ndn_shared_block_t* sp = ndn_name_from_uri(uri, strlen(uri)); if (sp == NULL) { printf("server (pid=%" PRIkernel_pid "): cannot create name from uri " "\"%s\"\n", handle->id, uri); return; } printf("server (pid=%" PRIkernel_pid "): register prefix \"%s\"\n", handle->id, uri); // pass ownership of "sp" to the API if (ndn_app_register_prefix(handle, sp, on_interest) != 0) { printf("server (pid=%" PRIkernel_pid "): failed to register prefix\n", handle->id); ndn_app_destroy(handle); return; } printf("server (pid=%" PRIkernel_pid "): enter app run loop\n", handle->id); ndn_app_run(handle); printf("server (pid=%" PRIkernel_pid "): returned from app run loop\n", handle->id); ndn_app_destroy(handle); }
static struct ndn_charbuf * ndnr_init_policy_cob(struct ndnr_handle *ndnr, struct ndn *h, struct ndn_charbuf *basename, int freshness, struct ndn_charbuf *content) { struct ndn_signing_params sp = NDN_SIGNING_PARAMS_INIT; struct ndn_charbuf *name = ndn_charbuf_create(); struct ndn_charbuf *pubid = ndn_charbuf_create(); struct ndn_charbuf *pubkey = ndn_charbuf_create(); struct ndn_charbuf *keyid = ndn_charbuf_create(); struct ndn_charbuf *tcob = ndn_charbuf_create(); struct ndn_charbuf *cob = NULL; // result int res; res = ndn_get_public_key(h, NULL, pubid, pubkey); if (res < 0) goto Leave; res = ndn_charbuf_append_charbuf(name, basename); if (ndn_name_from_uri(name, "%00") < 0) goto Leave; sp.sp_flags |= NDN_SP_FINAL_BLOCK; sp.type = NDN_CONTENT_DATA; sp.freshness = freshness; res |= ndn_sign_content(h, tcob, name, &sp, content->buf, content->length); if (res == 0) { cob = tcob; tcob = NULL; } Leave: ndn_charbuf_destroy(&name); ndn_charbuf_destroy(&pubid); ndn_charbuf_destroy(&pubkey); ndn_charbuf_destroy(&keyid); ndn_charbuf_destroy(&tcob); return (cob); }
int main(int argc, char **argv) { char *arg = NULL; struct ndn *ndnH = NULL; struct ndn_parsed_interest interest = {0}; struct ndn_charbuf *c = ndn_charbuf_create(); struct ndn_charbuf *templ = ndn_charbuf_create(); struct ndn_indexbuf *comps = ndn_indexbuf_create(); int i; int millis = 0; int secs = 0; int opt; int pool = 0; int regflgs = (NDN_FORW_CHILD_INHERIT | NDN_FORW_ACTIVE); int res; int status = 0; int val = 0; while ((opt = getopt(argc, argv, "hv")) != -1) { switch (opt) { default: case 'h': usage(); break; case 'v': verbose++; break; } } argc -= optind; argv += optind; ndnH = ndn_create(); if (ndn_connect(ndnH, NULL) == -1) { ndn_perror(ndnH, "ndn_connect"); exit(1); } for (i = 0; i < N_POOLS; i++) { store[i].me.p = &outgoing_content; store[i].me.data = &store[i]; store[i].me.intdata = i; incoming_content_action[i].p = &incoming_content; incoming_content_action[i].intdata = i; } for (i = 0; i < argc; i++) { arg = argv[i]; if (0 == strcmp(arg, "reconnect")) { if (argv[i+1] == NULL) usage(); secs = atoi(argv[i+1]); if (secs <= 0 && strcmp(argv[i+1], "0") != 0) usage(); i++; ndn_disconnect(ndnH); sleep(secs); if (ndn_connect(ndnH, NULL) == -1) { ndn_perror(ndnH, "ndn_connect"); exit(1); } continue; } if (0 == strcmp(arg, "pool")) { if (argv[i+1] == NULL) usage(); pool = argv[i+1][0] - '0'; if (argv[i+1][1] || pool < 0 || pool >= N_POOLS) usage(); fprintf(stderr, "Pool %d\n", pool); i++; continue; } if (0 == strcmp(arg, "prefix")) { if (argv[i+1] == NULL) usage(); c->length = 0; res = ndn_name_from_uri(c, argv[i+1]); if (res < 0) usage(); fprintf(stderr, "Prefix ff=%#x %s pool %d\n", regflgs, argv[i+1], pool); if (store[pool].me.intdata != pool) { abort(); } res = ndn_set_interest_filter_with_flags(ndnH, c, &store[pool].me, regflgs); if (res < 0) { ndn_perror(ndnH, "ndn_set_interest_filter_with_flags"); status = 1; } res = ndn_run(ndnH, 2); if (res < 0) break; i++; continue; } if (0 == strcmp(arg, "flags")) { if (argv[i+1] == NULL) usage(); regflgs = atoi(argv[i+1]); if (regflgs <= 0 && strcmp(argv[i+1], "0") != 0) usage(); i++; continue; } if (0 == strcmp(arg, "mincob")) { if (argv[i+1] == NULL) usage(); val = atoi(argv[i+1]); if (val <= 0 && strcmp(argv[i+1], "0") != 0) usage(); i++; if (n_pool(pool) < val) { fprintf(stderr, "Pool %d has %d cobs, expected at least %d\n", pool, n_pool(pool), val); exit(1); } continue; } if (0 == strcmp(arg, "maxcob")) { if (argv[i+1] == NULL) usage(); val = atoi(argv[i+1]); if (val <= 0 && strcmp(argv[i+1], "0") != 0) usage(); i++; if (n_pool(pool) > val) { fprintf(stderr, "Pool %d has %d cobs, expected at most %d\n", pool, n_pool(pool), val); exit(1); } continue; } if (0 == strcmp(arg, "run")) { if (argv[i+1] == NULL) usage(); millis = atoi(argv[i+1]); if (millis <= 0 && strcmp(argv[i+1], "0") != 0) usage(); i++; res = ndn_run(ndnH, millis); if (res < 0) { ndn_perror(ndnH, "ndn_run"); exit(1); } continue; } close(0); res = open(arg, O_RDONLY); if (res != 0) { perror(arg); exit(1); } fprintf(stderr, "Reading %s ... ", arg); rawlen = read(0, rawbuf, sizeof(rawbuf)); if (rawlen < 0) { perror("skipping"); // XXX - status continue; } // XXX - Should do a skeleton check before parse res = ndn_parse_interest(rawbuf, rawlen, &interest, NULL); if (res >= 0) { size_t name_start = interest.offset[NDN_PI_B_Name]; size_t name_size = interest.offset[NDN_PI_E_Name] - name_start; templ->length = 0; ndn_charbuf_append(templ, rawbuf, rawlen); fprintf(stderr, "Expressing interest with %d name components\n", res); c->length = 0; ndn_charbuf_append(c, rawbuf + name_start, name_size); // XXX - res is currently ignored ndn_express_interest(ndnH, c, &(incoming_content_action[pool]), templ); } else { struct ndn_parsed_ContentObject obj = {0}; int try; res = ndn_parse_ContentObject(rawbuf, rawlen, &obj, comps); if (res >= 0) { for (try = 0; try < 5; try++) { res = add_to_pool(pool, rawbuf, rawlen); if (res >= 0) { fprintf(stderr, "Added to pool %d\n", pool); break; } if (try == 5) { fprintf(stderr, "No buffer for %s\n", arg); status = 1; break; } fprintf(stderr, "Pool %d full - wait for drain\n", pool); if (ndn_run(ndnH, 1000) < 0) break; } res = ndn_run(ndnH, 10); } else { fprintf(stderr, "What is that?\n"); status = 1; } } res = ndn_run(ndnH, 10); if (res < 0) { ndn_perror(ndnH, "oops"); status = 1; } } res = ndn_run(ndnH, 10); if (res < 0) status = 1; ndn_destroy(&ndnH); exit(status); }
int main(int argc, char **argv) { const char *progname = argv[0]; struct ndn *ndn = NULL; struct ndn_charbuf *root = NULL; struct ndn_charbuf *name = NULL; struct ndn_charbuf *temp = NULL; struct ndn_charbuf *templ = NULL; struct ndn_signing_params sp = NDN_SIGNING_PARAMS_INIT; 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 ndn_closure in_content = {.p=&incoming_content, .data=&mydata}; struct ndn_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 (argc != 1) usage(progname); name = ndn_charbuf_create(); res = ndn_name_from_uri(name, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad NDN URI: %s\n", progname, argv[0]); exit(1); } ndn = ndn_create(); if (ndn_connect(ndn, NULL) == -1) { perror("Could not connect to ndnd"); exit(1); } buf = calloc(1, blocksize); root = name; name = ndn_charbuf_create(); temp = ndn_charbuf_create(); templ = ndn_charbuf_create(); /* Set up a handler for interests */ ndn_charbuf_append(name, root->buf, root->length); ndn_set_interest_filter(ndn, name, &in_interest); /* Initiate check to see whether there is already something there. */ ndn_charbuf_reset(temp); ndn_charbuf_putf(temp, "%d", 0); ndn_name_append(name, temp->buf, temp->length); ndn_charbuf_reset(templ); ndnb_element_begin(templ, NDN_DTAG_Interest); ndnb_element_begin(templ, NDN_DTAG_Name); ndnb_element_end(templ); /* </Name> */ ndnb_tagged_putf(templ, NDN_DTAG_MaxSuffixComponents, "%d", 1); // XXX - use pubid ndnb_element_end(templ); /* </Interest> */ res = ndn_express_interest(ndn, name, &in_content, templ); if (res < 0) abort(); sp.freshness = expire; for (i = 0;; i++) { read_res = read_full(0, buf, blocksize); if (read_res < 0) { perror("read"); read_res = 0; status = 1; } if (read_res < blocksize) { sp.sp_flags |= NDN_SP_FINAL_BLOCK; } ndn_charbuf_reset(name); ndn_charbuf_append(name, root->buf, root->length); ndn_charbuf_reset(temp); ndn_charbuf_putf(temp, "%d", i); ndn_name_append(name, temp->buf, temp->length); ndn_charbuf_reset(temp); ndn_charbuf_append(temp, buf, read_res); ndn_charbuf_reset(temp); res = ndn_sign_content(ndn, temp, name, &sp, buf, read_res); if (res != 0) { fprintf(stderr, "Failed to sign ContentObject (res == %d)\n", res); exit(1); } /* Put the keylocator in the first block only. */ sp.sp_flags |= NDN_SP_OMIT_KEY_LOCATOR; if (i == 0) { /* Finish check for old content */ if (mydata.content_received == 0) ndn_run(ndn, 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 = ndn_put(ndn, temp->buf, temp->length); if (res < 0) { fprintf(stderr, "ndn_put failed (res == %d)\n", res); exit(1); } if (read_res < blocksize) break; if (mydata.outstanding > 0) mydata.outstanding--; else res = 10; res = ndn_run(ndn, res * 100); if (res < 0) { status = 1; break; } } free(buf); buf = NULL; ndn_charbuf_destroy(&root); ndn_charbuf_destroy(&name); ndn_charbuf_destroy(&temp); ndn_destroy(&ndn); exit(status); }
/** * Process options and then loop through command line NDNx URIs retrieving * the data and writing it to stdout. */ int main(int argc, char **argv) { struct ndn *ndn = NULL; struct ndn_charbuf *name = NULL; struct ndn_charbuf *templ = NULL; struct ndn_closure *incoming = NULL; const char *arg = NULL; int i; int res; int opt; struct mydata *mydata; int allow_stale = 0; int *done; int exit_status = 0; done = calloc(1, sizeof(*done)); while ((opt = getopt(argc, argv, "ha")) != -1) { switch (opt) { case 'a': allow_stale = 1; break; case 'h': default: usage(argv[0]); } } arg = argv[optind]; if (arg == NULL) usage(argv[0]); name = ndn_charbuf_create(); /* Check the args first */ for (i = optind; argv[i] != NULL; i++) { name->length = 0; res = ndn_name_from_uri(name, argv[i]); if (res < 0) { fprintf(stderr, "%s: bad ndn URI: %s\n", argv[0], argv[i]); exit(1); } } for (i = optind; (arg = argv[i]) != NULL; i++) { *done = 0; name->length = 0; res = ndn_name_from_uri(name, arg); ndn = ndn_create(); if (ndn_connect(ndn, NULL) == -1) { perror("Could not connect to ndnd"); exit(1); } ndn_resolve_version(ndn, name, NDN_V_HIGHEST, 50); ndn_name_append_numeric(name, NDN_MARKER_SEQNUM, 0); incoming = calloc(1, sizeof(*incoming)); incoming->p = &incoming_content; mydata = calloc(1, sizeof(*mydata)); mydata->allow_stale = allow_stale; mydata->done = done; incoming->data = mydata; templ = make_template(mydata, NULL); ndn_express_interest(ndn, name, incoming, templ); ndn_charbuf_destroy(&templ); /* Run a little while to see if there is anything there */ res = ndn_run(ndn, 200); if ((!*done) && incoming->intdata == 0) { fprintf(stderr, "%s: not found: %s\n", argv[0], arg); res = -1; } /* We got something; run until end of data or somebody kills us */ while (res >= 0 && !*done) { fflush(stdout); res = ndn_run(ndn, 333); } if (res < 0) exit_status = 1; ndn_destroy(&ndn); fflush(stdout); free(incoming); incoming = NULL; } ndn_charbuf_destroy(&name); free(done); exit(exit_status); }
int main(int argc, char **argv) { const char *progname = argv[0]; struct ndn *ndn = NULL; struct ndn_charbuf *name = NULL; struct ndn_charbuf *pname = NULL; struct ndn_charbuf *temp = NULL; struct ndn_charbuf *extopt = NULL; long expire = -1; int versioned = 0; size_t blocksize = 8*1024; int status = 0; int res; ssize_t read_res; unsigned char *buf = NULL; enum ndn_content_type content_type = NDN_CONTENT_DATA; struct ndn_closure in_interest = {.p=&incoming_interest}; const char *postver = NULL; const char *key_uri = NULL; int force = 0; int verbose = 0; int timeout = -1; int setfinal = 0; int prefixcomps = -1; int fd; struct ndn_signing_params sp = NDN_SIGNING_PARAMS_INIT; while ((res = getopt(argc, argv, "e:fhk:lvV:p:t:w:x:")) != -1) { switch (res) { case 'e': if (extopt == NULL) extopt = ndn_charbuf_create(); fd = open(optarg, O_RDONLY); if (fd < 0) { perror(optarg); exit(1); } for (;;) { read_res = read(fd, ndn_charbuf_reserve(extopt, 64), 64); if (read_res <= 0) break; extopt->length += read_res; } if (read_res < 0) perror(optarg); close(fd); break; case 'f': force = 1; break; case 'l': setfinal = 1; // set FinalBlockID to last comp of name break; case 'k': key_uri = optarg; break; case 'p': prefixcomps = atoi(optarg); if (prefixcomps < 0) usage(progname); break; case 'x': expire = atol(optarg); if (expire <= 0) usage(progname); break; case 'v': verbose = 1; break; case 'V': versioned = 1; postver = optarg; if (0 == memcmp(postver, "%00", 3)) setfinal = 1; break; case 'w': timeout = atol(optarg); if (timeout <= 0) usage(progname); timeout *= 1000; break; case 't': if (0 == strcasecmp(optarg, "DATA")) { content_type = NDN_CONTENT_DATA; break; } if (0 == strcasecmp(optarg, "ENCR")) { content_type = NDN_CONTENT_ENCR; break; } if (0 == strcasecmp(optarg, "GONE")) { content_type = NDN_CONTENT_GONE; break; } if (0 == strcasecmp(optarg, "KEY")) { content_type = NDN_CONTENT_KEY; break; } if (0 == strcasecmp(optarg, "LINK")) { content_type = NDN_CONTENT_LINK; break; } if (0 == strcasecmp(optarg, "NACK")) { content_type = NDN_CONTENT_NACK; break; } content_type = atoi(optarg); if (content_type > 0 && content_type <= 0xffffff) break; fprintf(stderr, "Unknown content type %s\n", optarg); /* FALLTHRU */ default: case 'h': usage(progname); break; } } argc -= optind; argv += optind; if (argv[0] == NULL) usage(progname); name = ndn_charbuf_create(); res = ndn_name_from_uri(name, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad ndn URI: %s\n", progname, argv[0]); exit(1); } if (argv[1] != NULL) fprintf(stderr, "%s warning: extra arguments ignored\n", progname); /* Preserve the original prefix, in case we add versioning, * but trim it down if requested for the interest filter registration */ pname = ndn_charbuf_create(); ndn_charbuf_append(pname, name->buf, name->length); if (prefixcomps >= 0) { res = ndn_name_chop(pname, NULL, prefixcomps); if (res < 0) { fprintf(stderr, "%s: unable to trim name to %d component%s.\n", progname, prefixcomps, prefixcomps == 1 ? "" : "s"); exit(1); } } /* Connect to ndnd */ ndn = ndn_create(); if (ndn_connect(ndn, NULL) == -1) { perror("Could not connect to ndnd"); exit(1); } /* Read the actual user data from standard input */ buf = calloc(1, blocksize); read_res = read_full(0, buf, blocksize); if (read_res < 0) { perror("read"); read_res = 0; status = 1; } /* Tack on the version component if requested */ if (versioned) { res = ndn_create_version(ndn, name, NDN_V_REPLACE | NDN_V_NOW | NDN_V_HIGH, 0, 0); if (res < 0) { fprintf(stderr, "%s: ndn_create_version() failed\n", progname); exit(1); } if (postver != NULL) { res = ndn_name_from_uri(name, postver); if (res < 0) { fprintf(stderr, "-V %s: invalid name suffix\n", postver); exit(0); } } } temp = ndn_charbuf_create(); /* Ask for a FinalBlockID if appropriate. */ if (setfinal) sp.sp_flags |= NDN_SP_FINAL_BLOCK; if (res < 0) { fprintf(stderr, "Failed to create signed_info (res == %d)\n", res); exit(1); } /* Set content type */ sp.type = content_type; /* Set freshness */ if (expire >= 0) { if (sp.template_ndnb == NULL) { sp.template_ndnb = ndn_charbuf_create(); ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_SignedInfo, NDN_DTAG); } else if (sp.template_ndnb->length > 0) { sp.template_ndnb->length--; } ndnb_tagged_putf(sp.template_ndnb, NDN_DTAG_FreshnessSeconds, "%ld", expire); sp.sp_flags |= NDN_SP_TEMPL_FRESHNESS; ndn_charbuf_append_closer(sp.template_ndnb); } /* Set key locator, if supplied */ if (key_uri != NULL) { struct ndn_charbuf *c = ndn_charbuf_create(); res = ndn_name_from_uri(c, key_uri); if (res < 0) { fprintf(stderr, "%s is not a valid ndnx URI\n", key_uri); exit(1); } if (sp.template_ndnb == NULL) { sp.template_ndnb = ndn_charbuf_create(); ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_SignedInfo, NDN_DTAG); } else if (sp.template_ndnb->length > 0) { sp.template_ndnb->length--; } ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_KeyLocator, NDN_DTAG); ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_KeyName, NDN_DTAG); ndn_charbuf_append(sp.template_ndnb, c->buf, c->length); ndn_charbuf_append_closer(sp.template_ndnb); ndn_charbuf_append_closer(sp.template_ndnb); sp.sp_flags |= NDN_SP_TEMPL_KEY_LOCATOR; ndn_charbuf_append_closer(sp.template_ndnb); ndn_charbuf_destroy(&c); } if (extopt != NULL && extopt->length > 0) { if (sp.template_ndnb == NULL) { sp.template_ndnb = ndn_charbuf_create(); ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_SignedInfo, NDN_DTAG); } else if (sp.template_ndnb->length > 0) { sp.template_ndnb->length--; } ndnb_append_tagged_blob(sp.template_ndnb, NDN_DTAG_ExtOpt, extopt->buf, extopt->length); sp.sp_flags |= NDN_SP_TEMPL_EXT_OPT; ndn_charbuf_append_closer(sp.template_ndnb); } /* Create the signed content object, ready to go */ temp->length = 0; res = ndn_sign_content(ndn, temp, name, &sp, buf, read_res); if (res != 0) { fprintf(stderr, "Failed to encode ContentObject (res == %d)\n", res); exit(1); } if (read_res == blocksize) { read_res = read_full(0, buf, 1); if (read_res == 1) { fprintf(stderr, "%s: warning - truncated data\n", argv[0]); status = 1; } } free(buf); buf = NULL; if (force) { /* At user request, send without waiting to see an interest */ res = ndn_put(ndn, temp->buf, temp->length); if (res < 0) { fprintf(stderr, "ndn_put failed (res == %d)\n", res); exit(1); } } else { in_interest.data = temp; /* Set up a handler for interests */ res = ndn_set_interest_filter(ndn, pname, &in_interest); if (res < 0) { fprintf(stderr, "Failed to register interest (res == %d)\n", res); exit(1); } res = ndn_run(ndn, timeout); if (in_interest.intdata == 0) { if (verbose) fprintf(stderr, "Nobody's interested\n"); exit(1); } } if (verbose) { struct ndn_charbuf *uri = ndn_charbuf_create(); uri->length = 0; ndn_uri_append(uri, name->buf, name->length, 1); printf("wrote %s\n", ndn_charbuf_as_string(uri)); ndn_charbuf_destroy(&uri); } ndn_destroy(&ndn); ndn_charbuf_destroy(&name); ndn_charbuf_destroy(&pname); ndn_charbuf_destroy(&temp); ndn_charbuf_destroy(&sp.template_ndnb); ndn_charbuf_destroy(&extopt); exit(status); }
int main(int argc, char *argv[]) { const char *progname = argv[0]; struct ndn_ping_client client = {.identifier = 0, .interval = 1, .sent = 0, .received = 0, .total = -1, .number = -1, .print_timestamp = 0, .allow_caching = 0}; struct ndn_closure in_content = {.p = &incoming_content}; struct hashtb_param param = {0}; int res; struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = &handle_interrupt; sigaction(SIGINT, &sa, &osa); memset(&sta, 0, sizeof(sta)); gettimeofday(&sta.start, 0); sta.min = INT_MAX; while ((res = getopt(argc, argv, "htai:c:n:p:")) != -1) { switch (res) { case 'c': client.total = atol(optarg); if (client.total <= 0) usage(progname); break; case 'i': client.interval = atof(optarg); if (client.interval < PING_MIN_INTERVAL) usage(progname); break; case 'n': client.number = atol(optarg); if (client.number < 0) usage(progname); break; case 'p': client.identifier = optarg; if (!is_valid_identifier(client.identifier)) usage(progname); break; case 't': client.print_timestamp = 1; break; case 'a': client.allow_caching = 1; break; case 'h': default: usage(progname); break; } } if (client.number < 0) srandom(time(NULL) * getpid()); argc -= optind; argv += optind; if (argv[0] == NULL) usage(progname); sta.prefix = argv[0]; client.original_prefix = argv[0]; client.prefix = ndn_charbuf_create(); res = ndn_name_from_uri(client.prefix, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad ndn URI: %s\n", progname, argv[0]); exit(1); } if (argv[1] != NULL) fprintf(stderr, "%s warning: extra arguments ignored\n", progname); // Append "/ping" to the given name prefix. res = ndn_name_append_str(client.prefix, PING_COMPONENT); if (res < 0) { fprintf(stderr, "%s: error constructing ndn URI: %s/%s\n", progname, argv[0], PING_COMPONENT); exit(1); } // Append identifier if not empty. if (client.identifier) { res = ndn_name_append_str(client.prefix, client.identifier); if (res < 0) { fprintf(stderr, "%s: error constructing ndn URI: %s/%s/%s\n", progname, argv[0], PING_COMPONENT, client.identifier); exit(1); } } // Connect to ndnd. client.h = ndn_create(); if (ndn_connect(client.h, NULL) == -1) { perror("Could not connect to ndnd"); exit(1); } client.closure = &in_content; in_content.data = &client; client.ndn_ping_table = hashtb_create(sizeof(struct ndn_ping_entry), ¶m); client.sched = ndn_schedule_create(&client, &ndn_ping_ticker); client.event = ndn_schedule_event(client.sched, 0, &do_ping, NULL, 0); print_log(client.print_timestamp, "NDNPING %s\n", client.original_prefix); res = 0; while (res >= 0 && (client.total <= 0 || client.sent < client.total || hashtb_n(client.ndn_ping_table) > 0)) { if (client.total <= 0 || client.sent < client.total) ndn_schedule_run(client.sched); res = ndn_run(client.h, 10); } ndn_schedule_destroy(&client.sched); ndn_destroy(&client.h); ndn_charbuf_destroy(&client.prefix); print_statistics(); return 0; }