blob: 55da1732017aee61df6e7c7a50aa8273d27fac1e [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"
akmhoque8a5babe2012-08-16 17:39:33 -050024#include "nlsr_adl.h"
akmhoque59980a52012-08-09 12:36:09 -050025
26enum ccn_upcall_res
27incoming_interest(struct ccn_closure *selfp,
28 enum ccn_upcall_kind kind, struct ccn_upcall_info *info)
29{
30
31 switch (kind) {
32 case CCN_UPCALL_FINAL:
33 break;
34 case CCN_UPCALL_INTEREST:
35 // printing the name prefix for which it received interest
36 printf("Interest Received for name: ");
37 struct ccn_charbuf*c;
38 c=ccn_charbuf_create();
39 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
40 //ccn_name_chop(c,NULL,-1);
41 printf("%s\n",ccn_charbuf_as_string(c));
42 ccn_charbuf_destroy(&c);
43
akmhoque1c9b92f2012-08-13 10:57:50 -050044 process_incoming_interest(selfp, info);
akmhoque59980a52012-08-09 12:36:09 -050045
46 /*
47 struct ccn_charbuf *data=ccn_charbuf_create();
48 struct ccn_charbuf *name=ccn_charbuf_create();
49 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
50
51 ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],
52 info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
53
54 sp.template_ccnb=ccn_charbuf_create();
55 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
56 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 1010);
57 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
58 ccn_charbuf_append_closer(sp.template_ccnb);
59
60 res= ccn_sign_content(ospfndn->ccn, data, name, &sp, "hello", strlen("hello"));
61 res=ccn_put(ospfndn->ccn,data->buf,data->length);
62 ccn_charbuf_destroy(&data);
63
64 */
65 break;
66
67 default:
68 break;
69 }
70
71 return CCN_UPCALL_RESULT_OK;
72}
73
74
75enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
76 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
77{
78
79
80 switch(kind) {
81 case CCN_UPCALL_FINAL:
82 break;
83 case CCN_UPCALL_CONTENT:
84 printf("Content Received for name: ");
85 struct ccn_charbuf*c;
86 c=ccn_charbuf_create();
87 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
88 printf("%s\n",ccn_charbuf_as_string(c));
89 ccn_charbuf_destroy(&c);
akmhoquecb017752012-08-16 11:03:45 -050090
91 process_incoming_content(selfp, info);
92
akmhoque59980a52012-08-09 12:36:09 -050093 break;
94 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoquecb017752012-08-16 11:03:45 -050095 printf("Interest timed out \n");
96 struct ccn_charbuf*ic;
97 ic=ccn_charbuf_create();
98 ccn_uri_append(ic,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
99 printf("%s\n",ccn_charbuf_as_string(ic));
100 ccn_charbuf_destroy(&ic);
101 process_incoming_timed_out_interest(selfp,info);
akmhoque59980a52012-08-09 12:36:09 -0500102
akmhoque59980a52012-08-09 12:36:09 -0500103 break;
104 default:
105 fprintf(stderr, "Unexpected response of kind %d\n", kind);
106 return CCN_UPCALL_RESULT_ERR;
107 }
108
109 return CCN_UPCALL_RESULT_OK;
110}
akmhoque61fe4472012-08-10 10:13:34 -0500111
akmhoquecb017752012-08-16 11:03:45 -0500112
113void
114process_incoming_content(struct ccn_closure* selfp, struct ccn_upcall_info* info)
115{
116 printf("process_incoming_content called \n");
117
akmhoque638c20a2012-08-16 11:57:48 -0500118 struct ccn_charbuf*c;
119 c=ccn_charbuf_create();
akmhoquedde7e322012-08-16 13:57:06 -0500120 //ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
121 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
akmhoque638c20a2012-08-16 11:57:48 -0500122 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 {
136 nlsr_position=i;
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 {
148 process_incoming_content_lsdb(selfp,info);
149 }
150
151}
152
153
154void
155process_incoming_content_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
156{
157 printf("process_incoming_content_lsdb called \n");
158
159 const unsigned char *content_data;
160 size_t length;
161 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);
162
akmhoquedde7e322012-08-16 13:57:06 -0500163
164 if ( !strcmp((char *)content_data,"NACK"))
165 {
166 printf("NACK received for LSDB request. Do nothing \n");
167 }
168 else
169 {
170 // Do the LSDB processing here
171
akmhoque8a5babe2012-08-16 17:39:33 -0500172 const unsigned char *comp_ptr1;
173 size_t comp_size;
174 int res;
175
176 res=ccn_name_comp_get(info->content_ccnb, info->content_comps,info->interest_comps->n-1,&comp_ptr1, &comp_size);
akmhoque2852a222012-08-21 12:09:00 -0400177 if ( res >=0)
akmhoque8a5babe2012-08-16 17:39:33 -0500178 printf("Received Database Version: %s \n",(char *)comp_ptr1);
179
akmhoquedde7e322012-08-16 13:57:06 -0500180 }
akmhoque638c20a2012-08-16 11:57:48 -0500181
akmhoque8a5babe2012-08-16 17:39:33 -0500182
akmhoque638c20a2012-08-16 11:57:48 -0500183
akmhoquecb017752012-08-16 11:03:45 -0500184}
185
186
187void
188process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
189{
190 printf("process_incoming_timed_out_interest called \n");
191
akmhoquedde7e322012-08-16 13:57:06 -0500192 struct ccn_charbuf*c;
193 c=ccn_charbuf_create();
194 //ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
195 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
196 printf("%s\n",ccn_charbuf_as_string(c));
197 ccn_charbuf_destroy(&c);
198
199 const unsigned char *comp_ptr1;
200 size_t comp_size;
201 int res,i;
202 int nlsr_position=0;
203 int name_comps=(int)info->interest_comps->n;
204
205 for(i=0;i<name_comps;i++)
206 {
207 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
208 if( res == 0)
209 {
210 nlsr_position=i;
211 break;
212 }
213 }
214
215 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
216
217
218 printf("Det= %s \n",comp_ptr1);
219
220 if(!strcmp((char *)comp_ptr1,"lsdb"))
221 {
222 process_incoming_timed_out_interest_lsdb(selfp,info);
223 }
akmhoquecb017752012-08-16 11:03:45 -0500224}
225
226
akmhoque1c9b92f2012-08-13 10:57:50 -0500227void
akmhoquedde7e322012-08-16 13:57:06 -0500228process_incoming_timed_out_interest_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
229{
230 printf("process_incoming_timed_out_interest_lsdb called \n");
231
232
233 int res,i;
234 int nlsr_position=0;
235 int name_comps=(int)info->interest_comps->n;
236
237 //const unsigned char *comp_ptr1;
238 //size_t comp_size;
239
240 for(i=0;i<name_comps;i++)
241 {
242 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
243 if( res == 0)
244 {
245 nlsr_position=i;
246 break;
247 }
248 }
249
250
akmhoque8a5babe2012-08-16 17:39:33 -0500251 struct ccn_charbuf *nbr;
252 nbr=ccn_charbuf_create();
253
akmhoquedde7e322012-08-16 13:57:06 -0500254
akmhoque8a5babe2012-08-16 17:39:33 -0500255 const unsigned char *comp_ptr1;
256 size_t comp_size;
257 for(i=0;i<nlsr_position;i++)
258 {
259 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&comp_ptr1, &comp_size);
akmhoque6dc9a242012-08-21 11:23:57 -0400260 //printf("%s \n",comp_ptr1);
akmhoque8a5babe2012-08-16 17:39:33 -0500261 ccn_charbuf_append_string(nbr,"/");
262 ccn_charbuf_append_string(nbr,(const char *)comp_ptr1);
263 }
264
akmhoque71b552d2012-08-21 11:44:34 -0400265 ccn_charbuf_append_string(nbr,"\0");
akmhoque8a5babe2012-08-16 17:39:33 -0500266 printf("Interest Timed out for Neighbor: %s\n",ccn_charbuf_as_string(nbr));
267
268 update_adjacent_status_to_adl(nbr,1);
269
270 ccn_charbuf_destroy(&nbr);
akmhoquedde7e322012-08-16 13:57:06 -0500271}
272
273void
akmhoque1c9b92f2012-08-13 10:57:50 -0500274process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
275{
276 printf("process_incoming_interest called \n");
277
278
279 struct ccn_charbuf*c;
280 c=ccn_charbuf_create();
281 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
282 printf("%s\n",ccn_charbuf_as_string(c));
283 ccn_charbuf_destroy(&c);
284
285 const unsigned char *comp_ptr1;
286 size_t comp_size;
287 int res,i;
288 int nlsr_position=0;
289 int name_comps=(int)info->interest_comps->n;
290
291 for(i=0;i<name_comps;i++)
292 {
293 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
294 if( res == 0)
295 {
akmhoqueea3603e2012-08-13 11:24:09 -0500296 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500297 break;
298 }
299 }
300
301 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
akmhoquee0789862012-08-16 14:23:32 -0500302
akmhoque1c9b92f2012-08-13 10:57:50 -0500303 printf("Det= %s \n",comp_ptr1);
304
305 if(!strcmp((char *)comp_ptr1,"lsdb"))
306 {
akmhoquebf1aa832012-08-13 13:26:59 -0500307 process_incoming_interest_lsdb(selfp,info);
akmhoque1c9b92f2012-08-13 10:57:50 -0500308 }
akmhoquebf1aa832012-08-13 13:26:59 -0500309
310
311}
312
313
314void
315process_incoming_interest_lsdb(struct ccn_closure *selfp, struct ccn_upcall_info *info)
316{
317 printf("process_incoming_interest_lsdb called \n");
318
akmhoquecb017752012-08-16 11:03:45 -0500319 int l, res;
akmhoquefce8cfc2012-08-14 14:00:33 -0500320 const unsigned char *exclbase;
akmhoque6d49e4d2012-08-14 13:49:30 -0500321 size_t size;
akmhoquefce8cfc2012-08-14 14:00:33 -0500322 struct ccn_buf_decoder decoder;
323 struct ccn_buf_decoder *d;
324 const unsigned char *comp;
325
akmhoque6d49e4d2012-08-14 13:49:30 -0500326
327 l = info->pi->offset[CCN_PI_E_Exclude] - info->pi->offset[CCN_PI_B_Exclude];
328 if (l > 0)
329 {
330 comp = NULL;
331 size = 0;
332 exclbase = info->interest_ccnb + info->pi->offset[CCN_PI_B_Exclude];
333 d = ccn_buf_decoder_start(&decoder, exclbase, l);
334 if (ccn_buf_match_dtag(d, CCN_DTAG_Exclude))
335 {
336 ccn_buf_advance(d);
337 if (ccn_buf_match_dtag(d, CCN_DTAG_Any))
338 ccn_buf_advance_past_element(d);
339 if (ccn_buf_match_dtag(d, CCN_DTAG_Component))
340 {
341 ccn_buf_advance(d);
342 ccn_buf_match_blob(d, &comp, &size);
akmhoquec9286692012-08-16 09:57:58 -0500343 ccn_buf_check_close(d);
344
345
akmhoque6d49e4d2012-08-14 13:49:30 -0500346 }
347 ccn_buf_check_close(d);
348 }
akmhoque07dd8cc2012-08-16 10:23:01 -0500349 //if (d->decoder.state < 0)
350 //printf("Parse Failed\n");
akmhoque6d49e4d2012-08-14 13:49:30 -0500351 if (comp != NULL)
akmhoque37e3adf2012-08-14 15:52:50 -0500352 printf("Number in Exclusion Filter is %s\n",comp);
akmhoque6d49e4d2012-08-14 13:49:30 -0500353
354 /* Now comp points to the start of your potential number, and size is its length */
355 }
356
akmhoque6dc9a242012-08-21 11:23:57 -0400357 int dbcmp=strncmp(nlsr->lsdb->version,(char *)comp,16);
akmhoque898d4aa2012-08-16 10:26:15 -0500358
akmhoque6dc9a242012-08-21 11:23:57 -0400359 printf (" dbcmp = %d \n",dbcmp);
akmhoque898d4aa2012-08-16 10:26:15 -0500360
akmhoque07dd8cc2012-08-16 10:23:01 -0500361 if(dbcmp > 0)
akmhoquecb017752012-08-16 11:03:45 -0500362 {
akmhoque6dc9a242012-08-21 11:23:57 -0400363 printf("Has Updated database (Older: %s New: %s)\n",comp,nlsr->lsdb->version);
akmhoquecb017752012-08-16 11:03:45 -0500364 }
akmhoque07dd8cc2012-08-16 10:23:01 -0500365 else
akmhoquecb017752012-08-16 11:03:45 -0500366 {
akmhoque6dc9a242012-08-21 11:23:57 -0400367 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 -0500368 printf("Sending NACK Content back.....\n");
akmhoque07dd8cc2012-08-16 10:23:01 -0500369
akmhoquecb017752012-08-16 11:03:45 -0500370 struct ccn_charbuf *data=ccn_charbuf_create();
371 struct ccn_charbuf *name=ccn_charbuf_create();
372 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
373
akmhoque638c20a2012-08-16 11:57:48 -0500374 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]);
akmhoque8a5babe2012-08-16 17:39:33 -0500375 //ccn_name_append_str(name,"0000000000000001");
akmhoquecb017752012-08-16 11:03:45 -0500376
377 sp.template_ccnb=ccn_charbuf_create();
378 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
379 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
380 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
381 ccn_charbuf_append_closer(sp.template_ccnb);
382
383 res= ccn_sign_content(nlsr->ccn, data, name, &sp, "NACK", strlen("NACK"));
akmhoquedde7e322012-08-16 13:57:06 -0500384 if(res >= 0)
385 printf("Signing Content is successful \n");
386
akmhoquecb017752012-08-16 11:03:45 -0500387 res=ccn_put(nlsr->ccn,data->buf,data->length);
akmhoquebf11b1d2012-08-16 11:11:17 -0500388
389 if(res >= 0)
390 printf("Sending NACK Content is successful \n");
391
akmhoquecb017752012-08-16 11:03:45 -0500392 ccn_charbuf_destroy(&data);
393 ccn_charbuf_destroy(&sp.template_ccnb);
394
395 }
396
akmhoque1c9b92f2012-08-13 10:57:50 -0500397}
398
399int
400send_lsdb_interest(struct ccn_schedule *sched, void *clienth,
401 struct ccn_scheduled_event *ev, int flags)
402{
403
404 struct ccn_charbuf *name;
405 long int rnum;
406 char rnumstr[20];
akmhoqueea3603e2012-08-13 11:24:09 -0500407 char lsdb_str[5];
408 char nlsr_str[5];
akmhoque1c9b92f2012-08-13 10:57:50 -0500409
410 int res,i;
411 int adl_element;
412
413 rnum=random();
414 memset(&rnumstr,0,20);
415 sprintf(rnumstr,"%ld",rnum);
akmhoqueea3603e2012-08-13 11:24:09 -0500416 memset(&nlsr_str,0,5);
417 sprintf(nlsr_str,"nlsr");
418 memset(&lsdb_str,0,5);
419 sprintf(lsdb_str,"lsdb");
420
akmhoque1c9b92f2012-08-13 10:57:50 -0500421
422 struct ndn_neighbor *nbr;
423
424 struct hashtb_enumerator ee;
425 struct hashtb_enumerator *e = &ee;
426
427 hashtb_start(nlsr->adl, e);
428 adl_element=hashtb_n(nlsr->adl);
akmhoque07dd8cc2012-08-16 10:23:01 -0500429 //int mynumber=15;
akmhoque1c9b92f2012-08-13 10:57:50 -0500430
431 for(i=0;i<adl_element;i++)
432 {
433 nbr=e->data;
akmhoque6dc9a242012-08-21 11:23:57 -0400434 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 -0500435 name=ccn_charbuf_create();
akmhoque1d7343f2012-08-21 10:46:35 -0400436 res=ccn_name_from_uri(name,nbr->neighbor->name);
akmhoqueea3603e2012-08-13 11:24:09 -0500437 ccn_name_append_str(name,nlsr_str);
438 ccn_name_append_str(name,lsdb_str);
akmhoque6dc9a242012-08-21 11:23:57 -0400439 ccn_name_append_str(name,rnumstr);
akmhoque1c9b92f2012-08-13 10:57:50 -0500440
akmhoque6d49e4d2012-08-14 13:49:30 -0500441 /* adding Exclusion filter */
akmhoquebf1aa832012-08-13 13:26:59 -0500442
akmhoque6d49e4d2012-08-14 13:49:30 -0500443 struct ccn_charbuf *templ;
444 templ = ccn_charbuf_create();
akmhoque7f337272012-08-14 15:16:30 -0500445
akmhoque6d49e4d2012-08-14 13:49:30 -0500446 struct ccn_charbuf *c;
447 c = ccn_charbuf_create();
akmhoque7f337272012-08-14 15:16:30 -0500448
449
450 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
451 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
452 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque6d49e4d2012-08-14 13:49:30 -0500453 ccn_charbuf_append_tt(templ, CCN_DTAG_Exclude, CCN_DTAG);
454 ccnb_tagged_putf(templ, CCN_DTAG_Any, "");
455 ccn_charbuf_reset(c);
akmhoque07dd8cc2012-08-16 10:23:01 -0500456 //ccn_charbuf_putf(c, "%u", (unsigned)mynumber);
akmhoque6dc9a242012-08-21 11:23:57 -0400457 ccn_charbuf_putf(c, "%s", nbr->last_lsdb_version);
akmhoque6d49e4d2012-08-14 13:49:30 -0500458 ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length);
459 ccn_charbuf_append_closer(templ); /* </Exclude> */
akmhoque7f337272012-08-14 15:16:30 -0500460 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoque37e3adf2012-08-14 15:52:50 -0500461
akmhoque6d49e4d2012-08-14 13:49:30 -0500462
463 /* Adding Exclusion filter done */
464
akmhoque7f337272012-08-14 15:16:30 -0500465 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
466
akmhoque1c9b92f2012-08-13 10:57:50 -0500467 if ( res >= 0 )
468 printf("Interest sending Successfull .... \n");
akmhoque37e3adf2012-08-14 15:52:50 -0500469 ccn_charbuf_destroy(&c);
akmhoque7f337272012-08-14 15:16:30 -0500470 ccn_charbuf_destroy(&templ);
akmhoque1c9b92f2012-08-13 10:57:50 -0500471 ccn_charbuf_destroy(&name);
472
akmhoque1d7343f2012-08-21 10:46:35 -0400473 hashtb_next(e);
akmhoque1c9b92f2012-08-13 10:57:50 -0500474 }
475
476 hashtb_end(e);
477
akmhoquebf11b1d2012-08-16 11:11:17 -0500478 nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 60000000, &send_lsdb_interest, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500479
480 return 0;
481
482}
483