blob: b43a57dea385241e128c20a95671ef835486b316 [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
83 nbr->name=(char *)malloc(strlen(neighbor)+1);
84 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));
137 //memset(neighbor,0,len);
akmhoque53f64222012-09-05 13:57:51 -0500138
akmhoque03004e62012-09-06 01:12:28 -0500139 for(i=nlsr_position+3+offset; i<info->interest_comps->n-1;i++)
akmhoque53f64222012-09-05 13:57:51 -0500140 {
akmhoque3d319d42013-02-20 11:08:32 -0600141 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,
142 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500143 memcpy(neighbor+strlen(neighbor),"/",1);
144 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
145
146 }
akmhoque53f64222012-09-05 13:57:51 -0500147
148 lsaId->name=(char *)malloc(strlen(neighbor)+1);
149 memset(lsaId->name,0,strlen(neighbor)+1);
150 memcpy(lsaId->name,neighbor,strlen(neighbor)+1);
151 lsaId->length=strlen(neighbor)+1;
152
akmhoque7b791452012-10-30 11:24:56 -0500153 if ( nlsr->debugging )
154 printf("LSA Identifier: %s Length: %d\n",lsaId->name,lsaId->length-1);
155 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600156 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Identifier: %s Length: "
157 "%d\n",lsaId->name,lsaId->length-1);
akmhoque03004e62012-09-06 01:12:28 -0500158
akmhoquececba942013-02-25 17:33:34 -0600159 free(neighbor);
akmhoque03004e62012-09-06 01:12:28 -0500160}
161
162
akmhoque3d319d42013-02-20 11:08:32 -0600163
164/**
165* Call back function registered in ccnd to get all interest coming to NLSR
166* application
167*/
akmhoque03004e62012-09-06 01:12:28 -0500168
akmhoque59980a52012-08-09 12:36:09 -0500169enum ccn_upcall_res
170incoming_interest(struct ccn_closure *selfp,
171 enum ccn_upcall_kind kind, struct ccn_upcall_info *info)
172{
akmhoqueffacaa82012-09-13 17:48:30 -0500173
174 nlsr_lock();
175
akmhoque59980a52012-08-09 12:36:09 -0500176 switch (kind) {
177 case CCN_UPCALL_FINAL:
178 break;
179 case CCN_UPCALL_INTEREST:
akmhoque03004e62012-09-06 01:12:28 -0500180 // printing the name prefix for which it received interest
akmhoque7b791452012-10-30 11:24:56 -0500181 if ( nlsr->debugging )
182 printf("Interest Received for name: ");
183 if ( nlsr->detailed_logging )
184 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Received for name: ");
185
akmhoque03004e62012-09-06 01:12:28 -0500186 struct ccn_charbuf*c;
187 c=ccn_charbuf_create();
188 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
akmhoque7b791452012-10-30 11:24:56 -0500189
190 if ( nlsr->debugging )
191 printf("%s\n",ccn_charbuf_as_string(c));
192 if ( nlsr->detailed_logging )
193 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
194
akmhoque03004e62012-09-06 01:12:28 -0500195 ccn_charbuf_destroy(&c);
196
akmhoque1c9b92f2012-08-13 10:57:50 -0500197 process_incoming_interest(selfp, info);
akmhoque03004e62012-09-06 01:12:28 -0500198
akmhoque59980a52012-08-09 12:36:09 -0500199 break;
akmhoque03004e62012-09-06 01:12:28 -0500200
akmhoque59980a52012-08-09 12:36:09 -0500201 default:
202 break;
203 }
akmhoque03004e62012-09-06 01:12:28 -0500204
akmhoqueffacaa82012-09-13 17:48:30 -0500205 nlsr_unlock();
206
akmhoque59980a52012-08-09 12:36:09 -0500207 return CCN_UPCALL_RESULT_OK;
208}
209
akmhoque3d319d42013-02-20 11:08:32 -0600210/**
211* Function for processing incoming interest and reply with content/NACK content
212*/
akmhoque59980a52012-08-09 12:36:09 -0500213
akmhoqued79438d2012-08-27 13:31:42 -0500214void
akmhoque1c9b92f2012-08-13 10:57:50 -0500215process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
216{
akmhoque7b791452012-10-30 11:24:56 -0500217 if ( nlsr->debugging )
218 printf("process_incoming_interest called \n");
219 if ( nlsr->detailed_logging )
220 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest called \n");
221
akmhoque1c9b92f2012-08-13 10:57:50 -0500222 const unsigned char *comp_ptr1;
223 size_t comp_size;
224 int res,i;
225 int nlsr_position=0;
226 int name_comps=(int)info->interest_comps->n;
akmhoque53f64222012-09-05 13:57:51 -0500227
akmhoque1c9b92f2012-08-13 10:57:50 -0500228 for(i=0;i<name_comps;i++)
229 {
230 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
231 if( res == 0)
232 {
akmhoqueea3603e2012-08-13 11:24:09 -0500233 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500234 break;
235 }
236 }
237
akmhoque3d319d42013-02-20 11:08:32 -0600238 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,
239 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500240
akmhoque1c9b92f2012-08-13 10:57:50 -0500241
akmhoqued79438d2012-08-27 13:31:42 -0500242 if(!strcmp((char *)comp_ptr1,"info"))
243 {
244 process_incoming_interest_info(selfp,info);
245 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600246
akmhoque53f64222012-09-05 13:57:51 -0500247}
248
akmhoque3d319d42013-02-20 11:08:32 -0600249/**
akmhoque7ab49a32013-02-20 11:27:51 -0600250* Processes incoming interest for "info" interest. Send back reply content back,
251* if interest comes from a neighbor with status down, NLSR will send "info"
252* ineterst to that neighbor
akmhoque3d319d42013-02-20 11:08:32 -0600253*/
254
akmhoque53f64222012-09-05 13:57:51 -0500255void
256process_incoming_interest_info(struct ccn_closure *selfp, struct ccn_upcall_info *info)
257{
akmhoque7b791452012-10-30 11:24:56 -0500258 if ( nlsr->debugging )
259 {
260 printf("process_incoming_interest_info called \n");
261 printf("Sending Info Content back.....\n");
262 }
263 if ( nlsr->detailed_logging )
264 {
akmhoque3d319d42013-02-20 11:08:32 -0600265 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest_info"
266 " called \n");
akmhoque7b791452012-10-30 11:24:56 -0500267 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending Info Content back.....\n");
268 }
269
akmhoque53f64222012-09-05 13:57:51 -0500270
akmhoque53f64222012-09-05 13:57:51 -0500271 int res;
akmhoque6e2ba842013-03-05 19:35:26 -0600272 //struct ccn_charbuf *data=ccn_charbuf_create();
akmhoque53f64222012-09-05 13:57:51 -0500273 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoquedd7a7a72013-02-20 08:25:41 -0600274
akmhoque03004e62012-09-06 01:12:28 -0500275
akmhoque3d319d42013-02-20 11:08:32 -0600276 res=ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],
277 info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
akmhoque53f64222012-09-05 13:57:51 -0500278 if (res >= 0)
279 {
akmhoquedd7a7a72013-02-20 08:25:41 -0600280
akmhoque6e2ba842013-03-05 19:35:26 -0600281 /*
akmhoquec06dcf12013-02-20 08:13:37 -0600282 struct ccn_charbuf *pubid = ccn_charbuf_create();
283 struct ccn_charbuf *pubkey = ccn_charbuf_create();
284
akmhoque7ca519b2013-02-20 09:49:43 -0600285 //pubid is the digest_result pubkey is result
286 ccn_get_public_key(nlsr->ccn, NULL, pubid, pubkey);
287
288
akmhoquedd7a7a72013-02-20 08:25:41 -0600289
290 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
291 sp.template_ccnb=ccn_charbuf_create();
akmhoque3d319d42013-02-20 11:08:32 -0600292
akmhoqueb63a41e2013-03-01 12:00:20 -0600293 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
akmhoque3d319d42013-02-20 11:08:32 -0600294 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
295 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
296 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoqueb63a41e2013-03-01 12:00:20 -0600297
akmhoque53f64222012-09-05 13:57:51 -0500298
akmhoqueb77b95f2013-02-08 12:28:47 -0600299 char *raw_data=(char *)malloc(20);
300 memset(raw_data,0,20);
akmhoque6e2ba842013-03-05 19:35:26 -0600301 sprintf(raw_data,"%s", nlsr->lsdb->lsdb_version);
302 */
akmhoque53f64222012-09-05 13:57:51 -0500303
akmhoque6e2ba842013-03-05 19:35:26 -0600304 struct ccn_charbuf *resultbuf=ccn_charbuf_create();
305
306 res=sign_content_with_user_defined_keystore(name,
307 resultbuf,
308 "info",
309 strlen("info"),
310 nlsr->keystore_path,
311 nlsr->keystore_passphrase,
312 nlsr->root_key_prefix,
313 nlsr->site_name,
314 nlsr->router_name);
315
316
317 //res= ccn_sign_content(nlsr->ccn, data, name, &sp, "info",strlen("info"));
akmhoque53f64222012-09-05 13:57:51 -0500318 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500319 {
320 if ( nlsr->debugging )
321 printf("Signing info Content is successful \n");
322 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600323 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Signing info Content"
324 " is successful \n");
akmhoque53f64222012-09-05 13:57:51 -0500325
akmhoque7b791452012-10-30 11:24:56 -0500326 }
akmhoque6e2ba842013-03-05 19:35:26 -0600327 res=ccn_put(nlsr->ccn,resultbuf->buf,resultbuf->length);
akmhoque53f64222012-09-05 13:57:51 -0500328 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500329 {
330 if ( nlsr->debugging )
331 printf("Sending Info Content is successful \n");
332 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600333 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info Content"
334 " is successful \n");
akmhoque7b791452012-10-30 11:24:56 -0500335 }
336
akmhoque53f64222012-09-05 13:57:51 -0500337
338
akmhoquee1dd7772013-02-24 14:21:49 -0600339 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix));
akmhoque03004e62012-09-06 01:12:28 -0500340 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque7b791452012-10-30 11:24:56 -0500341
342 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600343 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,
344 get_adjacent_status(nbr));
akmhoque7b791452012-10-30 11:24:56 -0500345 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600346 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor : %s Length : %d"
347 " Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500348
akmhoque53f64222012-09-05 13:57:51 -0500349
akmhoque3d319d42013-02-20 11:08:32 -0600350 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr) >=
351 nlsr->interest_retry )
akmhoque53f64222012-09-05 13:57:51 -0500352 {
akmhoqueb8195202012-09-25 11:53:23 -0500353 update_adjacent_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500354 send_info_interest_to_neighbor(nbr);
355 }
356
357 free(nbr);
akmhoque6e2ba842013-03-05 19:35:26 -0600358 //free(raw_data);
359 //ccn_charbuf_destroy(&sp.template_ccnb);
360 //ccn_charbuf_destroy(&pubid);
361 //ccn_charbuf_destroy(&pubkey);
akmhoque53f64222012-09-05 13:57:51 -0500362 }
akmhoque03004e62012-09-06 01:12:28 -0500363
akmhoque6e2ba842013-03-05 19:35:26 -0600364 //ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500365 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500366
akmhoquebf1aa832012-08-13 13:26:59 -0500367}
368
369
akmhoque7ab49a32013-02-20 11:27:51 -0600370/**
371* Call back function registered in ccnd to get all content coming to NLSR
372* application
373*/
akmhoque03004e62012-09-06 01:12:28 -0500374
akmhoque53f64222012-09-05 13:57:51 -0500375enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
376 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500377{
378
akmhoqueffacaa82012-09-13 17:48:30 -0500379 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500380
akmhoque53f64222012-09-05 13:57:51 -0500381 switch(kind) {
382 case CCN_UPCALL_FINAL:
383 break;
384 case CCN_UPCALL_CONTENT:
akmhoque7b791452012-10-30 11:24:56 -0500385 if ( nlsr->debugging )
386 printf("Content Received for Name: ");
387 if ( nlsr->detailed_logging )
388 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Content Received for Name: ");
389
akmhoque03004e62012-09-06 01:12:28 -0500390 struct ccn_charbuf*c;
391 c=ccn_charbuf_create();
392 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500393 if ( nlsr->debugging )
394 printf("%s\n",ccn_charbuf_as_string(c));
395 if ( nlsr->detailed_logging )
396 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
397
akmhoque03004e62012-09-06 01:12:28 -0500398 ccn_charbuf_destroy(&c);
399
400 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500401
akmhoque53f64222012-09-05 13:57:51 -0500402 break;
403 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoqueedb68d92013-03-05 10:18:16 -0600404 //printf("Interest Timed Out Received for Name: ");
405 if ( nlsr->debugging )
406 printf("Interest Timed Out Received for Name: ");
407 if ( nlsr->detailed_logging )
408 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed Out Receiv"
akmhoque3d319d42013-02-20 11:08:32 -0600409 "ed for Name: ");
akmhoque03004e62012-09-06 01:12:28 -0500410
akmhoqueedb68d92013-03-05 10:18:16 -0600411 struct ccn_charbuf*ito;
412 ito=ccn_charbuf_create();
413 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
414 if ( nlsr->debugging )
415 printf("%s\n",ccn_charbuf_as_string(ito));
416 if ( nlsr->detailed_logging )
417 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(ito));
418 ccn_charbuf_destroy(&ito);
akmhoque03004e62012-09-06 01:12:28 -0500419
akmhoqueedb68d92013-03-05 10:18:16 -0600420 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500421
akmhoque53f64222012-09-05 13:57:51 -0500422 break;
akmhoqueedb68d92013-03-05 10:18:16 -0600423
424 case CCN_UPCALL_CONTENT_UNVERIFIED:
425 if ( nlsr->debugging )
426 printf("Unverified Content Received ..Waiting for verification\n");
427 if ( nlsr->detailed_logging )
428 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unverified Content"
429 " Received ..Waiting for verification\n");
akmhoque6e2ba842013-03-05 19:35:26 -0600430 //return CCN_UPCALL_RESULT_VERIFY;
akmhoque4637d9d2013-03-08 06:52:00 -0600431 process_incoming_content(selfp,info);
akmhoqueedb68d92013-03-05 10:18:16 -0600432 break;
433
akmhoque53f64222012-09-05 13:57:51 -0500434 default:
435 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoqueedb68d92013-03-05 10:18:16 -0600436 if ( nlsr->debugging )
437 printf("Unexpected response of kind %d\n", kind);
438 if ( nlsr->detailed_logging )
439 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unexpected response of "
akmhoque3d319d42013-02-20 11:08:32 -0600440 "kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500441 break;
akmhoque53f64222012-09-05 13:57:51 -0500442 }
akmhoqueffacaa82012-09-13 17:48:30 -0500443
444 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500445
akmhoque53f64222012-09-05 13:57:51 -0500446 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500447}
448
akmhoque7ab49a32013-02-20 11:27:51 -0600449/**
450* process any incoming content to NLSR from ccnd
451*/
akmhoque03004e62012-09-06 01:12:28 -0500452
akmhoqued79438d2012-08-27 13:31:42 -0500453void
akmhoque53f64222012-09-05 13:57:51 -0500454process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500455{
akmhoque7b791452012-10-30 11:24:56 -0500456 if ( nlsr->debugging )
457 printf("process_incoming_content called \n");
458 if ( nlsr->detailed_logging )
459 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500460
461 const unsigned char *comp_ptr1;
462 size_t comp_size;
463 int res,i;
464 int nlsr_position=0;
465 int name_comps=(int)info->interest_comps->n;
466
467 for(i=0;i<name_comps;i++)
468 {
469 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
470 if( res == 0)
471 {
472 nlsr_position=i;
473 break;
474 }
475 }
476
akmhoque3d319d42013-02-20 11:08:32 -0600477 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,
478 nlsr_position+1,&comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500479
akmhoque53f64222012-09-05 13:57:51 -0500480
481 if(!strcmp((char *)comp_ptr1,"info"))
482 {
483 process_incoming_content_info(selfp,info);
484 }
akmhoque03004e62012-09-06 01:12:28 -0500485
akmhoque53f64222012-09-05 13:57:51 -0500486}
487
akmhoque7ab49a32013-02-20 11:27:51 -0600488/**
489* process any incoming "info" content to NLSR from ccnd
490*/
akmhoque03004e62012-09-06 01:12:28 -0500491
akmhoque53f64222012-09-05 13:57:51 -0500492void
akmhoque3d319d42013-02-20 11:08:32 -0600493process_incoming_content_info(struct ccn_closure *selfp,
494 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500495{
akmhoque7b791452012-10-30 11:24:56 -0500496 if ( nlsr->debugging )
497 printf("process_incoming_content_info called \n");
498 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600499 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_info"
500 " called \n");
akmhoque03004e62012-09-06 01:12:28 -0500501
akmhoque53f64222012-09-05 13:57:51 -0500502 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
503 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500504
akmhoque7b791452012-10-30 11:24:56 -0500505 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600506 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,
507 nbr->length);
akmhoque7b791452012-10-30 11:24:56 -0500508 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600509 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Content Received For Nei"
510 "ghbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500511
akmhoquefc7016f2013-03-08 07:17:14 -0600512
akmhoque53f64222012-09-05 13:57:51 -0500513
akmhoque6e2ba842013-03-05 19:35:26 -0600514 if ( contain_key_name(info->content_ccnb, info->pco) == 1){
akmhoque6e2ba842013-03-05 19:35:26 -0600515
akmhoqueb7958182013-03-11 12:03:54 -0500516 int res_verify=verify_key(info->content_ccnb,info->pco,0);
akmhoque03004e62012-09-06 01:12:28 -0500517
akmhoqueb7958182013-03-11 12:03:54 -0500518 if ( res_verify != 0 ){
519 printf("Error in verfiying keys !! :( \n");
520 }
521 else{
522 printf("Key verification is successful :)\n");
523 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 */
akmhoque6e2ba842013-03-05 19:35:26 -0600573
akmhoque53f64222012-09-05 13:57:51 -0500574 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500575
akmhoque53f64222012-09-05 13:57:51 -0500576
577}
578
akmhoque7ab49a32013-02-20 11:27:51 -0600579/**
580* process any incoming interest timed out content to NLSR from ccnd
581*/
akmhoque53f64222012-09-05 13:57:51 -0500582
akmhoque03004e62012-09-06 01:12:28 -0500583
akmhoque53f64222012-09-05 13:57:51 -0500584void
akmhoque3d319d42013-02-20 11:08:32 -0600585process_incoming_timed_out_interest(struct ccn_closure* selfp,
586 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500587{
akmhoque3171d652012-11-13 11:44:33 -0600588
589
590 if ( nlsr->debugging )
591 printf("process_incoming_timed_out_interest called \n");
592 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600593 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
594 "erest called \n");
akmhoque3171d652012-11-13 11:44:33 -0600595
akmhoque53f64222012-09-05 13:57:51 -0500596 int res,i;
597 int nlsr_position=0;
598 int name_comps=(int)info->interest_comps->n;
599
600 for(i=0;i<name_comps;i++)
601 {
602 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
603 if( res == 0)
604 {
605 nlsr_position=i;
606 break;
607 }
608 }
609
akmhoque3d319d42013-02-20 11:08:32 -0600610 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,
611 nlsr_position+1,"info") == 0)
akmhoque53f64222012-09-05 13:57:51 -0500612 {
613 process_incoming_timed_out_interest_info(selfp,info);
614 }
615}
616
akmhoque7ab49a32013-02-20 11:27:51 -0600617/**
618* process any incoming "info" interest timed out content to NLSR from ccnd
619*/
620
akmhoque53f64222012-09-05 13:57:51 -0500621void
akmhoque3d319d42013-02-20 11:08:32 -0600622process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct
623 ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500624{
akmhoque3171d652012-11-13 11:44:33 -0600625
626 if ( nlsr->debugging )
627 printf("process_incoming_timed_out_interest_info called \n");
628 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600629 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
630 "erest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500631
632 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
633 get_nbr(nbr,selfp,info);
634
akmhoque3171d652012-11-13 11:44:33 -0600635 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600636 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",
637 nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600638 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600639 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for"
640 " Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600641
akmhoque03004e62012-09-06 01:12:28 -0500642
643
akmhoque53f64222012-09-05 13:57:51 -0500644 update_adjacent_timed_out_to_adl(nbr,1);
645 print_adjacent_from_adl();
646 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500647
akmhoque3171d652012-11-13 11:44:33 -0600648 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600649 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,
650 timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600651 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600652 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest "
653 "Timed Out: %d times\n",nbr->name,timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600654
akmhoque53f64222012-09-05 13:57:51 -0500655
656 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
657 {
akmhoque53f64222012-09-05 13:57:51 -0500658 send_info_interest_to_neighbor(nbr);
659 }
660 else
akmhoque3171d652012-11-13 11:44:33 -0600661 {
akmhoque53f64222012-09-05 13:57:51 -0500662 update_adjacent_status_to_adl(nbr,NBR_DOWN);
663 if(!nlsr->is_build_adj_lsa_sheduled)
664 {
akmhoque3d319d42013-02-20 11:08:32 -0600665 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000,
666 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500667 nlsr->is_build_adj_lsa_sheduled=1;
668 }
669 }
670
671 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500672
akmhoque53f64222012-09-05 13:57:51 -0500673
674}
675
akmhoque7ab49a32013-02-20 11:27:51 -0600676/**
677* send "info" interest to each and every neighbor in ADL and also schedule for
678* itself for periodical sending of "info" interest
679*/
akmhoque29c1db52012-09-07 14:47:43 -0500680
akmhoque03004e62012-09-06 01:12:28 -0500681int
akmhoque3d319d42013-02-20 11:08:32 -0600682send_info_interest(struct ccn_schedule *sched, void *clienth,
683 struct ccn_scheduled_event *ev, int flags)
akmhoque53f64222012-09-05 13:57:51 -0500684{
akmhoqueffacaa82012-09-13 17:48:30 -0500685 if(flags == CCN_SCHEDULE_CANCEL)
686 {
687 return -1;
688 }
689
690 nlsr_lock();
691
akmhoque3171d652012-11-13 11:44:33 -0600692 if ( nlsr->debugging )
693 printf("send_info_interest called \n");
694 if ( nlsr->detailed_logging )
695 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
696
697 if ( nlsr->debugging )
698 printf("\n");
699 if ( nlsr->detailed_logging )
700 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoqued79438d2012-08-27 13:31:42 -0500701
akmhoque53f64222012-09-05 13:57:51 -0500702 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500703 struct ndn_neighbor *nbr;
704
705 struct hashtb_enumerator ee;
706 struct hashtb_enumerator *e = &ee;
707
708 hashtb_start(nlsr->adl, e);
709 adl_element=hashtb_n(nlsr->adl);
710
711 for(i=0;i<adl_element;i++)
712 {
713 nbr=e->data;
714 send_info_interest_to_neighbor(nbr->neighbor);
715 hashtb_next(e);
716 }
akmhoque53f64222012-09-05 13:57:51 -0500717 hashtb_end(e);
718
akmhoqueffacaa82012-09-13 17:48:30 -0500719 nlsr_unlock();
720
akmhoque3d319d42013-02-20 11:08:32 -0600721 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &send_info_interest,
722 NULL, 0);
akmhoque9fa58a82012-10-05 07:56:02 -0500723
akmhoque53f64222012-09-05 13:57:51 -0500724 return 0;
725}
726
akmhoque7ab49a32013-02-20 11:27:51 -0600727
728/**
729* send "info" interest neighbor nbr
730*
731*/
732
akmhoque53f64222012-09-05 13:57:51 -0500733void
734send_info_interest_to_neighbor(struct name_prefix *nbr)
735{
akmhoque3171d652012-11-13 11:44:33 -0600736
737 if ( nlsr->debugging )
738 printf("send_info_interest_to_neighbor called \n");
739 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600740 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor"
741 " called \n");
akmhoque3171d652012-11-13 11:44:33 -0600742
akmhoqued79438d2012-08-27 13:31:42 -0500743
744 int res;
akmhoque53f64222012-09-05 13:57:51 -0500745 char info_str[5];
746 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500747
akmhoqued79438d2012-08-27 13:31:42 -0500748 memset(&nlsr_str,0,5);
749 sprintf(nlsr_str,"nlsr");
750 memset(&info_str,0,5);
751 sprintf(info_str,"info");
752
akmhoque53f64222012-09-05 13:57:51 -0500753
754 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500755 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500756
akmhoque3d319d42013-02-20 11:08:32 -0600757 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+
758 strlen(info_str)+strlen(nlsr->router_name)+1);
759 memset(int_name,0,strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+
760 strlen(nlsr->router_name)+1);
akmhoque53f64222012-09-05 13:57:51 -0500761 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
762 memcpy(int_name+strlen(int_name),"/",1);
763 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
764 memcpy(int_name+strlen(int_name),"/",1);
765 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500766 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500767
768
769 res=ccn_name_from_uri(name,int_name);
770 if ( res >=0 )
771 {
akmhoque53f64222012-09-05 13:57:51 -0500772 /* adding InterestLifeTime and InterestScope filter */
773
774 struct ccn_charbuf *templ;
775 templ = ccn_charbuf_create();
776
777 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
778 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
779 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500780 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
781 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
akmhoqueb77b95f2013-02-08 12:28:47 -0600782 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque3d319d42013-02-20 11:08:32 -0600783 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2
784 //(not further than next host)
akmhoque03004e62012-09-06 01:12:28 -0500785 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500786
787 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqueb77b95f2013-02-08 12:28:47 -0600788 unsigned int face_id=get_next_hop_face_from_adl(nbr->name);
789 ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face_id);
akmhoque53f64222012-09-05 13:57:51 -0500790 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoqueb77b95f2013-02-08 12:28:47 -0600791
akmhoque53f64222012-09-05 13:57:51 -0500792
akmhoque3171d652012-11-13 11:44:33 -0600793 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600794 printf("Sending info interest on name prefix : %s through Face:%u\n"
795 ,int_name,face_id);
akmhoque3171d652012-11-13 11:44:33 -0600796 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600797 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info interest on"
798 "name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque53f64222012-09-05 13:57:51 -0500799
800 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
801
802 if ( res >= 0 )
akmhoque3171d652012-11-13 11:44:33 -0600803 {
804 if ( nlsr->debugging )
805 printf("Info interest sending Successfull .... \n");
806 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600807 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info"
808 "interest sending Successfull .... \n");
akmhoque3171d652012-11-13 11:44:33 -0600809 }
akmhoque53f64222012-09-05 13:57:51 -0500810 ccn_charbuf_destroy(&templ);
811 }
akmhoque7adb2772013-03-05 16:30:59 -0600812
akmhoque53f64222012-09-05 13:57:51 -0500813 ccn_charbuf_destroy(&name);
814 free(int_name);
akmhoquec06dcf12013-02-20 08:13:37 -0600815}