blob: 3a724d5181127e1a9bacd34215a130ee1928123b [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"
akmhoqueedb68d92013-03-05 10:18:16 -060026#include "nlsr_km.h"
27#include "nlsr_km_util.h"
akmhoque53f64222012-09-05 13:57:51 -050028
akmhoqueedb68d92013-03-05 10:18:16 -060029
akmhoque59980a52012-08-09 12:36:09 -050030
akmhoque3d319d42013-02-20 11:08:32 -060031/**
32* get neighbor name prefix from interest/content name and put into nbr
33*/
akmhoque53f64222012-09-05 13:57:51 -050034
35void
akmhoque3d319d42013-02-20 11:08:32 -060036get_nbr(struct name_prefix *nbr,struct ccn_closure *selfp,
37 struct ccn_upcall_info *info)
akmhoque53f64222012-09-05 13:57:51 -050038{
akmhoque7b791452012-10-30 11:24:56 -050039 if ( nlsr->debugging )
40 printf("get_nbr called\n");
41 if ( nlsr->detailed_logging )
42 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_nbr called\n");
akmhoque53f64222012-09-05 13:57:51 -050043
akmhoque53f64222012-09-05 13:57:51 -050044 int res,i;
45 int nlsr_position=0;
46 int name_comps=(int)info->interest_comps->n;
47 int len=0;
48
49 for(i=0;i<name_comps;i++)
50 {
51 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
52 if( res == 0)
53 {
54 nlsr_position=i;
55 break;
56 }
57 }
58
59
60 const unsigned char *comp_ptr1;
61 size_t comp_size;
62 for(i=0;i<nlsr_position;i++)
63 {
akmhoque3d319d42013-02-20 11:08:32 -060064 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&
65 comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -050066 len+=1;
67 len+=(int)comp_size;
68 }
69 len++;
70
71 char *neighbor=(char *)malloc(len);
72 memset(neighbor,0,len);
73
74 for(i=0; i<nlsr_position;i++)
75 {
akmhoque3d319d42013-02-20 11:08:32 -060076 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,
77 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -050078 memcpy(neighbor+strlen(neighbor),"/",1);
79 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
80
81 }
akmhoque53f64222012-09-05 13:57:51 -050082
akmhoque284056b2013-03-15 01:20:16 -050083 nbr->name=(char *)calloc(strlen(neighbor)+1,sizeof(char));
akmhoque53f64222012-09-05 13:57:51 -050084 memcpy(nbr->name,neighbor,strlen(neighbor)+1);
85 nbr->length=strlen(neighbor)+1;
86
akmhoque7b791452012-10-30 11:24:56 -050087 if ( nlsr->debugging )
88 printf("Neighbor: %s Length: %d\n",nbr->name,nbr->length);
89 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -060090 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Length: %d\n",
91 nbr->name,nbr->length);
akmhoquececba942013-02-25 17:33:34 -060092 free(neighbor);
akmhoque53f64222012-09-05 13:57:51 -050093}
94
akmhoque3d319d42013-02-20 11:08:32 -060095/**
96* Retrieve LSA identifier from content name
97*/
akmhoque53f64222012-09-05 13:57:51 -050098
akmhoque3d319d42013-02-20 11:08:32 -060099void
100get_lsa_identifier(struct name_prefix *lsaId,struct ccn_closure *selfp,
101 struct ccn_upcall_info *info, int offset)
102{
akmhoque7b791452012-10-30 11:24:56 -0500103
104 if ( nlsr->debugging )
105 printf("get_lsa_identifier called\n");
106 if ( nlsr->detailed_logging )
107 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_lsa_identifier called\n");
108
akmhoque53f64222012-09-05 13:57:51 -0500109 int res,i;
110 int nlsr_position=0;
111 int name_comps=(int)info->interest_comps->n;
112 int len=0;
113
114 for(i=0;i<name_comps;i++)
115 {
116 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
117 if( res == 0)
118 {
119 nlsr_position=i;
120 break;
121 }
122 }
123
124
125 const unsigned char *comp_ptr1;
126 size_t comp_size;
akmhoque03004e62012-09-06 01:12:28 -0500127 for(i=nlsr_position+3+offset;i<info->interest_comps->n-1;i++)
akmhoque53f64222012-09-05 13:57:51 -0500128 {
akmhoque3d319d42013-02-20 11:08:32 -0600129 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,
130 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500131 len+=1;
132 len+=(int)comp_size;
133 }
134 len++;
135
akmhoquececba942013-02-25 17:33:34 -0600136 char *neighbor=(char *)calloc(len,sizeof(char));
akmhoque53f64222012-09-05 13:57:51 -0500137
akmhoque03004e62012-09-06 01:12:28 -0500138 for(i=nlsr_position+3+offset; i<info->interest_comps->n-1;i++)
akmhoque53f64222012-09-05 13:57:51 -0500139 {
akmhoque3d319d42013-02-20 11:08:32 -0600140 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,
141 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500142 memcpy(neighbor+strlen(neighbor),"/",1);
143 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
144
145 }
akmhoque53f64222012-09-05 13:57:51 -0500146
akmhoque83a94da2013-03-15 01:38:21 -0500147 lsaId->name=(char *)calloc(strlen(neighbor)+1,sizeof(char));
akmhoque53f64222012-09-05 13:57:51 -0500148 memcpy(lsaId->name,neighbor,strlen(neighbor)+1);
149 lsaId->length=strlen(neighbor)+1;
150
akmhoque7b791452012-10-30 11:24:56 -0500151 if ( nlsr->debugging )
152 printf("LSA Identifier: %s Length: %d\n",lsaId->name,lsaId->length-1);
153 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600154 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Identifier: %s Length: "
155 "%d\n",lsaId->name,lsaId->length-1);
akmhoque03004e62012-09-06 01:12:28 -0500156
akmhoquececba942013-02-25 17:33:34 -0600157 free(neighbor);
akmhoque03004e62012-09-06 01:12:28 -0500158}
159
160
akmhoque3d319d42013-02-20 11:08:32 -0600161
162/**
163* Call back function registered in ccnd to get all interest coming to NLSR
164* application
165*/
akmhoque03004e62012-09-06 01:12:28 -0500166
akmhoque59980a52012-08-09 12:36:09 -0500167enum ccn_upcall_res
168incoming_interest(struct ccn_closure *selfp,
169 enum ccn_upcall_kind kind, struct ccn_upcall_info *info)
170{
akmhoqueffacaa82012-09-13 17:48:30 -0500171
172 nlsr_lock();
173
akmhoque59980a52012-08-09 12:36:09 -0500174 switch (kind) {
175 case CCN_UPCALL_FINAL:
176 break;
177 case CCN_UPCALL_INTEREST:
akmhoque03004e62012-09-06 01:12:28 -0500178 // printing the name prefix for which it received interest
akmhoque7b791452012-10-30 11:24:56 -0500179 if ( nlsr->debugging )
180 printf("Interest Received for name: ");
181 if ( nlsr->detailed_logging )
182 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Received for name: ");
183
akmhoque03004e62012-09-06 01:12:28 -0500184 struct ccn_charbuf*c;
185 c=ccn_charbuf_create();
186 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
akmhoque7b791452012-10-30 11:24:56 -0500187
188 if ( nlsr->debugging )
189 printf("%s\n",ccn_charbuf_as_string(c));
190 if ( nlsr->detailed_logging )
191 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
192
akmhoque03004e62012-09-06 01:12:28 -0500193 ccn_charbuf_destroy(&c);
194
akmhoque1c9b92f2012-08-13 10:57:50 -0500195 process_incoming_interest(selfp, info);
akmhoque03004e62012-09-06 01:12:28 -0500196
akmhoque59980a52012-08-09 12:36:09 -0500197 break;
akmhoque03004e62012-09-06 01:12:28 -0500198
akmhoque59980a52012-08-09 12:36:09 -0500199 default:
200 break;
201 }
akmhoque03004e62012-09-06 01:12:28 -0500202
akmhoqueffacaa82012-09-13 17:48:30 -0500203 nlsr_unlock();
204
akmhoque59980a52012-08-09 12:36:09 -0500205 return CCN_UPCALL_RESULT_OK;
206}
207
akmhoque3d319d42013-02-20 11:08:32 -0600208/**
209* Function for processing incoming interest and reply with content/NACK content
210*/
akmhoque59980a52012-08-09 12:36:09 -0500211
akmhoqued79438d2012-08-27 13:31:42 -0500212void
akmhoque1c9b92f2012-08-13 10:57:50 -0500213process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
214{
akmhoque7b791452012-10-30 11:24:56 -0500215 if ( nlsr->debugging )
216 printf("process_incoming_interest called \n");
217 if ( nlsr->detailed_logging )
218 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest called \n");
219
akmhoque1c9b92f2012-08-13 10:57:50 -0500220 const unsigned char *comp_ptr1;
221 size_t comp_size;
222 int res,i;
223 int nlsr_position=0;
224 int name_comps=(int)info->interest_comps->n;
akmhoque53f64222012-09-05 13:57:51 -0500225
akmhoque1c9b92f2012-08-13 10:57:50 -0500226 for(i=0;i<name_comps;i++)
227 {
228 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
229 if( res == 0)
230 {
akmhoqueea3603e2012-08-13 11:24:09 -0500231 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500232 break;
233 }
234 }
235
akmhoque3d319d42013-02-20 11:08:32 -0600236 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,
237 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500238
akmhoque1c9b92f2012-08-13 10:57:50 -0500239
akmhoqued79438d2012-08-27 13:31:42 -0500240 if(!strcmp((char *)comp_ptr1,"info"))
241 {
242 process_incoming_interest_info(selfp,info);
243 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600244
akmhoque53f64222012-09-05 13:57:51 -0500245}
246
akmhoque3d319d42013-02-20 11:08:32 -0600247/**
akmhoque7ab49a32013-02-20 11:27:51 -0600248* Processes incoming interest for "info" interest. Send back reply content back,
249* if interest comes from a neighbor with status down, NLSR will send "info"
250* ineterst to that neighbor
akmhoque3d319d42013-02-20 11:08:32 -0600251*/
252
akmhoque53f64222012-09-05 13:57:51 -0500253void
254process_incoming_interest_info(struct ccn_closure *selfp, struct ccn_upcall_info *info)
255{
akmhoque7b791452012-10-30 11:24:56 -0500256 if ( nlsr->debugging )
257 {
258 printf("process_incoming_interest_info called \n");
259 printf("Sending Info Content back.....\n");
260 }
261 if ( nlsr->detailed_logging )
262 {
akmhoque3d319d42013-02-20 11:08:32 -0600263 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest_info"
264 " called \n");
akmhoque7b791452012-10-30 11:24:56 -0500265 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending Info Content back.....\n");
266 }
267
akmhoque53f64222012-09-05 13:57:51 -0500268
akmhoque53f64222012-09-05 13:57:51 -0500269 int res;
akmhoque6e2ba842013-03-05 19:35:26 -0600270 //struct ccn_charbuf *data=ccn_charbuf_create();
akmhoque53f64222012-09-05 13:57:51 -0500271 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoquedd7a7a72013-02-20 08:25:41 -0600272
akmhoque03004e62012-09-06 01:12:28 -0500273
akmhoque3d319d42013-02-20 11:08:32 -0600274 res=ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],
275 info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
akmhoque53f64222012-09-05 13:57:51 -0500276 if (res >= 0)
277 {
akmhoquedd7a7a72013-02-20 08:25:41 -0600278
akmhoque6e2ba842013-03-05 19:35:26 -0600279 /*
akmhoquec06dcf12013-02-20 08:13:37 -0600280 struct ccn_charbuf *pubid = ccn_charbuf_create();
281 struct ccn_charbuf *pubkey = ccn_charbuf_create();
282
akmhoque7ca519b2013-02-20 09:49:43 -0600283 //pubid is the digest_result pubkey is result
284 ccn_get_public_key(nlsr->ccn, NULL, pubid, pubkey);
285
286
akmhoquedd7a7a72013-02-20 08:25:41 -0600287
288 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
289 sp.template_ccnb=ccn_charbuf_create();
akmhoque3d319d42013-02-20 11:08:32 -0600290
akmhoqueb63a41e2013-03-01 12:00:20 -0600291 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
akmhoque3d319d42013-02-20 11:08:32 -0600292 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
293 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
294 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoqueb63a41e2013-03-01 12:00:20 -0600295
akmhoque53f64222012-09-05 13:57:51 -0500296
akmhoqueb77b95f2013-02-08 12:28:47 -0600297 char *raw_data=(char *)malloc(20);
298 memset(raw_data,0,20);
akmhoque6e2ba842013-03-05 19:35:26 -0600299 sprintf(raw_data,"%s", nlsr->lsdb->lsdb_version);
300 */
akmhoque53f64222012-09-05 13:57:51 -0500301
akmhoque6e2ba842013-03-05 19:35:26 -0600302 struct ccn_charbuf *resultbuf=ccn_charbuf_create();
303
304 res=sign_content_with_user_defined_keystore(name,
305 resultbuf,
306 "info",
307 strlen("info"),
308 nlsr->keystore_path,
309 nlsr->keystore_passphrase,
310 nlsr->root_key_prefix,
311 nlsr->site_name,
312 nlsr->router_name);
313
314
315 //res= ccn_sign_content(nlsr->ccn, data, name, &sp, "info",strlen("info"));
akmhoque53f64222012-09-05 13:57:51 -0500316 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500317 {
318 if ( nlsr->debugging )
319 printf("Signing info Content is successful \n");
320 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600321 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Signing info Content"
322 " is successful \n");
akmhoque53f64222012-09-05 13:57:51 -0500323
akmhoque7b791452012-10-30 11:24:56 -0500324 }
akmhoque6e2ba842013-03-05 19:35:26 -0600325 res=ccn_put(nlsr->ccn,resultbuf->buf,resultbuf->length);
akmhoque53f64222012-09-05 13:57:51 -0500326 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500327 {
328 if ( nlsr->debugging )
329 printf("Sending Info Content is successful \n");
330 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600331 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info Content"
332 " is successful \n");
akmhoque7b791452012-10-30 11:24:56 -0500333 }
334
akmhoque53f64222012-09-05 13:57:51 -0500335
336
akmhoquee1dd7772013-02-24 14:21:49 -0600337 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix));
akmhoque03004e62012-09-06 01:12:28 -0500338 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque7b791452012-10-30 11:24:56 -0500339
340 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600341 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,
342 get_adjacent_status(nbr));
akmhoque7b791452012-10-30 11:24:56 -0500343 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600344 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor : %s Length : %d"
345 " Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500346
akmhoque53f64222012-09-05 13:57:51 -0500347
akmhoque3d319d42013-02-20 11:08:32 -0600348 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr) >=
349 nlsr->interest_retry )
akmhoque53f64222012-09-05 13:57:51 -0500350 {
akmhoqueb8195202012-09-25 11:53:23 -0500351 update_adjacent_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500352 send_info_interest_to_neighbor(nbr);
353 }
354
akmhoque83a94da2013-03-15 01:38:21 -0500355 if ( nbr->name != NULL )
356 free(nbr->name);
357 if ( nbr != NULL )
358 free(nbr);
akmhoque284056b2013-03-15 01:20:16 -0500359 ccn_charbuf_destroy(&resultbuf);
akmhoque53f64222012-09-05 13:57:51 -0500360 }
akmhoque03004e62012-09-06 01:12:28 -0500361
akmhoque6e2ba842013-03-05 19:35:26 -0600362 //ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500363 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500364
akmhoquebf1aa832012-08-13 13:26:59 -0500365}
366
367
akmhoque7ab49a32013-02-20 11:27:51 -0600368/**
369* Call back function registered in ccnd to get all content coming to NLSR
370* application
371*/
akmhoque03004e62012-09-06 01:12:28 -0500372
akmhoque53f64222012-09-05 13:57:51 -0500373enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
374 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500375{
376
akmhoqueffacaa82012-09-13 17:48:30 -0500377 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500378
akmhoque53f64222012-09-05 13:57:51 -0500379 switch(kind) {
380 case CCN_UPCALL_FINAL:
381 break;
382 case CCN_UPCALL_CONTENT:
akmhoque7b791452012-10-30 11:24:56 -0500383 if ( nlsr->debugging )
384 printf("Content Received for Name: ");
385 if ( nlsr->detailed_logging )
386 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Content Received for Name: ");
387
akmhoque03004e62012-09-06 01:12:28 -0500388 struct ccn_charbuf*c;
389 c=ccn_charbuf_create();
390 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500391 if ( nlsr->debugging )
392 printf("%s\n",ccn_charbuf_as_string(c));
393 if ( nlsr->detailed_logging )
394 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
395
akmhoque03004e62012-09-06 01:12:28 -0500396 ccn_charbuf_destroy(&c);
397
398 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500399
akmhoque53f64222012-09-05 13:57:51 -0500400 break;
401 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoqueedb68d92013-03-05 10:18:16 -0600402 //printf("Interest Timed Out Received for Name: ");
403 if ( nlsr->debugging )
404 printf("Interest Timed Out Received for Name: ");
405 if ( nlsr->detailed_logging )
406 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed Out Receiv"
akmhoque3d319d42013-02-20 11:08:32 -0600407 "ed for Name: ");
akmhoque03004e62012-09-06 01:12:28 -0500408
akmhoqueedb68d92013-03-05 10:18:16 -0600409 struct ccn_charbuf*ito;
410 ito=ccn_charbuf_create();
411 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
412 if ( nlsr->debugging )
413 printf("%s\n",ccn_charbuf_as_string(ito));
414 if ( nlsr->detailed_logging )
415 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(ito));
416 ccn_charbuf_destroy(&ito);
akmhoque03004e62012-09-06 01:12:28 -0500417
akmhoqueedb68d92013-03-05 10:18:16 -0600418 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500419
akmhoque53f64222012-09-05 13:57:51 -0500420 break;
akmhoqueedb68d92013-03-05 10:18:16 -0600421
422 case CCN_UPCALL_CONTENT_UNVERIFIED:
423 if ( nlsr->debugging )
424 printf("Unverified Content Received ..Waiting for verification\n");
425 if ( nlsr->detailed_logging )
426 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unverified Content"
427 " Received ..Waiting for verification\n");
akmhoque6e2ba842013-03-05 19:35:26 -0600428 //return CCN_UPCALL_RESULT_VERIFY;
akmhoque4637d9d2013-03-08 06:52:00 -0600429 process_incoming_content(selfp,info);
akmhoqueedb68d92013-03-05 10:18:16 -0600430 break;
431
akmhoque53f64222012-09-05 13:57:51 -0500432 default:
433 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoqueedb68d92013-03-05 10:18:16 -0600434 if ( nlsr->debugging )
435 printf("Unexpected response of kind %d\n", kind);
436 if ( nlsr->detailed_logging )
437 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unexpected response of "
akmhoque3d319d42013-02-20 11:08:32 -0600438 "kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500439 break;
akmhoque53f64222012-09-05 13:57:51 -0500440 }
akmhoqueffacaa82012-09-13 17:48:30 -0500441
442 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500443
akmhoque53f64222012-09-05 13:57:51 -0500444 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500445}
446
akmhoque7ab49a32013-02-20 11:27:51 -0600447/**
448* process any incoming content to NLSR from ccnd
449*/
akmhoque03004e62012-09-06 01:12:28 -0500450
akmhoqued79438d2012-08-27 13:31:42 -0500451void
akmhoque53f64222012-09-05 13:57:51 -0500452process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500453{
akmhoque7b791452012-10-30 11:24:56 -0500454 if ( nlsr->debugging )
455 printf("process_incoming_content called \n");
456 if ( nlsr->detailed_logging )
457 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500458
459 const unsigned char *comp_ptr1;
460 size_t comp_size;
461 int res,i;
462 int nlsr_position=0;
463 int name_comps=(int)info->interest_comps->n;
464
465 for(i=0;i<name_comps;i++)
466 {
467 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
468 if( res == 0)
469 {
470 nlsr_position=i;
471 break;
472 }
473 }
474
akmhoque3d319d42013-02-20 11:08:32 -0600475 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,
476 nlsr_position+1,&comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500477
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
akmhoque7ab49a32013-02-20 11:27:51 -0600486/**
487* process any incoming "info" content to NLSR from ccnd
488*/
akmhoque03004e62012-09-06 01:12:28 -0500489
akmhoque53f64222012-09-05 13:57:51 -0500490void
akmhoque3d319d42013-02-20 11:08:32 -0600491process_incoming_content_info(struct ccn_closure *selfp,
492 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500493{
akmhoque7b791452012-10-30 11:24:56 -0500494 if ( nlsr->debugging )
495 printf("process_incoming_content_info called \n");
496 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600497 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_info"
498 " 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 )
akmhoque3d319d42013-02-20 11:08:32 -0600504 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,
505 nbr->length);
akmhoque7b791452012-10-30 11:24:56 -0500506 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600507 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Content Received For Nei"
508 "ghbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500509
akmhoquefc7016f2013-03-08 07:17:14 -0600510
akmhoque53f64222012-09-05 13:57:51 -0500511
akmhoque6e2ba842013-03-05 19:35:26 -0600512 if ( contain_key_name(info->content_ccnb, info->pco) == 1){
akmhoque6e2ba842013-03-05 19:35:26 -0600513
akmhoqueb7958182013-03-11 12:03:54 -0500514 int res_verify=verify_key(info->content_ccnb,info->pco,0);
akmhoque03004e62012-09-06 01:12:28 -0500515
akmhoqueb7958182013-03-11 12:03:54 -0500516 if ( res_verify != 0 ){
akmhoque1162d852013-03-15 01:07:00 -0500517 if ( nlsr->debugging )
518 printf("Error in verfiying keys !! :( \n");
akmhoqueb7958182013-03-11 12:03:54 -0500519 }
520 else{
akmhoque1162d852013-03-15 01:07:00 -0500521 if ( nlsr->debugging )
522 printf("Key verification is successful :)\n");
akmhoqueb7958182013-03-11 12:03:54 -0500523 update_adjacent_timed_out_zero_to_adl(nbr);
524 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
525 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500526
akmhoqueb7958182013-03-11 12:03:54 -0500527 if(!nlsr->is_build_adj_lsa_sheduled){
528 if ( nlsr->debugging )
529 printf("Scheduling Build and Install Adj LSA...\n");
530 if ( nlsr->detailed_logging )
531 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Scheduling"
akmhoque6e2ba842013-03-05 19:35:26 -0600532 "Build and Install Adj LSA...\n");
akmhoqueb7958182013-03-11 12:03:54 -0500533 nlsr->event_build_adj_lsa = ccn_schedule_event(
akmhoque6e2ba842013-03-05 19:35:26 -0600534 nlsr->sched, 100000,
akmhoque3d319d42013-02-20 11:08:32 -0600535 &build_and_install_adj_lsa, NULL, 0);
akmhoqueb7958182013-03-11 12:03:54 -0500536 nlsr->is_build_adj_lsa_sheduled=1;
537 }
538 else{
539 if ( nlsr->debugging )
540 printf("Build and Install Adj LSA already scheduled\n");
541 if ( nlsr->detailed_logging )
542 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Build and Install Adj LSA"
akmhoque3d319d42013-02-20 11:08:32 -0600543 " already scheduled\n");
akmhoqueb7958182013-03-11 12:03:54 -0500544 }
akmhoque6e2ba842013-03-05 19:35:26 -0600545
akmhoqueb7958182013-03-11 12:03:54 -0500546 }
akmhoque53f64222012-09-05 13:57:51 -0500547 }
akmhoquefc7016f2013-03-08 07:17:14 -0600548 /*
akmhoque10c9a212013-03-08 07:05:18 -0600549 update_adjacent_timed_out_zero_to_adl(nbr);
550 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
551 print_adjacent_from_adl();
552
553 if(!nlsr->is_build_adj_lsa_sheduled){
554 if ( nlsr->debugging )
555 printf("Scheduling Build and Install Adj LSA...\n");
556 if ( nlsr->detailed_logging )
557 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Scheduling"
558 "Build and Install Adj LSA...\n");
559 nlsr->event_build_adj_lsa = ccn_schedule_event(
560 nlsr->sched, 100000,
561 &build_and_install_adj_lsa, NULL, 0);
562 nlsr->is_build_adj_lsa_sheduled=1;
563 }
564 else{
565 if ( nlsr->debugging )
566 printf("Build and Install Adj LSA already scheduled\n");
567 if ( nlsr->detailed_logging )
568 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Build and Install Adj LSA"
569 " already scheduled\n");
570 }
akmhoque03004e62012-09-06 01:12:28 -0500571
akmhoquefc7016f2013-03-08 07:17:14 -0600572 */
akmhoque83a94da2013-03-15 01:38:21 -0500573 if ( nbr->name != NULL )
574 free(nbr->name);
575 if ( nbr != NULL )
576 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500577
akmhoque53f64222012-09-05 13:57:51 -0500578
579}
580
akmhoque7ab49a32013-02-20 11:27:51 -0600581/**
582* process any incoming interest timed out content to NLSR from ccnd
583*/
akmhoque53f64222012-09-05 13:57:51 -0500584
akmhoque03004e62012-09-06 01:12:28 -0500585
akmhoque53f64222012-09-05 13:57:51 -0500586void
akmhoque3d319d42013-02-20 11:08:32 -0600587process_incoming_timed_out_interest(struct ccn_closure* selfp,
588 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500589{
akmhoque3171d652012-11-13 11:44:33 -0600590
591
592 if ( nlsr->debugging )
593 printf("process_incoming_timed_out_interest called \n");
594 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600595 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
596 "erest called \n");
akmhoque3171d652012-11-13 11:44:33 -0600597
akmhoque53f64222012-09-05 13:57:51 -0500598 int res,i;
599 int nlsr_position=0;
600 int name_comps=(int)info->interest_comps->n;
601
602 for(i=0;i<name_comps;i++)
603 {
604 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
605 if( res == 0)
606 {
607 nlsr_position=i;
608 break;
609 }
610 }
611
akmhoque3d319d42013-02-20 11:08:32 -0600612 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,
613 nlsr_position+1,"info") == 0)
akmhoque53f64222012-09-05 13:57:51 -0500614 {
615 process_incoming_timed_out_interest_info(selfp,info);
616 }
617}
618
akmhoque7ab49a32013-02-20 11:27:51 -0600619/**
620* process any incoming "info" interest timed out content to NLSR from ccnd
621*/
622
akmhoque53f64222012-09-05 13:57:51 -0500623void
akmhoque3d319d42013-02-20 11:08:32 -0600624process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct
625 ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500626{
akmhoque3171d652012-11-13 11:44:33 -0600627
628 if ( nlsr->debugging )
629 printf("process_incoming_timed_out_interest_info called \n");
630 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600631 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
632 "erest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500633
634 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
635 get_nbr(nbr,selfp,info);
636
akmhoque3171d652012-11-13 11:44:33 -0600637 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600638 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",
639 nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600640 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600641 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for"
642 " Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600643
akmhoque03004e62012-09-06 01:12:28 -0500644
645
akmhoque53f64222012-09-05 13:57:51 -0500646 update_adjacent_timed_out_to_adl(nbr,1);
647 print_adjacent_from_adl();
648 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500649
akmhoque3171d652012-11-13 11:44:33 -0600650 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600651 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,
652 timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600653 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600654 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest "
655 "Timed Out: %d times\n",nbr->name,timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600656
akmhoque53f64222012-09-05 13:57:51 -0500657
658 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
659 {
akmhoque53f64222012-09-05 13:57:51 -0500660 send_info_interest_to_neighbor(nbr);
661 }
662 else
akmhoque3171d652012-11-13 11:44:33 -0600663 {
akmhoque53f64222012-09-05 13:57:51 -0500664 update_adjacent_status_to_adl(nbr,NBR_DOWN);
665 if(!nlsr->is_build_adj_lsa_sheduled)
666 {
akmhoque3d319d42013-02-20 11:08:32 -0600667 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000,
668 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500669 nlsr->is_build_adj_lsa_sheduled=1;
670 }
671 }
672
akmhoque284056b2013-03-15 01:20:16 -0500673 if ( nbr->name != NULL )
674 free(nbr->name);
675 if ( nbr != NULL )
676 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500677
akmhoque53f64222012-09-05 13:57:51 -0500678
679}
680
akmhoque7ab49a32013-02-20 11:27:51 -0600681/**
682* send "info" interest to each and every neighbor in ADL and also schedule for
683* itself for periodical sending of "info" interest
684*/
akmhoque29c1db52012-09-07 14:47:43 -0500685
akmhoque03004e62012-09-06 01:12:28 -0500686int
akmhoque3d319d42013-02-20 11:08:32 -0600687send_info_interest(struct ccn_schedule *sched, void *clienth,
688 struct ccn_scheduled_event *ev, int flags)
akmhoque53f64222012-09-05 13:57:51 -0500689{
akmhoqueffacaa82012-09-13 17:48:30 -0500690 if(flags == CCN_SCHEDULE_CANCEL)
691 {
692 return -1;
693 }
694
695 nlsr_lock();
696
akmhoque3171d652012-11-13 11:44:33 -0600697 if ( nlsr->debugging )
698 printf("send_info_interest called \n");
699 if ( nlsr->detailed_logging )
700 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
701
702 if ( nlsr->debugging )
703 printf("\n");
704 if ( nlsr->detailed_logging )
705 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoqued79438d2012-08-27 13:31:42 -0500706
akmhoque53f64222012-09-05 13:57:51 -0500707 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500708 struct ndn_neighbor *nbr;
709
710 struct hashtb_enumerator ee;
711 struct hashtb_enumerator *e = &ee;
712
713 hashtb_start(nlsr->adl, e);
714 adl_element=hashtb_n(nlsr->adl);
715
716 for(i=0;i<adl_element;i++)
717 {
718 nbr=e->data;
719 send_info_interest_to_neighbor(nbr->neighbor);
720 hashtb_next(e);
721 }
akmhoque53f64222012-09-05 13:57:51 -0500722 hashtb_end(e);
723
akmhoqueffacaa82012-09-13 17:48:30 -0500724 nlsr_unlock();
725
akmhoque3d319d42013-02-20 11:08:32 -0600726 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &send_info_interest,
727 NULL, 0);
akmhoque9fa58a82012-10-05 07:56:02 -0500728
akmhoque53f64222012-09-05 13:57:51 -0500729 return 0;
730}
731
akmhoque7ab49a32013-02-20 11:27:51 -0600732
733/**
734* send "info" interest neighbor nbr
735*
736*/
737
akmhoque53f64222012-09-05 13:57:51 -0500738void
739send_info_interest_to_neighbor(struct name_prefix *nbr)
740{
akmhoque3171d652012-11-13 11:44:33 -0600741
742 if ( nlsr->debugging )
743 printf("send_info_interest_to_neighbor called \n");
744 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600745 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor"
746 " called \n");
akmhoque3171d652012-11-13 11:44:33 -0600747
akmhoqued79438d2012-08-27 13:31:42 -0500748
749 int res;
akmhoque53f64222012-09-05 13:57:51 -0500750 char info_str[5];
751 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500752
akmhoqued79438d2012-08-27 13:31:42 -0500753 memset(&nlsr_str,0,5);
754 sprintf(nlsr_str,"nlsr");
755 memset(&info_str,0,5);
756 sprintf(info_str,"info");
757
akmhoque53f64222012-09-05 13:57:51 -0500758
759 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500760 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500761
akmhoque3d319d42013-02-20 11:08:32 -0600762 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+
763 strlen(info_str)+strlen(nlsr->router_name)+1);
764 memset(int_name,0,strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+
765 strlen(nlsr->router_name)+1);
akmhoque53f64222012-09-05 13:57:51 -0500766 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
767 memcpy(int_name+strlen(int_name),"/",1);
768 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
769 memcpy(int_name+strlen(int_name),"/",1);
770 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500771 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500772
773
774 res=ccn_name_from_uri(name,int_name);
775 if ( res >=0 )
776 {
akmhoque53f64222012-09-05 13:57:51 -0500777 /* adding InterestLifeTime and InterestScope filter */
778
779 struct ccn_charbuf *templ;
780 templ = ccn_charbuf_create();
781
782 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
783 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
784 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500785 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
786 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
akmhoqueb77b95f2013-02-08 12:28:47 -0600787 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque3d319d42013-02-20 11:08:32 -0600788 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2
789 //(not further than next host)
akmhoque03004e62012-09-06 01:12:28 -0500790 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500791
792 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqueb77b95f2013-02-08 12:28:47 -0600793 unsigned int face_id=get_next_hop_face_from_adl(nbr->name);
794 ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face_id);
akmhoque53f64222012-09-05 13:57:51 -0500795 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoqueb77b95f2013-02-08 12:28:47 -0600796
akmhoque53f64222012-09-05 13:57:51 -0500797
akmhoque3171d652012-11-13 11:44:33 -0600798 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600799 printf("Sending info interest on name prefix : %s through Face:%u\n"
800 ,int_name,face_id);
akmhoque3171d652012-11-13 11:44:33 -0600801 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600802 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info interest on"
803 "name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque53f64222012-09-05 13:57:51 -0500804
805 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
806
807 if ( res >= 0 )
akmhoque3171d652012-11-13 11:44:33 -0600808 {
809 if ( nlsr->debugging )
810 printf("Info interest sending Successfull .... \n");
811 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600812 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info"
813 "interest sending Successfull .... \n");
akmhoque3171d652012-11-13 11:44:33 -0600814 }
akmhoque53f64222012-09-05 13:57:51 -0500815 ccn_charbuf_destroy(&templ);
816 }
akmhoque7adb2772013-03-05 16:30:59 -0600817
akmhoque53f64222012-09-05 13:57:51 -0500818 ccn_charbuf_destroy(&name);
819 free(int_name);
akmhoquec06dcf12013-02-20 08:13:37 -0600820}