static int process_int_attribute(struct ccn_charbuf *interest, enum ccn_dtag tag, PyObject *py_obj_Interest, const char *attr_name) { PyObject *py_attr; int val, r; r = is_attr_set(py_obj_Interest, attr_name, &py_attr); if (r <= 0) return r; val = _pyccn_Int_AsLong(py_attr); Py_DECREF(py_attr); if (PyErr_Occurred()) return -1; #if 0 r = ccn_charbuf_append_tt(interest, tag, CCN_DTAG); JUMP_IF_NEG_MEM(r, error); r = ccnb_append_number(interest, val); JUMP_IF_NEG_MEM(r, error); r = ccn_charbuf_append_closer(interest); /* </Tag> */ JUMP_IF_NEG_MEM(r, error); #endif r = ccnb_tagged_putf(interest, tag, "%d", val); JUMP_IF_NEG_MEM(r, error); return 1; error: return -1; }
int get_content_by_content_name(char *content_name, unsigned char **content_data, char *orig_router) { int ret=-1; struct ccn_charbuf *name = NULL; struct ccn_charbuf *templ = NULL; struct ccn_charbuf *resultbuf = NULL; struct ccn_parsed_ContentObject pcobuf = { 0 }; int res; int allow_stale = 1; int content_only = 1; int scope = -1; const unsigned char *ptr,*ptr_in; size_t length,length_in; int resolve_version = CCN_V_HIGHEST; int timeout_ms = 3000; const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12; unsigned lifetime_l12 = lifetime_default; int get_flags = 0; name = ccn_charbuf_create(); res = ccn_name_from_uri(name,content_name); if (res < 0) { fprintf(stderr, "Bad ccn URI: %s\n", content_name); ccn_charbuf_destroy(&name); return ret; } if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) { templ = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); /* </Name> */ if (allow_stale) { ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG); ccnb_append_number(templ, CCN_AOK_DEFAULT | CCN_AOK_STALE); ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */ } if (scope != -1) { ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope); } if (lifetime_l12 != lifetime_default) { /* * Choose the interest lifetime so there are at least 3 * expressions (in the unsatisfied case). */ unsigned char buf[3] = { 0 }; int i; for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8) buf[i] = lifetime_l12 & 0xff; ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf)); }
struct ccn_charbuf * make_template(struct mydata *md, struct ccn_upcall_info *info) { struct ccn_charbuf *templ = ccn_charbuf_create(); ccnb_element_begin(templ, CCN_DTAG_Interest); ccnb_element_begin(templ, CCN_DTAG_Name); ccnb_element_end(templ); /* </Name> */ // XXX - use pubid if possible ccnb_element_begin(templ, CCN_DTAG_MaxSuffixComponents); ccnb_append_number(templ, 1); ccnb_element_end(templ); /* </MaxSuffixComponents> */ if (md->allow_stale) { ccnb_element_begin(templ, CCN_DTAG_AnswerOriginKind); ccnb_append_number(templ, CCN_AOK_DEFAULT | CCN_AOK_STALE); ccnb_element_end(templ); /* </AnswerOriginKind> */ } ccnb_element_end(templ); /* </Interest> */ return(templ); }
struct ccn_charbuf * make_template(struct mydata *md) { struct ccn_charbuf *templ = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); /* </Name> */ // XXX - use pubid if possible ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); ccnb_append_number(templ, 1); ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ if (md->allow_stale) { ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG); ccnb_append_number(templ, CCN_AOK_DEFAULT | CCN_AOK_STALE); ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */ } ccn_charbuf_append_closer(templ); /* </Interest> */ return(templ); }
/** * Creates a template for ccn interest fetch * * @param allow_stale * @param scope */ struct ccn_charbuf* CCNGet::make_template(int allow_stale, int scope) { struct ccn_charbuf *templ = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); /* </Name> */ ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); ccnb_append_number(templ, 1); ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ if (allow_stale) { ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG); ccnb_append_number(templ, CCN_AOK_DEFAULT | CCN_AOK_STALE); ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */ } if (scope >= 0 && scope <= 2) { ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope); } ccn_charbuf_append_closer(templ); /* </Interest> */ return(templ); }
static struct ccn_charbuf * make_data_template(int maxSuffix) { // creates a template for interests that only have a name // and a segment number struct ccn_charbuf *cb = ccn_charbuf_create(); ccn_charbuf_append_tt(cb, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(cb, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(cb); /* </Name> */ ccn_charbuf_append_tt(cb, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); ccnb_append_number(cb, maxSuffix); ccn_charbuf_append_closer(cb); /* </MaxSuffixComponents> */ ccn_charbuf_append_closer(cb); /* </Interest> */ return(cb); }
struct ccn_charbuf * make_connect_template() { struct ccn_charbuf *templ = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); /* <Interest> */ ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); /* <Name> */ ccn_charbuf_append_closer(templ); /* </Name> */ ccn_charbuf_append_tt(templ, CCN_DTAG_ChildSelector, CCN_DTAG); ccnb_append_number(templ,1); ccn_charbuf_append_closer(templ); ccn_charbuf_append_closer(templ); /* </Interest> */ return templ; }
struct ccn_charbuf *make_template(int allow_caching) { if (!allow_caching) { struct ccn_charbuf *templ = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); /* </Name> */ ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG); ccnb_append_number(templ, CCN_AOK_NEW); ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */ ccn_charbuf_append_closer(templ); /* </Interest> */ return(templ); } return NULL; }
int main (int argc, char **argv) { int res; //check if user supplied uri to trace if(argv[1] == NULL) { printf("Usage: trace URI\n"); exit(1); } //get the length of user provided URI int argv_length = strlen(argv[1]); //check first six chars for ccnx:/, if present, skip them int skip = 0; res = strncmp("ccnx:/", argv[1], 6); if(res == 0) { skip = 5; } //if URI does not begins with /, exit if (argv[1][skip] != '/') { printf("URI must begin with /\n"); exit(1); } //check if uri ends with slash, append if missing char *slash = ""; if (argv[1][argv_length-1] != '/') { slash = "/"; } //allocate memory for trace URI = /trace/user_input/random_number char *URI = (char *) malloc(sizeof(char)* argv_length+1); //find size of rand if(URI == NULL) { fprintf(stderr, "Can not allocate memory for URI\n"); exit(1); } //put together the trace URI, add a random number to end of URI srand ((unsigned int)time (NULL)*getpid()); sprintf(URI, "%s%s", argv[1]+skip, slash); //allocate memory for interest struct ccn_charbuf *ccnb = ccn_charbuf_create(); if(ccnb == NULL) { fprintf(stderr, "Can not allocate memory for interest\n"); exit(1); } //adding name to interest res = ccn_name_from_uri(ccnb, URI); if(res == -1) { fprintf(stderr, "Failed to assign name to interest"); exit(1); } //create the ccn handle struct ccn *ccn = ccn_create(); if(ccn == NULL) { fprintf(stderr, "Can not create ccn handle\n"); exit(1); } //connect to ccnd res = ccn_connect(ccn, NULL); if (res == -1) { fprintf(stderr, "Could not connect to ccnd... exiting\n"); exit(1); } #ifdef DEBUG printf("Connected to CCND, return code: %d\n", res); #endif //allocate buffer for response struct ccn_charbuf *resultbuf = ccn_charbuf_create(); if(resultbuf == NULL) { fprintf(stderr, "Can not allocate memory for URI\n"); exit(1); } //setting the parameters for ccn_get struct ccn_parsed_ContentObject pcobuf = { 0 }; int timeout_ms = 6000; //express interest //res = ccn_get(ccn, ccnb, NULL, timeout_ms, resultbuf, &pcobuf, NULL, 0); //if cached answer is not allowed, set the template //make sure AnswerOriginKind is 0x0 if(!ALLOW_CACHE) { struct ccn_charbuf *templ = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); /* </Name> */ ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG); ccnb_append_number(templ,0x0); ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */ ccn_charbuf_append_closer(templ); //send out the interest res = ccn_get(ccn, ccnb, templ, timeout_ms, resultbuf, &pcobuf, NULL, 0); } //cached answer is allowed, don't set template else { res = ccn_get(ccn, ccnb, NULL, timeout_ms, resultbuf, &pcobuf, NULL, 0); } if (res == -1) { fprintf(stderr, "Did not receive answer for trace to %s\n", argv[1]); #ifdef DEBUG fprintf(stderr, "Did not receive answer for trace to URI: %s\n", URI); #endif exit(1); } //extract data from the response const unsigned char *ptr; size_t length; ptr = resultbuf->buf; length = resultbuf->length; ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length); //check if received some data if(length == 0) { fprintf(stderr, "Received empty answer for trace to %s\n", argv[1]); #ifdef DEBUG fprintf(stderr, "Received empty answer for trace to URI: %s\n", URI); #endif exit(1); } //print the data printf("Reply: %s\n", ptr); printf("Length of data: %Zu\n", length); exit(0); }
int main(int argc, char **argv) { struct ccn *h = NULL; struct ccn_charbuf *name = NULL; struct ccn_charbuf *templ = NULL; struct ccn_charbuf *resultbuf = NULL; const char *arg = NULL; struct ccn_parsed_ContentObject pcobuf = { 0 }; int res; char ch; int allow_stale = 0; int content_only = 0; const unsigned char *ptr; size_t length; int resolve_version = 0; const char *env_timeout = getenv("CCN_LINGER"); int timeout_ms = 3000; while ((ch = getopt(argc, argv, "hacv")) != -1) { switch (ch) { case 'a': allow_stale = 1; break; case 'c': content_only = 1; break; case 'v': if (resolve_version == 0) resolve_version = CCN_V_HIGHEST; else resolve_version = CCN_V_HIGH; break; case 'h': default: usage(argv[0]); } } arg = argv[optind]; if (arg == NULL) usage(argv[0]); name = ccn_charbuf_create(); res = ccn_name_from_uri(name, arg); if (res < 0) { fprintf(stderr, "%s: bad ccn URI: %s\n", argv[0], arg); exit(1); } if (argv[optind + 1] != NULL) fprintf(stderr, "%s warning: extra arguments ignored\n", argv[0]); h = ccn_create(); res = ccn_connect(h, NULL); if (res < 0) { ccn_perror(h, "ccn_connect"); exit(1); } if (res < 0) { fprintf(stderr, "%s: bad ccn URI: %s\n", argv[0], arg); exit(1); } if (env_timeout != NULL && (res = atoi(env_timeout)) > 0) { timeout_ms = res * 1000; } if (allow_stale || env_timeout != NULL) { templ = ccn_charbuf_create(); ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); /* </Name> */ if (allow_stale) { ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG); ccnb_append_number(templ, CCN_AOK_DEFAULT | CCN_AOK_STALE); ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */ } if (env_timeout != NULL) { /* * Choose the interest lifetime so there are at least 3 * expressions (in the unsatisfied case). */ unsigned char buf[3] = { 0 }; unsigned lifetime; int i; if (timeout_ms > 60000) lifetime = 30 << 12; else { lifetime = timeout_ms * 2 / 5 * 4096 / 1000; } for (i = sizeof(buf) - 1; i >= 0; i--, lifetime >>= 8) buf[i] = lifetime & 0xff; ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf)); }