blob: bd594590c0b8fb2041c4b7aa7713b7cdc4fcc320 [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
akmhoque29c1db52012-09-07 14:47:43 -0500393 if( 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
akmhoque29c1db52012-09-07 14:47:43 -0500757
758
759
760
761
akmhoque53f64222012-09-05 13:57:51 -0500762 printf("LSA Data\n");
763
764 if( strlen((char *) ptr ) > 0 )
765 {
766
767 orig_router=strtok_r((char *)ptr,sep,&rem);
768 orl=strtok_r(NULL,sep,&rem);
769 orig_router_length=atoi(orl);
770
771 printf(" Orig Router Name : %s\n",orig_router);
772 printf(" Orig Router Length: %d\n",orig_router_length);
773
774 lst=strtok_r(NULL,sep,&rem);
775 ls_type=atoi(lst);
776
777 printf(" LS Type : %d\n",ls_type);
778
akmhoque03004e62012-09-06 01:12:28 -0500779 if ( ls_type == LS_TYPE_NAME )
akmhoque53f64222012-09-05 13:57:51 -0500780 {
781 lsid=strtok_r(NULL,sep,&rem);
782 ls_id=atoi(lsid);
akmhoque03004e62012-09-06 01:12:28 -0500783 orig_time=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -0500784 isvld=strtok_r(NULL,sep,&rem);
785 isValid=atoi(isvld);
786 np=strtok_r(NULL,sep,&rem);
787 np_length=strtok_r(NULL,sep,&rem);
788 name_length=atoi(np_length);
akmhoque53f64222012-09-05 13:57:51 -0500789 printf(" LS ID : %ld\n",ls_id);
790 printf(" isValid : %d\n",isValid);
791 printf(" Name Prefix : %s\n",np);
akmhoque03004e62012-09-06 01:12:28 -0500792 printf(" Orig Time : %s\n",orig_time);
akmhoque53f64222012-09-05 13:57:51 -0500793 printf(" Name Prefix length: %d\n",name_length);
794
akmhoque03004e62012-09-06 01:12:28 -0500795 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
796
akmhoque53f64222012-09-05 13:57:51 -0500797 }
akmhoque03004e62012-09-06 01:12:28 -0500798 else if ( ls_type == LS_TYPE_ADJ )
akmhoque53f64222012-09-05 13:57:51 -0500799 {
800 orig_time=strtok_r(NULL,sep,&rem);
801 num_link=strtok_r(NULL,sep,&rem);
802 no_link=atoi(num_link);
803 data=rem;
804
805 printf(" No Link : %d\n",no_link);
806 printf(" Data : %s\n",data);
807
808 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
809 }
akmhoque53f64222012-09-05 13:57:51 -0500810 }
akmhoque53f64222012-09-05 13:57:51 -0500811}
812
akmhoque03004e62012-09-06 01:12:28 -0500813
akmhoque53f64222012-09-05 13:57:51 -0500814void
815process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
816{
817 printf("process_incoming_timed_out_interest called \n");
akmhoque53f64222012-09-05 13:57:51 -0500818 int res,i;
819 int nlsr_position=0;
820 int name_comps=(int)info->interest_comps->n;
821
822 for(i=0;i<name_comps;i++)
823 {
824 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
825 if( res == 0)
826 {
827 nlsr_position=i;
828 break;
829 }
830 }
831
832 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"info") == 0)
833 {
834 process_incoming_timed_out_interest_info(selfp,info);
835 }
akmhoque29c1db52012-09-07 14:47:43 -0500836 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"lsdb") == 0)
837 {
838 process_incoming_timed_out_interest_lsdb(selfp,info);
839 }
840 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"lsa") == 0)
841 {
842 process_incoming_timed_out_interest_lsa(selfp,info);
843 }
akmhoque53f64222012-09-05 13:57:51 -0500844}
845
846void
847process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct ccn_upcall_info* info)
848{
849 printf("process_incoming_timed_out_interest_info called \n");
850
851 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
852 get_nbr(nbr,selfp,info);
853
854 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500855
856
akmhoque53f64222012-09-05 13:57:51 -0500857 update_adjacent_timed_out_to_adl(nbr,1);
858 print_adjacent_from_adl();
859 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500860
akmhoque53f64222012-09-05 13:57:51 -0500861
862 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
863 {
864 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
865 send_info_interest_to_neighbor(nbr);
866 }
867 else
868 {
869 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
870 update_adjacent_status_to_adl(nbr,NBR_DOWN);
871 if(!nlsr->is_build_adj_lsa_sheduled)
872 {
873 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
874 nlsr->is_build_adj_lsa_sheduled=1;
875 }
876 }
877
878 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500879
akmhoque53f64222012-09-05 13:57:51 -0500880
881}
882
akmhoque29c1db52012-09-07 14:47:43 -0500883void
884process_incoming_timed_out_interest_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
885{
886 printf("process_incoming_timed_out_interest_lsdb called \n");
887}
888
889void
890process_incoming_timed_out_interest_lsa(struct ccn_closure* selfp, struct ccn_upcall_info* info)
891{
892 printf("process_incoming_timed_out_interest_lsa called \n");
893
894}
895
akmhoque03004e62012-09-06 01:12:28 -0500896int
akmhoque53f64222012-09-05 13:57:51 -0500897send_info_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
898{
899 printf("send_info_interest called \n");
900 printf("\n");
akmhoqued79438d2012-08-27 13:31:42 -0500901
akmhoque53f64222012-09-05 13:57:51 -0500902 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500903 struct ndn_neighbor *nbr;
904
905 struct hashtb_enumerator ee;
906 struct hashtb_enumerator *e = &ee;
907
908 hashtb_start(nlsr->adl, e);
909 adl_element=hashtb_n(nlsr->adl);
910
911 for(i=0;i<adl_element;i++)
912 {
913 nbr=e->data;
914 send_info_interest_to_neighbor(nbr->neighbor);
915 hashtb_next(e);
916 }
akmhoque53f64222012-09-05 13:57:51 -0500917 hashtb_end(e);
918
akmhoque53f64222012-09-05 13:57:51 -0500919 return 0;
920}
921
922void
923send_info_interest_to_neighbor(struct name_prefix *nbr)
924{
925 printf("send_info_interest_to_neighbor called \n");
akmhoqued79438d2012-08-27 13:31:42 -0500926
927 int res;
akmhoque53f64222012-09-05 13:57:51 -0500928 char info_str[5];
929 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500930
akmhoqued79438d2012-08-27 13:31:42 -0500931 memset(&nlsr_str,0,5);
932 sprintf(nlsr_str,"nlsr");
933 memset(&info_str,0,5);
934 sprintf(info_str,"info");
935
akmhoque53f64222012-09-05 13:57:51 -0500936
937 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500938 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500939
akmhoque03004e62012-09-06 01:12:28 -0500940 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+strlen(nlsr->router_name)+1);
941 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 -0500942 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
943 memcpy(int_name+strlen(int_name),"/",1);
944 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
945 memcpy(int_name+strlen(int_name),"/",1);
946 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500947 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500948
949
950 res=ccn_name_from_uri(name,int_name);
951 if ( res >=0 )
952 {
akmhoque53f64222012-09-05 13:57:51 -0500953 /* adding InterestLifeTime and InterestScope filter */
954
955 struct ccn_charbuf *templ;
956 templ = ccn_charbuf_create();
957
958 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
959 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
960 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500961 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
962 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
963 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
964 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500965
966 appendLifetime(templ,nlsr->interest_resend_time);
967 ccn_charbuf_append_closer(templ); /* </Interest> */
968 /* Adding InterestLifeTime and InterestScope filter done */
969
970 printf("Sending info interest on name prefix : %s \n",int_name);
971
972 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
973
974 if ( res >= 0 )
975 printf("Info interest sending Successfull .... \n");
976 ccn_charbuf_destroy(&templ);
977 }
978 ccn_charbuf_destroy(&name);
979 free(int_name);
980
981}
982
983
984int
985send_lsdb_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
986{
987 printf("send_lsdb_interest called \n");
988
Obaid Amin485277a2012-09-07 01:08:28 -0400989 if(flags == CCN_SCHEDULE_CANCEL)
akmhoque29c1db52012-09-07 14:47:43 -0500990 {
991 return -1;
992 }
Obaid Amin485277a2012-09-07 01:08:28 -0400993
akmhoque53f64222012-09-05 13:57:51 -0500994 int i, adl_element;
995 struct ndn_neighbor *nbr;
996
997 struct hashtb_enumerator ee;
998 struct hashtb_enumerator *e = &ee;
999
1000 hashtb_start(nlsr->adl, e);
1001 adl_element=hashtb_n(nlsr->adl);
1002
1003 for(i=0;i<adl_element;i++)
1004 {
1005 nbr=e->data;
1006
1007 if(nbr->status == NBR_ACTIVE)
1008 {
1009 if(nbr->is_lsdb_send_interest_scheduled == 0)
1010 {
1011 long int time_diff=get_nbr_time_diff_lsdb_req(nbr->neighbor->name);
1012 printf("Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);
1013
1014 if(time_diff>=get_lsdb_synch_interval(nbr->neighbor->name))
1015 {
1016 nbr->is_lsdb_send_interest_scheduled=1;
1017 send_lsdb_interest_to_nbr(nbr->neighbor);
1018 }
1019 }
1020 }
1021 hashtb_next(e);
1022 }
1023
1024 hashtb_end(e);
1025 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 30000000, &send_lsdb_interest, NULL, 0);
1026
akmhoque53f64222012-09-05 13:57:51 -05001027 return 0;
1028}
1029
1030void
1031send_lsdb_interest_to_nbr(struct name_prefix *nbr)
1032{
1033 printf("send_lsdb_interest_to_nbr called \n");
akmhoque03004e62012-09-06 01:12:28 -05001034
akmhoque53f64222012-09-05 13:57:51 -05001035 char *last_lsdb_version=get_nbr_lsdb_version(nbr->name);
1036
1037 if(last_lsdb_version !=NULL)
1038 {
1039 printf("Last LSDB Version: %s \n",last_lsdb_version);
1040
1041
1042 struct ccn_charbuf *name;
1043 int res;
akmhoque53f64222012-09-05 13:57:51 -05001044 char lsdb_str[5];
1045 char nlsr_str[5];
akmhoque03004e62012-09-06 01:12:28 -05001046
akmhoque53f64222012-09-05 13:57:51 -05001047 memset(&nlsr_str,0,5);
1048 sprintf(nlsr_str,"nlsr");
1049 memset(&lsdb_str,0,5);
1050 sprintf(lsdb_str,"lsdb");
1051 //make and send interest with exclusion filter as last_lsdb_version
1052 printf("Sending interest for name prefix:%s/%s/%s\n",nbr->name,nlsr_str,lsdb_str);
1053 name=ccn_charbuf_create();
1054 res=ccn_name_from_uri(name,nbr->name);
1055
1056 if( res >= 0)
1057 {
1058 ccn_name_append_str(name,nlsr_str);
1059 ccn_name_append_str(name,lsdb_str);
akmhoque53f64222012-09-05 13:57:51 -05001060 /* adding Exclusion filter */
1061
1062 struct ccn_charbuf *templ;
1063 templ = ccn_charbuf_create();
akmhoque03004e62012-09-06 01:12:28 -05001064
akmhoque53f64222012-09-05 13:57:51 -05001065 struct ccn_charbuf *c;
1066 c = ccn_charbuf_create();
akmhoque03004e62012-09-06 01:12:28 -05001067
akmhoque53f64222012-09-05 13:57:51 -05001068
1069 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1070 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1071 ccn_charbuf_append_closer(templ); /* </Name> */
1072 ccn_charbuf_append_tt(templ, CCN_DTAG_Exclude, CCN_DTAG);
1073 ccnb_tagged_putf(templ, CCN_DTAG_Any, "");
1074 ccn_charbuf_reset(c);
1075 ccn_charbuf_putf(c, "%s", last_lsdb_version);
akmhoque03004e62012-09-06 01:12:28 -05001076
akmhoque53f64222012-09-05 13:57:51 -05001077 ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length);
1078 ccn_charbuf_append_closer(templ); /* </Exclude> */
1079 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1080 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1081 ccn_charbuf_append(templ, "2", 1);
1082 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque03004e62012-09-06 01:12:28 -05001083
akmhoque53f64222012-09-05 13:57:51 -05001084 appendLifetime(templ,nlsr->interest_resend_time);
akmhoque03004e62012-09-06 01:12:28 -05001085
akmhoque53f64222012-09-05 13:57:51 -05001086 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoque03004e62012-09-06 01:12:28 -05001087
1088
akmhoque53f64222012-09-05 13:57:51 -05001089 /* Adding Exclusion filter done */
akmhoque03004e62012-09-06 01:12:28 -05001090
akmhoque53f64222012-09-05 13:57:51 -05001091 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
1092
1093 if ( res >= 0 )
1094 {
1095 printf("Interest sending Successfull .... \n");
1096 update_adjacent_last_lsdb_requested_to_adl(nbr->name,get_current_time_sec());
1097
1098 }
1099 ccn_charbuf_destroy(&c);
1100 ccn_charbuf_destroy(&templ);
1101 }
1102 ccn_charbuf_destroy(&name);
1103 }
1104 set_is_lsdb_send_interest_scheduled_to_zero(nbr->name);
akmhoque53f64222012-09-05 13:57:51 -05001105}
1106
1107void
1108send_interest_for_name_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type, char *ls_id)
1109{
1110 printf("send_interest_for_name_lsa called\n");
1111
1112 int res;
akmhoque53f64222012-09-05 13:57:51 -05001113 char lsa_str[5];
1114 char nlsr_str[5];
1115
akmhoque53f64222012-09-05 13:57:51 -05001116 memset(&nlsr_str,0,5);
1117 sprintf(nlsr_str,"nlsr");
1118 memset(&lsa_str,0,5);
1119 sprintf(lsa_str,"lsa");
1120
akmhoque03004e62012-09-06 01:12:28 -05001121 char *int_name=(char *)malloc(nbr->length + strlen(ls_type)+strlen(orig_router)+strlen(nlsr_str)+strlen(lsa_str)+3);
1122 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 -05001123
1124 memcpy(int_name+strlen(int_name),nbr->name,nbr->length);
1125 memcpy(int_name+strlen(int_name),"/",1);
1126 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
1127 memcpy(int_name+strlen(int_name),"/",1);
1128 memcpy(int_name+strlen(int_name),lsa_str,strlen(lsa_str));
akmhoque03004e62012-09-06 01:12:28 -05001129 memcpy(int_name+strlen(int_name),"/",1);
1130 memcpy(int_name+strlen(int_name),ls_type,strlen(ls_type));
1131 memcpy(int_name+strlen(int_name),orig_router,strlen(orig_router));
akmhoque53f64222012-09-05 13:57:51 -05001132
akmhoque03004e62012-09-06 01:12:28 -05001133
akmhoque53f64222012-09-05 13:57:51 -05001134 struct ccn_charbuf *name;
1135 name=ccn_charbuf_create();
1136
1137
1138 res=ccn_name_from_uri(name,int_name);
1139 ccn_name_append_str(name,ls_type);
1140 ccn_name_append_str(name,ls_id);
akmhoque53f64222012-09-05 13:57:51 -05001141
1142
1143 /* adding InterestLifeTime and InterestScope filter */
1144
akmhoqued79438d2012-08-27 13:31:42 -05001145 struct ccn_charbuf *templ;
1146 templ = ccn_charbuf_create();
1147
akmhoqued79438d2012-08-27 13:31:42 -05001148 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1149 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1150 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque53f64222012-09-05 13:57:51 -05001151 //ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
1152 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1153 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1154 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
1155 ccn_charbuf_append_closer(templ); /* </Scope> */
1156
1157 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqued79438d2012-08-27 13:31:42 -05001158 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoque53f64222012-09-05 13:57:51 -05001159 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque03004e62012-09-06 01:12:28 -05001160
akmhoque53f64222012-09-05 13:57:51 -05001161 printf("Sending NAME LSA interest on name prefix : %s/%s/%s\n",int_name,ls_type,ls_id);
akmhoqued79438d2012-08-27 13:31:42 -05001162
akmhoqued79438d2012-08-27 13:31:42 -05001163 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
akmhoque53f64222012-09-05 13:57:51 -05001164
akmhoqued79438d2012-08-27 13:31:42 -05001165 if ( res >= 0 )
akmhoque53f64222012-09-05 13:57:51 -05001166 printf("NAME LSA interest sending Successfull .... \n");
1167
akmhoqued79438d2012-08-27 13:31:42 -05001168 ccn_charbuf_destroy(&templ);
1169 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -05001170 free(int_name);
akmhoque03004e62012-09-06 01:12:28 -05001171
1172
akmhoqued79438d2012-08-27 13:31:42 -05001173}
akmhoque53f64222012-09-05 13:57:51 -05001174
1175void
1176send_interest_for_adj_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type)
1177{
1178 printf("send_interest_for_adj_lsa called\n");
1179
1180 int res;
akmhoque53f64222012-09-05 13:57:51 -05001181 char lsa_str[5];
1182 char nlsr_str[5];
1183
akmhoque53f64222012-09-05 13:57:51 -05001184 memset(&nlsr_str,0,5);
1185 sprintf(nlsr_str,"nlsr");
1186 memset(&lsa_str,0,5);
1187 sprintf(lsa_str,"lsa");
1188
akmhoque29c1db52012-09-07 14:47:43 -05001189 char *int_name=(char *)malloc(nbr->length + strlen(ls_type)+strlen(orig_router)+strlen(nlsr_str)+strlen(lsa_str)+3+strlen(ls_type)+1);
1190 memset(int_name,0,nbr->length +strlen(ls_type)+ strlen(orig_router)+strlen(nlsr_str)+strlen(lsa_str)+3+strlen(ls_type)+1);
akmhoque53f64222012-09-05 13:57:51 -05001191
1192 memcpy(int_name+strlen(int_name),nbr->name,nbr->length);
1193 memcpy(int_name+strlen(int_name),"/",1);
1194 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
1195 memcpy(int_name+strlen(int_name),"/",1);
1196 memcpy(int_name+strlen(int_name),lsa_str,strlen(lsa_str));
akmhoque03004e62012-09-06 01:12:28 -05001197 memcpy(int_name+strlen(int_name),"/",1);
1198 memcpy(int_name+strlen(int_name),ls_type,strlen(ls_type));
akmhoque53f64222012-09-05 13:57:51 -05001199 memcpy(int_name+strlen(int_name),orig_router,strlen(orig_router));
akmhoque29c1db52012-09-07 14:47:43 -05001200 memcpy(int_name+strlen(int_name),"/",1);
1201 memcpy(int_name+strlen(int_name),ls_type,strlen(ls_type));
akmhoque03004e62012-09-06 01:12:28 -05001202
akmhoque53f64222012-09-05 13:57:51 -05001203 struct ccn_charbuf *name;
1204 name=ccn_charbuf_create();
akmhoque03004e62012-09-06 01:12:28 -05001205
akmhoque53f64222012-09-05 13:57:51 -05001206
1207 ccn_name_from_uri(name,int_name);
akmhoque03004e62012-09-06 01:12:28 -05001208
akmhoque53f64222012-09-05 13:57:51 -05001209 /* adding InterestLifeTime and InterestScope filter */
1210
1211 struct ccn_charbuf *templ;
1212 templ = ccn_charbuf_create();
1213
1214 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1215 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1216 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque53f64222012-09-05 13:57:51 -05001217 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1218 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1219 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
1220 ccn_charbuf_append_closer(templ); /* </Scope> */
1221
1222 appendLifetime(templ,nlsr->interest_resend_time);
1223 ccn_charbuf_append_closer(templ); /* </Interest> */
1224 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque03004e62012-09-06 01:12:28 -05001225
akmhoque29c1db52012-09-07 14:47:43 -05001226 printf("Sending ADJ LSA interest on name prefix : %s\n",int_name);
akmhoque53f64222012-09-05 13:57:51 -05001227
1228 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
1229
1230 if ( res >= 0 )
akmhoque29c1db52012-09-07 14:47:43 -05001231 {
akmhoque53f64222012-09-05 13:57:51 -05001232 printf("ADJ LSA interest sending Successfull .... \n");
akmhoque29c1db52012-09-07 14:47:43 -05001233 }
akmhoque53f64222012-09-05 13:57:51 -05001234
akmhoque53f64222012-09-05 13:57:51 -05001235 ccn_charbuf_destroy(&templ);
1236 ccn_charbuf_destroy(&name);
1237 free(int_name);
akmhoque53f64222012-09-05 13:57:51 -05001238}