akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1 | #include<stdio.h> |
| 2 | #include<string.h> |
| 3 | #include<stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <getopt.h> |
| 6 | #include <sys/time.h> |
| 7 | #include <assert.h> |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include <config.h> |
| 10 | #endif |
| 11 | |
| 12 | |
| 13 | #include <ccn/ccn.h> |
| 14 | #include <ccn/uri.h> |
| 15 | #include <ccn/keystore.h> |
| 16 | #include <ccn/signing.h> |
| 17 | #include <ccn/schedule.h> |
| 18 | #include <ccn/hashtb.h> |
| 19 | |
| 20 | #include "nlsr.h" |
| 21 | #include "nlsr_ndn.h" |
| 22 | #include "utility.h" |
| 23 | |
| 24 | enum ccn_upcall_res |
| 25 | incoming_interest(struct ccn_closure *selfp, |
| 26 | enum ccn_upcall_kind kind, struct ccn_upcall_info *info) |
| 27 | { |
| 28 | |
| 29 | switch (kind) { |
| 30 | case CCN_UPCALL_FINAL: |
| 31 | break; |
| 32 | case CCN_UPCALL_INTEREST: |
| 33 | // printing the name prefix for which it received interest |
| 34 | printf("Interest Received for name: "); |
| 35 | struct ccn_charbuf*c; |
| 36 | c=ccn_charbuf_create(); |
| 37 | ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0); |
| 38 | //ccn_name_chop(c,NULL,-1); |
| 39 | printf("%s\n",ccn_charbuf_as_string(c)); |
| 40 | ccn_charbuf_destroy(&c); |
| 41 | |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 42 | process_incoming_interest(selfp, info); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | struct ccn_charbuf *data=ccn_charbuf_create(); |
| 46 | struct ccn_charbuf *name=ccn_charbuf_create(); |
| 47 | struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT; |
| 48 | |
| 49 | ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name], |
| 50 | info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]); |
| 51 | |
| 52 | sp.template_ccnb=ccn_charbuf_create(); |
| 53 | ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG); |
| 54 | ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 1010); |
| 55 | sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS; |
| 56 | ccn_charbuf_append_closer(sp.template_ccnb); |
| 57 | |
| 58 | res= ccn_sign_content(ospfndn->ccn, data, name, &sp, "hello", strlen("hello")); |
| 59 | res=ccn_put(ospfndn->ccn,data->buf,data->length); |
| 60 | ccn_charbuf_destroy(&data); |
| 61 | |
| 62 | */ |
| 63 | break; |
| 64 | |
| 65 | default: |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | return CCN_UPCALL_RESULT_OK; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | enum ccn_upcall_res incoming_content(struct ccn_closure* selfp, |
| 74 | enum ccn_upcall_kind kind, struct ccn_upcall_info* info) |
| 75 | { |
| 76 | |
| 77 | |
| 78 | switch(kind) { |
| 79 | case CCN_UPCALL_FINAL: |
| 80 | break; |
| 81 | case CCN_UPCALL_CONTENT: |
| 82 | printf("Content Received for name: "); |
| 83 | struct ccn_charbuf*c; |
| 84 | c=ccn_charbuf_create(); |
| 85 | ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0); |
| 86 | printf("%s\n",ccn_charbuf_as_string(c)); |
| 87 | ccn_charbuf_destroy(&c); |
| 88 | |
| 89 | //process_incoming_content(selfp, info); |
| 90 | break; |
| 91 | case CCN_UPCALL_INTEREST_TIMED_OUT: |
| 92 | /* printf("Interest timed out \n"); |
| 93 | |
| 94 | const unsigned char *comp_ptr; |
| 95 | size_t comp_size; |
| 96 | int res; |
| 97 | |
| 98 | res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,2,&comp_ptr, &comp_size); |
| 99 | |
| 100 | printf("Parsed Interest: %s size: %d Size of name prefix: %d\n",comp_ptr,(int)comp_size,(int)info->interest_comps->n); |
| 101 | */ |
| 102 | |
| 103 | //process_timed_out_interest(selfp,info); |
| 104 | break; |
| 105 | default: |
| 106 | fprintf(stderr, "Unexpected response of kind %d\n", kind); |
| 107 | return CCN_UPCALL_RESULT_ERR; |
| 108 | } |
| 109 | |
| 110 | return CCN_UPCALL_RESULT_OK; |
| 111 | } |
akmhoque | 61fe447 | 2012-08-10 10:13:34 -0500 | [diff] [blame] | 112 | |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 113 | void |
| 114 | process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info) |
| 115 | { |
| 116 | printf("process_incoming_interest called \n"); |
| 117 | |
| 118 | |
| 119 | struct ccn_charbuf*c; |
| 120 | c=ccn_charbuf_create(); |
| 121 | ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0); |
| 122 | printf("%s\n",ccn_charbuf_as_string(c)); |
| 123 | ccn_charbuf_destroy(&c); |
| 124 | |
| 125 | const unsigned char *comp_ptr1; |
| 126 | size_t comp_size; |
| 127 | int res,i; |
| 128 | int nlsr_position=0; |
| 129 | int name_comps=(int)info->interest_comps->n; |
| 130 | |
| 131 | for(i=0;i<name_comps;i++) |
| 132 | { |
| 133 | res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr"); |
| 134 | if( res == 0) |
| 135 | { |
akmhoque | ea3603e | 2012-08-13 11:24:09 -0500 | [diff] [blame] | 136 | nlsr_position=i; |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size); |
| 142 | |
| 143 | |
| 144 | printf("Det= %s \n",comp_ptr1); |
| 145 | |
| 146 | if(!strcmp((char *)comp_ptr1,"lsdb")) |
| 147 | { |
akmhoque | bf1aa83 | 2012-08-13 13:26:59 -0500 | [diff] [blame] | 148 | process_incoming_interest_lsdb(selfp,info); |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 149 | } |
akmhoque | bf1aa83 | 2012-08-13 13:26:59 -0500 | [diff] [blame] | 150 | |
| 151 | |
| 152 | } |
| 153 | |
| 154 | |
| 155 | void |
| 156 | process_incoming_interest_lsdb(struct ccn_closure *selfp, struct ccn_upcall_info *info) |
| 157 | { |
| 158 | printf("process_incoming_interest_lsdb called \n"); |
| 159 | |
akmhoque | 6d49e4d | 2012-08-14 13:49:30 -0500 | [diff] [blame] | 160 | int l; |
akmhoque | fce8cfc | 2012-08-14 14:00:33 -0500 | [diff] [blame^] | 161 | const unsigned char *exclbase; |
akmhoque | 6d49e4d | 2012-08-14 13:49:30 -0500 | [diff] [blame] | 162 | size_t size; |
akmhoque | fce8cfc | 2012-08-14 14:00:33 -0500 | [diff] [blame^] | 163 | struct ccn_buf_decoder decoder; |
| 164 | struct ccn_buf_decoder *d; |
| 165 | const unsigned char *comp; |
| 166 | |
akmhoque | 6d49e4d | 2012-08-14 13:49:30 -0500 | [diff] [blame] | 167 | |
| 168 | l = info->pi->offset[CCN_PI_E_Exclude] - info->pi->offset[CCN_PI_B_Exclude]; |
| 169 | if (l > 0) |
| 170 | { |
| 171 | comp = NULL; |
| 172 | size = 0; |
| 173 | exclbase = info->interest_ccnb + info->pi->offset[CCN_PI_B_Exclude]; |
| 174 | d = ccn_buf_decoder_start(&decoder, exclbase, l); |
| 175 | if (ccn_buf_match_dtag(d, CCN_DTAG_Exclude)) |
| 176 | { |
| 177 | ccn_buf_advance(d); |
| 178 | if (ccn_buf_match_dtag(d, CCN_DTAG_Any)) |
| 179 | ccn_buf_advance_past_element(d); |
| 180 | if (ccn_buf_match_dtag(d, CCN_DTAG_Component)) |
| 181 | { |
| 182 | ccn_buf_advance(d); |
| 183 | ccn_buf_match_blob(d, &comp, &size); |
| 184 | ccn_buf_check_close(d); |
| 185 | } |
| 186 | ccn_buf_check_close(d); |
| 187 | } |
| 188 | if (d->decoder.state < 0) |
| 189 | printf("Parse Failed\n"); |
| 190 | if (comp != NULL) |
| 191 | printf("No Number in Exclusion Filter\n"); |
| 192 | |
| 193 | /* Now comp points to the start of your potential number, and size is its length */ |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | int |
| 201 | send_lsdb_interest(struct ccn_schedule *sched, void *clienth, |
| 202 | struct ccn_scheduled_event *ev, int flags) |
| 203 | { |
| 204 | |
| 205 | struct ccn_charbuf *name; |
| 206 | long int rnum; |
| 207 | char rnumstr[20]; |
akmhoque | ea3603e | 2012-08-13 11:24:09 -0500 | [diff] [blame] | 208 | char lsdb_str[5]; |
| 209 | char nlsr_str[5]; |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 210 | |
| 211 | int res,i; |
| 212 | int adl_element; |
| 213 | |
| 214 | rnum=random(); |
| 215 | memset(&rnumstr,0,20); |
| 216 | sprintf(rnumstr,"%ld",rnum); |
akmhoque | ea3603e | 2012-08-13 11:24:09 -0500 | [diff] [blame] | 217 | memset(&nlsr_str,0,5); |
| 218 | sprintf(nlsr_str,"nlsr"); |
| 219 | memset(&lsdb_str,0,5); |
| 220 | sprintf(lsdb_str,"lsdb"); |
| 221 | |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 222 | |
| 223 | struct ndn_neighbor *nbr; |
| 224 | |
| 225 | struct hashtb_enumerator ee; |
| 226 | struct hashtb_enumerator *e = ⅇ |
| 227 | |
| 228 | hashtb_start(nlsr->adl, e); |
| 229 | adl_element=hashtb_n(nlsr->adl); |
akmhoque | 6d49e4d | 2012-08-14 13:49:30 -0500 | [diff] [blame] | 230 | int mynumber=15; |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 231 | |
| 232 | for(i=0;i<adl_element;i++) |
| 233 | { |
| 234 | nbr=e->data; |
akmhoque | ea3603e | 2012-08-13 11:24:09 -0500 | [diff] [blame] | 235 | printf("Sending interest for name prefix:%s/%s/%s/%s\n",nbr->neighbor->name,nlsr_str,lsdb_str,rnumstr); |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 236 | name=ccn_charbuf_create(); |
| 237 | res=ccn_name_from_uri(name,nbr->neighbor->name); |
akmhoque | ea3603e | 2012-08-13 11:24:09 -0500 | [diff] [blame] | 238 | ccn_name_append_str(name,nlsr_str); |
| 239 | ccn_name_append_str(name,lsdb_str); |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 240 | ccn_name_append_str(name,rnumstr); |
| 241 | |
akmhoque | 6d49e4d | 2012-08-14 13:49:30 -0500 | [diff] [blame] | 242 | /* adding Exclusion filter */ |
akmhoque | bf1aa83 | 2012-08-13 13:26:59 -0500 | [diff] [blame] | 243 | |
akmhoque | 6d49e4d | 2012-08-14 13:49:30 -0500 | [diff] [blame] | 244 | struct ccn_charbuf *templ; |
| 245 | templ = ccn_charbuf_create(); |
| 246 | |
| 247 | struct ccn_charbuf *c; |
| 248 | c = ccn_charbuf_create(); |
| 249 | |
| 250 | ccn_charbuf_append_tt(templ, CCN_DTAG_Exclude, CCN_DTAG); |
| 251 | ccnb_tagged_putf(templ, CCN_DTAG_Any, ""); |
| 252 | ccn_charbuf_reset(c); |
| 253 | ccn_charbuf_putf(c, "%u", (unsigned)mynumber); |
| 254 | ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length); |
| 255 | ccn_charbuf_append_closer(templ); /* </Exclude> */ |
| 256 | |
| 257 | /* Adding Exclusion filter done */ |
| 258 | |
akmhoque | bf1aa83 | 2012-08-13 13:26:59 -0500 | [diff] [blame] | 259 | res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ); |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 260 | if ( res >= 0 ) |
| 261 | printf("Interest sending Successfull .... \n"); |
| 262 | ccn_charbuf_destroy(&name); |
| 263 | |
| 264 | hashtb_next(e); |
| 265 | } |
| 266 | |
| 267 | hashtb_end(e); |
| 268 | |
| 269 | nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 60000000, &send_lsdb_interest, NULL, 0); |
| 270 | |
| 271 | return 0; |
| 272 | |
| 273 | } |
| 274 | |