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 *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); }