// fill in the compVector (watch out: this modifies the uri string) int ccnl_URItoComponents(char **compVector, unsigned int *compLens, char *uri) { int i, len; if (*uri == '/') uri++; for (i = 0; *uri && i < (CCNL_MAX_NAME_COMP - 1); i++) { compVector[i] = uri; while (*uri && *uri != '/') { uri++; } if (*uri) { *uri = '\0'; uri++; } len = unescape_component(compVector[i]); if (compLens) compLens[i] = len; compVector[i][len] = '\0'; } compVector[i] = NULL; return i; }
int main(int argc, char *argv[]) { // char *private_key_path = 0; // char *witness = 0; unsigned char out[65*1024]; char *publisher = 0; char *infname = 0, *outdirname = 0, *outfname; int f, fout, contentlen = 0, opt, plen; // int suite = CCNL_SUITE_DEFAULT; int suite = CCNL_SUITE_CCNTLV; int chunk_size = CCNL_MAX_CHUNK_SIZE; struct ccnl_prefix_s *name; while ((opt = getopt(argc, argv, "hc:f:i:o:p:k:w:s:v:")) != -1) { switch (opt) { case 'c': chunk_size = atoi(optarg); if (chunk_size > CCNL_MAX_CHUNK_SIZE) { DEBUGMSG(WARNING, "max chunk size is %d (%d is to large), using max chunk size\n", CCNL_MAX_CHUNK_SIZE, chunk_size); chunk_size = CCNL_MAX_CHUNK_SIZE; } break; case 'f': outfname = optarg; break; case 'i': infname = optarg; break; case 'o': outdirname = optarg; break; /* case 'k': private_key_path = optarg; break; case 'w': witness = optarg; break; */ case 'p': publisher = optarg; plen = unescape_component(publisher); if (plen != 32) { DEBUGMSG(ERROR, "publisher key digest has wrong length (%d instead of 32)\n", plen); exit(-1); } break; case 's': suite = ccnl_str2suite(optarg); break; case 'v': #ifdef USE_LOGGING if (isdigit(optarg[0])) debug_level = atoi(optarg); else debug_level = ccnl_debug_str2level(optarg); #endif break; case 'h': default: Usage: fprintf(stderr, "Creates a chunked content object stream for the input data and writes them to stdout.\n" "usage: %s [options] URL\n" " -c SIZE size for each chunk (max %d)\n" " -f FNAME filename of the chunks when using -o\n" " -i FNAME input file (instead of stdin)\n" " -o DIR output dir (instead of stdout), filename default is cN, otherwise specify -f\n" " -p DIGEST publisher fingerprint\n" " -s SUITE (ccnb, ccnx2015, cisco2015, iot2014, ndn2013)\n" #ifdef USE_LOGGING " -v DEBUG_LEVEL (fatal, error, warning, info, debug, verbose, trace)\n" #endif , argv[0], CCNL_MAX_CHUNK_SIZE); exit(1); } } if (!ccnl_isSuite(suite)) goto Usage; // mandatory url if (!argv[optind]) goto Usage; char *url_orig = argv[optind]; char url[strlen(url_orig)]; optind++; // optional nfn char *nfnexpr = argv[optind]; int status; struct stat st_buf; if(outdirname) { // Check if outdirname is a directory and open it as a file status = stat(outdirname, &st_buf); if (status != 0) { // DEBUGMSG (ERROR, "Error (%d) when opening file %s\n", errno, outdirname); DEBUGMSG(ERROR, "Error (%d) when opening output dir %s (probaby does not exist)\n", errno, outdirname); goto Usage; } if (S_ISREG (st_buf.st_mode)) { // DEBUGMSG (ERROR, "Error: %s is a file and not a directory.\n", argv[optind]); DEBUGMSG(ERROR, "Error: output dir %s is a file and not a directory.\n", outdirname); goto Usage; } } if(infname) { // Check if outdirname is a directory and open it as a file status = stat(infname, &st_buf); if (status != 0) { // DEBUGMSG (ERROR, "Error (%d) when opening file %s\n", errno, outdirname); DEBUGMSG(ERROR, "Error (%d) when opening input file %s (probaby does not exist)\n", errno, infname); goto Usage; } if (S_ISDIR (st_buf.st_mode)) { // DEBUGMSG (ERROR, "Error: %s is a file and not a directory.\n", argv[optind]); DEBUGMSG(ERROR, "Error: input file %s is a directory and not a file.\n", infname); goto Usage; } f = open(infname, O_RDONLY); if (f < 0) { perror("file open:"); } } else { f = 0; } char default_file_name[2] = "c"; if (!outfname) { outfname = default_file_name; } else if(!outdirname) { DEBUGMSG(WARNING, "filename -f without -o output dir does nothing\n"); } char *chunk_buf; chunk_buf = ccnl_malloc(chunk_size * sizeof(unsigned char)); int chunk_len, is_last = 0, offs = -1; unsigned int chunknum = 0; char outpathname[255]; char fileext[10]; switch (suite) { case CCNL_SUITE_CCNB: strcpy(fileext, "ccnb"); break; case CCNL_SUITE_CCNTLV: strcpy(fileext, "ccntlv"); break; case CCNL_SUITE_CISTLV: strcpy(fileext, "cistlv"); break; case CCNL_SUITE_IOTTLV: strcpy(fileext, "iottlv"); break; case CCNL_SUITE_NDNTLV: strcpy(fileext, "ndntlv"); break; default: DEBUGMSG(ERROR, "fileext for suite %d not implemented\n", suite); } chunk_len = 1; chunk_len = read(f, chunk_buf, chunk_size); while (!is_last && chunk_len > 0) { if (chunk_len < chunk_size) { is_last = 1; } strcpy(url, url_orig); offs = CCNL_MAX_PACKET_SIZE; name = ccnl_URItoPrefix(url, suite, nfnexpr, &chunknum); switch (suite) { case CCNL_SUITE_CCNTLV: contentlen = ccnl_ccntlv_prependContentWithHdr(name, (unsigned char *)chunk_buf, chunk_len, is_last ? &chunknum : NULL, NULL, // int *contentpos &offs, out); break; case CCNL_SUITE_CISTLV: contentlen = ccnl_cistlv_prependContentWithHdr(name, (unsigned char *)chunk_buf, chunk_len, is_last ? &chunknum : NULL, &offs, NULL, // int *contentpos out); break; case CCNL_SUITE_IOTTLV: ccnl_iottlv_prependReply(name, (unsigned char *) chunk_buf, chunk_len, &offs, NULL, is_last ? &chunknum : NULL, out); ccnl_switch_prependCoding(CCNL_ENC_IOT2014, &offs, out); contentlen = CCNL_MAX_PACKET_SIZE - offs; break; case CCNL_SUITE_NDNTLV: contentlen = ccnl_ndntlv_prependContent(name, (unsigned char *) chunk_buf, chunk_len, NULL, is_last ? &chunknum : NULL, &offs, out); break; default: DEBUGMSG(ERROR, "produce for suite %i is not implemented\n", suite); goto Error; break; } if (outdirname) { sprintf(outpathname, "%s/%s%d.%s", outdirname, outfname, chunknum, fileext); // DEBUGMSG(INFO, "%s/%s%d.%s\n", outdirname, outfname, chunknum, fileext); DEBUGMSG(INFO, "writing chunk %d to file %s\n", chunknum, outpathname); fout = creat(outpathname, 0666); write(fout, out + offs, contentlen); close(fout); } else { DEBUGMSG(INFO, "writing chunk %d\n", chunknum); fwrite(out + offs, sizeof(unsigned char),contentlen, stdout); } chunknum++; if (!is_last) { chunk_len = read(f, chunk_buf, chunk_size); } } close(f); ccnl_free(chunk_buf); return 0; Error: close(f); ccnl_free(chunk_buf); return -1; }
int main(int argc, char *argv[]) { unsigned char out[CCNL_MAX_PACKET_SIZE]; char *minSuffix = 0, *maxSuffix = 0, *scope = 0; char *digest = 0, *publisher = 0; char *fname = 0; int f, len=0, opt; int dlen = 0, plen = 0; int packettype = CCNL_SUITE_NDNTLV; struct ccnl_prefix_s *prefix; time_t curtime; uint32_t nonce; int isLambda = 0; unsigned int chunknum = UINT_MAX; time(&curtime); // Get current time in double to avoid dealing with time_t nonce = (uint32_t) difftime(curtime, 0); while ((opt = getopt(argc, argv, "ha:c:d:e:i:ln:o:p:s:v:x:")) != -1) { switch (opt) { case 'a': minSuffix = optarg; break; case 'c': scope = optarg; break; case 'd': digest = optarg; dlen = unescape_component(digest); if (dlen != 32) { DEBUGMSG(ERROR, "digest has wrong length (%d instead of 32)\n", dlen); exit(-1); } break; case 'e': nonce = atoi(optarg); break; case 'l': isLambda = 1 - isLambda; break; case 'n': chunknum = atoi(optarg); break; case 'o': fname = optarg; break; case 'p': publisher = optarg; plen = unescape_component(publisher); if (plen != 32) { DEBUGMSG(ERROR, "publisher key digest has wrong length (%d instead of 32)\n", plen); exit(-1); } break; case 'v': #ifdef USE_LOGGING if (isdigit(optarg[0])) debug_level = atoi(optarg); else debug_level = ccnl_debug_str2level(optarg); #endif break; case 'x': maxSuffix = optarg; break; case 's': packettype = ccnl_str2suite(optarg); if (packettype >= 0 && packettype < CCNL_SUITE_LAST) break; case 'h': default: Usage: fprintf(stderr, "usage: %s [options] URI [NFNexpr]\n" " -a LEN miN additional components\n" " -c SCOPE\n" " -d DIGEST content digest (sets -x to 0)\n" " -e NONCE random 4 bytes\n" " -l URI is a Lambda expression\n" " -n CHUNKNUM positive integer for chunk interest\n" " -o FNAME output file (instead of stdout)\n" " -p DIGEST publisher fingerprint\n" " -s SUITE (ccnb, ccnx2015, cisco2015, iot2014, ndn2013)\n" #ifdef USE_LOGGING " -v DEBUG_LEVEL (fatal, error, warning, info, debug, verbose, trace)\n" #endif " -x LEN maX additional components\n", argv[0]); exit(1); } } if (!argv[optind]) goto Usage; /* if (isLambda) i = ccnl_lambdaStrToComponents(prefix, argv[optind]); else */ prefix = ccnl_URItoPrefix(argv[optind], packettype, argv[optind+1], chunknum == UINT_MAX ? NULL : &chunknum); if (!prefix) { DEBUGMSG(ERROR, "no URI found, aborting\n"); return -1; } switch (packettype) { case CCNL_SUITE_CCNB: len = ccnl_ccnb_mkInterest(prefix, minSuffix, maxSuffix, (unsigned char*) digest, dlen, (unsigned char*) publisher, plen, scope, &nonce, out); break; case CCNL_SUITE_CCNTLV: len = ccntlv_mkInterest(prefix, (int*)&nonce, out, CCNL_MAX_PACKET_SIZE); break; case CCNL_SUITE_CISTLV: len = cistlv_mkInterest(prefix, (int*)&nonce, out, CCNL_MAX_PACKET_SIZE); break; case CCNL_SUITE_IOTTLV: len = iottlv_mkRequest(prefix, NULL, out, CCNL_MAX_PACKET_SIZE); break; case CCNL_SUITE_NDNTLV: len = ndntlv_mkInterest(prefix, (int*)&nonce, out, CCNL_MAX_PACKET_SIZE); break; default: DEBUGMSG(ERROR, "Not Implemented (yet)\n"); return -1; } if (len <= 0) { DEBUGMSG(ERROR, "internal error: empty packet\n"); return -1; } if (fname) { f = creat(fname, 0666); if (f < 0) { perror("file open:"); return -1; } } else f = 1; write(f, out, len); close(f); return 0; }
int mkContent(char **namecomp, unsigned char *publisher, int plen, unsigned char *body, int blen, unsigned char *out) { int len = 0, k; len = mkHeader(out, CCN_DTAG_CONTENTOBJ, CCN_TT_DTAG); // interest // add signature #ifdef USE_SIGNATURES if(private_key_path) len += add_signature(out+len, private_key_path, body, blen); #endif len += mkHeader(out+len, CCN_DTAG_NAME, CCN_TT_DTAG); // name while (*namecomp) { len += mkHeader(out+len, CCN_DTAG_COMPONENT, CCN_TT_DTAG); // comp k = unescape_component((unsigned char*) *namecomp); len += mkHeader(out+len, k, CCN_TT_BLOB); memcpy(out+len, *namecomp++, k); len += k; out[len++] = 0; // end-of-component } out[len++] = 0; // end-of-name if (publisher) { struct timeval t; unsigned char tstamp[6]; uint32_t *sec; uint16_t *secfrac; gettimeofday(&t, NULL); sec = (uint32_t*)(tstamp + 0); // big endian *sec = htonl(t.tv_sec); secfrac = (uint16_t*)(tstamp + 4); *secfrac = htons(4048L * t.tv_usec / 1000000); len += mkHeader(out+len, CCN_DTAG_TIMESTAMP, CCN_TT_DTAG); len += mkHeader(out+len, sizeof(tstamp), CCN_TT_BLOB); memcpy(out+len, tstamp, sizeof(tstamp)); len += sizeof(tstamp); out[len++] = 0; // end-of-timestamp len += mkHeader(out+len, CCN_DTAG_SIGNEDINFO, CCN_TT_DTAG); len += mkHeader(out+len, CCN_DTAG_PUBPUBKDIGEST, CCN_TT_DTAG); len += mkHeader(out+len, plen, CCN_TT_BLOB); memcpy(out+len, publisher, plen); len += plen; out[len++] = 0; // end-of-publisher out[len++] = 0; // end-of-signedinfo } len += mkHeader(out+len, CCN_DTAG_CONTENT, CCN_TT_DTAG); len += mkHeader(out+len, blen, CCN_TT_BLOB); memcpy(out + len, body, blen); len += blen; out[len++] = 0; // end-of-content out[len++] = 0; // end-of-contentobj return len; }
int main(int argc, char *argv[]) { unsigned char body[64*1024]; unsigned char out[65*1024]; unsigned char *publisher = 0; char *infname = 0, *outfname = 0; int i = 0, f, len, opt, plen; char *prefix[CCNL_MAX_NAME_COMP], *cp; private_key_path = 0; witness = 0; while ((opt = getopt(argc, argv, "hi:o:p:k:w:")) != -1) { switch (opt) { case 'i': infname = optarg; break; case 'o': outfname = optarg; break; case 'k': private_key_path = optarg; break; case 'w': witness = optarg; break; case 'p': publisher = (unsigned char*)optarg; plen = unescape_component(publisher); if (plen != 32) { fprintf(stderr, "publisher key digest has wrong length (%d instead of 32)\n", plen); exit(-1); } break; case 'h': default: Usage: fprintf(stderr, "usage: %s [options] URI\n" " -i FNAME input file (instead of stdin)\n" " -o FNAME output file (instead of stdout)\n" " -p DIGEST publisher fingerprint\n" " -k FNAME publisher private key\n" " -w STRING witness\n" , argv[0]); exit(1); } } if (!argv[optind]) goto Usage; cp = strtok(argv[optind], "/"); while (i < (CCNL_MAX_NAME_COMP - 1) && cp) { prefix[i++] = cp; cp = strtok(NULL, "/"); } prefix[i] = NULL; if (infname) { f = open(infname, O_RDONLY); if (f < 0) perror("file open:"); } else f = 0; len = read(f, body, sizeof(body)); close(f); len = mkContent(prefix, publisher, plen, body, len, out); if (outfname) { f = creat(outfname, 0666); if (f < 0) perror("file open:"); } else f = 1; write(f, out, len); close(f); return 0; }
int main(int argc, char *argv[]) { // char *private_key_path = 0; // char *witness = 0; uint8_t out[65*1024]; char *publisher = 0; char *infname = 0, *outdirname = 0, *outfname = 0; size_t contentlen = 0, plen; int f, fout, opt; // int suite = CCNL_SUITE_DEFAULT; int suite = CCNL_SUITE_CCNTLV; size_t chunk_size = CCNL_MAX_CHUNK_SIZE; struct ccnl_prefix_s *name; ccnl_data_opts_u data_opts; while ((opt = getopt(argc, argv, "hc:f:i:o:p:k:w:s:v:")) != -1) { switch (opt) { case 'c': chunk_size = (size_t) strtol(optarg, (char **) NULL, 10); if (chunk_size > CCNL_MAX_CHUNK_SIZE) { DEBUGMSG(WARNING, "max chunk size is %d (%zu is to large), using max chunk size\n", CCNL_MAX_CHUNK_SIZE, chunk_size); chunk_size = CCNL_MAX_CHUNK_SIZE; } break; case 'f': outfname = optarg; break; case 'i': infname = optarg; break; case 'o': outdirname = optarg; break; /* case 'k': private_key_path = optarg; break; case 'w': witness = optarg; break; */ case 'p': publisher = optarg; plen = unescape_component(publisher); if (plen != 32) { DEBUGMSG(ERROR, "publisher key digest has wrong length (%zu instead of 32)\n", plen); exit(-1); } break; case 's': suite = ccnl_str2suite(optarg); break; case 'v': #ifdef USE_LOGGING if (isdigit(optarg[0])) debug_level = (int)strtol(optarg, (char **)NULL, 10); else debug_level = ccnl_debug_str2level(optarg); #endif break; case 'h': default: Usage: fprintf(stderr, "Creates a chunked content object stream for the input data and writes them to stdout.\n" "usage: %s [options] URL\n" " -c SIZE size for each chunk (max %d)\n" " -f FNAME filename of the chunks when using -o\n" " -i FNAME input file (instead of stdin)\n" " -o DIR output dir (instead of stdout), filename default is cN, otherwise specify -f\n" " -p DIGEST publisher fingerprint\n" " -s SUITE (ccnb, ccnx2015, ndn2013)\n" #ifdef USE_LOGGING " -v DEBUG_LEVEL (fatal, error, warning, info, debug, verbose, trace)\n" #endif , argv[0], CCNL_MAX_CHUNK_SIZE); exit(1); } } if (!ccnl_isSuite(suite)) { goto Usage; } // mandatory url if (!argv[optind]) { goto Usage; } char *url_orig = argv[optind]; char url[strlen(url_orig)]; optind++; int status; struct stat st_buf; if (outdirname) { // Check if outdirname is a directory and open it as a file status = stat(outdirname, &st_buf); if (status != 0) { // DEBUGMSG (ERROR, "Error (%d) when opening file %s\n", errno, outdirname); DEBUGMSG(ERROR, "Error (%d) when opening output dir %s (probaby does not exist)\n", errno, outdirname); goto Usage; } if (S_ISREG (st_buf.st_mode)) { // DEBUGMSG (ERROR, "Error: %s is a file and not a directory.\n", argv[optind]); DEBUGMSG(ERROR, "Error: output dir %s is a file and not a directory.\n", outdirname); goto Usage; } } if (infname) { // Check if outdirname is a directory and open it as a file status = stat(infname, &st_buf); if (status != 0) { // DEBUGMSG (ERROR, "Error (%d) when opening file %s\n", errno, outdirname); DEBUGMSG(ERROR, "Error (%d) when opening input file %s (probaby does not exist)\n", errno, infname); goto Usage; } if (S_ISDIR (st_buf.st_mode)) { // DEBUGMSG (ERROR, "Error: %s is a file and not a directory.\n", argv[optind]); DEBUGMSG(ERROR, "Error: input file %s is a directory and not a file.\n", infname); goto Usage; } f = open(infname, O_RDONLY); if (f < 0) { perror("file open:"); } } else { f = 0; } char default_file_name[2] = "c"; if (!outfname) { outfname = default_file_name; } else if(!outdirname) { DEBUGMSG(WARNING, "filename -f without -o output dir does nothing\n"); } uint8_t *chunk_buf; chunk_buf = ccnl_malloc(chunk_size * sizeof(uint8_t)); if (!chunk_buf) { DEBUGMSG(ERROR, "Error: Failed to allocate memory\n"); exit(1); } size_t chunk_len; ssize_t s_chunk_len; int8_t is_last = 0; size_t offs; uint32_t chunknum = 0; char outpathname[255]; char fileext[10]; switch (suite) { case CCNL_SUITE_CCNB: strcpy(fileext, "ccnb"); break; case CCNL_SUITE_CCNTLV: strcpy(fileext, "ccntlv"); break; case CCNL_SUITE_NDNTLV: strcpy(fileext, "ndntlv"); break; default: DEBUGMSG(ERROR, "fileext for suite %d not implemented\n", suite); } FILE *fp = fopen(infname, "r"); fseek(fp, 0L, SEEK_END); long sz = ftell(fp); size_t isz; if (sz < 0) { DEBUGMSG(ERROR, "Error reading input file offset; error: %d\n", errno); exit(1); } if ((unsigned long) sz > SIZE_MAX) { DEBUGMSG(ERROR, "Input file offset exceeds bounds: %ld", sz); exit(1); } isz = (size_t) sz; rewind(fp); fclose(fp); size_t lastchunknum_s = (isz / chunk_size); if (lastchunknum_s > UINT32_MAX) { DEBUGMSG(ERROR, "lastchunknum exceeds bounds: %zu", lastchunknum_s); exit(1); } uint32_t lastchunknum = (uint32_t) lastchunknum_s; if (sz % chunk_size == 0) { --lastchunknum; } s_chunk_len = read(f, chunk_buf, chunk_size); if (s_chunk_len < 0) { DEBUGMSG(ERROR, "Error reading input file; error: %d\n", errno); exit(1); } chunk_len = (size_t) s_chunk_len; while (!is_last && chunk_len > 0) { if (chunk_len < chunk_size) { is_last = 1; } strcpy(url, url_orig); offs = CCNL_MAX_PACKET_SIZE; name = ccnl_URItoPrefix(url, suite, &chunknum); switch (suite) { case CCNL_SUITE_CCNTLV: if (ccnl_ccntlv_prependContentWithHdr(name, chunk_buf, chunk_len, &lastchunknum, //is_last ? &chunknum : NULL, NULL, // int *contentpos &offs, out, &contentlen)) { goto Error; } break; case CCNL_SUITE_NDNTLV: data_opts.ndntlv.finalblockid = lastchunknum; if (ccnl_ndntlv_prependContent(name, chunk_buf, chunk_len, NULL, &(data_opts.ndntlv),// is_last ? &chunknum : NULL, &offs, out, &contentlen)) { goto Error; } break; default: DEBUGMSG(ERROR, "produce for suite %i is not implemented\n", suite); goto Error; break; } if (outdirname) { sprintf(outpathname, "%s/%s%d.%s", outdirname, outfname, chunknum, fileext); DEBUGMSG(INFO, "writing chunk %d to file %s\n", chunknum, outpathname); fout = creat(outpathname, 0666); write(fout, out + offs, contentlen); close(fout); } else { DEBUGMSG(INFO, "writing chunk %d\n", chunknum); fwrite(out + offs, sizeof(unsigned char),contentlen, stdout); } chunknum++; if (!is_last) { s_chunk_len = read(f, chunk_buf, chunk_size); if (s_chunk_len < 0) { DEBUGMSG(ERROR, "Error reading input file; error: %d\n", errno); exit(1); } chunk_len = (size_t) s_chunk_len; } } close(f); ccnl_free(chunk_buf); return 0; Error: close(f); ccnl_free(chunk_buf); return -1; }