blob: 88cb511b28dfe05da3110a2c11fb250f152a0b78 [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
311 int res1;
akmhoquedd7a7a72013-02-20 08:25:41 -0600312 res1 = ccn_get_public_key(nlsr->ccn, NULL, pubid, pubkey);
313
314 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
315 sp.template_ccnb=ccn_charbuf_create();
akmhoquec06dcf12013-02-20 08:13:37 -0600316 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
317 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
318 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
akmhoquedd7a7a72013-02-20 08:25:41 -0600319 ccn_charbuf_append_charbuf(sp.template_ccnb, name);
akmhoque53f64222012-09-05 13:57:51 -0500320 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600321 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600322 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquedd7a7a72013-02-20 08:25:41 -0600323
akmhoquec06dcf12013-02-20 08:13:37 -0600324 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
325 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
326 sp.type = CCN_CONTENT_KEY;
akmhoque63571b02013-02-20 08:18:49 -0600327 sp.freshness = 10;
akmhoquec06dcf12013-02-20 08:13:37 -0600328
329 //ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
330 //ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
331 //sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
332 //ccn_charbuf_append_closer(sp.template_ccnb);
akmhoque53f64222012-09-05 13:57:51 -0500333
334
akmhoqueb77b95f2013-02-08 12:28:47 -0600335 char *raw_data=(char *)malloc(20);
336 memset(raw_data,0,20);
337 sprintf(raw_data,"%s", nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500338
akmhoquec06dcf12013-02-20 08:13:37 -0600339 res= ccn_sign_content(nlsr->ccn, data, name, &sp, pubkey->buf,pubkey->length);
akmhoque53f64222012-09-05 13:57:51 -0500340 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500341 {
342 if ( nlsr->debugging )
343 printf("Signing info Content is successful \n");
344 if ( nlsr->detailed_logging )
345 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Signing info Content is successful \n");
akmhoque53f64222012-09-05 13:57:51 -0500346
akmhoque7b791452012-10-30 11:24:56 -0500347 }
akmhoque53f64222012-09-05 13:57:51 -0500348 res=ccn_put(nlsr->ccn,data->buf,data->length);
349 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500350 {
351 if ( nlsr->debugging )
352 printf("Sending Info Content is successful \n");
353 if ( nlsr->detailed_logging )
354 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info Content is successful \n");
355 }
356
akmhoque53f64222012-09-05 13:57:51 -0500357
358
359 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix *));
akmhoque03004e62012-09-06 01:12:28 -0500360 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque7b791452012-10-30 11:24:56 -0500361
362 if ( nlsr->debugging )
363 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
364 if ( nlsr->detailed_logging )
365 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
366
367 //printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500368
akmhoque53f64222012-09-05 13:57:51 -0500369
370 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr)>=nlsr->interest_retry )
371 {
akmhoqueb8195202012-09-25 11:53:23 -0500372 update_adjacent_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500373 send_info_interest_to_neighbor(nbr);
374 }
375
376 free(nbr);
377 free(raw_data);
378 ccn_charbuf_destroy(&sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600379 ccn_charbuf_destroy(&pubid);
380 ccn_charbuf_destroy(&pubkey);
akmhoque53f64222012-09-05 13:57:51 -0500381 }
akmhoque03004e62012-09-06 01:12:28 -0500382
akmhoque53f64222012-09-05 13:57:51 -0500383 ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500384 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500385
akmhoquebf1aa832012-08-13 13:26:59 -0500386}
387
388
akmhoque03004e62012-09-06 01:12:28 -0500389/* Call back function registered in ccnd to get all content coming to NLSR application */
390
akmhoque53f64222012-09-05 13:57:51 -0500391enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
392 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500393{
394
akmhoqueffacaa82012-09-13 17:48:30 -0500395 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500396
akmhoque53f64222012-09-05 13:57:51 -0500397 switch(kind) {
398 case CCN_UPCALL_FINAL:
399 break;
400 case CCN_UPCALL_CONTENT:
akmhoque7b791452012-10-30 11:24:56 -0500401 if ( nlsr->debugging )
402 printf("Content Received for Name: ");
403 if ( nlsr->detailed_logging )
404 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Content Received for Name: ");
405
akmhoque03004e62012-09-06 01:12:28 -0500406 struct ccn_charbuf*c;
407 c=ccn_charbuf_create();
408 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500409 if ( nlsr->debugging )
410 printf("%s\n",ccn_charbuf_as_string(c));
411 if ( nlsr->detailed_logging )
412 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
413
akmhoque03004e62012-09-06 01:12:28 -0500414 ccn_charbuf_destroy(&c);
415
416 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500417
akmhoque53f64222012-09-05 13:57:51 -0500418 break;
419 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoque7b791452012-10-30 11:24:56 -0500420 //printf("Interest Timed Out Received for Name: ");
421 if ( nlsr->debugging )
422 printf("Interest Timed Out Received for Name: ");
423 if ( nlsr->detailed_logging )
424 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed Out Received for Name: ");
akmhoque03004e62012-09-06 01:12:28 -0500425
426 struct ccn_charbuf*ito;
427 ito=ccn_charbuf_create();
428 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500429 if ( nlsr->debugging )
430 printf("%s\n",ccn_charbuf_as_string(ito));
431 if ( nlsr->detailed_logging )
432 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(ito));
akmhoque03004e62012-09-06 01:12:28 -0500433 ccn_charbuf_destroy(&ito);
434
akmhoquec06dcf12013-02-20 08:13:37 -0600435
436
akmhoque53f64222012-09-05 13:57:51 -0500437 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500438
akmhoque53f64222012-09-05 13:57:51 -0500439 break;
440 default:
441 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoque7b791452012-10-30 11:24:56 -0500442 if ( nlsr->debugging )
443 printf("Unexpected response of kind %d\n", kind);
444 if ( nlsr->detailed_logging )
445 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unexpected response of kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500446 break;
akmhoque53f64222012-09-05 13:57:51 -0500447 }
akmhoqueffacaa82012-09-13 17:48:30 -0500448
449 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500450
akmhoque53f64222012-09-05 13:57:51 -0500451 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500452}
453
akmhoque03004e62012-09-06 01:12:28 -0500454
akmhoqued79438d2012-08-27 13:31:42 -0500455void
akmhoque53f64222012-09-05 13:57:51 -0500456process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500457{
akmhoque7b791452012-10-30 11:24:56 -0500458 if ( nlsr->debugging )
459 printf("process_incoming_content called \n");
460 if ( nlsr->detailed_logging )
461 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500462
463 const unsigned char *comp_ptr1;
464 size_t comp_size;
465 int res,i;
466 int nlsr_position=0;
467 int name_comps=(int)info->interest_comps->n;
468
469 for(i=0;i<name_comps;i++)
470 {
471 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
472 if( res == 0)
473 {
474 nlsr_position=i;
475 break;
476 }
477 }
478
479 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
480
akmhoque53f64222012-09-05 13:57:51 -0500481
482 if(!strcmp((char *)comp_ptr1,"info"))
483 {
484 process_incoming_content_info(selfp,info);
485 }
akmhoque03004e62012-09-06 01:12:28 -0500486
akmhoque53f64222012-09-05 13:57:51 -0500487}
488
akmhoque03004e62012-09-06 01:12:28 -0500489
akmhoque53f64222012-09-05 13:57:51 -0500490void
491process_incoming_content_info(struct ccn_closure *selfp, struct ccn_upcall_info* info)
492{
akmhoque7b791452012-10-30 11:24:56 -0500493 if ( nlsr->debugging )
494 printf("process_incoming_content_info called \n");
495 if ( nlsr->detailed_logging )
496 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_info called \n");
akmhoque03004e62012-09-06 01:12:28 -0500497
akmhoque53f64222012-09-05 13:57:51 -0500498 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
499 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500500
akmhoque7b791452012-10-30 11:24:56 -0500501 if ( nlsr->debugging )
502 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,nbr->length);
503 if ( nlsr->detailed_logging )
504 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Content Received For Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500505
akmhoque53f64222012-09-05 13:57:51 -0500506
akmhoquec06dcf12013-02-20 08:13:37 -0600507 if ( contain_key_name(info->content_ccnb, info->pco) == 1)
508 {
509 struct ccn_charbuf *key_name=get_key_name(info->content_ccnb, info->pco);
510 if(nlsr->debugging)
511 printf("Key Name: %s\n",ccn_charbuf_as_string(key_name));
512 }
akmhoque03004e62012-09-06 01:12:28 -0500513
akmhoque53f64222012-09-05 13:57:51 -0500514 update_adjacent_timed_out_zero_to_adl(nbr);
515 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
akmhoque53f64222012-09-05 13:57:51 -0500516 print_adjacent_from_adl();
517
akmhoque03004e62012-09-06 01:12:28 -0500518
519
akmhoque53f64222012-09-05 13:57:51 -0500520 if(!nlsr->is_build_adj_lsa_sheduled)
521 {
akmhoque7b791452012-10-30 11:24:56 -0500522 if ( nlsr->debugging )
523 printf("Scheduling Build and Install Adj LSA...\n");
524 if ( nlsr->detailed_logging )
525 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Scheduling Build and Install Adj LSA...\n");
akmhoque53f64222012-09-05 13:57:51 -0500526 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 100000, &build_and_install_adj_lsa, NULL, 0);
527 nlsr->is_build_adj_lsa_sheduled=1;
528 }
529 else
530 {
akmhoque7b791452012-10-30 11:24:56 -0500531 if ( nlsr->debugging )
532 printf("Build and Install Adj LSA already scheduled\n");
533 if ( nlsr->detailed_logging )
534 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Build and Install Adj LSA already scheduled\n");
akmhoque53f64222012-09-05 13:57:51 -0500535 }
536
akmhoque03004e62012-09-06 01:12:28 -0500537
akmhoque53f64222012-09-05 13:57:51 -0500538 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500539
akmhoque53f64222012-09-05 13:57:51 -0500540
541}
542
akmhoque03004e62012-09-06 01:12:28 -0500543
akmhoque53f64222012-09-05 13:57:51 -0500544
akmhoque03004e62012-09-06 01:12:28 -0500545
akmhoque53f64222012-09-05 13:57:51 -0500546void
547process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
548{
akmhoque3171d652012-11-13 11:44:33 -0600549
550
551 if ( nlsr->debugging )
552 printf("process_incoming_timed_out_interest called \n");
553 if ( nlsr->detailed_logging )
554 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest called \n");
555
akmhoque53f64222012-09-05 13:57:51 -0500556 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 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,nlsr_position+1,"info") == 0)
571 {
572 process_incoming_timed_out_interest_info(selfp,info);
573 }
574}
575
576void
577process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct ccn_upcall_info* info)
578{
akmhoque3171d652012-11-13 11:44:33 -0600579
580 if ( nlsr->debugging )
581 printf("process_incoming_timed_out_interest_info called \n");
582 if ( nlsr->detailed_logging )
583 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500584
585 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
586 get_nbr(nbr,selfp,info);
587
akmhoque3171d652012-11-13 11:44:33 -0600588 if ( nlsr->debugging )
589 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
590 if ( nlsr->detailed_logging )
591 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
592
akmhoque03004e62012-09-06 01:12:28 -0500593
594
akmhoque53f64222012-09-05 13:57:51 -0500595 update_adjacent_timed_out_to_adl(nbr,1);
596 print_adjacent_from_adl();
597 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500598
akmhoque3171d652012-11-13 11:44:33 -0600599 if ( nlsr->debugging )
600 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
601 if ( nlsr->detailed_logging )
602 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
603
akmhoque53f64222012-09-05 13:57:51 -0500604
605 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
606 {
akmhoque53f64222012-09-05 13:57:51 -0500607 send_info_interest_to_neighbor(nbr);
608 }
609 else
akmhoque3171d652012-11-13 11:44:33 -0600610 {
akmhoque53f64222012-09-05 13:57:51 -0500611 update_adjacent_status_to_adl(nbr,NBR_DOWN);
612 if(!nlsr->is_build_adj_lsa_sheduled)
613 {
614 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
615 nlsr->is_build_adj_lsa_sheduled=1;
616 }
617 }
618
619 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500620
akmhoque53f64222012-09-05 13:57:51 -0500621
622}
623
akmhoque29c1db52012-09-07 14:47:43 -0500624
akmhoque03004e62012-09-06 01:12:28 -0500625int
akmhoque53f64222012-09-05 13:57:51 -0500626send_info_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
627{
akmhoqueffacaa82012-09-13 17:48:30 -0500628 if(flags == CCN_SCHEDULE_CANCEL)
629 {
630 return -1;
631 }
632
633 nlsr_lock();
634
akmhoque3171d652012-11-13 11:44:33 -0600635 if ( nlsr->debugging )
636 printf("send_info_interest called \n");
637 if ( nlsr->detailed_logging )
638 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
639
640 if ( nlsr->debugging )
641 printf("\n");
642 if ( nlsr->detailed_logging )
643 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoqued79438d2012-08-27 13:31:42 -0500644
akmhoque53f64222012-09-05 13:57:51 -0500645 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500646 struct ndn_neighbor *nbr;
647
648 struct hashtb_enumerator ee;
649 struct hashtb_enumerator *e = &ee;
650
651 hashtb_start(nlsr->adl, e);
652 adl_element=hashtb_n(nlsr->adl);
653
654 for(i=0;i<adl_element;i++)
655 {
656 nbr=e->data;
657 send_info_interest_to_neighbor(nbr->neighbor);
658 hashtb_next(e);
659 }
akmhoque53f64222012-09-05 13:57:51 -0500660 hashtb_end(e);
661
akmhoqueffacaa82012-09-13 17:48:30 -0500662 nlsr_unlock();
663
akmhoque9fa58a82012-10-05 07:56:02 -0500664 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &send_info_interest, NULL, 0);
665
akmhoque53f64222012-09-05 13:57:51 -0500666 return 0;
667}
668
669void
670send_info_interest_to_neighbor(struct name_prefix *nbr)
671{
akmhoque3171d652012-11-13 11:44:33 -0600672
673 if ( nlsr->debugging )
674 printf("send_info_interest_to_neighbor called \n");
675 if ( nlsr->detailed_logging )
676 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor called \n");
677
akmhoqued79438d2012-08-27 13:31:42 -0500678
679 int res;
akmhoque53f64222012-09-05 13:57:51 -0500680 char info_str[5];
681 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500682
akmhoqued79438d2012-08-27 13:31:42 -0500683 memset(&nlsr_str,0,5);
684 sprintf(nlsr_str,"nlsr");
685 memset(&info_str,0,5);
686 sprintf(info_str,"info");
687
akmhoque53f64222012-09-05 13:57:51 -0500688
689 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500690 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500691
akmhoque03004e62012-09-06 01:12:28 -0500692 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+strlen(nlsr->router_name)+1);
693 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 -0500694 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
695 memcpy(int_name+strlen(int_name),"/",1);
696 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
697 memcpy(int_name+strlen(int_name),"/",1);
698 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500699 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500700
701
702 res=ccn_name_from_uri(name,int_name);
703 if ( res >=0 )
704 {
akmhoque53f64222012-09-05 13:57:51 -0500705 /* adding InterestLifeTime and InterestScope filter */
706
707 struct ccn_charbuf *templ;
708 templ = ccn_charbuf_create();
709
710 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
711 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
712 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500713 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
714 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
akmhoqueb77b95f2013-02-08 12:28:47 -0600715 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque03004e62012-09-06 01:12:28 -0500716 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2 (not further than next host)
717 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500718
719 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqueb77b95f2013-02-08 12:28:47 -0600720 unsigned int face_id=get_next_hop_face_from_adl(nbr->name);
721 ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face_id);
akmhoque53f64222012-09-05 13:57:51 -0500722 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoqueb77b95f2013-02-08 12:28:47 -0600723
akmhoque53f64222012-09-05 13:57:51 -0500724
akmhoque3171d652012-11-13 11:44:33 -0600725 if ( nlsr->debugging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600726 printf("Sending info interest on name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque3171d652012-11-13 11:44:33 -0600727 if ( nlsr->detailed_logging )
akmhoqueb77b95f2013-02-08 12:28:47 -0600728 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 -0500729
730 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
731
732 if ( res >= 0 )
akmhoque3171d652012-11-13 11:44:33 -0600733 {
734 if ( nlsr->debugging )
735 printf("Info interest sending Successfull .... \n");
736 if ( nlsr->detailed_logging )
737 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info interest sending Successfull .... \n");
738 }
akmhoque53f64222012-09-05 13:57:51 -0500739 ccn_charbuf_destroy(&templ);
740 }
741 ccn_charbuf_destroy(&name);
742 free(int_name);
akmhoquec06dcf12013-02-20 08:13:37 -0600743}
akmhoque53f64222012-09-05 13:57:51 -0500744
akmhoquec06dcf12013-02-20 08:13:37 -0600745int
746contain_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
747{
748 if (pco->offset[CCN_PCO_B_KeyLocator] == pco->offset[CCN_PCO_E_KeyLocator])
749 return -1;
750
751 struct ccn_buf_decoder decoder;
752 struct ccn_buf_decoder *d;
753 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]);
754 if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName))
755 return 1;
756
757 return -1;
758}
759
760struct ccn_charbuf *
761get_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
762{
763 struct ccn_charbuf *key_name = ccn_charbuf_create();
764 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]);
765
766 return key_name;
akmhoque53f64222012-09-05 13:57:51 -0500767}