blob: f05a90d8e4cc36ed30604c31c0b8325d5b9a94da [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>
19
20#include "nlsr.h"
21#include "nlsr_ndn.h"
akmhoque03004e62012-09-06 01:12:28 -050022#include "nlsr_npl.h"
akmhoque8a5babe2012-08-16 17:39:33 -050023#include "nlsr_adl.h"
akmhoqued79438d2012-08-27 13:31:42 -050024#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050025#include "utility.h"
26
akmhoqued79438d2012-08-27 13:31:42 -050027int
28appendLifetime(struct ccn_charbuf *cb, int lifetime)
29{
30 unsigned char buf[sizeof(int32_t)];
31 int32_t dreck = lifetime << 12;
32 int pos = sizeof(int32_t);
33 int res = 0;
34 while (dreck > 0 && pos > 0)
35 {
36 pos--;
37 buf[pos] = dreck & 255;
38 dreck = dreck >> 8;
39 }
40 res |= ccnb_append_tagged_blob(cb, CCN_DTAG_InterestLifetime, buf+pos, sizeof(buf)-pos);
41 return res;
42}
akmhoque59980a52012-08-09 12:36:09 -050043
akmhoque53f64222012-09-05 13:57:51 -050044
45void
46get_nbr(struct name_prefix *nbr,struct ccn_closure *selfp, struct ccn_upcall_info *info)
47{
48
49 printf("get_nbr called\n");
50 int res,i;
51 int nlsr_position=0;
52 int name_comps=(int)info->interest_comps->n;
53 int len=0;
54
55 for(i=0;i<name_comps;i++)
56 {
57 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
58 if( res == 0)
59 {
60 nlsr_position=i;
61 break;
62 }
63 }
64
65
66 const unsigned char *comp_ptr1;
67 size_t comp_size;
68 for(i=0;i<nlsr_position;i++)
69 {
70 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&comp_ptr1, &comp_size);
71 len+=1;
72 len+=(int)comp_size;
73 }
74 len++;
75
76 char *neighbor=(char *)malloc(len);
77 memset(neighbor,0,len);
78
79 for(i=0; i<nlsr_position;i++)
80 {
81 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&comp_ptr1, &comp_size);
82 memcpy(neighbor+strlen(neighbor),"/",1);
83 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
84
85 }
akmhoque53f64222012-09-05 13:57:51 -050086
87 nbr->name=(char *)malloc(strlen(neighbor)+1);
88 memcpy(nbr->name,neighbor,strlen(neighbor)+1);
89 nbr->length=strlen(neighbor)+1;
90
91 printf("Neighbor: %s Length: %d\n",nbr->name,nbr->length);
92
93
94}
95
96void
akmhoque03004e62012-09-06 01:12:28 -050097get_lsa_identifier(struct name_prefix *lsaId,struct ccn_closure *selfp, struct ccn_upcall_info *info, int offset)
akmhoque53f64222012-09-05 13:57:51 -050098{
99
akmhoque03004e62012-09-06 01:12:28 -0500100 printf("get_lsa_identifier called\n");
akmhoque53f64222012-09-05 13:57:51 -0500101 int res,i;
102 int nlsr_position=0;
103 int name_comps=(int)info->interest_comps->n;
104 int len=0;
105
106 for(i=0;i<name_comps;i++)
107 {
108 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
109 if( res == 0)
110 {
111 nlsr_position=i;
112 break;
113 }
114 }
115
116
117 const unsigned char *comp_ptr1;
118 size_t comp_size;
akmhoque03004e62012-09-06 01:12:28 -0500119 for(i=nlsr_position+3+offset;i<info->interest_comps->n-1;i++)
akmhoque53f64222012-09-05 13:57:51 -0500120 {
121 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&comp_ptr1, &comp_size);
122 len+=1;
123 len+=(int)comp_size;
124 }
125 len++;
126
127 char *neighbor=(char *)malloc(len);
128 memset(neighbor,0,len);
129
akmhoque03004e62012-09-06 01:12:28 -0500130 for(i=nlsr_position+3+offset; i<info->interest_comps->n-1;i++)
akmhoque53f64222012-09-05 13:57:51 -0500131 {
132 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&comp_ptr1, &comp_size);
133 memcpy(neighbor+strlen(neighbor),"/",1);
134 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
135
136 }
akmhoque53f64222012-09-05 13:57:51 -0500137
138 lsaId->name=(char *)malloc(strlen(neighbor)+1);
139 memset(lsaId->name,0,strlen(neighbor)+1);
140 memcpy(lsaId->name,neighbor,strlen(neighbor)+1);
141 lsaId->length=strlen(neighbor)+1;
142
143 printf("LSA Identifier: %s Length: %d\n",lsaId->name,lsaId->length-1);
akmhoque03004e62012-09-06 01:12:28 -0500144
akmhoque53f64222012-09-05 13:57:51 -0500145
146}
147
akmhoque03004e62012-09-06 01:12:28 -0500148int
149get_ls_type(struct ccn_closure *selfp, struct ccn_upcall_info *info)
150{
151 int res,i;
152 int nlsr_position=0;
153 int name_comps=(int)info->interest_comps->n;
154
155 int ret=0;
156
157 for(i=0;i<name_comps;i++)
158 {
159 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
160 if( res == 0)
161 {
162 nlsr_position=i;
163 break;
164 }
165 }
166
167
168 const unsigned char *comp_ptr1;
169 size_t comp_size;
170 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+2,&comp_ptr1, &comp_size);
171
172 ret=atoi((char *)comp_ptr1);
173
174 return ret;
175
176}
177
178void
179get_lsdb_version(char *lsdb_version,struct ccn_closure *selfp, struct ccn_upcall_info *info )
180{
181 const unsigned char *comp_ptr1;
182 size_t comp_size;
183 ccn_name_comp_get(info->content_ccnb, info->content_comps,info->content_comps->n-2,&comp_ptr1, &comp_size);
184 memcpy(lsdb_version,(char *)comp_ptr1,(int)comp_size);
185
186}
187
188
189/* Call back function registered in ccnd to get all interest coming to NLSR application */
190
akmhoque59980a52012-08-09 12:36:09 -0500191enum ccn_upcall_res
192incoming_interest(struct ccn_closure *selfp,
193 enum ccn_upcall_kind kind, struct ccn_upcall_info *info)
194{
akmhoque03004e62012-09-06 01:12:28 -0500195
akmhoque59980a52012-08-09 12:36:09 -0500196 switch (kind) {
197 case CCN_UPCALL_FINAL:
198 break;
199 case CCN_UPCALL_INTEREST:
akmhoque03004e62012-09-06 01:12:28 -0500200 // printing the name prefix for which it received interest
201 printf("Interest Received for name: ");
202 struct ccn_charbuf*c;
203 c=ccn_charbuf_create();
204 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
205 printf("%s\n",ccn_charbuf_as_string(c));
206 ccn_charbuf_destroy(&c);
207
akmhoque1c9b92f2012-08-13 10:57:50 -0500208 process_incoming_interest(selfp, info);
akmhoque03004e62012-09-06 01:12:28 -0500209
akmhoque59980a52012-08-09 12:36:09 -0500210 break;
akmhoque03004e62012-09-06 01:12:28 -0500211
akmhoque59980a52012-08-09 12:36:09 -0500212 default:
213 break;
214 }
akmhoque03004e62012-09-06 01:12:28 -0500215
akmhoque59980a52012-08-09 12:36:09 -0500216 return CCN_UPCALL_RESULT_OK;
217}
218
akmhoque03004e62012-09-06 01:12:28 -0500219/* Function for processing incoming interest and reply with content/NACK content */
akmhoque59980a52012-08-09 12:36:09 -0500220
akmhoqued79438d2012-08-27 13:31:42 -0500221void
akmhoque1c9b92f2012-08-13 10:57:50 -0500222process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
223{
224 printf("process_incoming_interest called \n");
akmhoque1c9b92f2012-08-13 10:57:50 -0500225 const unsigned char *comp_ptr1;
226 size_t comp_size;
227 int res,i;
228 int nlsr_position=0;
229 int name_comps=(int)info->interest_comps->n;
akmhoque53f64222012-09-05 13:57:51 -0500230
akmhoque1c9b92f2012-08-13 10:57:50 -0500231 for(i=0;i<name_comps;i++)
232 {
233 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
234 if( res == 0)
235 {
akmhoqueea3603e2012-08-13 11:24:09 -0500236 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500237 break;
238 }
239 }
240
241 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500242
akmhoque1c9b92f2012-08-13 10:57:50 -0500243 printf("Det= %s \n",comp_ptr1);
244
akmhoqued79438d2012-08-27 13:31:42 -0500245 if(!strcmp((char *)comp_ptr1,"info"))
246 {
247 process_incoming_interest_info(selfp,info);
248 }
akmhoque53f64222012-09-05 13:57:51 -0500249 if(!strcmp((char *)comp_ptr1,"lsdb"))
250 {
251 process_incoming_interest_lsdb(selfp,info);
252 }
253 if(!strcmp((char *)comp_ptr1,"lsa"))
254 {
255 process_incoming_interest_lsa(selfp,info);
256 }
akmhoque53f64222012-09-05 13:57:51 -0500257}
258
259void
260process_incoming_interest_info(struct ccn_closure *selfp, struct ccn_upcall_info *info)
261{
262 printf("process_incoming_interest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500263
akmhoque03004e62012-09-06 01:12:28 -0500264 printf("Sending Info Content back.....\n");
akmhoque53f64222012-09-05 13:57:51 -0500265 int res;
266 struct ccn_charbuf *data=ccn_charbuf_create();
267 struct ccn_charbuf *name=ccn_charbuf_create();
268 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
akmhoque03004e62012-09-06 01:12:28 -0500269
akmhoque53f64222012-09-05 13:57:51 -0500270 res=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]);
271 if (res >= 0)
272 {
273 sp.template_ccnb=ccn_charbuf_create();
274 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
275 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
276 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
277 ccn_charbuf_append_closer(sp.template_ccnb);
278
279
280 char *raw_data=(char *)malloc(16);
281 memset(raw_data,0,16);
282 sprintf(raw_data,"%ld", nlsr->lsdb_synch_interval);
283
284 res= ccn_sign_content(nlsr->ccn, data, name, &sp, raw_data,strlen(raw_data));
285 if(res >= 0)
286 printf("Signing Content is successful \n");
287
288 res=ccn_put(nlsr->ccn,data->buf,data->length);
289 if(res >= 0)
290 printf("Sending Info Content is successful \n");
291
292 printf("Info Content sending done....\n");
293
294
295 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix *));
akmhoque03004e62012-09-06 01:12:28 -0500296 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque53f64222012-09-05 13:57:51 -0500297 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500298
akmhoque53f64222012-09-05 13:57:51 -0500299
300 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr)>=nlsr->interest_retry )
301 {
302 send_info_interest_to_neighbor(nbr);
303 }
304
305 free(nbr);
306 free(raw_data);
307 ccn_charbuf_destroy(&sp.template_ccnb);
308 }
akmhoque03004e62012-09-06 01:12:28 -0500309
akmhoque53f64222012-09-05 13:57:51 -0500310 ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500311 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500312
akmhoquebf1aa832012-08-13 13:26:59 -0500313}
314
315
316void
317process_incoming_interest_lsdb(struct ccn_closure *selfp, struct ccn_upcall_info *info)
318{
319 printf("process_incoming_interest_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500320
321 int l,res;
akmhoquefce8cfc2012-08-14 14:00:33 -0500322 const unsigned char *exclbase;
akmhoque6d49e4d2012-08-14 13:49:30 -0500323 size_t size;
akmhoquefce8cfc2012-08-14 14:00:33 -0500324 struct ccn_buf_decoder decoder;
325 struct ccn_buf_decoder *d;
akmhoque53f64222012-09-05 13:57:51 -0500326 const unsigned char *comp;
327 int dbcmp=0;
akmhoque6d49e4d2012-08-14 13:49:30 -0500328
329 l = info->pi->offset[CCN_PI_E_Exclude] - info->pi->offset[CCN_PI_B_Exclude];
330 if (l > 0)
331 {
332 comp = NULL;
333 size = 0;
334 exclbase = info->interest_ccnb + info->pi->offset[CCN_PI_B_Exclude];
335 d = ccn_buf_decoder_start(&decoder, exclbase, l);
336 if (ccn_buf_match_dtag(d, CCN_DTAG_Exclude))
337 {
338 ccn_buf_advance(d);
339 if (ccn_buf_match_dtag(d, CCN_DTAG_Any))
340 ccn_buf_advance_past_element(d);
341 if (ccn_buf_match_dtag(d, CCN_DTAG_Component))
342 {
343 ccn_buf_advance(d);
344 ccn_buf_match_blob(d, &comp, &size);
akmhoquec9286692012-08-16 09:57:58 -0500345 ccn_buf_check_close(d);
akmhoque53f64222012-09-05 13:57:51 -0500346
347
akmhoque6d49e4d2012-08-14 13:49:30 -0500348 }
349 ccn_buf_check_close(d);
350 }
akmhoque6d49e4d2012-08-14 13:49:30 -0500351 if (comp != NULL)
akmhoque53f64222012-09-05 13:57:51 -0500352 {
353 printf("LSDB Version in Exclusion Filter is %s\n",comp);
akmhoque03004e62012-09-06 01:12:28 -0500354 printf("LSDB Version of own NLSR is: %s \n",nlsr->lsdb->lsdb_version);
355 dbcmp=strcmp(nlsr->lsdb->lsdb_version,(char *)comp);
akmhoque53f64222012-09-05 13:57:51 -0500356 }
akmhoque6d49e4d2012-08-14 13:49:30 -0500357 /* Now comp points to the start of your potential number, and size is its length */
358 }
akmhoque53f64222012-09-05 13:57:51 -0500359 else
akmhoquecb017752012-08-16 11:03:45 -0500360 {
akmhoque53f64222012-09-05 13:57:51 -0500361 printf("LSDB Version in Exclusion Filter is: None Added\n");
362 dbcmp=1;
akmhoquecb017752012-08-16 11:03:45 -0500363
364 }
365
akmhoqued79438d2012-08-27 13:31:42 -0500366 struct ccn_charbuf *data=ccn_charbuf_create();
akmhoque53f64222012-09-05 13:57:51 -0500367 struct ccn_charbuf *name=ccn_charbuf_create();
368 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
369
akmhoque03004e62012-09-06 01:12:28 -0500370 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]);
akmhoque887fc0c2012-08-27 15:06:06 -0500371
akmhoqued79438d2012-08-27 13:31:42 -0500372 sp.template_ccnb=ccn_charbuf_create();
373 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
374 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
akmhoque53f64222012-09-05 13:57:51 -0500375 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
376 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoqued79438d2012-08-27 13:31:42 -0500377
akmhoqued79438d2012-08-27 13:31:42 -0500378
akmhoque53f64222012-09-05 13:57:51 -0500379 if(dbcmp>0)
380 {
381 printf("Has Updated Database than Neighbor\n");
akmhoque03004e62012-09-06 01:12:28 -0500382 printf("Sending LSDB Summary of Updated LSDB Content...\n");
akmhoque53f64222012-09-05 13:57:51 -0500383
akmhoque03004e62012-09-06 01:12:28 -0500384 ccn_name_append_str(name,nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500385
akmhoque03004e62012-09-06 01:12:28 -0500386 struct ccn_charbuf *lsdb_data=ccn_charbuf_create();
387 get_lsdb_summary(lsdb_data);
akmhoque53f64222012-09-05 13:57:51 -0500388
akmhoque03004e62012-09-06 01:12:28 -0500389 char *raw_data=ccn_charbuf_as_string(lsdb_data);
akmhoque53f64222012-09-05 13:57:51 -0500390
akmhoque03004e62012-09-06 01:12:28 -0500391 //printf("Content Data to be sent: %s \n",raw_data);
akmhoque53f64222012-09-05 13:57:51 -0500392
akmhoque03004e62012-09-06 01:12:28 -0500393 if( nlsr->adj_build_count == 0 || nlsr->is_build_adj_lsa_sheduled == 1 || strlen((char *)raw_data) == 0 )
akmhoque53f64222012-09-05 13:57:51 -0500394 {
395 res= ccn_sign_content(nlsr->ccn, data, name, &sp, "WAIT" , strlen("WAIT"));
396 }
397 else
398 {
399 res= ccn_sign_content(nlsr->ccn, data, name, &sp, raw_data , strlen(raw_data));
400 }
401 if(res >= 0)
402 printf("Signing LSDB Summary of Updated LSDB Content is successful \n");
403
404 res=ccn_put(nlsr->ccn,data->buf,data->length);
405
406 if(res >= 0)
407 printf("Sending LSDB Summary of Updated LSDB Content is successful \n");
akmhoque03004e62012-09-06 01:12:28 -0500408
409 ccn_charbuf_destroy(&lsdb_data);
akmhoque53f64222012-09-05 13:57:51 -0500410 }
411 else
412 {
413 printf("Does not have Updated Database than Neighbor\n");
414
415 printf("Sending NACK Content.....\n");
akmhoque53f64222012-09-05 13:57:51 -0500416 res= ccn_sign_content(nlsr->ccn, data, name, &sp, "NACK", strlen("NACK"));
417 if(res >= 0)
418 printf("Signing NACK Content is successful \n");
419
420 res=ccn_put(nlsr->ccn,data->buf,data->length);
421
422 if(res >= 0)
423 printf("Sending NACK Content is successful \n");
424
425
426 }
akmhoqued79438d2012-08-27 13:31:42 -0500427
428 ccn_charbuf_destroy(&data);
akmhoqued79438d2012-08-27 13:31:42 -0500429 ccn_charbuf_destroy(&name);
430 ccn_charbuf_destroy(&sp.template_ccnb);
431
akmhoque03004e62012-09-06 01:12:28 -0500432
akmhoqued79438d2012-08-27 13:31:42 -0500433}
434
akmhoque03004e62012-09-06 01:12:28 -0500435
akmhoque53f64222012-09-05 13:57:51 -0500436void
437process_incoming_interest_lsa(struct ccn_closure *selfp, struct ccn_upcall_info *info)
akmhoque1c9b92f2012-08-13 10:57:50 -0500438{
akmhoque53f64222012-09-05 13:57:51 -0500439 printf("process_incoming_interest_lsa called \n");
akmhoque1c9b92f2012-08-13 10:57:50 -0500440
akmhoque53f64222012-09-05 13:57:51 -0500441 int res;
akmhoque1c9b92f2012-08-13 10:57:50 -0500442
akmhoque53f64222012-09-05 13:57:51 -0500443 struct name_prefix *lsaId=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500444 get_lsa_identifier(lsaId,selfp,info,0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500445
akmhoque03004e62012-09-06 01:12:28 -0500446 printf("LSA Identifier: %s Length: %d\n",lsaId->name,lsaId->length);
447 int ls_type=get_ls_type(selfp, info);
akmhoque1c9b92f2012-08-13 10:57:50 -0500448
akmhoque53f64222012-09-05 13:57:51 -0500449 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
450
akmhoque03004e62012-09-06 01:12:28 -0500451 if ( ls_type == LS_TYPE_NAME )
akmhoque1c9b92f2012-08-13 10:57:50 -0500452 {
akmhoque53f64222012-09-05 13:57:51 -0500453 printf("Interest Received for NAME LSA\n");
454 get_name_lsa_data(lsa_data,lsaId);
455 }
akmhoque03004e62012-09-06 01:12:28 -0500456 else if ( ls_type == LS_TYPE_ADJ )
akmhoque53f64222012-09-05 13:57:51 -0500457 {
458 printf("Interest Received for ADJ LSA\n");
459 get_adj_lsa_data(lsa_data,lsaId);
akmhoque1c9b92f2012-08-13 10:57:50 -0500460 }
461
akmhoque53f64222012-09-05 13:57:51 -0500462 char *rdata=ccn_charbuf_as_string(lsa_data);
463 char *raw_data=(char *)malloc(strlen(rdata)+1);
464 memset(raw_data,0,strlen(rdata)+1);
465 memcpy(raw_data,(char *)rdata,strlen(rdata)+1);
akmhoque03004e62012-09-06 01:12:28 -0500466 //printf("Content Data to be sent: %s\n",raw_data);
akmhoque1c9b92f2012-08-13 10:57:50 -0500467
akmhoque53f64222012-09-05 13:57:51 -0500468 struct ccn_charbuf *data=ccn_charbuf_create();
469 struct ccn_charbuf *name=ccn_charbuf_create();
470 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
akmhoque03004e62012-09-06 01:12:28 -0500471
akmhoque53f64222012-09-05 13:57:51 -0500472 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]);
akmhoque1c9b92f2012-08-13 10:57:50 -0500473
akmhoque53f64222012-09-05 13:57:51 -0500474 sp.template_ccnb=ccn_charbuf_create();
475 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
476 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
477 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
478 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoque1c9b92f2012-08-13 10:57:50 -0500479
akmhoque53f64222012-09-05 13:57:51 -0500480 res= ccn_sign_content(nlsr->ccn, data, name, &sp, raw_data , strlen(raw_data));
481 if(res >= 0)
482 printf("Signing LSA Content is successful \n");
483
484 res=ccn_put(nlsr->ccn,data->buf,data->length);
485 if(res >= 0)
486 printf("Sending LSA Content is successful \n");
487
akmhoque03004e62012-09-06 01:12:28 -0500488
489
akmhoque53f64222012-09-05 13:57:51 -0500490 ccn_charbuf_destroy(&data);
491 ccn_charbuf_destroy(&name);
492 ccn_charbuf_destroy(&sp.template_ccnb);
493 ccn_charbuf_destroy(&lsa_data);
494
495 free(raw_data);
496 free(lsaId);
akmhoque1c9b92f2012-08-13 10:57:50 -0500497}
498
akmhoque03004e62012-09-06 01:12:28 -0500499/* Call back function registered in ccnd to get all content coming to NLSR application */
500
akmhoque53f64222012-09-05 13:57:51 -0500501enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
502 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500503{
504
akmhoque03004e62012-09-06 01:12:28 -0500505
akmhoque53f64222012-09-05 13:57:51 -0500506 switch(kind) {
507 case CCN_UPCALL_FINAL:
508 break;
509 case CCN_UPCALL_CONTENT:
akmhoque03004e62012-09-06 01:12:28 -0500510 printf("Content Received for Name: ");
511 struct ccn_charbuf*c;
512 c=ccn_charbuf_create();
513 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
514 printf("%s\n",ccn_charbuf_as_string(c));
515 ccn_charbuf_destroy(&c);
516
517 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500518
akmhoque53f64222012-09-05 13:57:51 -0500519 break;
520 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoque03004e62012-09-06 01:12:28 -0500521 printf("Interest Timed Out Received for Name: ");
522
523 struct ccn_charbuf*ito;
524 ito=ccn_charbuf_create();
525 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
526 printf("%s\n",ccn_charbuf_as_string(ito));
527 ccn_charbuf_destroy(&ito);
528
akmhoque53f64222012-09-05 13:57:51 -0500529 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500530
akmhoque53f64222012-09-05 13:57:51 -0500531 break;
532 default:
533 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoque03004e62012-09-06 01:12:28 -0500534 return CCN_UPCALL_RESULT_ERR;
akmhoque53f64222012-09-05 13:57:51 -0500535 }
akmhoqued79438d2012-08-27 13:31:42 -0500536
akmhoque53f64222012-09-05 13:57:51 -0500537 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500538}
539
akmhoque03004e62012-09-06 01:12:28 -0500540
akmhoqued79438d2012-08-27 13:31:42 -0500541void
akmhoque53f64222012-09-05 13:57:51 -0500542process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500543{
akmhoque53f64222012-09-05 13:57:51 -0500544 printf("process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500545
546 const unsigned char *comp_ptr1;
547 size_t comp_size;
548 int res,i;
549 int nlsr_position=0;
550 int name_comps=(int)info->interest_comps->n;
551
552 for(i=0;i<name_comps;i++)
553 {
554 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
555 if( res == 0)
556 {
557 nlsr_position=i;
558 break;
559 }
560 }
561
562 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
563
564 printf("Det= %s \n",comp_ptr1);
565
566 if(!strcmp((char *)comp_ptr1,"info"))
567 {
568 process_incoming_content_info(selfp,info);
569 }
570 if(!strcmp((char *)comp_ptr1,"lsdb"))
571 {
572 process_incoming_content_lsdb(selfp,info);
573 }
574 if(!strcmp((char *)comp_ptr1,"lsa"))
575 {
576 process_incoming_content_lsa(selfp,info);
577 }
akmhoque03004e62012-09-06 01:12:28 -0500578
akmhoque53f64222012-09-05 13:57:51 -0500579}
580
akmhoque03004e62012-09-06 01:12:28 -0500581
akmhoque53f64222012-09-05 13:57:51 -0500582void
583process_incoming_content_info(struct ccn_closure *selfp, struct ccn_upcall_info* info)
584{
585 printf("process_incoming_content_info called \n");
akmhoque03004e62012-09-06 01:12:28 -0500586
akmhoque53f64222012-09-05 13:57:51 -0500587 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
588 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500589
akmhoque53f64222012-09-05 13:57:51 -0500590 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500591
akmhoque53f64222012-09-05 13:57:51 -0500592
593 const unsigned char *ptr;
594 size_t length;
595 ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E_Content]-info->pco->offset[CCN_PCO_B_Content], info->pco, &ptr, &length);
akmhoque03004e62012-09-06 01:12:28 -0500596 //printf("Content data: %s\n",ptr);
akmhoque53f64222012-09-05 13:57:51 -0500597
598 long int interval=atoi((char *)ptr);
599
600
akmhoque03004e62012-09-06 01:12:28 -0500601
akmhoque53f64222012-09-05 13:57:51 -0500602 update_adjacent_timed_out_zero_to_adl(nbr);
603 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
604 update_lsdb_synch_interval_to_adl(nbr,interval);
605 print_adjacent_from_adl();
606
akmhoque03004e62012-09-06 01:12:28 -0500607
608
akmhoque53f64222012-09-05 13:57:51 -0500609 if(!nlsr->is_build_adj_lsa_sheduled)
610 {
611 printf("Scheduling Build and Install Adj LSA...\n");
612 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 100000, &build_and_install_adj_lsa, NULL, 0);
613 nlsr->is_build_adj_lsa_sheduled=1;
614 }
615 else
616 {
617 printf("Build and Install Adj LSA already scheduled\n");
618 }
619
akmhoque03004e62012-09-06 01:12:28 -0500620
akmhoque53f64222012-09-05 13:57:51 -0500621 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500622
akmhoque53f64222012-09-05 13:57:51 -0500623
624}
625
akmhoque03004e62012-09-06 01:12:28 -0500626
akmhoque53f64222012-09-05 13:57:51 -0500627void
628process_incoming_content_lsdb(struct ccn_closure *selfp, struct ccn_upcall_info* info)
629{
630 printf("process_incoming_content_lsdb called \n");
631
akmhoque53f64222012-09-05 13:57:51 -0500632 const unsigned char *ptr;
633 size_t length;
634 ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E_Content]-info->pco->offset[CCN_PCO_B_Content], info->pco, &ptr, &length);
akmhoque03004e62012-09-06 01:12:28 -0500635 //printf("Content data: %s\n",ptr);
akmhoque53f64222012-09-05 13:57:51 -0500636
637 if( (strcmp("NACK",(char *)ptr) != 0 ) && (strcmp("WAIT",(char *)ptr) != 0 ) )
638 {
639 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
640 get_nbr(nbr,selfp,info);
641
642 char *nl;
643 int num_element;
644 int i;
645 char *rem;
646 const char *sep="|";
647 char *orig_router;
648 char *lst;
649 int ls_type;
650 char *lsid;
651 long int ls_id;
akmhoque03004e62012-09-06 01:12:28 -0500652 char *orig_time;
akmhoque53f64222012-09-05 13:57:51 -0500653
654 nl=strtok_r((char *)ptr,sep,&rem);
655 num_element=atoi(nl);
656
657 for(i = 0 ; i < num_element ; i++)
658 {
659 orig_router=strtok_r(NULL,sep,&rem);
660 lst=strtok_r(NULL,sep,&rem);
661 ls_type=atoi(lst);
662 printf("Orig Router: %s ls Type: %d",orig_router,ls_type);
663
664 if(ls_type == LS_TYPE_NAME)
665 {
666 lsid=strtok_r(NULL,sep,&rem);
667 ls_id=atoi(lsid);
akmhoque03004e62012-09-06 01:12:28 -0500668 orig_time=strtok_r(NULL,sep,&rem);
669 printf(" LS Id: %ld Orig Time: %s\n",ls_id ,orig_time);
670 int is_new_name_lsa=check_is_new_name_lsa(orig_router,lst,lsid,orig_time);
671 if ( is_new_name_lsa == 1 )
672 {
673 printf("New NAME LSA.....\n");
674 send_interest_for_name_lsa(nbr,orig_router,lst,lsid);
675 }
676 else
677 {
678 printf("Name LSA already exists in LSDB\n");
679 }
akmhoque53f64222012-09-05 13:57:51 -0500680 }
681 else
682 {
akmhoque03004e62012-09-06 01:12:28 -0500683 orig_time=strtok_r(NULL,sep,&rem);
684 printf(" Orig Time: %s\n",orig_time);
685 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router,lst,orig_time);
686 if ( is_new_adj_lsa == 1 )
687 {
688 printf("New ADJ LSA.....\n");
689 send_interest_for_adj_lsa(nbr,orig_router,lst);
690 }
691 else
692 {
693 printf("ADJ LSA already exists in LSDB\n");
694 }
akmhoque53f64222012-09-05 13:57:51 -0500695 }
akmhoque03004e62012-09-06 01:12:28 -0500696
akmhoque53f64222012-09-05 13:57:51 -0500697 }
akmhoque53f64222012-09-05 13:57:51 -0500698
akmhoque03004e62012-09-06 01:12:28 -0500699 char *lsdb_version=(char *)malloc(20);
700 memset(lsdb_version,0,20);
701 get_lsdb_version(lsdb_version,selfp,info);
702
akmhoque53f64222012-09-05 13:57:51 -0500703 printf("Old LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
akmhoque03004e62012-09-06 01:12:28 -0500704 update_adjacent_lsdb_version_to_adl(nbr,lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500705 printf("New LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
akmhoque53f64222012-09-05 13:57:51 -0500706
akmhoque03004e62012-09-06 01:12:28 -0500707
708 free(lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500709 free(nbr);
710 }
711 else if (strcmp("WAIT",(char *)ptr) == 0)
712 {
713 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
714 get_nbr(nbr,selfp,info);
715 long int interval=get_lsdb_synch_interval(nbr->name);
716 adjust_adjacent_last_lsdb_requested_to_adl(nbr->name,(long int)interval/2);
akmhoque03004e62012-09-06 01:12:28 -0500717
akmhoque53f64222012-09-05 13:57:51 -0500718
719 free(nbr);
720 }
721 else
722 {
723 printf("NACK Content Received\n");
724 }
akmhoque53f64222012-09-05 13:57:51 -0500725}
726
akmhoque03004e62012-09-06 01:12:28 -0500727
akmhoque53f64222012-09-05 13:57:51 -0500728void
729process_incoming_content_lsa(struct ccn_closure *selfp, struct ccn_upcall_info* info)
730{
731 printf("process_incoming_content_lsa called \n");
732
733 char *sep="|";
734 char *rem;
735 char *orig_router;
736 char *orl;
737 int orig_router_length;
738 char *lst;
739 int ls_type;
740 char *lsid;
741 long int ls_id;
742 char *isvld;
743 int isValid;
744 char *num_link;
745 int no_link;
746 char *np;
747 char *np_length;
748 int name_length;
749 char *data;
750 char *orig_time;
751
752 const unsigned char *ptr;
753 size_t length;
754 ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E_Content]-info->pco->offset[CCN_PCO_B_Content], info->pco, &ptr, &length);
akmhoque03004e62012-09-06 01:12:28 -0500755 //printf("Content data Received: %s\n",ptr);
akmhoque53f64222012-09-05 13:57:51 -0500756
757 printf("LSA Data\n");
758
759 if( strlen((char *) ptr ) > 0 )
760 {
761
762 orig_router=strtok_r((char *)ptr,sep,&rem);
763 orl=strtok_r(NULL,sep,&rem);
764 orig_router_length=atoi(orl);
765
766 printf(" Orig Router Name : %s\n",orig_router);
767 printf(" Orig Router Length: %d\n",orig_router_length);
768
769 lst=strtok_r(NULL,sep,&rem);
770 ls_type=atoi(lst);
771
772 printf(" LS Type : %d\n",ls_type);
773
akmhoque03004e62012-09-06 01:12:28 -0500774 if ( ls_type == LS_TYPE_NAME )
akmhoque53f64222012-09-05 13:57:51 -0500775 {
776 lsid=strtok_r(NULL,sep,&rem);
777 ls_id=atoi(lsid);
akmhoque03004e62012-09-06 01:12:28 -0500778 orig_time=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -0500779 isvld=strtok_r(NULL,sep,&rem);
780 isValid=atoi(isvld);
781 np=strtok_r(NULL,sep,&rem);
782 np_length=strtok_r(NULL,sep,&rem);
783 name_length=atoi(np_length);
akmhoque53f64222012-09-05 13:57:51 -0500784 printf(" LS ID : %ld\n",ls_id);
785 printf(" isValid : %d\n",isValid);
786 printf(" Name Prefix : %s\n",np);
akmhoque03004e62012-09-06 01:12:28 -0500787 printf(" Orig Time : %s\n",orig_time);
akmhoque53f64222012-09-05 13:57:51 -0500788 printf(" Name Prefix length: %d\n",name_length);
789
akmhoque03004e62012-09-06 01:12:28 -0500790 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
791
akmhoque53f64222012-09-05 13:57:51 -0500792 }
akmhoque03004e62012-09-06 01:12:28 -0500793 else if ( ls_type == LS_TYPE_ADJ )
akmhoque53f64222012-09-05 13:57:51 -0500794 {
795 orig_time=strtok_r(NULL,sep,&rem);
796 num_link=strtok_r(NULL,sep,&rem);
797 no_link=atoi(num_link);
798 data=rem;
799
800 printf(" No Link : %d\n",no_link);
801 printf(" Data : %s\n",data);
802
803 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
804 }
akmhoque53f64222012-09-05 13:57:51 -0500805 }
akmhoque53f64222012-09-05 13:57:51 -0500806}
807
akmhoque03004e62012-09-06 01:12:28 -0500808
akmhoque53f64222012-09-05 13:57:51 -0500809void
810process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
811{
812 printf("process_incoming_timed_out_interest called \n");
akmhoque53f64222012-09-05 13:57:51 -0500813 int res,i;
814 int nlsr_position=0;
815 int name_comps=(int)info->interest_comps->n;
816
817 for(i=0;i<name_comps;i++)
818 {
819 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
820 if( res == 0)
821 {
822 nlsr_position=i;
823 break;
824 }
825 }
826
827 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"info") == 0)
828 {
829 process_incoming_timed_out_interest_info(selfp,info);
830 }
831}
832
833void
834process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct ccn_upcall_info* info)
835{
836 printf("process_incoming_timed_out_interest_info called \n");
837
838 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
839 get_nbr(nbr,selfp,info);
840
841 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500842
843
akmhoque53f64222012-09-05 13:57:51 -0500844 update_adjacent_timed_out_to_adl(nbr,1);
845 print_adjacent_from_adl();
846 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500847
akmhoque53f64222012-09-05 13:57:51 -0500848
849 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
850 {
851 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
852 send_info_interest_to_neighbor(nbr);
853 }
854 else
855 {
856 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
857 update_adjacent_status_to_adl(nbr,NBR_DOWN);
858 if(!nlsr->is_build_adj_lsa_sheduled)
859 {
860 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
861 nlsr->is_build_adj_lsa_sheduled=1;
862 }
863 }
864
865 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500866
akmhoque53f64222012-09-05 13:57:51 -0500867
868}
869
akmhoque03004e62012-09-06 01:12:28 -0500870int
akmhoque53f64222012-09-05 13:57:51 -0500871send_info_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
872{
873 printf("send_info_interest called \n");
874 printf("\n");
akmhoqued79438d2012-08-27 13:31:42 -0500875
akmhoque53f64222012-09-05 13:57:51 -0500876 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500877 struct ndn_neighbor *nbr;
878
879 struct hashtb_enumerator ee;
880 struct hashtb_enumerator *e = &ee;
881
882 hashtb_start(nlsr->adl, e);
883 adl_element=hashtb_n(nlsr->adl);
884
885 for(i=0;i<adl_element;i++)
886 {
887 nbr=e->data;
888 send_info_interest_to_neighbor(nbr->neighbor);
889 hashtb_next(e);
890 }
akmhoque53f64222012-09-05 13:57:51 -0500891 hashtb_end(e);
892
akmhoque53f64222012-09-05 13:57:51 -0500893 return 0;
894}
895
896void
897send_info_interest_to_neighbor(struct name_prefix *nbr)
898{
899 printf("send_info_interest_to_neighbor called \n");
akmhoqued79438d2012-08-27 13:31:42 -0500900
901 int res;
akmhoque53f64222012-09-05 13:57:51 -0500902 char info_str[5];
903 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500904
akmhoqued79438d2012-08-27 13:31:42 -0500905 memset(&nlsr_str,0,5);
906 sprintf(nlsr_str,"nlsr");
907 memset(&info_str,0,5);
908 sprintf(info_str,"info");
909
akmhoque53f64222012-09-05 13:57:51 -0500910
911 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500912 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500913
akmhoque03004e62012-09-06 01:12:28 -0500914 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+strlen(nlsr->router_name)+1);
915 memset(int_name,0,strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+strlen(nlsr->router_name)+1);
akmhoque53f64222012-09-05 13:57:51 -0500916 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
917 memcpy(int_name+strlen(int_name),"/",1);
918 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
919 memcpy(int_name+strlen(int_name),"/",1);
920 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500921 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500922
923
924 res=ccn_name_from_uri(name,int_name);
925 if ( res >=0 )
926 {
akmhoque53f64222012-09-05 13:57:51 -0500927 /* adding InterestLifeTime and InterestScope filter */
928
929 struct ccn_charbuf *templ;
930 templ = ccn_charbuf_create();
931
932 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
933 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
934 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500935 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
936 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
937 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
938 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500939
940 appendLifetime(templ,nlsr->interest_resend_time);
941 ccn_charbuf_append_closer(templ); /* </Interest> */
942 /* Adding InterestLifeTime and InterestScope filter done */
943
944 printf("Sending info interest on name prefix : %s \n",int_name);
945
946 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
947
948 if ( res >= 0 )
949 printf("Info interest sending Successfull .... \n");
950 ccn_charbuf_destroy(&templ);
951 }
952 ccn_charbuf_destroy(&name);
953 free(int_name);
954
955}
956
957
958int
959send_lsdb_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
960{
961 printf("send_lsdb_interest called \n");
962
Obaid Amin485277a2012-09-07 01:08:28 -0400963 if(flags == CCN_SCHEDULE_CANCEL)
964 return -1;
965
akmhoque53f64222012-09-05 13:57:51 -0500966 int i, adl_element;
967 struct ndn_neighbor *nbr;
968
969 struct hashtb_enumerator ee;
970 struct hashtb_enumerator *e = &ee;
971
972 hashtb_start(nlsr->adl, e);
973 adl_element=hashtb_n(nlsr->adl);
974
975 for(i=0;i<adl_element;i++)
976 {
977 nbr=e->data;
978
979 if(nbr->status == NBR_ACTIVE)
980 {
981 if(nbr->is_lsdb_send_interest_scheduled == 0)
982 {
983 long int time_diff=get_nbr_time_diff_lsdb_req(nbr->neighbor->name);
984 printf("Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);
985
986 if(time_diff>=get_lsdb_synch_interval(nbr->neighbor->name))
987 {
988 nbr->is_lsdb_send_interest_scheduled=1;
989 send_lsdb_interest_to_nbr(nbr->neighbor);
990 }
991 }
992 }
993 hashtb_next(e);
994 }
995
996 hashtb_end(e);
997 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 30000000, &send_lsdb_interest, NULL, 0);
998
akmhoque53f64222012-09-05 13:57:51 -0500999 return 0;
1000}
1001
1002void
1003send_lsdb_interest_to_nbr(struct name_prefix *nbr)
1004{
1005 printf("send_lsdb_interest_to_nbr called \n");
akmhoque03004e62012-09-06 01:12:28 -05001006
akmhoque53f64222012-09-05 13:57:51 -05001007 char *last_lsdb_version=get_nbr_lsdb_version(nbr->name);
1008
1009 if(last_lsdb_version !=NULL)
1010 {
1011 printf("Last LSDB Version: %s \n",last_lsdb_version);
1012
1013
1014 struct ccn_charbuf *name;
1015 int res;
akmhoque53f64222012-09-05 13:57:51 -05001016 char lsdb_str[5];
1017 char nlsr_str[5];
akmhoque03004e62012-09-06 01:12:28 -05001018
akmhoque53f64222012-09-05 13:57:51 -05001019 memset(&nlsr_str,0,5);
1020 sprintf(nlsr_str,"nlsr");
1021 memset(&lsdb_str,0,5);
1022 sprintf(lsdb_str,"lsdb");
1023 //make and send interest with exclusion filter as last_lsdb_version
1024 printf("Sending interest for name prefix:%s/%s/%s\n",nbr->name,nlsr_str,lsdb_str);
1025 name=ccn_charbuf_create();
1026 res=ccn_name_from_uri(name,nbr->name);
1027
1028 if( res >= 0)
1029 {
1030 ccn_name_append_str(name,nlsr_str);
1031 ccn_name_append_str(name,lsdb_str);
akmhoque53f64222012-09-05 13:57:51 -05001032 /* adding Exclusion filter */
1033
1034 struct ccn_charbuf *templ;
1035 templ = ccn_charbuf_create();
akmhoque03004e62012-09-06 01:12:28 -05001036
akmhoque53f64222012-09-05 13:57:51 -05001037 struct ccn_charbuf *c;
1038 c = ccn_charbuf_create();
akmhoque03004e62012-09-06 01:12:28 -05001039
akmhoque53f64222012-09-05 13:57:51 -05001040
1041 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1042 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1043 ccn_charbuf_append_closer(templ); /* </Name> */
1044 ccn_charbuf_append_tt(templ, CCN_DTAG_Exclude, CCN_DTAG);
1045 ccnb_tagged_putf(templ, CCN_DTAG_Any, "");
1046 ccn_charbuf_reset(c);
1047 ccn_charbuf_putf(c, "%s", last_lsdb_version);
akmhoque03004e62012-09-06 01:12:28 -05001048
akmhoque53f64222012-09-05 13:57:51 -05001049 ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length);
1050 ccn_charbuf_append_closer(templ); /* </Exclude> */
1051 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1052 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1053 ccn_charbuf_append(templ, "2", 1);
1054 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque03004e62012-09-06 01:12:28 -05001055
akmhoque53f64222012-09-05 13:57:51 -05001056 appendLifetime(templ,nlsr->interest_resend_time);
akmhoque03004e62012-09-06 01:12:28 -05001057
akmhoque53f64222012-09-05 13:57:51 -05001058 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoque03004e62012-09-06 01:12:28 -05001059
1060
akmhoque53f64222012-09-05 13:57:51 -05001061 /* Adding Exclusion filter done */
akmhoque03004e62012-09-06 01:12:28 -05001062
akmhoque53f64222012-09-05 13:57:51 -05001063 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
1064
1065 if ( res >= 0 )
1066 {
1067 printf("Interest sending Successfull .... \n");
1068 update_adjacent_last_lsdb_requested_to_adl(nbr->name,get_current_time_sec());
1069
1070 }
1071 ccn_charbuf_destroy(&c);
1072 ccn_charbuf_destroy(&templ);
1073 }
1074 ccn_charbuf_destroy(&name);
1075 }
1076 set_is_lsdb_send_interest_scheduled_to_zero(nbr->name);
akmhoque53f64222012-09-05 13:57:51 -05001077}
1078
1079void
1080send_interest_for_name_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type, char *ls_id)
1081{
1082 printf("send_interest_for_name_lsa called\n");
1083
1084 int res;
akmhoque53f64222012-09-05 13:57:51 -05001085 char lsa_str[5];
1086 char nlsr_str[5];
1087
akmhoque53f64222012-09-05 13:57:51 -05001088 memset(&nlsr_str,0,5);
1089 sprintf(nlsr_str,"nlsr");
1090 memset(&lsa_str,0,5);
1091 sprintf(lsa_str,"lsa");
1092
akmhoque03004e62012-09-06 01:12:28 -05001093 char *int_name=(char *)malloc(nbr->length + strlen(ls_type)+strlen(orig_router)+strlen(nlsr_str)+strlen(lsa_str)+3);
1094 memset(int_name,0,nbr->length +strlen(ls_type)+ strlen(orig_router)+strlen(nlsr_str)+strlen(lsa_str)+3);
akmhoque53f64222012-09-05 13:57:51 -05001095
1096 memcpy(int_name+strlen(int_name),nbr->name,nbr->length);
1097 memcpy(int_name+strlen(int_name),"/",1);
1098 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
1099 memcpy(int_name+strlen(int_name),"/",1);
1100 memcpy(int_name+strlen(int_name),lsa_str,strlen(lsa_str));
akmhoque03004e62012-09-06 01:12:28 -05001101 memcpy(int_name+strlen(int_name),"/",1);
1102 memcpy(int_name+strlen(int_name),ls_type,strlen(ls_type));
1103 memcpy(int_name+strlen(int_name),orig_router,strlen(orig_router));
akmhoque53f64222012-09-05 13:57:51 -05001104
akmhoque03004e62012-09-06 01:12:28 -05001105
akmhoque53f64222012-09-05 13:57:51 -05001106 struct ccn_charbuf *name;
1107 name=ccn_charbuf_create();
1108
1109
1110 res=ccn_name_from_uri(name,int_name);
1111 ccn_name_append_str(name,ls_type);
1112 ccn_name_append_str(name,ls_id);
akmhoque53f64222012-09-05 13:57:51 -05001113
1114
1115 /* adding InterestLifeTime and InterestScope filter */
1116
akmhoqued79438d2012-08-27 13:31:42 -05001117 struct ccn_charbuf *templ;
1118 templ = ccn_charbuf_create();
1119
akmhoqued79438d2012-08-27 13:31:42 -05001120 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1121 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1122 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque53f64222012-09-05 13:57:51 -05001123 //ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
1124 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1125 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1126 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
1127 ccn_charbuf_append_closer(templ); /* </Scope> */
1128
1129 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqued79438d2012-08-27 13:31:42 -05001130 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoque53f64222012-09-05 13:57:51 -05001131 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque03004e62012-09-06 01:12:28 -05001132
akmhoque53f64222012-09-05 13:57:51 -05001133 printf("Sending NAME LSA interest on name prefix : %s/%s/%s\n",int_name,ls_type,ls_id);
akmhoqued79438d2012-08-27 13:31:42 -05001134
akmhoqued79438d2012-08-27 13:31:42 -05001135 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
akmhoque53f64222012-09-05 13:57:51 -05001136
akmhoqued79438d2012-08-27 13:31:42 -05001137 if ( res >= 0 )
akmhoque53f64222012-09-05 13:57:51 -05001138 printf("NAME LSA interest sending Successfull .... \n");
1139
akmhoqued79438d2012-08-27 13:31:42 -05001140 ccn_charbuf_destroy(&templ);
1141 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -05001142 free(int_name);
akmhoque03004e62012-09-06 01:12:28 -05001143
1144
akmhoqued79438d2012-08-27 13:31:42 -05001145}
akmhoque53f64222012-09-05 13:57:51 -05001146
1147void
1148send_interest_for_adj_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type)
1149{
1150 printf("send_interest_for_adj_lsa called\n");
1151
1152 int res;
akmhoque53f64222012-09-05 13:57:51 -05001153 char lsa_str[5];
1154 char nlsr_str[5];
1155
akmhoque53f64222012-09-05 13:57:51 -05001156 memset(&nlsr_str,0,5);
1157 sprintf(nlsr_str,"nlsr");
1158 memset(&lsa_str,0,5);
1159 sprintf(lsa_str,"lsa");
1160
akmhoque03004e62012-09-06 01:12:28 -05001161 char *int_name=(char *)malloc(nbr->length + strlen(ls_type)+strlen(orig_router)+strlen(nlsr_str)+strlen(lsa_str)+3);
1162 memset(int_name,0,nbr->length +strlen(ls_type)+ strlen(orig_router)+strlen(nlsr_str)+strlen(lsa_str)+3);
akmhoque53f64222012-09-05 13:57:51 -05001163
1164 memcpy(int_name+strlen(int_name),nbr->name,nbr->length);
1165 memcpy(int_name+strlen(int_name),"/",1);
1166 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
1167 memcpy(int_name+strlen(int_name),"/",1);
1168 memcpy(int_name+strlen(int_name),lsa_str,strlen(lsa_str));
akmhoque03004e62012-09-06 01:12:28 -05001169 memcpy(int_name+strlen(int_name),"/",1);
1170 memcpy(int_name+strlen(int_name),ls_type,strlen(ls_type));
akmhoque53f64222012-09-05 13:57:51 -05001171 memcpy(int_name+strlen(int_name),orig_router,strlen(orig_router));
1172
akmhoque03004e62012-09-06 01:12:28 -05001173
akmhoque53f64222012-09-05 13:57:51 -05001174 struct ccn_charbuf *name;
1175 name=ccn_charbuf_create();
akmhoque03004e62012-09-06 01:12:28 -05001176
akmhoque53f64222012-09-05 13:57:51 -05001177
1178 ccn_name_from_uri(name,int_name);
1179 ccn_name_append_str(name,ls_type);
akmhoque03004e62012-09-06 01:12:28 -05001180
akmhoque53f64222012-09-05 13:57:51 -05001181
1182 /* adding InterestLifeTime and InterestScope filter */
1183
1184 struct ccn_charbuf *templ;
1185 templ = ccn_charbuf_create();
1186
1187 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1188 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1189 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque53f64222012-09-05 13:57:51 -05001190 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1191 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1192 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
1193 ccn_charbuf_append_closer(templ); /* </Scope> */
1194
1195 appendLifetime(templ,nlsr->interest_resend_time);
1196 ccn_charbuf_append_closer(templ); /* </Interest> */
1197 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque03004e62012-09-06 01:12:28 -05001198
akmhoque53f64222012-09-05 13:57:51 -05001199 printf("Sending ADJ LSA interest on name prefix : %s/%s/%s%s/%s/\n",nbr->name,nlsr_str,lsa_str,orig_router,ls_type);
1200
1201 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
1202
1203 if ( res >= 0 )
1204 printf("ADJ LSA interest sending Successfull .... \n");
1205
akmhoque53f64222012-09-05 13:57:51 -05001206 ccn_charbuf_destroy(&templ);
1207 ccn_charbuf_destroy(&name);
1208 free(int_name);
akmhoque53f64222012-09-05 13:57:51 -05001209}
1210
akmhoque03004e62012-09-06 01:12:28 -05001211