blob: e4b1378cad8725f95d2a78454f18386fb98b6611 [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;
272 struct ccn_charbuf *data=ccn_charbuf_create();
273 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
akmhoquec06dcf12013-02-20 08:13:37 -0600281
282 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);
301 sprintf(raw_data,"%s", nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500302
akmhoqueb63a41e2013-03-01 12:00:20 -0600303 res= ccn_sign_content(nlsr->ccn, data, name, &sp, "info",strlen("info"));
akmhoque53f64222012-09-05 13:57:51 -0500304 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500305 {
306 if ( nlsr->debugging )
307 printf("Signing info Content is successful \n");
308 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600309 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Signing info Content"
310 " is successful \n");
akmhoque53f64222012-09-05 13:57:51 -0500311
akmhoque7b791452012-10-30 11:24:56 -0500312 }
akmhoque53f64222012-09-05 13:57:51 -0500313 res=ccn_put(nlsr->ccn,data->buf,data->length);
314 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500315 {
316 if ( nlsr->debugging )
317 printf("Sending Info Content is successful \n");
318 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600319 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info Content"
320 " is successful \n");
akmhoque7b791452012-10-30 11:24:56 -0500321 }
322
akmhoque53f64222012-09-05 13:57:51 -0500323
324
akmhoquee1dd7772013-02-24 14:21:49 -0600325 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix));
akmhoque03004e62012-09-06 01:12:28 -0500326 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque7b791452012-10-30 11:24:56 -0500327
328 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600329 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,
330 get_adjacent_status(nbr));
akmhoque7b791452012-10-30 11:24:56 -0500331 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600332 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor : %s Length : %d"
333 " Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500334
akmhoque53f64222012-09-05 13:57:51 -0500335
akmhoque3d319d42013-02-20 11:08:32 -0600336 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr) >=
337 nlsr->interest_retry )
akmhoque53f64222012-09-05 13:57:51 -0500338 {
akmhoqueb8195202012-09-25 11:53:23 -0500339 update_adjacent_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500340 send_info_interest_to_neighbor(nbr);
341 }
342
343 free(nbr);
344 free(raw_data);
345 ccn_charbuf_destroy(&sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600346 ccn_charbuf_destroy(&pubid);
347 ccn_charbuf_destroy(&pubkey);
akmhoque53f64222012-09-05 13:57:51 -0500348 }
akmhoque03004e62012-09-06 01:12:28 -0500349
akmhoque53f64222012-09-05 13:57:51 -0500350 ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500351 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500352
akmhoquebf1aa832012-08-13 13:26:59 -0500353}
354
355
akmhoque7ab49a32013-02-20 11:27:51 -0600356/**
357* Call back function registered in ccnd to get all content coming to NLSR
358* application
359*/
akmhoque03004e62012-09-06 01:12:28 -0500360
akmhoque53f64222012-09-05 13:57:51 -0500361enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
362 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500363{
364
akmhoqueffacaa82012-09-13 17:48:30 -0500365 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500366
akmhoque53f64222012-09-05 13:57:51 -0500367 switch(kind) {
368 case CCN_UPCALL_FINAL:
369 break;
370 case CCN_UPCALL_CONTENT:
akmhoque7b791452012-10-30 11:24:56 -0500371 if ( nlsr->debugging )
372 printf("Content Received for Name: ");
373 if ( nlsr->detailed_logging )
374 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Content Received for Name: ");
375
akmhoque03004e62012-09-06 01:12:28 -0500376 struct ccn_charbuf*c;
377 c=ccn_charbuf_create();
378 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500379 if ( nlsr->debugging )
380 printf("%s\n",ccn_charbuf_as_string(c));
381 if ( nlsr->detailed_logging )
382 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
383
akmhoque03004e62012-09-06 01:12:28 -0500384 ccn_charbuf_destroy(&c);
385
386 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500387
akmhoque53f64222012-09-05 13:57:51 -0500388 break;
389 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoqueedb68d92013-03-05 10:18:16 -0600390 //printf("Interest Timed Out Received for Name: ");
391 if ( nlsr->debugging )
392 printf("Interest Timed Out Received for Name: ");
393 if ( nlsr->detailed_logging )
394 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed Out Receiv"
akmhoque3d319d42013-02-20 11:08:32 -0600395 "ed for Name: ");
akmhoque03004e62012-09-06 01:12:28 -0500396
akmhoqueedb68d92013-03-05 10:18:16 -0600397 struct ccn_charbuf*ito;
398 ito=ccn_charbuf_create();
399 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
400 if ( nlsr->debugging )
401 printf("%s\n",ccn_charbuf_as_string(ito));
402 if ( nlsr->detailed_logging )
403 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(ito));
404 ccn_charbuf_destroy(&ito);
akmhoque03004e62012-09-06 01:12:28 -0500405
akmhoqueedb68d92013-03-05 10:18:16 -0600406 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500407
akmhoque53f64222012-09-05 13:57:51 -0500408 break;
akmhoqueedb68d92013-03-05 10:18:16 -0600409
410 case CCN_UPCALL_CONTENT_UNVERIFIED:
411 if ( nlsr->debugging )
412 printf("Unverified Content Received ..Waiting for verification\n");
413 if ( nlsr->detailed_logging )
414 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unverified Content"
415 " Received ..Waiting for verification\n");
416 return CCN_UPCALL_RESULT_VERIFY;
417 break;
418
akmhoque53f64222012-09-05 13:57:51 -0500419 default:
420 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoqueedb68d92013-03-05 10:18:16 -0600421 if ( nlsr->debugging )
422 printf("Unexpected response of kind %d\n", kind);
423 if ( nlsr->detailed_logging )
424 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unexpected response of "
akmhoque3d319d42013-02-20 11:08:32 -0600425 "kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500426 break;
akmhoque53f64222012-09-05 13:57:51 -0500427 }
akmhoqueffacaa82012-09-13 17:48:30 -0500428
429 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500430
akmhoque53f64222012-09-05 13:57:51 -0500431 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500432}
433
akmhoque7ab49a32013-02-20 11:27:51 -0600434/**
435* process any incoming content to NLSR from ccnd
436*/
akmhoque03004e62012-09-06 01:12:28 -0500437
akmhoqued79438d2012-08-27 13:31:42 -0500438void
akmhoque53f64222012-09-05 13:57:51 -0500439process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500440{
akmhoque7b791452012-10-30 11:24:56 -0500441 if ( nlsr->debugging )
442 printf("process_incoming_content called \n");
443 if ( nlsr->detailed_logging )
444 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500445
446 const unsigned char *comp_ptr1;
447 size_t comp_size;
448 int res,i;
449 int nlsr_position=0;
450 int name_comps=(int)info->interest_comps->n;
451
452 for(i=0;i<name_comps;i++)
453 {
454 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
455 if( res == 0)
456 {
457 nlsr_position=i;
458 break;
459 }
460 }
461
akmhoque3d319d42013-02-20 11:08:32 -0600462 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,
463 nlsr_position+1,&comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500464
akmhoque53f64222012-09-05 13:57:51 -0500465
466 if(!strcmp((char *)comp_ptr1,"info"))
467 {
468 process_incoming_content_info(selfp,info);
469 }
akmhoque03004e62012-09-06 01:12:28 -0500470
akmhoque53f64222012-09-05 13:57:51 -0500471}
472
akmhoque7ab49a32013-02-20 11:27:51 -0600473/**
474* process any incoming "info" content to NLSR from ccnd
475*/
akmhoque03004e62012-09-06 01:12:28 -0500476
akmhoque53f64222012-09-05 13:57:51 -0500477void
akmhoque3d319d42013-02-20 11:08:32 -0600478process_incoming_content_info(struct ccn_closure *selfp,
479 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500480{
akmhoque7b791452012-10-30 11:24:56 -0500481 if ( nlsr->debugging )
482 printf("process_incoming_content_info called \n");
483 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600484 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_info"
485 " called \n");
akmhoque03004e62012-09-06 01:12:28 -0500486
akmhoque53f64222012-09-05 13:57:51 -0500487 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
488 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500489
akmhoque7b791452012-10-30 11:24:56 -0500490 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600491 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,
492 nbr->length);
akmhoque7b791452012-10-30 11:24:56 -0500493 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600494 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Content Received For Nei"
495 "ghbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500496
akmhoque53f64222012-09-05 13:57:51 -0500497
akmhoque03004e62012-09-06 01:12:28 -0500498
akmhoque53f64222012-09-05 13:57:51 -0500499 update_adjacent_timed_out_zero_to_adl(nbr);
500 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
akmhoque53f64222012-09-05 13:57:51 -0500501 print_adjacent_from_adl();
502
akmhoque03004e62012-09-06 01:12:28 -0500503
504
akmhoque53f64222012-09-05 13:57:51 -0500505 if(!nlsr->is_build_adj_lsa_sheduled)
506 {
akmhoque7b791452012-10-30 11:24:56 -0500507 if ( nlsr->debugging )
508 printf("Scheduling Build and Install Adj LSA...\n");
509 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600510 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Scheduling Build and Inst"
511 "all Adj LSA...\n");
512 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 100000,
513 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500514 nlsr->is_build_adj_lsa_sheduled=1;
515 }
516 else
517 {
akmhoque7b791452012-10-30 11:24:56 -0500518 if ( nlsr->debugging )
519 printf("Build and Install Adj LSA already scheduled\n");
520 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600521 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Build and Install Adj LSA"
522 " already scheduled\n");
akmhoque53f64222012-09-05 13:57:51 -0500523 }
524
akmhoque03004e62012-09-06 01:12:28 -0500525
akmhoque53f64222012-09-05 13:57:51 -0500526 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500527
akmhoque53f64222012-09-05 13:57:51 -0500528
529}
530
akmhoque7ab49a32013-02-20 11:27:51 -0600531/**
532* process any incoming interest timed out content to NLSR from ccnd
533*/
akmhoque53f64222012-09-05 13:57:51 -0500534
akmhoque03004e62012-09-06 01:12:28 -0500535
akmhoque53f64222012-09-05 13:57:51 -0500536void
akmhoque3d319d42013-02-20 11:08:32 -0600537process_incoming_timed_out_interest(struct ccn_closure* selfp,
538 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500539{
akmhoque3171d652012-11-13 11:44:33 -0600540
541
542 if ( nlsr->debugging )
543 printf("process_incoming_timed_out_interest called \n");
544 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600545 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
546 "erest called \n");
akmhoque3171d652012-11-13 11:44:33 -0600547
akmhoque53f64222012-09-05 13:57:51 -0500548 int res,i;
549 int nlsr_position=0;
550 int name_comps=(int)info->interest_comps->n;
551
552 for(i=0;i<name_comps;i++)
553 {
554 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
555 if( res == 0)
556 {
557 nlsr_position=i;
558 break;
559 }
560 }
561
akmhoque3d319d42013-02-20 11:08:32 -0600562 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,
563 nlsr_position+1,"info") == 0)
akmhoque53f64222012-09-05 13:57:51 -0500564 {
565 process_incoming_timed_out_interest_info(selfp,info);
566 }
567}
568
akmhoque7ab49a32013-02-20 11:27:51 -0600569/**
570* process any incoming "info" interest timed out content to NLSR from ccnd
571*/
572
akmhoque53f64222012-09-05 13:57:51 -0500573void
akmhoque3d319d42013-02-20 11:08:32 -0600574process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct
575 ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500576{
akmhoque3171d652012-11-13 11:44:33 -0600577
578 if ( nlsr->debugging )
579 printf("process_incoming_timed_out_interest_info called \n");
580 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600581 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
582 "erest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500583
584 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
585 get_nbr(nbr,selfp,info);
586
akmhoque3171d652012-11-13 11:44:33 -0600587 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600588 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",
589 nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600590 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600591 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for"
592 " Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600593
akmhoque03004e62012-09-06 01:12:28 -0500594
595
akmhoque53f64222012-09-05 13:57:51 -0500596 update_adjacent_timed_out_to_adl(nbr,1);
597 print_adjacent_from_adl();
598 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500599
akmhoque3171d652012-11-13 11:44:33 -0600600 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600601 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,
602 timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600603 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600604 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest "
605 "Timed Out: %d times\n",nbr->name,timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600606
akmhoque53f64222012-09-05 13:57:51 -0500607
608 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
609 {
akmhoque53f64222012-09-05 13:57:51 -0500610 send_info_interest_to_neighbor(nbr);
611 }
612 else
akmhoque3171d652012-11-13 11:44:33 -0600613 {
akmhoque53f64222012-09-05 13:57:51 -0500614 update_adjacent_status_to_adl(nbr,NBR_DOWN);
615 if(!nlsr->is_build_adj_lsa_sheduled)
616 {
akmhoque3d319d42013-02-20 11:08:32 -0600617 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000,
618 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500619 nlsr->is_build_adj_lsa_sheduled=1;
620 }
621 }
622
623 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500624
akmhoque53f64222012-09-05 13:57:51 -0500625
626}
627
akmhoque7ab49a32013-02-20 11:27:51 -0600628/**
629* send "info" interest to each and every neighbor in ADL and also schedule for
630* itself for periodical sending of "info" interest
631*/
akmhoque29c1db52012-09-07 14:47:43 -0500632
akmhoque03004e62012-09-06 01:12:28 -0500633int
akmhoque3d319d42013-02-20 11:08:32 -0600634send_info_interest(struct ccn_schedule *sched, void *clienth,
635 struct ccn_scheduled_event *ev, int flags)
akmhoque53f64222012-09-05 13:57:51 -0500636{
akmhoqueffacaa82012-09-13 17:48:30 -0500637 if(flags == CCN_SCHEDULE_CANCEL)
638 {
639 return -1;
640 }
641
642 nlsr_lock();
643
akmhoque3171d652012-11-13 11:44:33 -0600644 if ( nlsr->debugging )
645 printf("send_info_interest called \n");
646 if ( nlsr->detailed_logging )
647 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
648
649 if ( nlsr->debugging )
650 printf("\n");
651 if ( nlsr->detailed_logging )
652 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoqued79438d2012-08-27 13:31:42 -0500653
akmhoque53f64222012-09-05 13:57:51 -0500654 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500655 struct ndn_neighbor *nbr;
656
657 struct hashtb_enumerator ee;
658 struct hashtb_enumerator *e = &ee;
659
660 hashtb_start(nlsr->adl, e);
661 adl_element=hashtb_n(nlsr->adl);
662
663 for(i=0;i<adl_element;i++)
664 {
665 nbr=e->data;
666 send_info_interest_to_neighbor(nbr->neighbor);
667 hashtb_next(e);
668 }
akmhoque53f64222012-09-05 13:57:51 -0500669 hashtb_end(e);
670
akmhoqueffacaa82012-09-13 17:48:30 -0500671 nlsr_unlock();
672
akmhoque3d319d42013-02-20 11:08:32 -0600673 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &send_info_interest,
674 NULL, 0);
akmhoque9fa58a82012-10-05 07:56:02 -0500675
akmhoque53f64222012-09-05 13:57:51 -0500676 return 0;
677}
678
akmhoque7ab49a32013-02-20 11:27:51 -0600679
680/**
681* send "info" interest neighbor nbr
682*
683*/
684
akmhoque53f64222012-09-05 13:57:51 -0500685void
686send_info_interest_to_neighbor(struct name_prefix *nbr)
687{
akmhoque3171d652012-11-13 11:44:33 -0600688
689 if ( nlsr->debugging )
690 printf("send_info_interest_to_neighbor called \n");
691 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600692 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor"
693 " called \n");
akmhoque3171d652012-11-13 11:44:33 -0600694
akmhoqued79438d2012-08-27 13:31:42 -0500695
696 int res;
akmhoque53f64222012-09-05 13:57:51 -0500697 char info_str[5];
698 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500699
akmhoqued79438d2012-08-27 13:31:42 -0500700 memset(&nlsr_str,0,5);
701 sprintf(nlsr_str,"nlsr");
702 memset(&info_str,0,5);
703 sprintf(info_str,"info");
704
akmhoque53f64222012-09-05 13:57:51 -0500705
706 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500707 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500708
akmhoque3d319d42013-02-20 11:08:32 -0600709 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+
710 strlen(info_str)+strlen(nlsr->router_name)+1);
711 memset(int_name,0,strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+
712 strlen(nlsr->router_name)+1);
akmhoque53f64222012-09-05 13:57:51 -0500713 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
714 memcpy(int_name+strlen(int_name),"/",1);
715 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
716 memcpy(int_name+strlen(int_name),"/",1);
717 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500718 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500719
720
721 res=ccn_name_from_uri(name,int_name);
722 if ( res >=0 )
723 {
akmhoque53f64222012-09-05 13:57:51 -0500724 /* adding InterestLifeTime and InterestScope filter */
725
726 struct ccn_charbuf *templ;
727 templ = ccn_charbuf_create();
728
729 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
730 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
731 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500732 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
733 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
akmhoqueb77b95f2013-02-08 12:28:47 -0600734 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque3d319d42013-02-20 11:08:32 -0600735 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2
736 //(not further than next host)
akmhoque03004e62012-09-06 01:12:28 -0500737 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500738
739 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqueb77b95f2013-02-08 12:28:47 -0600740 unsigned int face_id=get_next_hop_face_from_adl(nbr->name);
741 ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face_id);
akmhoque53f64222012-09-05 13:57:51 -0500742 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoqueb77b95f2013-02-08 12:28:47 -0600743
akmhoque53f64222012-09-05 13:57:51 -0500744
akmhoque3171d652012-11-13 11:44:33 -0600745 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600746 printf("Sending info interest on name prefix : %s through Face:%u\n"
747 ,int_name,face_id);
akmhoque3171d652012-11-13 11:44:33 -0600748 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600749 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info interest on"
750 "name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque53f64222012-09-05 13:57:51 -0500751
752 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
753
754 if ( res >= 0 )
akmhoque3171d652012-11-13 11:44:33 -0600755 {
756 if ( nlsr->debugging )
757 printf("Info interest sending Successfull .... \n");
758 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600759 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info"
760 "interest sending Successfull .... \n");
akmhoque3171d652012-11-13 11:44:33 -0600761 }
akmhoque53f64222012-09-05 13:57:51 -0500762 ccn_charbuf_destroy(&templ);
763 }
764 ccn_charbuf_destroy(&name);
765 free(int_name);
akmhoquec06dcf12013-02-20 08:13:37 -0600766}