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