blob: f0ee538cc63bb897c150a71822b0ec65ec45d422 [file] [log] [blame]
akmhoque59980a52012-08-09 12:36:09 -05001#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>
akmhoque7f337272012-08-14 15:16:30 -050019#include <ccn/bloom.h>
akmhoque59980a52012-08-09 12:36:09 -050020
21#include "nlsr.h"
22#include "nlsr_ndn.h"
23#include "utility.h"
24
25enum ccn_upcall_res
26incoming_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
akmhoque1c9b92f2012-08-13 10:57:50 -050043 process_incoming_interest(selfp, info);
akmhoque59980a52012-08-09 12:36:09 -050044
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
74enum 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);
akmhoquecb017752012-08-16 11:03:45 -050089
90 process_incoming_content(selfp, info);
91
akmhoque59980a52012-08-09 12:36:09 -050092 break;
93 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoquecb017752012-08-16 11:03:45 -050094 printf("Interest timed out \n");
95 struct ccn_charbuf*ic;
96 ic=ccn_charbuf_create();
97 ccn_uri_append(ic,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
98 printf("%s\n",ccn_charbuf_as_string(ic));
99 ccn_charbuf_destroy(&ic);
100 process_incoming_timed_out_interest(selfp,info);
akmhoque59980a52012-08-09 12:36:09 -0500101
akmhoque59980a52012-08-09 12:36:09 -0500102 break;
103 default:
104 fprintf(stderr, "Unexpected response of kind %d\n", kind);
105 return CCN_UPCALL_RESULT_ERR;
106 }
107
108 return CCN_UPCALL_RESULT_OK;
109}
akmhoque61fe4472012-08-10 10:13:34 -0500110
akmhoquecb017752012-08-16 11:03:45 -0500111
112void
113process_incoming_content(struct ccn_closure* selfp, struct ccn_upcall_info* info)
114{
115 printf("process_incoming_content called \n");
116
akmhoque638c20a2012-08-16 11:57:48 -0500117 struct ccn_charbuf*c;
118 c=ccn_charbuf_create();
akmhoquedde7e322012-08-16 13:57:06 -0500119 //ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
120 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
akmhoque638c20a2012-08-16 11:57:48 -0500121 printf("%s\n",ccn_charbuf_as_string(c));
122 ccn_charbuf_destroy(&c);
123
124 const unsigned char *comp_ptr1;
125 size_t comp_size;
126 int res,i;
127 int nlsr_position=0;
128 int name_comps=(int)info->interest_comps->n;
129
130 for(i=0;i<name_comps;i++)
131 {
132 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
133 if( res == 0)
134 {
135 nlsr_position=i;
136 break;
137 }
138 }
139
140 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
141
142
143 printf("Det= %s \n",comp_ptr1);
144
145 if(!strcmp((char *)comp_ptr1,"lsdb"))
146 {
147 process_incoming_content_lsdb(selfp,info);
148 }
149
150}
151
152
153void
154process_incoming_content_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
155{
156 printf("process_incoming_content_lsdb called \n");
157
158 const unsigned char *content_data;
159 size_t length;
160 ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E_Content]-info->pco->offset[CCN_PCO_B_Content], info->pco, &content_data, &length);
161
akmhoquedde7e322012-08-16 13:57:06 -0500162
163 if ( !strcmp((char *)content_data,"NACK"))
164 {
165 printf("NACK received for LSDB request. Do nothing \n");
166 }
167 else
168 {
169 // Do the LSDB processing here
170
171 }
akmhoque638c20a2012-08-16 11:57:48 -0500172
akmhoquee0789862012-08-16 14:23:32 -0500173 const unsigned char *comp_ptr1;
174 size_t comp_size;
175 int res;
176
177 res=ccn_name_comp_get(info->content_ccnb, info->content_comps,info->interest_comps->n-1,&comp_ptr1, &comp_size);
178
179 printf("Database Version: %s \n",(char *)comp_ptr1);
akmhoque638c20a2012-08-16 11:57:48 -0500180
akmhoquecb017752012-08-16 11:03:45 -0500181}
182
183
184void
185process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
186{
187 printf("process_incoming_timed_out_interest called \n");
188
akmhoquedde7e322012-08-16 13:57:06 -0500189 struct ccn_charbuf*c;
190 c=ccn_charbuf_create();
191 //ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
192 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
193 printf("%s\n",ccn_charbuf_as_string(c));
194 ccn_charbuf_destroy(&c);
195
196 const unsigned char *comp_ptr1;
197 size_t comp_size;
198 int res,i;
199 int nlsr_position=0;
200 int name_comps=(int)info->interest_comps->n;
201
202 for(i=0;i<name_comps;i++)
203 {
204 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
205 if( res == 0)
206 {
207 nlsr_position=i;
208 break;
209 }
210 }
211
212 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
213
214
215 printf("Det= %s \n",comp_ptr1);
216
217 if(!strcmp((char *)comp_ptr1,"lsdb"))
218 {
219 process_incoming_timed_out_interest_lsdb(selfp,info);
220 }
akmhoquecb017752012-08-16 11:03:45 -0500221}
222
223
akmhoque1c9b92f2012-08-13 10:57:50 -0500224void
akmhoquedde7e322012-08-16 13:57:06 -0500225process_incoming_timed_out_interest_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
226{
227 printf("process_incoming_timed_out_interest_lsdb called \n");
228
229
230 int res,i;
231 int nlsr_position=0;
232 int name_comps=(int)info->interest_comps->n;
233
234 //const unsigned char *comp_ptr1;
235 //size_t comp_size;
236
237 for(i=0;i<name_comps;i++)
238 {
239 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
240 if( res == 0)
241 {
242 nlsr_position=i;
243 break;
244 }
245 }
246
247
248 struct ccn_charbuf*c;
249 c=ccn_charbuf_create();
250 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
251 printf("%s\n",ccn_charbuf_as_string(c));
252 ccn_charbuf_destroy(&c);
253
254
255}
256
257void
akmhoque1c9b92f2012-08-13 10:57:50 -0500258process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
259{
260 printf("process_incoming_interest called \n");
261
262
263 struct ccn_charbuf*c;
264 c=ccn_charbuf_create();
265 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
266 printf("%s\n",ccn_charbuf_as_string(c));
267 ccn_charbuf_destroy(&c);
268
269 const unsigned char *comp_ptr1;
270 size_t comp_size;
271 int res,i;
272 int nlsr_position=0;
273 int name_comps=(int)info->interest_comps->n;
274
275 for(i=0;i<name_comps;i++)
276 {
277 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
278 if( res == 0)
279 {
akmhoqueea3603e2012-08-13 11:24:09 -0500280 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500281 break;
282 }
283 }
284
285 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
akmhoquee0789862012-08-16 14:23:32 -0500286
akmhoque1c9b92f2012-08-13 10:57:50 -0500287 printf("Det= %s \n",comp_ptr1);
288
289 if(!strcmp((char *)comp_ptr1,"lsdb"))
290 {
akmhoquebf1aa832012-08-13 13:26:59 -0500291 process_incoming_interest_lsdb(selfp,info);
akmhoque1c9b92f2012-08-13 10:57:50 -0500292 }
akmhoquebf1aa832012-08-13 13:26:59 -0500293
294
295}
296
297
298void
299process_incoming_interest_lsdb(struct ccn_closure *selfp, struct ccn_upcall_info *info)
300{
301 printf("process_incoming_interest_lsdb called \n");
302
akmhoquecb017752012-08-16 11:03:45 -0500303 int l, res;
akmhoquefce8cfc2012-08-14 14:00:33 -0500304 const unsigned char *exclbase;
akmhoque6d49e4d2012-08-14 13:49:30 -0500305 size_t size;
akmhoquefce8cfc2012-08-14 14:00:33 -0500306 struct ccn_buf_decoder decoder;
307 struct ccn_buf_decoder *d;
308 const unsigned char *comp;
309
akmhoque6d49e4d2012-08-14 13:49:30 -0500310
311 l = info->pi->offset[CCN_PI_E_Exclude] - info->pi->offset[CCN_PI_B_Exclude];
312 if (l > 0)
313 {
314 comp = NULL;
315 size = 0;
316 exclbase = info->interest_ccnb + info->pi->offset[CCN_PI_B_Exclude];
317 d = ccn_buf_decoder_start(&decoder, exclbase, l);
318 if (ccn_buf_match_dtag(d, CCN_DTAG_Exclude))
319 {
320 ccn_buf_advance(d);
321 if (ccn_buf_match_dtag(d, CCN_DTAG_Any))
322 ccn_buf_advance_past_element(d);
323 if (ccn_buf_match_dtag(d, CCN_DTAG_Component))
324 {
325 ccn_buf_advance(d);
326 ccn_buf_match_blob(d, &comp, &size);
akmhoquec9286692012-08-16 09:57:58 -0500327 ccn_buf_check_close(d);
328
329
akmhoque6d49e4d2012-08-14 13:49:30 -0500330 }
331 ccn_buf_check_close(d);
332 }
akmhoque07dd8cc2012-08-16 10:23:01 -0500333 //if (d->decoder.state < 0)
334 //printf("Parse Failed\n");
akmhoque6d49e4d2012-08-14 13:49:30 -0500335 if (comp != NULL)
akmhoque37e3adf2012-08-14 15:52:50 -0500336 printf("Number in Exclusion Filter is %s\n",comp);
akmhoque6d49e4d2012-08-14 13:49:30 -0500337
338 /* Now comp points to the start of your potential number, and size is its length */
339 }
340
akmhoque07dd8cc2012-08-16 10:23:01 -0500341 int dbcmp=strncmp(nlsr->lsdb->version,(char *)comp,16);
akmhoque898d4aa2012-08-16 10:26:15 -0500342
343 printf (" dbcmp = %d \n",dbcmp);
344
akmhoque07dd8cc2012-08-16 10:23:01 -0500345 if(dbcmp > 0)
akmhoquecb017752012-08-16 11:03:45 -0500346 {
akmhoque898d4aa2012-08-16 10:26:15 -0500347 printf("Has Updated database (Older: %s New: %s)\n",comp,nlsr->lsdb->version);
akmhoquecb017752012-08-16 11:03:45 -0500348 }
akmhoque07dd8cc2012-08-16 10:23:01 -0500349 else
akmhoquecb017752012-08-16 11:03:45 -0500350 {
akmhoque898d4aa2012-08-16 10:26:15 -0500351 printf("Data base is not updated than the older one (Older: %s New: %s)\n",comp,nlsr->lsdb->version);
akmhoquecb017752012-08-16 11:03:45 -0500352 printf("Sending NACK Content back.....\n");
akmhoque07dd8cc2012-08-16 10:23:01 -0500353
akmhoquecb017752012-08-16 11:03:45 -0500354 struct ccn_charbuf *data=ccn_charbuf_create();
355 struct ccn_charbuf *name=ccn_charbuf_create();
356 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
357
akmhoque638c20a2012-08-16 11:57:48 -0500358 ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
akmhoquef9fffe72012-08-16 14:01:28 -0500359 ccn_name_append_str(name,"0000000000000001");
akmhoquecb017752012-08-16 11:03:45 -0500360
361 sp.template_ccnb=ccn_charbuf_create();
362 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
363 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
364 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
365 ccn_charbuf_append_closer(sp.template_ccnb);
366
367 res= ccn_sign_content(nlsr->ccn, data, name, &sp, "NACK", strlen("NACK"));
akmhoquedde7e322012-08-16 13:57:06 -0500368 if(res >= 0)
369 printf("Signing Content is successful \n");
370
akmhoquecb017752012-08-16 11:03:45 -0500371 res=ccn_put(nlsr->ccn,data->buf,data->length);
akmhoquebf11b1d2012-08-16 11:11:17 -0500372
373 if(res >= 0)
374 printf("Sending NACK Content is successful \n");
375
akmhoquecb017752012-08-16 11:03:45 -0500376 ccn_charbuf_destroy(&data);
377 ccn_charbuf_destroy(&sp.template_ccnb);
378
379 }
380
akmhoque1c9b92f2012-08-13 10:57:50 -0500381}
382
383int
384send_lsdb_interest(struct ccn_schedule *sched, void *clienth,
385 struct ccn_scheduled_event *ev, int flags)
386{
387
388 struct ccn_charbuf *name;
389 long int rnum;
390 char rnumstr[20];
akmhoqueea3603e2012-08-13 11:24:09 -0500391 char lsdb_str[5];
392 char nlsr_str[5];
akmhoque1c9b92f2012-08-13 10:57:50 -0500393
394 int res,i;
395 int adl_element;
396
397 rnum=random();
398 memset(&rnumstr,0,20);
399 sprintf(rnumstr,"%ld",rnum);
akmhoqueea3603e2012-08-13 11:24:09 -0500400 memset(&nlsr_str,0,5);
401 sprintf(nlsr_str,"nlsr");
402 memset(&lsdb_str,0,5);
403 sprintf(lsdb_str,"lsdb");
404
akmhoque1c9b92f2012-08-13 10:57:50 -0500405
406 struct ndn_neighbor *nbr;
407
408 struct hashtb_enumerator ee;
409 struct hashtb_enumerator *e = &ee;
410
411 hashtb_start(nlsr->adl, e);
412 adl_element=hashtb_n(nlsr->adl);
akmhoque07dd8cc2012-08-16 10:23:01 -0500413 //int mynumber=15;
akmhoque1c9b92f2012-08-13 10:57:50 -0500414
415 for(i=0;i<adl_element;i++)
416 {
417 nbr=e->data;
akmhoqueea3603e2012-08-13 11:24:09 -0500418 printf("Sending interest for name prefix:%s/%s/%s/%s\n",nbr->neighbor->name,nlsr_str,lsdb_str,rnumstr);
akmhoque1c9b92f2012-08-13 10:57:50 -0500419 name=ccn_charbuf_create();
420 res=ccn_name_from_uri(name,nbr->neighbor->name);
akmhoqueea3603e2012-08-13 11:24:09 -0500421 ccn_name_append_str(name,nlsr_str);
422 ccn_name_append_str(name,lsdb_str);
akmhoque1c9b92f2012-08-13 10:57:50 -0500423 ccn_name_append_str(name,rnumstr);
424
akmhoque6d49e4d2012-08-14 13:49:30 -0500425 /* adding Exclusion filter */
akmhoquebf1aa832012-08-13 13:26:59 -0500426
akmhoque6d49e4d2012-08-14 13:49:30 -0500427 struct ccn_charbuf *templ;
428 templ = ccn_charbuf_create();
akmhoque7f337272012-08-14 15:16:30 -0500429
akmhoque6d49e4d2012-08-14 13:49:30 -0500430 struct ccn_charbuf *c;
431 c = ccn_charbuf_create();
akmhoque7f337272012-08-14 15:16:30 -0500432
433
434 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
435 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
436 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque6d49e4d2012-08-14 13:49:30 -0500437 ccn_charbuf_append_tt(templ, CCN_DTAG_Exclude, CCN_DTAG);
438 ccnb_tagged_putf(templ, CCN_DTAG_Any, "");
439 ccn_charbuf_reset(c);
akmhoque07dd8cc2012-08-16 10:23:01 -0500440 //ccn_charbuf_putf(c, "%u", (unsigned)mynumber);
441 ccn_charbuf_putf(c, "%s", nbr->last_lsdb_version);
akmhoque6d49e4d2012-08-14 13:49:30 -0500442 ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length);
443 ccn_charbuf_append_closer(templ); /* </Exclude> */
akmhoque7f337272012-08-14 15:16:30 -0500444 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoque37e3adf2012-08-14 15:52:50 -0500445
akmhoque6d49e4d2012-08-14 13:49:30 -0500446
447 /* Adding Exclusion filter done */
448
akmhoque7f337272012-08-14 15:16:30 -0500449 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
450
akmhoque1c9b92f2012-08-13 10:57:50 -0500451 if ( res >= 0 )
452 printf("Interest sending Successfull .... \n");
akmhoque37e3adf2012-08-14 15:52:50 -0500453 ccn_charbuf_destroy(&c);
akmhoque7f337272012-08-14 15:16:30 -0500454 ccn_charbuf_destroy(&templ);
akmhoque1c9b92f2012-08-13 10:57:50 -0500455 ccn_charbuf_destroy(&name);
456
457 hashtb_next(e);
458 }
459
460 hashtb_end(e);
461
akmhoquebf11b1d2012-08-16 11:11:17 -0500462 nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 60000000, &send_lsdb_interest, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500463
464 return 0;
465
466}
467