blob: af45b912542918466c9ac96e929a27f9a91d477d [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{
akmhoque7b791452012-10-30 11:24:56 -050048 if ( nlsr->debugging )
49 printf("get_nbr called\n");
50 if ( nlsr->detailed_logging )
51 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_nbr called\n");
akmhoque53f64222012-09-05 13:57:51 -050052
akmhoque53f64222012-09-05 13:57:51 -050053 int res,i;
54 int nlsr_position=0;
55 int name_comps=(int)info->interest_comps->n;
56 int len=0;
57
58 for(i=0;i<name_comps;i++)
59 {
60 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
61 if( res == 0)
62 {
63 nlsr_position=i;
64 break;
65 }
66 }
67
68
69 const unsigned char *comp_ptr1;
70 size_t comp_size;
71 for(i=0;i<nlsr_position;i++)
72 {
73 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&comp_ptr1, &comp_size);
74 len+=1;
75 len+=(int)comp_size;
76 }
77 len++;
78
79 char *neighbor=(char *)malloc(len);
80 memset(neighbor,0,len);
81
82 for(i=0; i<nlsr_position;i++)
83 {
84 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&comp_ptr1, &comp_size);
85 memcpy(neighbor+strlen(neighbor),"/",1);
86 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
87
88 }
akmhoque53f64222012-09-05 13:57:51 -050089
90 nbr->name=(char *)malloc(strlen(neighbor)+1);
91 memcpy(nbr->name,neighbor,strlen(neighbor)+1);
92 nbr->length=strlen(neighbor)+1;
93
akmhoque7b791452012-10-30 11:24:56 -050094 if ( nlsr->debugging )
95 printf("Neighbor: %s Length: %d\n",nbr->name,nbr->length);
96 if ( nlsr->detailed_logging )
97 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Length: %d\n",nbr->name,nbr->length);
98
akmhoque53f64222012-09-05 13:57:51 -050099
100
101}
102
103void
akmhoque03004e62012-09-06 01:12:28 -0500104get_lsa_identifier(struct name_prefix *lsaId,struct ccn_closure *selfp, struct ccn_upcall_info *info, int offset)
akmhoque53f64222012-09-05 13:57:51 -0500105{
106
akmhoque7b791452012-10-30 11:24:56 -0500107 //printf("get_lsa_identifier called\n");
108
109 if ( nlsr->debugging )
110 printf("get_lsa_identifier called\n");
111 if ( nlsr->detailed_logging )
112 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_lsa_identifier called\n");
113
akmhoque53f64222012-09-05 13:57:51 -0500114 int res,i;
115 int nlsr_position=0;
116 int name_comps=(int)info->interest_comps->n;
117 int len=0;
118
119 for(i=0;i<name_comps;i++)
120 {
121 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
122 if( res == 0)
123 {
124 nlsr_position=i;
125 break;
126 }
127 }
128
129
130 const unsigned char *comp_ptr1;
131 size_t comp_size;
akmhoque03004e62012-09-06 01:12:28 -0500132 for(i=nlsr_position+3+offset;i<info->interest_comps->n-1;i++)
akmhoque53f64222012-09-05 13:57:51 -0500133 {
134 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&comp_ptr1, &comp_size);
135 len+=1;
136 len+=(int)comp_size;
137 }
138 len++;
139
140 char *neighbor=(char *)malloc(len);
141 memset(neighbor,0,len);
142
akmhoque03004e62012-09-06 01:12:28 -0500143 for(i=nlsr_position+3+offset; i<info->interest_comps->n-1;i++)
akmhoque53f64222012-09-05 13:57:51 -0500144 {
145 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&comp_ptr1, &comp_size);
146 memcpy(neighbor+strlen(neighbor),"/",1);
147 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
148
149 }
akmhoque53f64222012-09-05 13:57:51 -0500150
151 lsaId->name=(char *)malloc(strlen(neighbor)+1);
152 memset(lsaId->name,0,strlen(neighbor)+1);
153 memcpy(lsaId->name,neighbor,strlen(neighbor)+1);
154 lsaId->length=strlen(neighbor)+1;
155
akmhoque7b791452012-10-30 11:24:56 -0500156 if ( nlsr->debugging )
157 printf("LSA Identifier: %s Length: %d\n",lsaId->name,lsaId->length-1);
158 if ( nlsr->detailed_logging )
159 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Identifier: %s Length: %d\n",lsaId->name,lsaId->length-1);
160
akmhoque53f64222012-09-05 13:57:51 -0500161}
162
akmhoque03004e62012-09-06 01:12:28 -0500163int
164get_ls_type(struct ccn_closure *selfp, struct ccn_upcall_info *info)
165{
166 int res,i;
167 int nlsr_position=0;
168 int name_comps=(int)info->interest_comps->n;
169
170 int ret=0;
171
172 for(i=0;i<name_comps;i++)
173 {
174 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
175 if( res == 0)
176 {
177 nlsr_position=i;
178 break;
179 }
180 }
181
182
183 const unsigned char *comp_ptr1;
184 size_t comp_size;
185 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+2,&comp_ptr1, &comp_size);
186
187 ret=atoi((char *)comp_ptr1);
188
189 return ret;
190
191}
192
193void
194get_lsdb_version(char *lsdb_version,struct ccn_closure *selfp, struct ccn_upcall_info *info )
195{
196 const unsigned char *comp_ptr1;
197 size_t comp_size;
198 ccn_name_comp_get(info->content_ccnb, info->content_comps,info->content_comps->n-2,&comp_ptr1, &comp_size);
199 memcpy(lsdb_version,(char *)comp_ptr1,(int)comp_size);
200
201}
202
203
204/* Call back function registered in ccnd to get all interest coming to NLSR application */
205
akmhoque59980a52012-08-09 12:36:09 -0500206enum ccn_upcall_res
207incoming_interest(struct ccn_closure *selfp,
208 enum ccn_upcall_kind kind, struct ccn_upcall_info *info)
209{
akmhoqueffacaa82012-09-13 17:48:30 -0500210
211 nlsr_lock();
212
akmhoque59980a52012-08-09 12:36:09 -0500213 switch (kind) {
214 case CCN_UPCALL_FINAL:
215 break;
216 case CCN_UPCALL_INTEREST:
akmhoque03004e62012-09-06 01:12:28 -0500217 // printing the name prefix for which it received interest
akmhoque7b791452012-10-30 11:24:56 -0500218 if ( nlsr->debugging )
219 printf("Interest Received for name: ");
220 if ( nlsr->detailed_logging )
221 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Received for name: ");
222
akmhoque03004e62012-09-06 01:12:28 -0500223 struct ccn_charbuf*c;
224 c=ccn_charbuf_create();
225 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
akmhoque7b791452012-10-30 11:24:56 -0500226
227 if ( nlsr->debugging )
228 printf("%s\n",ccn_charbuf_as_string(c));
229 if ( nlsr->detailed_logging )
230 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
231
akmhoque03004e62012-09-06 01:12:28 -0500232 ccn_charbuf_destroy(&c);
233
akmhoque1c9b92f2012-08-13 10:57:50 -0500234 process_incoming_interest(selfp, info);
akmhoque03004e62012-09-06 01:12:28 -0500235
akmhoque59980a52012-08-09 12:36:09 -0500236 break;
akmhoque03004e62012-09-06 01:12:28 -0500237
akmhoque59980a52012-08-09 12:36:09 -0500238 default:
239 break;
240 }
akmhoque03004e62012-09-06 01:12:28 -0500241
akmhoqueffacaa82012-09-13 17:48:30 -0500242 nlsr_unlock();
243
akmhoque59980a52012-08-09 12:36:09 -0500244 return CCN_UPCALL_RESULT_OK;
245}
246
akmhoque03004e62012-09-06 01:12:28 -0500247/* Function for processing incoming interest and reply with content/NACK content */
akmhoque59980a52012-08-09 12:36:09 -0500248
akmhoqued79438d2012-08-27 13:31:42 -0500249void
akmhoque1c9b92f2012-08-13 10:57:50 -0500250process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
251{
akmhoque7b791452012-10-30 11:24:56 -0500252 if ( nlsr->debugging )
253 printf("process_incoming_interest called \n");
254 if ( nlsr->detailed_logging )
255 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest called \n");
256
akmhoque1c9b92f2012-08-13 10:57:50 -0500257 const unsigned char *comp_ptr1;
258 size_t comp_size;
259 int res,i;
260 int nlsr_position=0;
261 int name_comps=(int)info->interest_comps->n;
akmhoque53f64222012-09-05 13:57:51 -0500262
akmhoque1c9b92f2012-08-13 10:57:50 -0500263 for(i=0;i<name_comps;i++)
264 {
265 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
266 if( res == 0)
267 {
akmhoqueea3603e2012-08-13 11:24:09 -0500268 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500269 break;
270 }
271 }
272
273 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500274
akmhoque1c9b92f2012-08-13 10:57:50 -0500275
akmhoqued79438d2012-08-27 13:31:42 -0500276 if(!strcmp((char *)comp_ptr1,"info"))
277 {
278 process_incoming_interest_info(selfp,info);
279 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600280
akmhoque53f64222012-09-05 13:57:51 -0500281}
282
283void
284process_incoming_interest_info(struct ccn_closure *selfp, struct ccn_upcall_info *info)
285{
akmhoque7b791452012-10-30 11:24:56 -0500286 if ( nlsr->debugging )
287 {
288 printf("process_incoming_interest_info called \n");
289 printf("Sending Info Content back.....\n");
290 }
291 if ( nlsr->detailed_logging )
292 {
293 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest_info called \n");
294 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending Info Content back.....\n");
295 }
296
akmhoque53f64222012-09-05 13:57:51 -0500297
akmhoque53f64222012-09-05 13:57:51 -0500298 int res;
299 struct ccn_charbuf *data=ccn_charbuf_create();
300 struct ccn_charbuf *name=ccn_charbuf_create();
301 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
akmhoque03004e62012-09-06 01:12:28 -0500302
akmhoque53f64222012-09-05 13:57:51 -0500303 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]);
304 if (res >= 0)
305 {
306 sp.template_ccnb=ccn_charbuf_create();
akmhoquec06dcf12013-02-20 08:13:37 -0600307
308 struct ccn_charbuf *pubid = ccn_charbuf_create();
309 struct ccn_charbuf *pubkey = ccn_charbuf_create();
310
311 int res1;
312 res1 = ccn_get_public_key(nlsr->ccn, NULL, pubid, pubkey);
313 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
314 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
315 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
316 ccn_charbuf_append_charbuf(sp.template_ccnb, pubid);
akmhoque53f64222012-09-05 13:57:51 -0500317 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600318 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600319 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoque63571b02013-02-20 08:18:49 -0600320 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
akmhoquec06dcf12013-02-20 08:13:37 -0600321 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
322 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
323 sp.type = CCN_CONTENT_KEY;
akmhoque63571b02013-02-20 08:18:49 -0600324 sp.freshness = 10;
akmhoquec06dcf12013-02-20 08:13:37 -0600325
326 //ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
327 //ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
328 //sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
329 //ccn_charbuf_append_closer(sp.template_ccnb);
akmhoque53f64222012-09-05 13:57:51 -0500330
331
akmhoqueb77b95f2013-02-08 12:28:47 -0600332 char *raw_data=(char *)malloc(20);
333 memset(raw_data,0,20);
334 sprintf(raw_data,"%s", nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500335
akmhoquec06dcf12013-02-20 08:13:37 -0600336 res= ccn_sign_content(nlsr->ccn, data, name, &sp, pubkey->buf,pubkey->length);
akmhoque53f64222012-09-05 13:57:51 -0500337 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500338 {
339 if ( nlsr->debugging )
340 printf("Signing info Content is successful \n");
341 if ( nlsr->detailed_logging )
342 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Signing info Content is successful \n");
akmhoque53f64222012-09-05 13:57:51 -0500343
akmhoque7b791452012-10-30 11:24:56 -0500344 }
akmhoque53f64222012-09-05 13:57:51 -0500345 res=ccn_put(nlsr->ccn,data->buf,data->length);
346 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500347 {
348 if ( nlsr->debugging )
349 printf("Sending Info Content is successful \n");
350 if ( nlsr->detailed_logging )
351 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info Content is successful \n");
352 }
353
akmhoque53f64222012-09-05 13:57:51 -0500354
355
356 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix *));
akmhoque03004e62012-09-06 01:12:28 -0500357 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque7b791452012-10-30 11:24:56 -0500358
359 if ( nlsr->debugging )
360 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
361 if ( nlsr->detailed_logging )
362 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
363
364 //printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500365
akmhoque53f64222012-09-05 13:57:51 -0500366
367 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr)>=nlsr->interest_retry )
368 {
akmhoqueb8195202012-09-25 11:53:23 -0500369 update_adjacent_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500370 send_info_interest_to_neighbor(nbr);
371 }
372
373 free(nbr);
374 free(raw_data);
375 ccn_charbuf_destroy(&sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600376 ccn_charbuf_destroy(&pubid);
377 ccn_charbuf_destroy(&pubkey);
akmhoque53f64222012-09-05 13:57:51 -0500378 }
akmhoque03004e62012-09-06 01:12:28 -0500379
akmhoque53f64222012-09-05 13:57:51 -0500380 ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500381 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500382
akmhoquebf1aa832012-08-13 13:26:59 -0500383}
384
385
akmhoque03004e62012-09-06 01:12:28 -0500386/* Call back function registered in ccnd to get all content coming to NLSR application */
387
akmhoque53f64222012-09-05 13:57:51 -0500388enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
389 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500390{
391
akmhoqueffacaa82012-09-13 17:48:30 -0500392 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500393
akmhoque53f64222012-09-05 13:57:51 -0500394 switch(kind) {
395 case CCN_UPCALL_FINAL:
396 break;
397 case CCN_UPCALL_CONTENT:
akmhoque7b791452012-10-30 11:24:56 -0500398 if ( nlsr->debugging )
399 printf("Content Received for Name: ");
400 if ( nlsr->detailed_logging )
401 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Content Received for Name: ");
402
akmhoque03004e62012-09-06 01:12:28 -0500403 struct ccn_charbuf*c;
404 c=ccn_charbuf_create();
405 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500406 if ( nlsr->debugging )
407 printf("%s\n",ccn_charbuf_as_string(c));
408 if ( nlsr->detailed_logging )
409 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
410
akmhoque03004e62012-09-06 01:12:28 -0500411 ccn_charbuf_destroy(&c);
412
413 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500414
akmhoque53f64222012-09-05 13:57:51 -0500415 break;
416 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoque7b791452012-10-30 11:24:56 -0500417 //printf("Interest Timed Out Received for Name: ");
418 if ( nlsr->debugging )
419 printf("Interest Timed Out Received for Name: ");
420 if ( nlsr->detailed_logging )
421 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed Out Received for Name: ");
akmhoque03004e62012-09-06 01:12:28 -0500422
423 struct ccn_charbuf*ito;
424 ito=ccn_charbuf_create();
425 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500426 if ( nlsr->debugging )
427 printf("%s\n",ccn_charbuf_as_string(ito));
428 if ( nlsr->detailed_logging )
429 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(ito));
akmhoque03004e62012-09-06 01:12:28 -0500430 ccn_charbuf_destroy(&ito);
431
akmhoquec06dcf12013-02-20 08:13:37 -0600432
433
akmhoque53f64222012-09-05 13:57:51 -0500434 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500435
akmhoque53f64222012-09-05 13:57:51 -0500436 break;
437 default:
438 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoque7b791452012-10-30 11:24:56 -0500439 if ( nlsr->debugging )
440 printf("Unexpected response of kind %d\n", kind);
441 if ( nlsr->detailed_logging )
442 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unexpected response of kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500443 break;
akmhoque53f64222012-09-05 13:57:51 -0500444 }
akmhoqueffacaa82012-09-13 17:48:30 -0500445
446 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500447
akmhoque53f64222012-09-05 13:57:51 -0500448 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500449}
450
akmhoque03004e62012-09-06 01:12:28 -0500451
akmhoqued79438d2012-08-27 13:31:42 -0500452void
akmhoque53f64222012-09-05 13:57:51 -0500453process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500454{
akmhoque7b791452012-10-30 11:24:56 -0500455 if ( nlsr->debugging )
456 printf("process_incoming_content called \n");
457 if ( nlsr->detailed_logging )
458 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500459
460 const unsigned char *comp_ptr1;
461 size_t comp_size;
462 int res,i;
463 int nlsr_position=0;
464 int name_comps=(int)info->interest_comps->n;
465
466 for(i=0;i<name_comps;i++)
467 {
468 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
469 if( res == 0)
470 {
471 nlsr_position=i;
472 break;
473 }
474 }
475
476 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
477
akmhoque53f64222012-09-05 13:57:51 -0500478
479 if(!strcmp((char *)comp_ptr1,"info"))
480 {
481 process_incoming_content_info(selfp,info);
482 }
akmhoque03004e62012-09-06 01:12:28 -0500483
akmhoque53f64222012-09-05 13:57:51 -0500484}
485
akmhoque03004e62012-09-06 01:12:28 -0500486
akmhoque53f64222012-09-05 13:57:51 -0500487void
488process_incoming_content_info(struct ccn_closure *selfp, struct ccn_upcall_info* info)
489{
akmhoque7b791452012-10-30 11:24:56 -0500490 if ( nlsr->debugging )
491 printf("process_incoming_content_info called \n");
492 if ( nlsr->detailed_logging )
493 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_info called \n");
akmhoque03004e62012-09-06 01:12:28 -0500494
akmhoque53f64222012-09-05 13:57:51 -0500495 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
496 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500497
akmhoque7b791452012-10-30 11:24:56 -0500498 if ( nlsr->debugging )
499 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,nbr->length);
500 if ( nlsr->detailed_logging )
501 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Content Received For Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500502
akmhoque53f64222012-09-05 13:57:51 -0500503
akmhoquec06dcf12013-02-20 08:13:37 -0600504 if ( contain_key_name(info->content_ccnb, info->pco) == 1)
505 {
506 struct ccn_charbuf *key_name=get_key_name(info->content_ccnb, info->pco);
507 if(nlsr->debugging)
508 printf("Key Name: %s\n",ccn_charbuf_as_string(key_name));
509 }
akmhoque03004e62012-09-06 01:12:28 -0500510
akmhoque53f64222012-09-05 13:57:51 -0500511 update_adjacent_timed_out_zero_to_adl(nbr);
512 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
akmhoque53f64222012-09-05 13:57:51 -0500513 print_adjacent_from_adl();
514
akmhoque03004e62012-09-06 01:12:28 -0500515
516
akmhoque53f64222012-09-05 13:57:51 -0500517 if(!nlsr->is_build_adj_lsa_sheduled)
518 {
akmhoque7b791452012-10-30 11:24:56 -0500519 if ( nlsr->debugging )
520 printf("Scheduling Build and Install Adj LSA...\n");
521 if ( nlsr->detailed_logging )
522 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Scheduling Build and Install Adj LSA...\n");
akmhoque53f64222012-09-05 13:57:51 -0500523 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 100000, &build_and_install_adj_lsa, NULL, 0);
524 nlsr->is_build_adj_lsa_sheduled=1;
525 }
526 else
527 {
akmhoque7b791452012-10-30 11:24:56 -0500528 if ( nlsr->debugging )
529 printf("Build and Install Adj LSA already scheduled\n");
530 if ( nlsr->detailed_logging )
531 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Build and Install Adj LSA already scheduled\n");
akmhoque53f64222012-09-05 13:57:51 -0500532 }
533
akmhoque03004e62012-09-06 01:12:28 -0500534
akmhoque53f64222012-09-05 13:57:51 -0500535 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500536
akmhoque53f64222012-09-05 13:57:51 -0500537
538}
539
akmhoque03004e62012-09-06 01:12:28 -0500540
akmhoque53f64222012-09-05 13:57:51 -0500541
akmhoque03004e62012-09-06 01:12:28 -0500542
akmhoque53f64222012-09-05 13:57:51 -0500543void
544process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
545{
akmhoque3171d652012-11-13 11:44:33 -0600546
547
548 if ( nlsr->debugging )
549 printf("process_incoming_timed_out_interest called \n");
550 if ( nlsr->detailed_logging )
551 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest called \n");
552
akmhoque53f64222012-09-05 13:57:51 -0500553 int res,i;
554 int nlsr_position=0;
555 int name_comps=(int)info->interest_comps->n;
556
557 for(i=0;i<name_comps;i++)
558 {
559 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
560 if( res == 0)
561 {
562 nlsr_position=i;
563 break;
564 }
565 }
566
567 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"info") == 0)
568 {
569 process_incoming_timed_out_interest_info(selfp,info);
570 }
571}
572
573void
574process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct ccn_upcall_info* info)
575{
akmhoque3171d652012-11-13 11:44:33 -0600576
577 if ( nlsr->debugging )
578 printf("process_incoming_timed_out_interest_info called \n");
579 if ( nlsr->detailed_logging )
580 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500581
582 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
583 get_nbr(nbr,selfp,info);
584
akmhoque3171d652012-11-13 11:44:33 -0600585 if ( nlsr->debugging )
586 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
587 if ( nlsr->detailed_logging )
588 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
589
akmhoque03004e62012-09-06 01:12:28 -0500590
591
akmhoque53f64222012-09-05 13:57:51 -0500592 update_adjacent_timed_out_to_adl(nbr,1);
593 print_adjacent_from_adl();
594 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500595
akmhoque3171d652012-11-13 11:44:33 -0600596 if ( nlsr->debugging )
597 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
598 if ( nlsr->detailed_logging )
599 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
600
akmhoque53f64222012-09-05 13:57:51 -0500601
602 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
603 {
akmhoque53f64222012-09-05 13:57:51 -0500604 send_info_interest_to_neighbor(nbr);
605 }
606 else
akmhoque3171d652012-11-13 11:44:33 -0600607 {
akmhoque53f64222012-09-05 13:57:51 -0500608 update_adjacent_status_to_adl(nbr,NBR_DOWN);
609 if(!nlsr->is_build_adj_lsa_sheduled)
610 {
611 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
612 nlsr->is_build_adj_lsa_sheduled=1;
613 }
614 }
615
616 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500617
akmhoque53f64222012-09-05 13:57:51 -0500618
619}
620
akmhoque29c1db52012-09-07 14:47:43 -0500621
akmhoque03004e62012-09-06 01:12:28 -0500622int
akmhoque53f64222012-09-05 13:57:51 -0500623send_info_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
624{
akmhoqueffacaa82012-09-13 17:48:30 -0500625 if(flags == CCN_SCHEDULE_CANCEL)
626 {
627 return -1;
628 }
629
630 nlsr_lock();
631
akmhoque3171d652012-11-13 11:44:33 -0600632 if ( nlsr->debugging )
633 printf("send_info_interest called \n");
634 if ( nlsr->detailed_logging )
635 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
636
637 if ( nlsr->debugging )
638 printf("\n");
639 if ( nlsr->detailed_logging )
640 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoqued79438d2012-08-27 13:31:42 -0500641
akmhoque53f64222012-09-05 13:57:51 -0500642 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500643 struct ndn_neighbor *nbr;
644
645 struct hashtb_enumerator ee;
646 struct hashtb_enumerator *e = &ee;
647
648 hashtb_start(nlsr->adl, e);
649 adl_element=hashtb_n(nlsr->adl);
650
651 for(i=0;i<adl_element;i++)
652 {
653 nbr=e->data;
654 send_info_interest_to_neighbor(nbr->neighbor);
655 hashtb_next(e);
656 }
akmhoque53f64222012-09-05 13:57:51 -0500657 hashtb_end(e);
658
akmhoqueffacaa82012-09-13 17:48:30 -0500659 nlsr_unlock();
660
akmhoque9fa58a82012-10-05 07:56:02 -0500661 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &send_info_interest, NULL, 0);
662
akmhoque53f64222012-09-05 13:57:51 -0500663 return 0;
664}
665
666void
667send_info_interest_to_neighbor(struct name_prefix *nbr)
668{
akmhoque3171d652012-11-13 11:44:33 -0600669
670 if ( nlsr->debugging )
671 printf("send_info_interest_to_neighbor called \n");
672 if ( nlsr->detailed_logging )
673 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor called \n");
674
akmhoqued79438d2012-08-27 13:31:42 -0500675
676 int res;
akmhoque53f64222012-09-05 13:57:51 -0500677 char info_str[5];
678 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500679
akmhoqued79438d2012-08-27 13:31:42 -0500680 memset(&nlsr_str,0,5);
681 sprintf(nlsr_str,"nlsr");
682 memset(&info_str,0,5);
683 sprintf(info_str,"info");
684
akmhoque53f64222012-09-05 13:57:51 -0500685
686 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500687 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500688
akmhoque03004e62012-09-06 01:12:28 -0500689 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+strlen(nlsr->router_name)+1);
690 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 -0500691 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
692 memcpy(int_name+strlen(int_name),"/",1);
693 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
694 memcpy(int_name+strlen(int_name),"/",1);
695 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500696 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500697
698
699 res=ccn_name_from_uri(name,int_name);
700 if ( res >=0 )
701 {
akmhoque53f64222012-09-05 13:57:51 -0500702 /* adding InterestLifeTime and InterestScope filter */
703
704 struct ccn_charbuf *templ;
705 templ = ccn_charbuf_create();
706
707 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
708 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
709 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500710 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
711 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
akmhoqueb77b95f2013-02-08 12:28:47 -0600712 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque03004e62012-09-06 01:12:28 -0500713 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
714 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500715
716 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqueb77b95f2013-02-08 12:28:47 -0600717 unsigned int face_id=get_next_hop_face_from_adl(nbr->name);
718 ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face_id);
akmhoque53f64222012-09-05 13:57:51 -0500719 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoqueb77b95f2013-02-08 12:28:47 -0600720
akmhoque53f64222012-09-05 13:57:51 -0500721
akmhoque3171d652012-11-13 11:44:33 -0600722 if ( nlsr->debugging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600723 printf("Sending info interest on name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque3171d652012-11-13 11:44:33 -0600724 if ( nlsr->detailed_logging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600725 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info interest on name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque53f64222012-09-05 13:57:51 -0500726
727 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
728
729 if ( res >= 0 )
akmhoque3171d652012-11-13 11:44:33 -0600730 {
731 if ( nlsr->debugging )
732 printf("Info interest sending Successfull .... \n");
733 if ( nlsr->detailed_logging )
734 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info interest sending Successfull .... \n");
735 }
akmhoque53f64222012-09-05 13:57:51 -0500736 ccn_charbuf_destroy(&templ);
737 }
738 ccn_charbuf_destroy(&name);
739 free(int_name);
akmhoquec06dcf12013-02-20 08:13:37 -0600740}
akmhoque53f64222012-09-05 13:57:51 -0500741
akmhoquec06dcf12013-02-20 08:13:37 -0600742int
743contain_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
744{
745 if (pco->offset[CCN_PCO_B_KeyLocator] == pco->offset[CCN_PCO_E_KeyLocator])
746 return -1;
747
748 struct ccn_buf_decoder decoder;
749 struct ccn_buf_decoder *d;
750 d = ccn_buf_decoder_start(&decoder, ccnb + pco->offset[CCN_PCO_B_Key_Certificate_KeyName], pco->offset[CCN_PCO_E_Key_Certificate_KeyName] - pco->offset[CCN_PCO_B_Key_Certificate_KeyName]);
751 if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName))
752 return 1;
753
754 return -1;
755}
756
757struct ccn_charbuf *
758get_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
759{
760 struct ccn_charbuf *key_name = ccn_charbuf_create();
761 ccn_charbuf_append(key_name, ccnb + pco->offset[CCN_PCO_B_KeyName_Name], pco->offset[CCN_PCO_E_KeyName_Name] - pco->offset[CCN_PCO_B_KeyName_Name]);
762
763 return key_name;
akmhoque53f64222012-09-05 13:57:51 -0500764}