blob: 99bc66d24fa65ace44189153b33a2262bcc9dc95 [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{
akmhoqueffacaa82012-09-13 17:48:30 -0500195
196 nlsr_lock();
197
akmhoque59980a52012-08-09 12:36:09 -0500198 switch (kind) {
199 case CCN_UPCALL_FINAL:
200 break;
201 case CCN_UPCALL_INTEREST:
akmhoque03004e62012-09-06 01:12:28 -0500202 // printing the name prefix for which it received interest
203 printf("Interest Received for name: ");
204 struct ccn_charbuf*c;
205 c=ccn_charbuf_create();
206 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
207 printf("%s\n",ccn_charbuf_as_string(c));
208 ccn_charbuf_destroy(&c);
209
akmhoque1c9b92f2012-08-13 10:57:50 -0500210 process_incoming_interest(selfp, info);
akmhoque03004e62012-09-06 01:12:28 -0500211
akmhoque59980a52012-08-09 12:36:09 -0500212 break;
akmhoque03004e62012-09-06 01:12:28 -0500213
akmhoque59980a52012-08-09 12:36:09 -0500214 default:
215 break;
216 }
akmhoque03004e62012-09-06 01:12:28 -0500217
akmhoqueffacaa82012-09-13 17:48:30 -0500218 nlsr_unlock();
219
akmhoque59980a52012-08-09 12:36:09 -0500220 return CCN_UPCALL_RESULT_OK;
221}
222
akmhoque03004e62012-09-06 01:12:28 -0500223/* Function for processing incoming interest and reply with content/NACK content */
akmhoque59980a52012-08-09 12:36:09 -0500224
akmhoqued79438d2012-08-27 13:31:42 -0500225void
akmhoque1c9b92f2012-08-13 10:57:50 -0500226process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
227{
228 printf("process_incoming_interest called \n");
akmhoque1c9b92f2012-08-13 10:57:50 -0500229 const unsigned char *comp_ptr1;
230 size_t comp_size;
231 int res,i;
232 int nlsr_position=0;
233 int name_comps=(int)info->interest_comps->n;
akmhoque53f64222012-09-05 13:57:51 -0500234
akmhoque1c9b92f2012-08-13 10:57:50 -0500235 for(i=0;i<name_comps;i++)
236 {
237 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
238 if( res == 0)
239 {
akmhoqueea3603e2012-08-13 11:24:09 -0500240 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500241 break;
242 }
243 }
244
245 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500246
akmhoque1c9b92f2012-08-13 10:57:50 -0500247 printf("Det= %s \n",comp_ptr1);
248
akmhoqued79438d2012-08-27 13:31:42 -0500249 if(!strcmp((char *)comp_ptr1,"info"))
250 {
251 process_incoming_interest_info(selfp,info);
252 }
akmhoque53f64222012-09-05 13:57:51 -0500253 if(!strcmp((char *)comp_ptr1,"lsdb"))
254 {
255 process_incoming_interest_lsdb(selfp,info);
256 }
257 if(!strcmp((char *)comp_ptr1,"lsa"))
258 {
259 process_incoming_interest_lsa(selfp,info);
260 }
akmhoque53f64222012-09-05 13:57:51 -0500261}
262
263void
264process_incoming_interest_info(struct ccn_closure *selfp, struct ccn_upcall_info *info)
265{
266 printf("process_incoming_interest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500267
akmhoque03004e62012-09-06 01:12:28 -0500268 printf("Sending Info Content back.....\n");
akmhoque53f64222012-09-05 13:57:51 -0500269 int res;
270 struct ccn_charbuf *data=ccn_charbuf_create();
271 struct ccn_charbuf *name=ccn_charbuf_create();
272 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
akmhoque03004e62012-09-06 01:12:28 -0500273
akmhoque53f64222012-09-05 13:57:51 -0500274 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]);
275 if (res >= 0)
276 {
277 sp.template_ccnb=ccn_charbuf_create();
278 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
279 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
280 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
281 ccn_charbuf_append_closer(sp.template_ccnb);
282
283
284 char *raw_data=(char *)malloc(16);
285 memset(raw_data,0,16);
286 sprintf(raw_data,"%ld", nlsr->lsdb_synch_interval);
287
288 res= ccn_sign_content(nlsr->ccn, data, name, &sp, raw_data,strlen(raw_data));
289 if(res >= 0)
290 printf("Signing Content is successful \n");
291
292 res=ccn_put(nlsr->ccn,data->buf,data->length);
293 if(res >= 0)
294 printf("Sending Info Content is successful \n");
295
296 printf("Info Content sending done....\n");
297
298
299 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix *));
akmhoque03004e62012-09-06 01:12:28 -0500300 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque53f64222012-09-05 13:57:51 -0500301 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500302
akmhoque53f64222012-09-05 13:57:51 -0500303
304 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr)>=nlsr->interest_retry )
305 {
306 send_info_interest_to_neighbor(nbr);
307 }
308
309 free(nbr);
310 free(raw_data);
311 ccn_charbuf_destroy(&sp.template_ccnb);
312 }
akmhoque03004e62012-09-06 01:12:28 -0500313
akmhoque53f64222012-09-05 13:57:51 -0500314 ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500315 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500316
akmhoquebf1aa832012-08-13 13:26:59 -0500317}
318
319
320void
321process_incoming_interest_lsdb(struct ccn_closure *selfp, struct ccn_upcall_info *info)
322{
323 printf("process_incoming_interest_lsdb called \n");
akmhoque53f64222012-09-05 13:57:51 -0500324
325 int l,res;
akmhoquefce8cfc2012-08-14 14:00:33 -0500326 const unsigned char *exclbase;
akmhoque6d49e4d2012-08-14 13:49:30 -0500327 size_t size;
akmhoquefce8cfc2012-08-14 14:00:33 -0500328 struct ccn_buf_decoder decoder;
329 struct ccn_buf_decoder *d;
akmhoque53f64222012-09-05 13:57:51 -0500330 const unsigned char *comp;
331 int dbcmp=0;
akmhoque6d49e4d2012-08-14 13:49:30 -0500332
333 l = info->pi->offset[CCN_PI_E_Exclude] - info->pi->offset[CCN_PI_B_Exclude];
334 if (l > 0)
335 {
336 comp = NULL;
337 size = 0;
338 exclbase = info->interest_ccnb + info->pi->offset[CCN_PI_B_Exclude];
339 d = ccn_buf_decoder_start(&decoder, exclbase, l);
340 if (ccn_buf_match_dtag(d, CCN_DTAG_Exclude))
341 {
342 ccn_buf_advance(d);
343 if (ccn_buf_match_dtag(d, CCN_DTAG_Any))
344 ccn_buf_advance_past_element(d);
345 if (ccn_buf_match_dtag(d, CCN_DTAG_Component))
346 {
347 ccn_buf_advance(d);
348 ccn_buf_match_blob(d, &comp, &size);
akmhoquec9286692012-08-16 09:57:58 -0500349 ccn_buf_check_close(d);
akmhoque53f64222012-09-05 13:57:51 -0500350
351
akmhoque6d49e4d2012-08-14 13:49:30 -0500352 }
353 ccn_buf_check_close(d);
354 }
akmhoque6d49e4d2012-08-14 13:49:30 -0500355 if (comp != NULL)
akmhoque53f64222012-09-05 13:57:51 -0500356 {
357 printf("LSDB Version in Exclusion Filter is %s\n",comp);
akmhoque03004e62012-09-06 01:12:28 -0500358 printf("LSDB Version of own NLSR is: %s \n",nlsr->lsdb->lsdb_version);
359 dbcmp=strcmp(nlsr->lsdb->lsdb_version,(char *)comp);
akmhoque53f64222012-09-05 13:57:51 -0500360 }
akmhoque6d49e4d2012-08-14 13:49:30 -0500361 /* Now comp points to the start of your potential number, and size is its length */
362 }
akmhoque53f64222012-09-05 13:57:51 -0500363 else
akmhoquecb017752012-08-16 11:03:45 -0500364 {
akmhoque53f64222012-09-05 13:57:51 -0500365 printf("LSDB Version in Exclusion Filter is: None Added\n");
366 dbcmp=1;
akmhoquecb017752012-08-16 11:03:45 -0500367
368 }
369
akmhoqued79438d2012-08-27 13:31:42 -0500370 struct ccn_charbuf *data=ccn_charbuf_create();
akmhoque53f64222012-09-05 13:57:51 -0500371 struct ccn_charbuf *name=ccn_charbuf_create();
372 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
373
akmhoque03004e62012-09-06 01:12:28 -0500374 ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
akmhoque887fc0c2012-08-27 15:06:06 -0500375
akmhoqued79438d2012-08-27 13:31:42 -0500376 sp.template_ccnb=ccn_charbuf_create();
377 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
378 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
akmhoque53f64222012-09-05 13:57:51 -0500379 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
380 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoqued79438d2012-08-27 13:31:42 -0500381
akmhoqued79438d2012-08-27 13:31:42 -0500382
akmhoque53f64222012-09-05 13:57:51 -0500383 if(dbcmp>0)
384 {
385 printf("Has Updated Database than Neighbor\n");
akmhoque03004e62012-09-06 01:12:28 -0500386 printf("Sending LSDB Summary of Updated LSDB Content...\n");
akmhoque53f64222012-09-05 13:57:51 -0500387
akmhoque03004e62012-09-06 01:12:28 -0500388 ccn_name_append_str(name,nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500389
akmhoque03004e62012-09-06 01:12:28 -0500390 struct ccn_charbuf *lsdb_data=ccn_charbuf_create();
391 get_lsdb_summary(lsdb_data);
akmhoque53f64222012-09-05 13:57:51 -0500392
akmhoque03004e62012-09-06 01:12:28 -0500393 char *raw_data=ccn_charbuf_as_string(lsdb_data);
akmhoque53f64222012-09-05 13:57:51 -0500394
akmhoque03004e62012-09-06 01:12:28 -0500395 //printf("Content Data to be sent: %s \n",raw_data);
akmhoque53f64222012-09-05 13:57:51 -0500396
akmhoque29c1db52012-09-07 14:47:43 -0500397 if( nlsr->is_build_adj_lsa_sheduled == 1 || strlen((char *)raw_data) == 0 )
akmhoque53f64222012-09-05 13:57:51 -0500398 {
399 res= ccn_sign_content(nlsr->ccn, data, name, &sp, "WAIT" , strlen("WAIT"));
400 }
401 else
402 {
403 res= ccn_sign_content(nlsr->ccn, data, name, &sp, raw_data , strlen(raw_data));
404 }
405 if(res >= 0)
406 printf("Signing LSDB Summary of Updated LSDB Content is successful \n");
407
408 res=ccn_put(nlsr->ccn,data->buf,data->length);
409
410 if(res >= 0)
411 printf("Sending LSDB Summary of Updated LSDB Content is successful \n");
akmhoque03004e62012-09-06 01:12:28 -0500412
413 ccn_charbuf_destroy(&lsdb_data);
akmhoque53f64222012-09-05 13:57:51 -0500414 }
415 else
416 {
417 printf("Does not have Updated Database than Neighbor\n");
418
419 printf("Sending NACK Content.....\n");
akmhoque53f64222012-09-05 13:57:51 -0500420 res= ccn_sign_content(nlsr->ccn, data, name, &sp, "NACK", strlen("NACK"));
421 if(res >= 0)
422 printf("Signing NACK Content is successful \n");
423
424 res=ccn_put(nlsr->ccn,data->buf,data->length);
425
426 if(res >= 0)
427 printf("Sending NACK Content is successful \n");
428
429
430 }
akmhoqued79438d2012-08-27 13:31:42 -0500431
432 ccn_charbuf_destroy(&data);
akmhoqued79438d2012-08-27 13:31:42 -0500433 ccn_charbuf_destroy(&name);
434 ccn_charbuf_destroy(&sp.template_ccnb);
435
akmhoque03004e62012-09-06 01:12:28 -0500436
akmhoqued79438d2012-08-27 13:31:42 -0500437}
438
akmhoque03004e62012-09-06 01:12:28 -0500439
akmhoque53f64222012-09-05 13:57:51 -0500440void
441process_incoming_interest_lsa(struct ccn_closure *selfp, struct ccn_upcall_info *info)
akmhoque1c9b92f2012-08-13 10:57:50 -0500442{
akmhoque53f64222012-09-05 13:57:51 -0500443 printf("process_incoming_interest_lsa called \n");
akmhoque1c9b92f2012-08-13 10:57:50 -0500444
akmhoque53f64222012-09-05 13:57:51 -0500445 int res;
akmhoque1c9b92f2012-08-13 10:57:50 -0500446
akmhoque53f64222012-09-05 13:57:51 -0500447 struct name_prefix *lsaId=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500448 get_lsa_identifier(lsaId,selfp,info,0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500449
akmhoque03004e62012-09-06 01:12:28 -0500450 printf("LSA Identifier: %s Length: %d\n",lsaId->name,lsaId->length);
451 int ls_type=get_ls_type(selfp, info);
akmhoque1c9b92f2012-08-13 10:57:50 -0500452
akmhoque53f64222012-09-05 13:57:51 -0500453 struct ccn_charbuf *lsa_data=ccn_charbuf_create();
454
akmhoque03004e62012-09-06 01:12:28 -0500455 if ( ls_type == LS_TYPE_NAME )
akmhoque1c9b92f2012-08-13 10:57:50 -0500456 {
akmhoque53f64222012-09-05 13:57:51 -0500457 printf("Interest Received for NAME LSA\n");
458 get_name_lsa_data(lsa_data,lsaId);
459 }
akmhoque03004e62012-09-06 01:12:28 -0500460 else if ( ls_type == LS_TYPE_ADJ )
akmhoque53f64222012-09-05 13:57:51 -0500461 {
462 printf("Interest Received for ADJ LSA\n");
463 get_adj_lsa_data(lsa_data,lsaId);
akmhoque1c9b92f2012-08-13 10:57:50 -0500464 }
465
akmhoque53f64222012-09-05 13:57:51 -0500466 char *rdata=ccn_charbuf_as_string(lsa_data);
467 char *raw_data=(char *)malloc(strlen(rdata)+1);
468 memset(raw_data,0,strlen(rdata)+1);
469 memcpy(raw_data,(char *)rdata,strlen(rdata)+1);
akmhoque03004e62012-09-06 01:12:28 -0500470 //printf("Content Data to be sent: %s\n",raw_data);
akmhoque1c9b92f2012-08-13 10:57:50 -0500471
akmhoque53f64222012-09-05 13:57:51 -0500472 struct ccn_charbuf *data=ccn_charbuf_create();
473 struct ccn_charbuf *name=ccn_charbuf_create();
474 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
akmhoque03004e62012-09-06 01:12:28 -0500475
akmhoque53f64222012-09-05 13:57:51 -0500476 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 -0500477
akmhoque53f64222012-09-05 13:57:51 -0500478 sp.template_ccnb=ccn_charbuf_create();
479 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
480 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
481 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
482 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoque1c9b92f2012-08-13 10:57:50 -0500483
akmhoque53f64222012-09-05 13:57:51 -0500484 res= ccn_sign_content(nlsr->ccn, data, name, &sp, raw_data , strlen(raw_data));
485 if(res >= 0)
486 printf("Signing LSA Content is successful \n");
487
488 res=ccn_put(nlsr->ccn,data->buf,data->length);
489 if(res >= 0)
490 printf("Sending LSA Content is successful \n");
491
akmhoque03004e62012-09-06 01:12:28 -0500492
493
akmhoque53f64222012-09-05 13:57:51 -0500494 ccn_charbuf_destroy(&data);
495 ccn_charbuf_destroy(&name);
496 ccn_charbuf_destroy(&sp.template_ccnb);
497 ccn_charbuf_destroy(&lsa_data);
498
499 free(raw_data);
500 free(lsaId);
akmhoque1c9b92f2012-08-13 10:57:50 -0500501}
502
akmhoque03004e62012-09-06 01:12:28 -0500503/* Call back function registered in ccnd to get all content coming to NLSR application */
504
akmhoque53f64222012-09-05 13:57:51 -0500505enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
506 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500507{
508
akmhoqueffacaa82012-09-13 17:48:30 -0500509 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500510
akmhoque53f64222012-09-05 13:57:51 -0500511 switch(kind) {
512 case CCN_UPCALL_FINAL:
513 break;
514 case CCN_UPCALL_CONTENT:
akmhoque03004e62012-09-06 01:12:28 -0500515 printf("Content Received for Name: ");
516 struct ccn_charbuf*c;
517 c=ccn_charbuf_create();
518 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
519 printf("%s\n",ccn_charbuf_as_string(c));
520 ccn_charbuf_destroy(&c);
521
522 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500523
akmhoque53f64222012-09-05 13:57:51 -0500524 break;
525 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoque03004e62012-09-06 01:12:28 -0500526 printf("Interest Timed Out Received for Name: ");
527
528 struct ccn_charbuf*ito;
529 ito=ccn_charbuf_create();
530 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
531 printf("%s\n",ccn_charbuf_as_string(ito));
532 ccn_charbuf_destroy(&ito);
533
akmhoque53f64222012-09-05 13:57:51 -0500534 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500535
akmhoque53f64222012-09-05 13:57:51 -0500536 break;
537 default:
538 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500539 //return CCN_UPCALL_RESULT_ERR;
540 break;
akmhoque53f64222012-09-05 13:57:51 -0500541 }
akmhoqueffacaa82012-09-13 17:48:30 -0500542
543 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500544
akmhoque53f64222012-09-05 13:57:51 -0500545 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500546}
547
akmhoque03004e62012-09-06 01:12:28 -0500548
akmhoqued79438d2012-08-27 13:31:42 -0500549void
akmhoque53f64222012-09-05 13:57:51 -0500550process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500551{
akmhoque53f64222012-09-05 13:57:51 -0500552 printf("process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500553
554 const unsigned char *comp_ptr1;
555 size_t comp_size;
556 int res,i;
557 int nlsr_position=0;
558 int name_comps=(int)info->interest_comps->n;
559
560 for(i=0;i<name_comps;i++)
561 {
562 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
563 if( res == 0)
564 {
565 nlsr_position=i;
566 break;
567 }
568 }
569
570 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
571
572 printf("Det= %s \n",comp_ptr1);
573
574 if(!strcmp((char *)comp_ptr1,"info"))
575 {
576 process_incoming_content_info(selfp,info);
577 }
578 if(!strcmp((char *)comp_ptr1,"lsdb"))
579 {
580 process_incoming_content_lsdb(selfp,info);
581 }
582 if(!strcmp((char *)comp_ptr1,"lsa"))
583 {
584 process_incoming_content_lsa(selfp,info);
585 }
akmhoque03004e62012-09-06 01:12:28 -0500586
akmhoque53f64222012-09-05 13:57:51 -0500587}
588
akmhoque03004e62012-09-06 01:12:28 -0500589
akmhoque53f64222012-09-05 13:57:51 -0500590void
591process_incoming_content_info(struct ccn_closure *selfp, struct ccn_upcall_info* info)
592{
593 printf("process_incoming_content_info called \n");
akmhoque03004e62012-09-06 01:12:28 -0500594
akmhoque53f64222012-09-05 13:57:51 -0500595 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
596 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500597
akmhoque53f64222012-09-05 13:57:51 -0500598 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500599
akmhoque53f64222012-09-05 13:57:51 -0500600
601 const unsigned char *ptr;
602 size_t length;
603 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 -0500604 //printf("Content data: %s\n",ptr);
akmhoque53f64222012-09-05 13:57:51 -0500605
606 long int interval=atoi((char *)ptr);
607
608
akmhoque03004e62012-09-06 01:12:28 -0500609
akmhoque53f64222012-09-05 13:57:51 -0500610 update_adjacent_timed_out_zero_to_adl(nbr);
611 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
612 update_lsdb_synch_interval_to_adl(nbr,interval);
613 print_adjacent_from_adl();
614
akmhoque03004e62012-09-06 01:12:28 -0500615
616
akmhoque53f64222012-09-05 13:57:51 -0500617 if(!nlsr->is_build_adj_lsa_sheduled)
618 {
619 printf("Scheduling Build and Install Adj LSA...\n");
620 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 100000, &build_and_install_adj_lsa, NULL, 0);
621 nlsr->is_build_adj_lsa_sheduled=1;
622 }
623 else
624 {
625 printf("Build and Install Adj LSA already scheduled\n");
626 }
627
akmhoque03004e62012-09-06 01:12:28 -0500628
akmhoque53f64222012-09-05 13:57:51 -0500629 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500630
akmhoque53f64222012-09-05 13:57:51 -0500631
632}
633
akmhoque03004e62012-09-06 01:12:28 -0500634
akmhoque53f64222012-09-05 13:57:51 -0500635void
636process_incoming_content_lsdb(struct ccn_closure *selfp, struct ccn_upcall_info* info)
637{
638 printf("process_incoming_content_lsdb called \n");
639
akmhoque53f64222012-09-05 13:57:51 -0500640 const unsigned char *ptr;
641 size_t length;
642 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 -0500643 //printf("Content data: %s\n",ptr);
akmhoque53f64222012-09-05 13:57:51 -0500644
645 if( (strcmp("NACK",(char *)ptr) != 0 ) && (strcmp("WAIT",(char *)ptr) != 0 ) )
646 {
647 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
648 get_nbr(nbr,selfp,info);
649
650 char *nl;
651 int num_element;
652 int i;
653 char *rem;
654 const char *sep="|";
655 char *orig_router;
656 char *lst;
657 int ls_type;
658 char *lsid;
659 long int ls_id;
akmhoque03004e62012-09-06 01:12:28 -0500660 char *orig_time;
akmhoque53f64222012-09-05 13:57:51 -0500661
662 nl=strtok_r((char *)ptr,sep,&rem);
663 num_element=atoi(nl);
664
665 for(i = 0 ; i < num_element ; i++)
666 {
667 orig_router=strtok_r(NULL,sep,&rem);
668 lst=strtok_r(NULL,sep,&rem);
669 ls_type=atoi(lst);
670 printf("Orig Router: %s ls Type: %d",orig_router,ls_type);
671
672 if(ls_type == LS_TYPE_NAME)
673 {
674 lsid=strtok_r(NULL,sep,&rem);
675 ls_id=atoi(lsid);
akmhoque03004e62012-09-06 01:12:28 -0500676 orig_time=strtok_r(NULL,sep,&rem);
677 printf(" LS Id: %ld Orig Time: %s\n",ls_id ,orig_time);
678 int is_new_name_lsa=check_is_new_name_lsa(orig_router,lst,lsid,orig_time);
679 if ( is_new_name_lsa == 1 )
680 {
681 printf("New NAME LSA.....\n");
682 send_interest_for_name_lsa(nbr,orig_router,lst,lsid);
683 }
684 else
685 {
686 printf("Name LSA already exists in LSDB\n");
687 }
akmhoque53f64222012-09-05 13:57:51 -0500688 }
689 else
690 {
akmhoque03004e62012-09-06 01:12:28 -0500691 orig_time=strtok_r(NULL,sep,&rem);
692 printf(" Orig Time: %s\n",orig_time);
693 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router,lst,orig_time);
694 if ( is_new_adj_lsa == 1 )
695 {
696 printf("New ADJ LSA.....\n");
697 send_interest_for_adj_lsa(nbr,orig_router,lst);
698 }
699 else
700 {
701 printf("ADJ LSA already exists in LSDB\n");
702 }
akmhoque53f64222012-09-05 13:57:51 -0500703 }
akmhoque03004e62012-09-06 01:12:28 -0500704
akmhoque53f64222012-09-05 13:57:51 -0500705 }
akmhoque53f64222012-09-05 13:57:51 -0500706
akmhoque03004e62012-09-06 01:12:28 -0500707 char *lsdb_version=(char *)malloc(20);
708 memset(lsdb_version,0,20);
709 get_lsdb_version(lsdb_version,selfp,info);
710
akmhoque53f64222012-09-05 13:57:51 -0500711 printf("Old LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
akmhoque03004e62012-09-06 01:12:28 -0500712 update_adjacent_lsdb_version_to_adl(nbr,lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500713 printf("New LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
akmhoque53f64222012-09-05 13:57:51 -0500714
akmhoque62c0c192012-09-24 07:49:25 -0500715 update_lsdb_interest_timed_out_zero_to_adl(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500716
717 free(lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500718 free(nbr);
719 }
720 else if (strcmp("WAIT",(char *)ptr) == 0)
721 {
722 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
723 get_nbr(nbr,selfp,info);
724 long int interval=get_lsdb_synch_interval(nbr->name);
725 adjust_adjacent_last_lsdb_requested_to_adl(nbr->name,(long int)interval/2);
akmhoque03004e62012-09-06 01:12:28 -0500726
akmhoque62c0c192012-09-24 07:49:25 -0500727 update_lsdb_interest_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500728 free(nbr);
729 }
730 else
731 {
732 printf("NACK Content Received\n");
akmhoque62c0c192012-09-24 07:49:25 -0500733 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
734 get_nbr(nbr,selfp,info);
735 update_lsdb_interest_timed_out_zero_to_adl(nbr);
akmhoque3cced642012-09-24 16:20:20 -0500736 free(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500737 }
akmhoque53f64222012-09-05 13:57:51 -0500738}
739
akmhoque03004e62012-09-06 01:12:28 -0500740
akmhoque53f64222012-09-05 13:57:51 -0500741void
742process_incoming_content_lsa(struct ccn_closure *selfp, struct ccn_upcall_info* info)
743{
744 printf("process_incoming_content_lsa called \n");
745
746 char *sep="|";
747 char *rem;
748 char *orig_router;
749 char *orl;
750 int orig_router_length;
751 char *lst;
752 int ls_type;
753 char *lsid;
754 long int ls_id;
755 char *isvld;
756 int isValid;
757 char *num_link;
758 int no_link;
759 char *np;
760 char *np_length;
761 int name_length;
762 char *data;
763 char *orig_time;
764
765 const unsigned char *ptr;
766 size_t length;
767 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 -0500768 //printf("Content data Received: %s\n",ptr);
akmhoque53f64222012-09-05 13:57:51 -0500769
akmhoque29c1db52012-09-07 14:47:43 -0500770
771
772
773
774
akmhoque53f64222012-09-05 13:57:51 -0500775 printf("LSA Data\n");
776
777 if( strlen((char *) ptr ) > 0 )
778 {
779
780 orig_router=strtok_r((char *)ptr,sep,&rem);
781 orl=strtok_r(NULL,sep,&rem);
782 orig_router_length=atoi(orl);
783
784 printf(" Orig Router Name : %s\n",orig_router);
785 printf(" Orig Router Length: %d\n",orig_router_length);
786
787 lst=strtok_r(NULL,sep,&rem);
788 ls_type=atoi(lst);
789
790 printf(" LS Type : %d\n",ls_type);
791
akmhoque03004e62012-09-06 01:12:28 -0500792 if ( ls_type == LS_TYPE_NAME )
akmhoque53f64222012-09-05 13:57:51 -0500793 {
794 lsid=strtok_r(NULL,sep,&rem);
795 ls_id=atoi(lsid);
akmhoque03004e62012-09-06 01:12:28 -0500796 orig_time=strtok_r(NULL,sep,&rem);
akmhoque53f64222012-09-05 13:57:51 -0500797 isvld=strtok_r(NULL,sep,&rem);
798 isValid=atoi(isvld);
799 np=strtok_r(NULL,sep,&rem);
800 np_length=strtok_r(NULL,sep,&rem);
801 name_length=atoi(np_length);
akmhoque53f64222012-09-05 13:57:51 -0500802 printf(" LS ID : %ld\n",ls_id);
803 printf(" isValid : %d\n",isValid);
804 printf(" Name Prefix : %s\n",np);
akmhoque03004e62012-09-06 01:12:28 -0500805 printf(" Orig Time : %s\n",orig_time);
akmhoque53f64222012-09-05 13:57:51 -0500806 printf(" Name Prefix length: %d\n",name_length);
807
akmhoque03004e62012-09-06 01:12:28 -0500808 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
809
akmhoque53f64222012-09-05 13:57:51 -0500810 }
akmhoque03004e62012-09-06 01:12:28 -0500811 else if ( ls_type == LS_TYPE_ADJ )
akmhoque53f64222012-09-05 13:57:51 -0500812 {
813 orig_time=strtok_r(NULL,sep,&rem);
814 num_link=strtok_r(NULL,sep,&rem);
815 no_link=atoi(num_link);
816 data=rem;
817
818 printf(" No Link : %d\n",no_link);
819 printf(" Data : %s\n",data);
820
821 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
822 }
akmhoque53f64222012-09-05 13:57:51 -0500823 }
akmhoque53f64222012-09-05 13:57:51 -0500824}
825
akmhoque03004e62012-09-06 01:12:28 -0500826
akmhoque53f64222012-09-05 13:57:51 -0500827void
828process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
829{
830 printf("process_incoming_timed_out_interest called \n");
akmhoque53f64222012-09-05 13:57:51 -0500831 int res,i;
832 int nlsr_position=0;
833 int name_comps=(int)info->interest_comps->n;
834
835 for(i=0;i<name_comps;i++)
836 {
837 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
838 if( res == 0)
839 {
840 nlsr_position=i;
841 break;
842 }
843 }
844
845 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"info") == 0)
846 {
847 process_incoming_timed_out_interest_info(selfp,info);
848 }
akmhoque29c1db52012-09-07 14:47:43 -0500849 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"lsdb") == 0)
850 {
851 process_incoming_timed_out_interest_lsdb(selfp,info);
852 }
853 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"lsa") == 0)
854 {
855 process_incoming_timed_out_interest_lsa(selfp,info);
856 }
akmhoque53f64222012-09-05 13:57:51 -0500857}
858
859void
860process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct ccn_upcall_info* info)
861{
862 printf("process_incoming_timed_out_interest_info called \n");
863
864 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
865 get_nbr(nbr,selfp,info);
866
867 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500868
869
akmhoque53f64222012-09-05 13:57:51 -0500870 update_adjacent_timed_out_to_adl(nbr,1);
871 print_adjacent_from_adl();
872 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500873
akmhoque53f64222012-09-05 13:57:51 -0500874
875 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
876 {
877 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
878 send_info_interest_to_neighbor(nbr);
879 }
880 else
881 {
882 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
883 update_adjacent_status_to_adl(nbr,NBR_DOWN);
884 if(!nlsr->is_build_adj_lsa_sheduled)
885 {
886 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
887 nlsr->is_build_adj_lsa_sheduled=1;
888 }
889 }
890
891 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500892
akmhoque53f64222012-09-05 13:57:51 -0500893
894}
895
akmhoque29c1db52012-09-07 14:47:43 -0500896void
897process_incoming_timed_out_interest_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
898{
899 printf("process_incoming_timed_out_interest_lsdb called \n");
akmhoque14b3f342012-09-14 10:39:02 -0500900
901 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
902 get_nbr(nbr,selfp,info);
903
904 printf("LSDB Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
905
akmhoque62c0c192012-09-24 07:49:25 -0500906 update_lsdb_interest_timed_out_to_adl(nbr,1);
907
akmhoque14b3f342012-09-14 10:39:02 -0500908 int interst_timed_out_num=get_lsdb_interest_timed_out_number(nbr);
909
akmhoque62c0c192012-09-24 07:49:25 -0500910 printf("Interest Timed out number : %d Interest Retry: %d \n",interst_timed_out_num,nlsr->interest_retry);
911
912 if( interst_timed_out_num >= nlsr->interest_retry )
akmhoque14b3f342012-09-14 10:39:02 -0500913 {
akmhoque14b3f342012-09-14 10:39:02 -0500914 update_adjacent_status_to_adl(nbr,NBR_DOWN);
915 if(!nlsr->is_build_adj_lsa_sheduled)
916 {
917 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
918 nlsr->is_build_adj_lsa_sheduled=1;
919 }
920 }
921 free(nbr->name);
922 free(nbr);
akmhoque29c1db52012-09-07 14:47:43 -0500923}
924
925void
926process_incoming_timed_out_interest_lsa(struct ccn_closure* selfp, struct ccn_upcall_info* info)
927{
928 printf("process_incoming_timed_out_interest_lsa called \n");
929
930}
931
akmhoque03004e62012-09-06 01:12:28 -0500932int
akmhoque53f64222012-09-05 13:57:51 -0500933send_info_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
934{
akmhoqueffacaa82012-09-13 17:48:30 -0500935 if(flags == CCN_SCHEDULE_CANCEL)
936 {
937 return -1;
938 }
939
940 nlsr_lock();
941
akmhoque53f64222012-09-05 13:57:51 -0500942 printf("send_info_interest called \n");
943 printf("\n");
akmhoqued79438d2012-08-27 13:31:42 -0500944
akmhoque53f64222012-09-05 13:57:51 -0500945 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500946 struct ndn_neighbor *nbr;
947
948 struct hashtb_enumerator ee;
949 struct hashtb_enumerator *e = &ee;
950
951 hashtb_start(nlsr->adl, e);
952 adl_element=hashtb_n(nlsr->adl);
953
954 for(i=0;i<adl_element;i++)
955 {
956 nbr=e->data;
957 send_info_interest_to_neighbor(nbr->neighbor);
958 hashtb_next(e);
959 }
akmhoque53f64222012-09-05 13:57:51 -0500960 hashtb_end(e);
961
akmhoqueffacaa82012-09-13 17:48:30 -0500962 nlsr_unlock();
963
akmhoque53f64222012-09-05 13:57:51 -0500964 return 0;
965}
966
967void
968send_info_interest_to_neighbor(struct name_prefix *nbr)
969{
970 printf("send_info_interest_to_neighbor called \n");
akmhoqued79438d2012-08-27 13:31:42 -0500971
972 int res;
akmhoque53f64222012-09-05 13:57:51 -0500973 char info_str[5];
974 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500975
akmhoqued79438d2012-08-27 13:31:42 -0500976 memset(&nlsr_str,0,5);
977 sprintf(nlsr_str,"nlsr");
978 memset(&info_str,0,5);
979 sprintf(info_str,"info");
980
akmhoque53f64222012-09-05 13:57:51 -0500981
982 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500983 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500984
akmhoque03004e62012-09-06 01:12:28 -0500985 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+strlen(nlsr->router_name)+1);
986 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 -0500987 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
988 memcpy(int_name+strlen(int_name),"/",1);
989 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
990 memcpy(int_name+strlen(int_name),"/",1);
991 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500992 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500993
994
995 res=ccn_name_from_uri(name,int_name);
996 if ( res >=0 )
997 {
akmhoque53f64222012-09-05 13:57:51 -0500998 /* adding InterestLifeTime and InterestScope filter */
999
1000 struct ccn_charbuf *templ;
1001 templ = ccn_charbuf_create();
1002
1003 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1004 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1005 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -05001006 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1007 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1008 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
1009 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -05001010
1011 appendLifetime(templ,nlsr->interest_resend_time);
1012 ccn_charbuf_append_closer(templ); /* </Interest> */
1013 /* Adding InterestLifeTime and InterestScope filter done */
1014
1015 printf("Sending info interest on name prefix : %s \n",int_name);
1016
1017 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
1018
1019 if ( res >= 0 )
1020 printf("Info interest sending Successfull .... \n");
1021 ccn_charbuf_destroy(&templ);
1022 }
1023 ccn_charbuf_destroy(&name);
1024 free(int_name);
1025
1026}
1027
1028
1029int
1030send_lsdb_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1031{
1032 printf("send_lsdb_interest called \n");
1033
Obaid Amin485277a2012-09-07 01:08:28 -04001034 if(flags == CCN_SCHEDULE_CANCEL)
akmhoque29c1db52012-09-07 14:47:43 -05001035 {
1036 return -1;
1037 }
Obaid Amin485277a2012-09-07 01:08:28 -04001038
akmhoqueffacaa82012-09-13 17:48:30 -05001039 nlsr_lock();
1040
akmhoque53f64222012-09-05 13:57:51 -05001041 int i, adl_element;
1042 struct ndn_neighbor *nbr;
1043
1044 struct hashtb_enumerator ee;
1045 struct hashtb_enumerator *e = &ee;
1046
1047 hashtb_start(nlsr->adl, e);
1048 adl_element=hashtb_n(nlsr->adl);
1049
1050 for(i=0;i<adl_element;i++)
1051 {
1052 nbr=e->data;
1053
1054 if(nbr->status == NBR_ACTIVE)
1055 {
1056 if(nbr->is_lsdb_send_interest_scheduled == 0)
1057 {
1058 long int time_diff=get_nbr_time_diff_lsdb_req(nbr->neighbor->name);
1059 printf("Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);
1060
akmhoque14b3f342012-09-14 10:39:02 -05001061 if( time_diff >= ( get_lsdb_synch_interval(nbr->neighbor->name) + get_nbr_random_time_component(nbr->neighbor->name) ) )
akmhoque53f64222012-09-05 13:57:51 -05001062 {
1063 nbr->is_lsdb_send_interest_scheduled=1;
1064 send_lsdb_interest_to_nbr(nbr->neighbor);
1065 }
1066 }
1067 }
1068 hashtb_next(e);
1069 }
1070
1071 hashtb_end(e);
1072 nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 30000000, &send_lsdb_interest, NULL, 0);
1073
akmhoqueffacaa82012-09-13 17:48:30 -05001074 nlsr_unlock();
1075
akmhoque53f64222012-09-05 13:57:51 -05001076 return 0;
1077}
1078
1079void
1080send_lsdb_interest_to_nbr(struct name_prefix *nbr)
1081{
1082 printf("send_lsdb_interest_to_nbr called \n");
akmhoque03004e62012-09-06 01:12:28 -05001083
akmhoque53f64222012-09-05 13:57:51 -05001084 char *last_lsdb_version=get_nbr_lsdb_version(nbr->name);
1085
1086 if(last_lsdb_version !=NULL)
1087 {
1088 printf("Last LSDB Version: %s \n",last_lsdb_version);
1089
1090
1091 struct ccn_charbuf *name;
1092 int res;
akmhoque53f64222012-09-05 13:57:51 -05001093 char lsdb_str[5];
1094 char nlsr_str[5];
akmhoque03004e62012-09-06 01:12:28 -05001095
akmhoque53f64222012-09-05 13:57:51 -05001096 memset(&nlsr_str,0,5);
1097 sprintf(nlsr_str,"nlsr");
1098 memset(&lsdb_str,0,5);
1099 sprintf(lsdb_str,"lsdb");
1100 //make and send interest with exclusion filter as last_lsdb_version
1101 printf("Sending interest for name prefix:%s/%s/%s\n",nbr->name,nlsr_str,lsdb_str);
1102 name=ccn_charbuf_create();
1103 res=ccn_name_from_uri(name,nbr->name);
1104
1105 if( res >= 0)
1106 {
1107 ccn_name_append_str(name,nlsr_str);
1108 ccn_name_append_str(name,lsdb_str);
akmhoque53f64222012-09-05 13:57:51 -05001109 /* adding Exclusion filter */
1110
1111 struct ccn_charbuf *templ;
1112 templ = ccn_charbuf_create();
akmhoque03004e62012-09-06 01:12:28 -05001113
akmhoque53f64222012-09-05 13:57:51 -05001114 struct ccn_charbuf *c;
1115 c = ccn_charbuf_create();
akmhoque03004e62012-09-06 01:12:28 -05001116
akmhoque53f64222012-09-05 13:57:51 -05001117
1118 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1119 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1120 ccn_charbuf_append_closer(templ); /* </Name> */
1121 ccn_charbuf_append_tt(templ, CCN_DTAG_Exclude, CCN_DTAG);
1122 ccnb_tagged_putf(templ, CCN_DTAG_Any, "");
1123 ccn_charbuf_reset(c);
1124 ccn_charbuf_putf(c, "%s", last_lsdb_version);
akmhoque03004e62012-09-06 01:12:28 -05001125
akmhoque53f64222012-09-05 13:57:51 -05001126 ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length);
1127 ccn_charbuf_append_closer(templ); /* </Exclude> */
1128 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1129 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1130 ccn_charbuf_append(templ, "2", 1);
1131 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque03004e62012-09-06 01:12:28 -05001132
akmhoque53f64222012-09-05 13:57:51 -05001133 appendLifetime(templ,nlsr->interest_resend_time);
akmhoque03004e62012-09-06 01:12:28 -05001134
akmhoque53f64222012-09-05 13:57:51 -05001135 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoque03004e62012-09-06 01:12:28 -05001136
1137
akmhoque53f64222012-09-05 13:57:51 -05001138 /* Adding Exclusion filter done */
akmhoque03004e62012-09-06 01:12:28 -05001139
akmhoque53f64222012-09-05 13:57:51 -05001140 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
1141
1142 if ( res >= 0 )
1143 {
1144 printf("Interest sending Successfull .... \n");
1145 update_adjacent_last_lsdb_requested_to_adl(nbr->name,get_current_time_sec());
1146
1147 }
1148 ccn_charbuf_destroy(&c);
1149 ccn_charbuf_destroy(&templ);
1150 }
1151 ccn_charbuf_destroy(&name);
1152 }
1153 set_is_lsdb_send_interest_scheduled_to_zero(nbr->name);
akmhoque53f64222012-09-05 13:57:51 -05001154}
1155
1156void
1157send_interest_for_name_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type, char *ls_id)
1158{
1159 printf("send_interest_for_name_lsa called\n");
1160
1161 int res;
akmhoque53f64222012-09-05 13:57:51 -05001162 char lsa_str[5];
1163 char nlsr_str[5];
1164
akmhoque53f64222012-09-05 13:57:51 -05001165 memset(&nlsr_str,0,5);
1166 sprintf(nlsr_str,"nlsr");
1167 memset(&lsa_str,0,5);
1168 sprintf(lsa_str,"lsa");
1169
akmhoque03004e62012-09-06 01:12:28 -05001170 char *int_name=(char *)malloc(nbr->length + strlen(ls_type)+strlen(orig_router)+strlen(nlsr_str)+strlen(lsa_str)+3);
1171 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 -05001172
1173 memcpy(int_name+strlen(int_name),nbr->name,nbr->length);
1174 memcpy(int_name+strlen(int_name),"/",1);
1175 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
1176 memcpy(int_name+strlen(int_name),"/",1);
1177 memcpy(int_name+strlen(int_name),lsa_str,strlen(lsa_str));
akmhoque03004e62012-09-06 01:12:28 -05001178 memcpy(int_name+strlen(int_name),"/",1);
1179 memcpy(int_name+strlen(int_name),ls_type,strlen(ls_type));
1180 memcpy(int_name+strlen(int_name),orig_router,strlen(orig_router));
akmhoque53f64222012-09-05 13:57:51 -05001181
akmhoque03004e62012-09-06 01:12:28 -05001182
akmhoque53f64222012-09-05 13:57:51 -05001183 struct ccn_charbuf *name;
1184 name=ccn_charbuf_create();
1185
1186
1187 res=ccn_name_from_uri(name,int_name);
1188 ccn_name_append_str(name,ls_type);
1189 ccn_name_append_str(name,ls_id);
akmhoque53f64222012-09-05 13:57:51 -05001190
1191
1192 /* adding InterestLifeTime and InterestScope filter */
1193
akmhoqued79438d2012-08-27 13:31:42 -05001194 struct ccn_charbuf *templ;
1195 templ = ccn_charbuf_create();
1196
akmhoqued79438d2012-08-27 13:31:42 -05001197 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1198 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1199 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque53f64222012-09-05 13:57:51 -05001200 //ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
1201 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1202 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1203 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
1204 ccn_charbuf_append_closer(templ); /* </Scope> */
1205
1206 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqued79438d2012-08-27 13:31:42 -05001207 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoque53f64222012-09-05 13:57:51 -05001208 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque03004e62012-09-06 01:12:28 -05001209
akmhoque53f64222012-09-05 13:57:51 -05001210 printf("Sending NAME LSA interest on name prefix : %s/%s/%s\n",int_name,ls_type,ls_id);
akmhoqued79438d2012-08-27 13:31:42 -05001211
akmhoqued79438d2012-08-27 13:31:42 -05001212 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
akmhoque53f64222012-09-05 13:57:51 -05001213
akmhoqued79438d2012-08-27 13:31:42 -05001214 if ( res >= 0 )
akmhoque53f64222012-09-05 13:57:51 -05001215 printf("NAME LSA interest sending Successfull .... \n");
1216
akmhoqued79438d2012-08-27 13:31:42 -05001217 ccn_charbuf_destroy(&templ);
1218 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -05001219 free(int_name);
akmhoque03004e62012-09-06 01:12:28 -05001220
1221
akmhoqued79438d2012-08-27 13:31:42 -05001222}
akmhoque53f64222012-09-05 13:57:51 -05001223
1224void
1225send_interest_for_adj_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type)
1226{
1227 printf("send_interest_for_adj_lsa called\n");
1228
1229 int res;
akmhoque53f64222012-09-05 13:57:51 -05001230 char lsa_str[5];
1231 char nlsr_str[5];
1232
akmhoque53f64222012-09-05 13:57:51 -05001233 memset(&nlsr_str,0,5);
1234 sprintf(nlsr_str,"nlsr");
1235 memset(&lsa_str,0,5);
1236 sprintf(lsa_str,"lsa");
1237
akmhoque29c1db52012-09-07 14:47:43 -05001238 char *int_name=(char *)malloc(nbr->length + strlen(ls_type)+strlen(orig_router)+strlen(nlsr_str)+strlen(lsa_str)+3+strlen(ls_type)+1);
1239 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 -05001240
1241 memcpy(int_name+strlen(int_name),nbr->name,nbr->length);
1242 memcpy(int_name+strlen(int_name),"/",1);
1243 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
1244 memcpy(int_name+strlen(int_name),"/",1);
1245 memcpy(int_name+strlen(int_name),lsa_str,strlen(lsa_str));
akmhoque03004e62012-09-06 01:12:28 -05001246 memcpy(int_name+strlen(int_name),"/",1);
1247 memcpy(int_name+strlen(int_name),ls_type,strlen(ls_type));
akmhoque53f64222012-09-05 13:57:51 -05001248 memcpy(int_name+strlen(int_name),orig_router,strlen(orig_router));
akmhoque29c1db52012-09-07 14:47:43 -05001249 memcpy(int_name+strlen(int_name),"/",1);
1250 memcpy(int_name+strlen(int_name),ls_type,strlen(ls_type));
akmhoque03004e62012-09-06 01:12:28 -05001251
akmhoque53f64222012-09-05 13:57:51 -05001252 struct ccn_charbuf *name;
1253 name=ccn_charbuf_create();
akmhoque03004e62012-09-06 01:12:28 -05001254
akmhoque53f64222012-09-05 13:57:51 -05001255
1256 ccn_name_from_uri(name,int_name);
akmhoque03004e62012-09-06 01:12:28 -05001257
akmhoque53f64222012-09-05 13:57:51 -05001258 /* adding InterestLifeTime and InterestScope filter */
1259
1260 struct ccn_charbuf *templ;
1261 templ = ccn_charbuf_create();
1262
1263 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
1264 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
1265 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque53f64222012-09-05 13:57:51 -05001266 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
1267 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
1268 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
1269 ccn_charbuf_append_closer(templ); /* </Scope> */
1270
1271 appendLifetime(templ,nlsr->interest_resend_time);
1272 ccn_charbuf_append_closer(templ); /* </Interest> */
1273 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque03004e62012-09-06 01:12:28 -05001274
akmhoque29c1db52012-09-07 14:47:43 -05001275 printf("Sending ADJ LSA interest on name prefix : %s\n",int_name);
akmhoque53f64222012-09-05 13:57:51 -05001276
1277 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
1278
1279 if ( res >= 0 )
akmhoque29c1db52012-09-07 14:47:43 -05001280 {
akmhoque53f64222012-09-05 13:57:51 -05001281 printf("ADJ LSA interest sending Successfull .... \n");
akmhoque29c1db52012-09-07 14:47:43 -05001282 }
akmhoque53f64222012-09-05 13:57:51 -05001283
akmhoque53f64222012-09-05 13:57:51 -05001284 ccn_charbuf_destroy(&templ);
1285 ccn_charbuf_destroy(&name);
1286 free(int_name);
akmhoque53f64222012-09-05 13:57:51 -05001287}