bool hash_store_build_reply(struct hash_store* self, struct ndn_charbuf* reply, const uint8_t* hash, const uint8_t* payload, size_t payloadsz) { ndnb_element_begin(reply, NDN_DTAG_ContentObject); ndnb_element_begin(reply, NDN_DTAG_Signature); ndnb_tagged_putf(reply, NDN_DTAG_DigestAlgorithm, "SHA256"); ndnb_append_tagged_blob(reply, NDN_DTAG_SignatureBits, hash, SEGMENT_HASHSZ); ndnb_element_end(reply);//Signature ndnb_element_begin(reply, NDN_DTAG_Name); ndnb_append_tagged_blob(reply, NDN_DTAG_Component, REPO_SHA256, sizeof(REPO_SHA256)-1); ndnb_append_tagged_blob(reply, NDN_DTAG_Component, hash, SEGMENT_HASHSZ); ndnb_element_end(reply);//Name ndnb_element_begin(reply, NDN_DTAG_SignedInfo); ndnb_append_tagged_blob(reply, NDN_DTAG_PublisherPublicKeyDigest, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 32); ndnb_element_begin(reply, NDN_DTAG_Timestamp); ndnb_append_now_blob(reply, NDN_MARKER_NONE); ndnb_element_end(reply);//Timestamp ndnb_element_end(reply);//SignedInfo ndnb_append_tagged_blob(reply, NDN_DTAG_Content, payload, payloadsz); ndnb_element_end(reply);//ContentObject return true; }
int ndnb_append_forwarding_entry(struct ndn_charbuf *c, const struct ndn_forwarding_entry *fe) { int res; res = ndnb_element_begin(c, NDN_DTAG_ForwardingEntry); if (fe->action != NULL) res |= ndnb_tagged_putf(c, NDN_DTAG_Action, "%s", fe->action); if (fe->name_prefix != NULL && fe->name_prefix->length > 0) res |= ndn_charbuf_append(c, fe->name_prefix->buf, fe->name_prefix->length); if (fe->ndnd_id_size != 0) res |= ndnb_append_tagged_blob(c, NDN_DTAG_PublisherPublicKeyDigest, fe->ndnd_id, fe->ndnd_id_size); if (fe->faceid != ~0) res |= ndnb_tagged_putf(c, NDN_DTAG_FaceID, "%u", fe->faceid); if (fe->flags >= 0) res |= ndnb_tagged_putf(c, NDN_DTAG_ForwardingFlags, "%d", fe->flags); if (fe->lifetime >= 0) res |= ndnb_tagged_putf(c, NDN_DTAG_FreshnessSeconds, "%d", fe->lifetime); res |= ndnb_element_end(c); return(res); }
int ndnb_append_header(struct ndn_charbuf *c, const struct ndn_header *h) { int res; res = ndnb_element_begin(c, NDN_DTAG_Header); res |= ndnb_tagged_putf(c, NDN_DTAG_Start, "%u", h->start); res |= ndnb_tagged_putf(c, NDN_DTAG_Count, "%u", h->count); res |= ndnb_tagged_putf(c, NDN_DTAG_BlockSize, "%u", h->block_size); res |= ndnb_tagged_putf(c, NDN_DTAG_Length, "%u", h->length); if (h->content_digest != NULL) { res |= ndnb_append_tagged_blob(c, NDN_DTAG_ContentDigest, h->content_digest->buf, h->content_digest->length); } if (h->root_digest != NULL) { res |= ndnb_append_tagged_blob(c, NDN_DTAG_RootDigest, h->root_digest->buf, h->root_digest->length); } res |= ndnb_element_end(c); return (res); }
/** * Marshal an internal face instance representation into ndnb form */ int ndnb_append_face_instance(struct ndn_charbuf *c, const struct ndn_face_instance *fi) { int res; res = ndnb_element_begin(c, NDN_DTAG_FaceInstance); if (fi->action != NULL) res |= ndnb_tagged_putf(c, NDN_DTAG_Action, "%s", fi->action); if (fi->ndnd_id_size != 0) res |= ndnb_append_tagged_blob(c, NDN_DTAG_PublisherPublicKeyDigest, fi->ndnd_id, fi->ndnd_id_size); if (fi->faceid != ~0) res |= ndnb_tagged_putf(c, NDN_DTAG_FaceID, "%u", fi->faceid); if (fi->descr.ipproto >= 0) res |= ndnb_tagged_putf(c, NDN_DTAG_IPProto, "%d", fi->descr.ipproto); if (fi->descr.address != NULL) res |= ndnb_tagged_putf(c, NDN_DTAG_Host, "%s", fi->descr.address); if (fi->descr.port != NULL) res |= ndnb_tagged_putf(c, NDN_DTAG_Port, "%s", fi->descr.port); if (fi->descr.source_address != NULL) res |= ndnb_tagged_putf(c, NDN_DTAG_MulticastInterface, "%s", fi->descr.source_address); if (fi->descr.mcast_ttl >= 0 && fi->descr.mcast_ttl != 1) res |= ndnb_tagged_putf(c, NDN_DTAG_MulticastTTL, "%d", fi->descr.mcast_ttl); if (fi->lifetime >= 0) res |= ndnb_tagged_putf(c, NDN_DTAG_FreshnessSeconds, "%d", fi->lifetime); res |= ndnb_element_end(c); return(res); }
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); }