blob: c0e5db8ea8bfb9df2dc5e9be6323e6a6fe06e785 [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();
akmhoquedd7a7a72013-02-20 08:25:41 -0600301
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 {
akmhoquedd7a7a72013-02-20 08:25:41 -0600306
akmhoquec06dcf12013-02-20 08:13:37 -0600307
308 struct ccn_charbuf *pubid = ccn_charbuf_create();
309 struct ccn_charbuf *pubkey = ccn_charbuf_create();
310
akmhoque7ca519b2013-02-20 09:49:43 -0600311 //pubid is the digest_result pubkey is result
312 ccn_get_public_key(nlsr->ccn, NULL, pubid, pubkey);
313
314
akmhoquedd7a7a72013-02-20 08:25:41 -0600315
316 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
317 sp.template_ccnb=ccn_charbuf_create();
akmhoquec06dcf12013-02-20 08:13:37 -0600318 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
319 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
320 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
akmhoquedd7a7a72013-02-20 08:25:41 -0600321 ccn_charbuf_append_charbuf(sp.template_ccnb, name);
akmhoque53f64222012-09-05 13:57:51 -0500322 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600323 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600324 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquedd7a7a72013-02-20 08:25:41 -0600325
akmhoquec06dcf12013-02-20 08:13:37 -0600326 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
327 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
328 sp.type = CCN_CONTENT_KEY;
akmhoque63571b02013-02-20 08:18:49 -0600329 sp.freshness = 10;
akmhoquec06dcf12013-02-20 08:13:37 -0600330
331 //ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
332 //ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
333 //sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
334 //ccn_charbuf_append_closer(sp.template_ccnb);
akmhoque53f64222012-09-05 13:57:51 -0500335
336
akmhoqueb77b95f2013-02-08 12:28:47 -0600337 char *raw_data=(char *)malloc(20);
338 memset(raw_data,0,20);
339 sprintf(raw_data,"%s", nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500340
akmhoquec06dcf12013-02-20 08:13:37 -0600341 res= ccn_sign_content(nlsr->ccn, data, name, &sp, pubkey->buf,pubkey->length);
akmhoque53f64222012-09-05 13:57:51 -0500342 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500343 {
344 if ( nlsr->debugging )
345 printf("Signing info Content is successful \n");
346 if ( nlsr->detailed_logging )
347 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Signing info Content is successful \n");
akmhoque53f64222012-09-05 13:57:51 -0500348
akmhoque7b791452012-10-30 11:24:56 -0500349 }
akmhoque53f64222012-09-05 13:57:51 -0500350 res=ccn_put(nlsr->ccn,data->buf,data->length);
351 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500352 {
353 if ( nlsr->debugging )
354 printf("Sending Info Content is successful \n");
355 if ( nlsr->detailed_logging )
356 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info Content is successful \n");
357 }
358
akmhoque53f64222012-09-05 13:57:51 -0500359
360
361 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix *));
akmhoque03004e62012-09-06 01:12:28 -0500362 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque7b791452012-10-30 11:24:56 -0500363
364 if ( nlsr->debugging )
365 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
366 if ( nlsr->detailed_logging )
367 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
368
369 //printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500370
akmhoque53f64222012-09-05 13:57:51 -0500371
372 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr)>=nlsr->interest_retry )
373 {
akmhoqueb8195202012-09-25 11:53:23 -0500374 update_adjacent_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500375 send_info_interest_to_neighbor(nbr);
376 }
377
378 free(nbr);
379 free(raw_data);
380 ccn_charbuf_destroy(&sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600381 ccn_charbuf_destroy(&pubid);
382 ccn_charbuf_destroy(&pubkey);
akmhoque53f64222012-09-05 13:57:51 -0500383 }
akmhoque03004e62012-09-06 01:12:28 -0500384
akmhoque53f64222012-09-05 13:57:51 -0500385 ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500386 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500387
akmhoquebf1aa832012-08-13 13:26:59 -0500388}
389
390
akmhoque03004e62012-09-06 01:12:28 -0500391/* Call back function registered in ccnd to get all content coming to NLSR application */
392
akmhoque53f64222012-09-05 13:57:51 -0500393enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
394 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500395{
396
akmhoqueffacaa82012-09-13 17:48:30 -0500397 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500398
akmhoque53f64222012-09-05 13:57:51 -0500399 switch(kind) {
400 case CCN_UPCALL_FINAL:
401 break;
402 case CCN_UPCALL_CONTENT:
akmhoque7b791452012-10-30 11:24:56 -0500403 if ( nlsr->debugging )
404 printf("Content Received for Name: ");
405 if ( nlsr->detailed_logging )
406 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Content Received for Name: ");
407
akmhoque03004e62012-09-06 01:12:28 -0500408 struct ccn_charbuf*c;
409 c=ccn_charbuf_create();
410 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500411 if ( nlsr->debugging )
412 printf("%s\n",ccn_charbuf_as_string(c));
413 if ( nlsr->detailed_logging )
414 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
415
akmhoque03004e62012-09-06 01:12:28 -0500416 ccn_charbuf_destroy(&c);
417
418 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500419
akmhoque53f64222012-09-05 13:57:51 -0500420 break;
421 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoque7b791452012-10-30 11:24:56 -0500422 //printf("Interest Timed Out Received for Name: ");
423 if ( nlsr->debugging )
424 printf("Interest Timed Out Received for Name: ");
425 if ( nlsr->detailed_logging )
426 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed Out Received for Name: ");
akmhoque03004e62012-09-06 01:12:28 -0500427
428 struct ccn_charbuf*ito;
429 ito=ccn_charbuf_create();
430 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500431 if ( nlsr->debugging )
432 printf("%s\n",ccn_charbuf_as_string(ito));
433 if ( nlsr->detailed_logging )
434 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(ito));
akmhoque03004e62012-09-06 01:12:28 -0500435 ccn_charbuf_destroy(&ito);
436
akmhoquec06dcf12013-02-20 08:13:37 -0600437
438
akmhoque53f64222012-09-05 13:57:51 -0500439 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500440
akmhoque53f64222012-09-05 13:57:51 -0500441 break;
442 default:
443 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoque7b791452012-10-30 11:24:56 -0500444 if ( nlsr->debugging )
445 printf("Unexpected response of kind %d\n", kind);
446 if ( nlsr->detailed_logging )
447 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unexpected response of kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500448 break;
akmhoque53f64222012-09-05 13:57:51 -0500449 }
akmhoqueffacaa82012-09-13 17:48:30 -0500450
451 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500452
akmhoque53f64222012-09-05 13:57:51 -0500453 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500454}
455
akmhoque03004e62012-09-06 01:12:28 -0500456
akmhoqued79438d2012-08-27 13:31:42 -0500457void
akmhoque53f64222012-09-05 13:57:51 -0500458process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500459{
akmhoque7b791452012-10-30 11:24:56 -0500460 if ( nlsr->debugging )
461 printf("process_incoming_content called \n");
462 if ( nlsr->detailed_logging )
463 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500464
465 const unsigned char *comp_ptr1;
466 size_t comp_size;
467 int res,i;
468 int nlsr_position=0;
469 int name_comps=(int)info->interest_comps->n;
470
471 for(i=0;i<name_comps;i++)
472 {
473 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
474 if( res == 0)
475 {
476 nlsr_position=i;
477 break;
478 }
479 }
480
481 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
482
akmhoque53f64222012-09-05 13:57:51 -0500483
484 if(!strcmp((char *)comp_ptr1,"info"))
485 {
486 process_incoming_content_info(selfp,info);
487 }
akmhoque03004e62012-09-06 01:12:28 -0500488
akmhoque53f64222012-09-05 13:57:51 -0500489}
490
akmhoque03004e62012-09-06 01:12:28 -0500491
akmhoque53f64222012-09-05 13:57:51 -0500492void
493process_incoming_content_info(struct ccn_closure *selfp, struct ccn_upcall_info* info)
494{
akmhoque7b791452012-10-30 11:24:56 -0500495 if ( nlsr->debugging )
496 printf("process_incoming_content_info called \n");
497 if ( nlsr->detailed_logging )
498 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_info called \n");
akmhoque03004e62012-09-06 01:12:28 -0500499
akmhoque53f64222012-09-05 13:57:51 -0500500 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
501 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500502
akmhoque7b791452012-10-30 11:24:56 -0500503 if ( nlsr->debugging )
504 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,nbr->length);
505 if ( nlsr->detailed_logging )
506 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Content Received For Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500507
akmhoque53f64222012-09-05 13:57:51 -0500508
akmhoquec06dcf12013-02-20 08:13:37 -0600509 if ( contain_key_name(info->content_ccnb, info->pco) == 1)
510 {
511 struct ccn_charbuf *key_name=get_key_name(info->content_ccnb, info->pco);
akmhoque7ca519b2013-02-20 09:49:43 -0600512 struct ccn_charbuf *key_uri = ccn_charbuf_create();
513 ccn_uri_append(key_uri, key_name->buf, key_name->length, 1);
514
akmhoquec06dcf12013-02-20 08:13:37 -0600515 if(nlsr->debugging)
akmhoque7ca519b2013-02-20 09:49:43 -0600516 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
517
518 ccn_charbuf_destroy(&key_uri);
519 ccn_charbuf_destroy(&key_name);
520
521
akmhoquec06dcf12013-02-20 08:13:37 -0600522 }
akmhoque03004e62012-09-06 01:12:28 -0500523
akmhoque53f64222012-09-05 13:57:51 -0500524 update_adjacent_timed_out_zero_to_adl(nbr);
525 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
akmhoque53f64222012-09-05 13:57:51 -0500526 print_adjacent_from_adl();
527
akmhoque03004e62012-09-06 01:12:28 -0500528
529
akmhoque53f64222012-09-05 13:57:51 -0500530 if(!nlsr->is_build_adj_lsa_sheduled)
531 {
akmhoque7b791452012-10-30 11:24:56 -0500532 if ( nlsr->debugging )
533 printf("Scheduling Build and Install Adj LSA...\n");
534 if ( nlsr->detailed_logging )
535 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Scheduling Build and Install Adj LSA...\n");
akmhoque53f64222012-09-05 13:57:51 -0500536 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 100000, &build_and_install_adj_lsa, NULL, 0);
537 nlsr->is_build_adj_lsa_sheduled=1;
538 }
539 else
540 {
akmhoque7b791452012-10-30 11:24:56 -0500541 if ( nlsr->debugging )
542 printf("Build and Install Adj LSA already scheduled\n");
543 if ( nlsr->detailed_logging )
544 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Build and Install Adj LSA already scheduled\n");
akmhoque53f64222012-09-05 13:57:51 -0500545 }
546
akmhoque03004e62012-09-06 01:12:28 -0500547
akmhoque53f64222012-09-05 13:57:51 -0500548 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500549
akmhoque53f64222012-09-05 13:57:51 -0500550
551}
552
akmhoque03004e62012-09-06 01:12:28 -0500553
akmhoque53f64222012-09-05 13:57:51 -0500554
akmhoque03004e62012-09-06 01:12:28 -0500555
akmhoque53f64222012-09-05 13:57:51 -0500556void
557process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
558{
akmhoque3171d652012-11-13 11:44:33 -0600559
560
561 if ( nlsr->debugging )
562 printf("process_incoming_timed_out_interest called \n");
563 if ( nlsr->detailed_logging )
564 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest called \n");
565
akmhoque53f64222012-09-05 13:57:51 -0500566 int res,i;
567 int nlsr_position=0;
568 int name_comps=(int)info->interest_comps->n;
569
570 for(i=0;i<name_comps;i++)
571 {
572 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
573 if( res == 0)
574 {
575 nlsr_position=i;
576 break;
577 }
578 }
579
580 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"info") == 0)
581 {
582 process_incoming_timed_out_interest_info(selfp,info);
583 }
584}
585
586void
587process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct ccn_upcall_info* info)
588{
akmhoque3171d652012-11-13 11:44:33 -0600589
590 if ( nlsr->debugging )
591 printf("process_incoming_timed_out_interest_info called \n");
592 if ( nlsr->detailed_logging )
593 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500594
595 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
596 get_nbr(nbr,selfp,info);
597
akmhoque3171d652012-11-13 11:44:33 -0600598 if ( nlsr->debugging )
599 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
600 if ( nlsr->detailed_logging )
601 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
602
akmhoque03004e62012-09-06 01:12:28 -0500603
604
akmhoque53f64222012-09-05 13:57:51 -0500605 update_adjacent_timed_out_to_adl(nbr,1);
606 print_adjacent_from_adl();
607 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500608
akmhoque3171d652012-11-13 11:44:33 -0600609 if ( nlsr->debugging )
610 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
611 if ( nlsr->detailed_logging )
612 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
613
akmhoque53f64222012-09-05 13:57:51 -0500614
615 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
616 {
akmhoque53f64222012-09-05 13:57:51 -0500617 send_info_interest_to_neighbor(nbr);
618 }
619 else
akmhoque3171d652012-11-13 11:44:33 -0600620 {
akmhoque53f64222012-09-05 13:57:51 -0500621 update_adjacent_status_to_adl(nbr,NBR_DOWN);
622 if(!nlsr->is_build_adj_lsa_sheduled)
623 {
624 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
625 nlsr->is_build_adj_lsa_sheduled=1;
626 }
627 }
628
629 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500630
akmhoque53f64222012-09-05 13:57:51 -0500631
632}
633
akmhoque29c1db52012-09-07 14:47:43 -0500634
akmhoque03004e62012-09-06 01:12:28 -0500635int
akmhoque53f64222012-09-05 13:57:51 -0500636send_info_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
637{
akmhoqueffacaa82012-09-13 17:48:30 -0500638 if(flags == CCN_SCHEDULE_CANCEL)
639 {
640 return -1;
641 }
642
643 nlsr_lock();
644
akmhoque3171d652012-11-13 11:44:33 -0600645 if ( nlsr->debugging )
646 printf("send_info_interest called \n");
647 if ( nlsr->detailed_logging )
648 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
649
650 if ( nlsr->debugging )
651 printf("\n");
652 if ( nlsr->detailed_logging )
653 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoqued79438d2012-08-27 13:31:42 -0500654
akmhoque53f64222012-09-05 13:57:51 -0500655 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500656 struct ndn_neighbor *nbr;
657
658 struct hashtb_enumerator ee;
659 struct hashtb_enumerator *e = &ee;
660
661 hashtb_start(nlsr->adl, e);
662 adl_element=hashtb_n(nlsr->adl);
663
664 for(i=0;i<adl_element;i++)
665 {
666 nbr=e->data;
667 send_info_interest_to_neighbor(nbr->neighbor);
668 hashtb_next(e);
669 }
akmhoque53f64222012-09-05 13:57:51 -0500670 hashtb_end(e);
671
akmhoqueffacaa82012-09-13 17:48:30 -0500672 nlsr_unlock();
673
akmhoque9fa58a82012-10-05 07:56:02 -0500674 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &send_info_interest, NULL, 0);
675
akmhoque53f64222012-09-05 13:57:51 -0500676 return 0;
677}
678
679void
680send_info_interest_to_neighbor(struct name_prefix *nbr)
681{
akmhoque3171d652012-11-13 11:44:33 -0600682
683 if ( nlsr->debugging )
684 printf("send_info_interest_to_neighbor called \n");
685 if ( nlsr->detailed_logging )
686 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor called \n");
687
akmhoqued79438d2012-08-27 13:31:42 -0500688
689 int res;
akmhoque53f64222012-09-05 13:57:51 -0500690 char info_str[5];
691 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500692
akmhoqued79438d2012-08-27 13:31:42 -0500693 memset(&nlsr_str,0,5);
694 sprintf(nlsr_str,"nlsr");
695 memset(&info_str,0,5);
696 sprintf(info_str,"info");
697
akmhoque53f64222012-09-05 13:57:51 -0500698
699 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500700 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500701
akmhoque03004e62012-09-06 01:12:28 -0500702 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+strlen(nlsr->router_name)+1);
703 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 -0500704 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
705 memcpy(int_name+strlen(int_name),"/",1);
706 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
707 memcpy(int_name+strlen(int_name),"/",1);
708 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500709 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500710
711
712 res=ccn_name_from_uri(name,int_name);
713 if ( res >=0 )
714 {
akmhoque53f64222012-09-05 13:57:51 -0500715 /* adding InterestLifeTime and InterestScope filter */
716
717 struct ccn_charbuf *templ;
718 templ = ccn_charbuf_create();
719
720 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
721 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
722 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500723 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
724 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
akmhoqueb77b95f2013-02-08 12:28:47 -0600725 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque03004e62012-09-06 01:12:28 -0500726 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
727 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500728
729 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqueb77b95f2013-02-08 12:28:47 -0600730 unsigned int face_id=get_next_hop_face_from_adl(nbr->name);
731 ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face_id);
akmhoque53f64222012-09-05 13:57:51 -0500732 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoqueb77b95f2013-02-08 12:28:47 -0600733
akmhoque53f64222012-09-05 13:57:51 -0500734
akmhoque3171d652012-11-13 11:44:33 -0600735 if ( nlsr->debugging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600736 printf("Sending info interest on name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque3171d652012-11-13 11:44:33 -0600737 if ( nlsr->detailed_logging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600738 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 -0500739
740 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
741
742 if ( res >= 0 )
akmhoque3171d652012-11-13 11:44:33 -0600743 {
744 if ( nlsr->debugging )
745 printf("Info interest sending Successfull .... \n");
746 if ( nlsr->detailed_logging )
747 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info interest sending Successfull .... \n");
748 }
akmhoque53f64222012-09-05 13:57:51 -0500749 ccn_charbuf_destroy(&templ);
750 }
751 ccn_charbuf_destroy(&name);
752 free(int_name);
akmhoquec06dcf12013-02-20 08:13:37 -0600753}
akmhoque53f64222012-09-05 13:57:51 -0500754
akmhoquec06dcf12013-02-20 08:13:37 -0600755int
756contain_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
757{
758 if (pco->offset[CCN_PCO_B_KeyLocator] == pco->offset[CCN_PCO_E_KeyLocator])
759 return -1;
760
761 struct ccn_buf_decoder decoder;
762 struct ccn_buf_decoder *d;
763 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]);
764 if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName))
765 return 1;
766
767 return -1;
768}
769
770struct ccn_charbuf *
771get_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
772{
773 struct ccn_charbuf *key_name = ccn_charbuf_create();
774 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]);
775
776 return key_name;
akmhoque53f64222012-09-05 13:57:51 -0500777}